4 include term-colours.4th
24 make-type realnum-type
25 make-type boolean-type
26 make-type character-type
31 make-type primitive-proc-type
32 make-type compound-proc-type
33 : istype? ( obj type -- obj bool )
36 \ ------ Cons cell memory ------ {{{
38 1000 constant scheme-memsize
39 create car-cells scheme-memsize allot
40 create car-type-cells scheme-memsize allot
41 create cdr-cells scheme-memsize allot
42 create cdr-type-cells scheme-memsize allot
44 create nextfrees scheme-memsize allot
55 nextfrees nextfree @ + @
57 dup scheme-memsize < if
61 ." Out of memory! Aborting."
66 : cons ( car-obj cdr-obj -- pair-obj )
67 cdr-type-cells nextfree @ + !
68 cdr-cells nextfree @ + !
69 car-type-cells nextfree @ + !
70 car-cells nextfree @ + !
76 : car ( pair-obj -- car-obj )
78 dup car-cells + @ swap
82 : cdr ( pair-obj -- car-obj )
84 dup cdr-cells + @ swap
88 : set-car! ( obj pair-obj -- )
90 rot swap car-type-cells + !
94 : set-cdr! ( obj pair-obj -- )
96 rot swap cdr-type-cells + !
106 : nil? nil-type istype? ;
108 : objvar create nil swap , , ;
110 : value@ ( objvar -- val ) @ ;
111 : type@ ( objvar -- type ) 1+ @ ;
112 : value! ( newval objvar -- ) ! ;
113 : type! ( newtype objvar -- ) 1+ ! ;
114 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
115 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
117 : objeq? ( obj obj -- bool )
120 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
121 >R >R ( a1 a2 b1 b2 )
122 2swap ( b1 b2 a1 a2 )
123 R> R> ( b1 b2 a1 a2 c1 c2 )
127 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
128 2swap ( a1 a2 c1 c2 b1 b2 )
129 >R >R ( a1 a2 c1 c2 )
130 2swap ( c1 c2 a1 a2 )
136 \ ---- Garbage Collection ---- {{{
150 : pairlike? ( obj -- obj bool )
151 pair-type istype? if true exit then
152 string-type istype? if true exit then
153 symbol-type istype? if true exit then
154 compound-proc-type istype? if true exit then
159 : pairlike-marked? ( obj -- obj bool )
160 over nextfrees + @ 0=
163 : mark-pairlike ( obj -- obj )
164 over nextfrees + 0 swap !
174 : gc-mark-obj ( obj -- )
178 pairlike? invert if 2drop exit then
179 pairlike-marked? if 2drop exit then
190 scheme-memsize nextfree !
191 0 scheme-memsize 1- do
192 nextfrees i + @ 0<> if
193 nextfree @ nextfrees i + !
201 \ ---- Pre-defined symbols ---- {{{
205 : duplicate-charlist ( charlist -- copy )
206 2dup nil objeq? false = if
207 2dup car 2swap cdr recurse cons
210 : charlist-equiv ( charlist charlist -- bool )
219 2drop 2drop true exit
221 2drop 2drop false exit
226 2drop 2drop false exit
233 car drop -rot car drop = if
234 cdr 2swap cdr recurse
240 : charlist>symbol ( charlist -- symbol-obj )
259 drop symbol-type 2dup
260 symbol-table obj@ cons
265 : (create-symbol) ( addr n -- symbol-obj )
269 2dup drop @ character-type 2swap
277 : create-symbol ( -- )
291 does> dup @ swap 1+ @
294 create-symbol quote quote-symbol
295 create-symbol define define-symbol
296 create-symbol set! set!-symbol
297 create-symbol ok ok-symbol
298 create-symbol if if-symbol
299 create-symbol lambda lambda-symbol
300 create-symbol λ λ-symbol
304 \ ---- Environments ---- {{{
306 : enclosing-env ( env -- env )
309 : first-frame ( env -- frame )
312 : make-frame ( vars vals -- frame )
315 : frame-vars ( frame -- vars )
318 : frame-vals ( frame -- vals )
321 : add-binding ( var val frame -- )
322 2swap 2over frame-vals cons
324 2swap 2over frame-vars cons
328 : extend-env ( vars vals env -- env )
338 : get-vars-vals-frame ( var frame -- bool )
339 2dup frame-vars vars obj!
343 vars obj@ nil objeq? false =
345 2dup vars obj@ car objeq? if
350 vars obj@ cdr vars obj!
351 vals obj@ cdr vals obj!
357 : get-vars-vals ( var env -- vars? vals? bool )
360 2dup nil objeq? false =
362 2over 2over first-frame
363 get-vars-vals-frame if
365 vars obj@ vals obj@ true
379 : lookup-var ( var env -- val )
383 bold fg red ." Tried to read unbound variable." reset-term cr abort
387 : set-var ( var val env -- )
388 >R >R 2swap R> R> ( val var env )
390 2swap 2drop ( val vals )
393 bold fg red ." Tried to set unbound variable." reset-term cr abort
399 : define-var ( var val env -- )
402 2over env obj@ ( var val var env )
404 2swap 2drop ( var val vals )
409 first-frame ( var val frame )
417 nil nil nil extend-env
422 \ ---- Primitives ---- {{{
424 : make-primitive ( cfa -- )
428 \ 2dup ." Defining primitive " type ." ..." cr
439 rot primitive-proc-type ( var prim )
440 global-env obj@ define-var
444 bold fg red ." Incorrect argument count." reset-term cr
448 : ensure-arg-count ( args n -- )
450 drop nil objeq? false = if
454 -rot 2dup nil objeq? if
463 bold fg red ." Incorrect argument type." reset-term cr
467 : ensure-arg-type ( arg type -- arg )
473 include scheme-primitives.4th
480 variable stored-parse-idx
481 create parse-str 161 allot
482 variable parse-str-span
484 create parse-idx-stack 10 allot
485 variable parse-idx-sp
486 parse-idx-stack parse-idx-sp !
489 parse-idx @ parse-idx-sp @ !
494 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
498 parse-idx-sp @ @ parse-idx ! ;
502 '\n' parse-str parse-str-span @ + !
503 1 parse-str-span +! ;
510 parse-str 160 expect cr
511 span @ parse-str-span !
521 : charavailable? ( -- bool )
522 parse-str-span @ parse-idx @ > ;
524 : nextchar ( -- char )
525 charavailable? false = if getline then
526 parse-str parse-idx @ + @ ;
528 : whitespace? ( -- bool )
537 nextchar [char] ( = or
538 nextchar [char] ) = or
541 : commentstart? ( -- bool )
542 nextchar [char] ; = ;
546 false \ Indicates whether or not we're eating a comment
549 dup whitespace? or commentstart? or
551 dup nextchar '\n' = and if
552 invert \ Stop eating comment
554 dup false = commentstart? and if
555 invert \ Begin eating comment
570 nextchar [char] - = ;
573 nextchar [char] + = ;
575 : fixnum? ( -- bool )
601 : realnum? ( -- bool )
608 \ Record starting parse idx:
609 \ Want to detect whether any characters (following +/-) were eaten.
616 [char] . nextchar = if
623 [char] e nextchar = [char] E nextchar = or if
631 drop pop-parse-idx false exit
639 \ This is a real number if characters were
640 \ eaten and the next characer is a delimiter.
641 parse-idx @ < delim? and
646 : boolean? ( -- bool )
647 nextchar [char] # <> if false exit then
654 and if pop-parse-idx false exit then
666 : str-equiv? ( str -- bool )
683 delim? false = if drop false then
688 : character? ( -- bool )
689 nextchar [char] # <> if false exit then
694 nextchar [char] \ <> if pop-parse-idx false exit then
698 S" newline" str-equiv? if pop-parse-idx true exit then
699 S" space" str-equiv? if pop-parse-idx true exit then
700 S" tab" str-equiv? if pop-parse-idx true exit then
702 charavailable? false = if pop-parse-idx false exit then
708 nextchar [char] ( = ;
710 : string? ( -- bool )
711 nextchar [char] " = ;
713 : readfixnum ( -- num-atom )
724 10 * nextchar [char] 0 - +
733 : readrealnum ( -- realnum )
735 \ Remember that at this point we're guaranteed to
736 \ have a parsable real on this line.
738 parse-str parse-idx @ +
740 begin delim? false = while
744 parse-str parse-idx @ + over -
751 : readbool ( -- bool-atom )
754 nextchar [char] f = if
765 : readchar ( -- char-atom )
769 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
770 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
771 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
773 nextchar character-type
778 : readstring ( -- charlist )
779 nextchar [char] " = if
784 ." No delimiter following right double quote. Aborting." cr
793 nextchar [char] \ = if
796 [char] n of '\n' endof
797 [char] " of [char] " endof
803 inc-parse-idx character-type
810 : readsymbol ( -- charlist )
811 delim? if nil exit then
813 nextchar inc-parse-idx character-type
820 : readpair ( -- pairobj )
824 nextchar [char] ) = if
829 ." No delimiter following right paren. Aborting." cr
838 \ Read first pair element
843 nextchar [char] . = if
848 ." No delimiter following '.'. Aborting." cr
862 \ Parse a scheme expression
893 nextchar [char] " <> if
894 bold red ." Missing closing double-quote." reset-term cr
912 nextchar [char] ) <> if
913 bold red ." Missing closing paren." reset-term cr
922 nextchar [char] ' = if
924 quote-symbol recurse nil cons cons exit
929 bold fg blue ." Moriturus te saluto." reset-term cr
933 \ Anything else is parsed as a symbol
934 readsymbol charlist>symbol
936 \ Replace λ with lambda
937 2dup λ-symbol objeq? if
948 : self-evaluating? ( obj -- obj bool )
949 boolean-type istype? if true exit then
950 fixnum-type istype? if true exit then
951 realnum-type istype? if true exit then
952 character-type istype? if true exit then
953 string-type istype? if true exit then
954 nil-type istype? if true exit then
959 : tagged-list? ( obj tag-obj -- obj bool )
961 pair-type istype? false = if
967 : quote? ( obj -- obj bool )
968 quote-symbol tagged-list? ;
970 : quote-body ( quote-obj -- quote-body-obj )
973 : variable? ( obj -- obj bool )
974 symbol-type istype? ;
976 : definition? ( obj -- obj bool )
977 define-symbol tagged-list? ;
979 : make-lambda ( params body -- lambda-exp )
980 lambda-symbol -2rot cons cons ;
982 : definition-var ( obj -- var )
984 symbol-type istype? false = if car then
987 : definition-val ( obj -- val )
988 2dup cdr car symbol-type istype? if
997 : assignment? ( obj -- obj bool )
998 set!-symbol tagged-list? ;
1000 : assignment-var ( obj -- var )
1003 : assignment-val ( obj -- val )
1006 : eval-definition ( obj env -- res )
1008 2over 2over ( env obj env obj )
1009 definition-val 2swap ( env obj valexp env )
1010 eval ( env obj val )
1012 2swap definition-var 2swap ( env var val )
1014 2rot ( var val env )
1020 : eval-assignment ( obj env -- res )
1022 2over 2over ( env obj env obj )
1023 assignment-val 2swap ( env obj valexp env )
1024 eval ( env obj val )
1026 2swap assignment-var 2swap ( env var val )
1028 2rot ( var val env )
1034 : if? ( obj -- obj bool )
1035 if-symbol tagged-list? ;
1037 : if-predicate ( ifobj -- pred )
1040 : if-consequent ( ifobj -- conseq )
1043 : if-alternative ( ifobj -- alt|false )
1051 : false? ( boolobj -- boolean )
1052 boolean-type istype? if
1053 false boolean-type objeq?
1059 : true? ( boolobj -- bool )
1062 : lambda? ( obj -- obj bool )
1063 lambda-symbol tagged-list? ;
1065 : lambda-parameters ( obj -- params )
1068 : lambda-body ( obj -- body )
1071 : make-procedure ( params body env -- proc )
1074 drop compound-proc-type
1077 : application? ( obj -- obj bool)
1080 : operator ( obj -- operator )
1083 : operands ( obj -- operands )
1086 : nooperands? ( operands -- bool )
1089 : first-operand ( operands -- operand )
1092 : rest-operands ( operands -- other-operands )
1095 : list-of-vals ( args env -- vals )
1101 2over 2over first-operand 2swap eval
1102 -2rot rest-operands 2swap recurse
1107 : procedure-params ( proc -- params )
1108 drop pair-type car ;
1110 : procedure-body ( proc -- body )
1111 drop pair-type cdr car ;
1113 : procedure-env ( proc -- body )
1114 drop pair-type cdr cdr car ;
1116 : apply ( proc args )
1118 primitive-proc-type of
1122 compound-proc-type of
1123 2dup procedure-body ( args proc body )
1124 -2rot 2dup procedure-params ( body args proc params )
1125 -2rot procedure-env ( body params args procenv )
1127 extend-env ( body env )
1132 2dup cdr 2dup nil objeq? false =
1134 -2rot car 2over ( nextbody env exp env )
1136 2drop \ discard result
1137 2swap ( env nextbody )
1141 car 2swap ( exp env )
1143 R> drop ['] eval goto-deferred \ Tail call optimization
1146 bold fg red ." Object not applicable. Aboring." reset-term cr
1151 :noname ( obj env -- result )
1171 2swap eval-definition
1176 2swap eval-assignment
1192 ['] eval goto-deferred
1196 2dup lambda-parameters
1206 operands 2swap list-of-vals
1212 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1218 \ ---- Print ---- {{{
1220 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1222 : printrealnum ( realnumobj -- ) drop float-print ;
1224 : printbool ( numobj -- )
1232 : printchar ( charobj -- )
1235 9 of ." #\tab" endof
1236 bl of ." #\space" endof
1237 '\n' of ." #\newline" endof
1243 : (printstring) ( stringobj -- )
1244 nil-type istype? if 2drop exit then
1248 '\n' of ." \n" drop endof
1249 [char] \ of ." \\" drop endof
1250 [char] " of [char] \ emit [char] " emit drop endof
1256 : printstring ( stringobj -- )
1261 : printsymbol ( symbolobj -- )
1262 nil-type istype? if 2drop exit then
1268 : printnil ( nilobj -- )
1271 : printpair ( pairobj -- )
1275 nil-type istype? if 2drop exit then
1276 pair-type istype? if space recurse exit then
1280 : printprim ( primobj -- )
1281 2drop ." <primitive procedure>" ;
1283 : printcomp ( primobj -- )
1284 2drop ." <compound procedure>" ;
1287 fixnum-type istype? if printfixnum exit then
1288 realnum-type istype? if printrealnum exit then
1289 boolean-type istype? if printbool exit then
1290 character-type istype? if printchar exit then
1291 string-type istype? if printstring exit then
1292 symbol-type istype? if printsymbol exit then
1293 nil-type istype? if printnil exit then
1294 pair-type istype? if ." (" printpair ." )" exit then
1295 primitive-proc-type istype? if printprim exit then
1296 compound-proc-type istype? if printcomp exit then
1298 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1304 \ ---- DEBUGGING ---- {{{
1306 false value debug-mode
1310 ." Object: " 2dup cr print cr
1311 ." Pairlike: " pairlike? if
1328 symbol-table obj@ gc-mark-obj
1329 global-env obj@ gc-mark-obj
1336 nextfrees i + @ 0= if 1+ then
1342 nextfrees i + @ 0<> if
1355 cr ." Welcome to scheme.forth.jl!" cr
1356 ." Use Ctrl-D to exit." cr
1362 cr bold fg green ." > " reset-term
1365 global-env obj@ eval
1367 fg cyan ." ; " print reset-term