-
Notifications
You must be signed in to change notification settings - Fork 1
/
unparse.rkt
354 lines (335 loc) · 13.2 KB
/
unparse.rkt
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
#lang racket
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Print an expression or definition.
(require "library.rkt"
"env.rkt"
"r4rsprim.rkt"
"data.rkt"
"free.rkt"
"prim.rkt"
"mutrec.rkt")
(provide pexprs pexprs-renamed pexprs-with-labels
pexprs-with-checks
pexprs-with-no-checks pexprs-with-all-checks
pexpr pname
pfn)
;; pexprs - print a list of expressions without checks, renaming, or labels
;; pexprs-with-checks - print expressions with run-time checks and renaming R4RS primitives
;; pexprs-with-labels - print expressions with labels and renaming R4RS primitives
;; pexprs-with-no-checks - print expressions with renmaming
;; pexprs-with-all-checks - print expressions with all possible checks, and renaming
;;
;; pname - print a name
;; pname* - print a name with context information
;; pfn - print the name of a closure
;; action gets called with keyword, expr where keyword is
;; symbol - for Var
;; '() - for App
;; (symbol) - for other cases
(define unparse
(lambda (e pname action env)
(letrec
([rename (lambda (env x) (or (lookup? env x) (pname x)))]
[pexpr
(lambda (e env)
(match e
[(Define x body)
(action e `(define ,(rename env x) ,(pexpr body env)))]
[(Defstruct tag con pred sels muts)
(action e `(defstruct
,tag
,(rename env con)
,(rename env pred)
,@(map (lambda (sel mut)
(if mut
`(,(rename env sel) ,(rename env mut))
(rename env sel)))
sels
muts)))]
[(Defmacro m)
(action e m)]
[(E: (Var x))
(if (eq? x %internal-apply)
`(|#special| internal-apply)
(action e (rename env x)))]
[(E: (Lam: x e1))
(let* ([names (rename-avoiding x e pname)]
[new-env (extend-env* env x names)])
(action e `(lambda ,names ,(pexpr e1 new-env))))]
[(E: (Vlam: x rest e1))
(let* ([names (rename-avoiding (cons rest x) e pname)]
[new-env (extend-env* env (cons rest x) names)])
(action e `(lambda
,(append (cdr names) (car names))
,(pexpr e1 new-env))))]
[(E: (And exps))
(action e `(and ,@(map (lambda (e) (pexpr e env)) exps)))]
[(E: (Or exps))
(action e `(or ,@(map (lambda (e) (pexpr e env)) exps)))]
[(E: (Begin exps))
(action e `(begin ,@(map (lambda (e) (pexpr e env)) exps)))]
[(E: (Const x))
(action e (cond [(symbol? x) `(quote ,x)]
[(eq? (void) x) '(void)]
[(null? x) '(quote ())]
[else x]))]
[(E: (App fun args))
(action e (map (lambda (e) (pexpr e env)) (cons fun args)))]
; (match fun
; [(E: (Lam: names body))
; (let ([transform (make-E (Let
; (map (lambda (a b)
; (Define a b))
; names
; args)
; body))])
; (pexpr transform env))]
; [else (action e (map (lambda (e) (pexpr e env)) (cons fun args)))])]
[(E: (Let (list b1) (and e2 (E: (Let (list _) _)))))
(let* ([x (Define-name b1)]
[name (car (rename-avoiding (list x) e pname))]
[new-env (extend-env env x name)]
[pb1 `(,name ,(pexpr (Define-exp b1) env))])
(match-let ([(list-rest _ bindings body) (pexpr e2 new-env)])
(action e `(let* (,pb1 ,@bindings) ,@body))))]
[(E: (Let b e2))
(let* ([x (map Define-name b)]
[new-env (extend-env* env x (rename-avoiding x e pname))])
(let ([pb (mapLR
(match-lambda
[(Define x e1) `(,(rename new-env x) ,(pexpr e1 env))])
b)])
(action e `(let ,pb ,(pexpr e2 new-env)))))]
[(E: (Clet names bindings body))
(pexpr (make-E (Let bindings body)) env)]
[(E: (Letr b e2))
(let* ([x (definition-names b)]
[new-env (extend-env* env x (rename-avoiding x e pname))])
(let* ([non-defs (filter (lambda (x) (not (Define? x))) b)]
[pb (mapLR
(match-lambda
[(Define x e1) `(,(rename new-env x) ,(pexpr e1 new-env))])
(filter Define? b))]
[p2 (pexpr e2 new-env)]
[body (if (null? pb) p2 (action e `(letrec ,pb ,p2)))])
(if (null? non-defs)
body
`(let () ,@(mapLR (lambda (e) (pexpr e new-env)) non-defs) ,body))))]
[(E: (If e1 e2 e3))
(let* ([p1 (pexpr e1 env)]
[p2 (pexpr e2 env)]
[p3 (pexpr e3 env)])
(action e `(if ,p1 ,p2 ,p3)))]
[(E: (Letcc x e1))
(let* ([name (car (rename-avoiding (list x) e pname))]
[new-env (extend-env env x name)])
(action e `(lambda ,name ,(pexpr e1 new-env))))]
[(E: (Set! x exp))
(action e `(set! ,(rename env x) ,(pexpr exp env)))]
[err (error 'pexpr "Unknown expr: ~a" err)]))])
(pexpr e env))))
(define unparse*
(lambda (e pname action)
(match e
[(E: (Letr b e2))
(let* ([x (definition-names b)]
[new-env (extend-env* empty-env x (rename-avoiding x e pname))])
(mapLR (lambda (e) (unparse e pname action new-env)) b))]
[(E: (Const (? void?))) '()])))
(define pexpr
(lambda (e)
(unparse e pname (compact-quote (lambda (e p) p)) empty-env)))
(define rename-avoiding
(lambda (names e pname)
(let ([new-name
(lambda (name avoid)
(let loop ([n 1])
(let ([new (symbol-append name '@ n)])
(if (memq new avoid)
(loop (+ 1 n))
new))))]
[free (free-names e)])
(let loop ([names names]
[avoid (append
(map pname (setdiff free names))
'(define
defstruct
defmacro
and
begin
if
lambda
let
let*
letrec
or
delay
set!
letcc))])
(if (null? names)
'()
(let* ([name (car names)]
[printed (pname name)]
[fresh (if (memq printed avoid)
(new-name printed avoid)
printed)])
(cons fresh (loop (cdr names) (cons fresh avoid)))))))))
(define compact-quote
(let ([%Qlist? (lambda (x) (eq? x %Qlist))]
[%Qcons? (lambda (x) (eq? x %Qcons))]
[%Qbox? (lambda (x) (eq? x %Qbox))]
[%Qvector? (lambda (x) (eq? x %Qvector))]
[%Qmerge-list? (lambda (x) (eq? x %Qmerge-list))]
[unkwote (match-lambda
[`(quote ,x) x]
[(? boolean? x) x]
[(? number? x) x]
[(? char? x) x]
[(? string? x) x]
[(? null? x) x]
[(? box? x) x]
[(? vector? x) x])])
(lambda (continue)
(lambda (e p)
(match e
[(E: (App (E: (Var (? %Qlist?))) _))
`(quote ,(map unkwote (cdr p)))]
[(E: (App (E: (Var (? %Qmerge-list?))) _))
`(quote ,(map unkwote (cdr p)))]
[(E: (App (E: (Var (? %Qcons?))) _))
(let* ([a (unkwote (cadr p))]
[d (unkwote (caddr p))])
`(quote ,(cons a d)))]
[(E: (App (E: (Var (? %Qbox?))) (list b)))
(box (unkwote (cadr p)))]
[(E: (App (E: (Var (? %Qvector?))) args))
(list->vector (map unkwote (cdr p)))]
[_ (continue e p)])))))
(define pexprs
(lambda (e)
(unparse* e pname (compact-quote (lambda (e p) p)))))
(define pexprs-renamed
(lambda (e)
(unparse* e pname-rename (compact-quote (lambda (e p) p)))))
(define pexprs-with-labels
(lambda (e)
(unparse*
e
pname
(lambda (e p)
(let ([labelit (lambda (it e)
(if (labeled? e)
(symbol-append it '^ (labelof e))
it))]
[any-extra-labels (lambda (e)
(if (labeled? e)
(list (extra-labels e))
'()))])
(match e
[(E: (Var x))
(cond [(Name-primitive? x)
`(,(labelit 'PRIM e) ,@(any-extra-labels e) ,p)]
[(Name-let-bound? x)
`(,(labelit 'PVAR e) ,p)]
[else
`(,(labelit 'VAR e) ,p)])]
[(E: (? App?))
`(,(labelit 'AP e) ,@(any-extra-labels e) ,@p)]
[(E: (? If?))
`(,(labelit 'if e) ,@(any-extra-labels e) ,@(cdr p))]
[(E: (? Const?))
`(,(labelit 'CONST e) ,p)]
[(? E?)
(match p
[`(,(? symbol? s) ,args ...) `(,(labelit s e) ,@args)])]
[_ p]))))))
(define pexprs-with-checks
(lambda (e)
(let* ([pname pname-rename]
[check-counter (generate-counter 0)]
[defs (unparse*
e
pname
(compact-quote
(lambda (e p)
(match e
[(E: (Var x))
(if (and (Name-primitive? x) (check-needed? (labelof e)))
`(,(symbol-append 'CHECK- p) ,(check-counter))
p)]
[(E: (or (? Lam?) (? Vlam?)))
(if (check-needed? (labelof e))
`(CHECK-lambda (,(check-counter)) ,@(cdr p))
p)]
[(E: (? App?))
(if (check-needed? (labelof e))
`(CHECK-ap (,(check-counter)) ,@p)
p)]
[(E: (? Letcc?))
(if (check-needed? (labelof e))
`(CHECK-letcc (,(check-counter)) ,@(cdr p))
p)]
[(Defstruct tag con pred sels muts)
`(CHECK-defstruct ,@(cdr p))]
[_ p]))))])
(cons defs (check-counter)))))
(define pexprs-with-no-checks
(lambda (e)
(unparse* e pname-rename (compact-quote (lambda (e p) p)))))
(define pexprs-with-all-checks
(lambda (e)
(let* ([pname pname-rename]
[check-counter (generate-counter 0)]
[defs (unparse*
e
pname
(compact-quote
(lambda (e p)
(match e
[(E: (Var x))
(if (and (Name-primitive? x) (not (eq? x %internal-apply)))
`(,(symbol-append 'CHECK- p) ,(check-counter))
p)]
[(E: (or (? Lam?) (? Vlam?)))
`(CHECK-lambda (,(check-counter)) ,@(cdr p))]
[(E: (App (E: (Var (? Name-primitive?))) _))
p]
[(E: (? App?))
`(CHECK-ap (,(check-counter)) ,@p)]
[(Defstruct tag con pred sels muts)
`(CHECK-defstruct ,@(cdr p))]
[_ p]))))])
(cons defs (check-counter)))))
(define pname Name-name)
(set-pcontext!
(lambda (context)
(if (null? context)
'()
`(in ,(car context) ,@(pcontext (cdr context))))))
(set-pname*!
(lambda (x)
(if (null? (Name-context x))
(Name-name x)
(cons (Name-name x) (pcontext (Name-context x))))))
(define pname-rename
(lambda (n)
(let ([n (pname n)])
(if (memq n primitives-to-rename)
(symbol-append 'CF- n)
n))))
(define pfn
(lambda (e)
(match e
[(E: (Var x))
(and (Name-primitive? x) (pname x))]
[(E: (Lam: x _))
(if (E-context e)
(pname* (Define-name (E-context e)))
`(lambda ,(map pname x) ...))]
[(E: (Vlam: x rest _))
(if (E-context e)
(pname* (Define-name (E-context e)))
`(lambda ,(append (map pname x) (pname rest)) ...))]
[(E: (Letcc x _))
`(letcc ,(pname x) ...)]
[_ #f])))