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
280 : add-prim ( args -- )
285 2dup cdr recurse drop
291 ' add-prim make-primitive +
300 variable stored-parse-idx
301 create parse-str 161 allot
302 variable parse-str-span
304 create parse-idx-stack 10 allot
305 variable parse-idx-sp
306 parse-idx-stack parse-idx-sp !
309 parse-idx @ parse-idx-sp @ !
314 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
318 parse-idx-sp @ @ parse-idx ! ;
322 '\n' parse-str parse-str-span @ + !
323 1 parse-str-span +! ;
330 parse-str 160 expect cr
331 span @ parse-str-span !
341 : charavailable? ( -- bool )
342 parse-str-span @ parse-idx @ > ;
344 : nextchar ( -- char )
345 charavailable? false = if getline then
346 parse-str parse-idx @ + @ ;
348 : whitespace? ( -- bool )
357 nextchar [char] ( = or
358 nextchar [char] ) = or
375 nextchar [char] - = ;
377 : number? ( -- bool )
378 digit? minus? or false = if
399 : boolean? ( -- bool )
400 nextchar [char] # <> if false exit then
407 and if pop-parse-idx false exit then
419 : str-equiv? ( str -- bool )
436 delim? false = if drop false then
441 : character? ( -- bool )
442 nextchar [char] # <> if false exit then
447 nextchar [char] \ <> if pop-parse-idx false exit then
451 S" newline" str-equiv? if pop-parse-idx true exit then
452 S" space" str-equiv? if pop-parse-idx true exit then
453 S" tab" str-equiv? if pop-parse-idx true exit then
455 charavailable? false = if pop-parse-idx false exit then
461 nextchar [char] ( = ;
463 : string? ( -- bool )
464 nextchar [char] " = ;
466 : readnum ( -- num-atom )
474 10 * nextchar [char] 0 - +
483 : readbool ( -- bool-atom )
486 nextchar [char] f = if
497 : readchar ( -- char-atom )
501 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
502 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
503 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
505 nextchar character-type
510 : readstring ( -- charlist )
511 nextchar [char] " = if
516 ." No delimiter following right double quote. Aborting." cr
525 nextchar [char] \ = if
528 [char] n of '\n' endof
529 [char] " of [char] " endof
535 inc-parse-idx character-type
542 : readsymbol ( -- charlist )
543 delim? if nil exit then
545 nextchar inc-parse-idx character-type
552 : charlist-equiv ( charlist charlist -- bool )
561 2drop 2drop true exit
563 2drop 2drop false exit
568 2drop 2drop false exit
575 car drop -rot car drop = if
576 cdr 2swap cdr recurse
582 : charlist>symbol ( charlist -- symbol-obj )
584 symbol-table fetchobj
601 drop symbol-type 2dup
602 symbol-table fetchobj cons
606 : readpair ( -- pairobj )
610 nextchar [char] ) = if
615 ." No delimiter following right paren. Aborting." cr
624 \ Read first pair element
629 nextchar [char] . = if
634 ." No delimiter following '.'. Aborting." cr
648 \ Parse a scheme expression
674 nextchar [char] " <> if
675 bold red ." Missing closing double-quote." reset-term cr
693 nextchar [char] ) <> if
694 bold red ." Missing closing paren." reset-term cr
703 nextchar [char] ' = if
705 quote-symbol recurse nil cons cons exit
709 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
713 \ Anything else is parsed as a symbol
714 readsymbol charlist>symbol
724 : self-evaluating? ( obj -- obj bool )
725 boolean-type istype? if true exit then
726 number-type istype? if true exit then
727 character-type istype? if true exit then
728 string-type istype? if true exit then
729 nil-type istype? if true exit then
734 : tagged-list? ( obj tag-obj -- obj bool )
736 pair-type istype? false = if
742 : quote? ( obj -- obj bool )
743 quote-symbol tagged-list? ;
745 : quote-body ( quote-obj -- quote-body-obj )
748 : variable? ( obj -- obj bool )
749 symbol-type istype? ;
751 : definition? ( obj -- obj bool )
752 define-symbol tagged-list? ;
754 : definition-var ( obj -- var )
757 : definition-val ( obj -- val )
760 : assignment? ( obj -- obj bool )
761 set!-symbol tagged-list? ;
763 : assignment-var ( obj -- var )
766 : assignment-val ( obj -- val )
769 : eval-definition ( obj env -- res )
771 2over 2over ( env obj env obj )
772 definition-val 2swap ( env obj valexp env )
775 2swap definition-var 2swap ( env var val )
783 : eval-assignment ( obj env -- res )
785 2over 2over ( env obj env obj )
786 assignment-val 2swap ( env obj valexp env )
789 2swap assignment-var 2swap ( env var val )
797 : if? ( obj -- obj bool )
798 if-symbol tagged-list? ;
800 : if-predicate ( ifobj -- pred )
803 : if-consequent ( ifobj -- conseq )
806 : if-alternative ( ifobj -- alt|false )
814 : false? ( boolobj -- boolean )
815 boolean-type istype? if
816 false boolean-type objeq?
822 : true? ( boolobj -- bool )
825 : application? ( obj -- obj bool)
828 : operator ( obj -- operator )
831 : operands ( obj -- operands )
834 : nooperands? ( operands -- bool )
837 : first-operand ( operands -- operand )
840 : rest-operands ( operands -- other-operands )
843 : list-of-vals ( args env -- vals )
849 2over 2over first-operand 2swap eval
850 -2rot rest-operands 2swap recurse
855 :noname ( obj env -- result )
875 2swap eval-definition
880 2swap eval-assignment
902 primitive-type istype? false = if
903 bold fg red ." Object not applicable. Aboring." reset-term cr
908 operands 2swap list-of-vals
914 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
920 \ ---- Print ---- {{{
924 : printnum ( numobj -- ) drop 0 .R ;
926 : printbool ( numobj -- )
934 : printchar ( charobj -- )
938 bl of ." #\space" endof
939 '\n' of ." #\newline" endof
945 : (printstring) ( stringobj -- )
946 nil-type istype? if 2drop exit then
950 '\n' of ." \n" drop endof
951 [char] \ of ." \\" drop endof
952 [char] " of [char] \ emit [char] " emit drop endof
958 : printstring ( stringobj -- )
963 : printsymbol ( symbolobj -- )
964 nil-type istype? if 2drop exit then
970 : printnil ( nilobj -- )
973 : printpair ( pairobj -- )
977 nil-type istype? if 2drop exit then
978 pair-type istype? if space recurse exit then
982 : printprim ( primobj -- )
983 2drop ." <primitive procedure>" ;
986 number-type istype? if printnum exit then
987 boolean-type istype? if printbool exit then
988 character-type istype? if printchar exit then
989 string-type istype? if printstring exit then
990 symbol-type istype? if printsymbol exit then
991 nil-type istype? if printnil exit then
992 pair-type istype? if ." (" printpair ." )" exit then
993 primitive-type istype? if printprim exit then
995 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1004 cr ." Welcome to scheme.forth.jl!" cr
1005 ." Use Ctrl-D to exit." cr
1010 cr bold fg green ." > " reset-term
1012 global-env fetchobj eval
1013 fg cyan ." ; " print reset-term