diff options
Diffstat (limited to 'boil/library/BoilSimp.java')
-rw-r--r-- | boil/library/BoilSimp.java | 296 |
1 files changed, 151 insertions, 145 deletions
diff --git a/boil/library/BoilSimp.java b/boil/library/BoilSimp.java index fe54052..696b0f3 100644 --- a/boil/library/BoilSimp.java +++ b/boil/library/BoilSimp.java @@ -8,7 +8,12 @@ import java.util.*; public class BoilSimp extends GJDepthFirst<String,SymbolTable> { - TypeFactory tf = new TypeFactory(); + private String vapor; // the collected vapor program + private TypeFactory tf = new TypeFactory(); // the shared type generator + + public String getVapor() { + return this.vapor; + } // // Auto class visitors--probably don't need to be overridden. @@ -48,7 +53,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { String mod = ""; int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { - mod += e.nextElement().accept(this,symt); + e.nextElement().accept(this,symt); _count++; } return mod; @@ -66,10 +71,11 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { * f2 -> <EOF> */ public String visit(Goal n, SymbolTable symt) { + this.vapor = ""; String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -100,28 +106,28 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { symt.setActive(TypeEnum.method, "main"); this.tf.reset(); String mod = ""; - mod += "func Main()\n"; + this.vapor += "func Main()\n"; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); n.f1.accept(this, symt); // throw class name away - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); - mod += n.f6.accept(this, symt); - mod += n.f7.accept(this, symt); - mod += n.f8.accept(this, symt); - mod += n.f9.accept(this, symt); - mod += n.f10.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); n.f11.accept(this, symt); // throw boiler 'args' variable away - mod += n.f12.accept(this, symt); - mod += n.f13.accept(this, symt); - 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 += " goto :exit\nerror:\n" + + n.f12.accept(this, symt); + n.f13.accept(this, symt); + n.f14.accept(this, symt); // FIXME + n.f15.accept(this, symt); + n.f16.accept(this, symt); + n.f17.accept(this, symt); + + this.vapor += " goto :exit\nerror:\n" + " Error(\"Mem exhausted\")\n goto :exit\n" + "exit:\n ret\n\n"; @@ -135,7 +141,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(TypeDeclaration n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -152,17 +158,17 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { symt.setActive(TypeEnum.classname, id); String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); n.f1.accept(this, symt); - mod += String.format("const functable_%s\n", id); + this.vapor += String.format("const functable_%s\n", id); for (MethodInstance mtd : symt.getClass(id).getMethods()) { - mod += String.format(" :%s_%s\n", id, mtd); + this.vapor += String.format(" :%s_%s\n", id, mtd); } - mod += "\n"; - mod += n.f2.accept(this, symt); + this.vapor += "\n"; + n.f2.accept(this, symt); n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); return mod; } @@ -182,14 +188,14 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { 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); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); - mod += n.f6.accept(this, symt); - mod += n.f7.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); return mod; } @@ -204,12 +210,12 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { n.f0.accept(this, symt); String id = n.f1.accept(this, symt); TypeInstance t = symt.getType(id); - mod += String.format(" %s = HeapAllocZ(%d)\n", - this.tf.alias(t), - t.getSize()); // FIXME add proper allocation size - mod += String.format(" if0 %s goto :error\n", - this.tf.alias(symt.getType(id))); - mod += n.f2.accept(this, symt); + this.vapor += String.format(" %s = HeapAllocZ(%d)\n", + this.tf.alias(t), + t.getSize()); // FIXME add proper allocation size + this.vapor += String.format(" if0 %s goto :error\n", + this.tf.alias(symt.getType(id))); + n.f2.accept(this, symt); return mod; } @@ -234,32 +240,32 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { this.tf.reset(); String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); n.f1.accept(this, symt); n.f2.accept(this, symt); - mod += "func " + symt.getActive(TypeEnum.classname) + "_" + id + "(this"; - mod += n.f3.accept(this, symt); + this.vapor += "func " + symt.getActive(TypeEnum.classname) + "_" + id + "(this"; + n.f3.accept(this, symt); String args = n.f4.accept(this, symt); - mod += String.format("%s)\n", args); - mod += n.f5.accept(this, symt); - mod += n.f6.accept(this, symt); + this.vapor += String.format("%s)\n", args); + n.f5.accept(this, symt); + n.f6.accept(this, symt); // spill out all class attributes, if they're not used, who cares for (TypeInstance attr : symt.getMethod(id) .getClassInstance() .getLocals()) { - mod += String.format(" %s = this\n", - this.tf.alias(attr)); + String.format(" %s = this\n", + this.tf.alias(attr)); } - mod += n.f7.accept(this, symt); - mod += n.f8.accept(this, symt); - mod += n.f9.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); n.f10.accept(this, symt); // FIXME - mod += n.f11.accept(this, symt); - mod += n.f12.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); - mod += " ret\n\n"; + this.vapor += " ret\n\n"; symt.removeActive(TypeEnum.method); @@ -296,7 +302,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(FormalParameterRest n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); String arg = n.f1.accept(this, symt); if (arg != null) mod += " " + arg; @@ -322,9 +328,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ArrayType n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -333,7 +339,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(BooleanType n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -342,7 +348,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(IntegerType n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt);; + n.f0.accept(this, symt);; return mod; } @@ -356,7 +362,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(Statement n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -367,9 +373,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(Block n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -383,10 +389,10 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { String mod = ""; String id = n.f0.accept(this, symt); - mod += String.format(" [%s] = ", this.tf.alias(symt.getType(id))); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); + vapor += String.format(" [%s] = ", this.tf.alias(symt.getType(id))); + n.f1.accept(this, symt); + vapor += n.f2.accept(this, symt); + n.f3.accept(this, symt); return mod; } @@ -402,12 +408,12 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { public String visit(ArrayAssignmentStatement n, SymbolTable symt) { String mod = ""; n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); - mod += n.f6.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); return mod; } @@ -422,13 +428,13 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(IfStatement n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); - mod += n.f6.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); return mod; } @@ -441,11 +447,11 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(WhileStatement n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); return mod; } @@ -458,19 +464,19 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(PrintStatement n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); String expr = n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); - // mod += String.format(" %s = %s\n", + // String.format(" %s = %s\n", // this.tf.alias(tp1), // expr); - mod += String.format(" PrintIntS(%s)\n", - this.tf.retrieveRecentList(1)); + vapor += String.format(" PrintIntS(%s)\n", + this.tf.retrieveRecentList(1)); return mod; } @@ -498,9 +504,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(AndExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -511,9 +517,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(CompareExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -525,7 +531,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { public String visit(PlusExpression n, SymbolTable symt) { String mod = "p"; String oper1 = n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Add(%s %s)", @@ -543,7 +549,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { public String visit(MinusExpression n, SymbolTable symt) { String mod = ""; String oper1 = n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Sub(%s %s)", @@ -562,7 +568,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { public String visit(TimesExpression n, SymbolTable symt) { String mod = ""; String oper1 = n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Mul(%s %s)", @@ -581,10 +587,10 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ArrayLookup n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); return mod; } @@ -595,9 +601,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ArrayLength n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } @@ -612,7 +618,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { public String visit(MessageSend n, SymbolTable symt) { String mod = ""; String id = n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f1.accept(this, symt); String id2 = n.f2.accept(this, symt); TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); // TypeFactory likes to know who it's renting to TypeInstance tp2 = new TypeInstance("tp2", TypeEnum.ERROR); @@ -622,19 +628,19 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { int mtdIndex = cur.getClassInstance() .getMethods().indexOf(symt.getMethod(id2)) * 4; - mod += String.format(" %s = [%s+%d]\n", - this.tf.alias(tp1), - this.tf.alias(cur), - 0); + this.vapor += String.format(" %s = [%s+%d]\n", + this.tf.alias(tp1), + this.tf.alias(cur), + 0); - mod += String.format(" %s = [%s+%d]\n", - this.tf.alias(tp2), - this.tf.alias(tp1), - mtdIndex); + this.vapor += String.format(" %s = [%s+%d]\n", + this.tf.alias(tp2), + this.tf.alias(tp1), + mtdIndex); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); - mod += n.f5.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); mod += String.format("call %s(%s", this.tf.alias(tp2), @@ -661,7 +667,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { this.tf.alias(tp1), rhs); - mod += n.f1.accept(this, symt); + n.f1.accept(this, symt); return mod; } @@ -672,7 +678,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ExpressionRest n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); String rhs = n.f1.accept(this, symt); TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); @@ -714,7 +720,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(TrueLiteral n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -723,7 +729,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(FalseLiteral n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -741,7 +747,7 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ThisExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.accept(this, symt); return mod; } @@ -754,11 +760,11 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(ArrayAllocationExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); - mod += n.f3.accept(this, symt); - mod += n.f4.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); return mod; } @@ -770,12 +776,12 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(AllocationExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); + n.f0.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); + n.f2.accept(this, symt); + n.f3.accept(this, symt); return mod; } @@ -785,8 +791,8 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(NotExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); return mod; } @@ -797,9 +803,9 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> { */ public String visit(BracketExpression n, SymbolTable symt) { String mod = ""; - mod += n.f0.accept(this, symt); - mod += n.f1.accept(this, symt); - mod += n.f2.accept(this, symt); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); return mod; } |