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 getExpr(TocParser::NonOpExprContext * ctx);
\r
13 Expr getExpr(TocParser::NonSubscriptExprContext * ctx);
\r
14 Expr getExpr(TocParser::ExprContext * ctx);
\r
15 Stmt getStmt(TocParser::StmtContext * ctx);
\r
17 Type getType(TocParser::TypeContext * ctx) {
\r
19 result.name = ctx->typeName()->NAME()->toString();
\r
22 Variable getVariable(TocParser::VarContext * ctx) {
\r
24 result.name = ctx->varName()->NAME()->toString();
\r
25 result.type = getType(ctx->type());
\r
28 Body getBody(TocParser::BodyContext * ctx) {
\r
30 for (auto s : ctx->stmt()) {
\r
31 if (s->varDecl() != nullptr) {
\r
32 result.variables.push_back(getVariable(s->varDecl()->var()));
\r
35 result.statements.push_back(getStmt(s));
\r
40 Function getFunction(TocParser::FuncContext * ctx) {
\r
42 result.name = ctx->funcName()->NAME()->toString();
\r
43 result.returnType = getType(ctx->type());
\r
44 if (!ctx->parameter()->var().empty()) {
\r
45 for (auto p : ctx->parameter()->var())
\r
46 result.parameters.push_back(getVariable(p));
\r
48 result.body = getBody(ctx->body());
\r
51 Struct getStruct(TocParser::StructDeclContext * ctx) {
\r
53 result.name = ctx->structName()->NAME()->toString();
\r
54 for (auto m : ctx->structMember()) {
\r
55 if (m->structVar() != nullptr) {
\r
56 result.members.push_back(getVariable(m->structVar()->var()));
\r
58 if (m->structMethod() != nullptr) {
\r
59 result.methods.push_back(getFunction(m->structMethod()->func()));
\r
64 Program getProgram(TocParser::ProgContext * ctx) {
\r
66 for (auto d : ctx->decl()) {
\r
67 if (d->varDecl() != nullptr) {
\r
68 result.variables.push_back(getVariable(d->varDecl()->var()));
\r
70 if (d->funcDecl() != nullptr) {
\r
71 result.functions.push_back(getFunction(d->funcDecl()->func()));
\r
73 if (d->structDecl() != nullptr) {
\r
74 result.structs.push_back(getStruct(d->structDecl()));
\r
79 OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {
\r
80 OperatorExpr result;
\r
81 result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));
\r
82 result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(1)));
\r
84 std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString();
\r
85 if (op == "+") result.type = OperatorType::Plus;
\r
86 if (op == "-") result.type = OperatorType::Minus;
\r
87 if (op == "*") result.type = OperatorType::Multiply;
\r
88 if (op == "/") result.type = OperatorType::Divide;
\r
89 if (op == "==") result.type = OperatorType::Equals;
\r
90 if (op == "!=") result.type = OperatorType::NotEquals;
\r
91 if (op == "<") result.type = OperatorType::LessThan;
\r
92 if (op == ">") result.type = OperatorType::GreaterThan;
\r
95 Expr getExpr(TocParser::NonOpExprContext * ctx) {
\r
97 if (ctx->funcCall() != nullptr) {
\r
98 result.type = ExprType::Call;
\r
99 for (auto e : ctx->funcCall()->expr())
\r
100 result._call.arguments.push_back(getExpr(e));
\r
101 result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
\r
103 if (ctx->literal() != nullptr) {
\r
104 result.type = ExprType::Literal;
\r
105 result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
\r
107 if (ctx->identifier() != nullptr) {
\r
108 result.type = ExprType::Variable;
\r
109 result._variable.name = ctx->identifier()->varName()->NAME()->toString();
\r
111 if (ctx->subscript() != nullptr) {
\r
112 result.type = ExprType::Brackets;
\r
113 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
\r
114 result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
\r
116 if (ctx->memberAccess() != nullptr) {
\r
117 result.type = ExprType::Dot;
\r
118 Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
\r
119 result._dot.lexpr = std::make_unique<Expr>(e);
\r
120 result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
\r
124 Expr getExpr(TocParser::NonSubscriptExprContext * ctx) {
\r
126 if (ctx->funcCall() != nullptr) {
\r
127 result.type = ExprType::Call;
\r
128 for (auto e : ctx->funcCall()->expr())
\r
129 result._call.arguments.push_back(getExpr(e));
\r
130 result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
\r
132 if (ctx->literal() != nullptr) {
\r
133 result.type = ExprType::Literal;
\r
134 result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
\r
136 if (ctx->identifier() != nullptr) {
\r
137 result.type = ExprType::Variable;
\r
138 result._variable.name = ctx->identifier()->varName()->NAME()->toString();
\r
140 if (ctx->memberAccess() != nullptr) {
\r
141 result.type = ExprType::Dot;
\r
142 Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
\r
143 result._dot.lexpr = std::make_unique<Expr>(e);
\r
144 result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
\r
148 Expr getExpr(TocParser::ExprContext * ctx) {
\r
150 if (ctx->funcCall() != nullptr) {
\r
151 result.type = ExprType::Call;
\r
152 for (auto e : ctx->funcCall()->expr())
\r
153 result._call.arguments.push_back(getExpr(e));
\r
154 result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
\r
156 if (ctx->literal() != nullptr) {
\r
157 result.type = ExprType::Literal;
\r
158 result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
\r
160 if (ctx->identifier() != nullptr) {
\r
161 result.type = ExprType::Variable;
\r
162 result._variable.name = ctx->identifier()->varName()->NAME()->toString();
\r
164 if (ctx->subscript() != nullptr) {
\r
165 result.type = ExprType::Brackets;
\r
166 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
\r
167 result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
\r
169 if (ctx->memberAccess() != nullptr) {
\r
170 result.type = ExprType::Dot;
\r
171 Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
\r
172 result._dot.lexpr = std::make_unique<Expr>(e);
\r
173 result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
\r
175 if (ctx->operatorExpr() != nullptr) {
\r
176 result.type = ExprType::Operator;
\r
177 result._operator = getOperatorExpr(ctx->operatorExpr());
\r
181 Stmt getStmt(TocParser::StmtContext * ctx) {
\r
183 if (ctx->conditional() != nullptr) {
\r
184 result.type = StmtType::If;
\r
185 result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());
\r
186 result._if.body = getBody(ctx->conditional()->ifCond()->body());
\r
188 if (ctx->loop() != nullptr) {
\r
189 result.type = StmtType::While;
\r
190 result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());
\r
191 result._while.body = getBody(ctx->loop()->whileLoop()->body());
\r
193 if (ctx->assignment() != nullptr) {
\r
194 result.type = StmtType::Assign;
\r
195 //result._assign.lexpr = getExpr(ctx->assignment()->);
\r
196 result._assign.rexpr = getExpr(ctx->assignment()->expr());
\r
198 if (ctx->returnStmt() != nullptr) {
\r
199 result.type = StmtType::Return;
\r
200 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
202 if (ctx->expr() != nullptr) {
\r
203 result.type = StmtType::Expr;
\r
204 result._expr = getExpr(ctx->expr());
\r
206 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {
\r
207 result.type = StmtType::Assign;
\r
208 result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());
\r