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 ------ {{{
38 1000 constant scheme-memsize
39 create car-cells scheme-memsize allot
40 create car-type-cells scheme-memsize allot
41 create cdr-cells scheme-memsize allot
42 create cdr-type-cells scheme-memsize allot
44 create nextfrees scheme-memsize allot
55 nextfrees nextfree @ + @
57 dup scheme-memsize < if
61 ." Out of memory! Aborting."
66 : cons ( car-obj cdr-obj -- pair-obj )
67 cdr-type-cells nextfree @ + !
68 cdr-cells nextfree @ + !
69 car-type-cells nextfree @ + !
70 car-cells nextfree @ + !
76 : car ( pair-obj -- car-obj )
78 dup car-cells + @ swap
82 : cdr ( pair-obj -- car-obj )
84 dup cdr-cells + @ swap
88 : set-car! ( obj pair-obj -- )
90 rot swap car-type-cells + !
94 : set-cdr! ( obj pair-obj -- )
96 rot swap cdr-type-cells + !
106 : nil? nil-type istype? ;
108 : objvar create nil swap , , ;
110 : value@ ( objvar -- val ) @ ;
111 : type@ ( objvar -- type ) 1+ @ ;
112 : value! ( newval objvar -- ) ! ;
113 : type! ( newtype objvar -- ) 1+ ! ;
114 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
115 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
117 : objeq? ( obj obj -- bool )
120 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
121 >R >R ( a1 a2 b1 b2 )
122 2swap ( b1 b2 a1 a2 )
123 R> R> ( b1 b2 a1 a2 c1 c2 )
127 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
128 2swap ( a1 a2 c1 c2 b1 b2 )
129 >R >R ( a1 a2 c1 c2 )
130 2swap ( c1 c2 a1 a2 )
136 \ ---- Garbage Collection ---- {{{
150 : pairlike? ( obj -- obj bool )
151 pair-type istype? if true exit then
152 string-type istype? if true exit then
153 symbol-type istype? if true exit then
154 compound-proc-type istype? if true exit then
159 : pairlike-marked? ( obj -- obj bool )
163 : mark-pairlike ( obj -- obj )
164 over nextfrees + 0 swap !
167 : gc-mark-obj ( obj -- )
169 pairlike? invert if 2drop exit then
170 pairlike-marked? if 2drop exit then
181 scheme-memsize nextfree !
182 0 scheme-memsize 1- do
183 nextfrees i + @ 0<> if
184 nextfree @ nextfrees i + !
191 \ ---- Pre-defined symbols ---- {{{
195 : duplicate-charlist ( charlist -- copy )
196 2dup nil objeq? false = if
197 2dup car 2swap cdr recurse cons
200 : charlist-equiv ( charlist charlist -- bool )
209 2drop 2drop true exit
211 2drop 2drop false exit
216 2drop 2drop false exit
223 car drop -rot car drop = if
224 cdr 2swap cdr recurse
230 : charlist>symbol ( charlist -- symbol-obj )
249 drop symbol-type 2dup
250 symbol-table obj@ cons
255 : (create-symbol) ( addr n -- symbol-obj )
259 2dup drop @ character-type 2swap
267 : create-symbol ( -- )
281 does> dup @ swap 1+ @
284 create-symbol quote quote-symbol
285 create-symbol define define-symbol
286 create-symbol set! set!-symbol
287 create-symbol ok ok-symbol
288 create-symbol if if-symbol
289 create-symbol lambda lambda-symbol
290 create-symbol λ λ-symbol
294 \ ---- Environments ---- {{{
296 : enclosing-env ( env -- env )
299 : first-frame ( env -- frame )
302 : make-frame ( vars vals -- frame )
305 : frame-vars ( frame -- vars )
308 : frame-vals ( frame -- vals )
311 : add-binding ( var val frame -- )
312 2swap 2over frame-vals cons
314 2swap 2over frame-vars cons
318 : extend-env ( vars vals env -- env )
328 : get-vars-vals-frame ( var frame -- bool )
329 2dup frame-vars vars obj!
333 vars obj@ nil objeq? false =
335 2dup vars obj@ car objeq? if
340 vars obj@ cdr vars obj!
341 vals obj@ cdr vals obj!
347 : get-vars-vals ( var env -- vars? vals? bool )
350 2dup nil objeq? false =
352 2over 2over first-frame
353 get-vars-vals-frame if
355 vars obj@ vals obj@ true
369 : lookup-var ( var env -- val )
373 bold fg red ." Tried to read unbound variable." reset-term cr abort
377 : set-var ( var val env -- )
378 >R >R 2swap R> R> ( val var env )
380 2swap 2drop ( val vals )
383 bold fg red ." Tried to set unbound variable." reset-term cr abort
389 : define-var ( var val env -- )
392 2over env obj@ ( var val var env )
394 2swap 2drop ( var val vals )
399 first-frame ( var val frame )
407 nil nil nil extend-env
412 \ ---- Primitives ---- {{{
414 : make-primitive ( cfa -- )
418 \ 2dup ." Defining primitive " type ." ..." cr
429 rot primitive-proc-type ( var prim )
430 global-env obj@ define-var
434 bold fg red ." Incorrect argument count." reset-term cr
438 : ensure-arg-count ( args n -- )
440 drop nil objeq? false = if
444 -rot 2dup nil objeq? if
453 bold fg red ." Incorrect argument type." reset-term cr
457 : ensure-arg-type ( arg type -- arg )
463 include scheme-primitives.4th
470 variable stored-parse-idx
471 create parse-str 161 allot
472 variable parse-str-span
474 create parse-idx-stack 10 allot
475 variable parse-idx-sp
476 parse-idx-stack parse-idx-sp !
479 parse-idx @ parse-idx-sp @ !
484 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
488 parse-idx-sp @ @ parse-idx ! ;
492 '\n' parse-str parse-str-span @ + !
493 1 parse-str-span +! ;
500 parse-str 160 expect cr
501 span @ parse-str-span !
511 : charavailable? ( -- bool )
512 parse-str-span @ parse-idx @ > ;
514 : nextchar ( -- char )
515 charavailable? false = if getline then
516 parse-str parse-idx @ + @ ;
518 : whitespace? ( -- bool )
527 nextchar [char] ( = or
528 nextchar [char] ) = or
531 : commentstart? ( -- bool )
532 nextchar [char] ; = ;
536 false \ Indicates whether or not we're eating a comment
539 dup whitespace? or commentstart? or
541 dup nextchar '\n' = and if
542 invert \ Stop eating comment
544 dup false = commentstart? and if
545 invert \ Begin eating comment
560 nextchar [char] - = ;
563 nextchar [char] + = ;
565 : fixnum? ( -- bool )
591 : realnum? ( -- bool )
598 \ Record starting parse idx:
599 \ Want to detect whether any characters (following +/-) were eaten.
606 [char] . nextchar = if
613 [char] e nextchar = [char] E nextchar = or if
621 drop pop-parse-idx false exit
629 \ This is a real number if characters were
630 \ eaten and the next characer is a delimiter.
631 parse-idx @ < delim? and
636 : boolean? ( -- bool )
637 nextchar [char] # <> if false exit then
644 and if pop-parse-idx false exit then
656 : str-equiv? ( str -- bool )
673 delim? false = if drop false then
678 : character? ( -- bool )
679 nextchar [char] # <> if false exit then
684 nextchar [char] \ <> if pop-parse-idx false exit then
688 S" newline" str-equiv? if pop-parse-idx true exit then
689 S" space" str-equiv? if pop-parse-idx true exit then
690 S" tab" str-equiv? if pop-parse-idx true exit then
692 charavailable? false = if pop-parse-idx false exit then
698 nextchar [char] ( = ;
700 : string? ( -- bool )
701 nextchar [char] " = ;
703 : readfixnum ( -- num-atom )
714 10 * nextchar [char] 0 - +
723 : readrealnum ( -- realnum )
725 \ Remember that at this point we're guaranteed to
726 \ have a parsable real on this line.
728 parse-str parse-idx @ +
730 begin delim? false = while
734 parse-str parse-idx @ + over -
741 : readbool ( -- bool-atom )
744 nextchar [char] f = if
755 : readchar ( -- char-atom )
759 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
760 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
761 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
763 nextchar character-type
768 : readstring ( -- charlist )
769 nextchar [char] " = if
774 ." No delimiter following right double quote. Aborting." cr
783 nextchar [char] \ = if
786 [char] n of '\n' endof
787 [char] " of [char] " endof
793 inc-parse-idx character-type
800 : readsymbol ( -- charlist )
801 delim? if nil exit then
803 nextchar inc-parse-idx character-type
810 : readpair ( -- pairobj )
814 nextchar [char] ) = if
819 ." No delimiter following right paren. Aborting." cr
828 \ Read first pair element
833 nextchar [char] . = if
838 ." No delimiter following '.'. Aborting." cr
852 \ Parse a scheme expression
883 nextchar [char] " <> if
884 bold red ." Missing closing double-quote." reset-term cr
902 nextchar [char] ) <> if
903 bold red ." Missing closing paren." reset-term cr
912 nextchar [char] ' = if
914 quote-symbol recurse nil cons cons exit
919 bold fg blue ." Moriturus te saluto." reset-term cr
923 \ Anything else is parsed as a symbol
924 readsymbol charlist>symbol
926 \ Replace λ with lambda
927 2dup λ-symbol objeq? if
938 : self-evaluating? ( obj -- obj bool )
939 boolean-type istype? if true exit then
940 fixnum-type istype? if true exit then
941 realnum-type istype? if true exit then
942 character-type istype? if true exit then
943 string-type istype? if true exit then
944 nil-type istype? if true exit then
949 : tagged-list? ( obj tag-obj -- obj bool )
951 pair-type istype? false = if
957 : quote? ( obj -- obj bool )
958 quote-symbol tagged-list? ;
960 : quote-body ( quote-obj -- quote-body-obj )
963 : variable? ( obj -- obj bool )
964 symbol-type istype? ;
966 : definition? ( obj -- obj bool )
967 define-symbol tagged-list? ;
969 : make-lambda ( params body -- lambda-exp )
970 lambda-symbol -2rot cons cons ;
972 : definition-var ( obj -- var )
974 symbol-type istype? false = if car then
977 : definition-val ( obj -- val )
978 2dup cdr car symbol-type istype? if
987 : assignment? ( obj -- obj bool )
988 set!-symbol tagged-list? ;
990 : assignment-var ( obj -- var )
993 : assignment-val ( obj -- val )
996 : eval-definition ( obj env -- res )
998 2over 2over ( env obj env obj )
999 definition-val 2swap ( env obj valexp env )
1000 eval ( env obj val )
1002 2swap definition-var 2swap ( env var val )
1004 2rot ( var val env )
1010 : eval-assignment ( obj env -- res )
1012 2over 2over ( env obj env obj )
1013 assignment-val 2swap ( env obj valexp env )
1014 eval ( env obj val )
1016 2swap assignment-var 2swap ( env var val )
1018 2rot ( var val env )
1024 : if? ( obj -- obj bool )
1025 if-symbol tagged-list? ;
1027 : if-predicate ( ifobj -- pred )
1030 : if-consequent ( ifobj -- conseq )
1033 : if-alternative ( ifobj -- alt|false )
1041 : false? ( boolobj -- boolean )
1042 boolean-type istype? if
1043 false boolean-type objeq?
1049 : true? ( boolobj -- bool )
1052 : lambda? ( obj -- obj bool )
1053 lambda-symbol tagged-list? ;
1055 : lambda-parameters ( obj -- params )
1058 : lambda-body ( obj -- body )
1061 : make-procedure ( params body env -- proc )
1064 drop compound-proc-type
1067 : application? ( obj -- obj bool)
1070 : operator ( obj -- operator )
1073 : operands ( obj -- operands )
1076 : nooperands? ( operands -- bool )
1079 : first-operand ( operands -- operand )
1082 : rest-operands ( operands -- other-operands )
1085 : list-of-vals ( args env -- vals )
1091 2over 2over first-operand 2swap eval
1092 -2rot rest-operands 2swap recurse
1097 : procedure-params ( proc -- params )
1098 drop pair-type car ;
1100 : procedure-body ( proc -- body )
1101 drop pair-type cdr car ;
1103 : procedure-env ( proc -- body )
1104 drop pair-type cdr cdr car ;
1106 : apply ( proc args )
1108 primitive-proc-type of
1112 compound-proc-type of
1113 2dup procedure-body ( args proc body )
1114 -2rot 2dup procedure-params ( body args proc params )
1115 -2rot procedure-env ( body params args procenv )
1117 extend-env ( body env )
1122 2dup cdr 2dup nil objeq? false =
1124 -2rot car 2over ( nextbody env exp env )
1126 2drop \ discard result
1127 2swap ( env nextbody )
1131 car 2swap ( exp env )
1133 R> drop ['] eval goto-deferred \ Tail call optimization
1136 bold fg red ." Object not applicable. Aboring." reset-term cr
1141 :noname ( obj env -- result )
1161 2swap eval-definition
1166 2swap eval-assignment
1182 ['] eval goto-deferred
1186 2dup lambda-parameters
1196 operands 2swap list-of-vals
1202 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1208 \ ---- Print ---- {{{
1210 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1212 : printrealnum ( realnumobj -- ) drop float-print ;
1214 : printbool ( numobj -- )
1222 : printchar ( charobj -- )
1225 9 of ." #\tab" endof
1226 bl of ." #\space" endof
1227 '\n' of ." #\newline" endof
1233 : (printstring) ( stringobj -- )
1234 nil-type istype? if 2drop exit then
1238 '\n' of ." \n" drop endof
1239 [char] \ of ." \\" drop endof
1240 [char] " of [char] \ emit [char] " emit drop endof
1246 : printstring ( stringobj -- )
1251 : printsymbol ( symbolobj -- )
1252 nil-type istype? if 2drop exit then
1258 : printnil ( nilobj -- )
1261 : printpair ( pairobj -- )
1265 nil-type istype? if 2drop exit then
1266 pair-type istype? if space recurse exit then
1270 : printprim ( primobj -- )
1271 2drop ." <primitive procedure>" ;
1273 : printcomp ( primobj -- )
1274 2drop ." <compound procedure>" ;
1277 fixnum-type istype? if printfixnum exit then
1278 realnum-type istype? if printrealnum exit then
1279 boolean-type istype? if printbool exit then
1280 character-type istype? if printchar exit then
1281 string-type istype? if printstring exit then
1282 symbol-type istype? if printsymbol exit then
1283 nil-type istype? if printnil exit then
1284 pair-type istype? if ." (" printpair ." )" exit then
1285 primitive-proc-type istype? if printprim exit then
1286 compound-proc-type istype? if printcomp exit then
1288 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1297 cr ." Welcome to scheme.forth.jl!" cr
1298 ." Use Ctrl-D to exit." cr
1304 cr bold fg green ." > " reset-term
1307 global-env obj@ eval
1309 fg cyan ." ; " print reset-term