#include "ast.h" #include "StringTab.h" // for setw(n) #include // how much to indent by to print children of a node static const int INDENT = 2; // mostly polymorphic pretty-print functions // utility for indenting the beginning of a line by w spaces ostream &indent(ostream &o, int w) { return o << setw(w) << ""; } void TreeNode::dumpLineNum(ostream &o) { o << "(" << getLineNum() << "): "; } // n is how much to indent void Program::dump(ostream &o, int n) { indent(o,n) << "*Program"; dumpLineNum(o); o << endl; mainClass->dump(o, n+INDENT); // for some reason w/o the global namespace (::) type system can't figure out // to use template dump; wants it to be Program::dump. ::dump(o, classList, n+INDENT); } void Class::dump(ostream &o, int n) { indent(o,n) << "*Class"; dumpLineNum(o); o << name << endl; ::dump(o, memberList, n+INDENT); indent(o,n) << "*End Class " << name << endl; } void Field::dump(ostream &o, int n) { indent(o,n) << "*Field"; dumpLineNum(o); o << type << " " << name << endl; } void MethodAbstract::defaultDump(ostream &o, int n) { ::dump(o, formalList, n+INDENT); ::dump(o, localList, n + INDENT); ::dump(o, stmtList, n + INDENT); } void Method::dump(ostream &o, int n) { indent(o,n) << "*Method"; dumpLineNum(o); o << returnType << " "; o << name; o << endl; defaultDump(o, n); // dump inherited fields indent(o,n+INDENT-1) << "return from " << name << ":" << endl; returnExpr->dump(o,n+INDENT); indent(o,n) << "*End Method " << name << endl; } void MainMethod::dump(ostream &o, int n) { indent(o,n) << "*MainMethod"; dumpLineNum(o); o << endl; defaultDump(o, n); // dump inherited fields indent(o,n) << "*End MainMethod " << endl; } void Formal::dump(ostream &o, int n) { indent(o,n) << "*Formal"; dumpLineNum(o); o << type << " " << name << endl; } void Local::dump(ostream &o, int n) { indent(o,n) << "*Local"; dumpLineNum(o); o << type << " " << name << endl; } void Expr::dumpType(ostream &o) { o << "is type "; if (type == NULL) { o << "no_type"; } else { o << type; } } void ExprStmt::dump(ostream &o, int n) { indent(o, n) << "*ExprStmt"; dumpLineNum(o); o << endl; expr->dump(o, n+INDENT); } void Block::dump(ostream &o, int n) { indent(o, n) << "*Block"; dumpLineNum(o); o << endl; ::dump(o, stmtList, n+INDENT); } void If::dump(ostream &o, int n) { indent(o, n) << "*If"; dumpLineNum(o); o << endl; indent(o, n+INDENT-1) << "condition: " << endl; cond->dump(o, n+INDENT); indent(o, n+INDENT-1) << "then-part: " << endl; thenPart->dump(o, n+INDENT); if (elsePart) { indent(o, n+INDENT-1) << "else-part: " << endl; elsePart->dump(o, n+INDENT); } } void While::dump(ostream &o, int n) { indent(o, n) << "*While"; dumpLineNum(o); o << endl; indent(o, n+INDENT-1) << "condition: " << endl; cond->dump(o, n+INDENT); indent(o, n+INDENT-1) << "body: " << endl; body->dump(o, n+INDENT); } void Println::dump(ostream &o, int n) { indent(o, n) << "*Println"; dumpLineNum(o); o << endl; if (arg) { arg->dump(o, n+INDENT); } } void Assgt::dump(ostream &o, int n) { indent(o,n) << "*Assgt"; dumpLineNum(o); dumpType(o); o << endl; indent(o,n+INDENT) << id << " ="; o << endl; expr->dump(o, n+INDENT); } void VarRef::dump(ostream &o, int n) { indent(o, n) << "*VarRef"; dumpLineNum(o); o << id << " "; dumpType(o); o << endl; } void BoolLiteral::dump(ostream &o, int n) { indent(o, n) << "*BoolLiteral"; dumpLineNum(o); if (value) { o << "true"; } else { o << "false"; } o << " "; dumpType(o); o << endl; } void IntLiteral::dump(ostream &o, int n) { indent(o, n) << "*IntLiteral"; dumpLineNum(o); o << value << " "; dumpType(o); o << endl; } void Null::dump(ostream &o, int n) { indent(o, n) << "*Null"; dumpLineNum(o); dumpType(o); o << endl; } void New::dump(ostream &o, int n) { indent(o, n) << "*New"; dumpLineNum(o); o << objType << " "; dumpType(o); o << endl; } void FuncCall::dump(ostream &o, int n) { indent(o, n) << "*FuncCall"; dumpLineNum(o); o << funcName << " "; dumpType(o); o << endl; indent(o, n+INDENT-1) << "object expr: " << endl; objExpr->dump(o, n+INDENT); indent(o, n+INDENT-1) << "args: " << endl; ::dump(o, argList, n+INDENT); } void BinOp::defaultDump(ostream &o, int n) { dumpLineNum(o); dumpType(o); o << endl; left->dump(o, n+INDENT); right->dump(o, n+INDENT); } void Plus::dump(ostream &o, int n) { indent(o, n) << "*Plus"; defaultDump(o,n); } void Times::dump(ostream &o, int n) { indent(o, n) << "*Times"; defaultDump(o,n); } void Minus::dump(ostream &o, int n) { indent(o, n) << "*Minus"; defaultDump(o,n); } void Equals::dump(ostream &o, int n) { indent(o, n) << "*Equals"; defaultDump(o,n); } void NotEquals::dump(ostream &o, int n) { indent(o, n) << "*NotEquals"; defaultDump(o,n); } void LessThan::dump(ostream &o, int n) { indent(o, n) << "*LessThan"; defaultDump(o,n); } void And::dump(ostream &o, int n) { indent(o, n) << "*And"; defaultDump(o,n); } void Not::dump(ostream &o, int n) { indent(o,n) << "*Not"; dumpLineNum(o); dumpType(o); o << endl; expr->dump(o, n+INDENT); }