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 \ ------ 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
54 : nil? nil-type istype? ;
56 : objvar create 0 , 0 , ;
58 : value@ ( objvar -- val ) @ ;
59 : type@ ( objvar -- type ) 1+ @ ;
60 : value! ( newval objvar -- ) ! ;
61 : type! ( newtype objvar -- ) 1+ ! ;
62 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
63 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
66 nil symbol-table setobj
71 variable stored-parse-idx
72 create parse-str 161 allot
73 variable parse-str-span
75 create parse-idx-stack 10 allot
77 parse-idx-stack parse-idx-sp !
80 parse-idx @ parse-idx-sp @ !
85 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
89 parse-idx-sp @ @ parse-idx ! ;
93 '\n' parse-str parse-str-span @ + !
101 parse-str 160 expect cr
102 span @ parse-str-span !
112 : charavailable? ( -- bool )
113 parse-str-span @ parse-idx @ > ;
115 : nextchar ( -- char )
116 charavailable? false = if getline then
117 parse-str parse-idx @ + @ ;
119 : whitespace? ( -- bool )
128 nextchar [char] ( = or
129 nextchar [char] ) = or
146 nextchar [char] - = ;
148 : number? ( -- bool )
149 digit? minus? or false = if
170 : boolean? ( -- bool )
171 nextchar [char] # <> if false exit then
178 and if pop-parse-idx false exit then
190 : str-equiv? ( str -- bool )
207 delim? false = if drop false then
212 : character? ( -- bool )
213 nextchar [char] # <> if false exit then
218 nextchar [char] \ <> if pop-parse-idx false exit then
222 S" newline" str-equiv? if pop-parse-idx true exit then
223 S" space" str-equiv? if pop-parse-idx true exit then
224 S" tab" str-equiv? if pop-parse-idx true exit then
226 charavailable? false = if pop-parse-idx false exit then
232 nextchar [char] ( = ;
234 : string? ( -- bool )
235 nextchar [char] " = ;
237 : initial? ( -- bool )
238 nextchar [char] A >= nextchar [char] Z <= and if true exit then
239 nextchar [char] a >= nextchar [char] z <= and 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
246 nextchar [char] ! = if true exit then
250 : symbol? ( -- bool )
251 initial? if true exit then
253 nextchar [char] - = or if
255 delim? if dec-parse-idx true exit then
261 : readnum ( -- num-atom )
269 10 * nextchar [char] 0 - +
278 : readbool ( -- bool-atom )
281 nextchar [char] f = if
292 : readchar ( -- char-atom )
296 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
297 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
298 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
300 nextchar character-type
305 : readstring ( -- charlist )
306 nextchar [char] " = if
311 ." No delimiter following right double quote. Aborting." cr
320 nextchar [char] \ = if
323 [char] n of '\n' endof
324 [char] " of [char] " endof
330 inc-parse-idx character-type
337 : readsymbol ( -- charlist )
338 delim? if nil exit then
340 nextchar inc-parse-idx character-type
347 : charlist-equiv ( charlist charlist -- bool )
356 2drop 2drop true exit
358 2drop 2drop false exit
363 2drop 2drop false exit
370 car drop -rot car drop = if
371 cdr 2swap cdr recurse
377 : charlist>symbol ( charlist -- symbol-obj )
379 symbol-table fetchobj
396 drop symbol-type 2dup
397 symbol-table fetchobj cons
403 : readpair ( -- pairobj )
407 nextchar [char] ) = if
412 ." No delimiter following right paren. Aborting." cr
421 \ Read first pair element
426 nextchar [char] . = if
431 ." No delimiter following '.'. Aborting." cr
445 \ Parse a scheme expression
470 nextchar [char] " <> if
471 bold red ." Missing closing double-quote." reset-term cr
480 readsymbol charlist>symbol
493 nextchar [char] ) <> if
494 bold red ." Missing closing paren." reset-term cr
504 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
508 bold fg red ." Error parsing string starting at character '"
510 ." '. Aborting." reset-term cr
517 : self-evaluating? ( obj -- obj bool )
518 true \ everything self-evaluating for now
526 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
532 : printnum ( numobj -- ) drop 0 .R ;
534 : printbool ( numobj -- )
542 : printchar ( charobj -- )
546 bl of ." #\space" endof
547 '\n' of ." #\newline" endof
553 : (printstring) ( stringobj -- )
554 nil-type istype? if 2drop exit then
558 '\n' of ." \n" drop endof
559 [char] \ of ." \\" drop endof
560 [char] " of [char] \ emit [char] " emit drop endof
566 : printstring ( stringobj -- )
571 : printsymbol ( symbolobj -- )
572 nil-type istype? if 2drop exit then
578 : printnil ( nilobj -- )
582 : printpair ( pairobj -- )
586 nil-type istype? if 2drop exit then
587 pair-type istype? if space recurse exit then
592 number-type istype? if printnum exit then
593 boolean-type istype? if printbool exit then
594 character-type istype? if printchar exit then
595 string-type istype? if printstring exit then
596 symbol-type istype? if printsymbol exit then
597 nil-type istype? if printnil exit then
598 pair-type istype? if ." (" printpair ." )" exit then
600 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
607 cr ." Welcome to scheme.forth.jl!" cr
608 ." Use Ctrl-D to exit." cr
613 cr bold fg green ." > " reset-term
616 fg cyan ." ; " print reset-term