]> gitweb.ps.run Git - toc/blob - src/repr_get.h
pre change
[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, std::shared_ptr<Context> parent);\r
8 Function            getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent);\r
9 Struct              getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent);\r
10 Namespace           getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent);\r
11 Program             getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent);\r
12 \r
13 \r
14 Expr                getExpr(TocParser::FuncExprContext * ctx);\r
15 Expr                getExpr(TocParser::MethodExprContext * ctx);\r
16 Expr                getExpr(TocParser::LitExprContext * ctx);\r
17 Expr                getExpr(TocParser::ParenExprContext * ctx);\r
18 Expr                getExpr(TocParser::DotExprContext * ctx);\r
19 Expr                getExpr(TocParser::PrefixOpExprContext * ctx);\r
20 Expr                getExpr(TocParser::PostfixOpExprContext * ctx);\r
21 Expr                getExpr(TocParser::BinaryOpExprContext * ctx);\r
22 Expr                getExpr(TocParser::TernaryOpExprContext * ctx);\r
23 Expr                getExpr(TocParser::BracketExprContext * ctx);\r
24 Expr                getExpr(TocParser::IdentifierExprContext * ctx);\r
25 Expr                getExpr(TocParser::ExprContext * ctx);\r
26 \r
27 Stmt                getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
28 \r
29 Type getType(TocParser::TypeContext * ctx)\r
30 {\r
31   Type result;\r
32   for (auto n : ctx->namespaceSpecifier())\r
33     result.namespacePrefixes.push_back(n->typeName()->getText());\r
34   result.name = ctx->typeName()->NAME()->toString();\r
35   for (auto m : ctx->typeModifier())\r
36   {\r
37     bool isPointer = m->getText() == "*";\r
38     bool isStaticArray = m->INT_LIT() != nullptr;\r
39 \r
40     result.modifiers.emplace_back(\r
41       isPointer ? TypeModifierType::Pointer : TypeModifierType::Array,\r
42       isStaticArray,\r
43       isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1\r
44     );\r
45   }\r
46   return result;\r
47 }\r
48 Variable getVariable(TocParser::VarContext * ctx)\r
49 {\r
50   Variable result;\r
51   result.name = ctx->varName()->NAME()->toString();\r
52   result.type = getType(ctx->type());\r
53   return result;\r
54 }\r
55 Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent)\r
56 {\r
57   Body result;\r
58   result.ctx = std::make_unique<Context>();\r
59   result.ctx->parent = parent;\r
60   for (auto s : ctx->stmt())\r
61   {\r
62     if (s->varDecl() != nullptr)\r
63     {\r
64       result.ctx->variables.push_back(getVariable(s->varDecl()->var()));\r
65       if (s->varDecl()->var()->expr() != nullptr)\r
66         result.statements.push_back(getStmt(s, result.ctx));\r
67     }\r
68     else\r
69     {\r
70       result.statements.push_back(getStmt(s, result.ctx));\r
71     }\r
72   }\r
73   return result;\r
74 }\r
75 Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent)\r
76 {\r
77   Function result;\r
78   result.name = ctx->funcName()->NAME()->toString();\r
79   result.returnType = getType(ctx->type());\r
80   if (!ctx->parameter()->var().empty())\r
81   {\r
82     for (auto p : ctx->parameter()->var())\r
83       result.parameters.push_back(getVariable(p));\r
84   }\r
85   result.body = getBody(ctx->body(), parent);\r
86   return result;\r
87 }\r
88 Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent)\r
89 {\r
90   Struct result;\r
91   result.name = ctx->structName()->NAME()->toString();\r
92   for (auto m : ctx->structMember())\r
93   {\r
94     if (m->structVar() != nullptr)\r
95     {\r
96       result.members.push_back({\r
97         getVariable(m->structVar()->var()),\r
98         m->privateDecl() != nullptr\r
99       });\r
100     }\r
101     if (m->structMethod() != nullptr)\r
102     {\r
103       result.methods.push_back({\r
104         getFunction(m->structMethod()->func(), parent),\r
105         m->privateDecl() != nullptr\r
106       });\r
107     }\r
108   }\r
109   return result;\r
110 }\r
111 Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent)\r
112 {\r
113   Namespace result;\r
114   result.ctx = std::make_unique<Context>();\r
115   result.name = ctx->typeName()->getText();\r
116   for (auto d : ctx->decl())\r
117   {\r
118     if (d->varDecl() != nullptr)\r
119     {\r
120       result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
121     }\r
122     if (d->funcDecl() != nullptr)\r
123     {\r
124       result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
125     }\r
126     if (d->structDecl() != nullptr)\r
127     {\r
128       result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
129     }\r
130     if (d->namespaceDecl() != nullptr)\r
131     {\r
132       result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
133     }\r
134   }\r
135   return result;\r
136 }\r
137 Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent)\r
138 {\r
139   Program result;\r
140   result.ctx = std::make_unique<Context>();\r
141   for (auto d : ctx->decl())\r
142   {\r
143     if (d->varDecl() != nullptr)\r
144     {\r
145       result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
146     }\r
147     if (d->funcDecl() != nullptr)\r
148     {\r
149       result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
150     }\r
151     if (d->structDecl() != nullptr)\r
152     {\r
153       result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
154     }\r
155     if (d->namespaceDecl() != nullptr)\r
156     {\r
157       result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
158     }\r
159   }\r
160   return result;\r
161 }\r
162 template<typename OpType>\r
163 OpType getOperatorType(const std::string & s, std::string typeStrings[])\r
164 {\r
165   for (int i = 0; i < (int)OpType::COUNT; i++)\r
166   {\r
167     if (typeStrings[i] == s)\r
168     {\r
169       return (OpType)i;\r
170     }\r
171   }\r
172   return OpType::COUNT;\r
173 }\r
174 \r
175 \r
176 \r
177 \r
178 \r
179 \r
180 \r
181 \r
182 \r
183 \r
184 \r
185 \r
186 \r
187 Expr getExpr(TocParser::FuncExprContext * ctx)\r
188 {\r
189   Expr result;\r
190   result.type = ExprType::Func;\r
191   for (auto n : ctx->namespaceSpecifier())\r
192     result._func.namespacePrefixes.push_back(n->typeName()->getText());\r
193   result._func.functionName = ctx->funcName()->NAME()->toString();\r
194   for (auto e : ctx->expr())\r
195     result._func.arguments.push_back(getExpr(e));\r
196   return result;\r
197 }\r
198 Expr getExpr(TocParser::MethodExprContext * ctx)\r
199 {\r
200   Expr result;\r
201   result.type = ExprType::Method;\r
202   result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
203   result._method.methodName = ctx->funcName()->NAME()->toString();\r
204   for (int i = 1; i < ctx->expr().size(); i++)\r
205     result._method.arguments.push_back(getExpr(ctx->expr(i)));\r
206   return result;\r
207 }\r
208 Expr getExpr(TocParser::LitExprContext * ctx)\r
209 {\r
210   Expr result;\r
211   result.type = ExprType::Lit;\r
212   if (ctx->literal()->INT_LIT() != nullptr)\r
213   {\r
214     result._lit.type = LitType::Int;\r
215     result._lit._int = atoi(ctx->literal()->INT_LIT()->toString().c_str());\r
216   }\r
217   else if (ctx->literal()->DECIMAL_LIT() != nullptr)\r
218   {\r
219     result._lit.type = LitType::Decimal;\r
220     result._lit._decimal = atof(ctx->literal()->DECIMAL_LIT()->toString().c_str());\r
221   }\r
222   else if (ctx->literal()->STRING_LIT() != nullptr)\r
223   {\r
224     result._lit.type = LitType::String;\r
225     result._lit._string = ctx->literal()->STRING_LIT()->toString();\r
226   }\r
227   else if (ctx->literal()->BOOL_LIT() != nullptr)\r
228   {\r
229     result._lit.type = LitType::Bool;\r
230     result._lit._bool = ctx->literal()->BOOL_LIT()->toString() == "true";\r
231   }\r
232   return result;\r
233 }\r
234 Expr getExpr(TocParser::ParenExprContext * ctx)\r
235 {\r
236   Expr result;\r
237   result.type = ExprType::Paren;\r
238   result._paren.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
239   return result;\r
240 }\r
241 Expr getExpr(TocParser::DotExprContext * ctx)\r
242 {\r
243   Expr result;\r
244   result.type = ExprType::Dot;\r
245   result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
246   result._dot.identifier = ctx->varName()->getText();\r
247   return result;\r
248 }\r
249 Expr getExpr(TocParser::PrefixOpExprContext * ctx)\r
250 {\r
251   Expr result;\r
252   result.type = ExprType::PrefixOp;\r
253   result._prefixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
254   result._prefixOp.type = getOperatorType<PrefixOperatorType>(\r
255     ctx->prefix_op()->getText(),\r
256     PrefixOperatorTypeStrings);\r
257   return result;\r
258 }\r
259 Expr getExpr(TocParser::PostfixOpExprContext * ctx)\r
260 {\r
261   Expr result;\r
262   result.type = ExprType::PostfixOp;\r
263   result._postfixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
264   result._postfixOp.type = getOperatorType<PostfixOperatorType>(\r
265     ctx->postfix_op()->getText(),\r
266     PostfixOperatorTypeStrings);\r
267   return result;\r
268 }\r
269 Expr getExpr(TocParser::BinaryOpExprContext * ctx)\r
270 {\r
271   Expr result;\r
272   result.type = ExprType::BinaryOp;\r
273   result._binaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
274   result._binaryOp.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
275   result._binaryOp.type = getOperatorType<BinaryOperatorType>(\r
276     ctx->binary_op()->getText(),\r
277     BinaryOperatorTypeStrings);\r
278   return result;\r
279 }\r
280 Expr getExpr(TocParser::TernaryOpExprContext * ctx)\r
281 {\r
282   Expr result;\r
283   result.type = ExprType::TernaryOp;\r
284   result._ternaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
285   result._ternaryOp.rexprTrue = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
286   result._ternaryOp.rexprFalse = std::make_unique<Expr>(getExpr(ctx->expr(2)));\r
287   return result;\r
288 }\r
289 Expr getExpr(TocParser::BracketExprContext * ctx)\r
290 {\r
291   Expr result;\r
292   result.type = ExprType::Bracket;\r
293   result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
294   result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
295   return result;\r
296 }\r
297 Expr getExpr(TocParser::IdentifierExprContext * ctx)\r
298 {\r
299   Expr result;\r
300   result.type = ExprType::Identifier;\r
301   for (auto n : ctx->namespaceSpecifier())\r
302     result._identifier.namespacePrefixes.push_back(n->typeName()->getText());\r
303   result._identifier.identifier = ctx->varName()->getText();\r
304   return result;\r
305 }\r
306 \r
307 \r
308 \r
309 \r
310 \r
311 \r
312 \r
313 \r
314 \r
315 \r
316 \r
317 Expr getExpr(TocParser::ExprContext * ctx)\r
318 {\r
319   Expr result;\r
320   if (dynamic_cast<TocParser::FuncExprContext *>(ctx) != nullptr)\r
321     result = getExpr(dynamic_cast<TocParser::FuncExprContext *>(ctx));\r
322   if (dynamic_cast<TocParser::MethodExprContext *>(ctx) != nullptr)\r
323     result = getExpr(dynamic_cast<TocParser::MethodExprContext *>(ctx));\r
324   if (dynamic_cast<TocParser::LitExprContext *>(ctx) != nullptr)\r
325     result = getExpr(dynamic_cast<TocParser::LitExprContext *>(ctx));\r
326   if (dynamic_cast<TocParser::ParenExprContext *>(ctx) != nullptr)\r
327     result = getExpr(dynamic_cast<TocParser::ParenExprContext *>(ctx));\r
328   if (dynamic_cast<TocParser::DotExprContext *>(ctx) != nullptr)\r
329     result = getExpr(dynamic_cast<TocParser::DotExprContext *>(ctx));\r
330   if (dynamic_cast<TocParser::PrefixOpExprContext *>(ctx) != nullptr)\r
331     result = getExpr(dynamic_cast<TocParser::PrefixOpExprContext *>(ctx));\r
332   if (dynamic_cast<TocParser::PostfixOpExprContext *>(ctx) != nullptr)\r
333     result = getExpr(dynamic_cast<TocParser::PostfixOpExprContext *>(ctx));\r
334   if (dynamic_cast<TocParser::BinaryOpExprContext *>(ctx) != nullptr)\r
335     result = getExpr(dynamic_cast<TocParser::BinaryOpExprContext *>(ctx));\r
336   if (dynamic_cast<TocParser::TernaryOpExprContext *>(ctx) != nullptr)\r
337     result = getExpr(dynamic_cast<TocParser::TernaryOpExprContext *>(ctx));\r
338   if (dynamic_cast<TocParser::BracketExprContext *>(ctx) != nullptr)\r
339     result = getExpr(dynamic_cast<TocParser::BracketExprContext *>(ctx));\r
340   if (dynamic_cast<TocParser::IdentifierExprContext *>(ctx) != nullptr)\r
341     result = getExpr(dynamic_cast<TocParser::IdentifierExprContext *>(ctx));\r
342   return result;\r
343 }\r
344 Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent)\r
345 {\r
346   Stmt result;\r
347   if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)\r
348   {\r
349     result.type = StmtType::Assign;\r
350     result._assign.lexpr.type = ExprType::Identifier;\r
351     result._assign.lexpr._identifier.identifier = ctx->varDecl()->var()->varName()->getText();\r
352     result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());\r
353   }\r
354   if (ctx->ifStmt() != nullptr)\r
355   {\r
356     result.type = StmtType::If;\r
357     result._if.condition = getExpr(ctx->ifStmt()->expr());\r
358     result._if.body = getBody(ctx->ifStmt()->body(), parent);\r
359     for (auto ei : ctx->ifStmt()->elseIfStmt())\r
360     {\r
361       result._if.elses.emplace_back(\r
362         true,\r
363         std::make_unique<Expr>(getExpr(ei->expr())),\r
364         getBody(ei->body(), parent)\r
365       );\r
366     }\r
367     if (ctx->ifStmt()->elseStmt() != nullptr)\r
368     {\r
369       result._if.elses.emplace_back(\r
370         false,\r
371         nullptr,\r
372         getBody(ctx->ifStmt()->elseStmt()->body(), parent)\r
373       );\r
374     }\r
375   }\r
376   if (ctx->switchStmt() != nullptr)\r
377   {\r
378     result.type = StmtType::Switch;\r
379     result._switch.ident = std::make_unique<Expr>(getExpr(ctx->switchStmt()->expr()));\r
380     for (auto c : ctx->switchStmt()->switchBody()->switchCase())\r
381     {\r
382       result._switch.cases.emplace_back(\r
383         std::make_unique<Expr>(getExpr(c->expr())),\r
384         getBody(c->body(), parent)\r
385       );\r
386     }\r
387   }\r
388   if (ctx->forStmt() != nullptr)\r
389   {\r
390     result.type = StmtType::For;\r
391     result._for.init = std::make_unique<AssignStmt>();\r
392     result._for.init->lexpr.type = ExprType::Identifier;\r
393     result._for.init->lexpr._identifier.identifier = ctx->forStmt()->varInit()->varName()->getText();\r
394     result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr());\r
395     result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));\r
396     result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));\r
397     result._for.body = getBody(ctx->forStmt()->body(), parent);\r
398   }\r
399   if (ctx->whileStmt() != nullptr)\r
400   {\r
401     result.type = StmtType::While;\r
402     result._while.condition = getExpr(ctx->whileStmt()->expr());\r
403     result._while.body = getBody(ctx->whileStmt()->body(), parent);\r
404   }\r
405   if (ctx->assignStmt() != nullptr)\r
406   {\r
407     result.type = StmtType::Assign;\r
408     result._assign.lexpr = getExpr(ctx->assignStmt()->expr(0));\r
409     result._assign.rexpr = getExpr(ctx->assignStmt()->expr(1));\r
410   }\r
411   if (ctx->returnStmt() != nullptr)\r
412   {\r
413     result.type = StmtType::Return;\r
414     result._return.expr = getExpr(ctx->returnStmt()->expr());\r
415   }\r
416   if (ctx->expr() != nullptr)\r
417   {\r
418     result.type = StmtType::Expr;\r
419     result._expr = getExpr(ctx->expr());\r
420   }\r
421   return result;\r
422 }