]> gitweb.ps.run Git - toc/blobdiff - src/toc.h
function resolution, pre generics
[toc] / src / toc.h
index e4f36e521365e71b944d2fe6b1db35d5b363a58d..c0c73f155d5c86f75accb2549fa0b9c95c21ec57 100644 (file)
--- a/src/toc.h
+++ b/src/toc.h
@@ -4,32 +4,36 @@
 #include <sstream>\r
 \r
 #include "repr.h"\r
+#include "typeInfo.h"\r
 \r
 template<typename T>\r
-std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)\r
+std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)\r
 {\r
-  bool comma = false;\r
+  std::stringstream sstr;\r
+\r
+  bool putSeparator = false;\r
   for (auto t : v)\r
   {\r
-    if (comma) out << ", ";\r
-    else comma = true;\r
-    out << t;\r
+    if (putSeparator) sstr << separator;\r
+    else putSeparator = true;\r
+    sstr << t;\r
   }\r
-  return out;\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 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
+void tocNamespace  (std::ostream & out, const Namespace & n, bool stub);\r
 \r
 static const int TAB_WIDTH = 2;\r
 static int indentation = 0;\r
@@ -39,9 +43,25 @@ static void indent(std::ostream & out, int change = 0)
   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
+static Program globalPrg;\r
+static std::shared_ptr<Context> globalCtx;\r
+\r
 std::ostream & operator<< (std::ostream & out, const Type & t)\r
 {\r
-  out << t.name;\r
+  TypeInfo ti = typeType(globalPrg, t);\r
+  if (ti.isStruct)\r
+    out << "struct ";\r
+  out << vectorStr(t.namespacePrefixes, "_", true) << t.name;\r
 \r
   return out;\r
 }\r
@@ -76,11 +96,14 @@ std::ostream & operator<< (std::ostream & out, const Variable & v)
 }\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.variables)\r
+  for (auto v : b.ctx->variables)\r
   {\r
     indent(out);\r
     out << v << ";\n";\r
@@ -97,64 +120,59 @@ std::ostream & operator<< (std::ostream & out, const Body & b)
   indent(out, -2);\r
   out << "}\n";\r
 \r
-  return out;\r
-}\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
-  {\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
-{\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
-{\r
-  out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;\r
+  globalCtx = b.ctx->parent;\r
 \r
   return out;\r
 }\r
 std::ostream & operator<< (std::ostream & out, const Expr & e)\r
 {\r
-  if (e.parenthesized)\r
-    out << "(";\r
-\r
   switch (e.type)\r
   {\r
   case ExprType::Func:\r
-    out << e._func.functionName << "(" << e._func.arguments << ")"; break;\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 << "(" << vectorStr(e._func.arguments, ", ") << ")"; break;\r
+  }\r
+  case ExprType::Method:\r
+  {\r
+    TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, *e._method.expr);\r
+    out <<\r
+      vectorStr(ti.type.namespacePrefixes, "_", true) <<\r
+      ti.type.name << "_" << e._method.methodName <<\r
+      "(&" << *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::Identifier:\r
-    out << e._identifier.name; break;\r
-  case ExprType::Brackets:\r
-    out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
+  case ExprType::Paren:\r
+    out << "(" << e._paren.expr << ")"; break;\r
   case ExprType::Dot:\r
-    out << *e._dot.expr << "." << e._dot.ident.name; break;\r
-  case ExprType::UnaryOperator:\r
-    out << e._unaryOperator; break;\r
-  case ExprType::BinaryOperator:\r
-    out << e._binaryOperator; break;\r
-  case ExprType::TernaryOperator:\r
-    out << e._ternaryOperator; break;\r
+    out << *e._dot.expr << "." << 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
+    out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;\r
   }\r
-  \r
-  if (e.parenthesized)\r
-    out << ")";\r
 \r
   return out;\r
 }\r
@@ -165,7 +183,7 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
   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
+    out << "switch (" << s._switch.ident << ")\n{\n";\r
     for (auto c : s._switch.cases)\r
     {\r
       indent(out, 2);\r
@@ -176,14 +194,14 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
     break;\r
   case StmtType::For:\r
     out << "for (" <<\r
-      s._for.varName << " = " << *s._for.initValue << "; " <<\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.name << " = " << s._assign.expr << ";"; break;\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
@@ -196,7 +214,9 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
 \r
 void tocFunction (std::ostream & out, const Function & f, bool stub)\r
 {\r
-  out << f.returnType << " " << f.name << " (" << f.parameters << ")";\r
+  if (!stub && !f.defined) return;\r
+\r
+  out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";\r
 \r
   if (stub)\r
   {\r
@@ -209,19 +229,27 @@ void tocFunction (std::ostream & out, const Function & f, bool stub)
 }\r
 void tocStruct (std::ostream & out, const Struct & s, bool stub)\r
 {\r
-  out << "struct " << s.name;\r
+  out << "struct " << namespacePrefix() << s.name;\r
   if (stub)\r
   {\r
     out << ";\n";\r
     for (auto m : s.methods)\r
     {\r
-      m.parameters.insert(m.parameters.begin(),\r
+      Function f = m;\r
+\r
+      f.parameters.insert(f.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
+        {\r
+          namespaces,\r
+          s.name,\r
+          {\r
+            {TypeModifierType::Pointer, false, -1}\r
+          }\r
+        }\r
+      });\r
+      out << f.returnType << " " <<\r
+        namespacePrefix() << s.name << "_" << f.name <<\r
+        " (" << vectorStr(f.parameters, ", ") << ");\n";\r
     }\r
     return;\r
   }\r
@@ -239,34 +267,96 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
   \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
+    Function f = m;\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
+    namespacePrefix() << s.name << "_" << f.name <<\r
+    " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
   }\r
 }\r
 void tocProgram (std::ostream & out, const Program & p)\r
 {\r
+  globalCtx = p.ctx;\r
+\r
+  globalPrg = p;\r
+  for (auto n : p.namespaces)\r
+  {\r
+    tocNamespace(out, n, true);\r
+  }\r
+  out << "\n\n";\r
   for (auto s : p.structs)\r
   {\r
     tocStruct(out, s, true);\r
   }\r
+  out << "\n\n";\r
   for (auto f : p.functions)\r
   {\r
     tocFunction(out, f, true);\r
   }\r
+  out << "\n\n";\r
 \r
-  for (auto v : p.variables)\r
+  for (auto v : p.ctx->variables)\r
   {\r
     out << v << ";\n";\r
   }\r
+  out << "\n\n";\r
+  for (auto n : p.namespaces)\r
+  {\r
+    tocNamespace(out, n, false);\r
+  }\r
+  out << "\n\n";\r
   for (auto s : p.structs)\r
   {\r
     tocStruct(out, s, false);\r
   }\r
+  out << "\n\n";\r
   for (auto f : p.functions)\r
   {\r
     tocFunction(out, f, false);\r
   }\r
+  out << "\n\n";\r
 }\r
+\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.namespaces)\r
+  {\r
+    tocNamespace(out, n, stub);\r
+    out << "\n\n";\r
+  }\r
+  for (auto s : n.structs)\r
+  {\r
+    tocStruct(out, s, stub);\r
+    out << "\n\n";\r
+  }\r
+  for (auto f : n.functions)\r
+  {\r
+    tocFunction(out, f, stub);\r
+    out << "\n\n";\r
+  }\r
+  namespaces.pop_back();\r
+\r
+  globalCtx = n.ctx->parent;\r
+}
\ No newline at end of file