4 include term-colours.4th
7 include catch-throw.4th
33 make-type boolean-type
34 make-type character-type
40 make-type primitive-proc-type
41 make-type compound-proc-type
42 make-type continuation-type
44 : istype? ( obj type -- obj bool )
49 \ ---- Exceptions ---- {{{
51 variable nextexception
54 create nextexception @ ,
63 make-exception recoverable-exception
64 make-exception unrecoverable-exception
66 : throw reset-term cr throw ;
70 \ ---- List-structured memory ---- {{{
72 20000 constant scheme-memsize
74 create car-cells scheme-memsize allot
75 create car-type-cells scheme-memsize allot
76 create cdr-cells scheme-memsize allot
77 create cdr-type-cells scheme-memsize allot
79 create nextfrees scheme-memsize allot
90 nextfrees nextfree @ + @
93 nextfree @ scheme-memsize >= if
97 nextfree @ scheme-memsize >= if
98 except-message: ." Out of memory!" unrecoverable-exception throw
102 : cons ( car-obj cdr-obj -- pair-obj )
103 cdr-type-cells nextfree @ + !
104 cdr-cells nextfree @ + !
105 car-type-cells nextfree @ + !
106 car-cells nextfree @ + !
112 : car ( pair-obj -- car-obj )
114 dup car-cells + @ swap
118 : cdr ( pair-obj -- car-obj )
120 dup cdr-cells + @ swap
124 : set-car! ( obj pair-obj -- )
126 rot swap car-type-cells + !
130 : set-cdr! ( obj pair-obj -- )
132 rot swap cdr-type-cells + !
136 variable object-stack-base
137 : init-object-stack-base
138 depth object-stack-base ! ;
141 : nil? nil-type istype? ;
144 : none? none-type istype? ;
146 : objvar create nil swap , , ;
148 : value@ ( objvar -- val ) @ ;
149 : type@ ( objvar -- type ) 1+ @ ;
150 : value! ( newval objvar -- ) ! ;
151 : type! ( newtype objvar -- ) 1+ ! ;
152 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
153 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
155 : objeq? ( obj obj -- bool )
158 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
159 >R >R ( a1 a2 b1 b2 )
160 2swap ( b1 b2 a1 a2 )
161 R> R> ( b1 b2 a1 a2 c1 c2 )
165 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
166 2swap ( a1 a2 c1 c2 b1 b2 )
167 >R >R ( a1 a2 c1 c2 )
168 2swap ( c1 c2 a1 a2 )
172 : 2pick ( an bn an-1 bn-1 ... a0 b0 n -- an bn an-1 bn-1 ... a0 b0 an bn )
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 eof eof-symbol
277 create-symbol no-match no-match-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
476 : lookup-var ( var env -- val )
481 except-message: ." tried to read unbound variable '" var obj@ print ." '." recoverable-exception throw
485 : set-var ( var val env -- )
486 >R >R 2swap R> R> ( val var env )
489 2swap 2drop ( val vals )
492 except-message: ." tried to set unbound variable '" var obj@ print ." '." recoverable-exception throw
500 : define-var ( var val env -- )
503 2over env obj@ ( var val var env )
505 2swap 2drop ( var val vals )
510 first-frame ( var val frame )
517 : make-procedure ( params body env -- proc )
520 drop compound-proc-type
524 nil nil nil extend-env
529 \ ---- Continuations ---- {{{
531 : cons-return-stack ( -- listobj )
537 i 1+ @ fixnum-type 2swap cons
541 : cons-param-stack ( -- listobj )
544 depth 2- object-stack-base @ = if
548 depth 2- object-stack-base @ do
560 cons drop continuation-type
563 : restore-continuation
564 \ TODO: replace current parameter and return stacks with
565 \ contents of continuation object.
570 \ ---- Primitives ---- {{{
572 : make-primitive ( cfa -- )
579 rot primitive-proc-type ( var prim )
580 global-env obj@ define-var
583 : ensure-arg-count ( args n -- )
585 drop nil objeq? false = if
586 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
590 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
597 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
599 drop nil objeq? false = if
600 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
604 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
607 2dup cdr 2swap car ( ... t1 n args' arg1 )
608 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
610 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
618 : push-args-to-stack ( args -- arg1 arg2 ... argn )
628 : add-fa-checks ( cfa n -- cfa' )
629 here current @ 1+ dup @ , !
633 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
634 ['] push-args-to-stack ,
635 ['] lit , , ['] execute ,
639 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
640 here current @ 1+ dup @ , !
647 dup ( cfa t1 t2 ... tn n m )
652 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
658 ['] lit , , ['] ensure-arg-type-and-count ,
660 ['] push-args-to-stack ,
661 ['] lit , , ['] execute ,
667 : make-fa-primitive ( cfa n -- )
668 add-fa-checks make-primitive ;
670 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
671 add-fa-type-checks make-primitive ;
674 bold fg red ." Incorrect argument type." reset-term cr
678 : ensure-arg-type ( arg type -- arg )
680 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
687 \ ---- Macros ---- {{{
691 ( Look up macro in macro table. Returns nil if
693 : lookup-macro ( name_symbol -- proc )
695 symbol-type istype? invert if
696 \ Early exit if argument is not a symbol
718 : make-macro ( name_symbol params body env -- )
721 2swap ( proc name_symbol )
728 2over 2over ( proc name table name table )
730 2swap 2drop ( proc table )
742 macro-table obj@ cons
751 variable stored-parse-idx
752 create parse-str 161 allot
753 variable parse-str-span
755 create parse-idx-stack 10 allot
756 variable parse-idx-sp
757 parse-idx-stack parse-idx-sp !
760 parse-idx @ parse-idx-sp @ !
765 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
769 parse-idx-sp @ @ parse-idx ! ;
773 '\n' parse-str parse-str-span @ + !
774 1 parse-str-span +! ;
777 4 parse-str parse-str-span @ + !
778 1 parse-str-span +! ;
785 current-input-port obj@ console-i/o-port obj@ objeq? if
786 parse-str 160 expect cr
787 span @ parse-str-span !
789 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
790 drop swap parse-str-span !
792 parse-str-span @ 0= and if append-eof then
803 : charavailable? ( -- bool )
804 parse-str-span @ parse-idx @ > ;
806 : nextchar ( -- char )
807 charavailable? false = if getline then
808 parse-str parse-idx @ + @ ;
811 : whitespace? ( -- bool )
823 nextchar [char] ( = or
824 nextchar [char] ) = or
827 : commentstart? ( -- bool )
828 nextchar [char] ; = ;
832 false \ Indicates whether or not we're eating a comment
835 dup whitespace? or commentstart? or
837 dup nextchar '\n' = and if
838 invert \ Stop eating comment
840 dup false = commentstart? and if
841 invert \ Begin eating comment
856 nextchar [char] - = ;
859 nextchar [char] + = ;
861 : fixnum? ( -- bool )
887 : flonum? ( -- bool )
894 \ Record starting parse idx:
895 \ Want to detect whether any characters (following +/-) were eaten.
902 [char] . nextchar = if
909 [char] e nextchar = [char] E nextchar = or if
917 drop pop-parse-idx false exit
925 \ This is a real number if characters were
926 \ eaten and the next characer is a delimiter.
927 parse-idx @ < delim? and
932 : ratnum? ( -- bool )
940 pop-parse-idx false exit
949 [char] / nextchar <> if
950 pop-parse-idx false exit
956 pop-parse-idx false exit
968 : boolean? ( -- bool )
969 nextchar [char] # <> if false exit then
976 and if pop-parse-idx false exit then
988 : str-equiv? ( str -- bool )
1005 delim? false = if drop false then
1010 : character? ( -- bool )
1011 nextchar [char] # <> if false exit then
1016 nextchar [char] \ <> if pop-parse-idx false exit then
1020 S" newline" str-equiv? if pop-parse-idx true exit then
1021 S" space" str-equiv? if pop-parse-idx true exit then
1022 S" tab" str-equiv? if pop-parse-idx true exit then
1024 charavailable? false = if pop-parse-idx false exit then
1030 nextchar [char] ( = ;
1032 : string? ( -- bool )
1033 nextchar [char] " = ;
1035 : readfixnum ( -- fixnum )
1046 10 * nextchar [char] 0 - +
1055 : readflonum ( -- flonum )
1057 dup 0< swap abs i->f
1059 [char] . nextchar = if
1065 nextchar [char] 0 - i->f ( f exp d )
1066 over f/ rot f+ ( exp f' )
1067 swap 10.0 f* ( f' exp' )
1074 [char] e nextchar = [char] E nextchar = or if
1077 readfixnum drop i->f
1088 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1095 fixnum-type swap fixnum-type
1096 cons drop ratnum-type
1100 : readratnum ( -- ratnum )
1101 readfixnum inc-parse-idx readfixnum
1105 : readbool ( -- bool-obj )
1108 nextchar [char] f = if
1119 : readchar ( -- char-obj )
1123 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1124 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1125 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1127 nextchar character-type
1132 : readstring ( -- charlist )
1137 nextchar [char] " <>
1139 nextchar [char] \ = if
1142 [char] n of '\n' endof
1143 [char] " of [char] " endof
1149 inc-parse-idx character-type
1152 ( firstchar prevchar thischar )
1155 2drop 2swap 2drop 2dup ( thischar thischar )
1157 ( firstchar thischar prevchar )
1158 2over 2swap set-cdr! ( firstchar thischar )
1162 \ Discard previous character
1168 ." No delimiter following right double quote. Aborting." cr
1180 : readsymbol ( -- charlist )
1181 delim? if nil exit then
1183 nextchar inc-parse-idx character-type
1190 : readpair ( -- pairobj )
1194 nextchar [char] ) = if
1199 ." No delimiter following right paren. Aborting." cr
1208 \ Read first pair element
1213 nextchar [char] . = if
1218 ." No delimiter following '.'. Aborting." cr
1232 \ Parse a scheme expression
1267 nextchar [char] " <> if
1268 bold red ." Missing closing double-quote." reset-term cr
1286 nextchar [char] ) <> if
1287 bold red ." Missing closing paren." reset-term cr
1296 nextchar [char] ' = if
1298 quote-symbol recurse nil cons cons exit
1301 nextchar [char] ` = if
1303 quasiquote-symbol recurse nil cons cons exit
1306 nextchar [char] , = if
1308 nextchar [char] @ = if
1310 unquote-splicing-symbol recurse nil cons cons exit
1312 unquote-symbol recurse nil cons cons exit
1322 nextchar [char] ) = if
1324 except-message: ." unmatched closing parenthesis." recoverable-exception throw
1327 \ Anything else is parsed as a symbol
1328 readsymbol charlist>symbol
1330 \ Replace λ with lambda
1331 2dup λ-symbol objeq? if
1340 \ ---- Syntax ---- {{{
1342 : self-evaluating? ( obj -- obj bool )
1343 boolean-type istype? if true exit then
1344 fixnum-type istype? if true exit then
1345 flonum-type istype? if true exit then
1346 ratnum-type istype? if true exit then
1347 character-type istype? if true exit then
1348 string-type istype? if true exit then
1349 nil-type istype? if true exit then
1350 none-type istype? if true exit then
1355 : tagged-list? ( obj tag-obj -- obj bool )
1357 pair-type istype? false = if
1363 : quote? ( obj -- obj bool )
1364 quote-symbol tagged-list? ;
1366 : quote-body ( quote-obj -- quote-body-obj )
1369 : variable? ( obj -- obj bool )
1370 symbol-type istype? ;
1372 : definition? ( obj -- obj bool )
1373 define-symbol tagged-list? ;
1375 : definition-var ( obj -- var )
1378 : definition-val ( obj -- val )
1381 : assignment? ( obj -- obj bool )
1382 set!-symbol tagged-list? ;
1384 : assignment-var ( obj -- var )
1387 : assignment-val ( obj -- val )
1390 : macro-definition? ( obj -- obj bool )
1391 define-macro-symbol tagged-list? ;
1393 : macro-definition-name ( exp -- mname )
1396 : macro-definition-params ( exp -- params )
1399 : macro-definition-body ( exp -- body )
1402 : if? ( obj -- obj bool )
1403 if-symbol tagged-list? ;
1405 : if-predicate ( ifobj -- pred )
1408 : if-consequent ( ifobj -- conseq )
1411 : if-alternative ( ifobj -- alt|none )
1419 : false? ( boolobj -- boolean )
1420 boolean-type istype? if
1421 false boolean-type objeq?
1427 : true? ( boolobj -- bool )
1430 : lambda? ( obj -- obj bool )
1431 lambda-symbol tagged-list? ;
1433 : lambda-parameters ( obj -- params )
1436 : lambda-body ( obj -- body )
1439 : application? ( obj -- obj bool )
1442 : operator ( obj -- operator )
1445 : operands ( obj -- operands )
1448 : nooperands? ( operands -- bool )
1451 : first-operand ( operands -- operand )
1454 : rest-operands ( operands -- other-operands )
1457 : procedure-params ( proc -- params )
1458 drop pair-type car ;
1460 : procedure-body ( proc -- body )
1461 drop pair-type cdr car ;
1463 : procedure-env ( proc -- body )
1464 drop pair-type cdr cdr car ;
1466 ( Ensure terminating symbol arg name is handled
1467 specially to allow for variadic procedures. )
1468 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1470 2over nil? false = if
1471 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1478 symbol-type istype? if
1488 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
1495 recurse ( argvals argnames argvals'' argnames'' )
1496 2rot car 2swap cons ( argvals argvals'' argnames' )
1497 2rot car 2rot cons ( argnames' argvals' )
1503 \ ---- Analyze ---- {{{
1505 : evaluate-eproc ( eproc env --- res )
1516 2drop \ get rid of null
1520 \ Final element of eproc list is primitive procedure
1521 drop \ dump type signifier
1523 goto \ jump straight to primitive procedure (executor)
1526 : self-evaluating-executor ( exp env -- exp )
1529 : analyze-self-evaluating ( exp --- eproc )
1530 ['] self-evaluating-executor primitive-proc-type
1534 : quote-executor ( exp env -- exp )
1537 : analyze-quoted ( exp -- eproc )
1540 ['] quote-executor primitive-proc-type
1544 : variable-executor ( var env -- val )
1547 : analyze-variable ( exp -- eproc )
1548 ['] variable-executor primitive-proc-type
1552 : definition-executor ( var val-eproc env -- ok )
1553 2swap 2over ( var env val-eproc env )
1554 evaluate-eproc 2swap ( var val env )
1559 : analyze-definition ( exp -- eproc )
1561 2swap definition-val analyze
1563 ['] definition-executor primitive-proc-type
1567 : assignment-executor ( var val-eproc env -- ok )
1568 2swap 2over ( var env val-eproc env )
1569 evaluate-eproc 2swap ( var val env )
1574 : analyze-assignment ( exp -- eproc )
1576 2swap assignment-val analyze ( var val-eproc )
1578 ['] assignment-executor primitive-proc-type
1582 : sequence-executor ( eproc-list env -- res )
1586 2dup cdr ( env elist elist-rest)
1589 -2rot car 2over ( elist-rest env elist-head env )
1590 evaluate-eproc ( elist-rest env head-res )
1591 2drop 2swap ( env elist-rest )
1595 ['] evaluate-eproc goto
1599 : (analyze-sequence) ( explist -- eproc-list )
1608 : analyze-sequence ( explist -- eproc )
1610 ['] sequence-executor primitive-proc-type
1615 : macro-definition-executor ( name params bproc env -- ok )
1616 make-macro ok-symbol
1619 : analyze-macro-definition ( exp -- eproc )
1620 2dup macro-definition-name
1621 2swap 2dup macro-definition-params
1622 2swap macro-definition-body analyze-sequence
1624 ['] macro-definition-executor primitive-proc-type
1625 nil cons cons cons cons
1628 : if-executor ( cproc aproc pproc env -- res )
1629 2swap 2over ( cproc aproc env pproc env -- res )
1638 ['] evaluate-eproc goto
1641 : analyze-if ( exp -- eproc )
1642 2dup if-consequent analyze
1643 2swap 2dup if-alternative analyze
1644 2swap if-predicate analyze
1646 ['] if-executor primitive-proc-type
1647 nil cons cons cons cons
1650 : lambda-executor ( params bproc env -- res )
1652 ( Although this is packaged up as a regular compound procedure,
1653 the "body" element contains an _eproc_ to be evaluated in an
1654 environment resulting from extending env with the parameter
1658 : analyze-lambda ( exp -- eproc )
1659 2dup lambda-parameters
1663 except-message: ." encountered lambda with an empty body." recoverable-exception throw
1668 ['] lambda-executor primitive-proc-type
1672 : operand-eproc-list ( operands -- eprocs )
1680 : evaluate-operand-eprocs ( env aprocs -- vals )
1684 2over 2over car 2swap evaluate-eproc ( env aprocs thisval )
1685 -2rot cdr recurse ( thisval restvals )
1690 : apply ( vals proc )
1692 primitive-proc-type of
1696 compound-proc-type of
1697 2dup procedure-body ( argvals proc bproc )
1698 -2rot 2dup procedure-params ( bproc argvals proc argnames )
1699 -2rot procedure-env ( bproc argnames argvals procenv )
1705 extend-env ( bproc env )
1707 ['] evaluate-eproc goto
1710 continuation-type of
1711 \ TODO: Apply continuation
1714 except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
1718 : application-executor ( operator-proc arg-procs env -- res )
1719 2rot 2over ( aprocs env fproc env )
1720 evaluate-eproc ( aprocs env proc )
1722 -2rot 2swap ( proc env aprocs )
1723 evaluate-operand-eprocs ( proc vals )
1730 : analyze-application ( exp -- eproc )
1731 2dup operator analyze
1732 2swap operands operand-eproc-list
1734 ['] application-executor primitive-proc-type
1738 :noname ( exp --- eproc )
1740 self-evaluating? if analyze-self-evaluating exit then
1742 quote? if analyze-quoted exit then
1744 variable? if analyze-variable exit then
1746 definition? if analyze-definition exit then
1748 assignment? if analyze-assignment exit then
1750 macro-definition? if analyze-macro-definition exit then
1752 if? if analyze-if exit then
1754 lambda? if analyze-lambda exit then
1756 application? if analyze-application exit then
1758 except-message: ." tried to analyze unknown expression type." recoverable-exception throw
1764 \ ---- Macro Expansion ---- {{{
1766 ( Simply evaluates the given procedure with expbody as its argument. )
1767 : macro-eval ( proc expbody -- result )
1769 2dup procedure-body ( expbody proc bproc )
1770 -2rot 2dup procedure-params ( bproc expbody proc argnames )
1771 -2rot procedure-env ( bproc argnames expbody procenv )
1777 extend-env ( bproc env )
1779 ['] evaluate-eproc goto
1782 : expand-macro ( exp -- result )
1783 pair-type istype? invert if exit then
1785 2dup car symbol-type istype? invert if 2drop exit then
1787 lookup-macro nil? if 2drop exit then
1789 2over cdr macro-eval
1791 2dup no-match-symbol objeq? if
1797 R> drop ['] expand goto-deferred
1800 : expand-definition ( exp -- result )
1804 2swap definition-val expand
1805 nil ( define var val' nil )
1809 : expand-assignment ( exp -- result )
1813 2swap assignment-val expand
1814 nil ( define var val' nil )
1818 : expand-list ( exp -- res )
1826 : macro-definition-nameparams
1829 : expand-define-macro ( exp -- res )
1830 define-macro-symbol 2swap
1831 2dup macro-definition-nameparams
1832 2swap macro-definition-body expand-list
1836 : expand-lambda ( exp -- res )
1838 2dup lambda-parameters
1839 2swap lambda-body expand-list
1843 : expand-if ( exp -- res )
1846 2dup if-predicate expand
1847 2swap 2dup if-consequent expand
1848 2swap if-alternative none? if
1856 : expand-application ( exp -- res )
1857 2dup operator expand
1858 2swap operands expand-list
1862 :noname ( exp -- result )
1865 self-evaluating? if exit then
1869 definition? if expand-definition exit then
1871 assignment? if expand-assignment exit then
1873 macro-definition? if expand-define-macro exit then
1875 lambda? if expand-lambda exit then
1877 if? if expand-if exit then
1879 application? if expand-application exit then
1885 :noname ( exp env -- res )
1886 2swap expand analyze 2swap evaluate-eproc
1889 \ ---- Print ---- {{{
1891 : printfixnum ( fixnum -- ) drop 0 .R ;
1893 : printflonum ( flonum -- ) drop f. ;
1895 : printratnum ( ratnum -- )
1897 car print ." /" cdr print
1900 : printbool ( bool -- )
1908 : printchar ( charobj -- )
1911 9 of ." #\tab" endof
1912 bl of ." #\space" endof
1913 '\n' of ." #\newline" endof
1919 : (printstring) ( stringobj -- )
1920 nil? if 2drop exit then
1924 '\n' of ." \n" drop endof
1925 [char] \ of ." \\" drop endof
1926 [char] " of [char] \ emit [char] " emit drop endof
1932 : printstring ( stringobj -- )
1937 : printsymbol ( symbolobj -- )
1938 nil-type istype? if 2drop exit then
1944 : printnil ( nilobj -- )
1947 : printpair ( pairobj -- )
1951 nil-type istype? if 2drop exit then
1952 pair-type istype? if space recurse exit then
1956 : printprim ( primobj -- )
1957 2drop ." <primitive procedure>" ;
1959 : printcomp ( primobj -- )
1960 2drop ." <compound procedure>" ;
1962 : printcont ( primobj --)
1963 2drop ." <continuation>" ;
1965 : printnone ( noneobj -- )
1966 2drop ." Unspecified return value" ;
1968 : printport ( port -- )
1972 fixnum-type istype? if printfixnum exit then
1973 flonum-type istype? if printflonum exit then
1974 ratnum-type istype? if printratnum exit then
1975 boolean-type istype? if printbool exit then
1976 character-type istype? if printchar exit then
1977 string-type istype? if printstring exit then
1978 symbol-type istype? if printsymbol exit then
1979 nil-type istype? if printnil exit then
1980 pair-type istype? if ." (" printpair ." )" exit then
1981 primitive-proc-type istype? if printprim exit then
1982 compound-proc-type istype? if printcomp exit then
1983 continuation-type istype? if printcont exit then
1984 none-type istype? if printnone exit then
1985 port-type istype? if printport exit then
1987 except-message: ." tried to print object with unknown type." recoverable-exception throw
1992 \ ---- Garbage Collection ---- {{{
1994 : pairlike? ( obj -- obj bool )
1995 pair-type istype? if true exit then
1996 string-type istype? if true exit then
1997 symbol-type istype? if true exit then
1998 compound-proc-type istype? if true exit then
1999 port-type istype? if true exit then
2004 : pairlike-marked? ( obj -- obj bool )
2005 over nextfrees + @ 0=
2008 : mark-pairlike ( obj -- obj )
2009 over nextfrees + 0 swap !
2018 : gc-mark-obj ( obj -- )
2020 pairlike? invert if 2drop exit then
2021 pairlike-marked? if 2drop exit then
2032 scheme-memsize nextfree !
2033 0 scheme-memsize 1- do
2034 nextfrees i + @ 0<> if
2035 nextfree @ nextfrees i + !
2041 \ Following a GC, this gives the amount of free memory
2045 nextfrees i + @ 0= if 1+ then
2049 \ Debugging word - helps spot memory that is retained
2052 nextfrees i + @ 0<> if
2064 symbol-table obj@ gc-mark-obj
2065 macro-table obj@ gc-mark-obj
2066 console-i/o-port obj@ gc-mark-obj
2067 global-env obj@ gc-mark-obj
2069 depth object-stack-base @ do
2078 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
2079 ; is collect-garbage
2083 \ ---- Loading files ---- {{{
2085 : load ( addr n -- finalResult )
2090 ok-symbol ( port res )
2094 \ bold fg blue ." READ from " 2over drop . ." ==> " reset-term
2096 2over read-port ( port res obj )
2101 2dup EOF character-type objeq? if
2102 2drop 2swap close-port
2106 2swap 2drop ( port obj )
2108 global-env obj@ eval ( port res )
2114 \ ---- Standard Library ---- {{{
2116 include scheme-primitives.4th
2118 init-object-stack-base
2119 s" scheme-library.scm" load 2drop
2125 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2126 : repl-body ( -- bool )
2127 cr bold fg green ." > " reset-term
2131 2dup EOF character-type objeq? if
2133 bold fg blue ." Moriturus te saluto." reset-term cr
2137 global-env obj@ eval
2139 fg cyan ." ; " print reset-term
2147 init-object-stack-base
2149 \ Display welcome message
2150 welcome-symbol nil cons global-env obj@ eval 2drop
2155 recoverable-exception of false endof
2156 unrecoverable-exception of true endof