diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-06 21:10:55 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-06 21:10:55 -0600 |
commit | bb8d3aff71f8f89bed2ab94f382675a2312b1893 (patch) | |
tree | aea71a22ad5a893083feebb661a50a413f4f0049 | |
parent | 8e660afb356c1f6d0b9cd115426cf21129e5d304 (diff) |
Add test file, fix minor issues in SymbolTable
-rw-r--r-- | Playground.java | 114 | ||||
-rw-r--r-- | st/AbstractInstance.java | 3 | ||||
-rw-r--r-- | st/ClassInstance.java | 32 | ||||
-rw-r--r-- | st/MethodInstance.java | 13 | ||||
-rw-r--r-- | st/SymbolTable.java | 16 | ||||
-rw-r--r-- | st/TypeInstance.java | 2 |
6 files changed, 137 insertions, 43 deletions
diff --git a/Playground.java b/Playground.java index aae1af6..8e9cbc0 100644 --- a/Playground.java +++ b/Playground.java @@ -17,28 +17,104 @@ public class Playground { } public static void main(String[] args) { - HashMap<String,AbstractInstance> symt = new HashMap<>(); - symt.put("x", new TypeInstance("x", TypeEnum.integer)); - symt.put("a", new MethodInstance("a", TypeEnum.intarray)); - symt.put("A", new ClassInstance("A", new ClassInstance("B"))); - System.out.println("Our symbol table has lots of symbols: " + HashMapToString(symt)); - System.out.println("It seems class A extends B, but B isn't in the table... " + - ((ClassInstance) symt.get("A")).getExtend()); + /* + 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(); - ssymt.put("x", new TypeInstance("x", TypeEnum.integer)); - ssymt.put("A", new ClassInstance("A")); - System.out.println("A smarter approach. Here's type 'x':" + - ssymt.getType("x")); - System.out.println("Null class 'x':" + - ssymt.getClass("x")); - System.out.println("Null method 'x':" + - ssymt.getMethod("x")); - System.out.println("Null type 'y':" + - ssymt.getType("y")); - System.out.println("Null type 'A':" + - ssymt.getType("A")); + /* 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/st/AbstractInstance.java b/st/AbstractInstance.java index 7acc3e5..a6c2ca1 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -16,7 +16,8 @@ public abstract class AbstractInstance { } public boolean equals(AbstractInstance other) { - return this.name == other.getName(); + return this.name == other.getName() && + this.type == this.type; } public int hashCode() { diff --git a/st/ClassInstance.java b/st/ClassInstance.java index a5f1493..f926fa6 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -13,20 +13,7 @@ public class ClassInstance extends AbstractInstance { this.mtds = new ArrayList<>(); } - public ClassInstance(String name, ClassInstance ext) { - super(name, TypeEnum.classname); - this.ext = ext; - } - - public void addAttribute(TypeInstance attr) { - this.attrs.add(attr); - } - - public void addMethod(MethodInstance mtd) { - this.mtds.add(mtd); - } - - public AbstractInstance getExtend() { + public ClassInstance getExtend() { /** * Returns the parent class, or * `null' if unset. @@ -38,8 +25,23 @@ public class ClassInstance extends AbstractInstance { return this.attrs; } - public ArrayList<MethodInstance> getMethod() { + public ArrayList<MethodInstance> getMethods() { return this.mtds; } + protected void addAttribute(TypeInstance attr) { + this.attrs.add(attr); + } + + protected void addMethod(MethodInstance mtd) { + this.mtds.add(mtd); + } + + protected void setExtend(ClassInstance ext) { + if (this.ext != null) + throw new RuntimeException("setExtend: Attempted to set extended class twice!"); + + this.ext = ext; + } + } diff --git a/st/MethodInstance.java b/st/MethodInstance.java index 25096d2..c7df92f 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -6,7 +6,6 @@ public class MethodInstance extends AbstractInstance { private ArrayList<TypeInstance> args; // the list of arguments private ArrayList<TypeInstance> lvars; // the list of local variables private TypeEnum rtrn; // the returned type - private ClassInstance scope; // the surrounding class public MethodInstance(String name, TypeEnum rtrn) { super(name, TypeEnum.method); @@ -15,12 +14,20 @@ public class MethodInstance extends AbstractInstance { this.rtrn = rtrn; } - public void addArgument(TypeInstance arg) { + public ArrayList<TypeInstance> getArguments() { + return this.args; + } + + public ArrayList<TypeInstance> getLocals() { + return this.lvars; + } + + protected void addArgument(TypeInstance arg) { this.args.add(arg); this.lvars.add(arg); } - public void addLocal(TypeInstance lvar) { + protected void addLocal(TypeInstance lvar) { this.lvars.add(lvar); } diff --git a/st/SymbolTable.java b/st/SymbolTable.java index d6bba37..154c142 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -19,7 +19,7 @@ public class SymbolTable { public String toString() { StringBuilder mapAsString = new StringBuilder("{"); for (String key : this.symt.keySet()) { - mapAsString.append(key + ":" + this.symt.get(key) + ", "); + mapAsString.append(key + ":" + this.symt.get(key).getType() + ", "); } mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}"); return mapAsString.toString(); @@ -37,10 +37,20 @@ public class SymbolTable { /** * Methods intended to be used during the second pass */ - public void setActive(TypeEnum type, String id) { + public void setActive(String id, TypeEnum type) { this.active.put(type, id); } + public void setExtend(String arg) { + String str = this.active.get(TypeEnum.classname); + ClassInstance cls = this.getClass(str); + ClassInstance ext = this.getClass(arg); + + cls.setExtend(ext); + // for (TypeInstance : ext.attrs) //FIXME add the extended classes' stuff + // cls.add + } + public void addAttribute(String arg) { String str = this.active.get(TypeEnum.classname); ClassInstance cls = this.getClass(str); @@ -74,7 +84,7 @@ public class SymbolTable { TypeInstance var = this.getType(lvar); var.setScope(mtd); - mtd.addArgument(var); + mtd.addLocal(var); } diff --git a/st/TypeInstance.java b/st/TypeInstance.java index b4031d9..302f9f0 100644 --- a/st/TypeInstance.java +++ b/st/TypeInstance.java @@ -2,8 +2,6 @@ package st; public class TypeInstance extends AbstractInstance { - private AbstractInstance scope; - public TypeInstance(String name, TypeEnum type) { super(name, type); } |