depager 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README.en +5 -10
- data/bin/depager +17 -20
- data/examples/c89/c89.tab.rb +5632 -702
- data/examples/pl0d/pl0ds.dr +41 -41
- data/examples/pl0d/pl0ds.tab.rb +1887 -874
- data/examples/sample_calc/calc.action.tab.rb +243 -69
- data/examples/sample_calc/{calc.astl.action.dr → calc.ast.action.dr} +7 -7
- data/examples/sample_calc/calc.ast.action.tab.rb +755 -0
- data/examples/sample_calc/{calc.astl.dr → calc.ast.dr} +7 -7
- data/examples/sample_calc/calc.ast.tab.rb +672 -0
- data/examples/sample_calc/calc.astdf.dr +5 -5
- data/examples/sample_calc/calc.astdf.tab.rb +405 -202
- data/examples/sample_calc/calc.atree.tab.rb +243 -69
- data/examples/sample_calc/calc.cst.tab.rb +275 -109
- data/examples/sample_calc/calc.lex.tab.rb +210 -28
- data/examples/sample_calc/calc.nvaction.tab.rb +251 -77
- data/examples/sample_calc/calc.tab.rb +210 -28
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +224 -50
- data/examples/slex_test/divreg.slex.tab.rb +97 -21
- data/examples/slex_test/ljoin.slex.tab.rb +128 -35
- data/lib/depager.rb +77 -44
- data/lib/depager/{ast_base.dr → ast.dr} +56 -18
- data/lib/depager/{ast_base.rb → ast.rb} +432 -424
- data/lib/depager/astdf.rb +3 -6
- data/lib/depager/atree.rb +54 -62
- data/lib/depager/cst.dr +2 -2
- data/lib/depager/cst.rb +64 -77
- data/lib/depager/grammar.rb +225 -66
- data/lib/depager/lex.dr +1 -1
- data/lib/depager/lex.rb +45 -54
- data/lib/depager/lr.rb +181 -262
- data/lib/depager/lr_put_table.rb +116 -0
- data/lib/depager/nvaction.rb +1 -1
- data/lib/depager/parser.rb +23 -2
- data/lib/depager/slex.dr +3 -3
- data/lib/depager/slex.rb +148 -169
- data/lib/depager/srp.rb +1 -1
- data/lib/depager/template/ast.erbs +69 -0
- data/lib/depager/template/extension_lalr_master.erb +3 -3
- data/lib/depager/template/extension_lalr_slave.erb +7 -7
- data/lib/depager/template/simple.erb +4 -2
- data/lib/depager/template/single_lalr_parser.erb +30 -10
- data/lib/depager/utils.rb +10 -9
- data/lib/depager/version.rb +2 -8
- metadata +10 -11
- data/examples/sample_calc/calc.astl.action.tab.rb +0 -593
- data/examples/sample_calc/calc.astl.tab.rb +0 -501
- data/lib/depager/astl.rb +0 -14
- data/lib/depager/template/astdf.erbs +0 -57
- data/lib/depager/template/astl.erbs +0 -57
@@ -1,7 +1,7 @@
|
|
1
1
|
%class TinyCalc
|
2
2
|
%extend Lexer ('depager/lex.rb')
|
3
|
-
%extend
|
4
|
-
%decorate @
|
3
|
+
%extend ASTBuilder ('depager/ast.rb')
|
4
|
+
%decorate @ASTBuilder
|
5
5
|
#%decorate ShiftReducePrinter ('depager/srp.rb')
|
6
6
|
%%
|
7
7
|
|
@@ -14,11 +14,11 @@
|
|
14
14
|
%AST{
|
15
15
|
Node [value] { @value = nil }
|
16
16
|
Visitor { }
|
17
|
-
add(left, right) {
|
18
|
-
sub(left, right) {
|
19
|
-
mul(left, right) {
|
20
|
-
div(left, right) {
|
21
|
-
literal(-n) {
|
17
|
+
add(left, right) { $.value = visit($.left).value + visit($.right).value }
|
18
|
+
sub(left, right) { $.value = visit($.left).value - visit($.right).value }
|
19
|
+
mul(left, right) { $.value = visit($.left).value * visit($.right).value }
|
20
|
+
div(left, right) { $.value = visit($.left).value / visit($.right).value }
|
21
|
+
literal(-n) { $.value = $.n.value }
|
22
22
|
%}
|
23
23
|
|
24
24
|
#begin-rule
|
@@ -0,0 +1,672 @@
|
|
1
|
+
|
2
|
+
begin; require 'rubygems'; rescue Exception; end
|
3
|
+
require 'depager/parser.rb'
|
4
|
+
|
5
|
+
|
6
|
+
module Depager::DecoratorUtils; end
|
7
|
+
module D4TinyCalc; end
|
8
|
+
|
9
|
+
class TinyCalc < Depager::LALR::Basis
|
10
|
+
|
11
|
+
|
12
|
+
### Reduce Table
|
13
|
+
reduce_table = [
|
14
|
+
[ -1, 1 ], # ( 0) $start : expr
|
15
|
+
[ 0, 3 ], # ( 1) expr : expr '+' term
|
16
|
+
[ 0, 3 ], # ( 2) expr : expr '-' term
|
17
|
+
[ 0, 1 ], # ( 3) expr : term
|
18
|
+
[ 1, 3 ], # ( 4) term : term '*' fact
|
19
|
+
[ 1, 3 ], # ( 5) term : term '/' fact
|
20
|
+
[ 1, 1 ], # ( 6) term : fact
|
21
|
+
[ 2, 1 ], # ( 7) fact : NUM
|
22
|
+
[ 2, 3 ], # ( 8) fact : '(' expr ')'
|
23
|
+
]
|
24
|
+
### Extension Params
|
25
|
+
nparams = {
|
26
|
+
'ASTBuilder' => 2,
|
27
|
+
}
|
28
|
+
### Term to Int
|
29
|
+
t2i = {
|
30
|
+
nil => 0,
|
31
|
+
false => 1,
|
32
|
+
"+" => 2,
|
33
|
+
"-" => 3,
|
34
|
+
"*" => 4,
|
35
|
+
"/" => 5,
|
36
|
+
:NUM => 6,
|
37
|
+
"(" => 7,
|
38
|
+
")" => 8,
|
39
|
+
}
|
40
|
+
### Int to Term
|
41
|
+
i2t = [
|
42
|
+
nil,
|
43
|
+
false,
|
44
|
+
"+",
|
45
|
+
"-",
|
46
|
+
"*",
|
47
|
+
"/",
|
48
|
+
:NUM,
|
49
|
+
"(",
|
50
|
+
")",
|
51
|
+
]
|
52
|
+
### Action Table
|
53
|
+
action_table = [
|
54
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
55
|
+
[ ACC, nil, 6, 7, nil, nil, nil, nil, nil, ],
|
56
|
+
[ nil, nil, nil, nil, 8, 9, nil, nil, nil, ],
|
57
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
58
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
59
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
60
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
61
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
62
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
63
|
+
[ nil, nil, nil, nil, nil, nil, 4, 5, nil, ],
|
64
|
+
[ nil, nil, 6, 7, nil, nil, nil, nil, 15, ],
|
65
|
+
[ nil, nil, nil, nil, 8, 9, nil, nil, nil, ],
|
66
|
+
[ nil, nil, nil, nil, 8, 9, nil, nil, nil, ],
|
67
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
68
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
69
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
70
|
+
]
|
71
|
+
### Default Reduce Table
|
72
|
+
defred_table = [
|
73
|
+
nil,
|
74
|
+
nil,
|
75
|
+
-3,
|
76
|
+
-6,
|
77
|
+
-7,
|
78
|
+
nil,
|
79
|
+
nil,
|
80
|
+
nil,
|
81
|
+
nil,
|
82
|
+
nil,
|
83
|
+
nil,
|
84
|
+
-1,
|
85
|
+
-2,
|
86
|
+
-4,
|
87
|
+
-5,
|
88
|
+
-8,
|
89
|
+
]
|
90
|
+
defred_after_shift_table = [
|
91
|
+
nil,
|
92
|
+
nil,
|
93
|
+
nil,
|
94
|
+
-6,
|
95
|
+
-7,
|
96
|
+
nil,
|
97
|
+
nil,
|
98
|
+
nil,
|
99
|
+
nil,
|
100
|
+
nil,
|
101
|
+
nil,
|
102
|
+
nil,
|
103
|
+
nil,
|
104
|
+
-4,
|
105
|
+
-5,
|
106
|
+
-8,
|
107
|
+
]
|
108
|
+
### Nonterm to Int
|
109
|
+
nt2i = {
|
110
|
+
:expr => 0,
|
111
|
+
:term => 1,
|
112
|
+
:fact => 2,
|
113
|
+
}
|
114
|
+
### Int to Nonterm
|
115
|
+
i2nt = [
|
116
|
+
:expr,
|
117
|
+
:term,
|
118
|
+
:fact,
|
119
|
+
]
|
120
|
+
### Goto Table
|
121
|
+
goto_table = [
|
122
|
+
[ 1, 2, 3, ],
|
123
|
+
[ nil, nil, nil, ],
|
124
|
+
[ nil, nil, nil, ],
|
125
|
+
[ nil, nil, nil, ],
|
126
|
+
[ nil, nil, nil, ],
|
127
|
+
[ 10, 2, 3, ],
|
128
|
+
[ nil, 11, 3, ],
|
129
|
+
[ nil, 12, 3, ],
|
130
|
+
[ nil, nil, 13, ],
|
131
|
+
[ nil, nil, 14, ],
|
132
|
+
[ nil, nil, nil, ],
|
133
|
+
[ nil, nil, nil, ],
|
134
|
+
[ nil, nil, nil, ],
|
135
|
+
[ nil, nil, nil, ],
|
136
|
+
[ nil, nil, nil, ],
|
137
|
+
[ nil, nil, nil, ],
|
138
|
+
]
|
139
|
+
|
140
|
+
|
141
|
+
alias orig_error error
|
142
|
+
def error
|
143
|
+
orig_error
|
144
|
+
warn "current state: #{StateInfo[@stack.last]}"
|
145
|
+
end
|
146
|
+
|
147
|
+
### States
|
148
|
+
StateInfo = [
|
149
|
+
<<'----------',
|
150
|
+
I000 =
|
151
|
+
( 0) $start : _ expr
|
152
|
+
|
153
|
+
NUM shift, and goto to state 4
|
154
|
+
'(' shift, and goto to state 5
|
155
|
+
|
156
|
+
|
157
|
+
----------
|
158
|
+
|
159
|
+
<<'----------',
|
160
|
+
I001 =
|
161
|
+
( 0) $start : expr _
|
162
|
+
( 1) expr : expr _ '+' term
|
163
|
+
( 2) expr : expr _ '-' term
|
164
|
+
|
165
|
+
'+' shift, and goto to state 6
|
166
|
+
'-' shift, and goto to state 7
|
167
|
+
|
168
|
+
|
169
|
+
$end accept
|
170
|
+
|
171
|
+
----------
|
172
|
+
|
173
|
+
<<'----------',
|
174
|
+
I002 =
|
175
|
+
( 3) expr : term _
|
176
|
+
( 4) term : term _ '*' fact
|
177
|
+
( 5) term : term _ '/' fact
|
178
|
+
|
179
|
+
'*' shift, and goto to state 8
|
180
|
+
'/' shift, and goto to state 9
|
181
|
+
|
182
|
+
$default reduce using rule 3 (expr)
|
183
|
+
|
184
|
+
----------
|
185
|
+
|
186
|
+
<<'----------',
|
187
|
+
I003 =
|
188
|
+
( 6) term : fact _
|
189
|
+
|
190
|
+
|
191
|
+
$default reduce using rule 6 (term) [after shift]
|
192
|
+
|
193
|
+
----------
|
194
|
+
|
195
|
+
<<'----------',
|
196
|
+
I004 =
|
197
|
+
( 7) fact : NUM _
|
198
|
+
|
199
|
+
|
200
|
+
$default reduce using rule 7 (fact) [after shift]
|
201
|
+
|
202
|
+
----------
|
203
|
+
|
204
|
+
<<'----------',
|
205
|
+
I005 =
|
206
|
+
( 8) fact : '(' _ expr ')'
|
207
|
+
|
208
|
+
NUM shift, and goto to state 4
|
209
|
+
'(' shift, and goto to state 5
|
210
|
+
|
211
|
+
|
212
|
+
----------
|
213
|
+
|
214
|
+
<<'----------',
|
215
|
+
I006 =
|
216
|
+
( 1) expr : expr '+' _ term
|
217
|
+
|
218
|
+
NUM shift, and goto to state 4
|
219
|
+
'(' shift, and goto to state 5
|
220
|
+
|
221
|
+
|
222
|
+
----------
|
223
|
+
|
224
|
+
<<'----------',
|
225
|
+
I007 =
|
226
|
+
( 2) expr : expr '-' _ term
|
227
|
+
|
228
|
+
NUM shift, and goto to state 4
|
229
|
+
'(' shift, and goto to state 5
|
230
|
+
|
231
|
+
|
232
|
+
----------
|
233
|
+
|
234
|
+
<<'----------',
|
235
|
+
I008 =
|
236
|
+
( 4) term : term '*' _ fact
|
237
|
+
|
238
|
+
NUM shift, and goto to state 4
|
239
|
+
'(' shift, and goto to state 5
|
240
|
+
|
241
|
+
|
242
|
+
----------
|
243
|
+
|
244
|
+
<<'----------',
|
245
|
+
I009 =
|
246
|
+
( 5) term : term '/' _ fact
|
247
|
+
|
248
|
+
NUM shift, and goto to state 4
|
249
|
+
'(' shift, and goto to state 5
|
250
|
+
|
251
|
+
|
252
|
+
----------
|
253
|
+
|
254
|
+
<<'----------',
|
255
|
+
I010 =
|
256
|
+
( 8) fact : '(' expr _ ')'
|
257
|
+
( 1) expr : expr _ '+' term
|
258
|
+
( 2) expr : expr _ '-' term
|
259
|
+
|
260
|
+
'+' shift, and goto to state 6
|
261
|
+
'-' shift, and goto to state 7
|
262
|
+
')' shift, and goto to state 15
|
263
|
+
|
264
|
+
|
265
|
+
----------
|
266
|
+
|
267
|
+
<<'----------',
|
268
|
+
I011 =
|
269
|
+
( 1) expr : expr '+' term _
|
270
|
+
( 4) term : term _ '*' fact
|
271
|
+
( 5) term : term _ '/' fact
|
272
|
+
|
273
|
+
'*' shift, and goto to state 8
|
274
|
+
'/' shift, and goto to state 9
|
275
|
+
|
276
|
+
$default reduce using rule 1 (expr)
|
277
|
+
|
278
|
+
----------
|
279
|
+
|
280
|
+
<<'----------',
|
281
|
+
I012 =
|
282
|
+
( 2) expr : expr '-' term _
|
283
|
+
( 4) term : term _ '*' fact
|
284
|
+
( 5) term : term _ '/' fact
|
285
|
+
|
286
|
+
'*' shift, and goto to state 8
|
287
|
+
'/' shift, and goto to state 9
|
288
|
+
|
289
|
+
$default reduce using rule 2 (expr)
|
290
|
+
|
291
|
+
----------
|
292
|
+
|
293
|
+
<<'----------',
|
294
|
+
I013 =
|
295
|
+
( 4) term : term '*' fact _
|
296
|
+
|
297
|
+
|
298
|
+
$default reduce using rule 4 (term) [after shift]
|
299
|
+
|
300
|
+
----------
|
301
|
+
|
302
|
+
<<'----------',
|
303
|
+
I014 =
|
304
|
+
( 5) term : term '/' fact _
|
305
|
+
|
306
|
+
|
307
|
+
$default reduce using rule 5 (term) [after shift]
|
308
|
+
|
309
|
+
----------
|
310
|
+
|
311
|
+
<<'----------',
|
312
|
+
I015 =
|
313
|
+
( 8) fact : '(' expr ')' _
|
314
|
+
|
315
|
+
|
316
|
+
$default reduce using rule 8 (fact) [after shift]
|
317
|
+
|
318
|
+
----------
|
319
|
+
]
|
320
|
+
|
321
|
+
|
322
|
+
Tables = [ reduce_table, nparams, action_table,
|
323
|
+
defred_table, defred_after_shift_table, goto_table,
|
324
|
+
t2i, i2t, nt2i, i2nt ]
|
325
|
+
|
326
|
+
def self.createDecoratedParser
|
327
|
+
D4TinyCalc::ASTBuilder.new(TinyCalc.new())
|
328
|
+
end
|
329
|
+
|
330
|
+
### Inner Code
|
331
|
+
|
332
|
+
def lex
|
333
|
+
begin
|
334
|
+
until @line.empty?
|
335
|
+
case @line
|
336
|
+
|
337
|
+
when /\A\s+/, /\A\#.*/
|
338
|
+
@oldline = @line; @line = $'
|
339
|
+
|
340
|
+
|
341
|
+
|
342
|
+
when /\A[1-9][0-9]*/
|
343
|
+
@oldline = @line; @line = $'
|
344
|
+
yield _Token(:NUM, $&.to_i)
|
345
|
+
|
346
|
+
|
347
|
+
when /\A./
|
348
|
+
@oldline = @line; @line = $'
|
349
|
+
yield _Token($&, $&)
|
350
|
+
|
351
|
+
|
352
|
+
else
|
353
|
+
raise RuntimeError, "must not happen #{@line}"
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end while @line0 = @line = getline
|
357
|
+
yield nil, nil
|
358
|
+
end
|
359
|
+
|
360
|
+
end
|
361
|
+
|
362
|
+
### Outer Code
|
363
|
+
|
364
|
+
class Node
|
365
|
+
attr_accessor :lineno
|
366
|
+
attr_accessor :value
|
367
|
+
|
368
|
+
def initialize
|
369
|
+
@value = nil
|
370
|
+
|
371
|
+
end
|
372
|
+
|
373
|
+
def self.[] lineno, *args
|
374
|
+
self.new lineno, *args
|
375
|
+
end
|
376
|
+
def accept v
|
377
|
+
end
|
378
|
+
end
|
379
|
+
class NodeList < Node
|
380
|
+
attr_accessor :lineno
|
381
|
+
def initialize(lineno, lst=[])
|
382
|
+
@lineno = lineno
|
383
|
+
@lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
|
384
|
+
end
|
385
|
+
|
386
|
+
def all_value
|
387
|
+
@lst.map{|i| i.value}
|
388
|
+
end
|
389
|
+
|
390
|
+
def size
|
391
|
+
@lst.size
|
392
|
+
end
|
393
|
+
alias length size
|
394
|
+
def push(i)
|
395
|
+
@lst.push i unless i.is_a? NilNode
|
396
|
+
self
|
397
|
+
end
|
398
|
+
def concat(i)
|
399
|
+
@lst.concat i
|
400
|
+
self
|
401
|
+
end
|
402
|
+
def to_ary()
|
403
|
+
@lst
|
404
|
+
end
|
405
|
+
alias to_a to_ary
|
406
|
+
alias list to_ary
|
407
|
+
def accept(v)
|
408
|
+
@lst.each{|i| i.accept(v) }
|
409
|
+
end
|
410
|
+
end
|
411
|
+
class NilNode
|
412
|
+
attr_accessor :lineno
|
413
|
+
attr_accessor :value
|
414
|
+
def initialize(lineno, *args)
|
415
|
+
@lineno = lineno
|
416
|
+
end
|
417
|
+
def accept v
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
|
422
|
+
class Node_add < Node
|
423
|
+
attr_accessor :left, :right
|
424
|
+
attr_accessor
|
425
|
+
def initialize lineno, left, right
|
426
|
+
super()
|
427
|
+
@lineno = lineno
|
428
|
+
@lineno = lineno
|
429
|
+
@left = left
|
430
|
+
@right = right
|
431
|
+
|
432
|
+
end
|
433
|
+
def accept v
|
434
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
435
|
+
|
436
|
+
v.visit_Node_add(self)
|
437
|
+
self
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
class Node_sub < Node
|
442
|
+
attr_accessor :left, :right
|
443
|
+
attr_accessor
|
444
|
+
def initialize lineno, left, right
|
445
|
+
super()
|
446
|
+
@lineno = lineno
|
447
|
+
@lineno = lineno
|
448
|
+
@left = left
|
449
|
+
@right = right
|
450
|
+
|
451
|
+
end
|
452
|
+
def accept v
|
453
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
454
|
+
|
455
|
+
v.visit_Node_sub(self)
|
456
|
+
self
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
class Node_mul < Node
|
461
|
+
attr_accessor :left, :right
|
462
|
+
attr_accessor
|
463
|
+
def initialize lineno, left, right
|
464
|
+
super()
|
465
|
+
@lineno = lineno
|
466
|
+
@lineno = lineno
|
467
|
+
@left = left
|
468
|
+
@right = right
|
469
|
+
|
470
|
+
end
|
471
|
+
def accept v
|
472
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
473
|
+
|
474
|
+
v.visit_Node_mul(self)
|
475
|
+
self
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
class Node_div < Node
|
480
|
+
attr_accessor :left, :right
|
481
|
+
attr_accessor
|
482
|
+
def initialize lineno, left, right
|
483
|
+
super()
|
484
|
+
@lineno = lineno
|
485
|
+
@lineno = lineno
|
486
|
+
@left = left
|
487
|
+
@right = right
|
488
|
+
|
489
|
+
end
|
490
|
+
def accept v
|
491
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
492
|
+
|
493
|
+
v.visit_Node_div(self)
|
494
|
+
self
|
495
|
+
end
|
496
|
+
end
|
497
|
+
|
498
|
+
class Node_literal < Node
|
499
|
+
attr_accessor :n
|
500
|
+
attr_accessor :n
|
501
|
+
def initialize lineno, n
|
502
|
+
super()
|
503
|
+
@lineno = lineno
|
504
|
+
@lineno = lineno
|
505
|
+
@n = n
|
506
|
+
|
507
|
+
end
|
508
|
+
def accept v
|
509
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
510
|
+
|
511
|
+
v.visit_Node_literal(self)
|
512
|
+
self
|
513
|
+
end
|
514
|
+
end
|
515
|
+
|
516
|
+
class Visitor
|
517
|
+
def visit node
|
518
|
+
node.accept(self)
|
519
|
+
end
|
520
|
+
|
521
|
+
module_eval <<-'.,.,120998293265197.,.,', 'sample_calc/calc.ast.dr', 16
|
522
|
+
|
523
|
+
|
524
|
+
.,.,120998293265197.,.,
|
525
|
+
|
526
|
+
module_eval <<-'.,.,120998293245244.,.,', 'sample_calc/calc.ast.dr', 16
|
527
|
+
def visit_Node_add node
|
528
|
+
node.value = visit(node.left).value + visit(node.right).value
|
529
|
+
|
530
|
+
rescue
|
531
|
+
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
532
|
+
raise
|
533
|
+
|
534
|
+
end
|
535
|
+
.,.,120998293245244.,.,
|
536
|
+
|
537
|
+
module_eval <<-'.,.,120998293213935.,.,', 'sample_calc/calc.ast.dr', 17
|
538
|
+
def visit_Node_sub node
|
539
|
+
node.value = visit(node.left).value - visit(node.right).value
|
540
|
+
|
541
|
+
rescue
|
542
|
+
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
543
|
+
raise
|
544
|
+
|
545
|
+
end
|
546
|
+
.,.,120998293213935.,.,
|
547
|
+
|
548
|
+
module_eval <<-'.,.,12099829324616.,.,', 'sample_calc/calc.ast.dr', 18
|
549
|
+
def visit_Node_mul node
|
550
|
+
node.value = visit(node.left).value * visit(node.right).value
|
551
|
+
|
552
|
+
rescue
|
553
|
+
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
554
|
+
raise
|
555
|
+
|
556
|
+
end
|
557
|
+
.,.,12099829324616.,.,
|
558
|
+
|
559
|
+
module_eval <<-'.,.,120998293234368.,.,', 'sample_calc/calc.ast.dr', 19
|
560
|
+
def visit_Node_div node
|
561
|
+
node.value = visit(node.left).value / visit(node.right).value
|
562
|
+
|
563
|
+
rescue
|
564
|
+
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
565
|
+
raise
|
566
|
+
|
567
|
+
end
|
568
|
+
.,.,120998293234368.,.,
|
569
|
+
|
570
|
+
module_eval <<-'.,.,120998293249216.,.,', 'sample_calc/calc.ast.dr', 20
|
571
|
+
def visit_Node_literal node
|
572
|
+
node.value = node.n.value
|
573
|
+
|
574
|
+
rescue
|
575
|
+
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
576
|
+
raise
|
577
|
+
|
578
|
+
end
|
579
|
+
.,.,120998293249216.,.,
|
580
|
+
|
581
|
+
end
|
582
|
+
|
583
|
+
class D4TinyCalc::ASTBuilder < Depager::LALR::Action #:nodoc:all
|
584
|
+
include Depager::DecoratorUtils
|
585
|
+
[]
|
586
|
+
on_reduce = [
|
587
|
+
nil,
|
588
|
+
:_ast_0,
|
589
|
+
:_ast_1,
|
590
|
+
:_ast_2,
|
591
|
+
:_ast_3,
|
592
|
+
:_ast_4,
|
593
|
+
:_ast_5,
|
594
|
+
:_ast_6,
|
595
|
+
:_ast_7,
|
596
|
+
|
597
|
+
]
|
598
|
+
Tables = [on_reduce]
|
599
|
+
def initialize inside
|
600
|
+
super inside, 'ASTBuilder'
|
601
|
+
@on_reduce, = self.class::Tables
|
602
|
+
init_parser
|
603
|
+
end
|
604
|
+
|
605
|
+
|
606
|
+
module_eval <<-'.,.,120998293222606.,.,', 'sample_calc/calc.ast.dr', 26
|
607
|
+
def _ast_0 val
|
608
|
+
Node_add.new(val[0].lineno, val[0], val[2])
|
609
|
+
end
|
610
|
+
.,.,120998293222606.,.,
|
611
|
+
|
612
|
+
module_eval <<-'.,.,120998293214748.,.,', 'sample_calc/calc.ast.dr', 28
|
613
|
+
def _ast_1 val
|
614
|
+
Node_sub.new(val[0].lineno, val[0], val[2])
|
615
|
+
end
|
616
|
+
.,.,120998293214748.,.,
|
617
|
+
|
618
|
+
module_eval <<-'.,.,120998293241279.,.,', 'sample_calc/calc.ast.dr', 30
|
619
|
+
def _ast_2 val
|
620
|
+
val[0]
|
621
|
+
end
|
622
|
+
.,.,120998293241279.,.,
|
623
|
+
|
624
|
+
module_eval <<-'.,.,120998293210610.,.,', 'sample_calc/calc.ast.dr', 34
|
625
|
+
def _ast_3 val
|
626
|
+
Node_mul.new(val[0].lineno, val[0], val[2])
|
627
|
+
end
|
628
|
+
.,.,120998293210610.,.,
|
629
|
+
|
630
|
+
module_eval <<-'.,.,120998293222357.,.,', 'sample_calc/calc.ast.dr', 36
|
631
|
+
def _ast_4 val
|
632
|
+
Node_div.new(val[0].lineno, val[0], val[2])
|
633
|
+
end
|
634
|
+
.,.,120998293222357.,.,
|
635
|
+
|
636
|
+
module_eval <<-'.,.,120998293264485.,.,', 'sample_calc/calc.ast.dr', 38
|
637
|
+
def _ast_5 val
|
638
|
+
val[0]
|
639
|
+
end
|
640
|
+
.,.,120998293264485.,.,
|
641
|
+
|
642
|
+
module_eval <<-'.,.,120998293260105.,.,', 'sample_calc/calc.ast.dr', 42
|
643
|
+
def _ast_6 val
|
644
|
+
Node_literal.new(val[0].lineno, val[0])
|
645
|
+
end
|
646
|
+
.,.,120998293260105.,.,
|
647
|
+
|
648
|
+
module_eval <<-'.,.,120998293251005.,.,', 'sample_calc/calc.ast.dr', 44
|
649
|
+
def _ast_7 val
|
650
|
+
val[1]
|
651
|
+
end
|
652
|
+
.,.,120998293251005.,.,
|
653
|
+
|
654
|
+
end
|
655
|
+
|
656
|
+
|
657
|
+
def createDecoratedTinyCalc
|
658
|
+
D4TinyCalc::ASTBuilder.new(TinyCalc.new())
|
659
|
+
end
|
660
|
+
|
661
|
+
### main
|
662
|
+
if __FILE__ == $0
|
663
|
+
### Main Code
|
664
|
+
require 'pp'
|
665
|
+
parser = createDecoratedTinyCalc()
|
666
|
+
t, = parser.yyparse(STDIN)
|
667
|
+
v = Visitor.new
|
668
|
+
pp t.accept(v).value
|
669
|
+
pp t
|
670
|
+
|
671
|
+
|
672
|
+
end
|