X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme.4th;h=1796ea19631ff2bbad3907e675a53c2a722b5f9c;hb=7b36afd3962b8ac1389496d86da661664e03e20b;hp=3c4744e59f464c61d47dfe44ab6ce61899b4f421;hpb=41a8ffdeecfc1bf848f83a9ccbf97f73a2e8e739;p=scheme.forth.jl.git diff --git a/scheme.4th b/scheme.4th index 3c4744e..1796ea1 100644 --- a/scheme.4th +++ b/scheme.4th @@ -11,6 +11,8 @@ defer read defer eval defer print +defer collect-garbage + \ ------ Types ------ variable nexttype @@ -33,9 +35,10 @@ make-type compound-proc-type : istype? ( obj type -- obj bool ) over = ; -\ ------ Cons cell memory ------ {{{ +\ ------ List-structured memory ------ {{{ + +10000 constant scheme-memsize -1000 constant scheme-memsize create car-cells scheme-memsize allot create car-type-cells scheme-memsize allot create cdr-cells scheme-memsize allot @@ -53,11 +56,14 @@ variable nextfree : inc-nextfree nextfrees nextfree @ + @ + nextfree ! - dup scheme-memsize < if - nextfree ! - else - bold fg red + nextfree @ scheme-memsize >= if + collect-garbage + then + + nextfree @ scheme-memsize >= if + fg red bold ." Out of memory! Aborting." reset-term abort then @@ -133,63 +139,6 @@ 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 - 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-mark-obj ( obj -- ) - - pairlike? if - pairlike-marked? if 2drop exit then - - mark-pairlike - - 2dup - - car recurse - cdr recurse - else - 2drop - then -; - -: gc-sweep - scheme-memsize nextfree ! - 0 scheme-memsize 1- do - nextfrees i + @ 0<> if - nextfree @ nextfrees i + ! - i nextfree ! - then - -1 +loop ; - -\ }}} \ ---- Pre-defined symbols ---- {{{ @@ -741,7 +690,7 @@ parse-idx-stack parse-idx-sp ! realnum-type ; -: readbool ( -- bool-atom ) +: readbool ( -- bool-obj ) inc-parse-idx nextchar [char] f = if @@ -755,7 +704,7 @@ parse-idx-stack parse-idx-sp ! boolean-type ; -: readchar ( -- char-atom ) +: readchar ( -- char-obj ) inc-parse-idx inc-parse-idx @@ -1294,6 +1243,109 @@ 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 @@ -1303,6 +1355,8 @@ parse-idx-stack parse-idx-sp ! empty-parse-str + enable-gc + begin cr bold fg green ." > " reset-term read