4 include term-colours.4th
24 make-type realnum-type
25 make-type boolean-type
26 make-type character-type
31 make-type primitive-proc-type
32 make-type compound-proc-type
33 : istype? ( obj type -- obj bool )
36 \ ------ Cons cell memory ------ {{{
39 create car-cells N allot
40 create car-type-cells N allot
41 create cdr-cells N allot
42 create cdr-type-cells N allot
44 create nextfrees N allot
55 nextfrees nextfree @ + @
58 : cons ( car-obj cdr-obj -- pair-obj )
59 cdr-type-cells nextfree @ + !
60 cdr-cells nextfree @ + !
61 car-type-cells nextfree @ + !
62 car-cells nextfree @ + !
68 : car ( pair-obj -- car-obj )
70 dup car-cells + @ swap
74 : cdr ( pair-obj -- car-obj )
76 dup cdr-cells + @ swap
80 : set-car! ( obj pair-obj -- )
82 rot swap car-type-cells + !
86 : set-cdr! ( obj pair-obj -- )
88 rot swap cdr-type-cells + !
98 : nil? nil-type istype? ;
100 : objvar create nil swap , , ;
102 : value@ ( objvar -- val ) @ ;
103 : type@ ( objvar -- type ) 1+ @ ;
104 : value! ( newval objvar -- ) ! ;
105 : type! ( newtype objvar -- ) 1+ ! ;
106 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
107 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
109 : objeq? ( obj obj -- bool )
112 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
113 >R >R ( a1 a2 b1 b2 )
114 2swap ( b1 b2 a1 a2 )
115 R> R> ( b1 b2 a1 a2 c1 c2 )
119 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
120 2swap ( a1 a2 c1 c2 b1 b2 )
121 >R >R ( a1 a2 c1 c2 )
122 2swap ( c1 c2 a1 a2 )
128 \ ---- Garbage Collection ---- {{{
142 : pairlike? ( obj -- obj bool )
143 pair-type istype? if true exit then
144 string-type istype? if true exit then
145 symbol-type istype? if true exit then
146 compound-proc-type istype? if true exit then
151 : pairlike-marked? ( obj -- obj bool )
155 : mark-pairlike ( obj -- obj )
156 over nextfrees + 0 swap !
159 : gc-mark-obj ( obj -- )
162 pairlike-marked? if 2drop exit then
178 nextfrees i + @ 0<> if
179 nextfree @ nextfrees i + !
186 \ ---- Pre-defined symbols ---- {{{
190 : duplicate-charlist ( charlist -- copy )
191 2dup nil objeq? false = if
192 2dup car 2swap cdr recurse cons
195 : charlist-equiv ( charlist charlist -- bool )
204 2drop 2drop true exit
206 2drop 2drop false exit
211 2drop 2drop false exit
218 car drop -rot car drop = if
219 cdr 2swap cdr recurse
225 : charlist>symbol ( charlist -- symbol-obj )
244 drop symbol-type 2dup
245 symbol-table obj@ cons
250 : (create-symbol) ( addr n -- symbol-obj )
254 2dup drop @ character-type 2swap
262 : create-symbol ( -- )
276 does> dup @ swap 1+ @
279 create-symbol quote quote-symbol
280 create-symbol define define-symbol
281 create-symbol set! set!-symbol
282 create-symbol ok ok-symbol
283 create-symbol if if-symbol
284 create-symbol lambda lambda-symbol
285 create-symbol λ λ-symbol
289 \ ---- Environments ---- {{{
291 : enclosing-env ( env -- env )
294 : first-frame ( env -- frame )
297 : make-frame ( vars vals -- frame )
300 : frame-vars ( frame -- vars )
303 : frame-vals ( frame -- vals )
306 : add-binding ( var val frame -- )
307 2swap 2over frame-vals cons
309 2swap 2over frame-vars cons
313 : extend-env ( vars vals env -- env )
323 : get-vars-vals-frame ( var frame -- bool )
324 2dup frame-vars vars obj!
328 vars obj@ nil objeq? false =
330 2dup vars obj@ car objeq? if
335 vars obj@ cdr vars obj!
336 vals obj@ cdr vals obj!
342 : get-vars-vals ( var env -- vars? vals? bool )
345 2dup nil objeq? false =
347 2over 2over first-frame
348 get-vars-vals-frame if
350 vars obj@ vals obj@ true
364 : lookup-var ( var env -- val )
368 bold fg red ." Tried to read unbound variable." reset-term cr abort
372 : set-var ( var val env -- )
373 >R >R 2swap R> R> ( val var env )
375 2swap 2drop ( val vals )
378 bold fg red ." Tried to set unbound variable." reset-term cr abort
384 : define-var ( var val env -- )
387 2over env obj@ ( var val var env )
389 2swap 2drop ( var val vals )
394 first-frame ( var val frame )
402 nil nil nil extend-env
407 \ ---- Primitives ---- {{{
409 : make-primitive ( cfa -- )
413 \ 2dup ." Defining primitive " type ." ..." cr
424 rot primitive-proc-type ( var prim )
425 global-env obj@ define-var
429 bold fg red ." Incorrect argument count." reset-term cr
433 : ensure-arg-count ( args n -- )
435 drop nil objeq? false = if
439 -rot 2dup nil objeq? if
448 bold fg red ." Incorrect argument type." reset-term cr
452 : ensure-arg-type ( arg type -- arg )
458 include scheme-primitives.4th
465 variable stored-parse-idx
466 create parse-str 161 allot
467 variable parse-str-span
469 create parse-idx-stack 10 allot
470 variable parse-idx-sp
471 parse-idx-stack parse-idx-sp !
474 parse-idx @ parse-idx-sp @ !
479 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
483 parse-idx-sp @ @ parse-idx ! ;
487 '\n' parse-str parse-str-span @ + !
488 1 parse-str-span +! ;
495 parse-str 160 expect cr
496 span @ parse-str-span !
506 : charavailable? ( -- bool )
507 parse-str-span @ parse-idx @ > ;
509 : nextchar ( -- char )
510 charavailable? false = if getline then
511 parse-str parse-idx @ + @ ;
513 : whitespace? ( -- bool )
522 nextchar [char] ( = or
523 nextchar [char] ) = or
526 : commentstart? ( -- bool )
527 nextchar [char] ; = ;
531 false \ Indicates whether or not we're eating a comment
534 dup whitespace? or commentstart? or
536 dup nextchar '\n' = and if
537 invert \ Stop eating comment
539 dup false = commentstart? and if
540 invert \ Begin eating comment
555 nextchar [char] - = ;
558 nextchar [char] + = ;
560 : fixnum? ( -- bool )
586 : realnum? ( -- bool )
593 \ Record starting parse idx:
594 \ Want to detect whether any characters (following +/-) were eaten.
601 [char] . nextchar = if
608 [char] e nextchar = [char] E nextchar = or if
616 drop pop-parse-idx false exit
624 \ This is a real number if characters were
625 \ eaten and the next characer is a delimiter.
626 parse-idx @ < delim? and
631 : boolean? ( -- bool )
632 nextchar [char] # <> if false exit then
639 and if pop-parse-idx false exit then
651 : str-equiv? ( str -- bool )
668 delim? false = if drop false then
673 : character? ( -- bool )
674 nextchar [char] # <> if false exit then
679 nextchar [char] \ <> if pop-parse-idx false exit then
683 S" newline" str-equiv? if pop-parse-idx true exit then
684 S" space" str-equiv? if pop-parse-idx true exit then
685 S" tab" str-equiv? if pop-parse-idx true exit then
687 charavailable? false = if pop-parse-idx false exit then
693 nextchar [char] ( = ;
695 : string? ( -- bool )
696 nextchar [char] " = ;
698 : readfixnum ( -- num-atom )
709 10 * nextchar [char] 0 - +
718 : readrealnum ( -- realnum )
720 \ Remember that at this point we're guaranteed to
721 \ have a parsable real on this line.
723 parse-str parse-idx @ +
725 begin delim? false = while
729 parse-str parse-idx @ + over -
736 : readbool ( -- bool-atom )
739 nextchar [char] f = if
750 : readchar ( -- char-atom )
754 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
755 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
756 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
758 nextchar character-type
763 : readstring ( -- charlist )
764 nextchar [char] " = if
769 ." No delimiter following right double quote. Aborting." cr
778 nextchar [char] \ = if
781 [char] n of '\n' endof
782 [char] " of [char] " endof
788 inc-parse-idx character-type
795 : readsymbol ( -- charlist )
796 delim? if nil exit then
798 nextchar inc-parse-idx character-type
805 : readpair ( -- pairobj )
809 nextchar [char] ) = if
814 ." No delimiter following right paren. Aborting." cr
823 \ Read first pair element
828 nextchar [char] . = if
833 ." No delimiter following '.'. Aborting." cr
847 \ Parse a scheme expression
878 nextchar [char] " <> if
879 bold red ." Missing closing double-quote." reset-term cr
897 nextchar [char] ) <> if
898 bold red ." Missing closing paren." reset-term cr
907 nextchar [char] ' = if
909 quote-symbol recurse nil cons cons exit
914 bold fg blue ." Moriturus te saluto." reset-term cr
918 \ Anything else is parsed as a symbol
919 readsymbol charlist>symbol
921 \ Replace λ with lambda
922 2dup λ-symbol objeq? if
933 : self-evaluating? ( obj -- obj bool )
934 boolean-type istype? if true exit then
935 fixnum-type istype? if true exit then
936 realnum-type istype? if true exit then
937 character-type istype? if true exit then
938 string-type istype? if true exit then
939 nil-type istype? if true exit then
944 : tagged-list? ( obj tag-obj -- obj bool )
946 pair-type istype? false = if
952 : quote? ( obj -- obj bool )
953 quote-symbol tagged-list? ;
955 : quote-body ( quote-obj -- quote-body-obj )
958 : variable? ( obj -- obj bool )
959 symbol-type istype? ;
961 : definition? ( obj -- obj bool )
962 define-symbol tagged-list? ;
964 : make-lambda ( params body -- lambda-exp )
965 lambda-symbol -2rot cons cons ;
967 : definition-var ( obj -- var )
969 symbol-type istype? false = if car then
972 : definition-val ( obj -- val )
973 2dup cdr car symbol-type istype? if
982 : assignment? ( obj -- obj bool )
983 set!-symbol tagged-list? ;
985 : assignment-var ( obj -- var )
988 : assignment-val ( obj -- val )
991 : eval-definition ( obj env -- res )
993 2over 2over ( env obj env obj )
994 definition-val 2swap ( env obj valexp env )
997 2swap definition-var 2swap ( env var val )
1005 : eval-assignment ( obj env -- res )
1007 2over 2over ( env obj env obj )
1008 assignment-val 2swap ( env obj valexp env )
1009 eval ( env obj val )
1011 2swap assignment-var 2swap ( env var val )
1013 2rot ( var val env )
1019 : if? ( obj -- obj bool )
1020 if-symbol tagged-list? ;
1022 : if-predicate ( ifobj -- pred )
1025 : if-consequent ( ifobj -- conseq )
1028 : if-alternative ( ifobj -- alt|false )
1036 : false? ( boolobj -- boolean )
1037 boolean-type istype? if
1038 false boolean-type objeq?
1044 : true? ( boolobj -- bool )
1047 : lambda? ( obj -- obj bool )
1048 lambda-symbol tagged-list? ;
1050 : lambda-parameters ( obj -- params )
1053 : lambda-body ( obj -- body )
1056 : make-procedure ( params body env -- proc )
1059 drop compound-proc-type
1062 : application? ( obj -- obj bool)
1065 : operator ( obj -- operator )
1068 : operands ( obj -- operands )
1071 : nooperands? ( operands -- bool )
1074 : first-operand ( operands -- operand )
1077 : rest-operands ( operands -- other-operands )
1080 : list-of-vals ( args env -- vals )
1086 2over 2over first-operand 2swap eval
1087 -2rot rest-operands 2swap recurse
1092 : procedure-params ( proc -- params )
1093 drop pair-type car ;
1095 : procedure-body ( proc -- body )
1096 drop pair-type cdr car ;
1098 : procedure-env ( proc -- body )
1099 drop pair-type cdr cdr car ;
1101 : apply ( proc args )
1103 primitive-proc-type of
1107 compound-proc-type of
1108 2dup procedure-body ( args proc body )
1109 -2rot 2dup procedure-params ( body args proc params )
1110 -2rot procedure-env ( body params args procenv )
1112 extend-env ( body env )
1117 2dup cdr 2dup nil objeq? false =
1119 -2rot car 2over ( nextbody env exp env )
1121 2drop \ discard result
1122 2swap ( env nextbody )
1126 car 2swap ( exp env )
1128 R> drop ['] eval goto-deferred \ Tail call optimization
1131 bold fg red ." Object not applicable. Aboring." reset-term cr
1136 :noname ( obj env -- result )
1156 2swap eval-definition
1161 2swap eval-assignment
1177 ['] eval goto-deferred
1181 2dup lambda-parameters
1191 operands 2swap list-of-vals
1197 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1203 \ ---- Print ---- {{{
1205 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1207 : printrealnum ( realnumobj -- ) drop float-print ;
1209 : printbool ( numobj -- )
1217 : printchar ( charobj -- )
1220 9 of ." #\tab" endof
1221 bl of ." #\space" endof
1222 '\n' of ." #\newline" endof
1228 : (printstring) ( stringobj -- )
1229 nil-type istype? if 2drop exit then
1233 '\n' of ." \n" drop endof
1234 [char] \ of ." \\" drop endof
1235 [char] " of [char] \ emit [char] " emit drop endof
1241 : printstring ( stringobj -- )
1246 : printsymbol ( symbolobj -- )
1247 nil-type istype? if 2drop exit then
1253 : printnil ( nilobj -- )
1256 : printpair ( pairobj -- )
1260 nil-type istype? if 2drop exit then
1261 pair-type istype? if space recurse exit then
1265 : printprim ( primobj -- )
1266 2drop ." <primitive procedure>" ;
1268 : printcomp ( primobj -- )
1269 2drop ." <compound procedure>" ;
1272 fixnum-type istype? if printfixnum exit then
1273 realnum-type istype? if printrealnum exit then
1274 boolean-type istype? if printbool exit then
1275 character-type istype? if printchar exit then
1276 string-type istype? if printstring exit then
1277 symbol-type istype? if printsymbol exit then
1278 nil-type istype? if printnil exit then
1279 pair-type istype? if ." (" printpair ." )" exit then
1280 primitive-proc-type istype? if printprim exit then
1281 compound-proc-type istype? if printcomp exit then
1283 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1292 cr ." Welcome to scheme.forth.jl!" cr
1293 ." Use Ctrl-D to exit." cr
1299 cr bold fg green ." > " reset-term
1302 global-env obj@ eval
1304 fg cyan ." ; " print reset-term