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
274 create-symbol begin begin-symbol
278 \ ---- Environments ---- {{{
280 : enclosing-env ( env -- env )
283 : first-frame ( env -- frame )
286 : make-frame ( vars vals -- frame )
289 : frame-vars ( frame -- vars )
292 : frame-vals ( frame -- vals )
295 : add-binding ( var val frame -- )
296 2swap 2over frame-vals cons
298 2swap 2over frame-vars cons
302 : extend-env ( vars vals env -- env )
312 : get-vars-vals-frame ( var frame -- bool )
313 2dup frame-vars vars obj!
317 vars obj@ nil objeq? false =
319 2dup vars obj@ car objeq? if
324 vars obj@ cdr vars obj!
325 vals obj@ cdr vals obj!
331 : get-vars-vals ( var env -- vars? vals? bool )
334 2dup nil objeq? false =
336 2over 2over first-frame
337 get-vars-vals-frame if
339 vars obj@ vals obj@ true
353 : lookup-var ( var env -- val )
357 bold fg red ." Tried to read unbound variable." reset-term cr abort
361 : set-var ( var val env -- )
362 >R >R 2swap R> R> ( val var env )
364 2swap 2drop ( val vals )
367 bold fg red ." Tried to set unbound variable." reset-term cr abort
373 : define-var ( var val env -- )
376 2over env obj@ ( var val var env )
378 2swap 2drop ( var val vals )
383 first-frame ( var val frame )
391 nil nil nil extend-env
399 variable stored-parse-idx
400 create parse-str 161 allot
401 variable parse-str-span
403 create parse-idx-stack 10 allot
404 variable parse-idx-sp
405 parse-idx-stack parse-idx-sp !
408 parse-idx @ parse-idx-sp @ !
413 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
417 parse-idx-sp @ @ parse-idx ! ;
421 '\n' parse-str parse-str-span @ + !
422 1 parse-str-span +! ;
425 4 parse-str parse-str-span @ + !
426 1 parse-str-span +! ;
433 current-input-port obj@ console-i/o-port obj@ objeq? if
434 parse-str 160 expect cr
435 span @ parse-str-span !
437 parse-str 160 current-input-port obj@ fileport>fid read-line
438 drop swap parse-str-span !
440 parse-str-span @ 0= and if append-eof then
451 : charavailable? ( -- bool )
452 parse-str-span @ parse-idx @ > ;
454 : nextchar ( -- char )
455 charavailable? false = if getline then
456 parse-str parse-idx @ + @ ;
459 : whitespace? ( -- bool )
471 nextchar [char] ( = or
472 nextchar [char] ) = or
475 : commentstart? ( -- bool )
476 nextchar [char] ; = ;
480 false \ Indicates whether or not we're eating a comment
483 dup whitespace? or commentstart? or
485 dup nextchar '\n' = and if
486 invert \ Stop eating comment
488 dup false = commentstart? and if
489 invert \ Begin eating comment
504 nextchar [char] - = ;
507 nextchar [char] + = ;
509 : fixnum? ( -- bool )
535 : realnum? ( -- bool )
542 \ Record starting parse idx:
543 \ Want to detect whether any characters (following +/-) were eaten.
550 [char] . nextchar = if
557 [char] e nextchar = [char] E nextchar = or if
565 drop pop-parse-idx false exit
573 \ This is a real number if characters were
574 \ eaten and the next characer is a delimiter.
575 parse-idx @ < delim? and
580 : boolean? ( -- bool )
581 nextchar [char] # <> if false exit then
588 and if pop-parse-idx false exit then
600 : str-equiv? ( str -- bool )
617 delim? false = if drop false then
622 : character? ( -- bool )
623 nextchar [char] # <> if false exit then
628 nextchar [char] \ <> if pop-parse-idx false exit then
632 S" newline" str-equiv? if pop-parse-idx true exit then
633 S" space" str-equiv? if pop-parse-idx true exit then
634 S" tab" str-equiv? if pop-parse-idx true exit then
636 charavailable? false = if pop-parse-idx false exit then
642 nextchar [char] ( = ;
644 : string? ( -- bool )
645 nextchar [char] " = ;
647 : readfixnum ( -- num-atom )
658 10 * nextchar [char] 0 - +
667 : readrealnum ( -- realnum )
669 \ Remember that at this point we're guaranteed to
670 \ have a parsable real on this line.
672 parse-str parse-idx @ +
674 begin delim? false = while
678 parse-str parse-idx @ + over -
685 : readbool ( -- bool-obj )
688 nextchar [char] f = if
699 : readchar ( -- char-obj )
703 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
704 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
705 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
707 nextchar character-type
712 : readstring ( -- charlist )
713 nextchar [char] " = if
718 ." No delimiter following right double quote. Aborting." cr
727 nextchar [char] \ = if
730 [char] n of '\n' endof
731 [char] " of [char] " endof
737 inc-parse-idx character-type
744 : readsymbol ( -- charlist )
745 delim? if nil exit then
747 nextchar inc-parse-idx character-type
754 : readpair ( -- pairobj )
758 nextchar [char] ) = if
763 ." No delimiter following right paren. Aborting." cr
772 \ Read first pair element
777 nextchar [char] . = if
782 ." No delimiter following '.'. Aborting." cr
796 \ Parse a scheme expression
827 nextchar [char] " <> if
828 bold red ." Missing closing double-quote." reset-term cr
846 nextchar [char] ) <> if
847 bold red ." Missing closing paren." reset-term cr
856 nextchar [char] ' = if
858 quote-symbol recurse nil cons cons exit
867 \ Anything else is parsed as a symbol
868 readsymbol charlist>symbol
870 \ Replace λ with lambda
871 2dup λ-symbol objeq? if
882 : self-evaluating? ( obj -- obj bool )
883 boolean-type istype? if true exit then
884 fixnum-type istype? if true exit then
885 realnum-type istype? if true exit then
886 character-type istype? if true exit then
887 string-type istype? if true exit then
888 nil-type istype? if true exit then
893 : tagged-list? ( obj tag-obj -- obj bool )
895 pair-type istype? false = if
901 : quote? ( obj -- obj bool )
902 quote-symbol tagged-list? ;
904 : quote-body ( quote-obj -- quote-body-obj )
907 : variable? ( obj -- obj bool )
908 symbol-type istype? ;
910 : definition? ( obj -- obj bool )
911 define-symbol tagged-list? ;
913 : make-lambda ( params body -- lambda-exp )
914 lambda-symbol -2rot cons cons ;
916 : definition-var ( obj -- var )
918 symbol-type istype? false = if car then
921 : definition-val ( obj -- val )
922 2dup cdr car symbol-type istype? if
931 : assignment? ( obj -- obj bool )
932 set!-symbol tagged-list? ;
934 : assignment-var ( obj -- var )
937 : assignment-val ( obj -- val )
940 : eval-definition ( obj env -- res )
942 2over 2over ( env obj env obj )
943 definition-val 2swap ( env obj valexp env )
946 2swap definition-var 2swap ( env var val )
954 : eval-assignment ( obj env -- res )
956 2over 2over ( env obj env obj )
957 assignment-val 2swap ( env obj valexp env )
960 2swap assignment-var 2swap ( env var val )
968 : if? ( obj -- obj bool )
969 if-symbol tagged-list? ;
971 : if-predicate ( ifobj -- pred )
974 : if-consequent ( ifobj -- conseq )
977 : if-alternative ( ifobj -- alt|false )
985 : false? ( boolobj -- boolean )
986 boolean-type istype? if
987 false boolean-type objeq?
993 : true? ( boolobj -- bool )
996 : lambda? ( obj -- obj bool )
997 lambda-symbol tagged-list? ;
999 : lambda-parameters ( obj -- params )
1002 : lambda-body ( obj -- body )
1005 : make-procedure ( params body env -- proc )
1008 drop compound-proc-type
1011 : begin? ( obj -- obj bool )
1012 begin-symbol tagged-list? ;
1014 : begin-actions ( obj -- actions )
1017 : eval-sequence ( explist env -- finalexp env )
1018 ( Evaluates all bar the final expressions in
1019 an an expression list. The final expression
1020 is returned to allow for tail optimization. )
1022 2swap ( env explist )
1024 \ Abort on empty list
1025 2dup nil objeq? if 2swap exit then
1028 2dup cdr ( env explist nextexplist )
1029 2dup nil objeq? false =
1031 -2rot car 2over ( nextexplist env exp env )
1033 2drop \ discard result
1034 2swap ( env nextexplist )
1037 2drop car 2swap ( finalexp env )
1040 : application? ( obj -- obj bool)
1043 : operator ( obj -- operator )
1046 : operands ( obj -- operands )
1049 : nooperands? ( operands -- bool )
1052 : first-operand ( operands -- operand )
1055 : rest-operands ( operands -- other-operands )
1058 : list-of-vals ( args env -- vals )
1064 2over 2over first-operand 2swap eval
1065 -2rot rest-operands 2swap recurse
1070 : procedure-params ( proc -- params )
1071 drop pair-type car ;
1073 : procedure-body ( proc -- body )
1074 drop pair-type cdr car ;
1076 : procedure-env ( proc -- body )
1077 drop pair-type cdr cdr car ;
1079 : apply ( proc args )
1081 primitive-proc-type of
1085 compound-proc-type of
1086 2dup procedure-body ( args proc body )
1087 -2rot 2dup procedure-params ( body args proc params )
1088 -2rot procedure-env ( body params args procenv )
1090 extend-env ( body env )
1094 R> drop ['] eval goto-deferred \ Tail call optimization
1097 bold fg red ." Object not applicable. Aboring." reset-term cr
1102 :noname ( obj env -- result )
1122 2swap eval-definition
1127 2swap eval-assignment
1143 ['] eval goto-deferred
1147 2dup lambda-parameters
1161 operands 2swap list-of-vals
1167 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1173 \ ---- Print ---- {{{
1175 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1177 : printrealnum ( realnumobj -- ) drop float-print ;
1179 : printbool ( numobj -- )
1187 : printchar ( charobj -- )
1190 9 of ." #\tab" endof
1191 bl of ." #\space" endof
1192 '\n' of ." #\newline" endof
1198 : (printstring) ( stringobj -- )
1199 nil-type istype? if 2drop exit then
1203 '\n' of ." \n" drop endof
1204 [char] \ of ." \\" drop endof
1205 [char] " of [char] \ emit [char] " emit drop endof
1211 : printstring ( stringobj -- )
1216 : printsymbol ( symbolobj -- )
1217 nil-type istype? if 2drop exit then
1223 : printnil ( nilobj -- )
1226 : printpair ( pairobj -- )
1230 nil-type istype? if 2drop exit then
1231 pair-type istype? if space recurse exit then
1235 : printprim ( primobj -- )
1236 2drop ." <primitive procedure>" ;
1238 : printcomp ( primobj -- )
1239 2drop ." <compound procedure>" ;
1242 fixnum-type istype? if printfixnum exit then
1243 realnum-type istype? if printrealnum exit then
1244 boolean-type istype? if printbool exit then
1245 character-type istype? if printchar exit then
1246 string-type istype? if printstring exit then
1247 symbol-type istype? if printsymbol exit then
1248 nil-type istype? if printnil exit then
1249 pair-type istype? if ." (" printpair ." )" exit then
1250 primitive-proc-type istype? if printprim exit then
1251 compound-proc-type istype? if printcomp exit then
1253 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1259 \ ---- Garbage Collection ---- {{{
1264 variable gc-stack-depth
1267 depth gc-stack-depth !
1271 false gc-enabled ! ;
1276 : pairlike? ( obj -- obj bool )
1277 pair-type istype? if true exit then
1278 string-type istype? if true exit then
1279 symbol-type istype? if true exit then
1280 compound-proc-type istype? if true exit then
1285 : pairlike-marked? ( obj -- obj bool )
1286 over nextfrees + @ 0=
1289 : mark-pairlike ( obj -- obj )
1290 over nextfrees + 0 swap !
1299 : gc-mark-obj ( obj -- )
1301 pairlike? invert if 2drop exit then
1302 pairlike-marked? if 2drop exit then
1313 scheme-memsize nextfree !
1314 0 scheme-memsize 1- do
1315 nextfrees i + @ 0<> if
1316 nextfree @ nextfrees i + !
1322 \ Following a GC, this gives the amount of free memory
1326 nextfrees i + @ 0= if 1+ then
1330 \ Debugging word - helps spot memory that is retained
1333 nextfrees i + @ 0<> if
1345 symbol-table obj@ gc-mark-obj
1346 global-env obj@ gc-mark-obj
1348 depth gc-stack-depth @ do
1357 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1358 ; is collect-garbage
1362 \ ---- Primitives ---- {{{
1364 : make-primitive ( cfa -- )
1368 \ 2dup ." Defining primitive " type ." ..." cr
1379 rot primitive-proc-type ( var prim )
1380 global-env obj@ define-var
1384 bold fg red ." Incorrect argument count." reset-term cr
1388 : ensure-arg-count ( args n -- )
1390 drop nil objeq? false = if
1394 -rot 2dup nil objeq? if
1403 bold fg red ." Incorrect argument type." reset-term cr
1407 : ensure-arg-type ( arg type -- arg )
1413 include scheme-primitives.4th
1417 \ ---- Loading files ---- {{{
1419 : charlist>cstr ( charlist addr -- n )
1421 dup 2swap ( origaddr addr charlist )
1424 2dup nil objeq? false =
1427 drop ( origaddr addr charlist char )
1428 -rot 2swap ( origaddr charlist addr char )
1430 1+ -rot ( origaddr nextaddr charlist )
1433 2drop ( origaddr finaladdr )
1437 : load ( addr n -- finalResult )
1442 ok-symbol ( port res )
1445 2over read-port ( port res obj )
1447 2dup EOF character-type objeq? if
1448 2drop 2swap close-port
1452 2swap 2drop ( port obj )
1454 global-env obj@ eval ( port res )
1458 :noname ( args -- finalResult )
1459 2dup 1 ensure-arg-count
1460 car string-type ensure-arg-type
1465 ; make-primitive load
1472 cr ." Welcome to scheme.forth.jl!" cr
1473 ." Use Ctrl-D to exit." cr
1480 cr bold fg green ." > " reset-term
1483 2dup EOF character-type objeq? if
1484 bold fg blue ." Moriturus te saluto." reset-term cr
1488 global-env obj@ eval
1490 fg cyan ." ; " print reset-term