6 include term-colours.4th
9 1 constant boolean-type
10 2 constant character-type
11 : istype? ( obj -- obj b )
17 variable stored-parse-idx
18 create parse-str 161 allot
19 variable parse-str-span
21 create parse-idx-stack 10 allot
23 parse-idx-stack parse-idx-sp !
26 parse-idx @ parse-idx-sp @ !
31 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
35 parse-idx-sp @ @ parse-idx ! ;
39 '\n' parse-str parse-str-span @ + !
47 parse-str 160 expect cr
48 span @ parse-str-span !
58 : charavailable? ( -- bool )
59 parse-str-span @ parse-idx @ > ;
61 : nextchar ( -- char )
62 charavailable? false = if getline then
63 parse-str parse-idx @ + @ ;
65 : whitespace? ( -- bool )
74 nextchar [char] ( = or
75 nextchar [char] ) = or
95 digit? minus? or false = if
116 : boolean? ( -- bool )
117 nextchar [char] # <> if false exit then
124 and if pop-parse-idx false exit then
136 : str-equiv? ( str -- bool )
153 delim? false = if drop false then
158 : character? ( -- bool )
159 nextchar [char] # <> if false exit then
164 nextchar [char] \ <> if pop-parse-idx false exit then
168 S" newline" str-equiv? if true exit then
169 S" space" str-equiv? if true exit then
170 S" tab" str-equiv? if true exit then
172 charavailable? false = if pop-parse-idx false exit then
177 : readnum ( -- num-atom )
185 10 * nextchar [char] 0 - +
194 : readbool ( -- bool-atom )
197 nextchar [char] f = if
208 : readchar ( -- char-atom )
212 S" newline" str-equiv? if '\n' character-type exit then
213 S" space" str-equiv? if bl character-type exit then
214 S" tab" str-equiv? if 9 character-type exit then
216 nextchar character-type
221 \ Parse a scheme expression
242 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
246 bold fg red ." Error parsing string starting at character '"
248 ." '. Aborting." reset-term cr
254 : self-evaluating? ( obj -- obj bool )
255 number-type istype? if true exit then
256 boolean-type istype? if true exit then
257 character-type istype? if true exit then
265 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
271 : printnum ( numobj -- ) drop . ;
273 : printbool ( numobj -- )
281 : printchar ( charobj -- )
285 bl of ." #\space" endof
286 '\n' of ." #\newline" endof
296 number-type istype? if printnum exit then
297 boolean-type istype? if printbool exit then
298 character-type istype? if printchar exit then
304 cr ." Welcome to scheme.forth.jl!" cr
305 ." Use Ctrl-D to exit." cr
310 cr bold fg green ." > " reset-term
313 fg cyan print reset-term