diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-03-27 22:53:08 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-03-27 22:53:08 -0600 |
commit | b01fe1e8e5541d6c11f905d7fbb949d747f29230 (patch) | |
tree | d348b6b471fa2ebb2cece61daaf672c8b27b4299 /st/SymTableVis.java | |
parent | 8131ddc22af5d39114a55349d71bcdc467599187 (diff) |
SymbolTable to separate library, Class/Method Instances
Diffstat (limited to 'st/SymTableVis.java')
-rw-r--r-- | st/SymTableVis.java | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/st/SymTableVis.java b/st/SymTableVis.java new file mode 100644 index 0000000..280a89c --- /dev/null +++ b/st/SymTableVis.java @@ -0,0 +1,219 @@ +package st; + +import syntaxtree.*; +import visitor.*; +import java.util.*; +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 HashMap<String,AbstractInstance> symt = new HashMap<>(); + + /** + * 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, A argu) { + + PrintFilter.print("Processing main", true); + + String id = n.f1.f0.tokenImage; + + ClassInstance type = new ClassInstance(id); + PrintFilter.print("Inserting " + id + " => " + type, true); + symt.put(id, 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); + n.f13.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + n.f16.accept(this, argu); + n.f17.accept(this, argu); + return null; + + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, A argu) { + + PrintFilter.print("Processing class", true); + + String id = n.f1.f0.tokenImage; + + ClassInstance type = new ClassInstance(id); + PrintFilter.print("Inserting " + id + " => " + type, true); + // Safe? + + symt.put(id, 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); + return null; + + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, A argu) { + PrintFilter.print("Processing class", true); + + String id = n.f1.f0.tokenImage; + String ext = n.f3.f0.tokenImage; + + ClassInstance type = new ClassInstance(id, ext); + PrintFilter.print("Inserting " + id + " => " + type + + "(" + ext + ")", true); + + symt.put(id, 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); + return null; + } + + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public R visit(VarDeclaration n, A argu) { + + PrintFilter.print("Processing declaration", 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; + case 3: + rtrn = TypeEnum.classname; break; + default: + PrintFilter.print("Unsupported case", true); + } + + TypeInstance type = new TypeInstance(id, rtrn); + PrintFilter.print("Inserting " + type, true); + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + return null; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public R visit(MethodDeclaration n, A argu) { + + PrintFilter.print("Processing method", true); + + String id = n.f2.f0.tokenImage; + + 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; + default: + PrintFilter.print("Unsupported case", true); + } + + MethodInstance type = new MethodInstance(id, rtrn); + PrintFilter.print("Inserting " + type, true); + symt.put(id, 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; + } + +} |