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 7 constant primitive-type
17 : istype? ( obj type -- obj bool )
20 \ ------ Cons cell memory ------ {{{
23 create car-cells N allot
24 create car-type-cells N allot
25 create cdr-cells N allot
26 create cdr-type-cells N allot
31 : cons ( car-obj cdr-obj -- pair-obj )
32 cdr-type-cells nextfree @ + !
33 cdr-cells nextfree @ + !
34 car-type-cells nextfree @ + !
35 car-cells nextfree @ + !
42 : car ( pair-obj -- car-obj )
44 dup car-cells + @ swap
48 : cdr ( pair-obj -- car-obj )
50 dup cdr-cells + @ swap
54 : set-car! ( obj pair-obj -- )
56 rot swap car-type-cells + !
60 : set-cdr! ( obj pair-obj -- )
62 rot swap cdr-type-cells + !
72 : nil? nil-type istype? ;
74 : objvar create nil swap , , ;
76 : value@ ( objvar -- val ) @ ;
77 : type@ ( objvar -- type ) 1+ @ ;
78 : value! ( newval objvar -- ) ! ;
79 : type! ( newtype objvar -- ) 1+ ! ;
80 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
81 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
83 : objeq? ( obj obj -- bool )
86 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
89 R> R> ( b1 b2 a1 a2 c1 c2 )
93 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
94 2swap ( a1 a2 c1 c2 b1 b2 )
102 \ ---- Pre-defined symbols ---- {{{
106 : (create-symbol) ( addr n -- symbol-obj )
110 2dup drop @ character-type 2swap
118 : create-symbol ( -- )
127 symbol-table fetchobj
132 does> dup @ swap 1+ @
135 create-symbol quote quote-symbol
136 create-symbol define define-symbol
137 create-symbol set! set!-symbol
138 create-symbol ok ok-symbol
139 create-symbol if if-symbol
143 \ ---- Environments ---- {{{
145 : enclosing-env ( env -- env )
148 : first-frame ( env -- frame )
151 : make-frame ( vars vals -- frame )
154 : frame-vars ( frame -- vars )
157 : frame-vals ( frame -- vals )
160 : add-binding ( var val frame -- )
161 2swap 2over frame-vals cons
163 2swap 2over frame-vars cons
167 : extend-env ( vars vals env -- env )
177 : get-vars-vals-frame ( var frame -- bool )
178 2dup frame-vars vars setobj
179 frame-vals vals setobj
182 vars fetchobj nil objeq? false =
184 2dup vars fetchobj car objeq? if
189 vars fetchobj cdr vars setobj
190 vals fetchobj cdr vals setobj
196 : get-vars-vals ( var env -- vars? vals? bool )
199 2dup nil objeq? false =
201 2over 2over first-frame
202 get-vars-vals-frame if
204 vars fetchobj vals fetchobj true
218 : lookup-var ( var env -- val )
222 bold fg red ." Tried to read unbound variable." reset-term cr abort
226 : set-var ( var val env -- )
227 >R >R 2swap R> R> ( val var env )
229 2swap 2drop ( val vals )
232 bold fg red ." Tried to set unbound variable." reset-term cr abort
238 : define-var ( var val env -- )
241 2over env fetchobj ( var val var env )
243 2swap 2drop ( var val vals )
248 first-frame ( var val frame )
256 nil nil nil extend-env
261 \ ---- Primitives ---- {{{
263 : make-primitive ( cfa -- )
272 symbol-table fetchobj
276 rot primitive-type ( var prim )
277 global-env fetchobj define-var
285 2dup cdr recurse drop
298 variable stored-parse-idx
299 create parse-str 161 allot
300 variable parse-str-span
302 create parse-idx-stack 10 allot
303 variable parse-idx-sp
304 parse-idx-stack parse-idx-sp !
307 parse-idx @ parse-idx-sp @ !
312 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
316 parse-idx-sp @ @ parse-idx ! ;
320 '\n' parse-str parse-str-span @ + !
321 1 parse-str-span +! ;
328 parse-str 160 expect cr
329 span @ parse-str-span !
339 : charavailable? ( -- bool )
340 parse-str-span @ parse-idx @ > ;
342 : nextchar ( -- char )
343 charavailable? false = if getline then
344 parse-str parse-idx @ + @ ;
346 : whitespace? ( -- bool )
355 nextchar [char] ( = or
356 nextchar [char] ) = or
373 nextchar [char] - = ;
375 : number? ( -- bool )
407 : boolean? ( -- bool )
408 nextchar [char] # <> if false exit then
415 and if pop-parse-idx false exit then
427 : str-equiv? ( str -- bool )
444 delim? false = if drop false then
449 : character? ( -- bool )
450 nextchar [char] # <> if false exit then
455 nextchar [char] \ <> if pop-parse-idx false exit then
459 S" newline" str-equiv? if pop-parse-idx true exit then
460 S" space" str-equiv? if pop-parse-idx true exit then
461 S" tab" str-equiv? if pop-parse-idx true exit then
463 charavailable? false = if pop-parse-idx false exit then
469 nextchar [char] ( = ;
471 : string? ( -- bool )
472 nextchar [char] " = ;
474 : readnum ( -- num-atom )
482 10 * nextchar [char] 0 - +
491 : readbool ( -- bool-atom )
494 nextchar [char] f = if
505 : readchar ( -- char-atom )
509 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
510 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
511 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
513 nextchar character-type
518 : readstring ( -- charlist )
519 nextchar [char] " = if
524 ." No delimiter following right double quote. Aborting." cr
533 nextchar [char] \ = if
536 [char] n of '\n' endof
537 [char] " of [char] " endof
543 inc-parse-idx character-type
550 : readsymbol ( -- charlist )
551 delim? if nil exit then
553 nextchar inc-parse-idx character-type
560 : charlist-equiv ( charlist charlist -- bool )
569 2drop 2drop true exit
571 2drop 2drop false exit
576 2drop 2drop false exit
583 car drop -rot car drop = if
584 cdr 2swap cdr recurse
590 : charlist>symbol ( charlist -- symbol-obj )
592 symbol-table fetchobj
609 drop symbol-type 2dup
610 symbol-table fetchobj cons
614 : readpair ( -- pairobj )
618 nextchar [char] ) = if
623 ." No delimiter following right paren. Aborting." cr
632 \ Read first pair element
637 nextchar [char] . = if
642 ." No delimiter following '.'. Aborting." cr
656 \ Parse a scheme expression
682 nextchar [char] " <> if
683 bold red ." Missing closing double-quote." reset-term cr
701 nextchar [char] ) <> if
702 bold red ." Missing closing paren." reset-term cr
711 nextchar [char] ' = if
713 quote-symbol recurse nil cons cons exit
717 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
721 \ Anything else is parsed as a symbol
722 readsymbol charlist>symbol
732 : self-evaluating? ( obj -- obj bool )
733 boolean-type istype? if true exit then
734 number-type istype? if true exit then
735 character-type istype? if true exit then
736 string-type istype? if true exit then
737 nil-type istype? if true exit then
742 : tagged-list? ( obj tag-obj -- obj bool )
744 pair-type istype? false = if
750 : quote? ( obj -- obj bool )
751 quote-symbol tagged-list? ;
753 : quote-body ( quote-obj -- quote-body-obj )
756 : variable? ( obj -- obj bool )
757 symbol-type istype? ;
759 : definition? ( obj -- obj bool )
760 define-symbol tagged-list? ;
762 : definition-var ( obj -- var )
765 : definition-val ( obj -- val )
768 : assignment? ( obj -- obj bool )
769 set!-symbol tagged-list? ;
771 : assignment-var ( obj -- var )
774 : assignment-val ( obj -- val )
777 : eval-definition ( obj env -- res )
779 2over 2over ( env obj env obj )
780 definition-val 2swap ( env obj valexp env )
783 2swap definition-var 2swap ( env var val )
791 : eval-assignment ( obj env -- res )
793 2over 2over ( env obj env obj )
794 assignment-val 2swap ( env obj valexp env )
797 2swap assignment-var 2swap ( env var val )
805 : if? ( obj -- obj bool )
806 if-symbol tagged-list? ;
808 : if-predicate ( ifobj -- pred )
811 : if-consequent ( ifobj -- conseq )
814 : if-alternative ( ifobj -- alt|false )
822 : false? ( boolobj -- boolean )
823 boolean-type istype? if
824 false boolean-type objeq?
830 : true? ( boolobj -- bool )
833 : application? ( obj -- obj bool)
836 : operator ( obj -- operator )
839 : operands ( obj -- operands )
842 : nooperands? ( operands -- bool )
845 : first-operand ( operands -- operand )
848 : rest-operands ( operands -- other-operands )
851 : list-of-vals ( args env -- vals )
857 2over 2over first-operand 2swap eval
858 -2rot rest-operands 2swap recurse
863 :noname ( obj env -- result )
883 2swap eval-definition
888 2swap eval-assignment
910 primitive-type istype? false = if
911 bold fg red ." Object not applicable. Aboring." reset-term cr
916 operands 2swap list-of-vals
922 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
928 \ ---- Print ---- {{{
932 : printnum ( numobj -- ) drop 0 .R ;
934 : printbool ( numobj -- )
942 : printchar ( charobj -- )
946 bl of ." #\space" endof
947 '\n' of ." #\newline" endof
953 : (printstring) ( stringobj -- )
954 nil-type istype? if 2drop exit then
958 '\n' of ." \n" drop endof
959 [char] \ of ." \\" drop endof
960 [char] " of [char] \ emit [char] " emit drop endof
966 : printstring ( stringobj -- )
971 : printsymbol ( symbolobj -- )
972 nil-type istype? if 2drop exit then
978 : printnil ( nilobj -- )
981 : printpair ( pairobj -- )
985 nil-type istype? if 2drop exit then
986 pair-type istype? if space recurse exit then
990 : printprim ( primobj -- )
991 2drop ." <primitive procedure>" ;
994 number-type istype? if printnum exit then
995 boolean-type istype? if printbool exit then
996 character-type istype? if printchar exit then
997 string-type istype? if printstring exit then
998 symbol-type istype? if printsymbol exit then
999 nil-type istype? if printnil exit then
1000 pair-type istype? if ." (" printpair ." )" exit then
1001 primitive-type istype? if printprim exit then
1003 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1012 cr ." Welcome to scheme.forth.jl!" cr
1013 ." Use Ctrl-D to exit." cr
1018 cr bold fg green ." > " reset-term
1020 global-env fetchobj eval
1021 fg cyan ." ; " print reset-term