-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.n
1061 lines (918 loc) · 23.3 KB
/
repl.n
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
; (If then else False) -> else
; (If then else _) -> (then _)
; (def (check exp)
; (match exp
; ((cons a b) A)
; ((app a b) B)
; (else 42)))
; (def (check-type exp)
; (jt-cons (fn (a b) A)
; (jt-app (fn (a b) B)
; (fn (exp) 42))
; exp))
; Data types
; pair = 1
; symbol = 2
(boot (seq
;; standard birds
(def (B x y z) (x (y z)))
(def (C x y z) (x z y))
(def (D x y z w) (x y (z w)))
(def (E x y z w) (x y (z w)))
(def (F x y z) (z y x))
(def (G x y z w) (x w (y z)))
(def (H x y z) (x y z y))
(def (I x) x)
(def (J x y z w) (x y (x w z)))
(def (K x y) x)
(def (L x y) (x (y y)))
(def (M x) (x x))
(def (Q x y) (y (x y)))
(def (R x y z) (y z x))
(def (S x y z) (x z (y z)))
(def (T x y) (y x))
(def (U x y) (y (x x y)))
(def (V x y z) (z x y))
(def (W x y) (x y y))
; (def (Y x) (x (Y x)))
;; lispy birds
(def (o f g x) (f (g x)))
(def caar (o car car))
(def cadr (o car cdr))
(def cdar (o cdr car))
(def cddr (o cdr cdr))
(def caaar (o car caar))
(def caadr (o car cadr))
(def cadar (o car cdar))
(def caddr (o car cddr))
(def cdaar (o cdr caar))
(def cdadr (o cdr cadr))
(def cddar (o cdr cdar))
(def cdddr (o cdr cddr))
(def null ())
(def emit cons)
(def (fold op)
(def (loop st lst)
(if (null? lst)
st
(loop (op st (car lst)) (cdr lst))))
loop)
(def (foldr op st lst)
(if (null? lst) st
(op (car lst)
(foldr op st (cdr lst)))))
(def (interval from to)
(if (= from to)
(cons to ())
(cons from
(interval (+ from 1) to))))
(def (iter f)
(def (walk st)
(cons st
(walk (f st))))
walk)
(def (take lst n)
(if (= n 0)
null
(if (null? lst)
null
(cons (car lst)
(take (cdr lst) (- n 1))))))
(def length
(def (loop n lst)
(if (null? lst) n
(loop (+ n 1) (cdr lst))))
(loop 0))
(def (map op lst)
(if (null? lst)
()
(cons (op (car lst))
(map op (cdr lst)))))
(def (zip op a b)
(if (null? a)
()
(cons (op (car a) (car b))
(zip op (cdr a) (cdr b)))))
(def (append a b)
(if (null? a)
b
(cons (car a)
(append (cdr a) b))))
(def reverse
(def (loop a b)
(if (null? b) a
(loop (cons (car b) a) (cdr b))))
(loop ()))
(def (splice-at op lst)
(def (splice lst buff)
(if (null? lst)
(if (null? buff)
null
(cons (reverse buff) null))
(if (op (car lst))
(if (null? buff)
(splice (cdr lst) buff)
(cons (reverse buff)
(splice (cdr lst) null)))
(splice (cdr lst) (cons (car lst) buff)))))
(splice lst null))
(def (and a b) (if a b #f))
(def (or a b) (if a a b))
(def (not a) (if a #f #t))
(def (list-ref lst pos)
(if (= pos 0)
(car lst)
(list-ref (cdr lst) (- pos 1))))
; step and to are constants, should be lower
(def (iota step from to)
(if (= from to)
null
(cons from
(iota step (+ from step) to))))
(def (read-line lst cont)
(def (reader buff lst)
(if (= (car lst) 10)
(cont (reverse buff) (cdr lst))
(reader (cons (car lst) buff) (cdr lst))))
(reader null lst))
; parseri on inputin ja symbolitaulun funktio, koska sen taytyy
; pystya internoimaan parsitut symbolit
;;;
;;; Symbol tries
;;;
;
; (def (eq-list = a b)
; (if (null? a)
; (null? b)
; (if (null? b)
; #f
; (if (= (car a) (car b))
; (eq-list = (cdr a) (cdr b))
; #f))))
;
; (def (dumb-insert =)
; (def (ins lst key val)
; (if (null? lst)
; (cons (cons key val) null)
; (if (eq-list = (caar lst) key)
; (cons (cons key val) (cdr lst))
; (cons (car lst)
; (ins (cdr lst) key val)))))
; ins)
;
; (def (dumb-get =)
; (def (get lst key def)
; (if (null? lst)
; def
; (if (eq-list = (caar lst) key)
; (cdar lst)
; (get (cdr lst) key def))))
; get)
;
; symbol tree node = (byte . rest) -> this byte
; = ((byte . then) . rest) -> branch
; = null -> fail
; = (sym . rest) -> this symbol
;;;
;;; Symbol interning
;;;
(def syms-empty null)
; -> symbol | #f
(def (syms-get node key)
(if (null? node)
;; no match
#f ;; not found
(if (null? key) ;; found
;; maybe match
(if (symbol? (car node))
(car node)
#f)
(if (eq? (car node) (car key))
(syms-get (cdr node) (cdr key)) ;; match -> move on
(if (symbol? (car node))
(syms-get (cdr node) key) ;; skip sym
(if (pair? (car node))
(or ;; try branch
(syms-get (car node) key)
(syms-get (cdr node) key))
#f)))))) ;; no match
(def (syms-put node key val)
(if (null? key)
(cons val node) ;; insert value
(if (null? node)
(cons (car key) ;; grow node
(syms-put null (cdr key) val))
(if (pair? (car node))
(if (eq? (caar node) (car key))
(cons ;; follow branch
(syms-put (car node) key val)
(cdr node))
(cons (car node)
(syms-put (cdr node) key val))) ;; jump over branch
(if (symbol? (car node)) ;; jump over sym
(cons (car node) (syms-put (cdr node) key val))
(if (eq? (car node) (car key)) ;; move on
(cons (car node)
(syms-put (cdr node) (cdr key) val))
(cons ;; make a new branch
(syms-put null key val)
node)))))))
(def (intern-symbol syms bytes cont)
(def this (syms-get syms bytes))
;(def this #f)
(if this
(cont syms this)
(seq
(def this (mksym bytes))
(cont
(syms-put syms bytes this)
this))))
; initial env with uninterned names, only containing primitives
; used for bootstrapping and as base for the real env
(def initial-str-env
(cons (cons "s" (prim 0)))
(cons (cons "k" (prim 1)))
(cons (cons "i" (prim 2)))
(cons (cons "b" (prim 3)))
(cons (cons "c" (prim 4)))
(cons (cons "S" (prim 5)))
(cons (cons "C" (prim 6)))
(cons (cons "B" (prim 7)))
(cons (cons "y" (prim 8)))
(cons (cons "cons" (prim 20)))
(cons (cons "car" (prim 21)))
(cons (cons "cdr" (prim 22)))
(cons (cons "+" (prim 30)))
(cons (cons "-" (prim 31)))
(cons (cons "*" (prim 32)))
(cons (cons "/" (prim 33)))
(cons (cons "%" (prim 34)))
(cons (cons "mod" (prim 34)))
(cons (cons "=" (prim 35)))
(cons (cons ">" (prim 36)))
(cons (cons "<" (prim 37)))
(cons (cons "if" (prim 40)))
(cons (cons "null?" (prim 41)))
(cons (cons "fixnum?" (prim 42)))
(cons (cons "symbol?" (prim 43)))
(cons (cons "eq?" (prim 44)))
(cons (cons "@" (prim 45)))
(cons (cons "mksym" (prim 46)) )
(cons (cons "prim" (prim 47)))
(cons (cons "pair?" (prim 48)))
(cons (cons "mkapp" (prim 49)))
null)
; intern some initially required symbols
; the standard env syms come later
(def initial-syms
(fold
(fn (syms bytes)
(syms-put syms bytes (mksym bytes)))
syms-empty
(cons "fn"
(cons "def"
(cons "seq"
(cons "lit"
(cons "quote"
(cons "boot"
(cons "undefined"
(cons "list"
(map car initial-str-env)))))))))))
; intern the names of values in initial env
(def initial-env
(map
(fn (node)
(cons (syms-get initial-syms (car node)) (cdr node)))
initial-str-env))
(def fn-sym (syms-get initial-syms "fn"))
(def lit-sym (syms-get initial-syms "lit"))
(def def-sym (syms-get initial-syms "def"))
(def seq-sym (syms-get initial-syms "seq"))
(def quote-sym (syms-get initial-syms "quote"))
(def undefined-sym (syms-get initial-syms "undefined"))
(def boot-sym (syms-get initial-syms "boot"))
(def list-sym (syms-get initial-syms "list"))
(def y-sym (syms-get initial-syms "y"))
(def cons-sym (syms-get initial-syms "cons"))
(def scom (prim 0))
(def kcom (prim 1))
(def icom (prim 2))
(def bcom (prim 3))
(def ccom (prim 4))
(def spcom (prim 5))
(def cpcom (prim 6))
(def bpcom (prim 7))
;;;
;;; Output
;;;
;(def string->symbol
; (def (make-symtab done)
; (cons
; (mksym (reverse done))
; (map
; (fn (next)
; (make-symtab (cons next done)))
; (interval 0 (+ (+ 64 64) (+ 64 63))))))
; ;;; a hack for bytecode dumping
; (def symtab (make-symtab '()))
; (def (read-symtab bytes symtab)
; (if (null? bytes)
; (car symtab)
; (read-symtab (cdr bytes)
; (list-ref (cdr symtab) (car bytes)))))
; (fn (bytes)
; (read-symtab bytes symtab)))
;;; Rendering (sexp version)
(def (render-number val more)
(def (to-digit n)
(+ n 48))
(def (process digits num)
(if (= num 0)
(append digits more)
(process
(cons (to-digit (mod num 10)) digits)
(/ num 10))))
(if (= val 0)
(cons 48 more)
(process () val)))
(def (render-pair render)
(fn (val more)
(def (walk val)
(if (null? val)
(emit 41 more)
(if (pair? val)
(seq
(emit 32)
(render (car val))
(walk (cdr val)))
(seq
(emit 32)
(emit 46)
(emit 32)
(render val)
(emit 41 more)))))
(emit 40)
(render (car val))
(walk (cdr val))))
(def (render-symbol obj more)
(append (car obj) more))
(def (render-any obj more)
(if (pair? obj)
(render-pair render-any obj more)
(if (symbol? obj)
(render-symbol obj more)
(if (fixnum? obj)
(render-number obj more)
(emit 63 more)))))
;;;
;;; Parsing
;;;
(def char-lp? (= 40))
(def char-rp? (= 41))
(def char-quote? (= 39))
(def (char-digit? x) (and (< 47 x) (< x 58)))
(def char-comment? (= 59))
(def (char-whitespace? x)
(or (= x 32)
(or (= x 10)
(or (= x 9)
(char-comment? x)))))
(def (char-symbol? x)
(and (> x 32)
(or (< x 40)
(> x 41))))
(def char-string? (= 34))
(def char-special? (= 35))
(def (wipewhite input)
;;; wipe anything until a newline
(def (wipe-comment input)
(if (null? input)
input
(if (= (car input) 10)
(wipewhite (cdr input))
(wipe-comment (cdr input)))))
(if (null? input)
input
(if (char-comment? (car input))
(wipe-comment (cdr input))
(if (char-whitespace? (car input))
(wipewhite (cdr input))
input))))
; ehka vain parserin taytyy tehda symboleita, joten
; symbolitaulu voisi olla sen paikallinen muuttuja?
; (ok syms value input')
(def (parse-number in syms ok fail)
(def (get in n)
(if (null? in)
(ok syms n in)
(if (char-digit? (car in))
(get (cdr in) (+ (* n 10) (- (car in) 48)))
(ok syms n in))))
(if (char-digit? (car in))
(get (cdr in) (- (car in) 48))
fail))
(def (parse-symbol in syms ok fail)
(def (get in buff)
(if (null? in)
(intern-symbol syms (reverse buff)
(fn (syms sym)
(ok syms sym in)))
(if (char-symbol? (car in))
(get (cdr in) (cons (car in) buff))
(intern-symbol syms (reverse buff)
(fn (symsx sym)
(ok symsx sym in)))
)))
(if (char-symbol? (car in))
(get (cdr in) (cons (car in) null))
fail))
(def (parse-list parse in syms ok fail)
; input is always wiped
(def (get in syms buff)
(if (null? in)
fail
(parse syms in
(fn (syms this in)
(get (wipewhite in) syms (cons this buff)))
(if (char-rp? (car in))
(ok syms (reverse buff) (cdr in))
fail))))
(if (char-lp? (car in))
(get (wipewhite (cdr in)) syms null)
fail))
(def (parse-scheme-bool input syms ok fail)
(if (and (pair? input) (eq? (car input) 35))
(ok syms (eq? (cadr input) 116) (cddr input))
fail))
; INDECENT io to stderr via prod combinator, for debug only
(def (debug exp rest)
(def (walk lst)
(if (null? lst)
rest
(@ (car lst)
(walk (cdr lst)))))
(walk
(render-any exp null)))
(def (collect-string in buff cont)
(if (null? in)
(cont #f #f)
(if (= (car in) 34)
(cont
(cons quote-sym (cons (reverse buff) null))
(cdr in))
(collect-string (cdr in)
(cons (car in) buff)
cont))))
(def (parse-string input syms ok fail)
(if (= (car input) 34)
(collect-string
(cdr input)
null
(fn (bytes input)
(if bytes
(ok syms bytes input)
fail)))
fail))
(def (parse syms input ok fail)
(def in (wipewhite input))
(if (null? input)
(fail 1 input)
(parse-number in syms ok
(parse-list parse in syms ok
(parse-string in syms ok
(parse-scheme-bool in syms ok
(parse-symbol in syms ok
fail)))))))
; -----------------------------------------------------
;;;
;;; Pattern matching
;;;
(def (apply op lst)
(if (null? lst)
op
(apply (op (car lst)) (cdr lst))))
;;;
;;; hygienic macros here also?
;;;
;;;
;;; sexp->ast, binary tree
;;;
; lexp = [fn . [var . body]] | cexp
; cexp = comb | [a . b] | val
; tahan voisi laittaa valiaikaisesti seq ja def kaannon?
(def (ast-occurs-free? var exp)
(if (eq? var exp)
#t
(if (pair? exp)
(if (eq? (car exp) lit-sym)
#f
(if (eq? (car exp) fn-sym)
(if (eq? (cadr exp) var)
#f
(ast-occurs-free? var (cddr exp)))
(or
(ast-occurs-free? var (car exp))
(ast-occurs-free? var (cdr exp)))))
#f)))
(def ast
(def (maybe-seq node)
(if (and (pair? node) (eq? (car node) seq-sym))
node
(cons seq-sym node)))
(def (ast-lambda formals body)
(if (null? formals)
body
(cons fn-sym
(cons (car formals)
(ast-lambda (cdr formals) body)))))
(def (ast-call lst)
(def (walk call args)
(if (null? args)
call
(walk (cons call (car args)) (cdr args))))
(walk (car lst) (cdr lst)))
(def (ast-maybe-recurse me body)
(if (ast-occurs-free? me body)
(seq
(cons y-sym
(cons fn-sym
(cons me body))))
body))
; -> ((fn (name) ret) body)
(def (ast-def ast name body rest)
(if (pair? name)
(ast-def ast (car name) (cons fn-sym (cons (cdr name) body)) rest)
(cons
(cons fn-sym
(cons name rest))
(ast-maybe-recurse name
(ast body)))))
; (a b c d) -> (a . (b . (c . d)))
(def (ast-seq-apply lst)
(foldr
(fn (this tail)
(if tail (cons this tail) this))
#f lst))
(def (ast-seq-defs ast lst)
(if (null? lst)
(cons lit-sym 42) ; FIXME technically should be reported as a bug?
(if (and (pair? (car lst)) (eq? (caar lst) def-sym))
(ast-def ast
(cadr (car lst))
(maybe-seq (cddr (car lst)))
(ast-seq-defs ast (cdr lst)))
(ast-seq-apply (map ast lst)))))
(def (ast-list lst)
(if (null? lst)
(cons lit-sym null)
(cons
(cons cons-sym (car lst))
(ast-list (cdr lst)))))
; FIXME list and others temporarily keywords
(def (any exp)
(if (pair? exp)
(if (eq? (car exp) fn-sym)
(ast-lambda (cadr exp) (any (maybe-seq (cddr exp))))
(if (eq? (car exp) seq-sym)
(ast-seq-defs any (cdr exp))
(if (eq? (car exp) quote-sym)
(cons lit-sym (cadr exp))
(if (eq? (car exp) list-sym)
(ast-list (map any (cdr exp)))
(ast-call (map any exp))))))
(if (symbol? exp)
exp
(cons lit-sym exp))))
any)
;;;
;;; ast -> combinator exp
;;;
(def (merge-2 op a b) (cons (cons op a) b))
(def (merge-3l op rator rand)
(cons
(cons
(cons op (cdar rator))
(cdr rator))
rand))
(def (merge-3r op rator rand)
(cons
(cons
(cons op rator)
(cdar rand))
(cdr rand)))
(def (app2-rator? want exp)
(and (pair? exp)
(and (pair? (car exp))
(eq? want (caar exp)))))
(def (has-car? v x) (and (pair? x) (eq? (car x) v)))
(def i? (eq? icom))
(def o? (has-car? kcom))
(def x? (o not o?))
(def ox? (app2-rator? bcom))
(def xo? (app2-rator? ccom))
(def xx? (app2-rator? scom))
; FIXME try different sets when repl can run useful benchmarks
(def (carrier a b)
(if (ox? a)
(if (o? b)
(merge-3l cpcom a (cdr b)) ;; ox + o = (C' a b c d)
(merge-3l spcom a b)) ;; ox + x = (S' a b c d)
(if (o? a)
(if (ox? b)
(merge-3r bpcom (cdr a) b) ;; o + ox = (B' a b c d)
(if (i? b) (cdr a) ;; (K a) + I = a
(if (o? b)
(cons kcom ;; o + o = (K (a b))
(cons (cdr a) (cdr b)))
(merge-2 bcom (cdr a) b)))) ;; o + x = (B a b)
(if (o? b)
(merge-2 ccom a (cdr b)) ;; x + o = (C a b)
(merge-2 scom a b))))) ;; x + x = (S a b)
(def (literal? exp)
(and (pair? exp)
(eq? (car exp) lit-sym)))
(def (extract var exp)
(if (literal? exp)
(cons kcom exp)
(if (pair? exp)
(carrier
(extract var (car exp))
(extract var (cdr exp)))
(if (eq? var exp)
icom
(cons kcom exp)))))
(def (compile exp)
(if (eq? (car exp) fn-sym)
(extract (cadr exp)
(compile (cddr exp)))
(if (literal? exp)
exp
(if (pair? exp)
(cons
(compile (car exp))
(compile (cdr exp)))
exp))))
;;;
;;; cexp -> graph reduction code
;;;
(def (pick lst key def)
(if (null? lst) def
(if (eq? (caar lst) key)
(cdar lst)
(pick (cdr lst) key def))))
(def prim-codes
(cons (cons (prim 0) 115)) ; s
(cons (cons (prim 1) 107)) ; k
(cons (cons (prim 2) 105)) ; i
(cons (cons (prim 3) 98)) ; b
(cons (cons (prim 4) 99)) ; c
(cons (cons (prim 5) 111)) ; S = o
(cons (cons (prim 6) 109)) ; C = m
(cons (cons (prim 7) 110)) ; B = n
(cons (cons (prim 8) 121)) ; y
(cons (cons (prim 20) 67)) ; cons = C
(cons (cons (prim 21) 65)) ; car = A
(cons (cons (prim 22) 68)); cdr = D
(cons (cons (prim 30) 43)) ; +
(cons (cons (prim 31) 45)) ; -
(cons (cons (prim 32) 42)) ; *
(cons (cons (prim 33) 47)) ; /
(cons (cons (prim 34) 37)) ; mod = %
(cons (cons (prim 35) 61)) ; =
(cons (cons (prim 36) 62)) ; >
(cons (cons (prim 37) 60)) ; <
(cons (cons (prim 40) 73)) ; I
(cons (cons (prim 41) 78)) ; N
(cons (cons (prim 42) 85)) ; U
(cons (cons (prim 43) 83)) ; S
(cons (cons (prim 44) 69)) ; E
(cons (cons (prim 45) 64)) ; @ = @
(cons (cons (prim 47) 36)) ; prim = $
(cons (cons (prim 48) 80)) ; pair? = P
(cons (cons null 50)) ; null = 2
(cons (cons #t 51)) ; true = 3
(cons (cons #f 52)) ; false = 4
null)
(def (serialize-lit val tail)
(if (and (fixnum? val) (and (< val (+ 100 (+ 100 56))) (> (+ val 1) 0)))
(cons 108 (cons val tail))
(seq
(def char (pick prim-codes val #f))
(if char
(cons char tail)
(seq
(@ 48)
(debug val)
(cons 63 tail))))))
(def (serialize-prim node tail)
(if (eq? node mkapp)
(append "Q20" tail)
(if (eq? node mksym)
(append "Q12" tail)
(seq
(def char (pick prim-codes node #f))
(if char
(cons char tail)
(seq
(debug (cons undefined-sym node))
(cons 49 tail)))))))
(def (serialize-cexp exp tail)
(if (pair? exp)
(if (eq? (car exp) lit-sym)
(seq
(def val (cdr exp))
(if (pair? val)
(serialize-cexp
(cons
(cons cons (cons lit-sym (car val)))
(cons lit-sym (cdr val)))
tail)
(serialize-lit (cdr exp) tail)))
(cons 32
(serialize-cexp (car exp)
(serialize-cexp (cdr exp) tail))))
(serialize-prim exp tail)))
(def (reflect node)
(if (pair? node)
(if (eq? (car node) lit-sym)
(cdr node)
(mkapp
(reflect (car node))
(reflect (cdr node))))
node))
; FIXME: (reflect (reify node)) == node
; FIXME: what about sharing?
(def (reify node)
42)
(def (char? val)
(and (fixnum? val)
(or
(and (< val 127) (> val 31))
(= val 10))))
(def (render-value obj more)
(if (null? obj)
more
(if (and (pair? obj) (char? (car obj)))
(cons (car obj)
(render-value (cdr obj) more))
(render-any obj more))))
;;;
;;; Repl
;;;
(def (apply-env env node ok fail)
(if (pair? node)
(if (eq? (car node) lit-sym)
(ok node)
(apply-env env (car node)
(fn (rator)
(apply-env env (cdr node)
(fn (rand)
(ok (cons rator rand)))
fail))
fail))
(if (symbol? node)
(seq
; FIXME false not possible
(def value (pick env node #f))
(if value
(if (pair? value)
(ok (cons lit-sym value))
(ok value))
(fail node)))
(ok node))))
; bootstrap ->
(def (bootstrap exp ok fail)
(apply-env initial-env (compile (ast exp))
(fn (exp)
(ok
; prefix code with LEAF to mark begining of combinator code
(cons 76 (cons 69 (cons 65 (cons 70 (serialize-cexp exp null)))))))
(fn (name)
(fail (cons undefined-sym name)))))
(def (evaluate exp env ok fail)
(def lexp (ast exp))
(def cexp (compile lexp))
;(append " exp: ") (render-any exp) (emit 10)
;(append "lexp: ") (render-any lexp) (emit 10)
;(append "cexp: ") (render-any cexp) (emit 10)
(apply-env env cexp
(fn (val)
(ok (reflect val)))
(fn (name)
(fail (cons undefined-sym name)))))
; FIXME pattern match later
(def (definition? exp)
(and (pair? exp) (eq? (car exp) def-sym)))
; (repl-defn name val env cont) -> (cont env')
(def (repl-defn name val env cont)
;(render-any val) (emit 10)
(if (pair? name)
(repl-defn
(car name)
(cons fn-sym (cons (cdr name) (cons val null)))
env cont)
(evaluate val env
(fn (value)
(append "Defining ")
(render-any name)
(cont (cons (cons name value) env)))
(fn (reason)
; FIXME HACK: repl fills in recursive definitions by itself
(if (and (eq? (car reason) undefined-sym) (eq? (cdr reason) name))
(seq
(def formals (cadr val))
(def body (caddr val))
(repl-defn name
(cons y-sym
(cons
(cons fn-sym (cons (cons name formals) (cons body null)))
null))
env cont))
(seq
(append "Definition failed: ")
(render-any reason)
(cont env)))))))
(def (repl in syms env)
;(emit 10)
;(render-any (map car env))
;(emit 10)
(emit 10)
(append "* ")
(parse syms in
(fn (syms val in)
; (@ 43) (@ 32) (debug val) (@ 10)
(if (and (pair? val) (eq? (car val) def-sym))
(repl-defn (cadr val) (cons seq-sym (cddr val)) env
(fn (env)
(repl in syms env)))
(if (and (pair? val) (eq? (car val) boot-sym))
(bootstrap (cadr val)
(fn (bytes)
(append bytes)
(repl in syms env))
(fn (error)
(append "Bootstrap failed: ")
(render-any error)
(repl in syms env)))
(evaluate val env
(fn (val)
(render-value val)
(repl in syms env))
(fn (reason)
(append "Failed: ")
(render-any reason)
(repl in syms env))))))
(seq
(emit 63)
(emit 63)
(emit 63)