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 : duplicate-charlist ( charlist -- copy )
107 2dup nil objeq? false = if
108 2dup car 2swap cdr recurse cons
111 : charlist-equiv ( charlist charlist -- bool )
120 2drop 2drop true exit
122 2drop 2drop false exit
127 2drop 2drop false exit
134 car drop -rot car drop = if
135 cdr 2swap cdr recurse
141 : charlist>symbol ( charlist -- symbol-obj )
143 symbol-table fetchobj
160 drop symbol-type 2dup
161 symbol-table fetchobj cons
166 : (create-symbol) ( addr n -- symbol-obj )
170 2dup drop @ character-type 2swap
178 : create-symbol ( -- )
187 symbol-table fetchobj
192 does> dup @ swap 1+ @
195 create-symbol quote quote-symbol
196 create-symbol define define-symbol
197 create-symbol set! set!-symbol
198 create-symbol ok ok-symbol
199 create-symbol if if-symbol
203 \ ---- Environments ---- {{{
205 : enclosing-env ( env -- env )
208 : first-frame ( env -- frame )
211 : make-frame ( vars vals -- frame )
214 : frame-vars ( frame -- vars )
217 : frame-vals ( frame -- vals )
220 : add-binding ( var val frame -- )
221 2swap 2over frame-vals cons
223 2swap 2over frame-vars cons
227 : extend-env ( vars vals env -- env )
237 : get-vars-vals-frame ( var frame -- bool )
238 2dup frame-vars vars setobj
239 frame-vals vals setobj
242 vars fetchobj nil objeq? false =
244 2dup vars fetchobj car objeq? if
249 vars fetchobj cdr vars setobj
250 vals fetchobj cdr vals setobj
256 : get-vars-vals ( var env -- vars? vals? bool )
259 2dup nil objeq? false =
261 2over 2over first-frame
262 get-vars-vals-frame if
264 vars fetchobj vals fetchobj true
278 : lookup-var ( var env -- val )
282 bold fg red ." Tried to read unbound variable." reset-term cr abort
286 : set-var ( var val env -- )
287 >R >R 2swap R> R> ( val var env )
289 2swap 2drop ( val vals )
292 bold fg red ." Tried to set unbound variable." reset-term cr abort
298 : define-var ( var val env -- )
301 2over env fetchobj ( var val var env )
303 2swap 2drop ( var val vals )
308 first-frame ( var val frame )
316 nil nil nil extend-env
321 \ ---- Primitives ---- {{{
323 : make-primitive ( cfa -- )
332 symbol-table fetchobj
336 rot primitive-type ( var prim )
337 global-env fetchobj define-var
341 bold fg red ." Incorrect argument count." reset-term cr
345 : ensure-arg-count ( args n -- )
347 drop nil objeq? false = if
351 -rot 2dup nil objeq? if
360 bold fg red ." Incorrect argument type." reset-term cr
364 : ensure-arg-type ( arg type -- arg )
370 include scheme-primitives.4th
379 variable stored-parse-idx
380 create parse-str 161 allot
381 variable parse-str-span
383 create parse-idx-stack 10 allot
384 variable parse-idx-sp
385 parse-idx-stack parse-idx-sp !
388 parse-idx @ parse-idx-sp @ !
393 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
397 parse-idx-sp @ @ parse-idx ! ;
401 '\n' parse-str parse-str-span @ + !
402 1 parse-str-span +! ;
409 parse-str 160 expect cr
410 span @ parse-str-span !
420 : charavailable? ( -- bool )
421 parse-str-span @ parse-idx @ > ;
423 : nextchar ( -- char )
424 charavailable? false = if getline then
425 parse-str parse-idx @ + @ ;
427 : whitespace? ( -- bool )
436 nextchar [char] ( = or
437 nextchar [char] ) = or
440 : commentstart? ( -- bool )
441 nextchar [char] ; = ;
445 false \ Indicates whether or not we're eating a comment
448 dup whitespace? or commentstart? or
450 dup nextchar '\n' = and if
451 invert \ Stop eating comment
453 dup false = commentstart? and if
454 invert \ Begin eating comment
469 nextchar [char] - = ;
472 nextchar [char] + = ;
474 : fixnum? ( -- bool )
506 : boolean? ( -- bool )
507 nextchar [char] # <> if false exit then
514 and if pop-parse-idx false exit then
526 : str-equiv? ( str -- bool )
543 delim? false = if drop false then
548 : character? ( -- bool )
549 nextchar [char] # <> if false exit then
554 nextchar [char] \ <> if pop-parse-idx false exit then
558 S" newline" str-equiv? if pop-parse-idx true exit then
559 S" space" str-equiv? if pop-parse-idx true exit then
560 S" tab" str-equiv? if pop-parse-idx true exit then
562 charavailable? false = if pop-parse-idx false exit then
568 nextchar [char] ( = ;
570 : string? ( -- bool )
571 nextchar [char] " = ;
573 : readnum ( -- num-atom )
584 10 * nextchar [char] 0 - +
593 : readbool ( -- bool-atom )
596 nextchar [char] f = if
607 : readchar ( -- char-atom )
611 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
612 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
613 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
615 nextchar character-type
620 : readstring ( -- charlist )
621 nextchar [char] " = if
626 ." No delimiter following right double quote. Aborting." cr
635 nextchar [char] \ = if
638 [char] n of '\n' endof
639 [char] " of [char] " endof
645 inc-parse-idx character-type
652 : readsymbol ( -- charlist )
653 delim? if nil exit then
655 nextchar inc-parse-idx character-type
662 : readpair ( -- pairobj )
666 nextchar [char] ) = if
671 ." No delimiter following right paren. Aborting." cr
680 \ Read first pair element
685 nextchar [char] . = if
690 ." No delimiter following '.'. Aborting." cr
704 \ Parse a scheme expression
730 nextchar [char] " <> if
731 bold red ." Missing closing double-quote." reset-term cr
749 nextchar [char] ) <> if
750 bold red ." Missing closing paren." reset-term cr
759 nextchar [char] ' = if
761 quote-symbol recurse nil cons cons exit
765 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
769 \ Anything else is parsed as a symbol
770 readsymbol charlist>symbol
780 : self-evaluating? ( obj -- obj bool )
781 boolean-type istype? if true exit then
782 fixnum-type istype? if true exit then
783 character-type istype? if true exit then
784 string-type istype? if true exit then
785 nil-type istype? if true exit then
790 : tagged-list? ( obj tag-obj -- obj bool )
792 pair-type istype? false = if
798 : quote? ( obj -- obj bool )
799 quote-symbol tagged-list? ;
801 : quote-body ( quote-obj -- quote-body-obj )
804 : variable? ( obj -- obj bool )
805 symbol-type istype? ;
807 : definition? ( obj -- obj bool )
808 define-symbol tagged-list? ;
810 : definition-var ( obj -- var )
813 : definition-val ( obj -- val )
816 : assignment? ( obj -- obj bool )
817 set!-symbol tagged-list? ;
819 : assignment-var ( obj -- var )
822 : assignment-val ( obj -- val )
825 : eval-definition ( obj env -- res )
827 2over 2over ( env obj env obj )
828 definition-val 2swap ( env obj valexp env )
831 2swap definition-var 2swap ( env var val )
839 : eval-assignment ( obj env -- res )
841 2over 2over ( env obj env obj )
842 assignment-val 2swap ( env obj valexp env )
845 2swap assignment-var 2swap ( env var val )
853 : if? ( obj -- obj bool )
854 if-symbol tagged-list? ;
856 : if-predicate ( ifobj -- pred )
859 : if-consequent ( ifobj -- conseq )
862 : if-alternative ( ifobj -- alt|false )
870 : false? ( boolobj -- boolean )
871 boolean-type istype? if
872 false boolean-type objeq?
878 : true? ( boolobj -- bool )
881 : application? ( obj -- obj bool)
884 : operator ( obj -- operator )
887 : operands ( obj -- operands )
890 : nooperands? ( operands -- bool )
893 : first-operand ( operands -- operand )
896 : rest-operands ( operands -- other-operands )
899 : list-of-vals ( args env -- vals )
905 2over 2over first-operand 2swap eval
906 -2rot rest-operands 2swap recurse
911 :noname ( obj env -- result )
931 2swap eval-definition
936 2swap eval-assignment
958 primitive-type istype? false = if
959 bold fg red ." Object not applicable. Aboring." reset-term cr
964 operands 2swap list-of-vals
970 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
976 \ ---- Print ---- {{{
980 : printnum ( numobj -- ) drop 0 .R ;
982 : printbool ( numobj -- )
990 : printchar ( charobj -- )
994 bl of ." #\space" endof
995 '\n' of ." #\newline" endof
1001 : (printstring) ( stringobj -- )
1002 nil-type istype? if 2drop exit then
1006 '\n' of ." \n" drop endof
1007 [char] \ of ." \\" drop endof
1008 [char] " of [char] \ emit [char] " emit drop endof
1014 : printstring ( stringobj -- )
1019 : printsymbol ( symbolobj -- )
1020 nil-type istype? if 2drop exit then
1026 : printnil ( nilobj -- )
1029 : printpair ( pairobj -- )
1033 nil-type istype? if 2drop exit then
1034 pair-type istype? if space recurse exit then
1038 : printprim ( primobj -- )
1039 2drop ." <primitive procedure>" ;
1042 fixnum-type istype? if printnum exit then
1043 boolean-type istype? if printbool exit then
1044 character-type istype? if printchar exit then
1045 string-type istype? if printstring exit then
1046 symbol-type istype? if printsymbol exit then
1047 nil-type istype? if printnil exit then
1048 pair-type istype? if ." (" printpair ." )" exit then
1049 primitive-type istype? if printprim exit then
1051 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1060 cr ." Welcome to scheme.forth.jl!" cr
1061 ." Use Ctrl-D to exit." cr
1066 cr bold fg green ." > " reset-term
1068 global-env fetchobj eval
1069 fg cyan ." ; " print reset-term