\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
-OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * 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
+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
+UnaryOperatorType getUnaryOperatorType(const std::string & s);\r
+BinaryOperatorType getBinaryOperatorType(const std::string & s);\r
+UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx);\r
+BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx);\r
+TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx);\r
+Expr getExpr(TocParser::NonOpExprContext * ctx);\r
+Expr getExpr(TocParser::NonAccessExprContext * ctx);\r
+Expr getExpr(TocParser::ExprContext * ctx);\r
+Stmt getStmt(TocParser::StmtContext * ctx);\r
\r
Type getType(TocParser::TypeContext * ctx) {\r
Type result;\r
result.name = ctx->typeName()->NAME()->toString();\r
+ for (auto m : ctx->typeModifier()) {\r
+ result.modifiers.emplace_back(\r
+ m->toString() == "*" ? TypeModifierType::Pointer : TypeModifierType::Array,\r
+ m->toString() == "*" ? -1 : atoi(m->NUMBER()->toString().c_str())\r
+ );\r
+ }\r
return result;\r
}\r
Variable getVariable(TocParser::VarContext * ctx) {\r
for (auto s : ctx->stmt()) {\r
if (s->varDecl() != nullptr) {\r
result.variables.push_back(getVariable(s->varDecl()->var()));\r
+ if (s->varDecl()->var()->expr() != nullptr)\r
+ result.statements.push_back(getStmt(s));\r
}\r
else {\r
result.statements.push_back(getStmt(s));\r
}\r
return result;\r
}\r
-OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {\r
- OperatorExpr result;\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
+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->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::Multiply;\r
- if (op == "/") result.type = OperatorType::Divide;\r
- if (op == "==") result.type = OperatorType::Equals;\r
- if (op == "!=") result.type = OperatorType::NotEquals;\r
- if (op == "<") result.type = OperatorType::LessThan;\r
- if (op == ">") result.type = OperatorType::GreaterThan;\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->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
+ 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::NonSubscriptExprContext * ctx) {\r
+Expr getExpr(TocParser::NonAccessExprContext * 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
+ 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
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.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
- if (ctx->operatorExpr() != nullptr) {\r
- result.type = ExprType::Operator;\r
- result._operator = getOperatorExpr(ctx->operatorExpr());\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.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\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
+ if (ctx->opExpr() != nullptr) {\r
+ if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr) {\r
+ result.type = ExprType::UnaryOperator;\r
+ result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());\r
+ }\r
+ else if (ctx->opExpr()->binaryOp() != nullptr) {\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
+ 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
+ result.type = ExprType::TernaryOperator;\r
+ result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());\r
+ }\r
}\r
return result;\r
}\r
Stmt getStmt(TocParser::StmtContext * ctx) {\r
Stmt result;\r
- if (ctx->conditional() != nullptr) {\r
+ if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {\r
+ result.type = StmtType::Assign;\r
+ result._assign.name = ctx->varDecl()->var()->varName()->NAME()->toString();\r
+ result._assign.expr = getExpr(ctx->varDecl()->var()->expr());\r
+ }\r
+ if (ctx->ifStmt() != nullptr) {\r
result.type = StmtType::If;\r
- result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());\r
- result._if.body = getBody(ctx->conditional()->ifCond()->body());\r
+ result._if.condition = getExpr(ctx->ifStmt()->expr());\r
+ result._if.body = getBody(ctx->ifStmt()->body());\r
+ for (auto ei : ctx->ifStmt()->elseIfStmt()) {\r
+ result._if.elses.emplace_back(\r
+ true,\r
+ std::make_unique<Expr>(getExpr(ei->expr())),\r
+ getBody(ei->body())\r
+ );\r
+ }\r
+ if (ctx->ifStmt()->elseStmt() != nullptr) {\r
+ result._if.elses.emplace_back(\r
+ false,\r
+ nullptr,\r
+ getBody(ctx->ifStmt()->elseStmt()->body())\r
+ );\r
+ }\r
}\r
- if (ctx->loop() != nullptr) {\r
+ if (ctx->switchStmt() != nullptr) {\r
+ result.type = StmtType::Switch;\r
+ result._switch.ident.name = ctx->switchStmt()->identifierExpr()->varName()->NAME()->toString();\r
+ for (auto c : ctx->switchStmt()->switchBody()->switchCase()) {\r
+ result._switch.cases.emplace_back(\r
+ std::make_unique<Expr>(getExpr(c->expr())),\r
+ getBody(c->body())\r
+ );\r
+ }\r
+ }\r
+ if (ctx->forStmt() != nullptr) {\r
+ result.type = StmtType::For;\r
+ if (ctx->forStmt()->varInit() != nullptr) {\r
+ result._for.varName = ctx->forStmt()->varInit()->varName()->NAME()->toString();\r
+ result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->varInit()->expr()));\r
+ }\r
+ else {\r
+ result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();\r
+ result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));\r
+ }\r
+ result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));\r
+ result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));\r
+ result._for.body = getBody(ctx->forStmt()->body());\r
+ }\r
+ if (ctx->whileStmt() != nullptr) {\r
result.type = StmtType::While;\r
- result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());\r
- result._while.body = getBody(ctx->loop()->whileLoop()->body());\r
+ result._while.condition = getExpr(ctx->whileStmt()->expr());\r
+ result._while.body = getBody(ctx->whileStmt()->body());\r
}\r
- if (ctx->assignment() != nullptr) {\r
+ if (ctx->assignStmt() != nullptr) {\r
result.type = StmtType::Assign;\r
- //result._assign.lexpr = getExpr(ctx->assignment()->);\r
- result._assign.rexpr = getExpr(ctx->assignment()->expr());\r
+ result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString();\r
+ result._assign.expr = getExpr(ctx->assignStmt()->expr());\r
}\r
if (ctx->returnStmt() != nullptr) {\r
result.type = StmtType::Return;\r
result.type = StmtType::Expr;\r
result._expr = getExpr(ctx->expr());\r
}\r
- if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {\r
- result.type = StmtType::Assign;\r
- result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());\r
- }\r
return result;\r
}
\ No newline at end of file