4 include term-colours.4th
6 include throw-catch.4th
19 nexttype @ 1+ nexttype !
23 make-type realnum-type
24 make-type boolean-type
25 make-type character-type
30 make-type primitive-proc-type
31 make-type compound-proc-type
32 : istype? ( obj type -- obj bool )
35 \ ------ Cons cell memory ------ {{{
38 create car-cells N allot
39 create car-type-cells N allot
40 create cdr-cells N allot
41 create cdr-type-cells N allot
46 : cons ( car-obj cdr-obj -- pair-obj )
47 cdr-type-cells nextfree @ + !
48 cdr-cells nextfree @ + !
49 car-type-cells nextfree @ + !
50 car-cells nextfree @ + !
57 : car ( pair-obj -- car-obj )
59 dup car-cells + @ swap
63 : cdr ( pair-obj -- car-obj )
65 dup cdr-cells + @ swap
69 : set-car! ( obj pair-obj -- )
71 rot swap car-type-cells + !
75 : set-cdr! ( obj pair-obj -- )
77 rot swap cdr-type-cells + !
87 : nil? nil-type istype? ;
89 : objvar create nil swap , , ;
91 : value@ ( objvar -- val ) @ ;
92 : type@ ( objvar -- type ) 1+ @ ;
93 : value! ( newval objvar -- ) ! ;
94 : type! ( newtype objvar -- ) 1+ ! ;
95 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
96 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
98 : objeq? ( obj obj -- bool )
101 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
102 >R >R ( a1 a2 b1 b2 )
103 2swap ( b1 b2 a1 a2 )
104 R> R> ( b1 b2 a1 a2 c1 c2 )
108 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
109 2swap ( a1 a2 c1 c2 b1 b2 )
110 >R >R ( a1 a2 c1 c2 )
111 2swap ( c1 c2 a1 a2 )
117 \ ---- Pre-defined symbols ---- {{{
121 : duplicate-charlist ( charlist -- copy )
122 2dup nil objeq? false = if
123 2dup car 2swap cdr recurse cons
126 : charlist-equiv ( charlist charlist -- bool )
135 2drop 2drop true exit
137 2drop 2drop false exit
142 2drop 2drop false exit
149 car drop -rot car drop = if
150 cdr 2swap cdr recurse
156 : charlist>symbol ( charlist -- symbol-obj )
175 drop symbol-type 2dup
176 symbol-table obj@ cons
181 : (create-symbol) ( addr n -- symbol-obj )
185 2dup drop @ character-type 2swap
193 : create-symbol ( -- )
207 does> dup @ swap 1+ @
210 create-symbol quote quote-symbol
211 create-symbol define define-symbol
212 create-symbol set! set!-symbol
213 create-symbol ok ok-symbol
214 create-symbol if if-symbol
215 create-symbol lambda lambda-symbol
216 create-symbol λ λ-symbol
220 \ ---- Environments ---- {{{
222 : enclosing-env ( env -- env )
225 : first-frame ( env -- frame )
228 : make-frame ( vars vals -- frame )
231 : frame-vars ( frame -- vars )
234 : frame-vals ( frame -- vals )
237 : add-binding ( var val frame -- )
238 2swap 2over frame-vals cons
240 2swap 2over frame-vars cons
244 : extend-env ( vars vals env -- env )
254 : get-vars-vals-frame ( var frame -- bool )
255 2dup frame-vars vars obj!
259 vars obj@ nil objeq? false =
261 2dup vars obj@ car objeq? if
266 vars obj@ cdr vars obj!
267 vals obj@ cdr vals obj!
273 : get-vars-vals ( var env -- vars? vals? bool )
276 2dup nil objeq? false =
278 2over 2over first-frame
279 get-vars-vals-frame if
281 vars obj@ vals obj@ true
295 : lookup-var ( var env -- val )
299 bold fg red ." Tried to read unbound variable." reset-term cr abort
303 : set-var ( var val env -- )
304 >R >R 2swap R> R> ( val var env )
306 2swap 2drop ( val vals )
309 bold fg red ." Tried to set unbound variable." reset-term cr abort
315 : define-var ( var val env -- )
318 2over env obj@ ( var val var env )
320 2swap 2drop ( var val vals )
325 first-frame ( var val frame )
333 nil nil nil extend-env
338 \ ---- Primitives ---- {{{
340 : make-primitive ( cfa -- )
353 rot primitive-proc-type ( var prim )
354 global-env obj@ define-var
358 bold fg red ." Incorrect argument count." reset-term cr
362 : ensure-arg-count ( args n -- )
364 drop nil objeq? false = if
368 -rot 2dup nil objeq? if
377 bold fg red ." Incorrect argument type." reset-term cr
381 : ensure-arg-type ( arg type -- arg )
387 include scheme-primitives.4th
394 variable stored-parse-idx
395 create parse-str 161 allot
396 variable parse-str-span
398 create parse-idx-stack 10 allot
399 variable parse-idx-sp
400 parse-idx-stack parse-idx-sp !
403 parse-idx @ parse-idx-sp @ !
408 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
412 parse-idx-sp @ @ parse-idx ! ;
416 '\n' parse-str parse-str-span @ + !
417 1 parse-str-span +! ;
424 parse-str 160 expect cr
425 span @ parse-str-span !
435 : charavailable? ( -- bool )
436 parse-str-span @ parse-idx @ > ;
438 : nextchar ( -- char )
439 charavailable? false = if getline then
440 parse-str parse-idx @ + @ ;
442 : whitespace? ( -- bool )
451 nextchar [char] ( = or
452 nextchar [char] ) = or
455 : commentstart? ( -- bool )
456 nextchar [char] ; = ;
460 false \ Indicates whether or not we're eating a comment
463 dup whitespace? or commentstart? or
465 dup nextchar '\n' = and if
466 invert \ Stop eating comment
468 dup false = commentstart? and if
469 invert \ Begin eating comment
484 nextchar [char] - = ;
487 nextchar [char] + = ;
489 : fixnum? ( -- bool )
515 : realnum? ( -- bool )
516 \ Record starting parse idx:
517 \ Want to detect whether any characters were eaten.
530 [char] . nextchar = if
537 [char] e nextchar = [char] E nextchar = or if
549 \ This is a real number if characters were
550 \ eaten and the next characer is a delimiter.
551 parse-idx @ < delim? and
556 : boolean? ( -- bool )
557 nextchar [char] # <> if false exit then
564 and if pop-parse-idx false exit then
576 : str-equiv? ( str -- bool )
593 delim? false = if drop false then
598 : character? ( -- bool )
599 nextchar [char] # <> if false exit then
604 nextchar [char] \ <> if pop-parse-idx false exit then
608 S" newline" str-equiv? if pop-parse-idx true exit then
609 S" space" str-equiv? if pop-parse-idx true exit then
610 S" tab" str-equiv? if pop-parse-idx true exit then
612 charavailable? false = if pop-parse-idx false exit then
618 nextchar [char] ( = ;
620 : string? ( -- bool )
621 nextchar [char] " = ;
623 : readfixnum ( -- num-atom )
634 10 * nextchar [char] 0 - +
643 : readrealnum ( -- realnum )
645 \ Remember that at this point we're guaranteed to
646 \ have a parsable real on this line.
648 parse-str parse-idx @ +
650 begin delim? false = while
654 parse-str parse-idx @ + over -
661 : readbool ( -- bool-atom )
664 nextchar [char] f = if
675 : readchar ( -- char-atom )
679 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
680 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
681 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
683 nextchar character-type
688 : readstring ( -- charlist )
689 nextchar [char] " = if
694 ." No delimiter following right double quote. Aborting." cr
703 nextchar [char] \ = if
706 [char] n of '\n' endof
707 [char] " of [char] " endof
713 inc-parse-idx character-type
720 : readsymbol ( -- charlist )
721 delim? if nil exit then
723 nextchar inc-parse-idx character-type
730 : readpair ( -- pairobj )
734 nextchar [char] ) = if
739 ." No delimiter following right paren. Aborting." cr
748 \ Read first pair element
753 nextchar [char] . = if
758 ." No delimiter following '.'. Aborting." cr
772 \ Parse a scheme expression
803 nextchar [char] " <> if
804 bold red ." Missing closing double-quote." reset-term cr
822 nextchar [char] ) <> if
823 bold red ." Missing closing paren." reset-term cr
832 nextchar [char] ' = if
834 quote-symbol recurse nil cons cons exit
839 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
843 \ Anything else is parsed as a symbol
844 readsymbol charlist>symbol
846 \ Replace λ with lambda
847 2dup λ-symbol objeq? if
858 : self-evaluating? ( obj -- obj bool )
859 boolean-type istype? if true exit then
860 fixnum-type istype? if true exit then
861 realnum-type istype? if true exit then
862 character-type istype? if true exit then
863 string-type istype? if true exit then
864 nil-type istype? if true exit then
869 : tagged-list? ( obj tag-obj -- obj bool )
871 pair-type istype? false = if
877 : quote? ( obj -- obj bool )
878 quote-symbol tagged-list? ;
880 : quote-body ( quote-obj -- quote-body-obj )
883 : variable? ( obj -- obj bool )
884 symbol-type istype? ;
886 : definition? ( obj -- obj bool )
887 define-symbol tagged-list? ;
889 : make-lambda ( params body -- lambda-exp )
890 lambda-symbol -2rot cons cons ;
892 : definition-var ( obj -- var )
894 symbol-type istype? false = if car then
897 : definition-val ( obj -- val )
898 2dup cdr car symbol-type istype? if
907 : assignment? ( obj -- obj bool )
908 set!-symbol tagged-list? ;
910 : assignment-var ( obj -- var )
913 : assignment-val ( obj -- val )
916 : eval-definition ( obj env -- res )
918 2over 2over ( env obj env obj )
919 definition-val 2swap ( env obj valexp env )
922 2swap definition-var 2swap ( env var val )
930 : eval-assignment ( obj env -- res )
932 2over 2over ( env obj env obj )
933 assignment-val 2swap ( env obj valexp env )
936 2swap assignment-var 2swap ( env var val )
944 : if? ( obj -- obj bool )
945 if-symbol tagged-list? ;
947 : if-predicate ( ifobj -- pred )
950 : if-consequent ( ifobj -- conseq )
953 : if-alternative ( ifobj -- alt|false )
961 : false? ( boolobj -- boolean )
962 boolean-type istype? if
963 false boolean-type objeq?
969 : true? ( boolobj -- bool )
972 : lambda? ( obj -- obj bool )
973 lambda-symbol tagged-list? ;
975 : lambda-parameters ( obj -- params )
978 : lambda-body ( obj -- body )
981 : make-procedure ( params body env -- proc )
984 drop compound-proc-type
987 : application? ( obj -- obj bool)
990 : operator ( obj -- operator )
993 : operands ( obj -- operands )
996 : nooperands? ( operands -- bool )
999 : first-operand ( operands -- operand )
1002 : rest-operands ( operands -- other-operands )
1005 : list-of-vals ( args env -- vals )
1011 2over 2over first-operand 2swap eval
1012 -2rot rest-operands 2swap recurse
1017 : procedure-params ( proc -- params )
1018 drop pair-type car ;
1020 : procedure-body ( proc -- body )
1021 drop pair-type cdr car ;
1023 : procedure-env ( proc -- body )
1024 drop pair-type cdr cdr car ;
1026 : apply ( proc args )
1028 primitive-proc-type of
1032 compound-proc-type of
1033 2dup procedure-body ( args proc body )
1034 -2rot 2dup procedure-params ( body args proc params )
1035 -2rot procedure-env ( body params args procenv )
1037 extend-env ( body env )
1042 2dup cdr 2dup nil objeq? false =
1044 -2rot car 2over ( nextbody env exp env )
1046 2drop \ discard result
1047 2swap ( env nextbody )
1051 car 2swap ( exp env )
1053 R> drop ['] eval goto-deferred \ Tail call optimization
1056 bold fg red ." Object not applicable. Aboring." reset-term cr
1061 :noname ( obj env -- result )
1081 2swap eval-definition
1086 2swap eval-assignment
1102 ['] eval goto-deferred
1106 2dup lambda-parameters
1116 operands 2swap list-of-vals
1122 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1128 \ ---- Print ---- {{{
1130 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1132 : printrealnum ( realnumobj -- ) drop float-print ;
1134 : printbool ( numobj -- )
1142 : printchar ( charobj -- )
1145 9 of ." #\tab" endof
1146 bl of ." #\space" endof
1147 '\n' of ." #\newline" endof
1153 : (printstring) ( stringobj -- )
1154 nil-type istype? if 2drop exit then
1158 '\n' of ." \n" drop endof
1159 [char] \ of ." \\" drop endof
1160 [char] " of [char] \ emit [char] " emit drop endof
1166 : printstring ( stringobj -- )
1171 : printsymbol ( symbolobj -- )
1172 nil-type istype? if 2drop exit then
1178 : printnil ( nilobj -- )
1181 : printpair ( pairobj -- )
1185 nil-type istype? if 2drop exit then
1186 pair-type istype? if space recurse exit then
1190 : printprim ( primobj -- )
1191 2drop ." <primitive procedure>" ;
1193 : printcomp ( primobj -- )
1194 2drop ." <compound procedure>" ;
1197 fixnum-type istype? if printfixnum exit then
1198 realnum-type istype? if printrealnum exit then
1199 boolean-type istype? if printbool exit then
1200 character-type istype? if printchar exit then
1201 string-type istype? if printstring exit then
1202 symbol-type istype? if printsymbol exit then
1203 nil-type istype? if printnil exit then
1204 pair-type istype? if ." (" printpair ." )" exit then
1205 primitive-proc-type istype? if printprim exit then
1206 compound-proc-type istype? if printcomp exit then
1208 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1217 cr ." Welcome to scheme.forth.jl!" cr
1218 ." Use Ctrl-D to exit." cr
1223 cr bold fg green ." > " reset-term
1225 global-env obj@ eval
1226 fg cyan ." ; " print reset-term