robolson-simplesem 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,823 +0,0 @@
1
- module SimpleSem
2
- include Treetop::Runtime
3
-
4
- def root
5
- @root || :statement
6
- end
7
-
8
- include Arithmetic
9
-
10
- def _nt_statement
11
- start_index = index
12
- if node_cache[:statement].has_key?(index)
13
- cached = node_cache[:statement][index]
14
- @index = cached.interval.end if cached
15
- return cached
16
- end
17
-
18
- i0 = index
19
- r1 = _nt_set_stmt
20
- if r1
21
- r0 = r1
22
- else
23
- r2 = _nt_jump_stmt
24
- if r2
25
- r0 = r2
26
- else
27
- r3 = _nt_jumpt_stmt
28
- if r3
29
- r0 = r3
30
- else
31
- r4 = _nt_halt
32
- if r4
33
- r0 = r4
34
- else
35
- self.index = i0
36
- r0 = nil
37
- end
38
- end
39
- end
40
- end
41
-
42
- node_cache[:statement][start_index] = r0
43
-
44
- return r0
45
- end
46
-
47
- module Halt0
48
- def execute(env={})
49
- raise ProgramHalt
50
- end
51
- end
52
-
53
- def _nt_halt
54
- start_index = index
55
- if node_cache[:halt].has_key?(index)
56
- cached = node_cache[:halt][index]
57
- @index = cached.interval.end if cached
58
- return cached
59
- end
60
-
61
- if input.index('halt', index) == index
62
- r0 = (SyntaxNode).new(input, index...(index + 4))
63
- r0.extend(Halt0)
64
- @index += 4
65
- else
66
- terminal_parse_failure('halt')
67
- r0 = nil
68
- end
69
-
70
- node_cache[:halt][start_index] = r0
71
-
72
- return r0
73
- end
74
-
75
- def _nt_set_stmt
76
- start_index = index
77
- if node_cache[:set_stmt].has_key?(index)
78
- cached = node_cache[:set_stmt][index]
79
- @index = cached.interval.end if cached
80
- return cached
81
- end
82
-
83
- i0 = index
84
- r1 = _nt_set_stmt_assign
85
- if r1
86
- r0 = r1
87
- else
88
- r2 = _nt_set_stmt_write
89
- if r2
90
- r0 = r2
91
- else
92
- r3 = _nt_set_stmt_read
93
- if r3
94
- r0 = r3
95
- else
96
- self.index = i0
97
- r0 = nil
98
- end
99
- end
100
- end
101
-
102
- node_cache[:set_stmt][start_index] = r0
103
-
104
- return r0
105
- end
106
-
107
- module SetStmtAssign0
108
- def space
109
- elements[1]
110
- end
111
-
112
- def loc
113
- elements[2]
114
- end
115
-
116
- def comma
117
- elements[3]
118
- end
119
-
120
- def value
121
- elements[4]
122
- end
123
- end
124
-
125
- module SetStmtAssign1
126
- def execute(env)
127
- env.data[loc.eval(env)] = value.eval(env)
128
- end
129
- end
130
-
131
- def _nt_set_stmt_assign
132
- start_index = index
133
- if node_cache[:set_stmt_assign].has_key?(index)
134
- cached = node_cache[:set_stmt_assign][index]
135
- @index = cached.interval.end if cached
136
- return cached
137
- end
138
-
139
- i0, s0 = index, []
140
- if input.index('set', index) == index
141
- r1 = (SyntaxNode).new(input, index...(index + 3))
142
- @index += 3
143
- else
144
- terminal_parse_failure('set')
145
- r1 = nil
146
- end
147
- s0 << r1
148
- if r1
149
- r2 = _nt_space
150
- s0 << r2
151
- if r2
152
- r3 = _nt_additive
153
- s0 << r3
154
- if r3
155
- r4 = _nt_comma
156
- s0 << r4
157
- if r4
158
- r5 = _nt_additive
159
- s0 << r5
160
- end
161
- end
162
- end
163
- end
164
- if s0.last
165
- r0 = (SyntaxNode).new(input, i0...index, s0)
166
- r0.extend(SetStmtAssign0)
167
- r0.extend(SetStmtAssign1)
168
- else
169
- self.index = i0
170
- r0 = nil
171
- end
172
-
173
- node_cache[:set_stmt_assign][start_index] = r0
174
-
175
- return r0
176
- end
177
-
178
- module SetStmtWrite0
179
- def space
180
- elements[1]
181
- end
182
-
183
- def comma
184
- elements[3]
185
- end
186
-
187
- def expression
188
- elements[4]
189
- end
190
- end
191
-
192
- module SetStmtWrite1
193
- def execute(env)
194
- puts expression.eval(env)
195
- end
196
- end
197
-
198
- module SetStmtWrite2
199
- end
200
-
201
- module SetStmtWrite3
202
- def space
203
- elements[1]
204
- end
205
-
206
- def comma
207
- elements[3]
208
- end
209
-
210
- def string
211
- elements[5]
212
- end
213
-
214
- end
215
-
216
- module SetStmtWrite4
217
- def execute(env)
218
- puts string.text_value
219
- end
220
- end
221
-
222
- def _nt_set_stmt_write
223
- start_index = index
224
- if node_cache[:set_stmt_write].has_key?(index)
225
- cached = node_cache[:set_stmt_write][index]
226
- @index = cached.interval.end if cached
227
- return cached
228
- end
229
-
230
- i0 = index
231
- i1, s1 = index, []
232
- if input.index('set', index) == index
233
- r2 = (SyntaxNode).new(input, index...(index + 3))
234
- @index += 3
235
- else
236
- terminal_parse_failure('set')
237
- r2 = nil
238
- end
239
- s1 << r2
240
- if r2
241
- r3 = _nt_space
242
- s1 << r3
243
- if r3
244
- if input.index('write', index) == index
245
- r4 = (SyntaxNode).new(input, index...(index + 5))
246
- @index += 5
247
- else
248
- terminal_parse_failure('write')
249
- r4 = nil
250
- end
251
- s1 << r4
252
- if r4
253
- r5 = _nt_comma
254
- s1 << r5
255
- if r5
256
- r6 = _nt_expression
257
- s1 << r6
258
- end
259
- end
260
- end
261
- end
262
- if s1.last
263
- r1 = (SyntaxNode).new(input, i1...index, s1)
264
- r1.extend(SetStmtWrite0)
265
- r1.extend(SetStmtWrite1)
266
- else
267
- self.index = i1
268
- r1 = nil
269
- end
270
- if r1
271
- r0 = r1
272
- else
273
- i7, s7 = index, []
274
- if input.index('set', index) == index
275
- r8 = (SyntaxNode).new(input, index...(index + 3))
276
- @index += 3
277
- else
278
- terminal_parse_failure('set')
279
- r8 = nil
280
- end
281
- s7 << r8
282
- if r8
283
- r9 = _nt_space
284
- s7 << r9
285
- if r9
286
- if input.index('write', index) == index
287
- r10 = (SyntaxNode).new(input, index...(index + 5))
288
- @index += 5
289
- else
290
- terminal_parse_failure('write')
291
- r10 = nil
292
- end
293
- s7 << r10
294
- if r10
295
- r11 = _nt_comma
296
- s7 << r11
297
- if r11
298
- if input.index('"', index) == index
299
- r12 = (SyntaxNode).new(input, index...(index + 1))
300
- @index += 1
301
- else
302
- terminal_parse_failure('"')
303
- r12 = nil
304
- end
305
- s7 << r12
306
- if r12
307
- s13, i13 = [], index
308
- loop do
309
- i14, s14 = index, []
310
- i15 = index
311
- if input.index('"', index) == index
312
- r16 = (SyntaxNode).new(input, index...(index + 1))
313
- @index += 1
314
- else
315
- terminal_parse_failure('"')
316
- r16 = nil
317
- end
318
- if r16
319
- r15 = nil
320
- else
321
- self.index = i15
322
- r15 = SyntaxNode.new(input, index...index)
323
- end
324
- s14 << r15
325
- if r15
326
- if index < input_length
327
- r17 = (SyntaxNode).new(input, index...(index + 1))
328
- @index += 1
329
- else
330
- terminal_parse_failure("any character")
331
- r17 = nil
332
- end
333
- s14 << r17
334
- end
335
- if s14.last
336
- r14 = (SyntaxNode).new(input, i14...index, s14)
337
- r14.extend(SetStmtWrite2)
338
- else
339
- self.index = i14
340
- r14 = nil
341
- end
342
- if r14
343
- s13 << r14
344
- else
345
- break
346
- end
347
- end
348
- r13 = SyntaxNode.new(input, i13...index, s13)
349
- s7 << r13
350
- if r13
351
- if input.index('"', index) == index
352
- r18 = (SyntaxNode).new(input, index...(index + 1))
353
- @index += 1
354
- else
355
- terminal_parse_failure('"')
356
- r18 = nil
357
- end
358
- s7 << r18
359
- end
360
- end
361
- end
362
- end
363
- end
364
- end
365
- if s7.last
366
- r7 = (SyntaxNode).new(input, i7...index, s7)
367
- r7.extend(SetStmtWrite3)
368
- r7.extend(SetStmtWrite4)
369
- else
370
- self.index = i7
371
- r7 = nil
372
- end
373
- if r7
374
- r0 = r7
375
- else
376
- self.index = i0
377
- r0 = nil
378
- end
379
- end
380
-
381
- node_cache[:set_stmt_write][start_index] = r0
382
-
383
- return r0
384
- end
385
-
386
- module SetStmtRead0
387
- def space
388
- elements[1]
389
- end
390
-
391
- def loc
392
- elements[2]
393
- end
394
-
395
- def comma
396
- elements[3]
397
- end
398
-
399
- end
400
-
401
- module SetStmtRead1
402
- def execute(env)
403
- print "input: "
404
- env.data[loc.eval(env)] = $stdin.gets.strip.to_i
405
- end
406
- end
407
-
408
- def _nt_set_stmt_read
409
- start_index = index
410
- if node_cache[:set_stmt_read].has_key?(index)
411
- cached = node_cache[:set_stmt_read][index]
412
- @index = cached.interval.end if cached
413
- return cached
414
- end
415
-
416
- i0, s0 = index, []
417
- if input.index('set', index) == index
418
- r1 = (SyntaxNode).new(input, index...(index + 3))
419
- @index += 3
420
- else
421
- terminal_parse_failure('set')
422
- r1 = nil
423
- end
424
- s0 << r1
425
- if r1
426
- r2 = _nt_space
427
- s0 << r2
428
- if r2
429
- r3 = _nt_additive
430
- s0 << r3
431
- if r3
432
- r4 = _nt_comma
433
- s0 << r4
434
- if r4
435
- if input.index('read', index) == index
436
- r5 = (SyntaxNode).new(input, index...(index + 4))
437
- @index += 4
438
- else
439
- terminal_parse_failure('read')
440
- r5 = nil
441
- end
442
- s0 << r5
443
- end
444
- end
445
- end
446
- end
447
- if s0.last
448
- r0 = (SyntaxNode).new(input, i0...index, s0)
449
- r0.extend(SetStmtRead0)
450
- r0.extend(SetStmtRead1)
451
- else
452
- self.index = i0
453
- r0 = nil
454
- end
455
-
456
- node_cache[:set_stmt_read][start_index] = r0
457
-
458
- return r0
459
- end
460
-
461
- module JumpStmt0
462
- def space
463
- elements[1]
464
- end
465
-
466
- def loc
467
- elements[2]
468
- end
469
- end
470
-
471
- module JumpStmt1
472
- def execute(env)
473
- env.pc = loc.eval(env)
474
- end
475
- end
476
-
477
- def _nt_jump_stmt
478
- start_index = index
479
- if node_cache[:jump_stmt].has_key?(index)
480
- cached = node_cache[:jump_stmt][index]
481
- @index = cached.interval.end if cached
482
- return cached
483
- end
484
-
485
- i0, s0 = index, []
486
- if input.index('jump', index) == index
487
- r1 = (SyntaxNode).new(input, index...(index + 4))
488
- @index += 4
489
- else
490
- terminal_parse_failure('jump')
491
- r1 = nil
492
- end
493
- s0 << r1
494
- if r1
495
- r2 = _nt_space
496
- s0 << r2
497
- if r2
498
- r3 = _nt_additive
499
- s0 << r3
500
- end
501
- end
502
- if s0.last
503
- r0 = (SyntaxNode).new(input, i0...index, s0)
504
- r0.extend(JumpStmt0)
505
- r0.extend(JumpStmt1)
506
- else
507
- self.index = i0
508
- r0 = nil
509
- end
510
-
511
- node_cache[:jump_stmt][start_index] = r0
512
-
513
- return r0
514
- end
515
-
516
- module JumptStmt0
517
- def space
518
- elements[1]
519
- end
520
-
521
- def loc
522
- elements[2]
523
- end
524
-
525
- def comma
526
- elements[3]
527
- end
528
-
529
- def expression
530
- elements[4]
531
- end
532
- end
533
-
534
- module JumptStmt1
535
- def execute(env)
536
- if expression.eval(env)
537
- env.pc = loc.eval(env)
538
- end
539
- end
540
- end
541
-
542
- def _nt_jumpt_stmt
543
- start_index = index
544
- if node_cache[:jumpt_stmt].has_key?(index)
545
- cached = node_cache[:jumpt_stmt][index]
546
- @index = cached.interval.end if cached
547
- return cached
548
- end
549
-
550
- i0, s0 = index, []
551
- if input.index('jumpt', index) == index
552
- r1 = (SyntaxNode).new(input, index...(index + 5))
553
- @index += 5
554
- else
555
- terminal_parse_failure('jumpt')
556
- r1 = nil
557
- end
558
- s0 << r1
559
- if r1
560
- r2 = _nt_space
561
- s0 << r2
562
- if r2
563
- r3 = _nt_additive
564
- s0 << r3
565
- if r3
566
- r4 = _nt_comma
567
- s0 << r4
568
- if r4
569
- r5 = _nt_expression
570
- s0 << r5
571
- end
572
- end
573
- end
574
- end
575
- if s0.last
576
- r0 = (SyntaxNode).new(input, i0...index, s0)
577
- r0.extend(JumptStmt0)
578
- r0.extend(JumptStmt1)
579
- else
580
- self.index = i0
581
- r0 = nil
582
- end
583
-
584
- node_cache[:jumpt_stmt][start_index] = r0
585
-
586
- return r0
587
- end
588
-
589
- module Primary0
590
- def space
591
- elements[1]
592
- end
593
-
594
- def expression
595
- elements[2]
596
- end
597
-
598
- def space
599
- elements[3]
600
- end
601
-
602
- end
603
-
604
- module Primary1
605
- def eval(env={})
606
- expression.eval(env)
607
- end
608
- end
609
-
610
- def _nt_primary
611
- start_index = index
612
- if node_cache[:primary].has_key?(index)
613
- cached = node_cache[:primary][index]
614
- @index = cached.interval.end if cached
615
- return cached
616
- end
617
-
618
- i0 = index
619
- r1 = _nt_ip
620
- if r1
621
- r0 = r1
622
- else
623
- r2 = _nt_data_lookup
624
- if r2
625
- r0 = r2
626
- else
627
- r3 = _nt_number
628
- if r3
629
- r0 = r3
630
- else
631
- i4, s4 = index, []
632
- if input.index('(', index) == index
633
- r5 = (SyntaxNode).new(input, index...(index + 1))
634
- @index += 1
635
- else
636
- terminal_parse_failure('(')
637
- r5 = nil
638
- end
639
- s4 << r5
640
- if r5
641
- r6 = _nt_space
642
- s4 << r6
643
- if r6
644
- r7 = _nt_expression
645
- s4 << r7
646
- if r7
647
- r8 = _nt_space
648
- s4 << r8
649
- if r8
650
- if input.index(')', index) == index
651
- r9 = (SyntaxNode).new(input, index...(index + 1))
652
- @index += 1
653
- else
654
- terminal_parse_failure(')')
655
- r9 = nil
656
- end
657
- s4 << r9
658
- end
659
- end
660
- end
661
- end
662
- if s4.last
663
- r4 = (SyntaxNode).new(input, i4...index, s4)
664
- r4.extend(Primary0)
665
- r4.extend(Primary1)
666
- else
667
- self.index = i4
668
- r4 = nil
669
- end
670
- if r4
671
- r0 = r4
672
- else
673
- self.index = i0
674
- r0 = nil
675
- end
676
- end
677
- end
678
- end
679
-
680
- node_cache[:primary][start_index] = r0
681
-
682
- return r0
683
- end
684
-
685
- module DataLookup0
686
- def expr
687
- elements[1]
688
- end
689
-
690
- end
691
-
692
- module DataLookup1
693
- def eval(env)
694
- env.data[expr.eval(env)]
695
- end
696
- end
697
-
698
- def _nt_data_lookup
699
- start_index = index
700
- if node_cache[:data_lookup].has_key?(index)
701
- cached = node_cache[:data_lookup][index]
702
- @index = cached.interval.end if cached
703
- return cached
704
- end
705
-
706
- i0, s0 = index, []
707
- if input.index('D[', index) == index
708
- r1 = (SyntaxNode).new(input, index...(index + 2))
709
- @index += 2
710
- else
711
- terminal_parse_failure('D[')
712
- r1 = nil
713
- end
714
- s0 << r1
715
- if r1
716
- r2 = _nt_additive
717
- s0 << r2
718
- if r2
719
- if input.index(']', index) == index
720
- r3 = (SyntaxNode).new(input, index...(index + 1))
721
- @index += 1
722
- else
723
- terminal_parse_failure(']')
724
- r3 = nil
725
- end
726
- s0 << r3
727
- end
728
- end
729
- if s0.last
730
- r0 = (SyntaxNode).new(input, i0...index, s0)
731
- r0.extend(DataLookup0)
732
- r0.extend(DataLookup1)
733
- else
734
- self.index = i0
735
- r0 = nil
736
- end
737
-
738
- node_cache[:data_lookup][start_index] = r0
739
-
740
- return r0
741
- end
742
-
743
- module Ip0
744
- def eval(env)
745
- env.pc
746
- end
747
- end
748
-
749
- def _nt_ip
750
- start_index = index
751
- if node_cache[:ip].has_key?(index)
752
- cached = node_cache[:ip][index]
753
- @index = cached.interval.end if cached
754
- return cached
755
- end
756
-
757
- if input.index('ip', index) == index
758
- r0 = (SyntaxNode).new(input, index...(index + 2))
759
- r0.extend(Ip0)
760
- @index += 2
761
- else
762
- terminal_parse_failure('ip')
763
- r0 = nil
764
- end
765
-
766
- node_cache[:ip][start_index] = r0
767
-
768
- return r0
769
- end
770
-
771
- module Comma0
772
- def space
773
- elements[0]
774
- end
775
-
776
- def space
777
- elements[2]
778
- end
779
- end
780
-
781
- def _nt_comma
782
- start_index = index
783
- if node_cache[:comma].has_key?(index)
784
- cached = node_cache[:comma][index]
785
- @index = cached.interval.end if cached
786
- return cached
787
- end
788
-
789
- i0, s0 = index, []
790
- r1 = _nt_space
791
- s0 << r1
792
- if r1
793
- if input.index(',', index) == index
794
- r2 = (SyntaxNode).new(input, index...(index + 1))
795
- @index += 1
796
- else
797
- terminal_parse_failure(',')
798
- r2 = nil
799
- end
800
- s0 << r2
801
- if r2
802
- r3 = _nt_space
803
- s0 << r3
804
- end
805
- end
806
- if s0.last
807
- r0 = (SyntaxNode).new(input, i0...index, s0)
808
- r0.extend(Comma0)
809
- else
810
- self.index = i0
811
- r0 = nil
812
- end
813
-
814
- node_cache[:comma][start_index] = r0
815
-
816
- return r0
817
- end
818
-
819
- end
820
-
821
- class SimpleSemParser < Treetop::Runtime::CompiledParser
822
- include SimpleSem
823
- end