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
281 bold fg red ." Incorrect argument count." reset-term cr
285 : ensure-arg-count ( args n -- )
287 drop nil objeq? false = if
291 -rot 2dup nil objeq? if
299 include scheme-primitives.4th
308 variable stored-parse-idx
309 create parse-str 161 allot
310 variable parse-str-span
312 create parse-idx-stack 10 allot
313 variable parse-idx-sp
314 parse-idx-stack parse-idx-sp !
317 parse-idx @ parse-idx-sp @ !
322 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
326 parse-idx-sp @ @ parse-idx ! ;
330 '\n' parse-str parse-str-span @ + !
331 1 parse-str-span +! ;
338 parse-str 160 expect cr
339 span @ parse-str-span !
349 : charavailable? ( -- bool )
350 parse-str-span @ parse-idx @ > ;
352 : nextchar ( -- char )
353 charavailable? false = if getline then
354 parse-str parse-idx @ + @ ;
356 : whitespace? ( -- bool )
365 nextchar [char] ( = or
366 nextchar [char] ) = or
383 nextchar [char] - = ;
385 : fixnum? ( -- bool )
417 : boolean? ( -- bool )
418 nextchar [char] # <> if false exit then
425 and if pop-parse-idx false exit then
437 : str-equiv? ( str -- bool )
454 delim? false = if drop false then
459 : character? ( -- bool )
460 nextchar [char] # <> if false exit then
465 nextchar [char] \ <> if pop-parse-idx false exit then
469 S" newline" str-equiv? if pop-parse-idx true exit then
470 S" space" str-equiv? if pop-parse-idx true exit then
471 S" tab" str-equiv? if pop-parse-idx true exit then
473 charavailable? false = if pop-parse-idx false exit then
479 nextchar [char] ( = ;
481 : string? ( -- bool )
482 nextchar [char] " = ;
484 : readnum ( -- num-atom )
492 10 * nextchar [char] 0 - +
501 : readbool ( -- bool-atom )
504 nextchar [char] f = if
515 : readchar ( -- char-atom )
519 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
520 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
521 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
523 nextchar character-type
528 : readstring ( -- charlist )
529 nextchar [char] " = if
534 ." No delimiter following right double quote. Aborting." cr
543 nextchar [char] \ = if
546 [char] n of '\n' endof
547 [char] " of [char] " endof
553 inc-parse-idx character-type
560 : readsymbol ( -- charlist )
561 delim? if nil exit then
563 nextchar inc-parse-idx character-type
570 : charlist-equiv ( charlist charlist -- bool )
579 2drop 2drop true exit
581 2drop 2drop false exit
586 2drop 2drop false exit
593 car drop -rot car drop = if
594 cdr 2swap cdr recurse
600 : charlist>symbol ( charlist -- symbol-obj )
602 symbol-table fetchobj
619 drop symbol-type 2dup
620 symbol-table fetchobj cons
624 : readpair ( -- pairobj )
628 nextchar [char] ) = if
633 ." No delimiter following right paren. Aborting." cr
642 \ Read first pair element
647 nextchar [char] . = if
652 ." No delimiter following '.'. Aborting." cr
666 \ Parse a scheme expression
692 nextchar [char] " <> if
693 bold red ." Missing closing double-quote." reset-term cr
711 nextchar [char] ) <> if
712 bold red ." Missing closing paren." reset-term cr
721 nextchar [char] ' = if
723 quote-symbol recurse nil cons cons exit
727 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
731 \ Anything else is parsed as a symbol
732 readsymbol charlist>symbol
742 : self-evaluating? ( obj -- obj bool )
743 boolean-type istype? if true exit then
744 fixnum-type istype? if true exit then
745 character-type istype? if true exit then
746 string-type istype? if true exit then
747 nil-type istype? if true exit then
752 : tagged-list? ( obj tag-obj -- obj bool )
754 pair-type istype? false = if
760 : quote? ( obj -- obj bool )
761 quote-symbol tagged-list? ;
763 : quote-body ( quote-obj -- quote-body-obj )
766 : variable? ( obj -- obj bool )
767 symbol-type istype? ;
769 : definition? ( obj -- obj bool )
770 define-symbol tagged-list? ;
772 : definition-var ( obj -- var )
775 : definition-val ( obj -- val )
778 : assignment? ( obj -- obj bool )
779 set!-symbol tagged-list? ;
781 : assignment-var ( obj -- var )
784 : assignment-val ( obj -- val )
787 : eval-definition ( obj env -- res )
789 2over 2over ( env obj env obj )
790 definition-val 2swap ( env obj valexp env )
793 2swap definition-var 2swap ( env var val )
801 : eval-assignment ( obj env -- res )
803 2over 2over ( env obj env obj )
804 assignment-val 2swap ( env obj valexp env )
807 2swap assignment-var 2swap ( env var val )
815 : if? ( obj -- obj bool )
816 if-symbol tagged-list? ;
818 : if-predicate ( ifobj -- pred )
821 : if-consequent ( ifobj -- conseq )
824 : if-alternative ( ifobj -- alt|false )
832 : false? ( boolobj -- boolean )
833 boolean-type istype? if
834 false boolean-type objeq?
840 : true? ( boolobj -- bool )
843 : application? ( obj -- obj bool)
846 : operator ( obj -- operator )
849 : operands ( obj -- operands )
852 : nooperands? ( operands -- bool )
855 : first-operand ( operands -- operand )
858 : rest-operands ( operands -- other-operands )
861 : list-of-vals ( args env -- vals )
867 2over 2over first-operand 2swap eval
868 -2rot rest-operands 2swap recurse
873 :noname ( obj env -- result )
893 2swap eval-definition
898 2swap eval-assignment
920 primitive-type istype? false = if
921 bold fg red ." Object not applicable. Aboring." reset-term cr
926 operands 2swap list-of-vals
932 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
938 \ ---- Print ---- {{{
942 : printnum ( numobj -- ) drop 0 .R ;
944 : printbool ( numobj -- )
952 : printchar ( charobj -- )
956 bl of ." #\space" endof
957 '\n' of ." #\newline" endof
963 : (printstring) ( stringobj -- )
964 nil-type istype? if 2drop exit then
968 '\n' of ." \n" drop endof
969 [char] \ of ." \\" drop endof
970 [char] " of [char] \ emit [char] " emit drop endof
976 : printstring ( stringobj -- )
981 : printsymbol ( symbolobj -- )
982 nil-type istype? if 2drop exit then
988 : printnil ( nilobj -- )
991 : printpair ( pairobj -- )
995 nil-type istype? if 2drop exit then
996 pair-type istype? if space recurse exit then
1000 : printprim ( primobj -- )
1001 2drop ." <primitive procedure>" ;
1004 fixnum-type istype? if printnum exit then
1005 boolean-type istype? if printbool exit then
1006 character-type istype? if printchar exit then
1007 string-type istype? if printstring exit then
1008 symbol-type istype? if printsymbol exit then
1009 nil-type istype? if printnil exit then
1010 pair-type istype? if ." (" printpair ." )" exit then
1011 primitive-type istype? if printprim exit then
1013 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1022 cr ." Welcome to scheme.forth.jl!" cr
1023 ." Use Ctrl-D to exit." cr
1028 cr bold fg green ." > " reset-term
1030 global-env fetchobj eval
1031 fg cyan ." ; " print reset-term