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 )
87 \ ---- Pre-defined symbols ---- {{{
91 : (create-symbol) ( addr n -- symbol-obj )
95 2dup drop @ character-type 2swap
103 : create-symbol ( -- )
112 symbol-table fetchobj
117 does> dup @ swap 1+ @
120 create-symbol quote quote-symbol
121 create-symbol define define-symbol
122 create-symbol set! set!-symbol
126 \ ---- Environments ---- {{{
130 : enclosing-env ( env -- env )
133 : first-frame ( env -- frame )
136 : make-frame ( vars vals -- frame )
139 : frame-vars ( frame -- vars )
142 : frame-vals ( frame -- vals )
145 : add-binding ( var val frame -- )
146 2swap 2over frame-vals cons
148 2swap 2over frame-vars cons
152 : extend-env ( vars vals env -- env )
162 : get-vars-vals-frame ( var frame -- bool )
163 2dup frame-vars vars setobj
164 frame-vals vals setobj
167 vars fetchobj nil objeq? false =
169 2dup vars fetchobj car objeq? if
174 vars fetchobj cdr vars setobj
175 vals fetchobj cdr vals setobj
181 : get-vars-vals ( var env -- vars? vals? bool )
184 2dup nil objeq? false =
186 2over 2over first-frame
189 vars fetchobj vals fetchobj true
203 : lookup-var ( var env -- val )
207 bold fg red ." Tried to read unbound variable." reset-term abort
211 : set-var ( var val env -- )
212 >R >R 2swap R> R> ( val var env )
214 2swap 2drop ( val vals )
217 bold fg red ." Tried to set unbound variable." reset-term abort
223 : define-var ( var val env -- )
226 2over env objfetch ( var val var env )
228 2swap 2drop ( var val vals )
233 first-frame ( var val frame )
245 variable stored-parse-idx
246 create parse-str 161 allot
247 variable parse-str-span
249 create parse-idx-stack 10 allot
250 variable parse-idx-sp
251 parse-idx-stack parse-idx-sp !
254 parse-idx @ parse-idx-sp @ !
259 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
263 parse-idx-sp @ @ parse-idx ! ;
267 '\n' parse-str parse-str-span @ + !
268 1 parse-str-span +! ;
275 parse-str 160 expect cr
276 span @ parse-str-span !
286 : charavailable? ( -- bool )
287 parse-str-span @ parse-idx @ > ;
289 : nextchar ( -- char )
290 charavailable? false = if getline then
291 parse-str parse-idx @ + @ ;
293 : whitespace? ( -- bool )
302 nextchar [char] ( = or
303 nextchar [char] ) = or
320 nextchar [char] - = ;
322 : number? ( -- bool )
323 digit? minus? or false = if
344 : boolean? ( -- bool )
345 nextchar [char] # <> if false exit then
352 and if pop-parse-idx false exit then
364 : str-equiv? ( str -- bool )
381 delim? false = if drop false then
386 : character? ( -- bool )
387 nextchar [char] # <> if false exit then
392 nextchar [char] \ <> if pop-parse-idx false exit then
396 S" newline" str-equiv? if pop-parse-idx true exit then
397 S" space" str-equiv? if pop-parse-idx true exit then
398 S" tab" str-equiv? if pop-parse-idx true exit then
400 charavailable? false = if pop-parse-idx false exit then
406 nextchar [char] ( = ;
408 : string? ( -- bool )
409 nextchar [char] " = ;
411 : readnum ( -- num-atom )
419 10 * nextchar [char] 0 - +
428 : readbool ( -- bool-atom )
431 nextchar [char] f = if
442 : readchar ( -- char-atom )
446 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
447 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
448 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
450 nextchar character-type
455 : readstring ( -- charlist )
456 nextchar [char] " = if
461 ." No delimiter following right double quote. Aborting." cr
470 nextchar [char] \ = if
473 [char] n of '\n' endof
474 [char] " of [char] " endof
480 inc-parse-idx character-type
487 : readsymbol ( -- charlist )
488 delim? if nil exit then
490 nextchar inc-parse-idx character-type
497 : charlist-equiv ( charlist charlist -- bool )
506 2drop 2drop true exit
508 2drop 2drop false exit
513 2drop 2drop false exit
520 car drop -rot car drop = if
521 cdr 2swap cdr recurse
527 : charlist>symbol ( charlist -- symbol-obj )
529 symbol-table fetchobj
546 drop symbol-type 2dup
547 symbol-table fetchobj cons
553 : readpair ( -- pairobj )
557 nextchar [char] ) = if
562 ." No delimiter following right paren. Aborting." cr
571 \ Read first pair element
576 nextchar [char] . = if
581 ." No delimiter following '.'. Aborting." cr
595 \ Parse a scheme expression
621 nextchar [char] " <> if
622 bold red ." Missing closing double-quote." reset-term cr
640 nextchar [char] ) <> if
641 bold red ." Missing closing paren." reset-term cr
650 nextchar [char] ' = if
652 quote-symbol recurse nil cons cons exit
656 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
660 \ Anything else is parsed as a symbol
661 readsymbol charlist>symbol
669 : self-evaluating? ( obj -- obj bool )
670 boolean-type istype? if true exit then
671 number-type istype? if true exit then
672 character-type istype? if true exit then
673 string-type istype? if true exit then
674 nil-type istype? if true exit then
679 : tagged-list? ( obj tag-obj -- obj bool )
681 pair-type istype? false = if
687 : quote? ( obj -- obj bool )
688 quote-symbol tagged-list? ;
690 : quote-body ( quote-obj -- quote-body-obj )
693 : variable? ( obj -- obj bool )
694 symbol-type istype? ;
696 : definition? ( obj -- obj bool )
697 define-symbol tagged-list? ;
699 : assignment? ( obj -- obj bool )
700 set-symbol tagged-list? ;
702 : eval ( obj env -- result )
721 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
727 \ ---- Print ---- {{{
729 : printnum ( numobj -- ) drop 0 .R ;
731 : printbool ( numobj -- )
739 : printchar ( charobj -- )
743 bl of ." #\space" endof
744 '\n' of ." #\newline" endof
750 : (printstring) ( stringobj -- )
751 nil-type istype? if 2drop exit then
755 '\n' of ." \n" drop endof
756 [char] \ of ." \\" drop endof
757 [char] " of [char] \ emit [char] " emit drop endof
763 : printstring ( stringobj -- )
768 : printsymbol ( symbolobj -- )
769 nil-type istype? if 2drop exit then
775 : printnil ( nilobj -- )
779 : printpair ( pairobj -- )
783 nil-type istype? if 2drop exit then
784 pair-type istype? if space recurse exit then
789 number-type istype? if printnum exit then
790 boolean-type istype? if printbool exit then
791 character-type istype? if printchar exit then
792 string-type istype? if printstring exit then
793 symbol-type istype? if printsymbol exit then
794 nil-type istype? if printnil exit then
795 pair-type istype? if ." (" printpair ." )" exit then
797 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
806 cr ." Welcome to scheme.forth.jl!" cr
807 ." Use Ctrl-D to exit." cr
812 cr bold fg green ." > " reset-term
814 global-env fetchobj eval
815 fg cyan ." ; " print reset-term