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 : continuation->pstack-list
566 : continuation->rstack-list
569 : restore-param-stack ( continuation -- obj_stack continuation )
572 continuation->pstack-list
574 ( Idea: allocate stack space first using psp!,
575 then copy objects from list. )
578 : restore-continuation
579 \ TODO: replace current parameter and return stacks with
580 \ contents of continuation object.
585 \ ---- Primitives ---- {{{
587 : make-primitive ( cfa -- )
594 rot primitive-proc-type ( var prim )
595 global-env obj@ define-var
598 : ensure-arg-count ( args n -- )
600 drop nil objeq? false = if
601 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
605 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
612 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
614 drop nil objeq? false = if
615 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
619 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
622 2dup cdr 2swap car ( ... t1 n args' arg1 )
623 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
625 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
633 : push-args-to-stack ( args -- arg1 arg2 ... argn )
643 : add-fa-checks ( cfa n -- cfa' )
644 here current @ 1+ dup @ , !
648 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
649 ['] push-args-to-stack ,
650 ['] lit , , ['] execute ,
654 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
655 here current @ 1+ dup @ , !
662 dup ( cfa t1 t2 ... tn n m )
667 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
673 ['] lit , , ['] ensure-arg-type-and-count ,
675 ['] push-args-to-stack ,
676 ['] lit , , ['] execute ,
682 : make-fa-primitive ( cfa n -- )
683 add-fa-checks make-primitive ;
685 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
686 add-fa-type-checks make-primitive ;
689 bold fg red ." Incorrect argument type." reset-term cr
693 : ensure-arg-type ( arg type -- arg )
695 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
702 \ ---- Macros ---- {{{
706 ( Look up macro in macro table. Returns nil if
708 : lookup-macro ( name_symbol -- proc )
710 symbol-type istype? invert if
711 \ Early exit if argument is not a symbol
733 : make-macro ( name_symbol params body env -- )
736 2swap ( proc name_symbol )
743 2over 2over ( proc name table name table )
745 2swap 2drop ( proc table )
757 macro-table obj@ cons
766 variable stored-parse-idx
767 create parse-str 161 allot
768 variable parse-str-span
770 create parse-idx-stack 10 allot
771 variable parse-idx-sp
772 parse-idx-stack parse-idx-sp !
775 parse-idx @ parse-idx-sp @ !
780 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
784 parse-idx-sp @ @ parse-idx ! ;
788 '\n' parse-str parse-str-span @ + !
789 1 parse-str-span +! ;
792 4 parse-str parse-str-span @ + !
793 1 parse-str-span +! ;
800 current-input-port obj@ console-i/o-port obj@ objeq? if
801 parse-str 160 expect cr
802 span @ parse-str-span !
804 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
805 drop swap parse-str-span !
807 parse-str-span @ 0= and if append-eof then
818 : charavailable? ( -- bool )
819 parse-str-span @ parse-idx @ > ;
821 : nextchar ( -- char )
822 charavailable? false = if getline then
823 parse-str parse-idx @ + @ ;
826 : whitespace? ( -- bool )
838 nextchar [char] ( = or
839 nextchar [char] ) = or
842 : commentstart? ( -- bool )
843 nextchar [char] ; = ;
847 false \ Indicates whether or not we're eating a comment
850 dup whitespace? or commentstart? or
852 dup nextchar '\n' = and if
853 invert \ Stop eating comment
855 dup false = commentstart? and if
856 invert \ Begin eating comment
871 nextchar [char] - = ;
874 nextchar [char] + = ;
876 : fixnum? ( -- bool )
902 : flonum? ( -- bool )
909 \ Record starting parse idx:
910 \ Want to detect whether any characters (following +/-) were eaten.
917 [char] . nextchar = if
924 [char] e nextchar = [char] E nextchar = or if
932 drop pop-parse-idx false exit
940 \ This is a real number if characters were
941 \ eaten and the next characer is a delimiter.
942 parse-idx @ < delim? and
947 : ratnum? ( -- bool )
955 pop-parse-idx false exit
964 [char] / nextchar <> if
965 pop-parse-idx false exit
971 pop-parse-idx false exit
983 : boolean? ( -- bool )
984 nextchar [char] # <> if false exit then
991 and if pop-parse-idx false exit then
1003 : str-equiv? ( str -- bool )
1020 delim? false = if drop false then
1025 : character? ( -- bool )
1026 nextchar [char] # <> if false exit then
1031 nextchar [char] \ <> if pop-parse-idx false exit then
1035 S" newline" str-equiv? if pop-parse-idx true exit then
1036 S" space" str-equiv? if pop-parse-idx true exit then
1037 S" tab" str-equiv? if pop-parse-idx true exit then
1039 charavailable? false = if pop-parse-idx false exit then
1045 nextchar [char] ( = ;
1047 : string? ( -- bool )
1048 nextchar [char] " = ;
1050 : readfixnum ( -- fixnum )
1061 10 * nextchar [char] 0 - +
1070 : readflonum ( -- flonum )
1072 dup 0< swap abs i->f
1074 [char] . nextchar = if
1080 nextchar [char] 0 - i->f ( f exp d )
1081 over f/ rot f+ ( exp f' )
1082 swap 10.0 f* ( f' exp' )
1089 [char] e nextchar = [char] E nextchar = or if
1092 readfixnum drop i->f
1103 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1110 fixnum-type swap fixnum-type
1111 cons drop ratnum-type
1115 : readratnum ( -- ratnum )
1116 readfixnum inc-parse-idx readfixnum
1120 : readbool ( -- bool-obj )
1123 nextchar [char] f = if
1134 : readchar ( -- char-obj )
1138 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1139 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1140 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1142 nextchar character-type
1147 : readstring ( -- charlist )
1152 nextchar [char] " <>
1154 nextchar [char] \ = if
1157 [char] n of '\n' endof
1158 [char] " of [char] " endof
1164 inc-parse-idx character-type
1167 ( firstchar prevchar thischar )
1170 2drop 2swap 2drop 2dup ( thischar thischar )
1172 ( firstchar thischar prevchar )
1173 2over 2swap set-cdr! ( firstchar thischar )
1177 \ Discard previous character
1183 ." No delimiter following right double quote. Aborting." cr
1195 : readsymbol ( -- charlist )
1196 delim? if nil exit then
1198 nextchar inc-parse-idx character-type
1205 : readpair ( -- pairobj )
1209 nextchar [char] ) = if
1214 ." No delimiter following right paren. Aborting." cr
1223 \ Read first pair element
1228 nextchar [char] . = if
1233 ." No delimiter following '.'. Aborting." cr
1247 \ Parse a scheme expression
1282 nextchar [char] " <> if
1283 bold red ." Missing closing double-quote." reset-term cr
1301 nextchar [char] ) <> if
1302 bold red ." Missing closing paren." reset-term cr
1311 nextchar [char] ' = if
1313 quote-symbol recurse nil cons cons exit
1316 nextchar [char] ` = if
1318 quasiquote-symbol recurse nil cons cons exit
1321 nextchar [char] , = if
1323 nextchar [char] @ = if
1325 unquote-splicing-symbol recurse nil cons cons exit
1327 unquote-symbol recurse nil cons cons exit
1337 nextchar [char] ) = if
1339 except-message: ." unmatched closing parenthesis." recoverable-exception throw
1342 \ Anything else is parsed as a symbol
1343 readsymbol charlist>symbol
1345 \ Replace λ with lambda
1346 2dup λ-symbol objeq? if
1355 \ ---- Syntax ---- {{{
1357 : self-evaluating? ( obj -- obj bool )
1358 boolean-type istype? if true exit then
1359 fixnum-type istype? if true exit then
1360 flonum-type istype? if true exit then
1361 ratnum-type istype? if true exit then
1362 character-type istype? if true exit then
1363 string-type istype? if true exit then
1364 nil-type istype? if true exit then
1365 none-type istype? if true exit then
1370 : tagged-list? ( obj tag-obj -- obj bool )
1372 pair-type istype? false = if
1378 : quote? ( obj -- obj bool )
1379 quote-symbol tagged-list? ;
1381 : quote-body ( quote-obj -- quote-body-obj )
1384 : variable? ( obj -- obj bool )
1385 symbol-type istype? ;
1387 : definition? ( obj -- obj bool )
1388 define-symbol tagged-list? ;
1390 : definition-var ( obj -- var )
1393 : definition-val ( obj -- val )
1396 : assignment? ( obj -- obj bool )
1397 set!-symbol tagged-list? ;
1399 : assignment-var ( obj -- var )
1402 : assignment-val ( obj -- val )
1405 : macro-definition? ( obj -- obj bool )
1406 define-macro-symbol tagged-list? ;
1408 : macro-definition-name ( exp -- mname )
1411 : macro-definition-params ( exp -- params )
1414 : macro-definition-body ( exp -- body )
1417 : if? ( obj -- obj bool )
1418 if-symbol tagged-list? ;
1420 : if-predicate ( ifobj -- pred )
1423 : if-consequent ( ifobj -- conseq )
1426 : if-alternative ( ifobj -- alt|none )
1434 : false? ( boolobj -- boolean )
1435 boolean-type istype? if
1436 false boolean-type objeq?
1442 : true? ( boolobj -- bool )
1445 : lambda? ( obj -- obj bool )
1446 lambda-symbol tagged-list? ;
1448 : lambda-parameters ( obj -- params )
1451 : lambda-body ( obj -- body )
1454 : application? ( obj -- obj bool )
1457 : operator ( obj -- operator )
1460 : operands ( obj -- operands )
1463 : nooperands? ( operands -- bool )
1466 : first-operand ( operands -- operand )
1469 : rest-operands ( operands -- other-operands )
1472 : procedure-params ( proc -- params )
1473 drop pair-type car ;
1475 : procedure-body ( proc -- body )
1476 drop pair-type cdr car ;
1478 : procedure-env ( proc -- body )
1479 drop pair-type cdr cdr car ;
1481 ( Ensure terminating symbol arg name is handled
1482 specially to allow for variadic procedures. )
1483 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1485 2over nil? false = if
1486 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1493 symbol-type istype? if
1503 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
1510 recurse ( argvals argnames argvals'' argnames'' )
1511 2rot car 2swap cons ( argvals argvals'' argnames' )
1512 2rot car 2rot cons ( argnames' argvals' )
1518 \ ---- Analyze ---- {{{
1520 : evaluate-eproc ( eproc env --- res )
1531 2drop \ get rid of null
1535 \ Final element of eproc list is primitive procedure
1536 drop \ dump type signifier
1538 goto \ jump straight to primitive procedure (executor)
1541 : self-evaluating-executor ( exp env -- exp )
1544 : analyze-self-evaluating ( exp --- eproc )
1545 ['] self-evaluating-executor primitive-proc-type
1549 : quote-executor ( exp env -- exp )
1552 : analyze-quoted ( exp -- eproc )
1555 ['] quote-executor primitive-proc-type
1559 : variable-executor ( var env -- val )
1562 : analyze-variable ( exp -- eproc )
1563 ['] variable-executor primitive-proc-type
1567 : definition-executor ( var val-eproc env -- ok )
1568 2swap 2over ( var env val-eproc env )
1569 evaluate-eproc 2swap ( var val env )
1574 : analyze-definition ( exp -- eproc )
1576 2swap definition-val analyze
1578 ['] definition-executor primitive-proc-type
1582 : assignment-executor ( var val-eproc env -- ok )
1583 2swap 2over ( var env val-eproc env )
1584 evaluate-eproc 2swap ( var val env )
1589 : analyze-assignment ( exp -- eproc )
1591 2swap assignment-val analyze ( var val-eproc )
1593 ['] assignment-executor primitive-proc-type
1597 : sequence-executor ( eproc-list env -- res )
1601 2dup cdr ( env elist elist-rest)
1604 -2rot car 2over ( elist-rest env elist-head env )
1605 evaluate-eproc ( elist-rest env head-res )
1606 2drop 2swap ( env elist-rest )
1610 ['] evaluate-eproc goto
1614 : (analyze-sequence) ( explist -- eproc-list )
1623 : analyze-sequence ( explist -- eproc )
1625 ['] sequence-executor primitive-proc-type
1630 : macro-definition-executor ( name params bproc env -- ok )
1631 make-macro ok-symbol
1634 : analyze-macro-definition ( exp -- eproc )
1635 2dup macro-definition-name
1636 2swap 2dup macro-definition-params
1637 2swap macro-definition-body analyze-sequence
1639 ['] macro-definition-executor primitive-proc-type
1640 nil cons cons cons cons
1643 : if-executor ( cproc aproc pproc env -- res )
1644 2swap 2over ( cproc aproc env pproc env -- res )
1653 ['] evaluate-eproc goto
1656 : analyze-if ( exp -- eproc )
1657 2dup if-consequent analyze
1658 2swap 2dup if-alternative analyze
1659 2swap if-predicate analyze
1661 ['] if-executor primitive-proc-type
1662 nil cons cons cons cons
1665 : lambda-executor ( params bproc env -- res )
1667 ( Although this is packaged up as a regular compound procedure,
1668 the "body" element contains an _eproc_ to be evaluated in an
1669 environment resulting from extending env with the parameter
1673 : analyze-lambda ( exp -- eproc )
1674 2dup lambda-parameters
1678 except-message: ." encountered lambda with an empty body." recoverable-exception throw
1683 ['] lambda-executor primitive-proc-type
1687 : operand-eproc-list ( operands -- eprocs )
1695 : evaluate-operand-eprocs ( env aprocs -- vals )
1699 2over 2over car 2swap evaluate-eproc ( env aprocs thisval )
1700 -2rot cdr recurse ( thisval restvals )
1705 : apply ( vals proc )
1707 primitive-proc-type of
1711 compound-proc-type of
1712 2dup procedure-body ( argvals proc bproc )
1713 -2rot 2dup procedure-params ( bproc argvals proc argnames )
1714 -2rot procedure-env ( bproc argnames argvals procenv )
1720 extend-env ( bproc env )
1722 ['] evaluate-eproc goto
1725 continuation-type of
1726 \ TODO: Apply continuation
1729 except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
1733 : application-executor ( operator-proc arg-procs env -- res )
1734 2rot 2over ( aprocs env fproc env )
1735 evaluate-eproc ( aprocs env proc )
1737 -2rot 2swap ( proc env aprocs )
1738 evaluate-operand-eprocs ( proc vals )
1745 : analyze-application ( exp -- eproc )
1746 2dup operator analyze
1747 2swap operands operand-eproc-list
1749 ['] application-executor primitive-proc-type
1753 :noname ( exp --- eproc )
1755 self-evaluating? if analyze-self-evaluating exit then
1757 quote? if analyze-quoted exit then
1759 variable? if analyze-variable exit then
1761 definition? if analyze-definition exit then
1763 assignment? if analyze-assignment exit then
1765 macro-definition? if analyze-macro-definition exit then
1767 if? if analyze-if exit then
1769 lambda? if analyze-lambda exit then
1771 application? if analyze-application exit then
1773 except-message: ." tried to analyze unknown expression type." recoverable-exception throw
1779 \ ---- Macro Expansion ---- {{{
1781 ( Simply evaluates the given procedure with expbody as its argument. )
1782 : macro-eval ( proc expbody -- result )
1784 2dup procedure-body ( expbody proc bproc )
1785 -2rot 2dup procedure-params ( bproc expbody proc argnames )
1786 -2rot procedure-env ( bproc argnames expbody procenv )
1792 extend-env ( bproc env )
1794 ['] evaluate-eproc goto
1797 : expand-macro ( exp -- result )
1798 pair-type istype? invert if exit then
1800 2dup car symbol-type istype? invert if 2drop exit then
1802 lookup-macro nil? if 2drop exit then
1804 2over cdr macro-eval
1806 2dup no-match-symbol objeq? if
1812 R> drop ['] expand goto-deferred
1815 : expand-definition ( exp -- result )
1819 2swap definition-val expand
1820 nil ( define var val' nil )
1824 : expand-assignment ( exp -- result )
1828 2swap assignment-val expand
1829 nil ( define var val' nil )
1833 : expand-list ( exp -- res )
1841 : macro-definition-nameparams
1844 : expand-define-macro ( exp -- res )
1845 define-macro-symbol 2swap
1846 2dup macro-definition-nameparams
1847 2swap macro-definition-body expand-list
1851 : expand-lambda ( exp -- res )
1853 2dup lambda-parameters
1854 2swap lambda-body expand-list
1858 : expand-if ( exp -- res )
1861 2dup if-predicate expand
1862 2swap 2dup if-consequent expand
1863 2swap if-alternative none? if
1871 : expand-application ( exp -- res )
1872 2dup operator expand
1873 2swap operands expand-list
1877 :noname ( exp -- result )
1880 self-evaluating? if exit then
1884 definition? if expand-definition exit then
1886 assignment? if expand-assignment exit then
1888 macro-definition? if expand-define-macro exit then
1890 lambda? if expand-lambda exit then
1892 if? if expand-if exit then
1894 application? if expand-application exit then
1900 :noname ( exp env -- res )
1901 2swap expand analyze 2swap evaluate-eproc
1904 \ ---- Print ---- {{{
1906 : printfixnum ( fixnum -- ) drop 0 .R ;
1908 : printflonum ( flonum -- ) drop f. ;
1910 : printratnum ( ratnum -- )
1912 car print ." /" cdr print
1915 : printbool ( bool -- )
1923 : printchar ( charobj -- )
1926 9 of ." #\tab" endof
1927 bl of ." #\space" endof
1928 '\n' of ." #\newline" endof
1934 : (printstring) ( stringobj -- )
1935 nil? if 2drop exit then
1939 '\n' of ." \n" drop endof
1940 [char] \ of ." \\" drop endof
1941 [char] " of [char] \ emit [char] " emit drop endof
1947 : printstring ( stringobj -- )
1952 : printsymbol ( symbolobj -- )
1953 nil-type istype? if 2drop exit then
1959 : printnil ( nilobj -- )
1962 : printpair ( pairobj -- )
1966 nil-type istype? if 2drop exit then
1967 pair-type istype? if space recurse exit then
1971 : printprim ( primobj -- )
1972 2drop ." <primitive procedure>" ;
1974 : printcomp ( primobj -- )
1975 2drop ." <compound procedure>" ;
1977 : printcont ( primobj --)
1978 2drop ." <continuation>" ;
1980 : printnone ( noneobj -- )
1981 2drop ." Unspecified return value" ;
1983 : printport ( port -- )
1987 fixnum-type istype? if printfixnum exit then
1988 flonum-type istype? if printflonum exit then
1989 ratnum-type istype? if printratnum exit then
1990 boolean-type istype? if printbool exit then
1991 character-type istype? if printchar exit then
1992 string-type istype? if printstring exit then
1993 symbol-type istype? if printsymbol exit then
1994 nil-type istype? if printnil exit then
1995 pair-type istype? if ." (" printpair ." )" exit then
1996 primitive-proc-type istype? if printprim exit then
1997 compound-proc-type istype? if printcomp exit then
1998 continuation-type istype? if printcont exit then
1999 none-type istype? if printnone exit then
2000 port-type istype? if printport exit then
2002 except-message: ." tried to print object with unknown type." recoverable-exception throw
2007 \ ---- Garbage Collection ---- {{{
2009 : pairlike? ( obj -- obj bool )
2010 pair-type istype? if true exit then
2011 string-type istype? if true exit then
2012 symbol-type istype? if true exit then
2013 compound-proc-type istype? if true exit then
2014 port-type istype? if true exit then
2019 : pairlike-marked? ( obj -- obj bool )
2020 over nextfrees + @ 0=
2023 : mark-pairlike ( obj -- obj )
2024 over nextfrees + 0 swap !
2033 : gc-mark-obj ( obj -- )
2035 pairlike? invert if 2drop exit then
2036 pairlike-marked? if 2drop exit then
2047 scheme-memsize nextfree !
2048 0 scheme-memsize 1- do
2049 nextfrees i + @ 0<> if
2050 nextfree @ nextfrees i + !
2056 \ Following a GC, this gives the amount of free memory
2060 nextfrees i + @ 0= if 1+ then
2064 \ Debugging word - helps spot memory that is retained
2067 nextfrees i + @ 0<> if
2079 symbol-table obj@ gc-mark-obj
2080 macro-table obj@ gc-mark-obj
2081 console-i/o-port obj@ gc-mark-obj
2082 global-env obj@ gc-mark-obj
2084 depth object-stack-base @ do
2093 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
2094 ; is collect-garbage
2098 \ ---- Loading files ---- {{{
2100 : load ( addr n -- finalResult )
2105 ok-symbol ( port res )
2109 \ bold fg blue ." READ from " 2over drop . ." ==> " reset-term
2111 2over read-port ( port res obj )
2116 2dup EOF character-type objeq? if
2117 2drop 2swap close-port
2121 2swap 2drop ( port obj )
2123 global-env obj@ eval ( port res )
2129 \ ---- Standard Library ---- {{{
2131 include scheme-primitives.4th
2133 init-object-stack-base
2134 s" scheme-library.scm" load 2drop
2140 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2141 : repl-body ( -- bool )
2142 cr bold fg green ." > " reset-term
2146 2dup EOF character-type objeq? if
2148 bold fg blue ." Moriturus te saluto." reset-term cr
2152 global-env obj@ eval
2154 fg cyan ." ; " print reset-term
2162 init-object-stack-base
2164 \ Display welcome message
2165 welcome-symbol nil cons global-env obj@ eval 2drop
2170 recoverable-exception of false endof
2171 unrecoverable-exception of true endof