4 include term-colours.4th
6 include catch-throw.4th
31 make-type boolean-type
32 make-type character-type
38 make-type primitive-proc-type
39 make-type compound-proc-type
41 : istype? ( obj type -- obj bool )
46 \ ---- Exceptions ---- {{{
48 variable nextexception
51 create nextexception @ ,
60 make-exception recoverable-exception
61 make-exception unrecoverable-exception
63 : throw reset-term throw ;
67 \ ---- List-structured memory ---- {{{
69 20000 constant scheme-memsize
71 create car-cells scheme-memsize allot
72 create car-type-cells scheme-memsize allot
73 create cdr-cells scheme-memsize allot
74 create cdr-type-cells scheme-memsize allot
76 create nextfrees scheme-memsize allot
87 nextfrees nextfree @ + @
90 nextfree @ scheme-memsize >= if
94 nextfree @ scheme-memsize >= if
95 except-message: ." Out of memory!" unrecoverable-exception throw
99 : cons ( car-obj cdr-obj -- pair-obj )
100 cdr-type-cells nextfree @ + !
101 cdr-cells nextfree @ + !
102 car-type-cells nextfree @ + !
103 car-cells nextfree @ + !
109 : car ( pair-obj -- car-obj )
111 dup car-cells + @ swap
115 : cdr ( pair-obj -- car-obj )
117 dup cdr-cells + @ swap
121 : set-car! ( obj pair-obj -- )
123 rot swap car-type-cells + !
127 : set-cdr! ( obj pair-obj -- )
129 rot swap cdr-type-cells + !
134 : nil? nil-type istype? ;
137 : none? none-type istype? ;
139 : objvar create nil swap , , ;
141 : value@ ( objvar -- val ) @ ;
142 : type@ ( objvar -- type ) 1+ @ ;
143 : value! ( newval objvar -- ) ! ;
144 : type! ( newtype objvar -- ) 1+ ! ;
145 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
146 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
148 : objeq? ( obj obj -- bool )
151 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
152 >R >R ( a1 a2 b1 b2 )
153 2swap ( b1 b2 a1 a2 )
154 R> R> ( b1 b2 a1 a2 c1 c2 )
158 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
159 2swap ( a1 a2 c1 c2 b1 b2 )
160 >R >R ( a1 a2 c1 c2 )
161 2swap ( c1 c2 a1 a2 )
167 \ ---- Pre-defined symbols ---- {{{
171 : duplicate-charlist ( charlist -- copy )
173 2dup car 2swap cdr recurse cons
176 : charlist-equiv ( charlist charlist -- bool )
185 2drop 2drop true exit
187 2drop 2drop false exit
192 2drop 2drop false exit
199 car drop -rot car drop = if
200 cdr 2swap cdr recurse
206 : charlist>symbol ( charlist -- symbol-obj )
225 drop symbol-type 2dup
226 symbol-table obj@ cons
231 : cstr>charlist ( addr n -- charlist )
235 2dup drop @ character-type 2swap
243 : create-symbol ( -- )
251 does> dup @ swap 1+ @
254 create-symbol quote quote-symbol
255 create-symbol quasiquote quasiquote-symbol
256 create-symbol unquote unquote-symbol
257 create-symbol unquote-splicing unquote-splicing-symbol
258 create-symbol define define-symbol
259 create-symbol define-macro define-macro-symbol
260 create-symbol set! set!-symbol
261 create-symbol ok ok-symbol
262 create-symbol if if-symbol
263 create-symbol lambda lambda-symbol
264 create-symbol λ λ-symbol
265 create-symbol eof eof-symbol
266 create-symbol no-match no-match-symbol
268 \ Symbol to be bound to welcome message procedure by library
269 create-symbol welcome welcome-symbol
273 \ ---- Port I/O ---- {{{
275 ( Ports are pairs with the fid in the car and the peek buffer in the cdr. )
277 : fileport>fid ( fileport -- fid )
278 drop pair-type car drop ;
280 : get-last-peek ( fileport -- char/nil )
283 : set-last-peek ( char/nil fileport -- )
284 drop pair-type set-cdr!
287 : fid>fileport ( fid -- fileport )
288 fixnum-type nil cons drop port-type ;
290 : open-input-file ( addr n -- fileport )
291 r/o open-file drop fid>fileport
294 : close-port ( fileport -- )
295 fileport>fid close-file drop
298 objvar console-i/o-port
299 0 fixnum-type nil cons drop port-type console-i/o-port obj!
301 objvar current-input-port
302 console-i/o-port obj@ current-input-port obj!
304 : read-char ( port -- char )
305 2dup get-last-peek nil? if
307 2dup console-i/o-port obj@ objeq? if
311 fileport>fid pad 1 rot read-file 0= if
322 : peek-char ( port -- char )
323 2dup get-last-peek nil? if
325 2dup 2rot set-last-peek
331 variable read-line-buffer-span
332 variable read-line-buffer-offset
334 ( Hack to save original read-line while we transition to new one. )
335 : orig-read-line immediate
338 : read-line ( port -- string )
343 0 read-line-buffer-offset !
345 2over nil 2swap set-last-peek
347 2drop nil nil cons exit
350 1 read-line-buffer-offset !
354 2dup console-i/o-port obj@ objeq? if
356 pad read-line-buffer-offset @ + 200 expect cr
357 span @ read-line-buffer-offset @ + read-line-buffer-span !
359 pad read-line-buffer-offset @ + 200 2over fileport>fid orig-read-line
360 drop swap read-line-buffer-offset @ + read-line-buffer-span !
366 read-line-buffer-span @ 0>
368 pad read-line-buffer-span @ 1- + @ character-type 2swap cons
369 -1 read-line-buffer-span +!
373 nil cons drop string-type
379 : read-port ( fileport -- obj )
380 current-input-port obj!
383 : read-console ( -- obj )
384 console-i/o-port obj@ read-port ;
388 \ ---- Environments ---- {{{
390 : enclosing-env ( env -- env )
393 : first-frame ( env -- frame )
396 : make-frame ( vars vals -- frame )
399 : frame-vars ( frame -- vars )
402 : frame-vals ( frame -- vals )
405 : add-binding ( var val frame -- )
406 2swap 2over frame-vals cons
408 2swap 2over frame-vars cons
412 : extend-env ( vars vals env -- env )
422 : get-vars-vals-frame ( var frame -- bool )
423 2dup frame-vars vars obj!
427 vars obj@ nil objeq? false =
429 2dup vars obj@ car objeq? if
434 vars obj@ cdr vars obj!
435 vals obj@ cdr vals obj!
441 : get-vars-vals ( var env -- vars? vals? bool )
446 2over 2over first-frame
447 get-vars-vals-frame if
449 vars obj@ vals obj@ true
465 : lookup-var ( var env -- val )
470 except-message: ." tried to read unbound variable '" var obj@ print ." '." recoverable-exception throw
474 : set-var ( var val env -- )
475 >R >R 2swap R> R> ( val var env )
478 2swap 2drop ( val vals )
481 except-message: ." tried to set unbound variable '" var obj@ print ." '." recoverable-exception throw
489 : define-var ( var val env -- )
492 2over env obj@ ( var val var env )
494 2swap 2drop ( var val vals )
499 first-frame ( var val frame )
506 : make-procedure ( params body env -- proc )
509 drop compound-proc-type
513 nil nil nil extend-env
518 \ ---- Primitives ---- {{{
520 : make-primitive ( cfa -- )
527 rot primitive-proc-type ( var prim )
528 global-env obj@ define-var
531 : ensure-arg-count ( args n -- )
533 drop nil objeq? false = if
534 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
538 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
545 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
547 drop nil objeq? false = if
548 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
552 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
555 2dup cdr 2swap car ( ... t1 n args' arg1 )
556 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
558 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
566 : push-args-to-stack ( args -- arg1 arg2 ... argn )
576 : add-fa-checks ( cfa n -- cfa' )
577 here current @ 1+ dup @ , !
581 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
582 ['] push-args-to-stack ,
583 ['] lit , , ['] execute ,
587 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
588 here current @ 1+ dup @ , !
595 dup ( cfa t1 t2 ... tn n m )
600 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
606 ['] lit , , ['] ensure-arg-type-and-count ,
608 ['] push-args-to-stack ,
609 ['] lit , , ['] execute ,
615 : make-fa-primitive ( cfa n -- )
616 add-fa-checks make-primitive ;
618 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
619 add-fa-type-checks make-primitive ;
622 bold fg red ." Incorrect argument type." reset-term cr
626 : ensure-arg-type ( arg type -- arg )
628 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
635 \ ---- Macros ---- {{{
639 ( Look up macro in macro table. Returns nil if
641 : lookup-macro ( name_symbol -- proc )
643 symbol-type istype? invert if
644 \ Early exit if argument is not a symbol
666 : make-macro ( name_symbol params body env -- )
669 2swap ( proc name_symbol )
676 2over 2over ( proc name table name table )
678 2swap 2drop ( proc table )
690 macro-table obj@ cons
699 variable stored-parse-idx
700 create parse-str 161 allot
701 variable parse-str-span
703 create parse-idx-stack 10 allot
704 variable parse-idx-sp
705 parse-idx-stack parse-idx-sp !
708 parse-idx @ parse-idx-sp @ !
713 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
717 parse-idx-sp @ @ parse-idx ! ;
721 '\n' parse-str parse-str-span @ + !
722 1 parse-str-span +! ;
725 4 parse-str parse-str-span @ + !
726 1 parse-str-span +! ;
733 current-input-port obj@ console-i/o-port obj@ objeq? if
734 parse-str 160 expect cr
735 span @ parse-str-span !
737 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
738 drop swap parse-str-span !
740 parse-str-span @ 0= and if append-eof then
751 : charavailable? ( -- bool )
752 parse-str-span @ parse-idx @ > ;
754 : nextchar ( -- char )
755 charavailable? false = if getline then
756 parse-str parse-idx @ + @ ;
759 : whitespace? ( -- bool )
771 nextchar [char] ( = or
772 nextchar [char] ) = or
775 : commentstart? ( -- bool )
776 nextchar [char] ; = ;
780 false \ Indicates whether or not we're eating a comment
783 dup whitespace? or commentstart? or
785 dup nextchar '\n' = and if
786 invert \ Stop eating comment
788 dup false = commentstart? and if
789 invert \ Begin eating comment
804 nextchar [char] - = ;
807 nextchar [char] + = ;
809 : fixnum? ( -- bool )
835 : flonum? ( -- bool )
842 \ Record starting parse idx:
843 \ Want to detect whether any characters (following +/-) were eaten.
850 [char] . nextchar = if
857 [char] e nextchar = [char] E nextchar = or if
865 drop pop-parse-idx false exit
873 \ This is a real number if characters were
874 \ eaten and the next characer is a delimiter.
875 parse-idx @ < delim? and
880 : ratnum? ( -- bool )
888 pop-parse-idx false exit
897 [char] / nextchar <> if
898 pop-parse-idx false exit
904 pop-parse-idx false exit
916 : boolean? ( -- bool )
917 nextchar [char] # <> if false exit then
924 and if pop-parse-idx false exit then
936 : str-equiv? ( str -- bool )
953 delim? false = if drop false then
958 : character? ( -- bool )
959 nextchar [char] # <> if false exit then
964 nextchar [char] \ <> if pop-parse-idx false exit then
968 S" newline" str-equiv? if pop-parse-idx true exit then
969 S" space" str-equiv? if pop-parse-idx true exit then
970 S" tab" str-equiv? if pop-parse-idx true exit then
972 charavailable? false = if pop-parse-idx false exit then
978 nextchar [char] ( = ;
980 : string? ( -- bool )
981 nextchar [char] " = ;
983 : readfixnum ( -- fixnum )
994 10 * nextchar [char] 0 - +
1003 : readflonum ( -- flonum )
1005 dup 0< swap abs i->f
1007 [char] . nextchar = if
1013 nextchar [char] 0 - i->f ( f exp d )
1014 over f/ rot f+ ( exp f' )
1015 swap 10.0 f* ( f' exp' )
1022 [char] e nextchar = [char] E nextchar = or if
1025 readfixnum drop i->f
1036 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1043 fixnum-type swap fixnum-type
1044 cons drop ratnum-type
1048 : readratnum ( -- ratnum )
1049 readfixnum inc-parse-idx readfixnum
1053 : readbool ( -- bool-obj )
1056 nextchar [char] f = if
1067 : readchar ( -- char-obj )
1071 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1072 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1073 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1075 nextchar character-type
1080 : readstring ( -- charlist )
1085 nextchar [char] " <>
1087 nextchar [char] \ = if
1090 [char] n of '\n' endof
1091 [char] " of [char] " endof
1097 inc-parse-idx character-type
1100 ( firstchar prevchar thischar )
1103 2drop 2swap 2drop 2dup ( thischar thischar )
1105 ( firstchar thischar prevchar )
1106 2over 2swap set-cdr! ( firstchar thischar )
1110 \ Discard previous character
1116 ." No delimiter following right double quote. Aborting." cr
1128 : readsymbol ( -- charlist )
1129 delim? if nil exit then
1131 nextchar inc-parse-idx character-type
1138 : readpair ( -- pairobj )
1142 nextchar [char] ) = if
1147 ." No delimiter following right paren. Aborting." cr
1156 \ Read first pair element
1161 nextchar [char] . = if
1166 ." No delimiter following '.'. Aborting." cr
1180 \ Parse a scheme expression
1215 nextchar [char] " <> if
1216 bold red ." Missing closing double-quote." reset-term cr
1234 nextchar [char] ) <> if
1235 bold red ." Missing closing paren." reset-term cr
1244 nextchar [char] ' = if
1246 quote-symbol recurse nil cons cons exit
1249 nextchar [char] ` = if
1251 quasiquote-symbol recurse nil cons cons exit
1254 nextchar [char] , = if
1256 nextchar [char] @ = if
1258 unquote-splicing-symbol recurse nil cons cons exit
1260 unquote-symbol recurse nil cons cons exit
1270 \ Anything else is parsed as a symbol
1271 readsymbol charlist>symbol
1273 \ Replace λ with lambda
1274 2dup λ-symbol objeq? if
1283 \ ---- Eval ---- {{{
1285 : self-evaluating? ( obj -- obj bool )
1286 boolean-type istype? if true exit then
1287 fixnum-type istype? if true exit then
1288 flonum-type istype? if true exit then
1289 ratnum-type istype? if true exit then
1290 character-type istype? if true exit then
1291 string-type istype? if true exit then
1292 nil-type istype? if true exit then
1293 none-type istype? if true exit then
1298 : tagged-list? ( obj tag-obj -- obj bool )
1300 pair-type istype? false = if
1306 : quote? ( obj -- obj bool )
1307 quote-symbol tagged-list? ;
1309 : quote-body ( quote-obj -- quote-body-obj )
1312 : quasiquote? ( obj -- obj bool )
1313 quasiquote-symbol tagged-list? ;
1315 : unquote? ( obj -- obj bool )
1316 unquote-symbol tagged-list? ;
1318 : unquote-splicing? ( obj -- obj bool )
1319 unquote-splicing-symbol tagged-list? ;
1321 : eval-unquote ( env obj -- res )
1325 except-message: ." no arguments to unquote." recoverable-exception throw
1330 except-message: ." too many arguments to unquote." recoverable-exception throw
1333 2drop car 2swap eval
1336 ( Create a new list from elements of l1 consed on to l2 )
1337 : join-lists ( l2 l1 -- l3 )
1338 nil? if 2drop exit then
1345 defer eval-quasiquote-item
1346 : eval-quasiquote-pair ( env obj -- res )
1347 2over 2over ( env obj env obj )
1349 cdr eval-quasiquote-item
1351 -2rot car ( cdritem env objcar )
1353 unquote-splicing? if
1354 eval-unquote ( cdritems caritem )
1362 eval-quasiquote-item ( cdritems caritem )
1377 pair-type istype? if
1378 eval-quasiquote-pair exit
1382 ; is eval-quasiquote-item
1384 : eval-quasiquote ( obj env -- res )
1385 2swap cdr ( env args )
1388 except-message: ." no arguments to quasiquote." recoverable-exception throw
1391 2dup cdr ( env args args-cdr )
1393 except-message: ." too many arguments to quasiquote." recoverable-exception throw
1396 2drop car ( env arg )
1398 eval-quasiquote-item
1401 : variable? ( obj -- obj bool )
1402 symbol-type istype? ;
1404 : definition? ( obj -- obj bool )
1405 define-symbol tagged-list? ;
1407 : definition-var ( obj -- var )
1410 : definition-val ( obj -- val )
1413 : eval-definition ( obj env -- res )
1416 definition-val 2swap
1419 2swap definition-var 2swap
1427 : assignment? ( obj -- obj bool )
1428 set!-symbol tagged-list? ;
1430 : assignment-var ( obj -- var )
1433 : assignment-val ( obj -- val )
1436 : eval-assignment ( obj env -- res )
1438 2over 2over ( env obj env obj )
1439 assignment-val 2swap ( env obj valexp env )
1440 eval ( env obj val )
1442 2swap assignment-var 2swap ( env var val )
1444 2rot ( var val env )
1450 : macro-definition? ( obj -- obj bool )
1451 define-macro-symbol tagged-list? ;
1453 : macro-definition-name ( exp -- mname )
1456 : macro-definition-params ( exp -- params )
1459 : macro-definition-body ( exp -- body )
1463 : eval-define-macro ( obj env -- res )
1466 2dup macro-definition-name 2swap ( name obj )
1467 2dup macro-definition-params 2swap ( name params obj )
1468 macro-definition-body ( name params body )
1470 env obj@ ( name params body env )
1478 : if? ( obj -- obj bool )
1479 if-symbol tagged-list? ;
1481 : if-predicate ( ifobj -- pred )
1484 : if-consequent ( ifobj -- conseq )
1487 : if-alternative ( ifobj -- alt|none )
1495 : false? ( boolobj -- boolean )
1496 boolean-type istype? if
1497 false boolean-type objeq?
1503 : true? ( boolobj -- bool )
1506 : lambda? ( obj -- obj bool )
1507 lambda-symbol tagged-list? ;
1509 : lambda-parameters ( obj -- params )
1512 : lambda-body ( obj -- body )
1515 : eval-sequence ( explist env -- finalexp env )
1516 ( Evaluates all bar the final expressions in
1517 an an expression list. The final expression
1518 is returned to allow for tail optimization. )
1520 2swap ( env explist )
1522 \ Abort on empty list
1529 2dup cdr ( env explist nextexplist )
1532 -2rot car 2over ( nextexplist env exp env )
1534 2drop \ discard result
1535 2swap ( env nextexplist )
1538 2drop car 2swap ( finalexp env )
1541 : application? ( obj -- obj bool )
1544 : operator ( obj -- operator )
1547 : operands ( obj -- operands )
1550 : nooperands? ( operands -- bool )
1553 : first-operand ( operands -- operand )
1556 : rest-operands ( operands -- other-operands )
1559 : list-of-vals ( args env -- vals )
1565 2over 2over first-operand 2swap eval
1566 -2rot rest-operands 2swap recurse
1571 : procedure-params ( proc -- params )
1572 drop pair-type car ;
1574 : procedure-body ( proc -- body )
1575 drop pair-type cdr car ;
1577 : procedure-env ( proc -- body )
1578 drop pair-type cdr cdr car ;
1580 ( Ensure terminating symbol arg name is handled
1581 specially to allow for variadic procedures. )
1582 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1584 2over nil? false = if
1585 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1592 symbol-type istype? if
1602 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
1609 recurse ( argvals argnames argvals'' argnames'' )
1610 2rot car 2swap cons ( argvals argvals'' argnames' )
1611 2rot car 2rot cons ( argnames' argvals' )
1615 : apply ( proc argvals -- result )
1617 primitive-proc-type of
1621 compound-proc-type of
1622 2dup procedure-body ( argvals proc body )
1623 -2rot 2dup procedure-params ( body argvals proc argnames )
1624 -2rot procedure-env ( body argnames argvals procenv )
1630 extend-env ( body env )
1634 R> drop ['] eval goto-deferred \ Tail call optimization
1637 except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
1641 :noname ( obj env -- result )
1646 fg yellow ." Evaluating: " bold 2dup print reset-term
1647 space fg green ." PS: " bold depth . reset-term
1648 space fg blue ." RS: " bold RSP@ RSP0 - . reset-term cr
1663 2swap eval-quasiquote
1673 2swap eval-definition
1678 2swap eval-assignment
1682 macro-definition? if
1683 2swap eval-define-macro
1699 ['] eval goto-deferred
1703 2dup lambda-parameters
1711 2over 2over ( env exp env exp )
1712 operator ( env exp env opname )
1714 2swap eval ( env exp proc )
1716 -2rot ( proc env exp )
1717 operands 2swap ( proc operands env )
1718 list-of-vals ( proc argvals )
1724 except-message: ." tried to evaluate object with unknown type." recoverable-exception throw
1729 \ ---- Macro Expansion ---- {{{
1731 ( Simply evaluates the given procedure with expbody as its argument. )
1732 : macro-eval ( proc expbody -- result )
1734 2dup procedure-body ( expbody proc procbody )
1735 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1736 -2rot procedure-env ( procbody argnames expbody procenv )
1742 extend-env eval-sequence eval
1745 : expand-macro ( exp -- result )
1746 pair-type istype? invert if exit then
1747 2dup car symbol-type istype? invert if 2drop exit then
1749 lookup-macro nil? if
1752 2over cdr macro-eval
1754 2dup no-match-symbol objeq? if
1760 R> drop ['] expand goto-deferred
1763 : expand-quasiquote-item ( exp -- result )
1767 unquote-symbol 2swap cdr car expand nil cons cons
1771 unquote-splicing? if
1772 unquote-splicing-symbol 2swap cdr car expand nil cons cons
1776 pair-type istype? if
1783 : expand-quasiquote ( exp -- result )
1784 quasiquote-symbol 2swap cdr
1786 expand-quasiquote-item
1790 : expand-definition ( exp -- result )
1794 2swap definition-val expand
1795 nil ( define var val' nil )
1799 : expand-assignment ( exp -- result )
1803 2swap assignment-val expand
1804 nil ( define var val' nil )
1808 : expand-list ( exp -- res )
1816 : macro-definition-nameparams
1819 : expand-define-macro ( exp -- res )
1820 define-macro-symbol 2swap
1821 2dup macro-definition-nameparams
1822 2swap macro-definition-body expand-list
1826 : expand-lambda ( exp -- res )
1828 2dup lambda-parameters
1829 2swap lambda-body expand-list
1833 : expand-if ( exp -- res )
1836 2dup if-predicate expand
1837 2swap 2dup if-consequent expand
1838 2swap if-alternative none? if
1846 : expand-application ( exp -- res )
1847 2dup operator expand
1848 2swap operands expand-list
1852 :noname ( exp -- result )
1855 self-evaluating? if exit then
1859 quasiquote? if expand-quasiquote exit then
1861 definition? if expand-definition exit then
1863 assignment? if expand-assignment exit then
1865 macro-definition? if expand-define-macro exit then
1867 lambda? if expand-lambda exit then
1869 if? if expand-if exit then
1871 application? if expand-application exit then
1877 \ ---- Print ---- {{{
1879 : printfixnum ( fixnum -- ) drop 0 .R ;
1881 : printflonum ( flonum -- ) drop f. ;
1883 : printratnum ( ratnum -- )
1885 car print ." /" cdr print
1888 : printbool ( bool -- )
1896 : printchar ( charobj -- )
1899 9 of ." #\tab" endof
1900 bl of ." #\space" endof
1901 '\n' of ." #\newline" endof
1907 : (printstring) ( stringobj -- )
1908 nil? if 2drop exit then
1912 '\n' of ." \n" drop endof
1913 [char] \ of ." \\" drop endof
1914 [char] " of [char] \ emit [char] " emit drop endof
1920 : printstring ( stringobj -- )
1925 : printsymbol ( symbolobj -- )
1926 nil-type istype? if 2drop exit then
1932 : printnil ( nilobj -- )
1935 : printpair ( pairobj -- )
1939 nil-type istype? if 2drop exit then
1940 pair-type istype? if space recurse exit then
1944 : printprim ( primobj -- )
1945 2drop ." <primitive procedure>" ;
1947 : printcomp ( primobj -- )
1948 2drop ." <compound procedure>" ;
1950 : printnone ( noneobj -- )
1951 2drop ." Unspecified return value" ;
1953 : printport ( port -- )
1957 fixnum-type istype? if printfixnum exit then
1958 flonum-type istype? if printflonum exit then
1959 ratnum-type istype? if printratnum exit then
1960 boolean-type istype? if printbool exit then
1961 character-type istype? if printchar exit then
1962 string-type istype? if printstring exit then
1963 symbol-type istype? if printsymbol exit then
1964 nil-type istype? if printnil exit then
1965 pair-type istype? if ." (" printpair ." )" exit then
1966 primitive-proc-type istype? if printprim exit then
1967 compound-proc-type istype? if printcomp exit then
1968 none-type istype? if printnone exit then
1969 port-type istype? if printport exit then
1971 except-message: ." tried to print object with unknown type." recoverable-exception throw
1976 \ ---- Garbage Collection ---- {{{
1981 variable gc-stack-depth
1984 depth gc-stack-depth !
1988 false gc-enabled ! ;
1993 : pairlike? ( obj -- obj bool )
1994 pair-type istype? if true exit then
1995 string-type istype? if true exit then
1996 symbol-type istype? if true exit then
1997 compound-proc-type istype? if true exit then
1998 port-type istype? if true exit then
2003 : pairlike-marked? ( obj -- obj bool )
2004 over nextfrees + @ 0=
2007 : mark-pairlike ( obj -- obj )
2008 over nextfrees + 0 swap !
2017 : gc-mark-obj ( obj -- )
2019 pairlike? invert if 2drop exit then
2020 pairlike-marked? if 2drop exit then
2031 scheme-memsize nextfree !
2032 0 scheme-memsize 1- do
2033 nextfrees i + @ 0<> if
2034 nextfree @ nextfrees i + !
2040 \ Following a GC, this gives the amount of free memory
2044 nextfrees i + @ 0= if 1+ then
2048 \ Debugging word - helps spot memory that is retained
2051 nextfrees i + @ 0<> if
2063 symbol-table obj@ gc-mark-obj
2064 macro-table obj@ gc-mark-obj
2065 console-i/o-port obj@ gc-mark-obj
2066 global-env obj@ gc-mark-obj
2068 depth gc-stack-depth @ do
2077 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
2078 ; is collect-garbage
2082 \ ---- Loading files ---- {{{
2084 : load ( addr n -- finalResult )
2089 ok-symbol ( port res )
2092 2over read-port ( port res obj )
2094 2dup EOF character-type objeq? if
2095 2drop 2swap close-port
2099 2swap 2drop ( port obj )
2103 global-env obj@ eval ( port res )
2109 \ ---- Standard Library ---- {{{
2111 include scheme-primitives.4th
2113 s" scheme-library.scm" load 2drop
2119 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2120 : repl-body ( -- bool )
2121 cr bold fg green ." > " reset-term
2125 2dup EOF character-type objeq? if
2127 bold fg blue ." Moriturus te saluto." reset-term cr
2133 global-env obj@ eval
2135 fg cyan ." ; " print reset-term
2145 \ Display welcome message
2146 welcome-symbol nil cons global-env obj@ eval 2drop
2151 recoverable-exception of false endof
2152 unrecoverable-exception of true endof