4 include term-colours.4th
7 1 constant boolean-type
8 2 constant character-type
9 : istype? ( obj -- obj b )
15 variable stored-parse-idx
16 create parse-str 161 allot
17 variable parse-str-span
19 create parse-idx-stack 10 allot
21 parse-idx-stack parse-idx-sp !
24 parse-idx @ parse-idx-sp @ !
29 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
33 parse-idx-sp @ @ parse-idx ! ;
37 '\n' parse-str parse-str-span @ + !
45 parse-str 160 expect cr
46 span @ parse-str-span !
56 : charavailable? ( -- bool )
57 parse-str-span @ parse-idx @ > ;
59 : nextchar ( -- char )
60 charavailable? false = if getline then
61 parse-str parse-idx @ + @ ;
63 : whitespace? ( -- bool )
72 nextchar [char] ( = or
73 nextchar [char] ) = or
93 digit? minus? or false = if
114 : boolean? ( -- bool )
115 nextchar [char] # <> if false exit then
122 and if pop-parse-idx false exit then
134 : str-equiv? ( str -- bool )
151 delim? false = if drop false then
156 : character? ( -- bool )
157 nextchar [char] # <> if false exit then
162 nextchar [char] \ <> if pop-parse-idx false exit then
166 S" newline" str-equiv? if pop-parse-idx true exit then
167 S" space" str-equiv? if pop-parse-idx true exit then
168 S" tab" str-equiv? if pop-parse-idx true exit then
170 charavailable? false = if pop-parse-idx false exit then
175 : readnum ( -- num-atom )
183 10 * nextchar [char] 0 - +
192 : readbool ( -- bool-atom )
195 nextchar [char] f = if
206 : readchar ( -- char-atom )
210 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
211 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
212 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
214 nextchar character-type
219 \ Parse a scheme expression
240 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
244 bold fg red ." Error parsing string starting at character '"
246 ." '. Aborting." reset-term cr
252 : self-evaluating? ( obj -- obj bool )
253 number-type istype? if true exit then
254 boolean-type istype? if true exit then
255 character-type istype? if true exit then
263 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
269 : printnum ( numobj -- ) drop . ;
271 : printbool ( numobj -- )
279 : printchar ( charobj -- )
283 bl of ." #\space" endof
284 '\n' of ." #\newline" endof
292 number-type istype? if printnum exit then
293 boolean-type istype? if printbool exit then
294 character-type istype? if printchar exit then
300 cr ." Welcome to scheme.forth.jl!" cr
301 ." Use Ctrl-D to exit." cr
306 cr bold fg green ." > " reset-term
309 fg cyan print reset-term