Properly indent generated C++ code.
This commit is contained in:
parent
b0a01d2fb7
commit
25153e8a85
|
@ -28,15 +28,14 @@ using namespace monicelli;
|
||||||
|
|
||||||
static const std::string STATEMENT_TERMINATOR = ";\n";
|
static const std::string STATEMENT_TERMINATOR = ";\n";
|
||||||
static const std::string BLOCK = " ";
|
static const std::string BLOCK = " ";
|
||||||
static const int BLOCK_SIZE = 4;
|
|
||||||
|
|
||||||
|
|
||||||
void CppEmitter::indent() {
|
void CppEmitter::indent() {
|
||||||
indent_chars += BLOCK_SIZE;
|
indent_chars += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEmitter::dedent() {
|
void CppEmitter::dedent() {
|
||||||
indent_chars -= BLOCK_SIZE;
|
indent_chars -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEmitter::emitIndent() {
|
void CppEmitter::emitIndent() {
|
||||||
|
@ -75,6 +74,7 @@ void CppEmitter::emit(Program const& program) {
|
||||||
|
|
||||||
void CppEmitter::emitStatements(PointerList<Statement> const& node) {
|
void CppEmitter::emitStatements(PointerList<Statement> const& node) {
|
||||||
for (Statement *s: node) {
|
for (Statement *s: node) {
|
||||||
|
emitIndent();
|
||||||
s->emit(this);
|
s->emit(this);
|
||||||
stream << STATEMENT_TERMINATOR;
|
stream << STATEMENT_TERMINATOR;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,9 @@ void CppEmitter::emitStatements(PointerList<Statement> const& node) {
|
||||||
|
|
||||||
void CppEmitter::emit(Main const& main) {
|
void CppEmitter::emit(Main const& main) {
|
||||||
stream << "int main() {\n";
|
stream << "int main() {\n";
|
||||||
|
indent();
|
||||||
emitStatements(main.getBody());
|
emitStatements(main.getBody());
|
||||||
|
dedent();
|
||||||
stream << "}\n";
|
stream << "}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,6 +133,7 @@ void CppEmitter::emit(Input const& node) {
|
||||||
stream << "std::cout << \"";
|
stream << "std::cout << \"";
|
||||||
node.getVariable().emit(this);
|
node.getVariable().emit(this);
|
||||||
stream << "? \";\n";
|
stream << "? \";\n";
|
||||||
|
emitIndent();
|
||||||
stream << "std::cin >> ";
|
stream << "std::cin >> ";
|
||||||
node.getVariable().emit(this);
|
node.getVariable().emit(this);
|
||||||
}
|
}
|
||||||
|
@ -147,7 +150,10 @@ void CppEmitter::emit(Assert const& node) {
|
||||||
|
|
||||||
void CppEmitter::emit(Loop const& loop) {
|
void CppEmitter::emit(Loop const& loop) {
|
||||||
stream << "do {\n";
|
stream << "do {\n";
|
||||||
|
indent();
|
||||||
emitStatements(loop.getBody());
|
emitStatements(loop.getBody());
|
||||||
|
dedent();
|
||||||
|
emitIndent();
|
||||||
stream << "} while (";
|
stream << "} while (";
|
||||||
loop.getCondition().emit(this);
|
loop.getCondition().emit(this);
|
||||||
stream << ")";
|
stream << ")";
|
||||||
|
@ -156,7 +162,10 @@ void CppEmitter::emit(Loop const& loop) {
|
||||||
void CppEmitter::emit(BranchCase const& node) {
|
void CppEmitter::emit(BranchCase const& node) {
|
||||||
node.getCondition().emit(this);
|
node.getCondition().emit(this);
|
||||||
stream << ") {\n";
|
stream << ") {\n";
|
||||||
|
indent();
|
||||||
emitStatements(node.getBody());
|
emitStatements(node.getBody());
|
||||||
|
dedent();
|
||||||
|
emitIndent();
|
||||||
stream << "}";
|
stream << "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +192,10 @@ void CppEmitter::emit(Branch const& branch) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stream << " else {\n";
|
stream << " else {\n";
|
||||||
|
indent();
|
||||||
emitStatements(*body.getElse());
|
emitStatements(*body.getElse());
|
||||||
|
dedent();
|
||||||
|
emitIndent();
|
||||||
stream << "}";
|
stream << "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +227,9 @@ void CppEmitter::emit(FunctionCall const& funcall) {
|
||||||
void CppEmitter::emit(Function const& function) {
|
void CppEmitter::emit(Function const& function) {
|
||||||
emitFunctionSignature(function);
|
emitFunctionSignature(function);
|
||||||
stream << " {\n";
|
stream << " {\n";
|
||||||
|
indent();
|
||||||
emitStatements(function.getBody());
|
emitStatements(function.getBody());
|
||||||
|
dedent();
|
||||||
stream << "}\n\n";
|
stream << "}\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user