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
23 result.name = ctx->typeName()->NAME()->toString();
\r
24 for (auto m : ctx->typeModifier()) {
\r
25 result.modifiers.emplace_back(
\r
26 m->toString() == "*" ? TypeModifierType::Pointer : TypeModifierType::Array,
\r
27 m->toString() == "*" ? -1 : atoi(m->NUMBER()->toString().c_str())
\r
32 Variable getVariable(TocParser::VarContext * ctx) {
\r
34 result.name = ctx->varName()->NAME()->toString();
\r
35 result.type = getType(ctx->type());
\r
38 Body getBody(TocParser::BodyContext * ctx) {
\r
40 for (auto s : ctx->stmt()) {
\r
41 if (s->varDecl() != nullptr) {
\r
42 result.variables.push_back(getVariable(s->varDecl()->var()));
\r
43 if (s->varDecl()->var()->expr() != nullptr)
\r
44 result.statements.push_back(getStmt(s));
\r
47 result.statements.push_back(getStmt(s));
\r
52 Function getFunction(TocParser::FuncContext * ctx) {
\r
54 result.name = ctx->funcName()->NAME()->toString();
\r
55 result.returnType = getType(ctx->type());
\r
56 if (!ctx->parameter()->var().empty()) {
\r
57 for (auto p : ctx->parameter()->var())
\r
58 result.parameters.push_back(getVariable(p));
\r
60 result.body = getBody(ctx->body());
\r
63 Struct getStruct(TocParser::StructDeclContext * ctx) {
\r
65 result.name = ctx->structName()->NAME()->toString();
\r
66 for (auto m : ctx->structMember()) {
\r
67 if (m->structVar() != nullptr) {
\r
68 result.members.push_back(getVariable(m->structVar()->var()));
\r
70 if (m->structMethod() != nullptr) {
\r
71 result.methods.push_back(getFunction(m->structMethod()->func()));
\r
76 Program getProgram(TocParser::ProgContext * ctx) {
\r
78 for (auto d : ctx->decl()) {
\r
79 if (d->varDecl() != nullptr) {
\r
80 result.variables.push_back(getVariable(d->varDecl()->var()));
\r
82 if (d->funcDecl() != nullptr) {
\r
83 result.functions.push_back(getFunction(d->funcDecl()->func()));
\r
85 if (d->structDecl() != nullptr) {
\r
86 result.structs.push_back(getStruct(d->structDecl()));
\r
91 UnaryOperatorType getUnaryOperatorType(const std::string & s) {
\r
92 for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++) {
\r
93 if (UnaryOperatorTypeStrings[i] == s) {
\r
94 return (UnaryOperatorType)i;
\r
97 return UnaryOperatorType::COUNT;
\r
99 BinaryOperatorType getBinaryOperatorType(const std::string & s) {
\r
100 for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++) {
\r
101 if (BinaryOperatorTypeStrings[i] == s) {
\r
102 return (BinaryOperatorType)i;
\r
105 return BinaryOperatorType::COUNT;
\r
107 UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) {
\r
108 UnaryOperatorExpr result;
\r
109 if (ctx->prefixOp() != nullptr)
\r
110 result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));
\r
112 result.expr = std::make_unique<Expr>(getExpr(ctx->postfixOp()->nonOpExpr()));
\r
115 if (ctx->prefixOp() != nullptr)
\r
116 op = ctx->prefixOp()->prefix_op()->getText();
\r
118 op = ctx->postfixOp()->postfix_op()->getText();
\r
120 // TODO: postfix type
\r
122 result.type = getUnaryOperatorType(op);
\r
126 BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) {
\r
127 BinaryOperatorExpr result;
\r
128 result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));
\r
129 result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));
\r
131 std::string op = ctx->binaryOp()->binary_op(0)->getText();
\r
132 std::cout << op << std::endl;
\r
134 result.type = getBinaryOperatorType(op);
\r
138 TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) {
\r
139 TernaryOperatorExpr result;
\r
140 result.lexpr = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->nonOpExpr()));
\r
141 result.rexprTrue = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(0)));
\r
142 result.rexprFalse = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(1)));
\r
145 Expr getExpr(TocParser::NonOpExprContext * ctx) {
\r
147 if (ctx->funcExpr() != nullptr) {
\r
148 result.type = ExprType::Func;
\r
149 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
150 for (auto e : ctx->funcExpr()->expr())
\r
151 result._func.arguments.push_back(getExpr(e));
\r
153 if (ctx->litExpr() != nullptr) {
\r
154 result.type = ExprType::Lit;
\r
155 if (ctx->litExpr()->INT_LIT() != nullptr) {
\r
156 result._lit.type = LitType::Int;
\r
157 result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());
\r
159 else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) {
\r
160 result._lit.type = LitType::Decimal;
\r
161 result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());
\r
163 else if (ctx->litExpr()->STRING_LIT() != nullptr) {
\r
164 result._lit.type = LitType::String;
\r
165 result._lit._string = ctx->litExpr()->STRING_LIT()->toString();
\r
167 else if (ctx->litExpr()->BOOL_LIT() != nullptr) {
\r
168 result._lit.type = LitType::Bool;
\r
169 result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";
\r
172 if (ctx->identifierExpr() != nullptr) {
\r
173 result.type = ExprType::Identifier;
\r
174 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
176 if (ctx->parenExpr() != nullptr) {
\r
177 result = getExpr(ctx->parenExpr()->expr());
\r
179 if (ctx->accessExpr() != nullptr) {
\r
180 // TODO: access chain
\r
181 for (auto sub : ctx->accessExpr()->accessSubExpr()) {
\r
182 if (sub->accessMember() != nullptr) {
\r
183 result.type = ExprType::Dot;
\r
184 result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
187 result.type = ExprType::Brackets;
\r
188 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
189 result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
\r
195 Expr getExpr(TocParser::NonAccessExprContext * ctx) {
\r
197 if (ctx->funcExpr() != nullptr) {
\r
198 result.type = ExprType::Func;
\r
199 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
200 for (auto e : ctx->funcExpr()->expr())
\r
201 result._func.arguments.push_back(getExpr(e));
\r
203 if (ctx->identifierExpr() != nullptr) {
\r
204 result.type = ExprType::Identifier;
\r
205 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
207 if (ctx->parenExpr() != nullptr) {
\r
208 result = getExpr(ctx->parenExpr()->expr());
\r
212 Expr getExpr(TocParser::ExprContext * ctx) {
\r
214 if (ctx->funcExpr() != nullptr) {
\r
215 result.type = ExprType::Func;
\r
216 result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();
\r
217 for (auto e : ctx->funcExpr()->expr())
\r
218 result._func.arguments.push_back(getExpr(e));
\r
220 if (ctx->litExpr() != nullptr) {
\r
221 result.type = ExprType::Lit;
\r
222 if (ctx->litExpr()->INT_LIT() != nullptr) {
\r
223 result._lit.type = LitType::Int;
\r
224 result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());
\r
226 else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) {
\r
227 result._lit.type = LitType::Decimal;
\r
228 result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());
\r
230 else if (ctx->litExpr()->STRING_LIT() != nullptr) {
\r
231 result._lit.type = LitType::String;
\r
232 result._lit._string = ctx->litExpr()->STRING_LIT()->toString();
\r
234 else if (ctx->litExpr()->BOOL_LIT() != nullptr) {
\r
235 result._lit.type = LitType::Bool;
\r
236 result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";
\r
239 if (ctx->identifierExpr() != nullptr) {
\r
240 result.type = ExprType::Identifier;
\r
241 result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
\r
243 if (ctx->parenExpr() != nullptr) {
\r
244 result = getExpr(ctx->parenExpr()->expr());
\r
246 if (ctx->accessExpr() != nullptr) {
\r
247 // TODO: access chain
\r
248 for (auto sub : ctx->accessExpr()->accessSubExpr()) {
\r
249 if (sub->accessMember() != nullptr) {
\r
250 result.type = ExprType::Dot;
\r
251 result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
252 result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
\r
255 result.type = ExprType::Brackets;
\r
256 result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
\r
257 result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
\r
261 if (ctx->opExpr() != nullptr) {
\r
262 if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr) {
\r
263 result.type = ExprType::UnaryOperator;
\r
264 result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());
\r
266 else if (ctx->opExpr()->binaryOp() != nullptr) {
\r
267 result.type = ExprType::BinaryOperator;
\r
268 result._binaryOperator = getBinaryOperatorExpr(ctx->opExpr());
\r
269 for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++) {
\r
271 result._binaryOperator.lexpr = std::make_unique<Expr>(tmp);
\r
272 result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText());
\r
273 result._binaryOperator.rexpr = std::make_unique<Expr>(getExpr(ctx->opExpr()->binaryOp()->nonOpExpr(i+1)));
\r
276 else if (ctx->opExpr()->ternaryOp() != nullptr) {
\r
277 result.type = ExprType::TernaryOperator;
\r
278 result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());
\r
283 Stmt getStmt(TocParser::StmtContext * ctx) {
\r
285 if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {
\r
286 result.type = StmtType::Assign;
\r
287 result._assign.name = ctx->varDecl()->var()->varName()->NAME()->toString();
\r
288 result._assign.expr = getExpr(ctx->varDecl()->var()->expr());
\r
290 if (ctx->ifStmt() != nullptr) {
\r
291 result.type = StmtType::If;
\r
292 result._if.condition = getExpr(ctx->ifStmt()->expr());
\r
293 result._if.body = getBody(ctx->ifStmt()->body());
\r
294 for (auto ei : ctx->ifStmt()->elseIfStmt()) {
\r
295 result._if.elses.emplace_back(
\r
297 std::make_unique<Expr>(getExpr(ei->expr())),
\r
298 getBody(ei->body())
\r
301 if (ctx->ifStmt()->elseStmt() != nullptr) {
\r
302 result._if.elses.emplace_back(
\r
305 getBody(ctx->ifStmt()->elseStmt()->body())
\r
309 if (ctx->switchStmt() != nullptr) {
\r
310 result.type = StmtType::Switch;
\r
311 result._switch.ident.name = ctx->switchStmt()->identifierExpr()->varName()->NAME()->toString();
\r
312 for (auto c : ctx->switchStmt()->switchBody()->switchCase()) {
\r
313 result._switch.cases.emplace_back(
\r
314 std::make_unique<Expr>(getExpr(c->expr())),
\r
319 if (ctx->forStmt() != nullptr) {
\r
320 result.type = StmtType::For;
\r
321 if (ctx->forStmt()->varInit() != nullptr) {
\r
322 result._for.varName = ctx->forStmt()->varInit()->varName()->NAME()->toString();
\r
323 result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->varInit()->expr()));
\r
326 result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();
\r
327 result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));
\r
329 result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));
\r
330 result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));
\r
331 result._for.body = getBody(ctx->forStmt()->body());
\r
333 if (ctx->whileStmt() != nullptr) {
\r
334 result.type = StmtType::While;
\r
335 result._while.condition = getExpr(ctx->whileStmt()->expr());
\r
336 result._while.body = getBody(ctx->whileStmt()->body());
\r
338 if (ctx->assignStmt() != nullptr) {
\r
339 result.type = StmtType::Assign;
\r
340 result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString();
\r
341 result._assign.expr = getExpr(ctx->assignStmt()->expr());
\r
343 if (ctx->returnStmt() != nullptr) {
\r
344 result.type = StmtType::Return;
\r
345 result._return.expr = getExpr(ctx->returnStmt()->expr());
\r
347 if (ctx->expr() != nullptr) {
\r
348 result.type = StmtType::Expr;
\r
349 result._expr = getExpr(ctx->expr());
\r