\r
Type getType(TocParser::TypeContext * ctx);\r
Variable getVariable(TocParser::VarContext * ctx);\r
-Body getBody(TocParser::BodyContext * ctx);\r
-Function getFunction(TocParser::FuncContext * ctx);\r
-Struct getStruct(TocParser::StructDeclContext * ctx);\r
-Namespace getNamespace(TocParser::NamespaceDeclContext * ctx);\r
-Program getProgram(TocParser::ProgContext * ctx);\r
+Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);\r
+Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent);\r
+Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent);\r
+Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent);\r
+Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent);\r
\r
\r
Expr getExpr(TocParser::FuncExprContext * ctx);\r
Expr getExpr(TocParser::IdentifierExprContext * ctx);\r
Expr getExpr(TocParser::ExprContext * ctx);\r
\r
-Stmt getStmt(TocParser::StmtContext * ctx);\r
+Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
\r
Type getType(TocParser::TypeContext * ctx)\r
{\r
result.type = getType(ctx->type());\r
return result;\r
}\r
-Body getBody(TocParser::BodyContext * ctx)\r
+Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Body result;\r
+ result.ctx = std::make_unique<Context>();\r
+ result.ctx->parent = parent;\r
for (auto s : ctx->stmt())\r
{\r
if (s->varDecl() != nullptr)\r
{\r
- result.variables.push_back(getVariable(s->varDecl()->var()));\r
+ result.ctx->variables.push_back(getVariable(s->varDecl()->var()));\r
if (s->varDecl()->var()->expr() != nullptr)\r
- result.statements.push_back(getStmt(s));\r
+ result.statements.push_back(getStmt(s, result.ctx));\r
}\r
else\r
{\r
- result.statements.push_back(getStmt(s));\r
+ result.statements.push_back(getStmt(s, result.ctx));\r
}\r
}\r
return result;\r
}\r
-Function getFunction(TocParser::FuncContext * ctx)\r
+Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Function result;\r
result.name = ctx->funcName()->NAME()->toString();\r
for (auto p : ctx->parameter()->var())\r
result.parameters.push_back(getVariable(p));\r
}\r
- result.body = getBody(ctx->body());\r
+ result.body = getBody(ctx->body(), parent);\r
return result;\r
}\r
-Struct getStruct(TocParser::StructDeclContext * ctx)\r
+Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Struct result;\r
result.name = ctx->structName()->NAME()->toString();\r
if (m->structMethod() != nullptr)\r
{\r
result.methods.push_back({\r
- getFunction(m->structMethod()->func()),\r
+ getFunction(m->structMethod()->func(), parent),\r
m->privateDecl() != nullptr\r
});\r
}\r
}\r
return result;\r
}\r
-Namespace getNamespace(TocParser::NamespaceDeclContext * ctx)\r
+Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Namespace result;\r
+ result.ctx = std::make_unique<Context>();\r
result.name = ctx->typeName()->getText();\r
for (auto d : ctx->decl())\r
{\r
if (d->varDecl() != nullptr)\r
{\r
- result.variables.push_back(getVariable(d->varDecl()->var()));\r
+ result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
}\r
if (d->funcDecl() != nullptr)\r
{\r
- result.functions.push_back(getFunction(d->funcDecl()->func()));\r
+ result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
}\r
if (d->structDecl() != nullptr)\r
{\r
- result.structs.push_back(getStruct(d->structDecl()));\r
+ result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
}\r
if (d->namespaceDecl() != nullptr)\r
{\r
- result.namespaces.push_back(getNamespace(d->namespaceDecl()));\r
+ result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
}\r
}\r
return result;\r
}\r
-Program getProgram(TocParser::ProgContext * ctx)\r
+Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Program result;\r
+ result.ctx = std::make_unique<Context>();\r
for (auto d : ctx->decl())\r
{\r
if (d->varDecl() != nullptr)\r
{\r
- result.variables.push_back(getVariable(d->varDecl()->var()));\r
+ result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
}\r
if (d->funcDecl() != nullptr)\r
{\r
- result.functions.push_back(getFunction(d->funcDecl()->func()));\r
+ result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
}\r
if (d->structDecl() != nullptr)\r
{\r
- result.structs.push_back(getStruct(d->structDecl()));\r
+ result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
}\r
if (d->namespaceDecl() != nullptr)\r
{\r
- result.namespaces.push_back(getNamespace(d->namespaceDecl()));\r
+ result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
}\r
}\r
return result;\r
result = getExpr(dynamic_cast<TocParser::IdentifierExprContext *>(ctx));\r
return result;\r
}\r
-Stmt getStmt(TocParser::StmtContext * ctx)\r
+Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent)\r
{\r
Stmt result;\r
if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)\r
{\r
result.type = StmtType::If;\r
result._if.condition = getExpr(ctx->ifStmt()->expr());\r
- result._if.body = getBody(ctx->ifStmt()->body());\r
+ result._if.body = getBody(ctx->ifStmt()->body(), parent);\r
for (auto ei : ctx->ifStmt()->elseIfStmt())\r
{\r
result._if.elses.emplace_back(\r
true,\r
std::make_unique<Expr>(getExpr(ei->expr())),\r
- getBody(ei->body())\r
+ getBody(ei->body(), parent)\r
);\r
}\r
if (ctx->ifStmt()->elseStmt() != nullptr)\r
result._if.elses.emplace_back(\r
false,\r
nullptr,\r
- getBody(ctx->ifStmt()->elseStmt()->body())\r
+ getBody(ctx->ifStmt()->elseStmt()->body(), parent)\r
);\r
}\r
}\r
{\r
result._switch.cases.emplace_back(\r
std::make_unique<Expr>(getExpr(c->expr())),\r
- getBody(c->body())\r
+ getBody(c->body(), parent)\r
);\r
}\r
}\r
result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr());\r
result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));\r
result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));\r
- result._for.body = getBody(ctx->forStmt()->body());\r
+ result._for.body = getBody(ctx->forStmt()->body(), parent);\r
}\r
if (ctx->whileStmt() != nullptr)\r
{\r
result.type = StmtType::While;\r
result._while.condition = getExpr(ctx->whileStmt()->expr());\r
- result._while.body = getBody(ctx->whileStmt()->body());\r
+ result._while.body = getBody(ctx->whileStmt()->body(), parent);\r
}\r
if (ctx->assignStmt() != nullptr)\r
{\r