X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/dbc4a22d3c8c4189459f0361cb9da06415ec2dc9..c4231c6faf4e1b4650b075c641b0bb8c053739e4:/src/repr_get.h?ds=sidebyside diff --git a/src/repr_get.h b/src/repr_get.h index 54aea6f..a1f496a 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -4,11 +4,11 @@ Type getType(TocParser::TypeContext * ctx); Variable getVariable(TocParser::VarContext * ctx); -Body getBody(TocParser::BodyContext * ctx); -Function getFunction(TocParser::FuncContext * ctx); -Struct getStruct(TocParser::StructDeclContext * ctx); -Namespace getNamespace(TocParser::NamespaceDeclContext * ctx); -Program getProgram(TocParser::ProgContext * ctx); +Body getBody(TocParser::BodyContext * ctx, std::shared_ptr parent); +Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr parent); +Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr parent); +Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr parent); +Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr parent); Expr getExpr(TocParser::FuncExprContext * ctx); @@ -24,7 +24,7 @@ Expr getExpr(TocParser::BracketExprContext * ctx); Expr getExpr(TocParser::IdentifierExprContext * ctx); Expr getExpr(TocParser::ExprContext * ctx); -Stmt getStmt(TocParser::StmtContext * ctx); +Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr parent); Type getType(TocParser::TypeContext * ctx) { @@ -43,6 +43,13 @@ Type getType(TocParser::TypeContext * ctx) isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1 ); } + if (ctx->genericInstantiation() != nullptr) + { + for (auto g : ctx->genericInstantiation()->type()) + { + result.genericInstantiation.push_back(getType(g)); + } + } return result; } Variable getVariable(TocParser::VarContext * ctx) @@ -52,41 +59,68 @@ Variable getVariable(TocParser::VarContext * ctx) result.type = getType(ctx->type()); return result; } -Body getBody(TocParser::BodyContext * ctx) +Body getBody(TocParser::BodyContext * ctx, std::shared_ptr parent) { Body result; + result.ctx = std::make_unique(); + result.ctx->parent = parent; for (auto s : ctx->stmt()) { if (s->varDecl() != nullptr) { - result.variables.push_back(getVariable(s->varDecl()->var())); + result.ctx->variables.push_back(getVariable(s->varDecl()->var())); if (s->varDecl()->var()->expr() != nullptr) - result.statements.push_back(getStmt(s)); + result.statements.push_back(getStmt(s, result.ctx)); } else { - result.statements.push_back(getStmt(s)); + result.statements.push_back(getStmt(s, result.ctx)); } } return result; } -Function getFunction(TocParser::FuncContext * ctx) +Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr parent) { Function result; result.name = ctx->funcName()->NAME()->toString(); result.returnType = getType(ctx->type()); + if (ctx->genericDecl() != nullptr) + { + for (auto t : ctx->genericDecl()->typeName()) + { + result.genericTypeNames.push_back(t->getText()); + } + } + if (!ctx->parameter()->var().empty()) { for (auto p : ctx->parameter()->var()) result.parameters.push_back(getVariable(p)); } - result.body = getBody(ctx->body()); + + if (ctx->body() != nullptr) + { + result.body = getBody(ctx->body(), parent); + result.defined = true; + } + else + { + result.defined = false; + } return result; } -Struct getStruct(TocParser::StructDeclContext * ctx) +Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr parent) { Struct result; result.name = ctx->structName()->NAME()->toString(); + if (ctx->genericDecl() != nullptr) + { + for (auto t : ctx->genericDecl()->typeName()) + { + result.genericTypeNames.push_back(t->getText()); + } + } + for (auto m : ctx->structMember()) { if (m->structVar() != nullptr) @@ -99,58 +133,61 @@ Struct getStruct(TocParser::StructDeclContext * ctx) if (m->structMethod() != nullptr) { result.methods.push_back({ - getFunction(m->structMethod()->func()), + getFunction(m->structMethod()->func(), parent), m->privateDecl() != nullptr }); } } return result; } -Namespace getNamespace(TocParser::NamespaceDeclContext * ctx) +Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr parent) { Namespace result; + result.ctx = std::make_unique(); result.name = ctx->typeName()->getText(); + result.ctx->name = result.name; for (auto d : ctx->decl()) { if (d->varDecl() != nullptr) { - result.variables.push_back(getVariable(d->varDecl()->var())); + result.ctx->variables.push_back(getVariable(d->varDecl()->var())); } if (d->funcDecl() != nullptr) { - result.functions.push_back(getFunction(d->funcDecl()->func())); + result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx)); } if (d->structDecl() != nullptr) { - result.structs.push_back(getStruct(d->structDecl())); + result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx)); } if (d->namespaceDecl() != nullptr) { - result.namespaces.push_back(getNamespace(d->namespaceDecl())); + result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); } } return result; } -Program getProgram(TocParser::ProgContext * ctx) +Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr parent) { Program result; + result.ctx = std::make_unique(); for (auto d : ctx->decl()) { if (d->varDecl() != nullptr) { - result.variables.push_back(getVariable(d->varDecl()->var())); + result.ctx->variables.push_back(getVariable(d->varDecl()->var())); } if (d->funcDecl() != nullptr) { - result.functions.push_back(getFunction(d->funcDecl()->func())); + result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx)); } if (d->structDecl() != nullptr) { - result.structs.push_back(getStruct(d->structDecl())); + result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx)); } if (d->namespaceDecl() != nullptr) { - result.namespaces.push_back(getNamespace(d->namespaceDecl())); + result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx)); } } return result; @@ -187,6 +224,13 @@ Expr getExpr(TocParser::FuncExprContext * ctx) for (auto n : ctx->namespaceSpecifier()) result._func.namespacePrefixes.push_back(n->typeName()->getText()); result._func.functionName = ctx->funcName()->NAME()->toString(); + if (ctx->genericInstantiation() != nullptr) + { + for (auto g : ctx->genericInstantiation()->type()) + { + result._func.genericInstantiation.push_back(getType(g)); + } + } for (auto e : ctx->expr()) result._func.arguments.push_back(getExpr(e)); return result; @@ -197,6 +241,13 @@ Expr getExpr(TocParser::MethodExprContext * ctx) result.type = ExprType::Method; result._method.expr = std::make_unique(getExpr(ctx->expr(0))); result._method.methodName = ctx->funcName()->NAME()->toString(); + if (ctx->genericInstantiation() != nullptr) + { + for (auto g : ctx->genericInstantiation()->type()) + { + result._method.genericInstantiation.push_back(getType(g)); + } + } for (int i = 1; i < ctx->expr().size(); i++) result._method.arguments.push_back(getExpr(ctx->expr(i))); return result; @@ -215,10 +266,10 @@ Expr getExpr(TocParser::LitExprContext * ctx) result._lit.type = LitType::Decimal; result._lit._decimal = atof(ctx->literal()->DECIMAL_LIT()->toString().c_str()); } - else if (ctx->literal()->STRING_LIT() != nullptr) + else if (ctx->literal()->StringLit() != nullptr) { result._lit.type = LitType::String; - result._lit._string = ctx->literal()->STRING_LIT()->toString(); + result._lit._string = ctx->literal()->StringLit()->toString(); } else if (ctx->literal()->BOOL_LIT() != nullptr) { @@ -240,6 +291,7 @@ Expr getExpr(TocParser::DotExprContext * ctx) result.type = ExprType::Dot; result._dot.expr = std::make_unique(getExpr(ctx->expr())); result._dot.identifier = ctx->varName()->getText(); + result._dot.isPointer = ctx->arrow() != nullptr; return result; } Expr getExpr(TocParser::PrefixOpExprContext * ctx) @@ -337,7 +389,7 @@ Expr getExpr(TocParser::ExprContext * ctx) result = getExpr(dynamic_cast(ctx)); return result; } -Stmt getStmt(TocParser::StmtContext * ctx) +Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr parent) { Stmt result; if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) @@ -351,13 +403,13 @@ Stmt getStmt(TocParser::StmtContext * ctx) { result.type = StmtType::If; result._if.condition = getExpr(ctx->ifStmt()->expr()); - result._if.body = getBody(ctx->ifStmt()->body()); + result._if.body = getBody(ctx->ifStmt()->body(), parent); for (auto ei : ctx->ifStmt()->elseIfStmt()) { result._if.elses.emplace_back( true, std::make_unique(getExpr(ei->expr())), - getBody(ei->body()) + getBody(ei->body(), parent) ); } if (ctx->ifStmt()->elseStmt() != nullptr) @@ -365,7 +417,7 @@ Stmt getStmt(TocParser::StmtContext * ctx) result._if.elses.emplace_back( false, nullptr, - getBody(ctx->ifStmt()->elseStmt()->body()) + getBody(ctx->ifStmt()->elseStmt()->body(), parent) ); } } @@ -377,7 +429,7 @@ Stmt getStmt(TocParser::StmtContext * ctx) { result._switch.cases.emplace_back( std::make_unique(getExpr(c->expr())), - getBody(c->body()) + getBody(c->body(), parent) ); } } @@ -390,13 +442,13 @@ Stmt getStmt(TocParser::StmtContext * ctx) result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr()); result._for.condition = std::make_unique(getExpr(ctx->forStmt()->expr(0))); result._for.action = std::make_unique(getExpr(ctx->forStmt()->expr(1))); - result._for.body = getBody(ctx->forStmt()->body()); + result._for.body = getBody(ctx->forStmt()->body(), parent); } if (ctx->whileStmt() != nullptr) { result.type = StmtType::While; result._while.condition = getExpr(ctx->whileStmt()->expr()); - result._while.body = getBody(ctx->whileStmt()->body()); + result._while.body = getBody(ctx->whileStmt()->body(), parent); } if (ctx->assignStmt() != nullptr) {