4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
34 make-type primitive-proc-type
35 make-type compound-proc-type
36 make-type fileport-type
37 : istype? ( obj type -- obj bool )
40 \ ---- List-structured memory ---- {{{
42 10000 constant scheme-memsize
44 create car-cells scheme-memsize allot
45 create car-type-cells scheme-memsize allot
46 create cdr-cells scheme-memsize allot
47 create cdr-type-cells scheme-memsize allot
49 create nextfrees scheme-memsize allot
60 nextfrees nextfree @ + @
63 nextfree @ scheme-memsize >= if
67 nextfree @ scheme-memsize >= if
69 ." Out of memory! Aborting."
74 : cons ( car-obj cdr-obj -- pair-obj )
75 cdr-type-cells nextfree @ + !
76 cdr-cells nextfree @ + !
77 car-type-cells nextfree @ + !
78 car-cells nextfree @ + !
84 : car ( pair-obj -- car-obj )
86 dup car-cells + @ swap
90 : cdr ( pair-obj -- car-obj )
92 dup cdr-cells + @ swap
96 : set-car! ( obj pair-obj -- )
98 rot swap car-type-cells + !
102 : set-cdr! ( obj pair-obj -- )
104 rot swap cdr-type-cells + !
114 : nil? nil-type istype? ;
117 : none? none-type istype? ;
119 : objvar create nil swap , , ;
121 : value@ ( objvar -- val ) @ ;
122 : type@ ( objvar -- type ) 1+ @ ;
123 : value! ( newval objvar -- ) ! ;
124 : type! ( newtype objvar -- ) 1+ ! ;
125 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
126 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
128 : objeq? ( obj obj -- bool )
131 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
132 >R >R ( a1 a2 b1 b2 )
133 2swap ( b1 b2 a1 a2 )
134 R> R> ( b1 b2 a1 a2 c1 c2 )
138 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
139 2swap ( a1 a2 c1 c2 b1 b2 )
140 >R >R ( a1 a2 c1 c2 )
141 2swap ( c1 c2 a1 a2 )
147 \ ---- Port I/O ---- {{{
149 : fileport>fid ( fileport -- fid )
152 : fid>fileport ( fid -- fileport )
155 : open-input-file ( addr n -- fileport )
156 r/o open-file drop fid>fileport
159 : close-port ( fileport -- )
160 fileport>fid close-file drop
163 objvar console-i/o-port
164 0 fileport-type console-i/o-port obj!
166 objvar current-input-port
167 console-i/o-port obj@ current-input-port obj!
169 : read-port ( fileport -- obj )
170 current-input-port obj!
173 : read-console ( -- obj )
174 console-i/o-port obj@ read-port ;
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 define define-symbol
267 create-symbol define-macro define-macro-symbol
268 create-symbol set! set!-symbol
269 create-symbol ok ok-symbol
270 create-symbol if if-symbol
271 create-symbol lambda lambda-symbol
272 create-symbol λ λ-symbol
273 create-symbol begin begin-symbol
274 create-symbol apply apply-symbol
278 \ ---- Environments ---- {{{
280 : enclosing-env ( env -- env )
283 : first-frame ( env -- frame )
286 : make-frame ( vars vals -- frame )
289 : frame-vars ( frame -- vars )
292 : frame-vals ( frame -- vals )
295 : add-binding ( var val frame -- )
296 2swap 2over frame-vals cons
298 2swap 2over frame-vars cons
302 : extend-env ( vars vals env -- env )
312 : get-vars-vals-frame ( var frame -- bool )
313 2dup frame-vars vars obj!
317 vars obj@ nil objeq? false =
319 2dup vars obj@ car objeq? if
324 vars obj@ cdr vars obj!
325 vals obj@ cdr vals obj!
331 : get-vars-vals ( var env -- vars? vals? bool )
336 2over 2over first-frame
337 get-vars-vals-frame if
339 vars obj@ vals obj@ true
353 : lookup-var ( var env -- val )
357 bold fg red ." Tried to read unbound variable." reset-term cr abort
361 : set-var ( var val env -- )
362 >R >R 2swap R> R> ( val var env )
364 2swap 2drop ( val vals )
367 bold fg red ." Tried to set unbound variable." reset-term cr abort
373 : define-var ( var val env -- )
376 2over env obj@ ( var val var env )
378 2swap 2drop ( var val vals )
383 first-frame ( var val frame )
390 : make-procedure ( params body env -- proc )
393 drop compound-proc-type
397 nil nil nil extend-env
402 \ ---- Macros ---- {{{
406 ( Look up macro in macro table. Returns nil if
408 : lookup-macro ( name_symbol -- proc )
427 : make-macro ( name_symbol params body env -- )
430 2swap ( proc name_symbol )
437 2over 2over ( proc name table name table )
439 2swap 2drop ( proc table )
451 macro-table obj@ cons
460 variable stored-parse-idx
461 create parse-str 161 allot
462 variable parse-str-span
464 create parse-idx-stack 10 allot
465 variable parse-idx-sp
466 parse-idx-stack parse-idx-sp !
469 parse-idx @ parse-idx-sp @ !
474 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
478 parse-idx-sp @ @ parse-idx ! ;
482 '\n' parse-str parse-str-span @ + !
483 1 parse-str-span +! ;
486 4 parse-str parse-str-span @ + !
487 1 parse-str-span +! ;
494 current-input-port obj@ console-i/o-port obj@ objeq? if
495 parse-str 160 expect cr
496 span @ parse-str-span !
498 parse-str 160 current-input-port obj@ fileport>fid read-line
499 drop swap parse-str-span !
501 parse-str-span @ 0= and if append-eof then
512 : charavailable? ( -- bool )
513 parse-str-span @ parse-idx @ > ;
515 : nextchar ( -- char )
516 charavailable? false = if getline then
517 parse-str parse-idx @ + @ ;
520 : whitespace? ( -- bool )
532 nextchar [char] ( = or
533 nextchar [char] ) = or
536 : commentstart? ( -- bool )
537 nextchar [char] ; = ;
541 false \ Indicates whether or not we're eating a comment
544 dup whitespace? or commentstart? or
546 dup nextchar '\n' = and if
547 invert \ Stop eating comment
549 dup false = commentstart? and if
550 invert \ Begin eating comment
565 nextchar [char] - = ;
568 nextchar [char] + = ;
570 : fixnum? ( -- bool )
596 : realnum? ( -- bool )
603 \ Record starting parse idx:
604 \ Want to detect whether any characters (following +/-) were eaten.
611 [char] . nextchar = if
618 [char] e nextchar = [char] E nextchar = or if
626 drop pop-parse-idx false exit
634 \ This is a real number if characters were
635 \ eaten and the next characer is a delimiter.
636 parse-idx @ < delim? and
641 : boolean? ( -- bool )
642 nextchar [char] # <> if false exit then
649 and if pop-parse-idx false exit then
661 : str-equiv? ( str -- bool )
678 delim? false = if drop false then
683 : character? ( -- bool )
684 nextchar [char] # <> if false exit then
689 nextchar [char] \ <> if pop-parse-idx false exit then
693 S" newline" str-equiv? if pop-parse-idx true exit then
694 S" space" str-equiv? if pop-parse-idx true exit then
695 S" tab" str-equiv? if pop-parse-idx true exit then
697 charavailable? false = if pop-parse-idx false exit then
703 nextchar [char] ( = ;
705 : string? ( -- bool )
706 nextchar [char] " = ;
708 : readfixnum ( -- num-atom )
719 10 * nextchar [char] 0 - +
728 : readrealnum ( -- realnum )
730 \ Remember that at this point we're guaranteed to
731 \ have a parsable real on this line.
733 parse-str parse-idx @ +
735 begin delim? false = while
739 parse-str parse-idx @ + over -
746 : readbool ( -- bool-obj )
749 nextchar [char] f = if
760 : readchar ( -- char-obj )
764 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
765 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
766 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
768 nextchar character-type
773 : readstring ( -- charlist )
774 nextchar [char] " = if
779 ." No delimiter following right double quote. Aborting." cr
788 nextchar [char] \ = if
791 [char] n of '\n' endof
792 [char] " of [char] " endof
798 inc-parse-idx character-type
805 : readsymbol ( -- charlist )
806 delim? if nil exit then
808 nextchar inc-parse-idx character-type
815 : readpair ( -- pairobj )
819 nextchar [char] ) = if
824 ." No delimiter following right paren. Aborting." cr
833 \ Read first pair element
838 nextchar [char] . = if
843 ." No delimiter following '.'. Aborting." cr
857 \ Parse a scheme expression
888 nextchar [char] " <> if
889 bold red ." Missing closing double-quote." reset-term cr
907 nextchar [char] ) <> if
908 bold red ." Missing closing paren." reset-term cr
917 nextchar [char] ' = if
919 quote-symbol recurse nil cons cons exit
928 \ Anything else is parsed as a symbol
929 readsymbol charlist>symbol
931 \ Replace λ with lambda
932 2dup λ-symbol objeq? if
943 : self-evaluating? ( obj -- obj bool )
944 boolean-type istype? if true exit then
945 fixnum-type istype? if true exit then
946 realnum-type istype? if true exit then
947 character-type istype? if true exit then
948 string-type istype? if true exit then
949 nil-type istype? if true exit then
950 none-type istype? if true exit then
955 : tagged-list? ( obj tag-obj -- obj bool )
957 pair-type istype? false = if
963 : quote? ( obj -- obj bool )
964 quote-symbol tagged-list? ;
966 : quote-body ( quote-obj -- quote-body-obj )
969 : variable? ( obj -- obj bool )
970 symbol-type istype? ;
972 : definition? ( obj -- obj bool )
973 define-symbol tagged-list? ;
975 : make-lambda ( params body -- lambda-exp )
976 lambda-symbol -2rot cons cons ;
978 : definition-var ( obj -- var )
980 symbol-type istype? false = if car then
983 : definition-val ( obj -- val )
984 2dup cdr car symbol-type istype? if
993 : assignment? ( obj -- obj bool )
994 set!-symbol tagged-list? ;
996 : assignment-var ( obj -- var )
999 : assignment-val ( obj -- val )
1002 : eval-definition ( obj env -- res )
1004 2over 2over ( env obj env obj )
1005 definition-val 2swap ( env obj valexp env )
1006 eval ( env obj val )
1008 2swap definition-var 2swap ( env var val )
1010 2rot ( var val env )
1016 : eval-assignment ( obj env -- res )
1018 2over 2over ( env obj env obj )
1019 assignment-val 2swap ( env obj valexp env )
1020 eval ( env obj val )
1022 2swap assignment-var 2swap ( env var val )
1024 2rot ( var val env )
1030 : macro-definition? ( obj -- obj bool )
1031 define-macro-symbol tagged-list? ;
1033 : macro-definition-name ( exp -- mname )
1036 : macro-definition-params ( exp -- params )
1039 : macro-definition-body ( exp -- body )
1043 : eval-define-macro ( obj env -- res )
1046 2dup macro-definition-name 2swap ( name obj )
1047 2dup macro-definition-params 2swap ( name params obj )
1048 macro-definition-body ( name params body )
1050 env obj@ ( name params body env )
1058 : if? ( obj -- obj bool )
1059 if-symbol tagged-list? ;
1061 : if-predicate ( ifobj -- pred )
1064 : if-consequent ( ifobj -- conseq )
1067 : if-alternative ( ifobj -- alt|false )
1075 : false? ( boolobj -- boolean )
1076 boolean-type istype? if
1077 false boolean-type objeq?
1083 : true? ( boolobj -- bool )
1086 : lambda? ( obj -- obj bool )
1087 lambda-symbol tagged-list? ;
1089 : lambda-parameters ( obj -- params )
1092 : lambda-body ( obj -- body )
1095 : begin? ( obj -- obj bool )
1096 begin-symbol tagged-list? ;
1098 : begin-actions ( obj -- actions )
1101 : eval-sequence ( explist env -- finalexp env )
1102 ( Evaluates all bar the final expressions in
1103 an an expression list. The final expression
1104 is returned to allow for tail optimization. )
1106 2swap ( env explist )
1108 \ Abort on empty list
1115 2dup cdr ( env explist nextexplist )
1118 -2rot car 2over ( nextexplist env exp env )
1120 2drop \ discard result
1121 2swap ( env nextexplist )
1124 2drop car 2swap ( finalexp env )
1127 : application? ( obj -- obj bool )
1130 : operator ( obj -- operator )
1133 : operands ( obj -- operands )
1136 : nooperands? ( operands -- bool )
1139 : first-operand ( operands -- operand )
1142 : rest-operands ( operands -- other-operands )
1145 : list-of-vals ( args env -- vals )
1151 2over 2over first-operand 2swap eval
1152 -2rot rest-operands 2swap recurse
1157 : procedure-params ( proc -- params )
1158 drop pair-type car ;
1160 : procedure-body ( proc -- body )
1161 drop pair-type cdr car ;
1163 : procedure-env ( proc -- body )
1164 drop pair-type cdr cdr car ;
1166 ( Ensure terminating symbol arg name is handled
1167 specially to allow for variadic procedures. )
1168 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1171 symbol-type istype? if
1180 recurse ( argvals argnames argvals'' argnames'' )
1181 2rot car 2swap cons ( argvals argvals'' argnames' )
1182 2rot car 2rot cons ( argnames' argvals' )
1186 : apply ( proc argvals -- result )
1188 primitive-proc-type of
1192 compound-proc-type of
1193 2dup procedure-body ( argvals proc body )
1194 -2rot 2dup procedure-params ( body argvals proc argnames )
1195 -2rot procedure-env ( body argnames argvals procenv )
1201 extend-env ( body env )
1205 R> drop ['] eval goto-deferred \ Tail call optimization
1208 bold fg red ." Object not applicable. Aborting." reset-term cr
1213 : macro-expand ( proc expbody -- result )
1215 2dup procedure-body ( expbody proc procbody )
1216 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1217 -2rot procedure-env ( procbody argnames expbody procenv )
1223 extend-env eval-sequence eval
1226 :noname ( obj env -- result )
1246 2swap eval-definition
1251 2swap eval-assignment
1255 macro-definition? if
1256 2swap eval-define-macro
1272 ['] eval goto-deferred
1276 2dup lambda-parameters
1285 ['] eval goto-deferred
1290 2over 2over ( env exp env exp )
1291 operator ( env exp env opname )
1293 2dup apply-symbol objeq? if
1295 2drop 2swap ( env env exp )
1296 cdr 2dup car 2rot ( env expbody real-opname env )
1297 eval ( env expbody proc )
1299 nil? false = if car then ( env proc real-operand )
1301 2rot eval ( proc argvals )
1303 pair-type istype? false = if
1304 bold fg red ." Error: apply requires a list of operand arguments." cr
1312 2dup lookup-macro nil? false = if
1313 \ Macro function evaluation
1315 ( env exp env opname mproc )
1316 2swap 2drop -2rot 2drop cdr ( env mproc body )
1323 ['] eval goto-deferred
1326 \ Regular function application
1328 2drop ( env exp env opname )
1330 2swap eval ( env exp proc )
1331 -2rot ( proc env exp )
1332 operands 2swap ( proc operands env )
1333 list-of-vals ( proc argvals )
1339 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1345 \ ---- Print ---- {{{
1347 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1349 : printrealnum ( realnumobj -- ) drop float-print ;
1351 : printbool ( numobj -- )
1359 : printchar ( charobj -- )
1362 9 of ." #\tab" endof
1363 bl of ." #\space" endof
1364 '\n' of ." #\newline" endof
1370 : (printstring) ( stringobj -- )
1371 nil-type istype? if 2drop exit then
1375 '\n' of ." \n" drop endof
1376 [char] \ of ." \\" drop endof
1377 [char] " of [char] \ emit [char] " emit drop endof
1383 : printstring ( stringobj -- )
1388 : printsymbol ( symbolobj -- )
1389 nil-type istype? if 2drop exit then
1395 : printnil ( nilobj -- )
1398 : printpair ( pairobj -- )
1402 nil-type istype? if 2drop exit then
1403 pair-type istype? if space recurse exit then
1407 : printprim ( primobj -- )
1408 2drop ." <primitive procedure>" ;
1410 : printcomp ( primobj -- )
1411 2drop ." <compound procedure>" ;
1413 : printnone ( noneobj -- )
1414 2drop ." Unspecified return value" ;
1417 fixnum-type istype? if printfixnum exit then
1418 realnum-type istype? if printrealnum exit then
1419 boolean-type istype? if printbool exit then
1420 character-type istype? if printchar exit then
1421 string-type istype? if printstring exit then
1422 symbol-type istype? if printsymbol exit then
1423 nil-type istype? if printnil exit then
1424 pair-type istype? if ." (" printpair ." )" exit then
1425 primitive-proc-type istype? if printprim exit then
1426 compound-proc-type istype? if printcomp exit then
1427 none-type istype? if printnone exit then
1429 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1435 \ ---- Garbage Collection ---- {{{
1440 variable gc-stack-depth
1443 depth gc-stack-depth !
1447 false gc-enabled ! ;
1452 : pairlike? ( obj -- obj bool )
1453 pair-type istype? if true exit then
1454 string-type istype? if true exit then
1455 symbol-type istype? if true exit then
1456 compound-proc-type istype? if true exit then
1461 : pairlike-marked? ( obj -- obj bool )
1462 over nextfrees + @ 0=
1465 : mark-pairlike ( obj -- obj )
1466 over nextfrees + 0 swap !
1475 : gc-mark-obj ( obj -- )
1477 pairlike? invert if 2drop exit then
1478 pairlike-marked? if 2drop exit then
1489 scheme-memsize nextfree !
1490 0 scheme-memsize 1- do
1491 nextfrees i + @ 0<> if
1492 nextfree @ nextfrees i + !
1498 \ Following a GC, this gives the amount of free memory
1502 nextfrees i + @ 0= if 1+ then
1506 \ Debugging word - helps spot memory that is retained
1509 nextfrees i + @ 0<> if
1521 symbol-table obj@ gc-mark-obj
1522 macro-table obj@ gc-mark-obj
1523 global-env obj@ gc-mark-obj
1525 depth gc-stack-depth @ do
1534 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1535 ; is collect-garbage
1539 \ ---- Loading files ---- {{{
1541 : charlist>cstr ( charlist addr -- n )
1543 dup 2swap ( origaddr addr charlist )
1549 drop ( origaddr addr charlist char )
1550 -rot 2swap ( origaddr charlist addr char )
1552 1+ -rot ( origaddr nextaddr charlist )
1555 2drop ( origaddr finaladdr )
1559 : load ( addr n -- finalResult )
1564 ok-symbol ( port res )
1567 2over read-port ( port res obj )
1569 2dup EOF character-type objeq? if
1570 2drop 2swap close-port
1574 2swap 2drop ( port obj )
1576 global-env obj@ eval ( port res )
1582 \ ---- Primitives ---- {{{
1584 : make-primitive ( cfa -- )
1588 \ 2dup ." Defining primitive " type ." ..." cr
1593 rot primitive-proc-type ( var prim )
1594 global-env obj@ define-var
1598 bold fg red ." Incorrect argument count." reset-term cr
1602 : ensure-arg-count ( args n -- )
1604 drop nil objeq? false = if
1617 bold fg red ." Incorrect argument type." reset-term cr
1621 : ensure-arg-type ( arg type -- arg )
1627 include scheme-primitives.4th
1631 \ ---- Standard Library ---- {{{
1633 s" scheme-library.scm" load 2drop
1640 cr ." Welcome to scheme.forth.jl!" cr
1641 ." Use Ctrl-D to exit." cr
1648 cr bold fg green ." > " reset-term
1651 2dup EOF character-type objeq? if
1653 bold fg blue ." Moriturus te saluto." reset-term cr
1657 global-env obj@ eval
1659 fg cyan ." ; " print reset-term