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 UnaryOperatorType getUnaryOperatorType(const std::string & s);
\r
12 BinaryOperatorType getBinaryOperatorType(const std::string & s);
\r
13 UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx);
\r
14 BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx);
\r
15 TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx);
\r
16 Expr getExpr(TocParser::NonOpExprContext * ctx);
\r
17 Expr getExpr(TocParser::NonAccessExprContext * ctx);
\r
18 Expr getExpr(TocParser::ExprContext * ctx);
\r
19 Stmt getStmt(TocParser::StmtContext * ctx);
\r
21 Type getType(TocParser::TypeContext * ctx)
\r
24 result.name = ctx->typeName()->NAME()->toString();
\r
25 for (auto m : ctx->typeModifier())
\r
27 bool isPointer = m->getText() == "*";
\r
28 bool isStaticArray = m->INT_LIT() != nullptr;
\r
30 result.modifiers.emplace_back(
\r
31 isPointer ? TypeModifierType::Pointer : TypeModifierType::Array,
\r
33 isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1
\r
38 Variable getVariable(TocParser::VarContext * ctx)
\r
41 result.name = ctx->varName()->NAME()->toString();
\r
42 result.type = getType(ctx->type());
\r
45 Body getBody(TocParser::BodyContext * ctx)
\r
48 for (auto s : ctx->stmt())
\r
50 if (s->varDecl() != nullptr)
\r
52 result.variables.push_back(getVariable(s->varDecl()->var()));
\r
53 if (s->varDecl()->var()->expr() != nullptr)
\r
54 result.statements.push_back(getStmt(s));
\r
58 result.statements.push_back(getStmt(s));
\r
63 Function getFunction(TocParser::FuncContext * ctx)
\r
66 result.name = ctx->funcName()->NAME()->toString();
\r
67 result.returnType = getType(ctx->type());
\r
68 if (!ctx->parameter()->var().empty())
\r
70 for (auto p : ctx->parameter()->var())
\r
71 result.parameters.push_back(getVariable(p));
\r
73 result.body = getBody(ctx->body());
\r
76 Struct getStruct(TocParser::StructDeclContext * ctx)
\r
79 result.name = ctx->structName()->NAME()->toString();
\r
80 for (auto m : ctx->structMember())
\r
82 if (m->structVar() != nullptr)
\r
84 result.members.push_back(getVariable(m->structVar()->var()));
\r
86 if (m->structMethod() != nullptr)
\r
88 result.methods.push_back(getFunction(m->structMethod()->func()));
\r
93 Program getProgram(TocParser::ProgContext * ctx)
\r
96 for (auto d : ctx->decl())
\r
98 if (d->varDecl() != nullptr)
\r
100 result.variables.push_back(getVariable(d->varDecl()->var()));
\r
102 if (d->funcDecl() != nullptr)
\r
104 result.functions.push_back(getFunction(d->funcDecl()->func()));
\r
106 if (d->structDecl() != nullptr)
\r
108 result.structs.push_back(getStruct(d->structDecl()));
\r
113 UnaryOperatorType getUnaryOperatorType(const std::string & s)
\r
115 for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++)
\r
117 if (UnaryOperatorTypeStrings[i] == s)
\r
119 return (UnaryOperatorType)i;
\r
122 return UnaryOperatorType::COUNT;
\r
124 BinaryOperatorType getBinaryOperatorType(const std::string & s)
\r
126 for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++)
\r
128 if (BinaryOperatorTypeStrings[i] == s)
\r
130 return (BinaryOperatorType)i;
\r
133 return BinaryOperatorType::COUNT;
\r
135 UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx)
\r
137 UnaryOperatorExpr result;
\r
138 if (ctx->prefixOp() != nullptr)
\r
139 result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));
\r
141 result.expr = std::make_unique<Expr>(getExpr(ctx->postfixOp()->nonOpExpr()));
\r
144 if (ctx->prefixOp() != nullptr)
\r
145 op = ctx->prefixOp()->prefix_op()->getText();
\r
147 op = ctx->postfixOp()->postfix_op()->getText();
\r
149 // TODO: postfix type
\r
151 result.type = getUnaryOperatorType(op);
\r
155 BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx)
\r
157 BinaryOperatorExpr result;
\r
158 result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));
\r
159 result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));
\r
161 std::string op = ctx->binaryOp()->binary_op(0)->getText();
\r
163 result.type = getBinaryOperatorType(op);
\r
167 TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx)
\r
169 TernaryOperatorExpr result;
\r
170 result.lexpr = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->nonOpExpr()));
\r
171 result.rexprTrue = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(0)));
\r
172 result.rexprFalse = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(1)));
\r
175 Expr getExpr(TocParser::NonOpExprContext * ctx)
\r
178 result.parenthesized = false;
\r
179 if (ctx->funcExpr() != nullptr)
\r
181 result.type = ExprType::Func;
\r
182 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
183 for (auto e : ctx->funcExpr()->expr())
\r
184 result._func.arguments.push_back(getExpr(e));
\r
186 if (ctx->litExpr() != nullptr)
\r
188 result.type = ExprType::Lit;
\r
189 if (ctx->litExpr()->INT_LIT() != nullptr)
\r
191 result._lit.type = LitType::Int;
\r
192 result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());
\r
194 else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)
\r
196 result._lit.type = LitType::Decimal;
\r
197 result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());
\r
199 else if (ctx->litExpr()->STRING_LIT() != nullptr)
\r
201 result._lit.type = LitType::String;
\r
202 result._lit._string = ctx->litExpr()->STRING_LIT()->toString();
\r
204 else if (ctx->litExpr()->BOOL_LIT() != nullptr)
\r
206 result._lit.type = LitType::Bool;
\r
207 result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";
\r
210 if (ctx->identifierExpr() != nullptr)
\r
212 result.type = ExprType::Identifier;
\r
213 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
215 if (ctx->parenExpr() != nullptr)
\r
217 result = getExpr(ctx->parenExpr()->expr());
\r
218 result.parenthesized = true;
\r
220 if (ctx->accessExpr() != nullptr)
\r
222 auto firstSub = ctx->accessExpr()->accessSubExpr(0);
\r
223 if (firstSub->accessMember() != nullptr)
\r
225 result.type = ExprType::Dot;
\r
226 result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
227 result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
231 result.type = ExprType::Brackets;
\r
232 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
233 result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));
\r
235 for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)
\r
238 auto sub = ctx->accessExpr()->accessSubExpr(i);
\r
239 if (sub->accessMember() != nullptr)
\r
241 result.type = ExprType::Dot;
\r
242 result._dot.expr = std::make_unique<Expr>(tmp);
\r
243 result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
247 result.type = ExprType::Brackets;
\r
248 result._brackets.lexpr = std::make_unique<Expr>(tmp);
\r
249 result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
\r
255 Expr getExpr(TocParser::NonAccessExprContext * ctx)
\r
258 result.parenthesized = false;
\r
259 if (ctx->funcExpr() != nullptr)
\r
261 result.type = ExprType::Func;
\r
262 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
263 for (auto e : ctx->funcExpr()->expr())
\r
264 result._func.arguments.push_back(getExpr(e));
\r
266 if (ctx->identifierExpr() != nullptr)
\r
268 result.type = ExprType::Identifier;
\r
269 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
271 if (ctx->parenExpr() != nullptr)
\r
273 result = getExpr(ctx->parenExpr()->expr());
\r
274 result.parenthesized = true;
\r
278 Expr getExpr(TocParser::ExprContext * ctx)
\r
281 result.parenthesized = false;
\r
282 if (ctx->funcExpr() != nullptr)
\r
284 result.type = ExprType::Func;
\r
285 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
286 for (auto e : ctx->funcExpr()->expr())
\r
287 result._func.arguments.push_back(getExpr(e));
\r
289 if (ctx->litExpr() != nullptr)
\r
291 result.type = ExprType::Lit;
\r
292 if (ctx->litExpr()->INT_LIT() != nullptr)
\r
294 result._lit.type = LitType::Int;
\r
295 result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());
\r
297 else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)
\r
299 result._lit.type = LitType::Decimal;
\r
300 result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());
\r
302 else if (ctx->litExpr()->STRING_LIT() != nullptr)
\r
304 result._lit.type = LitType::String;
\r
305 result._lit._string = ctx->litExpr()->STRING_LIT()->toString();
\r
307 else if (ctx->litExpr()->BOOL_LIT() != nullptr)
\r
309 result._lit.type = LitType::Bool;
\r
310 result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";
\r
313 if (ctx->identifierExpr() != nullptr)
\r
315 result.type = ExprType::Identifier;
\r
316 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
318 if (ctx->parenExpr() != nullptr)
\r
320 result = getExpr(ctx->parenExpr()->expr());
\r
321 result.parenthesized = true;
\r
323 if (ctx->accessExpr() != nullptr)
\r
325 auto firstSub = ctx->accessExpr()->accessSubExpr(0);
\r
326 if (firstSub->accessMember() != nullptr)
\r
328 result.type = ExprType::Dot;
\r
329 result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
330 result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
334 result.type = ExprType::Brackets;
\r
335 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
336 result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));
\r
338 for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)
\r
341 auto sub = ctx->accessExpr()->accessSubExpr(i);
\r
342 if (sub->accessMember() != nullptr)
\r
344 result.type = ExprType::Dot;
\r
345 result._dot.expr = std::make_unique<Expr>(tmp);
\r
346 result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
350 result.type = ExprType::Brackets;
\r
351 result._brackets.lexpr = std::make_unique<Expr>(tmp);
\r
352 result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
\r
356 if (ctx->opExpr() != nullptr)
\r
358 if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr)
\r
360 result.type = ExprType::UnaryOperator;
\r
361 result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());
\r
363 else if (ctx->opExpr()->binaryOp() != nullptr)
\r
365 result.type = ExprType::BinaryOperator;
\r
366 result._binaryOperator = getBinaryOperatorExpr(ctx->opExpr());
\r
367 for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++)
\r
370 result._binaryOperator.lexpr = std::make_unique<Expr>(tmp);
\r
371 result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText());
\r
372 result._binaryOperator.rexpr = std::make_unique<Expr>(getExpr(ctx->opExpr()->binaryOp()->nonOpExpr(i+1)));
\r
375 else if (ctx->opExpr()->ternaryOp() != nullptr)
\r
377 result.type = ExprType::TernaryOperator;
\r
378 result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());
\r
383 Stmt getStmt(TocParser::StmtContext * ctx)
\r
386 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)
\r
388 result.type = StmtType::Assign;
\r
389 result._assign.name = ctx->varDecl()->var()->varName()->NAME()->toString();
\r
390 result._assign.expr = getExpr(ctx->varDecl()->var()->expr());
\r
392 if (ctx->ifStmt() != nullptr)
\r
394 result.type = StmtType::If;
\r
395 result._if.condition = getExpr(ctx->ifStmt()->expr());
\r
396 result._if.body = getBody(ctx->ifStmt()->body());
\r
397 for (auto ei : ctx->ifStmt()->elseIfStmt())
\r
399 result._if.elses.emplace_back(
\r
401 std::make_unique<Expr>(getExpr(ei->expr())),
\r
402 getBody(ei->body())
\r
405 if (ctx->ifStmt()->elseStmt() != nullptr)
\r
407 result._if.elses.emplace_back(
\r
410 getBody(ctx->ifStmt()->elseStmt()->body())
\r
414 if (ctx->switchStmt() != nullptr)
\r
416 result.type = StmtType::Switch;
\r
417 result._switch.ident.name = ctx->switchStmt()->identifierExpr()->varName()->NAME()->toString();
\r
418 for (auto c : ctx->switchStmt()->switchBody()->switchCase())
\r
420 result._switch.cases.emplace_back(
\r
421 std::make_unique<Expr>(getExpr(c->expr())),
\r
426 if (ctx->forStmt() != nullptr)
\r
428 result.type = StmtType::For;
\r
429 if (ctx->forStmt()->varInit() != nullptr)
\r
431 result._for.varName = ctx->forStmt()->varInit()->varName()->NAME()->toString();
\r
432 result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->varInit()->expr()));
\r
436 result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();
\r
437 result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));
\r
439 result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));
\r
440 result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));
\r
441 result._for.body = getBody(ctx->forStmt()->body());
\r
443 if (ctx->whileStmt() != nullptr)
\r
445 result.type = StmtType::While;
\r
446 result._while.condition = getExpr(ctx->whileStmt()->expr());
\r
447 result._while.body = getBody(ctx->whileStmt()->body());
\r
449 if (ctx->assignStmt() != nullptr)
\r
451 result.type = StmtType::Assign;
\r
452 result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString();
\r
453 result._assign.expr = getExpr(ctx->assignStmt()->expr());
\r
455 if (ctx->returnStmt() != nullptr)
\r
457 result.type = StmtType::Return;
\r
458 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
460 if (ctx->expr() != nullptr)
\r
462 result.type = StmtType::Expr;
\r
463 result._expr = getExpr(ctx->expr());
\r