-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.l
executable file
·1349 lines (1185 loc) · 44.2 KB
/
eval.l
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
;;; -*- coke -*-
(define-form gcdebug prog ())
;;(define-form gcdebug prog `(let () ,@prog))
(define-form debug prog ())
;;(define-form debug prog `(let () ,@prog))
(define-form safe prog ())
;;(define-form safe prog `(let () ,@prog))
(define-structure <header> (size flags next type))
(define-structure <buffer> (contents size position))
(define-function make-gc-protectors (vars)
(map (lambda (v) (list 'gc_push_root (list 'address-of v)))
vars))
(define-function make-gc-unprotectors (vars)
(let ((result ()))
(while (pair? vars)
(set result (cons (list 'gc_pop_root (list 'address-of (car vars))) result))
(set vars (cdr vars)))
result))
(define-form gc-protect (vars . prog)
`(let ()
,@(make-gc-protectors vars)
(let ((__answer__ ,@prog))
,@(make-gc-unprotectors vars)
__answer__)))
(define-function make-gc-let* (inits prog)
(if (pair? inits)
`((let (,(car inits)) (gc-protect (,(caar inits)) ,@(make-gc-let* (cdr inits) prog))))
prog))
(define-form gc-let* (inits . prog) (car (make-gc-let* inits prog)))
(compile-begin)
(define abort (extern 'abort))
(define exit (extern 'exit))
(define malloc (extern 'malloc))
(define free (extern 'free))
(define memset (extern 'memset))
(define memcpy (extern 'memcpy))
(define printf (extern 'printf))
(define fprintf (extern 'frintf))
(define sprintf (extern 'sprintf))
(define stderr 0)
(define gc_quantum 65504)
(define gc_frequency 65536)
(define gc_objects_live 0)
(define gc_bytes_used 0)
(define gc_bytes_free 0)
(define gc_roots 0)
(define gc_root_count 0)
(define gc_root_max 0)
(define gc_memory_base 0)
(define gc_memory_last 0)
(define gc_alloc_count 0)
(define gc_collection_count 0)
(define-form size-of-structure (type) (* 4 (array-at %structure-sizes (eval type))))
(define-form <header>-flags-used () 1)
(define-form <header>-flags-atom () 2)
(define-form <header>-flags-mark () 4)
(define-form <header>-flags-used+atom () (+ (<header>-flags-used) (<header>-flags-atom)))
(define-function max (a b) (if (> a b) a b))
(define trace_stack 0)
(define trace_depth 0)
(define-function die ()
(let ((i trace_depth))
(while (<= 0 (set i (- i 1)))
(printf "%3d: " i)
(k_dumpln (k_array_at trace_stack i))))
(exit 1))
(define-function fatal (reason) (printf "\neval.k: %s\n" reason) (exit 1))
(define-function fatal1 (fmt arg) (printf "\neval.k: ") (printf fmt arg) (printf "\n") (die))
(define-function fatal2 (fmt arg brg) (printf "\neval.k: ") (printf fmt arg brg) (printf "\n") (die))
(define-form k_error args
`(let ()
(printf "\neval.k: error: ")
,@(map (lambda (arg) (list (if (string? arg) 'printf 'k_print) arg)) args)
(printf "\n")
(die)))
(define-function new_memory_block (size)
(let ((ptr (malloc size)))
(or ptr (fatal "out of memory"))
(set (<header>-size ptr) (- size (size-of-structure <header>)))
(set (<header>-flags ptr) 0)
(set (<header>-next ptr) ptr)
(debug (printf "BRK %p %d %d/%d\n" ptr size gc_alloc_count gc_frequency))
ptr))
(define-function gc_initialise ()
(let ((ptr (new_memory_block gc_quantum)))
(set gc_memory_base ptr)
(set gc_memory_last ptr)))
(define-function gc_push_root (ptr)
(and (= gc_root_count gc_root_max)
(let* ((roots (malloc (* 4 (set gc_root_max (max 32 (* 2 gc_root_max)))))))
(memcpy roots gc_roots (* 4 gc_root_count))
(and gc_roots (free gc_roots))
(set gc_roots roots)))
(set-oop-at gc_roots gc_root_count ptr)
(debug (printf "gc add root %d at %p\n" gc_root_count ptr))
(set gc_root_count (+ 1 gc_root_count)))
(define-function gc_pop_root (ptr)
(or gc_root_count (fatal "root table underflow"))
(set gc_root_count (- gc_root_count 1))
(debug (printf "gc del root %d at %p %s\n" gc_root_count ptr))
(or (= ptr (oop-at gc_roots gc_root_count)) (fatal "non-lifo root")))
(define-function gc_grow_memory (size)
(let ((brk (new_memory_block size)))
(set (<header>-next brk) (<header>-next gc_memory_last))
(set (<header>-next gc_memory_last) brk)))
(define-function gc_size (obj) (<header>-size (- obj (size-of-structure <header>))))
(define-function gc_malloc_atomic (size)
(let* ((obj (gc_malloc size)))
(set (<header>-flags (- obj (size-of-structure <header>))) (<header>-flags-used+atom))
obj))
(define-function gc_malloc (size)
(set size (& -4 (+ 3 size)))
(and (= gc_alloc_count gc_frequency) (gc_gcollect))
(let* ((first (<header>-next gc_memory_last))
(chunk first)
(ssize (+ size (size-of-structure <header>))))
(while 1
(while
(let ()
(debug (printf "alloc? %d %p %p [%p] %d >= %d %d\n" (<header>-flags chunk) chunk (<header>-next chunk) first (<header>-size chunk) size (<= size (<header>-size chunk))))
(if (= 0 (<header>-flags chunk))
(let ((csize (<header>-size chunk)))
(while (and (= 0 (<header>-flags (<header>-next chunk)))
(= (<header>-next chunk) (+ chunk (+ (size-of-structure <header>) csize))))
(let ((next (<header>-next chunk)))
(set (<header>-next chunk) (<header>-next next))
(set csize (set (<header>-size chunk) (+ csize (+ (size-of-structure <header>) (<header>-size next)))))
(and (= next gc_memory_last) (set gc_memory_last chunk))))
(if (or (< ssize csize) (= size csize))
(let ()
(debug (printf "csize %d\n" csize))
(and (> csize ssize)
(let ((split (+ chunk ssize)))
(debug (printf "split %d: %p + %d -> %p + %d\n" csize chunk size split (- csize (+ size (size-of-structure <header>)))))
(set (<header>-size split) (- csize (+ size (size-of-structure <header>))))
(set (<header>-flags split) 0)
(set (<header>-next split) (<header>-next chunk))
(set (<header>-size chunk) size)
(set (<header>-next chunk) split)
(set csize size)))
(set (<header>-flags chunk) (<header>-flags-used))
(set gc_memory_last chunk)
(debug (printf "alloc chunk %p\n" chunk))
(let ((obj (+ chunk (size-of-structure <header>))))
(memset obj 0 csize)
(set gc_alloc_count (+ gc_alloc_count 1))
(return obj))))))
(!= first (set chunk (<header>-next chunk)))))
(gc_grow_memory (max (+ (size-of-structure <header>) size) gc_quantum)))))
(define-function gc_sweep ()
(debug (printf "sweep\n"))
(let ((ptr gc_memory_base)
(nobjs 0)
(nused 0)
(nfree 0))
(while ptr
(debug (printf "sweep? %d %p + %d\n" (<header>-flags ptr) ptr (<header>-size ptr)))
(let ((flags (<header>-flags ptr)))
(if (& flags (<header>-flags-mark))
(let ()
(set nused (+ nused (<header>-size ptr)))
(set nobjs (+ nobjs 1))
(set (<header>-flags ptr) (^ flags (<header>-flags-mark))))
(debug (printf "collect %p %d\n" ptr (<header>-size ptr)))
(set nfree (+ nfree (<header>-size ptr)))
(set (<header>-flags ptr) 0)))
(and (= gc_memory_base (set ptr (<header>-next ptr)))
(set ptr 0)))
(set gc_objects_live nobjs)
(set gc_bytes_used nused)
(set gc_bytes_free nfree)
(debug (printf "GC: %d used, %d free, %d allocations\n" nused nfree gc_alloc_count))
;;(and (< nfree nused) (gc_grow_memory gc_quantum))
))
(define-function gc_mark_and_trace (obj)
(and obj
(not (& 1 obj))
(let* ((ptr (- obj (size-of-structure <header>)))
(flags (<header>-flags ptr)))
(debug (printf "mark and trace %p flags %d\n" obj flags))
(safe (or (& (<header>-flags-used) flags) (fatal1 "attempt to mark dead object %p" ptr)))
(or (& flags (<header>-flags-mark))
(let ()
(set (<header>-flags ptr) (| flags (<header>-flags-mark)))
(or (& flags (<header>-flags-atom))
(let ((size (>> (<header>-size ptr) 2)))
(debug (printf "mark %p %d type %d\n" ptr size (<header>-type ptr)))
(while size
(set size (- size 1))
(debug (printf "@%d %p\n" size (oop-at obj size)))
(gc_mark_and_trace (oop-at obj size))))))))))
(define-function gc_gcollect ()
(gcdebug
(or (& 1023 (set gc_collection_count (+ gc_collection_count 1)))
(fprintf stderr "%d collections\n" gc_collection_count 1)))
(let ((i 0))
(while (< i gc_root_count)
(debug (let ((ptr (oop-at gc_roots i))) (printf "mark gc root %d : %p -> %p\n" i ptr (oop-at ptr 0))))
(gc_mark_and_trace (oop-at (oop-at gc_roots i) 0))
(set i (+ 1 i))))
(gc_sweep)
(set gc_alloc_count 0))
;;; ----------------------------------------------------------------
(define strlen (extern 'strlen))
(define strcmp (extern 'strcmp))
(define strdup (extern 'strdup))
(define strtoul (extern 'strtoul))
(define getc (extern 'getc))
(define putc (extern 'putc))
(define ungetc (extern 'ungetc))
(define fopen (extern 'fopen))
(define fdopen (extern 'fdopen))
(define fclose (extern 'fclose))
(define fflush (extern 'fflush))
(define fscanf (extern 'fscanf))
(define EOF -1)
(define <undefined> 0)
(define <long> 1)
(define <string> 2)
(define <symbol> 3)
(define <pair> 4)
(define <_array> 5)
(define <array> 6)
(define <expr> 7)
(define <form> 8)
(define <fixed> 9)
(define <subr> 10)
(define symbols 0)
(define globals 0)
(define expanders 0)
(define encoders 0)
(define evaluators 0)
(define applicators 0)
(define s_t 0)
(define s_dot 0)
(define s_set 0)
(define s_lambda 0)
(define s_let 0)
(define s_quote 0)
(define s_quasiquote 0)
(define s_unquote 0)
(define s_unquote_splicing 0)
(define s_expanders 0)
(define s_encoders 0)
(define s_evaluators 0)
(define s_applicators 0)
(define f_set 0)
(define f_quote 0)
(define f_lambda 0)
(define f_let 0)
(define opt_verbose 0)
(define-function new-bits (type size)
(let ((obj (gc_malloc_atomic size)))
(set (oop-at obj -1) type)
obj))
(define-function new-oops (type size)
(let ((obj (gc_malloc size)))
(set (oop-at obj -1) type)
obj))
(define-function new-<long> (bits)
(let ((obj (new-bits <long> (size-of-structure <long>))))
(set (<long>-_bits obj) bits)
obj))
(define-function _new-<string> (len)
(let ((str (new-oops <string> (size-of-structure <string>))))
(gc-protect (str)
(set (<string>-size str) (new-<long> len))
(set (<string>-_bits str) (gc_malloc_atomic (+ len 1)))
str)))
(define-function new-<string> (cstr)
(let ((len (strlen cstr)))
(let ((obj (_new-<string> len)))
(memcpy (<string>-_bits obj) cstr len)
obj)))
(define-function new-<symbol> (cstr)
(let ((obj (new-bits <symbol> (size-of-structure <symbol>))))
(set (<symbol>-_bits obj) (strdup cstr))
obj))
(define-function new-<pair> (head tail)
(let ((obj (new-oops <pair> (size-of-structure <pair>))))
(set (<pair>-head obj) head)
(set (<pair>-tail obj) tail)
obj))
(define-function new-<array> (size)
(let ((arr (new-oops <array> (size-of-structure <array>))))
(gc-protect (arr)
(set (<array>-_array arr) (new-oops <_array> (* 4 size)))
arr)))
(define-function new-<expr> (defn env)
(let ((obj (new-oops <expr> (size-of-structure <expr>))))
(set (<expr>-defn obj) defn)
(set (<expr>-env obj) env )
obj))
(define-function new-<form> (fn)
(let ((obj (new-oops <form> (size-of-structure <form>))))
(gc-protect (obj)
(set (<form>-function obj) fn)
obj)))
(define-function new-<fixed> (fn)
(let ((obj (new-oops <fixed> (size-of-structure <fixed>))))
(gc-protect (obj)
(set (<fixed>-function obj) fn)
obj)))
(define-function new-<subr> (_imp _name)
(let ((obj (new-bits <subr> (size-of-structure <subr>))))
(gc-protect (obj)
(set (<subr>-_imp obj) _imp )
(set (<subr>-_name obj) _name)
obj)))
(define-form is (type arg)
`(let ((__arg__ ,arg))
(and __arg__
(not (& 1 __arg__))
(= ,type (oop-at __arg__ -1)))))
(define-form get-type (arg)
`(let ((__arg__ ,arg))
(safe (and __arg__ (or (& (<header>-flags-used) (<header>-flags (- __arg__ (size-of-structure <header>)))) (fatal1 "attempt to access dead object %p type %d" __arg__))))
(if __arg__
(oop-at __arg__ -1)
<undefined>)))
(define-function type_check_fail (exp act)
(fatal2 "illegal type: expected %d got %d" exp act))
(define-form get (type field object)
`(let ((__obj__ ,object))
(safe (let ((t (get-type __obj__))) (or (= ,type t) (type_check_fail ,type t))))
(,(concat-symbol (concat-symbol type '-) field) __obj__)))
(define-form put (type field object value)
`(let ((__obj__ ,object))
(safe (let ((t (get-type __obj__))) (or (= ,type t) (type_check_fail ,type t))))
(set (,(concat-symbol (concat-symbol type '-) field) __obj__) ,value)))
(define-form get_long (obj) `(oop-at ,obj 0))
(define-form get_head (obj) `(oop-at ,obj 0))
(define-form get_tail (obj) `(oop-at ,obj 1))
(define-form set_tail (obj val) `(set-oop-at ,obj 1 ,val))
(define-function is_blank (c) (or ( = ? c) ; sp
( = ?\t c) ; ht
( = ?\n c) ; nl
( = ?\v c) ; vt
( = ?\f c) ; ff
( = ?\r c))) ; cr
(define-function is_digit10 (c) (and (<= ?0 c) (<= c ?9))) ; 0 1 2 3 4 5 6 7 8 9
(define-function is_digit16 (c) (or (and (<= ?0 c) (<= c ?9)) ; 0 1 2 3 4 5 6 7 8 9
(and (<= ?a c) (<= c ?f)) ; a b c d e f
(and (<= ?A c) (<= c ?F)))) ; A B C D E F
(define-function is_alpha (c) (or (and (<= ?a c) (<= c ?z)) ; a b c d e f g h i j k l m n o p q r s t u v w x y z
(and (<= ?A c) (<= c ?Z)))) ; A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
(define-function is_letter (c) (or ( = ?! c) ; !
(and (<= ?# c) (<= c ?&)) ; # $ % &
(and (<= ?* c) (<= c ?/)) ; * + , - . /
( = ?: c) ; :
(and (<= ?< c) (<= c ?Z)) ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
( = ?\\ c) ; \
( = ?^ c) ; ^
( = ?_ c) ; _
(and (<= ?a c) (<= c ?z)) ; a b c d e f g h i j k l m n o p q r s t u v w x y z
( = ?| c) ; |
( = ?~ c))) ; ~
(define-function new_buffer ()
(let ((buf (malloc (size-of-structure <buffer>))))
(set (<buffer>-contents buf) (malloc 32))
(set (<buffer>-size buf) 32)
(set (<buffer>-position buf) 0)
buf))
(define-function buffer_delete (buf)
(free (<buffer>-contents buf))
(free buf))
(define-function buffer_grow (buf)
(let* ((size (<buffer>-size buf))
(contents (malloc (* 2 size))))
(memcpy contents (<buffer>-contents buf) size)
(free (<buffer>-contents buf))
(set (<buffer>-contents buf) contents)
(set (<buffer>-size buf) (* 2 size))))
(define-function buffer_append (buf c)
(and (= (<buffer>-position buf) (<buffer>-size buf))
(buffer_grow buf))
(let ((posn (<buffer>-position buf)))
(set-string-at (<buffer>-contents buf) posn c)
(set (<buffer>-position buf) (+ 1 posn))))
(define-function buffer_append_all (buf s)
(let ((i 0)
(c 0))
(while (set c (string-at s i))
(buffer_append buf c)
(set i (+ 1 i)))))
(define-function buffer_contents (buf)
(buffer_append buf 0)
(set (<buffer>-position buf) (- (<buffer>-position buf) 1))
(<buffer>-contents buf))
(define-function intern (cstr)
(let ((list symbols))
(while (is <pair> list)
(let ((sym (get_head list)))
(or (strcmp cstr (get <symbol> _bits sym))
(return sym)))
(set list (get_tail list))))
(let ((sym (new-<symbol> cstr)))
(gc-protect (sym)
(set symbols (new-<pair> sym symbols))
sym)))
(define-function is_octal (c) (and (<= ?0 c) (<= c ?7)))
(define-function is_hexadecimal (c) (or (and (<= ?0 c) (<= c ?9))
(and (<= ?a c) (<= c ?f))
(and (<= ?A c) (<= c ?F))))
(define-function digit_value (c)
(cond
((and (<= ?0 c) (<= c ?9)) (- c ?0))
((and (<= ?a c) (<= c ?z)) (- c (- ?a 10)))
((and (<= ?A c) (<= c ?Z)) (- c (- ?A 10)))
(else (fatal "illegal digit in character escape"))))
(define-function read_char (c stream)
(if (= ?\\ c)
(let ()
(set c (getc stream))
(cond
((= c ?a) ?\a)
((= c ?b) ?\b)
((= c ?f) ?\f)
((= c ?n) ?\n)
((= c ?r) ?\r)
((= c ?t) ?\t)
((= c ?v) ?\v)
((= c ?u) (let ((a (getc stream))
(b (getc stream))
(c (getc stream))
(d (getc stream)))
(+ (<< (digit_value a) 24)
(+ (<< (digit_value b) 16)
(+ (<< (digit_value c) 8)
(digit_value d) )))))
((= c ?x) (let ((x 0))
(if (is_hexadecimal (set c (getc stream)))
(let ()
(set x (digit_value c))
(if (is_hexadecimal (set c (getc stream)))
(let ()
(set x (+ (* x 16) (digit_value c)))
(set c (getc stream))))))
(ungetc c stream)
x))
((and (<= ?0 c) (<= c ?7)) (let ((x (digit_value c)))
(if (is_octal (set c (getc stream)))
(let ()
(set x (+ (* x 8) (digit_value c)))
(if (is_octal (set c (getc stream)))
(let ()
(set x (+ (* x 8) (digit_value c)))
(set c (getc stream))))))
(ungetc c stream)
x))
(else (if (or (is_alpha c) (is_digit10 c))
(fatal1 "illegal character escape: \\%c" c)
c))))
c))
(define-function read_number (c stream)
(let ((buf (new_buffer))
(neg (= ?- c)))
(or neg (buffer_append buf c))
(while (is_digit10 (set c (getc stream))) (buffer_append buf c))
(and (= ?x c) (= 1 (<buffer>-position buf))
(let ()
(buffer_append buf c)
(while (is_digit16 (set c (getc stream))) (buffer_append buf c))))
(ungetc c stream)
(buffer_append buf 0)
(let ((result (strtoul (<buffer>-contents buf) 0 0)))
(buffer_delete buf)
(new-<long> (if neg (- result) result)))))
(define-function read_symbol (c stream)
(let ((buf (new_buffer)))
(while (or (is_letter c) (is_digit10 c))
(buffer_append buf c)
(set c (getc stream)))
(ungetc c stream)
(buffer_append buf 0)
(let ((result (intern (<buffer>-contents buf))))
(buffer_delete buf)
result)))
(define-function read_string (c stream)
(let ((buf (new_buffer)))
(while (!= ?\" (set c (getc stream)))
(set c (read_char c stream))
(and (= EOF c) (fatal "unterminated string literal"))
(buffer_append buf c))
(buffer_append buf 0)
(let ((result (new-<string> (<buffer>-contents buf))))
(buffer_delete buf)
result)))
(define-function read_list (delim stream)
(let* ((head (new-<pair> () ()))
(tail head)
(elt ()))
(gc-protect (head elt)
(while (and (!= EOF (set elt (k_read stream))) (!= s_dot elt))
(set elt (new-<pair> elt ()))
(set tail (set_tail tail elt)))
(and (= s_dot elt) (set_tail tail (k_read stream))))
(let ((c (getc stream)))
(if (= delim c)
(get_tail head)
(fatal1 "missing closing '%c' delimiter while reading list" delim)))))
(define-function read_quote (prefix stream)
(let ((qobj (k_read stream)))
(and (= EOF qobj) (fatal "EOF while reading quoted literal"))
(gc-protect (qobj)
(set qobj (new-<pair> qobj ()))
(set qobj (new-<pair> prefix qobj))
qobj)))
(define-function k_read (stream)
(while 1
(let ((c (getc stream)))
(cond
((is_blank c) ())
((= ?? c) (return (new-<long> (read_char (getc stream) stream))))
((= ?- c) (return (if (is_digit10 (ungetc (getc stream) stream))
(read_number c stream)
(read_symbol c stream))))
((= ?\' c) (return (read_quote s_quote stream)))
((= ?\` c) (return (read_quote s_quasiquote stream)))
((= ?\, c) (return (let ((d (getc stream)))
(if (= ?@ d)
(read_quote s_unquote_splicing stream)
(ungetc d stream)
(read_quote s_unquote)))))
((is_letter c) (return (read_symbol c stream)))
((= ?\( c) (return (read_list ?\) stream))) ((= ?\) c) (return (let () (ungetc c stream) EOF)))
((= ?\[ c) (return (read_list ?\] stream))) ((= ?\] c) (return (let () (ungetc c stream) EOF)))
((= ?\{ c) (return (read_list ?\} stream))) ((= ?\} c) (return (let () (ungetc c stream) EOF)))
((is_digit10 c) (return (read_number c stream)))
((= ?\; c) (while (and (!= ?\n (set c (getc stream)))
(!= ?\r c)
(!= EOF c))))
((= ?\" c) (return (read_string c stream)))
((< c 0) (return EOF))
(else (fatal1 "illegal character: %c" c))))))
(define-function do_print (obj storing)
(debug (printf "{%p}" obj))
(let ((type (get-type obj)))
(cond
((= type <undefined>) (printf "nil"))
((= type <long>) (printf "%d" (get <long> _bits obj)))
((= type <string>) (let ((bits (get <string> _bits obj)))
(if (not storing)
(printf "%s" bits)
(let ((i 0)
(c 0))
(printf "\"")
(while (set c (string-at bits i))
(if (and (<= 32 c) (<= c 126))
(cond
((= c ?\") (printf "\\\""))
((= c ?\\) (printf "\\\\"))
(else (printf "%c" c)))
(printf "\\%03o" c))
(set i (+ 1 i)))
(printf "\"")))))
((= type <symbol>) (printf "%s" (get <symbol> _bits obj)))
((= type <pair>) (let ()
(printf "(")
(while (and (is <pair> obj) (!= globals obj))
(do_print (get_head obj) storing)
(and (is <pair> (set obj (get_tail obj))) (printf " ")))
(if (= globals obj)
(printf "<globals>")
(and obj
(let ()
(printf " . ")
(do_print obj storing))))
(printf ")")))
((= type <array>) (let ((len (k_array_length obj)))
(printf "Array(")
(for (i 0 len)
(and i (printf " "))
(do_print (k_array_at obj i)))
(printf ")")))
((= type <expr>) (let ()
(printf "Expr(")
(do_print (k_car (get <expr> defn obj)) storing)
(printf ")")))
((= type <form>) (let ()
(printf "Form(")
(do_print (get <form> function obj) storing)
(printf ")")))
((= type <fixed>) (let ()
(printf "Fixed(")
(do_print (get <fixed> function obj) storing)
(printf ")")))
((= type <subr>) (printf "Subr(%s)" (get <subr> _name obj)))
(else (printf "<type:%d>" type)))))
(define-function k_print (obj) (do_print obj 0)) (define-function k_println (obj) (do_print obj 0) (printf "\n"))
(define-function k_dump (obj) (do_print obj 1)) (define-function k_dumpln (obj) (do_print obj 1) (printf "\n"))
;;; ----------------------------------------------------------------
(define-function k_define (name value env)
(let ((ass (new-<pair> name value)))
(gc-protect (ass)
(let ((ent (new-<pair> ass (get_tail env))))
(set_tail env ent)
ass))))
(define-function k_assq (key list)
(while (is <pair> list)
(let ((head (get_head list)))
(and (is <pair> head)
(= key (get_head head))
(return head)))
(set list (get_tail list))))
(define-function k_car (list) (and (is <pair> list) (get_head list)))
(define-function k_cdr (list) (and (is <pair> list) (get_tail list)))
(define-function k_caar (list) (k_car (k_car list)))
(define-function k_cadr (list) (k_car (k_cdr list)))
(define-function k_cddr (list) (k_cdr (k_cdr list)))
(define-function k_caddr (list) (k_car (k_cdr (k_cdr list))))
(define-function k_string_length (obj) (get_long (get <string> size obj)))
(define-function k_array_length (obj)
(and (is <array> obj)
(let ((_arr (get <array> _array obj)))
(let ((sz (/ (gc_size _arr) 4)))
sz))))
(define-function k_array_at (obj idx)
(and (is <array> obj)
(let* ((elts (get <array> _array obj))
(size (/ (gc_size elts) 4)))
(and (<= 0 idx) (< idx size)
(oop-at elts idx)))))
(define-function k_set_array_at (obj idx val)
(and (is <array> obj)
(let* ((elts (get <array> _array obj))
(size (/ (gc_size elts) 4)))
(and (<= 0 idx)
(let ()
(or (< idx size)
(let* ((nsize (max (+ idx 1) (* size 2)))
(oops (new-oops <_array> (* 4 nsize))))
(memcpy oops elts (* size 4))
(set elts (put <array> _array obj oops))))
(set-oop-at elts idx val))))))
(define-function k_concat (head tail)
(if (is <pair> head)
(let ()
(set tail (k_concat (get_tail head) tail))
(gc-protect (tail)
(new-<pair> (get_head head) tail)))
tail))
(define-function k_apply_expr (fun arguments env)
(let ((args arguments)
(defn (get <expr> defn fun))
(formals (k_car defn))
(tmp ()))
(set env (get <expr> env fun))
(gc-protect (defn env tmp)
(while (is <pair> formals)
(or (is <pair> args) (k_error "too few arguments: ("fun" "arguments")"))
(set tmp (new-<pair> (get_head formals) (get_head args)))
(set env (new-<pair> tmp env))
(set formals (get_tail formals))
(set args (get_tail args)))
(and (is <symbol> formals)
(let ()
(set tmp (new-<pair> formals args))
(set env (new-<pair> tmp env))
(set args ())))
(and args (k_error "too many arguments: ("fun" "arguments")"))
(set defn (get_tail defn))
(while (is <pair> defn)
(set args (k_eval (get_head defn) env))
(set defn (get_tail defn)))
args)))
(define-function k_apply (fun arguments env)
(let ((type (get-type fun)))
(cond
((= type <expr>) (k_apply_expr fun arguments env))
((= type <fixed>) (k_apply (get <fixed> function fun) arguments env))
((= type <subr>) ((get <subr> _imp fun) arguments env))
(else (let ((ap (k_array_at (get_tail applicators) (get-type fun))))
(if ap
(let ((args arguments))
(gc-protect (args)
(set args (new-<pair> fun args))
(k_apply ap args env)))
(k_error "cannot apply: " fun)))))))
;;; ----------------------------------------------------------------
(define-function exlist (list env)
(if (is <pair> list)
(let ((head (k_expand (get_head list) env)))
(gc-protect (head)
(let ((tail (exlist (get_tail list) env)))
(gc-protect (tail)
(new-<pair> head tail)))))
list))
(define-function k_expand (exp env)
(if (is <pair> exp)
(let ((head (k_expand (get_head exp) env)))
(gc_push_root (address-of head))
(if (is <symbol> head)
(let ((val (k_cdr (k_assq head env))))
(if (is <form> val)
(let ()
(set head (k_apply (get <form> function val) (get_tail exp) env))
(set head (k_expand head env))
(gc_pop_root (address-of head))
(return head)))))
(let ((tail (get_tail exp)))
(gc_push_root (address-of tail))
(or (= head s_quote) (set tail (exlist tail env)))
(and (= s_set head)
(is <pair> (k_car tail))
(is <symbol> (k_caar tail))
(let ((buf (new_buffer)))
(buffer_append_all buf "set-")
(buffer_append_all buf (get <symbol> _bits (get_head (get_head tail))))
(set head (intern (buffer_contents buf)))
(set tail (k_concat (get_tail (get_head tail)) (get_tail tail)))))
(set exp (new-<pair> head tail))
(gc_pop_root (address-of tail))
(gc_pop_root (address-of head))))
(let ((fn (k_array_at (get_tail expanders) (get-type exp))))
(and fn
(let ((args (new-<pair> exp ())))
(gc-protect (args)
(set exp (k_apply fn args env)))))))
exp)
(define-function enlist (list env)
(if (is <pair> list)
(let ((head (k_encode (get_head list) env)))
(gc-protect (head)
(let ((tail (enlist (get_tail list) env)))
(gc-protect (tail)
(new-<pair> head tail)))))
list))
(define-function k_encode (exp env)
(if (is <pair> exp)
(let ((head (k_encode (get_head exp) env))
(tail (get_tail exp)))
(gc-protect (head tail)
(if (is <symbol> head)
(let ((val (k_cdr (k_assq head env))))
(and (or (is <fixed> val) (is <subr> val))
(set head val))))
(cond
((= head f_let) (let ((args (k_cadr exp))
(tmp ()))
(gc-protect (env tmp)
(while (is <pair> args)
(let ((var (get_head args)))
(and (is <pair> var) (set var (get_head var)))
(set tmp (new-<pair> var ()))
(set env (new-<pair> tmp env))
(set args (get_tail args))))
(set tail (enlist tail env)))))
((= head f_lambda) (let ((args (k_cadr exp))
(tmp ()))
(gc-protect (env tmp)
(while (is <pair> args)
(set tmp (new-<pair> (get_head args) ()))
(set env (new-<pair> tmp env))
(set args (get_tail args)))
(and args
(let ()
(set tmp (new-<pair> args ()))
(set env (new-<pair> tmp env))))
(set tail (enlist tail env)))))
((!= head f_quote) (set tail (enlist tail env))))
(set exp (new-<pair> head tail))))
(let ((fn (k_array_at (get_tail encoders) (get-type exp))))
(and fn (let () (printf "APPLY GOT ENCODER\n... " (k_println fn) 1)))
(and fn
(let ((args (new-<pair> env ())))
(gc-protect (args)
(set args (new-<pair> exp args))
(printf "APPLY ENCODER\n... ") (k_print fn) (printf " ") (k_print args) (printf " ") (k_println env)
(set exp (k_apply fn args env)))))))
exp)
(define-function evlist (obj env)
(if (is <pair> obj)
(let ((head (k_eval (get_head obj) env)))
(gc-protect (head)
(let ((tail (evlist (get_tail obj) env)))
(gc-protect (tail)
(new-<pair> head tail)))))
obj))
(define-function k_eval (exp env)
(let ((type (get-type exp)))
(cond
((= type <undefined>) exp)
((= type <long>) exp)
((= type <string>) exp)
((= type <symbol>) (let ((val (k_assq exp env)))
(or (is <pair> val) (fatal1 "undefined variable: %s" (get <symbol> _bits exp)))
(get_tail val)))
((= type <pair>) (let ((head (k_eval (get_head exp) env)))
(gc-protect (head)
(k_set_array_at trace_stack trace_depth exp)
(set trace_depth (+ trace_depth 1))
(set head (if (is <fixed> head)
(k_apply (get <fixed> function head) (get_tail exp) env)
(let ((args (evlist (get_tail exp) env)))
(gc-protect (args)
(k_apply head args env)))))
(set trace_depth (- trace_depth 1))
head)))
(else (let ((ev (k_array_at (get_tail evaluators) (get-type exp))))
(and ev
(let ((args (new-<pair> exp ())))
(gc-protect (args)
(printf "consing args then ignoring them\n")
(k_set_array_at trace_stack trace_depth exp)
(set trace_depth (+ trace_depth 1))
(set exp (k_apply ev exp env))
(set trace_depth (- trace_depth 1)))))
exp)))))
(define-function subr_define (args env)
(let ((sym (k_car args)))
(or (is <symbol> sym) (k_error "non-symbol identifier in define: "sym))
(let ((val (k_eval (k_cadr args) env)))
(gc-protect (val)
(k_define sym val globals)
val))))
(define-function subr_lambda (args env)
(new-<expr> args env))
(define-function subr_let (args env)
(let ((env2 env)
(tmp ())
(bindings (k_car args))
(body (k_cdr args)))
(gc-protect (env2 tmp)
(while (is <pair> bindings)
(let ((binding (get_head bindings)))
(if (is <pair> binding)
(let ((sym (get_head binding))
(prog (get_tail binding)))
(while (is <pair> prog)
(set tmp (k_eval (get_head prog) env))
(set prog (get_tail prog)))
(set tmp (new-<pair> sym tmp))
(set env2 (new-<pair> tmp env2)))))
(set bindings (get_tail bindings)))
(set tmp ())
(while (is <pair> body)
(set tmp (k_eval (get_head body) env2))
(set body (get_tail body)))
tmp)))
(define-function subr_set (args env)
(let ((var (k_assq (k_car args) env)))
(or (is <pair> var) (k_error "undefined variable: (set "args")"))
(set_tail var (k_eval (k_cadr args) env))))
(define-function subr_while (args env)
(let ((tst (k_car args))
(prog args))
(while (k_eval tst env)
(let ((body prog))
(while (is <pair> (set body (k_cdr body)))
(k_eval (get_head body) env))))))
(define-function subr_if (args env)
(if (k_eval (k_car args) env)
(k_eval (k_cadr args) env)
(let ((ans ()))
(set args (k_cdr args))
(while (is <pair> (set args (k_cdr args)))
(set ans (k_eval (get_head args) env)))
ans)))
(define-function subr_or (args env)
(let ((ans ()))
(while (is <pair> args)
(and (set ans (k_eval (get_head args) env))
(return ans))
(set args (get_tail args)))))
(define-function subr_and (args env)
(let ((ans s_t))
(while (is <pair> args)
(or (set ans (k_eval (get_head args) env))
(return ()))
(set args (get_tail args)))
ans))
(define-function subr_quote (args env)
(k_car args))
(define-function subr_not (args env)
(if (k_car args) () s_t))
(define-function arity2 (op args)
(or (and (is <pair> args)
(is <pair> (get_tail args))
(= () (get_tail (get_tail args))))
(fatal1 "%s: expected 2 arguments" op)))
(define-function arity3 (op args)
(or (and (is <pair> args)