#pragma once\r
\r
#include <iostream>\r
+#include <sstream>\r
\r
-#include "TocParser.h"\r
-\r
-void toc(std::ostream & o, TocParser::ProgContext * ctx);\r
-void toc(std::ostream & o, TocParser::VarDeclContext * ctx);\r
-void toc(std::ostream & o, TocParser::FuncContext * ctx);\r
-void toc(std::ostream & o, TocParser::StructDeclContext * ctx);\r
-void toc(std::ostream & o, TocParser::BodyContext * ctx);\r
-void toc(std::ostream & o, TocParser::StmtContext * ctx);\r
-void toc(std::ostream & o, TocParser::IfCondContext * ctx);\r
-void toc(std::ostream & o, TocParser::WhileLoopContext * ctx);\r
-void toc(std::ostream & o, TocParser::AssignmentContext * ctx);\r
-void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx);\r
-void toc(std::ostream & o, TocParser::ExprContext * ctx);\r
-void toc(std::ostream & o, TocParser::NonOpExprContext * ctx);\r
-void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx);\r
-void toc(std::ostream & o, TocParser::FuncCallContext * ctx);\r
-void toc(std::ostream & o, TocParser::IdentifierContext * ctx);\r
-void toc(std::ostream & o, TocParser::LiteralContext * ctx);\r
-void toc(std::ostream & o, TocParser::SubscriptContext * ctx);\r
-void toc(std::ostream & o, TocParser::MemberAccessContext * ctx);\r
-void toc(std::ostream & o, TocParser::ParenExprContext * ctx);\r
-void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx);\r
-\r
-void toc_stub(std::ostream & o, TocParser::FuncContext * ctx);\r
-void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx);\r
-\r
-\r
-void toc(std::ostream & o, TocParser::ProgContext * ctx) {\r
- for (auto * decl : ctx->decl()) {\r
- /**/ if (decl->structDecl() != nullptr) toc_stub(o, decl->structDecl());\r
- else if (decl->funcDecl() != nullptr) toc_stub(o, decl->funcDecl()->func());\r
+#include "repr.h"\r
+#include "generic.h"\r
+#include "typeInfo.h"\r
+\r
+// print a generic vector with specified separator, optionally printing the separator at the end aswell\r
+template<typename T>\r
+std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)\r
+{\r
+ std::stringstream sstr;\r
+\r
+ bool putSeparator = false;\r
+ for (auto t : v)\r
+ {\r
+ if (putSeparator) sstr << separator;\r
+ else putSeparator = true;\r
+ sstr << t;\r
}\r
- for (auto * decl : ctx->decl()) {\r
- if (decl->varDecl() != nullptr) {\r
- toc(o, decl->varDecl());\r
- o << ";\n";\r
+ if (end && !v.empty())\r
+ sstr << separator;\r
+\r
+ return sstr.str();\r
+}\r
+\r
+std::ostream & operator<< (std::ostream & out, const Type & t);\r
+std::ostream & operator<< (std::ostream & out, const Variable & v);\r
+std::ostream & operator<< (std::ostream & out, const Body & b);\r
+std::ostream & operator<< (std::ostream & out, const Expr & e);\r
+std::ostream & operator<< (std::ostream & out, const Stmt & s);\r
+\r
+void tocFunction (std::ostream & out, const Function & f, bool stub);\r
+void tocStruct (std::ostream & out, const Struct & s, bool stub);\r
+void tocProgram (std::ostream & out, const Program & p);\r
+void tocNamespace (std::ostream & out, const Namespace & n, bool stub);\r
+\r
+static const int TAB_WIDTH = 2;\r
+static int indentation = 0;\r
+static void indent(std::ostream & out, int change = 0)\r
+{\r
+ indentation += change;\r
+ out << std::string(indentation, ' ');\r
+}\r
+\r
+static std::vector<std::string> namespaces;\r
+static std::string namespacePrefix() {\r
+ std::stringstream sstr;\r
+ for (auto n : namespaces)\r
+ {\r
+ sstr << n << "_";\r
+ }\r
+ return sstr.str();\r
+}\r
+\r
+// mapping from generic typenames (which are just names)\r
+// to actual instantiated types\r
+static std::map<std::string, Type> currentInstantiation;\r
+\r
+// set current context so that lookups can be made correctly\r
+static std::shared_ptr<Context> globalCtx;\r
+\r
+\r
+std::ostream & operator<< (std::ostream & out, const Type & t)\r
+{\r
+ // if the typename equals one of the current generic instantiations\r
+ // print instantiated type instead\r
+ for (auto kv : currentInstantiation)\r
+ {\r
+ if (t.name == kv.first)\r
+ {\r
+ out << kv.second;\r
+ return out;\r
+ }\r
+ }\r
+ TypeInfo ti = typeType(globalCtx, t);\r
+ if (ti.isStruct)\r
+ out << "struct ";\r
+ // try finding type in current context\r
+ auto s = findStruct(t.name, t.namespacePrefixes, globalCtx);\r
+ // print prefix for either found type or the specified \r
+ // prefix if type is not found (shouldn't happen)\r
+ if (s.has_value())\r
+ out << vectorStr(std::get<1>(*s), "_", true) << t.name; \r
+ else\r
+ out << vectorStr(t.namespacePrefixes, "_", true) << t.name;\r
+\r
+ // print generic appendix\r
+ if (!t.genericInstantiation.empty())\r
+ out << genericAppendix(t.genericInstantiation);\r
+\r
+ return out;\r
+}\r
+\r
+std::string generateModifiers (std::string s, std::vector<TypeModifier> modifiers)\r
+{\r
+ std::stringstream sstr;\r
+\r
+ // apply modifiers, inverted because C defines them\r
+ // the opposite direction\r
+ for (auto m = modifiers.rbegin(); m != 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
+ {\r
+ sstr.str(std::string());\r
+ sstr << "(" << s << ")[";\r
+ if (m->_staticArray)\r
+ sstr << m->_arraySize;\r
+ sstr << "]";\r
+ s = sstr.str();\r
}\r
- else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());\r
- else if (decl->funcDecl() != nullptr) toc(o, decl->funcDecl()->func());\r
}\r
+\r
+ return s;\r
}\r
-void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {\r
- o\r
- << ctx->var()->type()->getText()\r
- << " "\r
- << ctx->var()->varName()->getText();\r
+\r
+std::ostream & operator<< (std::ostream & out, const Variable & v)\r
+{\r
+ out << v.type << " ";\r
+\r
+ std::string s = v.name;\r
+ \r
+ // lookup variable and change name to reflect containing namespace\r
+ auto var = findVariable(v.name, namespaces, globalCtx);\r
+ if (var.has_value())\r
+ s = vectorStr(std::get<1>(*var), "_", true) + s;\r
+\r
+ // apply modifiers in C fashion\r
+ out << generateModifiers(s, v.type.modifiers);\r
+\r
+ return out;\r
+}\r
+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
- if (ctx->var()->expr() != nullptr) {\r
- o << " = ";\r
- toc(o, ctx->var()->expr());\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
+ // print function call\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
+ // get TypeInfo on the Expression that the method is called on\r
+ // then print method call\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
+ // try variable lookup\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
-void toc(std::ostream & o, TocParser::FuncContext * ctx) {\r
- o\r
- << ctx->type()->getText()\r
- << " "\r
- << ctx->funcName()->getText()\r
- << "(";\r
-\r
- if (ctx->parameter()->firstParameter() != nullptr) {\r
- o\r
- << ctx->parameter()->firstParameter()->var()->type()->getText()\r
- << " "\r
- << ctx->parameter()->firstParameter()->var()->varName()->getText();\r
-\r
- for (auto * par : ctx->parameter()->additionalParameter()) {\r
- o\r
- << ", "\r
- << par->var()->type()->getText()\r
- << " "\r
- << par->var()->varName()->getText();\r
+\r
+\r
+void tocFunction (std::ostream & out, const Function & f, bool stub)\r
+{\r
+ // for a function that is not defined, only the stub can be printed\r
+ if (!f.defined && !stub) return;\r
+\r
+ // regular function\r
+ if (f.genericTypeNames.empty())\r
+ {\r
+ out << f.returnType << " " << generateModifiers(namespacePrefix() + f.name, f.returnType.modifiers) << " (" << vectorStr(f.parameters, ", ") << ")";\r
+\r
+ if (stub)\r
+ {\r
+ out << ";\n";\r
+ }\r
+ else\r
+ {\r
+ out << "\n" << f.body;\r
}\r
}\r
+ // generic function\r
+ else\r
+ {\r
+ // print one instance per instantiation\r
+ for (auto instantiation : f.genericInstantiations)\r
+ {\r
+ // set global type mapping\r
+ for (int i = 0; i < f.genericTypeNames.size(); i++)\r
+ {\r
+ currentInstantiation[f.genericTypeNames[i]] = instantiation[i];\r
+ }\r
\r
- o << ")\n{\n";\r
+ out << f.returnType << " " <<\r
+ generateModifiers(namespacePrefix() + f.name + genericAppendix(instantiation), f.returnType.modifiers) <<\r
+ " (" << vectorStr(f.parameters, ", ") << ")";\r
\r
- toc(o, ctx->body());\r
+ if (stub)\r
+ {\r
+ out << ";\n";\r
+ }\r
+ else\r
+ {\r
+ out << "\n" << f.body;\r
+ }\r
\r
- o << "}\n";\r
+ currentInstantiation.clear();\r
+ }\r
+ }\r
}\r
-void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
- o\r
- << "typedef struct "\r
- << ctx->structName()->getText()\r
- << "\n{\n";\r
-\r
- for (auto * member : ctx->structMember()) {\r
- if (member->structVar() != nullptr) {\r
- o\r
- << member->structVar()->var()->type()->getText()\r
- << " "\r
- << member->structVar()->var()->varName()->getText()\r
- << ";\n";\r
+void tocStruct (std::ostream & out, const Struct & s, bool stub)\r
+{\r
+ // regular struct\r
+ if (s.genericTypeNames.empty())\r
+ {\r
+ out << "struct " << namespacePrefix() << s.name;\r
+ if (stub)\r
+ {\r
+ out << ";\n";\r
+ for (auto m : s.methods)\r
+ {\r
+ Function f = m;\r
+\r
+ // add implicit this parameter\r
+ f.parameters.insert(f.parameters.begin(),\r
+ {"this",\r
+ {\r
+ namespaces,\r
+ s.name,\r
+ {\r
+ {TypeModifierType::Pointer, false, -1}\r
+ }\r
+ }\r
+ });\r
+ out << f.returnType << " " <<\r
+ generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) <<\r
+ " (" << vectorStr(f.parameters, ", ") << ");\n";\r
+ }\r
+ return;\r
+ }\r
+ out << "\n{\n";\r
+ indentation += 2;\r
+\r
+ for (auto m : s.members)\r
+ {\r
+ indent(out);\r
+ out << m << ";\n";\r
+ }\r
+\r
+ indent(out, -2);\r
+ out << "};\n";\r
+ \r
+ for (auto m : s.methods)\r
+ {\r
+ Function f = m;\r
+ \r
+ // add implicit this parameter\r
+ f.parameters.insert(f.parameters.begin(),\r
+ {"this",\r
+ {\r
+ namespaces,\r
+ s.name,\r
+ {\r
+ {TypeModifierType::Pointer, false, -1}\r
+ }\r
+ }\r
+ });\r
+ out << f.returnType << " " <<\r
+ generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) <<\r
+ " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
}\r
}\r
- o << "} "\r
- << ctx->structName()->getText()\r
- << ";\n";\r
- for (auto * member : ctx->structMember()) {\r
- if (member->structMethod() != nullptr) {\r
- o\r
- << member->structMethod()->func()->type()->getText()\r
- << " "\r
- << ctx->structName()->getText()\r
- << "_"\r
- << member->structMethod()->func()->funcName()->getText()\r
- << "("\r
- << ctx->structName()->getText()\r
- << " * this";\r
-\r
- if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {\r
- o\r
- << ", "\r
- << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()\r
- << " "\r
- << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();\r
-\r
- for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {\r
- o\r
- << ", "\r
- << par->var()->type()->getText()\r
- << " "\r
- << par->var()->varName()->getText();\r
+ // generic struct\r
+ else\r
+ {\r
+ for (auto instantiation : s.genericInstantiations)\r
+ {\r
+ for (int i = 0; i < s.genericTypeNames.size(); i++)\r
+ {\r
+ currentInstantiation[s.genericTypeNames[i]] = instantiation[i];\r
+ }\r
+\r
+ out << "struct " << namespacePrefix() << s.name << genericAppendix(instantiation);\r
+ if (stub)\r
+ {\r
+ out << ";\n";\r
+ for (auto m : s.methods)\r
+ {\r
+ Function f = m;\r
+\r
+ // add implicit this parameter\r
+ f.parameters.insert(f.parameters.begin(),\r
+ {"this",\r
+ {\r
+ namespaces,\r
+ s.name + genericAppendix(instantiation),\r
+ {\r
+ {TypeModifierType::Pointer, false, -1}\r
+ }\r
+ }\r
+ });\r
+ out << f.returnType << " " <<\r
+ generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) <<\r
+ " (" << vectorStr(f.parameters, ", ") << ");\n";\r
}\r
+ return;\r
}\r
+ out << "\n{\n";\r
+ indentation += 2;\r
\r
- o << ")\n{\n";\r
+ for (auto m : s.members)\r
+ {\r
+ indent(out);\r
+ out << m << ";\n";\r
+ }\r
\r
- toc(o, member->structMethod()->func()->body());\r
+ indent(out, -2);\r
+ out << "};\n";\r
+ \r
+ for (auto m : s.methods)\r
+ {\r
+ Function f = m;\r
+ \r
+ // add implicit this parameter\r
+ f.parameters.insert(f.parameters.begin(),\r
+ {"this",\r
+ {\r
+ namespaces,\r
+ s.name + genericAppendix(instantiation),\r
+ {\r
+ {TypeModifierType::Pointer, false, -1}\r
+ }\r
+ }\r
+ });\r
+ out << f.returnType << " " <<\r
+ generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) <<\r
+ " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
+ }\r
\r
- o << "}\n";\r
+ currentInstantiation.clear();\r
}\r
}\r
}\r
-void toc(std::ostream & o, TocParser::BodyContext * ctx) {\r
- for (auto * stmt : ctx->stmt()) {\r
- toc(o, stmt);\r
- o << "\n";\r
+void tocProgram (std::ostream & out, const Program & p)\r
+{\r
+ globalCtx = p.ctx;\r
+\r
+ for (auto n : p.ctx->namespaces)\r
+ {\r
+ tocNamespace(out, n, true);\r
}\r
-}\r
-void toc(std::ostream & o, TocParser::StmtContext * ctx) {\r
- /**/ if (ctx->varDecl() != nullptr) toc(o, ctx->varDecl());\r
- else if (ctx->conditional() != nullptr) toc(o, ctx->conditional()->ifCond());\r
- else if (ctx->loop() != nullptr) toc(o, ctx->loop()->whileLoop());\r
- else if (ctx->assignment() != nullptr) toc(o, ctx->assignment());\r
- else if (ctx->returnStmt() != nullptr) toc(o, ctx->returnStmt());\r
- else if (ctx->expr() != nullptr) toc(o, ctx->expr());\r
-\r
- if (ctx->conditional() == nullptr && ctx->loop() == nullptr)\r
- o << ";";\r
-}\r
-void toc(std::ostream & o, TocParser::IfCondContext * ctx) {\r
- o << "if (";\r
- toc(o, ctx->expr());\r
- o << ")\n{\n";\r
- toc(o, ctx->body());\r
- o << "}\n";\r
-}\r
-void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {\r
- o << "while (";\r
- toc(o, ctx->expr());\r
- o << ")\n{\n";\r
- toc(o, ctx->body());\r
- o << "}\n";\r
-}\r
-void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {\r
- toc(o, ctx->identifier());\r
- o << " = ";\r
- toc(o, ctx->expr());\r
-}\r
-void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {\r
- o << "return ";\r
- toc(o, ctx->expr());\r
-}\r
-void toc(std::ostream & o, TocParser::ExprContext * ctx) {\r
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());\r
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());\r
- else if (ctx->literal() != nullptr) toc(o, ctx->literal());\r
- else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());\r
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());\r
- else if (ctx->operatorExpr() != nullptr) toc(o, ctx->operatorExpr()->binaryOperator());\r
-}\r
-void toc(std::ostream & o, TocParser::NonOpExprContext * ctx) {\r
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());\r
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());\r
- else if (ctx->literal() != nullptr) toc(o, ctx->literal());\r
- else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());\r
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());\r
-}\r
-void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx) {\r
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());\r
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());\r
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());\r
-}\r
-void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {\r
- o\r
- << ctx->funcName()->getText()\r
- << "(";\r
- for (int i = 0; i < ctx->expr().size(); i++) {\r
- if (i != 0) o << ", ";\r
- toc(o, ctx->expr(i));\r
+ out << "\n\n";\r
+ for (auto s : p.ctx->structs)\r
+ {\r
+ tocStruct(out, s, true);\r
}\r
- o << ")";\r
-}\r
-void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {\r
- o << ctx->getText();\r
-}\r
-void toc(std::ostream & o, TocParser::LiteralContext * ctx) {\r
- if (ctx->INTLIT() != nullptr) o << ctx->INTLIT()->getText();\r
-}\r
-void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {\r
- toc(o, ctx->nonSubscriptExpr());\r
- o << "[";\r
- toc(o, ctx->expr());\r
- o << "]";\r
-}\r
-void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {\r
- toc(o, ctx->identifier(0));\r
- o << ".";\r
- toc(o, ctx->identifier(1));\r
-}\r
-void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {\r
- o << "(";\r
- toc(o, ctx->expr());\r
- o << ")";\r
-}\r
-void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx) {\r
- for (int i = 0; i < ctx->BINARY_OPERATOR().size(); i++) {\r
- toc(o, ctx->nonOpExpr(i));\r
- o\r
- << " "\r
- << ctx->BINARY_OPERATOR(i)->getText()\r
- << " ";\r
- toc(o, ctx->nonOpExpr(i + 1));\r
+ out << "\n\n";\r
+ for (auto f : p.ctx->functions)\r
+ {\r
+ tocFunction(out, f, true);\r
}\r
+ out << "\n\n";\r
+\r
+ for (auto v : p.ctx->variables)\r
+ {\r
+ out << v << ";\n";\r
+ }\r
+ out << "\n\n";\r
+ for (auto n : p.ctx->namespaces)\r
+ {\r
+ tocNamespace(out, n, false);\r
+ }\r
+ out << "\n\n";\r
+ for (auto s : p.ctx->structs)\r
+ {\r
+ tocStruct(out, s, false);\r
+ }\r
+ out << "\n\n";\r
+ for (auto f : p.ctx->functions)\r
+ {\r
+ tocFunction(out, f, false);\r
+ }\r
+ out << "\n\n";\r
}\r
\r
-void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {\r
- o\r
- << ctx->type()->getText()\r
- << " "\r
- << ctx->funcName()->getText()\r
- << "(";\r
-\r
- if (ctx->parameter()->firstParameter() != nullptr) {\r
- o\r
- << ctx->parameter()->firstParameter()->var()->type()->getText()\r
- << " "\r
- << ctx->parameter()->firstParameter()->var()->varName()->getText();\r
-\r
- for (auto * par : ctx->parameter()->additionalParameter()) {\r
- o\r
- << ", "\r
- << par->var()->type()->getText()\r
- << " "\r
- << par->var()->varName()->getText();\r
+\r
+void tocNamespace (std::ostream & out, const Namespace & n, bool stub)\r
+{\r
+ n.ctx->parent = globalCtx;\r
+ globalCtx = n.ctx;\r
+\r
+ namespaces.push_back(n.name);\r
+ if (!stub)\r
+ {\r
+ for (auto v : n.ctx->variables)\r
+ {\r
+ out << v << ";\n";\r
}\r
+ out << "\n\n";\r
+ }\r
+ for (auto n : n.ctx->namespaces)\r
+ {\r
+ tocNamespace(out, n, stub);\r
+ out << "\n\n";\r
+ }\r
+ for (auto s : n.ctx->structs)\r
+ {\r
+ tocStruct(out, s, stub);\r
+ out << "\n\n";\r
}\r
+ for (auto f : n.ctx->functions)\r
+ {\r
+ tocFunction(out, f, stub);\r
+ out << "\n\n";\r
+ }\r
+ namespaces.pop_back();\r
\r
- o << ");\n";\r
-}\r
-void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
- o\r
- << "struct "\r
- << ctx->structName()->getText()\r
- << ";\n";\r
-}\r
+ globalCtx = n.ctx->parent;\r
+}
\ No newline at end of file