]> gitweb.ps.run Git - toc/blobdiff - src/toc.h
complete grammar
[toc] / src / toc.h
index b36d878cc580c33d673e1ed0410b4b13de83664c..67f92dc0753648093a1f3b156c48cca6121b1c93 100644 (file)
--- a/src/toc.h
+++ b/src/toc.h
 \r
 #include <iostream>\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
-  }\r
-  for (auto * decl : ctx->decl()) {\r
-    if (decl->varDecl()    != nullptr) {\r
-      toc(o, decl->varDecl());\r
-      o << ";\n";\r
-    }\r
-    else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());\r
-    else if (decl->funcDecl()   != nullptr) toc(o, decl->funcDecl()->func());\r
+#include "repr.h"\r
+\r
+template<typename T>\r
+std::ostream & operator<< (std::ostream & out, const std::vector<T> & v) {\r
+  bool comma = false;\r
+  for (auto t : v) {\r
+    if (comma) out << ", ";\r
+    else comma = true;\r
+    out << t;\r
   }\r
+  return out;\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
-  if (ctx->var()->expr() != nullptr) {\r
-    o << " = ";\r
-    toc(o, ctx->var()->expr());\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 UnaryOperatorExpr & o);\r
+std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o);\r
+std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o);\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
+\r
+static const int TAB_WIDTH = 2;\r
+static int indentation = 0;\r
+static void indent(std::ostream & out, int change = 0) {\r
+  indentation += change;\r
+  out << std::string(indentation, ' ');\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
 \r
-  o << ")\n{\n";\r
+std::ostream & operator<< (std::ostream & out, const Type & t) {\r
+  out << t.name;\r
 \r
-  toc(o, ctx->body());\r
+  return out;\r
+}\r
+std::ostream & operator<< (std::ostream & out, const Variable & v) {\r
+  out << v.type << " " << v.name;\r
 \r
-  o << "}\n";\r
+  return out;\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
-    }\r
+std::ostream & operator<< (std::ostream & out, const Body & b) {\r
+  indent(out);\r
+  out << "{\n";\r
+  indentation += 2;\r
+\r
+  for (auto v : b.variables) {\r
+    indent(out);\r
+    out << v << ";\n";\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
-        }\r
-      }\r
-\r
-      o << ")\n{\n";\r
-\r
-      toc(o, member->structMethod()->func()->body());\r
-\r
-      o << "}\n";\r
-    }\r
+\r
+  out << "\n";\r
+  \r
+  for (auto s : b.statements) {\r
+    indent(out);\r
+    out << s << ";\n";\r
   }\r
+\r
+  indent(out, -2);\r
+  out << "}\n";\r
+\r
+  return out;\r
 }\r
-void toc(std::ostream & o, TocParser::BodyContext * ctx) {\r
-  for (auto * stmt : ctx->stmt()) {\r
-    toc(o, stmt);\r
-    o << "\n";\r
+std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o) {\r
+  if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost) {\r
+    out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;\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
+  else {\r
+    out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];\r
   }\r
-  o << ")";\r
-}\r
-void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {\r
-  o << ctx->getText();\r
+\r
+  return out;\r
 }\r
-void toc(std::ostream & o, TocParser::LiteralContext * ctx) {\r
-  if (ctx->INTLIT() != nullptr) o << ctx->INTLIT()->getText();\r
+std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o) {\r
+  out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr;\r
+\r
+  return out;\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
+std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o) {\r
+  out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;\r
+\r
+  return out;\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
+std::ostream & operator<< (std::ostream & out, const Expr & e) {\r
+  switch (e.type) {\r
+  case ExprType::Brackets:\r
+    out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
+  case ExprType::Call:\r
+    out << e._call.functionName << "(" << e._call.arguments << ")"; break;\r
+  case ExprType::Dot:\r
+    out << e._dot.name << "." << *e._dot.lexpr; break;\r
+  case ExprType::Literal:\r
+    out << e._literal.i; break;\r
+  case ExprType::Operator:\r
+    out << e._operator; break;\r
+  case ExprType::Variable:\r
+    out << e._variable.name; break;\r
+  }\r
+\r
+  return out;\r
 }\r
-void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {\r
-  o << "(";\r
-  toc(o, ctx->expr());\r
-  o << ")";\r
+std::ostream & operator<< (std::ostream & out, const Stmt & s) {\r
+  switch (s.type) {\r
+  case StmtType::Assign:\r
+    out << s._assign.lexpr << "=" << s._assign.rexpr; break;\r
+   case StmtType::Expr:\r
+     out << s._expr; break;\r
+  case StmtType::If:\r
+    out << "if (" << s._if.condition << ")\n" << s._if.body; break;\r
+  case StmtType::Return:\r
+    out << "return " << s._return.expr; break;\r
+  case StmtType::While:\r
+    out << "while (" << s._while.condition << ")\n" << s._while.body; break;\r
+  }\r
+\r
+  return out;\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
+\r
+\r
+void tocFunction (std::ostream & out, const Function & f, bool stub) {\r
+  out << f.returnType << " " << f.name << " (";\r
+\r
+  bool comma = false;\r
+  for (auto p : f.parameters) {\r
+    if (comma) out << ", ";\r
+    else comma = true;\r
+\r
+    out << p.type << " " << p.name;\r
+  }\r
+\r
+  out << ")";\r
+\r
+  if (stub) {\r
+    out << ";\n";\r
+  }\r
+  else {\r
+    out << "\n" << f.body;\r
   }\r
 }\r
+void tocStruct (std::ostream & out, const Struct & s, bool stub) {\r
+  out << "struct " << s.name;\r
+  if (stub) {\r
+    out << ";\n";\r
+    return;\r
+  }\r
+  out << "\n{\n";\r
+  indentation += 2;\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
+  for (auto m : s.members) {\r
+    indent(out);\r
+    out << m << ";\n";\r
   }\r
 \r
-  o << ");\n";\r
+  indent(out, -2);\r
+  out << "};\n";\r
 }\r
-void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
-  o\r
-    << "struct "\r
-    << ctx->structName()->getText()\r
-    << ";\n";\r
+void tocProgram (std::ostream & out, const Program & p) {\r
+  for (auto s : p.structs) {\r
+    tocStruct(out, s, true);\r
+  }\r
+  for (auto s : p.structs) {\r
+    tocStruct(out, s, false);\r
+  }\r
+\r
+  for (auto v : p.variables) {\r
+    out << v << ";\n";\r
+  }\r
+\r
+  for (auto f : p.functions) {\r
+    tocFunction(out, f, true);\r
+  }\r
+  for (auto f : p.functions) {\r
+    tocFunction(out, f, false);\r
+  }\r
 }\r
+\r
+\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
+//   }\r
+//   for (auto * decl : ctx->decl()) {\r
+//     if (decl->varDecl()    != nullptr) {\r
+//       toc(o, decl->varDecl());\r
+//       out << ";\n";\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
+// void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {\r
+//   o\r
+//     << ctx->var()->type()->getText()\r
+//     << " "\r
+//     << ctx->var()->varName()->getText();\r
+  \r
+//   if (ctx->var()->expr() != nullptr) {\r
+//     out << " = ";\r
+//     toc(o, ctx->var()->expr());\r
+//   }\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
+\r
+//   out << ")\n{\n";\r
+\r
+//   toc(o, ctx->body());\r
+\r
+//   out << "}\n";\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
+//     }\r
+//   }\r
+//   out << "} "\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
+//         }\r
+//       }\r
+\r
+//       out << ")\n{\n";\r
+\r
+//       toc(o, member->structMethod()->func()->body());\r
+\r
+//       out << "}\n";\r
+//     }\r
+//   }\r
+// }\r
+// void toc(std::ostream & o, TocParser::BodyContext * ctx) {\r
+//   for (auto * stmt : ctx->stmt()) {\r
+//     toc(o, stmt);\r
+//     out << "\n";\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
+//     out << ";";\r
+// }\r
+// void toc(std::ostream & o, TocParser::IfCondContext * ctx) {\r
+//   out << "if (";\r
+//   toc(o, ctx->expr());\r
+//   out << ")\n{\n";\r
+//   toc(o, ctx->body());\r
+//   out << "}\n";\r
+// }\r
+// void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {\r
+//   out << "while (";\r
+//   toc(o, ctx->expr());\r
+//   out << ")\n{\n";\r
+//   toc(o, ctx->body());\r
+//   out << "}\n";\r
+// }\r
+// void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {\r
+//   toc(o, ctx->identifier());\r
+//   out << " = ";\r
+//   toc(o, ctx->expr());\r
+// }\r
+// void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {\r
+//   out << "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) out << ", ";\r
+//     toc(o, ctx->expr(i));\r
+//   }\r
+//   out << ")";\r
+// }\r
+// void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {\r
+//   out << ctx->getText();\r
+// }\r
+// void toc(std::ostream & o, TocParser::LiteralContext * ctx) {\r
+//   if (ctx->INTLIT() != nullptr) out << ctx->INTLIT()->getText();\r
+// }\r
+// void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {\r
+//   toc(o, ctx->nonSubscriptExpr());\r
+//   out << "[";\r
+//   toc(o, ctx->expr());\r
+//   out << "]";\r
+// }\r
+// void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {\r
+//   toc(o, ctx->identifier(0));\r
+//   out << ".";\r
+//   toc(o, ctx->identifier(1));\r
+// }\r
+// void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {\r
+//   out << "(";\r
+//   toc(o, ctx->expr());\r
+//   out << ")";\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
+//   }\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
+//   }\r
+\r
+//   out << ");\n";\r
+// }\r
+// void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
+//   o\r
+//     << "struct "\r
+//     << ctx->structName()->getText()\r
+//     << ";\n";\r
+// }\r