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 : readnum ( -- num-atom )
286 10 * nextchar [char] 0 - +
295 : readbool ( -- bool-atom )
298 nextchar [char] f = if
309 : readchar ( -- char-atom )
313 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
314 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
315 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
317 nextchar character-type
322 : readstring ( -- charlist )
323 nextchar [char] " = if
328 ." No delimiter following right double quote. Aborting." cr
337 nextchar [char] \ = if
340 [char] n of '\n' endof
341 [char] " of [char] " endof
347 inc-parse-idx character-type
354 : readsymbol ( -- charlist )
355 delim? if nil exit then
357 nextchar inc-parse-idx character-type
364 : charlist-equiv ( charlist charlist -- bool )
373 2drop 2drop true exit
375 2drop 2drop false exit
380 2drop 2drop false exit
387 car drop -rot car drop = if
388 cdr 2swap cdr recurse
394 : charlist>symbol ( charlist -- symbol-obj )
396 symbol-table fetchobj
413 drop symbol-type 2dup
414 symbol-table fetchobj cons
420 : readpair ( -- pairobj )
424 nextchar [char] ) = if
429 ." No delimiter following right paren. Aborting." cr
438 \ Read first pair element
443 nextchar [char] . = if
448 ." No delimiter following '.'. Aborting." cr
462 \ Parse a scheme expression
487 nextchar [char] " <> if
488 bold red ." Missing closing double-quote." reset-term cr
505 nextchar [char] ) <> if
506 bold red ." Missing closing paren." reset-term cr
515 nextchar [char] ' = if
517 quote recurse nil cons cons exit
521 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
525 \ Anything else is assumed to be a symbol
526 readsymbol charlist>symbol
532 : self-evaluating? ( obj -- obj bool )
533 boolean-type istype? if true exit then
534 number-type istype? if true exit then
535 character-type istype? if true exit then
536 string-type istype? if true exit then
537 nil-type istype? if true exit then
542 : tagged-list? ( obj tag-obj -- obj bool )
544 pair-type istype? false = if
550 : quote? ( obj -- obj bool )
553 : quote-body ( quote-obj -- quote-body-obj )
566 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
572 : printnum ( numobj -- ) drop 0 .R ;
574 : printbool ( numobj -- )
582 : printchar ( charobj -- )
586 bl of ." #\space" endof
587 '\n' of ." #\newline" endof
593 : (printstring) ( stringobj -- )
594 nil-type istype? if 2drop exit then
598 '\n' of ." \n" drop endof
599 [char] \ of ." \\" drop endof
600 [char] " of [char] \ emit [char] " emit drop endof
606 : printstring ( stringobj -- )
611 : printsymbol ( symbolobj -- )
612 nil-type istype? if 2drop exit then
618 : printnil ( nilobj -- )
622 : printpair ( pairobj -- )
626 nil-type istype? if 2drop exit then
627 pair-type istype? if space recurse exit then
632 number-type istype? if printnum exit then
633 boolean-type istype? if printbool exit then
634 character-type istype? if printchar exit then
635 string-type istype? if printstring exit then
636 symbol-type istype? if printsymbol exit then
637 nil-type istype? if printnil exit then
638 pair-type istype? if ." (" printpair ." )" exit then
640 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
647 cr ." Welcome to scheme.forth.jl!" cr
648 ." Use Ctrl-D to exit." cr
653 cr bold fg green ." > " reset-term
656 fg cyan ." ; " print reset-term