4 include term-colours.4th
6 include catch-throw.4th
27 make-type realnum-type
28 make-type boolean-type
29 make-type character-type
35 make-type primitive-proc-type
36 make-type compound-proc-type
37 make-type fileport-type
38 : istype? ( obj type -- obj bool )
43 \ ---- Exceptions ---- {{{
45 variable nextexception
48 create nextexception @ ,
52 make-exception recoverable-exception
53 make-exception unrecoverable-exception
55 : display-exception-msg ( addr count -- )
68 ['] display-exception-msg ,
76 \ ---- List-structured memory ---- {{{
78 10000 constant scheme-memsize
80 create car-cells scheme-memsize allot
81 create car-type-cells scheme-memsize allot
82 create cdr-cells scheme-memsize allot
83 create cdr-type-cells scheme-memsize allot
85 create nextfrees scheme-memsize allot
96 nextfrees nextfree @ + @
99 nextfree @ scheme-memsize >= if
103 nextfree @ scheme-memsize >= if
104 unrecoverable-exception throw s" Out of memory!"
108 : cons ( car-obj cdr-obj -- pair-obj )
109 cdr-type-cells nextfree @ + !
110 cdr-cells nextfree @ + !
111 car-type-cells nextfree @ + !
112 car-cells nextfree @ + !
118 : car ( pair-obj -- car-obj )
120 dup car-cells + @ swap
124 : cdr ( pair-obj -- car-obj )
126 dup cdr-cells + @ swap
130 : set-car! ( obj pair-obj -- )
132 rot swap car-type-cells + !
136 : set-cdr! ( obj pair-obj -- )
138 rot swap cdr-type-cells + !
143 : nil? nil-type istype? ;
146 : none? none-type istype? ;
148 : objvar create nil swap , , ;
150 : value@ ( objvar -- val ) @ ;
151 : type@ ( objvar -- type ) 1+ @ ;
152 : value! ( newval objvar -- ) ! ;
153 : type! ( newtype objvar -- ) 1+ ! ;
154 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
155 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
157 : objeq? ( obj obj -- bool )
160 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
161 >R >R ( a1 a2 b1 b2 )
162 2swap ( b1 b2 a1 a2 )
163 R> R> ( b1 b2 a1 a2 c1 c2 )
167 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
168 2swap ( a1 a2 c1 c2 b1 b2 )
169 >R >R ( a1 a2 c1 c2 )
170 2swap ( c1 c2 a1 a2 )
176 \ ---- Port I/O ---- {{{
178 : fileport>fid ( fileport -- fid )
181 : fid>fileport ( fid -- fileport )
184 : open-input-file ( addr n -- fileport )
185 r/o open-file drop fid>fileport
188 : close-port ( fileport -- )
189 fileport>fid close-file drop
192 objvar console-i/o-port
193 0 fileport-type console-i/o-port obj!
195 objvar current-input-port
196 console-i/o-port obj@ current-input-port obj!
198 : read-port ( fileport -- obj )
199 current-input-port obj!
202 : read-console ( -- obj )
203 console-i/o-port obj@ read-port ;
207 \ ---- Pre-defined symbols ---- {{{
211 : duplicate-charlist ( charlist -- copy )
213 2dup car 2swap cdr recurse cons
216 : charlist-equiv ( charlist charlist -- bool )
225 2drop 2drop true exit
227 2drop 2drop false exit
232 2drop 2drop false exit
239 car drop -rot car drop = if
240 cdr 2swap cdr recurse
246 : charlist>symbol ( charlist -- symbol-obj )
265 drop symbol-type 2dup
266 symbol-table obj@ cons
271 : cstr>charlist ( addr n -- charlist )
275 2dup drop @ character-type 2swap
283 : create-symbol ( -- )
291 does> dup @ swap 1+ @
294 create-symbol quote quote-symbol
295 create-symbol quasiquote quasiquote-symbol
296 create-symbol unquote unquote-symbol
297 create-symbol unquote-splicing unquote-splicing-symbol
298 create-symbol define define-symbol
299 create-symbol define-macro define-macro-symbol
300 create-symbol set! set!-symbol
301 create-symbol ok ok-symbol
302 create-symbol if if-symbol
303 create-symbol lambda lambda-symbol
304 create-symbol λ λ-symbol
305 create-symbol begin begin-symbol
309 \ ---- Environments ---- {{{
311 : enclosing-env ( env -- env )
314 : first-frame ( env -- frame )
317 : make-frame ( vars vals -- frame )
320 : frame-vars ( frame -- vars )
323 : frame-vals ( frame -- vals )
326 : add-binding ( var val frame -- )
327 2swap 2over frame-vals cons
329 2swap 2over frame-vars cons
333 : extend-env ( vars vals env -- env )
343 : get-vars-vals-frame ( var frame -- bool )
344 2dup frame-vars vars obj!
348 vars obj@ nil objeq? false =
350 2dup vars obj@ car objeq? if
355 vars obj@ cdr vars obj!
356 vals obj@ cdr vals obj!
362 : get-vars-vals ( var env -- vars? vals? bool )
367 2over 2over first-frame
368 get-vars-vals-frame if
370 vars obj@ vals obj@ true
384 : lookup-var ( var env -- val )
388 recoverable-exception throw" Tried to read unbound variable."
392 : set-var ( var val env -- )
393 >R >R 2swap R> R> ( val var env )
395 2swap 2drop ( val vals )
398 recoverable-exception throw" Tried to set unbound variable."
404 : define-var ( var val env -- )
407 2over env obj@ ( var val var env )
409 2swap 2drop ( var val vals )
414 first-frame ( var val frame )
421 : make-procedure ( params body env -- proc )
424 drop compound-proc-type
428 nil nil nil extend-env
433 \ ---- Primitives ---- {{{
435 : make-primitive ( cfa -- )
439 \ 2dup ." Defining primitive " type ." ..." cr
444 rot primitive-proc-type ( var prim )
445 global-env obj@ define-var
448 : ensure-arg-count ( args n -- )
450 drop nil objeq? false = if
451 recoverable-exception throw" Too many arguments for primitive procedure."
455 recoverable-exception throw" Too few arguments for primitive procedure."
463 bold fg red ." Incorrect argument type." reset-term cr
467 : ensure-arg-type ( arg type -- arg )
469 recoverable-exception throw" Incorrect argument type for primitive procedure."
476 \ ---- Macros ---- {{{
480 ( Look up macro in macro table. Returns nil if
482 : lookup-macro ( name_symbol -- proc )
501 : make-macro ( name_symbol params body env -- )
504 2swap ( proc name_symbol )
511 2over 2over ( proc name table name table )
513 2swap 2drop ( proc table )
525 macro-table obj@ cons
534 variable stored-parse-idx
535 create parse-str 161 allot
536 variable parse-str-span
538 create parse-idx-stack 10 allot
539 variable parse-idx-sp
540 parse-idx-stack parse-idx-sp !
543 parse-idx @ parse-idx-sp @ !
548 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
552 parse-idx-sp @ @ parse-idx ! ;
556 '\n' parse-str parse-str-span @ + !
557 1 parse-str-span +! ;
560 4 parse-str parse-str-span @ + !
561 1 parse-str-span +! ;
568 current-input-port obj@ console-i/o-port obj@ objeq? if
569 parse-str 160 expect cr
570 span @ parse-str-span !
572 parse-str 160 current-input-port obj@ fileport>fid read-line
573 drop swap parse-str-span !
575 parse-str-span @ 0= and if append-eof then
586 : charavailable? ( -- bool )
587 parse-str-span @ parse-idx @ > ;
589 : nextchar ( -- char )
590 charavailable? false = if getline then
591 parse-str parse-idx @ + @ ;
594 : whitespace? ( -- bool )
606 nextchar [char] ( = or
607 nextchar [char] ) = or
610 : commentstart? ( -- bool )
611 nextchar [char] ; = ;
615 false \ Indicates whether or not we're eating a comment
618 dup whitespace? or commentstart? or
620 dup nextchar '\n' = and if
621 invert \ Stop eating comment
623 dup false = commentstart? and if
624 invert \ Begin eating comment
639 nextchar [char] - = ;
642 nextchar [char] + = ;
644 : fixnum? ( -- bool )
670 : realnum? ( -- bool )
677 \ Record starting parse idx:
678 \ Want to detect whether any characters (following +/-) were eaten.
685 [char] . nextchar = if
692 [char] e nextchar = [char] E nextchar = or if
700 drop pop-parse-idx false exit
708 \ This is a real number if characters were
709 \ eaten and the next characer is a delimiter.
710 parse-idx @ < delim? and
715 : boolean? ( -- bool )
716 nextchar [char] # <> if false exit then
723 and if pop-parse-idx false exit then
735 : str-equiv? ( str -- bool )
752 delim? false = if drop false then
757 : character? ( -- bool )
758 nextchar [char] # <> if false exit then
763 nextchar [char] \ <> if pop-parse-idx false exit then
767 S" newline" str-equiv? if pop-parse-idx true exit then
768 S" space" str-equiv? if pop-parse-idx true exit then
769 S" tab" str-equiv? if pop-parse-idx true exit then
771 charavailable? false = if pop-parse-idx false exit then
777 nextchar [char] ( = ;
779 : string? ( -- bool )
780 nextchar [char] " = ;
782 : readfixnum ( -- num-atom )
793 10 * nextchar [char] 0 - +
802 : readrealnum ( -- realnum )
804 \ Remember that at this point we're guaranteed to
805 \ have a parsable real on this line.
807 parse-str parse-idx @ +
809 begin delim? false = while
813 parse-str parse-idx @ + over -
820 : readbool ( -- bool-obj )
823 nextchar [char] f = if
834 : readchar ( -- char-obj )
838 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
839 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
840 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
842 nextchar character-type
847 : readstring ( -- charlist )
848 nextchar [char] " = if
853 ." No delimiter following right double quote. Aborting." cr
862 nextchar [char] \ = if
865 [char] n of '\n' endof
866 [char] " of [char] " endof
872 inc-parse-idx character-type
879 : readsymbol ( -- charlist )
880 delim? if nil exit then
882 nextchar inc-parse-idx character-type
889 : readpair ( -- pairobj )
893 nextchar [char] ) = if
898 ." No delimiter following right paren. Aborting." cr
907 \ Read first pair element
912 nextchar [char] . = if
917 ." No delimiter following '.'. Aborting." cr
931 \ Parse a scheme expression
962 nextchar [char] " <> if
963 bold red ." Missing closing double-quote." reset-term cr
981 nextchar [char] ) <> if
982 bold red ." Missing closing paren." reset-term cr
991 nextchar [char] ' = if
993 quote-symbol recurse nil cons cons exit
996 nextchar [char] ` = if
998 quasiquote-symbol recurse nil cons cons exit
1001 nextchar [char] , = if
1003 nextchar [char] @ = if
1005 unquote-splicing-symbol recurse nil cons cons exit
1007 unquote-symbol recurse nil cons cons exit
1017 \ Anything else is parsed as a symbol
1018 readsymbol charlist>symbol
1020 \ Replace λ with lambda
1021 2dup λ-symbol objeq? if
1030 \ ---- Eval ---- {{{
1032 : self-evaluating? ( obj -- obj bool )
1033 boolean-type istype? if true exit then
1034 fixnum-type istype? if true exit then
1035 realnum-type istype? if true exit then
1036 character-type istype? if true exit then
1037 string-type istype? if true exit then
1038 nil-type istype? if true exit then
1039 none-type istype? if true exit then
1044 : tagged-list? ( obj tag-obj -- obj bool )
1046 pair-type istype? false = if
1052 : quote? ( obj -- obj bool )
1053 quote-symbol tagged-list? ;
1055 : quote-body ( quote-obj -- quote-body-obj )
1058 : quasiquote? ( obj -- obj bool )
1059 quasiquote-symbol tagged-list? ;
1061 : unquote? ( obj -- obj bool )
1062 unquote-symbol tagged-list? ;
1064 : unquote-splicing? ( obj -- obj bool )
1065 unquote-splicing-symbol tagged-list? ;
1067 : eval-unquote ( env obj -- res )
1071 recoverable-exception throw" no arguments to unquote."
1076 recoverable-exception throw" too many arguments to unquote."
1079 2drop car 2swap eval
1082 ( Create a new list from elements of l1 consed on to l2 )
1083 : join-lists ( l2 l1 -- l3 )
1084 nil? if 2drop exit then
1091 defer eval-quasiquote-item
1092 : eval-quasiquote-pair ( env obj -- res )
1093 2over 2over ( env obj env obj )
1095 cdr eval-quasiquote-item
1097 -2rot car ( cdritem env objcar )
1099 unquote-splicing? if
1100 eval-unquote ( cdritems caritem )
1108 eval-quasiquote-item ( cdritems caritem )
1123 pair-type istype? if
1124 eval-quasiquote-pair exit
1128 ; is eval-quasiquote-item
1130 : eval-quasiquote ( obj env -- res )
1131 2swap cdr ( env args )
1134 recoverable-exception throw" no arguments to quasiquote."
1137 2dup cdr ( env args args-cdr )
1139 recoverable-exception throw" too many arguments to quasiquote."
1142 2drop car ( env arg )
1144 eval-quasiquote-item
1147 : variable? ( obj -- obj bool )
1148 symbol-type istype? ;
1150 : definition? ( obj -- obj bool )
1151 define-symbol tagged-list? ;
1153 : make-lambda ( params body -- lambda-exp )
1154 lambda-symbol -2rot cons cons ;
1156 ( Handles iterative expansion of defines in
1157 terms of nested lambdas. Most Schemes only
1158 handle one iteration of expansion! )
1159 : definition-var-val ( obj -- var val )
1161 cdr 2dup cdr 2swap car ( val var )
1164 symbol-type istype? false =
1166 2dup cdr 2swap car ( val formals var' )
1167 -2rot 2swap ( var' formals val )
1168 make-lambda nil cons ( var' val' )
1175 : eval-definition ( obj env -- res )
1176 2dup 2rot ( env env obj )
1177 definition-var-val ( env env var val )
1178 2rot eval ( env var val )
1180 2rot ( var val env )
1186 : assignment? ( obj -- obj bool )
1187 set!-symbol tagged-list? ;
1189 : assignment-var ( obj -- var )
1192 : assignment-val ( obj -- val )
1195 : eval-assignment ( obj env -- res )
1197 2over 2over ( env obj env obj )
1198 assignment-val 2swap ( env obj valexp env )
1199 eval ( env obj val )
1201 2swap assignment-var 2swap ( env var val )
1203 2rot ( var val env )
1209 : macro-definition? ( obj -- obj bool )
1210 define-macro-symbol tagged-list? ;
1212 : macro-definition-name ( exp -- mname )
1215 : macro-definition-params ( exp -- params )
1218 : macro-definition-body ( exp -- body )
1222 : eval-define-macro ( obj env -- res )
1225 2dup macro-definition-name 2swap ( name obj )
1226 2dup macro-definition-params 2swap ( name params obj )
1227 macro-definition-body ( name params body )
1229 env obj@ ( name params body env )
1237 : if? ( obj -- obj bool )
1238 if-symbol tagged-list? ;
1240 : if-predicate ( ifobj -- pred )
1243 : if-consequent ( ifobj -- conseq )
1246 : if-alternative ( ifobj -- alt|none )
1254 : false? ( boolobj -- boolean )
1255 boolean-type istype? if
1256 false boolean-type objeq?
1262 : true? ( boolobj -- bool )
1265 : lambda? ( obj -- obj bool )
1266 lambda-symbol tagged-list? ;
1268 : lambda-parameters ( obj -- params )
1271 : lambda-body ( obj -- body )
1274 : begin? ( obj -- obj bool )
1275 begin-symbol tagged-list? ;
1277 : begin-actions ( obj -- actions )
1280 : eval-sequence ( explist env -- finalexp env )
1281 ( Evaluates all bar the final expressions in
1282 an an expression list. The final expression
1283 is returned to allow for tail optimization. )
1285 2swap ( env explist )
1287 \ Abort on empty list
1294 2dup cdr ( env explist nextexplist )
1297 -2rot car 2over ( nextexplist env exp env )
1299 2drop \ discard result
1300 2swap ( env nextexplist )
1303 2drop car 2swap ( finalexp env )
1306 : application? ( obj -- obj bool )
1309 : operator ( obj -- operator )
1312 : operands ( obj -- operands )
1315 : nooperands? ( operands -- bool )
1318 : first-operand ( operands -- operand )
1321 : rest-operands ( operands -- other-operands )
1324 : list-of-vals ( args env -- vals )
1330 2over 2over first-operand 2swap eval
1331 -2rot rest-operands 2swap recurse
1336 : procedure-params ( proc -- params )
1337 drop pair-type car ;
1339 : procedure-body ( proc -- body )
1340 drop pair-type cdr car ;
1342 : procedure-env ( proc -- body )
1343 drop pair-type cdr cdr car ;
1345 ( Ensure terminating symbol arg name is handled
1346 specially to allow for variadic procedures. )
1347 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1349 2over nil? false = if
1350 recoverable-exception throw" Too many arguments for compound procedure."
1357 symbol-type istype? if
1367 recoverable-exception throw" Too few arguments for compound procedure."
1374 recurse ( argvals argnames argvals'' argnames'' )
1375 2rot car 2swap cons ( argvals argvals'' argnames' )
1376 2rot car 2rot cons ( argnames' argvals' )
1380 : apply ( proc argvals -- result )
1382 primitive-proc-type of
1386 compound-proc-type of
1387 2dup procedure-body ( argvals proc body )
1388 -2rot 2dup procedure-params ( body argvals proc argnames )
1389 -2rot procedure-env ( body argnames argvals procenv )
1395 extend-env ( body env )
1399 R> drop ['] eval goto-deferred \ Tail call optimization
1402 recoverable-exception throw" Object not applicable."
1406 ( Simply evaluates the given procedure with expbody as its argument. )
1407 : macro-expand ( proc expbody -- result )
1409 2dup procedure-body ( expbody proc procbody )
1410 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1411 -2rot procedure-env ( procbody argnames expbody procenv )
1417 extend-env eval-sequence eval
1420 :noname ( obj env -- result )
1435 2swap eval-quasiquote
1445 2swap eval-definition
1450 2swap eval-assignment
1454 macro-definition? if
1455 2swap eval-define-macro
1471 ['] eval goto-deferred
1475 2dup lambda-parameters
1484 ['] eval goto-deferred
1489 2over 2over ( env exp env exp )
1490 operator ( env exp env opname )
1492 2dup lookup-macro nil? false = if
1493 \ Macro function evaluation
1495 ( env exp env opname mproc )
1496 2swap 2drop -2rot 2drop cdr ( env mproc body )
1501 ['] eval goto-deferred
1503 \ Regular function application
1505 2drop ( env exp env opname )
1507 2swap eval ( env exp proc )
1509 -2rot ( proc env exp )
1510 operands 2swap ( proc operands env )
1511 list-of-vals ( proc argvals )
1518 recoverable-exception throw" Tried to evaluate object with unknown type."
1523 \ ---- Print ---- {{{
1525 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1527 : printrealnum ( realnumobj -- ) drop float-print ;
1529 : printbool ( numobj -- )
1537 : printchar ( charobj -- )
1540 9 of ." #\tab" endof
1541 bl of ." #\space" endof
1542 '\n' of ." #\newline" endof
1548 : (printstring) ( stringobj -- )
1549 nil? if 2drop exit then
1553 '\n' of ." \n" drop endof
1554 [char] \ of ." \\" drop endof
1555 [char] " of [char] \ emit [char] " emit drop endof
1561 : printstring ( stringobj -- )
1566 : printsymbol ( symbolobj -- )
1567 nil-type istype? if 2drop exit then
1573 : printnil ( nilobj -- )
1576 : printpair ( pairobj -- )
1580 nil-type istype? if 2drop exit then
1581 pair-type istype? if space recurse exit then
1585 : printprim ( primobj -- )
1586 2drop ." <primitive procedure>" ;
1588 : printcomp ( primobj -- )
1589 2drop ." <compound procedure>" ;
1591 : printnone ( noneobj -- )
1592 2drop ." Unspecified return value" ;
1594 : printport ( port -- )
1598 fixnum-type istype? if printfixnum exit then
1599 realnum-type istype? if printrealnum exit then
1600 boolean-type istype? if printbool exit then
1601 character-type istype? if printchar exit then
1602 string-type istype? if printstring exit then
1603 symbol-type istype? if printsymbol exit then
1604 nil-type istype? if printnil exit then
1605 pair-type istype? if ." (" printpair ." )" exit then
1606 primitive-proc-type istype? if printprim exit then
1607 compound-proc-type istype? if printcomp exit then
1608 none-type istype? if printnone exit then
1610 recoverable-exception throw" Tried to print object with unknown type."
1615 \ ---- Garbage Collection ---- {{{
1620 variable gc-stack-depth
1623 depth gc-stack-depth !
1627 false gc-enabled ! ;
1632 : pairlike? ( obj -- obj bool )
1633 pair-type istype? if true exit then
1634 string-type istype? if true exit then
1635 symbol-type istype? if true exit then
1636 compound-proc-type istype? if true exit then
1641 : pairlike-marked? ( obj -- obj bool )
1642 over nextfrees + @ 0=
1645 : mark-pairlike ( obj -- obj )
1646 over nextfrees + 0 swap !
1655 : gc-mark-obj ( obj -- )
1657 pairlike? invert if 2drop exit then
1658 pairlike-marked? if 2drop exit then
1669 scheme-memsize nextfree !
1670 0 scheme-memsize 1- do
1671 nextfrees i + @ 0<> if
1672 nextfree @ nextfrees i + !
1678 \ Following a GC, this gives the amount of free memory
1682 nextfrees i + @ 0= if 1+ then
1686 \ Debugging word - helps spot memory that is retained
1689 nextfrees i + @ 0<> if
1701 symbol-table obj@ gc-mark-obj
1702 macro-table obj@ gc-mark-obj
1703 global-env obj@ gc-mark-obj
1705 depth gc-stack-depth @ do
1714 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1715 ; is collect-garbage
1719 \ ---- Loading files ---- {{{
1721 : charlist>cstr ( charlist addr -- n )
1723 dup 2swap ( origaddr addr charlist )
1729 drop ( origaddr addr charlist char )
1730 -rot 2swap ( origaddr charlist addr char )
1732 1+ -rot ( origaddr nextaddr charlist )
1735 2drop ( origaddr finaladdr )
1739 : load ( addr n -- finalResult )
1744 ok-symbol ( port res )
1747 2over read-port ( port res obj )
1749 2dup EOF character-type objeq? if
1750 2drop 2swap close-port
1754 2swap 2drop ( port obj )
1756 global-env obj@ eval ( port res )
1762 \ ---- Standard Library ---- {{{
1764 include scheme-primitives.4th
1766 s" scheme-library.scm" load 2drop
1772 ( REPL calls REPL-BODY in a loop until repl-body returns true. )
1773 : repl-body ( -- bool )
1774 cr bold fg green ." > " reset-term
1778 2dup EOF character-type objeq? if
1780 bold fg blue ." Moriturus te saluto." reset-term cr
1784 global-env obj@ eval
1786 fg cyan ." ; " print reset-term
1792 cr ." Welcome to scheme.forth.jl!" cr
1793 ." Use Ctrl-D to exit." cr
1802 recoverable-exception of false endof
1803 unrecoverable-exception of true endof