diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-06 22:36:22 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-06 22:38:00 -0600 |
commit | 8f1eeb71061e4009abec9356c9430075c0a7f7ee (patch) | |
tree | 5d4f35717bd90711b19acbb4feab37f469be19ac /stTest.java | |
parent | 1eac68a0e5fd00ee563ac43cb6f4dac3a4fb2c33 (diff) |
Add SymTableTopDown, changes to SymbolTable to make it possible
Delete SymTableVis
Diffstat (limited to 'stTest.java')
-rw-r--r-- | stTest.java | 140 |
1 files changed, 140 insertions, 0 deletions
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); + // } + + } +} |