4 include term-colours.4th
6 include catch-throw.4th
27 make-type realnum-type
28 make-type boolean-type
29 make-type character-type
35 make-type primitive-proc-type
36 make-type compound-proc-type
37 make-type fileport-type
38 : istype? ( obj type -- obj bool )
43 \ ---- Exceptions ---- {{{
45 variable nextexception
48 create nextexception @ ,
52 make-exception recoverable-exception
53 make-exception unrecoverable-exception
55 : display-exception-msg ( addr count -- )
68 ['] display-exception-msg ,
76 \ ---- List-structured memory ---- {{{
78 10000 constant scheme-memsize
80 create car-cells scheme-memsize allot
81 create car-type-cells scheme-memsize allot
82 create cdr-cells scheme-memsize allot
83 create cdr-type-cells scheme-memsize allot
85 create nextfrees scheme-memsize allot
96 nextfrees nextfree @ + @
99 nextfree @ scheme-memsize >= if
103 nextfree @ scheme-memsize >= if
104 unrecoverable-exception throw s" Out of memory!"
108 : cons ( car-obj cdr-obj -- pair-obj )
109 cdr-type-cells nextfree @ + !
110 cdr-cells nextfree @ + !
111 car-type-cells nextfree @ + !
112 car-cells nextfree @ + !
118 : car ( pair-obj -- car-obj )
120 dup car-cells + @ swap
124 : cdr ( pair-obj -- car-obj )
126 dup cdr-cells + @ swap
130 : set-car! ( obj pair-obj -- )
132 rot swap car-type-cells + !
136 : set-cdr! ( obj pair-obj -- )
138 rot swap cdr-type-cells + !
148 : nil? nil-type istype? ;
151 : none? none-type istype? ;
153 : objvar create nil swap , , ;
155 : value@ ( objvar -- val ) @ ;
156 : type@ ( objvar -- type ) 1+ @ ;
157 : value! ( newval objvar -- ) ! ;
158 : type! ( newtype objvar -- ) 1+ ! ;
159 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
160 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
162 : objeq? ( obj obj -- bool )
165 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
166 >R >R ( a1 a2 b1 b2 )
167 2swap ( b1 b2 a1 a2 )
168 R> R> ( b1 b2 a1 a2 c1 c2 )
172 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
173 2swap ( a1 a2 c1 c2 b1 b2 )
174 >R >R ( a1 a2 c1 c2 )
175 2swap ( c1 c2 a1 a2 )
181 \ ---- Port I/O ---- {{{
183 : fileport>fid ( fileport -- fid )
186 : fid>fileport ( fid -- fileport )
189 : open-input-file ( addr n -- fileport )
190 r/o open-file drop fid>fileport
193 : close-port ( fileport -- )
194 fileport>fid close-file drop
197 objvar console-i/o-port
198 0 fileport-type console-i/o-port obj!
200 objvar current-input-port
201 console-i/o-port obj@ current-input-port obj!
203 : read-port ( fileport -- obj )
204 current-input-port obj!
207 : read-console ( -- obj )
208 console-i/o-port obj@ read-port ;
212 \ ---- Pre-defined symbols ---- {{{
216 : duplicate-charlist ( charlist -- copy )
218 2dup car 2swap cdr recurse cons
221 : charlist-equiv ( charlist charlist -- bool )
230 2drop 2drop true exit
232 2drop 2drop false exit
237 2drop 2drop false exit
244 car drop -rot car drop = if
245 cdr 2swap cdr recurse
251 : charlist>symbol ( charlist -- symbol-obj )
270 drop symbol-type 2dup
271 symbol-table obj@ cons
276 : cstr>charlist ( addr n -- charlist )
280 2dup drop @ character-type 2swap
288 : create-symbol ( -- )
296 does> dup @ swap 1+ @
299 create-symbol quote quote-symbol
300 create-symbol quasiquote quasiquote-symbol
301 create-symbol unquote unquote-symbol
302 create-symbol unquote-splicing unquote-splicing-symbol
303 create-symbol define define-symbol
304 create-symbol define-macro define-macro-symbol
305 create-symbol set! set!-symbol
306 create-symbol ok ok-symbol
307 create-symbol if if-symbol
308 create-symbol lambda lambda-symbol
309 create-symbol λ λ-symbol
310 create-symbol begin begin-symbol
314 \ ---- Environments ---- {{{
316 : enclosing-env ( env -- env )
319 : first-frame ( env -- frame )
322 : make-frame ( vars vals -- frame )
325 : frame-vars ( frame -- vars )
328 : frame-vals ( frame -- vals )
331 : add-binding ( var val frame -- )
332 2swap 2over frame-vals cons
334 2swap 2over frame-vars cons
338 : extend-env ( vars vals env -- env )
348 : get-vars-vals-frame ( var frame -- bool )
349 2dup frame-vars vars obj!
353 vars obj@ nil objeq? false =
355 2dup vars obj@ car objeq? if
360 vars obj@ cdr vars obj!
361 vals obj@ cdr vals obj!
367 : get-vars-vals ( var env -- vars? vals? bool )
372 2over 2over first-frame
373 get-vars-vals-frame if
375 vars obj@ vals obj@ true
389 : lookup-var ( var env -- val )
393 recoverable-exception throw" Tried to read unbound variable."
397 : set-var ( var val env -- )
398 >R >R 2swap R> R> ( val var env )
400 2swap 2drop ( val vals )
403 recoverable-exception throw" Tried to set unbound variable."
409 : define-var ( var val env -- )
412 2over env obj@ ( var val var env )
414 2swap 2drop ( var val vals )
419 first-frame ( var val frame )
426 : make-procedure ( params body env -- proc )
429 drop compound-proc-type
433 nil nil nil extend-env
438 \ ---- Primitives ---- {{{
440 : make-primitive ( cfa -- )
444 \ 2dup ." Defining primitive " type ." ..." cr
449 rot primitive-proc-type ( var prim )
450 global-env obj@ define-var
453 : ensure-arg-count ( args n -- )
455 drop nil objeq? false = if
456 recoverable-exception throw" Too many arguments for primitive procedure."
460 recoverable-exception throw" Too few arguments for primitive procedure."
468 bold fg red ." Incorrect argument type." reset-term cr
472 : ensure-arg-type ( arg type -- arg )
474 recoverable-exception throw" Incorrect argument type for primitive procedure."
481 \ ---- Macros ---- {{{
485 ( Look up macro in macro table. Returns nil if
487 : lookup-macro ( name_symbol -- proc )
506 : make-macro ( name_symbol params body env -- )
509 2swap ( proc name_symbol )
516 2over 2over ( proc name table name table )
518 2swap 2drop ( proc table )
530 macro-table obj@ cons
539 variable stored-parse-idx
540 create parse-str 161 allot
541 variable parse-str-span
543 create parse-idx-stack 10 allot
544 variable parse-idx-sp
545 parse-idx-stack parse-idx-sp !
548 parse-idx @ parse-idx-sp @ !
553 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
557 parse-idx-sp @ @ parse-idx ! ;
561 '\n' parse-str parse-str-span @ + !
562 1 parse-str-span +! ;
565 4 parse-str parse-str-span @ + !
566 1 parse-str-span +! ;
573 current-input-port obj@ console-i/o-port obj@ objeq? if
574 parse-str 160 expect cr
575 span @ parse-str-span !
577 parse-str 160 current-input-port obj@ fileport>fid read-line
578 drop swap parse-str-span !
580 parse-str-span @ 0= and if append-eof then
591 : charavailable? ( -- bool )
592 parse-str-span @ parse-idx @ > ;
594 : nextchar ( -- char )
595 charavailable? false = if getline then
596 parse-str parse-idx @ + @ ;
599 : whitespace? ( -- bool )
611 nextchar [char] ( = or
612 nextchar [char] ) = or
615 : commentstart? ( -- bool )
616 nextchar [char] ; = ;
620 false \ Indicates whether or not we're eating a comment
623 dup whitespace? or commentstart? or
625 dup nextchar '\n' = and if
626 invert \ Stop eating comment
628 dup false = commentstart? and if
629 invert \ Begin eating comment
644 nextchar [char] - = ;
647 nextchar [char] + = ;
649 : fixnum? ( -- bool )
675 : realnum? ( -- bool )
682 \ Record starting parse idx:
683 \ Want to detect whether any characters (following +/-) were eaten.
690 [char] . nextchar = if
697 [char] e nextchar = [char] E nextchar = or if
705 drop pop-parse-idx false exit
713 \ This is a real number if characters were
714 \ eaten and the next characer is a delimiter.
715 parse-idx @ < delim? and
720 : boolean? ( -- bool )
721 nextchar [char] # <> if false exit then
728 and if pop-parse-idx false exit then
740 : str-equiv? ( str -- bool )
757 delim? false = if drop false then
762 : character? ( -- bool )
763 nextchar [char] # <> if false exit then
768 nextchar [char] \ <> if pop-parse-idx false exit then
772 S" newline" str-equiv? if pop-parse-idx true exit then
773 S" space" str-equiv? if pop-parse-idx true exit then
774 S" tab" str-equiv? if pop-parse-idx true exit then
776 charavailable? false = if pop-parse-idx false exit then
782 nextchar [char] ( = ;
784 : string? ( -- bool )
785 nextchar [char] " = ;
787 : readfixnum ( -- num-atom )
798 10 * nextchar [char] 0 - +
807 : readrealnum ( -- realnum )
809 \ Remember that at this point we're guaranteed to
810 \ have a parsable real on this line.
812 parse-str parse-idx @ +
814 begin delim? false = while
818 parse-str parse-idx @ + over -
825 : readbool ( -- bool-obj )
828 nextchar [char] f = if
839 : readchar ( -- char-obj )
843 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
844 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
845 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
847 nextchar character-type
852 : readstring ( -- charlist )
853 nextchar [char] " = if
858 ." No delimiter following right double quote. Aborting." cr
867 nextchar [char] \ = if
870 [char] n of '\n' endof
871 [char] " of [char] " endof
877 inc-parse-idx character-type
884 : readsymbol ( -- charlist )
885 delim? if nil exit then
887 nextchar inc-parse-idx character-type
894 : readpair ( -- pairobj )
898 nextchar [char] ) = if
903 ." No delimiter following right paren. Aborting." cr
912 \ Read first pair element
917 nextchar [char] . = if
922 ." No delimiter following '.'. Aborting." cr
936 \ Parse a scheme expression
967 nextchar [char] " <> if
968 bold red ." Missing closing double-quote." reset-term cr
986 nextchar [char] ) <> if
987 bold red ." Missing closing paren." reset-term cr
996 nextchar [char] ' = if
998 quote-symbol recurse nil cons cons exit
1001 nextchar [char] ` = if
1003 quasiquote-symbol recurse nil cons cons exit
1006 nextchar [char] , = if
1008 nextchar [char] @ = if
1010 unquote-splicing-symbol recurse nil cons cons exit
1012 unquote-symbol recurse nil cons cons exit
1022 \ Anything else is parsed as a symbol
1023 readsymbol charlist>symbol
1025 \ Replace λ with lambda
1026 2dup λ-symbol objeq? if
1035 \ ---- Eval ---- {{{
1037 : self-evaluating? ( obj -- obj bool )
1038 boolean-type istype? if true exit then
1039 fixnum-type istype? if true exit then
1040 realnum-type istype? if true exit then
1041 character-type istype? if true exit then
1042 string-type istype? if true exit then
1043 nil-type istype? if true exit then
1044 none-type istype? if true exit then
1049 : tagged-list? ( obj tag-obj -- obj bool )
1051 pair-type istype? false = if
1057 : quote? ( obj -- obj bool )
1058 quote-symbol tagged-list? ;
1060 : quote-body ( quote-obj -- quote-body-obj )
1063 : quasiquote? ( obj -- obj bool )
1064 quasiquote-symbol tagged-list? ;
1066 : unquote? ( obj -- obj bool )
1067 unquote-symbol tagged-list?
1070 : eval-unquote ( env obj -- res )
1074 recoverable-exception throw" no arguments to unquote."
1079 recoverable-exception throw" too many arguments to unquote."
1082 2drop car 2swap eval
1085 : (eval-quasiquote) ( env obj -- res )
1090 2over 2over car ( env obj env obj-car )
1098 -2rot cdr ( caritem env cdr )
1099 recurse ( caritem cdritems )
1103 : eval-quasiquote ( obj env -- res )
1104 2swap cdr ( env args )
1107 recoverable-exception throw" no arguments to quasiquote."
1110 2dup cdr ( env args args-cdr )
1112 recoverable-exception throw" too many arguments to quasiquote."
1115 2drop car ( env arg )
1121 pair-type istype? if
1122 (eval-quasiquote) exit
1127 : variable? ( obj -- obj bool )
1128 symbol-type istype? ;
1130 : definition? ( obj -- obj bool )
1131 define-symbol tagged-list? ;
1133 : make-lambda ( params body -- lambda-exp )
1134 lambda-symbol -2rot cons cons ;
1136 ( Handles iterative expansion of defines in
1137 terms of nested lambdas. Most Schemes only
1138 handle one iteration of expansion! )
1139 : definition-var-val ( obj -- var val )
1141 cdr 2dup cdr 2swap car ( val var )
1144 symbol-type istype? false =
1146 2dup cdr 2swap car ( val formals var' )
1147 -2rot 2swap ( var' formals val )
1148 make-lambda nil cons ( var' val' )
1155 : eval-definition ( obj env -- res )
1156 2dup 2rot ( env env obj )
1157 definition-var-val ( env env var val )
1158 2rot eval ( env var val )
1160 2rot ( var val env )
1166 : assignment? ( obj -- obj bool )
1167 set!-symbol tagged-list? ;
1169 : assignment-var ( obj -- var )
1172 : assignment-val ( obj -- val )
1175 : eval-assignment ( obj env -- res )
1177 2over 2over ( env obj env obj )
1178 assignment-val 2swap ( env obj valexp env )
1179 eval ( env obj val )
1181 2swap assignment-var 2swap ( env var val )
1183 2rot ( var val env )
1189 : macro-definition? ( obj -- obj bool )
1190 define-macro-symbol tagged-list? ;
1192 : macro-definition-name ( exp -- mname )
1195 : macro-definition-params ( exp -- params )
1198 : macro-definition-body ( exp -- body )
1202 : eval-define-macro ( obj env -- res )
1205 2dup macro-definition-name 2swap ( name obj )
1206 2dup macro-definition-params 2swap ( name params obj )
1207 macro-definition-body ( name params body )
1209 env obj@ ( name params body env )
1217 : if? ( obj -- obj bool )
1218 if-symbol tagged-list? ;
1220 : if-predicate ( ifobj -- pred )
1223 : if-consequent ( ifobj -- conseq )
1226 : if-alternative ( ifobj -- alt|false )
1234 : false? ( boolobj -- boolean )
1235 boolean-type istype? if
1236 false boolean-type objeq?
1242 : true? ( boolobj -- bool )
1245 : lambda? ( obj -- obj bool )
1246 lambda-symbol tagged-list? ;
1248 : lambda-parameters ( obj -- params )
1251 : lambda-body ( obj -- body )
1254 : begin? ( obj -- obj bool )
1255 begin-symbol tagged-list? ;
1257 : begin-actions ( obj -- actions )
1260 : eval-sequence ( explist env -- finalexp env )
1261 ( Evaluates all bar the final expressions in
1262 an an expression list. The final expression
1263 is returned to allow for tail optimization. )
1265 2swap ( env explist )
1267 \ Abort on empty list
1274 2dup cdr ( env explist nextexplist )
1277 -2rot car 2over ( nextexplist env exp env )
1279 2drop \ discard result
1280 2swap ( env nextexplist )
1283 2drop car 2swap ( finalexp env )
1286 : application? ( obj -- obj bool )
1289 : operator ( obj -- operator )
1292 : operands ( obj -- operands )
1295 : nooperands? ( operands -- bool )
1298 : first-operand ( operands -- operand )
1301 : rest-operands ( operands -- other-operands )
1304 : list-of-vals ( args env -- vals )
1310 2over 2over first-operand 2swap eval
1311 -2rot rest-operands 2swap recurse
1316 : procedure-params ( proc -- params )
1317 drop pair-type car ;
1319 : procedure-body ( proc -- body )
1320 drop pair-type cdr car ;
1322 : procedure-env ( proc -- body )
1323 drop pair-type cdr cdr car ;
1325 ( Ensure terminating symbol arg name is handled
1326 specially to allow for variadic procedures. )
1327 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1329 2over nil? false = if
1330 recoverable-exception throw" Too many arguments for compound procedure."
1337 symbol-type istype? if
1347 recoverable-exception throw" Too few arguments for compound procedure."
1354 recurse ( argvals argnames argvals'' argnames'' )
1355 2rot car 2swap cons ( argvals argvals'' argnames' )
1356 2rot car 2rot cons ( argnames' argvals' )
1360 : apply ( proc argvals -- result )
1362 primitive-proc-type of
1366 compound-proc-type of
1367 2dup procedure-body ( argvals proc body )
1368 -2rot 2dup procedure-params ( body argvals proc argnames )
1369 -2rot procedure-env ( body argnames argvals procenv )
1375 extend-env ( body env )
1379 R> drop ['] eval goto-deferred \ Tail call optimization
1382 recoverable-exception throw" Object not applicable."
1386 : macro-expand ( proc expbody -- result )
1388 2dup procedure-body ( expbody proc procbody )
1389 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1390 -2rot procedure-env ( procbody argnames expbody procenv )
1396 extend-env eval-sequence eval
1399 :noname ( obj env -- result )
1414 2swap eval-quasiquote
1424 2swap eval-definition
1429 2swap eval-assignment
1433 macro-definition? if
1434 2swap eval-define-macro
1450 ['] eval goto-deferred
1454 2dup lambda-parameters
1463 ['] eval goto-deferred
1468 2over 2over ( env exp env exp )
1469 operator ( env exp env opname )
1471 2dup lookup-macro nil? false = if
1472 \ Macro function evaluation
1474 ( env exp env opname mproc )
1475 2swap 2drop -2rot 2drop cdr ( env mproc body )
1480 ['] eval goto-deferred
1482 \ Regular function application
1484 2drop ( env exp env opname )
1486 2swap eval ( env exp proc )
1488 -2rot ( proc env exp )
1489 operands 2swap ( proc operands env )
1490 list-of-vals ( proc argvals )
1497 recoverable-exception throw" Tried to evaluate object with unknown type."
1502 \ ---- Print ---- {{{
1504 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1506 : printrealnum ( realnumobj -- ) drop float-print ;
1508 : printbool ( numobj -- )
1516 : printchar ( charobj -- )
1519 9 of ." #\tab" endof
1520 bl of ." #\space" endof
1521 '\n' of ." #\newline" endof
1527 : (printstring) ( stringobj -- )
1528 nil? if 2drop exit then
1532 '\n' of ." \n" drop endof
1533 [char] \ of ." \\" drop endof
1534 [char] " of [char] \ emit [char] " emit drop endof
1540 : printstring ( stringobj -- )
1545 : printsymbol ( symbolobj -- )
1546 nil-type istype? if 2drop exit then
1552 : printnil ( nilobj -- )
1555 : printpair ( pairobj -- )
1559 nil-type istype? if 2drop exit then
1560 pair-type istype? if space recurse exit then
1564 : printprim ( primobj -- )
1565 2drop ." <primitive procedure>" ;
1567 : printcomp ( primobj -- )
1568 2drop ." <compound procedure>" ;
1570 : printnone ( noneobj -- )
1571 2drop ." Unspecified return value" ;
1574 fixnum-type istype? if printfixnum exit then
1575 realnum-type istype? if printrealnum exit then
1576 boolean-type istype? if printbool exit then
1577 character-type istype? if printchar exit then
1578 string-type istype? if printstring exit then
1579 symbol-type istype? if printsymbol exit then
1580 nil-type istype? if printnil exit then
1581 pair-type istype? if ." (" printpair ." )" exit then
1582 primitive-proc-type istype? if printprim exit then
1583 compound-proc-type istype? if printcomp exit then
1584 none-type istype? if printnone exit then
1586 recoverable-exception throw" Tried to print object with unknown type."
1591 \ ---- Garbage Collection ---- {{{
1596 variable gc-stack-depth
1599 depth gc-stack-depth !
1603 false gc-enabled ! ;
1608 : pairlike? ( obj -- obj bool )
1609 pair-type istype? if true exit then
1610 string-type istype? if true exit then
1611 symbol-type istype? if true exit then
1612 compound-proc-type istype? if true exit then
1617 : pairlike-marked? ( obj -- obj bool )
1618 over nextfrees + @ 0=
1621 : mark-pairlike ( obj -- obj )
1622 over nextfrees + 0 swap !
1631 : gc-mark-obj ( obj -- )
1633 pairlike? invert if 2drop exit then
1634 pairlike-marked? if 2drop exit then
1645 scheme-memsize nextfree !
1646 0 scheme-memsize 1- do
1647 nextfrees i + @ 0<> if
1648 nextfree @ nextfrees i + !
1654 \ Following a GC, this gives the amount of free memory
1658 nextfrees i + @ 0= if 1+ then
1662 \ Debugging word - helps spot memory that is retained
1665 nextfrees i + @ 0<> if
1677 symbol-table obj@ gc-mark-obj
1678 macro-table obj@ gc-mark-obj
1679 global-env obj@ gc-mark-obj
1681 depth gc-stack-depth @ do
1690 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1691 ; is collect-garbage
1695 \ ---- Loading files ---- {{{
1697 : charlist>cstr ( charlist addr -- n )
1699 dup 2swap ( origaddr addr charlist )
1705 drop ( origaddr addr charlist char )
1706 -rot 2swap ( origaddr charlist addr char )
1708 1+ -rot ( origaddr nextaddr charlist )
1711 2drop ( origaddr finaladdr )
1715 : load ( addr n -- finalResult )
1720 ok-symbol ( port res )
1723 2over read-port ( port res obj )
1725 2dup EOF character-type objeq? if
1726 2drop 2swap close-port
1730 2swap 2drop ( port obj )
1732 global-env obj@ eval ( port res )
1738 \ ---- Standard Library ---- {{{
1740 include scheme-primitives.4th
1742 s" scheme-library.scm" load 2drop
1748 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
1749 : repl-body ( -- bool )
1750 cr bold fg green ." > " reset-term
1754 2dup EOF character-type objeq? if
1756 bold fg blue ." Moriturus te saluto." reset-term cr
1760 global-env obj@ eval
1762 fg cyan ." ; " print reset-term
1768 cr ." Welcome to scheme.forth.jl!" cr
1769 ." Use Ctrl-D to exit." cr
1778 recoverable-exception of false endof
1779 unrecoverable-exception of true endof