From 1ec847c7222b8adb9a70264c98a44dc9911d65d3 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Sun, 7 Apr 2024 12:26:47 -0600 Subject: More bugfixes in ST, changes to scoping --- st/AbstractInstance.java | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'st/AbstractInstance.java') diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index a6c2ca1..bddaccf 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -1,14 +1,17 @@ package st; +import java.util.ArrayList; + public abstract class AbstractInstance { - protected String name; // the literal name of the declaration - protected TypeEnum type; // the type of the declaration - protected int size; // the size in memory - protected AbstractInstance scope; // the surrounding method or class + protected String name; // the literal name of the declaration + protected TypeEnum type; // the type of the declaration + protected int size; // the size in memory + protected ArrayList scope; // the scope where the instance is valid public AbstractInstance(String name, TypeEnum type) { this.type = type; this.name = name; + this.scope = new ArrayList<>(); } public String toString() { @@ -24,11 +27,18 @@ public abstract class AbstractInstance { return this.name.hashCode(); } - public void setScope(AbstractInstance cls) { - if (this.scope != null) - throw new RuntimeException("setScope: Attempted to set scope twice!"); - - this.scope = cls; + public void setScope(AbstractInstance ins) { + /** + * If the scope is a MethodInstance, add the single method. + * If the scope is a ClassInstance, add the classes' methods, + * and the class itself. + */ + if (ins instanceof MethodInstance) + this.scope.add(ins); + else if (ins instanceof ClassInstance) { + for (MethodInstance mtd : ((ClassInstance) ins).getMethods()) + this.scope.add(mtd); + } } public String getName() { @@ -43,11 +53,7 @@ public abstract class AbstractInstance { return this.size; } - public AbstractInstance getScope() { - /** - * Returns the scope of the method, or - * `null' if unset. - */ + public ArrayList getScope() { return this.scope; } -- cgit v1.2.3