package heat; import syntaxtree.*; import visitor.*; import st.*; import misc.*; import java.util.*; public class HeatVisitor extends GJDepthFirst> { private SymbolTable symt; private ClassInstance recentClass = null; // the most recent method called public HeatVisitor(SymbolTable symt) { this.symt = symt; } // // Auto class visitors--probably don't need to be overridden. // public TypeBundle visit(NodeList n, ArrayList argu) { TypeBundle _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } public TypeBundle visit(NodeListOptional n, ArrayList argu) { if ( n.present() ) { TypeBundle _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } else return null; } public TypeBundle visit(NodeOptional n, ArrayList argu) { if ( n.present() ) return n.node.accept(this,argu); else return null; } public TypeBundle visit(NodeSequence n, ArrayList argu) { TypeBundle _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } public TypeBundle visit(NodeToken n, ArrayList argu) { return null; } // // User-generated visitor methods below // /** * f0 -> MainClass() * f1 -> ( TypeDeclaration() )* * f2 -> */ public TypeBundle visit(Goal n, ArrayList argu) { TypeBundle _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 with %s", n.getClass().getSimpleName(), _ret)); 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 TypeBundle visit(MainClass n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); this.symt.setActive(TypeEnum.classname, symt.getClass(id)); this.symt.setActive(TypeEnum.method, symt.getMethod(n.f6.tokenImage)); /////////////////////////////////////////////////////////////// n.f14.accept(this, argu); n.f15.accept(this, argu); /////////////////////////////////////////////////////////////// this.symt.removeActive(TypeEnum.method); this.symt.removeActive(TypeEnum.classname); MinimalLogger.info(String.format("<- %s (%s)", n.getClass().getSimpleName(), id)); return _ret; } /** * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ public TypeBundle visit(TypeDeclaration n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> ( VarDeclaration() )* * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ public TypeBundle visit(ClassDeclaration n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); this.symt.setActive(TypeEnum.classname, symt.getClass(id)); /////////////////////////////////////////////////////////////// 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 TypeBundle visit(ClassExtendsDeclaration n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; String id0 = n.f3.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); this.symt.setActive(TypeEnum.classname, symt.getClass(id)); /////////////////////////////////////////////////////////////// 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 TypeBundle visit(VarDeclaration n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// 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 TypeBundle visit(MethodDeclaration n, ArrayList argu) { TypeBundle _ret=null; String id = n.f2.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); this.symt.setActive(TypeEnum.method, symt.getMethod(id)); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f1.accept(this, argu); n.f4.accept(this, argu); n.f7.accept(this, argu); n.f8.accept(this, argu); _ret = n.f10.accept(this, argu); // NOT // if it is a class and the classes are equal // or it is not a class and the types are equal if (!tb.equals(_ret)) throw new TypecheckException(String.format("%s in %s returns the wrong type!", id, symt.getActive(TypeEnum.classname))); /////////////////////////////////////////////////////////////// this.symt.removeActive(TypeEnum.method); MinimalLogger.info(String.format("<- %s (%s)", n.getClass().getSimpleName(), id)); return _ret; } /** * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ public TypeBundle visit(FormalParameterList n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// n.f0.accept(this, argu); n.f1.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> Type() * f1 -> Identifier() */ public TypeBundle visit(FormalParameter n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s (%s)", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// return _ret; } /** * f0 -> "," * f1 -> FormalParameter() */ public TypeBundle visit(FormalParameterRest n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// n.f1.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> ArrayType() * | BooleanType() * | IntegerType() * | Identifier() */ public TypeBundle visit(Type n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// if (n.f0.which == 3) { String id = ((Identifier) n.f0.choice).f0.tokenImage; ClassInstance c = this.symt.getClass(id); if (c == null) throw new TypecheckException(String.format("%s was never declared!", id)); _ret = new TypeBundle(TypeEnum.classname, c); } else _ret = n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "int" * f1 -> "[" * f2 -> "]" */ public TypeBundle visit(ArrayType n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.intarray, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "boolean" */ public TypeBundle visit(BooleanType n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.bool, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "int" */ public TypeBundle visit(IntegerType n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.integer, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> Block() * | AssignmentStatement() * | ArrayAssignmentStatement() * | IfStatement() * | WhileStatement() * | PrintStatement() */ public TypeBundle visit(Statement n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "{" * f1 -> ( Statement() )* * f2 -> "}" */ public TypeBundle visit(Block n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f1.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> Identifier() * f1 -> "=" * f2 -> Expression() * f3 -> ";" */ public TypeBundle visit(AssignmentStatement n, ArrayList argu) { TypeBundle _ret=null; String id = n.f0.f0.tokenImage; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(id); if (t == null) t = this.symt.getTypeAttr(id); if (t == null) throw new TypecheckException(String.format("%s found that %s was never declared!", n.getClass().getSimpleName(), id)); TypeBundle tb = new TypeBundle(t.getType(), t.getClassInstance()); _ret = n.f2.accept(this, argu); MinimalLogger.severe(String.format("t is %s, tb is %s", tb.getInstance(), _ret.getInstance())); if (!tb.equals(_ret)) throw new TypecheckException(String.format("%s tried to assign unequal type to %s!", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> Identifier() * f1 -> "[" * f2 -> Expression() * f3 -> "]" * f4 -> "=" * f5 -> Expression() * f6 -> ";" */ public TypeBundle visit(ArrayAssignmentStatement n, ArrayList argu) { TypeBundle _ret=null; String id = n.f0.f0.tokenImage; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(id); if (t == null) t = this.symt.getTypeAttr(id); if (t == null) throw new TypecheckException(String.format("%s found that %s was never declared!", n.getClass().getSimpleName(), id)); if (t.getType() != TypeEnum.intarray) throw new TypecheckException(String.format("%s called on %s, a non-array!", n.getClass().getSimpleName(), id)); TypeBundle tb = n.f2.accept(this, argu); _ret = n.f5.accept(this, argu); if (!tb.isInt() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried to assign %s to a non-integer!", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "if" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() * f5 -> "else" * f6 -> Statement() */ public TypeBundle visit(IfStatement n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f2.accept(this, argu); if (!_ret.isBool()) throw new TypecheckException(String.format("%s called on non-bool conditional!", n.getClass().getSimpleName())); n.f4.accept(this, argu); n.f6.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "while" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() */ public TypeBundle visit(WhileStatement n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f2.accept(this, argu); if (!_ret.isBool()) throw new TypecheckException(String.format("%s called on non-bool conditional!", n.getClass().getSimpleName())); n.f4.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "System.out.println" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> ";" */ public TypeBundle visit(PrintStatement n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f2.accept(this, argu); if (!_ret.isInt()) throw new TypecheckException(String.format("%s called on a non-integer!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> AndExpression() * | CompareExpression() * | PlusExpression() * | MinusExpression() * | TimesExpression() * | ArrayLookup() * | ArrayLength() * | MessageSend() * | PrimaryExpression() */ public TypeBundle visit(Expression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "&&" * f2 -> PrimaryExpression() */ public TypeBundle visit(AndExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isBool() || !_ret.isBool()) throw new TypecheckException(String.format("%s tried with something other than an Boolean!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "<" * f2 -> PrimaryExpression() */ public TypeBundle visit(CompareExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isInt() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried with something other than an integer!", n.getClass().getSimpleName())); _ret = new TypeBundle(TypeEnum.bool, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "+" * f2 -> PrimaryExpression() */ public TypeBundle visit(PlusExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isInt() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried with something other than an integer!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "-" * f2 -> PrimaryExpression() */ public TypeBundle visit(MinusExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isInt() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried with something other than an integer!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "*" * f2 -> PrimaryExpression() */ public TypeBundle visit(TimesExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isInt() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried with something other than an integer!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "[" * f2 -> PrimaryExpression() * f3 -> "]" */ public TypeBundle visit(ArrayLookup n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeBundle tb = n.f0.accept(this, argu); _ret = n.f2.accept(this, argu); if (!tb.isArray() || !_ret.isInt()) throw new TypecheckException(String.format("%s tried on a non-array, or with a non-int index!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> "length" */ public TypeBundle visit(ArrayLength n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f0.accept(this, argu); if (_ret.isArray()) _ret = new TypeBundle(TypeEnum.integer, null); else throw new TypecheckException(String.format("%s tried on a non-array!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> Identifier() * f3 -> "(" * f4 -> ( ExpressionList() )? * f5 -> ")" */ public TypeBundle visit(MessageSend n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t; n.f0.accept(this, argu); switch (n.f0.f0.which) { case 3: MinimalLogger.info(String.format("Message send found IDENTIFIER")); t = this.symt.getType(((Identifier) n.f0.f0.choice).f0.tokenImage); if (t == null) t = this.symt.getTypeAttr(((Identifier) n.f0.f0.choice).f0.tokenImage); if (t == null) throw new TypecheckException(String.format("%s found that %s was never declared!", n.getClass().getSimpleName(), ((Identifier) n.f0.f0.choice).f0.tokenImage)); // HOW TO CALL? break; case 4: MinimalLogger.info(String.format("Message send found THIS")); t = new TypeInstance("null",null,null,null); t.addClassInstance((ClassInstance) this.symt.getActive(TypeEnum.classname)); break; case 6: MinimalLogger.info(String.format("Message send found ANONYMOUS")); t = new TypeInstance("null",null,null,null); t.addClassInstance((ClassInstance) this.symt.getClass(((AllocationExpression) (n.f0.f0.choice)).f1.f0.tokenImage)); break; case 8: MinimalLogger.info(String.format("Message send found BRACKET")); t = new TypeInstance("null",null,null,null); ClassInstance c = this.recentClass; MinimalLogger.info(String.format("Setting class to the most recent return: %s", c.toString())); t.addClassInstance(c); break; default: MinimalLogger.info(String.format("Message send found UNKNOWN %s", n.f0.f0.choice.toString())); t = null; } if (t == null || t.getClassInstance() == null) throw new TypecheckException(String.format("%s called on a non class variable!", n.getClass().getSimpleName())); MethodInstance m = this.symt.getMethod(n.f2.f0.tokenImage, t.getClassInstance()); if (m == null) throw new TypecheckException(String.format("%s called a method not part of %s!", n.getClass().getSimpleName(), t.getName())); MinimalLogger.severe(String.format("Class instance was: %s", t.getClassInstance())); MinimalLogger.severe(String.format("M was: %s with args %s", m, m.getParentClass())); ArrayList actual = new ArrayList(); ArrayList expected = new ArrayList(); n.f4.accept(this, actual); for (TypeInstance e : m.getArguments()) expected.add(new TypeBundle(e.getType(), e.getClassInstance())); MinimalLogger.info(String.format("Expected arguments: %s\nActual arguments %s", expected.toString(), actual.toString())); if (!expected.equals(actual)) throw new TypecheckException(String.format("Method %s called with incorrect arguments!", m.getName())); MinimalLogger.info(String.format("The most recently called method is now %s", m.toString())); this.recentClass = m.getReturn(); String ret = m.getReturn().getName(); if (ret.equals("intarray")) _ret = new TypeBundle(TypeEnum.intarray, null); else if (ret.equals("bool")) _ret = new TypeBundle(TypeEnum.bool, null); else if (ret.equals("int")) _ret = new TypeBundle(TypeEnum.integer, null); else _ret = new TypeBundle(TypeEnum.classname, m.getReturn()); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ public TypeBundle visit(ExpressionList n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f0.accept(this, argu); argu.add(_ret); n.f1.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "," * f1 -> Expression() */ public TypeBundle visit(ExpressionRest n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f1.accept(this, argu); argu.add(_ret); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> IntegerLiteral() * | TrueLiteral() * | FalseLiteral() * | Identifier() * | ThisExpression() * | ArrayAllocationExpression() * | AllocationExpression() * | NotExpression() * | BracketExpression() */ public TypeBundle visit(PrimaryExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> */ public TypeBundle visit(IntegerLiteral n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.integer, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "true" */ public TypeBundle visit(TrueLiteral n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.bool, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "false" */ public TypeBundle visit(FalseLiteral n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.bool, null); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> */ public TypeBundle visit(Identifier n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(n.f0.tokenImage); if (t == null) t = this.symt.getTypeAttr(n.f0.tokenImage); if (t == null) throw new TypecheckException(String.format("%s was never declared!", n.f0.tokenImage)); _ret = new TypeBundle(t.getType(), t.getClassInstance()); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "this" */ public TypeBundle visit(ThisExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = new TypeBundle(TypeEnum.classname, (ClassInstance) this.symt.getActive(TypeEnum.classname)); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "new" * f1 -> "int" * f2 -> "[" * f3 -> Expression() * f4 -> "]" */ public TypeBundle visit(ArrayAllocationExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f3.accept(this, argu); if (_ret.isInt()) _ret = new TypeBundle(TypeEnum.intarray, null); else throw new TypecheckException(String.format("%s tried with a non-integer!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "new" * f1 -> Identifier() * f2 -> "(" * f3 -> ")" */ public TypeBundle visit(AllocationExpression n, ArrayList argu) { TypeBundle _ret=null; String id = n.f1.f0.tokenImage; MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// this.recentClass = this.symt.getClass(id); _ret = new TypeBundle(TypeEnum.classname, this.symt.getClass(id)); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "!" * f1 -> Expression() */ public TypeBundle visit(NotExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f1.accept(this, argu); if (!_ret.isBool()) throw new TypecheckException(String.format("%s tried with a non-bool!", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } /** * f0 -> "(" * f1 -> Expression() * f2 -> ")" */ public TypeBundle visit(BracketExpression n, ArrayList argu) { TypeBundle _ret=null; MinimalLogger.info(String.format("-> %s", n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// _ret = n.f1.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", n.getClass().getSimpleName(), _ret)); return _ret; } }