]> git.tdb.fi Git - libs/gl.git/commitdiff
Make all components of a for loop optional
authorMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2019 21:52:33 +0000 (00:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2019 21:52:33 +0000 (00:52 +0300)
source/programcompiler.cpp
source/programparser.cpp
source/programsyntax.cpp

index a3691b112fd4fc22d8121f04c4d37024037f8470..88ec6e59d88054480457be91cd0da9e14f201f02 100644 (file)
@@ -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);
 }
 
index 3b87ea7b58b1a2a16c1a09d3cd6432c53009d73f..b607e1cfb8f063abfec2127b7c57247809677a81 100644 (file)
@@ -853,14 +853,19 @@ RefPtr<Iteration> ProgramParser::parse_iteration()
                loop->init_statement = parse_statement();
        else
        {
-               RefPtr<ExpressionStatement> expr = new ExpressionStatement;
-               expr->expression = parse_expression();
+               if(token!=";")
+               {
+                       RefPtr<ExpressionStatement> 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);
index 8cb276d9b6dcdef94e2afaccc7ba907ab580f675..47b59f6c8900868c21f91c633cf2c4b512da55e4 100644 (file)
@@ -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)