asCScriptNode *asCParser::ParseStatement() { sToken t1; GetToken(&t1); RewindTo(&t1); if( t1.type == ttIf ) return ParseIf(); else if( t1.type == ttFor ) return ParseFor(); else if( t1.type == ttWhile ) return ParseWhile(); else if( t1.type == ttReturn ) return ParseReturn(); else if( t1.type == ttStartStatementBlock ) return ParseStatementBlock(); else if( t1.type == ttBreak ) return ParseBreak(); else if( t1.type == ttContinue ) return ParseContinue(); else if( t1.type == ttDo ) return ParseDoWhile(); else if( t1.type == ttSwitch ) return ParseSwitch(); else return ParseExpressionStatement(); }
/// primary /// ::= identifierexpr /// ::= numberexpr /// ::= parenexpr std::unique_ptr<ExprAST> UDFParser::ParsePrimary() { switch (cur_tok_) { default: return LogError("unknown token when expecting an expression"); case tok_identifier: return ParseIdentifierExpr(); case tok_number: return ParseNumberExpr(); case '(': return ParseParenExpr(); case tok_return: return ParseReturn(); case tok_if: return ParseIfExpr(); case tok_semicolon: return nullptr; } }
AST* ParseOperator(ParserState* parser) { AST* ast; switch(parser->currentToken.type) { case TOKEN_BEGIN: return ParseBlock(parser); case TOKEN_ID: ast = parser->nextToken.type == TOKEN_LEFTBRACKET ? ParseFunctionCall(parser) : ParseAssignment(parser); Match(parser, TOKEN_SEMICOLON); return ast; case TOKEN_LOCAL: ast = ParseAssignment(parser); Match(parser, TOKEN_SEMICOLON); return ast; case TOKEN_WHILE: return ParseWhileCycle(parser); case TOKEN_IF: return ParseIfStatement(parser); case TOKEN_FUNCTION: return ParseFunctionDefinition(parser); case TOKEN_RETURN: ast = ParseReturn(parser); Match(parser, TOKEN_SEMICOLON); return ast; case TOKEN_PRINT: ast = ParsePrint(parser); Match(parser, TOKEN_SEMICOLON); return ast; case TOKEN_INCLUDE: return ParseInclude(parser); case TOKEN_LOAD: ast = ParseLoad(parser); Match(parser, TOKEN_SEMICOLON); return ast; /** TODO: Factor common parts out of switch */ } FailWithUnexpectedToken(parser, parser->currentToken.type, TOKEN_ID); return NULL; }
static void ParseStatement (scontext_t owner) { if (StatementIndex == MAX_STATEMENT_DEPTH) { PR_ParseError("statement overflow"); } ContextHistory[StatementIndex++] = owner; if (TK_CHECK(TK_LBRACE)) { ContextLevel += EnterContext[owner]; do { ParseStatement(owner); } while (!TK_CHECK(TK_RBRACE)); ContextLevel -= EnterContext[owner]; StatementIndex--; return; } if (TK_CHECK(TK_SEMICOLON)) { StatementIndex--; return; } if (LX_CheckFetch("return")) { ParseReturn(); StatementIndex--; return; } if (LX_CheckFetch("loop")) { ParseLoop(); StatementIndex--; return; } if (LX_CheckFetch("while")) { ParseWhile(); StatementIndex--; return; } if (LX_CheckFetch("until")) { ParseUntil(); StatementIndex--; return; } if (LX_CheckFetch("do")) { ParseDo(); StatementIndex--; return; } if (LX_CheckFetch("switch")) { ParseSwitch(); StatementIndex--; return; } if (LX_CheckFetch("case")) { if (owner != SCONTEXT_SWITCH) { PR_ParseError("misplaced case"); } ParseCase(); StatementIndex--; return; } if (LX_CheckFetch("break")) { if (BreakAncestor() == false) { PR_ParseError("misplaced break"); } ParseBreak(); StatementIndex--; return; } if (LX_CheckFetch("continue")) { if (ContinueAncestor() == false) { PR_ParseError("misplaced continue"); } ParseContinue(); StatementIndex--; return; } if (LX_CheckFetch("default")) { ParseDefault(); StatementIndex--; return; } if (LX_CheckFetch("thinktime")) { ParseThinktime(); StatementIndex--; return; } if (LX_CheckFetch("local")) { ParseLocalDefs(); StatementIndex--; return; } if (LX_Check("float") || LX_Check("vector") || LX_Check("entity") || LX_Check("string") || LX_Check("void")) { ParseLocalDefs(); StatementIndex--; return; } if (LX_CheckFetch("if")) { ParseIf(); StatementIndex--; return; } EX_Expression(TOP_PRIORITY); LX_Require(";"); StatementIndex--; }
/* ParseStatement - parse a statement */ void ParseStatement(ParseContext *c, Token tkn) { /* dispatch on the statement keyword */ switch (tkn) { case T_REM: /* just a comment so ignore the rest of the line */ break; case T_DEF: ParseDef(c); break; case T_END_DEF: ParseEndDef(c); break; case T_DIM: ParseDim(c); break; case T_LET: ParseLet(c); break; case T_IF: ParseIf(c); break; case T_ELSE: ParseElse(c); break; case T_ELSE_IF: ParseElseIf(c); break; case T_END_IF: ParseEndIf(c); break; case T_END: ParseEnd(c); break; case T_FOR: ParseFor(c); break; case T_NEXT: ParseNext(c); break; case T_DO: ParseDo(c); break; case T_DO_WHILE: ParseDoWhile(c); break; case T_DO_UNTIL: ParseDoUntil(c); break; case T_LOOP: ParseLoop(c); break; case T_LOOP_WHILE: ParseLoopWhile(c); break; case T_LOOP_UNTIL: ParseLoopUntil(c); break; case T_STOP: ParseStop(c); break; case T_GOTO: ParseGoto(c); break; case T_RETURN: ParseReturn(c); break; case T_PRINT: ParsePrint(c); break; case T_IDENTIFIER: if (SkipSpaces(c) == ':') { DefineLabel(c, c->token, codeaddr(c)); break; } UngetC(c); default: SaveToken(c, tkn); ParseImpliedLetOrFunctionCall(c); break; } }