4 include term-colours.4th
7 1 constant boolean-type
8 2 constant character-type
10 : istype? ( obj -- obj b )
16 variable stored-parse-idx
17 create parse-str 161 allot
18 variable parse-str-span
20 create parse-idx-stack 10 allot
22 parse-idx-stack parse-idx-sp !
25 parse-idx @ parse-idx-sp @ !
30 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
34 parse-idx-sp @ @ parse-idx ! ;
38 '\n' parse-str parse-str-span @ + !
46 parse-str 160 expect cr
47 span @ parse-str-span !
57 : charavailable? ( -- bool )
58 parse-str-span @ parse-idx @ > ;
60 : nextchar ( -- char )
61 charavailable? false = if getline then
62 parse-str parse-idx @ + @ ;
64 : whitespace? ( -- bool )
73 nextchar [char] ( = or
74 nextchar [char] ) = or
94 digit? minus? or false = if
115 : boolean? ( -- bool )
116 nextchar [char] # <> if false exit then
123 and if pop-parse-idx false exit then
135 : str-equiv? ( str -- bool )
152 delim? false = if drop false then
157 : character? ( -- bool )
158 nextchar [char] # <> if false exit then
163 nextchar [char] \ <> if pop-parse-idx false exit then
167 S" newline" str-equiv? if pop-parse-idx true exit then
168 S" space" str-equiv? if pop-parse-idx true exit then
169 S" tab" str-equiv? if pop-parse-idx true exit then
171 charavailable? false = if pop-parse-idx false exit then
176 : empty-list? ( -- bool )
177 nextchar [char] ( <> if false exit then
181 nextchar [char] ) <> if pop-parse-idx false exit then
185 : readnum ( -- num-atom )
193 10 * nextchar [char] 0 - +
202 : readbool ( -- bool-atom )
205 nextchar [char] f = if
216 : readchar ( -- char-atom )
220 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
221 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
222 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
224 nextchar character-type
229 : readnil ( -- nil-atom )
237 \ Parse a scheme expression
263 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
267 bold fg red ." Error parsing string starting at character '"
269 ." '. Aborting." reset-term cr
275 : self-evaluating? ( obj -- obj bool )
276 number-type istype? if true exit then
277 boolean-type istype? if true exit then
278 character-type istype? if true exit then
279 nil-type istype? if true exit then
287 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
293 : printnum ( numobj -- ) drop . ;
295 : printbool ( numobj -- )
303 : printchar ( charobj -- )
307 bl of ." #\space" endof
308 '\n' of ." #\newline" endof
314 : printnil ( nilobj -- )
319 number-type istype? if printnum exit then
320 boolean-type istype? if printbool exit then
321 character-type istype? if printchar exit then
322 nil-type istype? if printnil exit then
328 cr ." Welcome to scheme.forth.jl!" cr
329 ." Use Ctrl-D to exit." cr
334 cr bold fg green ." > " reset-term
337 fg cyan print reset-term