AST: generate tree from bottom and upwards

This commit is contained in:
Daniel Marjamäki 2012-12-16 11:48:19 +01:00
parent aad3a041ad
commit bca7927913
3 changed files with 22 additions and 31 deletions

View File

@ -1051,33 +1051,20 @@ std::string Token::stringifyList(bool varid) const
void Token::astOperand1(Token *tok)
{
// unary operand -> goto parent operator
while (tok->_astParent && !tok->_astParent->_astOperand2)
// goto parent operator
while (tok->_astParent)
tok = tok->_astParent;
// there is a parent.. relink
if (tok->_astParent) {
tok->_astParent->_astOperand2 = this;
_astParent = tok->_astParent;
}
tok->_astParent = this;
_astOperand1 = tok;
}
void Token::astOperand2(Token *tok)
{
// unary operand -> goto parent operator
while (tok->_astParent && !tok->_astParent->_astOperand2)
// goto parent operator
while (tok->_astParent)
tok = tok->_astParent;
// there is a parent.. relink
if (tok->_astParent) {
tok->_astParent->_astOperand1 = this;
_astParent = tok->_astParent;
}
_astOperand2 = tok;
tok->_astParent = this;
_astOperand2 = tok;
}
void Token::astFunctionCall()

View File

@ -343,20 +343,23 @@ void TokenList::createAst()
{
// operators that must be ordered according to C-precedence
const char * const operators[] = {
" , "
" :: ",
" [ . ++ -- ",
"> ++ -- + - ! ~ * & ", // prefix unary operators, from right to left
" || ",
" && ",
" | ",
" ^ ",
" & ",
" == != ",
" < <= > >= ",
" << >> ",
" ++ -- . ",
"> ++ -- + - ! ~ * & sizeof ", // prefix unary operators, from right to left
" * / % ",
" + - ",
" * / % "
" << >> ",
" < <= > >= ",
" == != ",
" & ",
" ^ ",
" | ",
" && ",
" || ",
" = ? : ",
" throw ",
" , "
" [ "
};
for (unsigned int i = 0; i < sizeof(operators) / sizeof(*operators); ++i) {

View File

@ -7645,10 +7645,11 @@ private:
}
void astexpr() { // simple expressions with arithmetical ops
ASSERT_EQUALS("123++", testAst("1+2+3"));
ASSERT_EQUALS("12+3+", testAst("1+2+3"));
ASSERT_EQUALS("12*3+", testAst("1*2+3"));
ASSERT_EQUALS("123*+", testAst("1+2*3"));
ASSERT_EQUALS("12*34*+", testAst("1*2+3*4"));
ASSERT_EQUALS("12*34*5*+", testAst("1*2+3*4*5"));
}
void astpar() { // parentheses