5 Type getType(TocParser::TypeContext * ctx);
\r
6 Variable getVariable(TocParser::VarContext * ctx);
\r
7 Body getBody(TocParser::BodyContext * ctx);
\r
8 Function getFunction(TocParser::FuncContext * ctx);
\r
9 Struct getStruct(TocParser::StructDeclContext * ctx);
\r
10 Program getProgram(TocParser::ProgContext * ctx);
\r
11 OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx);
\r
12 Expr getExpression(TocParser::ExprContext * ctx);
\r
13 Stmt getStmt(TocParser::StmtContext * ctx);
\r
15 Type getType(TocParser::TypeContext * ctx) {
\r
17 result.name = ctx->typeName()->NAME()->toString();
\r
20 Variable getVariable(TocParser::VarContext * ctx) {
\r
22 result.name = ctx->varName()->NAME()->toString();
\r
23 result.type = getType(ctx->type());
\r
26 Body getBody(TocParser::BodyContext * ctx) {
\r
28 for (auto s : ctx->stmt()) {
\r
29 if (s->varDecl() != nullptr) {
\r
30 result.variables.push_back(getVariable(s->varDecl()->var()));
\r
33 result.statements.push_back(getStmt(s));
\r
38 Function getFunction(TocParser::FuncContext * ctx) {
\r
40 result.name = ctx->funcName()->NAME()->toString();
\r
41 if (ctx->parameter()->firstParameter() != nullptr) {
\r
42 result.parameters.push_back(getVariable(ctx->parameter()->firstParameter()->var()));
\r
43 for (auto p : ctx->parameter()->additionalParameter())
\r
44 result.parameters.push_back(getVariable(p->var()));
\r
46 result.body = getBody(ctx->body());
\r
49 Struct getStruct(TocParser::StructDeclContext * ctx) {
\r
51 result.name = ctx->structName()->NAME()->toString();
\r
52 for (auto m : ctx->structMember()) {
\r
53 if (m->structVar() != nullptr) {
\r
54 result.members.push_back(getVariable(m->structVar()->var()));
\r
56 if (m->structMethod() != nullptr) {
\r
57 result.methods.push_back(getFunction(m->structMethod()->func()));
\r
62 Program getProgram(TocParser::ProgContext * ctx) {
\r
64 for (auto d : ctx->decl()) {
\r
65 if (d->varDecl() != nullptr) {
\r
66 result.variables.push_back(getVariable(d->varDecl()->var()));
\r
68 if (d->funcDecl() != nullptr) {
\r
69 result.functions.push_back(getFunction(d->funcDecl()->func()));
\r
71 if (d->structDecl() != nullptr) {
\r
72 result.structs.push_back(getStruct(d->structDecl()));
\r
77 OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {
\r
78 OperatorExpr result;
\r
79 //result.lexpr = getExpr(ctx->binaryOperator()->nonOpExpr(0));
\r
80 //result.rexpr = getExpr(ctx->binaryOperator()->nonOpExpr(1));
\r
81 std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString();
\r
82 if (op == "+") result.type = OperatorType::Plus;
\r
83 if (op == "-") result.type = OperatorType::Minus;
\r
84 if (op == "*") result.type = OperatorType::Multiply;
\r
85 if (op == "/") result.type = OperatorType::Divide;
\r
86 if (op == "==") result.type = OperatorType::Equals;
\r
87 if (op == "!=") result.type = OperatorType::NotEquals;
\r
88 if (op == "<") result.type = OperatorType::LessThan;
\r
89 if (op == ">") result.type = OperatorType::GreaterThan;
\r
92 Expr getExpr(TocParser::ExprContext * ctx) {
\r
94 if (ctx->funcCall() != nullptr) {
\r
95 result.type = ExprType::Call;
\r
96 for (auto e : ctx->funcCall()->expr())
\r
97 result._call.arguments.push_back(getExpr(e));
\r
98 //result._call.function = ctx->funcCall()->funcName();
\r
100 if (ctx->literal() != nullptr) {
\r
101 result.type = ExprType::Literal;
\r
102 result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
\r
104 if (ctx->identifier() != nullptr) {
\r
105 result.type = ExprType::Variable;
\r
106 result._variable.name = ctx->identifier()->varName()->NAME()->toString();
\r
108 if (ctx->subscript() != nullptr) {
\r
109 result.type = ExprType::Brackets;
\r
110 //result._brackets.lexpr = getExpr(ctx->subscript()->nonSubscriptExpr());
\r
111 result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
\r
113 if (ctx->memberAccess() != nullptr) {
\r
114 result.type = ExprType::Dot;
\r
115 //result._dot.lexpr = ctx->memberAccess()->identifier(0);
\r
116 result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
\r
118 if (ctx->operatorExpr() != nullptr) {
\r
119 result.type = ExprType::Operator;
\r
120 result._operator = getOperatorExpr(ctx->operatorExpr());
\r
124 Stmt getStmt(TocParser::StmtContext * ctx) {
\r
126 if (ctx->conditional() != nullptr) {
\r
127 result.type = StmtType::If;
\r
128 result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());
\r
129 result._if.body = getBody(ctx->conditional()->ifCond()->body());
\r
131 if (ctx->loop() != nullptr) {
\r
132 result.type = StmtType::While;
\r
133 result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());
\r
134 result._while.body = getBody(ctx->loop()->whileLoop()->body());
\r
136 if (ctx->assignment() != nullptr) {
\r
137 result.type = StmtType::Assign;
\r
138 //result._assign.lexpr = getExpr(ctx->assignment()->);
\r
139 result._assign.rexpr = getExpr(ctx->assignment()->expr());
\r
141 if (ctx->returnStmt() != nullptr) {
\r
142 result.type = StmtType::Return;
\r
143 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
145 if (ctx->expr() != nullptr) {
\r
146 result.type = StmtType::Expr;
\r
147 result._expr = getExpr(ctx->expr());
\r
149 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {
\r
150 result.type = StmtType::Assign;
\r
151 result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());
\r