From 9339ae44a89f295523b6c25681d87642767864c4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 May 2019 00:52:33 +0300 Subject: [PATCH] Make all components of a for loop optional --- source/programcompiler.cpp | 22 ++++++++++++++++------ source/programparser.cpp | 15 ++++++++++----- source/programsyntax.cpp | 3 ++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index a3691b11..88ec6e59 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -544,12 +544,22 @@ void ProgramCompiler::Formatter::visit(Conditional &cond) void ProgramCompiler::Formatter::visit(Iteration &iter) { formatted += "for("; - iter.init_statement->visit(*this); - formatted += ' '; - iter.condition->visit(*this); - formatted += "; "; - iter.loop_expression->visit(*this); - formatted += ")\n"; + if(iter.init_statement) + iter.init_statement->visit(*this); + else + formatted += ';'; + if(iter.condition) + { + formatted += ' '; + iter.condition->visit(*this); + } + formatted += ';'; + if(iter.loop_expression) + { + formatted += ' '; + iter.loop_expression->visit(*this); + } + formatted += ')'; iter.body.visit(*this); } diff --git a/source/programparser.cpp b/source/programparser.cpp index 3b87ea7b..b607e1cf 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -853,14 +853,19 @@ RefPtr ProgramParser::parse_iteration() loop->init_statement = parse_statement(); else { - RefPtr expr = new ExpressionStatement; - expr->expression = parse_expression(); + if(token!=";") + { + RefPtr expr = new ExpressionStatement; + expr->expression = parse_expression(); + loop->init_statement = expr; + } expect(";"); - loop->init_statement = expr; } - loop->condition = parse_expression(); + if(peek_token()!=";") + loop->condition = parse_expression(); expect(";"); - loop->loop_expression = parse_expression(); + if(peek_token()!=")") + loop->loop_expression = parse_expression(); expect(")"); parse_block(loop->body, false); diff --git a/source/programsyntax.cpp b/source/programsyntax.cpp index 8cb276d9..47b59f6c 100644 --- a/source/programsyntax.cpp +++ b/source/programsyntax.cpp @@ -290,7 +290,8 @@ void TraversingVisitor::visit(Conditional &cond) void TraversingVisitor::visit(Iteration &iter) { - iter.init_statement->visit(*this); + if(iter.init_statement) + iter.init_statement->visit(*this); if(iter.condition) iter.condition->visit(*this); if(iter.loop_expression) -- 2.45.2