+std::ostream & operator<< (std::ostream & out, const Body & b)\r
+{\r
+ b.ctx->parent = globalCtx;\r
+ globalCtx = b.ctx;\r
+\r
+ indent(out);\r
+ out << "{\n";\r
+ indentation += 2;\r
+\r
+ for (auto v : b.ctx->variables)\r
+ {\r
+ indent(out);\r
+ out << v << ";\n";\r
+ }\r
+\r
+ out << "\n";\r
+ \r
+ for (auto s : b.statements)\r
+ {\r
+ indent(out);\r
+ out << s << "\n";\r
+ }\r
+\r
+ indent(out, -2);\r
+ out << "}\n";\r
+\r
+ globalCtx = b.ctx->parent;\r
+\r
+ return out;\r
+}\r
+std::ostream & operator<< (std::ostream & out, const Expr & e)\r
+{\r
+ switch (e.type)\r
+ {\r
+ case ExprType::Func:\r
+ {\r
+ auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
+\r
+ if (std::get<0>(*f).defined)\r
+ out << vectorStr(std::get<1>(*f), "_", true);\r
+\r
+ out << e._func.functionName;\r
+ if (!e._func.genericInstantiation.empty())\r
+ out << genericAppendix(e._func.genericInstantiation);\r
+ out <<"(" << vectorStr(e._func.arguments, ", ") << ")"; break;\r
+ }\r
+ case ExprType::Method:\r
+ {\r
+ TypeInfo ti = typeExpr(globalCtx, *e._method.expr);\r
+ out <<\r
+ vectorStr(ti.type.namespacePrefixes, "_", true) <<\r
+ ti.type.name << genericAppendix(ti.type.genericInstantiation) << "_" << e._method.methodName;\r
+ if (!e._method.genericInstantiation.empty())\r
+ out << genericAppendix(e._method.genericInstantiation);\r
+ out << "(";\r
+ if (e._method.expr->type == ExprType::Identifier)\r
+ out << "&";\r
+ out << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") <<\r
+ vectorStr(e._method.arguments, ", ") << ")"; break;\r
+ }\r
+ case ExprType::Lit:\r
+ /**/ if (e._lit.type == LitType::Int) out << e._lit._int;\r
+ else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;\r
+ else if (e._lit.type == LitType::String) out << e._lit._string;\r
+ else if (e._lit.type == LitType::Bool) out << e._lit._bool;\r
+ break;\r
+ case ExprType::Paren:\r
+ out << "(" << *e._paren.expr << ")"; break;\r
+ case ExprType::Dot:\r
+ out << *e._dot.expr << (e._dot.isPointer ? "->" : ".") << e._dot.identifier; break;\r
+ case ExprType::PrefixOp:\r
+ out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;\r
+ case ExprType::PostfixOp:\r
+ out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;\r
+ case ExprType::BinaryOp:\r
+ out << *e._binaryOp.lexpr <<\r
+ " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<\r
+ *e._binaryOp.rexpr; break;\r
+ case ExprType::TernaryOp:\r
+ out << *e._ternaryOp.lexpr <<\r
+ " ? " << *e._ternaryOp.rexprTrue <<\r
+ " : " << *e._ternaryOp.rexprFalse; break;\r
+ case ExprType::Bracket:\r
+ out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
+ case ExprType::Identifier:\r
+ auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
+ if (v.has_value())\r
+ out << vectorStr(std::get<1>(*v), "_", true);\r
+ else\r
+ out << vectorStr(e._identifier.namespacePrefixes, "_", true);\r
+\r
+ out << e._identifier.identifier; break;\r
+ }\r
+\r
+ return out;\r
+}\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 << ")\n{\n";\r
+ for (auto c : s._switch.cases)\r
+ {\r
+ indent(out, 2);\r
+ out << "case " << *c.expr << ": " << c.body << "break;";\r
+ }\r
+ indent(out, -2);\r
+ out << "}\n";\r
+ break;\r
+ case StmtType::For:\r
+ out << "for (" <<\r
+ s._for.init << "; " <<\r
+ *s._for.condition << "; " <<\r
+ *s._for.action <<\r
+ ")\n" << s._for.body; break;\r
+ case StmtType::While:\r
+ out << "while (" << s._while.condition << ")\n" << s._while.body; break;\r
+ case StmtType::Assign:\r
+ out << s._assign.lexpr << " = " << s._assign.rexpr << ";"; break;\r
+ case StmtType::Return:\r
+ out << "return " << s._return.expr << ";"; break;\r
+ case StmtType::Expr:\r
+ out << s._expr << ";"; break;\r
+ }\r
+\r
+ return out;\r
+}\r
+\r
+\r
+void tocFunction (std::ostream & out, const Function & f, bool stub)\r
+{\r
+ if (!stub && !f.defined) return;\r
+\r
+ if (f.genericTypeNames.empty())\r
+ {\r
+ out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";\r
+\r
+ if (stub)\r
+ {\r
+ out << ";\n";\r
+ }\r
+ else\r
+ {\r
+ out << "\n" << f.body;\r