diff options
-rw-r--r-- | Typecheck.java | 5 | ||||
-rw-r--r-- | misc/PrintFilter.java | 2 | ||||
-rw-r--r-- | st/ClassInstance.java | 2 | ||||
-rw-r--r-- | st/MethodInstance.java | 19 | ||||
-rw-r--r-- | st/SymTableVis.java | 72 | ||||
-rw-r--r-- | typecheck/library/TypeCheckSimp.java | 2 |
6 files changed, 74 insertions, 28 deletions
diff --git a/Typecheck.java b/Typecheck.java index f08e1e7..3115c31 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -21,9 +21,8 @@ public class Typecheck { // // Build the symbol table. Top-down visitor, inherits from // // GJDepthFirst<R,A>. R=Void, A=Integer. - SymTableVis<Void,Integer> pv = - new SymTableVis<Void,Integer>(); - root.accept(pv, 0); + SymTableVis pv = new SymTableVis(); + root.accept(pv, new ArrayList<TypeInstance>()); HashMap<String, AbstractInstance> symt = pv.symt; PrintFilter.print("===================================================", true); diff --git a/misc/PrintFilter.java b/misc/PrintFilter.java index e3be310..971ef04 100644 --- a/misc/PrintFilter.java +++ b/misc/PrintFilter.java @@ -4,7 +4,7 @@ package misc; public class PrintFilter { public static void print(String message, boolean newline) { - boolean debug = false; + boolean debug = true; if (debug) { System.out.print(message); if (newline) diff --git a/st/ClassInstance.java b/st/ClassInstance.java index c1b07af..44b41c9 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -9,6 +9,8 @@ public class ClassInstance extends AbstractInstance { public ClassInstance(String name) { super(name, TypeEnum.classname); + this.attrs = new ArrayList<>(); + this.mtds = new ArrayList<>(); this.ext = null; } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index c0082af..11b233b 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -3,22 +3,29 @@ package st; import java.util.ArrayList; public class MethodInstance extends AbstractInstance { - private ArrayList<TypeEnum> args; // the list of arguments - private TypeEnum rtrn; // the returned type + private ArrayList<TypeInstance> args; // the list of arguments + private ArrayList<TypeInstance> lvars; // the list of local variables + private TypeEnum rtrn; // the returned type public MethodInstance(String name, TypeEnum rtrn) { super(name, TypeEnum.method); + this.lvars = new ArrayList<>(); this.args = new ArrayList<>(); this.rtrn = rtrn; } public String toString() { - return name + ":" + type + "[" + - this.rtrn + "]"; + return name + ":T[" + type + "]R[" + + this.rtrn + "]P[" + this.args.toString() + + "]V[" + this.lvars.toString() + "]"; } - public void add_argument(TypeEnum arg) { - this.args.add(arg); + public void set_args(ArrayList<TypeInstance> args) { + this.args = args; + } + + public void set_locals(ArrayList<TypeInstance> lvars) { + this.lvars = lvars; } } 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<R,A> extends GJDepthFirst<R,A> { +public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { public HashMap<String,AbstractInstance> symt = new HashMap<>(); + /** * f0 -> "class" * f1 -> Identifier() @@ -33,7 +34,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f16 -> "}" * f17 -> "}" */ - public R visit(MainClass n, A argu) { + public String visit(MainClass n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing main", true); @@ -73,7 +74,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public R visit(ClassDeclaration n, A argu) { + public String visit(ClassDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing class", true); @@ -105,7 +106,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public R visit(ClassExtendsDeclaration n, A argu) { + public String visit(ClassExtendsDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing class", true); String id = n.f1.f0.tokenImage; @@ -134,7 +135,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { * f1 -> Identifier() * f2 -> ";" */ - public R visit(VarDeclaration n, A argu) { + public String visit(VarDeclaration n, ArrayList<TypeInstance> argu) { PrintFilter.print("Processing declaration", true); @@ -156,6 +157,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { 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<R,A> extends GJDepthFirst<R,A> { * f11 -> ";" * f12 -> "}" */ - public R visit(MethodDeclaration n, A argu) { + public String visit(MethodDeclaration n, ArrayList<TypeInstance> argu) { + + ArrayList<TypeInstance> argu_list = new ArrayList<TypeInstance>(); + ArrayList<TypeInstance> var_list = new ArrayList<TypeInstance>(); + + 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<R,A> extends GJDepthFirst<R,A> { } 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<TypeInstance> 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; } diff --git a/typecheck/library/TypeCheckSimp.java b/typecheck/library/TypeCheckSimp.java index 3d262fd..59ee69b 100644 --- a/typecheck/library/TypeCheckSimp.java +++ b/typecheck/library/TypeCheckSimp.java @@ -10,7 +10,7 @@ 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 TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,AbstractInstance>> { +public class TypeCheckSimp extends GJDepthFirst<TypeInstance,HashMap<String,AbstractInstance>> { private int offset; |