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 )
522 \ Record starting parse idx:
523 \ Want to detect whether any characters (following +/-) were eaten.
530 [char] . nextchar = if
537 [char] e nextchar = [char] E nextchar = or if
545 drop pop-parse-idx false exit
553 \ This is a real number if characters were
554 \ eaten and the next characer is a delimiter.
555 parse-idx @ < delim? and
560 : boolean? ( -- bool )
561 nextchar [char] # <> if false exit then
568 and if pop-parse-idx false exit then
580 : str-equiv? ( str -- bool )
597 delim? false = if drop false then
602 : character? ( -- bool )
603 nextchar [char] # <> if false exit then
608 nextchar [char] \ <> if pop-parse-idx false exit then
612 S" newline" str-equiv? if pop-parse-idx true exit then
613 S" space" str-equiv? if pop-parse-idx true exit then
614 S" tab" str-equiv? if pop-parse-idx true exit then
616 charavailable? false = if pop-parse-idx false exit then
622 nextchar [char] ( = ;
624 : string? ( -- bool )
625 nextchar [char] " = ;
627 : readfixnum ( -- num-atom )
638 10 * nextchar [char] 0 - +
647 : readrealnum ( -- realnum )
649 \ Remember that at this point we're guaranteed to
650 \ have a parsable real on this line.
652 parse-str parse-idx @ +
654 begin delim? false = while
658 parse-str parse-idx @ + over -
665 : readbool ( -- bool-atom )
668 nextchar [char] f = if
679 : readchar ( -- char-atom )
683 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
684 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
685 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
687 nextchar character-type
692 : readstring ( -- charlist )
693 nextchar [char] " = if
698 ." No delimiter following right double quote. Aborting." cr
707 nextchar [char] \ = if
710 [char] n of '\n' endof
711 [char] " of [char] " endof
717 inc-parse-idx character-type
724 : readsymbol ( -- charlist )
725 delim? if nil exit then
727 nextchar inc-parse-idx character-type
734 : readpair ( -- pairobj )
738 nextchar [char] ) = if
743 ." No delimiter following right paren. Aborting." cr
752 \ Read first pair element
757 nextchar [char] . = if
762 ." No delimiter following '.'. Aborting." cr
776 \ Parse a scheme expression
807 nextchar [char] " <> if
808 bold red ." Missing closing double-quote." reset-term cr
826 nextchar [char] ) <> if
827 bold red ." Missing closing paren." reset-term cr
836 nextchar [char] ' = if
838 quote-symbol recurse nil cons cons exit
843 bold fg blue ." Moriturus te saluto." reset-term ." ok" cr
847 \ Anything else is parsed as a symbol
848 readsymbol charlist>symbol
850 \ Replace λ with lambda
851 2dup λ-symbol objeq? if
862 : self-evaluating? ( obj -- obj bool )
863 boolean-type istype? if true exit then
864 fixnum-type istype? if true exit then
865 realnum-type istype? if true exit then
866 character-type istype? if true exit then
867 string-type istype? if true exit then
868 nil-type istype? if true exit then
873 : tagged-list? ( obj tag-obj -- obj bool )
875 pair-type istype? false = if
881 : quote? ( obj -- obj bool )
882 quote-symbol tagged-list? ;
884 : quote-body ( quote-obj -- quote-body-obj )
887 : variable? ( obj -- obj bool )
888 symbol-type istype? ;
890 : definition? ( obj -- obj bool )
891 define-symbol tagged-list? ;
893 : make-lambda ( params body -- lambda-exp )
894 lambda-symbol -2rot cons cons ;
896 : definition-var ( obj -- var )
898 symbol-type istype? false = if car then
901 : definition-val ( obj -- val )
902 2dup cdr car symbol-type istype? if
911 : assignment? ( obj -- obj bool )
912 set!-symbol tagged-list? ;
914 : assignment-var ( obj -- var )
917 : assignment-val ( obj -- val )
920 : eval-definition ( obj env -- res )
922 2over 2over ( env obj env obj )
923 definition-val 2swap ( env obj valexp env )
926 2swap definition-var 2swap ( env var val )
934 : eval-assignment ( obj env -- res )
936 2over 2over ( env obj env obj )
937 assignment-val 2swap ( env obj valexp env )
940 2swap assignment-var 2swap ( env var val )
948 : if? ( obj -- obj bool )
949 if-symbol tagged-list? ;
951 : if-predicate ( ifobj -- pred )
954 : if-consequent ( ifobj -- conseq )
957 : if-alternative ( ifobj -- alt|false )
965 : false? ( boolobj -- boolean )
966 boolean-type istype? if
967 false boolean-type objeq?
973 : true? ( boolobj -- bool )
976 : lambda? ( obj -- obj bool )
977 lambda-symbol tagged-list? ;
979 : lambda-parameters ( obj -- params )
982 : lambda-body ( obj -- body )
985 : make-procedure ( params body env -- proc )
988 drop compound-proc-type
991 : application? ( obj -- obj bool)
994 : operator ( obj -- operator )
997 : operands ( obj -- operands )
1000 : nooperands? ( operands -- bool )
1003 : first-operand ( operands -- operand )
1006 : rest-operands ( operands -- other-operands )
1009 : list-of-vals ( args env -- vals )
1015 2over 2over first-operand 2swap eval
1016 -2rot rest-operands 2swap recurse
1021 : procedure-params ( proc -- params )
1022 drop pair-type car ;
1024 : procedure-body ( proc -- body )
1025 drop pair-type cdr car ;
1027 : procedure-env ( proc -- body )
1028 drop pair-type cdr cdr car ;
1030 : apply ( proc args )
1032 primitive-proc-type of
1036 compound-proc-type of
1037 2dup procedure-body ( args proc body )
1038 -2rot 2dup procedure-params ( body args proc params )
1039 -2rot procedure-env ( body params args procenv )
1041 extend-env ( body env )
1046 2dup cdr 2dup nil objeq? false =
1048 -2rot car 2over ( nextbody env exp env )
1050 2drop \ discard result
1051 2swap ( env nextbody )
1055 car 2swap ( exp env )
1057 R> drop ['] eval goto-deferred \ Tail call optimization
1060 bold fg red ." Object not applicable. Aboring." reset-term cr
1065 :noname ( obj env -- result )
1085 2swap eval-definition
1090 2swap eval-assignment
1106 ['] eval goto-deferred
1110 2dup lambda-parameters
1120 operands 2swap list-of-vals
1126 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1132 \ ---- Print ---- {{{
1134 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1136 : printrealnum ( realnumobj -- ) drop float-print ;
1138 : printbool ( numobj -- )
1146 : printchar ( charobj -- )
1149 9 of ." #\tab" endof
1150 bl of ." #\space" endof
1151 '\n' of ." #\newline" endof
1157 : (printstring) ( stringobj -- )
1158 nil-type istype? if 2drop exit then
1162 '\n' of ." \n" drop endof
1163 [char] \ of ." \\" drop endof
1164 [char] " of [char] \ emit [char] " emit drop endof
1170 : printstring ( stringobj -- )
1175 : printsymbol ( symbolobj -- )
1176 nil-type istype? if 2drop exit then
1182 : printnil ( nilobj -- )
1185 : printpair ( pairobj -- )
1189 nil-type istype? if 2drop exit then
1190 pair-type istype? if space recurse exit then
1194 : printprim ( primobj -- )
1195 2drop ." <primitive procedure>" ;
1197 : printcomp ( primobj -- )
1198 2drop ." <compound procedure>" ;
1201 fixnum-type istype? if printfixnum exit then
1202 realnum-type istype? if printrealnum exit then
1203 boolean-type istype? if printbool exit then
1204 character-type istype? if printchar exit then
1205 string-type istype? if printstring exit then
1206 symbol-type istype? if printsymbol exit then
1207 nil-type istype? if printnil exit then
1208 pair-type istype? if ." (" printpair ." )" exit then
1209 primitive-proc-type istype? if printprim exit then
1210 compound-proc-type istype? if printcomp exit then
1212 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1221 cr ." Welcome to scheme.forth.jl!" cr
1222 ." Use Ctrl-D to exit." cr
1227 cr bold fg green ." > " reset-term
1229 global-env obj@ eval
1230 fg cyan ." ; " print reset-term