Solidity ForStatements expressions are now optional

This commit is contained in:
Lefteris Karapetsas 2014-12-16 09:51:51 +01:00
parent 3f2600bb31
commit 90e39c04d6
3 changed files with 27 additions and 10 deletions

View File

@ -271,9 +271,12 @@ void ForStatement::accept(ASTVisitor& _visitor)
{
if (_visitor.visit(*this))
{
m_initExpression->accept(_visitor);
m_condExpression->accept(_visitor);
m_loopExpression->accept(_visitor);
if (m_initExpression)
m_initExpression->accept(_visitor);
if (m_condExpression)
m_condExpression->accept(_visitor);
if (m_loopExpression)
m_loopExpression->accept(_visitor);
m_body->accept(_visitor);
}
_visitor.endVisit(*this);
@ -283,9 +286,12 @@ void ForStatement::accept(ASTConstVisitor& _visitor) const
{
if (_visitor.visit(*this))
{
m_initExpression->accept(_visitor);
m_condExpression->accept(_visitor);
m_loopExpression->accept(_visitor);
if (m_initExpression)
m_initExpression->accept(_visitor);
if (m_condExpression)
m_condExpression->accept(_visitor);
if (m_loopExpression)
m_loopExpression->accept(_visitor);
m_body->accept(_visitor);
}
_visitor.endVisit(*this);

View File

@ -374,14 +374,25 @@ ASTPointer<WhileStatement> Parser::parseWhileStatement()
ASTPointer<ForStatement> Parser::parseForStatement()
{
ASTNodeFactory nodeFactory(*this);
ASTPointer<ASTNode> initExpression;
ASTPointer<Expression> conditionExpression;
ASTPointer<ExpressionStatement> loopExpression;
expectToken(Token::FOR);
expectToken(Token::LPAREN);
ASTPointer<ASTNode> initExpression = parseVardefOrExprstatement();
// LTODO: Maybe here have some predicate like peekExpression() instead of checking for semicolon and RPAREN?
if (m_scanner->getCurrentToken() != Token::SEMICOLON)
initExpression = parseVardefOrExprstatement();
expectToken(Token::SEMICOLON);
ASTPointer<Expression> conditionExpression = parseExpression();
if (m_scanner->getCurrentToken() != Token::SEMICOLON)
conditionExpression = parseExpression();
expectToken(Token::SEMICOLON);
ASTPointer<ExpressionStatement> loopExpression = parseExpressionStatement();
if (m_scanner->getCurrentToken() != Token::RPAREN)
loopExpression = parseExpressionStatement();
expectToken(Token::RPAREN);
ASTPointer<Statement> body = parseStatement();
nodeFactory.setEndPositionFromNode(body);
return nodeFactory.createNode<ForStatement>(initExpression,

View File

@ -20,7 +20,7 @@ Statement = IfStatement | WhileStatement | Block |
IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
WhileStatement = 'while' '(' Expression ')' Statement
ForStatement = 'for' '(' VardefOrExpressionstatement ';' Expression ';' Expressionstatement ')' Statement
ForStatement = 'for' '(' (VardefOrExpressionstatement)? ';' (Expression)? ';' (Expressionstatement)? ')' Statement
Continue = 'continue' ';'
Break = 'break' ';'
Return = 'return' Expression? ';'