]> gitweb.ps.run Git - toc/commitdiff
comments
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Thu, 12 Aug 2021 08:32:51 +0000 (10:32 +0200)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Thu, 12 Aug 2021 08:32:51 +0000 (10:32 +0200)
src/check.h [deleted file]
src/find.h
src/generic.h
src/main.cpp
src/repr.h
src/repr_get.h
src/toc.h
src/typeInfo.h
src/visit.h

diff --git a/src/check.h b/src/check.h
deleted file mode 100644 (file)
index 091b646..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-#pragma once\r
-\r
-#include "repr.h"\r
-\r
-bool checkStmt(\r
-  const Stmt & s,\r
-  std::vector<Namespace> namespaces,\r
-  std::vector<Variable> vars)\r
-{\r
-  return true;\r
-}\r
-\r
-bool checkFunction(\r
-  const Function & f,\r
-  std::vector<Namespace> namespaces,\r
-  std::vector<Variable> vars)\r
-{\r
-  vars.insert(vars.end(), f.parameters.begin(), f.parameters.end());\r
-  vars.insert(vars.end(), f.body.variables.begin(), f.body.variables.end());\r
-  for (auto s : f.body.statements)\r
-  {\r
-    if (!checkStmt(s, namespaces, vars))\r
-      return false;\r
-  }\r
-  return true;\r
-}\r
-\r
-bool checkProgram(const Program & p)\r
-{\r
-  for (auto f : p.functions)\r
-  {\r
-    if (!checkFunction(f, p.namespaces, p.variables))\r
-      return false;\r
-  }\r
-  for (auto s : p.structs)\r
-  {\r
-    std::vector<Variable> vars = p.variables;\r
-    for (auto v : s.members)\r
-      vars.push_back(v);\r
-    for (auto f : s.methods)\r
-    {\r
-      if (!checkFunction(f, p.namespaces, vars))\r
-        return false;\r
-    }\r
-  }\r
-  return true;\r
-}
\ No newline at end of file
index 3342536b1a05bfcc99bdd6396bb164c2a39e2889..cd7f5773901542f8d518614cdc1a17ddbccef123 100644 (file)
@@ -8,7 +8,10 @@
 \r
 template<typename T>\r
 using opt = std::optional<T>;\r
+template<typename ... Ts>\r
+using tup = std::tuple<Ts ...>;\r
 \r
+// find an item in a vector by predicate\r
 template<typename T>\r
 opt<T> find(const std::vector<T> & ts, std::function<bool(T)> f)\r
 {\r
@@ -18,6 +21,7 @@ opt<T> find(const std::vector<T> & ts, std::function<bool(T)> f)
   return nullopt;\r
 }\r
 \r
+// same as above but return pointer into raw array held by vector\r
 template<typename T>\r
 opt<T *> findPtr(const std::vector<T> & ts, std::function<bool(T)> f)\r
 {\r
@@ -27,12 +31,12 @@ opt<T *> findPtr(const std::vector<T> & ts, std::function<bool(T)> f)
   return nullopt;\r
 }\r
 \r
-std::optional<\r
-  std::tuple<\r
-    std::shared_ptr<Context>,\r
-    std::vector<std::string>>>\r
+opt<tup<\r
+        std::shared_ptr<Context>,\r
+        std::vector<std::string>>>\r
 getContext(std::shared_ptr<Context> ctx, const std::vector<std::string> & namespacePrefix)\r
 {\r
+  // try finding a continuos series of namespaces in a given context\r
   auto result = ctx;\r
 \r
   for (auto name : namespacePrefix)\r
@@ -48,6 +52,8 @@ getContext(std::shared_ptr<Context> ctx, const std::vector<std::string> & namesp
     }\r
   }\r
 \r
+  // if the found context is the end of a series of namespaces, also return\r
+  // a vector of namespace names\r
   std::vector<std::string> namespaces;\r
   for (auto it = result; it != nullptr; it = it->parent)\r
   {\r
@@ -65,9 +71,13 @@ getContext(std::shared_ptr<Context> ctx, const std::vector<std::string> & namesp
   return std::make_tuple(result, namespaces);\r
 }\r
 \r
+// all of the following functions work the same way,\r
+// walking up the context hierarchy until the global context.\r
+// return the first found instance that matches provided criteria\r
+// theres also a variant to get a pointer instead for functions and\r
+// structs used for generic instantiation\r
 \r
-\r
-opt<std::tuple<Function, std::vector<std::string>>> findFunction(\r
+opt<tup<Function, std::vector<std::string>>> findFunction(\r
   const std::string & name,\r
   const std::vector<std::string> & namespacePrefix,\r
   std::shared_ptr<Context> ctx)\r
@@ -85,7 +95,7 @@ opt<std::tuple<Function, std::vector<std::string>>> findFunction(
   return nullopt;\r
 }\r
 \r
-opt<std::tuple<Function *, std::vector<std::string>>> findFunctionPtr(\r
+opt<tup<Function *, std::vector<std::string>>> findFunctionPtr(\r
   const std::string & name,\r
   const std::vector<std::string> & namespacePrefix,\r
   std::shared_ptr<Context> ctx)\r
@@ -105,7 +115,7 @@ opt<std::tuple<Function *, std::vector<std::string>>> findFunctionPtr(
 \r
 \r
 \r
-opt<std::tuple<Struct, std::vector<std::string>>> findStruct(\r
+opt<tup<Struct, std::vector<std::string>>> findStruct(\r
   const std::string & name,\r
   const std::vector<std::string> & namespacePrefix,\r
   std::shared_ptr<Context> ctx)\r
@@ -123,7 +133,7 @@ opt<std::tuple<Struct, std::vector<std::string>>> findStruct(
   return nullopt;\r
 }\r
 \r
-opt<std::tuple<Struct *, std::vector<std::string>>> findStructPtr(\r
+opt<tup<Struct *, std::vector<std::string>>> findStructPtr(\r
   const std::string & name,\r
   const std::vector<std::string> & namespacePrefix,\r
   std::shared_ptr<Context> ctx)\r
@@ -143,7 +153,7 @@ opt<std::tuple<Struct *, std::vector<std::string>>> findStructPtr(
 \r
 \r
 \r
-opt<std::tuple<Variable, std::vector<std::string>>> findVariable(\r
+opt<tup<Variable, std::vector<std::string>>> findVariable(\r
   const std::string & name,\r
   const std::vector<std::string> & namespacePrefix,\r
   std::shared_ptr<Context> ctx)\r
@@ -163,6 +173,8 @@ opt<std::tuple<Variable, std::vector<std::string>>> findVariable(
 \r
 \r
 \r
+// find struct members and pointer variants\r
+\r
 opt<StructMember<Function>> findStructMethod(\r
   const std::string & name,\r
   const Struct & s)\r
index df203b327305c556a786dc274343061448c5cdf2..33323e4eb8e560289faac453b23934b8a5340565 100644 (file)
@@ -4,6 +4,7 @@
 #include "typeInfo.h"\r
 #include "visit.h"\r
 \r
+// add a generic instantiation if its not in the vector already\r
 void addGenericInstantiation(\r
   std::vector<std::vector<Type>> & insts,\r
   const std::vector<Type> & newInst)\r
@@ -32,6 +33,8 @@ Program instantiateGenerics(const Program & p)
 \r
   // Find generic instantiations\r
 \r
+  // visit expressions (only function calls are considered) and types,\r
+  // find the function/struct by pointer and add an instantiation\r
   Visitor findGenericInstantiations;\r
   findGenericInstantiations.onExpr =\r
   [&](const Expr & e, const std::shared_ptr<Context> ctx)\r
@@ -48,7 +51,6 @@ Program instantiateGenerics(const Program & p)
         addGenericInstantiation(std::get<0>(*f)->genericInstantiations, e._func.genericInstantiation);\r
       }\r
     }\r
-    // TODO: generic methods\r
   };\r
   findGenericInstantiations.onType =\r
   [&](const Type & t, const std::shared_ptr<Context> ctx)\r
@@ -72,6 +74,10 @@ Program instantiateGenerics(const Program & p)
   return result;\r
 }\r
 \r
+// generate the appendix for C struct/function names\r
+// including array/pointer indicators because\r
+// there might be distinct instantiations\r
+// for int and int* for example\r
 std::string genericAppendix(const std::vector<Type> & ts)\r
 {\r
   std::stringstream sstr;\r
index 5b53b1004c3641114bf820cad06fabdd4a749c10..bd9a2b2923f8e59f513ccf95370e95f22c7ced1b 100644 (file)
@@ -17,27 +17,36 @@ int main(int argc, const char * argv[])
 {\r
   std::ifstream ifs("test/test.toc");\r
 \r
+  // create ANTLR input from filestream\r
   ANTLRInputStream input(ifs);\r
 \r
+  // lex input\r
   TocLexer lexer(&input);\r
   CommonTokenStream tokens(&lexer);\r
 \r
+  // parse\r
   TocParser parser(&tokens);\r
+\r
+  // get Prog (root node)\r
   TocParser::ProgContext * prog = parser.prog();\r
-  tree::ParseTree * tree = prog;\r
 \r
+  // dont continue on parse error\r
   if (parser.getNumberOfSyntaxErrors() > 0)\r
   {\r
     std::cerr << "Parsing error" << std::endl;\r
     return 1;\r
   }\r
 \r
+  // print raw parse tree\r
+  //tree::ParseTree * tree = prog;\r
   //std::string s = tree->toStringTree(&parser) + "\n";\r
   //std::cout << "Parse Tree: " << s << std::endl;\r
 \r
+  // generate IR from tree and instantiate generics\r
   Program prg = getProgram(prog, nullptr);\r
   instantiateGenerics(prg);\r
 \r
+  // print to cout and file\r
   try\r
   {\r
     tocProgram(std::cout, prg);\r
index 959e74dfe891873eecfcdbf2671d003497f8bb95..569411c05db2315e7d1fd18c7b2a74b478ae07f7 100644 (file)
@@ -8,6 +8,8 @@
 \r
 using namespace std;\r
 \r
+// This contains a 1 to 1 representation of the defined language\r
+\r
 struct Type;\r
 struct Variable;\r
 struct Body;\r
@@ -37,6 +39,9 @@ struct AssignStmt;
 struct ReturnStmt;\r
 struct Stmt;\r
 \r
+// Context is a collection of everything that can be defined in a namespace\r
+// that is reused for bodies so that the hierarchy can be walked uniformly\r
+// both up and down using the parent variable\r
 struct Context\r
 {\r
   std::optional<std::string> name;\r
@@ -184,6 +189,8 @@ struct DotExpr
   std::string identifier;\r
 };\r
 \r
+// OperatorType enum with corresponding string array to lookup\r
+// enum from string and the other way round\r
 enum class PrefixOperatorType\r
 {\r
   Plus, Minus, Increment, Decrement,\r
index a1f496a0314ae5205b73d55c05af58526729e093..373b366ae3849da99bb4c7a1441986bf65c34efd 100644 (file)
@@ -2,6 +2,8 @@
 \r
 #include "repr.h"\r
 \r
+// Transform ANTLR-generated types to corresponding IR types recursively\r
+\r
 Type                getType(TocParser::TypeContext * ctx);\r
 Variable            getVariable(TocParser::VarContext * ctx);\r
 Body                getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);\r
@@ -26,6 +28,11 @@ Expr                getExpr(TocParser::ExprContext * ctx);
 \r
 Stmt                getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
 \r
+// all of these functions get the relevant information\r
+// from the parse tree and call each other for sub expressions\r
+// the getVariable is called for variable declarations and parameter definitions\r
+// for example, because they have the same rule in the grammar file\r
+\r
 Type getType(TocParser::TypeContext * ctx)\r
 {\r
   Type result;\r
@@ -216,7 +223,11 @@ OpType getOperatorType(const std::string & s, std::string typeStrings[])
 \r
 \r
 \r
-\r
+// Expressions are somewhat of an exception, because some of their\r
+// grammar rules are recursive, so they have to be defined\r
+// in a single rule using Labels (https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#alternative-labels)\r
+// Because this results in a polymorphic type, getExpr for the base expression type\r
+// is always called and from there the polymorphic type is determined at runtime\r
 Expr getExpr(TocParser::FuncExprContext * ctx)\r
 {\r
   Expr result;\r
@@ -361,7 +372,9 @@ Expr getExpr(TocParser::IdentifierExprContext * ctx)
 \r
 \r
 \r
-\r
+// this is always called for Expression rules\r
+// attempt dynamic_cast at runtime and call corresponding\r
+// function\r
 Expr getExpr(TocParser::ExprContext * ctx)\r
 {\r
   Expr result;\r
index e95ab3eca27872b427846614e33c7ba0fd2d93e5..095a72ccaec35d4ffb10936e64d15668b30490f8 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,28 +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
-\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
+  // 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
@@ -87,11 +78,16 @@ std::ostream & operator<< (std::ostream & out, const Type & t)
   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
@@ -104,10 +100,13 @@ std::ostream & operator<< (std::ostream & out, const Variable & v)
   std::stringstream sstr;\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
+  // nest modifiers, inverted because C defines them\r
+  // the opposite direction\r
   for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\r
   {\r
     if (m->type == TypeModifierType::Pointer)\r
@@ -166,6 +165,7 @@ std::ostream & operator<< (std::ostream & out, const Expr & e)
   {\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
@@ -178,6 +178,8 @@ std::ostream & operator<< (std::ostream & out, const Expr & e)
   }\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
@@ -215,6 +217,7 @@ 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
+    // 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
@@ -264,8 +267,10 @@ 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
@@ -279,10 +284,13 @@ 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
@@ -305,6 +313,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
@@ -315,6 +324,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
@@ -346,6 +356,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
@@ -361,6 +373,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
       " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
     }\r
   }\r
+  // generic struct\r
   else\r
   {\r
     for (auto instantiation : s.genericInstantiations)\r
@@ -378,6 +391,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
@@ -409,6 +423,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
@@ -432,7 +448,6 @@ void tocProgram (std::ostream & out, const Program & p)
 {\r
   globalCtx = p.ctx;\r
 \r
-  globalPrg = p;\r
   for (auto n : p.ctx->namespaces)\r
   {\r
     tocNamespace(out, n, true);\r
index e813612b6af90982dd031a6dd4cb83cf09c6d508..7862ef53282bbd556ebaa4c881d198ce6fc88ee9 100644 (file)
@@ -12,6 +12,7 @@ struct TypeInfo
 \r
 TypeInfo typeType(std::shared_ptr<Context> globalCtx, Type t)\r
 {\r
+  // used to differentiate basic types from user defined types\r
   TypeInfo result;\r
   result.isStruct = true;\r
   if (t.name == "int" || t.name == "float" || t.name == "double" ||\r
@@ -33,6 +34,7 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
   {\r
   case ExprType::Func:\r
   {\r
+    // get type info from return type\r
     auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
     if (!f.has_value())\r
       throw "Unknown function";\r
@@ -41,6 +43,7 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
   }\r
   case ExprType::Method:\r
   {\r
+    // get type info from return type\r
     TypeInfo tiCaller = typeExpr(globalCtx, *e._method.expr);\r
     if (!tiCaller.isStruct)\r
       throw "Calling method on non-struct";\r
@@ -54,6 +57,7 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
     break;\r
   }\r
   case ExprType::Lit:\r
+    // literal types are defined\r
     result.isStruct = false;\r
     switch (e._lit.type)\r
     {\r
@@ -68,6 +72,8 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
     break;\r
   case ExprType::Dot:\r
   {\r
+    // assume dot access is always member access\r
+    // and lookup struct variable\r
     auto tiCaller = typeExpr(globalCtx, *e._dot.expr);\r
     if (!tiCaller.isStruct)\r
       throw "Accessing member of non-struct";\r
@@ -94,6 +100,8 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
     break;\r
   case ExprType::Bracket:\r
   {\r
+    // get type of expr and remove array/ptr modifier to get\r
+    // type of [] access\r
     TypeInfo ti = typeExpr(globalCtx, *e._brackets.lexpr);\r
     if (!ti.type.modifiers.empty())\r
     {\r
@@ -107,6 +115,7 @@ TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
   }\r
   case ExprType::Identifier:\r
   {\r
+    // var lookup and return var type\r
     auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
     if (!v.has_value())\r
       throw "Unknown variable";\r
index 1175a86adc7424e6693c4bac874b0dfc8cf4c287..646ac61d270756dd7d9ab0266fd87e3dcebdcfdb 100644 (file)
@@ -4,6 +4,9 @@
 \r
 #include <functional>\r
 \r
+// struct with callback functions for all relevant types\r
+// tree can be walked selectively by providing only\r
+// needed callbacks\r
 struct Visitor {\r
   std::function<void(const Type &, const std::shared_ptr<Context> ctx)> onType = [](auto, auto){};\r
   std::function<void(const Expr &, const std::shared_ptr<Context> ctx)> onExpr = [](auto, auto){};\r
@@ -20,6 +23,7 @@ struct Visitor {
 \r
 #define VISIT(XS) for (auto x : XS) visit(x);\r
 \r
+// simply walk IR by recursively calling functions for all children\r
 struct Visit {\r
 private:\r
   Visitor v;\r