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 @ ,
59 make-exception recoverable-exception
60 make-exception unrecoverable-exception
62 : throw reset-term throw ;
66 \ ---- List-structured memory ---- {{{
68 20000 constant scheme-memsize
70 create car-cells scheme-memsize allot
71 create car-type-cells scheme-memsize allot
72 create cdr-cells scheme-memsize allot
73 create cdr-type-cells scheme-memsize allot
75 create nextfrees scheme-memsize allot
86 nextfrees nextfree @ + @
89 nextfree @ scheme-memsize >= if
93 nextfree @ scheme-memsize >= if
94 except-message: ." Out of memory!" unrecoverable-exception throw
98 : cons ( car-obj cdr-obj -- pair-obj )
99 cdr-type-cells nextfree @ + !
100 cdr-cells nextfree @ + !
101 car-type-cells nextfree @ + !
102 car-cells nextfree @ + !
108 : car ( pair-obj -- car-obj )
110 dup car-cells + @ swap
114 : cdr ( pair-obj -- car-obj )
116 dup cdr-cells + @ swap
120 : set-car! ( obj pair-obj -- )
122 rot swap car-type-cells + !
126 : set-cdr! ( obj pair-obj -- )
128 rot swap cdr-type-cells + !
133 : nil? nil-type istype? ;
136 : none? none-type istype? ;
138 : objvar create nil swap , , ;
140 : value@ ( objvar -- val ) @ ;
141 : type@ ( objvar -- type ) 1+ @ ;
142 : value! ( newval objvar -- ) ! ;
143 : type! ( newtype objvar -- ) 1+ ! ;
144 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
145 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
147 : objeq? ( obj obj -- bool )
150 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
151 >R >R ( a1 a2 b1 b2 )
152 2swap ( b1 b2 a1 a2 )
153 R> R> ( b1 b2 a1 a2 c1 c2 )
157 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
158 2swap ( a1 a2 c1 c2 b1 b2 )
159 >R >R ( a1 a2 c1 c2 )
160 2swap ( c1 c2 a1 a2 )
166 \ ---- Pre-defined symbols ---- {{{
170 : duplicate-charlist ( charlist -- copy )
172 2dup car 2swap cdr recurse cons
175 : charlist-equiv ( charlist charlist -- bool )
184 2drop 2drop true exit
186 2drop 2drop false exit
191 2drop 2drop false exit
198 car drop -rot car drop = if
199 cdr 2swap cdr recurse
205 : charlist>symbol ( charlist -- symbol-obj )
224 drop symbol-type 2dup
225 symbol-table obj@ cons
230 : cstr>charlist ( addr n -- charlist )
234 2dup drop @ character-type 2swap
242 : create-symbol ( -- )
250 does> dup @ swap 1+ @
253 create-symbol quote quote-symbol
254 create-symbol quasiquote quasiquote-symbol
255 create-symbol unquote unquote-symbol
256 create-symbol unquote-splicing unquote-splicing-symbol
257 create-symbol define define-symbol
258 create-symbol define-macro define-macro-symbol
259 create-symbol set! set!-symbol
260 create-symbol ok ok-symbol
261 create-symbol if if-symbol
262 create-symbol lambda lambda-symbol
263 create-symbol λ λ-symbol
264 create-symbol begin begin-symbol
265 create-symbol eof eof-symbol
267 \ Symbol to be bound to welcome message procedure by library
268 create-symbol welcome welcome-symbol
272 \ ---- Port I/O ---- {{{
274 ( Ports are pairs with the fid in the car and the peek buffer in the cdr. )
276 : fileport>fid ( fileport -- fid )
277 drop pair-type car drop ;
279 : get-last-peek ( fileport -- char/nil )
282 : set-last-peek ( char/nil fileport -- )
283 drop pair-type set-cdr!
286 : fid>fileport ( fid -- fileport )
287 fixnum-type nil cons drop port-type ;
289 : open-input-file ( addr n -- fileport )
290 r/o open-file drop fid>fileport
293 : close-port ( fileport -- )
294 fileport>fid close-file drop
297 objvar console-i/o-port
298 0 fixnum-type nil cons drop port-type console-i/o-port obj!
300 objvar current-input-port
301 console-i/o-port obj@ current-input-port obj!
303 : read-char ( port -- char )
304 2dup get-last-peek nil? if
306 2dup console-i/o-port obj@ objeq? if
310 fileport>fid pad 1 rot read-file 0= if
321 : peek-char ( port -- char )
322 2dup get-last-peek nil? if
324 2dup 2rot set-last-peek
330 variable read-line-buffer-span
331 variable read-line-buffer-offset
333 ( Hack to save original read-line while we transition to new one. )
334 : orig-read-line immediate
337 : read-line ( port -- string )
342 0 read-line-buffer-offset !
344 2over nil 2swap set-last-peek
346 2drop nil nil cons exit
349 1 read-line-buffer-offset !
353 2dup console-i/o-port obj@ objeq? if
355 pad read-line-buffer-offset @ + 200 expect cr
356 span @ read-line-buffer-offset @ + read-line-buffer-span !
358 pad read-line-buffer-offset @ + 200 2over fileport>fid orig-read-line
359 drop swap read-line-buffer-offset @ + read-line-buffer-span !
365 read-line-buffer-span @ 0>
367 pad read-line-buffer-span @ 1- + @ character-type 2swap cons
368 -1 read-line-buffer-span +!
372 nil cons drop string-type
378 : read-port ( fileport -- obj )
379 current-input-port obj!
382 : read-console ( -- obj )
383 console-i/o-port obj@ read-port ;
387 \ ---- Environments ---- {{{
389 : enclosing-env ( env -- env )
392 : first-frame ( env -- frame )
395 : make-frame ( vars vals -- frame )
398 : frame-vars ( frame -- vars )
401 : frame-vals ( frame -- vals )
404 : add-binding ( var val frame -- )
405 2swap 2over frame-vals cons
407 2swap 2over frame-vars cons
411 : extend-env ( vars vals env -- env )
421 : get-vars-vals-frame ( var frame -- bool )
422 2dup frame-vars vars obj!
426 vars obj@ nil objeq? false =
428 2dup vars obj@ car objeq? if
433 vars obj@ cdr vars obj!
434 vals obj@ cdr vals obj!
440 : get-vars-vals ( var env -- vars? vals? bool )
445 2over 2over first-frame
446 get-vars-vals-frame if
448 vars obj@ vals obj@ true
464 : lookup-var ( var env -- val )
469 except-message: ." tried to read unbound variable '" var obj@ print ." '." recoverable-exception throw
473 : set-var ( var val env -- )
474 >R >R 2swap R> R> ( val var env )
477 2swap 2drop ( val vals )
480 except-message: ." tried to set unbound variable '" var obj@ print ." '." recoverable-exception throw
488 : define-var ( var val env -- )
491 2over env obj@ ( var val var env )
493 2swap 2drop ( var val vals )
498 first-frame ( var val frame )
505 : make-procedure ( params body env -- proc )
508 drop compound-proc-type
512 nil nil nil extend-env
517 \ ---- Primitives ---- {{{
519 : make-primitive ( cfa -- )
526 rot primitive-proc-type ( var prim )
527 global-env obj@ define-var
530 : ensure-arg-count ( args n -- )
532 drop nil objeq? false = if
533 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
537 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
544 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
546 drop nil objeq? false = if
547 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
551 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
554 2dup cdr 2swap car ( ... t1 n args' arg1 )
555 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
557 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
565 : push-args-to-stack ( args -- arg1 arg2 ... argn )
575 : add-fa-checks ( cfa n -- cfa' )
576 here current @ 1+ dup @ , !
580 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
581 ['] push-args-to-stack ,
582 ['] lit , , ['] execute ,
586 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
587 here current @ 1+ dup @ , !
594 dup ( cfa t1 t2 ... tn n m )
599 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
605 ['] lit , , ['] ensure-arg-type-and-count ,
607 ['] push-args-to-stack ,
608 ['] lit , , ['] execute ,
614 : make-fa-primitive ( cfa n -- )
615 add-fa-checks make-primitive ;
617 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
618 add-fa-type-checks make-primitive ;
621 bold fg red ." Incorrect argument type." reset-term cr
625 : ensure-arg-type ( arg type -- arg )
627 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
634 \ ---- Macros ---- {{{
638 ( Look up macro in macro table. Returns nil if
640 : lookup-macro ( name_symbol -- proc )
642 symbol-type istype? invert if
643 \ Early exit if argument is not a symbol
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 except-message: ." no arguments to unquote." recoverable-exception throw
1329 except-message: ." too many arguments to unquote." recoverable-exception throw
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 except-message: ." no arguments to quasiquote." recoverable-exception throw
1390 2dup cdr ( env args args-cdr )
1392 except-message: ." too many arguments to quasiquote." recoverable-exception throw
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 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1610 symbol-type istype? if
1620 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
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 except-message: ." object not applicable." recoverable-exception throw
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 )
1678 fg yellow ." Evaluating: " bold 2dup print reset-term
1679 space fg green ." PS: " bold depth . reset-term
1680 space fg blue ." RS: " bold RSP@ RSP0 - . reset-term cr
1695 2swap eval-quasiquote
1705 2swap eval-definition
1710 2swap eval-assignment
1714 macro-definition? if
1715 2swap eval-define-macro
1731 ['] eval goto-deferred
1735 2dup lambda-parameters
1744 ['] eval goto-deferred
1749 2over 2over ( env exp env exp )
1750 operator ( env exp env opname )
1752 2dup lookup-macro nil? false = if
1753 \ Macro function evaluation
1755 ( env exp env opname mproc )
1756 2swap 2drop -2rot 2drop cdr ( env mproc body )
1761 ['] eval goto-deferred
1763 \ Regular function application
1765 2drop ( env exp env opname )
1767 2swap eval ( env exp proc )
1769 -2rot ( proc env exp )
1770 operands 2swap ( proc operands env )
1771 list-of-vals ( proc argvals )
1778 except-message: ." tried to evaluate object with unknown type." recoverable-exception throw
1783 \ ---- Print ---- {{{
1785 : printfixnum ( fixnum -- ) drop 0 .R ;
1787 : printflonum ( flonum -- ) drop f. ;
1789 : printratnum ( ratnum -- )
1791 car print ." /" cdr print
1794 : printbool ( bool -- )
1802 : printchar ( charobj -- )
1805 9 of ." #\tab" endof
1806 bl of ." #\space" endof
1807 '\n' of ." #\newline" endof
1813 : (printstring) ( stringobj -- )
1814 nil? if 2drop exit then
1818 '\n' of ." \n" drop endof
1819 [char] \ of ." \\" drop endof
1820 [char] " of [char] \ emit [char] " emit drop endof
1826 : printstring ( stringobj -- )
1831 : printsymbol ( symbolobj -- )
1832 nil-type istype? if 2drop exit then
1838 : printnil ( nilobj -- )
1841 : printpair ( pairobj -- )
1845 nil-type istype? if 2drop exit then
1846 pair-type istype? if space recurse exit then
1850 : printprim ( primobj -- )
1851 2drop ." <primitive procedure>" ;
1853 : printcomp ( primobj -- )
1854 2drop ." <compound procedure>" ;
1856 : printnone ( noneobj -- )
1857 2drop ." Unspecified return value" ;
1859 : printport ( port -- )
1863 fixnum-type istype? if printfixnum exit then
1864 flonum-type istype? if printflonum exit then
1865 ratnum-type istype? if printratnum exit then
1866 boolean-type istype? if printbool exit then
1867 character-type istype? if printchar exit then
1868 string-type istype? if printstring exit then
1869 symbol-type istype? if printsymbol exit then
1870 nil-type istype? if printnil exit then
1871 pair-type istype? if ." (" printpair ." )" exit then
1872 primitive-proc-type istype? if printprim exit then
1873 compound-proc-type istype? if printcomp exit then
1874 none-type istype? if printnone exit then
1875 port-type istype? if printport exit then
1877 except-message: ." tried to print object with unknown type." recoverable-exception throw
1882 \ ---- Garbage Collection ---- {{{
1887 variable gc-stack-depth
1890 depth gc-stack-depth !
1894 false gc-enabled ! ;
1899 : pairlike? ( obj -- obj bool )
1900 pair-type istype? if true exit then
1901 string-type istype? if true exit then
1902 symbol-type istype? if true exit then
1903 compound-proc-type istype? if true exit then
1904 port-type istype? if true exit then
1909 : pairlike-marked? ( obj -- obj bool )
1910 over nextfrees + @ 0=
1913 : mark-pairlike ( obj -- obj )
1914 over nextfrees + 0 swap !
1923 : gc-mark-obj ( obj -- )
1925 pairlike? invert if 2drop exit then
1926 pairlike-marked? if 2drop exit then
1937 scheme-memsize nextfree !
1938 0 scheme-memsize 1- do
1939 nextfrees i + @ 0<> if
1940 nextfree @ nextfrees i + !
1946 \ Following a GC, this gives the amount of free memory
1950 nextfrees i + @ 0= if 1+ then
1954 \ Debugging word - helps spot memory that is retained
1957 nextfrees i + @ 0<> if
1969 symbol-table obj@ gc-mark-obj
1970 macro-table obj@ gc-mark-obj
1971 console-i/o-port obj@ gc-mark-obj
1972 global-env obj@ gc-mark-obj
1974 depth gc-stack-depth @ do
1983 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1984 ; is collect-garbage
1988 \ ---- Loading files ---- {{{
1990 : load ( addr n -- finalResult )
1995 ok-symbol ( port res )
1998 2over read-port ( port res obj )
2000 2dup EOF character-type objeq? if
2001 2drop 2swap close-port
2005 2swap 2drop ( port obj )
2007 global-env obj@ eval ( port res )
2013 \ ---- Standard Library ---- {{{
2015 include scheme-primitives.4th
2017 s" scheme-library.scm" load 2drop
2023 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2024 : repl-body ( -- bool )
2025 cr bold fg green ." > " reset-term
2029 2dup EOF character-type objeq? if
2031 bold fg blue ." Moriturus te saluto." reset-term cr
2035 global-env obj@ eval
2037 fg cyan ." ; " print reset-term
2047 \ Display welcome message
2048 welcome-symbol nil cons global-env obj@ eval 2drop
2053 recoverable-exception of false endof
2054 unrecoverable-exception of true endof