4 include term-colours.4th
26 make-type realnum-type
27 make-type boolean-type
28 make-type character-type
34 make-type primitive-proc-type
35 make-type compound-proc-type
36 make-type fileport-type
37 : istype? ( obj type -- obj bool )
40 \ ---- List-structured memory ---- {{{
42 10000 constant scheme-memsize
44 create car-cells scheme-memsize allot
45 create car-type-cells scheme-memsize allot
46 create cdr-cells scheme-memsize allot
47 create cdr-type-cells scheme-memsize allot
49 create nextfrees scheme-memsize allot
60 nextfrees nextfree @ + @
63 nextfree @ scheme-memsize >= if
67 nextfree @ scheme-memsize >= if
69 ." Out of memory! Aborting."
74 : cons ( car-obj cdr-obj -- pair-obj )
75 cdr-type-cells nextfree @ + !
76 cdr-cells nextfree @ + !
77 car-type-cells nextfree @ + !
78 car-cells nextfree @ + !
84 : car ( pair-obj -- car-obj )
86 dup car-cells + @ swap
90 : cdr ( pair-obj -- car-obj )
92 dup cdr-cells + @ swap
96 : set-car! ( obj pair-obj -- )
98 rot swap car-type-cells + !
102 : set-cdr! ( obj pair-obj -- )
104 rot swap cdr-type-cells + !
114 : nil? nil-type istype? ;
117 : none? none-type istype? ;
119 : objvar create nil swap , , ;
121 : value@ ( objvar -- val ) @ ;
122 : type@ ( objvar -- type ) 1+ @ ;
123 : value! ( newval objvar -- ) ! ;
124 : type! ( newtype objvar -- ) 1+ ! ;
125 : obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
126 : obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
128 : objeq? ( obj obj -- bool )
131 : 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
132 >R >R ( a1 a2 b1 b2 )
133 2swap ( b1 b2 a1 a2 )
134 R> R> ( b1 b2 a1 a2 c1 c2 )
138 : -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
139 2swap ( a1 a2 c1 c2 b1 b2 )
140 >R >R ( a1 a2 c1 c2 )
141 2swap ( c1 c2 a1 a2 )
147 \ ---- Port I/O ---- {{{
149 : fileport>fid ( fileport -- fid )
152 : fid>fileport ( fid -- fileport )
155 : open-input-file ( addr n -- fileport )
156 r/o open-file drop fid>fileport
159 : close-port ( fileport -- )
160 fileport>fid close-file drop
163 objvar console-i/o-port
164 0 fileport-type console-i/o-port obj!
166 objvar current-input-port
167 console-i/o-port obj@ current-input-port obj!
169 : read-port ( fileport -- obj )
170 current-input-port obj!
173 : read-console ( -- obj )
174 console-i/o-port obj@ read-port ;
178 \ ---- Pre-defined symbols ---- {{{
182 : duplicate-charlist ( charlist -- copy )
183 2dup nil objeq? false = if
184 2dup car 2swap cdr recurse cons
187 : charlist-equiv ( charlist charlist -- bool )
196 2drop 2drop true exit
198 2drop 2drop false exit
203 2drop 2drop false exit
210 car drop -rot car drop = if
211 cdr 2swap cdr recurse
217 : charlist>symbol ( charlist -- symbol-obj )
236 drop symbol-type 2dup
237 symbol-table obj@ cons
242 : cstr>charlist ( addr n -- symbol-obj )
246 2dup drop @ character-type 2swap
254 : create-symbol ( -- )
268 does> dup @ swap 1+ @
271 create-symbol quote quote-symbol
272 create-symbol define define-symbol
273 create-symbol define-macro define-macro-symbol
274 create-symbol set! set!-symbol
275 create-symbol ok ok-symbol
276 create-symbol if if-symbol
277 create-symbol lambda lambda-symbol
278 create-symbol λ λ-symbol
279 create-symbol begin begin-symbol
283 \ ---- Environments ---- {{{
285 : enclosing-env ( env -- env )
288 : first-frame ( env -- frame )
291 : make-frame ( vars vals -- frame )
294 : frame-vars ( frame -- vars )
297 : frame-vals ( frame -- vals )
300 : add-binding ( var val frame -- )
301 2swap 2over frame-vals cons
303 2swap 2over frame-vars cons
307 : extend-env ( vars vals env -- env )
317 : get-vars-vals-frame ( var frame -- bool )
318 2dup frame-vars vars obj!
322 vars obj@ nil objeq? false =
324 2dup vars obj@ car objeq? if
329 vars obj@ cdr vars obj!
330 vals obj@ cdr vals obj!
336 : get-vars-vals ( var env -- vars? vals? bool )
339 2dup nil objeq? false =
341 2over 2over first-frame
342 get-vars-vals-frame if
344 vars obj@ vals obj@ true
358 : lookup-var ( var env -- val )
362 bold fg red ." Tried to read unbound variable." reset-term cr abort
366 : set-var ( var val env -- )
367 >R >R 2swap R> R> ( val var env )
369 2swap 2drop ( val vals )
372 bold fg red ." Tried to set unbound variable." reset-term cr abort
378 : define-var ( var val env -- )
381 2over env obj@ ( var val var env )
383 2swap 2drop ( var val vals )
388 first-frame ( var val frame )
396 nil nil nil extend-env
404 variable stored-parse-idx
405 create parse-str 161 allot
406 variable parse-str-span
408 create parse-idx-stack 10 allot
409 variable parse-idx-sp
410 parse-idx-stack parse-idx-sp !
413 parse-idx @ parse-idx-sp @ !
418 parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
422 parse-idx-sp @ @ parse-idx ! ;
426 '\n' parse-str parse-str-span @ + !
427 1 parse-str-span +! ;
430 4 parse-str parse-str-span @ + !
431 1 parse-str-span +! ;
438 current-input-port obj@ console-i/o-port obj@ objeq? if
439 parse-str 160 expect cr
440 span @ parse-str-span !
442 parse-str 160 current-input-port obj@ fileport>fid read-line
443 drop swap parse-str-span !
445 parse-str-span @ 0= and if append-eof then
456 : charavailable? ( -- bool )
457 parse-str-span @ parse-idx @ > ;
459 : nextchar ( -- char )
460 charavailable? false = if getline then
461 parse-str parse-idx @ + @ ;
464 : whitespace? ( -- bool )
476 nextchar [char] ( = or
477 nextchar [char] ) = or
480 : commentstart? ( -- bool )
481 nextchar [char] ; = ;
485 false \ Indicates whether or not we're eating a comment
488 dup whitespace? or commentstart? or
490 dup nextchar '\n' = and if
491 invert \ Stop eating comment
493 dup false = commentstart? and if
494 invert \ Begin eating comment
509 nextchar [char] - = ;
512 nextchar [char] + = ;
514 : fixnum? ( -- bool )
540 : realnum? ( -- bool )
547 \ Record starting parse idx:
548 \ Want to detect whether any characters (following +/-) were eaten.
555 [char] . nextchar = if
562 [char] e nextchar = [char] E nextchar = or if
570 drop pop-parse-idx false exit
578 \ This is a real number if characters were
579 \ eaten and the next characer is a delimiter.
580 parse-idx @ < delim? and
585 : boolean? ( -- bool )
586 nextchar [char] # <> if false exit then
593 and if pop-parse-idx false exit then
605 : str-equiv? ( str -- bool )
622 delim? false = if drop false then
627 : character? ( -- bool )
628 nextchar [char] # <> if false exit then
633 nextchar [char] \ <> if pop-parse-idx false exit then
637 S" newline" str-equiv? if pop-parse-idx true exit then
638 S" space" str-equiv? if pop-parse-idx true exit then
639 S" tab" str-equiv? if pop-parse-idx true exit then
641 charavailable? false = if pop-parse-idx false exit then
647 nextchar [char] ( = ;
649 : string? ( -- bool )
650 nextchar [char] " = ;
652 : readfixnum ( -- num-atom )
663 10 * nextchar [char] 0 - +
672 : readrealnum ( -- realnum )
674 \ Remember that at this point we're guaranteed to
675 \ have a parsable real on this line.
677 parse-str parse-idx @ +
679 begin delim? false = while
683 parse-str parse-idx @ + over -
690 : readbool ( -- bool-obj )
693 nextchar [char] f = if
704 : readchar ( -- char-obj )
708 S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
709 S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
710 S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
712 nextchar character-type
717 : readstring ( -- charlist )
718 nextchar [char] " = if
723 ." No delimiter following right double quote. Aborting." cr
732 nextchar [char] \ = if
735 [char] n of '\n' endof
736 [char] " of [char] " endof
742 inc-parse-idx character-type
749 : readsymbol ( -- charlist )
750 delim? if nil exit then
752 nextchar inc-parse-idx character-type
759 : readpair ( -- pairobj )
763 nextchar [char] ) = if
768 ." No delimiter following right paren. Aborting." cr
777 \ Read first pair element
782 nextchar [char] . = if
787 ." No delimiter following '.'. Aborting." cr
801 \ Parse a scheme expression
832 nextchar [char] " <> if
833 bold red ." Missing closing double-quote." reset-term cr
851 nextchar [char] ) <> if
852 bold red ." Missing closing paren." reset-term cr
861 nextchar [char] ' = if
863 quote-symbol recurse nil cons cons exit
872 \ Anything else is parsed as a symbol
873 readsymbol charlist>symbol
875 \ Replace λ with lambda
876 2dup λ-symbol objeq? if
887 : self-evaluating? ( obj -- obj bool )
888 boolean-type istype? if true exit then
889 fixnum-type istype? if true exit then
890 realnum-type istype? if true exit then
891 character-type istype? if true exit then
892 string-type istype? if true exit then
893 nil-type istype? if true exit then
894 none-type istype? if true exit then
899 : tagged-list? ( obj tag-obj -- obj bool )
901 pair-type istype? false = if
907 : quote? ( obj -- obj bool )
908 quote-symbol tagged-list? ;
910 : quote-body ( quote-obj -- quote-body-obj )
913 : variable? ( obj -- obj bool )
914 symbol-type istype? ;
916 : definition? ( obj -- obj bool )
917 define-symbol tagged-list? ;
919 : make-lambda ( params body -- lambda-exp )
920 lambda-symbol -2rot cons cons ;
922 : definition-var ( obj -- var )
924 symbol-type istype? false = if car then
927 : definition-val ( obj -- val )
928 2dup cdr car symbol-type istype? if
937 : assignment? ( obj -- obj bool )
938 set!-symbol tagged-list? ;
940 : assignment-var ( obj -- var )
943 : assignment-val ( obj -- val )
946 : eval-definition ( obj env -- res )
948 2over 2over ( env obj env obj )
949 definition-val 2swap ( env obj valexp env )
952 2swap definition-var 2swap ( env var val )
960 : eval-assignment ( obj env -- res )
962 2over 2over ( env obj env obj )
963 assignment-val 2swap ( env obj valexp env )
966 2swap assignment-var 2swap ( env var val )
974 : macro-definition? ( obj -- obj bool )
975 define-macro-symbol tagged-list? ;
977 : if? ( obj -- obj bool )
978 if-symbol tagged-list? ;
980 : if-predicate ( ifobj -- pred )
983 : if-consequent ( ifobj -- conseq )
986 : if-alternative ( ifobj -- alt|false )
994 : false? ( boolobj -- boolean )
995 boolean-type istype? if
996 false boolean-type objeq?
1002 : true? ( boolobj -- bool )
1005 : lambda? ( obj -- obj bool )
1006 lambda-symbol tagged-list? ;
1008 : lambda-parameters ( obj -- params )
1011 : lambda-body ( obj -- body )
1014 : make-procedure ( params body env -- proc )
1017 drop compound-proc-type
1020 : begin? ( obj -- obj bool )
1021 begin-symbol tagged-list? ;
1023 : begin-actions ( obj -- actions )
1026 : eval-sequence ( explist env -- finalexp env )
1027 ( Evaluates all bar the final expressions in
1028 an an expression list. The final expression
1029 is returned to allow for tail optimization. )
1031 2swap ( env explist )
1033 \ Abort on empty list
1040 2dup cdr ( env explist nextexplist )
1041 2dup nil objeq? false =
1043 -2rot car 2over ( nextexplist env exp env )
1045 2drop \ discard result
1046 2swap ( env nextexplist )
1049 2drop car 2swap ( finalexp env )
1052 : application? ( obj -- obj bool )
1055 : operator ( obj -- operator )
1058 : operands ( obj -- operands )
1061 : nooperands? ( operands -- bool )
1064 : first-operand ( operands -- operand )
1067 : rest-operands ( operands -- other-operands )
1070 : list-of-vals ( args env -- vals )
1076 2over 2over first-operand 2swap eval
1077 -2rot rest-operands 2swap recurse
1082 : procedure-params ( proc -- params )
1083 drop pair-type car ;
1085 : procedure-body ( proc -- body )
1086 drop pair-type cdr car ;
1088 : procedure-env ( proc -- body )
1089 drop pair-type cdr cdr car ;
1091 ( Ensure terminating symbol arg name is handled
1092 specially to allow for variadic procedures. )
1093 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
1096 symbol-type istype? if
1105 recurse ( argvals argnames argvals'' argnames'' )
1106 2rot car 2swap cons ( argvals argvals'' argnames' )
1107 2rot car 2rot cons ( argnames' argvals' )
1111 : apply ( proc argvals )
1113 primitive-proc-type of
1117 compound-proc-type of
1118 2dup procedure-body ( argvals proc body )
1119 -2rot 2dup procedure-params ( body argvals proc argnames )
1120 -2rot procedure-env ( body argnames argvals procenv )
1126 extend-env ( body env )
1130 R> drop ['] eval goto-deferred \ Tail call optimization
1133 bold fg red ." Object not applicable. Aboring." reset-term cr
1138 :noname ( obj env -- result )
1158 2swap eval-definition
1163 2swap eval-assignment
1167 macro-definition? if
1168 2swap eval-define-macro
1184 ['] eval goto-deferred
1188 2dup lambda-parameters
1197 ['] eval goto-deferred
1205 find-macro-proc nil objeq? if
1206 \ Regular function application
1210 operands 2swap list-of-vals
1214 \ Macro function evaluation
1216 2swap 2drop 2swap ( env mproc exp )
1218 apply 2swap ( expanded-exp env )
1220 ['] eval goto-deferred
1225 bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
1231 \ ---- Print ---- {{{
1233 : printfixnum ( fixnumobj -- ) drop 0 .R ;
1235 : printrealnum ( realnumobj -- ) drop float-print ;
1237 : printbool ( numobj -- )
1245 : printchar ( charobj -- )
1248 9 of ." #\tab" endof
1249 bl of ." #\space" endof
1250 '\n' of ." #\newline" endof
1256 : (printstring) ( stringobj -- )
1257 nil-type istype? if 2drop exit then
1261 '\n' of ." \n" drop endof
1262 [char] \ of ." \\" drop endof
1263 [char] " of [char] \ emit [char] " emit drop endof
1269 : printstring ( stringobj -- )
1274 : printsymbol ( symbolobj -- )
1275 nil-type istype? if 2drop exit then
1281 : printnil ( nilobj -- )
1284 : printpair ( pairobj -- )
1288 nil-type istype? if 2drop exit then
1289 pair-type istype? if space recurse exit then
1293 : printprim ( primobj -- )
1294 2drop ." <primitive procedure>" ;
1296 : printcomp ( primobj -- )
1297 2drop ." <compound procedure>" ;
1299 : printnone ( noneobj -- )
1300 2drop ." Unspecified return value" ;
1303 fixnum-type istype? if printfixnum exit then
1304 realnum-type istype? if printrealnum exit then
1305 boolean-type istype? if printbool exit then
1306 character-type istype? if printchar exit then
1307 string-type istype? if printstring exit then
1308 symbol-type istype? if printsymbol exit then
1309 nil-type istype? if printnil exit then
1310 pair-type istype? if ." (" printpair ." )" exit then
1311 primitive-proc-type istype? if printprim exit then
1312 compound-proc-type istype? if printcomp exit then
1313 none-type istype? if printnone exit then
1315 bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
1321 \ ---- Garbage Collection ---- {{{
1326 variable gc-stack-depth
1329 depth gc-stack-depth !
1333 false gc-enabled ! ;
1338 : pairlike? ( obj -- obj bool )
1339 pair-type istype? if true exit then
1340 string-type istype? if true exit then
1341 symbol-type istype? if true exit then
1342 compound-proc-type istype? if true exit then
1347 : pairlike-marked? ( obj -- obj bool )
1348 over nextfrees + @ 0=
1351 : mark-pairlike ( obj -- obj )
1352 over nextfrees + 0 swap !
1361 : gc-mark-obj ( obj -- )
1363 pairlike? invert if 2drop exit then
1364 pairlike-marked? if 2drop exit then
1375 scheme-memsize nextfree !
1376 0 scheme-memsize 1- do
1377 nextfrees i + @ 0<> if
1378 nextfree @ nextfrees i + !
1384 \ Following a GC, this gives the amount of free memory
1388 nextfrees i + @ 0= if 1+ then
1392 \ Debugging word - helps spot memory that is retained
1395 nextfrees i + @ 0<> if
1407 symbol-table obj@ gc-mark-obj
1408 global-env obj@ gc-mark-obj
1410 depth gc-stack-depth @ do
1419 \ ." (" gc-count-marked . ." pairs marked as used.)" cr
1420 ; is collect-garbage
1424 \ ---- Primitives ---- {{{
1426 : make-primitive ( cfa -- )
1430 \ 2dup ." Defining primitive " type ." ..." cr
1441 rot primitive-proc-type ( var prim )
1442 global-env obj@ define-var
1446 bold fg red ." Incorrect argument count." reset-term cr
1450 : ensure-arg-count ( args n -- )
1452 drop nil objeq? false = if
1456 -rot 2dup nil objeq? if
1465 bold fg red ." Incorrect argument type." reset-term cr
1469 : ensure-arg-type ( arg type -- arg )
1475 include scheme-primitives.4th
1479 \ ---- Loading files ---- {{{
1481 : charlist>cstr ( charlist addr -- n )
1483 dup 2swap ( origaddr addr charlist )
1486 2dup nil objeq? false =
1489 drop ( origaddr addr charlist char )
1490 -rot 2swap ( origaddr charlist addr char )
1492 1+ -rot ( origaddr nextaddr charlist )
1495 2drop ( origaddr finaladdr )
1499 : load ( addr n -- finalResult )
1504 ok-symbol ( port res )
1507 2over read-port ( port res obj )
1509 2dup EOF character-type objeq? if
1510 2drop 2swap close-port
1514 2swap 2drop ( port obj )
1516 global-env obj@ eval ( port res )
1520 :noname ( args -- finalResult )
1521 2dup 1 ensure-arg-count
1522 car string-type ensure-arg-type
1527 ; make-primitive load
1534 cr ." Welcome to scheme.forth.jl!" cr
1535 ." Use Ctrl-D to exit." cr
1542 cr bold fg green ." > " reset-term
1545 2dup EOF character-type objeq? if
1547 bold fg blue ." Moriturus te saluto." reset-term cr
1551 global-env obj@ eval
1553 fg cyan ." ; " print reset-term