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? ;
1069 : unquote-splicing? ( obj -- obj bool )
1070 unquote-splicing-symbol tagged-list? ;
1072 : eval-unquote ( env obj -- res )
1076 recoverable-exception throw" no arguments to unquote."
1081 recoverable-exception throw" too many arguments to unquote."
1084 2drop car 2swap eval
1087 ( Create a new list from elements of l1 consed on to l2 )
1088 : join-lists ( l2 l1 -- l3 )
1089 nil? if 2drop exit then
1096 defer eval-quasiquote-item
1097 : eval-quasiquote-list ( env obj -- res )
1102 2over 2over ( env obj env obj )
1105 -2rot car ( cdritems env objcar )
1107 unquote-splicing? if
1108 eval-unquote ( cdritems caritem )
1116 eval-quasiquote-item ( cdritems caritem )
1127 pair-type istype? if
1128 eval-quasiquote-list exit
1132 ; is eval-quasiquote-item
1134 : eval-quasiquote ( obj env -- res )
1135 2swap cdr ( env args )
1138 recoverable-exception throw" no arguments to quasiquote."
1141 2dup cdr ( env args args-cdr )
1143 recoverable-exception throw" too many arguments to quasiquote."
1146 2drop car ( env arg )
1148 eval-quasiquote-item
1151 : variable? ( obj -- obj bool )
1152 symbol-type istype? ;
1154 : definition? ( obj -- obj bool )
1155 define-symbol tagged-list? ;
1157 : make-lambda ( params body -- lambda-exp )
1158 lambda-symbol -2rot cons cons ;
1160 ( Handles iterative expansion of defines in
1161 terms of nested lambdas. Most Schemes only
1162 handle one iteration of expansion! )
1163 : definition-var-val ( obj -- var val )
1165 cdr 2dup cdr 2swap car ( val var )
1168 symbol-type istype? false =
1170 2dup cdr 2swap car ( val formals var' )
1171 -2rot 2swap ( var' formals val )
1172 make-lambda nil cons ( var' val' )
1179 : eval-definition ( obj env -- res )
1180 2dup 2rot ( env env obj )
1181 definition-var-val ( env env var val )
1182 2rot eval ( env var val )
1184 2rot ( var val env )
1190 : assignment? ( obj -- obj bool )
1191 set!-symbol tagged-list? ;
1193 : assignment-var ( obj -- var )
1196 : assignment-val ( obj -- val )
1199 : eval-assignment ( obj env -- res )
1201 2over 2over ( env obj env obj )
1202 assignment-val 2swap ( env obj valexp env )
1203 eval ( env obj val )
1205 2swap assignment-var 2swap ( env var val )
1207 2rot ( var val env )
1213 : macro-definition? ( obj -- obj bool )
1214 define-macro-symbol tagged-list? ;
1216 : macro-definition-name ( exp -- mname )
1219 : macro-definition-params ( exp -- params )
1222 : macro-definition-body ( exp -- body )
1226 : eval-define-macro ( obj env -- res )
1229 2dup macro-definition-name 2swap ( name obj )
1230 2dup macro-definition-params 2swap ( name params obj )
1231 macro-definition-body ( name params body )
1233 env obj@ ( name params body env )
1241 : if? ( obj -- obj bool )
1242 if-symbol tagged-list? ;
1244 : if-predicate ( ifobj -- pred )
1247 : if-consequent ( ifobj -- conseq )
1250 : if-alternative ( ifobj -- alt|none )
1258 : false? ( boolobj -- boolean )
1259 boolean-type istype? if
1260 false boolean-type objeq?
1266 : true? ( boolobj -- bool )
1269 : lambda? ( obj -- obj bool )
1270 lambda-symbol tagged-list? ;
1272 : lambda-parameters ( obj -- params )
1275 : lambda-body ( obj -- body )
1278 : begin? ( obj -- obj bool )
1279 begin-symbol tagged-list? ;
1281 : begin-actions ( obj -- actions )
1284 : eval-sequence ( explist env -- finalexp env )
1285 ( Evaluates all bar the final expressions in
1286 an an expression list. The final expression
1287 is returned to allow for tail optimization. )
1289 2swap ( env explist )
1291 \ Abort on empty list
1298 2dup cdr ( env explist nextexplist )
1301 -2rot car 2over ( nextexplist env exp env )
1303 2drop \ discard result
1304 2swap ( env nextexplist )
1307 2drop car 2swap ( finalexp env )
1310 : application? ( obj -- obj bool )
1313 : operator ( obj -- operator )
1316 : operands ( obj -- operands )
1319 : nooperands? ( operands -- bool )
1322 : first-operand ( operands -- operand )
1325 : rest-operands ( operands -- other-operands )
1328 : list-of-vals ( args env -- vals )
1334 2over 2over first-operand 2swap eval
1335 -2rot rest-operands 2swap recurse
1340 : procedure-params ( proc -- params )
1341 drop pair-type car ;
1343 : procedure-body ( proc -- body )
1344 drop pair-type cdr car ;
1346 : procedure-env ( proc -- body )
1347 drop pair-type cdr cdr car ;
1349 ( Ensure terminating symbol arg name is handled
1350 specially to allow for variadic procedures. )
1351 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1353 2over nil? false = if
1354 recoverable-exception throw" Too many arguments for compound procedure."
1361 symbol-type istype? if
1371 recoverable-exception throw" Too few arguments for compound procedure."
1378 recurse ( argvals argnames argvals'' argnames'' )
1379 2rot car 2swap cons ( argvals argvals'' argnames' )
1380 2rot car 2rot cons ( argnames' argvals' )
1384 : apply ( proc argvals -- result )
1386 primitive-proc-type of
1390 compound-proc-type of
1391 2dup procedure-body ( argvals proc body )
1392 -2rot 2dup procedure-params ( body argvals proc argnames )
1393 -2rot procedure-env ( body argnames argvals procenv )
1399 extend-env ( body env )
1403 R> drop ['] eval goto-deferred \ Tail call optimization
1406 recoverable-exception throw" Object not applicable."
1410 : macro-expand ( proc expbody -- result )
1412 2dup procedure-body ( expbody proc procbody )
1413 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1414 -2rot procedure-env ( procbody argnames expbody procenv )
1420 extend-env eval-sequence eval
1423 :noname ( obj env -- result )
1438 2swap eval-quasiquote
1448 2swap eval-definition
1453 2swap eval-assignment
1457 macro-definition? if
1458 2swap eval-define-macro
1474 ['] eval goto-deferred
1478 2dup lambda-parameters
1487 ['] eval goto-deferred
1492 2over 2over ( env exp env exp )
1493 operator ( env exp env opname )
1495 2dup lookup-macro nil? false = if
1496 \ Macro function evaluation
1498 ( env exp env opname mproc )
1499 2swap 2drop -2rot 2drop cdr ( env mproc body )
1504 ['] eval goto-deferred
1506 \ Regular function application
1508 2drop ( env exp env opname )
1510 2swap eval ( env exp proc )
1512 -2rot ( proc env exp )
1513 operands 2swap ( proc operands env )
1514 list-of-vals ( proc argvals )
1521 recoverable-exception throw" Tried to evaluate object with unknown type."
1526 \ ---- Print ---- {{{
1528 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1530 : printrealnum ( realnumobj -- ) drop float-print ;
1532 : printbool ( numobj -- )
1540 : printchar ( charobj -- )
1543 9 of ." #\tab" endof
1544 bl of ." #\space" endof
1545 '\n' of ." #\newline" endof
1551 : (printstring) ( stringobj -- )
1552 nil? if 2drop exit then
1556 '\n' of ." \n" drop endof
1557 [char] \ of ." \\" drop endof
1558 [char] " of [char] \ emit [char] " emit drop endof
1564 : printstring ( stringobj -- )
1569 : printsymbol ( symbolobj -- )
1570 nil-type istype? if 2drop exit then
1576 : printnil ( nilobj -- )
1579 : printpair ( pairobj -- )
1583 nil-type istype? if 2drop exit then
1584 pair-type istype? if space recurse exit then
1588 : printprim ( primobj -- )
1589 2drop ." <primitive procedure>" ;
1591 : printcomp ( primobj -- )
1592 2drop ." <compound procedure>" ;
1594 : printnone ( noneobj -- )
1595 2drop ." Unspecified return value" ;
1597 : printport ( port -- )
1601 fixnum-type istype? if printfixnum exit then
1602 realnum-type istype? if printrealnum exit then
1603 boolean-type istype? if printbool exit then
1604 character-type istype? if printchar exit then
1605 string-type istype? if printstring exit then
1606 symbol-type istype? if printsymbol exit then
1607 nil-type istype? if printnil exit then
1608 pair-type istype? if ." (" printpair ." )" exit then
1609 primitive-proc-type istype? if printprim exit then
1610 compound-proc-type istype? if printcomp exit then
1611 none-type istype? if printnone exit then
1613 recoverable-exception throw" Tried to print object with unknown type."
1618 \ ---- Garbage Collection ---- {{{
1623 variable gc-stack-depth
1626 depth gc-stack-depth !
1630 false gc-enabled ! ;
1635 : pairlike? ( obj -- obj bool )
1636 pair-type istype? if true exit then
1637 string-type istype? if true exit then
1638 symbol-type istype? if true exit then
1639 compound-proc-type istype? if true exit then
1644 : pairlike-marked? ( obj -- obj bool )
1645 over nextfrees + @ 0=
1648 : mark-pairlike ( obj -- obj )
1649 over nextfrees + 0 swap !
1658 : gc-mark-obj ( obj -- )
1660 pairlike? invert if 2drop exit then
1661 pairlike-marked? if 2drop exit then
1672 scheme-memsize nextfree !
1673 0 scheme-memsize 1- do
1674 nextfrees i + @ 0<> if
1675 nextfree @ nextfrees i + !
1681 \ Following a GC, this gives the amount of free memory
1685 nextfrees i + @ 0= if 1+ then
1689 \ Debugging word - helps spot memory that is retained
1692 nextfrees i + @ 0<> if
1704 symbol-table obj@ gc-mark-obj
1705 macro-table obj@ gc-mark-obj
1706 global-env obj@ gc-mark-obj
1708 depth gc-stack-depth @ do
1717 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1718 ; is collect-garbage
1722 \ ---- Loading files ---- {{{
1724 : charlist>cstr ( charlist addr -- n )
1726 dup 2swap ( origaddr addr charlist )
1732 drop ( origaddr addr charlist char )
1733 -rot 2swap ( origaddr charlist addr char )
1735 1+ -rot ( origaddr nextaddr charlist )
1738 2drop ( origaddr finaladdr )
1742 : load ( addr n -- finalResult )
1747 ok-symbol ( port res )
1750 2over read-port ( port res obj )
1752 2dup EOF character-type objeq? if
1753 2drop 2swap close-port
1757 2swap 2drop ( port obj )
1759 global-env obj@ eval ( port res )
1765 \ ---- Standard Library ---- {{{
1767 include scheme-primitives.4th
1769 s" scheme-library.scm" load 2drop
1775 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
1776 : repl-body ( -- bool )
1777 cr bold fg green ." > " reset-term
1781 2dup EOF character-type objeq? if
1783 bold fg blue ." Moriturus te saluto." reset-term cr
1787 global-env obj@ eval
1789 fg cyan ." ; " print reset-term
1795 cr ." Welcome to scheme.forth.jl!" cr
1796 ." Use Ctrl-D to exit." cr
1805 recoverable-exception of false endof
1806 unrecoverable-exception of true endof