diff options
-rw-r--r-- | J2V.java | 15 | ||||
-rw-r--r-- | Typecheck.java | 4 | ||||
-rw-r--r-- | st/ClassInstance.java | 12 | ||||
-rw-r--r-- | st/MethodInstance.java | 8 | ||||
-rw-r--r-- | st/SymTableVis.java | 53 | ||||
-rw-r--r-- | vaporize/tests_easy/ex1.java | 11 | ||||
-rw-r--r-- | vaporize/tests_easy/ex1.vapor | 5 | ||||
-rw-r--r-- | vaporize/tests_easy/ex2.java | 7 | ||||
-rw-r--r-- | vaporize/tests_easy/ex2.vapor | 11 |
9 files changed, 88 insertions, 38 deletions
@@ -1,11 +1,13 @@ import java.io.*; import visitor.*; +import parse.*; import syntaxtree.*; import java.util.*; -// Files are stored in the vaporize directory/package. -import vaporize.*; +import st.*; +import misc.*; +import vaporize.library.*; -public class Typecheck { +public class J2V { public static void main(String[] args) { Node root = null; try { @@ -15,6 +17,13 @@ public class Typecheck { // GJDepthFirst<R,A>. R=Void, A=String. PPrinter<Void,String> pp = new PPrinter<Void,String>(); root.accept(pp, ""); + + // Build the symbol table. Top-down visitor, inherits from + // GJDepthFirst<R,A>. R=Void, A=Integer. + SymTableVis<Void> pv = new SymTableVis<Void>(); + root.accept(pv, new ArrayList<String>()); + HashMap<String, AbstractInstance> symt = pv.symt; + } catch (ParseException e) { System.out.println(e.toString()); diff --git a/Typecheck.java b/Typecheck.java index 3115c31..23f2efc 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -19,8 +19,8 @@ public class Typecheck { root.accept(pp, ""); PrintFilter.print("===================================================", true); - // // Build the symbol table. Top-down visitor, inherits from - // // GJDepthFirst<R,A>. R=Void, A=Integer. + // Build the symbol table. Top-down visitor, inherits from + // GJDepthFirst<R,A>. R=Void, A=Integer. SymTableVis pv = new SymTableVis(); root.accept(pv, new ArrayList<TypeInstance>()); HashMap<String, AbstractInstance> symt = pv.symt; diff --git a/st/ClassInstance.java b/st/ClassInstance.java index 8b49236..a444f26 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -3,9 +3,9 @@ package st; import java.util.ArrayList; public class ClassInstance extends AbstractInstance { - private ArrayList<TypeInstance> attrs; // the list of class-fields - private ArrayList<MethodInstance> mtds; // the list of methods - private String ext; // the name of the extended class (null if none) + private ArrayList<String> attrs; // the list of class-fields + private ArrayList<String> mtds; // the list of methods + private String ext; // the name of the extended class (null if none) public ClassInstance(String name) { super(name, TypeEnum.classname); @@ -22,15 +22,15 @@ public class ClassInstance extends AbstractInstance { public String toString() { return name + ":T[" + type + "]E[" + this.ext + "]A[" + this.attrs.toString() + - "]";//M[" + this.mtds.toString() + "]"; + "]M[" + this.mtds.toString() + "]"; } - public void set_attrs(ArrayList<TypeInstance> attrs) { + public void set_attrs(ArrayList<String> attrs) { this.attrs = attrs; } - public void set_mtds(ArrayList<MethodInstance> mtds) { + public void set_mtds(ArrayList<String> mtds) { this.mtds = mtds; } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index 11b233b..8b4ed57 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -3,8 +3,8 @@ package st; import java.util.ArrayList; public class MethodInstance extends AbstractInstance { - private ArrayList<TypeInstance> args; // the list of arguments - private ArrayList<TypeInstance> lvars; // the list of local variables + private ArrayList<String> args; // the list of arguments + private ArrayList<String> lvars; // the list of local variables private TypeEnum rtrn; // the returned type public MethodInstance(String name, TypeEnum rtrn) { @@ -20,11 +20,11 @@ public class MethodInstance extends AbstractInstance { "]V[" + this.lvars.toString() + "]"; } - public void set_args(ArrayList<TypeInstance> args) { + public void set_args(ArrayList<String> args) { this.args = args; } - public void set_locals(ArrayList<TypeInstance> lvars) { + public void set_locals(ArrayList<String> lvars) { this.lvars = lvars; } diff --git a/st/SymTableVis.java b/st/SymTableVis.java index 2731a26..399ea09 100644 --- a/st/SymTableVis.java +++ b/st/SymTableVis.java @@ -9,7 +9,7 @@ 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 extends GJDepthFirst<String,ArrayList<TypeInstance>> { +public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> { public HashMap<String,AbstractInstance> symt = new HashMap<>(); @@ -34,9 +34,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f16 -> "}" * f17 -> "}" */ - public String visit(MainClass n, ArrayList<TypeInstance> argu) { + public R visit(MainClass n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -53,7 +54,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { n.f12.accept(this, argu); n.f13.accept(this, argu); n.f14.accept(this, attr_list); - n.f15.accept(this, argu); + n.f15.accept(this, mtd_list); n.f16.accept(this, argu); n.f17.accept(this, argu); @@ -63,7 +64,8 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { String id = n.f1.f0.tokenImage; ClassInstance type = new ClassInstance(id); - type.set_attrs(attr_list); + type.set_attrs(attr_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); symt.put(id, type); @@ -79,16 +81,16 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public String visit(ClassDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(ClassDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); - // ArrayList<TypeInstance> mtd_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, attr_list); - n.f4.accept(this, argu); + n.f4.accept(this, mtd_list); n.f5.accept(this, argu); @@ -98,7 +100,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { ClassInstance type = new ClassInstance(id); type.set_attrs(attr_list); - // type.set_mtds(mtd_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); // Safe? @@ -118,10 +120,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public String visit(ClassExtendsDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(ClassExtendsDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> attr_list = new ArrayList<TypeInstance>(); - // ArrayList<TypeInstance> mtd_list = new ArrayList<TypeInstance>(); + ArrayList<String> attr_list = new ArrayList<String>(); + ArrayList<String> mtd_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -129,7 +131,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, attr_list); - n.f6.accept(this, argu); + n.f6.accept(this, mtd_list); n.f7.accept(this, argu); @@ -140,7 +142,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { ClassInstance type = new ClassInstance(id, ext); type.set_attrs(attr_list); - // type.set_mtds(mtd_list); + type.set_mtds(mtd_list); PrintFilter.print("Inserting " + id + " => " + type, true); symt.put(id, type); @@ -154,7 +156,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f1 -> Identifier() * f2 -> ";" */ - public String visit(VarDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(VarDeclaration n, ArrayList<String> argu) { PrintFilter.print("Processing declaration", true); @@ -176,7 +178,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { TypeInstance type = new TypeInstance(id, rtrn); PrintFilter.print("Inserting " + type, true); symt.put(id, type); - argu.add(type); + argu.add(id); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -199,10 +201,10 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f11 -> ";" * f12 -> "}" */ - public String visit(MethodDeclaration n, ArrayList<TypeInstance> argu) { + public R visit(MethodDeclaration n, ArrayList<String> argu) { - ArrayList<TypeInstance> argu_list = new ArrayList<TypeInstance>(); - ArrayList<TypeInstance> var_list = new ArrayList<TypeInstance>(); + ArrayList<String> argu_list = new ArrayList<String>(); + ArrayList<String> var_list = new ArrayList<String>(); n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -236,11 +238,15 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { } MethodInstance type = new MethodInstance(id, rtrn); + // add children to current class type.set_args(argu_list); type.set_locals(var_list); PrintFilter.print("Inserting " + type, true); symt.put(id, type); + // add method to parent class + argu.add(id); + return null; } @@ -248,7 +254,7 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { * f0 -> Type() * f1 -> Identifier() */ - public String visit(FormalParameter n, ArrayList<TypeInstance> argu) { + public R visit(FormalParameter n, ArrayList<String> argu) { // PrintFilter.print("Processing parameter", true); String id = n.f1.f0.tokenImage; @@ -266,8 +272,9 @@ public class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> { } TypeInstance type = new TypeInstance(id, rtrn); - // PrintFilter.print("Adding Argument " + type, true); - argu.add(type); + + // add type to parent class + argu.add(id); n.f0.accept(this, argu); n.f1.accept(this, argu); diff --git a/vaporize/tests_easy/ex1.java b/vaporize/tests_easy/ex1.java new file mode 100644 index 0000000..22f0aa0 --- /dev/null +++ b/vaporize/tests_easy/ex1.java @@ -0,0 +1,11 @@ +class ex1{ + public static void main(String[] a){ + System.out.println(1); + } +} + +class A { + public int foo() { + return 1 ; + } +} diff --git a/vaporize/tests_easy/ex1.vapor b/vaporize/tests_easy/ex1.vapor new file mode 100644 index 0000000..bcfb38c --- /dev/null +++ b/vaporize/tests_easy/ex1.vapor @@ -0,0 +1,5 @@ +const functable_A + :A_foo + +func A_foo(this) + ret
\ No newline at end of file diff --git a/vaporize/tests_easy/ex2.java b/vaporize/tests_easy/ex2.java new file mode 100644 index 0000000..986ca7d --- /dev/null +++ b/vaporize/tests_easy/ex2.java @@ -0,0 +1,7 @@ +class A { + void foo() { } +} + +class B { + void bar() { } +} diff --git a/vaporize/tests_easy/ex2.vapor b/vaporize/tests_easy/ex2.vapor new file mode 100644 index 0000000..25811b3 --- /dev/null +++ b/vaporize/tests_easy/ex2.vapor @@ -0,0 +1,11 @@ +const functable_A + :A_foo + +const functable_B + :B_bar + +func A_foo (this) + ret + +func B_bar(this) + ret
\ No newline at end of file |