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 )
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 -- charlist )
246 2dup drop @ character-type 2swap
254 : create-symbol ( -- )
262 does> dup @ swap 1+ @
265 create-symbol quote quote-symbol
266 create-symbol quasiquote quasiquote-symbol
267 create-symbol unquote unquote-symbol
268 create-symbol unquote-splicing unquote-splicing-symbol
269 create-symbol define define-symbol
270 create-symbol define-macro define-macro-symbol
271 create-symbol set! set!-symbol
272 create-symbol ok ok-symbol
273 create-symbol if if-symbol
274 create-symbol lambda lambda-symbol
275 create-symbol λ λ-symbol
276 create-symbol begin begin-symbol
280 \ ---- Environments ---- {{{
282 : enclosing-env ( env -- env )
285 : first-frame ( env -- frame )
288 : make-frame ( vars vals -- frame )
291 : frame-vars ( frame -- vars )
294 : frame-vals ( frame -- vals )
297 : add-binding ( var val frame -- )
298 2swap 2over frame-vals cons
300 2swap 2over frame-vars cons
304 : extend-env ( vars vals env -- env )
314 : get-vars-vals-frame ( var frame -- bool )
315 2dup frame-vars vars obj!
319 vars obj@ nil objeq? false =
321 2dup vars obj@ car objeq? if
326 vars obj@ cdr vars obj!
327 vals obj@ cdr vals obj!
333 : get-vars-vals ( var env -- vars? vals? bool )
338 2over 2over first-frame
339 get-vars-vals-frame if
341 vars obj@ vals obj@ true
355 : lookup-var ( var env -- val )
359 bold fg red ." Tried to read unbound variable." reset-term cr abort
363 : set-var ( var val env -- )
364 >R >R 2swap R> R> ( val var env )
366 2swap 2drop ( val vals )
369 bold fg red ." Tried to set unbound variable." reset-term cr abort
375 : define-var ( var val env -- )
378 2over env obj@ ( var val var env )
380 2swap 2drop ( var val vals )
385 first-frame ( var val frame )
392 : make-procedure ( params body env -- proc )
395 drop compound-proc-type
399 nil nil nil extend-env
404 \ ---- Primitives ---- {{{
406 : make-primitive ( cfa -- )
410 \ 2dup ." Defining primitive " type ." ..." cr
415 rot primitive-proc-type ( var prim )
416 global-env obj@ define-var
420 bold fg red ." Incorrect argument count." reset-term cr
424 : ensure-arg-count ( args n -- )
426 drop nil objeq? false = if
439 bold fg red ." Incorrect argument type." reset-term cr
443 : ensure-arg-type ( arg type -- arg )
452 \ ---- Macros ---- {{{
456 ( Look up macro in macro table. Returns nil if
458 : lookup-macro ( name_symbol -- proc )
477 : make-macro ( name_symbol params body env -- )
480 2swap ( proc name_symbol )
487 2over 2over ( proc name table name table )
489 2swap 2drop ( proc table )
501 macro-table obj@ cons
510 variable stored-parse-idx
511 create parse-str 161 allot
512 variable parse-str-span
514 create parse-idx-stack 10 allot
515 variable parse-idx-sp
516 parse-idx-stack parse-idx-sp !
519 parse-idx @ parse-idx-sp @ !
524 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
528 parse-idx-sp @ @ parse-idx ! ;
532 '\n' parse-str parse-str-span @ + !
533 1 parse-str-span +! ;
536 4 parse-str parse-str-span @ + !
537 1 parse-str-span +! ;
544 current-input-port obj@ console-i/o-port obj@ objeq? if
545 parse-str 160 expect cr
546 span @ parse-str-span !
548 parse-str 160 current-input-port obj@ fileport>fid read-line
549 drop swap parse-str-span !
551 parse-str-span @ 0= and if append-eof then
562 : charavailable? ( -- bool )
563 parse-str-span @ parse-idx @ > ;
565 : nextchar ( -- char )
566 charavailable? false = if getline then
567 parse-str parse-idx @ + @ ;
570 : whitespace? ( -- bool )
582 nextchar [char] ( = or
583 nextchar [char] ) = or
586 : commentstart? ( -- bool )
587 nextchar [char] ; = ;
591 false \ Indicates whether or not we're eating a comment
594 dup whitespace? or commentstart? or
596 dup nextchar '\n' = and if
597 invert \ Stop eating comment
599 dup false = commentstart? and if
600 invert \ Begin eating comment
615 nextchar [char] - = ;
618 nextchar [char] + = ;
620 : fixnum? ( -- bool )
646 : realnum? ( -- bool )
653 \ Record starting parse idx:
654 \ Want to detect whether any characters (following +/-) were eaten.
661 [char] . nextchar = if
668 [char] e nextchar = [char] E nextchar = or if
676 drop pop-parse-idx false exit
684 \ This is a real number if characters were
685 \ eaten and the next characer is a delimiter.
686 parse-idx @ < delim? and
691 : boolean? ( -- bool )
692 nextchar [char] # <> if false exit then
699 and if pop-parse-idx false exit then
711 : str-equiv? ( str -- bool )
728 delim? false = if drop false then
733 : character? ( -- bool )
734 nextchar [char] # <> if false exit then
739 nextchar [char] \ <> if pop-parse-idx false exit then
743 S" newline" str-equiv? if pop-parse-idx true exit then
744 S" space" str-equiv? if pop-parse-idx true exit then
745 S" tab" str-equiv? if pop-parse-idx true exit then
747 charavailable? false = if pop-parse-idx false exit then
753 nextchar [char] ( = ;
755 : string? ( -- bool )
756 nextchar [char] " = ;
758 : readfixnum ( -- num-atom )
769 10 * nextchar [char] 0 - +
778 : readrealnum ( -- realnum )
780 \ Remember that at this point we're guaranteed to
781 \ have a parsable real on this line.
783 parse-str parse-idx @ +
785 begin delim? false = while
789 parse-str parse-idx @ + over -
796 : readbool ( -- bool-obj )
799 nextchar [char] f = if
810 : readchar ( -- char-obj )
814 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
815 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
816 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
818 nextchar character-type
823 : readstring ( -- charlist )
824 nextchar [char] " = if
829 ." No delimiter following right double quote. Aborting." cr
838 nextchar [char] \ = if
841 [char] n of '\n' endof
842 [char] " of [char] " endof
848 inc-parse-idx character-type
855 : readsymbol ( -- charlist )
856 delim? if nil exit then
858 nextchar inc-parse-idx character-type
865 : readpair ( -- pairobj )
869 nextchar [char] ) = if
874 ." No delimiter following right paren. Aborting." cr
883 \ Read first pair element
888 nextchar [char] . = if
893 ." No delimiter following '.'. Aborting." cr
907 \ Parse a scheme expression
938 nextchar [char] " <> if
939 bold red ." Missing closing double-quote." reset-term cr
957 nextchar [char] ) <> if
958 bold red ." Missing closing paren." reset-term cr
967 nextchar [char] ' = if
969 quote-symbol recurse nil cons cons exit
972 nextchar [char] ` = if
974 quasiquote-symbol recurse nil cons cons exit
977 nextchar [char] , = if
979 nextchar [char] @ = if
981 unquote-splicing-symbol recurse nil cons cons exit
983 unquote-symbol recurse nil cons cons exit
993 \ Anything else is parsed as a symbol
994 readsymbol charlist>symbol
996 \ Replace λ with lambda
997 2dup λ-symbol objeq? if
1006 \ ---- Eval ---- {{{
1008 : self-evaluating? ( obj -- obj bool )
1009 boolean-type istype? if true exit then
1010 fixnum-type istype? if true exit then
1011 realnum-type istype? if true exit then
1012 character-type istype? if true exit then
1013 string-type istype? if true exit then
1014 nil-type istype? if true exit then
1015 none-type istype? if true exit then
1020 : tagged-list? ( obj tag-obj -- obj bool )
1022 pair-type istype? false = if
1028 : quote? ( obj -- obj bool )
1029 quote-symbol tagged-list? ;
1031 : quote-body ( quote-obj -- quote-body-obj )
1034 : variable? ( obj -- obj bool )
1035 symbol-type istype? ;
1037 : definition? ( obj -- obj bool )
1038 define-symbol tagged-list? ;
1040 : make-lambda ( params body -- lambda-exp )
1041 lambda-symbol -2rot cons cons ;
1043 ( Handles iterative expansion of defines in
1044 terms of nested lambdas. Most Schemes only
1045 handle one iteration of expansion! )
1046 : definition-var-val ( obj -- var val )
1048 cdr 2dup cdr 2swap car ( val var )
1051 symbol-type istype? false =
1053 2dup cdr 2swap car ( val formals var' )
1054 -2rot 2swap ( var' formals val )
1055 make-lambda nil cons ( var' val' )
1062 : eval-definition ( obj env -- res )
1063 2dup 2rot ( env env obj )
1064 definition-var-val ( env env var val )
1065 2rot eval ( env var val )
1067 2rot ( var val env )
1073 : assignment? ( obj -- obj bool )
1074 set!-symbol tagged-list? ;
1076 : assignment-var ( obj -- var )
1079 : assignment-val ( obj -- val )
1082 : eval-assignment ( obj env -- res )
1084 2over 2over ( env obj env obj )
1085 assignment-val 2swap ( env obj valexp env )
1086 eval ( env obj val )
1088 2swap assignment-var 2swap ( env var val )
1090 2rot ( var val env )
1096 : macro-definition? ( obj -- obj bool )
1097 define-macro-symbol tagged-list? ;
1099 : macro-definition-name ( exp -- mname )
1102 : macro-definition-params ( exp -- params )
1105 : macro-definition-body ( exp -- body )
1109 : eval-define-macro ( obj env -- res )
1112 2dup macro-definition-name 2swap ( name obj )
1113 2dup macro-definition-params 2swap ( name params obj )
1114 macro-definition-body ( name params body )
1116 env obj@ ( name params body env )
1124 : if? ( obj -- obj bool )
1125 if-symbol tagged-list? ;
1127 : if-predicate ( ifobj -- pred )
1130 : if-consequent ( ifobj -- conseq )
1133 : if-alternative ( ifobj -- alt|false )
1141 : false? ( boolobj -- boolean )
1142 boolean-type istype? if
1143 false boolean-type objeq?
1149 : true? ( boolobj -- bool )
1152 : lambda? ( obj -- obj bool )
1153 lambda-symbol tagged-list? ;
1155 : lambda-parameters ( obj -- params )
1158 : lambda-body ( obj -- body )
1161 : begin? ( obj -- obj bool )
1162 begin-symbol tagged-list? ;
1164 : begin-actions ( obj -- actions )
1167 : eval-sequence ( explist env -- finalexp env )
1168 ( Evaluates all bar the final expressions in
1169 an an expression list. The final expression
1170 is returned to allow for tail optimization. )
1172 2swap ( env explist )
1174 \ Abort on empty list
1181 2dup cdr ( env explist nextexplist )
1184 -2rot car 2over ( nextexplist env exp env )
1186 2drop \ discard result
1187 2swap ( env nextexplist )
1190 2drop car 2swap ( finalexp env )
1193 : application? ( obj -- obj bool )
1196 : operator ( obj -- operator )
1199 : operands ( obj -- operands )
1202 : nooperands? ( operands -- bool )
1205 : first-operand ( operands -- operand )
1208 : rest-operands ( operands -- other-operands )
1211 : list-of-vals ( args env -- vals )
1217 2over 2over first-operand 2swap eval
1218 -2rot rest-operands 2swap recurse
1223 : procedure-params ( proc -- params )
1224 drop pair-type car ;
1226 : procedure-body ( proc -- body )
1227 drop pair-type cdr car ;
1229 : procedure-env ( proc -- body )
1230 drop pair-type cdr cdr car ;
1232 ( Ensure terminating symbol arg name is handled
1233 specially to allow for variadic procedures. )
1234 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1236 2over nil? false = if
1237 bold fg red ." Too many arguments supplied to compound method. Aborting." reset-term cr
1245 symbol-type istype? if
1255 bold fg red ." Too few arguments supplied to compound method. Aborting." reset-term cr
1263 recurse ( argvals argnames argvals'' argnames'' )
1264 2rot car 2swap cons ( argvals argvals'' argnames' )
1265 2rot car 2rot cons ( argnames' argvals' )
1269 : apply ( proc argvals -- result )
1271 primitive-proc-type of
1275 compound-proc-type of
1276 2dup procedure-body ( argvals proc body )
1277 -2rot 2dup procedure-params ( body argvals proc argnames )
1278 -2rot procedure-env ( body argnames argvals procenv )
1284 extend-env ( body env )
1288 R> drop ['] eval goto-deferred \ Tail call optimization
1291 bold fg red ." Object not applicable. Aborting." reset-term cr
1296 : macro-expand ( proc expbody -- result )
1298 2dup procedure-body ( expbody proc procbody )
1299 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1300 -2rot procedure-env ( procbody argnames expbody procenv )
1306 extend-env eval-sequence eval
1309 :noname ( obj env -- result )
1329 2swap eval-definition
1334 2swap eval-assignment
1338 macro-definition? if
1339 2swap eval-define-macro
1355 ['] eval goto-deferred
1359 2dup lambda-parameters
1368 ['] eval goto-deferred
1373 2over 2over ( env exp env exp )
1374 operator ( env exp env opname )
1376 2dup lookup-macro nil? false = if
1377 \ Macro function evaluation
1379 ( env exp env opname mproc )
1380 2swap 2drop -2rot 2drop cdr ( env mproc body )
1385 ['] eval goto-deferred
1387 \ Regular function application
1389 2drop ( env exp env opname )
1391 2swap eval ( env exp proc )
1393 -2rot ( proc env exp )
1394 operands 2swap ( proc operands env )
1395 list-of-vals ( proc argvals )
1402 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1408 \ ---- Print ---- {{{
1410 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1412 : printrealnum ( realnumobj -- ) drop float-print ;
1414 : printbool ( numobj -- )
1422 : printchar ( charobj -- )
1425 9 of ." #\tab" endof
1426 bl of ." #\space" endof
1427 '\n' of ." #\newline" endof
1433 : (printstring) ( stringobj -- )
1434 nil-type istype? if 2drop exit then
1438 '\n' of ." \n" drop endof
1439 [char] \ of ." \\" drop endof
1440 [char] " of [char] \ emit [char] " emit drop endof
1446 : printstring ( stringobj -- )
1451 : printsymbol ( symbolobj -- )
1452 nil-type istype? if 2drop exit then
1458 : printnil ( nilobj -- )
1461 : printpair ( pairobj -- )
1465 nil-type istype? if 2drop exit then
1466 pair-type istype? if space recurse exit then
1470 : printprim ( primobj -- )
1471 2drop ." <primitive procedure>" ;
1473 : printcomp ( primobj -- )
1474 2drop ." <compound procedure>" ;
1476 : printnone ( noneobj -- )
1477 2drop ." Unspecified return value" ;
1480 fixnum-type istype? if printfixnum exit then
1481 realnum-type istype? if printrealnum exit then
1482 boolean-type istype? if printbool exit then
1483 character-type istype? if printchar exit then
1484 string-type istype? if printstring exit then
1485 symbol-type istype? if printsymbol exit then
1486 nil-type istype? if printnil exit then
1487 pair-type istype? if ." (" printpair ." )" exit then
1488 primitive-proc-type istype? if printprim exit then
1489 compound-proc-type istype? if printcomp exit then
1490 none-type istype? if printnone exit then
1492 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1498 \ ---- Garbage Collection ---- {{{
1503 variable gc-stack-depth
1506 depth gc-stack-depth !
1510 false gc-enabled ! ;
1515 : pairlike? ( obj -- obj bool )
1516 pair-type istype? if true exit then
1517 string-type istype? if true exit then
1518 symbol-type istype? if true exit then
1519 compound-proc-type istype? if true exit then
1524 : pairlike-marked? ( obj -- obj bool )
1525 over nextfrees + @ 0=
1528 : mark-pairlike ( obj -- obj )
1529 over nextfrees + 0 swap !
1538 : gc-mark-obj ( obj -- )
1540 pairlike? invert if 2drop exit then
1541 pairlike-marked? if 2drop exit then
1552 scheme-memsize nextfree !
1553 0 scheme-memsize 1- do
1554 nextfrees i + @ 0<> if
1555 nextfree @ nextfrees i + !
1561 \ Following a GC, this gives the amount of free memory
1565 nextfrees i + @ 0= if 1+ then
1569 \ Debugging word - helps spot memory that is retained
1572 nextfrees i + @ 0<> if
1584 symbol-table obj@ gc-mark-obj
1585 macro-table obj@ gc-mark-obj
1586 global-env obj@ gc-mark-obj
1588 depth gc-stack-depth @ do
1597 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1598 ; is collect-garbage
1602 \ ---- Loading files ---- {{{
1604 : charlist>cstr ( charlist addr -- n )
1606 dup 2swap ( origaddr addr charlist )
1612 drop ( origaddr addr charlist char )
1613 -rot 2swap ( origaddr charlist addr char )
1615 1+ -rot ( origaddr nextaddr charlist )
1618 2drop ( origaddr finaladdr )
1622 : load ( addr n -- finalResult )
1627 ok-symbol ( port res )
1630 2over read-port ( port res obj )
1632 2dup EOF character-type objeq? if
1633 2drop 2swap close-port
1637 2swap 2drop ( port obj )
1639 global-env obj@ eval ( port res )
1645 \ ---- Standard Library ---- {{{
1647 include scheme-primitives.4th
1649 s" scheme-library.scm" load 2drop
1656 cr ." Welcome to scheme.forth.jl!" cr
1657 ." Use Ctrl-D to exit." cr
1664 cr bold fg green ." > " reset-term
1667 2dup EOF character-type objeq? if
1669 bold fg blue ." Moriturus te saluto." reset-term cr
1673 global-env obj@ eval
1675 fg cyan ." ; " print reset-term