4 include term-colours.4th
24 make-type realnum-type
25 make-type boolean-type
26 make-type character-type
31 make-type primitive-proc-type
32 make-type compound-proc-type
33 : istype? ( obj type -- obj bool )
36 \ ------ Cons cell memory ------ {{{
39 create car-cells N allot
40 create car-type-cells N allot
41 create cdr-cells N allot
42 create cdr-type-cells N allot
44 create nextfrees N allot
55 nextfrees nextfree @ + @
58 : cons ( car-obj cdr-obj -- pair-obj )
59 cdr-type-cells nextfree @ + !
60 cdr-cells nextfree @ + !
61 car-type-cells nextfree @ + !
62 car-cells nextfree @ + !
68 : car ( pair-obj -- car-obj )
70 dup car-cells + @ swap
74 : cdr ( pair-obj -- car-obj )
76 dup cdr-cells + @ swap
80 : set-car! ( obj pair-obj -- )
82 rot swap car-type-cells + !
86 : set-cdr! ( obj pair-obj -- )
88 rot swap cdr-type-cells + !
98 : nil? nil-type istype? ;
100 : objvar create nil swap , , ;
102 : value@ ( objvar -- val ) @ ;
103 : type@ ( objvar -- type ) 1+ @ ;
104 : value! ( newval objvar -- ) ! ;
105 : type! ( newtype objvar -- ) 1+ ! ;
106 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
107 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
109 : objeq? ( obj obj -- bool )
112 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
113 >R >R ( a1 a2 b1 b2 )
114 2swap ( b1 b2 a1 a2 )
115 R> R> ( b1 b2 a1 a2 c1 c2 )
119 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
120 2swap ( a1 a2 c1 c2 b1 b2 )
121 >R >R ( a1 a2 c1 c2 )
122 2swap ( c1 c2 a1 a2 )
128 \ ---- Garbage Collection ---- {{{
142 : pairlike? ( obj -- obj bool )
143 pair-type istype? if true exit then
144 string-type istype? if true exit then
145 symbol-type istype? if true exit then
150 : pairlike-marked? ( obj -- obj bool )
154 : mark-pairlike ( obj -- obj )
155 over nextfrees + 0 swap !
158 : gc-mark-obj ( obj -- )
161 pairlike-marked? if 2drop exit then
177 nextfrees i + @ 0<> if
178 nextfree @ nextfrees i + !
185 \ ---- Pre-defined symbols ---- {{{
189 : duplicate-charlist ( charlist -- copy )
190 2dup nil objeq? false = if
191 2dup car 2swap cdr recurse cons
194 : charlist-equiv ( charlist charlist -- bool )
203 2drop 2drop true exit
205 2drop 2drop false exit
210 2drop 2drop false exit
217 car drop -rot car drop = if
218 cdr 2swap cdr recurse
224 : charlist>symbol ( charlist -- symbol-obj )
243 drop symbol-type 2dup
244 symbol-table obj@ cons
249 : (create-symbol) ( addr n -- symbol-obj )
253 2dup drop @ character-type 2swap
261 : create-symbol ( -- )
275 does> dup @ swap 1+ @
278 create-symbol quote quote-symbol
279 create-symbol define define-symbol
280 create-symbol set! set!-symbol
281 create-symbol ok ok-symbol
282 create-symbol if if-symbol
283 create-symbol lambda lambda-symbol
284 create-symbol λ λ-symbol
288 \ ---- Environments ---- {{{
290 : enclosing-env ( env -- env )
293 : first-frame ( env -- frame )
296 : make-frame ( vars vals -- frame )
299 : frame-vars ( frame -- vars )
302 : frame-vals ( frame -- vals )
305 : add-binding ( var val frame -- )
306 2swap 2over frame-vals cons
308 2swap 2over frame-vars cons
312 : extend-env ( vars vals env -- env )
322 : get-vars-vals-frame ( var frame -- bool )
323 2dup frame-vars vars obj!
327 vars obj@ nil objeq? false =
329 2dup vars obj@ car objeq? if
334 vars obj@ cdr vars obj!
335 vals obj@ cdr vals obj!
341 : get-vars-vals ( var env -- vars? vals? bool )
344 2dup nil objeq? false =
346 2over 2over first-frame
347 get-vars-vals-frame if
349 vars obj@ vals obj@ true
363 : lookup-var ( var env -- val )
367 bold fg red ." Tried to read unbound variable." reset-term cr abort
371 : set-var ( var val env -- )
372 >R >R 2swap R> R> ( val var env )
374 2swap 2drop ( val vals )
377 bold fg red ." Tried to set unbound variable." reset-term cr abort
383 : define-var ( var val env -- )
386 2over env obj@ ( var val var env )
388 2swap 2drop ( var val vals )
393 first-frame ( var val frame )
401 nil nil nil extend-env
406 \ ---- Primitives ---- {{{
408 : make-primitive ( cfa -- )
412 \ 2dup ." Defining primitive " type ." ..." cr
423 rot primitive-proc-type ( var prim )
424 global-env obj@ define-var
428 bold fg red ." Incorrect argument count." reset-term cr
432 : ensure-arg-count ( args n -- )
434 drop nil objeq? false = if
438 -rot 2dup nil objeq? if
447 bold fg red ." Incorrect argument type." reset-term cr
451 : ensure-arg-type ( arg type -- arg )
457 include scheme-primitives.4th
464 variable stored-parse-idx
465 create parse-str 161 allot
466 variable parse-str-span
468 create parse-idx-stack 10 allot
469 variable parse-idx-sp
470 parse-idx-stack parse-idx-sp !
473 parse-idx @ parse-idx-sp @ !
478 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
482 parse-idx-sp @ @ parse-idx ! ;
486 '\n' parse-str parse-str-span @ + !
487 1 parse-str-span +! ;
494 parse-str 160 expect cr
495 span @ parse-str-span !
505 : charavailable? ( -- bool )
506 parse-str-span @ parse-idx @ > ;
508 : nextchar ( -- char )
509 charavailable? false = if getline then
510 parse-str parse-idx @ + @ ;
512 : whitespace? ( -- bool )
521 nextchar [char] ( = or
522 nextchar [char] ) = or
525 : commentstart? ( -- bool )
526 nextchar [char] ; = ;
530 false \ Indicates whether or not we're eating a comment
533 dup whitespace? or commentstart? or
535 dup nextchar '\n' = and if
536 invert \ Stop eating comment
538 dup false = commentstart? and if
539 invert \ Begin eating comment
554 nextchar [char] - = ;
557 nextchar [char] + = ;
559 : fixnum? ( -- bool )
585 : realnum? ( -- bool )
592 \ Record starting parse idx:
593 \ Want to detect whether any characters (following +/-) were eaten.
600 [char] . nextchar = if
607 [char] e nextchar = [char] E nextchar = or if
615 drop pop-parse-idx false exit
623 \ This is a real number if characters were
624 \ eaten and the next characer is a delimiter.
625 parse-idx @ < delim? and
630 : boolean? ( -- bool )
631 nextchar [char] # <> if false exit then
638 and if pop-parse-idx false exit then
650 : str-equiv? ( str -- bool )
667 delim? false = if drop false then
672 : character? ( -- bool )
673 nextchar [char] # <> if false exit then
678 nextchar [char] \ <> if pop-parse-idx false exit then
682 S" newline" str-equiv? if pop-parse-idx true exit then
683 S" space" str-equiv? if pop-parse-idx true exit then
684 S" tab" str-equiv? if pop-parse-idx true exit then
686 charavailable? false = if pop-parse-idx false exit then
692 nextchar [char] ( = ;
694 : string? ( -- bool )
695 nextchar [char] " = ;
697 : readfixnum ( -- num-atom )
708 10 * nextchar [char] 0 - +
717 : readrealnum ( -- realnum )
719 \ Remember that at this point we're guaranteed to
720 \ have a parsable real on this line.
722 parse-str parse-idx @ +
724 begin delim? false = while
728 parse-str parse-idx @ + over -
735 : readbool ( -- bool-atom )
738 nextchar [char] f = if
749 : readchar ( -- char-atom )
753 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
754 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
755 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
757 nextchar character-type
762 : readstring ( -- charlist )
763 nextchar [char] " = if
768 ." No delimiter following right double quote. Aborting." cr
777 nextchar [char] \ = if
780 [char] n of '\n' endof
781 [char] " of [char] " endof
787 inc-parse-idx character-type
794 : readsymbol ( -- charlist )
795 delim? if nil exit then
797 nextchar inc-parse-idx character-type
804 : readpair ( -- pairobj )
808 nextchar [char] ) = if
813 ." No delimiter following right paren. Aborting." cr
822 \ Read first pair element
827 nextchar [char] . = if
832 ." No delimiter following '.'. Aborting." cr
846 \ Parse a scheme expression
877 nextchar [char] " <> if
878 bold red ." Missing closing double-quote." reset-term cr
896 nextchar [char] ) <> if
897 bold red ." Missing closing paren." reset-term cr
906 nextchar [char] ' = if
908 quote-symbol recurse nil cons cons exit
913 bold fg blue ." Moriturus te saluto." reset-term cr
917 \ Anything else is parsed as a symbol
918 readsymbol charlist>symbol
920 \ Replace λ with lambda
921 2dup λ-symbol objeq? if
932 : self-evaluating? ( obj -- obj bool )
933 boolean-type istype? if true exit then
934 fixnum-type istype? if true exit then
935 realnum-type istype? if true exit then
936 character-type istype? if true exit then
937 string-type istype? if true exit then
938 nil-type istype? if true exit then
943 : tagged-list? ( obj tag-obj -- obj bool )
945 pair-type istype? false = if
951 : quote? ( obj -- obj bool )
952 quote-symbol tagged-list? ;
954 : quote-body ( quote-obj -- quote-body-obj )
957 : variable? ( obj -- obj bool )
958 symbol-type istype? ;
960 : definition? ( obj -- obj bool )
961 define-symbol tagged-list? ;
963 : make-lambda ( params body -- lambda-exp )
964 lambda-symbol -2rot cons cons ;
966 : definition-var ( obj -- var )
968 symbol-type istype? false = if car then
971 : definition-val ( obj -- val )
972 2dup cdr car symbol-type istype? if
981 : assignment? ( obj -- obj bool )
982 set!-symbol tagged-list? ;
984 : assignment-var ( obj -- var )
987 : assignment-val ( obj -- val )
990 : eval-definition ( obj env -- res )
992 2over 2over ( env obj env obj )
993 definition-val 2swap ( env obj valexp env )
996 2swap definition-var 2swap ( env var val )
1004 : eval-assignment ( obj env -- res )
1006 2over 2over ( env obj env obj )
1007 assignment-val 2swap ( env obj valexp env )
1008 eval ( env obj val )
1010 2swap assignment-var 2swap ( env var val )
1012 2rot ( var val env )
1018 : if? ( obj -- obj bool )
1019 if-symbol tagged-list? ;
1021 : if-predicate ( ifobj -- pred )
1024 : if-consequent ( ifobj -- conseq )
1027 : if-alternative ( ifobj -- alt|false )
1035 : false? ( boolobj -- boolean )
1036 boolean-type istype? if
1037 false boolean-type objeq?
1043 : true? ( boolobj -- bool )
1046 : lambda? ( obj -- obj bool )
1047 lambda-symbol tagged-list? ;
1049 : lambda-parameters ( obj -- params )
1052 : lambda-body ( obj -- body )
1055 : make-procedure ( params body env -- proc )
1058 drop compound-proc-type
1061 : application? ( obj -- obj bool)
1064 : operator ( obj -- operator )
1067 : operands ( obj -- operands )
1070 : nooperands? ( operands -- bool )
1073 : first-operand ( operands -- operand )
1076 : rest-operands ( operands -- other-operands )
1079 : list-of-vals ( args env -- vals )
1085 2over 2over first-operand 2swap eval
1086 -2rot rest-operands 2swap recurse
1091 : procedure-params ( proc -- params )
1092 drop pair-type car ;
1094 : procedure-body ( proc -- body )
1095 drop pair-type cdr car ;
1097 : procedure-env ( proc -- body )
1098 drop pair-type cdr cdr car ;
1100 : apply ( proc args )
1102 primitive-proc-type of
1106 compound-proc-type of
1107 2dup procedure-body ( args proc body )
1108 -2rot 2dup procedure-params ( body args proc params )
1109 -2rot procedure-env ( body params args procenv )
1111 extend-env ( body env )
1116 2dup cdr 2dup nil objeq? false =
1118 -2rot car 2over ( nextbody env exp env )
1120 2drop \ discard result
1121 2swap ( env nextbody )
1125 car 2swap ( exp env )
1127 R> drop ['] eval goto-deferred \ Tail call optimization
1130 bold fg red ." Object not applicable. Aboring." reset-term cr
1135 :noname ( obj env -- result )
1155 2swap eval-definition
1160 2swap eval-assignment
1176 ['] eval goto-deferred
1180 2dup lambda-parameters
1190 operands 2swap list-of-vals
1196 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1202 \ ---- Print ---- {{{
1204 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1206 : printrealnum ( realnumobj -- ) drop float-print ;
1208 : printbool ( numobj -- )
1216 : printchar ( charobj -- )
1219 9 of ." #\tab" endof
1220 bl of ." #\space" endof
1221 '\n' of ." #\newline" endof
1227 : (printstring) ( stringobj -- )
1228 nil-type istype? if 2drop exit then
1232 '\n' of ." \n" drop endof
1233 [char] \ of ." \\" drop endof
1234 [char] " of [char] \ emit [char] " emit drop endof
1240 : printstring ( stringobj -- )
1245 : printsymbol ( symbolobj -- )
1246 nil-type istype? if 2drop exit then
1252 : printnil ( nilobj -- )
1255 : printpair ( pairobj -- )
1259 nil-type istype? if 2drop exit then
1260 pair-type istype? if space recurse exit then
1264 : printprim ( primobj -- )
1265 2drop ." <primitive procedure>" ;
1267 : printcomp ( primobj -- )
1268 2drop ." <compound procedure>" ;
1271 fixnum-type istype? if printfixnum exit then
1272 realnum-type istype? if printrealnum exit then
1273 boolean-type istype? if printbool exit then
1274 character-type istype? if printchar exit then
1275 string-type istype? if printstring exit then
1276 symbol-type istype? if printsymbol exit then
1277 nil-type istype? if printnil exit then
1278 pair-type istype? if ." (" printpair ." )" exit then
1279 primitive-proc-type istype? if printprim exit then
1280 compound-proc-type istype? if printcomp exit then
1282 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1291 cr ." Welcome to scheme.forth.jl!" cr
1292 ." Use Ctrl-D to exit." cr
1298 cr bold fg green ." > " reset-term
1301 global-env obj@ eval
1303 fg cyan ." ; " print reset-term