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
280 : add-prim ( args -- )
284 2dup cdr recurse drop
290 ' add-prim make-primitive +
299 variable stored-parse-idx
300 create parse-str 161 allot
301 variable parse-str-span
303 create parse-idx-stack 10 allot
304 variable parse-idx-sp
305 parse-idx-stack parse-idx-sp !
308 parse-idx @ parse-idx-sp @ !
313 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
317 parse-idx-sp @ @ parse-idx ! ;
321 '\n' parse-str parse-str-span @ + !
322 1 parse-str-span +! ;
329 parse-str 160 expect cr
330 span @ parse-str-span !
340 : charavailable? ( -- bool )
341 parse-str-span @ parse-idx @ > ;
343 : nextchar ( -- char )
344 charavailable? false = if getline then
345 parse-str parse-idx @ + @ ;
347 : whitespace? ( -- bool )
356 nextchar [char] ( = or
357 nextchar [char] ) = or
374 nextchar [char] - = ;
376 : number? ( -- bool )
377 digit? minus? or false = if
398 : boolean? ( -- bool )
399 nextchar [char] # <> if false exit then
406 and if pop-parse-idx false exit then
418 : str-equiv? ( str -- bool )
435 delim? false = if drop false then
440 : character? ( -- bool )
441 nextchar [char] # <> if false exit then
446 nextchar [char] \ <> if pop-parse-idx false exit then
450 S" newline" str-equiv? if pop-parse-idx true exit then
451 S" space" str-equiv? if pop-parse-idx true exit then
452 S" tab" str-equiv? if pop-parse-idx true exit then
454 charavailable? false = if pop-parse-idx false exit then
460 nextchar [char] ( = ;
462 : string? ( -- bool )
463 nextchar [char] " = ;
465 : readnum ( -- num-atom )
473 10 * nextchar [char] 0 - +
482 : readbool ( -- bool-atom )
485 nextchar [char] f = if
496 : readchar ( -- char-atom )
500 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
501 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
502 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
504 nextchar character-type
509 : readstring ( -- charlist )
510 nextchar [char] " = if
515 ." No delimiter following right double quote. Aborting." cr
524 nextchar [char] \ = if
527 [char] n of '\n' endof
528 [char] " of [char] " endof
534 inc-parse-idx character-type
541 : readsymbol ( -- charlist )
542 delim? if nil exit then
544 nextchar inc-parse-idx character-type
551 : charlist-equiv ( charlist charlist -- bool )
560 2drop 2drop true exit
562 2drop 2drop false exit
567 2drop 2drop false exit
574 car drop -rot car drop = if
575 cdr 2swap cdr recurse
581 : charlist>symbol ( charlist -- symbol-obj )
583 symbol-table fetchobj
600 drop symbol-type 2dup
601 symbol-table fetchobj cons
605 : readpair ( -- pairobj )
609 nextchar [char] ) = if
614 ." No delimiter following right paren. Aborting." cr
623 \ Read first pair element
628 nextchar [char] . = if
633 ." No delimiter following '.'. Aborting." cr
647 \ Parse a scheme expression
673 nextchar [char] " <> if
674 bold red ." Missing closing double-quote." reset-term cr
692 nextchar [char] ) <> if
693 bold red ." Missing closing paren." reset-term cr
702 nextchar [char] ' = if
704 quote-symbol recurse nil cons cons exit
708 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
712 \ Anything else is parsed as a symbol
713 readsymbol charlist>symbol
723 : self-evaluating? ( obj -- obj bool )
724 boolean-type istype? if true exit then
725 number-type istype? if true exit then
726 character-type istype? if true exit then
727 string-type istype? if true exit then
728 nil-type istype? if true exit then
733 : tagged-list? ( obj tag-obj -- obj bool )
735 pair-type istype? false = if
741 : quote? ( obj -- obj bool )
742 quote-symbol tagged-list? ;
744 : quote-body ( quote-obj -- quote-body-obj )
747 : variable? ( obj -- obj bool )
748 symbol-type istype? ;
750 : definition? ( obj -- obj bool )
751 define-symbol tagged-list? ;
753 : definition-var ( obj -- var )
756 : definition-val ( obj -- val )
759 : assignment? ( obj -- obj bool )
760 set!-symbol tagged-list? ;
762 : assignment-var ( obj -- var )
765 : assignment-val ( obj -- val )
768 : eval-definition ( obj env -- res )
770 2over 2over ( env obj env obj )
771 definition-val 2swap ( env obj valexp env )
774 2swap definition-var 2swap ( env var val )
782 : eval-assignment ( obj env -- res )
784 2over 2over ( env obj env obj )
785 assignment-val 2swap ( env obj valexp env )
788 2swap assignment-var 2swap ( env var val )
796 : if? ( obj -- obj bool )
797 if-symbol tagged-list? ;
799 : if-predicate ( ifobj -- pred )
802 : if-consequent ( ifobj -- conseq )
805 : if-alternative ( ifobj -- alt|false )
813 : false? ( boolobj -- boolean )
814 boolean-type istype? if
815 false boolean-type objeq?
821 : true? ( boolobj -- bool )
824 : applicaion? ( obj -- obj bool)
827 : operator ( obj -- operator )
830 : operands ( obj -- operands )
833 : nooperands? ( operands -- bool )
836 : first-operand ( operands -- operand )
839 : rest-operands ( operands -- other-operands )
842 : list-of-vals ( args env -- vals )
846 :noname ( obj env -- result )
866 2swap eval-definition
871 2swap eval-assignment
893 primitive-type istype? false = if
894 bold fg red ." Object not applicable. Aboring." reset-term cr
899 operands 2swap list-of-vals
905 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
911 \ ---- Print ---- {{{
915 : printnum ( numobj -- ) drop 0 .R ;
917 : printbool ( numobj -- )
925 : printchar ( charobj -- )
929 bl of ." #\space" endof
930 '\n' of ." #\newline" endof
936 : (printstring) ( stringobj -- )
937 nil-type istype? if 2drop exit then
941 '\n' of ." \n" drop endof
942 [char] \ of ." \\" drop endof
943 [char] " of [char] \ emit [char] " emit drop endof
949 : printstring ( stringobj -- )
954 : printsymbol ( symbolobj -- )
955 nil-type istype? if 2drop exit then
961 : printnil ( nilobj -- )
964 : printpair ( pairobj -- )
968 nil-type istype? if 2drop exit then
969 pair-type istype? if space recurse exit then
973 : printprim ( primobj -- )
974 2drop ." <primitive procedure>" ;
977 number-type istype? if printnum exit then
978 boolean-type istype? if printbool exit then
979 character-type istype? if printchar exit then
980 string-type istype? if printstring exit then
981 symbol-type istype? if printsymbol exit then
982 nil-type istype? if printnil exit then
983 pair-type istype? if ." (" printpair ." )" exit then
984 primitive-type istype? if printprim exit then
986 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
995 cr ." Welcome to scheme.forth.jl!" cr
996 ." Use Ctrl-D to exit." cr
1001 cr bold fg green ." > " reset-term
1003 global-env fetchobj eval
1004 fg cyan ." ; " print reset-term