-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.ss
423 lines (389 loc) · 13 KB
/
parse.ss
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
;;Problem 4
;(load "chez-init.ss"); This is a parser for simple Scheme expressions, such as those in EOPL, 3.1 thru 3.3.
; You will want to replace this with your parser that includes more expression types, more options for these types, and error-checking.
; Procedures to make the parser a little bit saner.
(define 1st car)
(define 2nd cadr)
(define 3rd caddr)
(define 4th cadddr)
(define (length2? x) (if (list? x) (= (length x) 2) #f) )
(define (valid-lambda-args? args)
(cond
((null? args) #t)
((symbol? (car args)) (valid-lambda-args? (cdr args)))
((pair? (car args)) (if (and (eq? (length (car args)) 2) (eq? (car (car args)) 'ref) (symbol? (cadr (car args))))
(valid-lambda-args? (cdr args))
#f
))
(else #f)
)
)
(define (convert-lambda-arg arg)
(if (pair? arg)
(ref-arg (cadr arg))
(val-arg arg)
)
)
(define (parse-exp datum)
(cond
[(symbol? datum) (var-exp datum)]
[(literal? datum) (lit-exp datum)]
[(list? datum)
(cond
[(eqv? (1st datum) 'begin)
(begin-exp (map parse-exp (cdr datum)))
]
[(eqv? (1st datum) 'while)
(if (< (length datum) 3)
(eopl:error 'parse-exp "too few arguments in while expression: ~s" datum)
(while-exp (parse-exp (2nd datum)) (map parse-exp (cddr datum)))
)
]
[(eqv? (1st datum) 'lambda)
(cond
[(< (length datum) 3)
(eopl:error 'parse-exp "too few arguments in lambda expression: ~s" datum)
]
[(symbol? (2nd datum))
(lambda-exp-vari (2nd datum) (map parse-exp (cddr datum)))
]
[(and (pair? (2nd datum)) (not (list? (2nd datum))) (valid-lambda-args? (all-but-last-elem (2nd datum))))
(lambda-exp-dot (map convert-lambda-arg (all-but-last-elem (2nd datum))) (last-elem (2nd datum)) (map parse-exp (cddr datum)))
]
[(and (or (pair? (2nd datum)) (null? (2nd datum))) (valid-lambda-args? (2nd datum)))
(lambda-exp (map convert-lambda-arg (2nd datum)) (map parse-exp (cddr datum)))
]
[else (eopl:error 'parse-exp "incorrect arguments in lambda expression: ~s" datum)]
)
]
[(eqv? (1st datum) 'if)
(cond
[(= (length datum) 4)
(if-else-exp
(parse-exp (2nd datum))
(parse-exp (3rd datum))
(parse-exp (4th datum))
)
]
[(= (length datum) 3)
(if-exp
(parse-exp (2nd datum))
(parse-exp (3rd datum))
)
]
[else (eopl:error 'parse-exp "if expression of incorrect length: ~s" datum)]
)
]
[(eqv? (1st datum) 'let)
(cond
[(< (length datum) 3)
(eopl:error 'parse-exp "too few arguments in let expression: ~s" datum)
]
[(and ((list-of list?) (2nd datum)) (andmap length2? (2nd datum)))
(let ((vars (map car (2nd datum))))
(if ((list-of symbol?) vars)
(let-exp
vars
(map parse-exp (map 2nd (2nd datum)))
(map parse-exp (cddr datum))
)
(eopl:error 'parse-exp "sytax error in let expression: ~s" datum)
)
)
]
[(symbol? (2nd datum))
(if (< (length datum) 4)
(eopl:error 'parse-exp "too few arguments in named let expression: ~s" datum)
(let ((vars (map car (3rd datum))))
(if ((list-of symbol?) vars)
(let-named-exp
(2nd datum)
vars
(map parse-exp (map 2nd (3rd datum)))
(map parse-exp (cdddr datum))
)
(eopl:error 'parse-exp "sytax error in named let expression: ~s" datum)
)
)
)
]
[else (eopl:error 'parse-exp "incorrect arguments in let expression: ~s" datum)]
)
]
[(eqv? (1st datum) 'letrec)
(cond
[(< (length datum) 3)
(eopl:error 'parse-exp "too few arguments in letrec expression: ~s" datum)
]
[(and ((list-of list?) (2nd datum)) (andmap length2? (2nd datum)))
(let ((vars (map car (2nd datum))))
(if ((list-of symbol?) vars)
(letrec-exp
vars
(map parse-exp (map 2nd (2nd datum)))
(map parse-exp (cddr datum))
)
(eopl:error 'parse-exp "sytax error in letrec expression: ~s" datum)
)
)
]
[else (eopl:error 'parse-exp "incorrect arguments in letrec expression: ~s" datum)]
)
]
[(eqv? (1st datum) 'let*)
(cond
[(< (length datum) 3)
(eopl:error 'parse-exp "too few arguments in let* expression: ~s" datum)
]
[(and ((list-of list?) (2nd datum)) (andmap length2? (2nd datum)))
(let ((vars (map car (2nd datum))))
(if ((list-of symbol?) vars)
(let*-exp
vars
(map parse-exp (map 2nd (2nd datum)))
(map parse-exp (cddr datum))
)
(eopl:error 'parse-exp "sytax error in let* expression: ~s" datum)
)
)
]
[else (eopl:error 'parse-exp "incorrect arguments in let* expression: ~s" datum)]
)
]
[(eqv? (1st datum) 'set!)
(if (= (length datum) 3)
(if (symbol? (2nd datum))
(set!-exp (2nd datum) (parse-exp (3rd datum)))
(eopl:error 'parse-exp "first arg not a sumbol in set!: ~s" datum)
)
(eopl:error 'parse-exp "incorrect number of arguments in set!: ~s" datum)
)
]
[(eqv? (1st datum) 'define)
(if (= (length datum) 3)
(if (symbol? (2nd datum))
(define-exp (2nd datum) (parse-exp (3rd datum)))
(eopl:error 'parse-exp "first arg not a sumbol in define: ~s" datum)
)
(eopl:error 'parse-exp "incorrect number of arguments in define: ~s" datum)
)
]
[(eqv? (1st datum) 'quote)
(if (= (length datum) 2)
(quote-exp (2nd datum))
(eopl:error 'parse-exp "incorrect number of arguments in quote: ~s" datum)
)
]
[else (app-exp (parse-exp (1st datum)) (map parse-exp (cdr datum)))]
)
]
[else (eopl:error 'parse-exp "bad expression: ~s" datum)]
)
)
(define (syntax-expand exp)
(cond
((expression? exp)
(cases expression exp
[var-exp (id) exp]
[lambda-exp-dot (args arglist bodies) (lambda-exp-dot args arglist (map syntax-expand bodies))]
[lambda-exp-vari (arglist bodies) (lambda-exp-vari arglist (map syntax-expand bodies))]
[lambda-exp (args bodies) (lambda-exp args (map syntax-expand bodies))]
[let-exp (vars declarations bodies)
(app-exp (lambda-exp (map val-arg vars) (map syntax-expand bodies)) (map syntax-expand declarations))
]
;[let-named-exp (name vars declarations bodies) (let-named-exp name vars (map syntax-expand declarations) (map syntax-expand bodies))]
[let-named-exp (name vars declarations bodies)
(letrec-exp
(list name)
(list (lambda-exp
(map val-arg vars)
;vars
(map syntax-expand bodies)
))
(list (app-exp (var-exp name) (map syntax-expand declarations)))
)
]
[let*-exp (vars declarations bodies)
(car (fold-right (lambda (curVar curDecl next) (list (syntax-expand (let-exp (list curVar) (list (syntax-expand curDecl)) next))))
(map syntax-expand bodies)
vars
declarations
) )
]
[letrec-exp (vars declarations bodies) (letrec-exp vars (map syntax-expand declarations) (map syntax-expand bodies))]
[begin-exp (bodies) (begin-exp (map syntax-expand bodies))]
;A letrec expression of the form
;(letrec ((var expr) ...) body1 body2 ...)
;may be expressed in terms of let and set! as
;(let ((var #f) ...)
; (let ((temp expr) ...)
; (set! var temp) ...
; (let () body1 body2 ...)))
;[letrec-exp (vars declarations bodies)
; (syntax-expand
; (let-exp
; vars
; (map (lambda (x) (lit-exp #f)) vars)
; (let ((sym (gensym)))
; (list (let-exp
; (map (lambda (x) (concat-symbols x sym)) vars)
; declarations
; (append
; (map (lambda (var) (set!-exp var (concat-symbols var sym))) vars)
; (list (let-exp '() '() bodies))
; )
; ))
; )
; )
; )
;]
;[letrec-exp (vars declarations bodies)
; (syntax-expand
; (let-exp
; vars
; (map (lambda (x) (lit-exp #f)) vars)
; (let-exp
; '(sym)
; (list (app-exp (var-exp 'gensym) '()))
; (list
; (let-exp
; ;(map (lambda (x) (concat-symbols x sym)) vars)
; (app-exp
; (var-exp 'map)
; (list (lambda-exp
; (list 'x)
; (list (app-exp
; (var-exp 'concat-symbols)
; (list (var-exp 'x) (var-exp 'sym)))))
; )) ;vars was before these parens?
; declarations
; (append
; (map (lambda (var) (set!-exp var (app-exp
; (var-exp 'concat-symbols)
; (list (var-exp var) (var-exp 'sym))))) vars)
; (list (let-exp '() '() bodies))
; )
; ))
; )
; )
; )
;]
[set!-exp (var expr) (set!-exp var (syntax-expand expr))]
[define-exp (var expr) (define-exp var (syntax-expand expr))]
[while-exp (test-exp bodies) (while-exp (syntax-expand test-exp) (map syntax-expand bodies))]
[app-exp (func args)
(cases expression func
[var-exp (id)
(case id
;;Using fold for cond and case is less clear, but we only have to traverse the list once time.
;;We gain efficiency.
;;Additionally, nothing here is parsed into its own type, it is all done
;; entirely with the syntax expansion.
['cond
(fold-right
(lambda (cur next)
(cases expression cur
[app-exp (rator expr)
(cases expression rator
[var-exp (id)
(if (eqv? id 'else)
(app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ())))
(if-else-exp (syntax-expand rator) (app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ()))) next)
)
]
[else (if-else-exp (syntax-expand rator) (app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ()))) next)]
)
]
[else (eopl:error 'syntax-expand "incorrect cond statement: ~s" exp)]
)
)
'(app-exp (var-exp void) ())
args
)
]
['case
(if (<= (length args) 1)
(eopl:error 'syntax-expand "incorrect number of args in case statement: ~s" exp)
(let ((test (syntax-expand (car args))))
(fold-right
(lambda (cur next)
(cases expression cur
[app-exp (rator expr)
(cases expression rator
[var-exp (id)
(if (eqv? id 'else)
(app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ())))
(if-else-exp (app-exp (var-exp 'eqv?) (list (syntax-expand rator) test)) (app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ()))) next)
)
]
[app-exp (rat exprr)
(if-else-exp
(syntax-expand
(app-exp (var-exp 'or)
(cons (app-exp (var-exp 'eqv?) (list (syntax-expand rat) test))
(map (lambda (x) (app-exp (var-exp 'eqv?) (list (syntax-expand x) test))) exprr)
)
)
)
(app-exp (lambda-exp '() (map syntax-expand expr)) '((lit-exp ())))
next
)
]
[else (eopl:error 'syntax-expand "incorrect case statement: ~s" exp)]
)
]
[else (eopl:error 'syntax-expand "incorrect case statement: ~s" exp)]
)
)
'(app-exp (var-exp void) ())
(cdr args)
)
)
)
]
;['begin (app-exp (lambda-exp '() (map syntax-expand args)) '((lit-exp ())))]
['and
(if (null? args)
'(lit-exp #t)
(fold-right
(lambda (current next) (if-else-exp (syntax-expand current) next '(lit-exp #f)))
(car (last-pair args))
args
)
)
]
['or
(letrec
((expand-or
(lambda (args)
(if (null? args)
(lit-exp #f)
(let ((expanded-car (car args)) (sym (gensym)))
(let-exp (list sym) (list expanded-car)
(list (if-else-exp (var-exp sym) (var-exp sym) (expand-or (cdr args)))))
)
)
)
))
(expand-or args)
)
]
[else (app-exp func (map syntax-expand args))]
)
]
[else (app-exp (syntax-expand func) (map syntax-expand args))]
)
]
[if-else-exp (test consequent altern) (if-else-exp (syntax-expand test) (syntax-expand consequent) (syntax-expand altern))]
[if-exp (test consequent) (if-exp (syntax-expand test) (syntax-expand consequent))]
[lit-exp (id) exp]
[quote-exp (var) exp]
)
)
((proc-val? exp)
(cases proc-val exp
[else exp]
)
)
)
)