package boil.library; import syntaxtree.*; import visitor.*; import st.*; import misc.*; import java.util.*; public class BoilSimp extends GJDepthFirst { TypeFactory tf = new TypeFactory(); // // Auto class visitors--probably don't need to be overridden. // public String visit(NodeList n, SymbolTable symt) { String mod = ""; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { mod += e.nextElement().accept(this,symt); _count++; } return mod; } public String visit(NodeListOptional n, SymbolTable symt) { String mod = ""; if ( n.present() ) { int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { mod += e.nextElement().accept(this,symt); _count++; } return mod; } else return ""; } public String visit(NodeOptional n, SymbolTable symt) { if ( n.present() ) return n.node.accept(this,symt); else return ""; } public String visit(NodeSequence n, SymbolTable symt) { String mod = ""; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { mod += e.nextElement().accept(this,symt); _count++; } return mod; } public String visit(NodeToken n, SymbolTable symt) { return ""; } // // User-generated visitor methods below // /** * f0 -> MainClass() * f1 -> ( TypeDeclaration() )* * f2 -> */ public String visit(Goal n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); mod += n.f1.accept(this, symt); mod += n.f2.accept(this, symt); 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, 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"; mod += 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.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" + " Error(\"Mem exhausted\")\n goto :exit\n" + "exit:\n ret\n\n"; symt.removeActive(TypeEnum.method); return mod; } /** * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ public String visit(TypeDeclaration n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> ( VarDeclaration() )* * f4 -> ( MethodDeclaration() )* * 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); n.f1.accept(this, symt); mod += String.format("const functable_%s\n", id); for (MethodInstance mtd : symt.getClass(id).getMethods()) { mod += String.format(" :%s_%s\n", id, mtd); } mod += "\n"; mod += n.f2.accept(this, symt); n.f3.accept(this, symt); mod += n.f4.accept(this, symt); mod += n.f5.accept(this, symt); return mod; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "extends" * f3 -> Identifier() * f4 -> "{" * f5 -> ( VarDeclaration() )* * f6 -> ( MethodDeclaration() )* * 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); 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); return mod; } /** * f0 -> Type() * f1 -> Identifier() * f2 -> ";" */ public String visit(VarDeclaration n, SymbolTable symt) { String mod = ""; 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); 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, SymbolTable symt) { String id = n.f2.f0.tokenImage; symt.setActive(TypeEnum.method, id); this.tf.reset(); String mod = ""; mod += 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); 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); mod += n.f7.accept(this, symt); mod += n.f8.accept(this, symt); mod += n.f9.accept(this, symt); n.f10.accept(this, symt); // FIXME mod += n.f11.accept(this, symt); mod += n.f12.accept(this, symt); mod += " ret\n\n"; symt.removeActive(TypeEnum.method); return mod; } /** * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ public String visit(FormalParameterList n, SymbolTable symt) { String mod = ""; String arg = n.f0.accept(this, symt); if (arg != null) mod += " " + arg; mod += n.f1.accept(this, symt); return mod; } /** * f0 -> Type() * f1 -> Identifier() */ public String visit(FormalParameter n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); mod += n.f1.accept(this, symt); return mod; } /** * f0 -> "," * f1 -> FormalParameter() */ public String visit(FormalParameterRest n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); String arg = n.f1.accept(this, symt); if (arg != null) mod += " " + arg; return mod; } /** * f0 -> ArrayType() * | BooleanType() * | IntegerType() * | Identifier() */ public String visit(Type n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "int" * f1 -> "[" * f2 -> "]" */ 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); return mod; } /** * f0 -> "boolean" */ public String visit(BooleanType n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "int" */ public String visit(IntegerType n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt);; return mod; } /** * f0 -> Block() * | AssignmentStatement() * | ArrayAssignmentStatement() * | IfStatement() * | WhileStatement() * | PrintStatement() */ public String visit(Statement n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "{" * f1 -> ( Statement() )* * f2 -> "}" */ 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); return mod; } /** * f0 -> Identifier() * f1 -> "=" * f2 -> Expression() * f3 -> ";" */ public String visit(AssignmentStatement n, SymbolTable symt) { 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); return mod; } /** * f0 -> Identifier() * f1 -> "[" * f2 -> Expression() * f3 -> "]" * f4 -> "=" * f5 -> Expression() * f6 -> ";" */ 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); return mod; } /** * f0 -> "if" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() * f5 -> "else" * f6 -> Statement() */ 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); return mod; } /** * f0 -> "while" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() */ 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); return mod; } /** * f0 -> "System.out.println" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> ";" */ public String visit(PrintStatement 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 += 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, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "&&" * f2 -> PrimaryExpression() */ 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); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "<" * f2 -> PrimaryExpression() */ 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); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "+" * f2 -> PrimaryExpression() */ public String visit(PlusExpression n, SymbolTable symt) { String mod = "p"; String oper1 = n.f0.accept(this, symt); mod += n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Add(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "-" * f2 -> PrimaryExpression() */ public String visit(MinusExpression n, SymbolTable symt) { String mod = ""; String oper1 = n.f0.accept(this, symt); mod += n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Sub(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "*" * f2 -> PrimaryExpression() */ public String visit(TimesExpression n, SymbolTable symt) { String mod = ""; String oper1 = n.f0.accept(this, symt); mod += n.f1.accept(this, symt); String oper2 = n.f2.accept(this, symt); mod += String.format("Mul(%s %s)", oper1, oper2); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "[" * f2 -> PrimaryExpression() * f3 -> "]" */ 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); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> "length" */ 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); return mod; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> Identifier() * f3 -> "(" * f4 -> ( ExpressionList() )? * f5 -> ")" */ public String visit(MessageSend n, SymbolTable symt) { String mod = ""; String id = n.f0.accept(this, symt); mod += 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); TypeInstance cur = symt.getType(id); 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); mod += 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); String call = String.format("call %s(%s", this.tf.alias(tp2), this.tf.alias(cur)); call += String.format(" %s)", this.tf.retrieveRecentList(symt.getMethod(id2) .getArguments() .size())); TypeInstance tp3 = new TypeInstance("tp3", TypeEnum.ERROR); mod += String.format(" %s = %s\n", this.tf.alias(tp3), call); return mod; } /** * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ public String visit(ExpressionList n, SymbolTable symt) { String mod = ""; String rhs = n.f0.accept(this, symt); TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); mod += String.format(" %s = %s\n", this.tf.alias(tp1), rhs); mod += n.f1.accept(this, symt); return mod; } /** * f0 -> "," * f1 -> Expression() */ public String visit(ExpressionRest n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); String rhs = n.f1.accept(this, symt); TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); mod += String.format(" %s = %s\n", this.tf.alias(tp1), rhs); return mod; } /** * f0 -> IntegerLiteral() * | TrueLiteral() * | FalseLiteral() * | Identifier() * | ThisExpression() * | ArrayAllocationExpression() * | AllocationExpression() * | NotExpression() * | BracketExpression() */ public String visit(PrimaryExpression n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> */ public String visit(IntegerLiteral n, SymbolTable symt) { String mod = ""; mod += n.f0.tokenImage; return mod; } /** * f0 -> "true" */ public String visit(TrueLiteral n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "false" */ public String visit(FalseLiteral n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> */ public String visit(Identifier n, SymbolTable symt) { String mod = ""; mod += n.f0.tokenImage; return mod; } /** * f0 -> "this" */ public String visit(ThisExpression n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); return mod; } /** * f0 -> "new" * f1 -> "int" * f2 -> "[" * f3 -> Expression() * f4 -> "]" */ 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); return mod; } /** * f0 -> "new" * f1 -> Identifier() * f2 -> "(" * f3 -> ")" */ public String visit(AllocationExpression n, SymbolTable symt) { String mod = ""; mod += 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); return mod; } /** * f0 -> "!" * f1 -> Expression() */ public String visit(NotExpression n, SymbolTable symt) { String mod = ""; mod += n.f0.accept(this, symt); mod += n.f1.accept(this, symt); return mod; } /** * f0 -> "(" * f1 -> Expression() * f2 -> ")" */ 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); return mod; } }