From 288370d093357bbf0f21c65973e24bb2e7e4a578 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Mon, 18 Apr 2022 20:53:06 +0200 Subject: [PATCH] first try at very basic garbage collection --- bootstrap/ISBPL.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bootstrap/ISBPL.java b/bootstrap/ISBPL.java index b43112e..6efdfe8 100644 --- a/bootstrap/ISBPL.java +++ b/bootstrap/ISBPL.java @@ -28,6 +28,7 @@ public class ISBPL { HashMap level0 = new HashMap<>(); final ISBPLThreadLocal>> functionStack = ISBPLThreadLocal.withInitial(() -> { Stack> stack = new Stack<>(); stack.push(level0); return stack; }); final ISBPLThreadLocal> fileStack = ISBPLThreadLocal.withInitial(Stack::new); + HashMap varLinks = new HashMap<>(); HashMap vars = new HashMap<>(); final ISBPLThreadLocal> lastWords = ISBPLThreadLocal.withInitial(() -> new ArrayList<>(16)); int exitCode; @@ -53,8 +54,10 @@ public class ISBPL { return (idx, words, file, stack) -> { idx++; Object var = new Object(); - addFunction(words[idx], (stack1) -> stack1.push(vars.get(var))); + ISBPLCallable c; + addFunction(words[idx], c = ((stack1) -> stack1.push(vars.get(var)))); addFunction("=" + words[idx], (stack1) -> vars.put(var, stack1.pop())); + varLinks.put(c, var); return idx; }; case "if": @@ -1276,8 +1279,14 @@ public class ISBPL { e.printStackTrace(); } finally { - if(isFunction) - functionStack.get().pop(); + if(isFunction) { + HashMap fstack = functionStack.get().pop(); + for(ISBPLCallable c : fstack.values()) { + if(varLinks.containsKey(c)) { + vars.remove(varLinks.remove(c)); + } + } + } } }