From 23b6f865aad683d7f3590c46aa4b74f0a030f6af Mon Sep 17 00:00:00 2001 From: bd-912 Date: Sun, 31 Mar 2024 23:04:35 -0600 Subject: Added argument+parameter storing for methods to SymbolTable --- st/SymTableVis.java | 72 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 17 deletions(-) (limited to 'st/SymTableVis.java') 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 extends GJDepthFirst { +public class SymTableVis extends GJDepthFirst> { public HashMap symt = new HashMap<>(); + /** * f0 -> "class" * f1 -> Identifier() @@ -33,7 +34,7 @@ public class SymTableVis extends GJDepthFirst { * f16 -> "}" * f17 -> "}" */ - public R visit(MainClass n, A argu) { + public String visit(MainClass n, ArrayList argu) { PrintFilter.print("Processing main", true); @@ -73,7 +74,7 @@ public class SymTableVis extends GJDepthFirst { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public R visit(ClassDeclaration n, A argu) { + public String visit(ClassDeclaration n, ArrayList argu) { PrintFilter.print("Processing class", true); @@ -105,7 +106,7 @@ public class SymTableVis extends GJDepthFirst { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public R visit(ClassExtendsDeclaration n, A argu) { + public String visit(ClassExtendsDeclaration n, ArrayList argu) { PrintFilter.print("Processing class", true); String id = n.f1.f0.tokenImage; @@ -134,7 +135,7 @@ public class SymTableVis extends GJDepthFirst { * f1 -> Identifier() * f2 -> ";" */ - public R visit(VarDeclaration n, A argu) { + public String visit(VarDeclaration n, ArrayList argu) { PrintFilter.print("Processing declaration", true); @@ -156,6 +157,7 @@ public class SymTableVis extends GJDepthFirst { 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 extends GJDepthFirst { * f11 -> ";" * f12 -> "}" */ - public R visit(MethodDeclaration n, A argu) { + public String visit(MethodDeclaration n, ArrayList argu) { + + ArrayList argu_list = new ArrayList(); + ArrayList var_list = new ArrayList(); + + 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 extends GJDepthFirst { } 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 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; } -- cgit v1.2.3