]> gitweb.ps.run Git - toc/blobdiff - src/repr_get.h
comments
[toc] / src / repr_get.h
index 827c02bd63e27882d8ff029f59b9d189cbdacd0b..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
@@ -43,6 +50,13 @@ Type getType(TocParser::TypeContext * ctx)
       isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1\r
     );\r
   }\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result.genericInstantiation.push_back(getType(g));\r
+    }\r
+  }\r
   return result;\r
 }\r
 Variable getVariable(TocParser::VarContext * ctx)\r
@@ -77,11 +91,20 @@ Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> pare
   Function result;\r
   result.name = ctx->funcName()->NAME()->toString();\r
   result.returnType = getType(ctx->type());\r
+  if (ctx->genericDecl() != nullptr)\r
+  {\r
+    for (auto t : ctx->genericDecl()->typeName())\r
+    {\r
+      result.genericTypeNames.push_back(t->getText());\r
+    }\r
+  }\r
+\r
   if (!ctx->parameter()->var().empty())\r
   {\r
     for (auto p : ctx->parameter()->var())\r
       result.parameters.push_back(getVariable(p));\r
   }\r
+\r
   if (ctx->body() != nullptr)\r
   {\r
     result.body = getBody(ctx->body(), parent);\r
@@ -97,6 +120,14 @@ Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> pa
 {\r
   Struct result;\r
   result.name = ctx->structName()->NAME()->toString();\r
+  if (ctx->genericDecl() != nullptr)\r
+  {\r
+    for (auto t : ctx->genericDecl()->typeName())\r
+    {\r
+      result.genericTypeNames.push_back(t->getText());\r
+    }\r
+  }\r
+\r
   for (auto m : ctx->structMember())\r
   {\r
     if (m->structVar() != nullptr)\r
@@ -121,6 +152,7 @@ Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Co
   Namespace result;\r
   result.ctx = std::make_unique<Context>();\r
   result.name = ctx->typeName()->getText();\r
+  result.ctx->name = result.name;\r
   for (auto d : ctx->decl())\r
   {\r
     if (d->varDecl() != nullptr)\r
@@ -129,15 +161,15 @@ Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Co
     }\r
     if (d->funcDecl() != nullptr)\r
     {\r
-      result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
+      result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
     }\r
     if (d->structDecl() != nullptr)\r
     {\r
-      result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+      result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
     }\r
     if (d->namespaceDecl() != nullptr)\r
     {\r
-      result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
+      result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
     }\r
   }\r
   return result;\r
@@ -154,15 +186,15 @@ Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent
     }\r
     if (d->funcDecl() != nullptr)\r
     {\r
-      result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
+      result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
     }\r
     if (d->structDecl() != nullptr)\r
     {\r
-      result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+      result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
     }\r
     if (d->namespaceDecl() != nullptr)\r
     {\r
-      result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
+      result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
     }\r
   }\r
   return result;\r
@@ -191,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
@@ -199,6 +235,13 @@ Expr getExpr(TocParser::FuncExprContext * ctx)
   for (auto n : ctx->namespaceSpecifier())\r
     result._func.namespacePrefixes.push_back(n->typeName()->getText());\r
   result._func.functionName = ctx->funcName()->NAME()->toString();\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result._func.genericInstantiation.push_back(getType(g));\r
+    }\r
+  }\r
   for (auto e : ctx->expr())\r
     result._func.arguments.push_back(getExpr(e));\r
   return result;\r
@@ -209,6 +252,13 @@ Expr getExpr(TocParser::MethodExprContext * ctx)
   result.type = ExprType::Method;\r
   result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
   result._method.methodName = ctx->funcName()->NAME()->toString();\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result._method.genericInstantiation.push_back(getType(g));\r
+    }\r
+  }\r
   for (int i = 1; i < ctx->expr().size(); i++)\r
     result._method.arguments.push_back(getExpr(ctx->expr(i)));\r
   return result;\r
@@ -252,6 +302,7 @@ Expr getExpr(TocParser::DotExprContext * ctx)
   result.type = ExprType::Dot;\r
   result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
   result._dot.identifier = ctx->varName()->getText();\r
+  result._dot.isPointer = ctx->arrow() != nullptr;\r
   return result;\r
 }\r
 Expr getExpr(TocParser::PrefixOpExprContext * ctx)\r
@@ -321,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