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 -- obj b )
19 \ ------ 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
55 : objvar create 0 , 0 , ;
57 : value@ ( objvar -- val ) @ ;
58 : type@ ( objvar -- type ) 1+ @ ;
59 : value! ( newval objvar -- ) ! ;
60 : type! ( newtype objvar -- ) 1+ ! ;
61 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
62 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
65 nil symbol-table setobj
70 variable stored-parse-idx
71 create parse-str 161 allot
72 variable parse-str-span
74 create parse-idx-stack 10 allot
76 parse-idx-stack parse-idx-sp !
79 parse-idx @ parse-idx-sp @ !
84 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
88 parse-idx-sp @ @ parse-idx ! ;
92 '\n' parse-str parse-str-span @ + !
100 parse-str 160 expect cr
101 span @ parse-str-span !
111 : charavailable? ( -- bool )
112 parse-str-span @ parse-idx @ > ;
114 : nextchar ( -- char )
115 charavailable? false = if getline then
116 parse-str parse-idx @ + @ ;
118 : whitespace? ( -- bool )
127 nextchar [char] ( = or
128 nextchar [char] ) = or
145 nextchar [char] - = ;
147 : number? ( -- bool )
148 digit? minus? or false = if
169 : boolean? ( -- bool )
170 nextchar [char] # <> if false exit then
177 and if pop-parse-idx false exit then
189 : str-equiv? ( str -- bool )
206 delim? false = if drop false then
211 : character? ( -- bool )
212 nextchar [char] # <> if false exit then
217 nextchar [char] \ <> if pop-parse-idx false exit then
221 S" newline" str-equiv? if pop-parse-idx true exit then
222 S" space" str-equiv? if pop-parse-idx true exit then
223 S" tab" str-equiv? if pop-parse-idx true exit then
225 charavailable? false = if pop-parse-idx false exit then
231 nextchar [char] ( = ;
233 : string? ( -- bool )
234 nextchar [char] " = ;
236 : initial? ( -- bool )
237 nextchar [char] A >= nextchar [char] Z <= and if true exit then
238 nextchar [char] a >= nextchar [char] z <= and if true exit then
239 nextchar [char] * = if true exit then
240 nextchar [char] / = if true exit then
241 nextchar [char] > = if true exit then
242 nextchar [char] < = if true exit then
243 nextchar [char] = = if true exit then
244 nextchar [char] ? = if true exit then
245 nextchar [char] ! = if true exit then
249 : symbol? ( -- bool )
250 initial? if true exit then
252 nextchar [char] - = or if
254 delim? if dec-parse-idx true exit then
260 : readnum ( -- num-atom )
268 10 * nextchar [char] 0 - +
277 : readbool ( -- bool-atom )
280 nextchar [char] f = if
291 : readchar ( -- char-atom )
295 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
296 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
297 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
299 nextchar character-type
304 : readstring ( -- charlist )
305 nextchar [char] " = if
310 ." No delimiter following right double quote. Aborting." cr
319 nextchar [char] \ = if
322 [char] n of '\n' endof
323 [char] " of [char] " endof
329 inc-parse-idx character-type
336 : readsymbol ( -- charlist )
337 delim? if nil exit then
339 nextchar inc-parse-idx character-type
348 : readpair ( -- obj )
352 nextchar [char] ) = if
357 ." No delimiter following right paren. Aborting." cr
366 \ Read first pair element
371 nextchar [char] . = if
376 ." No delimiter following '.'. Aborting." cr
390 \ Parse a scheme expression
415 nextchar [char] " <> if
416 bold red ." Missing closing double-quote." reset-term cr
443 nextchar [char] ) <> if
444 bold red ." Missing closing paren." reset-term cr
454 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
458 bold fg red ." Error parsing string starting at character '"
460 ." '. Aborting." reset-term cr
467 : self-evaluating? ( obj -- obj bool )
468 true \ everything self-evaluating for now
476 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
482 : printnum ( numobj -- ) drop 0 .R ;
484 : printbool ( numobj -- )
492 : printchar ( charobj -- )
496 bl of ." #\space" endof
497 '\n' of ." #\newline" endof
503 : (printstring) ( stringobj -- )
504 nil-type istype? if 2drop exit then
508 '\n' of ." \n" drop endof
509 [char] \ of ." \\" drop endof
510 [char] " of [char] \ emit [char] " emit drop endof
516 : printstring ( stringobj -- )
521 : printsymbol ( symbolobj -- )
522 nil-type istype? if 2drop exit then
528 : printnil ( nilobj -- )
532 : printpair ( pairobj -- )
536 nil-type istype? if 2drop exit then
537 pair-type istype? if space recurse exit then
542 number-type istype? if printnum exit then
543 boolean-type istype? if printbool exit then
544 character-type istype? if printchar exit then
545 string-type istype? if printstring exit then
546 symbol-type istype? if printsymbol exit then
547 nil-type istype? if printnil exit then
548 pair-type istype? if ." (" printpair ." )" exit then
550 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
557 cr ." Welcome to scheme.forth.jl!" cr
558 ." Use Ctrl-D to exit." cr
563 cr bold fg green ." > " reset-term
566 fg cyan ." ; " print reset-term