]> gitweb.ps.run Git - toc/blobdiff - src/toc.h
fixed NAME grammar rule
[toc] / src / toc.h
index 659c9f32f4157cf832128ec3d828105c1827db2c..e95ab3eca27872b427846614e33c7ba0fd2d93e5 100644 (file)
--- a/src/toc.h
+++ b/src/toc.h
@@ -59,6 +59,21 @@ static std::map<std::string, Type> currentInstantiation;
 static Program globalPrg;\r
 static std::shared_ptr<Context> globalCtx;\r
 \r
+\r
+\r
+// std::string getPrefix(std::shared_ptr<Context> ctx)\r
+// {\r
+//   std::string result;\r
+//   for (auto it = ctx; it != nullptr; it = it->parent)\r
+//   {\r
+//     if (it->name.has_value())\r
+//     {\r
+//       result = it->name.value() + "_" + result;\r
+//     }\r
+//   }\r
+//   return result;\r
+// }\r
+\r
 std::ostream & operator<< (std::ostream & out, const Type & t)\r
 {\r
   for (auto kv : currentInstantiation)\r
@@ -69,10 +84,14 @@ std::ostream & operator<< (std::ostream & out, const Type & t)
       return out;\r
     }\r
   }\r
-  TypeInfo ti = typeType(globalPrg, t);\r
+  TypeInfo ti = typeType(globalCtx, t);\r
   if (ti.isStruct)\r
     out << "struct ";\r
-  out << vectorStr(t.namespacePrefixes, "_", true) << t.name;\r
+  auto s = findStruct(t.name, t.namespacePrefixes, globalCtx);\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
   if (!t.genericInstantiation.empty())\r
     out << genericAppendix(t.genericInstantiation);\r
 \r
@@ -84,6 +103,10 @@ std::ostream & operator<< (std::ostream & out, const Variable & v)
 \r
   std::stringstream sstr;\r
   std::string s = v.name;\r
+  \r
+  auto var = findVariable(v.name, namespaces, globalCtx);\r
+  if (var.has_value())\r
+    s = vectorStr(std::get<1>(*var), "_", true) + s;\r
 \r
   for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\r
   {\r
@@ -143,25 +166,28 @@ std::ostream & operator<< (std::ostream & out, const Expr & e)
   {\r
   case ExprType::Func:\r
   {\r
-    if (e._func.namespacePrefixes.empty())\r
-    {\r
-      TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, e);\r
-      \r
-    }\r
-    out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName;\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(globalPrg, namespaces, globalCtx, *e._method.expr);\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 << "(&" << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") <<\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
@@ -171,7 +197,7 @@ std::ostream & operator<< (std::ostream & out, const Expr & e)
     else if (e._lit.type == LitType::Bool) out << e._lit._bool;\r
     break;\r
   case ExprType::Paren:\r
-    out << "(" << e._paren.expr << ")"; break;\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
@@ -189,7 +215,13 @@ std::ostream & operator<< (std::ostream & out, const Expr & e)
   case ExprType::Bracket:\r
     out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
   case ExprType::Identifier:\r
-    out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;\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
@@ -396,24 +428,22 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
     }\r
   }\r
 }\r
-void tocProgram (std::ostream & out, const Program & _p)\r
+void tocProgram (std::ostream & out, const Program & p)\r
 {\r
-  Program p = instantiateGenerics(_p);\r
-\r
   globalCtx = p.ctx;\r
 \r
   globalPrg = p;\r
-  for (auto n : p.namespaces)\r
+  for (auto n : p.ctx->namespaces)\r
   {\r
     tocNamespace(out, n, true);\r
   }\r
   out << "\n\n";\r
-  for (auto s : p.structs)\r
+  for (auto s : p.ctx->structs)\r
   {\r
     tocStruct(out, s, true);\r
   }\r
   out << "\n\n";\r
-  for (auto f : p.functions)\r
+  for (auto f : p.ctx->functions)\r
   {\r
     tocFunction(out, f, true);\r
   }\r
@@ -424,17 +454,17 @@ void tocProgram (std::ostream & out, const Program & _p)
     out << v << ";\n";\r
   }\r
   out << "\n\n";\r
-  for (auto n : p.namespaces)\r
+  for (auto n : p.ctx->namespaces)\r
   {\r
     tocNamespace(out, n, false);\r
   }\r
   out << "\n\n";\r
-  for (auto s : p.structs)\r
+  for (auto s : p.ctx->structs)\r
   {\r
     tocStruct(out, s, false);\r
   }\r
   out << "\n\n";\r
-  for (auto f : p.functions)\r
+  for (auto f : p.ctx->functions)\r
   {\r
     tocFunction(out, f, false);\r
   }\r
@@ -456,17 +486,17 @@ void tocNamespace  (std::ostream & out, const Namespace & n, bool stub)
     }\r
     out << "\n\n";\r
   }\r
-  for (auto n : n.namespaces)\r
+  for (auto n : n.ctx->namespaces)\r
   {\r
     tocNamespace(out, n, stub);\r
     out << "\n\n";\r
   }\r
-  for (auto s : n.structs)\r
+  for (auto s : n.ctx->structs)\r
   {\r
     tocStruct(out, s, stub);\r
     out << "\n\n";\r
   }\r
-  for (auto f : n.functions)\r
+  for (auto f : n.ctx->functions)\r
   {\r
     tocFunction(out, f, stub);\r
     out << "\n\n";\r