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
277 \ ---- Environments ---- {{{
279 : enclosing-env ( env -- env )
282 : first-frame ( env -- frame )
285 : make-frame ( vars vals -- frame )
288 : frame-vars ( frame -- vars )
291 : frame-vals ( frame -- vals )
294 : add-binding ( var val frame -- )
295 2swap 2over frame-vals cons
297 2swap 2over frame-vars cons
301 : extend-env ( vars vals env -- env )
311 : get-vars-vals-frame ( var frame -- bool )
312 2dup frame-vars vars obj!
316 vars obj@ nil objeq? false =
318 2dup vars obj@ car objeq? if
323 vars obj@ cdr vars obj!
324 vals obj@ cdr vals obj!
330 : get-vars-vals ( var env -- vars? vals? bool )
335 2over 2over first-frame
336 get-vars-vals-frame if
338 vars obj@ vals obj@ true
352 : lookup-var ( var env -- val )
356 bold fg red ." Tried to read unbound variable." reset-term cr abort
360 : set-var ( var val env -- )
361 >R >R 2swap R> R> ( val var env )
363 2swap 2drop ( val vals )
366 bold fg red ." Tried to set unbound variable." reset-term cr abort
372 : define-var ( var val env -- )
375 2over env obj@ ( var val var env )
377 2swap 2drop ( var val vals )
382 first-frame ( var val frame )
389 : make-procedure ( params body env -- proc )
392 drop compound-proc-type
396 nil nil nil extend-env
401 \ ---- Primitives ---- {{{
403 : make-primitive ( cfa -- )
407 \ 2dup ." Defining primitive " type ." ..." cr
412 rot primitive-proc-type ( var prim )
413 global-env obj@ define-var
417 bold fg red ." Incorrect argument count." reset-term cr
421 : ensure-arg-count ( args n -- )
423 drop nil objeq? false = if
436 bold fg red ." Incorrect argument type." reset-term cr
440 : ensure-arg-type ( arg type -- arg )
449 \ ---- Macros ---- {{{
453 ( Look up macro in macro table. Returns nil if
455 : lookup-macro ( name_symbol -- proc )
474 : make-macro ( name_symbol params body env -- )
477 2swap ( proc name_symbol )
484 2over 2over ( proc name table name table )
486 2swap 2drop ( proc table )
498 macro-table obj@ cons
507 variable stored-parse-idx
508 create parse-str 161 allot
509 variable parse-str-span
511 create parse-idx-stack 10 allot
512 variable parse-idx-sp
513 parse-idx-stack parse-idx-sp !
516 parse-idx @ parse-idx-sp @ !
521 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
525 parse-idx-sp @ @ parse-idx ! ;
529 '\n' parse-str parse-str-span @ + !
530 1 parse-str-span +! ;
533 4 parse-str parse-str-span @ + !
534 1 parse-str-span +! ;
541 current-input-port obj@ console-i/o-port obj@ objeq? if
542 parse-str 160 expect cr
543 span @ parse-str-span !
545 parse-str 160 current-input-port obj@ fileport>fid read-line
546 drop swap parse-str-span !
548 parse-str-span @ 0= and if append-eof then
559 : charavailable? ( -- bool )
560 parse-str-span @ parse-idx @ > ;
562 : nextchar ( -- char )
563 charavailable? false = if getline then
564 parse-str parse-idx @ + @ ;
567 : whitespace? ( -- bool )
579 nextchar [char] ( = or
580 nextchar [char] ) = or
583 : commentstart? ( -- bool )
584 nextchar [char] ; = ;
588 false \ Indicates whether or not we're eating a comment
591 dup whitespace? or commentstart? or
593 dup nextchar '\n' = and if
594 invert \ Stop eating comment
596 dup false = commentstart? and if
597 invert \ Begin eating comment
612 nextchar [char] - = ;
615 nextchar [char] + = ;
617 : fixnum? ( -- bool )
643 : realnum? ( -- bool )
650 \ Record starting parse idx:
651 \ Want to detect whether any characters (following +/-) were eaten.
658 [char] . nextchar = if
665 [char] e nextchar = [char] E nextchar = or if
673 drop pop-parse-idx false exit
681 \ This is a real number if characters were
682 \ eaten and the next characer is a delimiter.
683 parse-idx @ < delim? and
688 : boolean? ( -- bool )
689 nextchar [char] # <> if false exit then
696 and if pop-parse-idx false exit then
708 : str-equiv? ( str -- bool )
725 delim? false = if drop false then
730 : character? ( -- bool )
731 nextchar [char] # <> if false exit then
736 nextchar [char] \ <> if pop-parse-idx false exit then
740 S" newline" str-equiv? if pop-parse-idx true exit then
741 S" space" str-equiv? if pop-parse-idx true exit then
742 S" tab" str-equiv? if pop-parse-idx true exit then
744 charavailable? false = if pop-parse-idx false exit then
750 nextchar [char] ( = ;
752 : string? ( -- bool )
753 nextchar [char] " = ;
755 : readfixnum ( -- num-atom )
766 10 * nextchar [char] 0 - +
775 : readrealnum ( -- realnum )
777 \ Remember that at this point we're guaranteed to
778 \ have a parsable real on this line.
780 parse-str parse-idx @ +
782 begin delim? false = while
786 parse-str parse-idx @ + over -
793 : readbool ( -- bool-obj )
796 nextchar [char] f = if
807 : readchar ( -- char-obj )
811 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
812 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
813 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
815 nextchar character-type
820 : readstring ( -- charlist )
821 nextchar [char] " = if
826 ." No delimiter following right double quote. Aborting." cr
835 nextchar [char] \ = if
838 [char] n of '\n' endof
839 [char] " of [char] " endof
845 inc-parse-idx character-type
852 : readsymbol ( -- charlist )
853 delim? if nil exit then
855 nextchar inc-parse-idx character-type
862 : readpair ( -- pairobj )
866 nextchar [char] ) = if
871 ." No delimiter following right paren. Aborting." cr
880 \ Read first pair element
885 nextchar [char] . = if
890 ." No delimiter following '.'. Aborting." cr
904 \ Parse a scheme expression
935 nextchar [char] " <> if
936 bold red ." Missing closing double-quote." reset-term cr
954 nextchar [char] ) <> if
955 bold red ." Missing closing paren." reset-term cr
964 nextchar [char] ' = if
966 quote-symbol recurse nil cons cons exit
975 \ Anything else is parsed as a symbol
976 readsymbol charlist>symbol
978 \ Replace λ with lambda
979 2dup λ-symbol objeq? if
990 : self-evaluating? ( obj -- obj bool )
991 boolean-type istype? if true exit then
992 fixnum-type istype? if true exit then
993 realnum-type istype? if true exit then
994 character-type istype? if true exit then
995 string-type istype? if true exit then
996 nil-type istype? if true exit then
997 none-type istype? if true exit then
1002 : tagged-list? ( obj tag-obj -- obj bool )
1004 pair-type istype? false = if
1010 : quote? ( obj -- obj bool )
1011 quote-symbol tagged-list? ;
1013 : quote-body ( quote-obj -- quote-body-obj )
1016 : variable? ( obj -- obj bool )
1017 symbol-type istype? ;
1019 : definition? ( obj -- obj bool )
1020 define-symbol tagged-list? ;
1022 : make-lambda ( params body -- lambda-exp )
1023 lambda-symbol -2rot cons cons ;
1025 : definition-var ( obj -- var )
1027 symbol-type istype? false = if car then
1030 : definition-val ( obj -- val )
1031 2dup cdr car symbol-type istype? if
1040 : assignment? ( obj -- obj bool )
1041 set!-symbol tagged-list? ;
1043 : assignment-var ( obj -- var )
1046 : assignment-val ( obj -- val )
1049 : eval-definition ( obj env -- res )
1051 2over 2over ( env obj env obj )
1052 definition-val 2swap ( env obj valexp env )
1053 eval ( env obj val )
1055 2swap definition-var 2swap ( env var val )
1057 2rot ( var val env )
1063 : eval-assignment ( obj env -- res )
1065 2over 2over ( env obj env obj )
1066 assignment-val 2swap ( env obj valexp env )
1067 eval ( env obj val )
1069 2swap assignment-var 2swap ( env var val )
1071 2rot ( var val env )
1077 : macro-definition? ( obj -- obj bool )
1078 define-macro-symbol tagged-list? ;
1080 : macro-definition-name ( exp -- mname )
1083 : macro-definition-params ( exp -- params )
1086 : macro-definition-body ( exp -- body )
1090 : eval-define-macro ( obj env -- res )
1093 2dup macro-definition-name 2swap ( name obj )
1094 2dup macro-definition-params 2swap ( name params obj )
1095 macro-definition-body ( name params body )
1097 env obj@ ( name params body env )
1105 : if? ( obj -- obj bool )
1106 if-symbol tagged-list? ;
1108 : if-predicate ( ifobj -- pred )
1111 : if-consequent ( ifobj -- conseq )
1114 : if-alternative ( ifobj -- alt|false )
1122 : false? ( boolobj -- boolean )
1123 boolean-type istype? if
1124 false boolean-type objeq?
1130 : true? ( boolobj -- bool )
1133 : lambda? ( obj -- obj bool )
1134 lambda-symbol tagged-list? ;
1136 : lambda-parameters ( obj -- params )
1139 : lambda-body ( obj -- body )
1142 : begin? ( obj -- obj bool )
1143 begin-symbol tagged-list? ;
1145 : begin-actions ( obj -- actions )
1148 : eval-sequence ( explist env -- finalexp env )
1149 ( Evaluates all bar the final expressions in
1150 an an expression list. The final expression
1151 is returned to allow for tail optimization. )
1153 2swap ( env explist )
1155 \ Abort on empty list
1162 2dup cdr ( env explist nextexplist )
1165 -2rot car 2over ( nextexplist env exp env )
1167 2drop \ discard result
1168 2swap ( env nextexplist )
1171 2drop car 2swap ( finalexp env )
1174 : application? ( obj -- obj bool )
1177 : operator ( obj -- operator )
1180 : operands ( obj -- operands )
1183 : nooperands? ( operands -- bool )
1186 : first-operand ( operands -- operand )
1189 : rest-operands ( operands -- other-operands )
1192 : list-of-vals ( args env -- vals )
1198 2over 2over first-operand 2swap eval
1199 -2rot rest-operands 2swap recurse
1204 : procedure-params ( proc -- params )
1205 drop pair-type car ;
1207 : procedure-body ( proc -- body )
1208 drop pair-type cdr car ;
1210 : procedure-env ( proc -- body )
1211 drop pair-type cdr cdr car ;
1213 ( Ensure terminating symbol arg name is handled
1214 specially to allow for variadic procedures. )
1215 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1218 symbol-type istype? if
1227 recurse ( argvals argnames argvals'' argnames'' )
1228 2rot car 2swap cons ( argvals argvals'' argnames' )
1229 2rot car 2rot cons ( argnames' argvals' )
1233 : apply ( proc argvals -- result )
1235 primitive-proc-type of
1239 compound-proc-type of
1240 2dup procedure-body ( argvals proc body )
1241 -2rot 2dup procedure-params ( body argvals proc argnames )
1242 -2rot procedure-env ( body argnames argvals procenv )
1248 extend-env ( body env )
1252 R> drop ['] eval goto-deferred \ Tail call optimization
1255 bold fg red ." Object not applicable. Aborting." reset-term cr
1260 : macro-expand ( proc expbody -- result )
1262 2dup procedure-body ( expbody proc procbody )
1263 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1264 -2rot procedure-env ( procbody argnames expbody procenv )
1270 extend-env eval-sequence eval
1273 :noname ( obj env -- result )
1293 2swap eval-definition
1298 2swap eval-assignment
1302 macro-definition? if
1303 2swap eval-define-macro
1319 ['] eval goto-deferred
1323 2dup lambda-parameters
1332 ['] eval goto-deferred
1337 2over 2over ( env exp env exp )
1338 operator ( env exp env opname )
1340 2dup lookup-macro nil? false = if
1341 \ Macro function evaluation
1343 ( env exp env opname mproc )
1344 2swap 2drop -2rot 2drop cdr ( env mproc body )
1351 ['] eval goto-deferred
1353 \ Regular function application
1355 2drop ( env exp env opname )
1357 2swap eval ( env exp proc )
1359 -2rot ( proc env exp )
1360 operands 2swap ( proc operands env )
1361 list-of-vals ( proc argvals )
1368 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1374 \ ---- Print ---- {{{
1376 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1378 : printrealnum ( realnumobj -- ) drop float-print ;
1380 : printbool ( numobj -- )
1388 : printchar ( charobj -- )
1391 9 of ." #\tab" endof
1392 bl of ." #\space" endof
1393 '\n' of ." #\newline" endof
1399 : (printstring) ( stringobj -- )
1400 nil-type istype? if 2drop exit then
1404 '\n' of ." \n" drop endof
1405 [char] \ of ." \\" drop endof
1406 [char] " of [char] \ emit [char] " emit drop endof
1412 : printstring ( stringobj -- )
1417 : printsymbol ( symbolobj -- )
1418 nil-type istype? if 2drop exit then
1424 : printnil ( nilobj -- )
1427 : printpair ( pairobj -- )
1431 nil-type istype? if 2drop exit then
1432 pair-type istype? if space recurse exit then
1436 : printprim ( primobj -- )
1437 2drop ." <primitive procedure>" ;
1439 : printcomp ( primobj -- )
1440 2drop ." <compound procedure>" ;
1442 : printnone ( noneobj -- )
1443 2drop ." Unspecified return value" ;
1446 fixnum-type istype? if printfixnum exit then
1447 realnum-type istype? if printrealnum exit then
1448 boolean-type istype? if printbool exit then
1449 character-type istype? if printchar exit then
1450 string-type istype? if printstring exit then
1451 symbol-type istype? if printsymbol exit then
1452 nil-type istype? if printnil exit then
1453 pair-type istype? if ." (" printpair ." )" exit then
1454 primitive-proc-type istype? if printprim exit then
1455 compound-proc-type istype? if printcomp exit then
1456 none-type istype? if printnone exit then
1458 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1464 \ ---- Garbage Collection ---- {{{
1469 variable gc-stack-depth
1472 depth gc-stack-depth !
1476 false gc-enabled ! ;
1481 : pairlike? ( obj -- obj bool )
1482 pair-type istype? if true exit then
1483 string-type istype? if true exit then
1484 symbol-type istype? if true exit then
1485 compound-proc-type istype? if true exit then
1490 : pairlike-marked? ( obj -- obj bool )
1491 over nextfrees + @ 0=
1494 : mark-pairlike ( obj -- obj )
1495 over nextfrees + 0 swap !
1504 : gc-mark-obj ( obj -- )
1506 pairlike? invert if 2drop exit then
1507 pairlike-marked? if 2drop exit then
1518 scheme-memsize nextfree !
1519 0 scheme-memsize 1- do
1520 nextfrees i + @ 0<> if
1521 nextfree @ nextfrees i + !
1527 \ Following a GC, this gives the amount of free memory
1531 nextfrees i + @ 0= if 1+ then
1535 \ Debugging word - helps spot memory that is retained
1538 nextfrees i + @ 0<> if
1550 symbol-table obj@ gc-mark-obj
1551 macro-table obj@ gc-mark-obj
1552 global-env obj@ gc-mark-obj
1554 depth gc-stack-depth @ do
1563 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1564 ; is collect-garbage
1568 \ ---- Loading files ---- {{{
1570 : charlist>cstr ( charlist addr -- n )
1572 dup 2swap ( origaddr addr charlist )
1578 drop ( origaddr addr charlist char )
1579 -rot 2swap ( origaddr charlist addr char )
1581 1+ -rot ( origaddr nextaddr charlist )
1584 2drop ( origaddr finaladdr )
1588 : load ( addr n -- finalResult )
1593 ok-symbol ( port res )
1596 2over read-port ( port res obj )
1598 2dup EOF character-type objeq? if
1599 2drop 2swap close-port
1603 2swap 2drop ( port obj )
1605 global-env obj@ eval ( port res )
1611 \ ---- Standard Library ---- {{{
1613 include scheme-primitives.4th
1615 s" scheme-library.scm" load 2drop
1622 cr ." Welcome to scheme.forth.jl!" cr
1623 ." Use Ctrl-D to exit." cr
1630 cr bold fg green ." > " reset-term
1633 2dup EOF character-type objeq? if
1635 bold fg blue ." Moriturus te saluto." reset-term cr
1639 global-env obj@ eval
1641 fg cyan ." ; " print reset-term