-
Notifications
You must be signed in to change notification settings - Fork 3
/
shared.rkt
356 lines (339 loc) · 18.4 KB
/
shared.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
355
356
#lang s-exp "lang/base.rkt"
(require (for-syntax "lang/base.rkt"
(prefix-in kernel: '#%kernel)
(only-in scheme/base syntax-case ... syntax->list syntax identifier? raise-syntax-error check-duplicate-identifier local-expand syntax-e free-identifier=? syntax/loc generate-temporaries regexp-match-positions datum->syntax syntax-local-value with-syntax syntax-case* quote-syntax quasisyntax unsyntax unsyntax-splicing)
syntax/kerncase
syntax/struct))
(provide shared)
(define undefined (letrec ([x x]) x))
(require (only-in "lang/base.rkt" [cons the-cons]))
(define-syntax shared
(lambda (stx)
(define make-check-cdr #f)
;; Include the implementation.
;; Used by ../shared.rkt, and also collects/lang/private/teach.rkt
;; Besides the usual things, this code expects `undefined' and
;; `the-cons' to be bound, and it expects `struct-declaration-info?'
;; from the "struct.rkt" library of the "syntax" collection.
(syntax-case stx ()
[(_ ([name expr] ...) body1 body ...)
(let ([names (syntax->list (syntax (name ...)))]
[exprs (syntax->list (syntax (expr ...)))])
(for-each (lambda (name)
(unless (identifier? name)
(raise-syntax-error
'shared
"not an identifier"
stx
name)))
names)
(let ([dup (check-duplicate-identifier names)])
(when dup
(raise-syntax-error
'shared
"duplicate identifier"
stx
dup)))
(let ([exprs (map (lambda (expr)
(let ([e (local-expand
expr
'expression
(append
(kernel-form-identifier-list)
(list #'#%app #'#%plain-app)
names))])
#;(printf "before: ~s\n" e)
#;(printf "free identifier eq? ~s ~s ~s\n"
(car (syntax-e e))
#'#%app
(free-identifier=? #'#%app (car (syntax-e e))))
;; Remove #%app if present...
(let ([result
(syntax-case e (#%app #%plain-app)
[(#%plain-app a ...)
(syntax/loc e (a ...))]
[(#%app a ...)
(syntax/loc e (a ...))]
[_else e])])
#;(printf "after: ~s~n" result)
result)))
exprs)]
[temp-ids (generate-temporaries names)]
[placeholder-ids (generate-temporaries names)]
[ph-used?s (map (lambda (x) (box #f)) names)]
[struct-decl-for (lambda (id)
(and (identifier? id)
(let* ([s (symbol->string (syntax-e id))]
[m (regexp-match-positions "make-" s)])
(and m
(let ([name (datum->syntax
id
(string->symbol (string-append (substring s 0 (caar m))
(substring s (cdar m) (string-length s))))
id)])
(let ([v (syntax-local-value name (lambda () #f))])
(and v
(struct-declaration-info? v)
(let ([decl (extract-struct-info v)])
(and (cadr decl)
(andmap values (list-ref decl 4))
decl)))))))))]
[append-ids null]
[same-special-id? (lambda (a b)
;; Almost module-or-top-identifier=?,
;; but handle the-cons specially
#;(printf "Comparing ~s and ~s: ~s at phase ~s\n" a b
(free-identifier=? a b)
(syntax-local-phase-level))
(let ([result
(or (free-identifier=? a b)
(free-identifier=?
a
(datum->syntax
#f
(if (eq? 'the-cons (syntax-e b))
'cons
(syntax-e b))))
;; dyoo: there's a hack here!
;; The identifiers introduced by quasiquote
;; are list and list*, but I can't seem
;; to reliably free-identifier=? against them...
(and (eq? (syntax-e a) 'list)
(eq? (syntax-e b) 'list))
(and (eq? (syntax-e a) 'list*)
(eq? (syntax-e b) 'list*)))])
#;(printf "result: ~s\n" result)
result))])
(with-syntax ([(graph-expr ...)
(map (lambda (expr)
(let loop ([expr expr])
(define (bad n)
(raise-syntax-error
'shared
(format "illegal use of ~a" n)
stx
expr))
(define (cons-elem expr)
(or (and (identifier? expr)
(ormap (lambda (i ph ph-used?)
(and (free-identifier=? i expr)
(set-box! ph-used? #t)
ph))
names placeholder-ids ph-used?s))
(loop expr)))
(define list-syntaxes
(syntax->list #'(list list* kernel:list kernel:list*)))
(syntax-case* expr (the-cons mcons append box box-immutable vector vector-immutable) same-special-id?
[(the-cons a d)
(with-syntax ([a (cons-elem #'a)]
[d (cons-elem #'d)])
(syntax/loc expr (cons a d)))]
[(the-cons . _)
(bad "cons")]
#;[(mcons a d)
(syntax (mcons undefined undefined))]
#;[(mcons . _)
(bad "mcons")]
[(lst e ...)
(ormap (lambda (x) (same-special-id? #'lst x))
list-syntaxes #;(syntax->list #'(list list*)))
(with-syntax ([(e ...)
(map (lambda (x) (cons-elem x))
(syntax->list (syntax (e ...))))])
(syntax/loc expr (lst e ...)))]
[(lst . _)
(ormap (lambda (x) (same-special-id? #'lst x))
list-syntaxes #;(syntax->list #'(list list*)))
(bad (syntax-e #'lst))]
[(append e0 ... e)
(let ([len-id (car (generate-temporaries '(len)))])
(set! append-ids (cons len-id append-ids))
(with-syntax ([e (cons-elem #'e)]
[len-id len-id])
(syntax/loc expr (let ([ph (make-placeholder e)]
[others (append e0 ... null)])
(set! len-id (length others))
(append others ph)))))]
[(append . _)
(bad "append")]
[(box v)
(syntax (box undefined))]
[(box . _)
(bad "box")]
[(box-immutable v)
(with-syntax ([v (cons-elem #'v)])
(syntax/loc expr (box-immutable v)))]
[(vector e ...)
(with-syntax ([(e ...)
(map (lambda (x) (syntax undefined))
(syntax->list (syntax (e ...))))])
(syntax (vector e ...)))]
[(vector . _)
(bad "vector")]
[(vector-immutable e ...)
(with-syntax ([(e ...)
(map (lambda (x) (cons-elem x))
(syntax->list (syntax (e ...))))])
(syntax/loc expr (vector-immutable e ...)))]
[(vector-immutable . _)
(bad "vector-immutable")]
[(make-x . args)
(struct-decl-for (syntax make-x))
(let ([decl (struct-decl-for (syntax make-x))]
[args (syntax->list (syntax args))])
(unless args
(bad "structure constructor"))
(unless (= (length (list-ref decl 4)) (length args))
(raise-syntax-error
'shared
(format "wrong argument count for structure constructor; expected ~a, found ~a"
(length (list-ref decl 4)) (length args))
stx
expr))
(with-syntax ([undefineds (map (lambda (x) (syntax undefined)) args)])
(syntax (make-x . undefineds))))]
[_else
expr])))
exprs)]
[(init-expr ...)
(map (lambda (expr temp-id used?)
(let ([init-id
(syntax-case* expr (the-cons mcons list list* append box box-immutable vector vector-immutable) same-special-id?
[(the-cons . _) temp-id]
[(mcons . _) temp-id]
[(list . _) temp-id]
[(list* . _) temp-id]
[(append . _) temp-id]
[(box . _) temp-id]
[(box-immutable . _) temp-id]
[(vector . _) temp-id]
[(vector-immutable . _) temp-id]
[(make-x . _)
(struct-decl-for (syntax make-x))
temp-id]
[else #f])])
(cond
[init-id
(set-box! used? #t)
init-id]
[(unbox used?)
temp-id]
[else
expr])))
exprs temp-ids ph-used?s)]
[(finish-expr ...)
(let ([gen-n (lambda (l)
(let loop ([l l][n 0])
(if (null? l)
null
(cons (datum->syntax (quote-syntax here) n #f)
(loop (cdr l) (add1 n))))))]
[append-ids (reverse append-ids)])
(map (lambda (name expr)
(let loop ([name name] [expr expr])
(with-syntax ([name name])
(syntax-case* expr (the-cons mcons list list* append box box-immutable vector vector-immutable)
same-special-id?
[(the-cons a d)
#`(begin #,(loop #`(car name) #'a)
#,(loop #`(cdr name) #'d))]
[(mcons a d)
(syntax (begin
(set-mcar! name a)
(set-mcdr! name d)))]
[(list e ...)
(let ([es (syntax->list #'(e ...))])
#`(begin
#,@(map (lambda (n e)
(loop #`(list-ref name #,n) e))
(gen-n es)
es)))]
[(list* e ...)
(let* ([es (syntax->list #'(e ...))]
[last-n (sub1 (length es))])
#`(begin
#,@(map (lambda (n e)
(loop #`(#,(if (= (syntax-e n) last-n)
#'list-tail
#'list-ref)
name
#,n)
e))
(gen-n es)
es)))]
[(append e0 ... e)
(with-syntax ([len-id (car append-ids)])
(set! append-ids (cdr append-ids))
(loop #`(list-tail name len-id) #'e))]
[(box v)
(syntax (set-box! name v))]
[(box-immutable v)
(loop #'(unbox name) #'v)]
[(vector e ...)
(with-syntax ([(n ...) (gen-n (syntax->list (syntax (e ...))))])
(syntax (let ([vec name])
(vector-set! vec n e)
...)))]
[(vector-immutable e ...)
(let ([es (syntax->list #'(e ...))])
#`(begin
#,@(map (lambda (n e)
(loop #`(vector-ref name #,n) e))
(gen-n es)
es)))]
[(make-x e ...)
(struct-decl-for (syntax make-x))
(let ([decl (struct-decl-for (syntax make-x))])
(syntax-case (reverse (list-ref decl 4)) ()
[()
(syntax (void))]
[(setter ...)
(syntax (begin (setter name e) ...))]))]
[_else (syntax (void))]))))
names exprs))]
[(check-expr ...)
(if make-check-cdr
(map (lambda (name expr)
(syntax-case* expr (the-cons) same-special-id?
[(the-cons a d)
(make-check-cdr name)]
[_else (syntax #t)]))
names exprs)
null)]
[(temp-id ...) temp-ids]
[(placeholder-id ...) placeholder-ids]
[(ph-used? ...) (map unbox ph-used?s)]
[(used-ph-id ...) (filter values
(map (lambda (ph ph-used?)
(and (unbox ph-used?)
ph))
placeholder-ids ph-used?s))]
[(maybe-ph-id ...) (map (lambda (ph ph-used?)
(and (unbox ph-used?)
ph))
placeholder-ids ph-used?s)])
(with-syntax ([(ph-init ...) (filter values
(map (lambda (ph ph-used? graph-expr)
(and (unbox ph-used?)
#`(placeholder-set! #,ph #,graph-expr)))
placeholder-ids ph-used?s
(syntax->list #'(graph-expr ...))))]
[(append-id ...) append-ids])
(syntax/loc stx
(letrec-values ([(used-ph-id) (make-placeholder #f)] ...
[(append-id) #f] ...
[(temp-id ...)
(begin
ph-init ...
(apply values (make-reader-graph
(list maybe-ph-id ...))))]
[(name) init-expr] ...)
finish-expr
...
check-expr
...
body1
body
...))))))])
;; See private/shared-body.ss.
#;(include "private/shared-body.rkt")))