package minijava; 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<>(); private void print_filter(String message) { boolean debug = true; if (debug) System.out.println(message); } /** * 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); this.print_filter("Processing main"); String id = n.f1.f0.tokenImage; TypeInstance type = new TypeInstance(id, TypeEnum.classname); this.print_filter("Inserting " + id + " => " + type); symt.put(id, type); return null; } /** * f0 -> Type() * f1 -> Identifier() * f2 -> ";" */ public R visit(VarDeclaration n, A argu) { this.print_filter("Processing declaration"); 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: this.print_filter("Unsupported case"); } this.print_filter("Inserting " + id + " => " + type); // Safe? symt.put(id, type); return null; } public R visit(ClassDeclaration n, A argu) { this.print_filter("Processing class"); String id = n.f1.f0.tokenImage; TypeInstance type = new TypeInstance(id, TypeEnum.classname); this.print_filter("Inserting " + id + " => " + type); // Safe? symt.put(id, type); return null; } }