diff options
-rw-r--r-- | Typecheck.java | 22 | ||||
-rw-r--r-- | st/AbstractInstance.java | 34 | ||||
-rw-r--r-- | st/SymTableBottomUp.java | 6 | ||||
-rw-r--r-- | st/SymbolTable.java | 7 | ||||
-rw-r--r-- | stTest.java | 25 |
5 files changed, 55 insertions, 39 deletions
diff --git a/Typecheck.java b/Typecheck.java index 4c43709..b4149d9 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -26,18 +26,18 @@ public class Typecheck { root.accept(new SymTableTopDown<Void>(), symt); PrintFilter.print("===================================================", true); - TypeCheckSimp ts = new TypeCheckSimp(); - TypeInstance res = root.accept(ts, symt); + // TypeCheckSimp ts = new TypeCheckSimp(); + // TypeInstance res = root.accept(ts, symt); - // Ugly code not to be inspired from: "my" way of storing - // type info / typecheck property: if some of my internal - // structure is empty, then things don't typecheck for - // me. This is specific to my own implementation. - // if (res != null && res.type_array.size() > 0) - if (res.getType() != TypeEnum.ERROR) - System.out.println("Program type checked successfully"); - else - System.out.println("Type error"); + // // Ugly code not to be inspired from: "my" way of storing + // // type info / typecheck property: if some of my internal + // // structure is empty, then things don't typecheck for + // // me. This is specific to my own implementation. + // // if (res != null && res.type_array.size() > 0) + // if (res.getType() != TypeEnum.ERROR) + // System.out.println("Program type checked successfully"); + // else + // System.out.println("Type error"); } catch (ParseException e) { System.out.println(e.toString()); diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index a6c2ca1..bddaccf 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -1,14 +1,17 @@ package st; +import java.util.ArrayList; + public abstract class AbstractInstance { - protected String name; // the literal name of the declaration - protected TypeEnum type; // the type of the declaration - protected int size; // the size in memory - protected AbstractInstance scope; // the surrounding method or class + protected String name; // the literal name of the declaration + protected TypeEnum type; // the type of the declaration + protected int size; // the size in memory + protected ArrayList<AbstractInstance> scope; // the scope where the instance is valid public AbstractInstance(String name, TypeEnum type) { this.type = type; this.name = name; + this.scope = new ArrayList<>(); } public String toString() { @@ -24,11 +27,18 @@ public abstract class AbstractInstance { return this.name.hashCode(); } - public void setScope(AbstractInstance cls) { - if (this.scope != null) - throw new RuntimeException("setScope: Attempted to set scope twice!"); - - this.scope = cls; + public void setScope(AbstractInstance ins) { + /** + * If the scope is a MethodInstance, add the single method. + * If the scope is a ClassInstance, add the classes' methods, + * and the class itself. + */ + if (ins instanceof MethodInstance) + this.scope.add(ins); + else if (ins instanceof ClassInstance) { + for (MethodInstance mtd : ((ClassInstance) ins).getMethods()) + this.scope.add(mtd); + } } public String getName() { @@ -43,11 +53,7 @@ public abstract class AbstractInstance { return this.size; } - public AbstractInstance getScope() { - /** - * Returns the scope of the method, or - * `null' if unset. - */ + public ArrayList<AbstractInstance> getScope() { return this.scope; } diff --git a/st/SymTableBottomUp.java b/st/SymTableBottomUp.java index f4abb2a..9f60b91 100644 --- a/st/SymTableBottomUp.java +++ b/st/SymTableBottomUp.java @@ -141,6 +141,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> { rtrn = TypeEnum.bool; break; case 2: rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; default: PrintFilter.print("Unsupported case", true); } @@ -193,6 +195,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> { rtrn = TypeEnum.bool; break; case 2: rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; default: PrintFilter.print("Unsupported case", true); } @@ -223,6 +227,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> { rtrn = TypeEnum.bool; break; case 2: rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; default: PrintFilter.print("Unsupported case", true); } diff --git a/st/SymbolTable.java b/st/SymbolTable.java index 9d7ffb4..7475e5e 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -96,22 +96,21 @@ public class SymbolTable { public TypeInstance getType(String id) { AbstractInstance symbol; return ((symbol = this.symt.get(id)) != - null && symbol.getType() != TypeEnum.classname && - symbol.getType() != TypeEnum.method) ? + null && symbol instanceof TypeInstance) ? (TypeInstance) symbol : null; } public MethodInstance getMethod(String id) { AbstractInstance symbol; return ((symbol = this.symt.get(id)) != - null && symbol.getType() == TypeEnum.method) ? + null && symbol instanceof MethodInstance) ? (MethodInstance) symbol : null; } public ClassInstance getClass(String id) { AbstractInstance symbol; return ((symbol = this.symt.get(id)) != - null && symbol.getType() == TypeEnum.classname) ? + null && symbol instanceof ClassInstance) ? (ClassInstance) symbol : null; } diff --git a/stTest.java b/stTest.java index 793719f..b6fd7ae 100644 --- a/stTest.java +++ b/stTest.java @@ -73,18 +73,23 @@ public class stTest { 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"); + ArrayList<AbstractInstance> scope_main = new ArrayList<>(); + scope_main.add(symt.getClass("Factorial")); + ArrayList<AbstractInstance> scope_computefac = new ArrayList<>(); + scope_computefac.add(symt.getClass("Fac")); + ArrayList<AbstractInstance> scope_uselessvar = new ArrayList<>(); + scope_uselessvar.add(symt.getClass("Fac")); + ArrayList<AbstractInstance> scope_num = new ArrayList<>(); + scope_num.add(symt.getMethod("ComputeFac")); + ArrayList<AbstractInstance> scope_numaux = new ArrayList<>(); + scope_numaux.add(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(symt.getMethod("main").getScope().equals(scope_main)); + System.out.println(symt.getMethod("ComputeFac").getScope().equals(scope_computefac)); + System.out.println(symt.getType("useless_var").getScope().equals(scope_uselessvar)); + System.out.println(symt.getType("num").getScope().equals(scope_num)); + System.out.println(symt.getType("num_aux").getScope().equals(scope_numaux)); System.out.println(); } |