4 include term-colours.4th
6 include throw-catch.4th
14 0 constant fixnum-type
15 1 constant boolean-type
16 2 constant character-type
17 3 constant string-type
20 6 constant symbol-type
21 7 constant primitive-proc-type
22 8 constant compound-proc-type
23 : istype? ( obj type -- obj bool )
26 \ ------ Cons cell memory ------ {{{
29 create car-cells N allot
30 create car-type-cells N allot
31 create cdr-cells N allot
32 create cdr-type-cells N allot
37 : cons ( car-obj cdr-obj -- pair-obj )
38 cdr-type-cells nextfree @ + !
39 cdr-cells nextfree @ + !
40 car-type-cells nextfree @ + !
41 car-cells nextfree @ + !
48 : car ( pair-obj -- car-obj )
50 dup car-cells + @ swap
54 : cdr ( pair-obj -- car-obj )
56 dup cdr-cells + @ swap
60 : set-car! ( obj pair-obj -- )
62 rot swap car-type-cells + !
66 : set-cdr! ( obj pair-obj -- )
68 rot swap cdr-type-cells + !
78 : nil? nil-type istype? ;
80 : objvar create nil swap , , ;
82 : value@ ( objvar -- val ) @ ;
83 : type@ ( objvar -- type ) 1+ @ ;
84 : value! ( newval objvar -- ) ! ;
85 : type! ( newtype objvar -- ) 1+ ! ;
86 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
87 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
89 : objeq? ( obj obj -- bool )
92 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
95 R> R> ( b1 b2 a1 a2 c1 c2 )
99 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
100 2swap ( a1 a2 c1 c2 b1 b2 )
101 >R >R ( a1 a2 c1 c2 )
102 2swap ( c1 c2 a1 a2 )
108 \ ---- Pre-defined symbols ---- {{{
112 : duplicate-charlist ( charlist -- copy )
113 2dup nil objeq? false = if
114 2dup car 2swap cdr recurse cons
117 : charlist-equiv ( charlist charlist -- bool )
126 2drop 2drop true exit
128 2drop 2drop false exit
133 2drop 2drop false exit
140 car drop -rot car drop = if
141 cdr 2swap cdr recurse
147 : charlist>symbol ( charlist -- symbol-obj )
166 drop symbol-type 2dup
167 symbol-table obj@ cons
172 : (create-symbol) ( addr n -- symbol-obj )
176 2dup drop @ character-type 2swap
184 : create-symbol ( -- )
198 does> dup @ swap 1+ @
201 create-symbol quote quote-symbol
202 create-symbol define define-symbol
203 create-symbol set! set!-symbol
204 create-symbol ok ok-symbol
205 create-symbol if if-symbol
206 create-symbol lambda lambda-symbol
207 create-symbol λ λ-symbol
211 \ ---- Environments ---- {{{
213 : enclosing-env ( env -- env )
216 : first-frame ( env -- frame )
219 : make-frame ( vars vals -- frame )
222 : frame-vars ( frame -- vars )
225 : frame-vals ( frame -- vals )
228 : add-binding ( var val frame -- )
229 2swap 2over frame-vals cons
231 2swap 2over frame-vars cons
235 : extend-env ( vars vals env -- env )
245 : get-vars-vals-frame ( var frame -- bool )
246 2dup frame-vars vars obj!
250 vars obj@ nil objeq? false =
252 2dup vars obj@ car objeq? if
257 vars obj@ cdr vars obj!
258 vals obj@ cdr vals obj!
264 : get-vars-vals ( var env -- vars? vals? bool )
267 2dup nil objeq? false =
269 2over 2over first-frame
270 get-vars-vals-frame if
272 vars obj@ vals obj@ true
286 : lookup-var ( var env -- val )
290 bold fg red ." Tried to read unbound variable." reset-term cr abort
294 : set-var ( var val env -- )
295 >R >R 2swap R> R> ( val var env )
297 2swap 2drop ( val vals )
300 bold fg red ." Tried to set unbound variable." reset-term cr abort
306 : define-var ( var val env -- )
309 2over env obj@ ( var val var env )
311 2swap 2drop ( var val vals )
316 first-frame ( var val frame )
324 nil nil nil extend-env
329 \ ---- Primitives ---- {{{
331 : make-primitive ( cfa -- )
344 rot primitive-proc-type ( var prim )
345 global-env obj@ define-var
349 bold fg red ." Incorrect argument count." reset-term cr
353 : ensure-arg-count ( args n -- )
355 drop nil objeq? false = if
359 -rot 2dup nil objeq? if
368 bold fg red ." Incorrect argument type." reset-term cr
372 : ensure-arg-type ( arg type -- arg )
378 include scheme-primitives.4th
385 variable stored-parse-idx
386 create parse-str 161 allot
387 variable parse-str-span
389 create parse-idx-stack 10 allot
390 variable parse-idx-sp
391 parse-idx-stack parse-idx-sp !
394 parse-idx @ parse-idx-sp @ !
399 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
403 parse-idx-sp @ @ parse-idx ! ;
407 '\n' parse-str parse-str-span @ + !
408 1 parse-str-span +! ;
415 parse-str 160 expect cr
416 span @ parse-str-span !
426 : charavailable? ( -- bool )
427 parse-str-span @ parse-idx @ > ;
429 : nextchar ( -- char )
430 charavailable? false = if getline then
431 parse-str parse-idx @ + @ ;
433 : whitespace? ( -- bool )
442 nextchar [char] ( = or
443 nextchar [char] ) = or
446 : commentstart? ( -- bool )
447 nextchar [char] ; = ;
451 false \ Indicates whether or not we're eating a comment
454 dup whitespace? or commentstart? or
456 dup nextchar '\n' = and if
457 invert \ Stop eating comment
459 dup false = commentstart? and if
460 invert \ Begin eating comment
475 nextchar [char] - = ;
478 nextchar [char] + = ;
480 : fixnum? ( -- bool )
512 : boolean? ( -- bool )
513 nextchar [char] # <> if false exit then
520 and if pop-parse-idx false exit then
532 : str-equiv? ( str -- bool )
549 delim? false = if drop false then
554 : character? ( -- bool )
555 nextchar [char] # <> if false exit then
560 nextchar [char] \ <> if pop-parse-idx false exit then
564 S" newline" str-equiv? if pop-parse-idx true exit then
565 S" space" str-equiv? if pop-parse-idx true exit then
566 S" tab" str-equiv? if pop-parse-idx true exit then
568 charavailable? false = if pop-parse-idx false exit then
574 nextchar [char] ( = ;
576 : string? ( -- bool )
577 nextchar [char] " = ;
579 : readnum ( -- num-atom )
590 10 * nextchar [char] 0 - +
599 : readbool ( -- bool-atom )
602 nextchar [char] f = if
613 : readchar ( -- char-atom )
617 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
618 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
619 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
621 nextchar character-type
626 : readstring ( -- charlist )
627 nextchar [char] " = if
632 ." No delimiter following right double quote. Aborting." cr
641 nextchar [char] \ = if
644 [char] n of '\n' endof
645 [char] " of [char] " endof
651 inc-parse-idx character-type
658 : readsymbol ( -- charlist )
659 delim? if nil exit then
661 nextchar inc-parse-idx character-type
668 : readpair ( -- pairobj )
672 nextchar [char] ) = if
677 ." No delimiter following right paren. Aborting." cr
686 \ Read first pair element
691 nextchar [char] . = if
696 ." No delimiter following '.'. Aborting." cr
710 \ Parse a scheme expression
736 nextchar [char] " <> if
737 bold red ." Missing closing double-quote." reset-term cr
755 nextchar [char] ) <> if
756 bold red ." Missing closing paren." reset-term cr
765 nextchar [char] ' = if
767 quote-symbol recurse nil cons cons exit
772 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
776 \ Anything else is parsed as a symbol
777 readsymbol charlist>symbol
779 \ Replace λ with lambda
780 2dup λ-symbol objeq? if
791 : self-evaluating? ( obj -- obj bool )
792 boolean-type istype? if true exit then
793 fixnum-type istype? if true exit then
794 character-type istype? if true exit then
795 string-type istype? if true exit then
796 nil-type istype? if true exit then
801 : tagged-list? ( obj tag-obj -- obj bool )
803 pair-type istype? false = if
809 : quote? ( obj -- obj bool )
810 quote-symbol tagged-list? ;
812 : quote-body ( quote-obj -- quote-body-obj )
815 : variable? ( obj -- obj bool )
816 symbol-type istype? ;
818 : definition? ( obj -- obj bool )
819 define-symbol tagged-list? ;
821 : make-lambda ( params body -- lambda-exp )
822 lambda-symbol -2rot cons cons ;
824 : definition-var ( obj -- var )
826 symbol-type istype? false = if car then
829 : definition-val ( obj -- val )
830 2dup cdr car symbol-type istype? if
839 : assignment? ( obj -- obj bool )
840 set!-symbol tagged-list? ;
842 : assignment-var ( obj -- var )
845 : assignment-val ( obj -- val )
848 : eval-definition ( obj env -- res )
850 2over 2over ( env obj env obj )
851 definition-val 2swap ( env obj valexp env )
854 2swap definition-var 2swap ( env var val )
862 : eval-assignment ( obj env -- res )
864 2over 2over ( env obj env obj )
865 assignment-val 2swap ( env obj valexp env )
868 2swap assignment-var 2swap ( env var val )
876 : if? ( obj -- obj bool )
877 if-symbol tagged-list? ;
879 : if-predicate ( ifobj -- pred )
882 : if-consequent ( ifobj -- conseq )
885 : if-alternative ( ifobj -- alt|false )
893 : false? ( boolobj -- boolean )
894 boolean-type istype? if
895 false boolean-type objeq?
901 : true? ( boolobj -- bool )
904 : lambda? ( obj -- obj bool )
905 lambda-symbol tagged-list? ;
907 : lambda-parameters ( obj -- params )
910 : lambda-body ( obj -- body )
913 : make-procedure ( params body env -- proc )
916 drop compound-proc-type
919 : application? ( obj -- obj bool)
922 : operator ( obj -- operator )
925 : operands ( obj -- operands )
928 : nooperands? ( operands -- bool )
931 : first-operand ( operands -- operand )
934 : rest-operands ( operands -- other-operands )
937 : list-of-vals ( args env -- vals )
943 2over 2over first-operand 2swap eval
944 -2rot rest-operands 2swap recurse
949 : procedure-params ( proc -- params )
952 : procedure-body ( proc -- body )
953 drop pair-type cdr car ;
955 : procedure-env ( proc -- body )
956 drop pair-type cdr cdr car ;
958 : apply ( proc args )
960 primitive-proc-type of
964 compound-proc-type of
965 2dup procedure-body ( args proc body )
966 -2rot 2dup procedure-params ( body args proc params )
967 -2rot procedure-env ( body params args procenv )
969 extend-env ( body env )
974 2dup cdr 2dup nil objeq? false =
976 -2rot car 2over ( nextbody env exp env )
978 2drop \ discard result
979 2swap ( env nextbody )
983 car 2swap ( exp env )
985 R> drop ['] eval goto-deferred \ Tail call optimization
988 bold fg red ." Object not applicable. Aboring." reset-term cr
993 :noname ( obj env -- result )
1013 2swap eval-definition
1018 2swap eval-assignment
1034 ['] eval goto-deferred
1038 2dup lambda-parameters
1048 operands 2swap list-of-vals
1054 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1060 \ ---- Print ---- {{{
1062 : printnum ( numobj -- ) drop 0 .R ;
1064 : printbool ( numobj -- )
1072 : printchar ( charobj -- )
1075 9 of ." #\tab" endof
1076 bl of ." #\space" endof
1077 '\n' of ." #\newline" endof
1083 : (printstring) ( stringobj -- )
1084 nil-type istype? if 2drop exit then
1088 '\n' of ." \n" drop endof
1089 [char] \ of ." \\" drop endof
1090 [char] " of [char] \ emit [char] " emit drop endof
1096 : printstring ( stringobj -- )
1101 : printsymbol ( symbolobj -- )
1102 nil-type istype? if 2drop exit then
1108 : printnil ( nilobj -- )
1111 : printpair ( pairobj -- )
1115 nil-type istype? if 2drop exit then
1116 pair-type istype? if space recurse exit then
1120 : printprim ( primobj -- )
1121 2drop ." <primitive procedure>" ;
1123 : printcomp ( primobj -- )
1124 2drop ." <compound procedure>" ;
1127 fixnum-type istype? if printnum exit then
1128 boolean-type istype? if printbool exit then
1129 character-type istype? if printchar exit then
1130 string-type istype? if printstring exit then
1131 symbol-type istype? if printsymbol exit then
1132 nil-type istype? if printnil exit then
1133 pair-type istype? if ." (" printpair ." )" exit then
1134 primitive-proc-type istype? if printprim exit then
1135 compound-proc-type istype? if printcomp exit then
1137 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1146 cr ." Welcome to scheme.forth.jl!" cr
1147 ." Use Ctrl-D to exit." cr
1152 cr bold fg green ." > " reset-term
1154 global-env obj@ eval
1155 fg cyan ." ; " print reset-term