diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-23 15:04:43 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-23 15:04:43 -0600 |
commit | 1cbc6acab2c1bd745649a639db023b0f6c87f821 (patch) | |
tree | cccc43f8849c00cf6d90806156e9a8c918a08248 | |
parent | df648047d1899345dd8b2d82f78b480712d4d8d6 (diff) |
Cleanup BoilSimp -> BoilVisitor
-rw-r--r-- | J2V.java | 2 | ||||
-rw-r--r-- | boil/library/BoilVisitor.java | 1112 | ||||
-rw-r--r-- | boil/library/TypeFactory.java | 3 | ||||
-rw-r--r-- | boil/tests/ex32.java | 1 | ||||
-rw-r--r-- | misc/MinimalSimpleFormatter.java | 3 | ||||
-rw-r--r-- | st/AbstractInstance.java | 2 | ||||
-rw-r--r-- | st/SymbolTable.java | 25 |
7 files changed, 1139 insertions, 9 deletions
@@ -24,7 +24,7 @@ public class J2V { root.accept(new SymTableBottomUp<Void>(), symt); root.accept(new SymTableTopDown<Void>(), symt); - BoilSimp vp = new BoilSimp(symt); + BoilVisitor vp = new BoilVisitor(symt); root.accept(vp, null); MinimalLogger.info("==================================================="); diff --git a/boil/library/BoilVisitor.java b/boil/library/BoilVisitor.java new file mode 100644 index 0000000..7de9939 --- /dev/null +++ b/boil/library/BoilVisitor.java @@ -0,0 +1,1112 @@ +package boil.library; + +import syntaxtree.*; +import visitor.*; +import st.*; +import misc.*; +import java.util.*; + +public class BoilVisitor extends GJDepthFirst<String,String> { + + private String vapor; + private TypeFactory tf; // the shared type generator + private int id; + private SymbolTable symt; + + public BoilVisitor(SymbolTable symt) { + this.symt = symt; + this.vapor = ""; + this.tf = new TypeFactory(); + this.id = 0; + } + + public String getVapor() { + return this.vapor; + } + + private void addVapor(String str) { + MinimalLogger.info(String.format("Adding \"%s\"", + str)); + this.vapor += str; + } + + private String getUniqueID() { + return Integer.toString(this.id++); + } + + private void resetIDs() { + MinimalLogger.info(String.format("Clearing tf for new function...")); + this.tf.reset(); + this.id = 0; + } + + // + // Auto class visitors--probably don't need to be overridden. + // + public String visit(NodeList n, String argu) { + String _ret=""; + int _count=0; + for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { + _ret += e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + + public String visit(NodeListOptional n, String argu) { + if ( n.present() ) { + String _ret=""; + int _count=0; + for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { + _ret += e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + else + return ""; + } + + public String visit(NodeOptional n, String argu) { + if ( n.present() ) + return n.node.accept(this,argu); + else + return ""; + } + + public String visit(NodeSequence n, String argu) { + String _ret=null; + int _count=0; + for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { + e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + + public String visit(NodeToken n, String argu) { + return null; + } + + // + // User-generated visitor methods below + // + + /** + * f0 -> MainClass() + * f1 -> ( TypeDeclaration() )* + * f2 -> <EOF> + */ + public String visit(Goal n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * 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 argu) { + String _ret=null; + this.tf.reset(); + String id = n.f1.f0.tokenImage; + this.symt.setActive(TypeEnum.classname, id); + this.symt.setActive(TypeEnum.method, "main"); + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + this.addVapor("func Main()\n"); + n.f1.accept(this, argu); + n.f11.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + this.addVapor(" ret\n\n"); + /////////////////////////////////////////////////////////////// + this.symt.removeActive(TypeEnum.method); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> ClassDeclaration() + * | ClassExtendsDeclaration() + */ + public String visit(TypeDeclaration n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public String visit(ClassDeclaration n, String argu) { + String _ret=null; + String id = n.f1.f0.tokenImage; + this.symt.setActive(TypeEnum.classname, id); + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + this.addVapor(String.format("const functable_%s\n", id)); + for (MethodInstance mtd : this.symt.getClass(id).getMethods()) { + this.addVapor(String.format(" :%s_%s\n", id, mtd)); + } + this.addVapor("\n"); + // n.f3.accept(this, argu); + n.f4.accept(this, argu); + /////////////////////////////////////////////////////////////// + this.symt.removeActive(TypeEnum.classname); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public String visit(ClassExtendsDeclaration n, String argu) { + String _ret=null; + String id = n.f1.f0.tokenImage; + this.symt.setActive(TypeEnum.classname, id); + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + this.addVapor(String.format("const functable_%s\n", id)); + for (MethodInstance mtd : this.symt.getClass(id).getMethods()) { + this.addVapor(String.format(" :%s_%s\n", id, mtd)); + } + this.addVapor("\n"); + // n.f3.accept(this, argu); + // n.f5.accept(this, argu); + n.f6.accept(this, argu); + /////////////////////////////////////////////////////////////// + this.symt.removeActive(TypeEnum.classname); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public String visit(VarDeclaration n, String argu) { + String _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + TypeInstance t = this.symt.getType(id); + if (t.getClassInstance() != null) + this.vapor += String.format(" %s = HeapAllocZ(%d)\n", + this.tf.alias(t.getName()), + t.getSize()); + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * 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 argu) { + String _ret=null; + this.tf.reset(); + String id = n.f2.f0.tokenImage; + this.symt.setActive(TypeEnum.method, id); + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + // n.f1.accept(this, argu); + this.addVapor(String.format("func %s_%s(this ", + this.symt.getActive(TypeEnum.classname), + id)); + String para = n.f4.accept(this, argu); + this.addVapor(String.format("%s)\n", para)); + + // spill out all class attributes, if they're not used, who cares + String selfID = this.getUniqueID(); + this.addVapor(String.format(" %s = this\n", + this.tf.alias(selfID))); + ClassInstance parent = this.symt.getMethod(id).getClassInstance(); + int attr_index = parent.getMethods().size() * 4; + for (TypeInstance attr : parent.getLocals()) { + this.addVapor(String.format(" %s = [%s+%d]\n", + this.tf.alias(attr.getName()), + this.tf.alias(selfID), + attr_index)); + attr_index += 4; + } + + n.f7.accept(this, argu); + n.f8.accept(this, argu); + String ret = n.f10.accept(this, argu); + String retID = this.getUniqueID(); + this.addVapor(String.format(" %s = %s\n ret %s\n\n", + this.tf.alias(retID), + ret, + this.tf.alias(retID))); + /////////////////////////////////////////////////////////////// + this.symt.removeActive(TypeEnum.method); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> FormalParameter() + * f1 -> ( FormalParameterRest() )* + */ + public String visit(FormalParameterList n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f0.accept(this, argu); + _ret += n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public String visit(FormalParameter n, String argu) { + String _ret=""; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + _ret += id + " "; + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> "," + * f1 -> FormalParameter() + */ + public String visit(FormalParameterRest n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> ArrayType() + * | BooleanType() + * | IntegerType() + * | Identifier() + */ + public String visit(Type n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "int" + * f1 -> "[" + * f2 -> "]" + */ + public String visit(ArrayType n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "boolean" + */ + public String visit(BooleanType n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "int" + */ + public String visit(IntegerType n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> Block() + * | AssignmentStatement() + * | ArrayAssignmentStatement() + * | IfStatement() + * | WhileStatement() + * | PrintStatement() + */ + public String visit(Statement n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "{" + * f1 -> ( Statement() )* + * f2 -> "}" + */ + public String visit(Block n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "=" + * f2 -> Expression() + * f3 -> ";" + */ + public String visit(AssignmentStatement n, String argu) { + String _ret=null; + String id = n.f0.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + TypeInstance t = this.symt.getType(id); + String lhs = n.f0.accept(this, argu); + String expr = n.f2.accept(this, argu); + + int attr_index = 0; + if (expr.contains("functable") // is this an allocation... :) + ) { + lhs = String.format("[%s+%d]", lhs, attr_index); + } + + this.addVapor(String.format(" %s = %s\n", + lhs, + expr)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "[" + * f2 -> Expression() + * f3 -> "]" + * f4 -> "=" + * f5 -> Expression() + * f6 -> ";" + */ + public String visit(ArrayAssignmentStatement n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f2.accept(this, argu); + n.f5.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "if" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + * f5 -> "else" + * f6 -> Statement() + */ + public String visit(IfStatement n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String ifID = this.getUniqueID(); + String cond = n.f2.accept(this, argu); + + this.addVapor(String.format(" %s = %s\n", + this.tf.alias(ifID), + cond)); + this.addVapor(String.format(" if0 %s goto :if%s_else\nif%s_body:\n", + this.tf.alias(ifID), + ifID, + ifID)); + + n.f4.accept(this, argu); + + this.addVapor(String.format(" goto :if%s_end\nif%s_else:\n", + ifID, + ifID)); + + n.f6.accept(this, argu); + + this.addVapor(String.format("if%s_end:\n", + ifID)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "while" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + */ + public String visit(WhileStatement n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String whileID = this.getUniqueID(); + String cond = n.f2.accept(this, argu); + + this.addVapor(String.format("while%s_test:\n", + whileID)); + + String condID = this.getUniqueID(); + this.addVapor(String.format(" %s = %s\n", + this.tf.alias(condID), + cond)); + this.addVapor(String.format(" if0 %s goto :while%s_end\nwhile%s_body:\n", + this.tf.alias(condID), + whileID, + whileID)); + + n.f4.accept(this, argu); + + this.addVapor(String.format(" goto :while%s_test\nwhile%s_end:\n", + whileID, + whileID)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "System.out.println" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> ";" + */ + public String visit(PrintStatement n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String printTemp = this.getUniqueID(); + String expr = n.f2.accept(this, argu); + + this.addVapor(String.format(" %s = %s\n", + this.tf.alias(printTemp), + expr)); + + this.addVapor(String.format(" PrintIntS(%s)\n", + this.tf.alias(printTemp))); + + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> AndExpression() + * | CompareExpression() + * | PlusExpression() + * | MinusExpression() + * | TimesExpression() + * | ArrayLookup() + * | ArrayLength() + * | MessageSend() + * | PrimaryExpression() + */ + public String visit(Expression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "&&" + * f2 -> PrimaryExpression() + */ + public String visit(AndExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String oper1 = n.f0.accept(this, argu); + String oper2 = n.f2.accept(this, argu); + _ret += String.format("Eq(%s %s)", + oper1, + oper2); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "<" + * f2 -> PrimaryExpression() + */ + public String visit(CompareExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String oper1 = n.f0.accept(this, argu); + String oper2 = n.f2.accept(this, argu); + _ret += String.format("LtS(%s %s)", + oper1, + oper2); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "+" + * f2 -> PrimaryExpression() + */ + public String visit(PlusExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String oper1 = n.f0.accept(this, argu); + String oper2 = n.f2.accept(this, argu); + _ret += String.format("Add(%s %s)", + oper1, + oper2); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "-" + * f2 -> PrimaryExpression() + */ + public String visit(MinusExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String oper1 = n.f0.accept(this, argu); + String oper2 = n.f2.accept(this, argu); + _ret += String.format("Sub(%s %s)", + oper1, + oper2); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "*" + * f2 -> PrimaryExpression() + */ + public String visit(TimesExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String oper1 = n.f0.accept(this, argu); + String oper2 = n.f2.accept(this, argu); + _ret += String.format("MulS(%s %s)", + oper1, + oper2); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "[" + * f2 -> PrimaryExpression() + * f3 -> "]" + */ + public String visit(ArrayLookup n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> "length" + */ + public String visit(ArrayLength n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( ExpressionList() )? + * f5 -> ")" + */ + public String visit(MessageSend n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String mtd = n.f2.f0.tokenImage; + String rhs; + TypeInstance t; + switch (n.f0.f0.which) { + case 3: + MinimalLogger.info("Message send found an IDENTIFIER"); + rhs = ((Identifier) n.f0.f0.choice).f0.tokenImage; + t = this.symt.getType(rhs); + break; + case 4: + // we'll do everything here and exit the function + MinimalLogger.info("Message send found THIS"); + rhs = n.f0.accept(this, argu); + String callID = this.tf.alias(this.getUniqueID()); + this.addVapor(n.f4.accept(this, argu)); + this.addVapor(String.format(" %s = call %s(this", + callID, + mtd)); + this.addVapor(String.format(" %s)\n", + this.tf.retrieveRecentList(this.symt.getMethod(mtd) + .getArguments() + .size()))); + return callID; + + case 6: + MinimalLogger.info("Message send found ANONYMOUS"); + // expand the entire object out! + rhs = n.f0.accept(this, argu); + t = new TypeInstance(this.getUniqueID(), TypeEnum.ERROR); + t.addClassInstance(this.symt.getClass(rhs.substring(rhs.indexOf('_')+1))); + + this.addVapor(String.format(" %s = HeapAllocZ(%d)\n", + this.tf.alias(t.getName()), + t.getSize())); + + this.addVapor(String.format(" [%s+%d] = %s\n", + this.tf.alias(t.getName()), + 0, + id)); + break; + default: + MinimalLogger.severe("Message send found UNKNOWN"); + rhs = null; + t = null; + } + + String tp1 = this.tf.alias(this.getUniqueID()); + String tp2 = this.tf.alias(this.getUniqueID()); + + this.vapor += String.format(" %s = [%s+%d]\n", + tp1, + this.tf.alias(t.getName()), + 0); + + MinimalLogger.info("Calculating method index to call..."); + int mtdIndex = t.getClassInstance().getMethods() + .indexOf(this.symt.getMethod(mtd)) * 4; + + this.vapor += String.format(" %s = [%s+%d]\n", + tp2, + tp1, + mtdIndex); + + this.addVapor(n.f4.accept(this, argu)); + + _ret += String.format("call %s(%s", + tp2, + this.tf.alias(t.getName())); + + _ret += String.format(" %s)", + this.tf.retrieveRecentList(this.symt.getMethod(mtd) + .getArguments() + .size())); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> Expression() + * f1 -> ( ExpressionRest() )* + */ + public String visit(ExpressionList n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String rhs = n.f0.accept(this, argu); + _ret += String.format(" %s = %s\n", + this.tf.alias(this.getUniqueID()), + rhs); + _ret += n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "," + * f1 -> Expression() + */ + public String visit(ExpressionRest n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String rhs = n.f1.accept(this, argu); + _ret += String.format(" %s = %s\n", + this.tf.alias(this.getUniqueID()), + rhs); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> IntegerLiteral() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() + */ + public String visit(PrimaryExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> <INTEGER_LITERAL> + */ + public String visit(IntegerLiteral n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += n.f0.tokenImage; + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "true" + */ + public String visit(TrueLiteral n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += "1"; + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "false" + */ + public String visit(FalseLiteral n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += "0"; + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> <IDENTIFIER> + */ + public String visit(Identifier n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + _ret += this.tf.alias(n.f0.tokenImage); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "this" + */ + public String visit(ThisExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String thisID = this.tf.alias(this.getUniqueID()); + this.addVapor(String.format(" %s = [this]\n", + thisID)); + this.addVapor(String.format(" %s = [%s+%d]\n", + thisID, + thisID, + 0)); + _ret += thisID; + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "new" + * f1 -> "int" + * f2 -> "[" + * f3 -> Expression() + * f4 -> "]" + */ + public String visit(ArrayAllocationExpression n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f3.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "new" + * f1 -> Identifier() + * f2 -> "(" + * f3 -> ")" + */ + public String visit(AllocationExpression n, String argu) { + String _ret=""; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + String id = n.f1.f0.tokenImage; + _ret += String.format(":functable_%s", + id); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "!" + * f1 -> Expression() + */ + public String visit(NotExpression n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + + /** + * f0 -> "(" + * f1 -> Expression() + * f2 -> ")" + */ + public String visit(BracketExpression n, String argu) { + String _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s", + n.getClass().getSimpleName())); + return _ret; + } + +} diff --git a/boil/library/TypeFactory.java b/boil/library/TypeFactory.java index 93966f0..47b97b2 100644 --- a/boil/library/TypeFactory.java +++ b/boil/library/TypeFactory.java @@ -1,5 +1,6 @@ package boil.library; +import misc.*; import java.util.HashMap; public class TypeFactory { @@ -19,6 +20,8 @@ public class TypeFactory { */ String alias; if ((alias = this.map.get(t)) == null) { + MinimalLogger.info(String.format("Creating new alias for %s...", + t)); alias = String.format("t.%d", this.type_num++); this.map.put(t, alias); } diff --git a/boil/tests/ex32.java b/boil/tests/ex32.java index 4ab80d6..f38ef62 100644 --- a/boil/tests/ex32.java +++ b/boil/tests/ex32.java @@ -7,6 +7,7 @@ class ex32 { } class A { + int z ; public int foo(int a, int b, int c, int d) { return 22 ; } diff --git a/misc/MinimalSimpleFormatter.java b/misc/MinimalSimpleFormatter.java index 3a42398..aa0efdf 100644 --- a/misc/MinimalSimpleFormatter.java +++ b/misc/MinimalSimpleFormatter.java @@ -7,6 +7,7 @@ import java.util.logging.SimpleFormatter; public class MinimalSimpleFormatter extends SimpleFormatter { @Override public String format(LogRecord record) { - return record.getLevel() + ": " + formatMessage(record) + "\n"; + return ""; + //return record.getLevel() + ": " + formatMessage(record) + "\n"; } } diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index 4e4287d..6db9d3c 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -18,7 +18,7 @@ public abstract class AbstractInstance { return this.name; } - @Override public boolean equals(AbstractInstance other) { + public boolean equals(AbstractInstance other) { return this.name == other.getName() && this.type == this.type; } diff --git a/st/SymbolTable.java b/st/SymbolTable.java index e3dd398..77b8a93 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -1,6 +1,7 @@ package st; import java.util.*; +import misc.*; /** * Class which provides methods for interacting with and managing @@ -103,23 +104,35 @@ public class SymbolTable { public TypeInstance getType(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(id)) != - null && symbol instanceof TypeInstance) ? + TypeInstance ret = ((symbol = this.symt.get(id)) != + null && symbol instanceof TypeInstance) ? (TypeInstance) symbol : null; + if (ret == null) + MinimalLogger.severe(String.format("getType returning null for missing alias %s!", + id)); + return ret; } public MethodInstance getMethod(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(id)) != - null && symbol instanceof MethodInstance) ? + MethodInstance ret = ((symbol = this.symt.get(id)) != + null && symbol instanceof MethodInstance) ? (MethodInstance) symbol : null; + if (ret == null) + MinimalLogger.severe(String.format("getMethod returning null for missing alias %s!", + id)); + return ret; } public ClassInstance getClass(String id) { AbstractInstance symbol; - return ((symbol = this.symt.get(id)) != - null && symbol instanceof ClassInstance) ? + ClassInstance ret = ((symbol = this.symt.get(id)) != + null && symbol instanceof ClassInstance) ? (ClassInstance) symbol : null; + if (ret == null) + MinimalLogger.severe(String.format("getClass returning null for missing alias %s!", + id)); + return ret; } public String getActive(TypeEnum type) { |