Implemented scaffolding for mark+sweep GC.
[scheme.forth.jl.git] / scheme.4th
index 8bc44d3..3e7b827 100644 (file)
@@ -3,7 +3,9 @@ scheme definitions
 
 include term-colours.4th
 include defer-is.4th
-include catch-throw.4th
+include float.4th
+
+include debugging.4th
 
 defer read
 defer eval
@@ -11,15 +13,23 @@ defer print
 
 \ ------ Types ------
 
-0 constant fixnum-type
-1 constant boolean-type
-2 constant character-type
-3 constant string-type
-4 constant nil-type
-5 constant pair-type
-6 constant symbol-type
-7 constant primitive-proc-type
-8 constant compound-proc-type
+variable nexttype
+0 nexttype !
+: make-type
+    create nexttype @ ,
+    1 nexttype +!
+    does> @ ;
+
+make-type fixnum-type
+make-type realnum-type
+make-type boolean-type
+make-type character-type
+make-type string-type
+make-type nil-type
+make-type pair-type
+make-type symbol-type
+make-type primitive-proc-type
+make-type compound-proc-type
 : istype? ( obj type -- obj bool )
     over = ;
 
@@ -31,9 +41,20 @@ create car-type-cells N allot
 create cdr-cells N allot
 create cdr-type-cells N allot
 
+create nextfrees N allot
+:noname
+    N 0 do
+        i 1+ nextfrees i + !
+    loop
+; execute
+        
 variable nextfree
 0 nextfree !
 
+: inc-nextfree
+    nextfrees nextfree @ + @
+    nextfree ! ;
+
 : cons ( car-obj cdr-obj -- pair-obj )
     cdr-type-cells nextfree @ + !
     cdr-cells nextfree @ + !
@@ -41,8 +62,7 @@ variable nextfree
     car-cells nextfree @ + !
 
     nextfree @ pair-type
-
-    1 nextfree +!
+    inc-nextfree
 ;
 
 : car ( pair-obj -- car-obj )
@@ -105,6 +125,63 @@ variable nextfree
 
 \ }}}
 
+\ ---- Garbage Collection ---- {{{
+
+variable gc-enabled
+false gc-enabled !
+
+: gc-enable
+    true gc-enabled ! ;
+
+: gc-disable
+    false gc-enabled ! ;
+
+: gc-enabled?
+    gc-enabled @ ;
+
+: pairlike? ( obj -- obj bool )
+    pair-type istype? if true exit then
+    string-type istype? if true exit then
+    symbol-type istype? if true exit then
+
+    false
+;
+
+: pairlike-marked? ( obj -- obj bool )
+    over nextfrees + 0=
+;
+
+: mark-pairlike ( obj -- obj )
+        over nextfrees + 0 swap !
+;
+
+: gc-mark-obj ( obj -- )
+
+    pairlike? if
+        pairlike-marked? if 2drop exit then
+            
+        mark-pairlike
+
+        2dup
+
+        car recurse
+        cdr recurse
+    else
+        2drop
+    then
+;
+
+: gc-sweep
+    N nextfree !
+    0 N 1- do
+        nextfrees i + @ 0<> if
+            nextfree @ nextfrees i + !
+            i nextfree !
+        then
+    -1 +loop ;
+
+\ }}}
+
 \ ---- Pre-defined symbols ---- {{{
 
 objvar symbol-table
@@ -332,6 +409,8 @@ global-env obj!
     bl word
     count
 
+    \ 2dup ." Defining primitive " type ." ..." cr
+
     (create-symbol)
     drop symbol-type
     
@@ -500,13 +579,52 @@ parse-idx-stack parse-idx-sp !
         inc-parse-idx
     repeat
 
-    delim? if
-        pop-parse-idx
-        true
-    else
-        pop-parse-idx
-        false
+    delim? pop-parse-idx
+;
+
+: realnum? ( -- bool )
+    push-parse-idx
+
+    minus? plus? or if
+        inc-parse-idx
+    then
+
+    \ Record starting parse idx:
+    \ Want to detect whether any characters (following +/-) were eaten.
+    parse-idx @
+
+    begin digit? while
+            inc-parse-idx
+    repeat
+
+    [char] . nextchar = if
+        inc-parse-idx
+        begin digit? while
+                inc-parse-idx
+        repeat
+    then
+
+    [char] e nextchar = [char] E nextchar = or if
+        inc-parse-idx
+
+        minus? plus? or if
+            inc-parse-idx
+        then
+
+        digit? invert if
+            drop pop-parse-idx false exit
+        then
+
+        begin digit? while
+                inc-parse-idx
+        repeat
     then
+
+    \ This is a real number if characters were
+    \ eaten and the next characer is a delimiter.
+    parse-idx @ < delim? and
+
+    pop-parse-idx
 ;
 
 : boolean? ( -- bool )
@@ -576,7 +694,7 @@ parse-idx-stack parse-idx-sp !
 : string? ( -- bool )
     nextchar [char] " = ;
 
-: readnum ( -- num-atom )
+: readfixnum ( -- num-atom )
     plus? minus? or if
         minus?
         inc-parse-idx
@@ -596,6 +714,24 @@ parse-idx-stack parse-idx-sp !
     fixnum-type
 ;
 
+: readrealnum ( -- realnum )
+
+    \ Remember that at this point we're guaranteed to
+    \ have a parsable real on this line.
+
+    parse-str parse-idx @ +
+
+    begin delim? false = while
+            inc-parse-idx
+    repeat
+
+    parse-str parse-idx @ + over -
+
+    float-parse
+
+    realnum-type
+;
+
 : readbool ( -- bool-atom )
     inc-parse-idx
     
@@ -713,7 +849,12 @@ parse-idx-stack parse-idx-sp !
     eatspaces
 
     fixnum? if
-        readnum
+        readfixnum
+        exit
+    then
+
+    realnum? if
+        readrealnum
         exit
     then
 
@@ -769,7 +910,7 @@ parse-idx-stack parse-idx-sp !
 
     eof? if
         inc-parse-idx
-        bold fg blue ." Moriturus te saluto." reset-term ."  ok" cr
+        bold fg blue ." Moriturus te saluto." reset-term cr
         quit
     then
 
@@ -791,6 +932,7 @@ parse-idx-stack parse-idx-sp !
 : self-evaluating? ( obj -- obj bool )
     boolean-type istype? if true exit then
     fixnum-type istype? if true exit then
+    realnum-type istype? if true exit then
     character-type istype? if true exit then
     string-type istype? if true exit then
     nil-type istype? if true exit then
@@ -1059,7 +1201,9 @@ parse-idx-stack parse-idx-sp !
 
 \ ---- Print ---- {{{
 
-: printnum ( numobj -- ) drop 0 .R ;
+: printfixnum ( fixnumobj -- ) drop 0 .R ;
+
+: printrealnum ( realnumobj -- ) drop float-print ;
 
 : printbool ( numobj -- )
     drop if
@@ -1124,7 +1268,8 @@ parse-idx-stack parse-idx-sp !
     2drop ." <compound procedure>" ;
 
 :noname ( obj -- )
-    fixnum-type istype? if printnum exit then
+    fixnum-type istype? if printfixnum exit then
+    realnum-type istype? if printrealnum exit then
     boolean-type istype? if printbool exit then
     character-type istype? if printchar exit then
     string-type istype? if printstring exit then
@@ -1146,12 +1291,15 @@ parse-idx-stack parse-idx-sp !
     cr ." Welcome to scheme.forth.jl!" cr
        ." Use Ctrl-D to exit." cr
 
+    
     empty-parse-str
 
     begin
         cr bold fg green ." > " reset-term
         read
+
         global-env obj@ eval
+
         fg cyan ." ; " print reset-term
     again
 ;