-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.scm
335 lines (287 loc) · 8.61 KB
/
parse.scm
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
; Copyright (c) 2011, Peter Brottveit Bock
; Released under the BSD 3-Clause License
; See: https://raw.github.com/peterbb/compiler/master/LICENSE
(define *global-variables* '())
(define (push-global! name var)
(let ((binding (cons name var)))
(set! *global-variables*
(cons binding *global-variables*))
var))
(define (lookup-global-variable! name)
(let ((binding (assq name *global-variables*)))
(if binding
(cdr binding)
(push-global! name (make-global-variable/original (gensym2) name #f)))))
(define (define-global-variable! name)
(let ((binding (assq name *global-variables*)))
(cond ((not binding)
(push-global! name (make-global-variable/original (gensym2) name #t)))
(else
(global-variable-defined! (cdr binding))
(cdr binding)))))
;;; Toplevel parser.
(define (parse cmd*)
(if (and (pair? cmd*)
(list? cmd*))
(let ((res (parse-command* cmd*)))
(for-each (lambda (binding)
(if (not (global-variable-defined? (cdr binding)))
(global->builtin! (cdr binding))))
*global-variables*)
res)
(error "parse: must be list")))
(define (parse-command* cmd*)
(if (null? cmd*)
'()
(let ((cmd (car cmd*))
(cmd* (cdr cmd*)))
(cond ((p:define? cmd)
(cons (parse-define cmd)
(parse-command* cmd*)))
((p:load? cmd)
(parse-command*
(append (io:read-file (cadr cmd))
cmd*)))
(else
(cons (parse-exp cmd '() 'toplevel)
(parse-command* cmd*)))))))
(define (p:load? e) (tagged-list? e 'load))
(define (p:begin? cmd) (tagged-list? cmd 'begin))
(define (p:define? e) (tagged-list? e 'define))
(define (parse-define cmd)
(let* ((name (p:define-name cmd))
(expr (p:define-expr cmd))
(new-name (define-global-variable! name)))
(make-definition new-name (parse-exp expr '() name))))
(define (p:define-name e)
(if (symbol? (cadr e))
(if (= 3 (length e))
(cadr e)
(error "define statement: malformed:" e))
(if (symbol? (caadr e))
(caadr e)
(error "define statement: expected symbol:" e))))
(define (p:define-expr e)
(if (symbol? (cadr e))
(caddr e)
(append (list 'lambda (cdadr e))
(cddr e))))
;;; Parse expression-dispatch
(define (parse-exp exp lex-env debug-name)
(cond ((p:variable? exp) (parse-variable exp lex-env debug-name))
((p:const? exp) (parse-const exp debug-name))
((p:if? exp) (parse-if exp lex-env debug-name))
((p:lambda? exp) (parse-lambda exp lex-env debug-name))
((p:set!? exp) (parse-set! exp lex-env debug-name))
;; syntax begin
((p:let? exp)
(parse-exp (let->exp exp) lex-env debug-name))
((p:let*? exp)
(parse-exp (let*->exp exp) lex-env debug-name))
((p:cond? exp)
(parse-exp (cond->exp exp) lex-env debug-name))
((p:and? exp)
(parse-exp (and->exp exp) lex-env debug-name))
((p:or? exp)
(parse-exp (or->exp exp) lex-env debug-name))
((p:begin? exp)
(parse-exp (begin->exp exp) lex-env debug-name))
((p:quasiquote? exp)
(error "parse-exp: not implemented: quasiquote" exp))
;; error checking
((p:define? exp)
(error "parse-exp: define may not appear here" exp))
;; normal again...
((pair? exp) (parse-app exp lex-env debug-name))
(else
(error "parse-exp: unrecognized:" exp))))
;; ... Variable
(define (p:variable? e) (symbol? e))
(define (parse-variable exp lex-env debug-name)
(let ((binding (assq exp lex-env)))
(if binding
(cdr binding)
(lookup-global-variable! exp))))
;; ... Constants
(define (p:const? exp)
(or (tagged-list? exp 'quote)
(number? exp)
(boolean? exp)
(char? exp)
(string? exp)))
(define (parse-const exp debug-name)
(if (tagged-list? exp 'quote)
(if (= 2 (length exp))
(make-constant (cadr exp))
(error "parse-const: malformed quote:" exp))
(make-constant exp)))
;; ... if
(define (p:if? e)
(and (tagged-list? e 'if)
(if (or (= 3 (length e))
(= 4 (length e)))
#t
(error "malformed if-expression"))))
(define (p:else-part e)
(if (= (length e) 4) (list-ref e 3) #f))
(define (parse-if exp lex-env debug-name)
(make-if (parse-exp (list-ref exp 1) lex-env debug-name)
(parse-exp (list-ref exp 2) lex-env debug-name)
(parse-exp (p:else-part exp) lex-env debug-name)))
;; -- lambda
(define (p:lambda? e) (tagged-list? e 'lambda))
(define (parse-lambda-list l)
(cond ((null? l) '())
((symbol? l)
(list (cons l (make-vararg-variable/original (gensym2) l))))
((and (pair? l)
(symbol? (car l)))
(cons (cons (car l) (make-local-variable/original (gensym2) (car l)))
(parse-lambda-list (cdr l))))
(else
(error "parse-lambda-list: malformed:" l))))
(define (parse-lambda e lex-env debug-name)
(let ((name:var-bindings (parse-lambda-list (cadr e)))
(body (fix-internal-defines (cddr e))))
(make-lambda (map cdr name:var-bindings)
(parse-exp body
(append name:var-bindings lex-env)
debug-name)
debug-name)))
(define (fix-internal-defines e*)
(let* ((internal-defines (p:get-internal-defines e*))
(real-body (p:skip-internal-defines e*))
(internal-names (map p:define-name internal-defines))
(internal-exprs (map p:define-expr internal-defines)))
(if (null? internal-defines)
(if (null? (cdr real-body))
(car real-body)
(cons 'begin real-body))
(make-internal-define internal-names
internal-exprs
real-body))))
(define (make-internal-define names exprs body)
(let ((bindings (map (lambda (v) (list v #f)) names))
(assignments (map (lambda (v e)
(list 'set! v e))
names exprs)))
(append (list 'let bindings)
(append assignments body))))
(define (p:get-internal-defines e*)
(cond ((null? e*)
(error "parse-lambda: not expressions after defines"))
((p:define? (car e*))
(cons (car e*)
(p:get-internal-defines (cdr e*))))
(else '())))
(define (p:skip-internal-defines e*)
(cond ((null? e*)
(error "p:lambda-skip-defs"))
((p:define? (car e*))
(p:skip-internal-defines (cdr e*)))
(else e*)))
;; -- set!
(define (p:set!? e)
(and (tagged-list? e 'set!)
(if (= 3 (length e))
#t
(error "parse-set!: wrong length" e))))
(define (parse-set! exp lex-env debug-name)
(let ((binding (assq (cadr exp) lex-env)))
(if binding
(make-assignment (cdr binding)
(parse-exp (caddr exp) lex-env debug-name))
(make-assignment (lookup-global-variable! (cadr exp))
(parse-exp (caddr exp) lex-env debug-name)))))
;; -- application
(define (parse-app exp* lex-env debug-name)
(if (not (list? exp*))
(error "parse-app: inproper argument list" exp*))
(make-application (parse-exp (car exp*) lex-env debug-name)
(map (lambda (e)
(parse-exp e lex-env debug-name))
(cdr exp*))))
;; -- let
(define (p:let? e) (tagged-list? e 'let))
(define (p:let-var x) (car x))
(define (p:let-exp x) (cadr x))
(define (let->exp exp)
(let ((bindings (cadr exp))
(body (cddr exp)))
(let ((vars (map p:let-var bindings))
(exps (map p:let-exp bindings)))
(cons (cons 'lambda
(cons vars body))
exps))))
;; -- let*
(define (p:let*? e) (tagged-list? e 'let*))
(define (let*->exp e)
(if (null? (cadr e))
(cons 'let (cdr e))
(let* ((binding-list (cadr e))
(body (cddr e))
(b (car binding-list))
(b* (cdr binding-list)))
(list 'let (list b)
(append (list 'let* b*)
body)))))
;; -- cond
(define (p:cond? e) (tagged-list? e 'cond))
(define (cond->exp e)
(cond->exp0 (cdr e)))
(define (cond->exp0 e)
(if (null? e)
'(quote undefined)
(let ((pred (car (car e)))
(seq (cdr (car e)))
(alt (cond->exp0 (cdr e))))
(if (eq? pred 'else)
(cons 'begin seq)
(list 'if
pred
(cons 'begin seq)
alt)))))
;; -- and
(define (p:and? e) (tagged-list? e 'and))
(define (and->exp e)
(and->exp0 (cdr e)))
;;- what is this doing here?
(define (gensym-symbol)
(string->symbol (string-append "tmp"
(number->string (gensym2)))))
(define (and->exp0 e)
(cond ((null? e) #t)
((null? (cdr e)) (car e))
(else
(let ((e (car e))
(e* (cdr e)))
(list 'if e
(cons 'and e*)
#f)))))
;; -- or
(define (p:or? e) (tagged-list? e 'or))
(define (or->exp e)
(or->exp0 (cdr e)))
(define (or->exp0 e)
(cond ((null? e) #f)
((null? (cdr e)) (car e))
(else
(let ((var (gensym-symbol))
(e (car e))
(e* (cdr e)))
(list 'let (list (list var e))
(list 'if var var (cons 'or e*)))))))
;; -- begin
(define (begin->exp exp)
(define (join e1 e2)
(list '(lambda (x y) (y))
e1
(list 'lambda '() e2)))
(define (begin->exp exp)
(if (null? (cdr exp))
(car exp)
(join (car exp)
(begin->exp (cdr exp)))))
(begin->exp (cdr exp)))
;; -- quasi-quote
(define (p:quasiquote? e) (tagged-list? e 'quasiquote))