+UnaryOperatorType getUnaryOperatorType(const std::string & s) {\r
+ for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++) {\r
+ if (UnaryOperatorTypeStrings[i] == s) {\r
+ return (UnaryOperatorType)i;\r
+ }\r
+ }\r
+ return UnaryOperatorType::COUNT;\r
+}\r
+BinaryOperatorType getBinaryOperatorType(const std::string & s) {\r
+ for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++) {\r
+ if (BinaryOperatorTypeStrings[i] == s) {\r
+ return (BinaryOperatorType)i;\r
+ }\r
+ }\r
+ return BinaryOperatorType::COUNT;\r
+}\r
+UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+ UnaryOperatorExpr result;\r
+ if (ctx->prefixOp() != nullptr)\r
+ result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));\r
+ else\r
+ result.expr = std::make_unique<Expr>(getExpr(ctx->postfixOp()->nonOpExpr()));\r
+\r
+ std::string op;\r
+ if (ctx->prefixOp() != nullptr)\r
+ op = ctx->prefixOp()->prefix_op()->getText();\r
+ else\r
+ op = ctx->postfixOp()->postfix_op()->getText();\r
+\r
+ // TODO: postfix type\r
+\r
+ result.type = getUnaryOperatorType(op);\r
+\r
+ return result;\r
+}\r
+BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+ BinaryOperatorExpr result;\r
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));\r
+ result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));\r
+ \r
+ std::string op = ctx->binaryOp()->binary_op(0)->getText();\r
+ std::cout << op << std::endl;\r
+\r
+ result.type = getBinaryOperatorType(op);\r
+\r
+ return result;\r
+}\r
+TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+ TernaryOperatorExpr result;\r
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->nonOpExpr()));\r
+ result.rexprTrue = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(0)));\r
+ result.rexprFalse = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(1)));\r
+ return result;\r
+}\r
+Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
+ Expr result;\r
+ if (ctx->funcExpr() != nullptr) {\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
+ result.type = ExprType::Lit;\r
+ if (ctx->litExpr()->INT_LIT() != nullptr) {\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
+ 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
+ result._lit.type = LitType::String;\r
+ result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
+ }\r
+ else if (ctx->litExpr()->BOOL_LIT() != nullptr) {\r
+ result._lit.type = LitType::Bool;\r
+ result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
+ }\r
+ }\r
+ if (ctx->identifierExpr() != nullptr) {\r
+ result.type = ExprType::Identifier;\r
+ result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->parenExpr() != nullptr) {\r
+ result = getExpr(ctx->parenExpr()->expr());\r
+ }\r
+ if (ctx->accessExpr() != nullptr) {\r
+ // TODO: access chain\r
+ for (auto sub : ctx->accessExpr()->accessSubExpr()) {\r
+ if (sub->accessMember() != nullptr) {\r
+ result.type = ExprType::Dot;\r
+ result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ else {\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(sub->accessBrackets()->expr()));\r
+ }\r
+ }\r
+ }\r
+ return result;\r
+}\r
+Expr getExpr(TocParser::NonAccessExprContext * ctx) {\r
+ Expr result;\r
+ if (ctx->funcExpr() != nullptr) {\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
+ result.type = ExprType::Identifier;\r
+ result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->parenExpr() != nullptr) {\r
+ result = getExpr(ctx->parenExpr()->expr());\r
+ }\r