Skip to content

Commit

Permalink
Complete todo: convert 'for' into 'while'.
Browse files Browse the repository at this point in the history
It's not possible to enable this code because of the disaster it will be created for all the checks with the 'for' pattern.
  • Loading branch information
Edoardo Prezioso committed Oct 27, 2011
1 parent 4a14672 commit bab7402
Showing 1 changed file with 63 additions and 22 deletions.
85 changes: 63 additions & 22 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2282,36 +2282,77 @@ bool Tokenizer::tokenize(std::istream &code,
tok = tok->link();
continue;
}
if (!Token::Match(tok->previous(),"[{};] for (") || Token::simpleMatch(tok, "for ( ;"))
if (!Token::Match(tok->previous(),"[{};] for ("))
continue;
//find the two needed semicolons inside the 'for'
const Token *firstsemicolon = Token::findmatch(tok->next(), ";", tok->next()->link());
if (!firstsemicolon || !Token::findmatch(firstsemicolon->next(), ";", tok->next()->link()))
if (!firstsemicolon)
continue;
tok = tok->previous();
tok->insertToken(";");
tok->insertToken("{");
tok = tok->next();
Token *end = tok->tokAt(3)->link();
if (Token::simpleMatch(end, ") {")) {
end = end->next()->link();
end->insertToken("}");
Token::createMutualLinks(tok, end->next());
end = end->link()->previous();
const Token *secondsemicolon = Token::findmatch(firstsemicolon->next(), ";", tok->next()->link());
if (!secondsemicolon)
continue;
if (Token::findmatch(secondsemicolon->next(), ";", tok->next()->link()))
continue; //no more than two semicolons!
if (!tok->next()->link()->next())
continue; //there should be always something after 'for (...)'
Token *fortok = tok;
Token *begin = tok->tokAt(2);
Token *end = tok->next()->link();
if ( begin->str() != ";" ) {
tok = tok->previous();
tok->insertToken(";");
tok->insertToken("{");
tok = tok->next();
if (end->next()->str() =="{") {
end = end->next()->link();
end->insertToken("}");
Token::createMutualLinks(tok, end->next());
end = end->link()->previous();
} else {
if (end->next()->str() != ";")
end->insertToken(";");
end = end->next();
end->insertToken("}");
Token::createMutualLinks(tok, end->next());
}
end = firstsemicolon->previous();
Token::move(begin, end, tok);
tok = fortok;
end = fortok->next()->link();
}
//every 'for' is changed to 'for(;b;c), now it's possible to convert the 'for' to a 'while'.
//precisely, 'for(;b;c){code}'-> 'while(b){code + c;}'
fortok->str("while");
begin = firstsemicolon->previous();
begin->deleteNext();
begin = secondsemicolon->previous();
begin->deleteNext();
begin = begin->next();
if (begin->str() == ")") { //'for(;b;)' -> 'while(b)'
if (begin->previous()->str() == "(") //'for(;;)' -> 'while(true)'
begin->previous()->insertToken("true");
tok = fortok;
continue;
}
if (end->next()->str() =="{") {
tok = end->next()->link()->previous();
tok->insertToken(";");
} else {
end->insertToken("}");
Token::createMutualLinks(tok, end->next());
tok = end;
if (end->next()->str() != ";")
tok->insertToken(";");
tok->insertToken("{");
tok = tok->tokAt(2);
tok->insertToken("}");
Token::createMutualLinks(tok->previous(), tok->next());
tok = tok->previous();
}
Token *begin = tok->tokAt(4);
end = firstsemicolon->previous();
end = end->previous();
Token::move(begin, end, tok);
}*/
/**
* @todo simplify "for"
* - try to change "for" loop to a "while" loop instead
*/
tok = fortok;
}*/

simplifyConst();

Expand Down

0 comments on commit bab7402

Please sign in to comment.