Mark-sweep garbage collection working.
[scheme.forth.jl.git] / scheme.4th
index bac3283..1796ea1 100644 (file)
@@ -3,20 +3,23 @@ scheme definitions
 
 include term-colours.4th
 include defer-is.4th
-include throw-catch.4th
 include float.4th
 
+include debugging.4th
+
 defer read
 defer eval
 defer print
 
+defer collect-garbage
+
 \ ------ Types ------
 
 variable nexttype
 0 nexttype !
 : make-type
     create nexttype @ ,
-    nexttype @ 1+ nexttype !
+    1 nexttype +!
     does> @ ;
 
 make-type fixnum-type
@@ -32,17 +35,40 @@ make-type compound-proc-type
 : istype? ( obj type -- obj bool )
     over = ;
 
-\ ------ Cons cell memory ------ {{{
+\ ------ List-structured memory ------ {{{
+
+10000 constant scheme-memsize
 
-10000 constant N
-create car-cells N allot
-create car-type-cells N allot
-create cdr-cells N allot
-create cdr-type-cells N allot
+create car-cells scheme-memsize allot
+create car-type-cells scheme-memsize allot
+create cdr-cells scheme-memsize allot
+create cdr-type-cells scheme-memsize allot
 
+create nextfrees scheme-memsize allot
+:noname
+    scheme-memsize 0 do
+        i 1+ nextfrees i + !
+    loop
+; execute
+        
 variable nextfree
 0 nextfree !
 
+: inc-nextfree
+    nextfrees nextfree @ + @
+    nextfree !
+
+    nextfree @ scheme-memsize >= if
+        collect-garbage
+    then
+
+    nextfree @ scheme-memsize >= if
+        fg red bold
+        ." Out of memory! Aborting."
+        reset-term abort
+    then
+;
+
 : cons ( car-obj cdr-obj -- pair-obj )
     cdr-type-cells nextfree @ + !
     cdr-cells nextfree @ + !
@@ -50,8 +76,7 @@ variable nextfree
     car-cells nextfree @ + !
 
     nextfree @ pair-type
-
-    1 nextfree +!
+    inc-nextfree
 ;
 
 : car ( pair-obj -- car-obj )
@@ -114,6 +139,7 @@ variable nextfree
 
 \ }}}
 
+
 \ ---- Pre-defined symbols ---- {{{
 
 objvar symbol-table
@@ -341,6 +367,8 @@ global-env obj!
     bl word
     count
 
+    \ 2dup ." Defining primitive " type ." ..." cr
+
     (create-symbol)
     drop symbol-type
     
@@ -513,16 +541,16 @@ parse-idx-stack parse-idx-sp !
 ;
 
 : realnum? ( -- bool )
-    \ Record starting parse idx:
-    \ Want to detect whether any characters were eaten.
-    parse-idx @
-
     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
@@ -541,6 +569,10 @@ parse-idx-stack parse-idx-sp !
             inc-parse-idx
         then
 
+        digit? invert if
+            drop pop-parse-idx false exit
+        then
+
         begin digit? while
                 inc-parse-idx
         repeat
@@ -658,7 +690,7 @@ parse-idx-stack parse-idx-sp !
     realnum-type
 ;
 
-: readbool ( -- bool-atom )
+: readbool ( -- bool-obj )
     inc-parse-idx
     
     nextchar [char] f = if
@@ -672,7 +704,7 @@ parse-idx-stack parse-idx-sp !
     boolean-type
 ;
 
-: readchar ( -- char-atom )
+: readchar ( -- char-obj )
     inc-parse-idx
     inc-parse-idx
 
@@ -836,7 +868,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
 
@@ -1211,18 +1243,126 @@ parse-idx-stack parse-idx-sp !
 
 \ }}}
 
+\ ---- Garbage Collection ---- {{{
+
+variable gc-enabled
+false gc-enabled !
+
+variable gc-stack-depth
+
+: enable-gc
+    depth gc-stack-depth !
+    true gc-enabled ! ;
+
+: disable-gc
+    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
+    compound-proc-type istype? if true exit then
+
+    false
+;
+
+: pairlike-marked? ( obj -- obj bool )
+    over nextfrees + @ 0=
+;
+
+: mark-pairlike ( obj -- obj )
+        over nextfrees + 0 swap !
+;
+
+: gc-unmark ( -- )
+    scheme-memsize 0 do
+        1 nextfrees i + !
+    loop
+;
+
+: gc-mark-obj ( obj -- )
+
+    pairlike? invert if 2drop exit then
+    pairlike-marked? if 2drop exit then
+
+    mark-pairlike
+
+    drop pair-type 2dup
+
+    car recurse
+    cdr recurse
+;
+
+: gc-sweep
+    scheme-memsize nextfree !
+    0 scheme-memsize 1- do
+        nextfrees i + @ 0<> if
+            nextfree @ nextfrees i + !
+            i nextfree !
+        then
+    -1 +loop
+;
+
+\ Following a GC, this gives the amount of free memory
+: gc-count-marked
+    0
+    scheme-memsize 0 do
+        nextfrees i + @ 0= if 1+ then
+    loop
+;
+
+\ Debugging word - helps spot memory that is retained
+: gc-zero-unmarked
+    scheme-memsize 0 do
+        nextfrees i + @ 0<> if
+            0 car-cells i + !
+            0 cdr-cells i + !
+        then
+    loop
+;
+
+:noname
+    \ ." GC! "
+
+    gc-unmark
+
+    symbol-table obj@ gc-mark-obj
+    global-env obj@ gc-mark-obj
+
+    depth gc-stack-depth @ do
+        PSP0 i + 1 + @
+        PSP0 i + 2 + @
+
+        gc-mark-obj
+    2 +loop
+
+    gc-sweep
+
+    \ ." (" gc-count-marked . ." pairs marked as used.)" cr
+; is collect-garbage
+
+\ }}}
+
 \ ---- REPL ----
 
 : repl
     cr ." Welcome to scheme.forth.jl!" cr
        ." Use Ctrl-D to exit." cr
 
+    
     empty-parse-str
 
+    enable-gc
+
     begin
         cr bold fg green ." > " reset-term
         read
+
         global-env obj@ eval
+
         fg cyan ." ; " print reset-term
     again
 ;