-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraillisp.fth
1606 lines (1348 loc) · 37 KB
/
raillisp.fth
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
\ -*- forth -*-
0 warnings !
utime drop constant start-time
here constant start-here
0 constant lisp-pair-tag
1 constant lisp-number-tag
2 constant lisp-symbol-tag
3 constant lisp-string-tag
4 constant lisp-vector-tag
5 constant lisp-function-tag
6 constant lisp-max-tag
create display-dispatch lisp-max-tag cells allot
create equal?-dispatch lisp-max-tag cells allot
create interpret-dispatch lisp-max-tag cells allot
create compile-dispatch lisp-max-tag cells allot
create type-names lisp-max-tag cells allot
\ lisp structs. The forth struct feature is not used for portability
\ struct
\ cell% field pair-tag
\ cell% field pair-car
\ cell% field pair-cdr
\ end-struct lisp-pair
\ translates to:
\ : pair-tag 0 + ;
: pair-car [ 1 cells ] literal + ;
: pair-cdr [ 2 cells ] literal + ;
3 cells constant sizeof-pair
\ : symbol-tag 0 + ;
: symbol-nameu [ 1 cells ] literal + ;
: symbol-namea [ 2 cells ] literal + ;
3 cells constant sizeof-symbol
\ : vector-tag 0 + ;
: vector-len [ 1 cells ] literal + ;
: vector-vec [ 2 cells ] literal + ;
3 cells constant sizeof-vector
variable curr-func
variable last-def
variable lisp-latest
-1 1 rshift invert dup
dup constant lisp-macro-mask
1 rshift dup constant lisp-special-mask
dup rot or swap 1 rshift dup constant lisp-&rest-mask
dup rot or swap 1 rshift dup constant lisp-indirect-mask
or invert constant lisp-len-mask
: func>symbol [ 1 cells ] literal + ;
: func>args [ 2 cells ] literal + ;
: func>returns [ 3 cells ] literal + ;
: func>flags [ 4 cells ] literal + ;
: get-lisp-tag ( lisp - type )
dup 1 and if
drop lisp-number-tag
else
dup 0= over -1 = or if
drop lisp-number-tag
else
( lisp-tag) @
then
then ;
: car ( pair -- lisp ) dup 0<> if pair-car @ then ;
: cdr ( pair -- lisp ) dup 0<> if pair-cdr @ then ;
: tag-num ( number -- lisp ) 1 lshift 1 or ;
: untag-num ( lisp - number ) 1 rshift ;
: symbol->string ( lisp -- namea nameu )
\ ." *symbol->string:* " dup . cr
dup symbol-namea @ swap symbol-nameu @ ;
: sym>string ( sym - namea nameu )
\ sym>name dup 1 cells + swap @ ;
\ symbol->string ; ( temp fix for compatibility with symbol struct)
dup symbol-namea @ swap symbol-nameu @ ;
: func>string ( f - a u ) func>symbol @ sym>string ;
: lisp. ( lisp -- )
dup 0= if
drop ." nil"
else
dup -1 = if
drop ." t"
else
dup get-lisp-tag
cells display-dispatch + @ execute
then then ;
: lisp-display-pair ( lisp -- )
[char] ( emit ( 32 emit)
begin
dup car lisp. 32 emit
cdr
dup 0<> if
dup get-lisp-tag lisp-pair-tag <> if
[char] . emit 32 emit lisp. 0
then
then
dup 0=
until
drop
[char] ) emit ;
' lisp-display-pair display-dispatch lisp-pair-tag cells + !
: lisp-display-number ( lisp -- ) untag-num . ;
' lisp-display-number display-dispatch lisp-number-tag cells + !
: lisp-display-symbol ( lisp -- ) symbol->string type ;
' lisp-display-symbol display-dispatch lisp-symbol-tag cells + !
: lisp-display-string ( lisp -- ) symbol->string type ;
' lisp-display-string display-dispatch lisp-string-tag cells + !
: lisp-display-vector ( lisp -- )
[char] [ emit
dup vector-vec swap vector-len @ 0 ?do
dup i cells + @ lisp. ." "
loop
drop
[char] ] emit ;
' lisp-display-vector display-dispatch lisp-vector-tag cells + !
: lisp-display-function ( lisp - )
[char] $ emit func>string type ;
' lisp-display-function display-dispatch lisp-function-tag cells + !
: func-macro? func>flags @ lisp-macro-mask and ;
: func-special? func>flags @ lisp-special-mask and ;
: func-&rest? func>flags @ lisp-&rest-mask and ;
: func-indirect? func>flags @ lisp-indirect-mask and ;
: func>int [ 5 cells ] literal + @ ;
: func-set-bit ( mask - ) last-def @ func>flags dup @ rot or swap ! ;
: func-indirect! lisp-indirect-mask func-set-bit ;
: func-special! lisp-special-mask func-set-bit ;
: func-macro! lisp-macro-mask func-set-bit func-special! ;
: func-&rest! lisp-&rest-mask func-set-bit ;
: func-args! ( n - ) last-def @ func>args ! ;
: func-returns! last-def @ func>returns ! ;
: set-curr-func ( func - ) curr-func ! ;
: curr-args ( - n ) curr-func @ dup 0<> if func>args @ then ;
: curr-returns ( - n ) curr-func @ dup 0<> if func>returns @ then ;
: curr-&rest ( - x ) curr-func @ dup 0<> if func-&rest? then ;
: check-alloc dup 1 and if ." lsb set" 1 throw then ;
variable stack-counter
\ Symbol struct formats:
\ Uninterned symbol: [type tag, name len, name...]
\ Interned symbol: value, parent ptr, [type tag, name len, name...]
: sym>value [ 2 cells ] literal - ;
: sym>parent [ 1 cells ] literal - ;
: sym>name [ 1 cells ] literal + ;
: make-sym ( namea nameu - lisp )
align 0 , \ symbol value
lisp-latest dup @ , \ parent pointer
here dup >r lisp-symbol-tag , \ type tag
swap ! \ set lisp-latest
dup , \ name length
\ mem, \ name
here [ 1 cells ] literal + , mem, ( temp fix for compatibility with symbol struct)
r>
;
: sym-lookup ( namea nameu - sym )
lisp-latest @ >r
begin
r@ 0<>
while
2dup r@ sym>string
compare 0= if
2drop r> exit
then
r> sym>parent @ >r
repeat
2drop r> drop 0 ;
: str-intern ( namea nameu - sym )
2dup sym-lookup dup 0<> if
nip nip exit \ already interned
else
drop make-sym
then ;
defined vtcopy, [if]
: lisp-header, :noname postpone [ 2drop 2drop 2drop ;
[else]
: lisp-header, :noname postpone [ 2drop 2drop drop ;
[then]
: start-defun ( namea nameu - )
str-intern align here last-def !
lisp-function-tag , \ tag
dup , \ symbol
0 , \ argument count
1 , \ return count
0 , \ flags
here 1221 , \ xt
lisp-header, latestxt swap !
last-def @ swap sym>value !
;
: end-defun postpone exit ;
: (defun ( num-args - )
parse-name start-defun
func-args! 1 func-returns!
] ;
: ) postpone [ end-defun ; immediate
: sym-print ( sym - )
." sym(" sym>string type ." )" ;
: builtin-func ( args returns - )
\ makes a lisp wrapper around a forth word
latest name>string str-intern >r
align here last-def !
lisp-function-tag , \ tag
r@ , \ symbol
swap , \ arg count
, \ return count
0 , \ flags
latestxt , \ word
last-def @ r> sym>value !
func-indirect!
;
: cons ( car cdr -- lisp )
sizeof-pair allocate throw check-alloc >r
r@ ( pair-tag) lisp-pair-tag swap !
r@ pair-cdr !
r@ pair-car !
r> ;
: create-symbol ( namea nameu -- lisp )
sizeof-symbol allocate throw check-alloc >r
r@ ( symbol-tag) lisp-symbol-tag swap !
r@ symbol-nameu !
r@ symbol-namea !
r> ;
: create-string ( namea nameu -- lisp )
create-symbol
dup ( symbol-tag) lisp-string-tag swap ! ;
: create-vector ( length -- lisp )
\ TODO: allocate vector header as part of the vector block
dup cells sizeof-vector + allocate throw check-alloc >r
r@ ( vector-tag) lisp-vector-tag swap !
r@ vector-len ! r> ;
: find-xt-sym ( xt - sym )
lisp-latest @
begin
dup 0<>
while
\ TODO: should not assume symbols and functions have the same ordering
dup sym>value @
dup get-lisp-tag lisp-function-tag = if
func>int rot dup rot
> if drop exit else swap then
else
drop
then
sym>parent @
repeat
2drop 0 ;
r@ constant rstack-base
: print-stack-trace
r> 0
begin
r> dup rstack-base <>
while
swap dup . 1 + swap ." "
dup . ." "
find-xt-sym dup if
sym>string type cr
else drop ." -----" cr
then
repeat
>r drop >r ;
defer raise
-1 constant lisp-true
: function-lookup ( namea nameu - func )
2dup sym-lookup dup 0= if
drop ." Undefined function: " type raise
else
nip nip sym>value @
then ;
: string-new ( a u -- a u )
dup rot over allocate drop
dup >r rot cmove r> swap ;
: symbol-new ( namea nameu -- lisp )
2dup sym-lookup dup 0<> if
\ Return the interned symbol
nip nip exit
else
\ Allocate a new symbol. Don't intern it.
\ Interned syms cannot be deleted. Don't want to use
\ up space for temp symbols like parameter names.
drop string-new create-symbol
then ;
: str-equal? ( lisp1 lisp2 -- lisp )
symbol->string rot symbol->string
compare 0= if
lisp-true
else
nil
then ;
: vec-compare ( v1 v2 len - )
0 ?do
2dup @ swap @ <> if unloop 2drop nil exit then
1+ swap 1+
loop
2drop lisp-true ;
: vec-equal? ( lisp1 lisp2 -- lisp )
2dup vector-len @ swap vector-len @ dup >r
= if r> drop 2drop lisp-true
else vector-vec swap vector-vec r> vec-compare then ;
: equal? ( lisp lisp - lisp )
2dup = if
2drop lisp-true
else
2dup 1 and swap 1 and or if
2drop nil \ number
else
2dup 2dup 0= swap 0= or -rot -1 = swap -1 = or or if
2drop nil \ nil or t
else
2dup get-lisp-tag swap get-lisp-tag <> if
2drop nil
else
dup get-lisp-tag cells equal?-dispatch + @ execute
then
then
then
then ;
: cons-equal? ( lisp lisp - lisp )
\ todo: non recursive version
2dup car swap car equal?
-rot cdr swap cdr equal?
and ;
' str-equal? equal?-dispatch lisp-symbol-tag cells + !
' str-equal? equal?-dispatch lisp-string-tag cells + !
' cons-equal? equal?-dispatch lisp-pair-tag cells + !
' vec-equal? equal?-dispatch lisp-vector-tag cells + !
s" &rest" str-intern constant &rest
: get-vararg
recursive ( arglist - vararg)
\ return the vararg symbol and remove from arglist
dup 0<> if
dup cdr 0<> if
dup cdr car &rest str-equal? if
dup cdr cdr car \ vararg
swap pair-cdr 0 swap !
else
cdr get-vararg
then
else
drop 0
then
else
drop 0
then ;
: split-args ( arglist - args vararg)
dup 0<> if
dup car &rest str-equal? if
0 swap cdr car
else
dup get-vararg
then
else
dup
then ;
: error-undefined-value
cr ." undefined value: " lisp. cr raise ;
\ interpretation words must return a single lisp value
\ compilation words must return nothing
variable lisp-state
: start-compile 1 lisp-state ! ;
: end-compile 0 lisp-state ! ;
end-compile
\ Used by the compiler to signal when the current
\ form should leave a return value on the stack.
variable return-context \ 1 if currently in a return context
: rcontext{ ( v - ) return-context @ r> swap >r >r return-context ! ;
: }rcontext ( v - ) r> r> swap >r return-context ! ;
: do-list-push ( x list - ) dup @ rot swap cons swap ! ;
: do-list-pop ( list - x ) dup @ dup car swap cdr rot ! ;
\ STACK is a list representing the current stack of the compiled program.
\ symbols in this list represent named stack positons (locals variables).
\ anonymous stack entries are nil.
variable stack
0 stack !
variable stack-depth
0 stack-depth !
: stack-print stack @ lisp. cr ;
: stack-push ( v - )
stack-depth dup @ 1+ swap !
stack do-list-push ;
: stack-push* ( - )
\ pushes a unique number onto the locals stack
stack-counter dup @ 1+ dup tag-num stack-push swap ! ;
: stack-drop ( - )
stack-depth dup @ dup 0= if
." compiler error: stack underflow" cr raise
then
1- swap !
stack do-list-pop drop ;
: stack-drop-n ( n - )
begin dup 0 > while
1- stack-drop
repeat drop ;
: stack-push-n ( n - )
begin dup while
1- stack-push*
repeat drop ;
: check-stack-depth ( n - )
dup stack-depth @ <> if
cr
." Compilation error" cr
." :: function '" last-def @ func>string type
." ' left " stack-depth @ . ." items on the stack, expected " . cr
." :: stack: " stack-print cr
raise
then drop ;
: maybe-ret ( - t ) \ used to return nil if in return context
return-context @ if 0 postpone literal stack-push* then nil ;
: lisp-interpret ( lisp - lisp? )
dup 0<> if
dup get-lisp-tag cells
lisp-state @
0= if interpret-dispatch else compile-dispatch then
+ @ execute
else
lisp-state @ 1 = if maybe-ret 2drop then
then ;
: lisp-interpret-list ( lisp - a1...an n )
0 >r
begin
dup 0<>
while
dup car lisp-interpret
swap cdr
r> 1+ >r
repeat
drop r> ;
: lisp-compile-list ( lisp - count )
0 swap
begin
dup 0<>
while
dup car lisp-interpret
cdr swap 1+ swap
repeat drop ;
: lisp-interpret-r ( lisp - lisp?)
\ forces return context
\ used when the lisp being compiled consumes the return result
\ The caller is responsible for maintaining the locals stack
1 rcontext{ lisp-interpret }rcontext ;
: lisp-compile-list-nr 0 rcontext{ lisp-compile-list drop }rcontext ;
: lisp-compile-progn ( lisp - )
>r return-context @ dup
0 return-context !
begin
r@ 0<>
while
r@ cdr 0= if
return-context !
then
r> dup car lisp-interpret cdr >r
repeat
return-context !
r> drop ;
\ special forms with < 0 args are passed all the arguments as a list
: special ( immediate) -1 0 builtin-func func-special! ;
: special* special func-&rest! ;
: progn lisp-compile-progn ; special*
variable macro-flag
: set-macro-flag 1 macro-flag ! ;
\ the reader
variable paren-count
variable stdin-lastchar
variable stdin-unread
variable read-from-string
: update-paren-count ( c - c )
dup [char] ( = if
paren-count dup @ 1+ swap !
else
dup [char] ) = if
paren-count dup @ 1- swap !
then
then ;
create read-buffer 64 allot
variable fd
: read-next-line ( - e a )
pad dup 100 accept
2dup + 10 swap c! 1+
over + swap ;
: next-line-from-file ( - e a )
fd @ dup if
begin
dup read-buffer 64 rot read-line throw
\ while not EOF and line is empty
2dup swap 0= and
while
2drop \ empty line
repeat
0= if \ EOF flag
over close-file throw 0 fd !
then
dup 0<> if \ read count
swap drop read-buffer
over 64 < if
\ reached end of line, insert a newline at end of buffer
2dup + 10 swap c! swap 1 +
else
swap
then
over + swap
else
2drop 0 dup
then
else
drop 0 dup
then ;
: next-key ( e a - e a c )
2dup <= if
\ TODO: reading multi-line strings
paren-count @ 0<> if
\ End of line but not end of list, read another line
cr ." : "
drop drop read-next-line
else
\ End of line
0 exit
then
then
dup c@ swap 1+ swap
update-paren-count \ TODO: only do this when reading lists
;
: lisp-char-from-input
stdin-unread @ if
0 stdin-unread !
stdin-lastchar @
else
next-key
dup stdin-lastchar !
then ;
: lisp-read-char ( e a -- e a c )
recursive
read-from-string @ if
2dup <= if
fd @ if
2drop next-line-from-file lisp-read-char
else
0
then
else
dup c@ swap 1+ swap
then
else
lisp-char-from-input
then ;
: lisp-unread-char ( e a -- e a )
read-from-string @ if
1-
else
1 stdin-unread !
then ;
: lisp-is-ws ( c -- flag )
dup 10 = swap dup 13 = swap dup 9 = swap 32 = or or or ;
: lisp-skip-ws ( e a -- e a )
lisp-read-char
begin
dup 0<> over lisp-is-ws and
while
drop lisp-read-char
repeat
0<> if
lisp-unread-char
then ;
: lisp-skip-line
lisp-read-char
begin
dup 0<> over 10 <> and \ 10 = \n
while
drop lisp-read-char
repeat
0<> if
lisp-unread-char
then ;
: lisp-skip-comments
begin
lisp-skip-ws lisp-read-char dup 59 = \ 59 = ;
while
drop lisp-skip-line
repeat
0<> if lisp-unread-char then ;
: lisp-skip lisp-skip-ws lisp-skip-comments ;
create token-buffer 128 allot
: lisp-read-token ( e a -- e a a u )
lisp-skip
0 >r
lisp-read-char
begin
dup [char] ) <> over 0<> and over lisp-is-ws 0= and
while
token-buffer r@ + c! r> 1+ >r lisp-read-char
repeat
0<> if
lisp-unread-char
then
token-buffer r> ;
defer lisp-read-lisp
: lisp-read-list-char
lisp-skip lisp-read-char dup [char] ) = swap 0 = or ;
: lisp-read-list-cons
lisp-unread-char lisp-read-lisp nil cons ;
: lisp-read-list ( e a -- e a lisp )
lisp-read-list-char if
nil
else
lisp-read-list-cons dup >r ( last item ) >r ( first item)
begin
lisp-read-list-char 0=
while
\ read next item, attach the cell to tail, set as new tail
lisp-read-list-cons dup r> pair-cdr ! >r
repeat
r> drop ( drop tail) r> ( return first)
then ;
: lisp-read-symbol ( e a -- e a lisp )
lisp-read-token 2dup s>number? if
drop tag-num nip nip
else
2drop 2dup s" nil" compare 0= if
nil -rot 2drop
else
symbol-new
then
then ;
: lisp-read-quote ( e a -- e a lisp )
lisp-read-lisp nil cons
s" quote" symbol-new swap cons ;
: lisp-escape-char ( c - c )
dup [char] n = if
drop 10
else
dup [char] \ = if \ todo: move escape codes to lisp
drop [char] \
else
dup [char] " = if
drop [char] "
else
cr ." invalid escape code: " emit cr throw
then
then
then ;
: lisp-read-string
0 >r
lisp-read-char
begin
dup 0<> over [char] " <> and
while
dup [char] \ = if
drop lisp-read-char lisp-escape-char
then
token-buffer r@ + c! r> 1+ >r lisp-read-char
repeat
dup 0<> swap [char] " <> and if
lisp-unread-char
then
token-buffer r>
string-new create-string ;
: _lisp-read-lisp ( e a -- e a lisp )
lisp-skip lisp-read-char
dup 0= if
drop 0
else
dup [char] ( = if
drop lisp-read-list
else
dup [char] ' = if
drop lisp-read-quote
else
dup [char] " = if
drop lisp-read-string
else
[char] ? = if
lisp-read-char tag-num \ char literal
else
lisp-unread-char lisp-read-symbol
then
then
then
then
then ;
' _lisp-read-lisp is lisp-read-lisp
: lisp-load-from-string ( a u -- lisp )
read-from-string @ 1 read-from-string !
over + swap 0 >r
begin
lisp-skip 2dup >
fd @ 0<> or
while
r> drop lisp-read-lisp lisp-interpret >r
repeat
2drop read-from-string ! r> ;
: lisp-read-from-string ( a u -- lisp )
\ save file reading state as we may be in the process of reading
fd @ -rot read-from-string @ -rot
0 fd ! 1 read-from-string !
over + swap lisp-skip lisp-read-lisp >r 2drop
read-from-string ! fd ! \ restore
r> ;
: lisp-load-from-file ( a u -- lisp )
r/o open-file
0<> if
drop ( 0 fd !) 0
else
fd @ swap fd ! 0 0 lisp-load-from-string
fd @ throw swap fd !
then ;
: lisp-list-len ( list - n )
0 swap
begin
dup 0<>
while
swap 1+ swap cdr
repeat drop ;
: comp, comp' drop postpone literal
[comp'] compile, drop compile, ; immediate
: maybe-drop \ compile a call to drop if not in return context
return-context @ 0= if
lisp-state @ 0= if
\ drop
else
comp, drop
stack-drop
then
then ;
: curr-func-name curr-func @ func>string ;
: arity-error ( actual expected u a - )
curr-func-name type
." - Invalid arg count, expected "
type . ." got " . cr raise ;
: check-arg-count ( argc - )
\ ARGC is the arg count curr-func is being called with
curr-args 2dup curr-&rest
if swap 1+ > if s" at least " arity-error then
else
<> curr-args 0> and if s" " arity-error then
then 2drop ;
: unpack-curr-args ( args - )
\ unpacks curr-args onto the stack
curr-args dup 0> if
curr-&rest if
1- 0 >r else 1 >r then >r \ rstack: &rest-flag arg-count -
begin r@ 0> while
dup car swap cdr \ unpack arg
r> 1- >r
repeat
r> ( count) drop
r> ( &rest flag) if drop then \ leave list tail for &rest arg
else drop then ;
: lisp-interpret-special ( lisp func - )
\ special form
\ Unpacks arguments onto stack before executing FUNCs word.
\ FUNC is a function struct
\ LISP has form: (function args...)
dup 0= if ." DEBUG ERROR: curr-func is nil" cr bye then \ temp
dup set-curr-func >r
cdr ( drop func) dup lisp-list-len check-arg-count
\ drop nil arglist if no args are expected. keep nil for &rest
dup 0= curr-&rest 0= and if drop then
unpack-curr-args
r> func>int execute ;
: lisp-interpret-macro ( lisp func - )
lisp-interpret-special lisp-interpret ;
: lisp-interpret-function ( lisp func - )
>r return-context @ >r 1 return-context !
cdr lisp-interpret-list
r> return-context !
r> dup set-curr-func func>int swap check-arg-count
execute ;
: lisp-compile-function ( lisp func - )
>r return-context @ 1 return-context !
swap cdr lisp-compile-list
swap return-context !
r> dup set-curr-func
func>int swap check-arg-count
compile, \ DOING: error here. check this function
curr-args stack-drop-n
curr-returns stack-push-n ;
: lisp-interpret-pair ( lisp - lisp?)
dup car
symbol->string function-lookup
dup func-macro? if
lisp-interpret-macro
else
dup func-special? if
lisp-interpret-special
else \ function
lisp-state @ 0= if
lisp-interpret-function
else
lisp-compile-function
then
maybe-drop
then then ;
' lisp-interpret-pair interpret-dispatch lisp-pair-tag cells + !
' lisp-interpret-pair compile-dispatch lisp-pair-tag cells + !
: lisp-interpret-number ;
: lisp-compile-number
dup stack-push
postpone literal
maybe-drop ;
' lisp-interpret-number interpret-dispatch lisp-number-tag cells + !
' lisp-compile-number compile-dispatch lisp-number-tag cells + !
: lisp-compile-string
stack-push*
postpone literal \ todo: compile it into the dictionary
maybe-drop ;
' lisp-interpret-number interpret-dispatch lisp-string-tag cells + !
' lisp-compile-string compile-dispatch lisp-string-tag cells + !
: get-list-func ( list - func?)
\ returns the function from the car of LIST
\ return 0 if it does not exist
dup get-lisp-tag lisp-pair-tag = if
dup car dup get-lisp-tag lisp-symbol-tag = if
symbol->string sym-lookup dup 0<> if
nip ( drop list) sym>value @ \ return
else 2drop 0 ( drop list, sym) then
else 2drop 0 ( drop list, car) then
else drop 0 ( drop list) then ;
: quote
lisp-state @ 0= if
car
else
car postpone literal
stack-push*
then
; special
\ locals-counter counts the locals in the current function
variable locals-counter
: ++locals locals-counter dup @ 1+ swap ! ;
: push-local-name ( symbol - ) stack-push ;
: push-local-names ( list - )
recursive
dup 0<> if
dup car push-local-name
cdr push-local-names
else drop then ;
: pop-local-names ( n - )
recursive
dup 0> if
stack-drop
return-context @ 1 = if
comp, nip
else
comp, drop
then
1- pop-local-names
else drop then ;
: list-index ( elem list - index )
0 >r
begin
dup 0<> if
2dup car equal? 0=
else 0 then
while
cdr r> 1+ >r
repeat
if drop r> else r> 2drop -1 then ;
: compile-pick postpone literal comp, pick ;
: compile-dup comp, dup ;
: compile-over comp, over ;
: compile-nip comp, nip ;