-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand.scm
611 lines (538 loc) · 17.4 KB
/
expand.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
(module expand
(main main))
(define (main argv)
(bigloo-warning-set! 0)
(write (cdr (expand `((
(define-syntax define
(syntax-rules ()
((define (f params ...) body ...)
(define f (lambda (params ...) body ...)))
((define (f params . rest) body ...)
(define f (lambda (params . rest) body ...)))
((define (f . args) body ...)
(define f (lambda args body ...)))
((define var val ...)
(mce-define var val ...))))
(define-syntax call/cc
(syntax-rules ()
((call/cc f)
(let/cc k (f k)))))
(define-syntax call-with-current-continuation
(syntax-rules ()
((call-with-current-continuation f)
(call/cc f))))
(define-syntax not
(syntax-rules ()
((not v)
(if v #f #t))))
; Simple version of values and call-with-values for the common case
(define-syntax values
(syntax-rules ()
((values v ...)
(list v ...))))
(define-syntax call-with-values
(syntax-rules ()
((call-with-values producer consumer)
(apply consumer (producer)))))
; R5RS Section 7.3
; https://schemers.org/Documents/Standards/R5RS/HTML/
(define-syntax and
(syntax-rules ()
((and) #t)
((and test) test)
((and test1 test2 ...)
(if test1 (and test2 ...) #f))))
(define-syntax or
(syntax-rules ()
((or) #f)
((or test) test)
((or test1 test2 ...)
(let ((x test1))
(if x x (or test2 ...))))))
(define-syntax case
(syntax-rules (else)
((case (key ...)
clauses ...)
(let ((atom-key (key ...)))
(case atom-key clauses ...)))
((case key
(else result1 result2 ...))
(begin result1 result2 ...))
((case key
((atoms ...) result1 result2 ...))
(if (memv key '(atoms ...))
(begin result1 result2 ...)))
((case key
((atoms ...) result1 result2 ...)
clause clauses ...)
(if (memv key '(atoms ...))
(begin result1 result2 ...)
(case key clause clauses ...)))))
; R6RS Appendix B
; http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-16.html#node_chap_B
(define-syntax cond
(syntax-rules (else =>)
((cond (else result1 result2 ...))
(begin result1 result2 ...))
((cond (test => result))
(let ((temp test))
(if temp (result temp))))
((cond (test => result) clause1 clause2 ...)
(let ((temp test))
(if temp
(result temp)
(cond clause1 clause2 ...))))
((cond (test)) test)
((cond (test) clause1 clause2 ...)
(let ((temp test))
(if temp
temp
(cond clause1 clause2 ...))))
((cond (test result1 result2 ...))
(if test (begin result1 result2 ...)))
((cond (test result1 result2 ...)
clause1 clause2 ...)
(if test
(begin result1 result2 ...)
(cond clause1 clause2 ...)))))
(define-syntax let*
(syntax-rules ()
((let* () body1 body2 ...)
(let () body1 body2 ...))
((let* ((name1 expr1) (name2 expr2) ...)
body1 body2 ...)
(let ((name1 expr1))
(let* ((name2 expr2) ...)
body1 body2 ...)))))
(define-syntax letrec
(syntax-rules ()
((letrec () body1 body2 ...)
(let () body1 body2 ...))
((letrec ((var init) ...) body1 body2 ...)
(letrec-helper
(var ...)
()
((var init) ...)
body1 body2 ...))))
(define-syntax letrec-helper
(syntax-rules ()
((letrec-helper
()
(temp ...)
((var init) ...)
body1 body2 ...)
(let ((var 'undefined) ...)
(let ((temp init) ...)
(set! var temp)
...)
(let () body1 body2 ...)))
((letrec-helper
(x y ...)
(temp ...)
((var init) ...)
body1 body2 ...)
(letrec-helper
(y ...)
(newtemp temp ...)
((var init) ...)
body1 body2 ...))))
(define-syntax letrec*
(syntax-rules ()
((letrec* ((var1 init1) ...) body1 body2 ...)
(let ((var1 'undefined) ...)
(set! var1 init1)
...
(let () body1 body2 ...)))))
(define-syntax let-values
(syntax-rules ()
((let-values (binding ...) body1 body2 ...)
(let-values-helper1
()
(binding ...)
body1 body2 ...))))
(define-syntax let-values-helper1
;; map over the bindings
(syntax-rules ()
((let-values
((id temp) ...)
()
body1 body2 ...)
(let ((id temp) ...) body1 body2 ...))
((let-values
assocs
((formals1 expr1) (formals2 expr2) ...)
body1 body2 ...)
(let-values-helper2
formals1
()
expr1
assocs
((formals2 expr2) ...)
body1 body2 ...))))
(define-syntax let-values-helper2
;; create temporaries for the formals
(syntax-rules ()
((let-values-helper2
()
temp-formals
expr1
assocs
bindings
body1 body2 ...)
(call-with-values
(lambda () expr1)
(lambda temp-formals
(let-values-helper1
assocs
bindings
body1 body2 ...))))
((let-values-helper2
(first . rest)
(temp ...)
expr1
(assoc ...)
bindings
body1 body2 ...)
(let-values-helper2
rest
(temp ... newtemp)
expr1
(assoc ... (first newtemp))
bindings
body1 body2 ...))
((let-values-helper2
rest-formal
(temp ...)
expr1
(assoc ...)
bindings
body1 body2 ...)
(call-with-values
(lambda () expr1)
(lambda (temp ... . newtemp)
(let-values-helper1
(assoc ... (rest-formal newtemp))
bindings
body1 body2 ...))))))
(define-syntax let
(syntax-rules ()
((let ((name val) ...) body1 body2 ...)
((lambda (name ...) body1 body2 ...)
val ...))
((let tag ((name val) ...) body1 body2 ...)
((letrec ((tag (lambda (name ...)
body1 body2 ...)))
tag)
val ...))))
(define-syntax let*-values
(syntax-rules ()
((let*-values () body1 body2 ...)
(let () body1 body2 ...))
((let*-values (binding1 binding2 ...)
body1 body2 ...)
(let-values (binding1)
(let*-values (binding2 ...)
body1 body2 ...)))))
(define-syntax toplevel
(syntax-rules ()
((toplevel (f params ...) body ...)
(f (lambda (params ...) body ...)))
((toplevel (f params . rest) body ...)
(f (lambda (params . rest) body ...)))
((toplevel (f . args) body ...)
(f (lambda args body ...)))
((toplevel var val ...)
(var initialize (begin val ...)))))
(define-syntax toplevel-helper
(syntax-rules ()
((toplevel-helper (f params ...) body ...)
(f helper (lambda (params ...) body ...)))
((toplevel-helper (f params . rest) body ...)
(f helper (lambda (params . rest) body ...)))
((toplevel-helper (f . args) body ...)
(f helper (lambda args body ...)))
((toplevel-helper var val ...)
(var helper initialize (begin val ...)))))
) (
(toplevel (list . args) args)
(toplevel (null? v)
(and (vector? v)
(= (vector-length v) 0)))
(toplevel (pair? v)
(and (vector? v)
(= (vector-length v) 2)))
(toplevel (car v) (vector-ref v 0))
(toplevel (cdr v) (vector-ref v 1))
(toplevel (set-car! v x) (vector-set! v 0 x))
(toplevel (set-cdr! v y) (vector-set! v 1 y))
(toplevel (length l)
(let loop ((i 0) (l l))
(if (null? l)
i
(loop (+ i 1) (cdr l)))))
(toplevel (vector . els)
(let* ((len (length els))
(v (make-vector len)))
(let loop ((i 0) (els els))
(if (= i len)
v
(begin (vector-set! v i (car els))
(loop (+ i 1) (cdr els)))))))
(toplevel (cons x y)
;(vector x y))
(let ((v (make-vector 2)))
(vector-set! v 0 x)
(vector-set! v 1 y)
v))
(toplevel (cons* . args)
(if (null? (cdr args))
(car args)
(cons (car args) (apply cons* (cdr args)))))
(toplevel (list-ref l i)
(if (= i 0)
(car l)
(list-ref (cdr l) (- i 1))))
(toplevel (binary=? x y)
(let ((len (binary-length x)))
(if (= (binary-length y) len)
(let loop ((i 0))
(cond ((= i len) #t)
((= (binary-ref x i) (binary-ref y i))
(loop (+ i 1)))
(else #f)))
#f)))
(toplevel (eq? x y)
(cond ((or (and (char? x) (char? y))
(and (symbol? x) (symbol? y)))
(binary=? x y))
((and (boolean? x) (boolean? y))
(or (and x y) (and (not x) (not y))))
((and (number? x) (number? y))
(= x y))
((and (null? x) (null? y))
#t)
((and (string? x) (string? y))
#f)
(else
(same-object? x y))))
(toplevel eqv? eq?)
(toplevel (equal? x y)
(cond ((eqv? x y) #t)
((and (string? x) (string? y))
(binary=? x y))
((and (vector? x) (vector? y))
(let ((len (vector-length x)))
(if (= (vector-length y) len)
(let loop ((i 0))
(cond ((= i len) #t)
((equal? (vector-ref x i) (vector-ref y i))
(loop (+ i 1)))
(else #f)))
#f)))
(else #f)))
(toplevel (memv o l)
(cond ((null? l) #f)
((eqv? (car l) o) #t)
(else (memv o (cdr l)))))
(toplevel (append l1 l2)
(if (null? l1)
l2
(cons (car l1) (append (cdr l1) l2))))
(toplevel (assoc k alist)
(if (null? alist)
#f
(if (equal? (car (car alist)) k)
(car alist)
(assoc k (cdr alist)))))
(toplevel (abs x)
(if (< x 0) (- x) x))
(toplevel (modulo x y)
(let ((d (floor (/ x y))))
(- x (* y d))))
(toplevel (<= x y)
(or (< x y) (= x y)))
(toplevel (>= x y)
(or (> x y) (= x y)))
(toplevel (char->integer c)
(binary-ref c 1))
(toplevel-helper char-code (char->integer #\B))
(toplevel-helper string-code (char->integer #\C))
(toplevel-helper symbol-code (char->integer #\D))
(toplevel (char? x)
(and (binary? x) (= (binary-length x) 2) (= (binary-ref x 0) char-code)))
(toplevel (string? x)
(and (binary? x) (> (binary-length x) 0) (= (binary-ref x 0) string-code)))
(toplevel (symbol? x)
(and (binary? x) (> (binary-length x) 0) (= (binary-ref x 0) symbol-code)))
(toplevel (binary-copy b . args)
(let* ((numargs (length args))
(start (if (> numargs 0) (list-ref args 0) 0))
(end (if (> numargs 1) (list-ref args 1) (binary-length b)))
(len (- end start))
(r (make-binary len)))
(let loop ((i 0))
(if (< i len)
(begin (binary-set! r i (binary-ref b (+ start i)))
(loop (+ i 1)))))
r))
(toplevel (for-each f l)
(if (not (null? l))
(begin (f (car l)) (for-each f (cdr l)))))
(toplevel (repeat f n)
(if (not (= n 0))
(begin (f) (repeat f (- n 1)))))
(toplevel-helper (make-port name output)
(lambda (x . args)
(cond ((and (binary? x) (not (null? args)))
(apply output (cons x args)))
((char? x)
(output x 1 2))
((or (symbol? x) (string? x))
(output x 1 (binary-length x)))
((binary? x)
(apply output (cons x args)))
(else
(error name "unknown expression" x)))))
(toplevel-helper output-port (make-port "current-output-port" output-binary-to-stdout))
(toplevel-helper error-port (make-port "current-error-port" output-binary-to-stderr))
(toplevel (current-output-port) output-port)
(toplevel (current-error-port) error-port)
(toplevel-helper hexchars "0123456789abcdef")
(toplevel-helper (byte->binhex b)
(let ((bin (make-binary 2)))
(binary-set! bin 1 (binary-ref hexchars (+ 1 (modulo b 16))))
(binary-set! bin 0 (binary-ref hexchars (+ 1 (floor (/ b 16)))))
bin))
(toplevel-helper space-code (char->integer #\space))
(toplevel-helper tilde-code (char->integer #\~))
(toplevel-helper backslash-code (char->integer #\\))
(toplevel-helper (printable? i)
(and (>= i space-code) (<= i tilde-code)))
(toplevel-helper (write-binary b port start wrap-char)
(let ((wrap-code (char->integer wrap-char))
(end (binary-length b)))
(port wrap-char)
(let loop ((i start) (prev start))
(if (< i end)
(let ((c (binary-ref b i)))
(cond ((or (= c wrap-code) (= c backslash-code))
(if (< prev i)
(port b prev i))
(port #\\)
(port b i (+ i 1))
(let ((next (+ i 1)))
(loop next next)))
((printable? c)
(loop (+ i 1) prev))
(else
(if (< prev i)
(port b prev i))
(port #\x)
(port (byte->binhex c) 0 2)
(let ((next (+ i 1)))
(loop next next)))))
(if (< prev end)
(port b prev end))))
(port wrap-char)))
(toplevel (display-binary b . args)
(let* ((args-len (length args))
(port (if (> args-len 0) (list-ref args 0) (current-output-port)))
(start (if (> args-len 1) (list-ref args 1) 0))
(end (if (> args-len 2) (list-ref args 2) (binary-length b))))
(port b start end)))
(toplevel-helper (display-aux x port is-write)
(cond ((null? x)
(port "()"))
((boolean? x)
(port (if x "#t" "#f")))
((number? x)
(if (< x 0)
(port #\-))
(let* ((ax (abs x))
(whole (floor ax))
(p '()))
(let loop ((n whole))
(let ((digit (modulo n 10))
(b (make-binary 1)))
(binary-set! b 0 (+ (char->integer #\0) digit))
(set! p (cons b p))
(let ((next (floor (/ n 10))))
(if (= next 0)
(for-each (lambda (b)
(port b 0 1))
p)
(loop next)))))
(let* ((digit (modulo (floor ax) 10))
(n (- ax (- (floor ax) digit))))
(if (> n digit)
(begin (port #\.)
(let loop ((i 0) (f (* n 10)) (zeros 0))
(let* ((digit (modulo (floor f) 10))
(n (- f (- (floor f) digit))))
(if (= digit 0)
(set! zeros (+ zeros 1))
(let ((b (make-binary 1)))
(repeat (lambda () (port #\0)) zeros)
(set! zeros 0)
(binary-set! b 0 (+ (char->integer #\0) digit))
(port b 0 1)))
(if (and (< i 6) (> n digit))
(loop (+ i 1) (* n 10) zeros)))))))))
((char? x)
(if is-write
(let ((c (char->integer x)))
(port #\\)
(if (printable? c)
(port x)
(begin (port "x" (port (byte->binhex c) 0 2)))))
(port x)))
((string? x)
(if is-write
(write-binary x port 1 #\")
(port x)))
((symbol? x)
(if is-write
(write-binary x port 1 #\|)
(port x)))
((pair? x)
(port #\()
(let loop ((x x))
(display-aux (car x) port is-write)
(let ((y (cdr x)))
(if (pair? y)
(begin (port #\space)
(loop y))
(if (not (null? y))
(begin (port " . ")
(display-aux y port is-write))))))
(port #\)))
((vector? x)
(port "#(")
(let loop ((i 0))
(if (< i (vector-length x))
(begin (if (> i 0)
(port #\space))
(display-aux (vector-ref x i) port is-write)
(loop (+ i 1)))))
(port #\)))
((procedure? x)
(port "#<procedure>"))
(else
(error "display-aux" "unknown expression" x))))
(toplevel-helper (port-from-args args)
(if (null? args) (current-output-port) (car args)))
(toplevel (display x . args)
(display-aux x (port-from-args args) #f))
(toplevel (newline . args)
((port-from-args args) #\newline))
(toplevel-helper (print-aux args port)
(for-each (lambda (x) (display-aux x port #f)) args)
(newline port))
(toplevel (print . args)
(print-aux args (current-output-port)))
(toplevel (eprint . args)
(print-aux args (current-error-port)))
(toplevel (write x . args)
(display-aux x (port-from-args args) #t))
)
,(read))))))