X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/17860defa84c6d8bc0e8bc088a7e09361f17db07..66a27d2fc7c1ad4e97de76d4982168a0fed9920a:/src/repr_get.h diff --git a/src/repr_get.h b/src/repr_get.h index 7436dde..373b366 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -2,6 +2,8 @@ #include "repr.h" +// Transform ANTLR-generated types to corresponding IR types recursively + Type getType(TocParser::TypeContext * ctx); Variable getVariable(TocParser::VarContext * ctx); Body getBody(TocParser::BodyContext * ctx, std::shared_ptr parent); @@ -26,6 +28,11 @@ Expr getExpr(TocParser::ExprContext * ctx); Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr parent); +// all of these functions get the relevant information +// from the parse tree and call each other for sub expressions +// the getVariable is called for variable declarations and parameter definitions +// for example, because they have the same rule in the grammar file + Type getType(TocParser::TypeContext * ctx) { Type result; @@ -145,6 +152,7 @@ Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr(); result.name = ctx->typeName()->getText(); + result.ctx->name = result.name; for (auto d : ctx->decl()) { if (d->varDecl() != nullptr) @@ -161,7 +169,7 @@ Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptrnamespaceDecl() != nullptr) { - result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); + result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); } } return result; @@ -186,7 +194,7 @@ Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr parent } if (d->namespaceDecl() != nullptr) { - result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); + result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); } } return result; @@ -215,7 +223,11 @@ OpType getOperatorType(const std::string & s, std::string typeStrings[]) - +// Expressions are somewhat of an exception, because some of their +// grammar rules are recursive, so they have to be defined +// in a single rule using Labels (https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#alternative-labels) +// Because this results in a polymorphic type, getExpr for the base expression type +// is always called and from there the polymorphic type is determined at runtime Expr getExpr(TocParser::FuncExprContext * ctx) { Expr result; @@ -360,7 +372,9 @@ Expr getExpr(TocParser::IdentifierExprContext * ctx) - +// this is always called for Expression rules +// attempt dynamic_cast at runtime and call corresponding +// function Expr getExpr(TocParser::ExprContext * ctx) { Expr result;