diff --git a/lab3-4/Makefile.dependencies b/lab3-4/Makefile.dependencies index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a87ec84413b39881b59bf29f39db2a4b10344677 100644 --- a/lab3-4/Makefile.dependencies +++ b/lab3-4/Makefile.dependencies @@ -0,0 +1,5 @@ +symtab.o: symtab.cc symtab.hh string.hh ast.hh codegen.hh +ast.o: ast.cc ast.hh symtab.hh string.hh codegen.hh +codegen.o: codegen.cc ast.hh symtab.hh string.hh codegen.hh +main.o: main.cc ast.hh symtab.hh string.hh codegen.hh parser.hh +string.o: string.cc string.hh diff --git a/lab3-4/ast.cc b/lab3-4/ast.cc index 3a640a5a7ccb414626e0a6156f6442861c9515d1..d156936eb9088e241e58cb63331dce15d9ecb6cf 100644 --- a/lab3-4/ast.cc +++ b/lab3-4/ast.cc @@ -45,7 +45,7 @@ void ASTNode::print(ostream& o) o << "ASTNode"; } -void ASTNode::xprint(ostream& o, char *cls) +void ASTNode::xprint(ostream& o, const char *cls) { o << "ASTNode (" << cls << ")"; } @@ -228,7 +228,7 @@ void BinaryOperation::print(ostream& o) xprint(o, "BinaryOperation"); } -void BinaryOperation::xprint(ostream& o, char *cls) +void BinaryOperation::xprint(ostream& o, const char *cls) { o << cls << " (left, right) [" << ShortSymbols << valueType << LongSymbols @@ -293,7 +293,7 @@ void BinaryRelation::print(ostream& o) xprint(o, "BinaryRelation"); } -void BinaryRelation::xprint(ostream& o, char *cls) +void BinaryRelation::xprint(ostream& o, const char *cls) { o << cls << " (left, right)\n"; beginChild(o); @@ -316,7 +316,7 @@ void BinaryCondition::print(ostream& o) xprint(o, "BinaryCondition"); } -void BinaryCondition::xprint(ostream& o, char *cls) +void BinaryCondition::xprint(ostream& o, const char *cls) { o << cls << " (left, right)\n"; beginChild(o); diff --git a/lab3-4/ast.hh b/lab3-4/ast.hh index 7f886b902358683a65c6313d1bf4cfdc1e9f3b20..07c72669bb52fdcb12a8d1cee75c3b21d8ecdebb 100644 --- a/lab3-4/ast.hh +++ b/lab3-4/ast.hh @@ -59,7 +59,7 @@ protected: void endChild(ostream& o); void lastChild(ostream& o); virtual void print(ostream& o); - virtual void xprint(ostream& o, char* cls); + virtual void xprint(ostream& o, const char* cls); public: virtual VariableInformation *GenerateCode(QuadsList &q) = 0; @@ -318,7 +318,7 @@ class BinaryOperation : public Expression { protected: virtual void print(ostream& o); - virtual void xprint(ostream& o, char *); + virtual void xprint(ostream& o, const char *); public: Expression *left, *right; @@ -480,7 +480,7 @@ class BinaryRelation : public Condition { protected: virtual void print(ostream& o); - virtual void xprint(ostream& o, char *cls); + virtual void xprint(ostream& o, const char *cls); public: Expression *left; @@ -563,7 +563,7 @@ class BinaryCondition : public Condition { protected: virtual void print(ostream& o); - virtual void xprint(ostream&o, char *cls); + virtual void xprint(ostream&o, const char *cls); public: Condition *left, *right; diff --git a/lab3-4/codegen.cc b/lab3-4/codegen.cc index a49b68265f38f3674f39b3b18251ffd777455ef8..299c6c7a7a46fb9eafcfa824d1443bb720926870 100644 --- a/lab3-4/codegen.cc +++ b/lab3-4/codegen.cc @@ -21,18 +21,18 @@ long QuadsList::labelCounter; * parse tree, but ensure that all exits from the code end up at a * particular label. This is easy to do for most parts of the parse * tree: just generate the code in the usual manner, then jump to the - * label. + * label. */ - + VariableInformation *ASTNode::GenerateCodeAndJump(QuadsList& q, - long label) + long label) { VariableInformation *info; info = this->GenerateCode(q); q += new Quad(jump, label, - (SymbolInformation *)NULL, (SymbolInformation *)NULL); + (SymbolInformation *)NULL, (SymbolInformation *)NULL); return info; } @@ -49,7 +49,7 @@ VariableInformation *ASTNode::GenerateCodeAndJump(QuadsList& q, */ VariableInformation *ElseIfList::GenerateCodeAndJump(QuadsList &q, - long lbl) + long lbl) { long next; VariableInformation *info; @@ -71,7 +71,7 @@ VariableInformation *ElseIfList::GenerateCodeAndJump(QuadsList &q, */ void ArrayReference::GenerateAssignment(QuadsList& q, - VariableInformation *val) + VariableInformation *val) { /* --- Your code here --- */ @@ -89,26 +89,26 @@ void Identifier::GenerateAssignment(QuadsList& q, VariableInformation *val) { if (val->type == NULL || id->type == NULL) { - cerr << "Bug: you created an untyped variable.\n"; - abort(); + cerr << "Bug: you created an untyped variable.\n"; + abort(); } if (id->type == kIntegerType) { - q += new Quad(iassign, - dynamic_cast<SymbolInformation*>(val), + q += new Quad(iassign, + dynamic_cast<SymbolInformation*>(val), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(id)); } else if (id->type == kRealType) { - q += new Quad(rassign, + q += new Quad(rassign, dynamic_cast<SymbolInformation*>(val), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(id)); } else if (id->type == val->type) { - q += new Quad(aassign, val, val->type->arrayDimensions, id); + q += new Quad(aassign, val, val->type->arrayDimensions, id); } } @@ -118,7 +118,7 @@ void Identifier::GenerateAssignment(QuadsList& q, VariableInformation *val) * * Generate code for a list of statements. Make sure the code comes * out in the right order. - * + * * Note: The impelementation here is absolutely ridiculous since it * uses stack space proportional to the number of statements in the * list. A more sensible definition would simply iterate down the @@ -131,7 +131,7 @@ VariableInformation *StatementList::GenerateCode(QuadsList &q) { if (precedingStatements != NULL) { - precedingStatements->GenerateCode(q); + precedingStatements->GenerateCode(q); } return statement->GenerateCode(q); } @@ -140,9 +140,9 @@ VariableInformation *StatementList::GenerateCode(QuadsList &q) /* * IfStatement::GenerateCode * - * If statements, particularly those with elseif branches are actually + * If statements, particularly those with elseif branches are actually * quite involved. They tend to use a lot of jumps. For the elseif - * branches you'll probably want to use the GenerateCodeAndJump method + * branches you'll probably want to use the GenerateCodeAndJump method * of ElseIfStatement (which you're also supposed to write.) */ @@ -150,9 +150,9 @@ VariableInformation *IfStatement::GenerateCode(QuadsList& q) { /* --- Your code here ---*/ - + /* --- End your code --- */ - + return NULL; } @@ -174,7 +174,7 @@ VariableInformation *ElseIfList::GenerateCode(QuadsList& q) /* --- End your code --- */ - cerr << "Call to ElseIfList::GenerateCode. You probably didn't want to do this.\n"; // + cerr << "Call to ElseIfList::GenerateCode. You probably didn't want to do this.\n"; // abort(); } @@ -217,7 +217,7 @@ VariableInformation *WhileStatement::GenerateCode(QuadsList &q) VariableInformation *IntegerConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kIntegerType); + currentFunction->TemporaryVariable(kIntegerType); q += new Quad(iconst, value, NULL, info); return info; @@ -226,7 +226,7 @@ VariableInformation *IntegerConstant::GenerateCode(QuadsList& q) VariableInformation *RealConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kRealType); + currentFunction->TemporaryVariable(kRealType); q += new Quad(rconst, value, NULL, info); return info; @@ -235,7 +235,7 @@ VariableInformation *RealConstant::GenerateCode(QuadsList& q) VariableInformation *BooleanConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kIntegerType); + currentFunction->TemporaryVariable(kIntegerType); q += new Quad(iconst, value ? 1L : 0L, NULL, info); return info; @@ -262,7 +262,7 @@ VariableInformation *ArrayReference::GenerateCode(QuadsList& q) /* * Identifier::GenerateCode * - * Generate code to get the value of an identifier. Actually, we don't + * Generate code to get the value of an identifier. Actually, we don't * need to generate any code at all. We just return the identifier. */ @@ -287,18 +287,18 @@ VariableInformation *ReturnStatement::GenerateCode(QuadsList &q) info = value->GenerateCode(q); if (info->type != currentFunction->GetReturnType()) { - cerr << "Bug: you forgot to typecheck return statements.\n"; - abort(); + cerr << "Bug: you forgot to typecheck return statements.\n"; + abort(); } - q += new Quad(creturn, + q += new Quad(creturn, static_cast<SymbolInformation*>(NULL), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(info)); return NULL; } - + /* * ExpressionList::GenerateCode @@ -318,37 +318,37 @@ VariableInformation *ExpressionList::GenerateCode(QuadsList& q) } void ExpressionList::GenerateParameterList(QuadsList &q, - VariableInformation *lastParam) + VariableInformation *lastParam) { VariableInformation *info; if (lastParam == NULL || - (lastParam->prev != NULL && precedingExpressions == NULL)) + (lastParam->prev != NULL && precedingExpressions == NULL)) { - cerr << "Bug: type checking of function params isn't good enough.\n"; - abort(); + cerr << "Bug: type checking of function params isn't good enough.\n"; + abort(); } if (precedingExpressions) - precedingExpressions->GenerateParameterList(q, lastParam->prev); + precedingExpressions->GenerateParameterList(q, lastParam->prev); info = expression->GenerateCode(q); if (expression->valueType == lastParam->type) { - q += new Quad(param, + q += new Quad(param, dynamic_cast<SymbolInformation*>(info), static_cast<SymbolInformation*>(NULL), static_cast<SymbolInformation*>(NULL)); } else { - cerr << "Bug: type checking of function params isn't good enough.\n"; - abort(); + cerr << "Bug: type checking of function params isn't good enough.\n"; + abort(); } } - + @@ -389,12 +389,12 @@ VariableInformation *IntegerToReal::GenerateCode(QuadsList& q) if (value->valueType != kIntegerType) { - cerr << "Bug: you're trying to convert a non-integer to a real.\n"; + cerr << "Bug: you're trying to convert a non-integer to a real.\n"; } info = currentFunction->TemporaryVariable(kRealType); valueInfo = value->GenerateCode(q); - q += new Quad(itor, + q += new Quad(itor, dynamic_cast<SymbolInformation*>(valueInfo), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(info)); @@ -408,12 +408,12 @@ VariableInformation *TruncateReal::GenerateCode(QuadsList& q) if (value->valueType != kRealType) { - cerr << "Bug: you're trying to truncate a non-real.\n"; + cerr << "Bug: you're trying to truncate a non-real.\n"; } info = currentFunction->TemporaryVariable(kIntegerType); valueInfo = value->GenerateCode(q); - q += new Quad(rtrunc, + q += new Quad(rtrunc, dynamic_cast<SymbolInformation*>(valueInfo), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(info)); @@ -453,18 +453,19 @@ VariableInformation *TruncateReal::GenerateCode(QuadsList& q) */ static VariableInformation *BinaryGenerateCode(QuadsList& q, - tQuadType realop, - tQuadType intop, - ASTNode *left, - ASTNode *right, - ASTNode *node, - TypeInformation *type = NULL) + tQuadType realop, + tQuadType intop, + ASTNode *left, + ASTNode *right, + ASTNode *node, + TypeInformation *type = NULL) { VariableInformation *leftInfo, *rightInfo, *result; /* --- Your code here --- */ - + /* --- End your code --- */ + return NULL; //Should not return NULL! } /* @@ -519,18 +520,18 @@ VariableInformation *UnaryMinus::GenerateCode(QuadsList& q) if (info->type == kIntegerType) { - q += new Quad(iconst, 0, NULL, constInfo); - q += new Quad(isub, constInfo, info, result); + q += new Quad(iconst, 0, NULL, constInfo); + q += new Quad(isub, constInfo, info, result); } else if (info->type == kRealType) { - q += new Quad(rconst, 0.0, NULL, constInfo); - q += new Quad(rsub, constInfo, info, result); + q += new Quad(rconst, 0.0, NULL, constInfo); + q += new Quad(rsub, constInfo, info, result); } else { - cerr << "Bug: unary minus of a non-numeric type.\n"; - abort(); + cerr << "Bug: unary minus of a non-numeric type.\n"; + abort(); } return result; @@ -632,12 +633,12 @@ VariableInformation *Not::GenerateCode(QuadsList& q) info = right->GenerateCode(q); if (info->type != kIntegerType) { - cerr << "Bug: not operator applied to a non-integer.\n"; - abort(); + cerr << "Bug: not operator applied to a non-integer.\n"; + abort(); } result = currentFunction->TemporaryVariable(kIntegerType); - q += new Quad(inot, + q += new Quad(inot, dynamic_cast<SymbolInformation*>(info), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(result)); @@ -663,9 +664,9 @@ VariableInformation *FunctionCall::GenerateCode(QuadsList& q) VariableInformation *info; if (arguments) - arguments->GenerateParameterList(q, function->GetLastParam()); + arguments->GenerateParameterList(q, function->GetLastParam()); info = currentFunction->TemporaryVariable(function->GetReturnType()); - q += new Quad(call, + q += new Quad(call, dynamic_cast<SymbolInformation*>(function), static_cast<SymbolInformation*>(NULL), dynamic_cast<SymbolInformation*>(info)); @@ -682,13 +683,13 @@ QuadsList& QuadsList::operator+=(Quad *q) { if (head == NULL) { - head = new QuadsListElement(q, NULL); - tail = head; + head = new QuadsListElement(q, NULL); + tail = head; } else { - tail->next = new QuadsListElement(q, NULL); - tail = tail->next; + tail->next = new QuadsListElement(q, NULL); + tail = tail->next; } return *this; @@ -704,8 +705,8 @@ ostream& QuadsList::print(ostream& o) elem = head; while (elem) { - o << elem->data << '\n'; - elem = elem->next; + o << elem->data << '\n'; + elem = elem->next; } o << LongSymbols; @@ -718,248 +719,248 @@ ostream& Quad::print(ostream& o) switch(opcode) { case iconst: - o << setw(8) << "iconst " - << setw(8) << int1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) << "iconst " + << setw(8) << int1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case rconst: - o << setw(8) <<"rconst " - << setw(8) <<real1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) <<"rconst " + << setw(8) <<real1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case iaddr: - o << setw(8) <<"iaddr " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; - case itor: - o << setw(8) <<"itor " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; - case rtrunc: - o << setw(8) <<"rtrunc " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; - case iadd: - o << setw(8) << "iadd " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case isub: - o << setw(8) << "isub " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case imul: - o << setw(8) << "imul " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case idiv: - o << setw(8) << "idiv " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case ipow: - o << setw(8) << "ipow " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case radd: - o << setw(8) << "radd " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rsub: - o << setw(8) << "rsub " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rmul: - o << setw(8) << "rmul " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rdiv: - o << setw(8) << "rdiv " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rpow: - o << setw(8) << "rpow " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case igt: - o << setw(8) << "igt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case ilt: - o << setw(8) << "ilt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case ieq: - o << setw(8) << "ieq " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rgt: - o << setw(8) << "rgt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case rlt: - o << setw(8) << "rlt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case req: - o << setw(8) << "req " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case iand: - o << setw(8) << "iand " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case ior: - o << setw(8) << "ior " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; - case inot: - o << setw(8) << "inot " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case jtrue: - o << setw(8) << "jtrue " - << setw(8) << int1 - << setw(8) << sym2 - << setw(8) << "-"; - break; - case jfalse: - o << setw(8) << "jfalse " - << setw(8) << int1 - << setw(8) << sym2 - << setw(8) << "-"; - break; - case jump: - o << setw(8) << "jump " - << setw(8) << int1 - << setw(8) << "-" - << setw(8) << "-"; - break; - case clabel: - o << setw(8) << "clabel " - << setw(8) << int1 - << setw(8) << "-" - << setw(8) << "-"; - break; - case istore: - o << setw(8) << "istore " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case iload: - o << setw(8) << "iload " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case rstore: - o << setw(8) << "rstore " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case rload: - o << setw(8) << "rload " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case creturn: - o << setw(8) << "creturn " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << sym3; - break; - case param: - o << setw(8) << "param " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << "-"; - break; - case call: - o << setw(8) << "call " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case iassign: - o << setw(8) << "iassign " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case rassign: - o << setw(8) << "rassign " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; - case aassign: - o << setw(8) << "aassign " - << setw(8) << sym1 - << setw(8) << int1 - << setw(8) << sym3; - break; - case hcf: - o << setw(8) << "hcf " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << "-"; - break; - case nop: - o << setw(8) << "nop " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) <<"iaddr " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; + case itor: + o << setw(8) <<"itor " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; + case rtrunc: + o << setw(8) <<"rtrunc " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; + case iadd: + o << setw(8) << "iadd " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case isub: + o << setw(8) << "isub " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case imul: + o << setw(8) << "imul " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case idiv: + o << setw(8) << "idiv " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case ipow: + o << setw(8) << "ipow " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case radd: + o << setw(8) << "radd " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rsub: + o << setw(8) << "rsub " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rmul: + o << setw(8) << "rmul " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rdiv: + o << setw(8) << "rdiv " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rpow: + o << setw(8) << "rpow " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case igt: + o << setw(8) << "igt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case ilt: + o << setw(8) << "ilt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case ieq: + o << setw(8) << "ieq " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rgt: + o << setw(8) << "rgt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case rlt: + o << setw(8) << "rlt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case req: + o << setw(8) << "req " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case iand: + o << setw(8) << "iand " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case ior: + o << setw(8) << "ior " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; + case inot: + o << setw(8) << "inot " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case jtrue: + o << setw(8) << "jtrue " + << setw(8) << int1 + << setw(8) << sym2 + << setw(8) << "-"; + break; + case jfalse: + o << setw(8) << "jfalse " + << setw(8) << int1 + << setw(8) << sym2 + << setw(8) << "-"; + break; + case jump: + o << setw(8) << "jump " + << setw(8) << int1 + << setw(8) << "-" + << setw(8) << "-"; + break; + case clabel: + o << setw(8) << "clabel " + << setw(8) << int1 + << setw(8) << "-" + << setw(8) << "-"; + break; + case istore: + o << setw(8) << "istore " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case iload: + o << setw(8) << "iload " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case rstore: + o << setw(8) << "rstore " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case rload: + o << setw(8) << "rload " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case creturn: + o << setw(8) << "creturn " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << sym3; + break; + case param: + o << setw(8) << "param " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << "-"; + break; + case call: + o << setw(8) << "call " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case iassign: + o << setw(8) << "iassign " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case rassign: + o << setw(8) << "rassign " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; + case aassign: + o << setw(8) << "aassign " + << setw(8) << sym1 + << setw(8) << int1 + << setw(8) << sym3; + break; + case hcf: + o << setw(8) << "hcf " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << "-"; + break; + case nop: + o << setw(8) << "nop " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << "-"; + break; default: - o << "unknown (" << opcode << ")"; - break; + o << "unknown (" << opcode << ")"; + break; } return o; @@ -978,7 +979,7 @@ void QuadsList::print_c(ostream& o) // o << elem->data << '\n'; elem->data->print_c(o); o << "\n"; - elem = elem->next; + elem = elem->next; } o << CFormat; @@ -987,247 +988,247 @@ void QuadsList::print_c(ostream& o) void Quad::print_c(ostream& o) { o << " "; - static ::string ss = "("; + static ::string ss = (char*) "("; static bool firstParam = true; switch(opcode) { case iconst: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << int1; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << int1; + break; case rconst: - o << setw(8) <<"real" - << setw(8) << sym3 + o << setw(8) <<"real" + << setw(8) << sym3 << setw(8) << "=" - << setw(8) << real1; - break; + << setw(8) << real1; + break; case iaddr: o << setw(8) << "integer" << setw(8) << sym3 << setw(8) << "=" - << setw(8) << "(integer)" << sym1; - break; + << setw(8) << "(integer)" << sym1; + break; case itor: - o << setw(8) << "real" + o << setw(8) << "real" << setw(8) << sym3 << setw(8) <<"=" - << setw(8) << "(real)"<< sym1; - break; + << setw(8) << "(real)"<< sym1; + break; case rtrunc: - o << setw(8) << "integer" + o << setw(8) << "integer" << setw(8) << sym3 - << setw(8) <<"=" - << setw(8) << "(integer)" <<sym3; - break; + << setw(8) <<"=" + << setw(8) << "(integer)" <<sym3; + break; case iadd: o << setw(8) << "integer" << setw(8) << sym3 << setw(8) << " = " - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << " + " - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case isub: o << setw(8) << "integer" << setw(8) << sym3 << setw(8) << " = " - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << " - " - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case imul: - o << setw(8) << "integer" - << setw(8) << sym3 + o << setw(8) << "integer" + << setw(8) << sym3 << setw(8) << " = " - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << " * " - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case idiv: - o << setw(8) << "real" + o << setw(8) << "real" << setw(8) << sym3 << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << "/" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case ipow: o << setw(8) << "real" << setw(8) << sym3 << setw(8) << "= pow(" - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << "," - << setw(8) << sym2 << ")"; - break; + << setw(8) << sym2 << ")"; + break; case radd: o << setw(8) << "real" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "+" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case rsub: o << setw(8) << "real" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "-" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case rmul: o << setw(8) << "real" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "*" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case rdiv: - o << setw(8) << "real" + o << setw(8) << "real" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "/" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case rpow: o << setw(8) << "real " << setw(8) << sym3 << setw(8) << "= pow(" - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << "," - << setw(8) << sym2 << ")"; - break; + << setw(8) << sym2 << ")"; + break; case igt: - o << setw(8) << "conditional" + o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << ">" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case ilt: o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "<" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case ieq: - o << setw(8) << "conditional" + o << setw(8) << "conditional" << setw(8) << sym3 << setw(8) << "=" << setw(8) << sym1 << setw(8) << "==" << setw(8) << sym2; - break; + break; case rgt: o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << ">" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case rlt: o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "<" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case req: o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "==" - << setw(8) << sym2; + << setw(8) << sym2; break; case iand: - o << setw(8) << "conditional" + o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "&&" - << setw(8) << sym2; + << setw(8) << sym2; break; case ior: o << setw(8) << "conditional" << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << "=" + << setw(8) << sym1 << setw(8) << "||" - << setw(8) << sym2; - break; + << setw(8) << sym2; + break; case inot: - o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" + o << setw(8) << "conditional" + << setw(8) << sym3 + << setw(8) << "=" << setw(8) << "!" - << setw(8) << sym3; - break; + << setw(8) << sym3; + break; case jtrue: o << setw(8) << "if(" << setw(8) << sym2 << setw(8) << ")" << setw(8) << setw(8) << "goto clabel" << int1; - break; + break; case jfalse: o << setw(8) << "if(!" << setw(8) << sym2 << setw(8) << ")" << setw(8) << setw(8) << "goto clabel" << int1; - break; + break; case jump: - o << setw(8) << "goto " - << setw(8) << "clabel" + o << setw(8) << "goto " + << setw(8) << "clabel" << int1 - << setw(8) << "/*jmp*/"; - break; + << setw(8) << "/*jmp*/"; + break; case clabel: - o << setw(8) << "clabel" - << int1 - << ":"; - break; + o << setw(8) << "clabel" + << int1 + << ":"; + break; case istore: - o << setw(8) << "" + o << setw(8) << "" << setw(8) << "*(integer*)" << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; - break; + << setw(8) << "=" + << setw(8) << sym1; + break; + break; case iload: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << "= /*iload*/" - << setw(8) << "*(integer*)" << sym1; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << "= /*iload*/" + << setw(8) << "*(integer*)" << sym1; + break; case rstore: - o << setw(8) << "" + o << setw(8) << "" << setw(8) << "*(real*)" << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + << setw(8) << "=" + << setw(8) << sym1; + break; case rload: - o + o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "= /*rload*/" - << setw(8) << "*(real*)" << sym1; - break; + << setw(8) << sym3 + << setw(8) << "= /*rload*/" + << setw(8) << "*(real*)" << sym1; + break; case creturn: - o << setw(8) << "return " - << setw(8) << sym3 - << setw(8) + o << setw(8) << "return " + << setw(8) << sym3 + << setw(8) << setw(8) << "/*Return statement*/"; - break; + break; case param: { if (firstParam) { ss = ss + sym1->id; @@ -1240,23 +1241,23 @@ void Quad::print_c(ostream& o) case call: { o << setw(8) << "integer " << sym3 //Let it be ints for now.. << setw(8) << "=" - << setw(8) << sym1 + << setw(8) << sym1 << setw(8) << ss << setw(8) << ")"; ss = "("; //Reset callstack firstParam = true; } - break; + break; case iassign: - o << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + o << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; case rassign: - o << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + o << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; case aassign: { ::string itVar = sym1->id + "_" + ::string(int1); o << setw(8) << "for(int " << itVar << " = 0 ;" @@ -1266,17 +1267,17 @@ void Quad::print_c(ostream& o) << setw(8) << sym3 << "[" << itVar << "]" << "=" << setw(8) << sym1 << "[" << itVar << "];"; } - break; + break; case hcf: o << setw(8) << "exit(-1)"; - break; + break; case nop: o << setw(8) << ";/*No operation*/" << setw(8) << ""; - break; + break; default: - o << "unknown (" << opcode << ")"; - break; + o << "unknown (" << opcode << ")"; + break; } //Add semicolon to all statements o << setw(8) << ";"; @@ -1286,9 +1287,9 @@ void Quad::print_c(ostream& o) ostream& operator<<(ostream& o, QuadsList *q) { if (q != NULL) - return q->print(o); + return q->print(o); else - return o << " QuadsList @ 0x0\n"; + return o << " QuadsList @ 0x0\n"; } ostream& operator<<(ostream& o, QuadsList& q) @@ -1299,9 +1300,9 @@ ostream& operator<<(ostream& o, QuadsList& q) ostream& operator<<(ostream& o, Quad *q) { if (q != NULL) - return q->print(o); + return q->print(o); else - return o << " Quad @ 0x0"; + return o << " Quad @ 0x0"; } ostream& operator<<(ostream& o, Quad& q) diff --git a/lab3-4/main.cc b/lab3-4/main.cc index 651111cefd5cd06aee23e259598525764c8e88bd..6975bc8d8b9bdfc934ee93b153cb0224c4036d89 100644 --- a/lab3-4/main.cc +++ b/lab3-4/main.cc @@ -6,14 +6,12 @@ #include "symtab.hh" #include <unistd.h> -using namespace std; - extern int yyparse(void); extern int yydebug; extern int errorCount; extern int warningCount; -static char *optionString = "dhc"; +static const char *OPTION_STRING = "dhc"; void Usage(char *program) { @@ -34,9 +32,11 @@ int main(int argc, char **argv) int option; extern FILE *yyin; - // - // Set up the symbol table - // + /* + Set up the symbol table. + Adds builtin functions used by the language. + Builtin functions include routines to print reals integers etc. + */ currentFunction = new FunctionInformation("main"); kIntegerType = new TypeInformation("integer", sizeof(long)); @@ -70,7 +70,7 @@ int main(int argc, char **argv) opterr = 0; optopt = '?'; - while ((option = getopt(argc, argv, optionString)) != EOF) + while ((option = getopt(argc, argv, OPTION_STRING)) != EOF) { switch (option) { diff --git a/lab3-4/parser.y b/lab3-4/parser.y index 054d45b20185835ec1a7abe30ead170194ad768c..5d7c442a13ff1ba8bb68f45678c767fe8eb34da5 100644 --- a/lab3-4/parser.y +++ b/lab3-4/parser.y @@ -10,7 +10,7 @@ extern int yylineno, errorCount, warningCount; extern FunctionInformation *currentFunction; extern int yylex(void); -extern void yyerror(char *); +extern void yyerror(const char *); extern char CheckCompatibleTypes(Expression **, Expression **); extern char CheckAssignmentTypes(LeftValue **, Expression **); extern char CheckFunctionParameters(FunctionInformation *, @@ -81,7 +81,7 @@ extern ostream& warning(void); */ %token FUNCTION ID DECLARE ARRAY INTEGER OF REAL XBEGIN XEND IF THEN -%token ELSE WHILE DO ASSIGN RETURN GE LE EQ NE ARRAY TRUE FALSE PROGRAM +%token ELSE WHILE DO ASSIGN RETURN GE LE EQ NE TRUE FALSE PROGRAM %token ELSEIF @@ -800,7 +800,7 @@ char CheckReturnType(Expression **expr, TypeInformation *info) } -void yyerror(char *message) +void yyerror(const char *message) { error() << message << '\n' << flush; } diff --git a/lab3-4/string.cc b/lab3-4/string.cc index de2ddce039156017c6d5a7ac9baf17c33fe56272..46b485cf6bd8463a3092cabfeab2bab2821d2b7b 100644 --- a/lab3-4/string.cc +++ b/lab3-4/string.cc @@ -73,6 +73,16 @@ string::string(char *s) chunk_size = 10; } +string::string(const char *s) +{ + text = strdup(s); + if (text == NULL) + abort(); + size = strlen(s) + 1; + position = strlen(s); + chunk_size = 10; +} + string::string(char c, int sz) { text = (char *)malloc(sz); diff --git a/lab3-4/string.hh b/lab3-4/string.hh index b7fa56ff4d7ff66cce0fd04c5c82c609bc9d583e..16cef64af7eb68d9de24ca7cf82d4fe54df015c3 100644 --- a/lab3-4/string.hh +++ b/lab3-4/string.hh @@ -34,10 +34,14 @@ private: void ensure_size(int); public: + //To kill warnings and make this custom string play nice... + + class error {}; // Exception thrown when out of memory string(); // Default constructor creates empty string string(char *); // Create string from character pointer + string(const char *); // Create string from a constant character pointer string(char, int); // Create string filles with characters string(const string &); // Copy constructor string(int); // Convert an integer diff --git a/lab3-4/symtab.cc b/lab3-4/symtab.cc index 337f5fc4124e3b7024351c20c0951b11c843dd1c..dfa6cfc2932e2612ca25f8bc5f01b085df6e943b 100644 --- a/lab3-4/symtab.cc +++ b/lab3-4/symtab.cc @@ -397,7 +397,7 @@ TypeInformation *FunctionInformation::AddArrayType(TypeInformation *elemType, else { - name = elemType->id + "<" + dimensions + ">."; + name = elemType->id + (char*)"<" + dimensions + (char*)">."; xinfo = symbolTable.LookupSymbol(name); if (xinfo == NULL) { diff --git a/lab3-4/symtab.hh b/lab3-4/symtab.hh index 7e06b3ede4fae221a4478d5d26018a81d8106a87..8c9ad58e1077a7846605cf983e508d8e6fa5b580 100644 --- a/lab3-4/symtab.hh +++ b/lab3-4/symtab.hh @@ -91,7 +91,7 @@ protected: friend class SymbolTable; friend ostream& LongSymbols(ostream&); friend ostream& SummarySymbols(ostream&); - friend ostream& ShortSymbols(ostream&); + friend ostream& ShortSymbols(ostream&); public: typedef enum { kFullFormat, kSummaryFormat, kShortFormat, kCFormat } tFormatType; static tFormatType outputFormat; @@ -100,8 +100,8 @@ public: SymbolTable *table; SymbolInformation(SymbolInformationType t, const ::string &i) : - tag(t), - id(i) {}; + tag(t), + id(i) {}; virtual ~SymbolInformation() {} virtual FunctionInformation *SymbolAsFunction(void) { return NULL; }; @@ -141,13 +141,13 @@ private: public: FunctionInformation(const ::string& i) : - SymbolInformation(kFunctionInformation, i), - parent(NULL), - returnType(NULL), - lastParam(NULL), - lastLocal(NULL), - body(NULL), - quads(NULL) {}; + SymbolInformation(kFunctionInformation, i), + parent(NULL), + returnType(NULL), + lastParam(NULL), + lastLocal(NULL), + body(NULL), + quads(NULL) {}; virtual FunctionInformation *SymbolAsFunction(void) { return this; }; @@ -164,7 +164,7 @@ public: FunctionInformation *AddFunction(const ::string&, FunctionInformation *); VariableInformation *AddParameter(const ::string&, TypeInformation *); - VariableInformation *AddVariable(const ::string&, TypeInformation *); + VariableInformation *AddVariable(const ::string&, TypeInformation *); SymbolInformation *AddSymbol(SymbolInformation *); TypeInformation *AddArrayType(TypeInformation *, int); @@ -197,10 +197,10 @@ public: virtual VariableInformation *SymbolAsVariable(void) { return this; }; VariableInformation(const ::string& i) : - SymbolInformation(kVariableInformation, i) {}; + SymbolInformation(kVariableInformation, i) {}; VariableInformation(const ::string& i, TypeInformation *t) : - SymbolInformation(kVariableInformation, i), - type(t) {}; + SymbolInformation(kVariableInformation, i), + type(t) {}; }; @@ -220,8 +220,8 @@ public: virtual TypeInformation *SymbolAsType(void) { return this; }; TypeInformation(const ::string& i, unsigned long s) : - SymbolInformation(kTypeInformation, i), - size(s) {}; + SymbolInformation(kTypeInformation, i), + size(s) {}; };