diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-03-31 23:04:35 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-03-31 23:04:35 -0600 |
commit | 23b6f865aad683d7f3590c46aa4b74f0a030f6af (patch) | |
tree | d1ce3e147b0ced8c47c248df2405588501b0dea8 /st | |
parent | 835e614e1e2531db3dc7dbe027de122a9fa0b78f (diff) |
Added argument+parameter storing for methods to SymbolTable
Diffstat (limited to 'st')
-rw-r--r-- | st/ClassInstance.java | 2 | ||||
-rw-r--r-- | st/MethodInstance.java | 19 | ||||
-rw-r--r-- | st/SymTableVis.java | 72 |
3 files changed, 70 insertions, 23 deletions
diff --git a/st/ClassInstance.java b/st/ClassInstance.java index c1b07af..44b41c9 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -9,6 +9,8 @@ public class ClassInstance extends AbstractInstance { public ClassInstance(String name) { super(name, TypeEnum.classname); + this.attrs = new ArrayList<>(); + this.mtds = new ArrayList<>(); this.ext = null; } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index c0082af..11b233b 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -3,22 +3,29 @@ package st; import java.util.ArrayList; public class MethodInstance extends AbstractInstance { - private ArrayList<TypeEnum> args; // the list of arguments - private TypeEnum rtrn; // the returned type + private ArrayList<TypeInstance> args; // the list of arguments + private ArrayList<TypeInstance> lvars; // the list of local variables + private TypeEnum rtrn; // the returned type public MethodInstance(String name, TypeEnum rtrn) { super(name, TypeEnum.method); + this.lvars = new ArrayList<>(); this.args = new ArrayList<>(); this.rtrn = rtrn; } public String toString() { - return name + ":" + type + "[" + - this.rtrn + "]"; + return name + ":T[" + type + "]R[" + + this.rtrn + "]P[" + this.args.toString() + + "]V[" + this.lvars.toString() + "]"; } - public void add_argument(TypeEnum arg) { - this.args.add(arg); + public void set_args(ArrayList<TypeInstance> args) { + this.args = args; + } + + public void set_locals(ArrayList<TypeInstance> lvars) { + this.lvars = lvars; } } diff --git a/st/SymTableVis.java b/st/SymTableVis.java index 280a89c..fb62979 100644 --- a/st/SymTableVis.java +++ b/st/SymTableVis.java @@ -9,10 +9,11 @@ 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<R,A> extends GJDepthFirst<R,A> { +public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { public HashMap<String,AbstractInstance> symt = new HashMap<>(); + /** * f0 -> "class" * f1 -> Identifier() @@ -33,7 +34,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f16 -> "}" * f17 -> "}" */ - public R visit(MainClass n, A argu) { + public String visit(MainClass n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing main", true); @@ -73,7 +74,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public R visit(ClassDeclaration n, A argu) { + public String visit(ClassDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing class", true); @@ -105,7 +106,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public R visit(ClassExtendsDeclaration n, A argu) { + public String visit(ClassExtendsDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing class", true); String id = n.f1.f0.tokenImage; @@ -134,7 +135,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f1 -> Identifier() * f2 -> ";" */ - public R visit(VarDeclaration n, A argu) { + public String visit(VarDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing declaration", true); @@ -156,6 +157,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { TypeInstance type = new TypeInstance(id, rtrn); PrintFilter.print("Inserting " + type, true); symt.put(id, type); + argu.add(type); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -178,7 +180,25 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f11 -> ";" * f12 -> "}" */ - public R visit(MethodDeclaration n, A argu) { + public String visit(MethodDeclaration n, ArrayList<TypeInstance> argu) { + + ArrayList<TypeInstance> argu_list = new ArrayList<TypeInstance>(); + ArrayList<TypeInstance> var_list = new ArrayList<TypeInstance>(); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu_list); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, var_list); + n.f8.accept(this, argu); + n.f9.accept(this, argu); + n.f10.accept(this, argu); + n.f11.accept(this, argu); + n.f12.accept(this, argu); + PrintFilter.print("Processing method", true); @@ -197,22 +217,40 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { } MethodInstance type = new MethodInstance(id, rtrn); + type.set_args(argu_list); + type.set_locals(var_list); PrintFilter.print("Inserting " + type, true); symt.put(id, type); + return null; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public String visit(FormalParameter n, ArrayList<TypeInstance> argu) { + PrintFilter.print("Processing parameter", true); + + String id = n.f1.f0.tokenImage; + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f0.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + default: + PrintFilter.print("Unsupported case", true); + } + + TypeInstance type = new TypeInstance(id, rtrn); + PrintFilter.print("Adding Argument " + type, true); + argu.add(type); + n.f0.accept(this, argu); n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); return null; } |