4 include term-colours.4th
6 include throw-catch.4th
18 nexttype @ 1+ nexttype !
23 make-type boolean-type
24 make-type character-type
29 make-type primitive-proc-type
30 make-type compound-proc-type
31 : istype? ( obj type -- obj bool )
34 \ ------ Cons cell memory ------ {{{
37 create car-cells N allot
38 create car-type-cells N allot
39 create cdr-cells N allot
40 create cdr-type-cells N allot
45 : cons ( car-obj cdr-obj -- pair-obj )
46 cdr-type-cells nextfree @ + !
47 cdr-cells nextfree @ + !
48 car-type-cells nextfree @ + !
49 car-cells nextfree @ + !
56 : car ( pair-obj -- car-obj )
58 dup car-cells + @ swap
62 : cdr ( pair-obj -- car-obj )
64 dup cdr-cells + @ swap
68 : set-car! ( obj pair-obj -- )
70 rot swap car-type-cells + !
74 : set-cdr! ( obj pair-obj -- )
76 rot swap cdr-type-cells + !
86 : nil? nil-type istype? ;
88 : objvar create nil swap , , ;
90 : value@ ( objvar -- val ) @ ;
91 : type@ ( objvar -- type ) 1+ @ ;
92 : value! ( newval objvar -- ) ! ;
93 : type! ( newtype objvar -- ) 1+ ! ;
94 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
95 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
97 : objeq? ( obj obj -- bool )
100 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
101 >R >R ( a1 a2 b1 b2 )
102 2swap ( b1 b2 a1 a2 )
103 R> R> ( b1 b2 a1 a2 c1 c2 )
107 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
108 2swap ( a1 a2 c1 c2 b1 b2 )
109 >R >R ( a1 a2 c1 c2 )
110 2swap ( c1 c2 a1 a2 )
116 \ ---- Pre-defined symbols ---- {{{
120 : duplicate-charlist ( charlist -- copy )
121 2dup nil objeq? false = if
122 2dup car 2swap cdr recurse cons
125 : charlist-equiv ( charlist charlist -- bool )
134 2drop 2drop true exit
136 2drop 2drop false exit
141 2drop 2drop false exit
148 car drop -rot car drop = if
149 cdr 2swap cdr recurse
155 : charlist>symbol ( charlist -- symbol-obj )
174 drop symbol-type 2dup
175 symbol-table obj@ cons
180 : (create-symbol) ( addr n -- symbol-obj )
184 2dup drop @ character-type 2swap
192 : create-symbol ( -- )
206 does> dup @ swap 1+ @
209 create-symbol quote quote-symbol
210 create-symbol define define-symbol
211 create-symbol set! set!-symbol
212 create-symbol ok ok-symbol
213 create-symbol if if-symbol
214 create-symbol lambda lambda-symbol
215 create-symbol λ λ-symbol
219 \ ---- Environments ---- {{{
221 : enclosing-env ( env -- env )
224 : first-frame ( env -- frame )
227 : make-frame ( vars vals -- frame )
230 : frame-vars ( frame -- vars )
233 : frame-vals ( frame -- vals )
236 : add-binding ( var val frame -- )
237 2swap 2over frame-vals cons
239 2swap 2over frame-vars cons
243 : extend-env ( vars vals env -- env )
253 : get-vars-vals-frame ( var frame -- bool )
254 2dup frame-vars vars obj!
258 vars obj@ nil objeq? false =
260 2dup vars obj@ car objeq? if
265 vars obj@ cdr vars obj!
266 vals obj@ cdr vals obj!
272 : get-vars-vals ( var env -- vars? vals? bool )
275 2dup nil objeq? false =
277 2over 2over first-frame
278 get-vars-vals-frame if
280 vars obj@ vals obj@ true
294 : lookup-var ( var env -- val )
298 bold fg red ." Tried to read unbound variable." reset-term cr abort
302 : set-var ( var val env -- )
303 >R >R 2swap R> R> ( val var env )
305 2swap 2drop ( val vals )
308 bold fg red ." Tried to set unbound variable." reset-term cr abort
314 : define-var ( var val env -- )
317 2over env obj@ ( var val var env )
319 2swap 2drop ( var val vals )
324 first-frame ( var val frame )
332 nil nil nil extend-env
337 \ ---- Primitives ---- {{{
339 : make-primitive ( cfa -- )
352 rot primitive-proc-type ( var prim )
353 global-env obj@ define-var
357 bold fg red ." Incorrect argument count." reset-term cr
361 : ensure-arg-count ( args n -- )
363 drop nil objeq? false = if
367 -rot 2dup nil objeq? if
376 bold fg red ." Incorrect argument type." reset-term cr
380 : ensure-arg-type ( arg type -- arg )
386 include scheme-primitives.4th
393 variable stored-parse-idx
394 create parse-str 161 allot
395 variable parse-str-span
397 create parse-idx-stack 10 allot
398 variable parse-idx-sp
399 parse-idx-stack parse-idx-sp !
402 parse-idx @ parse-idx-sp @ !
407 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
411 parse-idx-sp @ @ parse-idx ! ;
415 '\n' parse-str parse-str-span @ + !
416 1 parse-str-span +! ;
423 parse-str 160 expect cr
424 span @ parse-str-span !
434 : charavailable? ( -- bool )
435 parse-str-span @ parse-idx @ > ;
437 : nextchar ( -- char )
438 charavailable? false = if getline then
439 parse-str parse-idx @ + @ ;
441 : whitespace? ( -- bool )
450 nextchar [char] ( = or
451 nextchar [char] ) = or
454 : commentstart? ( -- bool )
455 nextchar [char] ; = ;
459 false \ Indicates whether or not we're eating a comment
462 dup whitespace? or commentstart? or
464 dup nextchar '\n' = and if
465 invert \ Stop eating comment
467 dup false = commentstart? and if
468 invert \ Begin eating comment
483 nextchar [char] - = ;
486 nextchar [char] + = ;
488 : fixnum? ( -- bool )
520 : boolean? ( -- bool )
521 nextchar [char] # <> if false exit then
528 and if pop-parse-idx false exit then
540 : str-equiv? ( str -- bool )
557 delim? false = if drop false then
562 : character? ( -- bool )
563 nextchar [char] # <> if false exit then
568 nextchar [char] \ <> if pop-parse-idx false exit then
572 S" newline" str-equiv? if pop-parse-idx true exit then
573 S" space" str-equiv? if pop-parse-idx true exit then
574 S" tab" str-equiv? if pop-parse-idx true exit then
576 charavailable? false = if pop-parse-idx false exit then
582 nextchar [char] ( = ;
584 : string? ( -- bool )
585 nextchar [char] " = ;
587 : readnum ( -- num-atom )
598 10 * nextchar [char] 0 - +
607 : readbool ( -- bool-atom )
610 nextchar [char] f = if
621 : readchar ( -- char-atom )
625 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
626 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
627 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
629 nextchar character-type
634 : readstring ( -- charlist )
635 nextchar [char] " = if
640 ." No delimiter following right double quote. Aborting." cr
649 nextchar [char] \ = if
652 [char] n of '\n' endof
653 [char] " of [char] " endof
659 inc-parse-idx character-type
666 : readsymbol ( -- charlist )
667 delim? if nil exit then
669 nextchar inc-parse-idx character-type
676 : readpair ( -- pairobj )
680 nextchar [char] ) = if
685 ." No delimiter following right paren. Aborting." cr
694 \ Read first pair element
699 nextchar [char] . = if
704 ." No delimiter following '.'. Aborting." cr
718 \ Parse a scheme expression
744 nextchar [char] " <> if
745 bold red ." Missing closing double-quote." reset-term cr
763 nextchar [char] ) <> if
764 bold red ." Missing closing paren." reset-term cr
773 nextchar [char] ' = if
775 quote-symbol recurse nil cons cons exit
780 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
784 \ Anything else is parsed as a symbol
785 readsymbol charlist>symbol
787 \ Replace λ with lambda
788 2dup λ-symbol objeq? if
799 : self-evaluating? ( obj -- obj bool )
800 boolean-type istype? if true exit then
801 fixnum-type istype? if true exit then
802 character-type istype? if true exit then
803 string-type istype? if true exit then
804 nil-type istype? if true exit then
809 : tagged-list? ( obj tag-obj -- obj bool )
811 pair-type istype? false = if
817 : quote? ( obj -- obj bool )
818 quote-symbol tagged-list? ;
820 : quote-body ( quote-obj -- quote-body-obj )
823 : variable? ( obj -- obj bool )
824 symbol-type istype? ;
826 : definition? ( obj -- obj bool )
827 define-symbol tagged-list? ;
829 : make-lambda ( params body -- lambda-exp )
830 lambda-symbol -2rot cons cons ;
832 : definition-var ( obj -- var )
834 symbol-type istype? false = if car then
837 : definition-val ( obj -- val )
838 2dup cdr car symbol-type istype? if
847 : assignment? ( obj -- obj bool )
848 set!-symbol tagged-list? ;
850 : assignment-var ( obj -- var )
853 : assignment-val ( obj -- val )
856 : eval-definition ( obj env -- res )
858 2over 2over ( env obj env obj )
859 definition-val 2swap ( env obj valexp env )
862 2swap definition-var 2swap ( env var val )
870 : eval-assignment ( obj env -- res )
872 2over 2over ( env obj env obj )
873 assignment-val 2swap ( env obj valexp env )
876 2swap assignment-var 2swap ( env var val )
884 : if? ( obj -- obj bool )
885 if-symbol tagged-list? ;
887 : if-predicate ( ifobj -- pred )
890 : if-consequent ( ifobj -- conseq )
893 : if-alternative ( ifobj -- alt|false )
901 : false? ( boolobj -- boolean )
902 boolean-type istype? if
903 false boolean-type objeq?
909 : true? ( boolobj -- bool )
912 : lambda? ( obj -- obj bool )
913 lambda-symbol tagged-list? ;
915 : lambda-parameters ( obj -- params )
918 : lambda-body ( obj -- body )
921 : make-procedure ( params body env -- proc )
924 drop compound-proc-type
927 : application? ( obj -- obj bool)
930 : operator ( obj -- operator )
933 : operands ( obj -- operands )
936 : nooperands? ( operands -- bool )
939 : first-operand ( operands -- operand )
942 : rest-operands ( operands -- other-operands )
945 : list-of-vals ( args env -- vals )
951 2over 2over first-operand 2swap eval
952 -2rot rest-operands 2swap recurse
957 : procedure-params ( proc -- params )
960 : procedure-body ( proc -- body )
961 drop pair-type cdr car ;
963 : procedure-env ( proc -- body )
964 drop pair-type cdr cdr car ;
966 : apply ( proc args )
968 primitive-proc-type of
972 compound-proc-type of
973 2dup procedure-body ( args proc body )
974 -2rot 2dup procedure-params ( body args proc params )
975 -2rot procedure-env ( body params args procenv )
977 extend-env ( body env )
982 2dup cdr 2dup nil objeq? false =
984 -2rot car 2over ( nextbody env exp env )
986 2drop \ discard result
987 2swap ( env nextbody )
991 car 2swap ( exp env )
993 R> drop ['] eval goto-deferred \ Tail call optimization
996 bold fg red ." Object not applicable. Aboring." reset-term cr
1001 :noname ( obj env -- result )
1021 2swap eval-definition
1026 2swap eval-assignment
1042 ['] eval goto-deferred
1046 2dup lambda-parameters
1056 operands 2swap list-of-vals
1062 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1068 \ ---- Print ---- {{{
1070 : printnum ( numobj -- ) drop 0 .R ;
1072 : printbool ( numobj -- )
1080 : printchar ( charobj -- )
1083 9 of ." #\tab" endof
1084 bl of ." #\space" endof
1085 '\n' of ." #\newline" endof
1091 : (printstring) ( stringobj -- )
1092 nil-type istype? if 2drop exit then
1096 '\n' of ." \n" drop endof
1097 [char] \ of ." \\" drop endof
1098 [char] " of [char] \ emit [char] " emit drop endof
1104 : printstring ( stringobj -- )
1109 : printsymbol ( symbolobj -- )
1110 nil-type istype? if 2drop exit then
1116 : printnil ( nilobj -- )
1119 : printpair ( pairobj -- )
1123 nil-type istype? if 2drop exit then
1124 pair-type istype? if space recurse exit then
1128 : printprim ( primobj -- )
1129 2drop ." <primitive procedure>" ;
1131 : printcomp ( primobj -- )
1132 2drop ." <compound procedure>" ;
1135 fixnum-type istype? if printnum exit then
1136 boolean-type istype? if printbool exit then
1137 character-type istype? if printchar exit then
1138 string-type istype? if printstring exit then
1139 symbol-type istype? if printsymbol exit then
1140 nil-type istype? if printnil exit then
1141 pair-type istype? if ." (" printpair ." )" exit then
1142 primitive-proc-type istype? if printprim exit then
1143 compound-proc-type istype? if printcomp exit then
1145 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1154 cr ." Welcome to scheme.forth.jl!" cr
1155 ." Use Ctrl-D to exit." cr
1160 cr bold fg green ." > " reset-term
1162 global-env obj@ eval
1163 fg cyan ." ; " print reset-term