5 Type getType(TocParser::TypeContext * ctx);
\r
6 Variable getVariable(TocParser::VarContext * ctx);
\r
7 Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);
\r
8 Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent);
\r
9 Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent);
\r
10 Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent);
\r
11 Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent);
\r
14 Expr getExpr(TocParser::FuncExprContext * ctx);
\r
15 Expr getExpr(TocParser::MethodExprContext * ctx);
\r
16 Expr getExpr(TocParser::LitExprContext * ctx);
\r
17 Expr getExpr(TocParser::ParenExprContext * ctx);
\r
18 Expr getExpr(TocParser::DotExprContext * ctx);
\r
19 Expr getExpr(TocParser::PrefixOpExprContext * ctx);
\r
20 Expr getExpr(TocParser::PostfixOpExprContext * ctx);
\r
21 Expr getExpr(TocParser::BinaryOpExprContext * ctx);
\r
22 Expr getExpr(TocParser::TernaryOpExprContext * ctx);
\r
23 Expr getExpr(TocParser::BracketExprContext * ctx);
\r
24 Expr getExpr(TocParser::IdentifierExprContext * ctx);
\r
25 Expr getExpr(TocParser::ExprContext * ctx);
\r
27 Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);
\r
29 Type getType(TocParser::TypeContext * ctx)
\r
32 for (auto n : ctx->namespaceSpecifier())
\r
33 result.namespacePrefixes.push_back(n->typeName()->getText());
\r
34 result.name = ctx->typeName()->NAME()->toString();
\r
35 for (auto m : ctx->typeModifier())
\r
37 bool isPointer = m->getText() == "*";
\r
38 bool isStaticArray = m->INT_LIT() != nullptr;
\r
40 result.modifiers.emplace_back(
\r
41 isPointer ? TypeModifierType::Pointer : TypeModifierType::Array,
\r
43 isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1
\r
46 if (ctx->genericInstantiation() != nullptr)
\r
48 for (auto g : ctx->genericInstantiation()->type())
\r
50 result.genericInstantiation.push_back(getType(g));
\r
55 Variable getVariable(TocParser::VarContext * ctx)
\r
58 result.name = ctx->varName()->NAME()->toString();
\r
59 result.type = getType(ctx->type());
\r
62 Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent)
\r
65 result.ctx = std::make_unique<Context>();
\r
66 result.ctx->parent = parent;
\r
67 for (auto s : ctx->stmt())
\r
69 if (s->varDecl() != nullptr)
\r
71 result.ctx->variables.push_back(getVariable(s->varDecl()->var()));
\r
72 if (s->varDecl()->var()->expr() != nullptr)
\r
73 result.statements.push_back(getStmt(s, result.ctx));
\r
77 result.statements.push_back(getStmt(s, result.ctx));
\r
82 Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent)
\r
85 result.name = ctx->funcName()->NAME()->toString();
\r
86 result.returnType = getType(ctx->type());
\r
87 if (ctx->genericDecl() != nullptr)
\r
89 for (auto t : ctx->genericDecl()->typeName())
\r
91 result.genericTypeNames.push_back(t->getText());
\r
95 if (!ctx->parameter()->var().empty())
\r
97 for (auto p : ctx->parameter()->var())
\r
98 result.parameters.push_back(getVariable(p));
\r
101 if (ctx->body() != nullptr)
\r
103 result.body = getBody(ctx->body(), parent);
\r
104 result.defined = true;
\r
108 result.defined = false;
\r
112 Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent)
\r
115 result.name = ctx->structName()->NAME()->toString();
\r
116 if (ctx->genericDecl() != nullptr)
\r
118 for (auto t : ctx->genericDecl()->typeName())
\r
120 result.genericTypeNames.push_back(t->getText());
\r
124 for (auto m : ctx->structMember())
\r
126 if (m->structVar() != nullptr)
\r
128 result.members.push_back({
\r
129 getVariable(m->structVar()->var()),
\r
130 m->privateDecl() != nullptr
\r
133 if (m->structMethod() != nullptr)
\r
135 result.methods.push_back({
\r
136 getFunction(m->structMethod()->func(), parent),
\r
137 m->privateDecl() != nullptr
\r
143 Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent)
\r
146 result.ctx = std::make_unique<Context>();
\r
147 result.name = ctx->typeName()->getText();
\r
148 for (auto d : ctx->decl())
\r
150 if (d->varDecl() != nullptr)
\r
152 result.ctx->variables.push_back(getVariable(d->varDecl()->var()));
\r
154 if (d->funcDecl() != nullptr)
\r
156 result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));
\r
158 if (d->structDecl() != nullptr)
\r
160 result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));
\r
162 if (d->namespaceDecl() != nullptr)
\r
164 result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));
\r
169 Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent)
\r
172 result.ctx = std::make_unique<Context>();
\r
173 for (auto d : ctx->decl())
\r
175 if (d->varDecl() != nullptr)
\r
177 result.ctx->variables.push_back(getVariable(d->varDecl()->var()));
\r
179 if (d->funcDecl() != nullptr)
\r
181 result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));
\r
183 if (d->structDecl() != nullptr)
\r
185 result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));
\r
187 if (d->namespaceDecl() != nullptr)
\r
189 result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));
\r
194 template<typename OpType>
\r
195 OpType getOperatorType(const std::string & s, std::string typeStrings[])
\r
197 for (int i = 0; i < (int)OpType::COUNT; i++)
\r
199 if (typeStrings[i] == s)
\r
204 return OpType::COUNT;
\r
219 Expr getExpr(TocParser::FuncExprContext * ctx)
\r
222 result.type = ExprType::Func;
\r
223 for (auto n : ctx->namespaceSpecifier())
\r
224 result._func.namespacePrefixes.push_back(n->typeName()->getText());
\r
225 result._func.functionName = ctx->funcName()->NAME()->toString();
\r
226 if (ctx->genericInstantiation() != nullptr)
\r
228 for (auto g : ctx->genericInstantiation()->type())
\r
230 result._func.genericInstantiation.push_back(getType(g));
\r
233 for (auto e : ctx->expr())
\r
234 result._func.arguments.push_back(getExpr(e));
\r
237 Expr getExpr(TocParser::MethodExprContext * ctx)
\r
240 result.type = ExprType::Method;
\r
241 result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
242 result._method.methodName = ctx->funcName()->NAME()->toString();
\r
243 if (ctx->genericInstantiation() != nullptr)
\r
245 for (auto g : ctx->genericInstantiation()->type())
\r
247 result._method.genericInstantiation.push_back(getType(g));
\r
250 for (int i = 1; i < ctx->expr().size(); i++)
\r
251 result._method.arguments.push_back(getExpr(ctx->expr(i)));
\r
254 Expr getExpr(TocParser::LitExprContext * ctx)
\r
257 result.type = ExprType::Lit;
\r
258 if (ctx->literal()->INT_LIT() != nullptr)
\r
260 result._lit.type = LitType::Int;
\r
261 result._lit._int = atoi(ctx->literal()->INT_LIT()->toString().c_str());
\r
263 else if (ctx->literal()->DECIMAL_LIT() != nullptr)
\r
265 result._lit.type = LitType::Decimal;
\r
266 result._lit._decimal = atof(ctx->literal()->DECIMAL_LIT()->toString().c_str());
\r
268 else if (ctx->literal()->StringLit() != nullptr)
\r
270 result._lit.type = LitType::String;
\r
271 result._lit._string = ctx->literal()->StringLit()->toString();
\r
273 else if (ctx->literal()->BOOL_LIT() != nullptr)
\r
275 result._lit.type = LitType::Bool;
\r
276 result._lit._bool = ctx->literal()->BOOL_LIT()->toString() == "true";
\r
280 Expr getExpr(TocParser::ParenExprContext * ctx)
\r
283 result.type = ExprType::Paren;
\r
284 result._paren.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
287 Expr getExpr(TocParser::DotExprContext * ctx)
\r
290 result.type = ExprType::Dot;
\r
291 result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
292 result._dot.identifier = ctx->varName()->getText();
\r
293 result._dot.isPointer = ctx->arrow() != nullptr;
\r
296 Expr getExpr(TocParser::PrefixOpExprContext * ctx)
\r
299 result.type = ExprType::PrefixOp;
\r
300 result._prefixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
301 result._prefixOp.type = getOperatorType<PrefixOperatorType>(
\r
302 ctx->prefix_op()->getText(),
\r
303 PrefixOperatorTypeStrings);
\r
306 Expr getExpr(TocParser::PostfixOpExprContext * ctx)
\r
309 result.type = ExprType::PostfixOp;
\r
310 result._postfixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
311 result._postfixOp.type = getOperatorType<PostfixOperatorType>(
\r
312 ctx->postfix_op()->getText(),
\r
313 PostfixOperatorTypeStrings);
\r
316 Expr getExpr(TocParser::BinaryOpExprContext * ctx)
\r
319 result.type = ExprType::BinaryOp;
\r
320 result._binaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
321 result._binaryOp.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
322 result._binaryOp.type = getOperatorType<BinaryOperatorType>(
\r
323 ctx->binary_op()->getText(),
\r
324 BinaryOperatorTypeStrings);
\r
327 Expr getExpr(TocParser::TernaryOpExprContext * ctx)
\r
330 result.type = ExprType::TernaryOp;
\r
331 result._ternaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
332 result._ternaryOp.rexprTrue = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
333 result._ternaryOp.rexprFalse = std::make_unique<Expr>(getExpr(ctx->expr(2)));
\r
336 Expr getExpr(TocParser::BracketExprContext * ctx)
\r
339 result.type = ExprType::Bracket;
\r
340 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
341 result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
344 Expr getExpr(TocParser::IdentifierExprContext * ctx)
\r
347 result.type = ExprType::Identifier;
\r
348 for (auto n : ctx->namespaceSpecifier())
\r
349 result._identifier.namespacePrefixes.push_back(n->typeName()->getText());
\r
350 result._identifier.identifier = ctx->varName()->getText();
\r
364 Expr getExpr(TocParser::ExprContext * ctx)
\r
367 if (dynamic_cast<TocParser::FuncExprContext *>(ctx) != nullptr)
\r
368 result = getExpr(dynamic_cast<TocParser::FuncExprContext *>(ctx));
\r
369 if (dynamic_cast<TocParser::MethodExprContext *>(ctx) != nullptr)
\r
370 result = getExpr(dynamic_cast<TocParser::MethodExprContext *>(ctx));
\r
371 if (dynamic_cast<TocParser::LitExprContext *>(ctx) != nullptr)
\r
372 result = getExpr(dynamic_cast<TocParser::LitExprContext *>(ctx));
\r
373 if (dynamic_cast<TocParser::ParenExprContext *>(ctx) != nullptr)
\r
374 result = getExpr(dynamic_cast<TocParser::ParenExprContext *>(ctx));
\r
375 if (dynamic_cast<TocParser::DotExprContext *>(ctx) != nullptr)
\r
376 result = getExpr(dynamic_cast<TocParser::DotExprContext *>(ctx));
\r
377 if (dynamic_cast<TocParser::PrefixOpExprContext *>(ctx) != nullptr)
\r
378 result = getExpr(dynamic_cast<TocParser::PrefixOpExprContext *>(ctx));
\r
379 if (dynamic_cast<TocParser::PostfixOpExprContext *>(ctx) != nullptr)
\r
380 result = getExpr(dynamic_cast<TocParser::PostfixOpExprContext *>(ctx));
\r
381 if (dynamic_cast<TocParser::BinaryOpExprContext *>(ctx) != nullptr)
\r
382 result = getExpr(dynamic_cast<TocParser::BinaryOpExprContext *>(ctx));
\r
383 if (dynamic_cast<TocParser::TernaryOpExprContext *>(ctx) != nullptr)
\r
384 result = getExpr(dynamic_cast<TocParser::TernaryOpExprContext *>(ctx));
\r
385 if (dynamic_cast<TocParser::BracketExprContext *>(ctx) != nullptr)
\r
386 result = getExpr(dynamic_cast<TocParser::BracketExprContext *>(ctx));
\r
387 if (dynamic_cast<TocParser::IdentifierExprContext *>(ctx) != nullptr)
\r
388 result = getExpr(dynamic_cast<TocParser::IdentifierExprContext *>(ctx));
\r
391 Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent)
\r
394 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)
\r
396 result.type = StmtType::Assign;
\r
397 result._assign.lexpr.type = ExprType::Identifier;
\r
398 result._assign.lexpr._identifier.identifier = ctx->varDecl()->var()->varName()->getText();
\r
399 result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());
\r
401 if (ctx->ifStmt() != nullptr)
\r
403 result.type = StmtType::If;
\r
404 result._if.condition = getExpr(ctx->ifStmt()->expr());
\r
405 result._if.body = getBody(ctx->ifStmt()->body(), parent);
\r
406 for (auto ei : ctx->ifStmt()->elseIfStmt())
\r
408 result._if.elses.emplace_back(
\r
410 std::make_unique<Expr>(getExpr(ei->expr())),
\r
411 getBody(ei->body(), parent)
\r
414 if (ctx->ifStmt()->elseStmt() != nullptr)
\r
416 result._if.elses.emplace_back(
\r
419 getBody(ctx->ifStmt()->elseStmt()->body(), parent)
\r
423 if (ctx->switchStmt() != nullptr)
\r
425 result.type = StmtType::Switch;
\r
426 result._switch.ident = std::make_unique<Expr>(getExpr(ctx->switchStmt()->expr()));
\r
427 for (auto c : ctx->switchStmt()->switchBody()->switchCase())
\r
429 result._switch.cases.emplace_back(
\r
430 std::make_unique<Expr>(getExpr(c->expr())),
\r
431 getBody(c->body(), parent)
\r
435 if (ctx->forStmt() != nullptr)
\r
437 result.type = StmtType::For;
\r
438 result._for.init = std::make_unique<AssignStmt>();
\r
439 result._for.init->lexpr.type = ExprType::Identifier;
\r
440 result._for.init->lexpr._identifier.identifier = ctx->forStmt()->varInit()->varName()->getText();
\r
441 result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr());
\r
442 result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));
\r
443 result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));
\r
444 result._for.body = getBody(ctx->forStmt()->body(), parent);
\r
446 if (ctx->whileStmt() != nullptr)
\r
448 result.type = StmtType::While;
\r
449 result._while.condition = getExpr(ctx->whileStmt()->expr());
\r
450 result._while.body = getBody(ctx->whileStmt()->body(), parent);
\r
452 if (ctx->assignStmt() != nullptr)
\r
454 result.type = StmtType::Assign;
\r
455 result._assign.lexpr = getExpr(ctx->assignStmt()->expr(0));
\r
456 result._assign.rexpr = getExpr(ctx->assignStmt()->expr(1));
\r
458 if (ctx->returnStmt() != nullptr)
\r
460 result.type = StmtType::Return;
\r
461 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
463 if (ctx->expr() != nullptr)
\r
465 result.type = StmtType::Expr;
\r
466 result._expr = getExpr(ctx->expr());
\r