]> gitweb.ps.run Git - toc/blob - src/repr_get.h
add namespace, private struct member grammar, change bracket style
[toc] / src / repr_get.h
1 #pragma once\r
2 \r
3 #include "repr.h"\r
4 \r
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
20 \r
21 Type getType(TocParser::TypeContext * ctx)\r
22 {\r
23   Type result;\r
24   result.name = ctx->typeName()->NAME()->toString();\r
25   for (auto m : ctx->typeModifier())\r
26   {\r
27     bool isPointer = m->getText() == "*";\r
28     bool isStaticArray = m->INT_LIT() != nullptr;\r
29 \r
30     result.modifiers.emplace_back(\r
31       isPointer ? TypeModifierType::Pointer : TypeModifierType::Array,\r
32       isStaticArray,\r
33       isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1\r
34     );\r
35   }\r
36   return result;\r
37 }\r
38 Variable getVariable(TocParser::VarContext * ctx)\r
39 {\r
40   Variable result;\r
41   result.name = ctx->varName()->NAME()->toString();\r
42   result.type = getType(ctx->type());\r
43   return result;\r
44 }\r
45 Body getBody(TocParser::BodyContext * ctx)\r
46 {\r
47   Body result;\r
48   for (auto s : ctx->stmt())\r
49   {\r
50     if (s->varDecl() != nullptr)\r
51     {\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
55     }\r
56     else\r
57     {\r
58       result.statements.push_back(getStmt(s));\r
59     }\r
60   }\r
61   return result;\r
62 }\r
63 Function getFunction(TocParser::FuncContext * ctx)\r
64 {\r
65   Function result;\r
66   result.name = ctx->funcName()->NAME()->toString();\r
67   result.returnType = getType(ctx->type());\r
68   if (!ctx->parameter()->var().empty())\r
69   {\r
70     for (auto p : ctx->parameter()->var())\r
71       result.parameters.push_back(getVariable(p));\r
72   }\r
73   result.body = getBody(ctx->body());\r
74   return result;\r
75 }\r
76 Struct getStruct(TocParser::StructDeclContext * ctx)\r
77 {\r
78   Struct result;\r
79   result.name = ctx->structName()->NAME()->toString();\r
80   for (auto m : ctx->structMember())\r
81   {\r
82     if (m->structVar() != nullptr)\r
83     {\r
84       result.members.push_back(getVariable(m->structVar()->var()));\r
85     }\r
86     if (m->structMethod() != nullptr)\r
87     {\r
88       result.methods.push_back(getFunction(m->structMethod()->func()));\r
89     }\r
90   }\r
91   return result;\r
92 }\r
93 Program getProgram(TocParser::ProgContext * ctx)\r
94 {\r
95   Program result;\r
96   for (auto d : ctx->decl())\r
97   {\r
98     if (d->varDecl() != nullptr)\r
99     {\r
100       result.variables.push_back(getVariable(d->varDecl()->var()));\r
101     }\r
102     if (d->funcDecl() != nullptr)\r
103     {\r
104       result.functions.push_back(getFunction(d->funcDecl()->func()));\r
105     }\r
106     if (d->structDecl() != nullptr)\r
107     {\r
108       result.structs.push_back(getStruct(d->structDecl()));\r
109     }\r
110   }\r
111   return result;\r
112 }\r
113 UnaryOperatorType getUnaryOperatorType(const std::string & s)\r
114 {\r
115   for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++)\r
116   {\r
117     if (UnaryOperatorTypeStrings[i] == s)\r
118     {\r
119       return (UnaryOperatorType)i;\r
120     }\r
121   }\r
122   return UnaryOperatorType::COUNT;\r
123 }\r
124 BinaryOperatorType getBinaryOperatorType(const std::string & s)\r
125 {\r
126   for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++)\r
127   {\r
128     if (BinaryOperatorTypeStrings[i] == s)\r
129     {\r
130       return (BinaryOperatorType)i;\r
131     }\r
132   }\r
133   return BinaryOperatorType::COUNT;\r
134 }\r
135 UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx)\r
136 {\r
137   UnaryOperatorExpr result;\r
138   if (ctx->prefixOp() != nullptr)\r
139     result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));\r
140   else\r
141     result.expr = std::make_unique<Expr>(getExpr(ctx->postfixOp()->nonOpExpr()));\r
142 \r
143   std::string op;\r
144   if (ctx->prefixOp() != nullptr)\r
145     op = ctx->prefixOp()->prefix_op()->getText();\r
146   else\r
147     op = ctx->postfixOp()->postfix_op()->getText();\r
148 \r
149   // TODO: postfix type\r
150 \r
151   result.type = getUnaryOperatorType(op);\r
152 \r
153   return result;\r
154 }\r
155 BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx)\r
156 {\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
160   \r
161   std::string op = ctx->binaryOp()->binary_op(0)->getText();\r
162 \r
163   result.type = getBinaryOperatorType(op);\r
164 \r
165   return result;\r
166 }\r
167 TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx)\r
168 {\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
173   return result;\r
174 }\r
175 Expr getExpr(TocParser::NonOpExprContext * ctx)\r
176 {\r
177   Expr result;\r
178   result.parenthesized = false;\r
179   if (ctx->funcExpr() != nullptr)\r
180   {\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
185   }\r
186   if (ctx->litExpr() != nullptr)\r
187   {\r
188     result.type = ExprType::Lit;\r
189     if (ctx->litExpr()->INT_LIT() != nullptr)\r
190     {\r
191       result._lit.type = LitType::Int;\r
192       result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());\r
193     }\r
194     else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)\r
195     {\r
196       result._lit.type = LitType::Decimal;\r
197       result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());\r
198     }\r
199     else if (ctx->litExpr()->STRING_LIT() != nullptr)\r
200     {\r
201       result._lit.type = LitType::String;\r
202       result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
203     }\r
204     else if (ctx->litExpr()->BOOL_LIT() != nullptr)\r
205     {\r
206       result._lit.type = LitType::Bool;\r
207       result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
208     }\r
209   }\r
210   if (ctx->identifierExpr() != nullptr)\r
211   {\r
212     result.type = ExprType::Identifier;\r
213     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
214   }\r
215   if (ctx->parenExpr() != nullptr)\r
216   {\r
217     result = getExpr(ctx->parenExpr()->expr());\r
218     result.parenthesized = true;\r
219   }\r
220   if (ctx->accessExpr() != nullptr)\r
221   {\r
222     auto firstSub = ctx->accessExpr()->accessSubExpr(0);\r
223     if (firstSub->accessMember() != nullptr)\r
224     {\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
228     }\r
229     else\r
230     {\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
234     }\r
235     for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)\r
236     {\r
237       Expr tmp = result;\r
238       auto sub = ctx->accessExpr()->accessSubExpr(i);\r
239       if (sub->accessMember() != nullptr)\r
240       {\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
244       }\r
245       else\r
246       {\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
250       }\r
251     }\r
252   }\r
253   return result;\r
254 }\r
255 Expr getExpr(TocParser::NonAccessExprContext * ctx)\r
256 {\r
257   Expr result;\r
258   result.parenthesized = false;\r
259   if (ctx->funcExpr() != nullptr)\r
260   {\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
265   }\r
266   if (ctx->identifierExpr() != nullptr)\r
267   {\r
268     result.type = ExprType::Identifier;\r
269     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
270   }\r
271   if (ctx->parenExpr() != nullptr)\r
272   {\r
273     result = getExpr(ctx->parenExpr()->expr());\r
274     result.parenthesized = true;\r
275   }\r
276   return result;\r
277 }\r
278 Expr getExpr(TocParser::ExprContext * ctx)\r
279 {\r
280   Expr result;\r
281   result.parenthesized = false;\r
282   if (ctx->funcExpr() != nullptr)\r
283   {\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
288   }\r
289   if (ctx->litExpr() != nullptr)\r
290   {\r
291     result.type = ExprType::Lit;\r
292     if (ctx->litExpr()->INT_LIT() != nullptr)\r
293     {\r
294       result._lit.type = LitType::Int;\r
295       result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());\r
296     }\r
297     else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)\r
298     {\r
299       result._lit.type = LitType::Decimal;\r
300       result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());\r
301     }\r
302     else if (ctx->litExpr()->STRING_LIT() != nullptr)\r
303     {\r
304       result._lit.type = LitType::String;\r
305       result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
306     }\r
307     else if (ctx->litExpr()->BOOL_LIT() != nullptr)\r
308     {\r
309       result._lit.type = LitType::Bool;\r
310       result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
311     }\r
312   }\r
313   if (ctx->identifierExpr() != nullptr)\r
314   {\r
315     result.type = ExprType::Identifier;\r
316     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
317   }\r
318   if (ctx->parenExpr() != nullptr)\r
319   {\r
320     result = getExpr(ctx->parenExpr()->expr());\r
321     result.parenthesized = true;\r
322   }\r
323   if (ctx->accessExpr() != nullptr)\r
324   {\r
325     auto firstSub = ctx->accessExpr()->accessSubExpr(0);\r
326     if (firstSub->accessMember() != nullptr)\r
327     {\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
331     }\r
332     else\r
333     {\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
337     }\r
338     for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)\r
339     {\r
340       Expr tmp = result;\r
341       auto sub = ctx->accessExpr()->accessSubExpr(i);\r
342       if (sub->accessMember() != nullptr)\r
343       {\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
347       }\r
348       else\r
349       {\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
353       }\r
354     }\r
355   }\r
356   if (ctx->opExpr() != nullptr)\r
357   {\r
358     if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr)\r
359     {\r
360       result.type = ExprType::UnaryOperator;\r
361       result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());\r
362     }\r
363     else if (ctx->opExpr()->binaryOp() != nullptr)\r
364     {\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
368       {\r
369         Expr tmp = result;\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
373       }\r
374     }\r
375     else if (ctx->opExpr()->ternaryOp() != nullptr)\r
376     {\r
377       result.type = ExprType::TernaryOperator;\r
378       result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());\r
379     }\r
380   }\r
381   return result;\r
382 }\r
383 Stmt getStmt(TocParser::StmtContext * ctx)\r
384 {\r
385   Stmt result;\r
386   if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)\r
387   {\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
391   }\r
392   if (ctx->ifStmt() != nullptr)\r
393   {\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
398     {\r
399       result._if.elses.emplace_back(\r
400         true,\r
401         std::make_unique<Expr>(getExpr(ei->expr())),\r
402         getBody(ei->body())\r
403       );\r
404     }\r
405     if (ctx->ifStmt()->elseStmt() != nullptr)\r
406     {\r
407       result._if.elses.emplace_back(\r
408         false,\r
409         nullptr,\r
410         getBody(ctx->ifStmt()->elseStmt()->body())\r
411       );\r
412     }\r
413   }\r
414   if (ctx->switchStmt() != nullptr)\r
415   {\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
419     {\r
420       result._switch.cases.emplace_back(\r
421         std::make_unique<Expr>(getExpr(c->expr())),\r
422         getBody(c->body())\r
423       );\r
424     }\r
425   }\r
426   if (ctx->forStmt() != nullptr)\r
427   {\r
428     result.type = StmtType::For;\r
429     if (ctx->forStmt()->varInit() != nullptr)\r
430     {\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
433     }\r
434     else\r
435     {\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
438     }\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
442   }\r
443   if (ctx->whileStmt() != nullptr)\r
444   {\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
448   }\r
449   if (ctx->assignStmt() != nullptr)\r
450   {\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
454   }\r
455   if (ctx->returnStmt() != nullptr)\r
456   {\r
457     result.type = StmtType::Return;\r
458     result._return.expr = getExpr(ctx->returnStmt()->expr());\r
459   }\r
460   if (ctx->expr() != nullptr)\r
461   {\r
462     result.type = StmtType::Expr;\r
463     result._expr = getExpr(ctx->expr());\r
464   }\r
465   return result;\r
466 }