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
85 create nextfrees scheme-memsize allot
96 nextfrees nextfree @ + @
99 nextfree @ scheme-memsize >= if
105 nextfree @ scheme-memsize >= if
106 except-message: ." Out of memory!" unrecoverable-exception throw
110 : cons ( car-obj cdr-obj -- pair-obj )
111 cdr-type-cells nextfree @ + !
112 cdr-cells nextfree @ + !
113 car-type-cells nextfree @ + !
114 car-cells nextfree @ + !
120 : car ( pair-obj -- car-obj )
122 dup car-cells + @ swap
126 : cdr ( pair-obj -- car-obj )
128 dup cdr-cells + @ swap
132 : set-car! ( obj pair-obj -- )
134 rot swap car-type-cells + !
138 : set-cdr! ( obj pair-obj -- )
140 rot swap cdr-type-cells + !
145 : nil? nil-type istype? ;
148 : none? none-type istype? ;
150 : objvar create nil swap , , ;
152 : value@ ( objvar -- val ) @ ;
153 : type@ ( objvar -- type ) 1+ @ ;
154 : value! ( newval objvar -- ) ! ;
155 : type! ( newtype objvar -- ) 1+ ! ;
156 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
157 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
159 : objeq? ( obj obj -- bool )
162 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
163 >R >R ( a1 a2 b1 b2 )
164 2swap ( b1 b2 a1 a2 )
165 R> R> ( b1 b2 a1 a2 c1 c2 )
169 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
170 2swap ( a1 a2 c1 c2 b1 b2 )
171 >R >R ( a1 a2 c1 c2 )
172 2swap ( c1 c2 a1 a2 )
176 : 2pick ( an bn an-1 bn-1 ... a0 b0 n -- an bn an-1 bn-1 ... a0 b0 an bn )
182 \ ---- Pre-defined symbols ---- {{{
186 : duplicate-charlist ( charlist -- copy )
188 2dup car 2swap cdr recurse cons
191 : charlist-equiv ( charlist charlist -- bool )
200 2drop 2drop true exit
202 2drop 2drop false exit
207 2drop 2drop false exit
214 car drop -rot car drop = if
215 cdr 2swap cdr recurse
221 : charlist>symbol ( charlist -- symbol-obj )
240 drop symbol-type 2dup
241 symbol-table obj@ cons
246 : cstr>charlist ( addr n -- charlist )
250 2dup drop @ character-type 2swap
258 : create-symbol ( -- )
266 does> dup @ swap 1+ @
269 create-symbol quote quote-symbol
270 create-symbol quasiquote quasiquote-symbol
271 create-symbol unquote unquote-symbol
272 create-symbol unquote-splicing unquote-splicing-symbol
273 create-symbol define define-symbol
274 create-symbol define-macro define-macro-symbol
275 create-symbol set! set!-symbol
276 create-symbol ok ok-symbol
277 create-symbol if if-symbol
278 create-symbol lambda lambda-symbol
279 create-symbol λ λ-symbol
280 create-symbol eof eof-symbol
281 create-symbol no-match no-match-symbol
283 \ Symbol to be bound to welcome message procedure by library
284 create-symbol welcome welcome-symbol
288 \ ---- Port I/O ---- {{{
290 ( Ports are pairs with the fid in the car and the peek buffer in the cdr. )
292 : fileport>fid ( fileport -- fid )
293 drop pair-type car drop ;
295 : get-last-peek ( fileport -- char/nil )
298 : set-last-peek ( char/nil fileport -- )
299 drop pair-type set-cdr!
302 : fid>fileport ( fid -- fileport )
303 fixnum-type nil cons drop port-type ;
305 : open-input-file ( addr n -- fileport )
306 r/o open-file drop fid>fileport
309 : close-port ( fileport -- )
310 fileport>fid close-file drop
313 objvar console-i/o-port
314 0 fixnum-type nil cons drop port-type console-i/o-port obj!
316 objvar current-input-port
317 console-i/o-port obj@ current-input-port obj!
319 : read-char ( port -- char )
320 2dup get-last-peek nil? if
322 2dup console-i/o-port obj@ objeq? if
326 fileport>fid pad 1 rot read-file 0= if
337 : peek-char ( port -- char )
338 2dup get-last-peek nil? if
340 2dup 2rot set-last-peek
346 variable read-line-buffer-span
347 variable read-line-buffer-offset
349 ( Hack to save original read-line while we transition to new one. )
350 : orig-read-line immediate
353 : read-line ( port -- string )
358 0 read-line-buffer-offset !
360 2over nil 2swap set-last-peek
362 2drop nil nil cons exit
365 1 read-line-buffer-offset !
369 2dup console-i/o-port obj@ objeq? if
371 pad read-line-buffer-offset @ + 200 expect cr
372 span @ read-line-buffer-offset @ + read-line-buffer-span !
374 pad read-line-buffer-offset @ + 200 2over fileport>fid orig-read-line
375 drop swap read-line-buffer-offset @ + read-line-buffer-span !
381 read-line-buffer-span @ 0>
383 pad read-line-buffer-span @ 1- + @ character-type 2swap cons
384 -1 read-line-buffer-span +!
388 nil cons drop string-type
394 : read-port ( fileport -- obj )
395 current-input-port obj!
398 : read-console ( -- obj )
399 console-i/o-port obj@ read-port ;
403 \ ---- Environments ---- {{{
405 : enclosing-env ( env -- env )
408 : first-frame ( env -- frame )
411 : make-frame ( vars vals -- frame )
414 : frame-vars ( frame -- vars )
417 : frame-vals ( frame -- vals )
420 : add-binding ( var val frame -- )
421 2swap 2over frame-vals cons
423 2swap 2over frame-vars cons
427 : extend-env ( vars vals env -- env )
437 : get-vars-vals-frame ( var frame -- bool )
438 2dup frame-vars vars obj!
442 vars obj@ nil objeq? false =
444 2dup vars obj@ car objeq? if
449 vars obj@ cdr vars obj!
450 vals obj@ cdr vals obj!
456 : get-vars-vals ( var env -- vars? vals? bool )
461 2over 2over first-frame
462 get-vars-vals-frame if
464 vars obj@ vals obj@ true
480 : lookup-var ( var env -- val )
485 except-message: ." tried to read unbound variable '" var obj@ print ." '." recoverable-exception throw
489 : set-var ( var val env -- )
490 >R >R 2swap R> R> ( val var env )
493 2swap 2drop ( val vals )
496 except-message: ." tried to set unbound variable '" var obj@ print ." '." recoverable-exception throw
504 : define-var ( var val env -- )
507 2over env obj@ ( var val var env )
509 2swap 2drop ( var val vals )
514 first-frame ( var val frame )
521 : make-procedure ( params body env -- proc )
524 drop compound-proc-type
528 nil nil nil extend-env
533 \ ---- Primitives ---- {{{
535 : make-primitive ( cfa -- )
542 rot primitive-proc-type ( var prim )
543 global-env obj@ define-var
546 : ensure-arg-count ( 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
560 : ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
562 drop nil objeq? false = if
563 except-message: ." Too many arguments for primitive procedure." recoverable-exception throw
567 except-message: ." Too few arguments for primitive procedure." recoverable-exception throw
570 2dup cdr 2swap car ( ... t1 n args' arg1 )
571 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
573 except-message: ." Incorrect type for primitive procedure." recoverable-exception throw
581 : push-args-to-stack ( args -- arg1 arg2 ... argn )
591 : add-fa-checks ( cfa n -- cfa' )
592 here current @ 1+ dup @ , !
596 ['] 2dup , ['] lit , , ['] ensure-arg-count ,
597 ['] push-args-to-stack ,
598 ['] lit , , ['] execute ,
602 : add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
603 here current @ 1+ dup @ , !
610 dup ( cfa t1 t2 ... tn n m )
615 rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
621 ['] lit , , ['] ensure-arg-type-and-count ,
623 ['] push-args-to-stack ,
624 ['] lit , , ['] execute ,
630 : make-fa-primitive ( cfa n -- )
631 add-fa-checks make-primitive ;
633 : make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
634 add-fa-type-checks make-primitive ;
637 bold fg red ." Incorrect argument type." reset-term cr
641 : ensure-arg-type ( arg type -- arg )
643 except-message: ." Incorrect argument type for primitive procedure." recoverable-exception throw
650 \ ---- Macros ---- {{{
654 ( Look up macro in macro table. Returns nil if
656 : lookup-macro ( name_symbol -- proc )
658 symbol-type istype? invert if
659 \ Early exit if argument is not a symbol
681 : make-macro ( name_symbol params body env -- )
684 2swap ( proc name_symbol )
691 2over 2over ( proc name table name table )
693 2swap 2drop ( proc table )
705 macro-table obj@ cons
714 variable stored-parse-idx
715 create parse-str 161 allot
716 variable parse-str-span
718 create parse-idx-stack 10 allot
719 variable parse-idx-sp
720 parse-idx-stack parse-idx-sp !
723 parse-idx @ parse-idx-sp @ !
728 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
732 parse-idx-sp @ @ parse-idx ! ;
736 '\n' parse-str parse-str-span @ + !
737 1 parse-str-span +! ;
740 4 parse-str parse-str-span @ + !
741 1 parse-str-span +! ;
748 current-input-port obj@ console-i/o-port obj@ objeq? if
749 parse-str 160 expect cr
750 span @ parse-str-span !
752 parse-str 160 current-input-port obj@ fileport>fid orig-read-line
753 drop swap parse-str-span !
755 parse-str-span @ 0= and if append-eof then
766 : charavailable? ( -- bool )
767 parse-str-span @ parse-idx @ > ;
769 : nextchar ( -- char )
770 charavailable? false = if getline then
771 parse-str parse-idx @ + @ ;
774 : whitespace? ( -- bool )
786 nextchar [char] ( = or
787 nextchar [char] ) = or
790 : commentstart? ( -- bool )
791 nextchar [char] ; = ;
795 false \ Indicates whether or not we're eating a comment
798 dup whitespace? or commentstart? or
800 dup nextchar '\n' = and if
801 invert \ Stop eating comment
803 dup false = commentstart? and if
804 invert \ Begin eating comment
819 nextchar [char] - = ;
822 nextchar [char] + = ;
824 : fixnum? ( -- bool )
850 : flonum? ( -- bool )
857 \ Record starting parse idx:
858 \ Want to detect whether any characters (following +/-) were eaten.
865 [char] . nextchar = if
872 [char] e nextchar = [char] E nextchar = or if
880 drop pop-parse-idx false exit
888 \ This is a real number if characters were
889 \ eaten and the next characer is a delimiter.
890 parse-idx @ < delim? and
895 : ratnum? ( -- bool )
903 pop-parse-idx false exit
912 [char] / nextchar <> if
913 pop-parse-idx false exit
919 pop-parse-idx false exit
931 : boolean? ( -- bool )
932 nextchar [char] # <> if false exit then
939 and if pop-parse-idx false exit then
951 : str-equiv? ( str -- bool )
968 delim? false = if drop false then
973 : character? ( -- bool )
974 nextchar [char] # <> if false exit then
979 nextchar [char] \ <> if pop-parse-idx false exit then
983 S" newline" str-equiv? if pop-parse-idx true exit then
984 S" space" str-equiv? if pop-parse-idx true exit then
985 S" tab" str-equiv? if pop-parse-idx true exit then
987 charavailable? false = if pop-parse-idx false exit then
993 nextchar [char] ( = ;
995 : string? ( -- bool )
996 nextchar [char] " = ;
998 : readfixnum ( -- fixnum )
1009 10 * nextchar [char] 0 - +
1018 : readflonum ( -- flonum )
1020 dup 0< swap abs i->f
1022 [char] . nextchar = if
1028 nextchar [char] 0 - i->f ( f exp d )
1029 over f/ rot f+ ( exp f' )
1030 swap 10.0 f* ( f' exp' )
1037 [char] e nextchar = [char] E nextchar = or if
1040 readfixnum drop i->f
1051 : make-rational ( fixnum fixnum -- ratnum|fixnum )
1058 fixnum-type swap fixnum-type
1059 cons drop ratnum-type
1063 : readratnum ( -- ratnum )
1064 readfixnum inc-parse-idx readfixnum
1068 : readbool ( -- bool-obj )
1071 nextchar [char] f = if
1082 : readchar ( -- char-obj )
1086 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
1087 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
1088 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
1090 nextchar character-type
1095 : readstring ( -- charlist )
1100 nextchar [char] " <>
1102 nextchar [char] \ = if
1105 [char] n of '\n' endof
1106 [char] " of [char] " endof
1112 inc-parse-idx character-type
1115 ( firstchar prevchar thischar )
1118 2drop 2swap 2drop 2dup ( thischar thischar )
1120 ( firstchar thischar prevchar )
1121 2over 2swap set-cdr! ( firstchar thischar )
1125 \ Discard previous character
1131 ." No delimiter following right double quote. Aborting." cr
1143 : readsymbol ( -- charlist )
1144 delim? if nil exit then
1146 nextchar inc-parse-idx character-type
1153 : readpair ( -- pairobj )
1157 nextchar [char] ) = if
1162 ." No delimiter following right paren. Aborting." cr
1171 \ Read first pair element
1176 nextchar [char] . = if
1181 ." No delimiter following '.'. Aborting." cr
1195 \ Parse a scheme expression
1230 nextchar [char] " <> if
1231 bold red ." Missing closing double-quote." reset-term cr
1249 nextchar [char] ) <> if
1250 bold red ." Missing closing paren." reset-term cr
1259 nextchar [char] ' = if
1261 quote-symbol recurse nil cons cons exit
1264 nextchar [char] ` = if
1266 quasiquote-symbol recurse nil cons cons exit
1269 nextchar [char] , = if
1271 nextchar [char] @ = if
1273 unquote-splicing-symbol recurse nil cons cons exit
1275 unquote-symbol recurse nil cons cons exit
1285 nextchar [char] ) = if
1287 except-message: ." unmatched closing parenthesis." recoverable-exception throw
1290 \ Anything else is parsed as a symbol
1291 readsymbol charlist>symbol
1293 \ Replace λ with lambda
1294 2dup λ-symbol objeq? if
1303 \ ---- Syntax ---- {{{
1305 : self-evaluating? ( obj -- obj bool )
1306 boolean-type istype? if true exit then
1307 fixnum-type istype? if true exit then
1308 flonum-type istype? if true exit then
1309 ratnum-type istype? if true exit then
1310 character-type istype? if true exit then
1311 string-type istype? if true exit then
1312 nil-type istype? if true exit then
1313 none-type istype? if true exit then
1318 : tagged-list? ( obj tag-obj -- obj bool )
1320 pair-type istype? false = if
1326 : quote? ( obj -- obj bool )
1327 quote-symbol tagged-list? ;
1329 : quote-body ( quote-obj -- quote-body-obj )
1332 : variable? ( obj -- obj bool )
1333 symbol-type istype? ;
1335 : definition? ( obj -- obj bool )
1336 define-symbol tagged-list? ;
1338 : definition-var ( obj -- var )
1341 : definition-val ( obj -- val )
1344 : assignment? ( obj -- obj bool )
1345 set!-symbol tagged-list? ;
1347 : assignment-var ( obj -- var )
1350 : assignment-val ( obj -- val )
1353 : macro-definition? ( obj -- obj bool )
1354 define-macro-symbol tagged-list? ;
1356 : macro-definition-name ( exp -- mname )
1359 : macro-definition-params ( exp -- params )
1362 : macro-definition-body ( exp -- body )
1365 : if? ( obj -- obj bool )
1366 if-symbol tagged-list? ;
1368 : if-predicate ( ifobj -- pred )
1371 : if-consequent ( ifobj -- conseq )
1374 : if-alternative ( ifobj -- alt|none )
1382 : false? ( boolobj -- boolean )
1383 boolean-type istype? if
1384 false boolean-type objeq?
1390 : true? ( boolobj -- bool )
1393 : lambda? ( obj -- obj bool )
1394 lambda-symbol tagged-list? ;
1396 : lambda-parameters ( obj -- params )
1399 : lambda-body ( obj -- body )
1402 : application? ( obj -- obj bool )
1405 : operator ( obj -- operator )
1408 : operands ( obj -- operands )
1411 : nooperands? ( operands -- bool )
1414 : first-operand ( operands -- operand )
1417 : rest-operands ( operands -- other-operands )
1420 : procedure-params ( proc -- params )
1421 drop pair-type car ;
1423 : procedure-body ( proc -- body )
1424 drop pair-type cdr car ;
1426 : procedure-env ( proc -- body )
1427 drop pair-type cdr cdr car ;
1429 ( Ensure terminating symbol arg name is handled
1430 specially to allow for variadic procedures. )
1431 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1433 2over nil? false = if
1434 except-message: ." Too many arguments for compound procedure." recoverable-exception throw
1441 symbol-type istype? if
1451 except-message: ." Too few arguments for compound procedure." recoverable-exception throw
1458 recurse ( argvals argnames argvals'' argnames'' )
1459 2rot car 2swap cons ( argvals argvals'' argnames' )
1460 2rot car 2rot cons ( argnames' argvals' )
1466 \ ---- Analyze ---- {{{
1468 : evaluate-eproc ( eproc env --- res )
1479 2drop \ get rid of null
1483 \ Final element of eproc list is primitive procedure
1484 drop \ dump type signifier
1486 goto \ jump straight to primitive procedure (executor)
1489 : self-evaluating-executor ( exp env -- exp )
1492 : analyze-self-evaluating ( exp --- eproc )
1493 ['] self-evaluating-executor primitive-proc-type
1497 : quote-executor ( exp env -- exp )
1500 : analyze-quoted ( exp -- eproc )
1503 ['] quote-executor primitive-proc-type
1507 : variable-executor ( var env -- val )
1510 : analyze-variable ( exp -- eproc )
1511 ['] variable-executor primitive-proc-type
1515 : definition-executor ( var val-eproc env -- ok )
1516 2swap 2over ( var env val-eproc env )
1517 evaluate-eproc 2swap ( var val env )
1522 : analyze-definition ( exp -- eproc )
1524 2swap definition-val analyze
1526 ['] definition-executor primitive-proc-type
1530 : assignment-executor ( var val-eproc env -- ok )
1531 2swap 2over ( var env val-eproc env )
1532 evaluate-eproc 2swap ( var val env )
1537 : analyze-assignment ( exp -- eproc )
1539 2swap assignment-val analyze ( var val-eproc )
1541 ['] assignment-executor primitive-proc-type
1545 : sequence-executor ( eproc-list env -- res )
1549 2dup cdr ( env elist elist-rest)
1552 -2rot car 2over ( elist-rest env elist-head env )
1553 evaluate-eproc ( elist-rest env head-res )
1554 2drop 2swap ( env elist-rest )
1558 ['] evaluate-eproc goto
1562 : (analyze-sequence) ( explist -- eproc-list )
1571 : analyze-sequence ( explist -- eproc )
1573 ['] sequence-executor primitive-proc-type
1578 : macro-definition-executor ( name params bproc env -- ok )
1579 make-macro ok-symbol
1582 : analyze-macro-definition ( exp -- eproc )
1583 2dup macro-definition-name
1584 2swap 2dup macro-definition-params
1585 2swap macro-definition-body analyze-sequence
1587 ['] macro-definition-executor primitive-proc-type
1588 nil cons cons cons cons
1591 : if-executor ( cproc aproc pproc env -- res )
1592 2swap 2over ( cproc aproc env pproc env -- res )
1601 ['] evaluate-eproc goto
1604 : analyze-if ( exp -- eproc )
1605 2dup if-consequent analyze
1606 2swap 2dup if-alternative analyze
1607 2swap if-predicate analyze
1609 ['] if-executor primitive-proc-type
1610 nil cons cons cons cons
1613 : lambda-executor ( params bproc env -- res )
1615 ( Although this is packaged up as a regular compound procedure,
1616 the "body" element contains an _eproc_ to be evaluated in an
1617 environment resulting from extending env with the parameter
1621 : analyze-lambda ( exp -- eproc )
1622 2dup lambda-parameters
1626 except-message: ." encountered lambda with an empty body." recoverable-exception throw
1631 ['] lambda-executor primitive-proc-type
1635 : operand-eproc-list ( operands -- eprocs )
1643 : evaluate-operand-eprocs ( env aprocs -- vals )
1647 2over 2over car 2swap evaluate-eproc ( env aprocs thisval )
1648 -2rot cdr recurse ( thisval restvals )
1653 : apply ( vals proc )
1655 primitive-proc-type of
1659 compound-proc-type of
1660 2dup procedure-body ( argvals proc bproc )
1661 -2rot 2dup procedure-params ( bproc argvals proc argnames )
1662 -2rot procedure-env ( bproc argnames argvals procenv )
1668 extend-env ( bproc env )
1670 ['] evaluate-eproc goto
1673 continuation-type of
1674 \ TODO: Apply continuation
1677 except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
1681 : application-executor ( operator-proc arg-procs env -- res )
1682 2rot 2over ( aprocs env fproc env )
1683 evaluate-eproc ( aprocs env proc )
1685 -2rot 2swap ( proc env aprocs )
1686 evaluate-operand-eprocs ( proc vals )
1693 : analyze-application ( exp -- eproc )
1694 2dup operator analyze
1695 2swap operands operand-eproc-list
1697 ['] application-executor primitive-proc-type
1701 :noname ( exp --- eproc )
1703 self-evaluating? if analyze-self-evaluating exit then
1705 quote? if analyze-quoted exit then
1707 variable? if analyze-variable exit then
1709 definition? if analyze-definition exit then
1711 assignment? if analyze-assignment exit then
1713 macro-definition? if analyze-macro-definition exit then
1715 if? if analyze-if exit then
1717 lambda? if analyze-lambda exit then
1719 application? if analyze-application exit then
1721 except-message: ." tried to analyze unknown expression type." recoverable-exception throw
1727 \ ---- Macro Expansion ---- {{{
1729 ( Simply evaluates the given procedure with expbody as its argument. )
1730 : macro-eval ( proc expbody -- result )
1732 2dup procedure-body ( expbody proc bproc )
1733 -2rot 2dup procedure-params ( bproc expbody proc argnames )
1734 -2rot procedure-env ( bproc argnames expbody procenv )
1740 extend-env ( bproc env )
1742 ['] evaluate-eproc goto
1745 : expand-macro ( exp -- result )
1746 pair-type istype? invert if exit then
1748 2dup car symbol-type istype? invert if 2drop exit then
1750 lookup-macro nil? if 2drop exit then
1752 2over cdr macro-eval
1754 2dup no-match-symbol objeq? if
1760 R> drop ['] expand goto-deferred
1763 : expand-definition ( exp -- result )
1767 2swap definition-val expand
1768 nil ( define var val' nil )
1772 : expand-assignment ( exp -- result )
1776 2swap assignment-val expand
1777 nil ( define var val' nil )
1781 : expand-list ( exp -- res )
1789 : macro-definition-nameparams
1792 : expand-define-macro ( exp -- res )
1793 define-macro-symbol 2swap
1794 2dup macro-definition-nameparams
1795 2swap macro-definition-body expand-list
1799 : expand-lambda ( exp -- res )
1801 2dup lambda-parameters
1802 2swap lambda-body expand-list
1806 : expand-if ( exp -- res )
1809 2dup if-predicate expand
1810 2swap 2dup if-consequent expand
1811 2swap if-alternative none? if
1819 : expand-application ( exp -- res )
1820 2dup operator expand
1821 2swap operands expand-list
1825 :noname ( exp -- result )
1828 self-evaluating? if exit then
1832 definition? if expand-definition exit then
1834 assignment? if expand-assignment exit then
1836 macro-definition? if expand-define-macro exit then
1838 lambda? if expand-lambda exit then
1840 if? if expand-if exit then
1842 application? if expand-application exit then
1848 :noname ( exp env -- res )
1849 2swap expand analyze 2swap evaluate-eproc
1852 \ ---- Print ---- {{{
1854 : printfixnum ( fixnum -- ) drop 0 .R ;
1856 : printflonum ( flonum -- ) drop f. ;
1858 : printratnum ( ratnum -- )
1860 car print ." /" cdr print
1863 : printbool ( bool -- )
1871 : printchar ( charobj -- )
1874 9 of ." #\tab" endof
1875 bl of ." #\space" endof
1876 '\n' of ." #\newline" endof
1882 : (printstring) ( stringobj -- )
1883 nil? if 2drop exit then
1887 '\n' of ." \n" drop endof
1888 [char] \ of ." \\" drop endof
1889 [char] " of [char] \ emit [char] " emit drop endof
1895 : printstring ( stringobj -- )
1900 : printsymbol ( symbolobj -- )
1901 nil-type istype? if 2drop exit then
1907 : printnil ( nilobj -- )
1910 : printpair ( pairobj -- )
1914 nil-type istype? if 2drop exit then
1915 pair-type istype? if space recurse exit then
1919 : printprim ( primobj -- )
1920 2drop ." <primitive procedure>" ;
1922 : printcomp ( primobj -- )
1923 2drop ." <compound procedure>" ;
1925 : printcont ( primobj --)
1926 2drop ." <continuation>" ;
1928 : printnone ( noneobj -- )
1929 2drop ." Unspecified return value" ;
1931 : printport ( port -- )
1935 fixnum-type istype? if printfixnum exit then
1936 flonum-type istype? if printflonum exit then
1937 ratnum-type istype? if printratnum exit then
1938 boolean-type istype? if printbool exit then
1939 character-type istype? if printchar exit then
1940 string-type istype? if printstring exit then
1941 symbol-type istype? if printsymbol exit then
1942 nil-type istype? if printnil exit then
1943 pair-type istype? if ." (" printpair ." )" exit then
1944 primitive-proc-type istype? if printprim exit then
1945 compound-proc-type istype? if printcomp exit then
1946 continuation-type istype? if printcont exit then
1947 none-type istype? if printnone exit then
1948 port-type istype? if printport exit then
1950 except-message: ." tried to print object with unknown type." recoverable-exception throw
1955 \ ---- Garbage Collection ---- {{{
1957 variable gc-stack-depth
1960 depth gc-stack-depth !
1964 false gc-enabled ! ;
1966 : pairlike? ( obj -- obj bool )
1967 pair-type istype? if true exit then
1968 string-type istype? if true exit then
1969 symbol-type istype? if true exit then
1970 compound-proc-type istype? if true exit then
1971 port-type istype? if true exit then
1976 : pairlike-marked? ( obj -- obj bool )
1977 over nextfrees + @ 0=
1980 : mark-pairlike ( obj -- obj )
1981 over nextfrees + 0 swap !
1990 : gc-mark-obj ( obj -- )
1992 pairlike? invert if 2drop exit then
1993 pairlike-marked? if 2drop exit then
2004 scheme-memsize nextfree !
2005 0 scheme-memsize 1- do
2006 nextfrees i + @ 0<> if
2007 nextfree @ nextfrees i + !
2013 \ Following a GC, this gives the amount of free memory
2017 nextfrees i + @ 0= if 1+ then
2021 \ Debugging word - helps spot memory that is retained
2024 nextfrees i + @ 0<> if
2036 symbol-table obj@ gc-mark-obj
2037 macro-table obj@ gc-mark-obj
2038 console-i/o-port obj@ gc-mark-obj
2039 global-env obj@ gc-mark-obj
2041 depth gc-stack-depth @ do
2050 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
2051 ; is collect-garbage
2055 \ ---- Loading files ---- {{{
2057 : load ( addr n -- finalResult )
2062 ok-symbol ( port res )
2066 \ bold fg blue ." READ from " 2over drop . ." ==> " reset-term
2068 2over read-port ( port res obj )
2073 2dup EOF character-type objeq? if
2074 2drop 2swap close-port
2078 2swap 2drop ( port obj )
2080 global-env obj@ eval ( port res )
2086 \ ---- Standard Library ---- {{{
2088 include scheme-primitives.4th
2092 s" scheme-library.scm" load 2drop
2100 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
2101 : repl-body ( -- bool )
2102 cr bold fg green ." > " reset-term
2106 2dup EOF character-type objeq? if
2108 bold fg blue ." Moriturus te saluto." reset-term cr
2112 global-env obj@ eval
2114 fg cyan ." ; " print reset-term
2124 \ Display welcome message
2125 welcome-symbol nil cons global-env obj@ eval 2drop
2130 recoverable-exception of false endof
2131 unrecoverable-exception of true endof