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 + !
203 \ ---- Pre-defined symbols ---- {{{
207 : duplicate-charlist ( charlist -- copy )
208 2dup nil objeq? false = if
209 2dup car 2swap cdr recurse cons
212 : charlist-equiv ( charlist charlist -- bool )
221 2drop 2drop true exit
223 2drop 2drop false exit
228 2drop 2drop false exit
235 car drop -rot car drop = if
236 cdr 2swap cdr recurse
242 : charlist>symbol ( charlist -- symbol-obj )
261 drop symbol-type 2dup
262 symbol-table obj@ cons
267 : (create-symbol) ( addr n -- symbol-obj )
271 2dup drop @ character-type 2swap
279 : create-symbol ( -- )
293 does> dup @ swap 1+ @
296 create-symbol quote quote-symbol
297 create-symbol define define-symbol
298 create-symbol set! set!-symbol
299 create-symbol ok ok-symbol
300 create-symbol if if-symbol
301 create-symbol lambda lambda-symbol
302 create-symbol λ λ-symbol
306 \ ---- Environments ---- {{{
308 : enclosing-env ( env -- env )
311 : first-frame ( env -- frame )
314 : make-frame ( vars vals -- frame )
317 : frame-vars ( frame -- vars )
320 : frame-vals ( frame -- vals )
323 : add-binding ( var val frame -- )
324 2swap 2over frame-vals cons
326 2swap 2over frame-vars cons
330 : extend-env ( vars vals env -- env )
340 : get-vars-vals-frame ( var frame -- bool )
341 2dup frame-vars vars obj!
345 vars obj@ nil objeq? false =
347 2dup vars obj@ car objeq? if
352 vars obj@ cdr vars obj!
353 vals obj@ cdr vals obj!
359 : get-vars-vals ( var env -- vars? vals? bool )
362 2dup nil objeq? false =
364 2over 2over first-frame
365 get-vars-vals-frame if
367 vars obj@ vals obj@ true
381 : lookup-var ( var env -- val )
385 bold fg red ." Tried to read unbound variable." reset-term cr abort
389 : set-var ( var val env -- )
390 >R >R 2swap R> R> ( val var env )
392 2swap 2drop ( val vals )
395 bold fg red ." Tried to set unbound variable." reset-term cr abort
401 : define-var ( var val env -- )
404 2over env obj@ ( var val var env )
406 2swap 2drop ( var val vals )
411 first-frame ( var val frame )
419 nil nil nil extend-env
424 \ ---- Primitives ---- {{{
426 : make-primitive ( cfa -- )
430 \ 2dup ." Defining primitive " type ." ..." cr
441 rot primitive-proc-type ( var prim )
442 global-env obj@ define-var
446 bold fg red ." Incorrect argument count." reset-term cr
450 : ensure-arg-count ( args n -- )
452 drop nil objeq? false = if
456 -rot 2dup nil objeq? if
465 bold fg red ." Incorrect argument type." reset-term cr
469 : ensure-arg-type ( arg type -- arg )
475 include scheme-primitives.4th
482 variable stored-parse-idx
483 create parse-str 161 allot
484 variable parse-str-span
486 create parse-idx-stack 10 allot
487 variable parse-idx-sp
488 parse-idx-stack parse-idx-sp !
491 parse-idx @ parse-idx-sp @ !
496 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
500 parse-idx-sp @ @ parse-idx ! ;
504 '\n' parse-str parse-str-span @ + !
505 1 parse-str-span +! ;
512 parse-str 160 expect cr
513 span @ parse-str-span !
523 : charavailable? ( -- bool )
524 parse-str-span @ parse-idx @ > ;
526 : nextchar ( -- char )
527 charavailable? false = if getline then
528 parse-str parse-idx @ + @ ;
530 : whitespace? ( -- bool )
539 nextchar [char] ( = or
540 nextchar [char] ) = or
543 : commentstart? ( -- bool )
544 nextchar [char] ; = ;
548 false \ Indicates whether or not we're eating a comment
551 dup whitespace? or commentstart? or
553 dup nextchar '\n' = and if
554 invert \ Stop eating comment
556 dup false = commentstart? and if
557 invert \ Begin eating comment
572 nextchar [char] - = ;
575 nextchar [char] + = ;
577 : fixnum? ( -- bool )
603 : realnum? ( -- bool )
610 \ Record starting parse idx:
611 \ Want to detect whether any characters (following +/-) were eaten.
618 [char] . nextchar = if
625 [char] e nextchar = [char] E nextchar = or if
633 drop pop-parse-idx false exit
641 \ This is a real number if characters were
642 \ eaten and the next characer is a delimiter.
643 parse-idx @ < delim? and
648 : boolean? ( -- bool )
649 nextchar [char] # <> if false exit then
656 and if pop-parse-idx false exit then
668 : str-equiv? ( str -- bool )
685 delim? false = if drop false then
690 : character? ( -- bool )
691 nextchar [char] # <> if false exit then
696 nextchar [char] \ <> if pop-parse-idx false exit then
700 S" newline" str-equiv? if pop-parse-idx true exit then
701 S" space" str-equiv? if pop-parse-idx true exit then
702 S" tab" str-equiv? if pop-parse-idx true exit then
704 charavailable? false = if pop-parse-idx false exit then
710 nextchar [char] ( = ;
712 : string? ( -- bool )
713 nextchar [char] " = ;
715 : readfixnum ( -- num-atom )
726 10 * nextchar [char] 0 - +
735 : readrealnum ( -- realnum )
737 \ Remember that at this point we're guaranteed to
738 \ have a parsable real on this line.
740 parse-str parse-idx @ +
742 begin delim? false = while
746 parse-str parse-idx @ + over -
753 : readbool ( -- bool-atom )
756 nextchar [char] f = if
767 : readchar ( -- char-atom )
771 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
772 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
773 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
775 nextchar character-type
780 : readstring ( -- charlist )
781 nextchar [char] " = if
786 ." No delimiter following right double quote. Aborting." cr
795 nextchar [char] \ = if
798 [char] n of '\n' endof
799 [char] " of [char] " endof
805 inc-parse-idx character-type
812 : readsymbol ( -- charlist )
813 delim? if nil exit then
815 nextchar inc-parse-idx character-type
822 : readpair ( -- pairobj )
826 nextchar [char] ) = if
831 ." No delimiter following right paren. Aborting." cr
840 \ Read first pair element
845 nextchar [char] . = if
850 ." No delimiter following '.'. Aborting." cr
864 \ Parse a scheme expression
895 nextchar [char] " <> if
896 bold red ." Missing closing double-quote." reset-term cr
914 nextchar [char] ) <> if
915 bold red ." Missing closing paren." reset-term cr
924 nextchar [char] ' = if
926 quote-symbol recurse nil cons cons exit
931 bold fg blue ." Moriturus te saluto." reset-term cr
935 \ Anything else is parsed as a symbol
936 readsymbol charlist>symbol
938 \ Replace λ with lambda
939 2dup λ-symbol objeq? if
950 : self-evaluating? ( obj -- obj bool )
951 boolean-type istype? if true exit then
952 fixnum-type istype? if true exit then
953 realnum-type istype? if true exit then
954 character-type istype? if true exit then
955 string-type istype? if true exit then
956 nil-type istype? if true exit then
961 : tagged-list? ( obj tag-obj -- obj bool )
963 pair-type istype? false = if
969 : quote? ( obj -- obj bool )
970 quote-symbol tagged-list? ;
972 : quote-body ( quote-obj -- quote-body-obj )
975 : variable? ( obj -- obj bool )
976 symbol-type istype? ;
978 : definition? ( obj -- obj bool )
979 define-symbol tagged-list? ;
981 : make-lambda ( params body -- lambda-exp )
982 lambda-symbol -2rot cons cons ;
984 : definition-var ( obj -- var )
986 symbol-type istype? false = if car then
989 : definition-val ( obj -- val )
990 2dup cdr car symbol-type istype? if
999 : assignment? ( obj -- obj bool )
1000 set!-symbol tagged-list? ;
1002 : assignment-var ( obj -- var )
1005 : assignment-val ( obj -- val )
1008 : eval-definition ( obj env -- res )
1010 2over 2over ( env obj env obj )
1011 definition-val 2swap ( env obj valexp env )
1012 eval ( env obj val )
1014 2swap definition-var 2swap ( env var val )
1016 2rot ( var val env )
1022 : eval-assignment ( obj env -- res )
1024 2over 2over ( env obj env obj )
1025 assignment-val 2swap ( env obj valexp env )
1026 eval ( env obj val )
1028 2swap assignment-var 2swap ( env var val )
1030 2rot ( var val env )
1036 : if? ( obj -- obj bool )
1037 if-symbol tagged-list? ;
1039 : if-predicate ( ifobj -- pred )
1042 : if-consequent ( ifobj -- conseq )
1045 : if-alternative ( ifobj -- alt|false )
1053 : false? ( boolobj -- boolean )
1054 boolean-type istype? if
1055 false boolean-type objeq?
1061 : true? ( boolobj -- bool )
1064 : lambda? ( obj -- obj bool )
1065 lambda-symbol tagged-list? ;
1067 : lambda-parameters ( obj -- params )
1070 : lambda-body ( obj -- body )
1073 : make-procedure ( params body env -- proc )
1076 drop compound-proc-type
1079 : application? ( obj -- obj bool)
1082 : operator ( obj -- operator )
1085 : operands ( obj -- operands )
1088 : nooperands? ( operands -- bool )
1091 : first-operand ( operands -- operand )
1094 : rest-operands ( operands -- other-operands )
1097 : list-of-vals ( args env -- vals )
1103 2over 2over first-operand 2swap eval
1104 -2rot rest-operands 2swap recurse
1109 : procedure-params ( proc -- params )
1110 drop pair-type car ;
1112 : procedure-body ( proc -- body )
1113 drop pair-type cdr car ;
1115 : procedure-env ( proc -- body )
1116 drop pair-type cdr cdr car ;
1118 : apply ( proc args )
1120 primitive-proc-type of
1124 compound-proc-type of
1125 2dup procedure-body ( args proc body )
1126 -2rot 2dup procedure-params ( body args proc params )
1127 -2rot procedure-env ( body params args procenv )
1129 extend-env ( body env )
1134 2dup cdr 2dup nil objeq? false =
1136 -2rot car 2over ( nextbody env exp env )
1138 2drop \ discard result
1139 2swap ( env nextbody )
1143 car 2swap ( exp env )
1145 R> drop ['] eval goto-deferred \ Tail call optimization
1148 bold fg red ." Object not applicable. Aboring." reset-term cr
1153 :noname ( obj env -- result )
1173 2swap eval-definition
1178 2swap eval-assignment
1194 ['] eval goto-deferred
1198 2dup lambda-parameters
1208 operands 2swap list-of-vals
1214 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1220 \ ---- Print ---- {{{
1222 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1224 : printrealnum ( realnumobj -- ) drop float-print ;
1226 : printbool ( numobj -- )
1234 : printchar ( charobj -- )
1237 9 of ." #\tab" endof
1238 bl of ." #\space" endof
1239 '\n' of ." #\newline" endof
1245 : (printstring) ( stringobj -- )
1246 nil-type istype? if 2drop exit then
1250 '\n' of ." \n" drop endof
1251 [char] \ of ." \\" drop endof
1252 [char] " of [char] \ emit [char] " emit drop endof
1258 : printstring ( stringobj -- )
1263 : printsymbol ( symbolobj -- )
1264 nil-type istype? if 2drop exit then
1270 : printnil ( nilobj -- )
1273 : printpair ( pairobj -- )
1277 nil-type istype? if 2drop exit then
1278 pair-type istype? if space recurse exit then
1282 : printprim ( primobj -- )
1283 2drop ." <primitive procedure>" ;
1285 : printcomp ( primobj -- )
1286 2drop ." <compound procedure>" ;
1289 fixnum-type istype? if printfixnum exit then
1290 realnum-type istype? if printrealnum exit then
1291 boolean-type istype? if printbool exit then
1292 character-type istype? if printchar exit then
1293 string-type istype? if printstring exit then
1294 symbol-type istype? if printsymbol exit then
1295 nil-type istype? if printnil exit then
1296 pair-type istype? if ." (" printpair ." )" exit then
1297 primitive-proc-type istype? if printprim exit then
1298 compound-proc-type istype? if printcomp exit then
1300 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1306 \ ---- DEBUGGING ---- {{{
1308 false value debug-mode
1312 ." Object: " 2dup cr print cr
1313 ." Pairlike: " pairlike? if
1330 symbol-table obj@ gc-mark-obj
1331 global-env obj@ gc-mark-obj
1338 nextfrees i + @ 0= if 1+ then
1344 nextfrees i + @ 0<> if
1357 cr ." Welcome to scheme.forth.jl!" cr
1358 ." Use Ctrl-D to exit." cr
1364 cr bold fg green ." > " reset-term
1367 global-env obj@ eval
1369 fg cyan ." ; " print reset-term