ProgramCompiler::Formatter::Formatter():
indent(0),
parameter_list(false),
- else_if(false)
+ else_if(0)
{ }
void ProgramCompiler::Formatter::visit(Literal &literal)
void ProgramCompiler::Formatter::visit(Block &block)
{
+ if(else_if)
+ --else_if;
+
+ unsigned brace_indent = indent;
if(block.use_braces)
- {
- if(else_if)
- {
- formatted += '\n';
- else_if = false;
- }
- formatted += format("%s{\n", string(indent*2, ' '));
- }
+ formatted += format("%s{\n", string(brace_indent*2, ' '));
- bool change_indent = (!formatted.empty() && !else_if);
- indent += change_indent;
+ SetForScope<unsigned> set(indent, indent+!formatted.empty());
string spaces(indent*2, ' ');
for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
if(i!=block.body.begin())
formatted += '\n';
- if(!else_if)
- formatted += spaces;
+ formatted += spaces;
(*i)->visit(*this);
+ else_if = 0;
}
- indent -= change_indent;
if(block.use_braces)
- formatted += format("\n%s}", string(indent*2, ' '));
+ formatted += format("\n%s}", string(brace_indent*2, ' '));
}
void ProgramCompiler::Formatter::visit(Layout &layout)
void ProgramCompiler::Formatter::visit(Conditional &cond)
{
if(else_if)
- {
- formatted += ' ';
- else_if = false;
- }
+ formatted.replace(formatted.rfind('\n'), string::npos, 1, ' ');
+
+ indent -= else_if;
formatted += "if(";
cond.condition->visit(*this);
cond.body.visit(*this);
if(!cond.else_body.body.empty())
{
- formatted += format("\n%selse", string(indent*2, ' '));
- SetFlag set(else_if);
+ formatted += format("\n%selse\n", string(indent*2, ' '));
+ SetForScope<unsigned> set(else_if, 2);
cond.else_body.visit(*this);
}
}