bool CAstStatReturn::TypeCheck(CToken *t, string *msg) const { const CType *st = GetScope()->GetType(); CAstExpression *e = GetExpression(); if (st->Match(CTypeManager::Get()->GetNull())) { if (e != NULL) { if (t != NULL) *t = e->GetToken(); if (msg != NULL) *msg = "superfluous expression after return."; return false; } } else { if (e == NULL) { if (t != NULL) *t = GetToken(); if (msg != NULL) *msg = "expression expected after return."; return false; } if (!e->TypeCheck(t, msg)) return false; if (!st->Match(e->GetType())) { if (t != NULL) *t = e->GetToken(); if (msg != NULL) *msg = "return type mismatch."; return false; } } return true; }
CAstStatCall* CParser::subroutineCall(CAstScope *s, CToken ident) { // // subroutineCall ::= ident "(" [ expression {"," expression} ] ")". // CToken t; Consume(tLBrak); CSymtab *symtab = s->GetSymbolTable(); CAstFunctionCall *call = NULL; // Check undefined procedure/function call. if(symtab->FindSymbol(ident.GetValue(), sLocal) == NULL && symtab->FindSymbol(ident.GetValue(), sGlobal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(ident.GetValue(), sGlobal)) == NULL) SetError(ident, "invalid procedure/function identifier."); call = new CAstFunctionCall(ident, dynamic_cast<const CSymProc *>(symtab->FindSymbol(ident.GetValue(), sGlobal))); } else if(symtab->FindSymbol(ident.GetValue(), sLocal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(ident.GetValue(), sLocal)) == NULL) SetError(ident, "invalid procedure/function identifier."); call = new CAstFunctionCall(ident, dynamic_cast<const CSymProc *>(symtab->FindSymbol(ident.GetValue(), sLocal))); } else SetError(ident, "undefined identifier."); // Add expressions. if(isExpr(_scanner->Peek())) { CAstExpression *expr = expression(s); assert(expr != NULL); // Arrays are addressed. if(expr->GetType()->IsArray()) expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL); call->AddArg(expr); while(_scanner->Peek().GetType() == tComma) { Consume(tComma); expr = expression(s); assert(expr != NULL); // Arrays are addressed. if(expr->GetType()->IsArray()) expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL); call->AddArg(expr); } } Consume(tRBrak); return new CAstStatCall(ident, call); }
bool CAstArrayDesignator::TypeCheck(CToken *t, string *msg) const { bool result = true; assert(_done); if(!GetType()) { // if GetType is NULL then it is an invalid array expression if(t != NULL) *t = GetToken(); if(msg != NULL) *msg = "invalid array expression."; return false; } int i; for(i = 0; i < GetNIndices(); i++) { CAstExpression *e = GetIndex(i); if(!e->TypeCheck(t, msg)) return false; // Do TypeCheck on indexing expression if(!e->GetType()->IsInt()) { // The indexing expression should be Int type if(t != NULL) *t = e->GetToken(); if(msg != NULL) *msg = "invalid array index expression."; return false; } } return result; }
bool CAstStatIf::TypeCheck(CToken *t, string *msg) const { CAstExpression *cond = GetCondition(); bool result = true; if (!cond->TypeCheck(t, msg)) return false; if (cond->GetType() != CTypeManager::Get()->GetBool()) { if (t != NULL) *t = cond->GetToken(); if (msg != NULL) *msg = "boolean expression expected."; return false; } try { CAstStatement *ifBody = GetIfBody(); CAstStatement *elseBody = GetElseBody(); while (result && (ifBody != NULL)) { result = ifBody->TypeCheck(t, msg); ifBody = ifBody->GetNext(); } while (result && (elseBody != NULL)) { result = elseBody->TypeCheck(t, msg); elseBody = elseBody->GetNext(); } } catch (...) { result = false; } return result; }
bool CAstFunctionCall::TypeCheck(CToken *t, string *msg) const { const CSymProc *symbol = GetSymbol(); // check the number of procedure/function arguments. if (GetNArgs() != symbol->GetNParams()) { if (t != NULL) *t = GetToken(); if (msg != NULL) *msg = "the number of arguments mismatch."; return false; } // first, type check for expression of arguments. // then, check the types of procedure/function arguments. // in this project, arguments type is only integer. for (int i = 0; i < GetNArgs(); i++) { CAstExpression *arg = GetArg(i); if (!arg->TypeCheck(t, msg)) return false; if (arg->GetType() != symbol->GetParam(i)->GetDataType()) { if (t != NULL) *t = arg->GetToken(); if (msg != NULL) *msg = "the type of arguments mismatch."; return false; } } return true; }
bool CAstStatAssign::TypeCheck(CToken *t, string *msg) const { CAstDesignator *id = GetLHS(); CAstExpression *e = GetRHS(); if (!id->TypeCheck(t, msg)) return false; if (!e->TypeCheck(t, msg)) return false; if (!id->GetType()->Match(e->GetType())) { if (t != NULL) *t = e->GetToken(); if (msg != NULL) *msg = "assignment type mismatch."; return false; } return true; }
CAstExpression* CParser::factor(CAstScope *s) { // // factor ::= qualident | number | boolean | char | string | "(" expression ")" | subroutineCall | "!" factor. // FIRST(factor) = { tIdent, tNumber, tTrue, tFalse, tCharacter, tString, tLBrak, tEMark }. // CToken t; EToken tt = _scanner->Peek().GetType(); CTypeManager *tm = CTypeManager::Get(); CAstExpression *n = NULL; CSymtab *symtab = s->GetSymbolTable(); switch(tt) { case tTrue: Consume(tTrue, &t); n = new CAstConstant(t, tm->GetBool(), 1); break; case tFalse: Consume(tFalse, &t); n = new CAstConstant(t, tm->GetBool(), 0); break; case tNumber: { Consume(tNumber, &t); errno = 0; long long v = strtoll(t.GetValue().c_str(), NULL, 10); if (errno != 0) SetError(t, "invalid number."); // integer range validation check. if(v > 2147483648) SetError(t, "integer constant outside valid range."); n = new CAstConstant(t, tm->GetInt(), v); break; } case tIdent: { Consume(tIdent, &t); if(_scanner->Peek().GetType() == tLBrak) { // subroutineCall Consume(tLBrak); CAstFunctionCall *f; // If subroutineCall calls undefined procedure/function, then set error. if(symtab->FindSymbol(t.GetValue(),sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) == NULL) SetError(t, "invalid procedure/function identifier."); f = new CAstFunctionCall(t, dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal))); } else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) == NULL) SetError(t, "invalid procedure/function identifier."); f = new CAstFunctionCall(t, dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal))); } else SetError(t, "undefined identifier."); assert(f != NULL); if(isExpr(_scanner->Peek())) { CAstExpression *expr = expression(s); assert(expr != NULL); // If array, then addressed. if(expr->GetType()->IsArray()) expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL); f->AddArg(expr); while(_scanner->Peek().GetType() == tComma) { Consume(tComma); expr = expression(s); assert(expr != NULL); // If array, then addressed. if(expr->GetType()->IsArray()) expr = new CAstSpecialOp(expr->GetToken(), opAddress, expr, NULL); f->AddArg(expr); } } Consume(tRBrak); n = f; } else { // qualident if(_scanner->Peek().GetType() == tLSBrak) { // It means array CAstArrayDesignator *f; if(symtab->FindSymbol(t.GetValue(), sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) != NULL) SetError(t, "designator expected."); f = new CAstArrayDesignator(t, symtab->FindSymbol(t.GetValue(), sGlobal)); } else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) != NULL) SetError(t, "designator expected"); f = new CAstArrayDesignator(t, symtab->FindSymbol(t.GetValue(), sLocal)); } else { SetError(t, "undefined identifier."); } while(_scanner->Peek().GetType() == tLSBrak) { Consume(tLSBrak); CAstExpression *expr = expression(s); assert(expr != NULL); f->AddIndex(expr); Consume(tRSBrak); } f->IndicesComplete(); n = f; } // non-array case. else { if(symtab->FindSymbol(t.GetValue(), sLocal) == NULL && symtab->FindSymbol(t.GetValue(), sGlobal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sGlobal)) != NULL) { SetError(t, "designator expected."); } n = new CAstDesignator(t, symtab->FindSymbol(t.GetValue(), sGlobal)); } else if(symtab->FindSymbol(t.GetValue(), sLocal) != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(symtab->FindSymbol(t.GetValue(), sLocal)) != NULL) { SetError(t, "designator expected."); } n = new CAstDesignator(t, symtab->FindSymbol(t.GetValue(), sLocal)); } else SetError(t, "undefined identifier."); } } break; } case tString: Consume(tString, &t); n = new CAstStringConstant(t, t.GetValue(), s); break; case tCharacter: { Consume(tCharacter, &t); char res; // If character length is 0, it means character is '\0' because invalid character cases are handled in scanner. if(t.GetValue().length() == 0) res = '\0'; else if(t.GetValue().length() == 1) { if(t.GetValue().at(0) != '\\') res = t.GetValue().at(0); else SetError(t, "wrong character"); } // unescape. else if(t.GetValue().length() == 2) { if(t.GetValue().at(0) == '\\') { if(t.GetValue().at(1) == 'n') { res = '\n'; } else if(t.GetValue().at(1) == 't') { res = '\t'; } else if(t.GetValue().at(1) == '"') { res = '"'; } else if(t.GetValue().at(1) == '\'') { res = '\''; } else if(t.GetValue().at(1) == '\\') { res = '\\'; } else if(t.GetValue().at(1) == '0') { res = '\0'; } else SetError(t, "wrong character"); } } else SetError(t, "wrong character"); n = new CAstConstant(t, tm->GetChar(), (long long)res); break; } case tLBrak: Consume(tLBrak); n = expression(s); Consume(tRBrak); break; case tEMark: Consume(tEMark, &t); n = new CAstUnaryOp(t, opNot, factor(s)); break; default: // cout << "got " << _scanner->Peek() << endl; SetError(_scanner->Peek(), "factor expected."); break; } return n; }