aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLefteris Karapetsas <lefteris@refu.co>2014-12-16 16:51:51 +0800
committerLefteris Karapetsas <lefteris@refu.co>2014-12-17 07:03:30 +0800
commit90e39c04d6e476491503573719c487744f529514 (patch)
tree0e307e5f4b3c5a85e4319b6167429bebed0ccbe4
parent3f2600bb31a5a71ce4ebb4c2fa95762d68eb516b (diff)
downloaddexon-solidity-90e39c04d6e476491503573719c487744f529514.tar
dexon-solidity-90e39c04d6e476491503573719c487744f529514.tar.gz
dexon-solidity-90e39c04d6e476491503573719c487744f529514.tar.bz2
dexon-solidity-90e39c04d6e476491503573719c487744f529514.tar.lz
dexon-solidity-90e39c04d6e476491503573719c487744f529514.tar.xz
dexon-solidity-90e39c04d6e476491503573719c487744f529514.tar.zst
dexon-solidity-90e39c04d6e476491503573719c487744f529514.zip
Solidity ForStatements expressions are now optional
-rw-r--r--AST_accept.h18
-rw-r--r--Parser.cpp17
-rw-r--r--grammar.txt2
3 files changed, 27 insertions, 10 deletions
diff --git a/AST_accept.h b/AST_accept.h
index ffef6f8b..0e5a71b6 100644
--- a/AST_accept.h
+++ b/AST_accept.h
@@ -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);
diff --git a/Parser.cpp b/Parser.cpp
index d936cfc7..598f5ad4 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -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,
diff --git a/grammar.txt b/grammar.txt
index 76e7bb6b..7c0ac3a5 100644
--- a/grammar.txt
+++ b/grammar.txt
@@ -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? ';'