diff options
| author | bd-912 <bdunahu@colostate.edu> | 2024-04-08 00:07:44 -0600 | 
|---|---|---|
| committer | bd-912 <bdunahu@colostate.edu> | 2024-04-08 00:07:44 -0600 | 
| commit | 0de1debf8b72c460d6974de8a8ab9cbbdeecb160 (patch) | |
| tree | 8f12392fbc7f287089cc2bf393e443a3e44ecded | |
| parent | c3d4ff012c568a50e3403caf040de704bc201101 (diff) | |
J2V works for simple examples
| -rw-r--r-- | st/SymbolTable.java | 20 | ||||
| -rw-r--r-- | vapor.jar | bin | 0 -> 110015 bytes | |||
| -rw-r--r-- | vaporize/library/TypeFactory.java | 25 | ||||
| -rw-r--r-- | vaporize/library/VaporizeSimp.java | 61 | ||||
| -rw-r--r-- | vaporize/tests_easy/ex29.java | 5 | ||||
| -rw-r--r-- | vaporize/tests_easy/ex30.java | 11 | ||||
| -rw-r--r-- | vaporize/tests_easy/ex31.java | 12 | 
7 files changed, 113 insertions, 21 deletions
diff --git a/st/SymbolTable.java b/st/SymbolTable.java index 7475e5e..d0ea221 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -37,14 +37,6 @@ public class SymbolTable {      /**       * Methods intended to be used during the second pass       */ -    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); @@ -93,6 +85,14 @@ public class SymbolTable {       * Methods to safely retrieve differentiable types       * in `typecheck', `vaporize' libraries       */ +    public void setActive(TypeEnum type, String id) { +        this.active.put(type, id); +    } + +    public void removeActive(TypeEnum type) { +        this.active.remove(type); +    } +      public TypeInstance getType(String id) {          AbstractInstance symbol;          return ((symbol = this.symt.get(id)) != @@ -114,4 +114,8 @@ public class SymbolTable {              (ClassInstance) symbol : null;      } +    public String getActive(TypeEnum type) { +        return this.active.get(type); +    } +  } diff --git a/vapor.jar b/vapor.jar Binary files differnew file mode 100644 index 0000000..3ffd591 --- /dev/null +++ b/vapor.jar diff --git a/vaporize/library/TypeFactory.java b/vaporize/library/TypeFactory.java new file mode 100644 index 0000000..0166e3c --- /dev/null +++ b/vaporize/library/TypeFactory.java @@ -0,0 +1,25 @@ +package vaporize.library; + +import java.util.HashMap; +import st.TypeInstance; + +public class TypeFactory { + +    private int type_num; +    private HashMap<TypeInstance,String> map; + +    public void reset() { +        this.type_num = 0; +        this.map = new HashMap<>(); +    } + +    public String addNewAlias(TypeInstance t) { +        String alias = String.format("t.%d", type_num); +        this.map.put(t, alias); +        return alias; +    } + +    public String retrieveAlias(TypeInstance t) { +        return this.map.get(t); +    } +} diff --git a/vaporize/library/VaporizeSimp.java b/vaporize/library/VaporizeSimp.java index fa026c0..44121a9 100644 --- a/vaporize/library/VaporizeSimp.java +++ b/vaporize/library/VaporizeSimp.java @@ -8,6 +8,8 @@ import java.util.*;  public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> { +    TypeFactory tf = new TypeFactory(); +      //      // Auto class visitors--probably don't need to be overridden.      // @@ -92,6 +94,11 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       * f17 -> "}"       */      public String visit(MainClass n, SymbolTable symt) { +        String id = n.f1.f0.tokenImage; + +        symt.setActive(TypeEnum.classname, id); +        symt.setActive(TypeEnum.method, "main"); +        this.tf.reset();          String mod = "";          mod += "func Main()\n"; @@ -109,12 +116,16 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {          n.f11.accept(this, symt); // throw boiler 'args' variable away          mod += n.f12.accept(this, symt);          mod += n.f13.accept(this, symt); -        n.f14.accept(this, symt); // FIXME +        mod += n.f14.accept(this, symt); // FIXME          mod += n.f15.accept(this, symt);          mod += n.f16.accept(this, symt);          mod += n.f17.accept(this, symt); -        mod += "  ret\n\n"; +        mod += "  goto :exit\nerror:\n" + +            "  Error(\"Mem exhausted\")\n  goto :exit\n" + +            "exit:\n  ret\n\n"; + +        symt.removeActive(TypeEnum.method);          return mod;      } @@ -137,12 +148,15 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       * f5 -> "}"       */      public String visit(ClassDeclaration n, SymbolTable symt) { +        String id = n.f1.f0.tokenImage; +        symt.setActive(TypeEnum.classname, id);          String mod = ""; +          mod += n.f0.accept(this, symt); -        String id= n.f1.accept(this, symt); -        mod += "const functable_" + id + "\n"; +        n.f1.accept(this, symt); +        mod += String.format("const functable_%s\n", id);          for (MethodInstance mtd : symt.getClass(id).getMethods()) { -            mod += "   :" + id + "_" + mtd + "\n"; +            mod += String.format("   :%s_%s\n", id, mtd);          }          mod += "\n";          mod += n.f2.accept(this, symt); @@ -164,7 +178,10 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       * f7 -> "}"       */      public String visit(ClassExtendsDeclaration n, SymbolTable symt) { +        String id = n.f1.f0.tokenImage; +        symt.setActive(TypeEnum.classname, id);          String mod = ""; +          mod += n.f0.accept(this, symt);          mod += n.f1.accept(this, symt);          mod += n.f2.accept(this, symt); @@ -183,8 +200,13 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       */      public String visit(VarDeclaration n, SymbolTable symt) {          String mod = ""; -        mod += n.f0.accept(this, symt); -        mod += n.f1.accept(this, symt); + +        n.f0.accept(this, symt); +        String id = n.f1.accept(this, symt); +        mod += String.format("  %s = HeapAllocZ(32)\n", +                             this.tf.addNewAlias(symt.getType(id))); // FIXME add proper allocation size +        mod += String.format("  if0 %s goto :error\n", +                             this.tf.retrieveAlias(symt.getType(id)));          mod += n.f2.accept(this, symt);          return mod;      } @@ -205,12 +227,16 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       * f12 -> "}"       */      public String visit(MethodDeclaration n, SymbolTable symt) { +        String id = n.f2.f0.tokenImage; +        symt.setActive(TypeEnum.method, id); +        this.tf.reset();          String mod = ""; +          mod += n.f0.accept(this, symt);          mod += n.f1.accept(this, symt); -        String id = n.f2.accept(this, symt); -        mod += "func " + id; //FIXME After ST properly handles scoping -	mod += n.f3.accept(this, symt); +        n.f2.accept(this, symt); +        mod += "func " + symt.getActive(TypeEnum.classname) + "_" + id + "(this)\n"; +        mod += n.f3.accept(this, symt);          mod += n.f4.accept(this, symt);          mod += n.f5.accept(this, symt);          mod += n.f6.accept(this, symt); @@ -220,6 +246,11 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {          mod += n.f10.accept(this, symt);          mod += n.f11.accept(this, symt);          mod += n.f12.accept(this, symt); + +        mod += "  ret\n\n"; + + +        symt.removeActive(TypeEnum.method);          return mod;      } @@ -334,7 +365,9 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       */      public String visit(AssignmentStatement n, SymbolTable symt) {          String mod = ""; -        mod += n.f0.accept(this, symt); + +        String id = n.f0.accept(this, symt); +        mod += String.format("  [%s] = ", this.tf.retrieveAlias(symt.getType(id)));          mod += n.f1.accept(this, symt);          mod += n.f2.accept(this, symt);          mod += n.f3.accept(this, symt); @@ -352,7 +385,7 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {       */      public String visit(ArrayAssignmentStatement n, SymbolTable symt) {          String mod = ""; -        mod += n.f0.accept(this, symt); +        n.f0.accept(this, symt);          mod += n.f1.accept(this, symt);          mod += n.f2.accept(this, symt);          mod += n.f3.accept(this, symt); @@ -656,7 +689,9 @@ public class VaporizeSimp extends GJDepthFirst<String,SymbolTable> {      public String visit(AllocationExpression n, SymbolTable symt) {          String mod = "";          mod += n.f0.accept(this, symt); -        mod += n.f1.accept(this, symt); +        String cls = n.f1.accept(this, symt); +        mod += String.format(":functable_%s\n", cls); +          mod += n.f2.accept(this, symt);          mod += n.f3.accept(this, symt);          return mod; diff --git a/vaporize/tests_easy/ex29.java b/vaporize/tests_easy/ex29.java new file mode 100644 index 0000000..30ea154 --- /dev/null +++ b/vaporize/tests_easy/ex29.java @@ -0,0 +1,5 @@ +class ex29 { +    public static void main(String[] a) { +        int x ; +    } +} diff --git a/vaporize/tests_easy/ex30.java b/vaporize/tests_easy/ex30.java new file mode 100644 index 0000000..4a5064d --- /dev/null +++ b/vaporize/tests_easy/ex30.java @@ -0,0 +1,11 @@ +class ex30 { +    public static void main(String[] a) { +        int x ; +    } +} + +class A { +    public int foo() { +        return 22 ; +    } +} diff --git a/vaporize/tests_easy/ex31.java b/vaporize/tests_easy/ex31.java new file mode 100644 index 0000000..6527b8b --- /dev/null +++ b/vaporize/tests_easy/ex31.java @@ -0,0 +1,12 @@ +class ex31 { +    public static void main(String[] a) { +        A a ; +        a = new A() ; +    } +} + +class A { +    public int foo() { +        return 22 ; +    } +}  | 
