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 define-macro define-macro-symbol
274 create-symbol set! set!-symbol
275 create-symbol ok ok-symbol
276 create-symbol if if-symbol
277 create-symbol lambda lambda-symbol
278 create-symbol λ λ-symbol
279 create-symbol begin begin-symbol
283 \ ---- Environments ---- {{{
285 : enclosing-env ( env -- env )
288 : first-frame ( env -- frame )
291 : make-frame ( vars vals -- frame )
294 : frame-vars ( frame -- vars )
297 : frame-vals ( frame -- vals )
300 : add-binding ( var val frame -- )
301 2swap 2over frame-vals cons
303 2swap 2over frame-vars cons
307 : extend-env ( vars vals env -- env )
317 : get-vars-vals-frame ( var frame -- bool )
318 2dup frame-vars vars obj!
322 vars obj@ nil objeq? false =
324 2dup vars obj@ car objeq? if
329 vars obj@ cdr vars obj!
330 vals obj@ cdr vals obj!
336 : get-vars-vals ( var env -- vars? vals? bool )
339 2dup nil objeq? false =
341 2over 2over first-frame
342 get-vars-vals-frame if
344 vars obj@ vals obj@ true
358 : lookup-var ( var env -- val )
362 bold fg red ." Tried to read unbound variable." reset-term cr abort
366 : set-var ( var val env -- )
367 >R >R 2swap R> R> ( val var env )
369 2swap 2drop ( val vals )
372 bold fg red ." Tried to set unbound variable." reset-term cr abort
378 : define-var ( var val env -- )
381 2over env obj@ ( var val var env )
383 2swap 2drop ( var val vals )
388 first-frame ( var val frame )
395 : make-procedure ( params body env -- proc )
398 drop compound-proc-type
402 nil nil nil extend-env
407 \ ---- Macros ---- {{{
411 : lookup-macro ( name_symbol -- proc? bool )
428 : make-macro ( name_symbol params body env -- )
431 2swap ( proc name_symbol )
438 2over 2over ( proc name table name table )
440 2swap 2drop ( proc table )
450 macro-table obj@ cons
459 variable stored-parse-idx
460 create parse-str 161 allot
461 variable parse-str-span
463 create parse-idx-stack 10 allot
464 variable parse-idx-sp
465 parse-idx-stack parse-idx-sp !
468 parse-idx @ parse-idx-sp @ !
473 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
477 parse-idx-sp @ @ parse-idx ! ;
481 '\n' parse-str parse-str-span @ + !
482 1 parse-str-span +! ;
485 4 parse-str parse-str-span @ + !
486 1 parse-str-span +! ;
493 current-input-port obj@ console-i/o-port obj@ objeq? if
494 parse-str 160 expect cr
495 span @ parse-str-span !
497 parse-str 160 current-input-port obj@ fileport>fid read-line
498 drop swap parse-str-span !
500 parse-str-span @ 0= and if append-eof then
511 : charavailable? ( -- bool )
512 parse-str-span @ parse-idx @ > ;
514 : nextchar ( -- char )
515 charavailable? false = if getline then
516 parse-str parse-idx @ + @ ;
519 : whitespace? ( -- bool )
531 nextchar [char] ( = or
532 nextchar [char] ) = or
535 : commentstart? ( -- bool )
536 nextchar [char] ; = ;
540 false \ Indicates whether or not we're eating a comment
543 dup whitespace? or commentstart? or
545 dup nextchar '\n' = and if
546 invert \ Stop eating comment
548 dup false = commentstart? and if
549 invert \ Begin eating comment
564 nextchar [char] - = ;
567 nextchar [char] + = ;
569 : fixnum? ( -- bool )
595 : realnum? ( -- bool )
602 \ Record starting parse idx:
603 \ Want to detect whether any characters (following +/-) were eaten.
610 [char] . nextchar = if
617 [char] e nextchar = [char] E nextchar = or if
625 drop pop-parse-idx false exit
633 \ This is a real number if characters were
634 \ eaten and the next characer is a delimiter.
635 parse-idx @ < delim? and
640 : boolean? ( -- bool )
641 nextchar [char] # <> if false exit then
648 and if pop-parse-idx false exit then
660 : str-equiv? ( str -- bool )
677 delim? false = if drop false then
682 : character? ( -- bool )
683 nextchar [char] # <> if false exit then
688 nextchar [char] \ <> if pop-parse-idx false exit then
692 S" newline" str-equiv? if pop-parse-idx true exit then
693 S" space" str-equiv? if pop-parse-idx true exit then
694 S" tab" str-equiv? if pop-parse-idx true exit then
696 charavailable? false = if pop-parse-idx false exit then
702 nextchar [char] ( = ;
704 : string? ( -- bool )
705 nextchar [char] " = ;
707 : readfixnum ( -- num-atom )
718 10 * nextchar [char] 0 - +
727 : readrealnum ( -- realnum )
729 \ Remember that at this point we're guaranteed to
730 \ have a parsable real on this line.
732 parse-str parse-idx @ +
734 begin delim? false = while
738 parse-str parse-idx @ + over -
745 : readbool ( -- bool-obj )
748 nextchar [char] f = if
759 : readchar ( -- char-obj )
763 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
764 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
765 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
767 nextchar character-type
772 : readstring ( -- charlist )
773 nextchar [char] " = if
778 ." No delimiter following right double quote. Aborting." cr
787 nextchar [char] \ = if
790 [char] n of '\n' endof
791 [char] " of [char] " endof
797 inc-parse-idx character-type
804 : readsymbol ( -- charlist )
805 delim? if nil exit then
807 nextchar inc-parse-idx character-type
814 : readpair ( -- pairobj )
818 nextchar [char] ) = if
823 ." No delimiter following right paren. Aborting." cr
832 \ Read first pair element
837 nextchar [char] . = if
842 ." No delimiter following '.'. Aborting." cr
856 \ Parse a scheme expression
887 nextchar [char] " <> if
888 bold red ." Missing closing double-quote." reset-term cr
906 nextchar [char] ) <> if
907 bold red ." Missing closing paren." reset-term cr
916 nextchar [char] ' = if
918 quote-symbol recurse nil cons cons exit
927 \ Anything else is parsed as a symbol
928 readsymbol charlist>symbol
930 \ Replace λ with lambda
931 2dup λ-symbol objeq? if
942 : self-evaluating? ( obj -- obj bool )
943 boolean-type istype? if true exit then
944 fixnum-type istype? if true exit then
945 realnum-type istype? if true exit then
946 character-type istype? if true exit then
947 string-type istype? if true exit then
948 nil-type istype? if true exit then
949 none-type istype? if true exit then
954 : tagged-list? ( obj tag-obj -- obj bool )
956 pair-type istype? false = if
962 : quote? ( obj -- obj bool )
963 quote-symbol tagged-list? ;
965 : quote-body ( quote-obj -- quote-body-obj )
968 : variable? ( obj -- obj bool )
969 symbol-type istype? ;
971 : definition? ( obj -- obj bool )
972 define-symbol tagged-list? ;
974 : make-lambda ( params body -- lambda-exp )
975 lambda-symbol -2rot cons cons ;
977 : definition-var ( obj -- var )
979 symbol-type istype? false = if car then
982 : definition-val ( obj -- val )
983 2dup cdr car symbol-type istype? if
992 : assignment? ( obj -- obj bool )
993 set!-symbol tagged-list? ;
995 : assignment-var ( obj -- var )
998 : assignment-val ( obj -- val )
1001 : eval-definition ( obj env -- res )
1003 2over 2over ( env obj env obj )
1004 definition-val 2swap ( env obj valexp env )
1005 eval ( env obj val )
1007 2swap definition-var 2swap ( env var val )
1009 2rot ( var val env )
1015 : eval-assignment ( obj env -- res )
1017 2over 2over ( env obj env obj )
1018 assignment-val 2swap ( env obj valexp env )
1019 eval ( env obj val )
1021 2swap assignment-var 2swap ( env var val )
1023 2rot ( var val env )
1029 : macro-definition? ( obj -- obj bool )
1030 define-macro-symbol tagged-list? ;
1032 : if? ( obj -- obj bool )
1033 if-symbol tagged-list? ;
1035 : if-predicate ( ifobj -- pred )
1038 : if-consequent ( ifobj -- conseq )
1041 : if-alternative ( ifobj -- alt|false )
1049 : false? ( boolobj -- boolean )
1050 boolean-type istype? if
1051 false boolean-type objeq?
1057 : true? ( boolobj -- bool )
1060 : lambda? ( obj -- obj bool )
1061 lambda-symbol tagged-list? ;
1063 : lambda-parameters ( obj -- params )
1066 : lambda-body ( obj -- body )
1069 : begin? ( obj -- obj bool )
1070 begin-symbol tagged-list? ;
1072 : begin-actions ( obj -- actions )
1075 : eval-sequence ( explist env -- finalexp env )
1076 ( Evaluates all bar the final expressions in
1077 an an expression list. The final expression
1078 is returned to allow for tail optimization. )
1080 2swap ( env explist )
1082 \ Abort on empty list
1089 2dup cdr ( env explist nextexplist )
1090 2dup nil objeq? false =
1092 -2rot car 2over ( nextexplist env exp env )
1094 2drop \ discard result
1095 2swap ( env nextexplist )
1098 2drop car 2swap ( finalexp env )
1101 : application? ( obj -- obj bool )
1104 : operator ( obj -- operator )
1107 : operands ( obj -- operands )
1110 : nooperands? ( operands -- bool )
1113 : first-operand ( operands -- operand )
1116 : rest-operands ( operands -- other-operands )
1119 : list-of-vals ( args env -- vals )
1125 2over 2over first-operand 2swap eval
1126 -2rot rest-operands 2swap recurse
1131 : procedure-params ( proc -- params )
1132 drop pair-type car ;
1134 : procedure-body ( proc -- body )
1135 drop pair-type cdr car ;
1137 : procedure-env ( proc -- body )
1138 drop pair-type cdr cdr car ;
1140 ( Ensure terminating symbol arg name is handled
1141 specially to allow for variadic procedures. )
1142 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1145 symbol-type istype? if
1154 recurse ( argvals argnames argvals'' argnames'' )
1155 2rot car 2swap cons ( argvals argvals'' argnames' )
1156 2rot car 2rot cons ( argnames' argvals' )
1160 : apply ( proc argvals )
1162 primitive-proc-type of
1166 compound-proc-type of
1167 2dup procedure-body ( argvals proc body )
1168 -2rot 2dup procedure-params ( body argvals proc argnames )
1169 -2rot procedure-env ( body argnames argvals procenv )
1175 extend-env ( body env )
1179 R> drop ['] eval goto-deferred \ Tail call optimization
1182 bold fg red ." Object not applicable. Aboring." reset-term cr
1187 :noname ( obj env -- result )
1207 2swap eval-definition
1212 2swap eval-assignment
1216 macro-definition? if
1217 2swap eval-define-macro
1233 ['] eval goto-deferred
1237 2dup lambda-parameters
1246 ['] eval goto-deferred
1254 find-macro-proc nil objeq? if
1255 \ Regular function application
1259 operands 2swap list-of-vals
1263 \ Macro function evaluation
1265 2swap 2drop 2swap ( env mproc exp )
1267 apply 2swap ( expanded-exp env )
1269 ['] eval goto-deferred
1274 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1280 \ ---- Print ---- {{{
1282 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1284 : printrealnum ( realnumobj -- ) drop float-print ;
1286 : printbool ( numobj -- )
1294 : printchar ( charobj -- )
1297 9 of ." #\tab" endof
1298 bl of ." #\space" endof
1299 '\n' of ." #\newline" endof
1305 : (printstring) ( stringobj -- )
1306 nil-type istype? if 2drop exit then
1310 '\n' of ." \n" drop endof
1311 [char] \ of ." \\" drop endof
1312 [char] " of [char] \ emit [char] " emit drop endof
1318 : printstring ( stringobj -- )
1323 : printsymbol ( symbolobj -- )
1324 nil-type istype? if 2drop exit then
1330 : printnil ( nilobj -- )
1333 : printpair ( pairobj -- )
1337 nil-type istype? if 2drop exit then
1338 pair-type istype? if space recurse exit then
1342 : printprim ( primobj -- )
1343 2drop ." <primitive procedure>" ;
1345 : printcomp ( primobj -- )
1346 2drop ." <compound procedure>" ;
1348 : printnone ( noneobj -- )
1349 2drop ." Unspecified return value" ;
1352 fixnum-type istype? if printfixnum exit then
1353 realnum-type istype? if printrealnum exit then
1354 boolean-type istype? if printbool exit then
1355 character-type istype? if printchar exit then
1356 string-type istype? if printstring exit then
1357 symbol-type istype? if printsymbol exit then
1358 nil-type istype? if printnil exit then
1359 pair-type istype? if ." (" printpair ." )" exit then
1360 primitive-proc-type istype? if printprim exit then
1361 compound-proc-type istype? if printcomp exit then
1362 none-type istype? if printnone exit then
1364 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1370 \ ---- Garbage Collection ---- {{{
1375 variable gc-stack-depth
1378 depth gc-stack-depth !
1382 false gc-enabled ! ;
1387 : pairlike? ( obj -- obj bool )
1388 pair-type istype? if true exit then
1389 string-type istype? if true exit then
1390 symbol-type istype? if true exit then
1391 compound-proc-type istype? if true exit then
1396 : pairlike-marked? ( obj -- obj bool )
1397 over nextfrees + @ 0=
1400 : mark-pairlike ( obj -- obj )
1401 over nextfrees + 0 swap !
1410 : gc-mark-obj ( obj -- )
1412 pairlike? invert if 2drop exit then
1413 pairlike-marked? if 2drop exit then
1424 scheme-memsize nextfree !
1425 0 scheme-memsize 1- do
1426 nextfrees i + @ 0<> if
1427 nextfree @ nextfrees i + !
1433 \ Following a GC, this gives the amount of free memory
1437 nextfrees i + @ 0= if 1+ then
1441 \ Debugging word - helps spot memory that is retained
1444 nextfrees i + @ 0<> if
1456 symbol-table obj@ gc-mark-obj
1457 macro-table obj@ gc-mark-obj
1458 global-env obj@ gc-mark-obj
1460 depth gc-stack-depth @ do
1469 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1470 ; is collect-garbage
1474 \ ---- Primitives ---- {{{
1476 : make-primitive ( cfa -- )
1480 \ 2dup ." Defining primitive " type ." ..." cr
1491 rot primitive-proc-type ( var prim )
1492 global-env obj@ define-var
1496 bold fg red ." Incorrect argument count." reset-term cr
1500 : ensure-arg-count ( args n -- )
1502 drop nil objeq? false = if
1506 -rot 2dup nil objeq? if
1515 bold fg red ." Incorrect argument type." reset-term cr
1519 : ensure-arg-type ( arg type -- arg )
1525 include scheme-primitives.4th
1529 \ ---- Loading files ---- {{{
1531 : charlist>cstr ( charlist addr -- n )
1533 dup 2swap ( origaddr addr charlist )
1536 2dup nil objeq? false =
1539 drop ( origaddr addr charlist char )
1540 -rot 2swap ( origaddr charlist addr char )
1542 1+ -rot ( origaddr nextaddr charlist )
1545 2drop ( origaddr finaladdr )
1549 : load ( addr n -- finalResult )
1554 ok-symbol ( port res )
1557 2over read-port ( port res obj )
1559 2dup EOF character-type objeq? if
1560 2drop 2swap close-port
1564 2swap 2drop ( port obj )
1566 global-env obj@ eval ( port res )
1570 :noname ( args -- finalResult )
1571 2dup 1 ensure-arg-count
1572 car string-type ensure-arg-type
1577 ; make-primitive load
1584 cr ." Welcome to scheme.forth.jl!" cr
1585 ." Use Ctrl-D to exit." cr
1592 cr bold fg green ." > " reset-term
1595 2dup EOF character-type objeq? if
1597 bold fg blue ." Moriturus te saluto." reset-term cr
1601 global-env obj@ eval
1603 fg cyan ." ; " print reset-term