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; } }