\r
#include "repr.h"\r
\r
-Type getType(TocParser::TypeContext * ctx);\r
-Variable getVariable(TocParser::VarContext * ctx);\r
-Body getBody(TocParser::BodyContext * ctx);\r
-Function getFunction(TocParser::FuncContext * ctx);\r
-Struct getStruct(TocParser::StructDeclContext * ctx);\r
-Program getProgram(TocParser::ProgContext * ctx);\r
+Type getType(TocParser::TypeContext * ctx);\r
+Variable getVariable(TocParser::VarContext * ctx);\r
+Body getBody(TocParser::BodyContext * ctx);\r
+Function getFunction(TocParser::FuncContext * ctx);\r
+Struct getStruct(TocParser::StructDeclContext * ctx);\r
+Program getProgram(TocParser::ProgContext * ctx);\r
OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx);\r
-Expr getExpression(TocParser::ExprContext * ctx);\r
-Stmt getStmt(TocParser::StmtContext * ctx);\r
+Expr getExpr(TocParser::NonOpExprContext * ctx);\r
+Expr getExpr(TocParser::NonSubscriptExprContext * ctx);\r
+Expr getExpr(TocParser::ExprContext * ctx);\r
+Stmt getStmt(TocParser::StmtContext * ctx);\r
\r
Type getType(TocParser::TypeContext * ctx) {\r
Type result;\r
Function getFunction(TocParser::FuncContext * ctx) {\r
Function result;\r
result.name = ctx->funcName()->NAME()->toString();\r
- if (ctx->parameter()->firstParameter() != nullptr) {\r
- result.parameters.push_back(getVariable(ctx->parameter()->firstParameter()->var()));\r
- for (auto p : ctx->parameter()->additionalParameter())\r
- result.parameters.push_back(getVariable(p->var()));\r
+ result.returnType = getType(ctx->type());\r
+ if (!ctx->parameter()->var().empty()) {\r
+ for (auto p : ctx->parameter()->var())\r
+ result.parameters.push_back(getVariable(p));\r
}\r
result.body = getBody(ctx->body());\r
return result;\r
}\r
OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {\r
OperatorExpr result;\r
- //result.lexpr = getExpr(ctx->binaryOperator()->nonOpExpr(0));\r
- //result.rexpr = getExpr(ctx->binaryOperator()->nonOpExpr(1));\r
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));\r
+ result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(1)));\r
+ \r
std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString();\r
if (op == "+") result.type = OperatorType::Plus;\r
if (op == "-") result.type = OperatorType::Minus;\r
if (op == ">") result.type = OperatorType::GreaterThan;\r
return result;\r
}\r
+Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
+ Expr result;\r
+ if (ctx->funcCall() != nullptr) {\r
+ result.type = ExprType::Call;\r
+ for (auto e : ctx->funcCall()->expr())\r
+ result._call.arguments.push_back(getExpr(e));\r
+ result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
+ }\r
+ if (ctx->literal() != nullptr) {\r
+ result.type = ExprType::Literal;\r
+ result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
+ }\r
+ if (ctx->identifier() != nullptr) {\r
+ result.type = ExprType::Variable;\r
+ result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->subscript() != nullptr) {\r
+ result.type = ExprType::Brackets;\r
+ result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
+ result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
+ }\r
+ if (ctx->memberAccess() != nullptr) {\r
+ result.type = ExprType::Dot;\r
+ Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
+ result._dot.lexpr = std::make_unique<Expr>(e);\r
+ result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
+ }\r
+ return result;\r
+}\r
+Expr getExpr(TocParser::NonSubscriptExprContext * ctx) {\r
+ Expr result;\r
+ if (ctx->funcCall() != nullptr) {\r
+ result.type = ExprType::Call;\r
+ for (auto e : ctx->funcCall()->expr())\r
+ result._call.arguments.push_back(getExpr(e));\r
+ result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
+ }\r
+ if (ctx->literal() != nullptr) {\r
+ result.type = ExprType::Literal;\r
+ result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
+ }\r
+ if (ctx->identifier() != nullptr) {\r
+ result.type = ExprType::Variable;\r
+ result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
+ }\r
+ if (ctx->memberAccess() != nullptr) {\r
+ result.type = ExprType::Dot;\r
+ Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
+ result._dot.lexpr = std::make_unique<Expr>(e);\r
+ result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
+ }\r
+ return result;\r
+}\r
Expr getExpr(TocParser::ExprContext * ctx) {\r
Expr result;\r
if (ctx->funcCall() != nullptr) {\r
result.type = ExprType::Call;\r
for (auto e : ctx->funcCall()->expr())\r
result._call.arguments.push_back(getExpr(e));\r
- //result._call.function = ctx->funcCall()->funcName();\r
+ result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
}\r
if (ctx->literal() != nullptr) {\r
result.type = ExprType::Literal;\r
}\r
if (ctx->subscript() != nullptr) {\r
result.type = ExprType::Brackets;\r
- //result._brackets.lexpr = getExpr(ctx->subscript()->nonSubscriptExpr());\r
+ result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
}\r
if (ctx->memberAccess() != nullptr) {\r
result.type = ExprType::Dot;\r
- //result._dot.lexpr = ctx->memberAccess()->identifier(0);\r
+ Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
+ result._dot.lexpr = std::make_unique<Expr>(e);\r
result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
}\r
if (ctx->operatorExpr() != nullptr) {\r