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 : variable? ( obj -- obj bool )
1064 symbol-type istype? ;
1066 : definition? ( obj -- obj bool )
1067 define-symbol tagged-list? ;
1069 : make-lambda ( params body -- lambda-exp )
1070 lambda-symbol -2rot cons cons ;
1072 ( Handles iterative expansion of defines in
1073 terms of nested lambdas. Most Schemes only
1074 handle one iteration of expansion! )
1075 : definition-var-val ( obj -- var val )
1077 cdr 2dup cdr 2swap car ( val var )
1080 symbol-type istype? false =
1082 2dup cdr 2swap car ( val formals var' )
1083 -2rot 2swap ( var' formals val )
1084 make-lambda nil cons ( var' val' )
1091 : eval-definition ( obj env -- res )
1092 2dup 2rot ( env env obj )
1093 definition-var-val ( env env var val )
1094 2rot eval ( env var val )
1096 2rot ( var val env )
1102 : assignment? ( obj -- obj bool )
1103 set!-symbol tagged-list? ;
1105 : assignment-var ( obj -- var )
1108 : assignment-val ( obj -- val )
1111 : eval-assignment ( obj env -- res )
1113 2over 2over ( env obj env obj )
1114 assignment-val 2swap ( env obj valexp env )
1115 eval ( env obj val )
1117 2swap assignment-var 2swap ( env var val )
1119 2rot ( var val env )
1125 : macro-definition? ( obj -- obj bool )
1126 define-macro-symbol tagged-list? ;
1128 : macro-definition-name ( exp -- mname )
1131 : macro-definition-params ( exp -- params )
1134 : macro-definition-body ( exp -- body )
1138 : eval-define-macro ( obj env -- res )
1141 2dup macro-definition-name 2swap ( name obj )
1142 2dup macro-definition-params 2swap ( name params obj )
1143 macro-definition-body ( name params body )
1145 env obj@ ( name params body env )
1153 : if? ( obj -- obj bool )
1154 if-symbol tagged-list? ;
1156 : if-predicate ( ifobj -- pred )
1159 : if-consequent ( ifobj -- conseq )
1162 : if-alternative ( ifobj -- alt|false )
1170 : false? ( boolobj -- boolean )
1171 boolean-type istype? if
1172 false boolean-type objeq?
1178 : true? ( boolobj -- bool )
1181 : lambda? ( obj -- obj bool )
1182 lambda-symbol tagged-list? ;
1184 : lambda-parameters ( obj -- params )
1187 : lambda-body ( obj -- body )
1190 : begin? ( obj -- obj bool )
1191 begin-symbol tagged-list? ;
1193 : begin-actions ( obj -- actions )
1196 : eval-sequence ( explist env -- finalexp env )
1197 ( Evaluates all bar the final expressions in
1198 an an expression list. The final expression
1199 is returned to allow for tail optimization. )
1201 2swap ( env explist )
1203 \ Abort on empty list
1210 2dup cdr ( env explist nextexplist )
1213 -2rot car 2over ( nextexplist env exp env )
1215 2drop \ discard result
1216 2swap ( env nextexplist )
1219 2drop car 2swap ( finalexp env )
1222 : application? ( obj -- obj bool )
1225 : operator ( obj -- operator )
1228 : operands ( obj -- operands )
1231 : nooperands? ( operands -- bool )
1234 : first-operand ( operands -- operand )
1237 : rest-operands ( operands -- other-operands )
1240 : list-of-vals ( args env -- vals )
1246 2over 2over first-operand 2swap eval
1247 -2rot rest-operands 2swap recurse
1252 : procedure-params ( proc -- params )
1253 drop pair-type car ;
1255 : procedure-body ( proc -- body )
1256 drop pair-type cdr car ;
1258 : procedure-env ( proc -- body )
1259 drop pair-type cdr cdr car ;
1261 ( Ensure terminating symbol arg name is handled
1262 specially to allow for variadic procedures. )
1263 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1265 2over nil? false = if
1266 recoverable-exception throw" Too many arguments for compound procedure."
1273 symbol-type istype? if
1283 recoverable-exception throw" Too few arguments for compound procedure."
1290 recurse ( argvals argnames argvals'' argnames'' )
1291 2rot car 2swap cons ( argvals argvals'' argnames' )
1292 2rot car 2rot cons ( argnames' argvals' )
1296 : apply ( proc argvals -- result )
1298 primitive-proc-type of
1302 compound-proc-type of
1303 2dup procedure-body ( argvals proc body )
1304 -2rot 2dup procedure-params ( body argvals proc argnames )
1305 -2rot procedure-env ( body argnames argvals procenv )
1311 extend-env ( body env )
1315 R> drop ['] eval goto-deferred \ Tail call optimization
1318 recoverable-exception throw" Object not applicable."
1322 : macro-expand ( proc expbody -- result )
1324 2dup procedure-body ( expbody proc procbody )
1325 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1326 -2rot procedure-env ( procbody argnames expbody procenv )
1332 extend-env eval-sequence eval
1335 :noname ( obj env -- result )
1355 2swap eval-definition
1360 2swap eval-assignment
1364 macro-definition? if
1365 2swap eval-define-macro
1381 ['] eval goto-deferred
1385 2dup lambda-parameters
1394 ['] eval goto-deferred
1399 2over 2over ( env exp env exp )
1400 operator ( env exp env opname )
1402 2dup lookup-macro nil? false = if
1403 \ Macro function evaluation
1405 ( env exp env opname mproc )
1406 2swap 2drop -2rot 2drop cdr ( env mproc body )
1411 ['] eval goto-deferred
1413 \ Regular function application
1415 2drop ( env exp env opname )
1417 2swap eval ( env exp proc )
1419 -2rot ( proc env exp )
1420 operands 2swap ( proc operands env )
1421 list-of-vals ( proc argvals )
1428 recoverable-exception throw" Tried to evaluate object with unknown type."
1433 \ ---- Print ---- {{{
1435 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1437 : printrealnum ( realnumobj -- ) drop float-print ;
1439 : printbool ( numobj -- )
1447 : printchar ( charobj -- )
1450 9 of ." #\tab" endof
1451 bl of ." #\space" endof
1452 '\n' of ." #\newline" endof
1458 : (printstring) ( stringobj -- )
1459 nil? if 2drop exit then
1463 '\n' of ." \n" drop endof
1464 [char] \ of ." \\" drop endof
1465 [char] " of [char] \ emit [char] " emit drop endof
1471 : printstring ( stringobj -- )
1476 : printsymbol ( symbolobj -- )
1477 nil-type istype? if 2drop exit then
1483 : printnil ( nilobj -- )
1486 : printpair ( pairobj -- )
1490 nil-type istype? if 2drop exit then
1491 pair-type istype? if space recurse exit then
1495 : printprim ( primobj -- )
1496 2drop ." <primitive procedure>" ;
1498 : printcomp ( primobj -- )
1499 2drop ." <compound procedure>" ;
1501 : printnone ( noneobj -- )
1502 2drop ." Unspecified return value" ;
1505 fixnum-type istype? if printfixnum exit then
1506 realnum-type istype? if printrealnum exit then
1507 boolean-type istype? if printbool exit then
1508 character-type istype? if printchar exit then
1509 string-type istype? if printstring exit then
1510 symbol-type istype? if printsymbol exit then
1511 nil-type istype? if printnil exit then
1512 pair-type istype? if ." (" printpair ." )" exit then
1513 primitive-proc-type istype? if printprim exit then
1514 compound-proc-type istype? if printcomp exit then
1515 none-type istype? if printnone exit then
1517 recoverable-exception throw" Tried to print object with unknown type."
1522 \ ---- Garbage Collection ---- {{{
1527 variable gc-stack-depth
1530 depth gc-stack-depth !
1534 false gc-enabled ! ;
1539 : pairlike? ( obj -- obj bool )
1540 pair-type istype? if true exit then
1541 string-type istype? if true exit then
1542 symbol-type istype? if true exit then
1543 compound-proc-type istype? if true exit then
1548 : pairlike-marked? ( obj -- obj bool )
1549 over nextfrees + @ 0=
1552 : mark-pairlike ( obj -- obj )
1553 over nextfrees + 0 swap !
1562 : gc-mark-obj ( obj -- )
1564 pairlike? invert if 2drop exit then
1565 pairlike-marked? if 2drop exit then
1576 scheme-memsize nextfree !
1577 0 scheme-memsize 1- do
1578 nextfrees i + @ 0<> if
1579 nextfree @ nextfrees i + !
1585 \ Following a GC, this gives the amount of free memory
1589 nextfrees i + @ 0= if 1+ then
1593 \ Debugging word - helps spot memory that is retained
1596 nextfrees i + @ 0<> if
1608 symbol-table obj@ gc-mark-obj
1609 macro-table obj@ gc-mark-obj
1610 global-env obj@ gc-mark-obj
1612 depth gc-stack-depth @ do
1621 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1622 ; is collect-garbage
1626 \ ---- Loading files ---- {{{
1628 : charlist>cstr ( charlist addr -- n )
1630 dup 2swap ( origaddr addr charlist )
1636 drop ( origaddr addr charlist char )
1637 -rot 2swap ( origaddr charlist addr char )
1639 1+ -rot ( origaddr nextaddr charlist )
1642 2drop ( origaddr finaladdr )
1646 : load ( addr n -- finalResult )
1651 ok-symbol ( port res )
1654 2over read-port ( port res obj )
1656 2dup EOF character-type objeq? if
1657 2drop 2swap close-port
1661 2swap 2drop ( port obj )
1663 global-env obj@ eval ( port res )
1669 \ ---- Standard Library ---- {{{
1671 include scheme-primitives.4th
1673 s" scheme-library.scm" load 2drop
1679 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
1680 : repl-body ( -- bool )
1681 cr bold fg green ." > " reset-term
1685 2dup EOF character-type objeq? if
1687 bold fg blue ." Moriturus te saluto." reset-term cr
1691 global-env obj@ eval
1693 fg cyan ." ; " print reset-term
1699 cr ." Welcome to scheme.forth.jl!" cr
1700 ." Use Ctrl-D to exit." cr
1709 recoverable-exception of false endof
1710 unrecoverable-exception of true endof