From 0ae01301d572b2e69585c4d1cb753ed7fc89dfe3 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Tue, 2 Apr 2024 23:23:06 -0600 Subject: Store SymTable child information as an array of strings --- st/SymTableVis.java | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'st/SymTableVis.java') 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> { +public class SymTableVis extends GJDepthFirst> { public HashMap symt = new HashMap<>(); @@ -34,9 +34,10 @@ public class SymTableVis extends GJDepthFirst> { * f16 -> "}" * f17 -> "}" */ - public String visit(MainClass n, ArrayList argu) { + public R visit(MainClass n, ArrayList argu) { - ArrayList attr_list = new ArrayList(); + ArrayList attr_list = new ArrayList(); + ArrayList mtd_list = new ArrayList(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -53,7 +54,7 @@ public class SymTableVis extends GJDepthFirst> { 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 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> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public String visit(ClassDeclaration n, ArrayList argu) { + public R visit(ClassDeclaration n, ArrayList argu) { - ArrayList attr_list = new ArrayList(); - // ArrayList mtd_list = new ArrayList(); + ArrayList attr_list = new ArrayList(); + ArrayList mtd_list = new ArrayList(); 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> { 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> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public String visit(ClassExtendsDeclaration n, ArrayList argu) { + public R visit(ClassExtendsDeclaration n, ArrayList argu) { - ArrayList attr_list = new ArrayList(); - // ArrayList mtd_list = new ArrayList(); + ArrayList attr_list = new ArrayList(); + ArrayList mtd_list = new ArrayList(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -129,7 +131,7 @@ public class SymTableVis extends GJDepthFirst> { 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> { 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> { * f1 -> Identifier() * f2 -> ";" */ - public String visit(VarDeclaration n, ArrayList argu) { + public R visit(VarDeclaration n, ArrayList argu) { PrintFilter.print("Processing declaration", true); @@ -176,7 +178,7 @@ public class SymTableVis extends GJDepthFirst> { 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> { * f11 -> ";" * f12 -> "}" */ - public String visit(MethodDeclaration n, ArrayList argu) { + public R visit(MethodDeclaration n, ArrayList argu) { - ArrayList argu_list = new ArrayList(); - ArrayList var_list = new ArrayList(); + ArrayList argu_list = new ArrayList(); + ArrayList var_list = new ArrayList(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -236,11 +238,15 @@ public class SymTableVis extends GJDepthFirst> { } 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> { * f0 -> Type() * f1 -> Identifier() */ - public String visit(FormalParameter n, ArrayList argu) { + public R visit(FormalParameter n, ArrayList argu) { // PrintFilter.print("Processing parameter", true); String id = n.f1.f0.tokenImage; @@ -266,8 +272,9 @@ public class SymTableVis extends GJDepthFirst> { } 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); -- cgit v1.2.3