void ProgramCompiler::Formatter::visit(Iteration &iter)
{
- formatted += "for(";
- if(iter.init_statement)
- iter.init_statement->visit(*this);
- else
- formatted += ';';
- if(iter.condition)
+ if(!iter.init_statement && iter.condition && !iter.loop_expression)
{
- formatted += ' ';
+ formatted += "while(";
iter.condition->visit(*this);
+ formatted += ')';
}
- formatted += ';';
- if(iter.loop_expression)
+ else
{
- formatted += ' ';
- iter.loop_expression->visit(*this);
+ formatted += "for(";
+ 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 += ')';
}
- formatted += ')';
if(iter.body.body.empty())
formatted += " { }";
if(token=="if")
return parse_conditional();
else if(token=="for")
- return parse_iteration();
+ return parse_for();
+ else if(token=="while")
+ return parse_while();
else if(token=="passthrough")
return parse_passthrough();
else if(token=="return")
return cond;
}
-RefPtr<Iteration> ProgramParser::parse_iteration()
+RefPtr<Iteration> ProgramParser::parse_for()
{
expect("for");
expect("(");
return loop;
}
+RefPtr<Iteration> ProgramParser::parse_while()
+{
+ expect("while");
+ expect("(");
+ RefPtr<Iteration> loop = new Iteration;
+ loop->condition = parse_expression();
+ expect(")");
+
+ parse_block(loop->body, false);
+
+ return loop;
+}
+
RefPtr<Passthrough> ProgramParser::parse_passthrough()
{
expect("passthrough");
RefPtr<ProgramSyntax::FunctionDeclaration> parse_function_declaration();
RefPtr<ProgramSyntax::InterfaceBlock> parse_interface_block();
RefPtr<ProgramSyntax::Conditional> parse_conditional();
- RefPtr<ProgramSyntax::Iteration> parse_iteration();
+ RefPtr<ProgramSyntax::Iteration> parse_for();
+ RefPtr<ProgramSyntax::Iteration> parse_while();
RefPtr<ProgramSyntax::Passthrough> parse_passthrough();
RefPtr<ProgramSyntax::Return> parse_return();
};