4 include term-colours.4th
6 include catch-throw.4th
32 make-type boolean-type
33 make-type character-type
39 make-type primitive-proc-type
40 make-type compound-proc-type
42 : istype? ( obj type -- obj bool )
47 \ ---- Exceptions ---- {{{
49 variable nextexception
52 create nextexception @ ,
61 make-exception recoverable-exception
62 make-exception unrecoverable-exception
64 : throw reset-term throw ;
68 \ ---- List-structured memory ---- {{{
70 20000 constant scheme-memsize
72 create car-cells scheme-memsize allot
73 create car-type-cells scheme-memsize allot
74 create cdr-cells scheme-memsize allot
75 create cdr-type-cells scheme-memsize allot
77 create nextfrees scheme-memsize allot
88 nextfrees nextfree @ + @
91 nextfree @ scheme-memsize >= if
95 nextfree @ scheme-memsize >= if
96 except-message: ." Out of memory!" unrecoverable-exception throw
100 : cons ( car-obj cdr-obj -- pair-obj )
101 cdr-type-cells nextfree @ + !
102 cdr-cells nextfree @ + !
103 car-type-cells nextfree @ + !
104 car-cells nextfree @ + !
110 : car ( pair-obj -- car-obj )
112 dup car-cells + @ swap
116 : cdr ( pair-obj -- car-obj )
118 dup cdr-cells + @ swap
122 : set-car! ( obj pair-obj -- )
124 rot swap car-type-cells + !
128 : set-cdr! ( obj pair-obj -- )
130 rot swap cdr-type-cells + !
135 : nil? nil-type istype? ;
138 : none? none-type istype? ;
140 : objvar create nil swap , , ;
142 : value@ ( objvar -- val ) @ ;
143 : type@ ( objvar -- type ) 1+ @ ;
144 : value! ( newval objvar -- ) ! ;
145 : type! ( newtype objvar -- ) 1+ ! ;
146 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
147 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
149 : objeq? ( obj obj -- bool )
152 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
153 >R >R ( a1 a2 b1 b2 )
154 2swap ( b1 b2 a1 a2 )
155 R> R> ( b1 b2 a1 a2 c1 c2 )
159 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
160 2swap ( a1 a2 c1 c2 b1 b2 )
161 >R >R ( a1 a2 c1 c2 )
162 2swap ( c1 c2 a1 a2 )
168 \ ---- Pre-defined symbols ---- {{{
172 : duplicate-charlist ( charlist -- copy )
174 2dup car 2swap cdr recurse cons
177 : charlist-equiv ( charlist charlist -- bool )
186 2drop 2drop true exit
188 2drop 2drop false exit
193 2drop 2drop false exit
200 car drop -rot car drop = if
201 cdr 2swap cdr recurse
207 : charlist>symbol ( charlist -- symbol-obj )
226 drop symbol-type 2dup
227 symbol-table obj@ cons
232 : cstr>charlist ( addr n -- charlist )
236 2dup drop @ character-type 2swap
244 : create-symbol ( -- )
252 does> dup @ swap 1+ @
255 create-symbol quote quote-symbol
256 create-symbol quasiquote quasiquote-symbol
257 create-symbol unquote unquote-symbol
258 create-symbol unquote-splicing unquote-splicing-symbol
259 create-symbol define define-symbol
260 create-symbol define-macro define-macro-symbol
261 create-symbol set! set!-symbol
262 create-symbol ok ok-symbol
263 create-symbol if if-symbol
264 create-symbol lambda lambda-symbol
265 create-symbol λ λ-symbol
266 create-symbol eof eof-symbol
267 create-symbol no-match no-match-symbol
269 \ Symbol to be bound to welcome message procedure by library
270 create-symbol welcome welcome-symbol
274 \ ---- Port I/O ---- {{{
276 ( Ports are pairs with the fid in the car and the peek buffer in the cdr. )
278 : fileport>fid ( fileport -- fid )
279 drop pair-type car drop ;
281 : get-last-peek ( fileport -- char/nil )
284 : set-last-peek ( char/nil fileport -- )
285 drop pair-type set-cdr!
288 : fid>fileport ( fid -- fileport )
289 fixnum-type nil cons drop port-type ;
291 : open-input-file ( addr n -- fileport )
292 r/o open-file drop fid>fileport
295 : close-port ( fileport -- )
296 fileport>fid close-file drop
299 objvar console-i/o-port
300 0 fixnum-type nil cons drop port-type console-i/o-port obj!
302 objvar current-input-port
303 console-i/o-port obj@ current-input-port obj!
305 : read-char ( port -- char )
306 2dup get-last-peek nil? if
308 2dup console-i/o-port obj@ objeq? if
312 fileport>fid pad 1 rot read-file 0= if
323 : peek-char ( port -- char )
324 2dup get-last-peek nil? if
326 2dup 2rot set-last-peek
332 variable read-line-buffer-span
333 variable read-line-buffer-offset
335 ( Hack to save original read-line while we transition to new one. )
336 : orig-read-line immediate
339 : read-line ( port -- string )
344 0 read-line-buffer-offset !
346 2over nil 2swap set-last-peek
348 2drop nil nil cons exit
351 1 read-line-buffer-offset !
355 2dup console-i/o-port obj@ objeq? if
357 pad read-line-buffer-offset @ + 200 expect cr
358 span @ read-line-buffer-offset @ + read-line-buffer-span !
360 pad read-line-buffer-offset @ + 200 2over fileport>fid orig-read-line
361 drop swap read-line-buffer-offset @ + read-line-buffer-span !
367 read-line-buffer-span @ 0>
369 pad read-line-buffer-span @ 1- + @ character-type 2swap cons
370 -1 read-line-buffer-span +!
374 nil cons drop string-type
380 : read-port ( fileport -- obj )
381 current-input-port obj!
384 : read-console ( -- obj )
385 console-i/o-port obj@ read-port ;
389 \ ---- Environments ---- {{{
391 : enclosing-env ( env -- env )
394 : first-frame ( env -- frame )
397 : make-frame ( vars vals -- frame )
400 : frame-vars ( frame -- vars )
403 : frame-vals ( frame -- vals )
406 : add-binding ( var val frame -- )
407 2swap 2over frame-vals cons
409 2swap 2over frame-vars cons
413 : extend-env ( vars vals env -- env )
423 : get-vars-vals-frame ( var frame -- bool )
424 2dup frame-vars vars obj!
428 vars obj@ nil objeq? false =
430 2dup vars obj@ car objeq? if
435 vars obj@ cdr vars obj!
436 vals obj@ cdr vals obj!
442 : get-vars-vals ( var env -- vars? vals? bool )
447 2over 2over first-frame
448 get-vars-vals-frame if
450 vars obj@ vals obj@ true
466 : lookup-var ( var env -- val )
471 except-message: ." tried to read unbound variable '" var obj@ print ." '." recoverable-exception throw
475 : set-var ( var val env -- )
476 >R >R 2swap R> R> ( val var env )
479 2swap 2drop ( val vals )
482 except-message: ." tried to set unbound variable '" var obj@ print ." '." recoverable-exception throw
490 : define-var ( var val env -- )
493 2over env obj@ ( var val var env )
495 2swap 2drop ( var val vals )
500 first-frame ( var val frame )
507 : make-procedure ( params body env -- proc )
510 drop compound-proc-type
514 nil nil nil extend-env
519 \ ---- Primitives ---- {{{
521 : make-primitive ( cfa -- )
528 rot primitive-proc-type ( var prim )
529 global-env obj@ define-var
532 : ensure-arg-count ( args n -- )
534 drop nil objeq? false = if
535 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
539 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
546 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
548 drop nil objeq? false = if
549 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
553 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
556 2dup cdr 2swap car ( ... t1 n args' arg1 )
557 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
559 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
567 : push-args-to-stack ( args -- arg1 arg2 ... argn )
577 : add-fa-checks ( cfa n -- cfa' )
578 here current @ 1+ dup @ , !
582 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
583 ['] push-args-to-stack ,
584 ['] lit , , ['] execute ,
588 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
589 here current @ 1+ dup @ , !
596 dup ( cfa t1 t2 ... tn n m )
601 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
607 ['] lit , , ['] ensure-arg-type-and-count ,
609 ['] push-args-to-stack ,
610 ['] lit , , ['] execute ,
616 : make-fa-primitive ( cfa n -- )
617 add-fa-checks make-primitive ;
619 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
620 add-fa-type-checks make-primitive ;
623 bold fg red ." Incorrect argument type." reset-term cr
627 : ensure-arg-type ( arg type -- arg )
629 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
636 \ ---- Macros ---- {{{
640 ( Look up macro in macro table. Returns nil if
642 : lookup-macro ( name_symbol -- proc )
644 symbol-type istype? invert if
645 \ Early exit if argument is not a symbol
667 : make-macro ( name_symbol params body env -- )
670 2swap ( proc name_symbol )
677 2over 2over ( proc name table name table )
679 2swap 2drop ( proc table )
691 macro-table obj@ cons
700 variable stored-parse-idx
701 create parse-str 161 allot
702 variable parse-str-span
704 create parse-idx-stack 10 allot
705 variable parse-idx-sp
706 parse-idx-stack parse-idx-sp !
709 parse-idx @ parse-idx-sp @ !
714 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
718 parse-idx-sp @ @ parse-idx ! ;
722 '\n' parse-str parse-str-span @ + !
723 1 parse-str-span +! ;
726 4 parse-str parse-str-span @ + !
727 1 parse-str-span +! ;
734 current-input-port obj@ console-i/o-port obj@ objeq? if
735 parse-str 160 expect cr
736 span @ parse-str-span !
738 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
739 drop swap parse-str-span !
741 parse-str-span @ 0= and if append-eof then
752 : charavailable? ( -- bool )
753 parse-str-span @ parse-idx @ > ;
755 : nextchar ( -- char )
756 charavailable? false = if getline then
757 parse-str parse-idx @ + @ ;
760 : whitespace? ( -- bool )
772 nextchar [char] ( = or
773 nextchar [char] ) = or
776 : commentstart? ( -- bool )
777 nextchar [char] ; = ;
781 false \ Indicates whether or not we're eating a comment
784 dup whitespace? or commentstart? or
786 dup nextchar '\n' = and if
787 invert \ Stop eating comment
789 dup false = commentstart? and if
790 invert \ Begin eating comment
805 nextchar [char] - = ;
808 nextchar [char] + = ;
810 : fixnum? ( -- bool )
836 : flonum? ( -- bool )
843 \ Record starting parse idx:
844 \ Want to detect whether any characters (following +/-) were eaten.
851 [char] . nextchar = if
858 [char] e nextchar = [char] E nextchar = or if
866 drop pop-parse-idx false exit
874 \ This is a real number if characters were
875 \ eaten and the next characer is a delimiter.
876 parse-idx @ < delim? and
881 : ratnum? ( -- bool )
889 pop-parse-idx false exit
898 [char] / nextchar <> if
899 pop-parse-idx false exit
905 pop-parse-idx false exit
917 : boolean? ( -- bool )
918 nextchar [char] # <> if false exit then
925 and if pop-parse-idx false exit then
937 : str-equiv? ( str -- bool )
954 delim? false = if drop false then
959 : character? ( -- bool )
960 nextchar [char] # <> if false exit then
965 nextchar [char] \ <> if pop-parse-idx false exit then
969 S" newline" str-equiv? if pop-parse-idx true exit then
970 S" space" str-equiv? if pop-parse-idx true exit then
971 S" tab" str-equiv? if pop-parse-idx true exit then
973 charavailable? false = if pop-parse-idx false exit then
979 nextchar [char] ( = ;
981 : string? ( -- bool )
982 nextchar [char] " = ;
984 : readfixnum ( -- fixnum )
995 10 * nextchar [char] 0 - +
1004 : readflonum ( -- flonum )
1006 dup 0< swap abs i->f
1008 [char] . nextchar = if
1014 nextchar [char] 0 - i->f ( f exp d )
1015 over f/ rot f+ ( exp f' )
1016 swap 10.0 f* ( f' exp' )
1023 [char] e nextchar = [char] E nextchar = or if
1026 readfixnum drop i->f
1037 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1044 fixnum-type swap fixnum-type
1045 cons drop ratnum-type
1049 : readratnum ( -- ratnum )
1050 readfixnum inc-parse-idx readfixnum
1054 : readbool ( -- bool-obj )
1057 nextchar [char] f = if
1068 : readchar ( -- char-obj )
1072 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1073 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1074 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1076 nextchar character-type
1081 : readstring ( -- charlist )
1086 nextchar [char] " <>
1088 nextchar [char] \ = if
1091 [char] n of '\n' endof
1092 [char] " of [char] " endof
1098 inc-parse-idx character-type
1101 ( firstchar prevchar thischar )
1104 2drop 2swap 2drop 2dup ( thischar thischar )
1106 ( firstchar thischar prevchar )
1107 2over 2swap set-cdr! ( firstchar thischar )
1111 \ Discard previous character
1117 ." No delimiter following right double quote. Aborting." cr
1129 : readsymbol ( -- charlist )
1130 delim? if nil exit then
1132 nextchar inc-parse-idx character-type
1139 : readpair ( -- pairobj )
1143 nextchar [char] ) = if
1148 ." No delimiter following right paren. Aborting." cr
1157 \ Read first pair element
1162 nextchar [char] . = if
1167 ." No delimiter following '.'. Aborting." cr
1181 \ Parse a scheme expression
1216 nextchar [char] " <> if
1217 bold red ." Missing closing double-quote." reset-term cr
1235 nextchar [char] ) <> if
1236 bold red ." Missing closing paren." reset-term cr
1245 nextchar [char] ' = if
1247 quote-symbol recurse nil cons cons exit
1250 nextchar [char] ` = if
1252 quasiquote-symbol recurse nil cons cons exit
1255 nextchar [char] , = if
1257 nextchar [char] @ = if
1259 unquote-splicing-symbol recurse nil cons cons exit
1261 unquote-symbol recurse nil cons cons exit
1271 \ Anything else is parsed as a symbol
1272 readsymbol charlist>symbol
1274 \ Replace λ with lambda
1275 2dup λ-symbol objeq? if
1284 \ ---- Eval ---- {{{
1286 : self-evaluating? ( obj -- obj bool )
1287 boolean-type istype? if true exit then
1288 fixnum-type istype? if true exit then
1289 flonum-type istype? if true exit then
1290 ratnum-type istype? if true exit then
1291 character-type istype? if true exit then
1292 string-type istype? if true exit then
1293 nil-type istype? if true exit then
1294 none-type istype? if true exit then
1299 : tagged-list? ( obj tag-obj -- obj bool )
1301 pair-type istype? false = if
1307 : quote? ( obj -- obj bool )
1308 quote-symbol tagged-list? ;
1310 : quote-body ( quote-obj -- quote-body-obj )
1313 : quasiquote? ( obj -- obj bool )
1314 quasiquote-symbol tagged-list? ;
1316 : unquote? ( obj -- obj bool )
1317 unquote-symbol tagged-list? ;
1319 : unquote-splicing? ( obj -- obj bool )
1320 unquote-splicing-symbol tagged-list? ;
1322 : eval-unquote ( env obj -- res )
1326 except-message: ." no arguments to unquote." recoverable-exception throw
1331 except-message: ." too many arguments to unquote." recoverable-exception throw
1334 2drop car 2swap eval
1337 ( Create a new list from elements of l1 consed on to l2 )
1338 : join-lists ( l2 l1 -- l3 )
1339 nil? if 2drop exit then
1346 defer eval-quasiquote-item
1347 : eval-quasiquote-pair ( env obj -- res )
1348 2over 2over ( env obj env obj )
1350 cdr eval-quasiquote-item
1352 -2rot car ( cdritem env objcar )
1354 unquote-splicing? if
1355 eval-unquote ( cdritems caritem )
1363 eval-quasiquote-item ( cdritems caritem )
1378 pair-type istype? if
1379 eval-quasiquote-pair exit
1383 ; is eval-quasiquote-item
1385 : eval-quasiquote ( obj env -- res )
1386 2swap cdr ( env args )
1389 except-message: ." no arguments to quasiquote." recoverable-exception throw
1392 2dup cdr ( env args args-cdr )
1394 except-message: ." too many arguments to quasiquote." recoverable-exception throw
1397 2drop car ( env arg )
1399 eval-quasiquote-item
1402 : variable? ( obj -- obj bool )
1403 symbol-type istype? ;
1405 : definition? ( obj -- obj bool )
1406 define-symbol tagged-list? ;
1408 : definition-var ( obj -- var )
1411 : definition-val ( obj -- val )
1414 : eval-definition ( obj env -- res )
1417 definition-val 2swap
1420 2swap definition-var 2swap
1428 : assignment? ( obj -- obj bool )
1429 set!-symbol tagged-list? ;
1431 : assignment-var ( obj -- var )
1434 : assignment-val ( obj -- val )
1437 : eval-assignment ( obj env -- res )
1439 2over 2over ( env obj env obj )
1440 assignment-val 2swap ( env obj valexp env )
1441 eval ( env obj val )
1443 2swap assignment-var 2swap ( env var val )
1445 2rot ( var val env )
1451 : macro-definition? ( obj -- obj bool )
1452 define-macro-symbol tagged-list? ;
1454 : macro-definition-name ( exp -- mname )
1457 : macro-definition-params ( exp -- params )
1460 : macro-definition-body ( exp -- body )
1464 : eval-define-macro ( obj env -- res )
1467 2dup macro-definition-name 2swap ( name obj )
1468 2dup macro-definition-params 2swap ( name params obj )
1469 macro-definition-body ( name params body )
1471 env obj@ ( name params body env )
1479 : if? ( obj -- obj bool )
1480 if-symbol tagged-list? ;
1482 : if-predicate ( ifobj -- pred )
1485 : if-consequent ( ifobj -- conseq )
1488 : if-alternative ( ifobj -- alt|none )
1496 : false? ( boolobj -- boolean )
1497 boolean-type istype? if
1498 false boolean-type objeq?
1504 : true? ( boolobj -- bool )
1507 : lambda? ( obj -- obj bool )
1508 lambda-symbol tagged-list? ;
1510 : lambda-parameters ( obj -- params )
1513 : lambda-body ( obj -- body )
1516 : eval-sequence ( explist env -- finalexp env )
1517 ( Evaluates all bar the final expressions in
1518 an an expression list. The final expression
1519 is returned to allow for tail optimization. )
1521 2swap ( env explist )
1523 \ Abort on empty list
1530 2dup cdr ( env explist nextexplist )
1533 -2rot car 2over ( nextexplist env exp env )
1535 2drop \ discard result
1536 2swap ( env nextexplist )
1539 2drop car 2swap ( finalexp env )
1542 : application? ( obj -- obj bool )
1545 : operator ( obj -- operator )
1548 : operands ( obj -- operands )
1551 : nooperands? ( operands -- bool )
1554 : first-operand ( operands -- operand )
1557 : rest-operands ( operands -- other-operands )
1560 : list-of-vals ( args env -- vals )
1566 2over 2over first-operand 2swap eval
1567 -2rot rest-operands 2swap recurse
1572 : procedure-params ( proc -- params )
1573 drop pair-type car ;
1575 : procedure-body ( proc -- body )
1576 drop pair-type cdr car ;
1578 : procedure-env ( proc -- body )
1579 drop pair-type cdr cdr car ;
1581 ( Ensure terminating symbol arg name is handled
1582 specially to allow for variadic procedures. )
1583 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1585 2over nil? false = if
1586 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1593 symbol-type istype? if
1603 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
1610 recurse ( argvals argnames argvals'' argnames'' )
1611 2rot car 2swap cons ( argvals argvals'' argnames' )
1612 2rot car 2rot cons ( argnames' argvals' )
1616 : apply ( proc argvals -- result )
1618 primitive-proc-type of
1622 compound-proc-type of
1623 2dup procedure-body ( argvals proc body )
1624 -2rot 2dup procedure-params ( body argvals proc argnames )
1625 -2rot procedure-env ( body argnames argvals procenv )
1631 extend-env ( body env )
1635 R> drop ['] eval goto-deferred \ Tail call optimization
1638 except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
1642 :noname ( obj env -- result )
1647 fg yellow ." Evaluating: " bold 2dup print reset-term
1648 space fg green ." PS: " bold depth . reset-term
1649 space fg blue ." RS: " bold RSP@ RSP0 - . reset-term cr
1664 2swap eval-quasiquote
1674 2swap eval-definition
1679 2swap eval-assignment
1683 macro-definition? if
1684 2swap eval-define-macro
1700 ['] eval goto-deferred
1704 2dup lambda-parameters
1712 2over 2over ( env exp env exp )
1713 operator ( env exp env opname )
1715 2swap eval ( env exp proc )
1717 -2rot ( proc env exp )
1718 operands 2swap ( proc operands env )
1719 list-of-vals ( proc argvals )
1725 except-message: ." tried to evaluate object with unknown type." recoverable-exception throw
1732 : evaluate-eproc ( env eproc --- res )
1740 2drop \ get rid of null
1742 \ Final element of eproc list is primitive procedure
1743 drop \ dump type signifier
1744 R> drop >body >R \ GOTO primitive procedure (executor)
1747 : self-evaluating-executor ( env exp -- exp )
1750 : analyze-self-evaluating ( exp --- eproc )
1751 ['] self-evaluating-executor primitive-proc-type
1755 : quote-executor ( env exp -- exp )
1758 : analyze-quoted ( exp -- eproc )
1761 ['] quote-executor primitive-proc-type
1765 : variable-executor ( env var -- val )
1768 : analyze-variable ( exp -- eproc )
1769 ['] variable-executor primitive-proc-type
1773 : assignment-executor ( env var val-eproc -- ok )
1774 2rot 2dup 2rot ( var env env val-eproc )
1775 evaluate-eproc 2swap ( var val env )
1779 : analyze-assignment ( exp -- eproc )
1781 2swap assignment-val analyze ( var val-eproc )
1783 ['] assignment-executor primitive-proc-type
1787 :noname ( exp --- eproc )
1790 analyze-self-evaluating
1812 \ ---- Macro Expansion ---- {{{
1814 ( Simply evaluates the given procedure with expbody as its argument. )
1815 : macro-eval ( proc expbody -- result )
1817 2dup procedure-body ( expbody proc procbody )
1818 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1819 -2rot procedure-env ( procbody argnames expbody procenv )
1825 extend-env eval-sequence eval
1828 : expand-macro ( exp -- result )
1829 pair-type istype? invert if exit then
1830 2dup car symbol-type istype? invert if 2drop exit then
1832 lookup-macro nil? if
1835 2over cdr macro-eval
1837 2dup no-match-symbol objeq? if
1843 R> drop ['] expand goto-deferred
1846 : expand-quasiquote-item ( exp -- result )
1850 unquote-symbol 2swap cdr car expand nil cons cons
1854 unquote-splicing? if
1855 unquote-splicing-symbol 2swap cdr car expand nil cons cons
1859 pair-type istype? if
1866 : expand-quasiquote ( exp -- result )
1867 quasiquote-symbol 2swap cdr
1869 expand-quasiquote-item
1873 : expand-definition ( exp -- result )
1877 2swap definition-val expand
1878 nil ( define var val' nil )
1882 : expand-assignment ( exp -- result )
1886 2swap assignment-val expand
1887 nil ( define var val' nil )
1891 : expand-list ( exp -- res )
1899 : macro-definition-nameparams
1902 : expand-define-macro ( exp -- res )
1903 define-macro-symbol 2swap
1904 2dup macro-definition-nameparams
1905 2swap macro-definition-body expand-list
1909 : expand-lambda ( exp -- res )
1911 2dup lambda-parameters
1912 2swap lambda-body expand-list
1916 : expand-if ( exp -- res )
1919 2dup if-predicate expand
1920 2swap 2dup if-consequent expand
1921 2swap if-alternative none? if
1929 : expand-application ( exp -- res )
1930 2dup operator expand
1931 2swap operands expand-list
1935 :noname ( exp -- result )
1938 self-evaluating? if exit then
1942 quasiquote? if expand-quasiquote exit then
1944 definition? if expand-definition exit then
1946 assignment? if expand-assignment exit then
1948 macro-definition? if expand-define-macro exit then
1950 lambda? if expand-lambda exit then
1952 if? if expand-if exit then
1954 application? if expand-application exit then
1960 \ ---- Print ---- {{{
1962 : printfixnum ( fixnum -- ) drop 0 .R ;
1964 : printflonum ( flonum -- ) drop f. ;
1966 : printratnum ( ratnum -- )
1968 car print ." /" cdr print
1971 : printbool ( bool -- )
1979 : printchar ( charobj -- )
1982 9 of ." #\tab" endof
1983 bl of ." #\space" endof
1984 '\n' of ." #\newline" endof
1990 : (printstring) ( stringobj -- )
1991 nil? if 2drop exit then
1995 '\n' of ." \n" drop endof
1996 [char] \ of ." \\" drop endof
1997 [char] " of [char] \ emit [char] " emit drop endof
2003 : printstring ( stringobj -- )
2008 : printsymbol ( symbolobj -- )
2009 nil-type istype? if 2drop exit then
2015 : printnil ( nilobj -- )
2018 : printpair ( pairobj -- )
2022 nil-type istype? if 2drop exit then
2023 pair-type istype? if space recurse exit then
2027 : printprim ( primobj -- )
2028 2drop ." <primitive procedure>" ;
2030 : printcomp ( primobj -- )
2031 2drop ." <compound procedure>" ;
2033 : printnone ( noneobj -- )
2034 2drop ." Unspecified return value" ;
2036 : printport ( port -- )
2040 fixnum-type istype? if printfixnum exit then
2041 flonum-type istype? if printflonum exit then
2042 ratnum-type istype? if printratnum exit then
2043 boolean-type istype? if printbool exit then
2044 character-type istype? if printchar exit then
2045 string-type istype? if printstring exit then
2046 symbol-type istype? if printsymbol exit then
2047 nil-type istype? if printnil exit then
2048 pair-type istype? if ." (" printpair ." )" exit then
2049 primitive-proc-type istype? if printprim exit then
2050 compound-proc-type istype? if printcomp exit then
2051 none-type istype? if printnone exit then
2052 port-type istype? if printport exit then
2054 except-message: ." tried to print object with unknown type." recoverable-exception throw
2059 \ ---- Garbage Collection ---- {{{
2064 variable gc-stack-depth
2067 depth gc-stack-depth !
2071 false gc-enabled ! ;
2076 : pairlike? ( obj -- obj bool )
2077 pair-type istype? if true exit then
2078 string-type istype? if true exit then
2079 symbol-type istype? if true exit then
2080 compound-proc-type istype? if true exit then
2081 port-type istype? if true exit then
2086 : pairlike-marked? ( obj -- obj bool )
2087 over nextfrees + @ 0=
2090 : mark-pairlike ( obj -- obj )
2091 over nextfrees + 0 swap !
2100 : gc-mark-obj ( obj -- )
2102 pairlike? invert if 2drop exit then
2103 pairlike-marked? if 2drop exit then
2114 scheme-memsize nextfree !
2115 0 scheme-memsize 1- do
2116 nextfrees i + @ 0<> if
2117 nextfree @ nextfrees i + !
2123 \ Following a GC, this gives the amount of free memory
2127 nextfrees i + @ 0= if 1+ then
2131 \ Debugging word - helps spot memory that is retained
2134 nextfrees i + @ 0<> if
2146 symbol-table obj@ gc-mark-obj
2147 macro-table obj@ gc-mark-obj
2148 console-i/o-port obj@ gc-mark-obj
2149 global-env obj@ gc-mark-obj
2151 depth gc-stack-depth @ do
2160 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
2161 ; is collect-garbage
2165 \ ---- Loading files ---- {{{
2167 : load ( addr n -- finalResult )
2172 ok-symbol ( port res )
2175 2over read-port ( port res obj )
2177 2dup EOF character-type objeq? if
2178 2drop 2swap close-port
2182 2swap 2drop ( port obj )
2186 global-env obj@ eval ( port res )
2192 \ ---- Standard Library ---- {{{
2194 include scheme-primitives.4th
2196 \ s" scheme-library.scm" load 2drop
2202 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2203 : repl-body ( -- bool )
2204 cr bold fg green ." > " reset-term
2208 2dup EOF character-type objeq? if
2210 bold fg blue ." Moriturus te saluto." reset-term cr
2216 global-env obj@ eval
2218 fg cyan ." ; " print reset-term
2228 \ Display welcome message
2229 \ welcome-symbol nil cons global-env obj@ eval 2drop
2234 recoverable-exception of false endof
2235 unrecoverable-exception of true endof