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 result.ctx->name = result.name;
\r
149 for (auto d : ctx->decl())
\r
151 if (d->varDecl() != nullptr)
\r
153 result.ctx->variables.push_back(getVariable(d->varDecl()->var()));
\r
155 if (d->funcDecl() != nullptr)
\r
157 result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));
\r
159 if (d->structDecl() != nullptr)
\r
161 result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));
\r
163 if (d->namespaceDecl() != nullptr)
\r
165 result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));
\r
170 Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent)
\r
173 result.ctx = std::make_unique<Context>();
\r
174 for (auto d : ctx->decl())
\r
176 if (d->varDecl() != nullptr)
\r
178 result.ctx->variables.push_back(getVariable(d->varDecl()->var()));
\r
180 if (d->funcDecl() != nullptr)
\r
182 result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));
\r
184 if (d->structDecl() != nullptr)
\r
186 result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));
\r
188 if (d->namespaceDecl() != nullptr)
\r
190 result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));
\r
195 template<typename OpType>
\r
196 OpType getOperatorType(const std::string & s, std::string typeStrings[])
\r
198 for (int i = 0; i < (int)OpType::COUNT; i++)
\r
200 if (typeStrings[i] == s)
\r
205 return OpType::COUNT;
\r
220 Expr getExpr(TocParser::FuncExprContext * ctx)
\r
223 result.type = ExprType::Func;
\r
224 for (auto n : ctx->namespaceSpecifier())
\r
225 result._func.namespacePrefixes.push_back(n->typeName()->getText());
\r
226 result._func.functionName = ctx->funcName()->NAME()->toString();
\r
227 if (ctx->genericInstantiation() != nullptr)
\r
229 for (auto g : ctx->genericInstantiation()->type())
\r
231 result._func.genericInstantiation.push_back(getType(g));
\r
234 for (auto e : ctx->expr())
\r
235 result._func.arguments.push_back(getExpr(e));
\r
238 Expr getExpr(TocParser::MethodExprContext * ctx)
\r
241 result.type = ExprType::Method;
\r
242 result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
243 result._method.methodName = ctx->funcName()->NAME()->toString();
\r
244 if (ctx->genericInstantiation() != nullptr)
\r
246 for (auto g : ctx->genericInstantiation()->type())
\r
248 result._method.genericInstantiation.push_back(getType(g));
\r
251 for (int i = 1; i < ctx->expr().size(); i++)
\r
252 result._method.arguments.push_back(getExpr(ctx->expr(i)));
\r
255 Expr getExpr(TocParser::LitExprContext * ctx)
\r
258 result.type = ExprType::Lit;
\r
259 if (ctx->literal()->INT_LIT() != nullptr)
\r
261 result._lit.type = LitType::Int;
\r
262 result._lit._int = atoi(ctx->literal()->INT_LIT()->toString().c_str());
\r
264 else if (ctx->literal()->DECIMAL_LIT() != nullptr)
\r
266 result._lit.type = LitType::Decimal;
\r
267 result._lit._decimal = atof(ctx->literal()->DECIMAL_LIT()->toString().c_str());
\r
269 else if (ctx->literal()->StringLit() != nullptr)
\r
271 result._lit.type = LitType::String;
\r
272 result._lit._string = ctx->literal()->StringLit()->toString();
\r
274 else if (ctx->literal()->BOOL_LIT() != nullptr)
\r
276 result._lit.type = LitType::Bool;
\r
277 result._lit._bool = ctx->literal()->BOOL_LIT()->toString() == "true";
\r
281 Expr getExpr(TocParser::ParenExprContext * ctx)
\r
284 result.type = ExprType::Paren;
\r
285 result._paren.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
288 Expr getExpr(TocParser::DotExprContext * ctx)
\r
291 result.type = ExprType::Dot;
\r
292 result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
293 result._dot.identifier = ctx->varName()->getText();
\r
294 result._dot.isPointer = ctx->arrow() != nullptr;
\r
297 Expr getExpr(TocParser::PrefixOpExprContext * ctx)
\r
300 result.type = ExprType::PrefixOp;
\r
301 result._prefixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
302 result._prefixOp.type = getOperatorType<PrefixOperatorType>(
\r
303 ctx->prefix_op()->getText(),
\r
304 PrefixOperatorTypeStrings);
\r
307 Expr getExpr(TocParser::PostfixOpExprContext * ctx)
\r
310 result.type = ExprType::PostfixOp;
\r
311 result._postfixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));
\r
312 result._postfixOp.type = getOperatorType<PostfixOperatorType>(
\r
313 ctx->postfix_op()->getText(),
\r
314 PostfixOperatorTypeStrings);
\r
317 Expr getExpr(TocParser::BinaryOpExprContext * ctx)
\r
320 result.type = ExprType::BinaryOp;
\r
321 result._binaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
322 result._binaryOp.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
323 result._binaryOp.type = getOperatorType<BinaryOperatorType>(
\r
324 ctx->binary_op()->getText(),
\r
325 BinaryOperatorTypeStrings);
\r
328 Expr getExpr(TocParser::TernaryOpExprContext * ctx)
\r
331 result.type = ExprType::TernaryOp;
\r
332 result._ternaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
333 result._ternaryOp.rexprTrue = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
334 result._ternaryOp.rexprFalse = std::make_unique<Expr>(getExpr(ctx->expr(2)));
\r
337 Expr getExpr(TocParser::BracketExprContext * ctx)
\r
340 result.type = ExprType::Bracket;
\r
341 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));
\r
342 result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));
\r
345 Expr getExpr(TocParser::IdentifierExprContext * ctx)
\r
348 result.type = ExprType::Identifier;
\r
349 for (auto n : ctx->namespaceSpecifier())
\r
350 result._identifier.namespacePrefixes.push_back(n->typeName()->getText());
\r
351 result._identifier.identifier = ctx->varName()->getText();
\r
365 Expr getExpr(TocParser::ExprContext * ctx)
\r
368 if (dynamic_cast<TocParser::FuncExprContext *>(ctx) != nullptr)
\r
369 result = getExpr(dynamic_cast<TocParser::FuncExprContext *>(ctx));
\r
370 if (dynamic_cast<TocParser::MethodExprContext *>(ctx) != nullptr)
\r
371 result = getExpr(dynamic_cast<TocParser::MethodExprContext *>(ctx));
\r
372 if (dynamic_cast<TocParser::LitExprContext *>(ctx) != nullptr)
\r
373 result = getExpr(dynamic_cast<TocParser::LitExprContext *>(ctx));
\r
374 if (dynamic_cast<TocParser::ParenExprContext *>(ctx) != nullptr)
\r
375 result = getExpr(dynamic_cast<TocParser::ParenExprContext *>(ctx));
\r
376 if (dynamic_cast<TocParser::DotExprContext *>(ctx) != nullptr)
\r
377 result = getExpr(dynamic_cast<TocParser::DotExprContext *>(ctx));
\r
378 if (dynamic_cast<TocParser::PrefixOpExprContext *>(ctx) != nullptr)
\r
379 result = getExpr(dynamic_cast<TocParser::PrefixOpExprContext *>(ctx));
\r
380 if (dynamic_cast<TocParser::PostfixOpExprContext *>(ctx) != nullptr)
\r
381 result = getExpr(dynamic_cast<TocParser::PostfixOpExprContext *>(ctx));
\r
382 if (dynamic_cast<TocParser::BinaryOpExprContext *>(ctx) != nullptr)
\r
383 result = getExpr(dynamic_cast<TocParser::BinaryOpExprContext *>(ctx));
\r
384 if (dynamic_cast<TocParser::TernaryOpExprContext *>(ctx) != nullptr)
\r
385 result = getExpr(dynamic_cast<TocParser::TernaryOpExprContext *>(ctx));
\r
386 if (dynamic_cast<TocParser::BracketExprContext *>(ctx) != nullptr)
\r
387 result = getExpr(dynamic_cast<TocParser::BracketExprContext *>(ctx));
\r
388 if (dynamic_cast<TocParser::IdentifierExprContext *>(ctx) != nullptr)
\r
389 result = getExpr(dynamic_cast<TocParser::IdentifierExprContext *>(ctx));
\r
392 Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent)
\r
395 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)
\r
397 result.type = StmtType::Assign;
\r
398 result._assign.lexpr.type = ExprType::Identifier;
\r
399 result._assign.lexpr._identifier.identifier = ctx->varDecl()->var()->varName()->getText();
\r
400 result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());
\r
402 if (ctx->ifStmt() != nullptr)
\r
404 result.type = StmtType::If;
\r
405 result._if.condition = getExpr(ctx->ifStmt()->expr());
\r
406 result._if.body = getBody(ctx->ifStmt()->body(), parent);
\r
407 for (auto ei : ctx->ifStmt()->elseIfStmt())
\r
409 result._if.elses.emplace_back(
\r
411 std::make_unique<Expr>(getExpr(ei->expr())),
\r
412 getBody(ei->body(), parent)
\r
415 if (ctx->ifStmt()->elseStmt() != nullptr)
\r
417 result._if.elses.emplace_back(
\r
420 getBody(ctx->ifStmt()->elseStmt()->body(), parent)
\r
424 if (ctx->switchStmt() != nullptr)
\r
426 result.type = StmtType::Switch;
\r
427 result._switch.ident = std::make_unique<Expr>(getExpr(ctx->switchStmt()->expr()));
\r
428 for (auto c : ctx->switchStmt()->switchBody()->switchCase())
\r
430 result._switch.cases.emplace_back(
\r
431 std::make_unique<Expr>(getExpr(c->expr())),
\r
432 getBody(c->body(), parent)
\r
436 if (ctx->forStmt() != nullptr)
\r
438 result.type = StmtType::For;
\r
439 result._for.init = std::make_unique<AssignStmt>();
\r
440 result._for.init->lexpr.type = ExprType::Identifier;
\r
441 result._for.init->lexpr._identifier.identifier = ctx->forStmt()->varInit()->varName()->getText();
\r
442 result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr());
\r
443 result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));
\r
444 result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));
\r
445 result._for.body = getBody(ctx->forStmt()->body(), parent);
\r
447 if (ctx->whileStmt() != nullptr)
\r
449 result.type = StmtType::While;
\r
450 result._while.condition = getExpr(ctx->whileStmt()->expr());
\r
451 result._while.body = getBody(ctx->whileStmt()->body(), parent);
\r
453 if (ctx->assignStmt() != nullptr)
\r
455 result.type = StmtType::Assign;
\r
456 result._assign.lexpr = getExpr(ctx->assignStmt()->expr(0));
\r
457 result._assign.rexpr = getExpr(ctx->assignStmt()->expr(1));
\r
459 if (ctx->returnStmt() != nullptr)
\r
461 result.type = StmtType::Return;
\r
462 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
464 if (ctx->expr() != nullptr)
\r
466 result.type = StmtType::Expr;
\r
467 result._expr = getExpr(ctx->expr());
\r