4 include term-colours.4th
10 1 constant boolean-type
11 2 constant character-type
12 3 constant string-type
15 6 constant symbol-type
16 7 constant primitive-type
17 : istype? ( obj type -- obj bool )
20 \ ------ Cons cell memory ------ {{{
23 create car-cells N allot
24 create car-type-cells N allot
25 create cdr-cells N allot
26 create cdr-type-cells N allot
31 : cons ( car-obj cdr-obj -- pair-obj )
32 cdr-type-cells nextfree @ + !
33 cdr-cells nextfree @ + !
34 car-type-cells nextfree @ + !
35 car-cells nextfree @ + !
42 : car ( pair-obj -- car-obj )
44 dup car-cells + @ swap
48 : cdr ( pair-obj -- car-obj )
50 dup cdr-cells + @ swap
54 : set-car! ( obj pair-obj -- )
56 rot swap car-type-cells + !
60 : set-cdr! ( obj pair-obj -- )
62 rot swap cdr-type-cells + !
72 : nil? nil-type istype? ;
74 : objvar create nil swap , , ;
76 : value@ ( objvar -- val ) @ ;
77 : type@ ( objvar -- type ) 1+ @ ;
78 : value! ( newval objvar -- ) ! ;
79 : type! ( newtype objvar -- ) 1+ ! ;
80 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
81 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
83 : objeq? ( obj obj -- bool )
86 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
89 R> R> ( b1 b2 a1 a2 c1 c2 )
93 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
94 2swap ( a1 a2 c1 c2 b1 b2 )
102 \ ---- Pre-defined symbols ---- {{{
106 : (create-symbol) ( addr n -- symbol-obj )
110 2dup drop @ character-type 2swap
118 : create-symbol ( -- )
127 symbol-table fetchobj
132 does> dup @ swap 1+ @
135 create-symbol quote quote-symbol
136 create-symbol define define-symbol
137 create-symbol set! set!-symbol
138 create-symbol ok ok-symbol
139 create-symbol if if-symbol
143 \ ---- Environments ---- {{{
145 : enclosing-env ( env -- env )
148 : first-frame ( env -- frame )
151 : make-frame ( vars vals -- frame )
154 : frame-vars ( frame -- vars )
157 : frame-vals ( frame -- vals )
160 : add-binding ( var val frame -- )
161 2swap 2over frame-vals cons
163 2swap 2over frame-vars cons
167 : extend-env ( vars vals env -- env )
177 : get-vars-vals-frame ( var frame -- bool )
178 2dup frame-vars vars setobj
179 frame-vals vals setobj
182 vars fetchobj nil objeq? false =
184 2dup vars fetchobj car objeq? if
189 vars fetchobj cdr vars setobj
190 vals fetchobj cdr vals setobj
196 : get-vars-vals ( var env -- vars? vals? bool )
199 2dup nil objeq? false =
201 2over 2over first-frame
202 get-vars-vals-frame if
204 vars fetchobj vals fetchobj true
218 : lookup-var ( var env -- val )
222 bold fg red ." Tried to read unbound variable." reset-term cr abort
226 : set-var ( var val env -- )
227 >R >R 2swap R> R> ( val var env )
229 2swap 2drop ( val vals )
232 bold fg red ." Tried to set unbound variable." reset-term cr abort
238 : define-var ( var val env -- )
241 2over env fetchobj ( var val var env )
243 2swap 2drop ( var val vals )
248 first-frame ( var val frame )
256 nil nil nil extend-env
261 \ ---- Primitives ---- {{{
263 : make-primitive ( cfa -- )
272 symbol-table fetchobj
276 rot primitive-type ( var prim )
277 global-env fetchobj define-var
282 : add-prim ( args -- )
288 -rot cdr recurse drop
292 ' add-prim make-primitive +
300 -rot cdr add-prim drop
311 -rot cdr recurse drop
323 variable stored-parse-idx
324 create parse-str 161 allot
325 variable parse-str-span
327 create parse-idx-stack 10 allot
328 variable parse-idx-sp
329 parse-idx-stack parse-idx-sp !
332 parse-idx @ parse-idx-sp @ !
337 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
341 parse-idx-sp @ @ parse-idx ! ;
345 '\n' parse-str parse-str-span @ + !
346 1 parse-str-span +! ;
353 parse-str 160 expect cr
354 span @ parse-str-span !
364 : charavailable? ( -- bool )
365 parse-str-span @ parse-idx @ > ;
367 : nextchar ( -- char )
368 charavailable? false = if getline then
369 parse-str parse-idx @ + @ ;
371 : whitespace? ( -- bool )
380 nextchar [char] ( = or
381 nextchar [char] ) = or
398 nextchar [char] - = ;
400 : fixnum? ( -- bool )
432 : boolean? ( -- bool )
433 nextchar [char] # <> if false exit then
440 and if pop-parse-idx false exit then
452 : str-equiv? ( str -- bool )
469 delim? false = if drop false then
474 : character? ( -- bool )
475 nextchar [char] # <> if false exit then
480 nextchar [char] \ <> if pop-parse-idx false exit then
484 S" newline" str-equiv? if pop-parse-idx true exit then
485 S" space" str-equiv? if pop-parse-idx true exit then
486 S" tab" str-equiv? if pop-parse-idx true exit then
488 charavailable? false = if pop-parse-idx false exit then
494 nextchar [char] ( = ;
496 : string? ( -- bool )
497 nextchar [char] " = ;
499 : readnum ( -- num-atom )
507 10 * nextchar [char] 0 - +
516 : readbool ( -- bool-atom )
519 nextchar [char] f = if
530 : readchar ( -- char-atom )
534 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
535 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
536 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
538 nextchar character-type
543 : readstring ( -- charlist )
544 nextchar [char] " = if
549 ." No delimiter following right double quote. Aborting." cr
558 nextchar [char] \ = if
561 [char] n of '\n' endof
562 [char] " of [char] " endof
568 inc-parse-idx character-type
575 : readsymbol ( -- charlist )
576 delim? if nil exit then
578 nextchar inc-parse-idx character-type
585 : charlist-equiv ( charlist charlist -- bool )
594 2drop 2drop true exit
596 2drop 2drop false exit
601 2drop 2drop false exit
608 car drop -rot car drop = if
609 cdr 2swap cdr recurse
615 : charlist>symbol ( charlist -- symbol-obj )
617 symbol-table fetchobj
634 drop symbol-type 2dup
635 symbol-table fetchobj cons
639 : readpair ( -- pairobj )
643 nextchar [char] ) = if
648 ." No delimiter following right paren. Aborting." cr
657 \ Read first pair element
662 nextchar [char] . = if
667 ." No delimiter following '.'. Aborting." cr
681 \ Parse a scheme expression
707 nextchar [char] " <> if
708 bold red ." Missing closing double-quote." reset-term cr
726 nextchar [char] ) <> if
727 bold red ." Missing closing paren." reset-term cr
736 nextchar [char] ' = if
738 quote-symbol recurse nil cons cons exit
742 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
746 \ Anything else is parsed as a symbol
747 readsymbol charlist>symbol
757 : self-evaluating? ( obj -- obj bool )
758 boolean-type istype? if true exit then
759 fixnum-type istype? if true exit then
760 character-type istype? if true exit then
761 string-type istype? if true exit then
762 nil-type istype? if true exit then
767 : tagged-list? ( obj tag-obj -- obj bool )
769 pair-type istype? false = if
775 : quote? ( obj -- obj bool )
776 quote-symbol tagged-list? ;
778 : quote-body ( quote-obj -- quote-body-obj )
781 : variable? ( obj -- obj bool )
782 symbol-type istype? ;
784 : definition? ( obj -- obj bool )
785 define-symbol tagged-list? ;
787 : definition-var ( obj -- var )
790 : definition-val ( obj -- val )
793 : assignment? ( obj -- obj bool )
794 set!-symbol tagged-list? ;
796 : assignment-var ( obj -- var )
799 : assignment-val ( obj -- val )
802 : eval-definition ( obj env -- res )
804 2over 2over ( env obj env obj )
805 definition-val 2swap ( env obj valexp env )
808 2swap definition-var 2swap ( env var val )
816 : eval-assignment ( obj env -- res )
818 2over 2over ( env obj env obj )
819 assignment-val 2swap ( env obj valexp env )
822 2swap assignment-var 2swap ( env var val )
830 : if? ( obj -- obj bool )
831 if-symbol tagged-list? ;
833 : if-predicate ( ifobj -- pred )
836 : if-consequent ( ifobj -- conseq )
839 : if-alternative ( ifobj -- alt|false )
847 : false? ( boolobj -- boolean )
848 boolean-type istype? if
849 false boolean-type objeq?
855 : true? ( boolobj -- bool )
858 : application? ( obj -- obj bool)
861 : operator ( obj -- operator )
864 : operands ( obj -- operands )
867 : nooperands? ( operands -- bool )
870 : first-operand ( operands -- operand )
873 : rest-operands ( operands -- other-operands )
876 : list-of-vals ( args env -- vals )
882 2over 2over first-operand 2swap eval
883 -2rot rest-operands 2swap recurse
888 :noname ( obj env -- result )
908 2swap eval-definition
913 2swap eval-assignment
935 primitive-type istype? false = if
936 bold fg red ." Object not applicable. Aboring." reset-term cr
941 operands 2swap list-of-vals
947 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
953 \ ---- Print ---- {{{
957 : printnum ( numobj -- ) drop 0 .R ;
959 : printbool ( numobj -- )
967 : printchar ( charobj -- )
971 bl of ." #\space" endof
972 '\n' of ." #\newline" endof
978 : (printstring) ( stringobj -- )
979 nil-type istype? if 2drop exit then
983 '\n' of ." \n" drop endof
984 [char] \ of ." \\" drop endof
985 [char] " of [char] \ emit [char] " emit drop endof
991 : printstring ( stringobj -- )
996 : printsymbol ( symbolobj -- )
997 nil-type istype? if 2drop exit then
1003 : printnil ( nilobj -- )
1006 : printpair ( pairobj -- )
1010 nil-type istype? if 2drop exit then
1011 pair-type istype? if space recurse exit then
1015 : printprim ( primobj -- )
1016 2drop ." <primitive procedure>" ;
1019 fixnum-type istype? if printnum exit then
1020 boolean-type istype? if printbool exit then
1021 character-type istype? if printchar exit then
1022 string-type istype? if printstring exit then
1023 symbol-type istype? if printsymbol exit then
1024 nil-type istype? if printnil exit then
1025 pair-type istype? if ." (" printpair ." )" exit then
1026 primitive-type istype? if printprim exit then
1028 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1037 cr ." Welcome to scheme.forth.jl!" cr
1038 ." Use Ctrl-D to exit." cr
1043 cr bold fg green ." > " reset-term
1045 global-env fetchobj eval
1046 fg cyan ." ; " print reset-term