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
300 bold fg red ." Incorrect argument type." reset-term cr
304 : ensure-arg-type ( arg type -- arg )
310 include scheme-primitives.4th
319 variable stored-parse-idx
320 create parse-str 161 allot
321 variable parse-str-span
323 create parse-idx-stack 10 allot
324 variable parse-idx-sp
325 parse-idx-stack parse-idx-sp !
328 parse-idx @ parse-idx-sp @ !
333 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
337 parse-idx-sp @ @ parse-idx ! ;
341 '\n' parse-str parse-str-span @ + !
342 1 parse-str-span +! ;
349 parse-str 160 expect cr
350 span @ parse-str-span !
360 : charavailable? ( -- bool )
361 parse-str-span @ parse-idx @ > ;
363 : nextchar ( -- char )
364 charavailable? false = if getline then
365 parse-str parse-idx @ + @ ;
367 : whitespace? ( -- bool )
376 nextchar [char] ( = or
377 nextchar [char] ) = or
380 : commentstart? ( -- bool )
381 nextchar [char] ; = ;
385 false \ Indicates whether or not we're eating a comment
388 dup whitespace? or commentstart? or
390 dup nextchar '\n' = and if
391 invert \ Stop eating comment
393 dup false = commentstart? and if
394 invert \ Begin eating comment
409 nextchar [char] - = ;
412 nextchar [char] + = ;
414 : fixnum? ( -- bool )
446 : boolean? ( -- bool )
447 nextchar [char] # <> if false exit then
454 and if pop-parse-idx false exit then
466 : str-equiv? ( str -- bool )
483 delim? false = if drop false then
488 : character? ( -- bool )
489 nextchar [char] # <> if false exit then
494 nextchar [char] \ <> if pop-parse-idx false exit then
498 S" newline" str-equiv? if pop-parse-idx true exit then
499 S" space" str-equiv? if pop-parse-idx true exit then
500 S" tab" str-equiv? if pop-parse-idx true exit then
502 charavailable? false = if pop-parse-idx false exit then
508 nextchar [char] ( = ;
510 : string? ( -- bool )
511 nextchar [char] " = ;
513 : readnum ( -- num-atom )
524 10 * nextchar [char] 0 - +
533 : readbool ( -- bool-atom )
536 nextchar [char] f = if
547 : readchar ( -- char-atom )
551 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
552 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
553 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
555 nextchar character-type
560 : readstring ( -- charlist )
561 nextchar [char] " = if
566 ." No delimiter following right double quote. Aborting." cr
575 nextchar [char] \ = if
578 [char] n of '\n' endof
579 [char] " of [char] " endof
585 inc-parse-idx character-type
592 : readsymbol ( -- charlist )
593 delim? if nil exit then
595 nextchar inc-parse-idx character-type
602 : charlist-equiv ( charlist charlist -- bool )
611 2drop 2drop true exit
613 2drop 2drop false exit
618 2drop 2drop false exit
625 car drop -rot car drop = if
626 cdr 2swap cdr recurse
632 : charlist>symbol ( charlist -- symbol-obj )
634 symbol-table fetchobj
651 drop symbol-type 2dup
652 symbol-table fetchobj cons
656 : readpair ( -- pairobj )
660 nextchar [char] ) = if
665 ." No delimiter following right paren. Aborting." cr
674 \ Read first pair element
679 nextchar [char] . = if
684 ." No delimiter following '.'. Aborting." cr
698 \ Parse a scheme expression
724 nextchar [char] " <> if
725 bold red ." Missing closing double-quote." reset-term cr
743 nextchar [char] ) <> if
744 bold red ." Missing closing paren." reset-term cr
753 nextchar [char] ' = if
755 quote-symbol recurse nil cons cons exit
759 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
763 \ Anything else is parsed as a symbol
764 readsymbol charlist>symbol
774 : self-evaluating? ( obj -- obj bool )
775 boolean-type istype? if true exit then
776 fixnum-type istype? if true exit then
777 character-type istype? if true exit then
778 string-type istype? if true exit then
779 nil-type istype? if true exit then
784 : tagged-list? ( obj tag-obj -- obj bool )
786 pair-type istype? false = if
792 : quote? ( obj -- obj bool )
793 quote-symbol tagged-list? ;
795 : quote-body ( quote-obj -- quote-body-obj )
798 : variable? ( obj -- obj bool )
799 symbol-type istype? ;
801 : definition? ( obj -- obj bool )
802 define-symbol tagged-list? ;
804 : definition-var ( obj -- var )
807 : definition-val ( obj -- val )
810 : assignment? ( obj -- obj bool )
811 set!-symbol tagged-list? ;
813 : assignment-var ( obj -- var )
816 : assignment-val ( obj -- val )
819 : eval-definition ( obj env -- res )
821 2over 2over ( env obj env obj )
822 definition-val 2swap ( env obj valexp env )
825 2swap definition-var 2swap ( env var val )
833 : eval-assignment ( obj env -- res )
835 2over 2over ( env obj env obj )
836 assignment-val 2swap ( env obj valexp env )
839 2swap assignment-var 2swap ( env var val )
847 : if? ( obj -- obj bool )
848 if-symbol tagged-list? ;
850 : if-predicate ( ifobj -- pred )
853 : if-consequent ( ifobj -- conseq )
856 : if-alternative ( ifobj -- alt|false )
864 : false? ( boolobj -- boolean )
865 boolean-type istype? if
866 false boolean-type objeq?
872 : true? ( boolobj -- bool )
875 : application? ( obj -- obj bool)
878 : operator ( obj -- operator )
881 : operands ( obj -- operands )
884 : nooperands? ( operands -- bool )
887 : first-operand ( operands -- operand )
890 : rest-operands ( operands -- other-operands )
893 : list-of-vals ( args env -- vals )
899 2over 2over first-operand 2swap eval
900 -2rot rest-operands 2swap recurse
905 :noname ( obj env -- result )
925 2swap eval-definition
930 2swap eval-assignment
952 primitive-type istype? false = if
953 bold fg red ." Object not applicable. Aboring." reset-term cr
958 operands 2swap list-of-vals
964 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
970 \ ---- Print ---- {{{
974 : printnum ( numobj -- ) drop 0 .R ;
976 : printbool ( numobj -- )
984 : printchar ( charobj -- )
988 bl of ." #\space" endof
989 '\n' of ." #\newline" endof
995 : (printstring) ( stringobj -- )
996 nil-type istype? if 2drop exit then
1000 '\n' of ." \n" drop endof
1001 [char] \ of ." \\" drop endof
1002 [char] " of [char] \ emit [char] " emit drop endof
1008 : printstring ( stringobj -- )
1013 : printsymbol ( symbolobj -- )
1014 nil-type istype? if 2drop exit then
1020 : printnil ( nilobj -- )
1023 : printpair ( pairobj -- )
1027 nil-type istype? if 2drop exit then
1028 pair-type istype? if space recurse exit then
1032 : printprim ( primobj -- )
1033 2drop ." <primitive procedure>" ;
1036 fixnum-type istype? if printnum exit then
1037 boolean-type istype? if printbool exit then
1038 character-type istype? if printchar exit then
1039 string-type istype? if printstring exit then
1040 symbol-type istype? if printsymbol exit then
1041 nil-type istype? if printnil exit then
1042 pair-type istype? if ." (" printpair ." )" exit then
1043 primitive-type istype? if printprim exit then
1045 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1054 cr ." Welcome to scheme.forth.jl!" cr
1055 ." Use Ctrl-D to exit." cr
1060 cr bold fg green ." > " reset-term
1062 global-env fetchobj eval
1063 fg cyan ." ; " print reset-term