]> gitweb.ps.run Git - toc/blobdiff - src/toc.h
add antlr source code and ReadMe
[toc] / src / toc.h
index 659c9f32f4157cf832128ec3d828105c1827db2c..c31e12b3f61d18b93f90d871e8181a4053f9bb64 100644 (file)
--- a/src/toc.h
+++ b/src/toc.h
@@ -7,6 +7,7 @@
 #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
@@ -54,13 +55,18 @@ static std::string namespacePrefix() {
   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
-static Program globalPrg;\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
@@ -69,23 +75,32 @@ 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
+  // 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
-std::ostream & operator<< (std::ostream & out, const Variable & v)\r
-{\r
-  out << v.type << " ";\r
 \r
+std::string generateModifiers (std::string s, std::vector<TypeModifier> modifiers)\r
+{\r
   std::stringstream sstr;\r
-  std::string s = v.name;\r
 \r
-  for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\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
@@ -103,7 +118,23 @@ std::ostream & operator<< (std::ostream & out, const Variable & v)
       s = sstr.str();\r
     }\r
   }\r
-  out << s;\r
+\r
+  return s;\r
+}\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
@@ -143,25 +174,31 @@ 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
+    // 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
-    TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, *e._method.expr);\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 << "(&" << *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 +208,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 +226,14 @@ 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
+    // 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
@@ -232,11 +276,13 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
 \r
 void tocFunction (std::ostream & out, const Function & f, bool stub)\r
 {\r
-  if (!stub && !f.defined) return;\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 << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";\r
+    out << f.returnType << " " << generateModifiers(namespacePrefix() + f.name, f.returnType.modifiers) << " (" << vectorStr(f.parameters, ", ") << ")";\r
 \r
     if (stub)\r
     {\r
@@ -247,16 +293,21 @@ void tocFunction (std::ostream & out, const Function & f, bool stub)
       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
-      out << f.returnType << " " << namespacePrefix() << f.name << genericAppendix(instantiation) << " (" << vectorStr(f.parameters, ", ") << ")";\r
+      out << f.returnType << " " <<\r
+        generateModifiers(namespacePrefix() + f.name + genericAppendix(instantiation), f.returnType.modifiers) <<\r
+        " (" << vectorStr(f.parameters, ", ") << ")";\r
 \r
       if (stub)\r
       {\r
@@ -273,6 +324,7 @@ void tocFunction (std::ostream & out, const Function & f, bool stub)
 }\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
@@ -283,6 +335,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
       {\r
         Function f = m;\r
 \r
+        // add implicit this parameter\r
         f.parameters.insert(f.parameters.begin(),\r
         {"this",\r
           {\r
@@ -294,7 +347,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
           }\r
         });\r
         out << f.returnType << " " <<\r
-          namespacePrefix() << s.name << "_" << f.name <<\r
+          generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) <<\r
           " (" << vectorStr(f.parameters, ", ") << ");\n";\r
       }\r
       return;\r
@@ -314,6 +367,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
     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
@@ -325,10 +380,11 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
           }\r
         });\r
       out << f.returnType << " " <<\r
-      namespacePrefix() << s.name << "_" << f.name <<\r
-      " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
+        generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) <<\r
+        " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
     }\r
   }\r
+  // generic struct\r
   else\r
   {\r
     for (auto instantiation : s.genericInstantiations)\r
@@ -346,6 +402,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
         {\r
           Function f = m;\r
 \r
+        // add implicit this parameter\r
           f.parameters.insert(f.parameters.begin(),\r
           {"this",\r
             {\r
@@ -357,7 +414,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
             }\r
           });\r
           out << f.returnType << " " <<\r
-            namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<\r
+            generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) <<\r
             " (" << vectorStr(f.parameters, ", ") << ");\n";\r
         }\r
         return;\r
@@ -377,6 +434,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
       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
@@ -388,32 +447,29 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
             }\r
           });\r
         out << f.returnType << " " <<\r
-        namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<\r
-        " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
+          generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) <<\r
+          " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
       }\r
 \r
       currentInstantiation.clear();\r
     }\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 +480,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 +512,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