-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.scm
1786 lines (1607 loc) · 57.3 KB
/
eval.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
;;;; eval.scm - Interpreter for CHICKEN
;
; Copyright (c) 2008-2015, The CHICKEN Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; 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.
(declare
(unit eval)
(uses expand modules)
(hide pds pdss pxss d)
(not inline ##sys#repl-read-hook ##sys#repl-print-hook
##sys#read-prompt-hook ##sys#alias-global-hook ##sys#user-read-hook
##sys#syntax-error-hook))
#>
#ifndef C_INSTALL_EGG_HOME
# define C_INSTALL_EGG_HOME "."
#endif
#ifndef C_INSTALL_SHARE_HOME
# define C_INSTALL_SHARE_HOME NULL
#endif
#ifndef C_BINARY_VERSION
# define C_BINARY_VERSION 0
#endif
#define C_rnd_fix() (C_fix(rand()))
<#
(include "common-declarations.scm")
(define-syntax d (syntax-rules () ((_ . _) (void))))
(define-foreign-variable install-egg-home c-string "C_INSTALL_EGG_HOME")
(define-foreign-variable installation-home c-string "C_INSTALL_SHARE_HOME")
(define-foreign-variable binary-version int "C_BINARY_VERSION")
(define-foreign-variable uses-soname? bool "C_USES_SONAME")
(define-foreign-variable install-lib-name c-string "C_INSTALL_LIB_NAME")
(define ##sys#core-library-modules
'(extras lolevel utils files tcp irregex posix srfi-1 srfi-4 srfi-13
srfi-14 srfi-18 srfi-69 data-structures ports))
(define ##sys#core-syntax-modules
'(chicken-syntax chicken-ffi-syntax))
(define ##sys#explicit-library-modules '())
(define default-dynamic-load-libraries
`(,(string-append "lib" install-lib-name)))
(define-constant cygwin-default-dynamic-load-libraries '("cygchicken-0"))
(define-constant macosx-load-library-extension ".dylib")
(define-constant windows-load-library-extension ".dll")
(define-constant hppa-load-library-extension ".sl")
(define-constant default-load-library-extension ".so")
(define-constant environment-table-size 301)
(define-constant source-file-extension ".scm")
(define-constant setup-file-extension "setup-info")
(define-constant repository-environment-variable "CHICKEN_REPOSITORY")
(define-constant prefix-environment-variable "CHICKEN_PREFIX")
; these are actually in unit extras, but that is used by default
; srfi-12 in unit library
; srfi-98 partially in unit posix
(define-constant builtin-features
'(chicken srfi-2 srfi-6 srfi-10 srfi-12 srfi-23 srfi-28 srfi-30 srfi-39
srfi-55 srfi-88 srfi-98) )
(define-constant builtin-features/compiled
'(srfi-8 srfi-9 srfi-11 srfi-15 srfi-16 srfi-17 srfi-26) )
(define ##sys#chicken-prefix
(let ((prefix (and-let* ((p (get-environment-variable prefix-environment-variable)))
(##sys#string-append
p
(if (memq (string-ref p (fx- (##sys#size p) 1)) '(#\\ #\/)) "" "/")) ) ) )
(lambda (#!optional dir)
(and prefix
(if dir (##sys#string-append prefix dir) prefix) ) ) ) )
;;; System settings
(define (chicken-home)
(or (##sys#chicken-prefix "share/chicken")
installation-home) )
;;; Lo-level hashtable support:
(define ##sys#hash-symbol
(let ([cache-s #f]
[cache-h #f]
;; NOTE: All low-level hash tables share the same randomization factor
[rand (##core#inline "C_rnd_fix")] )
(lambda (s n)
(if (eq? s cache-s)
(##core#inline "C_fixnum_modulo" cache-h n)
(begin
(set! cache-s s)
(set! cache-h (##core#inline "C_u_i_string_hash" (##sys#slot s 1) rand))
(##core#inline "C_fixnum_modulo" cache-h n))))))
(define (##sys#hash-table-ref ht key)
(let loop ((bucket (##sys#slot ht (##sys#hash-symbol key (##core#inline "C_block_size" ht)))))
(and (not (eq? '() bucket))
(if (eq? key (##sys#slot (##sys#slot bucket 0) 0))
(##sys#slot (##sys#slot bucket 0) 1)
(loop (##sys#slot bucket 1))))))
(define (##sys#hash-table-set! ht key val)
(let* ((k (##sys#hash-symbol key (##core#inline "C_block_size" ht)))
(ib (##sys#slot ht k)))
(let loop ((bucket ib))
(if (eq? '() bucket)
(##sys#setslot ht k (cons (cons key val) ib))
(if (eq? key (##sys#slot (##sys#slot bucket 0) 0))
(##sys#setslot (##sys#slot bucket 0) 1 val)
(loop (##sys#slot bucket 1)))))))
(define (##sys#hash-table-update! ht key updtfunc valufunc)
(##sys#hash-table-set! ht key (updtfunc (or (##sys#hash-table-ref ht key) (valufunc)))) )
(define (##sys#hash-table-for-each p ht)
(let ((len (##core#inline "C_block_size" ht)))
(do ((i 0 (fx+ i 1)))
((fx>= i len))
(##sys#for-each (lambda (bucket) (p (##sys#slot bucket 0) (##sys#slot bucket 1)))
(##sys#slot ht i) ) ) ) )
(define ##sys#hash-table-location
(let ([unbound (##sys#slot '##sys#arbitrary-unbound-symbol 0)])
(lambda (ht key addp)
(let* ([k (##sys#hash-symbol key (##sys#size ht))]
[bucket0 (##sys#slot ht k)] )
(let loop ([bucket bucket0])
(if (null? bucket)
(and addp
(let ([p (vector key unbound #t)])
(##sys#setslot ht k (cons p bucket0))
p) )
(let ([b (##sys#slot bucket 0)])
(if (eq? key (##sys#slot b 0))
b
(loop (##sys#slot bucket 1)) ) ) ) ) ) ) ) )
(define (##sys#hash-table-size ht)
(let loop ((len (##sys#size ht)) (bkt 0) (size 0))
(if (fx= bkt len)
size
(loop len (fx+ bkt 1) (fx+ size (##sys#length (##sys#slot ht bkt)))))))
;;; Compile lambda to closure:
(define (##sys#eval-decorator p ll h cntr)
(##sys#decorate-lambda
p
(lambda (x) (and (not (##sys#immediate? x)) (##core#inline "C_lambdainfop" x)))
(lambda (p i)
(##sys#setslot
p i
(##sys#make-lambda-info
(let ((o (open-output-string)))
(write ll o)
(get-output-string o))))
p) ) )
(define ##sys#unbound-in-eval #f)
(define ##sys#eval-debug-level (make-parameter 1))
(define ##sys#compile-to-closure
(let ([write write]
[reverse reverse]
[with-input-from-file with-input-from-file]
[unbound (##sys#slot '##sys#arbitrary-unbound-symbol 0)]
[display display] )
(lambda (exp env se #!optional cntr evalenv static)
(define (find-id id se) ; ignores macro bindings
(cond ((null? se) #f)
((and (eq? id (caar se)) (symbol? (cdar se))) (cdar se))
(else (find-id id (cdr se)))))
(define (rename var se)
(cond ((find-id var se))
((##sys#get var '##core#macro-alias))
(else var)))
(define (lookup var0 e se)
(let ((var (rename var0 se)))
(d `(LOOKUP/EVAL: ,var0 ,var ,e ,(map (lambda (x) (car x)) se)))
(let loop ((envs e) (ei 0))
(cond ((null? envs) (values #f var))
((posq var (##sys#slot envs 0)) => (lambda (p) (values ei p)))
(else (loop (##sys#slot envs 1) (fx+ ei 1))) ) ) ))
(define (posq x lst)
(let loop ((lst lst) (i 0))
(cond ((null? lst) #f)
((eq? x (##sys#slot lst 0)) i)
(else (loop (##sys#slot lst 1) (fx+ i 1))) ) ) )
(define (emit-trace-info tf info cntr e v)
(when tf
(##core#inline
"C_emit_eval_trace_info"
info
(##sys#make-structure 'frameinfo cntr e v)
##sys#current-thread) ) )
(define (emit-syntax-trace-info tf info cntr)
(when tf
(##core#inline
"C_emit_syntax_trace_info"
info
cntr
##sys#current-thread) ) )
(define (decorate p ll h cntr)
(##sys#eval-decorator p ll h cntr) )
(define (compile x e h tf cntr se)
(cond ((keyword? x) (lambda v x))
((symbol? x)
(receive (i j) (lookup x e se)
(cond ((not i)
(let ((var (cond ((not (symbol? j)) x) ; syntax?
((not (assq x se))
(and (not static)
(##sys#alias-global-hook j #f cntr)))
(else (or (##sys#get j '##core#primitive) j)))))
(when (and ##sys#unbound-in-eval
(or (not var)
(not (##sys#symbol-has-toplevel-binding? var))))
(set! ##sys#unbound-in-eval
(cons (cons var cntr) ##sys#unbound-in-eval)) )
(cond ((not var)
(lambda (v)
(##sys#error "unbound variable" x)))
((##sys#symbol-has-toplevel-binding? var)
(lambda v (##sys#slot var 0)))
(else
(lambda v (##core#inline "C_fast_retrieve" var))))))
(else
(case i
((0) (lambda (v)
(##sys#slot (##sys#slot v 0) j)))
((1) (lambda (v)
(##sys#slot (##sys#slot (##sys#slot v 1) 0) j)))
((2) (lambda (v)
(##sys#slot
(##sys#slot (##sys#slot (##sys#slot v 1) 1) 0)
j)))
((3) (lambda (v)
(##sys#slot
(##sys#slot
(##sys#slot (##sys#slot (##sys#slot v 1) 1) 1)
0)
j)))
(else
(lambda (v)
(##sys#slot (##core#inline "C_u_i_list_ref" v i) j))))))))
[(##sys#number? x)
(case x
[(-1) (lambda v -1)]
[(0) (lambda v 0)]
[(1) (lambda v 1)]
[(2) (lambda v 2)]
[else (lambda v x)] ) ]
[(boolean? x)
(if x
(lambda v #t)
(lambda v #f) ) ]
((or (char? x)
(eof-object? x)
(string? x)
(blob? x)
(vector? x)
(##sys#srfi-4-vector? x))
(lambda v x) )
[(not (pair? x))
(##sys#syntax-error/context "illegal non-atomic object" x)]
[(symbol? (##sys#slot x 0))
(emit-syntax-trace-info tf x cntr)
(let ((x2 (##sys#expand x se #f)))
(d `(EVAL/EXPANDED: ,x2))
(if (not (eq? x2 x))
(compile x2 e h tf cntr se)
(let ((head (rename (##sys#slot x 0) se)))
;; here we did't resolve ##core#primitive, but that is done in compile-call (via
;; a normal walking of the operator)
(case head
[(##core#quote)
(let* ((c (##sys#strip-syntax (cadr x))))
(case c
[(-1) (lambda v -1)]
[(0) (lambda v 0)]
[(1) (lambda v 1)]
[(2) (lambda v 2)]
[(#t) (lambda v #t)]
[(#f) (lambda v #f)]
[(()) (lambda v '())]
[else (lambda v c)] ) ) ]
((##core#syntax)
(let ((c (cadr x)))
(lambda v c)))
[(##core#check)
(compile (cadr x) e h tf cntr se) ]
[(##core#immutable)
(compile (cadr x) e #f tf cntr se) ]
[(##core#undefined) (lambda (v) (##core#undefined))]
[(##core#if)
(let* ([test (compile (cadr x) e #f tf cntr se)]
[cns (compile (caddr x) e #f tf cntr se)]
[alt (if (pair? (cdddr x))
(compile (cadddr x) e #f tf cntr se)
(compile '(##core#undefined) e #f tf cntr se) ) ] )
(lambda (v) (if (##core#app test v) (##core#app cns v) (##core#app alt v))) ) ]
[(##core#begin ##core#toplevel-begin)
(let* ((body (##sys#slot x 1))
(len (length body)) )
(case len
[(0) (compile '(##core#undefined) e #f tf cntr se)]
[(1) (compile (##sys#slot body 0) e #f tf cntr se)]
[(2) (let* ([x1 (compile (##sys#slot body 0) e #f tf cntr se)]
[x2 (compile (cadr body) e #f tf cntr se)] )
(lambda (v) (##core#app x1 v) (##core#app x2 v)) ) ]
[else
(let* ([x1 (compile (##sys#slot body 0) e #f tf cntr se)]
[x2 (compile (cadr body) e #f tf cntr se)]
[x3 (compile `(##core#begin ,@(##sys#slot (##sys#slot body 1) 1)) e #f tf cntr se)] )
(lambda (v) (##core#app x1 v) (##core#app x2 v) (##core#app x3 v)) ) ] ) ) ]
[(##core#set!)
(let ((var (cadr x)))
(receive (i j) (lookup var e se)
(let ((val (compile (caddr x) e var tf cntr se)))
(cond [(not i)
(when ##sys#notices-enabled
(and-let* ((a (assq var (##sys#current-environment)))
((symbol? (cdr a))))
(##sys#notice "assignment to imported value binding" var)))
(let ((var
(if (not (assq x se)) ;XXX this looks wrong
(and (not static)
(##sys#alias-global-hook j #t cntr))
(or (##sys#get j '##core#primitive) j))))
(if (not var) ; static
(lambda (v)
(##sys#error 'eval "environment is not mutable" evalenv var)) ;XXX var?
(lambda (v)
(##sys#setslot var 0 (##core#app val v))) ) ) ]
[(zero? i) (lambda (v) (##sys#setslot (##sys#slot v 0) j (##core#app val v)))]
[else
(lambda (v)
(##sys#setslot
(##core#inline "C_u_i_list_ref" v i) j (##core#app val v)) ) ] ) ) ) ) ]
[(##core#let)
(let* ([bindings (cadr x)]
[n (length bindings)]
[vars (map (lambda (x) (car x)) bindings)]
(aliases (map gensym vars))
[e2 (cons aliases e)]
(se2 (##sys#extend-se se vars aliases))
[body (##sys#compile-to-closure
(##sys#canonicalize-body (cddr x) se2 #f)
e2 se2 cntr evalenv static) ] )
(case n
[(1) (let ([val (compile (cadar bindings) e (car vars) tf cntr se)])
(lambda (v)
(##core#app body (cons (vector (##core#app val v)) v)) ) ) ]
[(2) (let ([val1 (compile (cadar bindings) e (car vars) tf cntr se)]
[val2 (compile (cadadr bindings) e (cadr vars) tf cntr se)] )
(lambda (v)
(##core#app body (cons (vector (##core#app val1 v) (##core#app val2 v)) v)) ) ) ]
[(3) (let* ([val1 (compile (cadar bindings) e (car vars) tf cntr se)]
[val2 (compile (cadadr bindings) e (cadr vars) tf cntr se)]
[t (cddr bindings)]
[val3 (compile (cadar t) e (caddr vars) tf cntr se)] )
(lambda (v)
(##core#app
body
(cons (vector (##core#app val1 v) (##core#app val2 v) (##core#app val3 v)) v)) ) ) ]
[(4) (let* ([val1 (compile (cadar bindings) e (car vars) tf cntr se)]
[val2 (compile (cadadr bindings) e (cadr vars) tf cntr se)]
[t (cddr bindings)]
[val3 (compile (cadar t) e (caddr vars) tf cntr se)]
[val4 (compile (cadadr t) e (cadddr vars) tf cntr se)] )
(lambda (v)
(##core#app
body
(cons (vector (##core#app val1 v)
(##core#app val2 v)
(##core#app val3 v)
(##core#app val4 v))
v)) ) ) ]
[else
(let ([vals (map (lambda (x) (compile (cadr x) e (car x) tf cntr se)) bindings)])
(lambda (v)
(let ([v2 (##sys#make-vector n)])
(do ([i 0 (fx+ i 1)]
[vlist vals (##sys#slot vlist 1)] )
((fx>= i n))
(##sys#setslot v2 i (##core#app (##sys#slot vlist 0) v)) )
(##core#app body (cons v2 v)) ) ) ) ] ) ) ]
((##core#letrec*)
(let ((bindings (cadr x))
(body (cddr x)) )
(compile
`(##core#let
,(##sys#map (lambda (b)
(list (car b) '(##core#undefined)))
bindings)
,@(##sys#map (lambda (b)
`(##core#set! ,(car b) ,(cadr b)))
bindings)
(##core#let () ,@body) )
e h tf cntr se)))
((##core#letrec)
(let* ((bindings (cadr x))
(vars (map car bindings))
(tmps (map gensym vars))
(body (cddr x)) )
(compile
`(##core#let
,(map (lambda (b)
(list (car b) '(##core#undefined)))
bindings)
(##core#let ,(map (lambda (t b) (list t (cadr b))) tmps bindings)
,@(map (lambda (v t)
`(##core#set! ,v ,t))
vars tmps)
(##core#let () ,@body) ) )
e h tf cntr se)))
[(##core#lambda)
(##sys#check-syntax 'lambda x '(_ lambda-list . #(_ 1)) #f se)
(let* ([llist (cadr x)]
[body (cddr x)]
[info (cons (or h '?) llist)] )
(when (##sys#extended-lambda-list? llist)
(set!-values
(llist body)
(##sys#expand-extended-lambda-list
llist body ##sys#syntax-error-hook se) ) )
(##sys#decompose-lambda-list
llist
(lambda (vars argc rest)
(let* ((aliases (map gensym vars))
(se2 (##sys#extend-se se vars aliases))
(e2 (cons aliases e))
(body
(##sys#compile-to-closure
(##sys#canonicalize-body body se2 #f)
e2 se2 (or h cntr) evalenv static) ) )
(case argc
[(0) (if rest
(lambda (v)
(decorate
(lambda r
(##core#app body (cons (vector r) v)))
info h cntr) )
(lambda (v)
(decorate
(lambda () (##core#app body (cons #f v)))
info h cntr) ) ) ]
[(1) (if rest
(lambda (v)
(decorate
(lambda (a1 . r)
(##core#app body (cons (vector a1 r) v)))
info h cntr) )
(lambda (v)
(decorate
(lambda (a1)
(##core#app body (cons (vector a1) v)))
info h cntr) ) ) ]
[(2) (if rest
(lambda (v)
(decorate
(lambda (a1 a2 . r)
(##core#app body (cons (vector a1 a2 r) v)))
info h cntr) )
(lambda (v)
(decorate
(lambda (a1 a2)
(##core#app body (cons (vector a1 a2) v)))
info h cntr) ) ) ]
[(3) (if rest
(lambda (v)
(decorate
(lambda (a1 a2 a3 . r)
(##core#app body (cons (vector a1 a2 a3 r) v)))
info h cntr) )
(lambda (v)
(decorate
(lambda (a1 a2 a3)
(##core#app body (cons (vector a1 a2 a3) v)))
info h cntr) ) ) ]
[(4) (if rest
(lambda (v)
(decorate
(lambda (a1 a2 a3 a4 . r)
(##core#app body (cons (vector a1 a2 a3 a4 r) v)))
info h cntr) )
(lambda (v)
(decorate
(lambda (a1 a2 a3 a4)
(##core#app body (##sys#cons (##sys#vector a1 a2 a3 a4) v)))
info h cntr) ) ) ]
[else
(if rest
(lambda (v)
(decorate
(lambda as
(##core#app
body
(##sys#cons (apply ##sys#vector (fudge-argument-list argc as)) v)) )
info h cntr) )
(lambda (v)
(decorate
(lambda as
(let ([len (length as)])
(if (not (fx= len argc))
(##sys#error "bad argument count" argc len)
(##core#app body (##sys#cons (apply ##sys#vector as) v)))))
info h cntr) ) ) ] ) ) ) ) ) ]
((##core#let-syntax)
(let ((se2 (append
(map (lambda (b)
(list
(car b)
se
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
(##sys#strip-syntax (car b)))))
(cadr x) )
se) ) )
(compile
(##sys#canonicalize-body (cddr x) se2 #f)
e #f tf cntr se2)))
((##core#letrec-syntax)
(let* ((ms (map (lambda (b)
(list
(car b)
#f
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
(##sys#strip-syntax (car b)))))
(cadr x) ) )
(se2 (append ms se)) )
(for-each
(lambda (sb)
(set-car! (cdr sb) se2) )
ms)
(compile
(##sys#canonicalize-body (cddr x) se2 #f)
e #f tf cntr se2)))
((##core#define-syntax)
(let* ((var (cadr x))
(body (caddr x))
(name (rename var se)))
(when (and static (not (assq var se)))
(##sys#error 'eval "environment is not mutable" evalenv var))
(##sys#register-syntax-export
name (##sys#current-module)
body) ; not really necessary, it only shouldn't be #f
(##sys#extend-macro-environment
name
(##sys#current-environment)
(##sys#eval/meta body))
(compile '(##core#undefined) e #f tf cntr se) ) )
((##core#define-compiler-syntax)
(compile '(##core#undefined) e #f tf cntr se))
((##core#let-compiler-syntax)
(compile
(##sys#canonicalize-body (cddr x) se #f)
e #f tf cntr se))
((##core#include)
(compile
`(##core#begin
,@(##sys#include-forms-from-file (cadr x)))
e #f tf cntr se))
((##core#let-module-alias)
(##sys#with-module-aliases
(map (lambda (b)
(##sys#check-syntax 'functor b '(symbol symbol))
(##sys#strip-syntax b))
(cadr x))
(lambda ()
(compile `(##core#begin ,@(cddr x)) e #f tf cntr se))))
((##core#module)
(let* ((x (##sys#strip-syntax x))
(name (cadr x))
(exports
(or (eq? #t (caddr x))
(map (lambda (exp)
(cond ((symbol? exp) exp)
((and (pair? exp)
(let loop ((exp exp))
(or (null? exp)
(and (symbol? (car exp))
(loop (cdr exp))))))
exp)
(else
(##sys#syntax-error-hook
'module
"invalid export syntax" exp name))))
(caddr x)))))
(when (##sys#current-module)
(##sys#syntax-error-hook 'module "modules may not be nested" name))
(parameterize ((##sys#current-module
(##sys#register-module name exports))
(##sys#current-environment '())
(##sys#macro-environment
##sys#initial-macro-environment)
(##sys#module-alias-environment
(##sys#module-alias-environment)))
(##sys#with-property-restore
(lambda ()
(let loop ((body (cdddr x)) (xs '()))
(if (null? body)
(let ((xs (reverse xs)))
(##sys#finalize-module (##sys#current-module))
(lambda (v)
(let loop2 ((xs xs))
(if (null? xs)
(##sys#void)
(let ((n (cdr xs)))
(cond ((pair? n)
((car xs) v)
(loop2 n))
(else
((car xs) v))))))))
(loop
(cdr body)
(cons (compile
(car body)
'() #f tf cntr
(##sys#current-environment))
xs))))) ) )))
[(##core#loop-lambda)
(compile `(,(rename 'lambda se) ,@(cdr x)) e #f tf cntr se) ]
[(##core#require-for-syntax)
(let ([ids (map (lambda (x) (##sys#eval/meta x))
(cdr x))])
(apply ##sys#require ids)
(let ([rs (##sys#lookup-runtime-requirements ids)])
(compile
(if (null? rs)
'(##core#undefined)
`(##sys#require ,@(map (lambda (x) `',x) rs)) )
e #f tf cntr se) ) ) ]
[(##core#require-extension)
(let ((imp? (caddr x)))
(compile
(let loop ([ids (##sys#strip-syntax (cadr x))])
(if (null? ids)
'(##core#undefined)
(let-values (((exp f real-id)
(##sys#do-the-right-thing (car ids) #f imp?)))
`(##core#begin ,exp ,(loop (cdr ids))) ) ) )
e #f tf cntr se) ) ]
[(##core#elaborationtimeonly ##core#elaborationtimetoo) ; <- Note this!
(##sys#eval/meta (cadr x))
(compile '(##core#undefined) e #f tf cntr se) ]
[(##core#compiletimetoo)
(compile (cadr x) e #f tf cntr se) ]
[(##core#compiletimeonly ##core#callunit)
(compile '(##core#undefined) e #f tf cntr se) ]
[(##core#declare)
(if (memq #:compiling ##sys#features)
(for-each (lambda (d) (##compiler#process-declaration d se)) (cdr x))
(##sys#notice
"declarations are ignored in interpreted code"
x) )
(compile '(##core#undefined) e #f tf cntr se) ]
[(##core#define-inline ##core#define-constant)
(compile `(,(rename 'define se) ,@(cdr x)) e #f tf cntr se) ]
[(##core#primitive ##core#inline ##core#inline_allocate ##core#foreign-lambda
##core#define-foreign-variable
##core#define-external-variable ##core#let-location
##core#foreign-primitive ##core#location
##core#foreign-lambda* ##core#define-foreign-type)
(##sys#syntax-error-hook "cannot evaluate compiler-special-form" x) ]
[(##core#app)
(compile-call (cdr x) e tf cntr se) ]
((##core#the)
(compile (cadddr x) e h tf cntr se))
((##core#typecase)
;; drops exp and requires "else" clause
(cond ((assq 'else (##sys#strip-syntax (cdddr x))) =>
(lambda (cl)
(compile (cadr cl) e h tf cntr se)))
(else
(##sys#syntax-error-hook
'compiler-typecase
"no `else-clause' in unresolved `compiler-typecase' form"
x))))
(else
(fluid-let ((##sys#syntax-context (cons head ##sys#syntax-context)))
(compile-call x e tf cntr se)))))))]
[else
(emit-syntax-trace-info tf x cntr)
(compile-call x e tf cntr se)] ) )
(define (fudge-argument-list n alst)
(if (null? alst)
(list alst)
(do ((n n (fx- n 1))
(c 0 (fx+ c 1))
(args alst
(if (eq? '() args)
(##sys#error "bad argument count" n c)
(##sys#slot args 1)))
(last #f args) )
((fx= n 0)
(##sys#setslot last 1 (list args))
alst) ) ) )
(define (checked-length lst)
(let loop ([lst lst] [n 0])
(cond [(null? lst) n]
[(pair? lst) (loop (##sys#slot lst 1) (fx+ n 1))]
[else #f] ) ) )
(define (compile-call x e tf cntr se)
(let* ((head (##sys#slot x 0))
(fn (if (procedure? head)
(lambda _ head)
(compile (##sys#slot x 0) e #f tf cntr se)))
(args (##sys#slot x 1))
(argc (checked-length args))
(info x) )
(case argc
[(#f) (##sys#syntax-error/context "malformed expression" x)]
[(0) (lambda (v)
(emit-trace-info tf info cntr e v)
((##core#app fn v)))]
[(1) (let ([a1 (compile (##sys#slot args 0) e #f tf cntr se)])
(lambda (v)
(emit-trace-info tf info cntr e v)
((##core#app fn v) (##core#app a1 v))) ) ]
[(2) (let* ([a1 (compile (##sys#slot args 0) e #f tf cntr se)]
[a2 (compile (##core#inline "C_u_i_list_ref" args 1) e #f tf cntr se)] )
(lambda (v)
(emit-trace-info tf info cntr e v)
((##core#app fn v) (##core#app a1 v) (##core#app a2 v))) ) ]
[(3) (let* ([a1 (compile (##sys#slot args 0) e #f tf cntr se)]
[a2 (compile (##core#inline "C_u_i_list_ref" args 1) e #f tf cntr se)]
[a3 (compile (##core#inline "C_u_i_list_ref" args 2) e #f tf cntr se)] )
(lambda (v)
(emit-trace-info tf info cntr e v)
((##core#app fn v) (##core#app a1 v) (##core#app a2 v) (##core#app a3 v))) ) ]
[(4) (let* ([a1 (compile (##sys#slot args 0) e #f tf cntr se)]
[a2 (compile (##core#inline "C_u_i_list_ref" args 1) e #f tf cntr se)]
[a3 (compile (##core#inline "C_u_i_list_ref" args 2) e #f tf cntr se)]
[a4 (compile (##core#inline "C_u_i_list_ref" args 3) e #f tf cntr se)] )
(lambda (v)
(emit-trace-info tf info cntr e v)
((##core#app fn v) (##core#app a1 v) (##core#app a2 v) (##core#app a3 v) (##core#app a4 v))) ) ]
[else (let ([as (##sys#map (lambda (a) (compile a e #f tf cntr se)) args)])
(lambda (v)
(emit-trace-info tf info cntr e v)
(apply (##core#app fn v) (##sys#map (lambda (a) (##core#app a v)) as))) ) ] ) ) )
(compile exp env #f (fx> (##sys#eval-debug-level) 0) cntr se) ) ) )
;;; evaluate in the macro-expansion/compile-time environment
(define (##sys#eval/meta form)
(let ((oldcm (##sys#current-module))
(oldme (##sys#macro-environment))
(oldce (##sys#current-environment))
(mme (##sys#meta-macro-environment))
(cme (##sys#current-meta-environment))
(aee (##sys#active-eval-environment)))
(dynamic-wind
(lambda ()
(##sys#current-module #f)
(##sys#macro-environment mme)
(##sys#current-environment cme)
(##sys#active-eval-environment ##sys#current-meta-environment))
(lambda ()
((##sys#compile-to-closure
form
'()
(##sys#current-meta-environment)) ;XXX evalenv? static?
'() ) )
(lambda ()
(##sys#active-eval-environment aee)
(##sys#current-module oldcm)
(##sys#current-meta-environment (##sys#current-environment))
(##sys#current-environment oldce)
(##sys#meta-macro-environment (##sys#macro-environment))
(##sys#macro-environment oldme)))))
(define ##sys#eval-handler
(make-parameter
(lambda (x #!optional env)
(let ((se (##sys#current-environment)))
(cond (env
(##sys#check-structure env 'environment 'eval)
(let ((se2 (##sys#slot env 2)))
((if se2 ; not interaction-environment?
(parameterize ((##sys#macro-environment '()))
(##sys#compile-to-closure x '() se2 #f env (##sys#slot env 3)))
(##sys#compile-to-closure x '() se #f env #f))
'() ) ) )
(else
((##sys#compile-to-closure x '() se #f #f #f) '() ) ) ) ) )))
(define eval-handler ##sys#eval-handler)
(define (eval x . env)
(apply (##sys#eval-handler)
x
env) )
;;; Setting properties dynamically scoped
(define-values (##sys#put/restore! ##sys#with-property-restore)
(let ((trail '())
(restoring #f))
(values
(lambda (sym prop val)
(when restoring
(set! trail (cons (list sym prop (##sys#get sym prop)) trail)))
(##sys#put! sym prop val)
val)
(lambda (thunk)
(let ((t0 #f)
(r0 restoring))
(dynamic-wind
(lambda ()
(set! t0 trail)
(set! restoring #t))
thunk
(lambda ()
(do () ((eq? t0 trail))
(apply ##sys#put! (car trail))
(set! trail (cdr trail)))
(set! restoring r0))))))))
;;; Split lambda-list into its parts:
(define ##sys#decompose-lambda-list
(let ([reverse reverse])
(lambda (llist0 k)
(define (err)
(set! ##sys#syntax-error-culprit #f)
(##sys#syntax-error-hook "illegal lambda-list syntax" llist0) )
(let loop ([llist llist0] [vars '()] [argc 0])
(cond [(eq? llist '()) (k (reverse vars) argc #f)]
[(not (##core#inline "C_blockp" llist)) (err)]
[(##core#inline "C_symbolp" llist) (k (reverse (cons llist vars)) argc llist)]
[(not (##core#inline "C_pairp" llist)) (err)]
[else (loop (##sys#slot llist 1)
(cons (##sys#slot llist 0) vars)
(fx+ argc 1) ) ] ) ) ) ) )
;;; Loading source/object files:
(define load-verbose (make-parameter (##sys#fudge 13)))
(define (##sys#abort-load) #f)
(define ##sys#current-source-filename #f)
(define ##sys#current-load-path "")
(define ##sys#dload-disabled #f)
(define-foreign-variable _dlerror c-string "C_dlerror")
(define (set-dynamic-load-mode! mode)
(let ([mode (if (pair? mode) mode (list mode))]
[now #f]
[global #t] )
(let loop ([mode mode])
(when (pair? mode)
(case (##sys#slot mode 0)
[(global) (set! global #t)]
[(local) (set! global #f)]
[(lazy) (set! now #f)]
[(now) (set! now #t)]
[else (##sys#signal-hook 'set-dynamic-load-mode! "invalid dynamic-load mode" (##sys#slot mode 0))] )
(loop (##sys#slot mode 1)) ) )
(##sys#set-dlopen-flags! now global) ) )
(let ([read read]
[write write]
[display display]
[newline newline]
[eval eval]
[open-input-file open-input-file]
[close-input-port close-input-port]
[string-append string-append]
[topentry (##sys#make-c-string "C_toplevel")] )
(define (has-sep? str)
(let loop ([i (fx- (##sys#size str) 1)])
(and (not (zero? i))
(if (memq (##core#inline "C_subchar" str i) '(#\\ #\/))
i
(loop (fx- i 1)) ) ) ) )
(define (badfile x)
(##sys#signal-hook #:type-error 'load "bad argument type - not a port or string" x) )
(set! ##sys#load
(lambda (input evaluator pf #!optional timer printer)
(let* ((fname
(cond [(port? input) #f]
[(not (string? input)) (badfile input)]
((##sys#file-exists? input #t #f 'load) input)
(else
(let ([fname2 (##sys#string-append input ##sys#load-dynamic-extension)])
(if (and (not ##sys#dload-disabled)
(##sys#fudge 24) ; dload?
(##sys#file-exists? fname2 #t #f 'load))
fname2
(let ([fname3 (##sys#string-append input source-file-extension)])
(if (##sys#file-exists? fname3 #t #f 'load)
fname3
input) ) ) ) )))
[evproc (or evaluator eval)] )
(cond [(and (string? input) (not fname))
(##sys#signal-hook #:file-error 'load "cannot open file" input) ]
[(and (load-verbose) fname)
(display "; loading ")
(display fname)
(display " ...\n")
(flush-output)] )
(or (and fname
(or (##sys#dload (##sys#make-c-string fname 'load) topentry)
(and (not (has-sep? fname))
(##sys#dload
(##sys#make-c-string
(##sys#string-append "./" fname)
'load)
topentry) ) ) )
(call-with-current-continuation
(lambda (abrt)