From 8131ddc22af5d39114a55349d71bcdc467599187 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Wed, 27 Mar 2024 13:09:08 -0600 Subject: Expand file structure, Vaporize skeleton --- typecheck/library/SymTableVis.java | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 typecheck/library/SymTableVis.java (limited to 'typecheck/library/SymTableVis.java') diff --git a/typecheck/library/SymTableVis.java b/typecheck/library/SymTableVis.java new file mode 100644 index 0000000..615b1c9 --- /dev/null +++ b/typecheck/library/SymTableVis.java @@ -0,0 +1,101 @@ +package typecheck.library; + +import syntaxtree.*; +import visitor.*; +import java.util.*; + +/** + * 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 HashMap 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) { + + n.f1.accept(this, argu); + n.f11.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + + Utilities.print_filter("Processing main", true); + + String id = n.f1.f0.tokenImage; + + TypeInstance type = new TypeInstance(id, TypeEnum.classname); + Utilities.print_filter("Inserting " + id + " => " + type, true); + symt.put(id, type); + + return null; + + } + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public R visit(VarDeclaration n, A argu) { + + Utilities.print_filter("Processing declaration", true); + + String id = n.f1.f0.tokenImage; + TypeInstance type = new TypeInstance("ERROR", TypeEnum.ERROR); + switch (n.f0.f0.which) { + case 0: + type = new TypeInstance("int_array", TypeEnum.int_array); break; + case 1: + type = new TypeInstance("bool", TypeEnum.bool); break; + case 2: + type = new TypeInstance("int", TypeEnum.integer); break; + case 3: + type = new TypeInstance(id, TypeEnum.classname); break; + default: + Utilities.print_filter("Unsupported case", true); + } + + Utilities.print_filter("Inserting " + id + " => " + type, true); + // Safe? + symt.put(id, type); + + return null; + } + + public R visit(ClassDeclaration n, A argu) { + + Utilities.print_filter("Processing class", true); + + String id = n.f1.f0.tokenImage; + + TypeInstance type = new TypeInstance(id, TypeEnum.classname); + Utilities.print_filter("Inserting " + id + " => " + type, true); + // Safe? + symt.put(id, type); + + return null; + + } + + +} -- cgit v1.2.3