4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
33 make-type primitive-proc-type
34 make-type compound-proc-type
35 : istype? ( obj type -- obj bool )
38 \ ------ List-structured memory ------ {{{
40 10000 constant scheme-memsize
42 create car-cells scheme-memsize allot
43 create car-type-cells scheme-memsize allot
44 create cdr-cells scheme-memsize allot
45 create cdr-type-cells scheme-memsize allot
47 create nextfrees scheme-memsize allot
58 nextfrees nextfree @ + @
61 nextfree @ scheme-memsize >= if
65 nextfree @ scheme-memsize >= if
67 ." Out of memory! Aborting."
72 : cons ( car-obj cdr-obj -- pair-obj )
73 cdr-type-cells nextfree @ + !
74 cdr-cells nextfree @ + !
75 car-type-cells nextfree @ + !
76 car-cells nextfree @ + !
82 : car ( pair-obj -- car-obj )
84 dup car-cells + @ swap
88 : cdr ( pair-obj -- car-obj )
90 dup cdr-cells + @ swap
94 : set-car! ( obj pair-obj -- )
96 rot swap car-type-cells + !
100 : set-cdr! ( obj pair-obj -- )
102 rot swap cdr-type-cells + !
112 : nil? nil-type istype? ;
114 : objvar create nil swap , , ;
116 : value@ ( objvar -- val ) @ ;
117 : type@ ( objvar -- type ) 1+ @ ;
118 : value! ( newval objvar -- ) ! ;
119 : type! ( newtype objvar -- ) 1+ ! ;
120 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
121 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
123 : objeq? ( obj obj -- bool )
126 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
127 >R >R ( a1 a2 b1 b2 )
128 2swap ( b1 b2 a1 a2 )
129 R> R> ( b1 b2 a1 a2 c1 c2 )
133 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
134 2swap ( a1 a2 c1 c2 b1 b2 )
135 >R >R ( a1 a2 c1 c2 )
136 2swap ( c1 c2 a1 a2 )
143 \ ---- Pre-defined symbols ---- {{{
147 : duplicate-charlist ( charlist -- copy )
148 2dup nil objeq? false = if
149 2dup car 2swap cdr recurse cons
152 : charlist-equiv ( charlist charlist -- bool )
161 2drop 2drop true exit
163 2drop 2drop false exit
168 2drop 2drop false exit
175 car drop -rot car drop = if
176 cdr 2swap cdr recurse
182 : charlist>symbol ( charlist -- symbol-obj )
201 drop symbol-type 2dup
202 symbol-table obj@ cons
207 : (create-symbol) ( addr n -- symbol-obj )
211 2dup drop @ character-type 2swap
219 : create-symbol ( -- )
233 does> dup @ swap 1+ @
236 create-symbol quote quote-symbol
237 create-symbol define define-symbol
238 create-symbol set! set!-symbol
239 create-symbol ok ok-symbol
240 create-symbol if if-symbol
241 create-symbol lambda lambda-symbol
242 create-symbol λ λ-symbol
246 \ ---- Environments ---- {{{
248 : enclosing-env ( env -- env )
251 : first-frame ( env -- frame )
254 : make-frame ( vars vals -- frame )
257 : frame-vars ( frame -- vars )
260 : frame-vals ( frame -- vals )
263 : add-binding ( var val frame -- )
264 2swap 2over frame-vals cons
266 2swap 2over frame-vars cons
270 : extend-env ( vars vals env -- env )
280 : get-vars-vals-frame ( var frame -- bool )
281 2dup frame-vars vars obj!
285 vars obj@ nil objeq? false =
287 2dup vars obj@ car objeq? if
292 vars obj@ cdr vars obj!
293 vals obj@ cdr vals obj!
299 : get-vars-vals ( var env -- vars? vals? bool )
302 2dup nil objeq? false =
304 2over 2over first-frame
305 get-vars-vals-frame if
307 vars obj@ vals obj@ true
321 : lookup-var ( var env -- val )
325 bold fg red ." Tried to read unbound variable." reset-term cr abort
329 : set-var ( var val env -- )
330 >R >R 2swap R> R> ( val var env )
332 2swap 2drop ( val vals )
335 bold fg red ." Tried to set unbound variable." reset-term cr abort
341 : define-var ( var val env -- )
344 2over env obj@ ( var val var env )
346 2swap 2drop ( var val vals )
351 first-frame ( var val frame )
359 nil nil nil extend-env
364 \ ---- Primitives ---- {{{
366 : make-primitive ( cfa -- )
370 \ 2dup ." Defining primitive " type ." ..." cr
381 rot primitive-proc-type ( var prim )
382 global-env obj@ define-var
386 bold fg red ." Incorrect argument count." reset-term cr
390 : ensure-arg-count ( args n -- )
392 drop nil objeq? false = if
396 -rot 2dup nil objeq? if
405 bold fg red ." Incorrect argument type." reset-term cr
409 : ensure-arg-type ( arg type -- arg )
415 include scheme-primitives.4th
422 variable stored-parse-idx
423 create parse-str 161 allot
424 variable parse-str-span
426 create parse-idx-stack 10 allot
427 variable parse-idx-sp
428 parse-idx-stack parse-idx-sp !
431 parse-idx @ parse-idx-sp @ !
436 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
440 parse-idx-sp @ @ parse-idx ! ;
444 '\n' parse-str parse-str-span @ + !
445 1 parse-str-span +! ;
452 parse-str 160 expect cr
453 span @ parse-str-span !
463 : charavailable? ( -- bool )
464 parse-str-span @ parse-idx @ > ;
466 : nextchar ( -- char )
467 charavailable? false = if getline then
468 parse-str parse-idx @ + @ ;
470 : whitespace? ( -- bool )
479 nextchar [char] ( = or
480 nextchar [char] ) = or
483 : commentstart? ( -- bool )
484 nextchar [char] ; = ;
488 false \ Indicates whether or not we're eating a comment
491 dup whitespace? or commentstart? or
493 dup nextchar '\n' = and if
494 invert \ Stop eating comment
496 dup false = commentstart? and if
497 invert \ Begin eating comment
512 nextchar [char] - = ;
515 nextchar [char] + = ;
517 : fixnum? ( -- bool )
543 : realnum? ( -- bool )
550 \ Record starting parse idx:
551 \ Want to detect whether any characters (following +/-) were eaten.
558 [char] . nextchar = if
565 [char] e nextchar = [char] E nextchar = or if
573 drop pop-parse-idx false exit
581 \ This is a real number if characters were
582 \ eaten and the next characer is a delimiter.
583 parse-idx @ < delim? and
588 : boolean? ( -- bool )
589 nextchar [char] # <> if false exit then
596 and if pop-parse-idx false exit then
608 : str-equiv? ( str -- bool )
625 delim? false = if drop false then
630 : character? ( -- bool )
631 nextchar [char] # <> if false exit then
636 nextchar [char] \ <> if pop-parse-idx false exit then
640 S" newline" str-equiv? if pop-parse-idx true exit then
641 S" space" str-equiv? if pop-parse-idx true exit then
642 S" tab" str-equiv? if pop-parse-idx true exit then
644 charavailable? false = if pop-parse-idx false exit then
650 nextchar [char] ( = ;
652 : string? ( -- bool )
653 nextchar [char] " = ;
655 : readfixnum ( -- num-atom )
666 10 * nextchar [char] 0 - +
675 : readrealnum ( -- realnum )
677 \ Remember that at this point we're guaranteed to
678 \ have a parsable real on this line.
680 parse-str parse-idx @ +
682 begin delim? false = while
686 parse-str parse-idx @ + over -
693 : readbool ( -- bool-obj )
696 nextchar [char] f = if
707 : readchar ( -- char-obj )
711 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
712 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
713 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
715 nextchar character-type
720 : readstring ( -- charlist )
721 nextchar [char] " = if
726 ." No delimiter following right double quote. Aborting." cr
735 nextchar [char] \ = if
738 [char] n of '\n' endof
739 [char] " of [char] " endof
745 inc-parse-idx character-type
752 : readsymbol ( -- charlist )
753 delim? if nil exit then
755 nextchar inc-parse-idx character-type
762 : readpair ( -- pairobj )
766 nextchar [char] ) = if
771 ." No delimiter following right paren. Aborting." cr
780 \ Read first pair element
785 nextchar [char] . = if
790 ." No delimiter following '.'. Aborting." cr
804 \ Parse a scheme expression
835 nextchar [char] " <> if
836 bold red ." Missing closing double-quote." reset-term cr
854 nextchar [char] ) <> if
855 bold red ." Missing closing paren." reset-term cr
864 nextchar [char] ' = if
866 quote-symbol recurse nil cons cons exit
871 bold fg blue ." Moriturus te saluto." reset-term cr
875 \ Anything else is parsed as a symbol
876 readsymbol charlist>symbol
878 \ Replace λ with lambda
879 2dup λ-symbol objeq? if
890 : self-evaluating? ( obj -- obj bool )
891 boolean-type istype? if true exit then
892 fixnum-type istype? if true exit then
893 realnum-type istype? if true exit then
894 character-type istype? if true exit then
895 string-type istype? if true exit then
896 nil-type istype? if true exit then
901 : tagged-list? ( obj tag-obj -- obj bool )
903 pair-type istype? false = if
909 : quote? ( obj -- obj bool )
910 quote-symbol tagged-list? ;
912 : quote-body ( quote-obj -- quote-body-obj )
915 : variable? ( obj -- obj bool )
916 symbol-type istype? ;
918 : definition? ( obj -- obj bool )
919 define-symbol tagged-list? ;
921 : make-lambda ( params body -- lambda-exp )
922 lambda-symbol -2rot cons cons ;
924 : definition-var ( obj -- var )
926 symbol-type istype? false = if car then
929 : definition-val ( obj -- val )
930 2dup cdr car symbol-type istype? if
939 : assignment? ( obj -- obj bool )
940 set!-symbol tagged-list? ;
942 : assignment-var ( obj -- var )
945 : assignment-val ( obj -- val )
948 : eval-definition ( obj env -- res )
950 2over 2over ( env obj env obj )
951 definition-val 2swap ( env obj valexp env )
954 2swap definition-var 2swap ( env var val )
962 : eval-assignment ( obj env -- res )
964 2over 2over ( env obj env obj )
965 assignment-val 2swap ( env obj valexp env )
968 2swap assignment-var 2swap ( env var val )
976 : if? ( obj -- obj bool )
977 if-symbol tagged-list? ;
979 : if-predicate ( ifobj -- pred )
982 : if-consequent ( ifobj -- conseq )
985 : if-alternative ( ifobj -- alt|false )
993 : false? ( boolobj -- boolean )
994 boolean-type istype? if
995 false boolean-type objeq?
1001 : true? ( boolobj -- bool )
1004 : lambda? ( obj -- obj bool )
1005 lambda-symbol tagged-list? ;
1007 : lambda-parameters ( obj -- params )
1010 : lambda-body ( obj -- body )
1013 : make-procedure ( params body env -- proc )
1016 drop compound-proc-type
1019 : application? ( obj -- obj bool)
1022 : operator ( obj -- operator )
1025 : operands ( obj -- operands )
1028 : nooperands? ( operands -- bool )
1031 : first-operand ( operands -- operand )
1034 : rest-operands ( operands -- other-operands )
1037 : list-of-vals ( args env -- vals )
1043 2over 2over first-operand 2swap eval
1044 -2rot rest-operands 2swap recurse
1049 : procedure-params ( proc -- params )
1050 drop pair-type car ;
1052 : procedure-body ( proc -- body )
1053 drop pair-type cdr car ;
1055 : procedure-env ( proc -- body )
1056 drop pair-type cdr cdr car ;
1058 : apply ( proc args )
1060 primitive-proc-type of
1064 compound-proc-type of
1065 2dup procedure-body ( args proc body )
1066 -2rot 2dup procedure-params ( body args proc params )
1067 -2rot procedure-env ( body params args procenv )
1069 extend-env ( body env )
1074 2dup cdr 2dup nil objeq? false =
1076 -2rot car 2over ( nextbody env exp env )
1078 2drop \ discard result
1079 2swap ( env nextbody )
1083 car 2swap ( exp env )
1085 R> drop ['] eval goto-deferred \ Tail call optimization
1088 bold fg red ." Object not applicable. Aboring." reset-term cr
1093 :noname ( obj env -- result )
1113 2swap eval-definition
1118 2swap eval-assignment
1134 ['] eval goto-deferred
1138 2dup lambda-parameters
1148 operands 2swap list-of-vals
1154 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1160 \ ---- Print ---- {{{
1162 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1164 : printrealnum ( realnumobj -- ) drop float-print ;
1166 : printbool ( numobj -- )
1174 : printchar ( charobj -- )
1177 9 of ." #\tab" endof
1178 bl of ." #\space" endof
1179 '\n' of ." #\newline" endof
1185 : (printstring) ( stringobj -- )
1186 nil-type istype? if 2drop exit then
1190 '\n' of ." \n" drop endof
1191 [char] \ of ." \\" drop endof
1192 [char] " of [char] \ emit [char] " emit drop endof
1198 : printstring ( stringobj -- )
1203 : printsymbol ( symbolobj -- )
1204 nil-type istype? if 2drop exit then
1210 : printnil ( nilobj -- )
1213 : printpair ( pairobj -- )
1217 nil-type istype? if 2drop exit then
1218 pair-type istype? if space recurse exit then
1222 : printprim ( primobj -- )
1223 2drop ." <primitive procedure>" ;
1225 : printcomp ( primobj -- )
1226 2drop ." <compound procedure>" ;
1229 fixnum-type istype? if printfixnum exit then
1230 realnum-type istype? if printrealnum exit then
1231 boolean-type istype? if printbool exit then
1232 character-type istype? if printchar exit then
1233 string-type istype? if printstring exit then
1234 symbol-type istype? if printsymbol exit then
1235 nil-type istype? if printnil exit then
1236 pair-type istype? if ." (" printpair ." )" exit then
1237 primitive-proc-type istype? if printprim exit then
1238 compound-proc-type istype? if printcomp exit then
1240 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1246 \ ---- Garbage Collection ---- {{{
1251 variable gc-stack-depth
1254 depth gc-stack-depth !
1258 false gc-enabled ! ;
1263 : pairlike? ( obj -- obj bool )
1264 pair-type istype? if true exit then
1265 string-type istype? if true exit then
1266 symbol-type istype? if true exit then
1267 compound-proc-type istype? if true exit then
1272 : pairlike-marked? ( obj -- obj bool )
1273 over nextfrees + @ 0=
1276 : mark-pairlike ( obj -- obj )
1277 over nextfrees + 0 swap !
1286 : gc-mark-obj ( obj -- )
1288 pairlike? invert if 2drop exit then
1289 pairlike-marked? if 2drop exit then
1300 scheme-memsize nextfree !
1301 0 scheme-memsize 1- do
1302 nextfrees i + @ 0<> if
1303 nextfree @ nextfrees i + !
1309 \ Following a GC, this gives the amount of free memory
1313 nextfrees i + @ 0= if 1+ then
1317 \ Debugging word - helps spot memory that is retained
1320 nextfrees i + @ 0<> if
1332 symbol-table obj@ gc-mark-obj
1333 global-env obj@ gc-mark-obj
1335 depth gc-stack-depth @ do
1344 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1345 ; is collect-garbage
1352 cr ." Welcome to scheme.forth.jl!" cr
1353 ." Use Ctrl-D to exit." cr
1361 cr bold fg green ." > " reset-term
1364 global-env obj@ eval
1366 fg cyan ." ; " print reset-term