CAstDesignator* CParser::qualident(CAstScope *s) { // // qualident ::= ident { "[" expression "]" } // CToken t; // qualident -> ident ... CAstDesignator *id = ident(s); t = _scanner->Peek(); if (t.GetType() != tLBrak) return id; const CToken saveToken = id->GetToken(); const CSymbol* saveSymbol = id->GetSymbol(); CAstArrayDesignator *arrayId = new CAstArrayDesignator(saveToken, saveSymbol); if (!arrayId->GetType() || !arrayId->GetType()->IsArray()) { SetError(t, "access with index which is actually not an array"); return id; } free(id); while (t.GetType() == tLBrak) { // qualident -> ... "[" ... Consume(tLBrak); // qualident -> ... expression ... arrayId->AddIndex(expression(s)); // qualident -> ... "]" ... Consume(tRBrak); t = _scanner->Peek(); } arrayId->IndicesComplete(); return arrayId; }
CAstStatAssign* CParser::assignment(CAstScope *s, CToken ident) { // // assignment ::= qualident ":=" expression. // qualident ::= ident { "[" expression "]" }. // CToken t; CAstDesignator *lhs = NULL; CSymtab *symtab = s->GetSymbolTable(); if(_scanner->Peek().GetType() == tAssign) { // non-array case. const CSymbol *tmpsym = symtab->FindSymbol(ident.GetValue(), sLocal); if(tmpsym == 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, "designator expected."); } lhs = new CAstDesignator(ident, symtab->FindSymbol(ident.GetValue(), sGlobal)); } else if(tmpsym != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(tmpsym) != NULL) { SetError(ident, "designator expected."); } lhs = new CAstDesignator(ident, symtab->FindSymbol(ident.GetValue(), sLocal)); } else SetError(ident, "undefined identifier."); assert(lhs != NULL); } else if(_scanner->Peek().GetType() == tLSBrak) { // array case. const CSymbol *tmpsym = symtab->FindSymbol(ident.GetValue(), sLocal); CAstArrayDesignator *arraylhs; if(tmpsym == 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, "designator expected."); } arraylhs = new CAstArrayDesignator(ident, symtab->FindSymbol(ident.GetValue(), sGlobal)); } else if(tmpsym != NULL) { // Check if symbol is procedure. if(dynamic_cast<const CSymProc *>(tmpsym) != NULL) { SetError(ident, "designator expected."); } arraylhs = new CAstArrayDesignator(ident, symtab->FindSymbol(ident.GetValue(), sLocal)); } else SetError(ident, "undefined identifier."); // multiple index. while(_scanner->Peek().GetType() == tLSBrak) { Consume(tLSBrak); CAstExpression *expr = expression(s); assert(expr != NULL); arraylhs->AddIndex(expr); Consume(tRSBrak); } arraylhs->IndicesComplete(); lhs = dynamic_cast<CAstDesignator *>(arraylhs); assert(lhs != NULL); } CToken k; Consume(tAssign, &k); CAstExpression *rhs = expression(s); assert(rhs != NULL); return new CAstStatAssign(k, lhs, rhs); }
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; }