summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-25 12:58:10 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-25 12:58:10 -0600
commitc28a1cc9d206bdde41a621b018c01980b3c8a617 (patch)
tree90e9de370313682558172c8cebac9389b48c0855
parentbd44adf2b180fcc1198d612a8ae0d2a28468088d (diff)
Rewrote Symbol Table to be more context aware and avoid collisions
-rw-r--r--J2V.java20
-rw-r--r--st/AbstractInstance.java13
-rw-r--r--st/ClassInstance.java6
-rw-r--r--st/MethodInstance.java19
-rw-r--r--st/SymTableClasses.java116
-rw-r--r--st/SymTableMethods.java (renamed from st/SymTableTopDown.java)100
-rw-r--r--st/SymTableVars.java (renamed from st/SymTableBottomUp.java)80
-rw-r--r--st/SymbolTable.java30
-rw-r--r--st/TokenKey.java17
-rw-r--r--st/TypeInstance.java41
10 files changed, 292 insertions, 150 deletions
diff --git a/J2V.java b/J2V.java
index d19b84e..16f26df 100644
--- a/J2V.java
+++ b/J2V.java
@@ -21,14 +21,18 @@ public class J2V {
// Build the symbol table. Top-down visitor, inherits from
// GJDepthFirst<R,A>. R=Void, A=Integer.
SymbolTable symt = new SymbolTable();
- root.accept(new SymTableBottomUp<Void>(), symt);
- root.accept(new SymTableTopDown<Void>(), symt);
-
- BoilVisitor vp = new BoilVisitor(symt);
- root.accept(vp, null);
-
- MinimalLogger.info("===================================================");
- System.out.println(vp.getVapor());
+ MinimalLogger.info("Populating classes...");
+ root.accept(new SymTableClasses<Void>(), symt);
+ MinimalLogger.info("Populating methods...");
+ root.accept(new SymTableMethods<Void>(), symt);
+ MinimalLogger.info("Populating variables...");
+ root.accept(new SymTableVars<Void>(), symt);
+
+ // BoilVisitor vp = new BoilVisitor(symt);
+ // root.accept(vp, null);
+
+ // MinimalLogger.info("===================================================");
+ // System.out.println(vp.getVapor());
}
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java
index cdeef8e..a7052a1 100644
--- a/st/AbstractInstance.java
+++ b/st/AbstractInstance.java
@@ -5,7 +5,6 @@ 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 ClassInstance cls; // the surrounding class
public AbstractInstance(String name, TypeEnum type) {
this.type = type;
@@ -16,18 +15,12 @@ public abstract class AbstractInstance {
return this.name;
}
- public boolean equals(AbstractInstance other) {
- return this.name == other.getName() &&
- this.type == this.type;
- }
+ @Override public abstract boolean equals(Object other);
@Override public int hashCode() {
return this.name.hashCode();
}
- public void addClassInstance(ClassInstance cls) {
- this.cls = cls;
- }
public String getName() {
return this.name;
@@ -37,8 +30,4 @@ public abstract class AbstractInstance {
return this.type;
}
- public ClassInstance getClassInstance() {
- return this.cls;
- }
-
}
diff --git a/st/ClassInstance.java b/st/ClassInstance.java
index e93d3bd..8394a2c 100644
--- a/st/ClassInstance.java
+++ b/st/ClassInstance.java
@@ -13,6 +13,12 @@ public class ClassInstance extends AbstractInstance {
this.mtds = new ArrayList<>();
}
+ public boolean equals(Object other) {
+ ClassInstance o;
+ return (other instanceof ClassInstance &&
+ ((o = (ClassInstance) other).getName() == this.getName()));
+ }
+
public ClassInstance getExtend() {
/**
* Returns the parent class, or
diff --git a/st/MethodInstance.java b/st/MethodInstance.java
index 6cf6ccc..c2f978b 100644
--- a/st/MethodInstance.java
+++ b/st/MethodInstance.java
@@ -5,15 +5,24 @@ import java.util.ArrayList;
public class MethodInstance extends AbstractInstance {
private ArrayList<TypeInstance> args; // the list of arguments
private ArrayList<TypeInstance> lvars; // the list of local variables
+ protected ClassInstance par_cls; // the surrounding class
private TypeEnum rtrn; // the returned type
- public MethodInstance(String name, TypeEnum rtrn) {
+ public MethodInstance(String name, TypeEnum rtrn, ClassInstance par_cls) {
super(name, TypeEnum.method);
this.lvars = new ArrayList<>();
this.args = new ArrayList<>();
+ this.par_cls = par_cls;
this.rtrn = rtrn;
}
+ public boolean equals(Object other) {
+ MethodInstance o;
+ return (other instanceof MethodInstance &&
+ ((o = (MethodInstance) other).getName() == this.getName() &&
+ o.getParentClass() == this.getParentClass()));
+ }
+
public ArrayList<TypeInstance> getArguments() {
return this.args;
}
@@ -35,4 +44,12 @@ public class MethodInstance extends AbstractInstance {
this.lvars.add(lvar);
}
+ public void addParentClass(ClassInstance par_cls) {
+ this.par_cls = par_cls;
+ }
+
+ public ClassInstance getParentClass() {
+ return this.par_cls;
+ }
+
}
diff --git a/st/SymTableClasses.java b/st/SymTableClasses.java
new file mode 100644
index 0000000..3c0b761
--- /dev/null
+++ b/st/SymTableClasses.java
@@ -0,0 +1,116 @@
+package st;
+
+import syntaxtree.*;
+import visitor.*;
+import java.util.*;
+import misc.*;
+
+/**
+ * Performs a bottom-up preliminary visit through the AST
+ * initializing all ClassInstances and placing them in the passed ST
+ */
+public class SymTableClasses<R> extends GJDepthFirst<R,SymbolTable> {
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> "public"
+ * f4 -> "static"
+ * f5 -> "void"
+ * f6 -> "main"
+ * f7 -> "("
+ * f8 -> "String"
+ * f9 -> "["
+ * f10 -> "]"
+ * f11 -> Identifier()
+ * f12 -> ")"
+ * f13 -> "{"
+ * f14 -> ( VarDeclaration() )*
+ * f15 -> ( Statement() )*
+ * f16 -> "}"
+ * f17 -> "}"
+ */
+ public R visit(MainClass n, SymbolTable symt) {
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ n.f6.accept(this, symt);
+ n.f7.accept(this, symt);
+ n.f8.accept(this, symt);
+ n.f9.accept(this, symt);
+ n.f10.accept(this, symt);
+ n.f11.accept(this, symt);
+ n.f12.accept(this, symt);
+ n.f13.accept(this, symt);
+ n.f14.accept(this, symt);
+ n.f15.accept(this, symt);
+ n.f16.accept(this, symt);
+ n.f17.accept(this, symt);
+
+
+ TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null);
+ ClassInstance instance = new ClassInstance(id.getName());
+ symt.put(id, instance);
+
+ return null;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> ( VarDeclaration() )*
+ * f4 -> ( MethodDeclaration() )*
+ * f5 -> "}"
+ */
+ public R visit(ClassDeclaration n, SymbolTable symt) {
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+
+
+ TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null);
+ ClassInstance instance = new ClassInstance(id.getName());
+ symt.put(id, instance);
+
+
+ return null;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "extends"
+ * f3 -> Identifier()
+ * f4 -> "{"
+ * f5 -> ( VarDeclaration() )*
+ * f6 -> ( MethodDeclaration() )*
+ * f7 -> "}"
+ */
+ public R visit(ClassExtendsDeclaration n, SymbolTable symt) {
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ n.f6.accept(this, symt);
+ n.f7.accept(this, symt);
+
+
+ TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null);
+ ClassInstance instance = new ClassInstance(id.getName());
+ symt.put(id, instance);
+
+
+ return null;
+ }
+
+}
diff --git a/st/SymTableTopDown.java b/st/SymTableMethods.java
index 5bfd971..6dc7d68 100644
--- a/st/SymTableTopDown.java
+++ b/st/SymTableMethods.java
@@ -6,10 +6,10 @@ import java.util.*;
import misc.*;
/**
- * Performs a top-down final visit through the AST adding all local
- * methods, attributes, extension, and scoping information to the ST
+ * Performs a bottom-up preliminary visit through the AST
+ * initializing all Instances and placing them in the passed ST
*/
-public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
+public class SymTableMethods<R> extends GJDepthFirst<R,SymbolTable> {
/**
* f0 -> "class"
@@ -32,13 +32,8 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
* f17 -> "}"
*/
public R visit(MainClass n, SymbolTable symt) {
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- symt.setActive(TypeEnum.classname, symt.getClass(id));
- TokenKey main = new TokenKey(n.f6.tokenImage, n.f6.beginLine);
- symt.setActive(TypeEnum.method, symt.getMethod(main));
- symt.addMethod(main);
-
-
+ String act = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(act));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -58,7 +53,12 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
n.f16.accept(this, symt);
n.f17.accept(this, symt);
- symt.removeActive(TypeEnum.method);
+ TokenKey id = new TokenKey(n.f6.tokenImage, (ClassInstance) symt.getActive(TypeEnum.classname), null);
+ MethodInstance main = new MethodInstance(id.getName(), TypeEnum.ERROR, (ClassInstance) symt.getActive(TypeEnum.classname));
+ symt.put(id, main);
+
+
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -71,16 +71,17 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
* f5 -> "}"
*/
public R visit(ClassDeclaration n, SymbolTable symt) {
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- symt.setActive(TypeEnum.classname, symt.getClass(id));
-
-
+ String act = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(act));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
n.f3.accept(this, symt);
n.f4.accept(this, symt);
n.f5.accept(this, symt);
+
+
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -95,12 +96,8 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
* f7 -> "}"
*/
public R visit(ClassExtendsDeclaration n, SymbolTable symt) {
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- symt.setActive(TypeEnum.classname, symt.getClass(id));
- TokenKey ext = new TokenKey(n.f3.f0.tokenImage, 0);
- symt.setExtend(ext);
-
-
+ String act = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(act));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -109,28 +106,9 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
n.f5.accept(this, symt);
n.f6.accept(this, symt);
n.f7.accept(this, symt);
- return null;
- }
-
- /**
- * f0 -> Type()
- * f1 -> Identifier()
- * f2 -> ";"
- */
- public R visit(VarDeclaration n, SymbolTable symt) {
- String cls = (n.f0.f0.which == 3) ?
- ((Identifier) n.f0.f0.choice).f0.tokenImage :
- null;
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine);
- TypeInstance me = symt.getType(id);
- symt.addLocal(id);
- symt.addClassInstance(me, cls);
- n.f0.accept(this, symt);
- n.f1.accept(this, symt);
- n.f2.accept(this, symt);
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -150,15 +128,6 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
* f12 -> "}"
*/
public R visit(MethodDeclaration n, SymbolTable symt) {
- ClassInstance cls = (ClassInstance) symt.getActive(TypeEnum.classname);
-
- TokenKey id = new TokenKey(n.f2.f0.tokenImage, n.f2.f0.beginLine);
- MethodInstance me = symt.getMethod(id);
- symt.setActive(TypeEnum.method, me);
- symt.addMethod(id);
- symt.addClassInstance(me, cls.getName());
-
-
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -172,21 +141,28 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
n.f10.accept(this, symt);
n.f11.accept(this, symt);
n.f12.accept(this, symt);
- symt.removeActive(TypeEnum.method);
- return null;
- }
- /**
- * f0 -> Type()
- * f1 -> Identifier()
- */
- public R visit(FormalParameter n, SymbolTable symt) {
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine);
- symt.addParameter(id);
- n.f0.accept(this, symt);
- n.f1.accept(this, symt);
+ TokenKey id = new TokenKey(n.f2.f0.tokenImage, (ClassInstance) symt.getActive(TypeEnum.classname), null);
+ TypeEnum rtrn = TypeEnum.ERROR;
+ switch (n.f1.f0.which) {
+ case 0:
+ rtrn = TypeEnum.intarray; break;
+ case 1:
+ rtrn = TypeEnum.bool; break;
+ case 2:
+ rtrn = TypeEnum.integer; break;
+ case 3:
+ rtrn = TypeEnum.classname; break;
+ default:
+ MinimalLogger.severe("Unsupported case");
+ }
+
+ MethodInstance instance = new MethodInstance(id.getName(), rtrn, (ClassInstance) symt.getActive(TypeEnum.classname));
+ symt.put(id, instance);
+
+
return null;
}
diff --git a/st/SymTableBottomUp.java b/st/SymTableVars.java
index 72a4575..f356a8c 100644
--- a/st/SymTableBottomUp.java
+++ b/st/SymTableVars.java
@@ -9,7 +9,7 @@ import misc.*;
* Performs a bottom-up preliminary visit through the AST
* initializing all Instances and placing them in the passed ST
*/
-public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
+public class SymTableVars<R> extends GJDepthFirst<R,SymbolTable> {
/**
* f0 -> "class"
@@ -32,6 +32,10 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
* f17 -> "}"
*/
public R visit(MainClass n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(id));
+ id = n.f6.tokenImage;
+ symt.setActive(TypeEnum.method, symt.getMethod(id));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -51,16 +55,9 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
n.f16.accept(this, symt);
n.f17.accept(this, symt);
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- ClassInstance instance = new ClassInstance(id.getName());
- symt.put(id, instance);
-
- id = new TokenKey(n.f6.tokenImage, n.f6.beginLine);
- MethodInstance main = new MethodInstance(id.getName(), TypeEnum.ERROR);
- symt.put(id, main);
-
-
+ symt.addMethod(id);
+ symt.removeActive(TypeEnum.method);
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -73,6 +70,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
* f5 -> "}"
*/
public R visit(ClassDeclaration n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(id));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -80,12 +79,7 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
n.f4.accept(this, symt);
n.f5.accept(this, symt);
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- ClassInstance instance = new ClassInstance(id.getName());
- symt.put(id, instance);
-
-
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -100,6 +94,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
* f7 -> "}"
*/
public R visit(ClassExtendsDeclaration n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, symt.getClass(id));
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -109,12 +105,7 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
n.f6.accept(this, symt);
n.f7.accept(this, symt);
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0);
- ClassInstance instance = new ClassInstance(id.getName());
- symt.put(id, instance);
-
-
+ symt.removeActive(TypeEnum.classname);
return null;
}
@@ -127,8 +118,9 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine);
+ TokenKey id = new TokenKey(n.f1.f0.tokenImage,
+ (ClassInstance) symt.getActive(TypeEnum.classname),
+ (MethodInstance) symt.getActive(TypeEnum.method));
TypeEnum rtrn = TypeEnum.ERROR;
switch (n.f0.f0.which) {
case 0:
@@ -143,9 +135,10 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
MinimalLogger.severe("Unsupported case");
}
- TypeInstance instance = new TypeInstance(id.getName(), rtrn);
+ TypeInstance instance = new TypeInstance(id.getName(), rtrn, (MethodInstance) symt.getActive(TypeEnum.method),
+ (ClassInstance) symt.getActive(TypeEnum.classname));
symt.put(id, instance);
-
+ symt.addLocal(id.getName());
return null;
}
@@ -166,6 +159,9 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
* f12 -> "}"
*/
public R visit(MethodDeclaration n, SymbolTable symt) {
+ String id = n.f2.f0.tokenImage;
+ symt.setActive(TypeEnum.method, symt.getMethod(id));
+ symt.addMethod(id);
n.f0.accept(this, symt);
n.f1.accept(this, symt);
n.f2.accept(this, symt);
@@ -181,26 +177,7 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
n.f12.accept(this, symt);
-
- TokenKey id = new TokenKey(n.f2.f0.tokenImage, n.f2.f0.beginLine);
- TypeEnum rtrn = TypeEnum.ERROR;
- switch (n.f1.f0.which) {
- case 0:
- rtrn = TypeEnum.intarray; break;
- case 1:
- rtrn = TypeEnum.bool; break;
- case 2:
- rtrn = TypeEnum.integer; break;
- case 3:
- rtrn = TypeEnum.classname; break;
- default:
- MinimalLogger.severe("Unsupported case");
- }
-
- MethodInstance instance = new MethodInstance(id.getName(), rtrn);
- symt.put(id, instance);
-
-
+ symt.removeActive(TypeEnum.method);
return null;
}
@@ -211,9 +188,10 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
public R visit(FormalParameter n, SymbolTable symt) {
n.f0.accept(this, symt);
n.f1.accept(this, symt);
+ TokenKey id = new TokenKey(n.f1.f0.tokenImage,
+ (ClassInstance) symt.getActive(TypeEnum.classname),
+ (MethodInstance) symt.getActive(TypeEnum.method));
-
- TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine);
TypeEnum rtrn = TypeEnum.ERROR;
switch (n.f0.f0.which) {
case 0:
@@ -228,8 +206,10 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
MinimalLogger.severe("Unsupported case");
}
- TypeInstance instance = new TypeInstance(id.getName(), rtrn);
+ TypeInstance instance = new TypeInstance(id.getName(), rtrn, (MethodInstance) symt.getActive(TypeEnum.method),
+ (ClassInstance) symt.getActive(TypeEnum.classname));
symt.put(id, instance);
+ symt.addParameter(id.getName());
return null;
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index dcc1f5b..a8a6dcf 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -42,7 +42,7 @@ public class SymbolTable {
/**
* Methods intended to be used during the second pass
*/
- public void setExtend(TokenKey arg) {
+ public void setExtend(String arg) {
ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname);
ClassInstance ext = this.getClass(arg);
@@ -64,7 +64,7 @@ public class SymbolTable {
}
}
- public void addLocal(TokenKey lvar) {
+ public void addLocal(String lvar) {
TypeInstance var = this.getType(lvar);
AbstractInstance par;
if (this.active.get(TypeEnum.method) != null) { // we are in a method
@@ -81,7 +81,7 @@ public class SymbolTable {
par.getName(), par.getType()));
}
- public void addMethod(TokenKey mtd) {
+ public void addMethod(String mtd) {
ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname);
MethodInstance lmtd = this.getMethod(mtd);
@@ -91,7 +91,7 @@ public class SymbolTable {
lmtd.getName(), cls.getName()));
}
- public void addParameter(TokenKey arg) {
+ public void addParameter(String arg) {
MethodInstance mtd = (MethodInstance) this.active.get(TypeEnum.method);
TypeInstance para = this.getType(arg);
@@ -99,11 +99,14 @@ public class SymbolTable {
MinimalLogger.info(String.format("Added %s as a parameter of %s",
para.getName(), mtd.getName()));
+ MinimalLogger.info(String.format("Added %s as a localvar of %s",
+ para.getName(), mtd.getName()));
+
}
- public void addClassInstance(AbstractInstance t, String c) {
+ public void addClassInstance(TypeInstance t, String c) {
ClassInstance cls = (c != null) ?
- this.getClass(new TokenKey(c, 0)) :
+ this.getClass(c) :
null;
t.addClassInstance(cls);
@@ -128,7 +131,10 @@ public class SymbolTable {
this.active.remove(type);
}
- public TypeInstance getType(TokenKey id) {
+ public TypeInstance getType(String name) {
+ TokenKey id = new TokenKey(name,
+ (ClassInstance) this.getActive(TypeEnum.classname),
+ (MethodInstance) this.getActive(TypeEnum.method));
AbstractInstance symbol;
TypeInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof TypeInstance) ?
@@ -139,7 +145,10 @@ public class SymbolTable {
return ret;
}
- public MethodInstance getMethod(TokenKey id) {
+ public MethodInstance getMethod(String name) {
+ TokenKey id = new TokenKey(name,
+ (ClassInstance) this.getActive(TypeEnum.classname),
+ null);
AbstractInstance symbol;
MethodInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof MethodInstance) ?
@@ -150,7 +159,10 @@ public class SymbolTable {
return ret;
}
- public ClassInstance getClass(TokenKey id) {
+ public ClassInstance getClass(String name) {
+ TokenKey id = new TokenKey(name,
+ null,
+ null);
AbstractInstance symbol;
ClassInstance ret = ((symbol = this.symt.get(id)) !=
null && symbol instanceof ClassInstance) ?
diff --git a/st/TokenKey.java b/st/TokenKey.java
index 2319917..54086c5 100644
--- a/st/TokenKey.java
+++ b/st/TokenKey.java
@@ -8,18 +8,20 @@ package st;
public class TokenKey {
private String name;
- private int beginLine;
+ private AbstractInstance cls; // null for classes
+ private AbstractInstance mtd; // null for classes, methods
- public TokenKey(String name, int beginLine) {
- // classes CANNOT collide, so CALLEES ARE EXPECTED TO USE ZERO!
+ public TokenKey(String name, AbstractInstance cls, AbstractInstance mtd) {
this.name = name;
- this.beginLine = beginLine;
+ this.cls = cls;
+ this.mtd = mtd;
}
@Override public String toString() {
- return String.format("%s (%d)",
+ return String.format("%s (%s,%s)",
this.name,
- this.beginLine);
+ this.mtd,
+ this.cls);
}
@@ -28,7 +30,8 @@ public class TokenKey {
TokenKey o;
if (other instanceof TokenKey &&
(o = (TokenKey) other).name == this.name &&
- o.beginLine == this.beginLine) {
+ o.cls == this.cls &&
+ o.mtd == this.mtd) {
ret = true;
}
return ret;
diff --git a/st/TypeInstance.java b/st/TypeInstance.java
index 88b73f3..e43c5b3 100644
--- a/st/TypeInstance.java
+++ b/st/TypeInstance.java
@@ -2,8 +2,23 @@ package st;
public class TypeInstance extends AbstractInstance {
- public TypeInstance(String name, TypeEnum type) {
+ protected ClassInstance par_cls; // the surrounding class
+ protected MethodInstance par_mtd; // the surrounding method
+ protected ClassInstance cls; // the class instance
+
+
+ public TypeInstance(String name, TypeEnum type, MethodInstance par_mtd, ClassInstance par_cls) {
super(name, type);
+ this.par_cls = par_cls;
+ this.par_mtd = par_mtd;
+ }
+
+ @Override public boolean equals(Object other) {
+ TypeInstance o;
+ return (other instanceof TypeInstance &&
+ ((o = (TypeInstance) other).getName() == this.getName() &&
+ o.getParentClass() == this.getParentClass() &&
+ o.getParentMethod() == this.getParentMethod()));
}
public int getSize() {
@@ -30,4 +45,28 @@ public class TypeInstance extends AbstractInstance {
return type != TypeEnum.ERROR;
}
+ public void addClassInstance(ClassInstance cls) {
+ this.cls = cls;
+ }
+
+ public ClassInstance getClassInstance() {
+ return this.cls;
+ }
+
+ public void addParentClass(ClassInstance par_cls) {
+ this.par_cls = par_cls;
+ }
+
+ public ClassInstance getParentClass() {
+ return this.par_cls;
+ }
+
+ public void addParentMethod(MethodInstance par_mtd) {
+ this.par_mtd = par_mtd;
+ }
+
+ public MethodInstance getParentMethod() {
+ return this.par_mtd;
+ }
+
}