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
68 \ ---- Pre-defined symbols ----
70 : (create-symbol) ( addr n -- symbol-obj )
74 2dup drop @ character-type 2swap
82 : create-symbol ( -- )
98 variable stored-parse-idx
99 create parse-str 161 allot
100 variable parse-str-span
102 create parse-idx-stack 10 allot
103 variable parse-idx-sp
104 parse-idx-stack parse-idx-sp !
107 parse-idx @ parse-idx-sp @ !
112 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
116 parse-idx-sp @ @ parse-idx ! ;
120 '\n' parse-str parse-str-span @ + !
121 1 parse-str-span +! ;
128 parse-str 160 expect cr
129 span @ parse-str-span !
139 : charavailable? ( -- bool )
140 parse-str-span @ parse-idx @ > ;
142 : nextchar ( -- char )
143 charavailable? false = if getline then
144 parse-str parse-idx @ + @ ;
146 : whitespace? ( -- bool )
155 nextchar [char] ( = or
156 nextchar [char] ) = or
173 nextchar [char] - = ;
175 : number? ( -- bool )
176 digit? minus? or false = if
197 : boolean? ( -- bool )
198 nextchar [char] # <> if false exit then
205 and if pop-parse-idx false exit then
217 : str-equiv? ( str -- bool )
234 delim? false = if drop false then
239 : character? ( -- bool )
240 nextchar [char] # <> if false exit then
245 nextchar [char] \ <> if pop-parse-idx false exit then
249 S" newline" str-equiv? if pop-parse-idx true exit then
250 S" space" str-equiv? if pop-parse-idx true exit then
251 S" tab" str-equiv? if pop-parse-idx true exit then
253 charavailable? false = if pop-parse-idx false exit then
259 nextchar [char] ( = ;
261 : string? ( -- bool )
262 nextchar [char] " = ;
264 : initial? ( -- bool )
265 nextchar [char] A >= nextchar [char] Z <= and if true exit then
266 nextchar [char] a >= nextchar [char] z <= and if true exit then
267 nextchar [char] * = if true exit then
268 nextchar [char] / = if true exit then
269 nextchar [char] > = if true exit then
270 nextchar [char] < = if true exit then
271 nextchar [char] = = if true exit then
272 nextchar [char] ? = if true exit then
273 nextchar [char] ! = if true exit then
277 : symbol? ( -- bool )
278 initial? if true exit then
280 nextchar [char] - = or if
282 delim? if dec-parse-idx true exit then
288 : readnum ( -- num-atom )
296 10 * nextchar [char] 0 - +
305 : readbool ( -- bool-atom )
308 nextchar [char] f = if
319 : readchar ( -- char-atom )
323 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
324 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
325 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
327 nextchar character-type
332 : readstring ( -- charlist )
333 nextchar [char] " = if
338 ." No delimiter following right double quote. Aborting." cr
347 nextchar [char] \ = if
350 [char] n of '\n' endof
351 [char] " of [char] " endof
357 inc-parse-idx character-type
364 : readsymbol ( -- charlist )
365 delim? if nil exit then
367 nextchar inc-parse-idx character-type
374 : charlist-equiv ( charlist charlist -- bool )
383 2drop 2drop true exit
385 2drop 2drop false exit
390 2drop 2drop false exit
397 car drop -rot car drop = if
398 cdr 2swap cdr recurse
404 : charlist>symbol ( charlist -- symbol-obj )
406 symbol-table fetchobj
423 drop symbol-type 2dup
424 symbol-table fetchobj cons
430 : readpair ( -- pairobj )
434 nextchar [char] ) = if
439 ." No delimiter following right paren. Aborting." cr
448 \ Read first pair element
453 nextchar [char] . = if
458 ." No delimiter following '.'. Aborting." cr
472 \ Parse a scheme expression
497 nextchar [char] " <> if
498 bold red ." Missing closing double-quote." reset-term cr
507 readsymbol charlist>symbol
520 nextchar [char] ) <> if
521 bold red ." Missing closing paren." reset-term cr
531 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
535 bold fg red ." Error parsing string starting at character '"
537 ." '. Aborting." reset-term cr
544 : self-evaluating? ( obj -- obj bool )
545 true \ everything self-evaluating for now
553 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
559 : printnum ( numobj -- ) drop 0 .R ;
561 : printbool ( numobj -- )
569 : printchar ( charobj -- )
573 bl of ." #\space" endof
574 '\n' of ." #\newline" endof
580 : (printstring) ( stringobj -- )
581 nil-type istype? if 2drop exit then
585 '\n' of ." \n" drop endof
586 [char] \ of ." \\" drop endof
587 [char] " of [char] \ emit [char] " emit drop endof
593 : printstring ( stringobj -- )
598 : printsymbol ( symbolobj -- )
599 nil-type istype? if 2drop exit then
605 : printnil ( nilobj -- )
609 : printpair ( pairobj -- )
613 nil-type istype? if 2drop exit then
614 pair-type istype? if space recurse exit then
619 number-type istype? if printnum exit then
620 boolean-type istype? if printbool exit then
621 character-type istype? if printchar exit then
622 string-type istype? if printstring exit then
623 symbol-type istype? if printsymbol exit then
624 nil-type istype? if printnil exit then
625 pair-type istype? if ." (" printpair ." )" exit then
627 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
634 cr ." Welcome to scheme.forth.jl!" cr
635 ." Use Ctrl-D to exit." cr
640 cr bold fg green ." > " reset-term
643 fg cyan ." ; " print reset-term