4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
34 make-type primitive-proc-type
35 make-type compound-proc-type
36 make-type fileport-type
37 : istype? ( obj type -- obj bool )
40 \ ------ List-structured memory ------ {{{
42 10000 constant scheme-memsize
44 create car-cells scheme-memsize allot
45 create car-type-cells scheme-memsize allot
46 create cdr-cells scheme-memsize allot
47 create cdr-type-cells scheme-memsize allot
49 create nextfrees scheme-memsize allot
60 nextfrees nextfree @ + @
63 nextfree @ scheme-memsize >= if
67 nextfree @ scheme-memsize >= if
69 ." Out of memory! Aborting."
74 : cons ( car-obj cdr-obj -- pair-obj )
75 cdr-type-cells nextfree @ + !
76 cdr-cells nextfree @ + !
77 car-type-cells nextfree @ + !
78 car-cells nextfree @ + !
84 : car ( pair-obj -- car-obj )
86 dup car-cells + @ swap
90 : cdr ( pair-obj -- car-obj )
92 dup cdr-cells + @ swap
96 : set-car! ( obj pair-obj -- )
98 rot swap car-type-cells + !
102 : set-cdr! ( obj pair-obj -- )
104 rot swap cdr-type-cells + !
114 : nil? nil-type istype? ;
117 : none? none-type istype? ;
119 : objvar create nil swap , , ;
121 : value@ ( objvar -- val ) @ ;
122 : type@ ( objvar -- type ) 1+ @ ;
123 : value! ( newval objvar -- ) ! ;
124 : type! ( newtype objvar -- ) 1+ ! ;
125 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
126 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
128 : objeq? ( obj obj -- bool )
131 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
132 >R >R ( a1 a2 b1 b2 )
133 2swap ( b1 b2 a1 a2 )
134 R> R> ( b1 b2 a1 a2 c1 c2 )
138 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
139 2swap ( a1 a2 c1 c2 b1 b2 )
140 >R >R ( a1 a2 c1 c2 )
141 2swap ( c1 c2 a1 a2 )
147 \ ---- Port I/O ---- {{{
149 : fileport>fid ( fileport -- fid )
152 : fid>fileport ( fid -- fileport )
155 : open-input-file ( addr n -- fileport )
156 r/o open-file drop fid>fileport
159 : close-port ( fileport -- )
160 fileport>fid close-file drop
163 objvar console-i/o-port
164 0 fileport-type console-i/o-port obj!
166 objvar current-input-port
167 console-i/o-port obj@ current-input-port obj!
169 : read-port ( fileport -- obj )
170 current-input-port obj!
173 : read-console ( -- obj )
174 console-i/o-port obj@ read-port ;
178 \ ---- Pre-defined symbols ---- {{{
182 : duplicate-charlist ( charlist -- copy )
183 2dup nil objeq? false = if
184 2dup car 2swap cdr recurse cons
187 : charlist-equiv ( charlist charlist -- bool )
196 2drop 2drop true exit
198 2drop 2drop false exit
203 2drop 2drop false exit
210 car drop -rot car drop = if
211 cdr 2swap cdr recurse
217 : charlist>symbol ( charlist -- symbol-obj )
236 drop symbol-type 2dup
237 symbol-table obj@ cons
242 : cstr>charlist ( addr n -- symbol-obj )
246 2dup drop @ character-type 2swap
254 : create-symbol ( -- )
268 does> dup @ swap 1+ @
271 create-symbol quote quote-symbol
272 create-symbol define define-symbol
273 create-symbol set! set!-symbol
274 create-symbol ok ok-symbol
275 create-symbol if if-symbol
276 create-symbol lambda lambda-symbol
277 create-symbol λ λ-symbol
278 create-symbol begin begin-symbol
282 \ ---- Environments ---- {{{
284 : enclosing-env ( env -- env )
287 : first-frame ( env -- frame )
290 : make-frame ( vars vals -- frame )
293 : frame-vars ( frame -- vars )
296 : frame-vals ( frame -- vals )
299 : add-binding ( var val frame -- )
300 2swap 2over frame-vals cons
302 2swap 2over frame-vars cons
306 : extend-env ( vars vals env -- env )
316 : get-vars-vals-frame ( var frame -- bool )
317 2dup frame-vars vars obj!
321 vars obj@ nil objeq? false =
323 2dup vars obj@ car objeq? if
328 vars obj@ cdr vars obj!
329 vals obj@ cdr vals obj!
335 : get-vars-vals ( var env -- vars? vals? bool )
338 2dup nil objeq? false =
340 2over 2over first-frame
341 get-vars-vals-frame if
343 vars obj@ vals obj@ true
357 : lookup-var ( var env -- val )
361 bold fg red ." Tried to read unbound variable." reset-term cr abort
365 : set-var ( var val env -- )
366 >R >R 2swap R> R> ( val var env )
368 2swap 2drop ( val vals )
371 bold fg red ." Tried to set unbound variable." reset-term cr abort
377 : define-var ( var val env -- )
380 2over env obj@ ( var val var env )
382 2swap 2drop ( var val vals )
387 first-frame ( var val frame )
395 nil nil nil extend-env
403 variable stored-parse-idx
404 create parse-str 161 allot
405 variable parse-str-span
407 create parse-idx-stack 10 allot
408 variable parse-idx-sp
409 parse-idx-stack parse-idx-sp !
412 parse-idx @ parse-idx-sp @ !
417 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
421 parse-idx-sp @ @ parse-idx ! ;
425 '\n' parse-str parse-str-span @ + !
426 1 parse-str-span +! ;
429 4 parse-str parse-str-span @ + !
430 1 parse-str-span +! ;
437 current-input-port obj@ console-i/o-port obj@ objeq? if
438 parse-str 160 expect cr
439 span @ parse-str-span !
441 parse-str 160 current-input-port obj@ fileport>fid read-line
442 drop swap parse-str-span !
444 parse-str-span @ 0= and if append-eof then
455 : charavailable? ( -- bool )
456 parse-str-span @ parse-idx @ > ;
458 : nextchar ( -- char )
459 charavailable? false = if getline then
460 parse-str parse-idx @ + @ ;
463 : whitespace? ( -- bool )
475 nextchar [char] ( = or
476 nextchar [char] ) = or
479 : commentstart? ( -- bool )
480 nextchar [char] ; = ;
484 false \ Indicates whether or not we're eating a comment
487 dup whitespace? or commentstart? or
489 dup nextchar '\n' = and if
490 invert \ Stop eating comment
492 dup false = commentstart? and if
493 invert \ Begin eating comment
508 nextchar [char] - = ;
511 nextchar [char] + = ;
513 : fixnum? ( -- bool )
539 : realnum? ( -- bool )
546 \ Record starting parse idx:
547 \ Want to detect whether any characters (following +/-) were eaten.
554 [char] . nextchar = if
561 [char] e nextchar = [char] E nextchar = or if
569 drop pop-parse-idx false exit
577 \ This is a real number if characters were
578 \ eaten and the next characer is a delimiter.
579 parse-idx @ < delim? and
584 : boolean? ( -- bool )
585 nextchar [char] # <> if false exit then
592 and if pop-parse-idx false exit then
604 : str-equiv? ( str -- bool )
621 delim? false = if drop false then
626 : character? ( -- bool )
627 nextchar [char] # <> if false exit then
632 nextchar [char] \ <> if pop-parse-idx false exit then
636 S" newline" str-equiv? if pop-parse-idx true exit then
637 S" space" str-equiv? if pop-parse-idx true exit then
638 S" tab" str-equiv? if pop-parse-idx true exit then
640 charavailable? false = if pop-parse-idx false exit then
646 nextchar [char] ( = ;
648 : string? ( -- bool )
649 nextchar [char] " = ;
651 : readfixnum ( -- num-atom )
662 10 * nextchar [char] 0 - +
671 : readrealnum ( -- realnum )
673 \ Remember that at this point we're guaranteed to
674 \ have a parsable real on this line.
676 parse-str parse-idx @ +
678 begin delim? false = while
682 parse-str parse-idx @ + over -
689 : readbool ( -- bool-obj )
692 nextchar [char] f = if
703 : readchar ( -- char-obj )
707 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
708 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
709 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
711 nextchar character-type
716 : readstring ( -- charlist )
717 nextchar [char] " = if
722 ." No delimiter following right double quote. Aborting." cr
731 nextchar [char] \ = if
734 [char] n of '\n' endof
735 [char] " of [char] " endof
741 inc-parse-idx character-type
748 : readsymbol ( -- charlist )
749 delim? if nil exit then
751 nextchar inc-parse-idx character-type
758 : readpair ( -- pairobj )
762 nextchar [char] ) = if
767 ." No delimiter following right paren. Aborting." cr
776 \ Read first pair element
781 nextchar [char] . = if
786 ." No delimiter following '.'. Aborting." cr
800 \ Parse a scheme expression
831 nextchar [char] " <> if
832 bold red ." Missing closing double-quote." reset-term cr
850 nextchar [char] ) <> if
851 bold red ." Missing closing paren." reset-term cr
860 nextchar [char] ' = if
862 quote-symbol recurse nil cons cons exit
871 \ Anything else is parsed as a symbol
872 readsymbol charlist>symbol
874 \ Replace λ with lambda
875 2dup λ-symbol objeq? if
886 : self-evaluating? ( obj -- obj bool )
887 boolean-type istype? if true exit then
888 fixnum-type istype? if true exit then
889 realnum-type istype? if true exit then
890 character-type istype? if true exit then
891 string-type istype? if true exit then
892 nil-type istype? if true exit then
893 none-type istype? if true exit then
898 : tagged-list? ( obj tag-obj -- obj bool )
900 pair-type istype? false = if
906 : quote? ( obj -- obj bool )
907 quote-symbol tagged-list? ;
909 : quote-body ( quote-obj -- quote-body-obj )
912 : variable? ( obj -- obj bool )
913 symbol-type istype? ;
915 : definition? ( obj -- obj bool )
916 define-symbol tagged-list? ;
918 : make-lambda ( params body -- lambda-exp )
919 lambda-symbol -2rot cons cons ;
921 : definition-var ( obj -- var )
923 symbol-type istype? false = if car then
926 : definition-val ( obj -- val )
927 2dup cdr car symbol-type istype? if
936 : assignment? ( obj -- obj bool )
937 set!-symbol tagged-list? ;
939 : assignment-var ( obj -- var )
942 : assignment-val ( obj -- val )
945 : eval-definition ( obj env -- res )
947 2over 2over ( env obj env obj )
948 definition-val 2swap ( env obj valexp env )
951 2swap definition-var 2swap ( env var val )
959 : eval-assignment ( obj env -- res )
961 2over 2over ( env obj env obj )
962 assignment-val 2swap ( env obj valexp env )
965 2swap assignment-var 2swap ( env var val )
973 : if? ( obj -- obj bool )
974 if-symbol tagged-list? ;
976 : if-predicate ( ifobj -- pred )
979 : if-consequent ( ifobj -- conseq )
982 : if-alternative ( ifobj -- alt|false )
990 : false? ( boolobj -- boolean )
991 boolean-type istype? if
992 false boolean-type objeq?
998 : true? ( boolobj -- bool )
1001 : lambda? ( obj -- obj bool )
1002 lambda-symbol tagged-list? ;
1004 : lambda-parameters ( obj -- params )
1007 : lambda-body ( obj -- body )
1010 : make-procedure ( params body env -- proc )
1013 drop compound-proc-type
1016 : begin? ( obj -- obj bool )
1017 begin-symbol tagged-list? ;
1019 : begin-actions ( obj -- actions )
1022 : eval-sequence ( explist env -- finalexp env )
1023 ( Evaluates all bar the final expressions in
1024 an an expression list. The final expression
1025 is returned to allow for tail optimization. )
1027 2swap ( env explist )
1029 \ Abort on empty list
1036 2dup cdr ( env explist nextexplist )
1037 2dup nil objeq? false =
1039 -2rot car 2over ( nextexplist env exp env )
1041 2drop \ discard result
1042 2swap ( env nextexplist )
1045 2drop car 2swap ( finalexp env )
1048 : application? ( obj -- obj bool)
1051 : operator ( obj -- operator )
1054 : operands ( obj -- operands )
1057 : nooperands? ( operands -- bool )
1060 : first-operand ( operands -- operand )
1063 : rest-operands ( operands -- other-operands )
1066 : list-of-vals ( args env -- vals )
1072 2over 2over first-operand 2swap eval
1073 -2rot rest-operands 2swap recurse
1078 : procedure-params ( proc -- params )
1079 drop pair-type car ;
1081 : procedure-body ( proc -- body )
1082 drop pair-type cdr car ;
1084 : procedure-env ( proc -- body )
1085 drop pair-type cdr cdr car ;
1087 : apply ( proc args )
1089 primitive-proc-type of
1093 compound-proc-type of
1094 2dup procedure-body ( args proc body )
1095 -2rot 2dup procedure-params ( body args proc params )
1096 -2rot procedure-env ( body params args procenv )
1098 extend-env ( body env )
1102 R> drop ['] eval goto-deferred \ Tail call optimization
1105 bold fg red ." Object not applicable. Aboring." reset-term cr
1110 :noname ( obj env -- result )
1130 2swap eval-definition
1135 2swap eval-assignment
1151 ['] eval goto-deferred
1155 2dup lambda-parameters
1164 ['] eval goto-deferred
1171 operands 2swap list-of-vals
1177 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1183 \ ---- Print ---- {{{
1185 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1187 : printrealnum ( realnumobj -- ) drop float-print ;
1189 : printbool ( numobj -- )
1197 : printchar ( charobj -- )
1200 9 of ." #\tab" endof
1201 bl of ." #\space" endof
1202 '\n' of ." #\newline" endof
1208 : (printstring) ( stringobj -- )
1209 nil-type istype? if 2drop exit then
1213 '\n' of ." \n" drop endof
1214 [char] \ of ." \\" drop endof
1215 [char] " of [char] \ emit [char] " emit drop endof
1221 : printstring ( stringobj -- )
1226 : printsymbol ( symbolobj -- )
1227 nil-type istype? if 2drop exit then
1233 : printnil ( nilobj -- )
1236 : printpair ( pairobj -- )
1240 nil-type istype? if 2drop exit then
1241 pair-type istype? if space recurse exit then
1245 : printprim ( primobj -- )
1246 2drop ." <primitive procedure>" ;
1248 : printcomp ( primobj -- )
1249 2drop ." <compound procedure>" ;
1251 : printnone ( noneobj -- )
1252 2drop ." Unspecified return value" ;
1255 fixnum-type istype? if printfixnum exit then
1256 realnum-type istype? if printrealnum exit then
1257 boolean-type istype? if printbool exit then
1258 character-type istype? if printchar exit then
1259 string-type istype? if printstring exit then
1260 symbol-type istype? if printsymbol exit then
1261 nil-type istype? if printnil exit then
1262 pair-type istype? if ." (" printpair ." )" exit then
1263 primitive-proc-type istype? if printprim exit then
1264 compound-proc-type istype? if printcomp exit then
1265 none-type istype? if printnone exit then
1267 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1273 \ ---- Garbage Collection ---- {{{
1278 variable gc-stack-depth
1281 depth gc-stack-depth !
1285 false gc-enabled ! ;
1290 : pairlike? ( obj -- obj bool )
1291 pair-type istype? if true exit then
1292 string-type istype? if true exit then
1293 symbol-type istype? if true exit then
1294 compound-proc-type istype? if true exit then
1299 : pairlike-marked? ( obj -- obj bool )
1300 over nextfrees + @ 0=
1303 : mark-pairlike ( obj -- obj )
1304 over nextfrees + 0 swap !
1313 : gc-mark-obj ( obj -- )
1315 pairlike? invert if 2drop exit then
1316 pairlike-marked? if 2drop exit then
1327 scheme-memsize nextfree !
1328 0 scheme-memsize 1- do
1329 nextfrees i + @ 0<> if
1330 nextfree @ nextfrees i + !
1336 \ Following a GC, this gives the amount of free memory
1340 nextfrees i + @ 0= if 1+ then
1344 \ Debugging word - helps spot memory that is retained
1347 nextfrees i + @ 0<> if
1359 symbol-table obj@ gc-mark-obj
1360 global-env obj@ gc-mark-obj
1362 depth gc-stack-depth @ do
1371 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1372 ; is collect-garbage
1376 \ ---- Primitives ---- {{{
1378 : make-primitive ( cfa -- )
1382 \ 2dup ." Defining primitive " type ." ..." cr
1393 rot primitive-proc-type ( var prim )
1394 global-env obj@ define-var
1398 bold fg red ." Incorrect argument count." reset-term cr
1402 : ensure-arg-count ( args n -- )
1404 drop nil objeq? false = if
1408 -rot 2dup nil objeq? if
1417 bold fg red ." Incorrect argument type." reset-term cr
1421 : ensure-arg-type ( arg type -- arg )
1427 include scheme-primitives.4th
1431 \ ---- Loading files ---- {{{
1433 : charlist>cstr ( charlist addr -- n )
1435 dup 2swap ( origaddr addr charlist )
1438 2dup nil objeq? false =
1441 drop ( origaddr addr charlist char )
1442 -rot 2swap ( origaddr charlist addr char )
1444 1+ -rot ( origaddr nextaddr charlist )
1447 2drop ( origaddr finaladdr )
1451 : load ( addr n -- finalResult )
1456 ok-symbol ( port res )
1459 2over read-port ( port res obj )
1461 2dup EOF character-type objeq? if
1462 2drop 2swap close-port
1466 2swap 2drop ( port obj )
1468 global-env obj@ eval ( port res )
1472 :noname ( args -- finalResult )
1473 2dup 1 ensure-arg-count
1474 car string-type ensure-arg-type
1479 ; make-primitive load
1486 cr ." Welcome to scheme.forth.jl!" cr
1487 ." Use Ctrl-D to exit." cr
1494 cr bold fg green ." > " reset-term
1497 2dup EOF character-type objeq? if
1499 bold fg blue ." Moriturus te saluto." reset-term cr
1503 global-env obj@ eval
1505 fg cyan ." ; " print reset-term