heist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/History.txt +21 -0
  2. data/Manifest.txt +53 -0
  3. data/README.txt +274 -0
  4. data/Rakefile +12 -0
  5. data/bin/heist +16 -0
  6. data/lib/bin_spec.rb +25 -0
  7. data/lib/builtin/library.scm +95 -0
  8. data/lib/builtin/primitives.rb +306 -0
  9. data/lib/builtin/syntax.rb +166 -0
  10. data/lib/builtin/syntax.scm +155 -0
  11. data/lib/heist.rb +47 -0
  12. data/lib/parser/nodes.rb +105 -0
  13. data/lib/parser/scheme.rb +1081 -0
  14. data/lib/parser/scheme.tt +80 -0
  15. data/lib/repl.rb +112 -0
  16. data/lib/runtime/binding.rb +31 -0
  17. data/lib/runtime/callable/continuation.rb +24 -0
  18. data/lib/runtime/callable/function.rb +55 -0
  19. data/lib/runtime/callable/macro.rb +170 -0
  20. data/lib/runtime/callable/macro/expansion.rb +15 -0
  21. data/lib/runtime/callable/macro/matches.rb +77 -0
  22. data/lib/runtime/callable/macro/splice.rb +56 -0
  23. data/lib/runtime/data/expression.rb +23 -0
  24. data/lib/runtime/data/identifier.rb +20 -0
  25. data/lib/runtime/data/list.rb +36 -0
  26. data/lib/runtime/frame.rb +118 -0
  27. data/lib/runtime/runtime.rb +61 -0
  28. data/lib/runtime/scope.rb +121 -0
  29. data/lib/runtime/stack.rb +60 -0
  30. data/lib/runtime/stackless.rb +49 -0
  31. data/lib/stdlib/benchmark.scm +12 -0
  32. data/lib/stdlib/birdhouse.scm +82 -0
  33. data/test/arithmetic.scm +57 -0
  34. data/test/benchmarks.scm +27 -0
  35. data/test/booleans.scm +6 -0
  36. data/test/closures.scm +16 -0
  37. data/test/conditionals.scm +55 -0
  38. data/test/continuations.scm +144 -0
  39. data/test/define_functions.scm +27 -0
  40. data/test/define_values.scm +28 -0
  41. data/test/delay.scm +8 -0
  42. data/test/file_loading.scm +9 -0
  43. data/test/hygienic.scm +39 -0
  44. data/test/let.scm +42 -0
  45. data/test/lib.scm +2 -0
  46. data/test/macro-helpers.scm +19 -0
  47. data/test/macros.scm +343 -0
  48. data/test/numbers.scm +19 -0
  49. data/test/plt-macros.txt +40 -0
  50. data/test/test_heist.rb +84 -0
  51. data/test/unhygienic.scm +11 -0
  52. data/test/vars.scm +2 -0
  53. metadata +138 -0
@@ -0,0 +1,155 @@
1
+ ; Control structures
2
+
3
+ ; (cond) goes through a list of tests, evaluating each one
4
+ ; in order of appearance. Once a matching precondition is
5
+ ; found, its consequent is tail-called and no further
6
+ ; preconditions are evaluated.
7
+ (define-syntax cond (syntax-rules (else =>)
8
+ [(cond) #f]
9
+ [(cond (else expr1 expr2 ...))
10
+ (begin expr1 expr2 ...)]
11
+ [(cond (test => function) clause ...)
12
+ (let ([temp test])
13
+ (if temp
14
+ (function temp)
15
+ (cond clause ...)))]
16
+ [(cond (test expression ...) clause ...)
17
+ (if test
18
+ (begin expression ...)
19
+ (cond clause ...))]))
20
+
21
+ ; (case) acts like Ruby's case statement. The value of the
22
+ ; given expression is compared against a series of lists;
23
+ ; once a list is found to include the value, the expressions
24
+ ; following the list are evaluated and no further lists
25
+ ; are tested.
26
+ (define-syntax case (syntax-rules (else)
27
+ [(case key) #f]
28
+ [(case key (else expr1 expr2 ...))
29
+ (begin expr1 expr2 ...)]
30
+ [(case key (() expr ...) clause ...)
31
+ (case key clause ...)]
32
+ [(case key
33
+ ((datum1 datum2 ...) expr1 expr2 ...)
34
+ clause ...)
35
+ (let ([temp key])
36
+ (if (eqv? temp 'datum1)
37
+ (begin expr1 expr2 ...)
38
+ (case temp
39
+ ((datum2 ...) expr1 expr2 ...)
40
+ clause ...)))]))
41
+
42
+ ;----------------------------------------------------------------
43
+
44
+ ; Binding constructs
45
+
46
+ ; (let), (let*) and (letrec) each create a new scope and bind
47
+ ; values to some symbols before executing a series of lists.
48
+ ; They differ according to how they evaluate the bound values.
49
+
50
+ ; (let) evaluates values in the enclosing scope, so lambdas will
51
+ ; not be able to refer to other values assigned using the (let).
52
+ (define-syntax let (syntax-rules ()
53
+ [(let ([variable init] ...) body ...)
54
+ ((lambda (variable ...)
55
+ body ...)
56
+ init ...)]
57
+ [(let name ([variable init] ...) body ...)
58
+ (letrec ([name (lambda (variable ...)
59
+ body ...)])
60
+ (name init ...))]))
61
+
62
+ ; (let*) creates a new scope for each variable and evaluates
63
+ ; each expression in its enclosing scope. Basically a shorthand
64
+ ; for several nested (let)s. Variables may refer to those that
65
+ ; preceed them but not vice versa.
66
+ (define-syntax let* (syntax-rules ()
67
+ [(let* ([name expression]) body ...)
68
+ (let ([name expression]) body ...)]
69
+ [(let* ([n1 e1] [n2 e2] ...) body ...)
70
+ (let ([n1 e1])
71
+ (let* ([n2 e2] ...) body ...))]))
72
+
73
+ ; (letrec) evaluates values in the inner scope, so lambdas are
74
+ ; able to refer to other values assigned using the (letrec).
75
+ (define-syntax letrec (syntax-rules ()
76
+ [(letrec ([variable init] ...) body ...)
77
+ ((lambda ()
78
+ (define variable init) ...
79
+ body ...))]))
80
+
81
+ (define let-syntax let)
82
+ (define letrec-syntax letrec)
83
+
84
+ ;----------------------------------------------------------------
85
+
86
+ ; Iteration
87
+
88
+ ; (do) is similar to the 'while' construct in procedural
89
+ ; languages. It assigns initial values to a set of variables,
90
+ ; then performs the list of given commands in a loop. If
91
+ ; before any iteration the test is found to be false, the
92
+ ; loop is halted and the value of the expression following
93
+ ; the test is returned.
94
+ (define-syntax do (syntax-rules ()
95
+ [(do ([variable init step ...] ...) ; Allow 0 or 1 step
96
+ (test expression ...)
97
+ command ...)
98
+ (let loop ([variable init] ...)
99
+ (if test
100
+ (begin expression ...)
101
+ (begin
102
+ command ...
103
+ (loop (do "step" variable step ...) ...))))]
104
+ [(do "step" variable)
105
+ variable]
106
+ [(do "step" variable step)
107
+ step]))
108
+
109
+ ;----------------------------------------------------------------
110
+
111
+ ; Boolean combinators
112
+
113
+ ; (and) returns the first falsey value returned by the list
114
+ ; of expressions, or returns the value of the last expression
115
+ ; if all values are truthy.
116
+ (define-syntax and (syntax-rules ()
117
+ [(and test) test]
118
+ [(and test1 test2 ...)
119
+ (let ([temp test1])
120
+ (if (not temp)
121
+ temp
122
+ (and test2 ...)))]))
123
+
124
+ ; (or) returns the first truthy value returned by the list
125
+ ; of expressions, or returns the value of the last expression
126
+ ; if all values are falsey.
127
+ (define-syntax or (syntax-rules ()
128
+ [(or test) test]
129
+ [(or test1 test2 ...)
130
+ (let ([temp test1])
131
+ (if temp
132
+ temp
133
+ (or test2 ...)))]))
134
+
135
+ ;----------------------------------------------------------------
136
+
137
+ ; Delayed evaluation
138
+
139
+ ; (delay) allows the evaluation of an expression to be delayed
140
+ ; by wrapping it in a promise. Use (force) to evaluate the promise
141
+ ; at a later time. The expression inside a promise is only
142
+ ; ever evaluated once, so a promise can be implemented as a
143
+ ; memoized closure.
144
+ (define-syntax delay (syntax-rules ()
145
+ [(delay expression)
146
+ (let ([forced #f]
147
+ [memo #f])
148
+ (lambda ()
149
+ (if forced
150
+ memo
151
+ (begin
152
+ (set! memo expression)
153
+ (set! forced #t)
154
+ memo))))]))
155
+
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'treetop'
3
+
4
+ module Heist
5
+ VERSION = '0.1.0'
6
+
7
+ ROOT_PATH = File.expand_path(File.dirname(__FILE__))
8
+ PARSER_PATH = ROOT_PATH + '/parser/'
9
+ RUNTIME_PATH = ROOT_PATH + '/runtime/'
10
+ BUILTIN_PATH = ROOT_PATH + '/builtin/'
11
+ LIB_PATH = ROOT_PATH + '/stdlib/'
12
+
13
+ require PARSER_PATH + 'scheme'
14
+ require PARSER_PATH + 'nodes'
15
+ require RUNTIME_PATH + 'runtime'
16
+ require ROOT_PATH + '/repl'
17
+
18
+ LOAD_PATH = [LIB_PATH]
19
+ FILE_EXT = ".scm"
20
+
21
+ class HeistError < StandardError; end
22
+ class RuntimeError < HeistError; end
23
+ class UndefinedVariable < RuntimeError; end
24
+ class SyntaxError < RuntimeError; end
25
+ class MacroError < SyntaxError; end
26
+ class MacroTemplateMismatch < MacroError; end
27
+
28
+ def self.parse(source)
29
+ @parser ||= SchemeParser.new
30
+ @parser.parse(source)
31
+ end
32
+
33
+ def self.evaluate(expression, scope)
34
+ Runtime::Expression === expression ?
35
+ expression.eval(scope) :
36
+ expression
37
+ end
38
+
39
+ def self.info(runtime)
40
+ puts "Heist Scheme interpreter v. #{ VERSION }"
41
+ puts "Evaluation mode: #{ runtime.lazy? ? 'LAZY' : 'EAGER' }"
42
+ puts "Continuations enabled? #{ runtime.stackless? ? 'NO' : 'YES' }"
43
+ puts "Macros: #{ runtime.hygienic? ? 'HYGIENIC' : 'UNHYGIENIC' }\n\n"
44
+ end
45
+
46
+ end
47
+
@@ -0,0 +1,105 @@
1
+ module Heist
2
+ module Scheme
3
+
4
+ SHORTHANDS = {
5
+ "'" => :quote,
6
+ "`" => :quasiquote,
7
+ "," => :unquote,
8
+ ",@" => :'unquote-splicing'
9
+ }
10
+
11
+ class Program < Treetop::Runtime::SyntaxNode
12
+ def eval(scope)
13
+ convert!
14
+ @data.map { |part| Heist.evaluate(part, scope) }.last
15
+ end
16
+
17
+ def convert!
18
+ return if @data
19
+ @data = []
20
+ elements.each_with_index { |cell, i| self[i] = cell.eval }
21
+ end
22
+
23
+ def [](index)
24
+ @data[index]
25
+ end
26
+
27
+ def []=(index, value)
28
+ value.exists_at!(self, index) if Runtime::Expression === value
29
+ @data[index] = value
30
+ end
31
+ end
32
+
33
+ module List
34
+ def eval
35
+ list = Runtime::List.new
36
+ cells.each { |c| list << c.eval }
37
+ list
38
+ end
39
+
40
+ def cells
41
+ @cells ||= elements[1].elements
42
+ end
43
+ end
44
+
45
+ class Cell < Treetop::Runtime::SyntaxNode
46
+ def eval
47
+ result = elements[3].eval
48
+ string = elements[1].text_value
49
+ SHORTHANDS.has_key?(string) ?
50
+ Runtime::List.new([Runtime::Identifier.new(SHORTHANDS[string]), result]) :
51
+ result
52
+ end
53
+ end
54
+
55
+ class Datum < Treetop::Runtime::SyntaxNode
56
+ def eval
57
+ elements[0].eval
58
+ end
59
+ end
60
+
61
+ module Boolean
62
+ def eval
63
+ @value ||= (text_value == "#t")
64
+ end
65
+ end
66
+
67
+ class Complex < Treetop::Runtime::SyntaxNode
68
+ def eval
69
+ # TODO
70
+ end
71
+ end
72
+
73
+ class Real < Treetop::Runtime::SyntaxNode
74
+ def eval
75
+ @value ||= Kernel.eval(text_value).to_f
76
+ end
77
+ end
78
+
79
+ class Rational < Treetop::Runtime::SyntaxNode
80
+ def eval
81
+ @value ||= numerator.eval.to_f / denominator.eval
82
+ end
83
+ end
84
+
85
+ class Integer < Treetop::Runtime::SyntaxNode
86
+ def eval
87
+ @value ||= Kernel.eval(text_value).to_i
88
+ end
89
+ end
90
+
91
+ class String < Treetop::Runtime::SyntaxNode
92
+ def eval
93
+ @value ||= Kernel.eval(text_value)
94
+ end
95
+ end
96
+
97
+ class Identifier < Treetop::Runtime::SyntaxNode
98
+ def eval
99
+ Runtime::Identifier.new(text_value)
100
+ end
101
+ end
102
+
103
+ end
104
+ end
105
+
@@ -0,0 +1,1081 @@
1
+ module Heist
2
+ module Scheme
3
+ include Treetop::Runtime
4
+
5
+ def root
6
+ @root || :program
7
+ end
8
+
9
+ def _nt_program
10
+ start_index = index
11
+ if node_cache[:program].has_key?(index)
12
+ cached = node_cache[:program][index]
13
+ @index = cached.interval.end if cached
14
+ return cached
15
+ end
16
+
17
+ s0, i0 = [], index
18
+ loop do
19
+ r1 = _nt_cell
20
+ if r1
21
+ s0 << r1
22
+ else
23
+ break
24
+ end
25
+ end
26
+ r0 = Program.new(input, i0...index, s0)
27
+
28
+ node_cache[:program][start_index] = r0
29
+
30
+ return r0
31
+ end
32
+
33
+ module Cell0
34
+ def ignore
35
+ elements[0]
36
+ end
37
+
38
+ def ignore
39
+ elements[2]
40
+ end
41
+
42
+ def ignore
43
+ elements[4]
44
+ end
45
+ end
46
+
47
+ def _nt_cell
48
+ start_index = index
49
+ if node_cache[:cell].has_key?(index)
50
+ cached = node_cache[:cell][index]
51
+ @index = cached.interval.end if cached
52
+ return cached
53
+ end
54
+
55
+ i0, s0 = index, []
56
+ r1 = _nt_ignore
57
+ s0 << r1
58
+ if r1
59
+ r3 = _nt_quote
60
+ if r3
61
+ r2 = r3
62
+ else
63
+ r2 = SyntaxNode.new(input, index...index)
64
+ end
65
+ s0 << r2
66
+ if r2
67
+ r4 = _nt_ignore
68
+ s0 << r4
69
+ if r4
70
+ i5 = index
71
+ r6 = _nt_list
72
+ if r6
73
+ r5 = r6
74
+ else
75
+ r7 = _nt_atom
76
+ if r7
77
+ r5 = r7
78
+ else
79
+ self.index = i5
80
+ r5 = nil
81
+ end
82
+ end
83
+ s0 << r5
84
+ if r5
85
+ r8 = _nt_ignore
86
+ s0 << r8
87
+ end
88
+ end
89
+ end
90
+ end
91
+ if s0.last
92
+ r0 = (Cell).new(input, i0...index, s0)
93
+ r0.extend(Cell0)
94
+ else
95
+ self.index = i0
96
+ r0 = nil
97
+ end
98
+
99
+ node_cache[:cell][start_index] = r0
100
+
101
+ return r0
102
+ end
103
+
104
+ def _nt_quote
105
+ start_index = index
106
+ if node_cache[:quote].has_key?(index)
107
+ cached = node_cache[:quote][index]
108
+ @index = cached.interval.end if cached
109
+ return cached
110
+ end
111
+
112
+ i0 = index
113
+ if input.index("'", index) == index
114
+ r1 = (SyntaxNode).new(input, index...(index + 1))
115
+ @index += 1
116
+ else
117
+ terminal_parse_failure("'")
118
+ r1 = nil
119
+ end
120
+ if r1
121
+ r0 = r1
122
+ else
123
+ if input.index("`", index) == index
124
+ r2 = (SyntaxNode).new(input, index...(index + 1))
125
+ @index += 1
126
+ else
127
+ terminal_parse_failure("`")
128
+ r2 = nil
129
+ end
130
+ if r2
131
+ r0 = r2
132
+ else
133
+ if input.index(",@", index) == index
134
+ r3 = (SyntaxNode).new(input, index...(index + 2))
135
+ @index += 2
136
+ else
137
+ terminal_parse_failure(",@")
138
+ r3 = nil
139
+ end
140
+ if r3
141
+ r0 = r3
142
+ else
143
+ if input.index(",", index) == index
144
+ r4 = (SyntaxNode).new(input, index...(index + 1))
145
+ @index += 1
146
+ else
147
+ terminal_parse_failure(",")
148
+ r4 = nil
149
+ end
150
+ if r4
151
+ r0 = r4
152
+ else
153
+ self.index = i0
154
+ r0 = nil
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ node_cache[:quote][start_index] = r0
161
+
162
+ return r0
163
+ end
164
+
165
+ module List0
166
+ end
167
+
168
+ module List1
169
+ end
170
+
171
+ def _nt_list
172
+ start_index = index
173
+ if node_cache[:list].has_key?(index)
174
+ cached = node_cache[:list][index]
175
+ @index = cached.interval.end if cached
176
+ return cached
177
+ end
178
+
179
+ i0 = index
180
+ i1, s1 = index, []
181
+ if input.index("(", index) == index
182
+ r2 = (SyntaxNode).new(input, index...(index + 1))
183
+ @index += 1
184
+ else
185
+ terminal_parse_failure("(")
186
+ r2 = nil
187
+ end
188
+ s1 << r2
189
+ if r2
190
+ s3, i3 = [], index
191
+ loop do
192
+ r4 = _nt_cell
193
+ if r4
194
+ s3 << r4
195
+ else
196
+ break
197
+ end
198
+ end
199
+ r3 = SyntaxNode.new(input, i3...index, s3)
200
+ s1 << r3
201
+ if r3
202
+ if input.index(")", index) == index
203
+ r5 = (SyntaxNode).new(input, index...(index + 1))
204
+ @index += 1
205
+ else
206
+ terminal_parse_failure(")")
207
+ r5 = nil
208
+ end
209
+ s1 << r5
210
+ end
211
+ end
212
+ if s1.last
213
+ r1 = (SyntaxNode).new(input, i1...index, s1)
214
+ r1.extend(List0)
215
+ else
216
+ self.index = i1
217
+ r1 = nil
218
+ end
219
+ if r1
220
+ r0 = r1
221
+ r0.extend(List)
222
+ else
223
+ i6, s6 = index, []
224
+ if input.index("[", index) == index
225
+ r7 = (SyntaxNode).new(input, index...(index + 1))
226
+ @index += 1
227
+ else
228
+ terminal_parse_failure("[")
229
+ r7 = nil
230
+ end
231
+ s6 << r7
232
+ if r7
233
+ s8, i8 = [], index
234
+ loop do
235
+ r9 = _nt_cell
236
+ if r9
237
+ s8 << r9
238
+ else
239
+ break
240
+ end
241
+ end
242
+ r8 = SyntaxNode.new(input, i8...index, s8)
243
+ s6 << r8
244
+ if r8
245
+ if input.index("]", index) == index
246
+ r10 = (SyntaxNode).new(input, index...(index + 1))
247
+ @index += 1
248
+ else
249
+ terminal_parse_failure("]")
250
+ r10 = nil
251
+ end
252
+ s6 << r10
253
+ end
254
+ end
255
+ if s6.last
256
+ r6 = (SyntaxNode).new(input, i6...index, s6)
257
+ r6.extend(List1)
258
+ else
259
+ self.index = i6
260
+ r6 = nil
261
+ end
262
+ if r6
263
+ r0 = r6
264
+ r0.extend(List)
265
+ else
266
+ self.index = i0
267
+ r0 = nil
268
+ end
269
+ end
270
+
271
+ node_cache[:list][start_index] = r0
272
+
273
+ return r0
274
+ end
275
+
276
+ def _nt_atom
277
+ start_index = index
278
+ if node_cache[:atom].has_key?(index)
279
+ cached = node_cache[:atom][index]
280
+ @index = cached.interval.end if cached
281
+ return cached
282
+ end
283
+
284
+ i0 = index
285
+ r1 = _nt_datum
286
+ if r1
287
+ r0 = r1
288
+ else
289
+ r2 = _nt_identifier
290
+ if r2
291
+ r0 = r2
292
+ else
293
+ self.index = i0
294
+ r0 = nil
295
+ end
296
+ end
297
+
298
+ node_cache[:atom][start_index] = r0
299
+
300
+ return r0
301
+ end
302
+
303
+ module Datum0
304
+ end
305
+
306
+ module Datum1
307
+ end
308
+
309
+ def _nt_datum
310
+ start_index = index
311
+ if node_cache[:datum].has_key?(index)
312
+ cached = node_cache[:datum][index]
313
+ @index = cached.interval.end if cached
314
+ return cached
315
+ end
316
+
317
+ i0, s0 = index, []
318
+ i1 = index
319
+ r2 = _nt_boolean
320
+ if r2
321
+ r1 = r2
322
+ else
323
+ r3 = _nt_number
324
+ if r3
325
+ r1 = r3
326
+ else
327
+ r4 = _nt_string
328
+ if r4
329
+ r1 = r4
330
+ else
331
+ self.index = i1
332
+ r1 = nil
333
+ end
334
+ end
335
+ end
336
+ s0 << r1
337
+ if r1
338
+ i5 = index
339
+ i6, s6 = index, []
340
+ i7 = index
341
+ r8 = _nt_delimiter
342
+ if r8
343
+ r7 = nil
344
+ else
345
+ self.index = i7
346
+ r7 = SyntaxNode.new(input, index...index)
347
+ end
348
+ s6 << r7
349
+ if r7
350
+ if index < input_length
351
+ r9 = (SyntaxNode).new(input, index...(index + 1))
352
+ @index += 1
353
+ else
354
+ terminal_parse_failure("any character")
355
+ r9 = nil
356
+ end
357
+ s6 << r9
358
+ end
359
+ if s6.last
360
+ r6 = (SyntaxNode).new(input, i6...index, s6)
361
+ r6.extend(Datum0)
362
+ else
363
+ self.index = i6
364
+ r6 = nil
365
+ end
366
+ if r6
367
+ r5 = nil
368
+ else
369
+ self.index = i5
370
+ r5 = SyntaxNode.new(input, index...index)
371
+ end
372
+ s0 << r5
373
+ end
374
+ if s0.last
375
+ r0 = (Datum).new(input, i0...index, s0)
376
+ r0.extend(Datum1)
377
+ else
378
+ self.index = i0
379
+ r0 = nil
380
+ end
381
+
382
+ node_cache[:datum][start_index] = r0
383
+
384
+ return r0
385
+ end
386
+
387
+ def _nt_boolean
388
+ start_index = index
389
+ if node_cache[:boolean].has_key?(index)
390
+ cached = node_cache[:boolean][index]
391
+ @index = cached.interval.end if cached
392
+ return cached
393
+ end
394
+
395
+ i0 = index
396
+ if input.index("#t", index) == index
397
+ r1 = (SyntaxNode).new(input, index...(index + 2))
398
+ @index += 2
399
+ else
400
+ terminal_parse_failure("#t")
401
+ r1 = nil
402
+ end
403
+ if r1
404
+ r0 = r1
405
+ r0.extend(Boolean)
406
+ else
407
+ if input.index("#f", index) == index
408
+ r2 = (SyntaxNode).new(input, index...(index + 2))
409
+ @index += 2
410
+ else
411
+ terminal_parse_failure("#f")
412
+ r2 = nil
413
+ end
414
+ if r2
415
+ r0 = r2
416
+ r0.extend(Boolean)
417
+ else
418
+ self.index = i0
419
+ r0 = nil
420
+ end
421
+ end
422
+
423
+ node_cache[:boolean][start_index] = r0
424
+
425
+ return r0
426
+ end
427
+
428
+ def _nt_number
429
+ start_index = index
430
+ if node_cache[:number].has_key?(index)
431
+ cached = node_cache[:number][index]
432
+ @index = cached.interval.end if cached
433
+ return cached
434
+ end
435
+
436
+ i0 = index
437
+ r1 = _nt_complex
438
+ if r1
439
+ r0 = r1
440
+ else
441
+ r2 = _nt_real
442
+ if r2
443
+ r0 = r2
444
+ else
445
+ r3 = _nt_rational
446
+ if r3
447
+ r0 = r3
448
+ else
449
+ r4 = _nt_integer
450
+ if r4
451
+ r0 = r4
452
+ else
453
+ self.index = i0
454
+ r0 = nil
455
+ end
456
+ end
457
+ end
458
+ end
459
+
460
+ node_cache[:number][start_index] = r0
461
+
462
+ return r0
463
+ end
464
+
465
+ module Complex0
466
+ def real
467
+ elements[0]
468
+ end
469
+
470
+ def real
471
+ elements[2]
472
+ end
473
+
474
+ end
475
+
476
+ def _nt_complex
477
+ start_index = index
478
+ if node_cache[:complex].has_key?(index)
479
+ cached = node_cache[:complex][index]
480
+ @index = cached.interval.end if cached
481
+ return cached
482
+ end
483
+
484
+ i0, s0 = index, []
485
+ r1 = _nt_real
486
+ s0 << r1
487
+ if r1
488
+ if input.index("+", index) == index
489
+ r2 = (SyntaxNode).new(input, index...(index + 1))
490
+ @index += 1
491
+ else
492
+ terminal_parse_failure("+")
493
+ r2 = nil
494
+ end
495
+ s0 << r2
496
+ if r2
497
+ r3 = _nt_real
498
+ s0 << r3
499
+ if r3
500
+ if input.index("i", index) == index
501
+ r4 = (SyntaxNode).new(input, index...(index + 1))
502
+ @index += 1
503
+ else
504
+ terminal_parse_failure("i")
505
+ r4 = nil
506
+ end
507
+ s0 << r4
508
+ end
509
+ end
510
+ end
511
+ if s0.last
512
+ r0 = (Complex).new(input, i0...index, s0)
513
+ r0.extend(Complex0)
514
+ else
515
+ self.index = i0
516
+ r0 = nil
517
+ end
518
+
519
+ node_cache[:complex][start_index] = r0
520
+
521
+ return r0
522
+ end
523
+
524
+ module Real0
525
+ end
526
+
527
+ module Real1
528
+ def integer
529
+ elements[0]
530
+ end
531
+
532
+ end
533
+
534
+ def _nt_real
535
+ start_index = index
536
+ if node_cache[:real].has_key?(index)
537
+ cached = node_cache[:real][index]
538
+ @index = cached.interval.end if cached
539
+ return cached
540
+ end
541
+
542
+ i0, s0 = index, []
543
+ r1 = _nt_integer
544
+ s0 << r1
545
+ if r1
546
+ i2, s2 = index, []
547
+ if input.index(".", index) == index
548
+ r3 = (SyntaxNode).new(input, index...(index + 1))
549
+ @index += 1
550
+ else
551
+ terminal_parse_failure(".")
552
+ r3 = nil
553
+ end
554
+ s2 << r3
555
+ if r3
556
+ s4, i4 = [], index
557
+ loop do
558
+ r5 = _nt_digit
559
+ if r5
560
+ s4 << r5
561
+ else
562
+ break
563
+ end
564
+ end
565
+ if s4.empty?
566
+ self.index = i4
567
+ r4 = nil
568
+ else
569
+ r4 = SyntaxNode.new(input, i4...index, s4)
570
+ end
571
+ s2 << r4
572
+ end
573
+ if s2.last
574
+ r2 = (SyntaxNode).new(input, i2...index, s2)
575
+ r2.extend(Real0)
576
+ else
577
+ self.index = i2
578
+ r2 = nil
579
+ end
580
+ s0 << r2
581
+ end
582
+ if s0.last
583
+ r0 = (Real).new(input, i0...index, s0)
584
+ r0.extend(Real1)
585
+ else
586
+ self.index = i0
587
+ r0 = nil
588
+ end
589
+
590
+ node_cache[:real][start_index] = r0
591
+
592
+ return r0
593
+ end
594
+
595
+ module Rational0
596
+ def numerator
597
+ elements[0]
598
+ end
599
+
600
+ def denominator
601
+ elements[2]
602
+ end
603
+ end
604
+
605
+ def _nt_rational
606
+ start_index = index
607
+ if node_cache[:rational].has_key?(index)
608
+ cached = node_cache[:rational][index]
609
+ @index = cached.interval.end if cached
610
+ return cached
611
+ end
612
+
613
+ i0, s0 = index, []
614
+ r1 = _nt_integer
615
+ s0 << r1
616
+ if r1
617
+ if input.index("/", index) == index
618
+ r2 = (SyntaxNode).new(input, index...(index + 1))
619
+ @index += 1
620
+ else
621
+ terminal_parse_failure("/")
622
+ r2 = nil
623
+ end
624
+ s0 << r2
625
+ if r2
626
+ r3 = _nt_integer
627
+ s0 << r3
628
+ end
629
+ end
630
+ if s0.last
631
+ r0 = (Rational).new(input, i0...index, s0)
632
+ r0.extend(Rational0)
633
+ else
634
+ self.index = i0
635
+ r0 = nil
636
+ end
637
+
638
+ node_cache[:rational][start_index] = r0
639
+
640
+ return r0
641
+ end
642
+
643
+ module Integer0
644
+ end
645
+
646
+ module Integer1
647
+ end
648
+
649
+ def _nt_integer
650
+ start_index = index
651
+ if node_cache[:integer].has_key?(index)
652
+ cached = node_cache[:integer][index]
653
+ @index = cached.interval.end if cached
654
+ return cached
655
+ end
656
+
657
+ i0, s0 = index, []
658
+ if input.index("-", index) == index
659
+ r2 = (SyntaxNode).new(input, index...(index + 1))
660
+ @index += 1
661
+ else
662
+ terminal_parse_failure("-")
663
+ r2 = nil
664
+ end
665
+ if r2
666
+ r1 = r2
667
+ else
668
+ r1 = SyntaxNode.new(input, index...index)
669
+ end
670
+ s0 << r1
671
+ if r1
672
+ i3 = index
673
+ if input.index("0", index) == index
674
+ r4 = (SyntaxNode).new(input, index...(index + 1))
675
+ @index += 1
676
+ else
677
+ terminal_parse_failure("0")
678
+ r4 = nil
679
+ end
680
+ if r4
681
+ r3 = r4
682
+ else
683
+ i5, s5 = index, []
684
+ if input.index(Regexp.new('[1-9]'), index) == index
685
+ r6 = (SyntaxNode).new(input, index...(index + 1))
686
+ @index += 1
687
+ else
688
+ r6 = nil
689
+ end
690
+ s5 << r6
691
+ if r6
692
+ s7, i7 = [], index
693
+ loop do
694
+ r8 = _nt_digit
695
+ if r8
696
+ s7 << r8
697
+ else
698
+ break
699
+ end
700
+ end
701
+ r7 = SyntaxNode.new(input, i7...index, s7)
702
+ s5 << r7
703
+ end
704
+ if s5.last
705
+ r5 = (SyntaxNode).new(input, i5...index, s5)
706
+ r5.extend(Integer0)
707
+ else
708
+ self.index = i5
709
+ r5 = nil
710
+ end
711
+ if r5
712
+ r3 = r5
713
+ else
714
+ self.index = i3
715
+ r3 = nil
716
+ end
717
+ end
718
+ s0 << r3
719
+ end
720
+ if s0.last
721
+ r0 = (Integer).new(input, i0...index, s0)
722
+ r0.extend(Integer1)
723
+ else
724
+ self.index = i0
725
+ r0 = nil
726
+ end
727
+
728
+ node_cache[:integer][start_index] = r0
729
+
730
+ return r0
731
+ end
732
+
733
+ module String0
734
+ end
735
+
736
+ def _nt_string
737
+ start_index = index
738
+ if node_cache[:string].has_key?(index)
739
+ cached = node_cache[:string][index]
740
+ @index = cached.interval.end if cached
741
+ return cached
742
+ end
743
+
744
+ i0, s0 = index, []
745
+ if input.index('"', index) == index
746
+ r1 = (SyntaxNode).new(input, index...(index + 1))
747
+ @index += 1
748
+ else
749
+ terminal_parse_failure('"')
750
+ r1 = nil
751
+ end
752
+ s0 << r1
753
+ if r1
754
+ s2, i2 = [], index
755
+ loop do
756
+ i3 = index
757
+ if input.index('\\"', index) == index
758
+ r4 = (SyntaxNode).new(input, index...(index + 2))
759
+ @index += 2
760
+ else
761
+ terminal_parse_failure('\\"')
762
+ r4 = nil
763
+ end
764
+ if r4
765
+ r3 = r4
766
+ else
767
+ if input.index(Regexp.new('[^"]'), index) == index
768
+ r5 = (SyntaxNode).new(input, index...(index + 1))
769
+ @index += 1
770
+ else
771
+ r5 = nil
772
+ end
773
+ if r5
774
+ r3 = r5
775
+ else
776
+ self.index = i3
777
+ r3 = nil
778
+ end
779
+ end
780
+ if r3
781
+ s2 << r3
782
+ else
783
+ break
784
+ end
785
+ end
786
+ r2 = SyntaxNode.new(input, i2...index, s2)
787
+ s0 << r2
788
+ if r2
789
+ if input.index('"', index) == index
790
+ r6 = (SyntaxNode).new(input, index...(index + 1))
791
+ @index += 1
792
+ else
793
+ terminal_parse_failure('"')
794
+ r6 = nil
795
+ end
796
+ s0 << r6
797
+ end
798
+ end
799
+ if s0.last
800
+ r0 = (String).new(input, i0...index, s0)
801
+ r0.extend(String0)
802
+ else
803
+ self.index = i0
804
+ r0 = nil
805
+ end
806
+
807
+ node_cache[:string][start_index] = r0
808
+
809
+ return r0
810
+ end
811
+
812
+ module Identifier0
813
+ end
814
+
815
+ def _nt_identifier
816
+ start_index = index
817
+ if node_cache[:identifier].has_key?(index)
818
+ cached = node_cache[:identifier][index]
819
+ @index = cached.interval.end if cached
820
+ return cached
821
+ end
822
+
823
+ s0, i0 = [], index
824
+ loop do
825
+ i1, s1 = index, []
826
+ i2 = index
827
+ r3 = _nt_delimiter
828
+ if r3
829
+ r2 = nil
830
+ else
831
+ self.index = i2
832
+ r2 = SyntaxNode.new(input, index...index)
833
+ end
834
+ s1 << r2
835
+ if r2
836
+ if index < input_length
837
+ r4 = (SyntaxNode).new(input, index...(index + 1))
838
+ @index += 1
839
+ else
840
+ terminal_parse_failure("any character")
841
+ r4 = nil
842
+ end
843
+ s1 << r4
844
+ end
845
+ if s1.last
846
+ r1 = (SyntaxNode).new(input, i1...index, s1)
847
+ r1.extend(Identifier0)
848
+ else
849
+ self.index = i1
850
+ r1 = nil
851
+ end
852
+ if r1
853
+ s0 << r1
854
+ else
855
+ break
856
+ end
857
+ end
858
+ if s0.empty?
859
+ self.index = i0
860
+ r0 = nil
861
+ else
862
+ r0 = Identifier.new(input, i0...index, s0)
863
+ end
864
+
865
+ node_cache[:identifier][start_index] = r0
866
+
867
+ return r0
868
+ end
869
+
870
+ def _nt_digit
871
+ start_index = index
872
+ if node_cache[:digit].has_key?(index)
873
+ cached = node_cache[:digit][index]
874
+ @index = cached.interval.end if cached
875
+ return cached
876
+ end
877
+
878
+ if input.index(Regexp.new('[0-9]'), index) == index
879
+ r0 = (SyntaxNode).new(input, index...(index + 1))
880
+ @index += 1
881
+ else
882
+ r0 = nil
883
+ end
884
+
885
+ node_cache[:digit][start_index] = r0
886
+
887
+ return r0
888
+ end
889
+
890
+ def _nt_delimiter
891
+ start_index = index
892
+ if node_cache[:delimiter].has_key?(index)
893
+ cached = node_cache[:delimiter][index]
894
+ @index = cached.interval.end if cached
895
+ return cached
896
+ end
897
+
898
+ i0 = index
899
+ if input.index(Regexp.new('[\\(\\)\\[\\]]'), index) == index
900
+ r1 = (SyntaxNode).new(input, index...(index + 1))
901
+ @index += 1
902
+ else
903
+ r1 = nil
904
+ end
905
+ if r1
906
+ r0 = r1
907
+ else
908
+ r2 = _nt_space
909
+ if r2
910
+ r0 = r2
911
+ else
912
+ self.index = i0
913
+ r0 = nil
914
+ end
915
+ end
916
+
917
+ node_cache[:delimiter][start_index] = r0
918
+
919
+ return r0
920
+ end
921
+
922
+ def _nt_space
923
+ start_index = index
924
+ if node_cache[:space].has_key?(index)
925
+ cached = node_cache[:space][index]
926
+ @index = cached.interval.end if cached
927
+ return cached
928
+ end
929
+
930
+ if input.index(Regexp.new('[\\s\\n\\r\\t]'), index) == index
931
+ r0 = (SyntaxNode).new(input, index...(index + 1))
932
+ @index += 1
933
+ else
934
+ r0 = nil
935
+ end
936
+
937
+ node_cache[:space][start_index] = r0
938
+
939
+ return r0
940
+ end
941
+
942
+ module Ignore0
943
+ end
944
+
945
+ def _nt_ignore
946
+ start_index = index
947
+ if node_cache[:ignore].has_key?(index)
948
+ cached = node_cache[:ignore][index]
949
+ @index = cached.interval.end if cached
950
+ return cached
951
+ end
952
+
953
+ i0, s0 = index, []
954
+ s1, i1 = [], index
955
+ loop do
956
+ r2 = _nt_space
957
+ if r2
958
+ s1 << r2
959
+ else
960
+ break
961
+ end
962
+ end
963
+ r1 = SyntaxNode.new(input, i1...index, s1)
964
+ s0 << r1
965
+ if r1
966
+ r4 = _nt_comment
967
+ if r4
968
+ r3 = r4
969
+ else
970
+ r3 = SyntaxNode.new(input, index...index)
971
+ end
972
+ s0 << r3
973
+ end
974
+ if s0.last
975
+ r0 = (SyntaxNode).new(input, i0...index, s0)
976
+ r0.extend(Ignore0)
977
+ else
978
+ self.index = i0
979
+ r0 = nil
980
+ end
981
+
982
+ node_cache[:ignore][start_index] = r0
983
+
984
+ return r0
985
+ end
986
+
987
+ module Comment0
988
+ end
989
+
990
+ module Comment1
991
+ def ignore
992
+ elements[2]
993
+ end
994
+ end
995
+
996
+ def _nt_comment
997
+ start_index = index
998
+ if node_cache[:comment].has_key?(index)
999
+ cached = node_cache[:comment][index]
1000
+ @index = cached.interval.end if cached
1001
+ return cached
1002
+ end
1003
+
1004
+ i0, s0 = index, []
1005
+ if input.index(";", index) == index
1006
+ r1 = (SyntaxNode).new(input, index...(index + 1))
1007
+ @index += 1
1008
+ else
1009
+ terminal_parse_failure(";")
1010
+ r1 = nil
1011
+ end
1012
+ s0 << r1
1013
+ if r1
1014
+ s2, i2 = [], index
1015
+ loop do
1016
+ i3, s3 = index, []
1017
+ i4 = index
1018
+ if input.index(Regexp.new('[\\n\\r]'), index) == index
1019
+ r5 = (SyntaxNode).new(input, index...(index + 1))
1020
+ @index += 1
1021
+ else
1022
+ r5 = nil
1023
+ end
1024
+ if r5
1025
+ r4 = nil
1026
+ else
1027
+ self.index = i4
1028
+ r4 = SyntaxNode.new(input, index...index)
1029
+ end
1030
+ s3 << r4
1031
+ if r4
1032
+ if index < input_length
1033
+ r6 = (SyntaxNode).new(input, index...(index + 1))
1034
+ @index += 1
1035
+ else
1036
+ terminal_parse_failure("any character")
1037
+ r6 = nil
1038
+ end
1039
+ s3 << r6
1040
+ end
1041
+ if s3.last
1042
+ r3 = (SyntaxNode).new(input, i3...index, s3)
1043
+ r3.extend(Comment0)
1044
+ else
1045
+ self.index = i3
1046
+ r3 = nil
1047
+ end
1048
+ if r3
1049
+ s2 << r3
1050
+ else
1051
+ break
1052
+ end
1053
+ end
1054
+ r2 = SyntaxNode.new(input, i2...index, s2)
1055
+ s0 << r2
1056
+ if r2
1057
+ r7 = _nt_ignore
1058
+ s0 << r7
1059
+ end
1060
+ end
1061
+ if s0.last
1062
+ r0 = (SyntaxNode).new(input, i0...index, s0)
1063
+ r0.extend(Comment1)
1064
+ else
1065
+ self.index = i0
1066
+ r0 = nil
1067
+ end
1068
+
1069
+ node_cache[:comment][start_index] = r0
1070
+
1071
+ return r0
1072
+ end
1073
+
1074
+ end
1075
+
1076
+ class SchemeParser < Treetop::Runtime::CompiledParser
1077
+ include Scheme
1078
+ end
1079
+
1080
+ end
1081
+