vector<string> CodeGenerator::statement(vector<Node>::iterator& it) { vector<string> stmtCode; vector<Node>::iterator temp = ++it; if (temp->symbol == "print") { // print id; temp = temp + 2; appendVectors(stmtCode, printID(temp)); it += 3; } else if (temp->symbol == "Expr") { // Expr; appendVectors(stmtCode, expr(it)); ++it; } else if (temp->symbol == "if") { // if ( Expr ) Stmt else Stmt appendVectors(stmtCode, ifElse(it)); } else if (temp->symbol == "while") { // while ( Expr ) Stmt appendVectors(stmtCode, whileStatement(it)); } else if (temp->symbol == "return") { // TODO: return string ret = "ret i32 0\n"; stmtCode.push_back(ret); } else if (temp->symbol == "Block") { appendVectors(stmtCode, block(it)); } return stmtCode; }
// <statement> ::= <simple statement> // | <complex statement> void SyntaxAnalyzer::statement(std::set<symboltype> followers) { //ako neterminala ne se vika ot drugo mqsto //ne se pravi startcheck & endcheck v tqh a samo v parenta if (statementStarters.find(symbol) != statementStarters.end()) { switch(symbol) { case ident: assignment(followers); break; case beginsy: compoundStatement(followers); break; case ifsy: ifStatement(followers); break; case whilesy: whileStatement(followers); break; case readsy: readStatement(followers); break; case writesy: writeStatement(followers); break; default: break; } // swtich } else { syntaxerror(othersy); } } // statement()
NodePointer Parser::statement() { NodePointer result; switch (currentSymbol_.symbolType_) { case IF: return ifStatement(); case WHILE: return whileStatement(); case IDENTIFIER: case GLOBAL: case ARGUMENT: case CALL: case CONSTANT: result = expression(); accept(SEMICOLON); return result; case MY: return localVariable(); case PRINT: return printStatement(); case SUBROUTINE: return procedureDefinition(); case RETURN: return returnStatement(); case LEFT_BRACE: return blockStatement(); default: throw ErrorMessage("Unexpected symbol " + symbolToString(currentSymbol_), getLineNumber()); } }
AST* Parser::statement(Scope* ps) { AST* ret = NULL; if (expect(";")) { ret = emptyStatement(); } else if (expect("var")) { ret = varStatement(ps); opteol(); } else if (expect("{")) { ret = block(ps); opteol(); } else if (expect("if")) { ret = ifStatement(ps); } else if (expect("switch")) { ret = switchStatement(ps); } else if (expect("do")) { ret = doStatement(ps); opteol(); } else if (expect("while")) { ret = whileStatement(ps); } else if (expect("for")) { ret = forStatement(ps); } else if (expect("with")) { ret = withStatement(ps); } else if (expect("continue")) { ret = continueStatement(); opteol(); } else if (expect("break")) { ret = breakStatement(); opteol(); } else if (expect("return")) { ret = returnStatement(ps); opteol(); } else if (expect("try")) { ret = tryStatement(ps); } else if (expect("throw")) { ret = throwStatement(ps); opteol(); } else { ret = expression(0, ps); opteol(); } return ret; }
StatementPtr Parser::parseStatement() { if (m_curToken.tokenType() == TokenType::Begin) { return parseCompoundStatement(); } else if (match(TokenType::If)) { ExpressionPtr expression = parseExpression(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } if (!match(TokenType::Then)) { reportError(ErrorCodes::ExpectedThen); panic(statementFollow, statementFollowSize); return StatementPtr(); } StatementPtr thenStatement = parseStatement(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } if (!match(TokenType::Else)) { reportError(ErrorCodes::ExpectedElse); panic(statementFollow, statementFollowSize); return StatementPtr(); } StatementPtr elseStatement = parseStatement(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } IfStatementPtr statement(new IfStatement); statement->expression = expression; statement->thenPart = thenStatement; statement->elsePart = elseStatement; return statement; } else if (match(TokenType::While)) { ExpressionPtr expression = parseExpression(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } if (!match(TokenType::Do)) { reportError(ErrorCodes::ExpectedDo); panic(statementFollow, statementFollowSize); return StatementPtr(); } StatementPtr doPart = parseStatement(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } WhileStatementPtr whileStatement(new WhileStatement); whileStatement->expression = expression; whileStatement->doPart = doPart; return whileStatement; } IdentifierPtr identifier = parseIdentifier(); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } AssignmentOrCallStatementPtr statement(new AssignmentOrCallStatement); statement->id = identifier; parseStatement_p(statement); if (m_errorCode > ErrorCodes::NoError) { panic(statementFollow, statementFollowSize); return StatementPtr(); } return statement; }
void statement (void) { //------------------------------------------------------------------- // NOTE: Since we currently don't support generic BEGIN/END (compound // statement) blocks... if ((curToken != TKN_CODE) /*&& (curToken != TKN_BEGIN)*/) crunchStatementMarker(); switch (curToken) { case TKN_IDENTIFIER: { SymTableNodePtr IdPtr = NULL; //-------------------------------------------------------------- // First, do we have an assignment statement or a function call? searchAndFindAllSymTables(IdPtr); if ((IdPtr->defn.key == DFN_FUNCTION)/* || (IdPtr->defn.key == DFN_MODULE)*/) { RoutineKey key = IdPtr->defn.info.routine.key; if ((key == RTN_ASSERT) || (key == RTN_PRINT) || (key == RTN_CONCAT)) { bool uncrunch = ((key == RTN_ASSERT) && !AssertEnabled) || ((key == RTN_PRINT) && !PrintEnabled) || ((key == RTN_CONCAT) && !StringFunctionsEnabled); if (uncrunch) { uncrunchStatementMarker(); Crunch = false; } } crunchSymTableNodePtr(IdPtr); if (IdPtr->defn.info.routine.flags & ROUTINE_FLAG_ORDER) { if (NumOrderCalls == MAX_ORDERS) syntaxError(ABL_ERR_SYNTAX_TOO_MANY_ORDERS); crunchByte((unsigned char)(NumOrderCalls / 32)); crunchByte((unsigned char)(NumOrderCalls % 32)); NumOrderCalls++; } getToken(); SymTableNodePtr thisRoutineIdPtr = CurRoutineIdPtr; routineCall(IdPtr, 1); CurRoutineIdPtr = thisRoutineIdPtr; Crunch = true; } else assignmentStatement(IdPtr); } break; case TKN_REPEAT: repeatStatement(); break; case TKN_WHILE: whileStatement(); break; case TKN_IF: ifStatement(); break; case TKN_FOR: forStatement(); break; case TKN_SWITCH: switchStatement(); break; case TKN_TRANS: transStatement(); break; case TKN_TRANS_BACK: transBackStatement(); break; } //--------------------------------------------------------------------- // Now, make sure the statement is closed off with the proper block end // statement, if necessary (which is usually the case :). synchronize(statementEndList, NULL, NULL); if (tokenIn(statementStartList)) syntaxError(ABL_ERR_SYNTAX_MISSING_SEMICOLON); }
struct statementNode* stmt() { struct statementNode* stm; struct printStatement* prt; struct varNode* temp; ttype = getToken(); if (ttype == ID) // assign_stmt { stm = make_stmt(ASSIGNSTMT); stm->assignSt = assignment(); //printf("assignSt->op1->vValue: %d\n",stm->assignSt->op1->vValue); stm->stmtType = ASSIGNSTMT; ttype = getToken(); if (ttype == SEMICOLON) { //printf("SEMICOLON: %d\n",ttype); return stm; } else { return NULL; } } else if (ttype == WHILE) // while_stmt { //struct gotoStatment* tempstm; //tempstm = (struct gotoStatement*) malloc(sizeof(struct gotoStatement)); struct statementNode* loop; struct statementNode* noop; struct gotoStatement* gt; stm = make_stmt(WHILESTMT); stm->whileSt = whileStatement(); stm->stmtType = WHILESTMT; //target/goto loop = make_stmt(GOTOSTMT); gt = make_gotoSt(); gt->target = stm; loop->gotoSt = gt; findLast(stm->whileSt->stmt_list)->next = loop; //noop noop = make_stmt(NOOPSTMT); stm->next = noop; findLast(stm->whileSt->stmt_list)->next = stm->next; //false stm->whileSt->condition->falseBranch = stm->next; ungetToken(); if(ttype == RBRACE) { return stm; } } else if (ttype == IF) { stm = make_stmt(IFSTMT); stm->ifSt = ifSt(); stm->stmtType = IFSTMT; //noop stm->next = stm->ifSt->condition->falseBranch; ungetToken(); if(ttype == RBRACE) { return stm; } else { return NULL; } } else if (ttype == PRINT) { //printf("stmt PRINT: %d\n",ttype); stm = make_stmt(PRINTSTMT); ttype = getToken(); //printf("token: %s\n",token); temp = symSearch(token); stm->stmtType = PRINTSTMT; if(temp != NULL) { prt = make_printSt(temp); stm->printSt = prt; ttype = getToken(); //printf ("SEMICOLON 22: %d \n", ttype); if(ttype == SEMICOLON) { return stm; } else { return NULL; } } else { return NULL; } } else { return NULL; } }
struct statementNode* stmt() { struct statementNode* stm; struct printStatement* prt; struct varNode* temp; ttype = getToken(); if (ttype == ID) // assign_stmt { printf("stmt ID: %d\n",ttype); stm = make_stmt(ASSIGNSTMT); printf("token: %s\n",token); stm->assignSt = assignment(token); stm->stmtType = ASSIGNSTMT; ttype = getToken(); if (ttype == SEMICOLON) { printf("SEMICOLON: %d\n",ttype); return stm; } else { return NULL; } } else if (ttype == WHILE) // while_stmt { ungetToken(); stm = make_stmt(WHILESTMT); stm->whileSt = whileStatement(); stm->stmtType = WHILESTMT; ttype = getToken(); if(ttype = RBRACE) { return stm; } else { return NULL; } } else if (ttype == IF) { ungetToken(); stm = make_stmt(IFSTMT); stm->ifSt = ifSt(); stm->stmtType = IFSTMT; ttype = getToken(); if(ttype = RBRACE) { return stm; } else { return NULL; } } else if (ttype == PRINT) { printf("stmt PRINT: %d\n",ttype); stm = make_stmt(PRINTSTMT); ttype = getToken(); printf("token: %s\n",token); temp = symSearch(token); stm->stmtType = PRINTSTMT; if(temp != NULL) { prt = make_printSt(temp); stm->printSt = prt; ttype = getToken(); if(ttype == SEMICOLON) { return stm; } else { return NULL; } } else { return NULL; } //return stm; } else { return NULL; } }
void loopsatetement() { forStatement(); whileStatement(); doWhileStatement(); }