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 20000 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 )
648 symbol-type istype? invert if
649 \ Early exit if argument is not a symbol
671 : make-macro ( name_symbol params body env -- )
674 2swap ( proc name_symbol )
681 2over 2over ( proc name table name table )
683 2swap 2drop ( proc table )
695 macro-table obj@ cons
704 variable stored-parse-idx
705 create parse-str 161 allot
706 variable parse-str-span
708 create parse-idx-stack 10 allot
709 variable parse-idx-sp
710 parse-idx-stack parse-idx-sp !
713 parse-idx @ parse-idx-sp @ !
718 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
722 parse-idx-sp @ @ parse-idx ! ;
726 '\n' parse-str parse-str-span @ + !
727 1 parse-str-span +! ;
730 4 parse-str parse-str-span @ + !
731 1 parse-str-span +! ;
738 current-input-port obj@ console-i/o-port obj@ objeq? if
739 parse-str 160 expect cr
740 span @ parse-str-span !
742 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
743 drop swap parse-str-span !
745 parse-str-span @ 0= and if append-eof then
756 : charavailable? ( -- bool )
757 parse-str-span @ parse-idx @ > ;
759 : nextchar ( -- char )
760 charavailable? false = if getline then
761 parse-str parse-idx @ + @ ;
764 : whitespace? ( -- bool )
776 nextchar [char] ( = or
777 nextchar [char] ) = or
780 : commentstart? ( -- bool )
781 nextchar [char] ; = ;
785 false \ Indicates whether or not we're eating a comment
788 dup whitespace? or commentstart? or
790 dup nextchar '\n' = and if
791 invert \ Stop eating comment
793 dup false = commentstart? and if
794 invert \ Begin eating comment
809 nextchar [char] - = ;
812 nextchar [char] + = ;
814 : fixnum? ( -- bool )
840 : flonum? ( -- bool )
847 \ Record starting parse idx:
848 \ Want to detect whether any characters (following +/-) were eaten.
855 [char] . nextchar = if
862 [char] e nextchar = [char] E nextchar = or if
870 drop pop-parse-idx false exit
878 \ This is a real number if characters were
879 \ eaten and the next characer is a delimiter.
880 parse-idx @ < delim? and
885 : ratnum? ( -- bool )
893 pop-parse-idx false exit
902 [char] / nextchar <> if
903 pop-parse-idx false exit
909 pop-parse-idx false exit
921 : boolean? ( -- bool )
922 nextchar [char] # <> if false exit then
929 and if pop-parse-idx false exit then
941 : str-equiv? ( str -- bool )
958 delim? false = if drop false then
963 : character? ( -- bool )
964 nextchar [char] # <> if false exit then
969 nextchar [char] \ <> if pop-parse-idx false exit then
973 S" newline" str-equiv? if pop-parse-idx true exit then
974 S" space" str-equiv? if pop-parse-idx true exit then
975 S" tab" str-equiv? if pop-parse-idx true exit then
977 charavailable? false = if pop-parse-idx false exit then
983 nextchar [char] ( = ;
985 : string? ( -- bool )
986 nextchar [char] " = ;
988 : readfixnum ( -- fixnum )
999 10 * nextchar [char] 0 - +
1008 : readflonum ( -- flonum )
1010 dup 0< swap abs i->f
1012 [char] . nextchar = if
1018 nextchar [char] 0 - i->f ( f exp d )
1019 over f/ rot f+ ( exp f' )
1020 swap 10.0 f* ( f' exp' )
1027 [char] e nextchar = [char] E nextchar = or if
1030 readfixnum drop i->f
1041 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1048 fixnum-type swap fixnum-type
1049 cons drop ratnum-type
1053 : readratnum ( -- ratnum )
1054 readfixnum inc-parse-idx readfixnum
1058 : readbool ( -- bool-obj )
1061 nextchar [char] f = if
1072 : readchar ( -- char-obj )
1076 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1077 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1078 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1080 nextchar character-type
1085 : readstring ( -- charlist )
1090 nextchar [char] " <>
1092 nextchar [char] \ = if
1095 [char] n of '\n' endof
1096 [char] " of [char] " endof
1102 inc-parse-idx character-type
1105 ( firstchar prevchar thischar )
1108 2drop 2swap 2drop 2dup ( thischar thischar )
1110 ( firstchar thischar prevchar )
1111 2over 2swap set-cdr! ( firstchar thischar )
1115 \ Discard previous character
1121 ." No delimiter following right double quote. Aborting." cr
1133 : readsymbol ( -- charlist )
1134 delim? if nil exit then
1136 nextchar inc-parse-idx character-type
1143 : readpair ( -- pairobj )
1147 nextchar [char] ) = if
1152 ." No delimiter following right paren. Aborting." cr
1161 \ Read first pair element
1166 nextchar [char] . = if
1171 ." No delimiter following '.'. Aborting." cr
1185 \ Parse a scheme expression
1220 nextchar [char] " <> if
1221 bold red ." Missing closing double-quote." reset-term cr
1239 nextchar [char] ) <> if
1240 bold red ." Missing closing paren." reset-term cr
1249 nextchar [char] ' = if
1251 quote-symbol recurse nil cons cons exit
1254 nextchar [char] ` = if
1256 quasiquote-symbol recurse nil cons cons exit
1259 nextchar [char] , = if
1261 nextchar [char] @ = if
1263 unquote-splicing-symbol recurse nil cons cons exit
1265 unquote-symbol recurse nil cons cons exit
1275 \ Anything else is parsed as a symbol
1276 readsymbol charlist>symbol
1278 \ Replace λ with lambda
1279 2dup λ-symbol objeq? if
1288 \ ---- Eval ---- {{{
1290 : self-evaluating? ( obj -- obj bool )
1291 boolean-type istype? if true exit then
1292 fixnum-type istype? if true exit then
1293 flonum-type istype? if true exit then
1294 ratnum-type istype? if true exit then
1295 character-type istype? if true exit then
1296 string-type istype? if true exit then
1297 nil-type istype? if true exit then
1298 none-type istype? if true exit then
1303 : tagged-list? ( obj tag-obj -- obj bool )
1305 pair-type istype? false = if
1311 : quote? ( obj -- obj bool )
1312 quote-symbol tagged-list? ;
1314 : quote-body ( quote-obj -- quote-body-obj )
1317 : quasiquote? ( obj -- obj bool )
1318 quasiquote-symbol tagged-list? ;
1320 : unquote? ( obj -- obj bool )
1321 unquote-symbol tagged-list? ;
1323 : unquote-splicing? ( obj -- obj bool )
1324 unquote-splicing-symbol tagged-list? ;
1326 : eval-unquote ( env obj -- res )
1330 recoverable-exception throw" no arguments to unquote."
1335 recoverable-exception throw" too many arguments to unquote."
1338 2drop car 2swap eval
1341 ( Create a new list from elements of l1 consed on to l2 )
1342 : join-lists ( l2 l1 -- l3 )
1343 nil? if 2drop exit then
1350 defer eval-quasiquote-item
1351 : eval-quasiquote-pair ( env obj -- res )
1352 2over 2over ( env obj env obj )
1354 cdr eval-quasiquote-item
1356 -2rot car ( cdritem env objcar )
1358 unquote-splicing? if
1359 eval-unquote ( cdritems caritem )
1367 eval-quasiquote-item ( cdritems caritem )
1382 pair-type istype? if
1383 eval-quasiquote-pair exit
1387 ; is eval-quasiquote-item
1389 : eval-quasiquote ( obj env -- res )
1390 2swap cdr ( env args )
1393 recoverable-exception throw" no arguments to quasiquote."
1396 2dup cdr ( env args args-cdr )
1398 recoverable-exception throw" too many arguments to quasiquote."
1401 2drop car ( env arg )
1403 eval-quasiquote-item
1406 : variable? ( obj -- obj bool )
1407 symbol-type istype? ;
1409 : definition? ( obj -- obj bool )
1410 define-symbol tagged-list? ;
1412 : make-lambda ( params body -- lambda-exp )
1413 lambda-symbol -2rot cons cons ;
1415 ( Handles iterative expansion of defines in
1416 terms of nested lambdas. Most Schemes only
1417 handle one iteration of expansion! )
1418 : definition-var-val ( obj -- var val )
1420 cdr 2dup cdr 2swap car ( val var )
1423 symbol-type istype? false =
1425 2dup cdr 2swap car ( val formals var' )
1426 -2rot 2swap ( var' formals val )
1427 make-lambda nil cons ( var' val' )
1434 : eval-definition ( obj env -- res )
1435 2dup 2rot ( env env obj )
1436 definition-var-val ( env env var val )
1437 2rot eval ( env var val )
1439 2rot ( var val env )
1445 : assignment? ( obj -- obj bool )
1446 set!-symbol tagged-list? ;
1448 : assignment-var ( obj -- var )
1451 : assignment-val ( obj -- val )
1454 : eval-assignment ( obj env -- res )
1456 2over 2over ( env obj env obj )
1457 assignment-val 2swap ( env obj valexp env )
1458 eval ( env obj val )
1460 2swap assignment-var 2swap ( env var val )
1462 2rot ( var val env )
1468 : macro-definition? ( obj -- obj bool )
1469 define-macro-symbol tagged-list? ;
1471 : macro-definition-name ( exp -- mname )
1474 : macro-definition-params ( exp -- params )
1477 : macro-definition-body ( exp -- body )
1481 : eval-define-macro ( obj env -- res )
1484 2dup macro-definition-name 2swap ( name obj )
1485 2dup macro-definition-params 2swap ( name params obj )
1486 macro-definition-body ( name params body )
1488 env obj@ ( name params body env )
1496 : if? ( obj -- obj bool )
1497 if-symbol tagged-list? ;
1499 : if-predicate ( ifobj -- pred )
1502 : if-consequent ( ifobj -- conseq )
1505 : if-alternative ( ifobj -- alt|none )
1513 : false? ( boolobj -- boolean )
1514 boolean-type istype? if
1515 false boolean-type objeq?
1521 : true? ( boolobj -- bool )
1524 : lambda? ( obj -- obj bool )
1525 lambda-symbol tagged-list? ;
1527 : lambda-parameters ( obj -- params )
1530 : lambda-body ( obj -- body )
1533 : begin? ( obj -- obj bool )
1534 begin-symbol tagged-list? ;
1536 : begin-actions ( obj -- actions )
1539 : eval-sequence ( explist env -- finalexp env )
1540 ( Evaluates all bar the final expressions in
1541 an an expression list. The final expression
1542 is returned to allow for tail optimization. )
1544 2swap ( env explist )
1546 \ Abort on empty list
1553 2dup cdr ( env explist nextexplist )
1556 -2rot car 2over ( nextexplist env exp env )
1558 2drop \ discard result
1559 2swap ( env nextexplist )
1562 2drop car 2swap ( finalexp env )
1565 : application? ( obj -- obj bool )
1568 : operator ( obj -- operator )
1571 : operands ( obj -- operands )
1574 : nooperands? ( operands -- bool )
1577 : first-operand ( operands -- operand )
1580 : rest-operands ( operands -- other-operands )
1583 : list-of-vals ( args env -- vals )
1589 2over 2over first-operand 2swap eval
1590 -2rot rest-operands 2swap recurse
1595 : procedure-params ( proc -- params )
1596 drop pair-type car ;
1598 : procedure-body ( proc -- body )
1599 drop pair-type cdr car ;
1601 : procedure-env ( proc -- body )
1602 drop pair-type cdr cdr car ;
1604 ( Ensure terminating symbol arg name is handled
1605 specially to allow for variadic procedures. )
1606 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1608 2over nil? false = if
1609 recoverable-exception throw" Too many arguments for compound procedure."
1616 symbol-type istype? if
1626 recoverable-exception throw" Too few arguments for compound procedure."
1633 recurse ( argvals argnames argvals'' argnames'' )
1634 2rot car 2swap cons ( argvals argvals'' argnames' )
1635 2rot car 2rot cons ( argnames' argvals' )
1639 : apply ( proc argvals -- result )
1641 primitive-proc-type of
1645 compound-proc-type of
1646 2dup procedure-body ( argvals proc body )
1647 -2rot 2dup procedure-params ( body argvals proc argnames )
1648 -2rot procedure-env ( body argnames argvals procenv )
1654 extend-env ( body env )
1658 R> drop ['] eval goto-deferred \ Tail call optimization
1661 recoverable-exception throw" Object not applicable."
1665 ( Simply evaluates the given procedure with expbody as its argument. )
1666 : macro-expand ( proc expbody -- result )
1668 2dup procedure-body ( expbody proc procbody )
1669 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1670 -2rot procedure-env ( procbody argnames expbody procenv )
1676 extend-env eval-sequence eval
1679 :noname ( obj env -- result )
1684 fg yellow ." Evaluating: " bold 2dup print reset-term
1685 space fg green ." PS: " bold depth . reset-term
1686 space fg blue ." RS: " bold RSP@ RSP0 - . reset-term cr
1701 2swap eval-quasiquote
1711 2swap eval-definition
1716 2swap eval-assignment
1720 macro-definition? if
1721 2swap eval-define-macro
1737 ['] eval goto-deferred
1741 2dup lambda-parameters
1750 ['] eval goto-deferred
1755 2over 2over ( env exp env exp )
1756 operator ( env exp env opname )
1758 2dup lookup-macro nil? false = if
1759 \ Macro function evaluation
1761 ( env exp env opname mproc )
1762 2swap 2drop -2rot 2drop cdr ( env mproc body )
1767 ['] eval goto-deferred
1769 \ Regular function application
1771 2drop ( env exp env opname )
1773 2swap eval ( env exp proc )
1775 -2rot ( proc env exp )
1776 operands 2swap ( proc operands env )
1777 list-of-vals ( proc argvals )
1784 recoverable-exception throw" Tried to evaluate object with unknown type."
1789 \ ---- Print ---- {{{
1791 : printfixnum ( fixnum -- ) drop 0 .R ;
1793 : printflonum ( flonum -- ) drop f. ;
1795 : printratnum ( ratnum -- )
1797 car print ." /" cdr print
1800 : printbool ( bool -- )
1808 : printchar ( charobj -- )
1811 9 of ." #\tab" endof
1812 bl of ." #\space" endof
1813 '\n' of ." #\newline" endof
1819 : (printstring) ( stringobj -- )
1820 nil? if 2drop exit then
1824 '\n' of ." \n" drop endof
1825 [char] \ of ." \\" drop endof
1826 [char] " of [char] \ emit [char] " emit drop endof
1832 : printstring ( stringobj -- )
1837 : printsymbol ( symbolobj -- )
1838 nil-type istype? if 2drop exit then
1844 : printnil ( nilobj -- )
1847 : printpair ( pairobj -- )
1851 nil-type istype? if 2drop exit then
1852 pair-type istype? if space recurse exit then
1856 : printprim ( primobj -- )
1857 2drop ." <primitive procedure>" ;
1859 : printcomp ( primobj -- )
1860 2drop ." <compound procedure>" ;
1862 : printnone ( noneobj -- )
1863 2drop ." Unspecified return value" ;
1865 : printport ( port -- )
1869 fixnum-type istype? if printfixnum exit then
1870 flonum-type istype? if printflonum exit then
1871 ratnum-type istype? if printratnum exit then
1872 boolean-type istype? if printbool exit then
1873 character-type istype? if printchar exit then
1874 string-type istype? if printstring exit then
1875 symbol-type istype? if printsymbol exit then
1876 nil-type istype? if printnil exit then
1877 pair-type istype? if ." (" printpair ." )" exit then
1878 primitive-proc-type istype? if printprim exit then
1879 compound-proc-type istype? if printcomp exit then
1880 none-type istype? if printnone exit then
1881 port-type istype? if printport exit then
1883 recoverable-exception throw" Tried to print object with unknown type."
1888 \ ---- Garbage Collection ---- {{{
1893 variable gc-stack-depth
1896 depth gc-stack-depth !
1900 false gc-enabled ! ;
1905 : pairlike? ( obj -- obj bool )
1906 pair-type istype? if true exit then
1907 string-type istype? if true exit then
1908 symbol-type istype? if true exit then
1909 compound-proc-type istype? if true exit then
1910 port-type istype? if true exit then
1915 : pairlike-marked? ( obj -- obj bool )
1916 over nextfrees + @ 0=
1919 : mark-pairlike ( obj -- obj )
1920 over nextfrees + 0 swap !
1929 : gc-mark-obj ( obj -- )
1931 pairlike? invert if 2drop exit then
1932 pairlike-marked? if 2drop exit then
1943 scheme-memsize nextfree !
1944 0 scheme-memsize 1- do
1945 nextfrees i + @ 0<> if
1946 nextfree @ nextfrees i + !
1952 \ Following a GC, this gives the amount of free memory
1956 nextfrees i + @ 0= if 1+ then
1960 \ Debugging word - helps spot memory that is retained
1963 nextfrees i + @ 0<> if
1975 symbol-table obj@ gc-mark-obj
1976 macro-table obj@ gc-mark-obj
1977 console-i/o-port obj@ gc-mark-obj
1978 global-env obj@ gc-mark-obj
1980 depth gc-stack-depth @ do
1989 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1990 ; is collect-garbage
1994 \ ---- Loading files ---- {{{
1996 : load ( addr n -- finalResult )
2001 ok-symbol ( port res )
2004 2over read-port ( port res obj )
2006 2dup EOF character-type objeq? if
2007 2drop 2swap close-port
2011 2swap 2drop ( port obj )
2013 global-env obj@ eval ( port res )
2019 \ ---- Standard Library ---- {{{
2021 include scheme-primitives.4th
2023 s" scheme-library.scm" load 2drop
2029 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2030 : repl-body ( -- bool )
2031 cr bold fg green ." > " reset-term
2035 2dup EOF character-type objeq? if
2037 bold fg blue ." Moriturus te saluto." reset-term cr
2041 global-env obj@ eval
2043 fg cyan ." ; " print reset-term
2053 \ Display welcome message
2054 welcome-symbol nil cons global-env obj@ eval 2drop
2059 recoverable-exception of false endof
2060 unrecoverable-exception of true endof