-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpand.scm
1552 lines (1429 loc) · 48.5 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; expand.scm - The HI/LO expander
;
; Copyright (c) 2008-2015, The CHICKEN Team
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
;; this unit needs the "modules" unit, but must be initialized first, so it doesn't
;; declare "modules" as used - if you use "-explicit-use", take care of this.
(declare
(unit expand)
(disable-interrupts)
(fixnum)
(hide match-expression
macro-alias
check-for-multiple-bindings
d dd dm dx map-se
lookup check-for-redef)
(not inline ##sys#syntax-error-hook ##sys#compiler-syntax-hook
##sys#toplevel-definition-hook))
(include "common-declarations.scm")
(define-syntax d (syntax-rules () ((_ . _) (void))))
;; Macro to avoid "unused variable map-se" when "d" is disabled
(define-syntax map-se
(syntax-rules ()
((_ ?se)
(map (lambda (a)
(cons (car a) (if (symbol? (cdr a)) (cdr a) '<macro>)))
?se))))
(set! ##sys#features
(append '(#:hygienic-macros
#:syntax-rules
#:srfi-0 #:srfi-2 #:srfi-6 #:srfi-9 #:srfi-46 #:srfi-55 #:srfi-61)
##sys#features))
(define-alias dd d)
(define-alias dm d)
(define-alias dx d)
(define-inline (getp sym prop)
(##core#inline "C_i_getprop" sym prop #f))
(define-inline (putp sym prop val)
(##core#inline_allocate ("C_a_i_putprop" 8) sym prop val))
;;; Syntactic environments
(define ##sys#current-environment (make-parameter '()))
(define ##sys#current-meta-environment (make-parameter '()))
;;XXX should this be in eval.scm?
(define ##sys#active-eval-environment (make-parameter ##sys#current-environment))
(define (lookup id se)
(cond ((##core#inline "C_u_i_assq" id se) => cdr)
((getp id '##core#macro-alias))
(else #f)))
(define (macro-alias var se)
(if (or (##sys#qualified-symbol? var)
(let* ((str (##sys#slot var 1))
(len (##sys#size str)))
(and (fx> len 0)
(char=? #\# (##core#inline "C_subchar" str 0)))))
var
(let* ((alias (gensym var))
(ua (or (lookup var se) var))
(rn (or (getp var '##core#real-name) var)))
(putp alias '##core#macro-alias ua)
(putp alias '##core#real-name rn)
(dd "aliasing " alias " (real: " var ") to "
(if (pair? ua)
'<macro>
ua))
alias) ) )
(define (strip-syntax exp)
;; if se is given, retain bound vars
(let ((seen '()))
(let walk ((x exp))
(cond ((assq x seen) => cdr)
((symbol? x)
(let ((x2 (getp x '##core#macro-alias) ) )
(cond ((getp x '##core#real-name))
((not x2) x)
((pair? x2) x)
(else x2))))
((pair? x)
(let ((cell (cons #f #f)))
(set! seen (cons (cons x cell) seen))
(set-car! cell (walk (car x)))
(set-cdr! cell (walk (cdr x)))
cell))
((vector? x)
(let* ((len (##sys#size x))
(vec (make-vector len)))
(set! seen (cons (cons x vec) seen))
(do ((i 0 (fx+ i 1)))
((fx>= i len) vec)
(##sys#setslot vec i (walk (##sys#slot x i))))))
(else x)))))
(define ##sys#strip-syntax strip-syntax)
(define (##sys#extend-se se vars #!optional (aliases (map gensym vars)))
(for-each
(lambda (alias sym)
(let ((original-real-name (getp sym '##core#real-name)))
(putp alias '##core#real-name (or original-real-name sym))))
aliases vars)
(append (map (lambda (x y) (cons x y)) vars aliases) se)) ; inline cons
;;; resolve symbol to global name
(define (##sys#globalize sym se)
(let loop1 ((sym sym))
(cond ((not (symbol? sym)) sym)
((getp sym '##core#macro-alias) =>
(lambda (a) (if (symbol? a) (loop1 a) sym)))
(else
(let loop ((se se)) ; ignores syntax bindings
(cond ((null? se)
(##sys#alias-global-hook sym #t #f)) ;XXX could hint at decl (3rd arg)
((and (eq? sym (caar se)) (symbol? (cdar se))) (cdar se))
(else (loop (cdr se)))))))))
;;; Macro handling
(define ##sys#macro-environment (make-parameter '()))
(define ##sys#chicken-macro-environment '()) ; used later in chicken.import.scm
(define ##sys#chicken-ffi-macro-environment '()) ; used later in foreign.import.scm
(define (##sys#ensure-transformer t #!optional loc)
(cond ((procedure? t) (##sys#slot (##sys#er-transformer t) 1)) ; DEPRECATED
((##sys#structure? t 'transformer) (##sys#slot t 1))
(else (##sys#error loc "expected syntax-transformer, but got" t))))
(define (##sys#extend-macro-environment name se transformer)
(let ((me (##sys#macro-environment))
(handler (##sys#ensure-transformer transformer name)))
(cond ((lookup name me) =>
(lambda (a)
(set-car! a se)
(set-car! (cdr a) handler)
a))
(else
(let ((data (list se handler)))
(##sys#macro-environment
(cons (cons name data) me))
data)))))
(define (##sys#copy-macro old new)
(let ((def (lookup old (##sys#macro-environment))))
(apply ##sys#extend-macro-environment new def) ) )
(define (##sys#macro? sym #!optional (senv (##sys#current-environment)))
(or (let ((l (lookup sym senv)))
(pair? l))
(and-let* ((l (lookup sym (##sys#macro-environment))))
(pair? l))))
(define (##sys#unregister-macro name)
(##sys#macro-environment
;; this builds up stack, but isn't used often anyway...
(let loop ((me (##sys#macro-environment)) (me2 '()))
(cond ((null? me) '())
((eq? name (caar me)) (cdr me))
(else (cons (car me) (loop (cdr me))))))))
(define (##sys#undefine-macro! name)
(##sys#unregister-macro name) )
;; The basic macro-expander
(define (##sys#expand-0 exp dse cs?)
(define (call-handler name handler exp se cs)
(dd "invoking macro: " name)
(dd `(STATIC-SE: ,@(map-se se)))
(handle-exceptions ex
;; modify error message in condition object to include
;; currently expanded macro-name
(##sys#abort
(if (and (##sys#structure? ex 'condition)
(memv 'exn (##sys#slot ex 1)) )
(##sys#make-structure
'condition
(##sys#slot ex 1)
(let copy ([ps (##sys#slot ex 2)])
(if (null? ps)
'()
(let ([p (car ps)]
[r (cdr ps)])
(if (and (equal? '(exn . message) p)
(pair? r)
(string? (car r)) )
(cons
'(exn . message)
(cons (string-append
"during expansion of ("
(##sys#slot name 1)
" ...) - "
(car r) )
(cdr r) ) )
(copy r) ) ) ) ) )
ex) )
(let ((exp2
(if cs
;; compiler-syntax may "fall through"
(fluid-let ((##sys#syntax-rules-mismatch (lambda (input) exp))) ; a bit of a hack
(handler exp se dse))
(handler exp se dse))) )
(when (and (not cs) (eq? exp exp2))
(##sys#syntax-error-hook
(string-append
"syntax transformer for `" (symbol->string name)
"' returns original form, which would result in endless expansion")
exp))
(dx `(,name --> ,exp2))
exp2)))
(define (expand head exp mdef)
(dd `(EXPAND:
,head
,(cond ((getp head '##core#macro-alias) =>
(lambda (a) (if (symbol? a) a '<macro>)) )
(else '_))
,exp
,(if (pair? mdef)
`(SE: ,@(map-se (car mdef)))
mdef)))
(cond ((not (list? exp))
(##sys#syntax-error-hook "invalid syntax in macro form" exp) )
((pair? mdef)
(values
;; force ref. opaqueness by passing dynamic se [what does this comment mean? I forgot ...]
(call-handler head (cadr mdef) exp (car mdef) #f)
#t))
(else (values exp #f)) ) )
(let loop ((exp exp))
(if (pair? exp)
(let ((head (car exp))
(body (cdr exp)) )
(if (symbol? head)
(let ((head2 (or (lookup head dse) head)))
(unless (pair? head2)
(set! head2 (or (lookup head2 (##sys#macro-environment)) head2)) )
(cond [(eq? head2 '##core#let)
(##sys#check-syntax 'let body '#(_ 2) #f dse)
(let ([bindings (car body)])
(cond [(symbol? bindings) ; expand named let
(##sys#check-syntax 'let body '(_ #((variable _) 0) . #(_ 1)) #f dse)
(let ([bs (cadr body)])
(values
`(##core#app
(##core#letrec*
([,bindings
(##core#loop-lambda
,(map (lambda (b) (car b)) bs) ,@(cddr body))])
,bindings)
,@(##sys#map cadr bs) )
#t) ) ]
[else (values exp #f)] ) ) ]
((and cs? (symbol? head2) (getp head2 '##compiler#compiler-syntax)) =>
(lambda (cs)
(let ((result (call-handler head (car cs) exp (cdr cs) #t)))
(cond ((eq? result exp) (expand head exp head2))
(else
(when ##sys#compiler-syntax-hook
(##sys#compiler-syntax-hook head result))
(loop result))))))
[else (expand head exp head2)] ) )
(values exp #f) ) )
(values exp #f) ) ) )
(define ##sys#compiler-syntax-hook #f)
(define ##sys#enable-runtime-macros #f)
;;; User-level macroexpansion
(define (expand exp #!optional (se (##sys#current-environment)) cs?)
(let loop ((exp exp))
(let-values (((exp2 m) (##sys#expand-0 exp se cs?)))
(if m
(loop exp2)
exp2) ) ) )
(define ##sys#expand expand)
;;; Extended (DSSSL-style) lambda lists
;
; Assumptions:
;
; 1) #!rest must come before #!key
; 2) default values may refer to earlier variables
; 3) optional/key args may be either variable or (variable default)
; 4) an argument marker may not be specified more than once
; 5) no special handling of extra keywords (no error)
; 6) default value of optional/key args is #f
; 7) mixing with dotted list syntax is allowed
(define (##sys#extended-lambda-list? llist)
(let loop ([llist llist])
(and (pair? llist)
(case (##sys#slot llist 0)
[(#!rest #!optional #!key) #t]
[else (loop (cdr llist))] ) ) ) )
(define ##sys#expand-extended-lambda-list
(let ([reverse reverse])
(lambda (llist0 body errh se)
(define (err msg) (errh msg llist0))
(define (->keyword s) (string->keyword (##sys#slot s 1)))
(let ([rvar #f]
[hasrest #f]
(%let* (macro-alias 'let* se))
(%lambda '##core#lambda)
(%opt (macro-alias 'optional se))
(%let-optionals* (macro-alias 'let-optionals* se))
(%let (macro-alias 'let se)))
(let loop ([mode 0] ; req=0, opt=1, rest=2, key=3, end=4
[req '()]
[opt '()]
[key '()]
[llist llist0] )
(cond [(null? llist)
(values
(if rvar (##sys#append (reverse req) rvar) (reverse req))
(let ([body
(if (null? key)
body
`((,%let*
,(map (lambda (k)
(let ([s (car k)])
`(,s (##sys#get-keyword
(##core#quote ,(->keyword (##sys#strip-syntax s))) ,(or hasrest rvar)
,@(if (pair? (cdr k))
`((,%lambda () ,@(cdr k)))
'())))))
(reverse key) )
,@body) ) ) ] )
(cond [(null? opt) body]
[(and (not hasrest) (null? key) (null? (cdr opt)))
`((,%let
([,(caar opt) (,%opt ,rvar ,(cadar opt))])
,@body) ) ]
[(and (not hasrest) (null? key))
`((,%let-optionals*
,rvar ,(reverse opt) ,@body))]
[else
`((,%let-optionals*
,rvar ,(##sys#append (reverse opt) (list (or hasrest rvar)))
,@body))] ) ) ) ]
[(symbol? llist)
(if (fx> mode 2)
(err "rest argument list specified more than once")
(begin
(unless rvar (set! rvar llist))
(set! hasrest llist)
(loop 4 req opt '() '()) ) ) ]
[(not (pair? llist))
(err "invalid lambda list syntax") ]
[else
(let* ((var (car llist))
(x (or (and (symbol? var) (not (eq? 3 mode)) (lookup var se)) var))
(r (cdr llist)))
(case x
[(#!optional)
(unless rvar (set! rvar (macro-alias 'tmp se)))
(if (eq? mode 0)
(loop 1 req '() '() r)
(err "`#!optional' argument marker in wrong context") ) ]
[(#!rest)
(if (fx<= mode 1)
(if (and (pair? r) (symbol? (car r)))
(begin
(if (not rvar) (set! rvar (car r)))
(set! hasrest (car r))
(loop 2 req opt '() (cdr r)) )
(err "invalid syntax of `#!rest' argument") )
(err "`#!rest' argument marker in wrong context") ) ]
[(#!key)
(if (not rvar) (set! rvar (macro-alias 'tmp se)))
(if (fx<= mode 2)
(loop 3 req opt '() r)
(err "`#!key' argument marker in wrong context") ) ]
[else
(cond [(symbol? var)
(case mode
[(0) (loop 0 (cons var req) '() '() r)]
[(1) (loop 1 req (cons (list var #f) opt) '() r)]
[(2) (err "invalid lambda list syntax after `#!rest' marker")]
[else (loop 3 req opt (cons (list var) key) r)] ) ]
[(and (list? var) (eq? 2 (length var)) (symbol? (car var)))
(case mode
[(0) (err "invalid required argument syntax")]
[(1) (loop 1 req (cons var opt) '() r)]
[(2) (err "invalid lambda list syntax after `#!rest' marker")]
[else (loop 3 req opt (cons var key) r)] ) ]
[else (err "invalid lambda list syntax")] ) ] ) ) ] ) ) ) ) ) )
;;; Error message for redefinition of currently used defining form
;
; (i.e.`"(define define ...)")
(define (##sys#defjam-error form)
(##sys#syntax-error-hook
"redefinition of currently used defining form" ; help me find something better
form))
;;; Expansion of multiple values assignments.
;
; Given a lambda list and a multi-valued expression, returns a form that
; will `set!` each variable to its corresponding value in order.
(define (##sys#expand-multiple-values-assignment formals expr)
(##sys#decompose-lambda-list
formals
(lambda (vars argc rest)
(let ((aliases (if (symbol? formals) '() (map gensym formals)))
(rest-alias (if (not rest) '() (gensym rest))))
`(##sys#call-with-values
(##core#lambda () ,expr)
(##core#lambda
,(append aliases rest-alias)
,@(map (lambda (v a) `(##core#set! ,v ,a)) vars aliases)
,@(cond
((null? formals) '((##core#undefined)))
((null? rest-alias) '())
(else `((##core#set! ,rest ,rest-alias))))))))))
;;; Expansion of bodies (and internal definitions)
;
; This code is disgustingly complex.
(define ##sys#define-definition)
(define ##sys#define-syntax-definition)
(define ##sys#define-values-definition)
(define ##sys#canonicalize-body
(lambda (body #!optional (se (##sys#current-environment)) cs?)
(define (comp s id)
(let ((f (lookup id se)))
(or (eq? s f)
(case s
((define) (if f (eq? f ##sys#define-definition) (eq? s id)))
((define-syntax) (if f (eq? f ##sys#define-syntax-definition) (eq? s id)))
((define-values) (if f (eq? f ##sys#define-values-definition) (eq? s id)))
(else (eq? s id))))))
(define (fini vars vals mvars mvals body)
(if (and (null? vars) (null? mvars))
(let loop ([body2 body] [exps '()])
(if (not (pair? body2))
(cons
'##core#begin
body) ; no more defines, otherwise we would have called `expand'
(let ([x (car body2)])
(if (and (pair? x)
(let ((d (car x)))
(and (symbol? d)
(or (comp 'define d)
(comp 'define-values d)))))
(cons
'##core#begin
(##sys#append (reverse exps) (list (expand body2))))
(loop (cdr body2) (cons x exps)) ) ) ) )
(let* ((vars (reverse vars))
(result
`(##core#let
,(##sys#map
(lambda (v) (##sys#list v '(##core#undefined)))
(foldl (lambda (l v) ; flatten multi-value formals
(##sys#append l (##sys#decompose-lambda-list
v (lambda (a _ _) a))))
vars
mvars))
,@(map (lambda (v x) `(##core#set! ,v ,x)) vars (reverse vals))
,@(map ##sys#expand-multiple-values-assignment
(reverse mvars)
(reverse mvals) )
,@body) ) )
(dd `(BODY: ,result))
result)))
(define (fini/syntax vars vals mvars mvals body)
(fini
vars vals mvars mvals
(let loop ((body body) (defs '()) (done #f))
(cond (done `((##core#letrec-syntax
,(map cdr (reverse defs)) ,@body) ))
((not (pair? body)) (loop body defs #t))
((and (list? (car body))
(>= 3 (length (car body)))
(symbol? (caar body))
(comp 'define-syntax (caar body)))
(let ((def (car body)))
(loop
(cdr body)
(cons (cond ((pair? (cadr def)) ; DEPRECATED
`(define-syntax ; (the first element is actually ignored)
,(caadr def)
(##sys#er-transformer
(##core#lambda ,(cdadr def) ,@(cddr def)))))
;; insufficient, if introduced by different expansions, but
;; better than nothing:
((eq? (car def) (cadr def))
(##sys#defjam-error def))
(else def))
defs)
#f)))
(else (loop body defs #t))))))
(define (expand body)
(let loop ([body body] [vars '()] [vals '()] [mvars '()] [mvals '()])
(if (not (pair? body))
(fini vars vals mvars mvals body)
(let* ((x (car body))
(rest (cdr body))
(exp1 (and (pair? x) (car x)))
(head (and exp1 (symbol? exp1) exp1)))
(if (not (symbol? head))
(fini vars vals mvars mvals body)
(cond
((comp 'define head)
(##sys#check-syntax 'define x '(_ _ . #(_ 0)) #f se)
(let loop2 ([x x])
(let ([head (cadr x)])
(cond [(not (pair? head))
(##sys#check-syntax 'define x '(_ variable . #(_ 0)) #f se)
(when (eq? (car x) head) ; see above
(##sys#defjam-error x))
(loop rest (cons head vars)
(cons (if (pair? (cddr x))
(caddr x)
'(##core#undefined) )
vals)
mvars mvals) ]
[(pair? (car head))
(##sys#check-syntax
'define x '(_ (_ . lambda-list) . #(_ 1)) #f se)
(loop2
(##sys#expand-curried-define head (cddr x) se)) ]
[else
(##sys#check-syntax
'define x
'(_ (variable . lambda-list) . #(_ 1)) #f se)
(loop rest
(cons (car head) vars)
(cons `(##core#lambda ,(cdr head) ,@(cddr x)) vals)
mvars mvals) ] ) ) ) )
((comp 'define-syntax head)
(##sys#check-syntax 'define-syntax x '(_ _ . #(_ 1)) se)
(fini/syntax vars vals mvars mvals body) )
((comp 'define-values head)
;;XXX check for any of the variables being `define-values'
(##sys#check-syntax 'define-values x '(_ lambda-list _) #f se)
(loop rest vars vals (cons (cadr x) mvars) (cons (caddr x) mvals)))
((comp '##core#begin head)
(loop (##sys#append (cdr x) rest) vars vals mvars mvals) )
(else
(if (or (memq head vars) (memq head mvars))
(fini vars vals mvars mvals body)
(let ((x2 (##sys#expand-0 x se cs?)))
(if (eq? x x2)
(fini vars vals mvars mvals body)
(loop (cons x2 rest)
vars vals mvars mvals) ) ) ) ) ) ) ) ) ) )
(expand body) ) )
;;; A simple expression matcher
(define match-expression
(lambda (exp pat vars)
(let ((env '()))
(define (mwalk x p)
(cond ((not (pair? p))
(cond ((assq p env) => (lambda (a) (equal? x (cdr a))))
((memq p vars)
(set! env (cons (cons p x) env))
#t)
(else (eq? x p)) ) )
((pair? x)
(and (mwalk (car x) (car p))
(mwalk (cdr x) (cdr p)) ) )
(else #f) ) )
(and (mwalk exp pat) env) ) ) )
;;; Expand "curried" lambda-list syntax for `define'
(define (##sys#expand-curried-define head body se)
(let ((name #f))
(define (loop head body)
(if (symbol? (car head))
(begin
(set! name (car head))
`(##core#lambda ,(cdr head) ,@body) )
(loop (car head) `((##core#lambda ,(cdr head) ,@body)) ) ))
(let ([exp (loop head body)])
(list 'define name exp) ) ) )
;;; General syntax checking routine:
(define ##sys#line-number-database #f)
(define ##sys#syntax-error-culprit #f)
(define ##sys#syntax-context '())
(define (syntax-error . args)
(apply ##sys#signal-hook #:syntax-error
(##sys#strip-syntax args)))
(define ##sys#syntax-error-hook syntax-error)
(define ##sys#syntax-error/context
(lambda (msg arg)
(define (syntax-imports sym)
(let loop ((defs (or (##sys#get (##sys#strip-syntax sym) '##core#db) '())))
(cond ((null? defs) '())
((eq? 'syntax (caar defs))
(cons (cadar defs) (loop (cdr defs))))
(else (loop (cdr defs))))))
(if (null? ##sys#syntax-context)
(##sys#syntax-error-hook msg arg)
(let ((out (open-output-string)))
(define (outstr str)
(##sys#print str #f out))
(let loop ((cx ##sys#syntax-context))
(cond ((null? cx) ; no unimported syntax found
(outstr msg)
(outstr ": ")
(##sys#print arg #t out)
(outstr "\ninside expression `(")
(##sys#print (##sys#strip-syntax (car ##sys#syntax-context)) #t out)
(outstr " ...)'"))
(else
(let* ((sym (##sys#strip-syntax (car cx)))
(us (syntax-imports sym)))
(cond ((pair? us)
(outstr msg)
(outstr ": ")
(##sys#print arg #t out)
(outstr "\n\n Perhaps you intended to use the syntax `(")
(##sys#print sym #t out)
(outstr " ...)' without importing it first.\n")
(if (fx= 1 (length us))
(outstr
(string-append
" Suggesting: `(import "
(symbol->string (car us))
")'"))
(outstr
(string-append
" Suggesting one of:\n"
(let loop ((lst us))
(if (null? lst)
""
(string-append
"\n (import " (symbol->string (car lst)) ")'"
(loop (cdr lst)))))))))
(else (loop (cdr cx))))))))
(##sys#syntax-error-hook (get-output-string out))))))
(define (##sys#syntax-rules-mismatch input)
(##sys#syntax-error-hook "no rule matches form" input))
(define (get-line-number sexp)
(and ##sys#line-number-database
(pair? sexp)
(let ([head (car sexp)])
(and (symbol? head)
(cond [(##sys#hash-table-ref ##sys#line-number-database head)
=> (lambda (pl)
(let ([a (assq sexp pl)])
(and a (cdr a)) ) ) ]
[else #f] ) ) ) ) )
(define-constant +default-argument-count-limit+ 99999)
(define ##sys#check-syntax
(lambda (id exp pat #!optional culprit (se (##sys#current-environment)))
(define (test x pred msg)
(unless (pred x) (err msg)) )
(define (err msg)
(let* ([sexp ##sys#syntax-error-culprit]
[ln (get-line-number sexp)] )
(##sys#syntax-error-hook
(if ln
(string-append "(" ln ") in `" (symbol->string id) "' - " msg)
(string-append "in `" (symbol->string id) "' - " msg) )
exp) ) )
(define (lambda-list? x)
(or (##sys#extended-lambda-list? x)
(let loop ((x x))
(cond ((null? x))
((symbol? x) (not (keyword? x)))
((pair? x)
(let ((s (car x)))
(and (symbol? s) (not (keyword? s))
(loop (cdr x)) ) ) )
(else #f) ) ) ) )
(define (proper-list? x)
(let loop ((x x))
(cond ((eq? x '()))
((pair? x) (loop (cdr x)))
(else #f) ) ) )
(when culprit (set! ##sys#syntax-error-culprit culprit))
(let walk ((x exp) (p pat))
(cond ((vector? p)
(let* ((p2 (vector-ref p 0))
(vlen (##sys#size p))
(min (if (fx> vlen 1)
(vector-ref p 1)
0) )
(max (cond ((eq? vlen 1) 1)
((fx> vlen 2) (vector-ref p 2))
(else +default-argument-count-limit+) ) ) )
(do ((x x (cdr x))
(n 0 (fx+ n 1)) )
((eq? x '())
(if (fx< n min)
(err "not enough arguments") ) )
(cond ((fx>= n max)
(err "too many arguments") )
((not (pair? x))
(err "not a proper list") )
(else (walk (car x) p2) ) ) ) ) )
((##sys#immediate? p)
(if (not (eq? p x)) (err "unexpected object")) )
((symbol? p)
(case p
((_) #t)
((pair) (test x pair? "pair expected"))
((variable) (test x symbol? "identifier expected"))
((symbol) (test x symbol? "symbol expected"))
((list) (test x proper-list? "proper list expected"))
((number) (test x number? "number expected"))
((string) (test x string? "string expected"))
((lambda-list) (test x lambda-list? "lambda-list expected"))
(else
(test
x
(lambda (y)
(let ((y2 (and (symbol? y) (lookup y se))))
(eq? (if (symbol? y2) y2 y) p)))
"missing keyword")) ) )
((not (pair? p))
(err "incomplete form") )
((not (pair? x)) (err "pair expected"))
(else
(walk (car x) (car p))
(walk (cdr x) (cdr p)) ) ) ) ) )
;;; explicit/implicit-renaming transformer
(define (make-er/ir-transformer handler explicit-renaming?)
(##sys#make-structure
'transformer
(lambda (form se dse)
(let ((renv '())) ; keep rename-environment for this expansion
(assert (list? se) "not a list" se) ;XXX remove later
(define (rename sym)
(cond ((pair? sym)
(cons (rename (car sym)) (rename (cdr sym))))
((vector? sym)
(list->vector (rename (vector->list sym))))
((not (symbol? sym)) sym)
((assq sym renv) =>
(lambda (a)
(dd `(RENAME/RENV: ,sym --> ,(cdr a)))
(cdr a)))
((lookup sym se) =>
(lambda (a)
(cond ((symbol? a)
;; Add an extra level of indirection for already aliased
;; symbols. This prevents aliased symbols from popping up
;; in syntax-stripped output.
(cond ((or (getp a '##core#aliased)
(getp a '##core#primitive))
(let ((a2 (macro-alias sym se)))
(dd `(RENAME/LOOKUP/ALIASED: ,sym --> ,a ==> ,a2))
(set! renv (cons (cons sym a2) renv))
a2))
(else (dd `(RENAME/LOOKUP: ,sym --> ,a))
(set! renv (cons (cons sym a) renv))
a)))
(else
(let ((a2 (macro-alias sym se)))
(dd `(RENAME/LOOKUP/MACRO: ,sym --> ,a2))
(set! renv (cons (cons sym a2) renv))
a2)))))
(else
(let ((a (macro-alias sym se)))
(dd `(RENAME: ,sym --> ,a))
(set! renv (cons (cons sym a) renv))
a))))
(define (compare s1 s2)
(let ((result
(cond ((pair? s1)
(and (pair? s2)
(compare (car s1) (car s2))
(compare (cdr s1) (cdr s2))))
((vector? s1)
(and (vector? s2)
(let ((len (vector-length s1)))
(and (fx= len (vector-length s2))
(do ((i 0 (fx+ i 1))
(f #t (compare (vector-ref s1 i) (vector-ref s2 i))))
((or (fx>= i len) (not f)) f))))))
((and (symbol? s1) (symbol? s2))
(let ((ss1 (or (getp s1 '##core#macro-alias)
(lookup2 1 s1 dse)
s1) )
(ss2 (or (getp s2 '##core#macro-alias)
(lookup2 2 s2 dse)
s2) ) )
(cond ((symbol? ss1)
(cond ((symbol? ss2)
(eq? (or (getp ss1 '##core#primitive) ss1)
(or (getp ss2 '##core#primitive) ss2)))
((assq ss1 (##sys#macro-environment)) =>
(lambda (a) (eq? (cdr a) ss2)))
(else #f) ) )
((symbol? ss2)
(cond ((assq ss2 (##sys#macro-environment)) =>
(lambda (a) (eq? ss1 (cdr a))))
(else #f)))
(else (eq? ss1 ss2)))))
(else (eq? s1 s2))) ) )
(dd `(COMPARE: ,s1 ,s2 --> ,result))
result))
(define (lookup2 n sym dse)
(let ((r (lookup sym dse)))
(dd " (lookup/DSE " (list n) ": " sym " --> "
(if (and r (pair? r))
'<macro>
r)
")")
r))
(define (assq-reverse s l)
(cond
((null? l) #f)
((eq? (cdar l) s) (car l))
(else (assq-reverse s (cdr l)))))
(define (mirror-rename sym)
(cond ((pair? sym)
(cons (mirror-rename (car sym)) (mirror-rename (cdr sym))))
((vector? sym)
(list->vector (mirror-rename (vector->list sym))))
((not (symbol? sym)) sym)
(else ; Code stolen from ##sys#strip-syntax
(let ((renamed (lookup sym se) ) )
(cond ((assq-reverse sym renv) =>
(lambda (a)
(dd "REVERSING RENAME: " sym " --> " (car a)) (car a)))
((not renamed)
(dd "IMPLICITLY RENAMED: " sym) (rename sym))
((pair? renamed)
(dd "MACRO: " sym) (rename sym))
((getp sym '##core#real-name) =>
(lambda (name)
(dd "STRIP SYNTAX ON " sym " ---> " name)
name))
;; Rename builtin aliases so strip-syntax can still
;; access symbols as entered by the user
(else (let ((implicitly-renamed (rename sym)))
(dd "BUILTIN ALIAS: " sym " as " renamed
" --> " implicitly-renamed)
implicitly-renamed)))))))
(if explicit-renaming?
;; Let the user handle renaming
(handler form rename compare)
;; Implicit renaming:
;; Rename everything in the input first, feed it to the transformer
;; and then swap out all renamed identifiers by their non-renamed
;; versions, and vice versa. User can decide when to inject code
;; unhygienically this way.
(mirror-rename (handler (rename form) rename compare)) ) ) )))
(define (er-macro-transformer handler) (make-er/ir-transformer handler #t))
(define (ir-macro-transformer handler) (make-er/ir-transformer handler #f))
(define ##sys#er-transformer er-macro-transformer)
(define ##sys#ir-transformer ir-macro-transformer)
;;; Macro definitions:
(##sys#extend-macro-environment
'import '()
(##sys#er-transformer
(cut ##sys#expand-import <> <> <> ##sys#current-environment ##sys#macro-environment
#f #f 'import) ) )
(##sys#extend-macro-environment
'import-for-syntax '()
(##sys#er-transformer
(cut ##sys#expand-import <> <> <> ##sys#current-meta-environment
##sys#meta-macro-environment
#t #f 'import-for-syntax) ) )
(##sys#extend-macro-environment
'reexport '()
(##sys#er-transformer
(cut ##sys#expand-import <> <> <> ##sys#current-environment ##sys#macro-environment
#f #t 'reexport) ) )
;; contains only "import[-for-syntax]" and "reexport"
(define ##sys#initial-macro-environment (##sys#macro-environment))
(##sys#extend-macro-environment
'lambda
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'lambda x '(_ lambda-list . #(_ 1)))
`(##core#lambda ,@(cdr x)))))
(##sys#extend-macro-environment
'quote
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'quote x '(_ _))
`(##core#quote ,(cadr x)))))
(##sys#extend-macro-environment
'syntax
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'syntax x '(_ _))
`(##core#syntax ,(cadr x)))))
(##sys#extend-macro-environment
'if
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'if x '(_ _ _ . #(_)))
`(##core#if ,@(cdr x)))))
(##sys#extend-macro-environment
'begin
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'begin x '(_ . #(_ 0)))
`(##core#begin ,@(cdr x)))))
(set! ##sys#define-definition
(##sys#extend-macro-environment
'define
'()
(##sys#er-transformer
(lambda (x r c)
(##sys#check-syntax 'define x '(_ . #(_ 1)))
(let loop ((form x))
(let ((head (cadr form))
(body (cddr form)) )
(cond ((not (pair? head))
(##sys#check-syntax 'define form '(_ symbol . #(_ 0 1)))
(let ((name (or (getp head '##core#macro-alias) head)))
(##sys#register-export name (##sys#current-module)))
(when (c (r 'define) head)