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
123 create-symbol ok ok-symbol
124 create-symbol if if-symbol
128 \ ---- 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
187 get-vars-vals-frame if
189 vars fetchobj vals fetchobj true
203 : lookup-var ( var env -- val )
207 bold fg red ." Tried to read unbound variable." reset-term cr 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 cr abort
223 : define-var ( var val env -- )
226 2over env fetchobj ( var val var env )
228 2swap 2drop ( var val vals )
233 first-frame ( var val frame )
241 nil nil nil extend-env
249 variable stored-parse-idx
250 create parse-str 161 allot
251 variable parse-str-span
253 create parse-idx-stack 10 allot
254 variable parse-idx-sp
255 parse-idx-stack parse-idx-sp !
258 parse-idx @ parse-idx-sp @ !
263 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
267 parse-idx-sp @ @ parse-idx ! ;
271 '\n' parse-str parse-str-span @ + !
272 1 parse-str-span +! ;
279 parse-str 160 expect cr
280 span @ parse-str-span !
290 : charavailable? ( -- bool )
291 parse-str-span @ parse-idx @ > ;
293 : nextchar ( -- char )
294 charavailable? false = if getline then
295 parse-str parse-idx @ + @ ;
297 : whitespace? ( -- bool )
306 nextchar [char] ( = or
307 nextchar [char] ) = or
324 nextchar [char] - = ;
326 : number? ( -- bool )
327 digit? minus? or false = if
348 : boolean? ( -- bool )
349 nextchar [char] # <> if false exit then
356 and if pop-parse-idx false exit then
368 : str-equiv? ( str -- bool )
385 delim? false = if drop false then
390 : character? ( -- bool )
391 nextchar [char] # <> if false exit then
396 nextchar [char] \ <> if pop-parse-idx false exit then
400 S" newline" str-equiv? if pop-parse-idx true exit then
401 S" space" str-equiv? if pop-parse-idx true exit then
402 S" tab" str-equiv? if pop-parse-idx true exit then
404 charavailable? false = if pop-parse-idx false exit then
410 nextchar [char] ( = ;
412 : string? ( -- bool )
413 nextchar [char] " = ;
415 : readnum ( -- num-atom )
423 10 * nextchar [char] 0 - +
432 : readbool ( -- bool-atom )
435 nextchar [char] f = if
446 : readchar ( -- char-atom )
450 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
451 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
452 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
454 nextchar character-type
459 : readstring ( -- charlist )
460 nextchar [char] " = if
465 ." No delimiter following right double quote. Aborting." cr
474 nextchar [char] \ = if
477 [char] n of '\n' endof
478 [char] " of [char] " endof
484 inc-parse-idx character-type
491 : readsymbol ( -- charlist )
492 delim? if nil exit then
494 nextchar inc-parse-idx character-type
501 : charlist-equiv ( charlist charlist -- bool )
510 2drop 2drop true exit
512 2drop 2drop false exit
517 2drop 2drop false exit
524 car drop -rot car drop = if
525 cdr 2swap cdr recurse
531 : charlist>symbol ( charlist -- symbol-obj )
533 symbol-table fetchobj
550 drop symbol-type 2dup
551 symbol-table fetchobj cons
557 : readpair ( -- pairobj )
561 nextchar [char] ) = if
566 ." No delimiter following right paren. Aborting." cr
575 \ Read first pair element
580 nextchar [char] . = if
585 ." No delimiter following '.'. Aborting." cr
599 \ Parse a scheme expression
625 nextchar [char] " <> if
626 bold red ." Missing closing double-quote." reset-term cr
644 nextchar [char] ) <> if
645 bold red ." Missing closing paren." reset-term cr
654 nextchar [char] ' = if
656 quote-symbol recurse nil cons cons exit
660 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
664 \ Anything else is parsed as a symbol
665 readsymbol charlist>symbol
675 : self-evaluating? ( obj -- obj bool )
676 boolean-type istype? if true exit then
677 number-type istype? if true exit then
678 character-type istype? if true exit then
679 string-type istype? if true exit then
680 nil-type istype? if true exit then
685 : tagged-list? ( obj tag-obj -- obj bool )
687 pair-type istype? false = if
693 : quote? ( obj -- obj bool )
694 quote-symbol tagged-list? ;
696 : quote-body ( quote-obj -- quote-body-obj )
699 : variable? ( obj -- obj bool )
700 symbol-type istype? ;
702 : definition? ( obj -- obj bool )
703 define-symbol tagged-list? ;
705 : definition-var ( obj -- var )
708 : definition-val ( obj -- val )
711 : assignment? ( obj -- obj bool )
712 set!-symbol tagged-list? ;
714 : assignment-var ( obj -- var )
717 : assignment-val ( obj -- val )
720 : eval-definition ( obj env -- res )
722 2over 2over ( env obj env obj )
723 definition-val 2swap ( env obj valexp env )
726 2swap definition-var 2swap ( env var val )
728 >R >R 2swap R> R> 2swap ( var val env )
734 : eval-assignment ( obj env -- res )
736 2over 2over ( env obj env obj )
737 assignment-val 2swap ( env obj valexp env )
740 2swap assignment-var 2swap ( env var val )
742 >R >R 2swap R> R> 2swap ( var val env )
748 : if? ( obj -- obj bool )
749 if-symbol tagged-list? ;
751 : if-predicate ( ifobj -- pred )
754 : if-consequent ( ifobj -- conseq )
757 : if-alternative ( ifobj -- alt|false )
765 : false? ( boolobj -- boolean )
766 boolean-type istype? if
767 false boolean-type objeq?
773 : true? ( boolobj -- boolean )
776 :noname ( obj env -- result )
796 2swap eval-definition
801 2swap eval-assignment
819 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
825 \ ---- Print ---- {{{
827 : printnum ( numobj -- ) drop 0 .R ;
829 : printbool ( numobj -- )
837 : printchar ( charobj -- )
841 bl of ." #\space" endof
842 '\n' of ." #\newline" endof
848 : (printstring) ( stringobj -- )
849 nil-type istype? if 2drop exit then
853 '\n' of ." \n" drop endof
854 [char] \ of ." \\" drop endof
855 [char] " of [char] \ emit [char] " emit drop endof
861 : printstring ( stringobj -- )
866 : printsymbol ( symbolobj -- )
867 nil-type istype? if 2drop exit then
873 : printnil ( nilobj -- )
877 : printpair ( pairobj -- )
881 nil-type istype? if 2drop exit then
882 pair-type istype? if space recurse exit then
887 number-type istype? if printnum exit then
888 boolean-type istype? if printbool exit then
889 character-type istype? if printchar exit then
890 string-type istype? if printstring exit then
891 symbol-type istype? if printsymbol exit then
892 nil-type istype? if printnil exit then
893 pair-type istype? if ." (" printpair ." )" exit then
895 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
904 cr ." Welcome to scheme.forth.jl!" cr
905 ." Use Ctrl-D to exit." cr
910 cr bold fg green ." > " reset-term
912 global-env fetchobj eval
913 fg cyan ." ; " print reset-term