package boil.library; import syntaxtree.*; import visitor.*; import st.*; import misc.*; import java.util.*; public class BoilSimp extends GJDepthFirst { private String vapor; // the collected vapor program private TypeFactory tf; // the shared type generator private int id; private SymbolTable symt; public BoilSimp(SymbolTable symt) { this.symt = symt; this.vapor = ""; this.tf = new TypeFactory(); this.id = 0; } public String getVapor() { return this.vapor; } public int getVarIndex(ClassInstance cls, TypeInstance type) { /** * Returns the index of the attribute in the class, or a negative number * if it is not included. */ int attr_index = -1; if (cls != null && ((attr_index = cls.getLocals().indexOf(type)) >= 0)) { attr_index += cls.getMethods().size() * 4; } return attr_index; } // // Auto class visitors--probably don't need to be overridden. // public String visit(NodeList n, String args) { String mod = ""; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { mod += e.nextElement().accept(this,args); _count++; } return mod; } public String visit(NodeListOptional n, String args) { String mod = ""; if ( n.present() ) { int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { mod += e.nextElement().accept(this,args); _count++; } return mod; } else return ""; } public String visit(NodeOptional n, String args) { if ( n.present() ) return n.node.accept(this,args); else return ""; } public String visit(NodeSequence n, String args) { String mod = ""; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,args); _count++; } return mod; } public String visit(NodeToken n, String args) { return ""; } // // User-generated visitor methods below // /** * f0 -> MainClass() * f1 -> ( TypeDeclaration() )* * f2 -> */ public String visit(Goal n, String args) { String mod = ""; /////////////////////////////////////////////////////////////// n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); /////////////////////////////////////////////////////////////// return mod; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> "public" * f4 -> "static" * f5 -> "void" * f6 -> "main" * f7 -> "(" * f8 -> "String" * f9 -> "[" * f10 -> "]" * f11 -> Identifier() * f12 -> ")" * f13 -> "{" * f14 -> ( VarDeclaration() )* * f15 -> ( Statement() )* * f16 -> "}" * f17 -> "}" */ public String visit(MainClass n, String args) { String id = n.f1.f0.tokenImage; /////////////////////////////////////////////////////////////// this.symt.setActive(TypeEnum.classname, id); this.symt.setActive(TypeEnum.method, "main"); this.tf.reset(); String mod = ""; this.vapor += "func Main()\n"; n.f0.accept(this, args); n.f1.accept(this, args); // throw class name away n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); n.f5.accept(this, args); n.f6.accept(this, args); n.f7.accept(this, args); n.f8.accept(this, args); n.f9.accept(this, args); n.f10.accept(this, args); n.f11.accept(this, args); // throw boiler 'args' variable away n.f12.accept(this, args); n.f13.accept(this, args); n.f14.accept(this, args); // FIXME n.f15.accept(this, args); n.f16.accept(this, args); n.f17.accept(this, args); this.vapor += " goto :exit\nerror:\n" + " Error(\"Mem exhausted\")\n goto :exit\n" + "exit:\n ret\n\n"; this.symt.removeActive(TypeEnum.method); /////////////////////////////////////////////////////////////// return mod; } /** * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ public String visit(TypeDeclaration n, String args) { String mod = ""; /////////////////////////////////////////////////////////////// n.f0.accept(this, args); /////////////////////////////////////////////////////////////// return mod; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> ( VarDeclaration() )* * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ public String visit(ClassDeclaration n, String args) { String id = n.f1.f0.tokenImage; String mod = ""; /////////////////////////////////////////////////////////////// this.symt.setActive(TypeEnum.classname, id); n.f0.accept(this, args); n.f1.accept(this, args); this.vapor += String.format("const functable_%s\n", id); for (MethodInstance mtd : this.symt.getClass(id).getMethods()) { this.vapor += String.format(" :%s_%s\n", id, mtd); } this.vapor += "\n"; n.f2.accept(this, args); // n.f3.accept(this, args); n.f4.accept(this, args); n.f5.accept(this, args); return mod; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "extends" * f3 -> Identifier() * f4 -> "{" * f5 -> ( VarDeclaration() )* * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ public String visit(ClassExtendsDeclaration n, String args) { String id = n.f1.f0.tokenImage; this.symt.setActive(TypeEnum.classname, id); String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); n.f5.accept(this, args); n.f6.accept(this, args); n.f7.accept(this, args); return mod; } /** * f0 -> Type() * f1 -> Identifier() * f2 -> ";" */ public String visit(VarDeclaration n, String args) { String mod = ""; String cls = n.f0.accept(this, args); String id = n.f1.f0.tokenImage; TypeInstance t = this.symt.getType(id); if (!cls.isEmpty()) { this.vapor += String.format(" %s = HeapAllocZ(%d)\n", this.tf.alias(t.getName()), t.getSize()); // this.vapor += String.format(" if0 %s goto :error\n", // this.tf.alias(this.symt.getType(id).getName())); } n.f2.accept(this, args); return mod; } /** * f0 -> "public" * f1 -> Type() * f2 -> Identifier() * f3 -> "(" * f4 -> ( FormalParameterList() )? * f5 -> ")" * f6 -> "{" * f7 -> ( VarDeclaration() )* * f8 -> ( Statement() )* * f9 -> "return" * f10 -> Expression() * f11 -> ";" * f12 -> "}" */ public String visit(MethodDeclaration n, String args) { String id = n.f2.f0.tokenImage; this.symt.setActive(TypeEnum.method, id); this.tf.reset(); String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); this.vapor += "func " + this.symt.getActive(TypeEnum.classname) + "_" + id + "(this"; n.f3.accept(this, args); String argu = n.f4.accept(this, args); this.vapor += String.format("%s)\n", argu); n.f5.accept(this, args); n.f6.accept(this, args); // spill out all class attributes, if they're not used, who cares this.vapor += String.format(" %s = this\n", this.tf.alias(Integer.toString(this.id++))); ClassInstance parent = this.symt.getMethod(id).getClassInstance(); int attr_index = parent.getMethods().size() * 4; for (TypeInstance attr : parent.getLocals()) { this.vapor += String.format(" %s = [%s+%d]\n", this.tf.alias(attr.getName()), this.tf.alias(Integer.toString(this.id-1)), attr_index); attr_index += 4; } n.f7.accept(this, args); n.f8.accept(this, args); n.f9.accept(this, args); String ret = n.f10.accept(this, args); n.f11.accept(this, args); n.f12.accept(this, args); // minor cleanup if (n.f10.f0.which == 8 && // primary expression ((PrimaryExpression) n.f10.f0.choice).f0.which == 3) { TypeInstance sym = this.symt.getType(((Identifier) ((PrimaryExpression) n.f10.f0.choice).f0.choice).f0.tokenImage); ClassInstance cls = symt.getClass(symt.getActive(TypeEnum.classname)); if ((attr_index = this.getVarIndex(cls, sym)) >=0) { ret = String.format("[%s+%d]", ret, attr_index); } } this.vapor += String.format(" %s = %s\n ret %s\n\n", this.tf.alias(Integer.toString(this.id++)), ret, this.tf.alias(Integer.toString(this.id-1))); this.symt.removeActive(TypeEnum.method); return mod; } /** * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ public String visit(FormalParameterList n, String args) { String mod = ""; String arg = n.f0.accept(this, args); if (arg != null) mod += " " + arg; mod += n.f1.accept(this, args); return mod; } /** * f0 -> Type() * f1 -> Identifier() */ public String visit(FormalParameter n, String args) { String mod = ""; mod += n.f0.accept(this, args); mod += n.f1.accept(this, args); return mod; } /** * f0 -> "," * f1 -> FormalParameter() */ public String visit(FormalParameterRest n, String args) { String mod = ""; n.f0.accept(this, args); String arg = n.f1.accept(this, args); if (arg != null) mod += " " + arg; return mod; } /** * f0 -> ArrayType() * | BooleanType() * | IntegerType() * | Identifier() */ public String visit(Type n, String args) { String mod = ""; mod += n.f0.accept(this, args); return mod; } /** * f0 -> "int" * f1 -> "[" * f2 -> "]" */ public String visit(ArrayType n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); return mod; } /** * f0 -> "boolean" */ public String visit(BooleanType n, String args) { String mod = ""; n.f0.accept(this, args); return mod; } /** * f0 -> "int" */ public String visit(IntegerType n, String args) { String mod = ""; n.f0.accept(this, args);; return mod; } /** * f0 -> Block() * | AssignmentStatement() * | ArrayAssignmentStatement() * | IfStatement() * | WhileStatement() * | PrintStatement() */ public String visit(Statement n, String args) { String mod = ""; mod += n.f0.accept(this, args); return mod; } /** * f0 -> "{" * f1 -> ( Statement() )* * f2 -> "}" */ public String visit(Block n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); return mod; } /** * f0 -> Identifier() * f1 -> "=" * f2 -> Expression() * f3 -> ";" */ public String visit(AssignmentStatement n, String args) { String mod = ""; String lhs = n.f0.accept(this, args); String id = n.f0.f0.tokenImage; n.f1.accept(this, args); String expr = n.f2.accept(this, args); n.f3.accept(this, args); TypeInstance t = this.symt.getType(id); ClassInstance cls = this.symt.getClass(this.symt.getActive(TypeEnum.classname)); int attr_index = 0; if (expr.contains("functable") // is this an allocation... :) ) { lhs = String.format("[%s+%d]", lhs, attr_index); } this.vapor += String.format(" %s = %s\n", lhs, expr); return mod; } /** * f0 -> Identifier() * f1 -> "[" * f2 -> Expression() * f3 -> "]" * f4 -> "=" * f5 -> Expression() * f6 -> ";" */ public String visit(ArrayAssignmentStatement n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); n.f5.accept(this, args); n.f6.accept(this, args); return mod; } /** * f0 -> "if" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() * f5 -> "else" * f6 -> Statement() */ public String visit(IfStatement n, String args) { String mod = ""; int if_id = this.id++; n.f0.accept(this, args); n.f1.accept(this, args); String cond = n.f2.accept(this, args); vapor += String.format(" %s = %s\n", this.tf.alias(Integer.toString(this.id++)), cond); vapor += String.format(" if0 %s goto :if%d_else\nif%d_body:\n", this.tf.alias(Integer.toString(this.id-1)), if_id, if_id); n.f3.accept(this, args); n.f4.accept(this, args); vapor += String.format(" goto :if%d_end\nif%d_else:\n", if_id, if_id); n.f5.accept(this, args); n.f6.accept(this, args); vapor += String.format("if%d_end:\n", if_id); return mod; } /** * f0 -> "while" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() */ public String visit(WhileStatement n, String args) { String mod = ""; int while_id = this.id++; vapor += String.format("while%d_test:\n", while_id); n.f0.accept(this, args); n.f1.accept(this, args); String expr = n.f2.accept(this, args); vapor += String.format(" %s = %s\n", this.tf.alias(Integer.toString(this.id++)), expr); vapor += String.format(" if0 %s goto :while%d_end\nwhile%d_body:\n", this.tf.alias(Integer.toString(this.id-1)), while_id, while_id); n.f3.accept(this, args); n.f4.accept(this, args); vapor += String.format(" goto :while%d_test\nwhile%d_end:\n", while_id, while_id); return mod; } /** * f0 -> "System.out.println" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> ";" */ public String visit(PrintStatement n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); String expr = n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); vapor += String.format(" %s = %s\n", this.tf.alias(Integer.toString(this.id++)), expr); vapor += String.format(" PrintIntS(%s)\n", this.tf.retrieveRecentList(1)); return mod; } /** * f0 -> AndExpression() * | CompareExpression() * | PlusExpression() * | MinusExpression() * | TimesExpression() * | ArrayLookup() * | ArrayLength() * | MessageSend() * | PrimaryExpression() */ public String visit(Expression n, String args) { String mod = ""; mod += n.f0.accept(this, args); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "&&" * f2 -> PrimaryExpression() */ public String visit(AndExpression n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "<" * f2 -> PrimaryExpression() */ public String visit(CompareExpression n, String args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); String oper2 = n.f2.accept(this, args); mod += String.format("LtS(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "+" * f2 -> PrimaryExpression() */ public String visit(PlusExpression n, String args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); String oper2 = n.f2.accept(this, args); mod += String.format("Add(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "-" * f2 -> PrimaryExpression() */ public String visit(MinusExpression n, String args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); String oper2 = n.f2.accept(this, args); mod += String.format("Sub(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "*" * f2 -> PrimaryExpression() */ public String visit(TimesExpression n, String args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); String oper2 = n.f2.accept(this, args); mod += String.format("MulS(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "[" * f2 -> PrimaryExpression() * f3 -> "]" */ public String visit(ArrayLookup n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); n.f3.accept(this, args); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> "length" */ public String visit(ArrayLength n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> Identifier() * f3 -> "(" * f4 -> ( ExpressionList() )? * f5 -> ")" */ public String visit(MessageSend n, String args) { // if you have to read this, good luck String mod = ""; String id; String id2 = n.f2.f0.tokenImage; TypeInstance cur; ClassInstance cls; switch (n.f0.f0.which) { case 3: id = ((Identifier) n.f0.f0.choice).f0.tokenImage; cur = this.symt.getType(id); break; case 4: id = n.f0.accept(this, args); String call_tmp = this.tf.alias(Integer.toString(this.id++)); this.vapor += n.f4.accept(this, args); vapor += String.format(" %s = call %s(this", call_tmp, id); vapor += String.format(" %s)\n", this.tf.retrieveRecentList(this.symt.getMethod(id2) .getArguments() .size())); return call_tmp; case 6: id = n.f0.accept(this, args); cur = new TypeInstance(Integer.toString(this.id++), TypeEnum.ERROR); cur.addClassInstance(this.symt.getClass(id.substring(id.indexOf('_')+1))); this.vapor += String.format(" %s = HeapAllocZ(%d)\n", this.tf.alias(cur.getName()), cur.getSize()); // this.vapor += String.format(" if0 %s goto :error\n", // this.tf.alias(cur.getName())); this.vapor += String.format(" [%s+%d] = %s\n", this.tf.alias(cur.getName()), 0, id); break; default: System.out.println("ERROR! MessageSend is not implemented for passed case! :("); id = null; cur = null; } n.f1.accept(this, args); String tp1 = Integer.toString(this.id++); // TypeFactory likes to know who it's renting to String tp2 = Integer.toString(this.id++); MinimalLogger.warning("id " + id); MinimalLogger.warning("cur " + cur); int mtdIndex = cur.getClassInstance().getMethods() .indexOf(this.symt.getMethod(id2)) * 4; this.vapor += String.format(" %s = [%s+%d]\n", this.tf.alias(tp1), this.tf.alias(cur.getName()), 0); this.vapor += String.format(" %s = [%s+%d]\n", this.tf.alias(tp2), this.tf.alias(tp1), mtdIndex); n.f3.accept(this, args); this.vapor += n.f4.accept(this, args); n.f5.accept(this, args); mod += String.format("call %s(%s", this.tf.alias(tp2), this.tf.alias(cur.getName())); mod += String.format(" %s)", this.tf.retrieveRecentList(this.symt.getMethod(id2) .getArguments() .size())); return mod; } /** * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ public String visit(ExpressionList n, String args) { String mod = ""; String rhs = n.f0.accept(this, args); mod += String.format(" %s = %s\n", this.tf.alias(Integer.toString(this.id++)), rhs); mod += n.f1.accept(this, args); return mod; } /** * f0 -> "," * f1 -> Expression() */ public String visit(ExpressionRest n, String args) { String mod = ""; n.f0.accept(this, args); String rhs = n.f1.accept(this, args); mod += String.format(" %s = %s\n", this.tf.alias(Integer.toString(this.id++)), rhs); return mod; } /** * f0 -> IntegerLiteral() * | TrueLiteral() * | FalseLiteral() * | Identifier() * | ThisExpression() * | ArrayAllocationExpression() * | AllocationExpression() * | NotExpression() * | BracketExpression() */ public String visit(PrimaryExpression n, String args) { String mod = ""; mod += n.f0.accept(this, args); return mod; } /** * f0 -> */ public String visit(IntegerLiteral n, String args) { String mod = ""; mod += n.f0.tokenImage; return mod; } /** * f0 -> "true" */ public String visit(TrueLiteral n, String args) { String mod = ""; mod += "1"; return mod; } /** * f0 -> "false" */ public String visit(FalseLiteral n, String args) { String mod = ""; mod += "0"; return mod; } /** * f0 -> */ public String visit(Identifier n, String args) { String mod = ""; return this.tf.alias(n.f0.tokenImage); } /** * f0 -> "this" */ public String visit(ThisExpression n, String args) { String mod = ""; vapor += String.format(" %s = [this]\n", this.tf.alias(Integer.toString(this.id++))); vapor += String.format(" %s = [%s+%d]\n", this.tf.alias(Integer.toString(this.id-1)), this.tf.alias(Integer.toString(this.id-1)), 0); mod += this.tf.alias(Integer.toString(this.id-1)); return mod; } /** * f0 -> "new" * f1 -> "int" * f2 -> "[" * f3 -> Expression() * f4 -> "]" */ public String visit(ArrayAllocationExpression n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); return mod; } /** * f0 -> "new" * f1 -> Identifier() * f2 -> "(" * f3 -> ")" */ public String visit(AllocationExpression n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); mod += String.format(":functable_%s", n.f1.f0.tokenImage); // System.out.println(vapor); n.f2.accept(this, args); n.f3.accept(this, args); return mod; } /** * f0 -> "!" * f1 -> Expression() */ public String visit(NotExpression n, String args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); return mod; } /** * f0 -> "(" * f1 -> Expression() * f2 -> ")" */ public String visit(BracketExpression n, String args) { String mod = ""; n.f0.accept(this, args); mod += n.f1.accept(this, args); n.f2.accept(this, args); return mod; } }