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 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
88 R> R> ( b1 b2 a1 a2 c1 c2 )
92 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
93 2swap ( a1 a2 c1 c2 b1 b2 )
101 \ ---- Pre-defined symbols ---- {{{
105 : (create-symbol) ( addr n -- symbol-obj )
109 2dup drop @ character-type 2swap
117 : create-symbol ( -- )
126 symbol-table fetchobj
131 does> dup @ swap 1+ @
134 create-symbol quote quote-symbol
135 create-symbol define define-symbol
136 create-symbol set! set!-symbol
137 create-symbol ok ok-symbol
138 create-symbol if if-symbol
142 \ ---- Environments ---- {{{
144 : enclosing-env ( env -- env )
147 : first-frame ( env -- frame )
150 : make-frame ( vars vals -- frame )
153 : frame-vars ( frame -- vars )
156 : frame-vals ( frame -- vals )
159 : add-binding ( var val frame -- )
160 2swap 2over frame-vals cons
162 2swap 2over frame-vars cons
166 : extend-env ( vars vals env -- env )
176 : get-vars-vals-frame ( var frame -- bool )
177 2dup frame-vars vars setobj
178 frame-vals vals setobj
181 vars fetchobj nil objeq? false =
183 2dup vars fetchobj car objeq? if
188 vars fetchobj cdr vars setobj
189 vals fetchobj cdr vals setobj
195 : get-vars-vals ( var env -- vars? vals? bool )
198 2dup nil objeq? false =
200 2over 2over first-frame
201 get-vars-vals-frame if
203 vars fetchobj vals fetchobj true
217 : lookup-var ( var env -- val )
221 bold fg red ." Tried to read unbound variable." reset-term cr abort
225 : set-var ( var val env -- )
226 >R >R 2swap R> R> ( val var env )
228 2swap 2drop ( val vals )
231 bold fg red ." Tried to set unbound variable." reset-term cr abort
237 : define-var ( var val env -- )
240 2over env fetchobj ( var val var env )
242 2swap 2drop ( var val vals )
247 first-frame ( var val frame )
255 nil nil nil extend-env
263 variable stored-parse-idx
264 create parse-str 161 allot
265 variable parse-str-span
267 create parse-idx-stack 10 allot
268 variable parse-idx-sp
269 parse-idx-stack parse-idx-sp !
272 parse-idx @ parse-idx-sp @ !
277 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
281 parse-idx-sp @ @ parse-idx ! ;
285 '\n' parse-str parse-str-span @ + !
286 1 parse-str-span +! ;
293 parse-str 160 expect cr
294 span @ parse-str-span !
304 : charavailable? ( -- bool )
305 parse-str-span @ parse-idx @ > ;
307 : nextchar ( -- char )
308 charavailable? false = if getline then
309 parse-str parse-idx @ + @ ;
311 : whitespace? ( -- bool )
320 nextchar [char] ( = or
321 nextchar [char] ) = or
338 nextchar [char] - = ;
340 : number? ( -- bool )
341 digit? minus? or false = if
362 : boolean? ( -- bool )
363 nextchar [char] # <> if false exit then
370 and if pop-parse-idx false exit then
382 : str-equiv? ( str -- bool )
399 delim? false = if drop false then
404 : character? ( -- bool )
405 nextchar [char] # <> if false exit then
410 nextchar [char] \ <> if pop-parse-idx false exit then
414 S" newline" str-equiv? if pop-parse-idx true exit then
415 S" space" str-equiv? if pop-parse-idx true exit then
416 S" tab" str-equiv? if pop-parse-idx true exit then
418 charavailable? false = if pop-parse-idx false exit then
424 nextchar [char] ( = ;
426 : string? ( -- bool )
427 nextchar [char] " = ;
429 : readnum ( -- num-atom )
437 10 * nextchar [char] 0 - +
446 : readbool ( -- bool-atom )
449 nextchar [char] f = if
460 : readchar ( -- char-atom )
464 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
465 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
466 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
468 nextchar character-type
473 : readstring ( -- charlist )
474 nextchar [char] " = if
479 ." No delimiter following right double quote. Aborting." cr
488 nextchar [char] \ = if
491 [char] n of '\n' endof
492 [char] " of [char] " endof
498 inc-parse-idx character-type
505 : readsymbol ( -- charlist )
506 delim? if nil exit then
508 nextchar inc-parse-idx character-type
515 : charlist-equiv ( charlist charlist -- bool )
524 2drop 2drop true exit
526 2drop 2drop false exit
531 2drop 2drop false exit
538 car drop -rot car drop = if
539 cdr 2swap cdr recurse
545 : charlist>symbol ( charlist -- symbol-obj )
547 symbol-table fetchobj
564 drop symbol-type 2dup
565 symbol-table fetchobj cons
571 : readpair ( -- pairobj )
575 nextchar [char] ) = if
580 ." No delimiter following right paren. Aborting." cr
589 \ Read first pair element
594 nextchar [char] . = if
599 ." No delimiter following '.'. Aborting." cr
613 \ Parse a scheme expression
639 nextchar [char] " <> if
640 bold red ." Missing closing double-quote." reset-term cr
658 nextchar [char] ) <> if
659 bold red ." Missing closing paren." reset-term cr
668 nextchar [char] ' = if
670 quote-symbol recurse nil cons cons exit
674 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
678 \ Anything else is parsed as a symbol
679 readsymbol charlist>symbol
689 : self-evaluating? ( obj -- obj bool )
690 boolean-type istype? if true exit then
691 number-type istype? if true exit then
692 character-type istype? if true exit then
693 string-type istype? if true exit then
694 nil-type istype? if true exit then
699 : tagged-list? ( obj tag-obj -- obj bool )
701 pair-type istype? false = if
707 : quote? ( obj -- obj bool )
708 quote-symbol tagged-list? ;
710 : quote-body ( quote-obj -- quote-body-obj )
713 : variable? ( obj -- obj bool )
714 symbol-type istype? ;
716 : definition? ( obj -- obj bool )
717 define-symbol tagged-list? ;
719 : definition-var ( obj -- var )
722 : definition-val ( obj -- val )
725 : assignment? ( obj -- obj bool )
726 set!-symbol tagged-list? ;
728 : assignment-var ( obj -- var )
731 : assignment-val ( obj -- val )
734 : eval-definition ( obj env -- res )
736 2over 2over ( env obj env obj )
737 definition-val 2swap ( env obj valexp env )
740 2swap definition-var 2swap ( env var val )
748 : eval-assignment ( obj env -- res )
750 2over 2over ( env obj env obj )
751 assignment-val 2swap ( env obj valexp env )
754 2swap assignment-var 2swap ( env var val )
762 : if? ( obj -- obj bool )
763 if-symbol tagged-list? ;
765 : if-predicate ( ifobj -- pred )
768 : if-consequent ( ifobj -- conseq )
771 : if-alternative ( ifobj -- alt|false )
779 : false? ( boolobj -- boolean )
780 boolean-type istype? if
781 false boolean-type objeq?
787 : true? ( boolobj -- boolean )
790 :noname ( obj env -- result )
810 2swap eval-definition
815 2swap eval-assignment
833 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
839 \ ---- Print ---- {{{
841 : printnum ( numobj -- ) drop 0 .R ;
843 : printbool ( numobj -- )
851 : printchar ( charobj -- )
855 bl of ." #\space" endof
856 '\n' of ." #\newline" endof
862 : (printstring) ( stringobj -- )
863 nil-type istype? if 2drop exit then
867 '\n' of ." \n" drop endof
868 [char] \ of ." \\" drop endof
869 [char] " of [char] \ emit [char] " emit drop endof
875 : printstring ( stringobj -- )
880 : printsymbol ( symbolobj -- )
881 nil-type istype? if 2drop exit then
887 : printnil ( nilobj -- )
891 : printpair ( pairobj -- )
895 nil-type istype? if 2drop exit then
896 pair-type istype? if space recurse exit then
901 number-type istype? if printnum exit then
902 boolean-type istype? if printbool exit then
903 character-type istype? if printchar exit then
904 string-type istype? if printstring exit then
905 symbol-type istype? if printsymbol exit then
906 nil-type istype? if printnil exit then
907 pair-type istype? if ." (" printpair ." )" exit then
909 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
918 cr ." Welcome to scheme.forth.jl!" cr
919 ." Use Ctrl-D to exit." cr
924 cr bold fg green ." > " reset-term
926 global-env fetchobj eval
927 fg cyan ." ; " print reset-term