diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-06 20:00:31 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-06 20:00:31 -0600 |
commit | 8e660afb356c1f6d0b9cd115426cf21129e5d304 (patch) | |
tree | a8c885c83b138762ca51af640355048bc305b437 | |
parent | b03859dce5991169b07d1d5040c8faf7ba82e5b5 (diff) |
Untested SymbolTable full implementation
-rw-r--r-- | st/AbstractInstance.java | 22 | ||||
-rw-r--r-- | st/MethodInstance.java | 8 | ||||
-rw-r--r-- | st/SymbolTable.java | 77 | ||||
-rw-r--r-- | st/TypeInstance.java | 9 |
4 files changed, 85 insertions, 31 deletions
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index 65b0db7..7acc3e5 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -1,9 +1,10 @@ package st; 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 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 public AbstractInstance(String name, TypeEnum type) { this.type = type; @@ -22,6 +23,13 @@ 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 String getName() { return this.name; } @@ -34,4 +42,12 @@ public abstract class AbstractInstance { return this.size; } + public AbstractInstance getScope() { + /** + * Returns the scope of the method, or + * `null' if unset. + */ + return this.scope; + } + } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index d6c22b6..25096d2 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -24,12 +24,4 @@ public class MethodInstance extends AbstractInstance { this.lvars.add(lvar); } - public AbstractInstance getScope() { - /** - * Returns the scope of the method, or - * `null' if unset. - */ - return this.scope; - } - } diff --git a/st/SymbolTable.java b/st/SymbolTable.java index 0757541..d6bba37 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -2,12 +2,18 @@ package st; import java.util.*; +/** + * Class which provides methods for interacting with and managing + * the symbol table. Maintains context-awareness to keep the state + * of each symbol consistent. + */ public class SymbolTable { - - private HashMap<String,AbstractInstance> symt; + private HashMap<String,AbstractInstance> symt; // the mapping of ids to Instances + private HashMap<TypeEnum,String> active; // the current scope of the visitor (class, method) public SymbolTable() { this.symt = new HashMap<>(); + this.active = new HashMap<>(); } public String toString() { @@ -19,32 +25,81 @@ public class SymbolTable { return mapAsString.toString(); } - public void addMethod() { - this. - } + /** + * Methods intended to be used during the first pass + */ public void put(String id, AbstractInstance symbol) { this.symt.put(id, symbol); } - public TypeInstance getType(String index) { + + /** + * Methods intended to be used during the second pass + */ + public void setActive(TypeEnum type, String id) { + this.active.put(type, id); + } + + public void addAttribute(String arg) { + String str = this.active.get(TypeEnum.classname); + ClassInstance cls = this.getClass(str); + TypeInstance attr = this.getType(arg); + + attr.setScope(cls); + cls.addAttribute(attr); + } + + public void addMethod(String mtd) { + String str = this.active.get(TypeEnum.classname); + ClassInstance cls = this.getClass(str); + MethodInstance lmtd = this.getMethod(mtd); + + lmtd.setScope(cls); + cls.addMethod(lmtd); + } + + public void addParameter(String arg) { + String str = this.active.get(TypeEnum.method); + MethodInstance mtd = this.getMethod(str); + TypeInstance para = this.getType(arg); + + para.setScope(mtd); + mtd.addArgument(para); // also adds to local vars + } + + public void addLocal(String lvar) { + String str = this.active.get(TypeEnum.method); + MethodInstance mtd = this.getMethod(str); + TypeInstance var = this.getType(lvar); + + var.setScope(mtd); + mtd.addArgument(var); + } + + + /** + * Methods to safely retrieve differentiable types + * in `typecheck', `vaporize' libraries + */ + public TypeInstance getType(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(index)) != + return ((symbol = this.symt.get(id)) != null && symbol.getType() != TypeEnum.classname && symbol.getType() != TypeEnum.method) ? (TypeInstance) symbol : null; } - public MethodInstance getMethod(String index) { + public MethodInstance getMethod(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(index)) != + return ((symbol = this.symt.get(id)) != null && symbol.getType() == TypeEnum.method) ? (MethodInstance) symbol : null; } - public ClassInstance getClass(String index) { + public ClassInstance getClass(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(index)) != + return ((symbol = this.symt.get(id)) != null && symbol.getType() == TypeEnum.classname) ? (ClassInstance) symbol : null; } diff --git a/st/TypeInstance.java b/st/TypeInstance.java index e00c1f9..b4031d9 100644 --- a/st/TypeInstance.java +++ b/st/TypeInstance.java @@ -6,7 +6,6 @@ public class TypeInstance extends AbstractInstance { public TypeInstance(String name, TypeEnum type) { super(name, type); - this.scope = null; } public boolean sameType(TypeInstance other) { @@ -28,12 +27,4 @@ public class TypeInstance extends AbstractInstance { return type != TypeEnum.ERROR; } - public AbstractInstance getScope() { - /** - * Returns the scope of the variable, or - * `null' if unset. - */ - return this.scope; - } - } |