4 include term-colours.4th
6 include catch-throw.4th
30 make-type boolean-type
31 make-type character-type
37 make-type primitive-proc-type
38 make-type compound-proc-type
39 make-type fileport-type
40 : istype? ( obj type -- obj bool )
45 \ ---- Exceptions ---- {{{
47 variable nextexception
50 create nextexception @ ,
54 make-exception recoverable-exception
55 make-exception unrecoverable-exception
57 : display-exception-msg ( addr count -- )
70 ['] display-exception-msg ,
78 \ ---- List-structured memory ---- {{{
80 10000 constant scheme-memsize
82 create car-cells scheme-memsize allot
83 create car-type-cells scheme-memsize allot
84 create cdr-cells scheme-memsize allot
85 create cdr-type-cells scheme-memsize allot
87 create nextfrees scheme-memsize allot
98 nextfrees nextfree @ + @
101 nextfree @ scheme-memsize >= if
105 nextfree @ scheme-memsize >= if
106 unrecoverable-exception throw s" Out of memory!"
110 : cons ( car-obj cdr-obj -- pair-obj )
111 cdr-type-cells nextfree @ + !
112 cdr-cells nextfree @ + !
113 car-type-cells nextfree @ + !
114 car-cells nextfree @ + !
120 : car ( pair-obj -- car-obj )
122 dup car-cells + @ swap
126 : cdr ( pair-obj -- car-obj )
128 dup cdr-cells + @ swap
132 : set-car! ( obj pair-obj -- )
134 rot swap car-type-cells + !
138 : set-cdr! ( obj pair-obj -- )
140 rot swap cdr-type-cells + !
145 : nil? nil-type istype? ;
148 : none? none-type istype? ;
150 : objvar create nil swap , , ;
152 : value@ ( objvar -- val ) @ ;
153 : type@ ( objvar -- type ) 1+ @ ;
154 : value! ( newval objvar -- ) ! ;
155 : type! ( newtype objvar -- ) 1+ ! ;
156 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
157 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
159 : objeq? ( obj obj -- bool )
162 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
163 >R >R ( a1 a2 b1 b2 )
164 2swap ( b1 b2 a1 a2 )
165 R> R> ( b1 b2 a1 a2 c1 c2 )
169 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
170 2swap ( a1 a2 c1 c2 b1 b2 )
171 >R >R ( a1 a2 c1 c2 )
172 2swap ( c1 c2 a1 a2 )
178 \ ---- Port I/O ---- {{{
180 : fileport>fid ( fileport -- fid )
183 : fid>fileport ( fid -- fileport )
186 : open-input-file ( addr n -- fileport )
187 r/o open-file drop fid>fileport
190 : close-port ( fileport -- )
191 fileport>fid close-file drop
194 objvar console-i/o-port
195 0 fileport-type console-i/o-port obj!
197 objvar current-input-port
198 console-i/o-port obj@ current-input-port obj!
200 : read-port ( fileport -- obj )
201 current-input-port obj!
204 : read-console ( -- obj )
205 console-i/o-port obj@ read-port ;
209 \ ---- Pre-defined symbols ---- {{{
213 : duplicate-charlist ( charlist -- copy )
215 2dup car 2swap cdr recurse cons
218 : charlist-equiv ( charlist charlist -- bool )
227 2drop 2drop true exit
229 2drop 2drop false exit
234 2drop 2drop false exit
241 car drop -rot car drop = if
242 cdr 2swap cdr recurse
248 : charlist>symbol ( charlist -- symbol-obj )
267 drop symbol-type 2dup
268 symbol-table obj@ cons
273 : cstr>charlist ( addr n -- charlist )
277 2dup drop @ character-type 2swap
285 : create-symbol ( -- )
293 does> dup @ swap 1+ @
296 create-symbol quote quote-symbol
297 create-symbol quasiquote quasiquote-symbol
298 create-symbol unquote unquote-symbol
299 create-symbol unquote-splicing unquote-splicing-symbol
300 create-symbol define define-symbol
301 create-symbol define-macro define-macro-symbol
302 create-symbol set! set!-symbol
303 create-symbol ok ok-symbol
304 create-symbol if if-symbol
305 create-symbol lambda lambda-symbol
306 create-symbol λ λ-symbol
307 create-symbol begin begin-symbol
311 \ ---- Environments ---- {{{
313 : enclosing-env ( env -- env )
316 : first-frame ( env -- frame )
319 : make-frame ( vars vals -- frame )
322 : frame-vars ( frame -- vars )
325 : frame-vals ( frame -- vals )
328 : add-binding ( var val frame -- )
329 2swap 2over frame-vals cons
331 2swap 2over frame-vars cons
335 : extend-env ( vars vals env -- env )
345 : get-vars-vals-frame ( var frame -- bool )
346 2dup frame-vars vars obj!
350 vars obj@ nil objeq? false =
352 2dup vars obj@ car objeq? if
357 vars obj@ cdr vars obj!
358 vals obj@ cdr vals obj!
364 : get-vars-vals ( var env -- vars? vals? bool )
369 2over 2over first-frame
370 get-vars-vals-frame if
372 vars obj@ vals obj@ true
386 : lookup-var ( var env -- val )
390 recoverable-exception throw" Tried to read unbound variable."
394 : set-var ( var val env -- )
395 >R >R 2swap R> R> ( val var env )
397 2swap 2drop ( val vals )
400 recoverable-exception throw" Tried to set unbound variable."
406 : define-var ( var val env -- )
409 2over env obj@ ( var val var env )
411 2swap 2drop ( var val vals )
416 first-frame ( var val frame )
423 : make-procedure ( params body env -- proc )
426 drop compound-proc-type
430 nil nil nil extend-env
435 \ ---- Primitives ---- {{{
437 : make-primitive ( cfa -- )
444 rot primitive-proc-type ( var prim )
445 global-env obj@ define-var
448 : ensure-arg-count ( args n -- )
450 drop nil objeq? false = if
451 recoverable-exception throw" Too many arguments for primitive procedure."
455 recoverable-exception throw" Too few arguments for primitive procedure."
462 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
464 drop nil objeq? false = if
465 recoverable-exception throw" Too many arguments for primitive procedure."
469 recoverable-exception throw" Too few arguments for primitive procedure."
472 2dup cdr 2swap car ( ... t1 n args' arg1 )
473 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
475 recoverable-exception throw" Incorrect type for primitive procedure."
483 : push-args-to-stack ( args -- arg1 arg2 ... argn )
493 : add-fa-checks ( cfa n -- cfa' )
494 here current @ 1+ dup @ , !
498 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
499 ['] push-args-to-stack ,
500 ['] lit , , ['] execute ,
504 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
505 here current @ 1+ dup @ , !
512 dup ( cfa t1 t2 ... tn n m )
517 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
523 ['] lit , , ['] ensure-arg-type-and-count ,
525 ['] push-args-to-stack ,
526 ['] lit , , ['] execute ,
532 : make-fa-primitive ( cfa n -- )
533 add-fa-checks make-primitive ;
535 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
536 add-fa-type-checks make-primitive ;
539 bold fg red ." Incorrect argument type." reset-term cr
543 : ensure-arg-type ( arg type -- arg )
545 recoverable-exception throw" Incorrect argument type for primitive procedure."
552 \ ---- Macros ---- {{{
556 ( Look up macro in macro table. Returns nil if
558 : lookup-macro ( name_symbol -- proc )
577 : make-macro ( name_symbol params body env -- )
580 2swap ( proc name_symbol )
587 2over 2over ( proc name table name table )
589 2swap 2drop ( proc table )
601 macro-table obj@ cons
610 variable stored-parse-idx
611 create parse-str 161 allot
612 variable parse-str-span
614 create parse-idx-stack 10 allot
615 variable parse-idx-sp
616 parse-idx-stack parse-idx-sp !
619 parse-idx @ parse-idx-sp @ !
624 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
628 parse-idx-sp @ @ parse-idx ! ;
632 '\n' parse-str parse-str-span @ + !
633 1 parse-str-span +! ;
636 4 parse-str parse-str-span @ + !
637 1 parse-str-span +! ;
644 current-input-port obj@ console-i/o-port obj@ objeq? if
645 parse-str 160 expect cr
646 span @ parse-str-span !
648 parse-str 160 current-input-port obj@ fileport>fid read-line
649 drop swap parse-str-span !
651 parse-str-span @ 0= and if append-eof then
662 : charavailable? ( -- bool )
663 parse-str-span @ parse-idx @ > ;
665 : nextchar ( -- char )
666 charavailable? false = if getline then
667 parse-str parse-idx @ + @ ;
670 : whitespace? ( -- bool )
682 nextchar [char] ( = or
683 nextchar [char] ) = or
686 : commentstart? ( -- bool )
687 nextchar [char] ; = ;
691 false \ Indicates whether or not we're eating a comment
694 dup whitespace? or commentstart? or
696 dup nextchar '\n' = and if
697 invert \ Stop eating comment
699 dup false = commentstart? and if
700 invert \ Begin eating comment
715 nextchar [char] - = ;
718 nextchar [char] + = ;
720 : fixnum? ( -- bool )
746 : flonum? ( -- bool )
753 \ Record starting parse idx:
754 \ Want to detect whether any characters (following +/-) were eaten.
761 [char] . nextchar = if
768 [char] e nextchar = [char] E nextchar = or if
776 drop pop-parse-idx false exit
784 \ This is a real number if characters were
785 \ eaten and the next characer is a delimiter.
786 parse-idx @ < delim? and
791 : ratnum? ( -- bool )
799 pop-parse-idx false exit
808 [char] / nextchar <> if
809 pop-parse-idx false exit
815 pop-parse-idx false exit
827 : boolean? ( -- bool )
828 nextchar [char] # <> if false exit then
835 and if pop-parse-idx false exit then
847 : str-equiv? ( str -- bool )
864 delim? false = if drop false then
869 : character? ( -- bool )
870 nextchar [char] # <> if false exit then
875 nextchar [char] \ <> if pop-parse-idx false exit then
879 S" newline" str-equiv? if pop-parse-idx true exit then
880 S" space" str-equiv? if pop-parse-idx true exit then
881 S" tab" str-equiv? if pop-parse-idx true exit then
883 charavailable? false = if pop-parse-idx false exit then
889 nextchar [char] ( = ;
891 : string? ( -- bool )
892 nextchar [char] " = ;
894 : readfixnum ( -- fixnum )
905 10 * nextchar [char] 0 - +
914 : readflonum ( -- flonum )
918 [char] . nextchar = if
924 nextchar [char] 0 - i->f ( f exp d )
925 over f/ rot f+ ( exp f' )
926 swap 10.0 f* ( f' exp' )
933 [char] e nextchar = [char] E nextchar = or if
947 : make-rational ( fixnum fixnum -- ratnum|fixnum )
954 fixnum-type swap fixnum-type
955 cons drop ratnum-type
959 : readratnum ( -- ratnum )
960 readfixnum inc-parse-idx readfixnum
964 : readbool ( -- bool-obj )
967 nextchar [char] f = if
978 : readchar ( -- char-obj )
982 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
983 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
984 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
986 nextchar character-type
991 : readstring ( -- charlist )
992 nextchar [char] " = if
997 ." No delimiter following right double quote. Aborting." cr
1006 nextchar [char] \ = if
1009 [char] n of '\n' endof
1010 [char] " of [char] " endof
1016 inc-parse-idx character-type
1023 : readsymbol ( -- charlist )
1024 delim? if nil exit then
1026 nextchar inc-parse-idx character-type
1033 : readpair ( -- pairobj )
1037 nextchar [char] ) = if
1042 ." No delimiter following right paren. Aborting." cr
1051 \ Read first pair element
1056 nextchar [char] . = if
1061 ." No delimiter following '.'. Aborting." cr
1075 \ Parse a scheme expression
1111 nextchar [char] " <> if
1112 bold red ." Missing closing double-quote." reset-term cr
1130 nextchar [char] ) <> if
1131 bold red ." Missing closing paren." reset-term cr
1140 nextchar [char] ' = if
1142 quote-symbol recurse nil cons cons exit
1145 nextchar [char] ` = if
1147 quasiquote-symbol recurse nil cons cons exit
1150 nextchar [char] , = if
1152 nextchar [char] @ = if
1154 unquote-splicing-symbol recurse nil cons cons exit
1156 unquote-symbol recurse nil cons cons exit
1166 \ Anything else is parsed as a symbol
1167 readsymbol charlist>symbol
1169 \ Replace λ with lambda
1170 2dup λ-symbol objeq? if
1179 \ ---- Eval ---- {{{
1181 : self-evaluating? ( obj -- obj bool )
1182 boolean-type istype? if true exit then
1183 fixnum-type istype? if true exit then
1184 flonum-type istype? if true exit then
1185 ratnum-type istype? if true exit then
1186 character-type istype? if true exit then
1187 string-type istype? if true exit then
1188 nil-type istype? if true exit then
1189 none-type istype? if true exit then
1194 : tagged-list? ( obj tag-obj -- obj bool )
1196 pair-type istype? false = if
1202 : quote? ( obj -- obj bool )
1203 quote-symbol tagged-list? ;
1205 : quote-body ( quote-obj -- quote-body-obj )
1208 : quasiquote? ( obj -- obj bool )
1209 quasiquote-symbol tagged-list? ;
1211 : unquote? ( obj -- obj bool )
1212 unquote-symbol tagged-list? ;
1214 : unquote-splicing? ( obj -- obj bool )
1215 unquote-splicing-symbol tagged-list? ;
1217 : eval-unquote ( env obj -- res )
1221 recoverable-exception throw" no arguments to unquote."
1226 recoverable-exception throw" too many arguments to unquote."
1229 2drop car 2swap eval
1232 ( Create a new list from elements of l1 consed on to l2 )
1233 : join-lists ( l2 l1 -- l3 )
1234 nil? if 2drop exit then
1241 defer eval-quasiquote-item
1242 : eval-quasiquote-pair ( env obj -- res )
1243 2over 2over ( env obj env obj )
1245 cdr eval-quasiquote-item
1247 -2rot car ( cdritem env objcar )
1249 unquote-splicing? if
1250 eval-unquote ( cdritems caritem )
1258 eval-quasiquote-item ( cdritems caritem )
1273 pair-type istype? if
1274 eval-quasiquote-pair exit
1278 ; is eval-quasiquote-item
1280 : eval-quasiquote ( obj env -- res )
1281 2swap cdr ( env args )
1284 recoverable-exception throw" no arguments to quasiquote."
1287 2dup cdr ( env args args-cdr )
1289 recoverable-exception throw" too many arguments to quasiquote."
1292 2drop car ( env arg )
1294 eval-quasiquote-item
1297 : variable? ( obj -- obj bool )
1298 symbol-type istype? ;
1300 : definition? ( obj -- obj bool )
1301 define-symbol tagged-list? ;
1303 : make-lambda ( params body -- lambda-exp )
1304 lambda-symbol -2rot cons cons ;
1306 ( Handles iterative expansion of defines in
1307 terms of nested lambdas. Most Schemes only
1308 handle one iteration of expansion! )
1309 : definition-var-val ( obj -- var val )
1311 cdr 2dup cdr 2swap car ( val var )
1314 symbol-type istype? false =
1316 2dup cdr 2swap car ( val formals var' )
1317 -2rot 2swap ( var' formals val )
1318 make-lambda nil cons ( var' val' )
1325 : eval-definition ( obj env -- res )
1326 2dup 2rot ( env env obj )
1327 definition-var-val ( env env var val )
1328 2rot eval ( env var val )
1330 2rot ( var val env )
1336 : assignment? ( obj -- obj bool )
1337 set!-symbol tagged-list? ;
1339 : assignment-var ( obj -- var )
1342 : assignment-val ( obj -- val )
1345 : eval-assignment ( obj env -- res )
1347 2over 2over ( env obj env obj )
1348 assignment-val 2swap ( env obj valexp env )
1349 eval ( env obj val )
1351 2swap assignment-var 2swap ( env var val )
1353 2rot ( var val env )
1359 : macro-definition? ( obj -- obj bool )
1360 define-macro-symbol tagged-list? ;
1362 : macro-definition-name ( exp -- mname )
1365 : macro-definition-params ( exp -- params )
1368 : macro-definition-body ( exp -- body )
1372 : eval-define-macro ( obj env -- res )
1375 2dup macro-definition-name 2swap ( name obj )
1376 2dup macro-definition-params 2swap ( name params obj )
1377 macro-definition-body ( name params body )
1379 env obj@ ( name params body env )
1387 : if? ( obj -- obj bool )
1388 if-symbol tagged-list? ;
1390 : if-predicate ( ifobj -- pred )
1393 : if-consequent ( ifobj -- conseq )
1396 : if-alternative ( ifobj -- alt|none )
1404 : false? ( boolobj -- boolean )
1405 boolean-type istype? if
1406 false boolean-type objeq?
1412 : true? ( boolobj -- bool )
1415 : lambda? ( obj -- obj bool )
1416 lambda-symbol tagged-list? ;
1418 : lambda-parameters ( obj -- params )
1421 : lambda-body ( obj -- body )
1424 : begin? ( obj -- obj bool )
1425 begin-symbol tagged-list? ;
1427 : begin-actions ( obj -- actions )
1430 : eval-sequence ( explist env -- finalexp env )
1431 ( Evaluates all bar the final expressions in
1432 an an expression list. The final expression
1433 is returned to allow for tail optimization. )
1435 2swap ( env explist )
1437 \ Abort on empty list
1444 2dup cdr ( env explist nextexplist )
1447 -2rot car 2over ( nextexplist env exp env )
1449 2drop \ discard result
1450 2swap ( env nextexplist )
1453 2drop car 2swap ( finalexp env )
1456 : application? ( obj -- obj bool )
1459 : operator ( obj -- operator )
1462 : operands ( obj -- operands )
1465 : nooperands? ( operands -- bool )
1468 : first-operand ( operands -- operand )
1471 : rest-operands ( operands -- other-operands )
1474 : list-of-vals ( args env -- vals )
1480 2over 2over first-operand 2swap eval
1481 -2rot rest-operands 2swap recurse
1486 : procedure-params ( proc -- params )
1487 drop pair-type car ;
1489 : procedure-body ( proc -- body )
1490 drop pair-type cdr car ;
1492 : procedure-env ( proc -- body )
1493 drop pair-type cdr cdr car ;
1495 ( Ensure terminating symbol arg name is handled
1496 specially to allow for variadic procedures. )
1497 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1499 2over nil? false = if
1500 recoverable-exception throw" Too many arguments for compound procedure."
1507 symbol-type istype? if
1517 recoverable-exception throw" Too few arguments for compound procedure."
1524 recurse ( argvals argnames argvals'' argnames'' )
1525 2rot car 2swap cons ( argvals argvals'' argnames' )
1526 2rot car 2rot cons ( argnames' argvals' )
1530 : apply ( proc argvals -- result )
1532 primitive-proc-type of
1536 compound-proc-type of
1537 2dup procedure-body ( argvals proc body )
1538 -2rot 2dup procedure-params ( body argvals proc argnames )
1539 -2rot procedure-env ( body argnames argvals procenv )
1545 extend-env ( body env )
1549 R> drop ['] eval goto-deferred \ Tail call optimization
1552 recoverable-exception throw" Object not applicable."
1556 ( Simply evaluates the given procedure with expbody as its argument. )
1557 : macro-expand ( proc expbody -- result )
1559 2dup procedure-body ( expbody proc procbody )
1560 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1561 -2rot procedure-env ( procbody argnames expbody procenv )
1567 extend-env eval-sequence eval
1570 :noname ( obj env -- result )
1585 2swap eval-quasiquote
1595 2swap eval-definition
1600 2swap eval-assignment
1604 macro-definition? if
1605 2swap eval-define-macro
1621 ['] eval goto-deferred
1625 2dup lambda-parameters
1634 ['] eval goto-deferred
1639 2over 2over ( env exp env exp )
1640 operator ( env exp env opname )
1642 2dup lookup-macro nil? false = if
1643 \ Macro function evaluation
1645 ( env exp env opname mproc )
1646 2swap 2drop -2rot 2drop cdr ( env mproc body )
1651 ['] eval goto-deferred
1653 \ Regular function application
1655 2drop ( env exp env opname )
1657 2swap eval ( env exp proc )
1659 -2rot ( proc env exp )
1660 operands 2swap ( proc operands env )
1661 list-of-vals ( proc argvals )
1668 recoverable-exception throw" Tried to evaluate object with unknown type."
1673 \ ---- Print ---- {{{
1675 : printfixnum ( fixnum -- ) drop 0 .R ;
1677 : printflonum ( flonum -- ) drop f. ;
1679 : printratnum ( ratnum -- )
1681 car print ." /" cdr print
1684 : printbool ( bool -- )
1692 : printchar ( charobj -- )
1695 9 of ." #\tab" endof
1696 bl of ." #\space" endof
1697 '\n' of ." #\newline" endof
1703 : (printstring) ( stringobj -- )
1704 nil? if 2drop exit then
1708 '\n' of ." \n" drop endof
1709 [char] \ of ." \\" drop endof
1710 [char] " of [char] \ emit [char] " emit drop endof
1716 : printstring ( stringobj -- )
1721 : printsymbol ( symbolobj -- )
1722 nil-type istype? if 2drop exit then
1728 : printnil ( nilobj -- )
1731 : printpair ( pairobj -- )
1735 nil-type istype? if 2drop exit then
1736 pair-type istype? if space recurse exit then
1740 : printprim ( primobj -- )
1741 2drop ." <primitive procedure>" ;
1743 : printcomp ( primobj -- )
1744 2drop ." <compound procedure>" ;
1746 : printnone ( noneobj -- )
1747 2drop ." Unspecified return value" ;
1749 : printport ( port -- )
1753 fixnum-type istype? if printfixnum exit then
1754 flonum-type istype? if printflonum exit then
1755 ratnum-type istype? if printratnum exit then
1756 boolean-type istype? if printbool exit then
1757 character-type istype? if printchar exit then
1758 string-type istype? if printstring exit then
1759 symbol-type istype? if printsymbol exit then
1760 nil-type istype? if printnil exit then
1761 pair-type istype? if ." (" printpair ." )" exit then
1762 primitive-proc-type istype? if printprim exit then
1763 compound-proc-type istype? if printcomp exit then
1764 none-type istype? if printnone exit then
1766 recoverable-exception throw" Tried to print object with unknown type."
1771 \ ---- Garbage Collection ---- {{{
1776 variable gc-stack-depth
1779 depth gc-stack-depth !
1783 false gc-enabled ! ;
1788 : pairlike? ( obj -- obj bool )
1789 pair-type istype? if true exit then
1790 string-type istype? if true exit then
1791 symbol-type istype? if true exit then
1792 compound-proc-type istype? if true exit then
1797 : pairlike-marked? ( obj -- obj bool )
1798 over nextfrees + @ 0=
1801 : mark-pairlike ( obj -- obj )
1802 over nextfrees + 0 swap !
1811 : gc-mark-obj ( obj -- )
1813 pairlike? invert if 2drop exit then
1814 pairlike-marked? if 2drop exit then
1825 scheme-memsize nextfree !
1826 0 scheme-memsize 1- do
1827 nextfrees i + @ 0<> if
1828 nextfree @ nextfrees i + !
1834 \ Following a GC, this gives the amount of free memory
1838 nextfrees i + @ 0= if 1+ then
1842 \ Debugging word - helps spot memory that is retained
1845 nextfrees i + @ 0<> if
1857 symbol-table obj@ gc-mark-obj
1858 macro-table obj@ gc-mark-obj
1859 global-env obj@ gc-mark-obj
1861 depth gc-stack-depth @ do
1870 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1871 ; is collect-garbage
1875 \ ---- Loading files ---- {{{
1877 : charlist>cstr ( charlist addr -- n )
1879 dup 2swap ( origaddr addr charlist )
1885 drop ( origaddr addr charlist char )
1886 -rot 2swap ( origaddr charlist addr char )
1888 1+ -rot ( origaddr nextaddr charlist )
1891 2drop ( origaddr finaladdr )
1895 : load ( addr n -- finalResult )
1900 ok-symbol ( port res )
1903 2over read-port ( port res obj )
1905 2dup EOF character-type objeq? if
1906 2drop 2swap close-port
1910 2swap 2drop ( port obj )
1912 global-env obj@ eval ( port res )
1918 \ ---- Standard Library ---- {{{
1920 include scheme-primitives.4th
1922 s" scheme-library.scm" load 2drop
1928 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
1929 : repl-body ( -- bool )
1930 cr bold fg green ." > " reset-term
1934 2dup EOF character-type objeq? if
1936 bold fg blue ." Moriturus te saluto." reset-term cr
1940 global-env obj@ eval
1942 fg cyan ." ; " print reset-term
1948 cr ." Welcome to scheme.forth.jl!" cr
1949 ." Use Ctrl-D to exit." cr
1958 recoverable-exception of false endof
1959 unrecoverable-exception of true endof