+Expr getExpr(TocParser::NonAccessExprContext * ctx)\r
+{\r
+ Expr result;\r
+ result.parenthesized = false;\r
+ if (ctx->funcExpr() != nullptr)\r
+ {\r
+ result.type = ExprType::Func;\r
+ result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();\r
+ for (auto e : ctx->funcExpr()->expr())\r
+ result._func.arguments.push_back(getExpr(e));\r
+ }\r
+ if (ctx->identifierExpr() != nullptr)\r
+ {\r
+ result.type = ExprType::Identifier;\r
+ result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->parenExpr() != nullptr)\r
+ {\r
+ result = getExpr(ctx->parenExpr()->expr());\r
+ result.parenthesized = true;\r
+ }\r
+ return result;\r
+}\r
+Expr getExpr(TocParser::ExprContext * ctx)\r
+{\r
+ Expr result;\r
+ result.parenthesized = false;\r
+ if (ctx->funcExpr() != nullptr)\r
+ {\r
+ result.type = ExprType::Func;\r
+ result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();\r
+ for (auto e : ctx->funcExpr()->expr())\r
+ result._func.arguments.push_back(getExpr(e));\r
+ }\r
+ if (ctx->litExpr() != nullptr)\r
+ {\r
+ result.type = ExprType::Lit;\r
+ if (ctx->litExpr()->INT_LIT() != nullptr)\r
+ {\r
+ result._lit.type = LitType::Int;\r
+ result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());\r
+ }\r
+ else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)\r
+ {\r
+ result._lit.type = LitType::Decimal;\r
+ result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());\r
+ }\r
+ else if (ctx->litExpr()->STRING_LIT() != nullptr)\r
+ {\r
+ result._lit.type = LitType::String;\r
+ result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
+ }\r
+ else if (ctx->litExpr()->BOOL_LIT() != nullptr)\r
+ {\r
+ result._lit.type = LitType::Bool;\r
+ result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
+ }\r
+ }\r
+ if (ctx->identifierExpr() != nullptr)\r
+ {\r
+ result.type = ExprType::Identifier;\r
+ result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->parenExpr() != nullptr)\r
+ {\r
+ result = getExpr(ctx->parenExpr()->expr());\r
+ result.parenthesized = true;\r
+ }\r
+ if (ctx->accessExpr() != nullptr)\r
+ {\r
+ auto firstSub = ctx->accessExpr()->accessSubExpr(0);\r
+ if (firstSub->accessMember() != nullptr)\r
+ {\r
+ result.type = ExprType::Dot;\r
+ result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
+ result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ else\r
+ {\r
+ result.type = ExprType::Brackets;\r
+ result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
+ result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));\r
+ }\r
+ for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)\r
+ {\r
+ Expr tmp = result;\r
+ auto sub = ctx->accessExpr()->accessSubExpr(i);\r
+ if (sub->accessMember() != nullptr)\r
+ {\r
+ result.type = ExprType::Dot;\r
+ result._dot.expr = std::make_unique<Expr>(tmp);\r
+ result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ else\r
+ {\r
+ result.type = ExprType::Brackets;\r
+ result._brackets.lexpr = std::make_unique<Expr>(tmp);\r
+ result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));\r
+ }\r
+ }\r
+ }\r
+ if (ctx->opExpr() != nullptr)\r
+ {\r
+ if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr)\r
+ {\r
+ result.type = ExprType::UnaryOperator;\r
+ result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());\r
+ }\r
+ else if (ctx->opExpr()->binaryOp() != nullptr)\r
+ {\r
+ result.type = ExprType::BinaryOperator;\r
+ result._binaryOperator = getBinaryOperatorExpr(ctx->opExpr());\r
+ for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++)\r
+ {\r
+ Expr tmp = result;\r
+ result._binaryOperator.lexpr = std::make_unique<Expr>(tmp);\r
+ result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText());\r
+ result._binaryOperator.rexpr = std::make_unique<Expr>(getExpr(ctx->opExpr()->binaryOp()->nonOpExpr(i+1)));\r
+ }\r
+ }\r
+ else if (ctx->opExpr()->ternaryOp() != nullptr)\r
+ {\r
+ result.type = ExprType::TernaryOperator;\r
+ result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());\r
+ }\r
+ }\r
+ return result;\r
+}\r
+Stmt getStmt(TocParser::StmtContext * ctx)\r
+{\r