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
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 \ ---- 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
277 create-symbol eof eof-symbol
279 \ Symbol to be bound to welcome message procedure by library
280 create-symbol welcome welcome-symbol
284 \ ---- Port I/O ---- {{{
286 ( Ports are pairs with the fid in the car and the peek buffer in the cdr. )
288 : fileport>fid ( fileport -- fid )
289 drop pair-type car drop ;
291 : get-last-peek ( fileport -- char/nil )
294 : set-last-peek ( char/nil fileport -- )
295 drop pair-type set-cdr!
298 : fid>fileport ( fid -- fileport )
299 fixnum-type nil cons drop port-type ;
301 : open-input-file ( addr n -- fileport )
302 r/o open-file drop fid>fileport
305 : close-port ( fileport -- )
306 fileport>fid close-file drop
309 objvar console-i/o-port
310 0 fixnum-type nil cons drop port-type console-i/o-port obj!
312 objvar current-input-port
313 console-i/o-port obj@ current-input-port obj!
315 : read-char ( port -- char )
316 2dup get-last-peek nil? if
318 2dup console-i/o-port obj@ objeq? if
322 fileport>fid pad 1 rot read-file 0= if
333 : peek-char ( port -- char )
334 2dup get-last-peek nil? if
336 2dup 2rot set-last-peek
342 variable read-line-buffer-span
343 variable read-line-buffer-offset
345 ( Hack to save original read-line while we transition to new one. )
346 : orig-read-line immediate
349 : read-line ( port -- string )
354 0 read-line-buffer-offset !
356 2over nil 2swap set-last-peek
358 2drop nil nil cons exit
361 1 read-line-buffer-offset !
365 2dup console-i/o-port obj@ objeq? if
367 pad read-line-buffer-offset @ + 200 expect cr
368 span @ read-line-buffer-offset @ + read-line-buffer-span !
370 pad read-line-buffer-offset @ + 200 2over fileport>fid orig-read-line
371 drop swap read-line-buffer-offset @ + read-line-buffer-span !
377 read-line-buffer-span @ 0>
379 pad read-line-buffer-span @ 1- + @ character-type 2swap cons
380 -1 read-line-buffer-span +!
384 nil cons drop string-type
390 : read-port ( fileport -- obj )
391 current-input-port obj!
394 : read-console ( -- obj )
395 console-i/o-port obj@ read-port ;
399 \ ---- Environments ---- {{{
401 : enclosing-env ( env -- env )
404 : first-frame ( env -- frame )
407 : make-frame ( vars vals -- frame )
410 : frame-vars ( frame -- vars )
413 : frame-vals ( frame -- vals )
416 : add-binding ( var val frame -- )
417 2swap 2over frame-vals cons
419 2swap 2over frame-vars cons
423 : extend-env ( vars vals env -- env )
433 : get-vars-vals-frame ( var frame -- bool )
434 2dup frame-vars vars obj!
438 vars obj@ nil objeq? false =
440 2dup vars obj@ car objeq? if
445 vars obj@ cdr vars obj!
446 vals obj@ cdr vals obj!
452 : get-vars-vals ( var env -- vars? vals? bool )
457 2over 2over first-frame
458 get-vars-vals-frame if
460 vars obj@ vals obj@ true
474 : lookup-var ( var env -- val )
478 recoverable-exception throw" Tried to read unbound variable."
482 : set-var ( var val env -- )
483 >R >R 2swap R> R> ( val var env )
485 2swap 2drop ( val vals )
488 recoverable-exception throw" Tried to set unbound variable."
494 : define-var ( var val env -- )
497 2over env obj@ ( var val var env )
499 2swap 2drop ( var val vals )
504 first-frame ( var val frame )
511 : make-procedure ( params body env -- proc )
514 drop compound-proc-type
518 nil nil nil extend-env
523 \ ---- Primitives ---- {{{
525 : make-primitive ( cfa -- )
532 rot primitive-proc-type ( var prim )
533 global-env obj@ define-var
536 : ensure-arg-count ( args n -- )
538 drop nil objeq? false = if
539 recoverable-exception throw" Too many arguments for primitive procedure."
543 recoverable-exception throw" Too few arguments for primitive procedure."
550 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
552 drop nil objeq? false = if
553 recoverable-exception throw" Too many arguments for primitive procedure."
557 recoverable-exception throw" Too few arguments for primitive procedure."
560 2dup cdr 2swap car ( ... t1 n args' arg1 )
561 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
563 recoverable-exception throw" Incorrect type for primitive procedure."
571 : push-args-to-stack ( args -- arg1 arg2 ... argn )
581 : add-fa-checks ( cfa n -- cfa' )
582 here current @ 1+ dup @ , !
586 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
587 ['] push-args-to-stack ,
588 ['] lit , , ['] execute ,
592 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
593 here current @ 1+ dup @ , !
600 dup ( cfa t1 t2 ... tn n m )
605 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
611 ['] lit , , ['] ensure-arg-type-and-count ,
613 ['] push-args-to-stack ,
614 ['] lit , , ['] execute ,
620 : make-fa-primitive ( cfa n -- )
621 add-fa-checks make-primitive ;
623 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
624 add-fa-type-checks make-primitive ;
627 bold fg red ." Incorrect argument type." reset-term cr
631 : ensure-arg-type ( arg type -- arg )
633 recoverable-exception throw" Incorrect argument type for primitive procedure."
640 \ ---- Macros ---- {{{
644 ( Look up macro in macro table. Returns nil if
646 : lookup-macro ( name_symbol -- proc )
665 : make-macro ( name_symbol params body env -- )
668 2swap ( proc name_symbol )
675 2over 2over ( proc name table name table )
677 2swap 2drop ( proc table )
689 macro-table obj@ cons
698 variable stored-parse-idx
699 create parse-str 161 allot
700 variable parse-str-span
702 create parse-idx-stack 10 allot
703 variable parse-idx-sp
704 parse-idx-stack parse-idx-sp !
707 parse-idx @ parse-idx-sp @ !
712 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
716 parse-idx-sp @ @ parse-idx ! ;
720 '\n' parse-str parse-str-span @ + !
721 1 parse-str-span +! ;
724 4 parse-str parse-str-span @ + !
725 1 parse-str-span +! ;
732 current-input-port obj@ console-i/o-port obj@ objeq? if
733 parse-str 160 expect cr
734 span @ parse-str-span !
736 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
737 drop swap parse-str-span !
739 parse-str-span @ 0= and if append-eof then
750 : charavailable? ( -- bool )
751 parse-str-span @ parse-idx @ > ;
753 : nextchar ( -- char )
754 charavailable? false = if getline then
755 parse-str parse-idx @ + @ ;
758 : whitespace? ( -- bool )
770 nextchar [char] ( = or
771 nextchar [char] ) = or
774 : commentstart? ( -- bool )
775 nextchar [char] ; = ;
779 false \ Indicates whether or not we're eating a comment
782 dup whitespace? or commentstart? or
784 dup nextchar '\n' = and if
785 invert \ Stop eating comment
787 dup false = commentstart? and if
788 invert \ Begin eating comment
803 nextchar [char] - = ;
806 nextchar [char] + = ;
808 : fixnum? ( -- bool )
834 : flonum? ( -- bool )
841 \ Record starting parse idx:
842 \ Want to detect whether any characters (following +/-) were eaten.
849 [char] . nextchar = if
856 [char] e nextchar = [char] E nextchar = or if
864 drop pop-parse-idx false exit
872 \ This is a real number if characters were
873 \ eaten and the next characer is a delimiter.
874 parse-idx @ < delim? and
879 : ratnum? ( -- bool )
887 pop-parse-idx false exit
896 [char] / nextchar <> if
897 pop-parse-idx false exit
903 pop-parse-idx false exit
915 : boolean? ( -- bool )
916 nextchar [char] # <> if false exit then
923 and if pop-parse-idx false exit then
935 : str-equiv? ( str -- bool )
952 delim? false = if drop false then
957 : character? ( -- bool )
958 nextchar [char] # <> if false exit then
963 nextchar [char] \ <> if pop-parse-idx false exit then
967 S" newline" str-equiv? if pop-parse-idx true exit then
968 S" space" str-equiv? if pop-parse-idx true exit then
969 S" tab" str-equiv? if pop-parse-idx true exit then
971 charavailable? false = if pop-parse-idx false exit then
977 nextchar [char] ( = ;
979 : string? ( -- bool )
980 nextchar [char] " = ;
982 : readfixnum ( -- fixnum )
993 10 * nextchar [char] 0 - +
1002 : readflonum ( -- flonum )
1004 dup 0< swap abs i->f
1006 [char] . nextchar = if
1012 nextchar [char] 0 - i->f ( f exp d )
1013 over f/ rot f+ ( exp f' )
1014 swap 10.0 f* ( f' exp' )
1021 [char] e nextchar = [char] E nextchar = or if
1024 readfixnum drop i->f
1035 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1042 fixnum-type swap fixnum-type
1043 cons drop ratnum-type
1047 : readratnum ( -- ratnum )
1048 readfixnum inc-parse-idx readfixnum
1052 : readbool ( -- bool-obj )
1055 nextchar [char] f = if
1066 : readchar ( -- char-obj )
1070 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1071 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1072 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1074 nextchar character-type
1079 : readstring ( -- charlist )
1084 nextchar [char] " <>
1086 nextchar [char] \ = if
1089 [char] n of '\n' endof
1090 [char] " of [char] " endof
1096 inc-parse-idx character-type
1099 ( firstchar prevchar thischar )
1102 2drop 2swap 2drop 2dup ( thischar thischar )
1104 ( firstchar thischar prevchar )
1105 2over 2swap set-cdr! ( firstchar thischar )
1109 \ Discard previous character
1115 ." No delimiter following right double quote. Aborting." cr
1127 : readsymbol ( -- charlist )
1128 delim? if nil exit then
1130 nextchar inc-parse-idx character-type
1137 : readpair ( -- pairobj )
1141 nextchar [char] ) = if
1146 ." No delimiter following right paren. Aborting." cr
1155 \ Read first pair element
1160 nextchar [char] . = if
1165 ." No delimiter following '.'. Aborting." cr
1179 \ Parse a scheme expression
1214 nextchar [char] " <> if
1215 bold red ." Missing closing double-quote." reset-term cr
1233 nextchar [char] ) <> if
1234 bold red ." Missing closing paren." reset-term cr
1243 nextchar [char] ' = if
1245 quote-symbol recurse nil cons cons exit
1248 nextchar [char] ` = if
1250 quasiquote-symbol recurse nil cons cons exit
1253 nextchar [char] , = if
1255 nextchar [char] @ = if
1257 unquote-splicing-symbol recurse nil cons cons exit
1259 unquote-symbol recurse nil cons cons exit
1269 \ Anything else is parsed as a symbol
1270 readsymbol charlist>symbol
1272 \ Replace λ with lambda
1273 2dup λ-symbol objeq? if
1282 \ ---- Eval ---- {{{
1284 : self-evaluating? ( obj -- obj bool )
1285 boolean-type istype? if true exit then
1286 fixnum-type istype? if true exit then
1287 flonum-type istype? if true exit then
1288 ratnum-type istype? if true exit then
1289 character-type istype? if true exit then
1290 string-type istype? if true exit then
1291 nil-type istype? if true exit then
1292 none-type istype? if true exit then
1297 : tagged-list? ( obj tag-obj -- obj bool )
1299 pair-type istype? false = if
1305 : quote? ( obj -- obj bool )
1306 quote-symbol tagged-list? ;
1308 : quote-body ( quote-obj -- quote-body-obj )
1311 : quasiquote? ( obj -- obj bool )
1312 quasiquote-symbol tagged-list? ;
1314 : unquote? ( obj -- obj bool )
1315 unquote-symbol tagged-list? ;
1317 : unquote-splicing? ( obj -- obj bool )
1318 unquote-splicing-symbol tagged-list? ;
1320 : eval-unquote ( env obj -- res )
1324 recoverable-exception throw" no arguments to unquote."
1329 recoverable-exception throw" too many arguments to unquote."
1332 2drop car 2swap eval
1335 ( Create a new list from elements of l1 consed on to l2 )
1336 : join-lists ( l2 l1 -- l3 )
1337 nil? if 2drop exit then
1344 defer eval-quasiquote-item
1345 : eval-quasiquote-pair ( env obj -- res )
1346 2over 2over ( env obj env obj )
1348 cdr eval-quasiquote-item
1350 -2rot car ( cdritem env objcar )
1352 unquote-splicing? if
1353 eval-unquote ( cdritems caritem )
1361 eval-quasiquote-item ( cdritems caritem )
1376 pair-type istype? if
1377 eval-quasiquote-pair exit
1381 ; is eval-quasiquote-item
1383 : eval-quasiquote ( obj env -- res )
1384 2swap cdr ( env args )
1387 recoverable-exception throw" no arguments to quasiquote."
1390 2dup cdr ( env args args-cdr )
1392 recoverable-exception throw" too many arguments to quasiquote."
1395 2drop car ( env arg )
1397 eval-quasiquote-item
1400 : variable? ( obj -- obj bool )
1401 symbol-type istype? ;
1403 : definition? ( obj -- obj bool )
1404 define-symbol tagged-list? ;
1406 : make-lambda ( params body -- lambda-exp )
1407 lambda-symbol -2rot cons cons ;
1409 ( Handles iterative expansion of defines in
1410 terms of nested lambdas. Most Schemes only
1411 handle one iteration of expansion! )
1412 : definition-var-val ( obj -- var val )
1414 cdr 2dup cdr 2swap car ( val var )
1417 symbol-type istype? false =
1419 2dup cdr 2swap car ( val formals var' )
1420 -2rot 2swap ( var' formals val )
1421 make-lambda nil cons ( var' val' )
1428 : eval-definition ( obj env -- res )
1429 2dup 2rot ( env env obj )
1430 definition-var-val ( env env var val )
1431 2rot eval ( env var val )
1433 2rot ( var val env )
1439 : assignment? ( obj -- obj bool )
1440 set!-symbol tagged-list? ;
1442 : assignment-var ( obj -- var )
1445 : assignment-val ( obj -- val )
1448 : eval-assignment ( obj env -- res )
1450 2over 2over ( env obj env obj )
1451 assignment-val 2swap ( env obj valexp env )
1452 eval ( env obj val )
1454 2swap assignment-var 2swap ( env var val )
1456 2rot ( var val env )
1462 : macro-definition? ( obj -- obj bool )
1463 define-macro-symbol tagged-list? ;
1465 : macro-definition-name ( exp -- mname )
1468 : macro-definition-params ( exp -- params )
1471 : macro-definition-body ( exp -- body )
1475 : eval-define-macro ( obj env -- res )
1478 2dup macro-definition-name 2swap ( name obj )
1479 2dup macro-definition-params 2swap ( name params obj )
1480 macro-definition-body ( name params body )
1482 env obj@ ( name params body env )
1490 : if? ( obj -- obj bool )
1491 if-symbol tagged-list? ;
1493 : if-predicate ( ifobj -- pred )
1496 : if-consequent ( ifobj -- conseq )
1499 : if-alternative ( ifobj -- alt|none )
1507 : false? ( boolobj -- boolean )
1508 boolean-type istype? if
1509 false boolean-type objeq?
1515 : true? ( boolobj -- bool )
1518 : lambda? ( obj -- obj bool )
1519 lambda-symbol tagged-list? ;
1521 : lambda-parameters ( obj -- params )
1524 : lambda-body ( obj -- body )
1527 : begin? ( obj -- obj bool )
1528 begin-symbol tagged-list? ;
1530 : begin-actions ( obj -- actions )
1533 : eval-sequence ( explist env -- finalexp env )
1534 ( Evaluates all bar the final expressions in
1535 an an expression list. The final expression
1536 is returned to allow for tail optimization. )
1538 2swap ( env explist )
1540 \ Abort on empty list
1547 2dup cdr ( env explist nextexplist )
1550 -2rot car 2over ( nextexplist env exp env )
1552 2drop \ discard result
1553 2swap ( env nextexplist )
1556 2drop car 2swap ( finalexp env )
1559 : application? ( obj -- obj bool )
1562 : operator ( obj -- operator )
1565 : operands ( obj -- operands )
1568 : nooperands? ( operands -- bool )
1571 : first-operand ( operands -- operand )
1574 : rest-operands ( operands -- other-operands )
1577 : list-of-vals ( args env -- vals )
1583 2over 2over first-operand 2swap eval
1584 -2rot rest-operands 2swap recurse
1589 : procedure-params ( proc -- params )
1590 drop pair-type car ;
1592 : procedure-body ( proc -- body )
1593 drop pair-type cdr car ;
1595 : procedure-env ( proc -- body )
1596 drop pair-type cdr cdr car ;
1598 ( Ensure terminating symbol arg name is handled
1599 specially to allow for variadic procedures. )
1600 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1602 2over nil? false = if
1603 recoverable-exception throw" Too many arguments for compound procedure."
1610 symbol-type istype? if
1620 recoverable-exception throw" Too few arguments for compound procedure."
1627 recurse ( argvals argnames argvals'' argnames'' )
1628 2rot car 2swap cons ( argvals argvals'' argnames' )
1629 2rot car 2rot cons ( argnames' argvals' )
1633 : apply ( proc argvals -- result )
1635 primitive-proc-type of
1639 compound-proc-type of
1640 2dup procedure-body ( argvals proc body )
1641 -2rot 2dup procedure-params ( body argvals proc argnames )
1642 -2rot procedure-env ( body argnames argvals procenv )
1648 extend-env ( body env )
1652 R> drop ['] eval goto-deferred \ Tail call optimization
1655 recoverable-exception throw" Object not applicable."
1659 ( Simply evaluates the given procedure with expbody as its argument. )
1660 : macro-expand ( proc expbody -- result )
1662 2dup procedure-body ( expbody proc procbody )
1663 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1664 -2rot procedure-env ( procbody argnames expbody procenv )
1670 extend-env eval-sequence eval
1673 :noname ( obj env -- result )
1688 2swap eval-quasiquote
1698 2swap eval-definition
1703 2swap eval-assignment
1707 macro-definition? if
1708 2swap eval-define-macro
1724 ['] eval goto-deferred
1728 2dup lambda-parameters
1737 ['] eval goto-deferred
1742 2over 2over ( env exp env exp )
1743 operator ( env exp env opname )
1745 2dup lookup-macro nil? false = if
1746 \ Macro function evaluation
1748 ( env exp env opname mproc )
1749 2swap 2drop -2rot 2drop cdr ( env mproc body )
1754 ['] eval goto-deferred
1756 \ Regular function application
1758 2drop ( env exp env opname )
1760 2swap eval ( env exp proc )
1762 -2rot ( proc env exp )
1763 operands 2swap ( proc operands env )
1764 list-of-vals ( proc argvals )
1771 recoverable-exception throw" Tried to evaluate object with unknown type."
1776 \ ---- Print ---- {{{
1778 : printfixnum ( fixnum -- ) drop 0 .R ;
1780 : printflonum ( flonum -- ) drop f. ;
1782 : printratnum ( ratnum -- )
1784 car print ." /" cdr print
1787 : printbool ( bool -- )
1795 : printchar ( charobj -- )
1798 9 of ." #\tab" endof
1799 bl of ." #\space" endof
1800 '\n' of ." #\newline" endof
1806 : (printstring) ( stringobj -- )
1807 nil? if 2drop exit then
1811 '\n' of ." \n" drop endof
1812 [char] \ of ." \\" drop endof
1813 [char] " of [char] \ emit [char] " emit drop endof
1819 : printstring ( stringobj -- )
1824 : printsymbol ( symbolobj -- )
1825 nil-type istype? if 2drop exit then
1831 : printnil ( nilobj -- )
1834 : printpair ( pairobj -- )
1838 nil-type istype? if 2drop exit then
1839 pair-type istype? if space recurse exit then
1843 : printprim ( primobj -- )
1844 2drop ." <primitive procedure>" ;
1846 : printcomp ( primobj -- )
1847 2drop ." <compound procedure>" ;
1849 : printnone ( noneobj -- )
1850 2drop ." Unspecified return value" ;
1852 : printport ( port -- )
1856 fixnum-type istype? if printfixnum exit then
1857 flonum-type istype? if printflonum exit then
1858 ratnum-type istype? if printratnum exit then
1859 boolean-type istype? if printbool exit then
1860 character-type istype? if printchar exit then
1861 string-type istype? if printstring exit then
1862 symbol-type istype? if printsymbol exit then
1863 nil-type istype? if printnil exit then
1864 pair-type istype? if ." (" printpair ." )" exit then
1865 primitive-proc-type istype? if printprim exit then
1866 compound-proc-type istype? if printcomp exit then
1867 none-type istype? if printnone exit then
1868 port-type istype? if printport exit then
1870 recoverable-exception throw" Tried to print object with unknown type."
1875 \ ---- Garbage Collection ---- {{{
1880 variable gc-stack-depth
1883 depth gc-stack-depth !
1887 false gc-enabled ! ;
1892 : pairlike? ( obj -- obj bool )
1893 pair-type istype? if true exit then
1894 string-type istype? if true exit then
1895 symbol-type istype? if true exit then
1896 compound-proc-type istype? if true exit then
1897 port-type istype? if true exit then
1902 : pairlike-marked? ( obj -- obj bool )
1903 over nextfrees + @ 0=
1906 : mark-pairlike ( obj -- obj )
1907 over nextfrees + 0 swap !
1916 : gc-mark-obj ( obj -- )
1918 pairlike? invert if 2drop exit then
1919 pairlike-marked? if 2drop exit then
1930 scheme-memsize nextfree !
1931 0 scheme-memsize 1- do
1932 nextfrees i + @ 0<> if
1933 nextfree @ nextfrees i + !
1939 \ Following a GC, this gives the amount of free memory
1943 nextfrees i + @ 0= if 1+ then
1947 \ Debugging word - helps spot memory that is retained
1950 nextfrees i + @ 0<> if
1962 symbol-table obj@ gc-mark-obj
1963 macro-table obj@ gc-mark-obj
1964 console-i/o-port obj@ gc-mark-obj
1965 global-env obj@ gc-mark-obj
1967 depth gc-stack-depth @ do
1976 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1977 ; is collect-garbage
1981 \ ---- Loading files ---- {{{
1983 : load ( addr n -- finalResult )
1988 ok-symbol ( port res )
1991 2over read-port ( port res obj )
1993 2dup EOF character-type objeq? if
1994 2drop 2swap close-port
1998 2swap 2drop ( port obj )
2000 global-env obj@ eval ( port res )
2006 \ ---- Standard Library ---- {{{
2008 include scheme-primitives.4th
2010 s" scheme-library.scm" load 2drop
2016 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2017 : repl-body ( -- bool )
2018 cr bold fg green ." > " reset-term
2022 2dup EOF character-type objeq? if
2024 bold fg blue ." Moriturus te saluto." reset-term cr
2028 global-env obj@ eval
2030 fg cyan ." ; " print reset-term
2041 \ Display welcome message
2042 welcome-symbol nil cons global-env obj@ eval 2drop
2047 recoverable-exception of false endof
2048 unrecoverable-exception of true endof