Skip to content

Commit

Permalink
Add Expression::extractLast
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Apr 17, 2014
1 parent 6385a7b commit b7f67fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/expression.c
Expand Up @@ -1994,6 +1994,9 @@ int Expression::rvalue(bool allowVoid)
return 1;
}

/**********************************
* Combine e1 and e2 by CommaExp if both are not NULL.
*/
Expression *Expression::combine(Expression *e1, Expression *e2)
{
if (e1)
Expand All @@ -2009,6 +2012,35 @@ Expression *Expression::combine(Expression *e1, Expression *e2)
return e1;
}

/**********************************
* If 'e' is a tree of commas, returns the leftmost expression
* by stripping off it from the tree. The remained part of the tree
* is returned via *pe0.
* Otherwise 'e' is directly returned and *pe0 is set to NULL.
*/
Expression *Expression::extractLast(Expression *e, Expression **pe0)
{
if (e->op == TOKcomma)
{
CommaExp *ce = (CommaExp *)e;
*pe0 = ce;

Expression **pe = &e;
while (((CommaExp *)(*pe))->e2->op == TOKcomma)
{
ce = (CommaExp *)(*pe);
pe = &ce->e2;
}

*pe = ce->e2;
if (pe == &e)
*pe0 = ce->e1;
}
else
*pe0 = NULL;
return e;
}

dinteger_t Expression::toInteger()
{
//printf("Expression %s\n", Token::toChars(op));
Expand Down
1 change: 1 addition & 0 deletions src/expression.h
Expand Up @@ -165,6 +165,7 @@ class Expression : public RootObject
// this new expression does not necessarily need to have valid D source code representation,
// for example, it may include declaration expressions
static Expression *combine(Expression *e1, Expression *e2);
static Expression *extractLast(Expression *e, Expression **pe0);
static Expressions *arraySyntaxCopy(Expressions *exps);

virtual dinteger_t toInteger();
Expand Down

0 comments on commit b7f67fe

Please sign in to comment.