[cond-expr] add an AST node

This commit is contained in:
Lu Guanqun 2015-12-22 16:44:51 +00:00
parent 67c855c583
commit 4fa0326813
3 changed files with 50 additions and 0 deletions

View File

@ -1119,6 +1119,33 @@ public:
ExpressionAnnotation& annotation() const override;
};
class Conditional: public Expression
{
public:
Conditional(
SourceLocation const& _location,
ASTPointer<Expression> const& _condition,
ASTPointer<Expression> const& _trueExpression,
ASTPointer<Expression> const& _falseExpression
):
Expression(_location),
m_condition(_condition),
m_trueExpression(_trueExpression),
m_falseExpression(_falseExpression)
{}
virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override;
Expression const& condition() const { return *m_condition; }
Expression const& trueExpression() const { return *m_trueExpression; }
Expression const& falseExpression() const { return *m_falseExpression; }
private:
ASTPointer<Expression> m_condition;
ASTPointer<Expression> m_trueExpression;
ASTPointer<Expression> m_falseExpression;
};
/// Assignment, can also be a compound assignment.
/// Examples: (a = 7 + 8) or (a *= 2)
class Assignment: public Expression

View File

@ -69,6 +69,7 @@ class Throw;
class VariableDeclarationStatement;
class ExpressionStatement;
class Expression;
class Conditional;
class Assignment;
class TupleExpression;
class UnaryOperation;

View File

@ -551,6 +551,28 @@ void VariableDeclarationStatement::accept(ASTConstVisitor& _visitor) const
_visitor.endVisit(*this);
}
void Conditional::accept(ASTVisitor& _visitor)
{
if (_visitor.visit(*this))
{
m_condition->accept(_visitor);
m_trueExpression->accept(_visitor);
m_falseExpression->accept(_visitor);
}
_visitor.endVisit(*this);
}
void Conditional::accept(ASTConstVisitor& _visitor) const
{
if (_visitor.visit(*this))
{
m_condition->accept(_visitor);
m_trueExpression->accept(_visitor);
m_falseExpression->accept(_visitor);
}
_visitor.endVisit(*this);
}
void Assignment::accept(ASTVisitor& _visitor)
{
if (_visitor.visit(*this))