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