-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp-preprocess.scm
453 lines (406 loc) · 13.1 KB
/
interp-preprocess.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
(set! list (lambda x x))
(define-macro (define header . es)
(if (pair? header)
(list 'set!
(car header)
(list 'lambda (cdr header) (cons 'begin es)))
(list 'set! header (car es))))
;;;; cxr
(define caar (lambda (x) (car (car x))))
(define caaar (lambda (x) (car (car (car x)))))
(define caaaar (lambda (x) (car (car (car (car x))))))
(define cdaaar (lambda (x) (cdr (car (car (car x))))))
(define cdaar (lambda (x) (cdr (car (car x)))))
(define cadaar (lambda (x) (car (cdr (car (car x))))))
(define cddaar (lambda (x) (cdr (cdr (car (car x))))))
(define cdar (lambda (x) (cdr (car x))))
(define cadar (lambda (x) (car (cdr (car x)))))
(define caadar (lambda (x) (car (car (cdr (car x))))))
(define cdadar (lambda (x) (cdr (car (cdr (car x))))))
(define cddar (lambda (x) (cdr (cdr (car x)))))
(define caddar (lambda (x) (car (cdr (cdr (car x))))))
(define cdddar (lambda (x) (cdr (cdr (cdr (car x))))))
(define cadr (lambda (x) (car (cdr x))))
(define caadr (lambda (x) (car (car (cdr x)))))
(define caaadr (lambda (x) (car (car (car (cdr x))))))
(define cdaadr (lambda (x) (cdr (car (car (cdr x))))))
(define cdadr (lambda (x) (cdr (car (cdr x)))))
(define cadadr (lambda (x) (car (cdr (car (cdr x))))))
(define cddadr (lambda (x) (cdr (cdr (car (cdr x))))))
(define cddr (lambda (x) (cdr (cdr x))))
(define caddr (lambda (x) (car (cdr (cdr x)))))
(define caaddr (lambda (x) (car (car (cdr (cdr x))))))
(define cdaddr (lambda (x) (cdr (car (cdr (cdr x))))))
(define cdddr (lambda (x) (cdr (cdr (cdr x)))))
(define cadddr (lambda (x) (car (cdr (cdr (cdr x))))))
(define cddddr (lambda (x) (cdr (cdr (cdr (cdr x))))))
; workaround to make map function private
(define-macro (let bs . es) ((lambda (map)
(set! map (lambda (f xs)
(if (pair? xs)
(cons (f (car xs)) (map f (cdr xs)))
'())))
(if (pair? bs)
(cons (list 'lambda (map car bs) (cons 'begin es))
(map (lambda (b) (cadr b)) bs))
(if (symbol? bs)
(let ((name bs)
(inits (map cadr (car es)))
(params (map car (car es)))
(body (cdr es)))
(list 'letrec
(list
(list name (list 'lambda params (cons 'begin body))))
(cons name inits)))
(cons 'begin es)))
) 0))
(define-macro (let* bindings . body)
(define (let*->let-aux bindings body)
(if (null? (cdr bindings))
(list 'let (list (car bindings)) (cons 'begin body))
(list 'let (list (car bindings)) (let*->let-aux (cdr bindings) body))))
(if (null? bindings)
(cons 'begin body)
(let*->let-aux bindings body)))
(define-macro (cond . clauses)
(if (not (pair? clauses))
#f
(let ((pred (caar clauses))
(conseq (cons 'begin (cdar clauses)))
(clauses (cdr clauses)))
(if (eq? pred 'else)
(if (pair? clauses)
(error "cond-clauses->ifs" "misplaced else" clauses)
conseq)
(list 'if pred
conseq
(cons 'cond clauses))))))
(define-macro (and . es)
(if (pair? es)
(list 'if (car es) (cons 'and (cdr es)) #f)
#t))
(define-macro (or . es)
(if (pair? es)
(list 'if (car es) #t (cons 'or (cdr es)))
#f))
(define-macro (not e)
(list 'if e #f #t))
(define (error . es)
(write "error") (newline)
(write es) (newline)
(exit 1))
(define (fold-right proc init xs)
(if (pair? xs)
(proc (car xs) (fold-right proc init (cdr xs)))
init))
(define (improper-list? xs)
(cond
((null? xs) #f)
((pair? xs) (improper-list? (cdr xs)))
(else #t)))
(define (improper->proper xs)
(cond
((null? xs) xs)
((symbol? xs) (list xs))
((pair? xs) (cons (car xs) (improper->proper (cdr xs))))
(else (error "improper->proper" "unknown object" xs))))
(define (length xs)
(fold-right (lambda (_ acm) (+ 1 acm)) 0 xs))
(let ((map 0)
(fold-right 0)
(append 0))
(define (map f xs)
(if (pair? xs)
(cons (f (car xs)) (map f (cdr xs)))
'()))
(define (fold-right proc init xs)
(if (pair? xs)
(proc (car xs) (fold-right proc init (cdr xs)))
init))
(define (append xs ys)
(fold-right cons ys xs))
(define-macro (letrec bs . es)
(if (pair? bs)
(append
(list 'let (map (lambda (v) (list v 0)) (map car bs))) (append
(map (lambda (var-init) (list 'set! (car var-init) (cadr var-init)))
bs)
es))
(cons 'begin es)))
)
(load "lib/match-defmacro.scm")
(define-macro (match . e)
(compile-match (cons 'match e)))
(define-macro (quasiquote e)
(expand-qq e))
(define (make-env) '())
(define (extend-env xs vs env)
(cond
((symbol? xs)
(cons (list xs vs) env))
((pair? xs)
(if (pair? vs)
(cons (list (car xs) (car vs))
(extend-env (cdr xs) (cdr vs) env))
(error "extend-env" "bad value list" vs)))
(else env)))
(define (maybe-apply-env x env)
(cond
((null? env) #f)
((not (and (pair? env) (pair? (car env))))
(error "maybe-apply-env" "improperly formed env"))
((eq? x (caar env))
(car env))
(else (maybe-apply-env x (cdr env)))))
(define (apply-env x env)
(let ((res (maybe-apply-env x env)))
(if res
(cadr res)
(error "apply-env" "unbound" x (map car env)))))
;;; uniquify
(define (init-uenv)
(extend-env (map car uniquify-methods) (map cadr uniquify-methods) (make-env)))
;; sugars
(define (let*->let e)
(if (null? (cadr e))
(cons 'begin (cddr e))
(let*->let-aux (cadr e) (cddr e))))
(define (let*->let-aux bindings body)
(if (null? (cdr bindings))
(list* 'let (list (car bindings)) body)
(list 'let (list (car bindings)) (let*->let-aux (cdr bindings) body))))
(define (cond->ifs e)
(if (or (not (pair? e)) (null? (cdr e)))
(error "cond" "syntax error" e)
(cond-clauses->ifs (cdr e))))
(define (cond-clauses->ifs clauses)
(if (not (pair? clauses))
#f
(let ((pred (caar clauses))
(conseq (cons 'begin (cdar clauses)))
(clauses (cdr clauses)))
(if (eq? pred 'else)
(if (pair? clauses)
(error "cond-clauses->ifs" "misplaced else" clauses)
conseq)
(list 'if pred
conseq
(cond-clauses->ifs clauses))))))
(define (not->if e)
(list 'if e #f #t))
(define (or->ifs es)
(if (pair? es)
(list 'if (car es) #t (or->ifs (cdr es)))
#f))
(define (and->ifs es)
(if (pair? es)
(list 'if (car es) (and->ifs (cdr es)) #f)
#t))
(define (quote->cons q)
(cond
((null? q) ''())
((pair? q)
`(cons ,(quote->cons (car q))
,(quote->cons (cdr q))))
((symbol? q) `(quote ,q))
(else q)))
(define (expand-qq form)
(cond
((not (pair? form)) (list 'quote form))
((eq? 'quasiquote (car form)) (expand-qq (cadr form)))
((eq? 'unquote (car form)) (cadr form))
(else (qq-list form))))
(define (tail-unquote? form)
(and
(pair? form)
(pair? (cdr form))
(null? (cddr form))
(eq? (car form) 'unquote)))
(define (tail-unquote a)
(cadr a))
(define (qq-list form)
(cond
((null? form) ''())
((not (pair? form))
(list 'list (expand-qq form)))
((tail-unquote? form)
(tail-unquote form))
((and (pair? (car form))
(eq? 'unquote-splicing (caar form)))
(list 'append (cadar form) (qq-list (cdr form))))
(else (list 'append (list 'list (expand-qq (car form))) (qq-list (cdr form))))))
(define (linearize-seq es)
(define (linearize e)
(if (and (pair? e) (eq? (car es) 'begin))
(map linearize-seq (cdr es))
(list e)))
(fold-right (lambda (e acm) (append (linearize e) acm)) '() es))
(define (linearize-begin es)
(let ((es (linearize-seq es)))
(if (= (length es) 1)
(car es)
(cons 'begin es))))
(define (uniquify-each es env)
(if (pair? es)
(cons (uniquify (car es) env)
(uniquify-each (cdr es) env))
'()))
(define (uniquify-seq es env)
(collect-definition (linearize-seq es)
(lambda (defns es)
(define (defn->binding d)
(match d
((define (,fn . ,params) . ,body)
`(,fn (lambda ,params . ,body)))
((define ,x ,e)
`(,x ,e))
(,() (error "defn->binding" "unmatch" d))))
(cond
((pair? defns)
(list (uniquify `(letrec ,(map defn->binding defns) . ,es) env)))
(else (uniquify-each es env))))))
(define uniquify-methods (list
(list 'quote
(lambda (e env) e))
(list 'quasiquote
(lambda (e env) (uniquify (expand-qq (cadr e)) env)))
(list 'begin
(lambda (e env)
(let ((e* (uniquify-seq (cdr e) env)))
(if (and (pair? e*) (not (pair? (cdr e*))))
(car e*)
(cons 'begin e*)))))
(list 'set!
(lambda (e env)
`(set! ,(cadr e) ,(uniquify (caddr e) env))))
(list 'if
(lambda (e env)
(let ((pred (cadr e))
(conseq (caddr e))
(altern (if (pair? (cdddr e)) (cadddr e) #f)))
`(if ,(uniquify pred env)
,(uniquify conseq env)
,(uniquify altern env)))))
(list 'cond
(lambda (e env)
(uniquify (cond-clauses->ifs (cdr e)) env)))
(list 'not
(lambda (e env) (uniquify (not->if (cadr e)) env)))
(list 'and
(lambda (e env) (uniquify (and->ifs (cdr e)) env)))
(list 'or
(lambda (e env) (uniquify (or->ifs (cdr e)) env)))
(list 'match
(lambda (e env) (uniquify (compile-match e) env)))
(list 'case-lambda
(lambda (e env)
(uniquify
`(lambda args
(apply
(cond
,@(map (lambda (clause)
`((,(if (improper-list? (car clause)) '<= '=)
,(length (improper->proper (car clause)))
(length args))
(lambda ,@clause)))
(cdr e))
(else (error args)))
args))
env)))
(list 'lambda
(lambda (e env)
(let* ((params (cadr e))
(body (cddr e))
(params* (improper->proper params))
(env (extend-env params* params* env)))
`(lambda ,params . ,(uniquify-seq body env)))))
(list 'let
(lambda (e env)
(let ((bs (cadr e))
(es (cddr e)))
(cond
((null? bs)
(uniquify (cons 'begin es) env))
((symbol? bs)
(let* ((fn bs)
(inits (car es))
(es (cdr es))
(params (map car inits))
(args (map cadr inits)))
(uniquify
`(letrec ((,fn (lambda ,params . ,es)))
(,fn . ,args))
env)))
(else
(let* ((xs (map car bs))
(inner-env (extend-env xs xs env))
(inits (map (lambda (b) (uniquify (cadr b) env)) bs)))
`((lambda ,xs ,@(uniquify-seq es inner-env)) ,@inits)))))))
(list 'let*
(lambda (e env)
(uniquify (let*->let e) env)))
(list 'letrec
(lambda (e env)
(let ((bs (cadr e))
(es (cddr e)))
(define (simplify-letrec bs es)
(let* ((vars (map car bs))
(inits (map cadr bs)))
;;; unfaithful implementation
`(let ,(map (lambda (v) (list v 0)) vars)
,@(map (lambda (v init) `(set! ,v ,init)) vars inits)
,@es)))
(uniquify (simplify-letrec bs es) env))))
(list 'define-macro
(lambda (e env)
(let* ((header (cadr e))
(body (cddr e))
(env (extend-env (list (car header)) (list (car header)) env)))
`(define-macro ,header . ,(uniquify-seq body env)))
))
(list 'include (lambda _ ''()))
(list 'import (lambda _ ''()))
))
(define (uniquify e env)
(if (not (pair? e))
e
(let ((res (maybe-apply-env (car e) env)))
(if (not (and res (procedure? (cadr res))))
(uniquify-each e env)
((cadr res) e env)))))
(define (definition-variable defn)
(match defn
((define (,fn . ,()) . ,()) fn)
((define ,fn ,()) fn)
(,() (error "definition-variable" "unmatch" defn))))
(define (definition-value defn)
(match defn
((define (,() . ,params) . ,body)
`(lambda ,params . ,body))
((define ,() ,e)
e)
(,() (error "definition-value" "unmatch" defn))))
(define (collect-definition es cont)
(if (pair? es)
(match (car es)
((define . ,())
(collect-definition (cdr es)
(lambda (defns es*)
(cont (cons (car es) defns) es*))))
(,e (collect-definition (cdr es)
(lambda (defns es) (cont defns (cons e es))))))
(cont '() '())))
(define (uniquify-program prog)
(match prog
((begin . ,es)
(collect-definition es
(lambda (defns es)
(let* ((vars (map definition-variable defns))
(vals (map definition-value defns))
(env (extend-env vars vars (init-uenv)))
(vals* (uniquify-each vals env))
(es (uniquify-each es env)))
`(begin
,@(map (lambda (v w) `(set! ,v ,w)) vars vals*)
,@es)))))
(,() (error "uniquify-program" "unmatch" prog))))