/
more-scheme.rkt
396 lines (361 loc) · 13.2 KB
/
more-scheme.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
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
;;----------------------------------------------------------------------
;; more-scheme : case, do, etc. - remaining syntax
(module more-scheme '#%kernel
(#%require "define-et-al.rkt" "qq-and-or.rkt" "cond.rkt" "define.rkt" '#%paramz "case.rkt" "logger.rkt"
"member.rkt"
(for-syntax '#%kernel "stx.rkt" "define-et-al.rkt" "qq-and-or.rkt" "cond.rkt" "stxcase-scheme.rkt" "qqstx.rkt"))
;; For `old-case`:
(define-syntax case-test
(lambda (x)
(syntax-case x ()
;; For up to 3 elements, inline `eqv?' tests:
[(_ x (k))
(syntax (eqv? x 'k))]
[(_ x (k1 k2))
(syntax (let ([tmp x]) (if (eqv? tmp 'k1) #t (eqv? tmp 'k2))))]
[(_ x (k1 k2 k3))
(syntax (let ([tmp x]) (if (eqv? tmp 'k1) #t (if (eqv? tmp 'k2) #t (eqv? tmp 'k3)))))]
[(_ x (k ...))
(syntax (memv x '(k ...)))])))
;; Mostly from Dybvig:
(define-syntax (old-case x)
(syntax-case* x (else) (let ([else-stx (datum->syntax #f 'else)])
(lambda (a b) (free-identifier=? a else-stx)))
((_ v)
(syntax (#%expression (begin v (void)))))
((_ v (else e1 e2 ...))
(syntax/loc x (#%expression (begin v (let-values () e1 e2 ...)))))
((_ v ((k ...) e1 e2 ...))
(syntax/loc x (if (case-test v (k ...)) (let-values () e1 e2 ...) (void))))
((self v ((k ...) e1 e2 ...) c1 c2 ...)
(syntax/loc x (let ((x v))
(if (case-test x (k ...))
(let-values () e1 e2 ...)
(self x c1 c2 ...)))))
((_ v (bad e1 e2 ...) . rest)
(raise-syntax-error
#f
"bad syntax (not a datum sequence)"
x
(syntax bad)))
((_ v clause . rest)
(raise-syntax-error
#f
"bad syntax (missing expression after datum sequence)"
x
(syntax clause)))
((_ . v)
(not (null? (syntax-e (syntax v))))
(raise-syntax-error
#f
"bad syntax (illegal use of `.')"
x))))
;; From Dybvig:
(define-syntax do
(lambda (orig-x)
(syntax-case orig-x ()
((_ ((var init . step) ...) (e0 e1 ...) c ...)
(with-syntax (((step ...)
(map (lambda (v s)
(syntax-case s ()
(() v)
((e) (syntax e))
(_ (raise-syntax-error
#f
"bad variable syntax"
orig-x))))
(syntax->list (syntax (var ...)))
(syntax->list (syntax (step ...))))))
(syntax/loc orig-x
(let doloop ((var init) ...)
(if e0
(begin (void) e1 ...)
(begin c ... (doloop step ...))))))))))
(define-syntax parameterize
(lambda (stx)
(syntax-case stx ()
[(_ () expr1 expr ...)
(syntax (let () expr1 expr ...))]
[(_ ([param val] ...) expr1 expr ...)
(with-syntax ([(p/v ...)
(apply append
(map list
(syntax->list #'(param ...))
(syntax->list #'(val ...))))])
(syntax-protect
(syntax/loc stx
(with-continuation-mark
parameterization-key
(extend-parameterization
(continuation-mark-set-first #f parameterization-key)
p/v ...)
(let ()
expr1
expr ...)))))])))
(define-syntax parameterize*
(syntax-rules ()
[(_ () body1 body ...)
(let () body1 body ...)]
[(_ ([lhs1 rhs1] [lhs rhs] ...) body1 body ...)
(parameterize ([lhs1 rhs1])
(parameterize* ([lhs rhs] ...)
body1 body ...))]))
(define (current-parameterization)
(continuation-mark-set-first #f parameterization-key))
(define (call-with-parameterization paramz thunk)
(unless (parameterization? paramz)
(raise-argument-error 'call-with-parameterization "parameterization?" 0 paramz thunk))
(unless (and (procedure? thunk)
(procedure-arity-includes? thunk 0))
(raise-argument-error 'call-with-parameterization "(-> any)" 1 paramz thunk))
(with-continuation-mark
parameterization-key
paramz
(thunk)))
(define-syntax parameterize-break
(lambda (stx)
(syntax-case stx ()
[(_ bool-expr expr1 expr ...)
(syntax-protect
(syntax/loc stx
(with-continuation-mark
break-enabled-key
(make-thread-cell (and bool-expr #t))
(begin
(check-for-break)
(let ()
expr1
expr ...)))))])))
(define-values (struct:break-paramz make-break-paramz break-paramz? break-paramz-ref break-paramz-set!)
(make-struct-type 'break-parameterization #f 1 0 #f))
(-define-struct break-parameterization (cell))
(define (current-break-parameterization)
(make-break-paramz (continuation-mark-set-first #f break-enabled-key)))
(define (call-with-break-parameterization paramz thunk)
(unless (break-paramz? paramz)
(raise-argument-error 'call-with-break-parameterization "break-parameterization?" 0 paramz thunk))
(unless (and (procedure? thunk)
(procedure-arity-includes? thunk 0))
(raise-argument-error 'call-with-parameterization "(-> any)" 1 paramz thunk))
(begin0
(with-continuation-mark
break-enabled-key
(break-paramz-ref paramz 0)
(begin
(check-for-break)
(thunk)))
(check-for-break)))
(define (select-handler/no-breaks e bpz l)
(with-continuation-mark
break-enabled-key
;; make a fresh thread cell so that the shared one isn't mutated
(make-thread-cell #f)
(let loop ([l l])
(cond
[(null? l)
(raise e)]
[((caar l) e)
(begin0
((cdar l) e)
(with-continuation-mark
break-enabled-key
bpz
(check-for-break)))]
[else
(loop (cdr l))]))))
(define (select-handler/breaks-as-is e bpz l)
(cond
[(null? l)
(raise e)]
[((caar l) e)
(with-continuation-mark
break-enabled-key
bpz
(begin
(check-for-break)
((cdar l) e)))]
[else
(select-handler/breaks-as-is e bpz (cdr l))]))
(define false-thread-cell (make-thread-cell #f))
(define (check-with-handlers-in-context handler-prompt-key)
(unless (continuation-prompt-available? handler-prompt-key)
(error 'with-handlers
"exception handler used out of context")))
(define handler-prompt-key (make-continuation-prompt-tag 'handler-prompt-tag))
(define (call-handled-body bpz handle-proc body-thunk)
;; Disable breaks here, so that when the exception handler jumps
;; to run a handler, breaks are disabled for the handler
(with-continuation-mark
break-enabled-key
false-thread-cell
(call-with-continuation-prompt
(lambda (bpz body-thunk)
;; Restore the captured break parameterization for
;; evaluating the `with-handlers' body. In this
;; special case, no check for breaks is needed,
;; because bpz is quickly restored past call/ec.
;; Thus, `with-handlers' can evaluate its body in
;; tail position.
(with-continuation-mark
break-enabled-key
bpz
(with-continuation-mark
exception-handler-key
(lambda (e)
;; Deliver the exception to the escape handler:
(abort-current-continuation
handler-prompt-key
e))
(body-thunk))))
handler-prompt-key
handle-proc
bpz body-thunk)))
(define-syntaxes (with-handlers with-handlers*)
(let ([wh
(lambda (disable-break?)
(lambda (stx)
(syntax-case stx ()
[(_ () expr1 expr ...) (syntax/loc stx (let () expr1 expr ...))]
[(_ ([pred handler] ...) expr1 expr ...)
(with-syntax ([(pred-name ...) (generate-temporaries (map (lambda (x) 'with-handlers-predicate)
(syntax->list #'(pred ...))))]
[(handler-name ...) (generate-temporaries (map (lambda (x) 'with-handlers-handler)
(syntax->list #'(handler ...))))])
(syntax-protect
(quasisyntax/loc stx
(let-values ([(pred-name) pred] ...
[(handler-name) handler] ...)
;; Capture current break parameterization, so we can use it to
;; evaluate the body
(let ([bpz (continuation-mark-set-first #f break-enabled-key)])
(call-handled-body
bpz
(lambda (e)
(#,(if disable-break?
#'select-handler/no-breaks
#'select-handler/breaks-as-is)
e bpz
(list (cons pred-name handler-name) ...)))
#,(syntax/loc stx
(lambda ()
expr1 expr ...))))))))])))])
(values (wh #t) (wh #f))))
(define (call-with-exception-handler exnh thunk)
;; The `begin0' ensures that we don't overwrite an enclosing
;; exception handler.
(begin0
(with-continuation-mark
exception-handler-key
exnh
(thunk))
(void)))
(define-syntax set!-values
(lambda (stx)
(syntax-case stx ()
[(_ () expr) (syntax (let-values ([() expr]) (void)))]
[(_ (id) expr) (identifier? (syntax id)) (syntax (set! id expr))]
[(_ (id ...) expr)
(let ([ids (stx->list (syntax (id ...)))])
(for-each
(lambda (id)
(unless (identifier? id)
(raise-syntax-error #f
"not an identifier"
stx
id)))
ids)
(let ([dup (check-duplicate-identifier ids)])
(when dup
(raise-syntax-error #f
"duplicate identifier"
stx
dup))))
(with-syntax ([(temp ...) (generate-temporaries (syntax (id ...)))])
(syntax/loc
stx
(let-values ([(temp ...) expr])
(set! id temp) ...)))])))
(define-values (call/cc) call-with-current-continuation)
(define-syntax let/cc
(lambda (stx)
(syntax-case stx ()
[(_ var body1 body ...)
(syntax/loc stx (call/cc (lambda (var) body1 body ...)))])))
(define-syntax fluid-let
(lambda (stx)
(syntax-case stx ()
[(_ () body1 body ...) (syntax/loc stx (let () body1 body ...))]
[(_ ([name val] ...) body1 body ...)
(with-syntax ([(tmp ...) (generate-temporaries (syntax (name ...)))])
(syntax/loc
stx
(let ([tmp val] ...)
(let ([swap
(lambda ()
(let ([s tmp])
(set! tmp name)
(set! name s))
...)])
(dynamic-wind
swap
(lambda () body1 body ...)
swap)))))])))
(define-syntax time
(lambda (stx)
(syntax-case stx ()
[(_ expr1 expr ...)
(syntax/loc
stx
(let-values ([(v cpu user gc) (time-apply (lambda () expr1 expr ...) null)])
(printf "cpu time: ~s real time: ~s gc time: ~s\n" cpu user gc)
(apply values v)))])))
(define not-there (gensym))
(define (do-hash-update who mut? set ht key xform default)
(unless (variable-reference-from-unsafe? (#%variable-reference))
(unless (and (hash? ht)
(if mut?
(not (immutable? ht))
(immutable? ht)))
(raise-argument-error who (if mut? "(and/c hash? (not/c immutable?))" "(and/c hash? immutable?)") ht))
(unless (and (procedure? xform)
(procedure-arity-includes? xform 1))
(raise-argument-error who "(any/c . -> . any/c)" xform)))
(let ([v (hash-ref ht key default)])
(if (eq? v not-there)
(raise-mismatch-error who "no value found for key: " key)
(set ht key (xform v)))))
(define hash-update
(case-lambda
[(ht key xform default)
(do-hash-update 'hash-update #f hash-set ht key xform default)]
[(ht key xform)
(hash-update ht key xform not-there)]))
(define hash-update!
(case-lambda
[(ht key xform default)
(do-hash-update 'hash-update! #t hash-set! ht key xform default)]
[(ht key xform)
(hash-update! ht key xform not-there)]))
(define (hash-has-key? ht key)
(unless (hash? ht)
(raise-argument-error 'hash-has-key? "hash?" 0 ht key))
(not (eq? not-there (hash-ref ht key not-there))))
(define (hash-ref! ht key new)
(unless (and (hash? ht)
(not (immutable? ht)))
(raise-argument-error 'hash-ref! "(and/c hash? (not/c immutable?))" 0 ht key new))
(let ([v (hash-ref ht key not-there)])
(if (eq? not-there v)
(let ([n (if (procedure? new) (new) new)])
(hash-set! ht key n)
n)
v)))
(#%provide case old-case do
parameterize parameterize* current-parameterization call-with-parameterization
parameterize-break current-break-parameterization call-with-break-parameterization
(rename break-paramz? break-parameterization?)
with-handlers with-handlers* call-with-exception-handler
set!-values
let/cc call/cc fluid-let time
log-fatal log-error log-warning log-info log-debug define-logger
hash-ref! hash-has-key? hash-update hash-update!))