summaryrefslogtreecommitdiff
path: root/st/SymbolTable.java
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-25 00:17:56 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-25 00:18:51 -0600
commit765337c53286db39ffc3c64eede602afe3899647 (patch)
tree893b488f70b22c4d18cec6934ed479feb0943b54 /st/SymbolTable.java
parentd2017d232557b45792857b972ca8883df6e79506 (diff)
Rewrote SymbolTable to add uniqueness to stored tokens
Diffstat (limited to 'st/SymbolTable.java')
-rw-r--r--st/SymbolTable.java80
1 files changed, 50 insertions, 30 deletions
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index 77b8a93..e6c4738 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -9,18 +9,19 @@ import misc.*;
* of each symbol consistent.
*/
public class SymbolTable {
- private HashMap<String,AbstractInstance> symt; // the mapping of ids to Instances
- private HashMap<TypeEnum,String> active; // the current scope of the visitor (class, method)
+ private HashMap<TokenKey,AbstractInstance> symt; // the mapping of ids to Instances
+ private HashMap<TypeEnum,AbstractInstance> active; // the current scope of the visitor (class, method)
public SymbolTable() {
+ MinimalLogger.info("Creating a new SymbolTable...");
this.symt = new HashMap<>();
this.active = new HashMap<>();
}
@Override public String toString() {
StringBuilder mapAsString = new StringBuilder("{");
- for (String key : this.symt.keySet()) {
- mapAsString.append(key + ":" + this.symt.get(key).getType() + ", ");
+ for (TokenKey key : this.symt.keySet()) {
+ mapAsString.append(key.toString() + ":" + this.symt.get(key).getType() + ", ");
}
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
return mapAsString.toString();
@@ -30,7 +31,10 @@ public class SymbolTable {
/**
* Methods intended to be used during the first pass
*/
- public void put(String id, AbstractInstance symbol) {
+ public void put(TokenKey id, AbstractInstance symbol) {
+ MinimalLogger.info(String.format("Inserting %s -> %s",
+ id,
+ symbol.getType()));
this.symt.put(id, symbol);
}
@@ -38,71 +42,87 @@ public class SymbolTable {
/**
* Methods intended to be used during the second pass
*/
- public void setExtend(String arg) {
- String str = this.active.get(TypeEnum.classname);
- ClassInstance cls = this.getClass(str);
+ public void setExtend(TokenKey arg) {
+ ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname);
ClassInstance ext = this.getClass(arg);
+ MinimalLogger.info(String.format("%s found to extend %s",
+ cls.getName(),
+ ext.getName()));
cls.setExtend(ext);
// for (TypeInstance : ext.attrs) //FIXME add the extended classes' stuff
// cls.add
}
- public void addLocal(String lvar) {
+ public void addLocal(TokenKey lvar) {
TypeInstance var = this.getType(lvar);
+ AbstractInstance par;
if (this.active.get(TypeEnum.method) != null) { // we are in a method
- String str = this.active.get(TypeEnum.method);
- MethodInstance mtd = this.getMethod(str);
- mtd.addLocal(var);
- var.setScope(mtd);
+ MethodInstance par1 = (MethodInstance) this.active.get(TypeEnum.method);
+ par1.addLocal(var);
+ var.setScope(par1);
+ par = par1;
} else {
- String str = this.active.get(TypeEnum.classname);
- ClassInstance cls = this.getClass(str);
- cls.addLocal(var);
- var.setScope(cls);
+ ClassInstance par1 = (ClassInstance) this.active.get(TypeEnum.classname);
+ par1.addLocal(var);
+ var.setScope(par1);
+ par = par1;
}
+ MinimalLogger.info(String.format("Added %s (%s) as a local var of %s (%s)",
+ var.getName(), var.getType(),
+ par.getName(), par.getType()));
}
- public void addMethod(String mtd) {
- String str = this.active.get(TypeEnum.classname);
- ClassInstance cls = this.getClass(str);
+ public void addMethod(TokenKey mtd) {
+ ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname);
MethodInstance lmtd = this.getMethod(mtd);
lmtd.setScope(cls);
cls.addMethod(lmtd);
+
+ MinimalLogger.info(String.format("Added %s as a method of %s",
+ lmtd.getName(), cls.getName()));
}
- public void addParameter(String arg) {
- String str = this.active.get(TypeEnum.method);
- MethodInstance mtd = this.getMethod(str);
+ public void addParameter(TokenKey arg) {
+ MethodInstance mtd = (MethodInstance) this.active.get(TypeEnum.method);
TypeInstance para = this.getType(arg);
para.setScope(mtd);
mtd.addArgument(para); // also adds to local vars
+
+ MinimalLogger.info(String.format("Added %s as a parameter of %s",
+ para.getName(), mtd.getName()));
}
public void addClassInstance(AbstractInstance t, String c) {
ClassInstance cls = (c != null) ?
- this.getClass(c) :
+ this.getClass(new TokenKey(c, 0)) :
null;
t.addClassInstance(cls);
}
-
/**
* Methods to safely retrieve differentiable types
* in `typecheck', `vaporize' libraries
*/
- public void setActive(TypeEnum type, String id) {
+ public void setActive(TypeEnum type, AbstractInstance id) {
+ MinimalLogger.info(String.format("%s is now the active %s.",
+ id.getName(),
+ type));
this.active.put(type, id);
}
public void removeActive(TypeEnum type) {
+ AbstractInstance id = this.getActive(type);
+ MinimalLogger.info(String.format("%s is no longer the active %s.",
+ id.getName(),
+ type));
this.active.remove(type);
}
- public TypeInstance getType(String id) {
+ public TypeInstance getType(TokenKey id) {
AbstractInstance symbol;
TypeInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof TypeInstance) ?
@@ -113,7 +133,7 @@ public class SymbolTable {
return ret;
}
- public MethodInstance getMethod(String id) {
+ public MethodInstance getMethod(TokenKey id) {
AbstractInstance symbol;
MethodInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof MethodInstance) ?
@@ -124,7 +144,7 @@ public class SymbolTable {
return ret;
}
- public ClassInstance getClass(String id) {
+ public ClassInstance getClass(TokenKey id) {
AbstractInstance symbol;
ClassInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof ClassInstance) ?
@@ -135,7 +155,7 @@ public class SymbolTable {
return ret;
}
- public String getActive(TypeEnum type) {
+ public AbstractInstance getActive(TypeEnum type) {
return this.active.get(type);
}