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-proc-type
17 8 constant compound-proc-type
18 : istype? ( obj type -- obj bool )
21 \ ------ Cons cell memory ------ {{{
24 create car-cells N allot
25 create car-type-cells N allot
26 create cdr-cells N allot
27 create cdr-type-cells N allot
32 : cons ( car-obj cdr-obj -- pair-obj )
33 cdr-type-cells nextfree @ + !
34 cdr-cells nextfree @ + !
35 car-type-cells nextfree @ + !
36 car-cells nextfree @ + !
43 : car ( pair-obj -- car-obj )
45 dup car-cells + @ swap
49 : cdr ( pair-obj -- car-obj )
51 dup cdr-cells + @ swap
55 : set-car! ( obj pair-obj -- )
57 rot swap car-type-cells + !
61 : set-cdr! ( obj pair-obj -- )
63 rot swap cdr-type-cells + !
73 : nil? nil-type istype? ;
75 : objvar create nil swap , , ;
77 : value@ ( objvar -- val ) @ ;
78 : type@ ( objvar -- type ) 1+ @ ;
79 : value! ( newval objvar -- ) ! ;
80 : type! ( newtype objvar -- ) 1+ ! ;
81 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
82 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
84 : objeq? ( obj obj -- bool )
87 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
90 R> R> ( b1 b2 a1 a2 c1 c2 )
94 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
95 2swap ( a1 a2 c1 c2 b1 b2 )
103 \ ---- Pre-defined symbols ---- {{{
107 : duplicate-charlist ( charlist -- copy )
108 2dup nil objeq? false = if
109 2dup car 2swap cdr recurse cons
112 : charlist-equiv ( charlist charlist -- bool )
121 2drop 2drop true exit
123 2drop 2drop false exit
128 2drop 2drop false exit
135 car drop -rot car drop = if
136 cdr 2swap cdr recurse
142 : charlist>symbol ( charlist -- symbol-obj )
144 symbol-table fetchobj
161 drop symbol-type 2dup
162 symbol-table fetchobj cons
167 : (create-symbol) ( addr n -- symbol-obj )
171 2dup drop @ character-type 2swap
179 : create-symbol ( -- )
188 symbol-table fetchobj
193 does> dup @ swap 1+ @
196 create-symbol quote quote-symbol
197 create-symbol define define-symbol
198 create-symbol set! set!-symbol
199 create-symbol ok ok-symbol
200 create-symbol if if-symbol
201 create-symbol lambda lambda-symbol
205 \ ---- Environments ---- {{{
207 : enclosing-env ( env -- env )
210 : first-frame ( env -- frame )
213 : make-frame ( vars vals -- frame )
216 : frame-vars ( frame -- vars )
219 : frame-vals ( frame -- vals )
222 : add-binding ( var val frame -- )
223 2swap 2over frame-vals cons
225 2swap 2over frame-vars cons
229 : extend-env ( vars vals env -- env )
239 : get-vars-vals-frame ( var frame -- bool )
240 2dup frame-vars vars setobj
241 frame-vals vals setobj
244 vars fetchobj nil objeq? false =
246 2dup vars fetchobj car objeq? if
251 vars fetchobj cdr vars setobj
252 vals fetchobj cdr vals setobj
258 : get-vars-vals ( var env -- vars? vals? bool )
261 2dup nil objeq? false =
263 2over 2over first-frame
264 get-vars-vals-frame if
266 vars fetchobj vals fetchobj true
280 : lookup-var ( var env -- val )
284 bold fg red ." Tried to read unbound variable." reset-term cr abort
288 : set-var ( var val env -- )
289 >R >R 2swap R> R> ( val var env )
291 2swap 2drop ( val vals )
294 bold fg red ." Tried to set unbound variable." reset-term cr abort
300 : define-var ( var val env -- )
303 2over env fetchobj ( var val var env )
305 2swap 2drop ( var val vals )
310 first-frame ( var val frame )
318 nil nil nil extend-env
323 \ ---- Primitives ---- {{{
325 : make-primitive ( cfa -- )
334 symbol-table fetchobj
338 rot primitive-proc-type ( var prim )
339 global-env fetchobj define-var
343 bold fg red ." Incorrect argument count." reset-term cr
347 : ensure-arg-count ( args n -- )
349 drop nil objeq? false = if
353 -rot 2dup nil objeq? if
362 bold fg red ." Incorrect argument type." reset-term cr
366 : ensure-arg-type ( arg type -- arg )
372 include scheme-primitives.4th
381 variable stored-parse-idx
382 create parse-str 161 allot
383 variable parse-str-span
385 create parse-idx-stack 10 allot
386 variable parse-idx-sp
387 parse-idx-stack parse-idx-sp !
390 parse-idx @ parse-idx-sp @ !
395 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
399 parse-idx-sp @ @ parse-idx ! ;
403 '\n' parse-str parse-str-span @ + !
404 1 parse-str-span +! ;
411 parse-str 160 expect cr
412 span @ parse-str-span !
422 : charavailable? ( -- bool )
423 parse-str-span @ parse-idx @ > ;
425 : nextchar ( -- char )
426 charavailable? false = if getline then
427 parse-str parse-idx @ + @ ;
429 : whitespace? ( -- bool )
438 nextchar [char] ( = or
439 nextchar [char] ) = or
442 : commentstart? ( -- bool )
443 nextchar [char] ; = ;
447 false \ Indicates whether or not we're eating a comment
450 dup whitespace? or commentstart? or
452 dup nextchar '\n' = and if
453 invert \ Stop eating comment
455 dup false = commentstart? and if
456 invert \ Begin eating comment
471 nextchar [char] - = ;
474 nextchar [char] + = ;
476 : fixnum? ( -- bool )
508 : boolean? ( -- bool )
509 nextchar [char] # <> if false exit then
516 and if pop-parse-idx false exit then
528 : str-equiv? ( str -- bool )
545 delim? false = if drop false then
550 : character? ( -- bool )
551 nextchar [char] # <> if false exit then
556 nextchar [char] \ <> if pop-parse-idx false exit then
560 S" newline" str-equiv? if pop-parse-idx true exit then
561 S" space" str-equiv? if pop-parse-idx true exit then
562 S" tab" str-equiv? if pop-parse-idx true exit then
564 charavailable? false = if pop-parse-idx false exit then
570 nextchar [char] ( = ;
572 : string? ( -- bool )
573 nextchar [char] " = ;
575 : readnum ( -- num-atom )
586 10 * nextchar [char] 0 - +
595 : readbool ( -- bool-atom )
598 nextchar [char] f = if
609 : readchar ( -- char-atom )
613 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
614 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
615 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
617 nextchar character-type
622 : readstring ( -- charlist )
623 nextchar [char] " = if
628 ." No delimiter following right double quote. Aborting." cr
637 nextchar [char] \ = if
640 [char] n of '\n' endof
641 [char] " of [char] " endof
647 inc-parse-idx character-type
654 : readsymbol ( -- charlist )
655 delim? if nil exit then
657 nextchar inc-parse-idx character-type
664 : readpair ( -- pairobj )
668 nextchar [char] ) = if
673 ." No delimiter following right paren. Aborting." cr
682 \ Read first pair element
687 nextchar [char] . = if
692 ." No delimiter following '.'. Aborting." cr
706 \ Parse a scheme expression
732 nextchar [char] " <> if
733 bold red ." Missing closing double-quote." reset-term cr
751 nextchar [char] ) <> if
752 bold red ." Missing closing paren." reset-term cr
761 nextchar [char] ' = if
763 quote-symbol recurse nil cons cons exit
768 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
772 \ Anything else is parsed as a symbol
773 readsymbol charlist>symbol
783 : self-evaluating? ( obj -- obj bool )
784 boolean-type istype? if true exit then
785 fixnum-type istype? if true exit then
786 character-type istype? if true exit then
787 string-type istype? if true exit then
788 nil-type istype? if true exit then
793 : tagged-list? ( obj tag-obj -- obj bool )
795 pair-type istype? false = if
801 : quote? ( obj -- obj bool )
802 quote-symbol tagged-list? ;
804 : quote-body ( quote-obj -- quote-body-obj )
807 : variable? ( obj -- obj bool )
808 symbol-type istype? ;
810 : definition? ( obj -- obj bool )
811 define-symbol tagged-list? ;
813 : definition-var ( obj -- var )
816 : definition-val ( obj -- val )
819 : assignment? ( obj -- obj bool )
820 set!-symbol tagged-list? ;
822 : assignment-var ( obj -- var )
825 : assignment-val ( obj -- val )
828 : eval-definition ( obj env -- res )
830 2over 2over ( env obj env obj )
831 definition-val 2swap ( env obj valexp env )
834 2swap definition-var 2swap ( env var val )
842 : eval-assignment ( obj env -- res )
844 2over 2over ( env obj env obj )
845 assignment-val 2swap ( env obj valexp env )
848 2swap assignment-var 2swap ( env var val )
856 : if? ( obj -- obj bool )
857 if-symbol tagged-list? ;
859 : if-predicate ( ifobj -- pred )
862 : if-consequent ( ifobj -- conseq )
865 : if-alternative ( ifobj -- alt|false )
873 : false? ( boolobj -- boolean )
874 boolean-type istype? if
875 false boolean-type objeq?
881 : true? ( boolobj -- bool )
884 : application? ( obj -- obj bool)
887 : operator ( obj -- operator )
890 : operands ( obj -- operands )
893 : nooperands? ( operands -- bool )
896 : first-operand ( operands -- operand )
899 : rest-operands ( operands -- other-operands )
902 : list-of-vals ( args env -- vals )
908 2over 2over first-operand 2swap eval
909 -2rot rest-operands 2swap recurse
914 :noname ( obj env -- result )
934 2swap eval-definition
939 2swap eval-assignment
961 primitive-proc-type istype? false = if
962 bold fg red ." Object not applicable. Aboring." reset-term cr
967 operands 2swap list-of-vals
973 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
979 \ ---- Print ---- {{{
983 : printnum ( numobj -- ) drop 0 .R ;
985 : printbool ( numobj -- )
993 : printchar ( charobj -- )
997 bl of ." #\space" endof
998 '\n' of ." #\newline" endof
1004 : (printstring) ( stringobj -- )
1005 nil-type istype? if 2drop exit then
1009 '\n' of ." \n" drop endof
1010 [char] \ of ." \\" drop endof
1011 [char] " of [char] \ emit [char] " emit drop endof
1017 : printstring ( stringobj -- )
1022 : printsymbol ( symbolobj -- )
1023 nil-type istype? if 2drop exit then
1029 : printnil ( nilobj -- )
1032 : printpair ( pairobj -- )
1036 nil-type istype? if 2drop exit then
1037 pair-type istype? if space recurse exit then
1041 : printprim ( primobj -- )
1042 2drop ." <primitive procedure>" ;
1045 fixnum-type istype? if printnum exit then
1046 boolean-type istype? if printbool exit then
1047 character-type istype? if printchar exit then
1048 string-type istype? if printstring exit then
1049 symbol-type istype? if printsymbol exit then
1050 nil-type istype? if printnil exit then
1051 pair-type istype? if ." (" printpair ." )" exit then
1052 primitive-proc-type istype? if printprim exit then
1054 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1063 cr ." Welcome to scheme.forth.jl!" cr
1064 ." Use Ctrl-D to exit." cr
1069 cr bold fg green ." > " reset-term
1071 global-env fetchobj eval
1072 fg cyan ." ; " print reset-term