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 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
82 : obj@ ( 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 )
161 drop symbol-type 2dup
162 symbol-table obj@ cons
167 : (create-symbol) ( addr n -- symbol-obj )
171 2dup drop @ character-type 2swap
179 : create-symbol ( -- )
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
202 create-symbol λ λ-symbol
206 \ ---- Environments ---- {{{
208 : enclosing-env ( env -- env )
211 : first-frame ( env -- frame )
214 : make-frame ( vars vals -- frame )
217 : frame-vars ( frame -- vars )
220 : frame-vals ( frame -- vals )
223 : add-binding ( var val frame -- )
224 2swap 2over frame-vals cons
226 2swap 2over frame-vars cons
230 : extend-env ( vars vals env -- env )
240 : get-vars-vals-frame ( var frame -- bool )
241 2dup frame-vars vars obj!
245 vars obj@ nil objeq? false =
247 2dup vars obj@ car objeq? if
252 vars obj@ cdr vars obj!
253 vals obj@ cdr vals obj!
259 : get-vars-vals ( var env -- vars? vals? bool )
262 2dup nil objeq? false =
264 2over 2over first-frame
265 get-vars-vals-frame if
267 vars obj@ vals obj@ true
281 : lookup-var ( var env -- val )
285 bold fg red ." Tried to read unbound variable." reset-term cr abort
289 : set-var ( var val env -- )
290 >R >R 2swap R> R> ( val var env )
292 2swap 2drop ( val vals )
295 bold fg red ." Tried to set unbound variable." reset-term cr abort
301 : define-var ( var val env -- )
304 2over env obj@ ( var val var env )
306 2swap 2drop ( var val vals )
311 first-frame ( var val frame )
319 nil nil nil extend-env
324 \ ---- Primitives ---- {{{
326 : make-primitive ( cfa -- )
339 rot primitive-proc-type ( var prim )
340 global-env obj@ define-var
344 bold fg red ." Incorrect argument count." reset-term cr
348 : ensure-arg-count ( args n -- )
350 drop nil objeq? false = if
354 -rot 2dup nil objeq? if
363 bold fg red ." Incorrect argument type." reset-term cr
367 : ensure-arg-type ( arg type -- arg )
373 include scheme-primitives.4th
382 variable stored-parse-idx
383 create parse-str 161 allot
384 variable parse-str-span
386 create parse-idx-stack 10 allot
387 variable parse-idx-sp
388 parse-idx-stack parse-idx-sp !
391 parse-idx @ parse-idx-sp @ !
396 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
400 parse-idx-sp @ @ parse-idx ! ;
404 '\n' parse-str parse-str-span @ + !
405 1 parse-str-span +! ;
412 parse-str 160 expect cr
413 span @ parse-str-span !
423 : charavailable? ( -- bool )
424 parse-str-span @ parse-idx @ > ;
426 : nextchar ( -- char )
427 charavailable? false = if getline then
428 parse-str parse-idx @ + @ ;
430 : whitespace? ( -- bool )
439 nextchar [char] ( = or
440 nextchar [char] ) = or
443 : commentstart? ( -- bool )
444 nextchar [char] ; = ;
448 false \ Indicates whether or not we're eating a comment
451 dup whitespace? or commentstart? or
453 dup nextchar '\n' = and if
454 invert \ Stop eating comment
456 dup false = commentstart? and if
457 invert \ Begin eating comment
472 nextchar [char] - = ;
475 nextchar [char] + = ;
477 : fixnum? ( -- bool )
509 : boolean? ( -- bool )
510 nextchar [char] # <> if false exit then
517 and if pop-parse-idx false exit then
529 : str-equiv? ( str -- bool )
546 delim? false = if drop false then
551 : character? ( -- bool )
552 nextchar [char] # <> if false exit then
557 nextchar [char] \ <> if pop-parse-idx false exit then
561 S" newline" str-equiv? if pop-parse-idx true exit then
562 S" space" str-equiv? if pop-parse-idx true exit then
563 S" tab" str-equiv? if pop-parse-idx true exit then
565 charavailable? false = if pop-parse-idx false exit then
571 nextchar [char] ( = ;
573 : string? ( -- bool )
574 nextchar [char] " = ;
576 : readnum ( -- num-atom )
587 10 * nextchar [char] 0 - +
596 : readbool ( -- bool-atom )
599 nextchar [char] f = if
610 : readchar ( -- char-atom )
614 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
615 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
616 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
618 nextchar character-type
623 : readstring ( -- charlist )
624 nextchar [char] " = if
629 ." No delimiter following right double quote. Aborting." cr
638 nextchar [char] \ = if
641 [char] n of '\n' endof
642 [char] " of [char] " endof
648 inc-parse-idx character-type
655 : readsymbol ( -- charlist )
656 delim? if nil exit then
658 nextchar inc-parse-idx character-type
665 : readpair ( -- pairobj )
669 nextchar [char] ) = if
674 ." No delimiter following right paren. Aborting." cr
683 \ Read first pair element
688 nextchar [char] . = if
693 ." No delimiter following '.'. Aborting." cr
707 \ Parse a scheme expression
733 nextchar [char] " <> if
734 bold red ." Missing closing double-quote." reset-term cr
752 nextchar [char] ) <> if
753 bold red ." Missing closing paren." reset-term cr
762 nextchar [char] ' = if
764 quote-symbol recurse nil cons cons exit
769 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
773 \ Anything else is parsed as a symbol
774 readsymbol charlist>symbol
776 \ Replace λ with lambda
777 2dup λ-symbol objeq? if
790 : self-evaluating? ( obj -- obj bool )
791 boolean-type istype? if true exit then
792 fixnum-type istype? if true exit then
793 character-type istype? if true exit then
794 string-type istype? if true exit then
795 nil-type istype? if true exit then
800 : tagged-list? ( obj tag-obj -- obj bool )
802 pair-type istype? false = if
808 : quote? ( obj -- obj bool )
809 quote-symbol tagged-list? ;
811 : quote-body ( quote-obj -- quote-body-obj )
814 : variable? ( obj -- obj bool )
815 symbol-type istype? ;
817 : definition? ( obj -- obj bool )
818 define-symbol tagged-list? ;
820 : make-lambda ( params body -- lambda-exp )
821 lambda-symbol -2rot cons cons ;
823 : definition-var ( obj -- var )
825 symbol-type istype? false = if car then
828 : definition-val ( obj -- val )
829 2dup cdr car symbol-type istype? if
838 : assignment? ( obj -- obj bool )
839 set!-symbol tagged-list? ;
841 : assignment-var ( obj -- var )
844 : assignment-val ( obj -- val )
847 : eval-definition ( obj env -- res )
849 2over 2over ( env obj env obj )
850 definition-val 2swap ( env obj valexp env )
853 2swap definition-var 2swap ( env var val )
861 : eval-assignment ( obj env -- res )
863 2over 2over ( env obj env obj )
864 assignment-val 2swap ( env obj valexp env )
867 2swap assignment-var 2swap ( env var val )
875 : if? ( obj -- obj bool )
876 if-symbol tagged-list? ;
878 : if-predicate ( ifobj -- pred )
881 : if-consequent ( ifobj -- conseq )
884 : if-alternative ( ifobj -- alt|false )
892 : false? ( boolobj -- boolean )
893 boolean-type istype? if
894 false boolean-type objeq?
900 : true? ( boolobj -- bool )
903 : lambda? ( obj -- obj bool )
904 lambda-symbol tagged-list? ;
906 : lambda-parameters ( obj -- params )
909 : lambda-body ( obj -- body )
912 : make-procedure ( params body env -- proc )
915 drop compound-proc-type
918 : application? ( obj -- obj bool)
921 : operator ( obj -- operator )
924 : operands ( obj -- operands )
927 : nooperands? ( operands -- bool )
930 : first-operand ( operands -- operand )
933 : rest-operands ( operands -- other-operands )
936 : list-of-vals ( args env -- vals )
942 2over 2over first-operand 2swap eval
943 -2rot rest-operands 2swap recurse
948 : procedure-params ( proc -- params )
951 : procedure-body ( proc -- body )
952 drop pair-type cdr car ;
954 : procedure-env ( proc -- body )
955 drop pair-type cdr cdr car ;
957 : apply ( proc args )
959 primitive-proc-type of
963 compound-proc-type of
964 2dup procedure-body ( args proc body )
965 -2rot 2dup procedure-params ( body args proc params )
966 -2rot procedure-env ( body params args procenv )
968 extend-env ( body env )
973 2dup cdr 2dup nil objeq? false =
975 -2rot car over ( nextbody env exp env )
977 2drop \ discard result
978 2swap ( env nextbody )
982 car 2swap ( exp env )
984 ['] eval goto-prime \ Tail call optimization
985 \ eval \ No tail call optimization
988 bold fg red ." Object not applicable. Aboring." reset-term cr
993 :noname ( obj env -- result )
1013 2swap eval-definition
1018 2swap eval-assignment
1037 2dup lambda-parameters
1047 operands 2swap list-of-vals
1053 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1059 \ ---- Print ---- {{{
1063 : printnum ( numobj -- ) drop 0 .R ;
1065 : printbool ( numobj -- )
1073 : printchar ( charobj -- )
1076 9 of ." #\tab" endof
1077 bl of ." #\space" endof
1078 '\n' of ." #\newline" endof
1084 : (printstring) ( stringobj -- )
1085 nil-type istype? if 2drop exit then
1089 '\n' of ." \n" drop endof
1090 [char] \ of ." \\" drop endof
1091 [char] " of [char] \ emit [char] " emit drop endof
1097 : printstring ( stringobj -- )
1102 : printsymbol ( symbolobj -- )
1103 nil-type istype? if 2drop exit then
1109 : printnil ( nilobj -- )
1112 : printpair ( pairobj -- )
1116 nil-type istype? if 2drop exit then
1117 pair-type istype? if space recurse exit then
1121 : printprim ( primobj -- )
1122 2drop ." <primitive procedure>" ;
1124 : printcomp ( primobj -- )
1125 2drop ." <compound procedure>" ;
1128 fixnum-type istype? if printnum exit then
1129 boolean-type istype? if printbool exit then
1130 character-type istype? if printchar exit then
1131 string-type istype? if printstring exit then
1132 symbol-type istype? if printsymbol exit then
1133 nil-type istype? if printnil exit then
1134 pair-type istype? if ." (" printpair ." )" exit then
1135 primitive-proc-type istype? if printprim exit then
1136 compound-proc-type istype? if printcomp exit then
1138 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1147 cr ." Welcome to scheme.forth.jl!" cr
1148 ." Use Ctrl-D to exit." cr
1153 cr bold fg green ." > " reset-term
1155 global-env obj@ eval
1156 fg cyan ." ; " print reset-term