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(); }
Statement *Statement::ParseIf() { Statement *snp; dfs.puts("<ParseIf>"); NextToken(); if (lastst == kw_firstcall) return (ParseFirstcall()); currentFn->UsesPredicate = TRUE; snp = NewStatement(st_if, FALSE); snp->predreg = iflevel; iflevel++; if ((iflevel > maxPn-1) && isThor) error(ERR_OUTOFPREDS); if( lastst != openpa ) error(ERR_EXPREXPECT); else { NextToken(); if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); if (lastst == semicolon) { NextToken(); snp->prediction = (GetIntegerExpression(NULL) & 1) | 2; } needpunc( closepa,19 ); if (lastst==kw_then) NextToken(); snp->s1 = Statement::Parse(); if (snp->s1) snp->s1->outer = snp; if( lastst == kw_else ) { NextToken(); snp->s2 = Statement::Parse(); if (snp->s2) snp->s2->outer = snp; } else if (lastst == kw_elsif) { snp->s2 = ParseIf(); if (snp->s2) snp->s2->outer = snp; } else snp->s2 = 0; } iflevel--; dfs.puts("</ParseIf>"); return snp; }
NodeStatement* Parser::ParseElement() { if (t.GetValue() == "begin") return ParseBlock(); if (t.GetValue() == "if") return ParseIf(); if (t.GetValue() == "while") return ParseWhile(); if (t.GetValue() == "repeat") return ParseRepeat(); if (t.GetValue() == "for") return ParseFor(); if (t.GetValue() == "write") return ParseWrite(true, false); if (t.GetValue() == "writeln") return ParseWrite(true, true); if (t.GetValue() == "exit") { t = sc.GetNextToken(); return new StatementExit(true); } if (t.GetType() == identifier) { bool found = false; _Table::iterator it = TableStack.Find(t.GetValue(), found); if (found) { if (it->second->IsProc() || it->second->IsFunc()) { t = sc.GetNextToken(); return ParseSub((SymProc*)it->second); } } return ParseAssignment(); } if (t.GetValue() == "(") { return ParseAssignment(); } throw Error("Statement error", t); }
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--; }
void Preprocessor::RecursivePreprocess( std::string filename, FileLoader& file_source, LexemList& lexems, DefineTable& define_table ) { unsigned int start_line = CurrentLine; LinesThisFile = 0; CurrentFile = filename; SetFileMacro( define_table, CurrentFile ); SetLineMacro( define_table, LinesThisFile ); // Path formatting must be done in main application std::string CurrentFileRoot = RootPath + CurrentFile; if( std::find( FilesPreprocessed.begin(), FilesPreprocessed.end(), CurrentFileRoot ) == FilesPreprocessed.end() ) FilesPreprocessed.push_back( CurrentFileRoot ); std::vector<char> data; bool loaded = file_source.LoadFile( RootPath, filename, data ); if( !loaded ) { PrintErrorMessage( std::string( "Could not open file " ) + RootPath + filename ); return; } if( data.size() == 0 ) return; char* d_end = &data[data.size() - 1]; ++d_end; Lex( &data[0], d_end, lexems ); LexemList::iterator itr = lexems.begin(); LexemList::iterator end = lexems.end(); LLITR old = end; while( itr != end ) { if( itr->Type == Lexem::NEWLINE ) { if( itr != old ) { CurrentLine++; LinesThisFile++; SetLineMacro( define_table, LinesThisFile ); } old = itr; ++itr; } else if( itr->Type == Lexem::PREPROCESSOR ) { LLITR start_of_line = itr; LLITR end_of_line = ParsePreprocessor( lexems, itr, end ); LexemList directive( start_of_line, end_of_line ); if( SkipPragmas && directive.begin()->Value == "#pragma" ) { itr = end_of_line; Lexem wspace; wspace.Type = Lexem::WHITESPACE; wspace.Value = " "; for( LLITR it = start_of_line; it != end_of_line;) { ++it; it = lexems.insert( it, wspace ); ++it; } continue; } itr = lexems.erase( start_of_line, end_of_line ); std::string value = directive.begin()->Value; if( value == "#define" ) { ParseDefine( define_table, directive ); } else if( value == "#ifdef" ) { std::string def_name; ParseIf( directive, def_name ); DefineTable::iterator dti = define_table.find( def_name ); if( dti == define_table.end() ) { LLITR splice_to = ParseIfDef( itr, end ); itr = lexems.erase( itr, splice_to ); } } else if( value == "#ifndef" ) { std::string def_name; ParseIf( directive, def_name ); DefineTable::iterator dti = define_table.find( def_name ); if( dti != define_table.end() ) { LLITR splice_to = ParseIfDef( itr, end ); itr = lexems.erase( itr, splice_to ); } } else if( value == "#if" ) { bool satisfied = EvaluateExpression( define_table, directive ) != 0; if( !satisfied ) { LLITR splice_to = ParseIfDef( itr, end ); itr = lexems.erase( itr, splice_to ); } } else if( value == "#endif" ) { // ignore } else if( value == "#include" ) { if( LNT ) LNT->AddLineRange( PrependRootPath( filename ), start_line, CurrentLine - LinesThisFile ); unsigned int save_lines_this_file = LinesThisFile; std::string file_name; ParseIf( directive, file_name ); std::string file_name_ = RemoveQuotes( file_name ); if( IncludeTranslator ) IncludeTranslator->Call( file_name_ ); if( std::find( FileDependencies.begin(), FileDependencies.end(), file_name_ ) == FileDependencies.end() ) FileDependencies.push_back( file_name_ ); LexemList next_file; RecursivePreprocess( AddPaths( filename, file_name_ ), file_source, next_file, define_table ); lexems.splice( itr, next_file ); start_line = CurrentLine; LinesThisFile = save_lines_this_file; CurrentFile = filename; SetFileMacro( define_table, CurrentFile ); SetLineMacro( define_table, LinesThisFile ); } else if( value == "#pragma" ) { ParsePragma( directive ); } else if( value == "#message" ) { std::string message; ParseTextLine( directive, message ); PrintMessage( message ); } else if( value == "#warning" ) { std::string warning; ParseTextLine( directive, warning ); PrintWarningMessage( warning ); } else if( value == "#error" ) { std::string error; ParseTextLine( directive, error ); PrintErrorMessage( error ); } else { PrintErrorMessage( "Unknown directive '" + value + "'." ); } } else if( itr->Type == Lexem::IDENTIFIER ) { itr = ExpandDefine( itr, end, lexems, define_table ); } else { ++itr; } } if( LNT ) LNT->AddLineRange( PrependRootPath( filename ), start_line, CurrentLine - LinesThisFile ); }
/* 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; } }
ExprNode* Parser :: ParseStatement(){ Token *token = scan.Get(); ExprNode* Statement; if(token->Value == "if") return ParseIf(); if(token->Value == "for"){ if (isCanUseBreak){ CycleStatement = ParseFor(); Statement = CycleStatement; } else { isCanUseBreak = true; CycleStatement = ParseFor(); Statement = CycleStatement; isCanUseBreak = false; } return Statement; } if(token->Value == "while"){ if (isCanUseBreak){ CycleStatement = ParseWhile(); Statement = CycleStatement; } else { isCanUseBreak = true; CycleStatement = ParseWhile(); Statement = CycleStatement; CycleStatement = nullptr; isCanUseBreak = false; } return Statement; } if(token->Value == "do"){ if (CycleStatement){ CycleStatement = ParseDoWhile(); Statement = CycleStatement; } else { isCanUseBreak = true; CycleStatement = ParseDoWhile(); Statement = CycleStatement; CycleStatement = nullptr; isCanUseBreak = false; } return Statement; } // if(isEq(token, _SEPARATION, "(")) // return ParseBlock(); if(token->Value == "return" || token->Value == "break" || token->Value == "continue") return ParseJumpStatement(); else return ParseExpr(); int *a ; int s = a - a; s++; }