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 \ ------ Cons cell 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
53 : set-car! ( obj pair-obj -- )
55 rot swap car-type-cells + !
59 : set-cdr! ( obj pair-obj -- )
61 rot swap cdr-type-cells + !
71 : nil? nil-type istype? ;
73 : objvar create nil swap , , ;
75 : value@ ( objvar -- val ) @ ;
76 : type@ ( objvar -- type ) 1+ @ ;
77 : value! ( newval objvar -- ) ! ;
78 : type! ( newtype objvar -- ) 1+ ! ;
79 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
80 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
82 : objeq? ( obj obj -- bool )
85 \ ---- Pre-defined symbols ----
89 : (create-symbol) ( addr n -- symbol-obj )
93 2dup drop @ character-type 2swap
101 : create-symbol ( -- )
110 symbol-table fetchobj
115 does> dup @ swap 1+ @
118 create-symbol quote quote-symbol
119 create-symbol define define-symbol
120 create-symbol set! set!-symbol
122 \ ---- Environments ----
124 objvar global-environment
126 : enclosing-env ( env -- env )
129 : first-frame ( env -- frame )
132 : make-frame ( vars vals -- frame )
135 : frame-vars ( frame -- vars )
138 : frame-vals ( frame -- vals )
141 : add-binding ( var val frame -- )
148 variable stored-parse-idx
149 create parse-str 161 allot
150 variable parse-str-span
152 create parse-idx-stack 10 allot
153 variable parse-idx-sp
154 parse-idx-stack parse-idx-sp !
157 parse-idx @ parse-idx-sp @ !
162 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
166 parse-idx-sp @ @ parse-idx ! ;
170 '\n' parse-str parse-str-span @ + !
171 1 parse-str-span +! ;
178 parse-str 160 expect cr
179 span @ parse-str-span !
189 : charavailable? ( -- bool )
190 parse-str-span @ parse-idx @ > ;
192 : nextchar ( -- char )
193 charavailable? false = if getline then
194 parse-str parse-idx @ + @ ;
196 : whitespace? ( -- bool )
205 nextchar [char] ( = or
206 nextchar [char] ) = or
223 nextchar [char] - = ;
225 : number? ( -- bool )
226 digit? minus? or false = if
247 : boolean? ( -- bool )
248 nextchar [char] # <> if false exit then
255 and if pop-parse-idx false exit then
267 : str-equiv? ( str -- bool )
284 delim? false = if drop false then
289 : character? ( -- bool )
290 nextchar [char] # <> if false exit then
295 nextchar [char] \ <> if pop-parse-idx false exit then
299 S" newline" str-equiv? if pop-parse-idx true exit then
300 S" space" str-equiv? if pop-parse-idx true exit then
301 S" tab" str-equiv? if pop-parse-idx true exit then
303 charavailable? false = if pop-parse-idx false exit then
309 nextchar [char] ( = ;
311 : string? ( -- bool )
312 nextchar [char] " = ;
314 : readnum ( -- num-atom )
322 10 * nextchar [char] 0 - +
331 : readbool ( -- bool-atom )
334 nextchar [char] f = if
345 : readchar ( -- char-atom )
349 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
350 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
351 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
353 nextchar character-type
358 : readstring ( -- charlist )
359 nextchar [char] " = if
364 ." No delimiter following right double quote. Aborting." cr
373 nextchar [char] \ = if
376 [char] n of '\n' endof
377 [char] " of [char] " endof
383 inc-parse-idx character-type
390 : readsymbol ( -- charlist )
391 delim? if nil exit then
393 nextchar inc-parse-idx character-type
400 : charlist-equiv ( charlist charlist -- bool )
409 2drop 2drop true exit
411 2drop 2drop false exit
416 2drop 2drop false exit
423 car drop -rot car drop = if
424 cdr 2swap cdr recurse
430 : charlist>symbol ( charlist -- symbol-obj )
432 symbol-table fetchobj
449 drop symbol-type 2dup
450 symbol-table fetchobj cons
456 : readpair ( -- pairobj )
460 nextchar [char] ) = if
465 ." No delimiter following right paren. Aborting." cr
474 \ Read first pair element
479 nextchar [char] . = if
484 ." No delimiter following '.'. Aborting." cr
498 \ Parse a scheme expression
523 nextchar [char] " <> if
524 bold red ." Missing closing double-quote." reset-term cr
541 nextchar [char] ) <> if
542 bold red ." Missing closing paren." reset-term cr
551 nextchar [char] ' = if
553 quote-symbol recurse nil cons cons exit
557 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
561 \ Anything else is assumed to be a symbol
562 readsymbol charlist>symbol
568 : self-evaluating? ( obj -- obj bool )
569 boolean-type istype? if true exit then
570 number-type istype? if true exit then
571 character-type istype? if true exit then
572 string-type istype? if true exit then
573 nil-type istype? if true exit then
578 : tagged-list? ( obj tag-obj -- obj bool )
580 pair-type istype? false = if
586 : quote? ( obj -- obj bool )
587 quote-symbol tagged-list? ;
589 : quote-body ( quote-obj -- quote-body-obj )
602 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
608 : printnum ( numobj -- ) drop 0 .R ;
610 : printbool ( numobj -- )
618 : printchar ( charobj -- )
622 bl of ." #\space" endof
623 '\n' of ." #\newline" endof
629 : (printstring) ( stringobj -- )
630 nil-type istype? if 2drop exit then
634 '\n' of ." \n" drop endof
635 [char] \ of ." \\" drop endof
636 [char] " of [char] \ emit [char] " emit drop endof
642 : printstring ( stringobj -- )
647 : printsymbol ( symbolobj -- )
648 nil-type istype? if 2drop exit then
654 : printnil ( nilobj -- )
658 : printpair ( pairobj -- )
662 nil-type istype? if 2drop exit then
663 pair-type istype? if space recurse exit then
668 number-type istype? if printnum exit then
669 boolean-type istype? if printbool exit then
670 character-type istype? if printchar exit then
671 string-type istype? if printstring exit then
672 symbol-type istype? if printsymbol exit then
673 nil-type istype? if printnil exit then
674 pair-type istype? if ." (" printpair ." )" exit then
676 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
683 cr ." Welcome to scheme.forth.jl!" cr
684 ." Use Ctrl-D to exit." cr
689 cr bold fg green ." > " reset-term
692 fg cyan ." ; " print reset-term