4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
33 make-type primitive-proc-type
34 make-type compound-proc-type
35 make-type fileport-type
36 : istype? ( obj type -- obj bool )
39 \ ------ List-structured memory ------ {{{
41 10000 constant scheme-memsize
43 create car-cells scheme-memsize allot
44 create car-type-cells scheme-memsize allot
45 create cdr-cells scheme-memsize allot
46 create cdr-type-cells scheme-memsize allot
48 create nextfrees scheme-memsize allot
59 nextfrees nextfree @ + @
62 nextfree @ scheme-memsize >= if
66 nextfree @ scheme-memsize >= if
68 ." Out of memory! Aborting."
73 : cons ( car-obj cdr-obj -- pair-obj )
74 cdr-type-cells nextfree @ + !
75 cdr-cells nextfree @ + !
76 car-type-cells nextfree @ + !
77 car-cells nextfree @ + !
83 : car ( pair-obj -- car-obj )
85 dup car-cells + @ swap
89 : cdr ( pair-obj -- car-obj )
91 dup cdr-cells + @ swap
95 : set-car! ( obj pair-obj -- )
97 rot swap car-type-cells + !
101 : set-cdr! ( obj pair-obj -- )
103 rot swap cdr-type-cells + !
113 : nil? nil-type istype? ;
115 : objvar create nil swap , , ;
117 : value@ ( objvar -- val ) @ ;
118 : type@ ( objvar -- type ) 1+ @ ;
119 : value! ( newval objvar -- ) ! ;
120 : type! ( newtype objvar -- ) 1+ ! ;
121 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
122 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
124 : objeq? ( obj obj -- bool )
127 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
128 >R >R ( a1 a2 b1 b2 )
129 2swap ( b1 b2 a1 a2 )
130 R> R> ( b1 b2 a1 a2 c1 c2 )
134 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
135 2swap ( a1 a2 c1 c2 b1 b2 )
136 >R >R ( a1 a2 c1 c2 )
137 2swap ( c1 c2 a1 a2 )
143 \ ---- Port I/O ---- {{{
145 : fileport>fid ( fileport -- fid )
148 : fid>fileport ( fid -- fileport )
151 : open-input-file ( addr n -- fileport )
152 r/o open-file drop fid>fileport
155 : close-port ( fileport -- )
156 fileport>fid close-file drop
159 objvar console-i/o-port
160 0 fileport-type console-i/o-port obj!
162 objvar current-input-port
163 console-i/o-port obj@ current-input-port obj!
165 : read-port ( fileport -- obj )
166 current-input-port obj!
169 : read-console ( -- obj )
170 console-i/o-port obj@ read-port ;
174 \ ---- Pre-defined symbols ---- {{{
178 : duplicate-charlist ( charlist -- copy )
179 2dup nil objeq? false = if
180 2dup car 2swap cdr recurse cons
183 : charlist-equiv ( charlist charlist -- bool )
192 2drop 2drop true exit
194 2drop 2drop false exit
199 2drop 2drop false exit
206 car drop -rot car drop = if
207 cdr 2swap cdr recurse
213 : charlist>symbol ( charlist -- symbol-obj )
232 drop symbol-type 2dup
233 symbol-table obj@ cons
238 : cstr>charlist ( addr n -- symbol-obj )
242 2dup drop @ character-type 2swap
250 : create-symbol ( -- )
264 does> dup @ swap 1+ @
267 create-symbol quote quote-symbol
268 create-symbol define define-symbol
269 create-symbol set! set!-symbol
270 create-symbol ok ok-symbol
271 create-symbol if if-symbol
272 create-symbol lambda lambda-symbol
273 create-symbol λ λ-symbol
277 \ ---- Environments ---- {{{
279 : enclosing-env ( env -- env )
282 : first-frame ( env -- frame )
285 : make-frame ( vars vals -- frame )
288 : frame-vars ( frame -- vars )
291 : frame-vals ( frame -- vals )
294 : add-binding ( var val frame -- )
295 2swap 2over frame-vals cons
297 2swap 2over frame-vars cons
301 : extend-env ( vars vals env -- env )
311 : get-vars-vals-frame ( var frame -- bool )
312 2dup frame-vars vars obj!
316 vars obj@ nil objeq? false =
318 2dup vars obj@ car objeq? if
323 vars obj@ cdr vars obj!
324 vals obj@ cdr vals obj!
330 : get-vars-vals ( var env -- vars? vals? bool )
333 2dup nil objeq? false =
335 2over 2over first-frame
336 get-vars-vals-frame if
338 vars obj@ vals obj@ true
352 : lookup-var ( var env -- val )
356 bold fg red ." Tried to read unbound variable." reset-term cr abort
360 : set-var ( var val env -- )
361 >R >R 2swap R> R> ( val var env )
363 2swap 2drop ( val vals )
366 bold fg red ." Tried to set unbound variable." reset-term cr abort
372 : define-var ( var val env -- )
375 2over env obj@ ( var val var env )
377 2swap 2drop ( var val vals )
382 first-frame ( var val frame )
390 nil nil nil extend-env
398 variable stored-parse-idx
399 create parse-str 161 allot
400 variable parse-str-span
402 create parse-idx-stack 10 allot
403 variable parse-idx-sp
404 parse-idx-stack parse-idx-sp !
407 parse-idx @ parse-idx-sp @ !
412 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
416 parse-idx-sp @ @ parse-idx ! ;
420 '\n' parse-str parse-str-span @ + !
421 1 parse-str-span +! ;
424 4 parse-str parse-str-span @ + !
425 1 parse-str-span +! ;
432 current-input-port obj@ console-i/o-port obj@ objeq? if
433 parse-str 160 expect cr
434 span @ parse-str-span !
436 parse-str 160 current-input-port obj@ fileport>fid read-line
437 drop swap parse-str-span !
439 parse-str-span @ 0= and if append-eof then
450 : charavailable? ( -- bool )
451 parse-str-span @ parse-idx @ > ;
453 : nextchar ( -- char )
454 charavailable? false = if getline then
455 parse-str parse-idx @ + @ ;
458 : whitespace? ( -- bool )
470 nextchar [char] ( = or
471 nextchar [char] ) = or
474 : commentstart? ( -- bool )
475 nextchar [char] ; = ;
479 false \ Indicates whether or not we're eating a comment
482 dup whitespace? or commentstart? or
484 dup nextchar '\n' = and if
485 invert \ Stop eating comment
487 dup false = commentstart? and if
488 invert \ Begin eating comment
503 nextchar [char] - = ;
506 nextchar [char] + = ;
508 : fixnum? ( -- bool )
534 : realnum? ( -- bool )
541 \ Record starting parse idx:
542 \ Want to detect whether any characters (following +/-) were eaten.
549 [char] . nextchar = if
556 [char] e nextchar = [char] E nextchar = or if
564 drop pop-parse-idx false exit
572 \ This is a real number if characters were
573 \ eaten and the next characer is a delimiter.
574 parse-idx @ < delim? and
579 : boolean? ( -- bool )
580 nextchar [char] # <> if false exit then
587 and if pop-parse-idx false exit then
599 : str-equiv? ( str -- bool )
616 delim? false = if drop false then
621 : character? ( -- bool )
622 nextchar [char] # <> if false exit then
627 nextchar [char] \ <> if pop-parse-idx false exit then
631 S" newline" str-equiv? if pop-parse-idx true exit then
632 S" space" str-equiv? if pop-parse-idx true exit then
633 S" tab" str-equiv? if pop-parse-idx true exit then
635 charavailable? false = if pop-parse-idx false exit then
641 nextchar [char] ( = ;
643 : string? ( -- bool )
644 nextchar [char] " = ;
646 : readfixnum ( -- num-atom )
657 10 * nextchar [char] 0 - +
666 : readrealnum ( -- realnum )
668 \ Remember that at this point we're guaranteed to
669 \ have a parsable real on this line.
671 parse-str parse-idx @ +
673 begin delim? false = while
677 parse-str parse-idx @ + over -
684 : readbool ( -- bool-obj )
687 nextchar [char] f = if
698 : readchar ( -- char-obj )
702 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
703 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
704 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
706 nextchar character-type
711 : readstring ( -- charlist )
712 nextchar [char] " = if
717 ." No delimiter following right double quote. Aborting." cr
726 nextchar [char] \ = if
729 [char] n of '\n' endof
730 [char] " of [char] " endof
736 inc-parse-idx character-type
743 : readsymbol ( -- charlist )
744 delim? if nil exit then
746 nextchar inc-parse-idx character-type
753 : readpair ( -- pairobj )
757 nextchar [char] ) = if
762 ." No delimiter following right paren. Aborting." cr
771 \ Read first pair element
776 nextchar [char] . = if
781 ." No delimiter following '.'. Aborting." cr
795 \ Parse a scheme expression
826 nextchar [char] " <> if
827 bold red ." Missing closing double-quote." reset-term cr
845 nextchar [char] ) <> if
846 bold red ." Missing closing paren." reset-term cr
855 nextchar [char] ' = if
857 quote-symbol recurse nil cons cons exit
866 \ Anything else is parsed as a symbol
867 readsymbol charlist>symbol
869 \ Replace λ with lambda
870 2dup λ-symbol objeq? if
881 : self-evaluating? ( obj -- obj bool )
882 boolean-type istype? if true exit then
883 fixnum-type istype? if true exit then
884 realnum-type istype? if true exit then
885 character-type istype? if true exit then
886 string-type istype? if true exit then
887 nil-type istype? if true exit then
892 : tagged-list? ( obj tag-obj -- obj bool )
894 pair-type istype? false = if
900 : quote? ( obj -- obj bool )
901 quote-symbol tagged-list? ;
903 : quote-body ( quote-obj -- quote-body-obj )
906 : variable? ( obj -- obj bool )
907 symbol-type istype? ;
909 : definition? ( obj -- obj bool )
910 define-symbol tagged-list? ;
912 : make-lambda ( params body -- lambda-exp )
913 lambda-symbol -2rot cons cons ;
915 : definition-var ( obj -- var )
917 symbol-type istype? false = if car then
920 : definition-val ( obj -- val )
921 2dup cdr car symbol-type istype? if
930 : assignment? ( obj -- obj bool )
931 set!-symbol tagged-list? ;
933 : assignment-var ( obj -- var )
936 : assignment-val ( obj -- val )
939 : eval-definition ( obj env -- res )
941 2over 2over ( env obj env obj )
942 definition-val 2swap ( env obj valexp env )
945 2swap definition-var 2swap ( env var val )
953 : eval-assignment ( obj env -- res )
955 2over 2over ( env obj env obj )
956 assignment-val 2swap ( env obj valexp env )
959 2swap assignment-var 2swap ( env var val )
967 : if? ( obj -- obj bool )
968 if-symbol tagged-list? ;
970 : if-predicate ( ifobj -- pred )
973 : if-consequent ( ifobj -- conseq )
976 : if-alternative ( ifobj -- alt|false )
984 : false? ( boolobj -- boolean )
985 boolean-type istype? if
986 false boolean-type objeq?
992 : true? ( boolobj -- bool )
995 : lambda? ( obj -- obj bool )
996 lambda-symbol tagged-list? ;
998 : lambda-parameters ( obj -- params )
1001 : lambda-body ( obj -- body )
1004 : make-procedure ( params body env -- proc )
1007 drop compound-proc-type
1010 : application? ( obj -- obj bool)
1013 : operator ( obj -- operator )
1016 : operands ( obj -- operands )
1019 : nooperands? ( operands -- bool )
1022 : first-operand ( operands -- operand )
1025 : rest-operands ( operands -- other-operands )
1028 : list-of-vals ( args env -- vals )
1034 2over 2over first-operand 2swap eval
1035 -2rot rest-operands 2swap recurse
1040 : procedure-params ( proc -- params )
1041 drop pair-type car ;
1043 : procedure-body ( proc -- body )
1044 drop pair-type cdr car ;
1046 : procedure-env ( proc -- body )
1047 drop pair-type cdr cdr car ;
1049 : apply ( proc args )
1051 primitive-proc-type of
1055 compound-proc-type of
1056 2dup procedure-body ( args proc body )
1057 -2rot 2dup procedure-params ( body args proc params )
1058 -2rot procedure-env ( body params args procenv )
1060 extend-env ( body env )
1065 2dup cdr 2dup nil objeq? false =
1067 -2rot car 2over ( nextbody env exp env )
1069 2drop \ discard result
1070 2swap ( env nextbody )
1074 car 2swap ( exp env )
1076 R> drop ['] eval goto-deferred \ Tail call optimization
1079 bold fg red ." Object not applicable. Aboring." reset-term cr
1084 :noname ( obj env -- result )
1104 2swap eval-definition
1109 2swap eval-assignment
1125 ['] eval goto-deferred
1129 2dup lambda-parameters
1139 operands 2swap list-of-vals
1145 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1151 \ ---- Print ---- {{{
1153 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1155 : printrealnum ( realnumobj -- ) drop float-print ;
1157 : printbool ( numobj -- )
1165 : printchar ( charobj -- )
1168 9 of ." #\tab" endof
1169 bl of ." #\space" endof
1170 '\n' of ." #\newline" endof
1176 : (printstring) ( stringobj -- )
1177 nil-type istype? if 2drop exit then
1181 '\n' of ." \n" drop endof
1182 [char] \ of ." \\" drop endof
1183 [char] " of [char] \ emit [char] " emit drop endof
1189 : printstring ( stringobj -- )
1194 : printsymbol ( symbolobj -- )
1195 nil-type istype? if 2drop exit then
1201 : printnil ( nilobj -- )
1204 : printpair ( pairobj -- )
1208 nil-type istype? if 2drop exit then
1209 pair-type istype? if space recurse exit then
1213 : printprim ( primobj -- )
1214 2drop ." <primitive procedure>" ;
1216 : printcomp ( primobj -- )
1217 2drop ." <compound procedure>" ;
1220 fixnum-type istype? if printfixnum exit then
1221 realnum-type istype? if printrealnum exit then
1222 boolean-type istype? if printbool exit then
1223 character-type istype? if printchar exit then
1224 string-type istype? if printstring exit then
1225 symbol-type istype? if printsymbol exit then
1226 nil-type istype? if printnil exit then
1227 pair-type istype? if ." (" printpair ." )" exit then
1228 primitive-proc-type istype? if printprim exit then
1229 compound-proc-type istype? if printcomp exit then
1231 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1237 \ ---- Garbage Collection ---- {{{
1242 variable gc-stack-depth
1245 depth gc-stack-depth !
1249 false gc-enabled ! ;
1254 : pairlike? ( obj -- obj bool )
1255 pair-type istype? if true exit then
1256 string-type istype? if true exit then
1257 symbol-type istype? if true exit then
1258 compound-proc-type istype? if true exit then
1263 : pairlike-marked? ( obj -- obj bool )
1264 over nextfrees + @ 0=
1267 : mark-pairlike ( obj -- obj )
1268 over nextfrees + 0 swap !
1277 : gc-mark-obj ( obj -- )
1279 pairlike? invert if 2drop exit then
1280 pairlike-marked? if 2drop exit then
1291 scheme-memsize nextfree !
1292 0 scheme-memsize 1- do
1293 nextfrees i + @ 0<> if
1294 nextfree @ nextfrees i + !
1300 \ Following a GC, this gives the amount of free memory
1304 nextfrees i + @ 0= if 1+ then
1308 \ Debugging word - helps spot memory that is retained
1311 nextfrees i + @ 0<> if
1323 symbol-table obj@ gc-mark-obj
1324 global-env obj@ gc-mark-obj
1326 depth gc-stack-depth @ do
1335 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1336 ; is collect-garbage
1340 \ ---- Primitives ---- {{{
1342 : make-primitive ( cfa -- )
1346 \ 2dup ." Defining primitive " type ." ..." cr
1357 rot primitive-proc-type ( var prim )
1358 global-env obj@ define-var
1362 bold fg red ." Incorrect argument count." reset-term cr
1366 : ensure-arg-count ( args n -- )
1368 drop nil objeq? false = if
1372 -rot 2dup nil objeq? if
1381 bold fg red ." Incorrect argument type." reset-term cr
1385 : ensure-arg-type ( arg type -- arg )
1391 include scheme-primitives.4th
1395 \ ---- Loading files ---- {{{
1397 : charlist>cstr ( charlist addr -- n )
1399 dup 2swap ( origaddr addr charlist )
1402 2dup nil objeq? false =
1405 drop ( origaddr addr charlist char )
1406 -rot 2swap ( origaddr charlist addr char )
1408 1+ -rot ( origaddr nextaddr charlist )
1411 2drop ( origaddr finaladdr )
1415 : load ( addr n -- finalResult )
1420 ok-symbol ( port res )
1423 2over read-port ( port res obj )
1425 2dup EOF character-type objeq? if
1426 2drop 2swap close-port
1430 2swap 2drop ( port obj )
1432 global-env obj@ eval ( port res )
1441 cr ." Welcome to scheme.forth.jl!" cr
1442 ." Use Ctrl-D to exit." cr
1449 cr bold fg green ." > " reset-term
1452 2dup EOF character-type objeq? if
1453 bold fg blue ." Moriturus te saluto." reset-term cr
1457 global-env obj@ eval
1459 fg cyan ." ; " print reset-term
1463 : test s" fact.scm" ;