From: Mikko Rasa Date: Sat, 25 May 2019 21:52:33 +0000 (+0300) Subject: Make all components of a for loop optional X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9339ae44a89f295523b6c25681d87642767864c4;p=libs%2Fgl.git Make all components of a for loop optional --- 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)