From 0de1debf8b72c460d6974de8a8ab9cbbdeecb160 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Mon, 8 Apr 2024 00:07:44 -0600 Subject: J2V works for simple examples --- vaporize/library/VaporizeSimp.java | 61 ++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'vaporize/library/VaporizeSimp.java') 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 { + TypeFactory tf = new TypeFactory(); + // // Auto class visitors--probably don't need to be overridden. // @@ -92,6 +94,11 @@ public class VaporizeSimp extends GJDepthFirst { * 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 { 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 { * 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 { * 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 { */ 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 { * 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 { 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 { */ 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 { */ 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 { 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; -- cgit v1.2.3