4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
33 make-type primitive-proc-type
34 make-type compound-proc-type
35 : istype? ( obj type -- obj bool )
38 \ ------ Cons cell memory ------ {{{
41 create car-cells N allot
42 create car-type-cells N allot
43 create cdr-cells N allot
44 create cdr-type-cells N allot
49 : cons ( car-obj cdr-obj -- pair-obj )
50 cdr-type-cells nextfree @ + !
51 cdr-cells nextfree @ + !
52 car-type-cells nextfree @ + !
53 car-cells nextfree @ + !
62 : car ( pair-obj -- car-obj )
64 dup car-cells + @ swap
68 : cdr ( pair-obj -- car-obj )
70 dup cdr-cells + @ swap
74 : set-car! ( obj pair-obj -- )
76 rot swap car-type-cells + !
80 : set-cdr! ( obj pair-obj -- )
82 rot swap cdr-type-cells + !
92 : nil? nil-type istype? ;
94 : objvar create nil swap , , ;
96 : value@ ( objvar -- val ) @ ;
97 : type@ ( objvar -- type ) 1+ @ ;
98 : value! ( newval objvar -- ) ! ;
99 : type! ( newtype objvar -- ) 1+ ! ;
100 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
101 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
103 : objeq? ( obj obj -- bool )
106 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
107 >R >R ( a1 a2 b1 b2 )
108 2swap ( b1 b2 a1 a2 )
109 R> R> ( b1 b2 a1 a2 c1 c2 )
113 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
114 2swap ( a1 a2 c1 c2 b1 b2 )
115 >R >R ( a1 a2 c1 c2 )
116 2swap ( c1 c2 a1 a2 )
122 \ ---- Garbage Collection ---- {{{
144 \ ---- Pre-defined symbols ---- {{{
148 : duplicate-charlist ( charlist -- copy )
149 2dup nil objeq? false = if
150 2dup car 2swap cdr recurse cons
153 : charlist-equiv ( charlist charlist -- bool )
162 2drop 2drop true exit
164 2drop 2drop false exit
169 2drop 2drop false exit
176 car drop -rot car drop = if
177 cdr 2swap cdr recurse
183 : charlist>symbol ( charlist -- symbol-obj )
202 drop symbol-type 2dup
203 symbol-table obj@ cons
208 : (create-symbol) ( addr n -- symbol-obj )
212 2dup drop @ character-type 2swap
220 : create-symbol ( -- )
234 does> dup @ swap 1+ @
237 create-symbol quote quote-symbol
238 create-symbol define define-symbol
239 create-symbol set! set!-symbol
240 create-symbol ok ok-symbol
241 create-symbol if if-symbol
242 create-symbol lambda lambda-symbol
243 create-symbol λ λ-symbol
247 \ ---- Environments ---- {{{
249 : enclosing-env ( env -- env )
252 : first-frame ( env -- frame )
255 : make-frame ( vars vals -- frame )
258 : frame-vars ( frame -- vars )
261 : frame-vals ( frame -- vals )
264 : add-binding ( var val frame -- )
265 2swap 2over frame-vals cons
267 2swap 2over frame-vars cons
271 : extend-env ( vars vals env -- env )
281 : get-vars-vals-frame ( var frame -- bool )
282 2dup frame-vars vars obj!
286 vars obj@ nil objeq? false =
288 2dup vars obj@ car objeq? if
293 vars obj@ cdr vars obj!
294 vals obj@ cdr vals obj!
300 : get-vars-vals ( var env -- vars? vals? bool )
303 2dup nil objeq? false =
305 2over 2over first-frame
306 get-vars-vals-frame if
308 vars obj@ vals obj@ true
322 : lookup-var ( var env -- val )
326 bold fg red ." Tried to read unbound variable." reset-term cr abort
330 : set-var ( var val env -- )
331 >R >R 2swap R> R> ( val var env )
333 2swap 2drop ( val vals )
336 bold fg red ." Tried to set unbound variable." reset-term cr abort
342 : define-var ( var val env -- )
345 2over env obj@ ( var val var env )
347 2swap 2drop ( var val vals )
352 first-frame ( var val frame )
360 nil nil nil extend-env
365 \ ---- Primitives ---- {{{
367 : make-primitive ( cfa -- )
371 \ 2dup ." Defining primitive " type ." ..." cr
382 rot primitive-proc-type ( var prim )
383 global-env obj@ define-var
387 bold fg red ." Incorrect argument count." reset-term cr
391 : ensure-arg-count ( args n -- )
393 drop nil objeq? false = if
397 -rot 2dup nil objeq? if
406 bold fg red ." Incorrect argument type." reset-term cr
410 : ensure-arg-type ( arg type -- arg )
416 include scheme-primitives.4th
423 variable stored-parse-idx
424 create parse-str 161 allot
425 variable parse-str-span
427 create parse-idx-stack 10 allot
428 variable parse-idx-sp
429 parse-idx-stack parse-idx-sp !
432 parse-idx @ parse-idx-sp @ !
437 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
441 parse-idx-sp @ @ parse-idx ! ;
445 '\n' parse-str parse-str-span @ + !
446 1 parse-str-span +! ;
453 parse-str 160 expect cr
454 span @ parse-str-span !
464 : charavailable? ( -- bool )
465 parse-str-span @ parse-idx @ > ;
467 : nextchar ( -- char )
468 charavailable? false = if getline then
469 parse-str parse-idx @ + @ ;
471 : whitespace? ( -- bool )
480 nextchar [char] ( = or
481 nextchar [char] ) = or
484 : commentstart? ( -- bool )
485 nextchar [char] ; = ;
489 false \ Indicates whether or not we're eating a comment
492 dup whitespace? or commentstart? or
494 dup nextchar '\n' = and if
495 invert \ Stop eating comment
497 dup false = commentstart? and if
498 invert \ Begin eating comment
513 nextchar [char] - = ;
516 nextchar [char] + = ;
518 : fixnum? ( -- bool )
544 : realnum? ( -- bool )
551 \ Record starting parse idx:
552 \ Want to detect whether any characters (following +/-) were eaten.
559 [char] . nextchar = if
566 [char] e nextchar = [char] E nextchar = or if
574 drop pop-parse-idx false exit
582 \ This is a real number if characters were
583 \ eaten and the next characer is a delimiter.
584 parse-idx @ < delim? and
589 : boolean? ( -- bool )
590 nextchar [char] # <> if false exit then
597 and if pop-parse-idx false exit then
609 : str-equiv? ( str -- bool )
626 delim? false = if drop false then
631 : character? ( -- bool )
632 nextchar [char] # <> if false exit then
637 nextchar [char] \ <> if pop-parse-idx false exit then
641 S" newline" str-equiv? if pop-parse-idx true exit then
642 S" space" str-equiv? if pop-parse-idx true exit then
643 S" tab" str-equiv? if pop-parse-idx true exit then
645 charavailable? false = if pop-parse-idx false exit then
651 nextchar [char] ( = ;
653 : string? ( -- bool )
654 nextchar [char] " = ;
656 : readfixnum ( -- num-atom )
667 10 * nextchar [char] 0 - +
676 : readrealnum ( -- realnum )
678 \ Remember that at this point we're guaranteed to
679 \ have a parsable real on this line.
681 parse-str parse-idx @ +
683 begin delim? false = while
687 parse-str parse-idx @ + over -
694 : readbool ( -- bool-atom )
697 nextchar [char] f = if
708 : readchar ( -- char-atom )
712 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
713 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
714 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
716 nextchar character-type
721 : readstring ( -- charlist )
722 nextchar [char] " = if
727 ." No delimiter following right double quote. Aborting." cr
736 nextchar [char] \ = if
739 [char] n of '\n' endof
740 [char] " of [char] " endof
746 inc-parse-idx character-type
753 : readsymbol ( -- charlist )
754 delim? if nil exit then
756 nextchar inc-parse-idx character-type
763 : readpair ( -- pairobj )
767 nextchar [char] ) = if
772 ." No delimiter following right paren. Aborting." cr
781 \ Read first pair element
786 nextchar [char] . = if
791 ." No delimiter following '.'. Aborting." cr
805 \ Parse a scheme expression
836 nextchar [char] " <> if
837 bold red ." Missing closing double-quote." reset-term cr
855 nextchar [char] ) <> if
856 bold red ." Missing closing paren." reset-term cr
865 nextchar [char] ' = if
867 quote-symbol recurse nil cons cons exit
872 bold fg blue ." Moriturus te saluto." reset-term cr
876 \ Anything else is parsed as a symbol
877 readsymbol charlist>symbol
879 \ Replace λ with lambda
880 2dup λ-symbol objeq? if
891 : self-evaluating? ( obj -- obj bool )
892 boolean-type istype? if true exit then
893 fixnum-type istype? if true exit then
894 realnum-type istype? if true exit then
895 character-type istype? if true exit then
896 string-type istype? if true exit then
897 nil-type istype? if true exit then
902 : tagged-list? ( obj tag-obj -- obj bool )
904 pair-type istype? false = if
910 : quote? ( obj -- obj bool )
911 quote-symbol tagged-list? ;
913 : quote-body ( quote-obj -- quote-body-obj )
916 : variable? ( obj -- obj bool )
917 symbol-type istype? ;
919 : definition? ( obj -- obj bool )
920 define-symbol tagged-list? ;
922 : make-lambda ( params body -- lambda-exp )
923 lambda-symbol -2rot cons cons ;
925 : definition-var ( obj -- var )
927 symbol-type istype? false = if car then
930 : definition-val ( obj -- val )
931 2dup cdr car symbol-type istype? if
940 : assignment? ( obj -- obj bool )
941 set!-symbol tagged-list? ;
943 : assignment-var ( obj -- var )
946 : assignment-val ( obj -- val )
949 : eval-definition ( obj env -- res )
951 2over 2over ( env obj env obj )
952 definition-val 2swap ( env obj valexp env )
955 2swap definition-var 2swap ( env var val )
963 : eval-assignment ( obj env -- res )
965 2over 2over ( env obj env obj )
966 assignment-val 2swap ( env obj valexp env )
969 2swap assignment-var 2swap ( env var val )
977 : if? ( obj -- obj bool )
978 if-symbol tagged-list? ;
980 : if-predicate ( ifobj -- pred )
983 : if-consequent ( ifobj -- conseq )
986 : if-alternative ( ifobj -- alt|false )
994 : false? ( boolobj -- boolean )
995 boolean-type istype? if
996 false boolean-type objeq?
1002 : true? ( boolobj -- bool )
1005 : lambda? ( obj -- obj bool )
1006 lambda-symbol tagged-list? ;
1008 : lambda-parameters ( obj -- params )
1011 : lambda-body ( obj -- body )
1014 : make-procedure ( params body env -- proc )
1017 drop compound-proc-type
1020 : application? ( obj -- obj bool)
1023 : operator ( obj -- operator )
1026 : operands ( obj -- operands )
1029 : nooperands? ( operands -- bool )
1032 : first-operand ( operands -- operand )
1035 : rest-operands ( operands -- other-operands )
1038 : list-of-vals ( args env -- vals )
1044 2over 2over first-operand 2swap eval
1045 -2rot rest-operands 2swap recurse
1050 : procedure-params ( proc -- params )
1051 drop pair-type car ;
1053 : procedure-body ( proc -- body )
1054 drop pair-type cdr car ;
1056 : procedure-env ( proc -- body )
1057 drop pair-type cdr cdr car ;
1059 : apply ( proc args )
1061 primitive-proc-type of
1065 compound-proc-type of
1066 2dup procedure-body ( args proc body )
1067 -2rot 2dup procedure-params ( body args proc params )
1068 -2rot procedure-env ( body params args procenv )
1070 extend-env ( body env )
1075 2dup cdr 2dup nil objeq? false =
1077 -2rot car 2over ( nextbody env exp env )
1079 2drop \ discard result
1080 2swap ( env nextbody )
1084 car 2swap ( exp env )
1086 R> drop ['] eval goto-deferred \ Tail call optimization
1089 bold fg red ." Object not applicable. Aboring." reset-term cr
1094 :noname ( obj env -- result )
1114 2swap eval-definition
1119 2swap eval-assignment
1135 ['] eval goto-deferred
1139 2dup lambda-parameters
1149 operands 2swap list-of-vals
1155 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1161 \ ---- Print ---- {{{
1163 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1165 : printrealnum ( realnumobj -- ) drop float-print ;
1167 : printbool ( numobj -- )
1175 : printchar ( charobj -- )
1178 9 of ." #\tab" endof
1179 bl of ." #\space" endof
1180 '\n' of ." #\newline" endof
1186 : (printstring) ( stringobj -- )
1187 nil-type istype? if 2drop exit then
1191 '\n' of ." \n" drop endof
1192 [char] \ of ." \\" drop endof
1193 [char] " of [char] \ emit [char] " emit drop endof
1199 : printstring ( stringobj -- )
1204 : printsymbol ( symbolobj -- )
1205 nil-type istype? if 2drop exit then
1211 : printnil ( nilobj -- )
1214 : printpair ( pairobj -- )
1218 nil-type istype? if 2drop exit then
1219 pair-type istype? if space recurse exit then
1223 : printprim ( primobj -- )
1224 2drop ." <primitive procedure>" ;
1226 : printcomp ( primobj -- )
1227 2drop ." <compound procedure>" ;
1230 fixnum-type istype? if printfixnum exit then
1231 realnum-type istype? if printrealnum exit then
1232 boolean-type istype? if printbool exit then
1233 character-type istype? if printchar exit then
1234 string-type istype? if printstring exit then
1235 symbol-type istype? if printsymbol exit then
1236 nil-type istype? if printnil exit then
1237 pair-type istype? if ." (" printpair ." )" exit then
1238 primitive-proc-type istype? if printprim exit then
1239 compound-proc-type istype? if printcomp exit then
1241 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1250 cr ." Welcome to scheme.forth.jl!" cr
1251 ." Use Ctrl-D to exit." cr
1259 cr bold fg green ." > " reset-term
1262 global-env obj@ eval
1264 fg cyan ." ; " print reset-term