\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
\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
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
}\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
}\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
\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
\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