summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-06 22:36:22 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-06 22:38:00 -0600
commit8f1eeb71061e4009abec9356c9430075c0a7f7ee (patch)
tree5d4f35717bd90711b19acbb4feab37f469be19ac
parent1eac68a0e5fd00ee563ac43cb6f4dac3a4fb2c33 (diff)
Add SymTableTopDown, changes to SymbolTable to make it possible
Delete SymTableVis
-rw-r--r--Playground.java120
-rw-r--r--Typecheck.java6
-rw-r--r--st/ClassInstance.java4
-rw-r--r--st/SymTableBottomUp.java (renamed from st/SymTableFirst.java)6
-rw-r--r--st/SymTableTopDown.java181
-rw-r--r--st/SymTableVis.java286
-rw-r--r--st/SymbolTable.java35
-rw-r--r--stTest.java140
-rw-r--r--typecheck/tests/FactorialEdit.java20
9 files changed, 367 insertions, 431 deletions
diff --git a/Playground.java b/Playground.java
deleted file mode 100644
index 8e9cbc0..0000000
--- a/Playground.java
+++ /dev/null
@@ -1,120 +0,0 @@
-import java.util.*;
-import st.*;
-
-/**
- * A small method to test the basic functionality
- * of the st Instance library
- */
-public class Playground {
-
- public static String HashMapToString(Map<String, AbstractInstance> map) {
- StringBuilder mapAsString = new StringBuilder("{");
- for (String key : map.keySet()) {
- mapAsString.append(key + "=" + map.get(key) + ", ");
- }
- mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
- return mapAsString.toString();
- }
-
- public static void main(String[] args) {
-
- /*
- class Factorial{
- ....public static void main(String[] a){
- ........System.out.println(new Fac().ComputeFac(10));
- ....}
- }
- class Fac {
- ....int[] useless_var ;
- ....public int ComputeFac(int num){
- ........int num_aux ;
- ........if (num < 1)
- ............num_aux = 1 ;
- ........else
- ............num_aux = num * (this.ComputeFac(num-1)) ;
- ........return num_aux ;
- ....}
- }
- class Fac2 extends Fac {
- }
-
- The below is a manual practice run of the SymbolTable class.
- */
-
- SymbolTable ssymt = new SymbolTable();
-
- /* pass one */
- ssymt.put("Factorial", new ClassInstance("Factorial"));
- ssymt.put("main", new MethodInstance("main", TypeEnum.ERROR));
- ssymt.put("Fac", new ClassInstance("Fac"));
- ssymt.put("useless_var", new TypeInstance("useless_var", TypeEnum.intarray));
- ssymt.put("ComputeFac", new MethodInstance("ComputeFac", TypeEnum.integer));
- ssymt.put("num", new TypeInstance("num", TypeEnum.integer));
- ssymt.put("num_aux", new TypeInstance("num_aux", TypeEnum.integer));
- ssymt.put("Fac2", new ClassInstance("Fac2"));
-
- System.out.println("Post pass one: " + ssymt.toString());
-
- /* pass two */
- ssymt.setActive("Factorial", TypeEnum.classname);
- ssymt.setActive("main", TypeEnum.method);
- ssymt.addMethod("main");
- ssymt.setActive("Fac", TypeEnum.classname);
- ssymt.addAttribute("useless_var");
- ssymt.setActive("ComputeFac", TypeEnum.method);
- ssymt.addMethod("ComputeFac");
- ssymt.addParameter("num");
- ssymt.addLocal("num_aux");
- ssymt.setActive("Fac2", TypeEnum.classname);
- ssymt.setExtend("Fac");
-
- ArrayList<TypeInstance> attrs_in_factorial = new ArrayList<>();
- ArrayList<MethodInstance> mtds_in_factorial = new ArrayList<>();
- mtds_in_factorial.add(ssymt.getMethod("main"));
- ArrayList<TypeInstance> attrs_in_fac = new ArrayList<>();
- attrs_in_fac.add(ssymt.getType("useless_var"));
- ArrayList<MethodInstance> mtds_in_fac = new ArrayList<>();
- mtds_in_fac.add(ssymt.getMethod("ComputeFac"));
- ArrayList<TypeInstance> attrs_in_fac2 = new ArrayList<>(); // FIXME
- ArrayList<MethodInstance> mtds_in_fac2 = new ArrayList<>(); // FIXME
-
- System.out.println("test class children");
- System.out.println(ssymt.getClass("Factorial").getAttributes().equals(attrs_in_factorial));
- System.out.println(ssymt.getClass("Factorial").getMethods().equals(mtds_in_factorial));
- System.out.println(ssymt.getClass("Fac").getAttributes().equals(attrs_in_fac));
- System.out.println(ssymt.getClass("Fac").getMethods().equals(mtds_in_fac));
- System.out.println(ssymt.getClass("Fac2").getAttributes().equals(attrs_in_fac2));
- System.out.println(ssymt.getClass("Fac2").getMethods().equals(mtds_in_fac2));
-
- ArrayList<TypeInstance> args_in_computefac = new ArrayList<>();
- args_in_computefac.add(ssymt.getType("num"));
- ArrayList<TypeInstance> locals_in_computefac = new ArrayList<>(args_in_computefac);
- locals_in_computefac.add(ssymt.getType("num_aux"));
-
- System.out.println("test method children");
- System.out.println(ssymt.getMethod("ComputeFac").getArguments().equals(args_in_computefac));
- System.out.println(ssymt.getMethod("ComputeFac").getLocals().equals(locals_in_computefac));
-
- ClassInstance exts_factorial = null;
- ClassInstance exts_fac = null;
- ClassInstance exts_fac2 = ssymt.getClass("Fac");
-
- System.out.println("test inheritance");
- System.out.println(ssymt.getClass("Factorial").getExtend() == exts_factorial);
- System.out.println(ssymt.getClass("Fac").getExtend() == exts_fac);
- System.out.println(ssymt.getClass("Fac2").getExtend() == exts_fac2);
-
- AbstractInstance scope_main = ssymt.getClass("Factorial");
- AbstractInstance scope_computefac = ssymt.getClass("Fac");
- AbstractInstance scope_uselessvar = ssymt.getClass("Fac");
- AbstractInstance scope_num = ssymt.getMethod("ComputeFac");
- AbstractInstance scope_numaux = ssymt.getMethod("ComputeFac");
-
- System.out.println("test scope");
- System.out.println(ssymt.getMethod("main").getScope() == scope_main);
- System.out.println(ssymt.getMethod("ComputeFac").getScope() == scope_computefac);
- System.out.println(ssymt.getType("useless_var").getScope() == scope_uselessvar);
- System.out.println(ssymt.getType("num").getScope() == scope_num);
- System.out.println(ssymt.getType("num_aux").getScope() == scope_numaux);
- }
-}
diff --git a/Typecheck.java b/Typecheck.java
index 070b71e..cf86389 100644
--- a/Typecheck.java
+++ b/Typecheck.java
@@ -21,9 +21,9 @@ public class Typecheck {
// Build the symbol table. Top-down visitor, inherits from
// GJDepthFirst<R,A>. R=Void, A=Integer.
- SymbolTable st = new SymbolTable();
- root.accept(new SymTableFirst<Void>(), st);
- // root.accept(new SymTableSecond(), st);
+ SymbolTable symt = new SymbolTable();
+ root.accept(new SymTableBottomUp<Void>(), symt);
+ root.accept(new SymTableTopDown<Void>(), symt);
PrintFilter.print("===================================================", true);
// TypeCheckSimp ts = new TypeCheckSimp();
diff --git a/st/ClassInstance.java b/st/ClassInstance.java
index f926fa6..ad7e6b0 100644
--- a/st/ClassInstance.java
+++ b/st/ClassInstance.java
@@ -21,7 +21,7 @@ public class ClassInstance extends AbstractInstance {
return this.ext;
}
- public ArrayList<TypeInstance> getAttributes() {
+ public ArrayList<TypeInstance> getLocals() {
return this.attrs;
}
@@ -29,7 +29,7 @@ public class ClassInstance extends AbstractInstance {
return this.mtds;
}
- protected void addAttribute(TypeInstance attr) {
+ protected void addLocal(TypeInstance attr) {
this.attrs.add(attr);
}
diff --git a/st/SymTableFirst.java b/st/SymTableBottomUp.java
index 13380fd..f4abb2a 100644
--- a/st/SymTableFirst.java
+++ b/st/SymTableBottomUp.java
@@ -6,10 +6,10 @@ import java.util.*;
import misc.*;
/**
- * Performs a preliminary visit through the AST,
- * initializing all Instances and placing them in the table.
+ * Performs a bottom-up preliminary visit through the AST
+ * initializing all Instances and placing them in the passed ST
*/
-public class SymTableFirst<R> extends GJDepthFirst<R,SymbolTable> {
+public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
/**
* f0 -> "class"
diff --git a/st/SymTableTopDown.java b/st/SymTableTopDown.java
new file mode 100644
index 0000000..7258211
--- /dev/null
+++ b/st/SymTableTopDown.java
@@ -0,0 +1,181 @@
+package st;
+
+import syntaxtree.*;
+import visitor.*;
+import java.util.*;
+import misc.*;
+
+/**
+ * Performs a top-down final visit through the AST adding all local
+ * methods, attributes, extension, and scoping information to the ST
+ */
+public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
+
+ /**
+ * 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, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, id);
+ symt.setActive(TypeEnum.method, "main");
+ symt.addMethod("main");
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ n.f6.accept(this, symt);
+ n.f7.accept(this, symt);
+ n.f8.accept(this, symt);
+ n.f9.accept(this, symt);
+ n.f10.accept(this, symt);
+ n.f11.accept(this, symt);
+ n.f12.accept(this, symt);
+ n.f13.accept(this, symt);
+ n.f14.accept(this, symt);
+ n.f15.accept(this, symt);
+ n.f16.accept(this, symt);
+ n.f17.accept(this, symt);
+
+ symt.removeActive(TypeEnum.method);
+ return null;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> ( VarDeclaration() )*
+ * f4 -> ( MethodDeclaration() )*
+ * f5 -> "}"
+ */
+ public R visit(ClassDeclaration n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, id);
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ return null;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "extends"
+ * f3 -> Identifier()
+ * f4 -> "{"
+ * f5 -> ( VarDeclaration() )*
+ * f6 -> ( MethodDeclaration() )*
+ * f7 -> "}"
+ */
+ public R visit(ClassExtendsDeclaration n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.setActive(TypeEnum.classname, id);
+ symt.setExtend(n.f3.f0.tokenImage);
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ n.f6.accept(this, symt);
+ n.f7.accept(this, symt);
+ return null;
+ }
+
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ * f2 -> ";"
+ */
+ public R visit(VarDeclaration n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.addLocal(id);
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ return null;
+ }
+
+ /**
+ * f0 -> "public"
+ * f1 -> Type()
+ * f2 -> Identifier()
+ * f3 -> "("
+ * f4 -> ( FormalParameterList() )?
+ * f5 -> ")"
+ * f6 -> "{"
+ * f7 -> ( VarDeclaration() )*
+ * f8 -> ( Statement() )*
+ * f9 -> "return"
+ * f10 -> Expression()
+ * f11 -> ";"
+ * f12 -> "}"
+ */
+ public R visit(MethodDeclaration n, SymbolTable symt) {
+ String id = n.f2.f0.tokenImage;
+ symt.setActive(TypeEnum.method, id);
+ symt.addMethod(id);
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ n.f2.accept(this, symt);
+ n.f3.accept(this, symt);
+ n.f4.accept(this, symt);
+ n.f5.accept(this, symt);
+ n.f6.accept(this, symt);
+ n.f7.accept(this, symt);
+ n.f8.accept(this, symt);
+ n.f9.accept(this, symt);
+ n.f10.accept(this, symt);
+ n.f11.accept(this, symt);
+ n.f12.accept(this, symt);
+ symt.removeActive(TypeEnum.method);
+ return null;
+ }
+
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ */
+ public R visit(FormalParameter n, SymbolTable symt) {
+ String id = n.f1.f0.tokenImage;
+ symt.addParameter(id);
+
+
+ n.f0.accept(this, symt);
+ n.f1.accept(this, symt);
+ return null;
+ }
+
+}
diff --git a/st/SymTableVis.java b/st/SymTableVis.java
deleted file mode 100644
index 36233b3..0000000
--- a/st/SymTableVis.java
+++ /dev/null
@@ -1,286 +0,0 @@
-package st;
-
-import syntaxtree.*;
-import visitor.*;
-import java.util.*;
-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> extends GJDepthFirst<R,ArrayList<String>> {
-
- public HashMap<String,AbstractInstance> 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, ArrayList<String> argu) {
-
- 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, 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);
- n.f13.accept(this, argu);
- n.f14.accept(this, attr_list);
- n.f15.accept(this, mtd_list);
- n.f16.accept(this, argu);
- n.f17.accept(this, argu);
-
-
- PrintFilter.print("Processing main", true);
-
- String id = n.f1.f0.tokenImage;
-
- ClassInstance type = new ClassInstance(id);
- type.set_attrs(attr_list);
- type.set_mtds(mtd_list);
- PrintFilter.print("Inserting " + id + " => " + type, true);
- symt.put(id, type);
-
- return null;
-
- }
-
- /**
- * f0 -> "class"
- * f1 -> Identifier()
- * f2 -> "{"
- * f3 -> ( VarDeclaration() )*
- * f4 -> ( MethodDeclaration() )*
- * f5 -> "}"
- */
- public R visit(ClassDeclaration n, ArrayList<String> argu) {
-
- 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, mtd_list);
- n.f5.accept(this, argu);
-
-
- PrintFilter.print("Processing class", true);
-
- String id = n.f1.f0.tokenImage;
-
- ClassInstance type = new ClassInstance(id);
- type.set_attrs(attr_list);
- type.set_mtds(mtd_list);
- PrintFilter.print("Inserting " + id + " => " + type, true);
- // Safe?
-
- symt.put(id, type);
-
- return null;
-
- }
-
- /**
- * f0 -> "class"
- * f1 -> Identifier()
- * f2 -> "extends"
- * f3 -> Identifier()
- * f4 -> "{"
- * f5 -> ( VarDeclaration() )*
- * f6 -> ( MethodDeclaration() )*
- * f7 -> "}"
- */
- public R visit(ClassExtendsDeclaration n, ArrayList<String> argu) {
-
- 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, argu);
- n.f4.accept(this, argu);
- n.f5.accept(this, attr_list);
- n.f6.accept(this, mtd_list);
- n.f7.accept(this, argu);
-
-
- PrintFilter.print("Processing extension class", true);
-
- String id = n.f1.f0.tokenImage;
- String ext = n.f3.f0.tokenImage;
-
- ClassInstance type = new ClassInstance(id, ext);
- ClassInstance parent = (ClassInstance) symt.get(ext);
- System.out.println("Parent addrs: " + parent.get_attrs());
- // type.set_attrs(attr_list.addAll(parent.get_attrs()));
- // type.set_mtds(mtd_list.addAll(parent.get_mtds()));
- PrintFilter.print("Inserting " + id + " => " + type, true);
-
- symt.put(id, type);
-
- return null;
- }
-
-
- /**
- * f0 -> Type()
- * f1 -> Identifier()
- * f2 -> ";"
- */
- public R visit(VarDeclaration n, ArrayList<String> argu) {
-
- PrintFilter.print("Processing declaration", 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;
- case 3:
- rtrn = TypeEnum.classname; break;
- default:
- PrintFilter.print("Unsupported case", true);
- }
-
- TypeInstance type = new TypeInstance(id, rtrn);
- PrintFilter.print("Inserting " + type, true);
- symt.put(id, type);
- argu.add(id);
-
- n.f0.accept(this, argu);
- n.f1.accept(this, argu);
- n.f2.accept(this, argu);
- return null;
- }
-
- /**
- * f0 -> "public"
- * f1 -> Type()
- * f2 -> Identifier()
- * f3 -> "("
- * f4 -> ( FormalParameterList() )?
- * f5 -> ")"
- * f6 -> "{"
- * f7 -> ( VarDeclaration() )*
- * f8 -> ( Statement() )*
- * f9 -> "return"
- * f10 -> Expression()
- * f11 -> ";"
- * f12 -> "}"
- */
- public R visit(MethodDeclaration n, ArrayList<String> argu) {
-
- ArrayList<String> argu_list = new ArrayList<String>();
- ArrayList<String> var_list = new ArrayList<String>();
-
- 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);
-
- String id = n.f2.f0.tokenImage;
-
- TypeEnum rtrn = TypeEnum.ERROR;
- switch (n.f1.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);
- }
-
- 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;
- }
-
- /**
- * f0 -> Type()
- * f1 -> Identifier()
- */
- public R visit(FormalParameter n, ArrayList<String> 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);
-
- // add type to parent class
- argu.add(id);
-
- n.f0.accept(this, argu);
- n.f1.accept(this, argu);
- return null;
- }
-
-}
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index 154c142..9d7ffb4 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -37,10 +37,14 @@ public class SymbolTable {
/**
* Methods intended to be used during the second pass
*/
- public void setActive(String id, TypeEnum type) {
+ public void setActive(TypeEnum type, String id) {
this.active.put(type, id);
}
+ public void removeActive(TypeEnum type) {
+ this.active.remove(type);
+ }
+
public void setExtend(String arg) {
String str = this.active.get(TypeEnum.classname);
ClassInstance cls = this.getClass(str);
@@ -51,13 +55,19 @@ public class SymbolTable {
// cls.add
}
- public void addAttribute(String arg) {
- String str = this.active.get(TypeEnum.classname);
- ClassInstance cls = this.getClass(str);
- TypeInstance attr = this.getType(arg);
-
- attr.setScope(cls);
- cls.addAttribute(attr);
+ public void addLocal(String lvar) {
+ TypeInstance var = this.getType(lvar);
+ if (this.active.get(TypeEnum.method) != null) { // we are in a method
+ String str = this.active.get(TypeEnum.method);
+ MethodInstance mtd = this.getMethod(str);
+ mtd.addLocal(var);
+ var.setScope(mtd);
+ } else {
+ String str = this.active.get(TypeEnum.classname);
+ ClassInstance cls = this.getClass(str);
+ cls.addLocal(var);
+ var.setScope(cls);
+ }
}
public void addMethod(String mtd) {
@@ -78,15 +88,6 @@ public class SymbolTable {
mtd.addArgument(para); // also adds to local vars
}
- public void addLocal(String lvar) {
- String str = this.active.get(TypeEnum.method);
- MethodInstance mtd = this.getMethod(str);
- TypeInstance var = this.getType(lvar);
-
- var.setScope(mtd);
- mtd.addLocal(var);
- }
-
/**
* Methods to safely retrieve differentiable types
diff --git a/stTest.java b/stTest.java
new file mode 100644
index 0000000..793719f
--- /dev/null
+++ b/stTest.java
@@ -0,0 +1,140 @@
+import java.io.*;
+import visitor.*;
+import parse.*;
+import syntaxtree.*;
+import java.util.*;
+import st.*;
+import misc.*;
+
+
+/**
+ * A small method to test the basic functionality
+ * of the st Instance library
+ */
+public class stTest {
+
+ public static void FactorialTests(SymbolTable symt) {
+ /*
+ class Factorial{
+ ....public static void main(String[] a){
+ ........System.out.println(new Fac().ComputeFac(10));
+ ....}
+ }
+ class Fac {
+ ....int[] useless_var ;
+ ....public int ComputeFac(int num){
+ ........int num_aux ;
+ ........if (num < 1)
+ ............num_aux = 1 ;
+ ........else
+ ............num_aux = num * (this.ComputeFac(num-1)) ;
+ ........return num_aux ;
+ ....}
+ }
+ class Fac2 extends Fac {
+ }
+
+ The below is a manual practice run of the SymbolTable class.
+ */
+
+ ArrayList<TypeInstance> attrs_in_factorial = new ArrayList<>();
+ ArrayList<MethodInstance> mtds_in_factorial = new ArrayList<>();
+ mtds_in_factorial.add(symt.getMethod("main"));
+ ArrayList<TypeInstance> attrs_in_fac = new ArrayList<>();
+ attrs_in_fac.add(symt.getType("useless_var"));
+ ArrayList<MethodInstance> mtds_in_fac = new ArrayList<>();
+ mtds_in_fac.add(symt.getMethod("ComputeFac"));
+ ArrayList<TypeInstance> attrs_in_fac2 = new ArrayList<>(); // FIXME
+ ArrayList<MethodInstance> mtds_in_fac2 = new ArrayList<>(); // FIXME
+
+ System.out.println("test class children");
+ System.out.println(symt.getClass("Factorial").getLocals().equals(attrs_in_factorial));
+ System.out.println(symt.getClass("Factorial").getMethods().equals(mtds_in_factorial));
+ System.out.println(symt.getClass("Fac").getLocals().equals(attrs_in_fac));
+ System.out.println(symt.getClass("Fac").getMethods().equals(mtds_in_fac));
+ System.out.println(symt.getClass("Fac2").getLocals().equals(attrs_in_fac2));
+ System.out.println(symt.getClass("Fac2").getMethods().equals(mtds_in_fac2));
+
+ ArrayList<TypeInstance> args_in_computefac = new ArrayList<>();
+ args_in_computefac.add(symt.getType("num"));
+ ArrayList<TypeInstance> locals_in_computefac = new ArrayList<>(args_in_computefac);
+ locals_in_computefac.add(symt.getType("num_aux"));
+
+ System.out.println("test method children");
+ System.out.println(symt.getMethod("ComputeFac").getArguments().equals(args_in_computefac));
+ System.out.println(symt.getMethod("ComputeFac").getLocals().equals(locals_in_computefac));
+
+ ClassInstance exts_factorial = null;
+ ClassInstance exts_fac = null;
+ ClassInstance exts_fac2 = symt.getClass("Fac");
+
+ System.out.println("test inheritance");
+ System.out.println(symt.getClass("Factorial").getExtend() == exts_factorial);
+ System.out.println(symt.getClass("Fac").getExtend() == exts_fac);
+ System.out.println(symt.getClass("Fac2").getExtend() == exts_fac2);
+
+ AbstractInstance scope_main = symt.getClass("Factorial");
+ AbstractInstance scope_computefac = symt.getClass("Fac");
+ AbstractInstance scope_uselessvar = symt.getClass("Fac");
+ AbstractInstance scope_num = symt.getMethod("ComputeFac");
+ AbstractInstance scope_numaux = symt.getMethod("ComputeFac");
+
+ System.out.println("test scope");
+ System.out.println(symt.getMethod("main").getScope() == scope_main);
+ System.out.println(symt.getMethod("ComputeFac").getScope() == scope_computefac);
+ System.out.println(symt.getType("useless_var").getScope() == scope_uselessvar);
+ System.out.println(symt.getType("num").getScope() == scope_num);
+ System.out.println(symt.getType("num_aux").getScope() == scope_numaux);
+ System.out.println();
+ }
+
+ public static void main(String[] args) {
+
+ SymbolTable symt = new SymbolTable();
+
+ /* pass one */
+ symt.put("Factorial", new ClassInstance("Factorial"));
+ symt.put("main", new MethodInstance("main", TypeEnum.ERROR));
+ symt.put("Fac", new ClassInstance("Fac"));
+ symt.put("useless_var", new TypeInstance("useless_var", TypeEnum.intarray));
+ symt.put("ComputeFac", new MethodInstance("ComputeFac", TypeEnum.integer));
+ symt.put("num", new TypeInstance("num", TypeEnum.integer));
+ symt.put("num_aux", new TypeInstance("num_aux", TypeEnum.integer));
+ symt.put("Fac2", new ClassInstance("Fac2"));
+
+ /* pass two */
+ symt.setActive(TypeEnum.classname, "Factorial");
+ symt.setActive(TypeEnum.method, "main");
+ symt.addMethod("main");
+ symt.removeActive(TypeEnum.method);
+ symt.setActive(TypeEnum.classname, "Fac");
+ symt.addLocal("useless_var");
+ symt.setActive(TypeEnum.method, "ComputeFac");
+ symt.addMethod("ComputeFac");
+ symt.addParameter("num");
+ symt.addLocal("num_aux");
+ symt.removeActive(TypeEnum.method);
+ symt.setActive(TypeEnum.classname, "Fac2");
+ symt.setExtend("Fac");
+
+ System.out.println("Manual results:");
+ FactorialTests(symt);
+
+ /* using the visitors */
+ // Node root = null;
+ // try {
+ // root = new MiniJavaParser(System.in).Goal();
+ // symt = new SymbolTable();
+ // root.accept(new SymTableBottomUp<Void>(), symt);
+ // root.accept(new SymTableTopDown<Void>(), symt);
+
+ // System.out.println("Visitor results:");
+ // FactorialTests(symt);
+ // }
+ // catch (ParseException e) {
+ // System.out.println(e.toString());
+ // System.exit(1);
+ // }
+
+ }
+}
diff --git a/typecheck/tests/FactorialEdit.java b/typecheck/tests/FactorialEdit.java
new file mode 100644
index 0000000..919ed82
--- /dev/null
+++ b/typecheck/tests/FactorialEdit.java
@@ -0,0 +1,20 @@
+class Factorial{
+ public static void main(String[] a){
+ System.out.println(new Fac().ComputeFac(10));
+ }
+}
+
+class Fac {
+ int[] useless_var ;
+ public int ComputeFac(int num){
+ int num_aux ;
+ if (num < 1)
+ num_aux = 1 ;
+ else
+ num_aux = num * (this.ComputeFac(num-1)) ;
+ return num_aux ;
+ }
+}
+
+class Fac2 extends Fac {
+}