diff options
Diffstat (limited to 'st')
-rw-r--r-- | st/ClassInstance.java | 12 | ||||
-rw-r--r-- | st/MethodInstance.java | 8 | ||||
-rw-r--r-- | st/SymTableVis.java | 53 |
3 files changed, 40 insertions, 33 deletions
diff --git a/st/ClassInstance.java b/st/ClassInstance.java index 8b49236..a444f26 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -3,9 +3,9 @@ package st; import java.util.ArrayList; public class ClassInstance extends AbstractInstance { - private ArrayList<TypeInstance> attrs; // the list of class-fields - private ArrayList<MethodInstance> mtds; // the list of methods - private String ext; // the name of the extended class (null if none) + private ArrayList<String> attrs; // the list of class-fields + private ArrayList<String> mtds; // the list of methods + private String ext; // the name of the extended class (null if none) public ClassInstance(String name) { super(name, TypeEnum.classname); @@ -22,15 +22,15 @@ public class ClassInstance extends AbstractInstance { public String toString() { return name + ":T[" + type + "]E[" + this.ext + "]A[" + this.attrs.toString() + - "]";//M[" + this.mtds.toString() + "]"; + "]M[" + this.mtds.toString() + "]"; } - public void set_attrs(ArrayList<TypeInstance> attrs) { + public void set_attrs(ArrayList<String> attrs) { this.attrs = attrs; } - public void set_mtds(ArrayList<MethodInstance> mtds) { + public void set_mtds(ArrayList<String> mtds) { this.mtds = mtds; } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index 11b233b..8b4ed57 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -3,8 +3,8 @@ package st; 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 + private ArrayList<String> args; // the list of arguments + private ArrayList<String> lvars; // the list of local variables private TypeEnum rtrn; // the returned type public MethodInstance(String name, TypeEnum rtrn) { @@ -20,11 +20,11 @@ public class MethodInstance extends AbstractInstance { "]V[" + this.lvars.toString() + "]"; } - public void set_args(ArrayList<TypeInstance> args) { + public void set_args(ArrayList<String> args) { this.args = args; } - public void set_locals(ArrayList<TypeInstance> lvars) { + public void set_locals(ArrayList<String> lvars) { this.lvars = lvars; } diff --git a/st/SymTableVis.java b/st/SymTableVis.java index 2731a26..399ea09 100644 --- a/st/SymTableVis.java +++ b/st/SymTableVis.java @@ -9,7 +9,7 @@ import misc.*; * Provides default methods which visit each node in the tree in depth-first * order. Your visitors may extend this class. */ -public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { +public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> { public HashMap<String,AbstractInstance> symt = new HashMap<>(); @@ -34,9 +34,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f16 -> "}" * f17 -> "}" */ - public String visit(MainClass n, ArrayList<TypeInstance> argu) { + public R visit(MainClass n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -53,7 +54,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { n.f12.accept(this, argu); n.f13.accept(this, argu); n.f14.accept(this, attr_list); - n.f15.accept(this, argu); + n.f15.accept(this, mtd_list); n.f16.accept(this, argu); n.f17.accept(this, argu); @@ -63,7 +64,8 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { String id = n.f1.f0.tokenImage; ClassInstance type = new ClassInstance(id); - type.set_attrs(attr_list); + type.set_attrs(attr_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); symt.put(id, type); @@ -79,16 +81,16 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public String visit(ClassDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(ClassDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); - // ArrayList<TypeInstance> mtd_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, attr_list); - n.f4.accept(this, argu); + n.f4.accept(this, mtd_list); n.f5.accept(this, argu); @@ -98,7 +100,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { ClassInstance type = new ClassInstance(id); type.set_attrs(attr_list); - // type.set_mtds(mtd_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); // Safe? @@ -118,10 +120,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public String visit(ClassExtendsDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(ClassExtendsDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); - // ArrayList<TypeInstance> mtd_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -129,7 +131,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, attr_list); - n.f6.accept(this, argu); + n.f6.accept(this, mtd_list); n.f7.accept(this, argu); @@ -140,7 +142,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { ClassInstance type = new ClassInstance(id, ext); type.set_attrs(attr_list); - // type.set_mtds(mtd_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); symt.put(id, type); @@ -154,7 +156,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f1 -> Identifier() * f2 -> ";" */ - public String visit(VarDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(VarDeclaration n, ArrayList<String> argu) { PrintFilter.print("Processing declaration", true); @@ -176,7 +178,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { TypeInstance type = new TypeInstance(id, rtrn); PrintFilter.print("Inserting " + type, true); symt.put(id, type); - argu.add(type); + argu.add(id); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -199,10 +201,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f11 -> ";" * f12 -> "}" */ - public String visit(MethodDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(MethodDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> argu_list = new ArrayList<TypeInstance>(); - ArrayList<TypeInstance> var_list = new ArrayList<TypeInstance>(); + ArrayList<String> argu_list = new ArrayList<String>(); + ArrayList<String> var_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -236,11 +238,15 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { } MethodInstance type = new MethodInstance(id, rtrn); + // add children to current class type.set_args(argu_list); type.set_locals(var_list); PrintFilter.print("Inserting " + type, true); symt.put(id, type); + // add method to parent class + argu.add(id); + return null; } @@ -248,7 +254,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f0 -> Type() * f1 -> Identifier() */ - public String visit(FormalParameter n, ArrayList<TypeInstance> argu) { + public R visit(FormalParameter n, ArrayList<String> argu) { // PrintFilter.print("Processing parameter", true); String id = n.f1.f0.tokenImage; @@ -266,8 +272,9 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { } TypeInstance type = new TypeInstance(id, rtrn); - // PrintFilter.print("Adding Argument " + type, true); - argu.add(type); + + // add type to parent class + argu.add(id); n.f0.accept(this, argu); n.f1.accept(this, argu); |