#include "repr.h"\r
\r
template<typename T>\r
-std::ostream & operator<< (std::ostream & out, const std::vector<T> & v) {\r
+std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)\r
+{\r
bool comma = false;\r
- for (auto t : v) {\r
+ for (auto t : v)\r
+ {\r
if (comma) out << ", ";\r
else comma = true;\r
out << t;\r
\r
static const int TAB_WIDTH = 2;\r
static int indentation = 0;\r
-static void indent(std::ostream & out, int change = 0) {\r
+static void indent(std::ostream & out, int change = 0)\r
+{\r
indentation += change;\r
out << std::string(indentation, ' ');\r
}\r
\r
-std::ostream & operator<< (std::ostream & out, const Type & t) {\r
+std::ostream & operator<< (std::ostream & out, const Type & t)\r
+{\r
out << t.name;\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const Variable & v) {\r
+std::ostream & operator<< (std::ostream & out, const Variable & v)\r
+{\r
out << v.type << " ";\r
\r
std::stringstream sstr;\r
std::string s = v.name;\r
\r
- for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++) {\r
- if (m->type == TypeModifierType::Pointer) {\r
+ for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\r
+ {\r
+ if (m->type == TypeModifierType::Pointer)\r
+ {\r
sstr.str(std::string());\r
sstr << "*(" << s << ")";\r
s = sstr.str();\r
}\r
- else {\r
+ else\r
+ {\r
sstr.str(std::string());\r
sstr << "(" << s << ")[";\r
if (m->_staticArray)\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const Body & b) {\r
+std::ostream & operator<< (std::ostream & out, const Body & b)\r
+{\r
indent(out);\r
out << "{\n";\r
indentation += 2;\r
\r
- for (auto v : b.variables) {\r
+ for (auto v : b.variables)\r
+ {\r
indent(out);\r
out << v << ";\n";\r
}\r
\r
out << "\n";\r
\r
- for (auto s : b.statements) {\r
+ for (auto s : b.statements)\r
+ {\r
indent(out);\r
out << s << "\n";\r
}\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o) {\r
- if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost) {\r
+std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o)\r
+{\r
+ if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost)\r
+ {\r
out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;\r
}\r
- else {\r
+ else\r
+ {\r
out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];\r
}\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o) {\r
+std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o)\r
+{\r
out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr;\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o) {\r
+std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o)\r
+{\r
out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const Expr & e) {\r
+std::ostream & operator<< (std::ostream & out, const Expr & e)\r
+{\r
if (e.parenthesized)\r
out << "(";\r
\r
- switch (e.type) {\r
+ switch (e.type)\r
+ {\r
case ExprType::Func:\r
out << e._func.functionName << "(" << e._func.arguments << ")"; break;\r
case ExprType::Lit:\r
\r
return out;\r
}\r
-std::ostream & operator<< (std::ostream & out, const Stmt & s) {\r
- switch (s.type) {\r
+std::ostream & operator<< (std::ostream & out, const Stmt & s)\r
+{\r
+ switch (s.type)\r
+ {\r
case StmtType::If:\r
out << "if (" << s._if.condition << ")\n" << s._if.body; break;\r
case StmtType::Switch:\r
out << "switch (" << s._switch.ident.name << ")\n{\n";\r
- for (auto c : s._switch.cases) {\r
+ for (auto c : s._switch.cases)\r
+ {\r
indent(out, 2);\r
out << "case " << *c.expr << ": " << c.body << "break;";\r
}\r
}\r
\r
\r
-void tocFunction (std::ostream & out, const Function & f, bool stub) {\r
+void tocFunction (std::ostream & out, const Function & f, bool stub)\r
+{\r
out << f.returnType << " " << f.name << " (" << f.parameters << ")";\r
\r
- if (stub) {\r
+ if (stub)\r
+ {\r
out << ";\n";\r
}\r
- else {\r
+ else\r
+ {\r
out << "\n" << f.body;\r
}\r
}\r
-void tocStruct (std::ostream & out, const Struct & s, bool stub) {\r
+void tocStruct (std::ostream & out, const Struct & s, bool stub)\r
+{\r
out << "struct " << s.name;\r
- if (stub) {\r
+ if (stub)\r
+ {\r
out << ";\n";\r
- for (auto m : s.methods) {\r
- m.parameters.insert(m.parameters.begin(), {"this", {s.name, {{TypeModifierType::Pointer, false, -1}}}});\r
+ for (auto m : s.methods)\r
+ {\r
+ m.parameters.insert(m.parameters.begin(),\r
+ {"this",\r
+ {s.name,\r
+ {{TypeModifierType::Pointer, false, -1}}}});\r
out << m.returnType << " " <<\r
s.name << "_" << m.name <<\r
" (" << m.parameters << ");\n";\r
out << "\n{\n";\r
indentation += 2;\r
\r
- for (auto m : s.members) {\r
+ for (auto m : s.members)\r
+ {\r
indent(out);\r
out << m << ";\n";\r
}\r
indent(out, -2);\r
out << "};\n";\r
\r
- for (auto m : s.methods) {\r
- m.parameters.insert(m.parameters.begin(), {"this", {s.name, {{TypeModifierType::Pointer, false, -1}}}}); \r
+ for (auto m : s.methods)\r
+ {\r
+ m.parameters.insert(m.parameters.begin(),\r
+ {"this",\r
+ {s.name,\r
+ {{TypeModifierType::Pointer, false, -1}}}}); \r
out << m.returnType << " " << s.name << "_" << m.name << " (" << m.parameters << ")\n" << m.body;\r
}\r
}\r
-void tocProgram (std::ostream & out, const Program & p) {\r
- for (auto s : p.structs) {\r
+void tocProgram (std::ostream & out, const Program & p)\r
+{\r
+ for (auto s : p.structs)\r
+ {\r
tocStruct(out, s, true);\r
}\r
- for (auto f : p.functions) {\r
+ for (auto f : p.functions)\r
+ {\r
tocFunction(out, f, true);\r
}\r
\r
- for (auto v : p.variables) {\r
+ for (auto v : p.variables)\r
+ {\r
out << v << ";\n";\r
}\r
- for (auto s : p.structs) {\r
+ for (auto s : p.structs)\r
+ {\r
tocStruct(out, s, false);\r
}\r
- for (auto f : p.functions) {\r
+ for (auto f : p.functions)\r
+ {\r
tocFunction(out, f, false);\r
}\r
}\r