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 ( Handles iterative expansion of defines in
1026 terms of nested lambdas. Most Schemes only
1027 handle one iteration of expansion! )
1028 : definition-var-val ( obj -- var val )
1030 cdr 2dup cdr 2swap car ( val var )
1033 symbol-type istype? false =
1035 2dup cdr 2swap car ( val formals var' )
1036 -2rot 2swap ( var' formals val )
1037 make-lambda nil cons ( var' val' )
1044 : eval-definition ( obj env -- res )
1045 2dup 2rot ( env env obj )
1046 definition-var-val ( env env var val )
1047 2rot eval ( env var val )
1049 2rot ( var val env )
1055 : assignment? ( obj -- obj bool )
1056 set!-symbol tagged-list? ;
1058 : assignment-var ( obj -- var )
1061 : assignment-val ( obj -- val )
1064 : eval-assignment ( obj env -- res )
1066 2over 2over ( env obj env obj )
1067 assignment-val 2swap ( env obj valexp env )
1068 eval ( env obj val )
1070 2swap assignment-var 2swap ( env var val )
1072 2rot ( var val env )
1078 : macro-definition? ( obj -- obj bool )
1079 define-macro-symbol tagged-list? ;
1081 : macro-definition-name ( exp -- mname )
1084 : macro-definition-params ( exp -- params )
1087 : macro-definition-body ( exp -- body )
1091 : eval-define-macro ( obj env -- res )
1094 2dup macro-definition-name 2swap ( name obj )
1095 2dup macro-definition-params 2swap ( name params obj )
1096 macro-definition-body ( name params body )
1098 env obj@ ( name params body env )
1106 : if? ( obj -- obj bool )
1107 if-symbol tagged-list? ;
1109 : if-predicate ( ifobj -- pred )
1112 : if-consequent ( ifobj -- conseq )
1115 : if-alternative ( ifobj -- alt|false )
1123 : false? ( boolobj -- boolean )
1124 boolean-type istype? if
1125 false boolean-type objeq?
1131 : true? ( boolobj -- bool )
1134 : lambda? ( obj -- obj bool )
1135 lambda-symbol tagged-list? ;
1137 : lambda-parameters ( obj -- params )
1140 : lambda-body ( obj -- body )
1143 : begin? ( obj -- obj bool )
1144 begin-symbol tagged-list? ;
1146 : begin-actions ( obj -- actions )
1149 : eval-sequence ( explist env -- finalexp env )
1150 ( Evaluates all bar the final expressions in
1151 an an expression list. The final expression
1152 is returned to allow for tail optimization. )
1154 2swap ( env explist )
1156 \ Abort on empty list
1163 2dup cdr ( env explist nextexplist )
1166 -2rot car 2over ( nextexplist env exp env )
1168 2drop \ discard result
1169 2swap ( env nextexplist )
1172 2drop car 2swap ( finalexp env )
1175 : application? ( obj -- obj bool )
1178 : operator ( obj -- operator )
1181 : operands ( obj -- operands )
1184 : nooperands? ( operands -- bool )
1187 : first-operand ( operands -- operand )
1190 : rest-operands ( operands -- other-operands )
1193 : list-of-vals ( args env -- vals )
1199 2over 2over first-operand 2swap eval
1200 -2rot rest-operands 2swap recurse
1205 : procedure-params ( proc -- params )
1206 drop pair-type car ;
1208 : procedure-body ( proc -- body )
1209 drop pair-type cdr car ;
1211 : procedure-env ( proc -- body )
1212 drop pair-type cdr cdr car ;
1214 ( Ensure terminating symbol arg name is handled
1215 specially to allow for variadic procedures. )
1216 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1218 2over nil? false = if
1219 bold fg red ." Too many arguments supplied to compound method. Aborting." reset-term cr
1227 symbol-type istype? if
1237 bold fg red ." Too few arguments supplied to compound method. Aborting." reset-term cr
1245 recurse ( argvals argnames argvals'' argnames'' )
1246 2rot car 2swap cons ( argvals argvals'' argnames' )
1247 2rot car 2rot cons ( argnames' argvals' )
1251 : apply ( proc argvals -- result )
1253 primitive-proc-type of
1257 compound-proc-type of
1258 2dup procedure-body ( argvals proc body )
1259 -2rot 2dup procedure-params ( body argvals proc argnames )
1260 -2rot procedure-env ( body argnames argvals procenv )
1266 extend-env ( body env )
1270 R> drop ['] eval goto-deferred \ Tail call optimization
1273 bold fg red ." Object not applicable. Aborting." reset-term cr
1278 : macro-expand ( proc expbody -- result )
1280 2dup procedure-body ( expbody proc procbody )
1281 -2rot 2dup procedure-params ( procbody expbody proc argnames )
1282 -2rot procedure-env ( procbody argnames expbody procenv )
1288 extend-env eval-sequence eval
1291 :noname ( obj env -- result )
1311 2swap eval-definition
1316 2swap eval-assignment
1320 macro-definition? if
1321 2swap eval-define-macro
1337 ['] eval goto-deferred
1341 2dup lambda-parameters
1350 ['] eval goto-deferred
1355 2over 2over ( env exp env exp )
1356 operator ( env exp env opname )
1358 2dup lookup-macro nil? false = if
1359 \ Macro function evaluation
1361 ( env exp env opname mproc )
1362 2swap 2drop -2rot 2drop cdr ( env mproc body )
1367 ['] eval goto-deferred
1369 \ Regular function application
1371 2drop ( env exp env opname )
1373 2swap eval ( env exp proc )
1375 -2rot ( proc env exp )
1376 operands 2swap ( proc operands env )
1377 list-of-vals ( proc argvals )
1384 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1390 \ ---- Print ---- {{{
1392 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1394 : printrealnum ( realnumobj -- ) drop float-print ;
1396 : printbool ( numobj -- )
1404 : printchar ( charobj -- )
1407 9 of ." #\tab" endof
1408 bl of ." #\space" endof
1409 '\n' of ." #\newline" endof
1415 : (printstring) ( stringobj -- )
1416 nil-type istype? if 2drop exit then
1420 '\n' of ." \n" drop endof
1421 [char] \ of ." \\" drop endof
1422 [char] " of [char] \ emit [char] " emit drop endof
1428 : printstring ( stringobj -- )
1433 : printsymbol ( symbolobj -- )
1434 nil-type istype? if 2drop exit then
1440 : printnil ( nilobj -- )
1443 : printpair ( pairobj -- )
1447 nil-type istype? if 2drop exit then
1448 pair-type istype? if space recurse exit then
1452 : printprim ( primobj -- )
1453 2drop ." <primitive procedure>" ;
1455 : printcomp ( primobj -- )
1456 2drop ." <compound procedure>" ;
1458 : printnone ( noneobj -- )
1459 2drop ." Unspecified return value" ;
1462 fixnum-type istype? if printfixnum exit then
1463 realnum-type istype? if printrealnum exit then
1464 boolean-type istype? if printbool exit then
1465 character-type istype? if printchar exit then
1466 string-type istype? if printstring exit then
1467 symbol-type istype? if printsymbol exit then
1468 nil-type istype? if printnil exit then
1469 pair-type istype? if ." (" printpair ." )" exit then
1470 primitive-proc-type istype? if printprim exit then
1471 compound-proc-type istype? if printcomp exit then
1472 none-type istype? if printnone exit then
1474 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1480 \ ---- Garbage Collection ---- {{{
1485 variable gc-stack-depth
1488 depth gc-stack-depth !
1492 false gc-enabled ! ;
1497 : pairlike? ( obj -- obj bool )
1498 pair-type istype? if true exit then
1499 string-type istype? if true exit then
1500 symbol-type istype? if true exit then
1501 compound-proc-type istype? if true exit then
1506 : pairlike-marked? ( obj -- obj bool )
1507 over nextfrees + @ 0=
1510 : mark-pairlike ( obj -- obj )
1511 over nextfrees + 0 swap !
1520 : gc-mark-obj ( obj -- )
1522 pairlike? invert if 2drop exit then
1523 pairlike-marked? if 2drop exit then
1534 scheme-memsize nextfree !
1535 0 scheme-memsize 1- do
1536 nextfrees i + @ 0<> if
1537 nextfree @ nextfrees i + !
1543 \ Following a GC, this gives the amount of free memory
1547 nextfrees i + @ 0= if 1+ then
1551 \ Debugging word - helps spot memory that is retained
1554 nextfrees i + @ 0<> if
1566 symbol-table obj@ gc-mark-obj
1567 macro-table obj@ gc-mark-obj
1568 global-env obj@ gc-mark-obj
1570 depth gc-stack-depth @ do
1579 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1580 ; is collect-garbage
1584 \ ---- Loading files ---- {{{
1586 : charlist>cstr ( charlist addr -- n )
1588 dup 2swap ( origaddr addr charlist )
1594 drop ( origaddr addr charlist char )
1595 -rot 2swap ( origaddr charlist addr char )
1597 1+ -rot ( origaddr nextaddr charlist )
1600 2drop ( origaddr finaladdr )
1604 : load ( addr n -- finalResult )
1609 ok-symbol ( port res )
1612 2over read-port ( port res obj )
1614 2dup EOF character-type objeq? if
1615 2drop 2swap close-port
1619 2swap 2drop ( port obj )
1621 global-env obj@ eval ( port res )
1627 \ ---- Standard Library ---- {{{
1629 include scheme-primitives.4th
1631 s" scheme-library.scm" load 2drop
1638 cr ." Welcome to scheme.forth.jl!" cr
1639 ." Use Ctrl-D to exit." cr
1646 cr bold fg green ." > " reset-term
1649 2dup EOF character-type objeq? if
1651 bold fg blue ." Moriturus te saluto." reset-term cr
1655 global-env obj@ eval
1657 fg cyan ." ; " print reset-term