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
192 bold fg red ." Unbound variable " print ." . Aborting." reset-term cr
199 variable stored-parse-idx
200 create parse-str 161 allot
201 variable parse-str-span
203 create parse-idx-stack 10 allot
204 variable parse-idx-sp
205 parse-idx-stack parse-idx-sp !
208 parse-idx @ parse-idx-sp @ !
213 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
217 parse-idx-sp @ @ parse-idx ! ;
221 '\n' parse-str parse-str-span @ + !
222 1 parse-str-span +! ;
229 parse-str 160 expect cr
230 span @ parse-str-span !
240 : charavailable? ( -- bool )
241 parse-str-span @ parse-idx @ > ;
243 : nextchar ( -- char )
244 charavailable? false = if getline then
245 parse-str parse-idx @ + @ ;
247 : whitespace? ( -- bool )
256 nextchar [char] ( = or
257 nextchar [char] ) = or
274 nextchar [char] - = ;
276 : number? ( -- bool )
277 digit? minus? or false = if
298 : boolean? ( -- bool )
299 nextchar [char] # <> if false exit then
306 and if pop-parse-idx false exit then
318 : str-equiv? ( str -- bool )
335 delim? false = if drop false then
340 : character? ( -- bool )
341 nextchar [char] # <> if false exit then
346 nextchar [char] \ <> if pop-parse-idx false exit then
350 S" newline" str-equiv? if pop-parse-idx true exit then
351 S" space" str-equiv? if pop-parse-idx true exit then
352 S" tab" str-equiv? if pop-parse-idx true exit then
354 charavailable? false = if pop-parse-idx false exit then
360 nextchar [char] ( = ;
362 : string? ( -- bool )
363 nextchar [char] " = ;
365 : readnum ( -- num-atom )
373 10 * nextchar [char] 0 - +
382 : readbool ( -- bool-atom )
385 nextchar [char] f = if
396 : readchar ( -- char-atom )
400 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
401 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
402 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
404 nextchar character-type
409 : readstring ( -- charlist )
410 nextchar [char] " = if
415 ." No delimiter following right double quote. Aborting." cr
424 nextchar [char] \ = if
427 [char] n of '\n' endof
428 [char] " of [char] " endof
434 inc-parse-idx character-type
441 : readsymbol ( -- charlist )
442 delim? if nil exit then
444 nextchar inc-parse-idx character-type
451 : charlist-equiv ( charlist charlist -- bool )
460 2drop 2drop true exit
462 2drop 2drop false exit
467 2drop 2drop false exit
474 car drop -rot car drop = if
475 cdr 2swap cdr recurse
481 : charlist>symbol ( charlist -- symbol-obj )
483 symbol-table fetchobj
500 drop symbol-type 2dup
501 symbol-table fetchobj cons
507 : readpair ( -- pairobj )
511 nextchar [char] ) = if
516 ." No delimiter following right paren. Aborting." cr
525 \ Read first pair element
530 nextchar [char] . = if
535 ." No delimiter following '.'. Aborting." cr
549 \ Parse a scheme expression
574 nextchar [char] " <> if
575 bold red ." Missing closing double-quote." reset-term cr
592 nextchar [char] ) <> if
593 bold red ." Missing closing paren." reset-term cr
602 nextchar [char] ' = if
604 quote-symbol recurse nil cons cons exit
608 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
612 \ Anything else is assumed to be a symbol
613 readsymbol charlist>symbol
619 : self-evaluating? ( obj -- obj bool )
620 boolean-type istype? if true exit then
621 number-type istype? if true exit then
622 character-type istype? if true exit then
623 string-type istype? if true exit then
624 nil-type istype? if true exit then
629 : tagged-list? ( obj tag-obj -- obj bool )
631 pair-type istype? false = if
637 : quote? ( obj -- obj bool )
638 quote-symbol tagged-list? ;
640 : quote-body ( quote-obj -- quote-body-obj )
643 : eval ( obj env -- result )
657 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
663 : printnum ( numobj -- ) drop 0 .R ;
665 : printbool ( numobj -- )
673 : printchar ( charobj -- )
677 bl of ." #\space" endof
678 '\n' of ." #\newline" endof
684 : (printstring) ( stringobj -- )
685 nil-type istype? if 2drop exit then
689 '\n' of ." \n" drop endof
690 [char] \ of ." \\" drop endof
691 [char] " of [char] \ emit [char] " emit drop endof
697 : printstring ( stringobj -- )
702 : printsymbol ( symbolobj -- )
703 nil-type istype? if 2drop exit then
709 : printnil ( nilobj -- )
713 : printpair ( pairobj -- )
717 nil-type istype? if 2drop exit then
718 pair-type istype? if space recurse exit then
723 number-type istype? if printnum exit then
724 boolean-type istype? if printbool exit then
725 character-type istype? if printchar exit then
726 string-type istype? if printstring exit then
727 symbol-type istype? if printsymbol exit then
728 nil-type istype? if printnil exit then
729 pair-type istype? if ." (" printpair ." )" exit then
731 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
738 cr ." Welcome to scheme.forth.jl!" cr
739 ." Use Ctrl-D to exit." cr
744 cr bold fg green ." > " reset-term
746 global-env fetchobj eval
747 fg cyan ." ; " print reset-term