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 ------ {{{
38 1000 constant scheme-memsize
39 create car-cells scheme-memsize allot
40 create car-type-cells scheme-memsize allot
41 create cdr-cells scheme-memsize allot
42 create cdr-type-cells scheme-memsize allot
44 create nextfrees scheme-memsize allot
55 nextfrees nextfree @ + @
57 dup scheme-memsize < if
61 ." Out of memory! Aborting."
66 : cons ( car-obj cdr-obj -- pair-obj )
67 cdr-type-cells nextfree @ + !
68 cdr-cells nextfree @ + !
69 car-type-cells nextfree @ + !
70 car-cells nextfree @ + !
76 : car ( pair-obj -- car-obj )
78 dup car-cells + @ swap
82 : cdr ( pair-obj -- car-obj )
84 dup cdr-cells + @ swap
88 : set-car! ( obj pair-obj -- )
90 rot swap car-type-cells + !
94 : set-cdr! ( obj pair-obj -- )
96 rot swap cdr-type-cells + !
106 : nil? nil-type istype? ;
108 : objvar create nil swap , , ;
110 : value@ ( objvar -- val ) @ ;
111 : type@ ( objvar -- type ) 1+ @ ;
112 : value! ( newval objvar -- ) ! ;
113 : type! ( newtype objvar -- ) 1+ ! ;
114 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
115 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
117 : objeq? ( obj obj -- bool )
120 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
121 >R >R ( a1 a2 b1 b2 )
122 2swap ( b1 b2 a1 a2 )
123 R> R> ( b1 b2 a1 a2 c1 c2 )
127 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
128 2swap ( a1 a2 c1 c2 b1 b2 )
129 >R >R ( a1 a2 c1 c2 )
130 2swap ( c1 c2 a1 a2 )
136 \ ---- Garbage Collection ---- {{{
150 : pairlike? ( obj -- obj bool )
151 pair-type istype? if true exit then
152 string-type istype? if true exit then
153 symbol-type istype? if true exit then
154 compound-proc-type istype? if true exit then
159 : pairlike-marked? ( obj -- obj bool )
163 : mark-pairlike ( obj -- obj )
164 over nextfrees + 0 swap !
167 : gc-mark-obj ( obj -- )
170 pairlike-marked? if 2drop exit then
184 scheme-memsize nextfree !
185 0 scheme-memsize 1- do
186 nextfrees i + @ 0<> if
187 nextfree @ nextfrees i + !
194 \ ---- Pre-defined symbols ---- {{{
198 : duplicate-charlist ( charlist -- copy )
199 2dup nil objeq? false = if
200 2dup car 2swap cdr recurse cons
203 : charlist-equiv ( charlist charlist -- bool )
212 2drop 2drop true exit
214 2drop 2drop false exit
219 2drop 2drop false exit
226 car drop -rot car drop = if
227 cdr 2swap cdr recurse
233 : charlist>symbol ( charlist -- symbol-obj )
252 drop symbol-type 2dup
253 symbol-table obj@ cons
258 : (create-symbol) ( addr n -- symbol-obj )
262 2dup drop @ character-type 2swap
270 : create-symbol ( -- )
284 does> dup @ swap 1+ @
287 create-symbol quote quote-symbol
288 create-symbol define define-symbol
289 create-symbol set! set!-symbol
290 create-symbol ok ok-symbol
291 create-symbol if if-symbol
292 create-symbol lambda lambda-symbol
293 create-symbol λ λ-symbol
297 \ ---- Environments ---- {{{
299 : enclosing-env ( env -- env )
302 : first-frame ( env -- frame )
305 : make-frame ( vars vals -- frame )
308 : frame-vars ( frame -- vars )
311 : frame-vals ( frame -- vals )
314 : add-binding ( var val frame -- )
315 2swap 2over frame-vals cons
317 2swap 2over frame-vars cons
321 : extend-env ( vars vals env -- env )
331 : get-vars-vals-frame ( var frame -- bool )
332 2dup frame-vars vars obj!
336 vars obj@ nil objeq? false =
338 2dup vars obj@ car objeq? if
343 vars obj@ cdr vars obj!
344 vals obj@ cdr vals obj!
350 : get-vars-vals ( var env -- vars? vals? bool )
353 2dup nil objeq? false =
355 2over 2over first-frame
356 get-vars-vals-frame if
358 vars obj@ vals obj@ true
372 : lookup-var ( var env -- val )
376 bold fg red ." Tried to read unbound variable." reset-term cr abort
380 : set-var ( var val env -- )
381 >R >R 2swap R> R> ( val var env )
383 2swap 2drop ( val vals )
386 bold fg red ." Tried to set unbound variable." reset-term cr abort
392 : define-var ( var val env -- )
395 2over env obj@ ( var val var env )
397 2swap 2drop ( var val vals )
402 first-frame ( var val frame )
410 nil nil nil extend-env
415 \ ---- Primitives ---- {{{
417 : make-primitive ( cfa -- )
421 \ 2dup ." Defining primitive " type ." ..." cr
432 rot primitive-proc-type ( var prim )
433 global-env obj@ define-var
437 bold fg red ." Incorrect argument count." reset-term cr
441 : ensure-arg-count ( args n -- )
443 drop nil objeq? false = if
447 -rot 2dup nil objeq? if
456 bold fg red ." Incorrect argument type." reset-term cr
460 : ensure-arg-type ( arg type -- arg )
466 include scheme-primitives.4th
473 variable stored-parse-idx
474 create parse-str 161 allot
475 variable parse-str-span
477 create parse-idx-stack 10 allot
478 variable parse-idx-sp
479 parse-idx-stack parse-idx-sp !
482 parse-idx @ parse-idx-sp @ !
487 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
491 parse-idx-sp @ @ parse-idx ! ;
495 '\n' parse-str parse-str-span @ + !
496 1 parse-str-span +! ;
503 parse-str 160 expect cr
504 span @ parse-str-span !
514 : charavailable? ( -- bool )
515 parse-str-span @ parse-idx @ > ;
517 : nextchar ( -- char )
518 charavailable? false = if getline then
519 parse-str parse-idx @ + @ ;
521 : whitespace? ( -- bool )
530 nextchar [char] ( = or
531 nextchar [char] ) = or
534 : commentstart? ( -- bool )
535 nextchar [char] ; = ;
539 false \ Indicates whether or not we're eating a comment
542 dup whitespace? or commentstart? or
544 dup nextchar '\n' = and if
545 invert \ Stop eating comment
547 dup false = commentstart? and if
548 invert \ Begin eating comment
563 nextchar [char] - = ;
566 nextchar [char] + = ;
568 : fixnum? ( -- bool )
594 : realnum? ( -- bool )
601 \ Record starting parse idx:
602 \ Want to detect whether any characters (following +/-) were eaten.
609 [char] . nextchar = if
616 [char] e nextchar = [char] E nextchar = or if
624 drop pop-parse-idx false exit
632 \ This is a real number if characters were
633 \ eaten and the next characer is a delimiter.
634 parse-idx @ < delim? and
639 : boolean? ( -- bool )
640 nextchar [char] # <> if false exit then
647 and if pop-parse-idx false exit then
659 : str-equiv? ( str -- bool )
676 delim? false = if drop false then
681 : character? ( -- bool )
682 nextchar [char] # <> if false exit then
687 nextchar [char] \ <> if pop-parse-idx false exit then
691 S" newline" str-equiv? if pop-parse-idx true exit then
692 S" space" str-equiv? if pop-parse-idx true exit then
693 S" tab" str-equiv? if pop-parse-idx true exit then
695 charavailable? false = if pop-parse-idx false exit then
701 nextchar [char] ( = ;
703 : string? ( -- bool )
704 nextchar [char] " = ;
706 : readfixnum ( -- num-atom )
717 10 * nextchar [char] 0 - +
726 : readrealnum ( -- realnum )
728 \ Remember that at this point we're guaranteed to
729 \ have a parsable real on this line.
731 parse-str parse-idx @ +
733 begin delim? false = while
737 parse-str parse-idx @ + over -
744 : readbool ( -- bool-atom )
747 nextchar [char] f = if
758 : readchar ( -- char-atom )
762 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
763 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
764 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
766 nextchar character-type
771 : readstring ( -- charlist )
772 nextchar [char] " = if
777 ." No delimiter following right double quote. Aborting." cr
786 nextchar [char] \ = if
789 [char] n of '\n' endof
790 [char] " of [char] " endof
796 inc-parse-idx character-type
803 : readsymbol ( -- charlist )
804 delim? if nil exit then
806 nextchar inc-parse-idx character-type
813 : readpair ( -- pairobj )
817 nextchar [char] ) = if
822 ." No delimiter following right paren. Aborting." cr
831 \ Read first pair element
836 nextchar [char] . = if
841 ." No delimiter following '.'. Aborting." cr
855 \ Parse a scheme expression
886 nextchar [char] " <> if
887 bold red ." Missing closing double-quote." reset-term cr
905 nextchar [char] ) <> if
906 bold red ." Missing closing paren." reset-term cr
915 nextchar [char] ' = if
917 quote-symbol recurse nil cons cons exit
922 bold fg blue ." Moriturus te saluto." reset-term cr
926 \ Anything else is parsed as a symbol
927 readsymbol charlist>symbol
929 \ Replace λ with lambda
930 2dup λ-symbol objeq? if
941 : self-evaluating? ( obj -- obj bool )
942 boolean-type istype? if true exit then
943 fixnum-type istype? if true exit then
944 realnum-type istype? if true exit then
945 character-type istype? if true exit then
946 string-type istype? if true exit then
947 nil-type istype? if true exit then
952 : tagged-list? ( obj tag-obj -- obj bool )
954 pair-type istype? false = if
960 : quote? ( obj -- obj bool )
961 quote-symbol tagged-list? ;
963 : quote-body ( quote-obj -- quote-body-obj )
966 : variable? ( obj -- obj bool )
967 symbol-type istype? ;
969 : definition? ( obj -- obj bool )
970 define-symbol tagged-list? ;
972 : make-lambda ( params body -- lambda-exp )
973 lambda-symbol -2rot cons cons ;
975 : definition-var ( obj -- var )
977 symbol-type istype? false = if car then
980 : definition-val ( obj -- val )
981 2dup cdr car symbol-type istype? if
990 : assignment? ( obj -- obj bool )
991 set!-symbol tagged-list? ;
993 : assignment-var ( obj -- var )
996 : assignment-val ( obj -- val )
999 : eval-definition ( obj env -- res )
1001 2over 2over ( env obj env obj )
1002 definition-val 2swap ( env obj valexp env )
1003 eval ( env obj val )
1005 2swap definition-var 2swap ( env var val )
1007 2rot ( var val env )
1013 : eval-assignment ( obj env -- res )
1015 2over 2over ( env obj env obj )
1016 assignment-val 2swap ( env obj valexp env )
1017 eval ( env obj val )
1019 2swap assignment-var 2swap ( env var val )
1021 2rot ( var val env )
1027 : if? ( obj -- obj bool )
1028 if-symbol tagged-list? ;
1030 : if-predicate ( ifobj -- pred )
1033 : if-consequent ( ifobj -- conseq )
1036 : if-alternative ( ifobj -- alt|false )
1044 : false? ( boolobj -- boolean )
1045 boolean-type istype? if
1046 false boolean-type objeq?
1052 : true? ( boolobj -- bool )
1055 : lambda? ( obj -- obj bool )
1056 lambda-symbol tagged-list? ;
1058 : lambda-parameters ( obj -- params )
1061 : lambda-body ( obj -- body )
1064 : make-procedure ( params body env -- proc )
1067 drop compound-proc-type
1070 : application? ( obj -- obj bool)
1073 : operator ( obj -- operator )
1076 : operands ( obj -- operands )
1079 : nooperands? ( operands -- bool )
1082 : first-operand ( operands -- operand )
1085 : rest-operands ( operands -- other-operands )
1088 : list-of-vals ( args env -- vals )
1094 2over 2over first-operand 2swap eval
1095 -2rot rest-operands 2swap recurse
1100 : procedure-params ( proc -- params )
1101 drop pair-type car ;
1103 : procedure-body ( proc -- body )
1104 drop pair-type cdr car ;
1106 : procedure-env ( proc -- body )
1107 drop pair-type cdr cdr car ;
1109 : apply ( proc args )
1111 primitive-proc-type of
1115 compound-proc-type of
1116 2dup procedure-body ( args proc body )
1117 -2rot 2dup procedure-params ( body args proc params )
1118 -2rot procedure-env ( body params args procenv )
1120 extend-env ( body env )
1125 2dup cdr 2dup nil objeq? false =
1127 -2rot car 2over ( nextbody env exp env )
1129 2drop \ discard result
1130 2swap ( env nextbody )
1134 car 2swap ( exp env )
1136 R> drop ['] eval goto-deferred \ Tail call optimization
1139 bold fg red ." Object not applicable. Aboring." reset-term cr
1144 :noname ( obj env -- result )
1164 2swap eval-definition
1169 2swap eval-assignment
1185 ['] eval goto-deferred
1189 2dup lambda-parameters
1199 operands 2swap list-of-vals
1205 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1211 \ ---- Print ---- {{{
1213 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1215 : printrealnum ( realnumobj -- ) drop float-print ;
1217 : printbool ( numobj -- )
1225 : printchar ( charobj -- )
1228 9 of ." #\tab" endof
1229 bl of ." #\space" endof
1230 '\n' of ." #\newline" endof
1236 : (printstring) ( stringobj -- )
1237 nil-type istype? if 2drop exit then
1241 '\n' of ." \n" drop endof
1242 [char] \ of ." \\" drop endof
1243 [char] " of [char] \ emit [char] " emit drop endof
1249 : printstring ( stringobj -- )
1254 : printsymbol ( symbolobj -- )
1255 nil-type istype? if 2drop exit then
1261 : printnil ( nilobj -- )
1264 : printpair ( pairobj -- )
1268 nil-type istype? if 2drop exit then
1269 pair-type istype? if space recurse exit then
1273 : printprim ( primobj -- )
1274 2drop ." <primitive procedure>" ;
1276 : printcomp ( primobj -- )
1277 2drop ." <compound procedure>" ;
1280 fixnum-type istype? if printfixnum exit then
1281 realnum-type istype? if printrealnum exit then
1282 boolean-type istype? if printbool exit then
1283 character-type istype? if printchar exit then
1284 string-type istype? if printstring exit then
1285 symbol-type istype? if printsymbol exit then
1286 nil-type istype? if printnil exit then
1287 pair-type istype? if ." (" printpair ." )" exit then
1288 primitive-proc-type istype? if printprim exit then
1289 compound-proc-type istype? if printcomp exit then
1291 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1300 cr ." Welcome to scheme.forth.jl!" cr
1301 ." Use Ctrl-D to exit." cr
1307 cr bold fg green ." > " reset-term
1310 global-env obj@ eval
1312 fg cyan ." ; " print reset-term