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