meta_compile 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/meta_compile +229 -0
- metadata +46 -0
data/bin/meta_compile
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "strscan"
|
3
|
+
class RMetaII
|
4
|
+
def compile(str, out)
|
5
|
+
@i, @o = StringScanner.new(str), out
|
6
|
+
compile_program
|
7
|
+
end
|
8
|
+
def compile_arg
|
9
|
+
begin
|
10
|
+
@i.scan /\s*/; s='$'
|
11
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
12
|
+
if @f
|
13
|
+
@o.print '@o.print @t'
|
14
|
+
@o.print "\n"
|
15
|
+
end
|
16
|
+
break if @f
|
17
|
+
@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
|
18
|
+
if @f
|
19
|
+
@o.print '@o.print '
|
20
|
+
@o.print @t
|
21
|
+
@o.print "\n"
|
22
|
+
end
|
23
|
+
end while false
|
24
|
+
end
|
25
|
+
def compile_output
|
26
|
+
begin
|
27
|
+
@i.scan /\s*/; s='<'
|
28
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
29
|
+
if @f
|
30
|
+
begin
|
31
|
+
compile_arg
|
32
|
+
end while @f
|
33
|
+
@f = true
|
34
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
35
|
+
@i.scan /\s*/; s='>'
|
36
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
37
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
38
|
+
@o.print '@o.print "\n"'
|
39
|
+
@o.print "\n"
|
40
|
+
end
|
41
|
+
end while false
|
42
|
+
end
|
43
|
+
def compile_exp3
|
44
|
+
begin
|
45
|
+
@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
|
46
|
+
if @f
|
47
|
+
@o.print 'compile_'
|
48
|
+
@o.print @t
|
49
|
+
@o.print "\n"
|
50
|
+
end
|
51
|
+
break if @f
|
52
|
+
@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
|
53
|
+
if @f
|
54
|
+
@o.print '@i.scan /\s*/; s='
|
55
|
+
@o.print @t
|
56
|
+
@o.print "\n"
|
57
|
+
@o.print '@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil'
|
58
|
+
@o.print "\n"
|
59
|
+
end
|
60
|
+
break if @f
|
61
|
+
@i.scan /\s*/; s='.id'
|
62
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
63
|
+
if @f
|
64
|
+
@o.print '@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/'
|
65
|
+
@o.print "\n"
|
66
|
+
end
|
67
|
+
break if @f
|
68
|
+
@i.scan /\s*/; s='.string'
|
69
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
70
|
+
if @f
|
71
|
+
@o.print '@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/'
|
72
|
+
@o.print "\n"
|
73
|
+
end
|
74
|
+
break if @f
|
75
|
+
@i.scan /\s*/; s='('
|
76
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
77
|
+
if @f
|
78
|
+
compile_exp1
|
79
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
80
|
+
@i.scan /\s*/; s=')'
|
81
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
82
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
83
|
+
end
|
84
|
+
break if @f
|
85
|
+
@i.scan /\s*/; s='.e'
|
86
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
87
|
+
if @f
|
88
|
+
@o.print '@f = true'
|
89
|
+
@o.print "\n"
|
90
|
+
end
|
91
|
+
break if @f
|
92
|
+
@i.scan /\s*/; s='*'
|
93
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
94
|
+
if @f
|
95
|
+
@o.print 'begin'
|
96
|
+
@o.print "\n"
|
97
|
+
compile_exp3
|
98
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
99
|
+
@o.print 'end while @f'
|
100
|
+
@o.print "\n"
|
101
|
+
@o.print '@f = true'
|
102
|
+
@o.print "\n"
|
103
|
+
end
|
104
|
+
end while false
|
105
|
+
end
|
106
|
+
def compile_exp2
|
107
|
+
begin
|
108
|
+
begin
|
109
|
+
compile_exp3
|
110
|
+
if @f
|
111
|
+
@o.print 'if @f'
|
112
|
+
@o.print "\n"
|
113
|
+
end
|
114
|
+
break if @f
|
115
|
+
compile_output
|
116
|
+
if @f
|
117
|
+
@o.print 'if true'
|
118
|
+
@o.print "\n"
|
119
|
+
end
|
120
|
+
end while false
|
121
|
+
if @f
|
122
|
+
begin
|
123
|
+
begin
|
124
|
+
compile_exp3
|
125
|
+
if @f
|
126
|
+
@o.print 'raise("error at: " + @i.rest.split("\n")[0]) if !@f'
|
127
|
+
@o.print "\n"
|
128
|
+
end
|
129
|
+
break if @f
|
130
|
+
compile_output
|
131
|
+
if @f
|
132
|
+
end
|
133
|
+
end while false
|
134
|
+
end while @f
|
135
|
+
@f = true
|
136
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
137
|
+
@o.print 'end'
|
138
|
+
@o.print "\n"
|
139
|
+
end
|
140
|
+
end while false
|
141
|
+
end
|
142
|
+
def compile_exp1
|
143
|
+
begin
|
144
|
+
@o.print 'begin'
|
145
|
+
@o.print "\n"
|
146
|
+
if true
|
147
|
+
compile_exp2
|
148
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
149
|
+
begin
|
150
|
+
begin
|
151
|
+
@i.scan /\s*/; s='|'
|
152
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
153
|
+
if @f
|
154
|
+
@o.print 'break if @f'
|
155
|
+
@o.print "\n"
|
156
|
+
compile_exp2
|
157
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
158
|
+
end
|
159
|
+
end while false
|
160
|
+
end while @f
|
161
|
+
@f = true
|
162
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
163
|
+
@o.print 'end while false'
|
164
|
+
@o.print "\n"
|
165
|
+
end
|
166
|
+
end while false
|
167
|
+
end
|
168
|
+
def compile_stat
|
169
|
+
begin
|
170
|
+
@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
|
171
|
+
if @f
|
172
|
+
@o.print 'def compile_'
|
173
|
+
@o.print @t
|
174
|
+
@o.print "\n"
|
175
|
+
@i.scan /\s*/; s='='
|
176
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
177
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
178
|
+
compile_exp1
|
179
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
180
|
+
@i.scan /\s*/; s=';'
|
181
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
182
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
183
|
+
@o.print 'end'
|
184
|
+
@o.print "\n"
|
185
|
+
end
|
186
|
+
end while false
|
187
|
+
end
|
188
|
+
def compile_program
|
189
|
+
begin
|
190
|
+
@i.scan /\s*/; s='.syntax'
|
191
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
192
|
+
if @f
|
193
|
+
@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
|
194
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
195
|
+
@o.print '#!/usr/bin/env ruby'
|
196
|
+
@o.print "\n"
|
197
|
+
@o.print 'require "strscan"'
|
198
|
+
@o.print "\n"
|
199
|
+
@o.print 'class '
|
200
|
+
@o.print @t
|
201
|
+
@o.print "\n"
|
202
|
+
@o.print 'def compile(str, out)'
|
203
|
+
@o.print "\n"
|
204
|
+
@o.print '@i, @o = StringScanner.new(str), out'
|
205
|
+
@o.print "\n"
|
206
|
+
@o.print 'compile_program'
|
207
|
+
@o.print "\n"
|
208
|
+
@o.print 'end'
|
209
|
+
@o.print "\n"
|
210
|
+
begin
|
211
|
+
compile_stat
|
212
|
+
end while @f
|
213
|
+
@f = true
|
214
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
215
|
+
@i.scan /\s*/; s='.end'
|
216
|
+
@f = (@i.peek(s.length) == s) ? (@t=s; @i.pos += s.length) : nil
|
217
|
+
raise("error at: " + @i.rest.split("\n")[0]) if !@f
|
218
|
+
@o.print 'end'
|
219
|
+
@o.print "\n"
|
220
|
+
@o.print 'begin; puts "Use: meta_compile <in> <out>"; exit; end if ARGV.length != 2'
|
221
|
+
@o.print "\n"
|
222
|
+
@o.print 'File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}'
|
223
|
+
@o.print "\n"
|
224
|
+
end
|
225
|
+
end while false
|
226
|
+
end
|
227
|
+
end
|
228
|
+
begin; puts "Use: meta_compile <in> <out>"; exit; end if ARGV.length != 2
|
229
|
+
File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meta_compile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert Feldt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A meta compilation framework à la Meta-II by Val Schorre
|
15
|
+
email: robert.feldt@gmail.com
|
16
|
+
executables:
|
17
|
+
- meta_compile
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/meta_compile
|
22
|
+
homepage: http://rubygems.org/gems/meta_compile
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.24
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: meta compiler framework
|
46
|
+
test_files: []
|