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 : istype? ( obj type -- obj bool )
19 \ ------ Cons cell memory ------
22 create car-cells N allot
23 create car-type-cells N allot
24 create cdr-cells N allot
25 create cdr-type-cells N allot
30 : cons ( car-obj cdr-obj -- pair-obj )
31 cdr-type-cells nextfree @ + !
32 cdr-cells nextfree @ + !
33 car-type-cells nextfree @ + !
34 car-cells nextfree @ + !
41 : car ( pair-obj -- car-obj )
43 dup car-cells + @ swap
47 : cdr ( pair-obj -- car-obj )
49 dup cdr-cells + @ swap
53 : set-car! ( obj pair-obj -- )
55 rot swap car-type-cells + !
59 : set-cdr! ( obj pair-obj -- )
61 rot swap cdr-type-cells + !
71 : nil? nil-type istype? ;
73 : objvar create nil swap , , ;
75 : value@ ( objvar -- val ) @ ;
76 : type@ ( objvar -- type ) 1+ @ ;
77 : value! ( newval objvar -- ) ! ;
78 : type! ( newtype objvar -- ) 1+ ! ;
79 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
80 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
82 : objeq? ( obj obj -- bool )
85 \ ---- Pre-defined symbols ----
89 : (create-symbol) ( addr n -- symbol-obj )
93 2dup drop @ character-type 2swap
101 : create-symbol ( -- )
110 symbol-table fetchobj
115 does> dup @ swap 1+ @
118 create-symbol quote quote-symbol
119 create-symbol define define-symbol
120 create-symbol set! set!-symbol
122 \ ---- Environments ----
126 : enclosing-env ( env -- env )
129 : first-frame ( env -- frame )
132 : make-frame ( vars vals -- frame )
135 : frame-vars ( frame -- vars )
138 : frame-vals ( frame -- vals )
141 : add-binding ( var val frame -- )
142 2swap 2over frame-vals cons
144 2swap 2over frame-vars cons
148 : extend-env ( vars vals env -- env )
158 : lookup-var-frame ( var frame -- val? bool )
159 2dup frame-vars vars setobj
160 frame-vals vals setobj
163 vars fetchobj nil objeq? false =
165 2dup vars fetchobj car objeq? if
167 vals fetchobj car true
171 vars fetchobj cdr vars setobj
172 vals fetchobj cdr vals setobj
178 : lookup-var ( var env -- val )
180 2dup nil objeq? false =
182 2over 2over first-frame
184 -rot 2drop -rot 2drop
191 bold fg red ." Unbound variable. Aborting." reset-term cr
197 : set-var-frame ( var frame -- )
198 2dup frame-vars vars setobj
199 frame-vals vals setobj
202 vars fetchobj nil objeq? false =
204 2dup vars fetchobj car objeq? if
209 vars fetchobj cdr vars setobj
210 vals fetchobj cdr vals setobj
215 : set-var ( var val env -- )
220 2dup nil objeq? false =
222 2over 2over first-frame
230 bold fg red ." Unbound variable. Aborting." reset-term cr
241 variable stored-parse-idx
242 create parse-str 161 allot
243 variable parse-str-span
245 create parse-idx-stack 10 allot
246 variable parse-idx-sp
247 parse-idx-stack parse-idx-sp !
250 parse-idx @ parse-idx-sp @ !
255 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
259 parse-idx-sp @ @ parse-idx ! ;
263 '\n' parse-str parse-str-span @ + !
264 1 parse-str-span +! ;
271 parse-str 160 expect cr
272 span @ parse-str-span !
282 : charavailable? ( -- bool )
283 parse-str-span @ parse-idx @ > ;
285 : nextchar ( -- char )
286 charavailable? false = if getline then
287 parse-str parse-idx @ + @ ;
289 : whitespace? ( -- bool )
298 nextchar [char] ( = or
299 nextchar [char] ) = or
316 nextchar [char] - = ;
318 : number? ( -- bool )
319 digit? minus? or false = if
340 : boolean? ( -- bool )
341 nextchar [char] # <> if false exit then
348 and if pop-parse-idx false exit then
360 : str-equiv? ( str -- bool )
377 delim? false = if drop false then
382 : character? ( -- bool )
383 nextchar [char] # <> if false exit then
388 nextchar [char] \ <> if pop-parse-idx false exit then
392 S" newline" str-equiv? if pop-parse-idx true exit then
393 S" space" str-equiv? if pop-parse-idx true exit then
394 S" tab" str-equiv? if pop-parse-idx true exit then
396 charavailable? false = if pop-parse-idx false exit then
402 nextchar [char] ( = ;
404 : string? ( -- bool )
405 nextchar [char] " = ;
407 : readnum ( -- num-atom )
415 10 * nextchar [char] 0 - +
424 : readbool ( -- bool-atom )
427 nextchar [char] f = if
438 : readchar ( -- char-atom )
442 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
443 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
444 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
446 nextchar character-type
451 : readstring ( -- charlist )
452 nextchar [char] " = if
457 ." No delimiter following right double quote. Aborting." cr
466 nextchar [char] \ = if
469 [char] n of '\n' endof
470 [char] " of [char] " endof
476 inc-parse-idx character-type
483 : readsymbol ( -- charlist )
484 delim? if nil exit then
486 nextchar inc-parse-idx character-type
493 : charlist-equiv ( charlist charlist -- bool )
502 2drop 2drop true exit
504 2drop 2drop false exit
509 2drop 2drop false exit
516 car drop -rot car drop = if
517 cdr 2swap cdr recurse
523 : charlist>symbol ( charlist -- symbol-obj )
525 symbol-table fetchobj
542 drop symbol-type 2dup
543 symbol-table fetchobj cons
549 : readpair ( -- pairobj )
553 nextchar [char] ) = if
558 ." No delimiter following right paren. Aborting." cr
567 \ Read first pair element
572 nextchar [char] . = if
577 ." No delimiter following '.'. Aborting." cr
591 \ Parse a scheme expression
616 nextchar [char] " <> if
617 bold red ." Missing closing double-quote." reset-term cr
634 nextchar [char] ) <> if
635 bold red ." Missing closing paren." reset-term cr
644 nextchar [char] ' = if
646 quote-symbol recurse nil cons cons exit
650 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
654 \ Anything else is parsed as a symbol
655 readsymbol charlist>symbol
661 : self-evaluating? ( obj -- obj bool )
662 boolean-type istype? if true exit then
663 number-type istype? if true exit then
664 character-type istype? if true exit then
665 string-type istype? if true exit then
666 nil-type istype? if true exit then
671 : tagged-list? ( obj tag-obj -- obj bool )
673 pair-type istype? false = if
679 : quote? ( obj -- obj bool )
680 quote-symbol tagged-list? ;
682 : quote-body ( quote-obj -- quote-body-obj )
685 : eval ( obj env -- result )
699 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
705 : printnum ( numobj -- ) drop 0 .R ;
707 : printbool ( numobj -- )
715 : printchar ( charobj -- )
719 bl of ." #\space" endof
720 '\n' of ." #\newline" endof
726 : (printstring) ( stringobj -- )
727 nil-type istype? if 2drop exit then
731 '\n' of ." \n" drop endof
732 [char] \ of ." \\" drop endof
733 [char] " of [char] \ emit [char] " emit drop endof
739 : printstring ( stringobj -- )
744 : printsymbol ( symbolobj -- )
745 nil-type istype? if 2drop exit then
751 : printnil ( nilobj -- )
755 : printpair ( pairobj -- )
759 nil-type istype? if 2drop exit then
760 pair-type istype? if space recurse exit then
765 number-type istype? if printnum exit then
766 boolean-type istype? if printbool exit then
767 character-type istype? if printchar exit then
768 string-type istype? if printstring exit then
769 symbol-type istype? if printsymbol exit then
770 nil-type istype? if printnil exit then
771 pair-type istype? if ." (" printpair ." )" exit then
773 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
780 cr ." Welcome to scheme.forth.jl!" cr
781 ." Use Ctrl-D to exit." cr
786 cr bold fg green ." > " reset-term
788 global-env fetchobj eval
789 fg cyan ." ; " print reset-term