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
59 : nil? nil-type istype? ;
61 : objvar create 0 , 0 , ;
63 : value@ ( objvar -- val ) @ ;
64 : type@ ( objvar -- type ) 1+ @ ;
65 : value! ( newval objvar -- ) ! ;
66 : type! ( newtype objvar -- ) 1+ ! ;
67 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
68 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
71 nil symbol-table setobj
73 : objeq? ( obj obj -- bool )
76 \ ---- Pre-defined symbols ----
78 : (create-symbol) ( addr n -- symbol-obj )
82 2dup drop @ character-type 2swap
90 : create-symbol ( -- )
104 does> dup @ swap 1+ @
107 create-symbol quote quote
112 variable stored-parse-idx
113 create parse-str 161 allot
114 variable parse-str-span
116 create parse-idx-stack 10 allot
117 variable parse-idx-sp
118 parse-idx-stack parse-idx-sp !
121 parse-idx @ parse-idx-sp @ !
126 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
130 parse-idx-sp @ @ parse-idx ! ;
134 '\n' parse-str parse-str-span @ + !
135 1 parse-str-span +! ;
142 parse-str 160 expect cr
143 span @ parse-str-span !
153 : charavailable? ( -- bool )
154 parse-str-span @ parse-idx @ > ;
156 : nextchar ( -- char )
157 charavailable? false = if getline then
158 parse-str parse-idx @ + @ ;
160 : whitespace? ( -- bool )
169 nextchar [char] ( = or
170 nextchar [char] ) = or
187 nextchar [char] - = ;
189 : number? ( -- bool )
190 digit? minus? or false = if
211 : boolean? ( -- bool )
212 nextchar [char] # <> if false exit then
219 and if pop-parse-idx false exit then
231 : str-equiv? ( str -- bool )
248 delim? false = if drop false then
253 : character? ( -- bool )
254 nextchar [char] # <> if false exit then
259 nextchar [char] \ <> if pop-parse-idx false exit then
263 S" newline" str-equiv? if pop-parse-idx true exit then
264 S" space" str-equiv? if pop-parse-idx true exit then
265 S" tab" str-equiv? if pop-parse-idx true exit then
267 charavailable? false = if pop-parse-idx false exit then
273 nextchar [char] ( = ;
275 : string? ( -- bool )
276 nextchar [char] " = ;
278 : initial? ( -- bool )
279 nextchar [char] A >= nextchar [char] Z <= and if true exit then
280 nextchar [char] a >= nextchar [char] z <= and if true exit then
281 nextchar [char] * = if true exit then
282 nextchar [char] / = if true exit then
283 nextchar [char] > = if true exit then
284 nextchar [char] < = if true exit then
285 nextchar [char] = = if true exit then
286 nextchar [char] ? = if true exit then
287 nextchar [char] ! = if true exit then
291 : symbol? ( -- bool )
292 initial? if true exit then
294 nextchar [char] - = or if
296 delim? if dec-parse-idx true exit then
302 : readnum ( -- num-atom )
310 10 * nextchar [char] 0 - +
319 : readbool ( -- bool-atom )
322 nextchar [char] f = if
333 : readchar ( -- char-atom )
337 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
338 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
339 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
341 nextchar character-type
346 : readstring ( -- charlist )
347 nextchar [char] " = if
352 ." No delimiter following right double quote. Aborting." cr
361 nextchar [char] \ = if
364 [char] n of '\n' endof
365 [char] " of [char] " endof
371 inc-parse-idx character-type
378 : readsymbol ( -- charlist )
379 delim? if nil exit then
381 nextchar inc-parse-idx character-type
388 : charlist-equiv ( charlist charlist -- bool )
397 2drop 2drop true exit
399 2drop 2drop false exit
404 2drop 2drop false exit
411 car drop -rot car drop = if
412 cdr 2swap cdr recurse
418 : charlist>symbol ( charlist -- symbol-obj )
420 symbol-table fetchobj
437 drop symbol-type 2dup
438 symbol-table fetchobj cons
444 : readpair ( -- pairobj )
448 nextchar [char] ) = if
453 ." No delimiter following right paren. Aborting." cr
462 \ Read first pair element
467 nextchar [char] . = if
472 ." No delimiter following '.'. Aborting." cr
486 \ Parse a scheme expression
511 nextchar [char] " <> if
512 bold red ." Missing closing double-quote." reset-term cr
521 readsymbol charlist>symbol
534 nextchar [char] ) <> if
535 bold red ." Missing closing paren." reset-term cr
544 nextchar [char] ' = if
546 quote recurse nil cons cons exit
550 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
554 bold fg red ." Error parsing string starting at character '"
556 ." '. Aborting." reset-term cr
563 : self-evaluating? ( obj -- obj bool )
564 boolean-type istype? if true exit then
565 number-type istype? if true exit then
566 character-type istype? if true exit then
567 string-type istype? if true exit then
568 nil-type istype? if true exit then
573 : tagged-list? ( obj tag-obj -- obj bool )
575 pair-type istype? false = if
581 : quote? ( obj -- obj bool )
584 : quote-body ( quote-obj -- quote-body-obj )
597 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
603 : printnum ( numobj -- ) drop 0 .R ;
605 : printbool ( numobj -- )
613 : printchar ( charobj -- )
617 bl of ." #\space" endof
618 '\n' of ." #\newline" endof
624 : (printstring) ( stringobj -- )
625 nil-type istype? if 2drop exit then
629 '\n' of ." \n" drop endof
630 [char] \ of ." \\" drop endof
631 [char] " of [char] \ emit [char] " emit drop endof
637 : printstring ( stringobj -- )
642 : printsymbol ( symbolobj -- )
643 nil-type istype? if 2drop exit then
649 : printnil ( nilobj -- )
653 : printpair ( pairobj -- )
657 nil-type istype? if 2drop exit then
658 pair-type istype? if space recurse exit then
663 number-type istype? if printnum exit then
664 boolean-type istype? if printbool exit then
665 character-type istype? if printchar exit then
666 string-type istype? if printstring exit then
667 symbol-type istype? if printsymbol exit then
668 nil-type istype? if printnil exit then
669 pair-type istype? if ." (" printpair ." )" exit then
671 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
678 cr ." Welcome to scheme.forth.jl!" cr
679 ." Use Ctrl-D to exit." cr
684 cr bold fg green ." > " reset-term
687 fg cyan ." ; " print reset-term