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 -- symbol-obj )
246 2dup drop @ character-type 2swap
254 : create-symbol ( -- )
268 does> dup @ swap 1+ @
271 create-symbol quote quote-symbol
272 create-symbol define define-symbol
273 create-symbol define-macro define-macro-symbol
274 create-symbol set! set!-symbol
275 create-symbol ok ok-symbol
276 create-symbol if if-symbol
277 create-symbol lambda lambda-symbol
278 create-symbol λ λ-symbol
279 create-symbol begin begin-symbol
283 \ ---- Environments ---- {{{
285 : enclosing-env ( env -- env )
288 : first-frame ( env -- frame )
291 : make-frame ( vars vals -- frame )
294 : frame-vars ( frame -- vars )
297 : frame-vals ( frame -- vals )
300 : add-binding ( var val frame -- )
301 2swap 2over frame-vals cons
303 2swap 2over frame-vars cons
307 : extend-env ( vars vals env -- env )
317 : get-vars-vals-frame ( var frame -- bool )
318 2dup frame-vars vars obj!
322 vars obj@ nil objeq? false =
324 2dup vars obj@ car objeq? if
329 vars obj@ cdr vars obj!
330 vals obj@ cdr vals obj!
336 : get-vars-vals ( var env -- vars? vals? bool )
341 2over 2over first-frame
342 get-vars-vals-frame if
344 vars obj@ vals obj@ true
358 : lookup-var ( var env -- val )
362 bold fg red ." Tried to read unbound variable." reset-term cr abort
366 : set-var ( var val env -- )
367 >R >R 2swap R> R> ( val var env )
369 2swap 2drop ( val vals )
372 bold fg red ." Tried to set unbound variable." reset-term cr abort
378 : define-var ( var val env -- )
381 2over env obj@ ( var val var env )
383 2swap 2drop ( var val vals )
388 first-frame ( var val frame )
395 : make-procedure ( params body env -- proc )
398 drop compound-proc-type
402 nil nil nil extend-env
407 \ ---- Macros ---- {{{
411 ( Look up macro in macro table. Returns nil if
413 : lookup-macro ( name_symbol -- proc )
432 : make-macro ( name_symbol params body env -- )
435 2swap ( proc name_symbol )
442 2over 2over ( proc name table name table )
444 2swap 2drop ( proc table )
456 macro-table obj@ cons
465 variable stored-parse-idx
466 create parse-str 161 allot
467 variable parse-str-span
469 create parse-idx-stack 10 allot
470 variable parse-idx-sp
471 parse-idx-stack parse-idx-sp !
474 parse-idx @ parse-idx-sp @ !
479 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
483 parse-idx-sp @ @ parse-idx ! ;
487 '\n' parse-str parse-str-span @ + !
488 1 parse-str-span +! ;
491 4 parse-str parse-str-span @ + !
492 1 parse-str-span +! ;
499 current-input-port obj@ console-i/o-port obj@ objeq? if
500 parse-str 160 expect cr
501 span @ parse-str-span !
503 parse-str 160 current-input-port obj@ fileport>fid read-line
504 drop swap parse-str-span !
506 parse-str-span @ 0= and if append-eof then
517 : charavailable? ( -- bool )
518 parse-str-span @ parse-idx @ > ;
520 : nextchar ( -- char )
521 charavailable? false = if getline then
522 parse-str parse-idx @ + @ ;
525 : whitespace? ( -- bool )
537 nextchar [char] ( = or
538 nextchar [char] ) = or
541 : commentstart? ( -- bool )
542 nextchar [char] ; = ;
546 false \ Indicates whether or not we're eating a comment
549 dup whitespace? or commentstart? or
551 dup nextchar '\n' = and if
552 invert \ Stop eating comment
554 dup false = commentstart? and if
555 invert \ Begin eating comment
570 nextchar [char] - = ;
573 nextchar [char] + = ;
575 : fixnum? ( -- bool )
601 : realnum? ( -- bool )
608 \ Record starting parse idx:
609 \ Want to detect whether any characters (following +/-) were eaten.
616 [char] . nextchar = if
623 [char] e nextchar = [char] E nextchar = or if
631 drop pop-parse-idx false exit
639 \ This is a real number if characters were
640 \ eaten and the next characer is a delimiter.
641 parse-idx @ < delim? and
646 : boolean? ( -- bool )
647 nextchar [char] # <> if false exit then
654 and if pop-parse-idx false exit then
666 : str-equiv? ( str -- bool )
683 delim? false = if drop false then
688 : character? ( -- bool )
689 nextchar [char] # <> if false exit then
694 nextchar [char] \ <> if pop-parse-idx false exit then
698 S" newline" str-equiv? if pop-parse-idx true exit then
699 S" space" str-equiv? if pop-parse-idx true exit then
700 S" tab" str-equiv? if pop-parse-idx true exit then
702 charavailable? false = if pop-parse-idx false exit then
708 nextchar [char] ( = ;
710 : string? ( -- bool )
711 nextchar [char] " = ;
713 : readfixnum ( -- num-atom )
724 10 * nextchar [char] 0 - +
733 : readrealnum ( -- realnum )
735 \ Remember that at this point we're guaranteed to
736 \ have a parsable real on this line.
738 parse-str parse-idx @ +
740 begin delim? false = while
744 parse-str parse-idx @ + over -
751 : readbool ( -- bool-obj )
754 nextchar [char] f = if
765 : readchar ( -- char-obj )
769 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
770 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
771 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
773 nextchar character-type
778 : readstring ( -- charlist )
779 nextchar [char] " = if
784 ." No delimiter following right double quote. Aborting." cr
793 nextchar [char] \ = if
796 [char] n of '\n' endof
797 [char] " of [char] " endof
803 inc-parse-idx character-type
810 : readsymbol ( -- charlist )
811 delim? if nil exit then
813 nextchar inc-parse-idx character-type
820 : readpair ( -- pairobj )
824 nextchar [char] ) = if
829 ." No delimiter following right paren. Aborting." cr
838 \ Read first pair element
843 nextchar [char] . = if
848 ." No delimiter following '.'. Aborting." cr
862 \ Parse a scheme expression
893 nextchar [char] " <> if
894 bold red ." Missing closing double-quote." reset-term cr
912 nextchar [char] ) <> if
913 bold red ." Missing closing paren." reset-term cr
922 nextchar [char] ' = if
924 quote-symbol recurse nil cons cons exit
933 \ Anything else is parsed as a symbol
934 readsymbol charlist>symbol
936 \ Replace λ with lambda
937 2dup λ-symbol objeq? if
948 : self-evaluating? ( obj -- obj bool )
949 boolean-type istype? if true exit then
950 fixnum-type istype? if true exit then
951 realnum-type istype? if true exit then
952 character-type istype? if true exit then
953 string-type istype? if true exit then
954 nil-type istype? if true exit then
955 none-type istype? if true exit then
960 : tagged-list? ( obj tag-obj -- obj bool )
962 pair-type istype? false = if
968 : quote? ( obj -- obj bool )
969 quote-symbol tagged-list? ;
971 : quote-body ( quote-obj -- quote-body-obj )
974 : variable? ( obj -- obj bool )
975 symbol-type istype? ;
977 : definition? ( obj -- obj bool )
978 define-symbol tagged-list? ;
980 : make-lambda ( params body -- lambda-exp )
981 lambda-symbol -2rot cons cons ;
983 : definition-var ( obj -- var )
985 symbol-type istype? false = if car then
988 : definition-val ( obj -- val )
989 2dup cdr car symbol-type istype? if
998 : assignment? ( obj -- obj bool )
999 set!-symbol tagged-list? ;
1001 : assignment-var ( obj -- var )
1004 : assignment-val ( obj -- val )
1007 : eval-definition ( obj env -- res )
1009 2over 2over ( env obj env obj )
1010 definition-val 2swap ( env obj valexp env )
1011 eval ( env obj val )
1013 2swap definition-var 2swap ( env var val )
1015 2rot ( var val env )
1021 : eval-assignment ( obj env -- res )
1023 2over 2over ( env obj env obj )
1024 assignment-val 2swap ( env obj valexp env )
1025 eval ( env obj val )
1027 2swap assignment-var 2swap ( env var val )
1029 2rot ( var val env )
1035 : macro-definition? ( obj -- obj bool )
1036 define-macro-symbol tagged-list? ;
1038 : macro-definition-name ( exp -- mname )
1041 : macro-definition-params ( exp -- params )
1044 : macro-definition-body ( exp -- body )
1048 : eval-define-macro ( obj env -- res )
1051 2dup macro-definition-name 2swap ( name obj )
1052 2dup macro-definition-params 2swap ( name params obj )
1053 macro-definition-body ( name params body )
1055 env obj@ ( name params body env )
1063 : if? ( obj -- obj bool )
1064 if-symbol tagged-list? ;
1066 : if-predicate ( ifobj -- pred )
1069 : if-consequent ( ifobj -- conseq )
1072 : if-alternative ( ifobj -- alt|false )
1080 : false? ( boolobj -- boolean )
1081 boolean-type istype? if
1082 false boolean-type objeq?
1088 : true? ( boolobj -- bool )
1091 : lambda? ( obj -- obj bool )
1092 lambda-symbol tagged-list? ;
1094 : lambda-parameters ( obj -- params )
1097 : lambda-body ( obj -- body )
1100 : begin? ( obj -- obj bool )
1101 begin-symbol tagged-list? ;
1103 : begin-actions ( obj -- actions )
1106 : eval-sequence ( explist env -- finalexp env )
1107 ( Evaluates all bar the final expressions in
1108 an an expression list. The final expression
1109 is returned to allow for tail optimization. )
1111 2swap ( env explist )
1113 \ Abort on empty list
1120 2dup cdr ( env explist nextexplist )
1123 -2rot car 2over ( nextexplist env exp env )
1125 2drop \ discard result
1126 2swap ( env nextexplist )
1129 2drop car 2swap ( finalexp env )
1132 : application? ( obj -- obj bool )
1135 : operator ( obj -- operator )
1138 : operands ( obj -- operands )
1141 : nooperands? ( operands -- bool )
1144 : first-operand ( operands -- operand )
1147 : rest-operands ( operands -- other-operands )
1150 : list-of-vals ( args env -- vals )
1156 2over 2over first-operand 2swap eval
1157 -2rot rest-operands 2swap recurse
1162 : procedure-params ( proc -- params )
1163 drop pair-type car ;
1165 : procedure-body ( proc -- body )
1166 drop pair-type cdr car ;
1168 : procedure-env ( proc -- body )
1169 drop pair-type cdr cdr car ;
1171 ( Ensure terminating symbol arg name is handled
1172 specially to allow for variadic procedures. )
1173 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1176 symbol-type istype? if
1185 recurse ( argvals argnames argvals'' argnames'' )
1186 2rot car 2swap cons ( argvals argvals'' argnames' )
1187 2rot car 2rot cons ( argnames' argvals' )
1191 : apply ( proc argvals )
1193 primitive-proc-type of
1197 compound-proc-type of
1198 2dup procedure-body ( argvals proc body )
1199 -2rot 2dup procedure-params ( body argvals proc argnames )
1200 -2rot procedure-env ( body argnames argvals procenv )
1206 extend-env ( body env )
1210 R> drop ['] eval goto-deferred \ Tail call optimization
1213 bold fg red ." Object not applicable. Aboring." reset-term cr
1218 :noname ( obj env -- result )
1238 2swap eval-definition
1243 2swap eval-assignment
1247 macro-definition? if
1248 2swap eval-define-macro
1264 ['] eval goto-deferred
1268 2dup lambda-parameters
1277 ['] eval goto-deferred
1282 2over 2over ( env exp env exp )
1283 operator 2dup ( env exp env opname opname )
1285 lookup-macro nil? if
1286 \ Regular function application
1288 2drop ( env exp env opname )
1290 2swap eval ( env exp proc )
1291 -2rot ( proc env exp )
1292 operands 2swap ( proc operands env )
1293 list-of-vals ( proc argvals )
1297 \ Macro function evaluation
1301 ( env exp env opname mproc )
1302 2swap 2drop -2rot 2drop cdr ( env mproc body )
1304 \ TODO: evaluate macro procedure on expression body
1305 ." ABORTED: Macros not yet fully implemented!" abort
1310 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1316 \ ---- Print ---- {{{
1318 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1320 : printrealnum ( realnumobj -- ) drop float-print ;
1322 : printbool ( numobj -- )
1330 : printchar ( charobj -- )
1333 9 of ." #\tab" endof
1334 bl of ." #\space" endof
1335 '\n' of ." #\newline" endof
1341 : (printstring) ( stringobj -- )
1342 nil-type istype? if 2drop exit then
1346 '\n' of ." \n" drop endof
1347 [char] \ of ." \\" drop endof
1348 [char] " of [char] \ emit [char] " emit drop endof
1354 : printstring ( stringobj -- )
1359 : printsymbol ( symbolobj -- )
1360 nil-type istype? if 2drop exit then
1366 : printnil ( nilobj -- )
1369 : printpair ( pairobj -- )
1373 nil-type istype? if 2drop exit then
1374 pair-type istype? if space recurse exit then
1378 : printprim ( primobj -- )
1379 2drop ." <primitive procedure>" ;
1381 : printcomp ( primobj -- )
1382 2drop ." <compound procedure>" ;
1384 : printnone ( noneobj -- )
1385 2drop ." Unspecified return value" ;
1388 fixnum-type istype? if printfixnum exit then
1389 realnum-type istype? if printrealnum exit then
1390 boolean-type istype? if printbool exit then
1391 character-type istype? if printchar exit then
1392 string-type istype? if printstring exit then
1393 symbol-type istype? if printsymbol exit then
1394 nil-type istype? if printnil exit then
1395 pair-type istype? if ." (" printpair ." )" exit then
1396 primitive-proc-type istype? if printprim exit then
1397 compound-proc-type istype? if printcomp exit then
1398 none-type istype? if printnone exit then
1400 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1406 \ ---- Garbage Collection ---- {{{
1411 variable gc-stack-depth
1414 depth gc-stack-depth !
1418 false gc-enabled ! ;
1423 : pairlike? ( obj -- obj bool )
1424 pair-type istype? if true exit then
1425 string-type istype? if true exit then
1426 symbol-type istype? if true exit then
1427 compound-proc-type istype? if true exit then
1432 : pairlike-marked? ( obj -- obj bool )
1433 over nextfrees + @ 0=
1436 : mark-pairlike ( obj -- obj )
1437 over nextfrees + 0 swap !
1446 : gc-mark-obj ( obj -- )
1448 pairlike? invert if 2drop exit then
1449 pairlike-marked? if 2drop exit then
1460 scheme-memsize nextfree !
1461 0 scheme-memsize 1- do
1462 nextfrees i + @ 0<> if
1463 nextfree @ nextfrees i + !
1469 \ Following a GC, this gives the amount of free memory
1473 nextfrees i + @ 0= if 1+ then
1477 \ Debugging word - helps spot memory that is retained
1480 nextfrees i + @ 0<> if
1492 symbol-table obj@ gc-mark-obj
1493 macro-table obj@ gc-mark-obj
1494 global-env obj@ gc-mark-obj
1496 depth gc-stack-depth @ do
1505 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1506 ; is collect-garbage
1510 \ ---- Primitives ---- {{{
1512 : make-primitive ( cfa -- )
1516 \ 2dup ." Defining primitive " type ." ..." cr
1527 rot primitive-proc-type ( var prim )
1528 global-env obj@ define-var
1532 bold fg red ." Incorrect argument count." reset-term cr
1536 : ensure-arg-count ( args n -- )
1538 drop nil objeq? false = if
1551 bold fg red ." Incorrect argument type." reset-term cr
1555 : ensure-arg-type ( arg type -- arg )
1561 include scheme-primitives.4th
1565 \ ---- Loading files ---- {{{
1567 : charlist>cstr ( charlist addr -- n )
1569 dup 2swap ( origaddr addr charlist )
1575 drop ( origaddr addr charlist char )
1576 -rot 2swap ( origaddr charlist addr char )
1578 1+ -rot ( origaddr nextaddr charlist )
1581 2drop ( origaddr finaladdr )
1585 : load ( addr n -- finalResult )
1590 ok-symbol ( port res )
1593 2over read-port ( port res obj )
1595 2dup EOF character-type objeq? if
1596 2drop 2swap close-port
1600 2swap 2drop ( port obj )
1602 global-env obj@ eval ( port res )
1606 :noname ( args -- finalResult )
1607 2dup 1 ensure-arg-count
1608 car string-type ensure-arg-type
1613 ; make-primitive load
1620 cr ." Welcome to scheme.forth.jl!" cr
1621 ." Use Ctrl-D to exit." cr
1628 cr bold fg green ." > " reset-term
1631 2dup EOF character-type objeq? if
1633 bold fg blue ." Moriturus te saluto." reset-term cr
1637 global-env obj@ eval
1639 fg cyan ." ; " print reset-term