-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec-asc.sld
254 lines (227 loc) · 8.49 KB
/
rec-asc.sld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(define-library (rec-asc)
(import (scheme base)
(chibi match)
(utils))
(export rec-asc-parse)
(begin
(define (rec-asc-parse tokens)
(if (null? tokens)
'()
(car (state0 tokens))))
(define (state0 tokens)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power))
((term tl) (state3 tl factor))
((expr tl) (state2 tl term)))
(state1 tl expr))))
(define (state1 tokens node)
(let loop ((tokens tokens)
(node node))
(match tokens
(() (list node tokens))
(('PLUS tl ...)
(match-let (((node tokens) (state10 tl node)))
(loop tokens node)))
(('MINUS tl ...)
(match-let (((node tokens) (state11 tl node)))
(loop tokens node)))
(_ (syntax-err)))))
(define (state2 tokens node)
(let loop ((tokens tokens)
(node node))
(match tokens
(() (list node tokens))
(('STAR tl ...)
(match-let (((node tokens) (state12 tl node)))
(loop tokens node)))
(('SLASH tl ...)
(match-let (((node tokens) (state13 tl node)))
(loop tokens node)))
(('PERCENT tl ...)
(match-let (((node tokens) (state14 tl node)))
(loop tokens node)))
(((not (or (? number? hd) 'LPAREN 'CARET)) tl ...)
(list node tokens))
(_ (syntax-err)))))
(define (state3 tokens node)
(list node tokens))
(define (state4 tokens node)
(match tokens
(('CARET tl ...) (state15 tl node))
(_ (list node tokens))))
(define state5 state3)
(define (state6 tokens)
(list (car tokens) (cdr tokens)))
(define (state7 tokens)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base)))
(state16 tl power))))
(define (state8 tokens)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base)))
(state17 tl power))))
(define (state9 tokens)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power))
((term tl) (state3 tl factor))
((expr tl) (state2 tl term)))
(state18 tl expr))))
(define (state10 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power))
((term tl) (state3 tl factor)))
(state19 tl node term))))
(define (state11 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power))
((term tl) (state3 tl factor)))
(state20 tl node term))))
(define (state12 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power)))
(state21 tl node factor))))
(define (state13 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power)))
(state22 tl node factor))))
(define (state14 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power)))
(state23 tl node factor))))
(define (state15 tokens node)
(let ((node-tl
(match tokens
(((? number? hd) tl ...) (state6 tokens))
(('PLUS tl ...) (state7 tl))
(('MINUS tl ...) (state8 tl))
(('LPAREN tl ...) (state9 tl))
(_ (syntax-err)))))
(match-let* (((base tl) node-tl)
((power tl) (state5 tl base))
((factor tl) (state4 tl power)))
(state24 tl node factor))))
(define (state16 tokens node)
(list `(+ ,node) tokens))
(define (state17 tokens node)
(list `(- ,node) tokens))
(define (state18 tokens node)
(let loop ((tokens tokens)
(node node))
(match tokens
(('PLUS tl ...)
(match-let (((node tokens) (state10 tl node)))
(loop tokens node)))
(('MINUS tl ...)
(match-let (((node tokens) (state11 tl node)))
(loop tokens node)))
(('RPAREN tl ...)
(state25 tl node))
(_ (syntax-err)))))
(define (state19 tokens left right)
(let loop ((tokens tokens)
(right right))
(match tokens
(('STAR tl ...)
(match-let (((right tokens) (state12 tl right)))
(loop tokens right)))
(('SLASH tl ...)
(match-let (((right tokens) (state13 tl right)))
(loop tokens right)))
(('PERCENT tl ...)
(match-let (((right tokens) (state14 tl right)))
(loop tokens right)))
(_ (list `(+ ,left ,right) tokens)))))
(define (state20 tokens left right)
(let loop ((tokens tokens)
(right right))
(match tokens
(('STAR tl ...)
(match-let (((right tokens) (state12 tl right)))
(loop tokens right)))
(('SLASH tl ...)
(match-let (((right tokens) (state13 tl right)))
(loop tokens right)))
(('PERCENT tl ...)
(match-let (((right tokens) (state14 tl right)))
(loop tokens right)))
(_ (list `(- ,left ,right) tokens)))))
(define (state21 tokens left right)
(list `(* ,left ,right) tokens))
(define (state22 tokens left right)
(list `(/ ,left ,right) tokens))
(define (state23 tokens left right)
(list `(remainder ,left ,right) tokens))
(define (state24 tokens left right)
(list `(expt ,left ,right) tokens))
(define state25 state3)
))