]> gitweb.ps.run Git - toc/blob - src/repr_get.h
compile again
[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   Type result;\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
28     );\r
29   }\r
30   return result;\r
31 }\r
32 Variable getVariable(TocParser::VarContext * ctx) {\r
33   Variable result;\r
34   result.name = ctx->varName()->NAME()->toString();\r
35   result.type = getType(ctx->type());\r
36   return result;\r
37 }\r
38 Body getBody(TocParser::BodyContext * ctx) {\r
39   Body result;\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
45     }\r
46     else {\r
47       result.statements.push_back(getStmt(s));\r
48     }\r
49   }\r
50   return result;\r
51 }\r
52 Function getFunction(TocParser::FuncContext * ctx) {\r
53   Function result;\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
59   }\r
60   result.body = getBody(ctx->body());\r
61   return result;\r
62 }\r
63 Struct getStruct(TocParser::StructDeclContext * ctx) {\r
64   Struct result;\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
69     }\r
70     if (m->structMethod() != nullptr) {\r
71       result.methods.push_back(getFunction(m->structMethod()->func()));\r
72     }\r
73   }\r
74   return result;\r
75 }\r
76 Program getProgram(TocParser::ProgContext * ctx) {\r
77   Program result;\r
78   for (auto d : ctx->decl()) {\r
79     if (d->varDecl() != nullptr) {\r
80       result.variables.push_back(getVariable(d->varDecl()->var()));\r
81     }\r
82     if (d->funcDecl() != nullptr) {\r
83       result.functions.push_back(getFunction(d->funcDecl()->func()));\r
84     }\r
85     if (d->structDecl() != nullptr) {\r
86       result.structs.push_back(getStruct(d->structDecl()));\r
87     }\r
88   }\r
89   return result;\r
90 }\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
95     }\r
96   }\r
97   return UnaryOperatorType::COUNT;\r
98 }\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
103     }\r
104   }\r
105   return BinaryOperatorType::COUNT;\r
106 }\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
111   else\r
112     result.expr = std::make_unique<Expr>(getExpr(ctx->postfixOp()->nonOpExpr()));\r
113 \r
114   std::string op;\r
115   if (ctx->prefixOp() != nullptr)\r
116     op = ctx->prefixOp()->prefix_op()->getText();\r
117   else\r
118     op = ctx->postfixOp()->postfix_op()->getText();\r
119 \r
120   // TODO: postfix type\r
121 \r
122   result.type = getUnaryOperatorType(op);\r
123 \r
124   return result;\r
125 }\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
130   \r
131   std::string op = ctx->binaryOp()->binary_op(0)->getText();\r
132   std::cout << op << std::endl;\r
133 \r
134   result.type = getBinaryOperatorType(op);\r
135 \r
136   return result;\r
137 }\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
143   return result;\r
144 }\r
145 Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
146   Expr result;\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
152   }\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
158     }\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
162     }\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
166     }\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
170     }\r
171   }\r
172   if (ctx->identifierExpr() != nullptr) {\r
173     result.type = ExprType::Identifier;\r
174     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
175   }\r
176   if (ctx->parenExpr() != nullptr) {\r
177     result = getExpr(ctx->parenExpr()->expr());\r
178   }\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
185       }\r
186       else {\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
190       }\r
191     }\r
192   }\r
193   return result;\r
194 }\r
195 Expr getExpr(TocParser::NonAccessExprContext * ctx) {\r
196   Expr result;\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
202   }\r
203   if (ctx->identifierExpr() != nullptr) {\r
204     result.type = ExprType::Identifier;\r
205     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
206   }\r
207   if (ctx->parenExpr() != nullptr) {\r
208     result = getExpr(ctx->parenExpr()->expr());\r
209   }\r
210   return result;\r
211 }\r
212 Expr getExpr(TocParser::ExprContext * ctx) {\r
213   Expr result;\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
219   }\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
225     }\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
229     }\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
233     }\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
237     }\r
238   }\r
239   if (ctx->identifierExpr() != nullptr) {\r
240     result.type = ExprType::Identifier;\r
241     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
242   }\r
243   if (ctx->parenExpr() != nullptr) {\r
244     result = getExpr(ctx->parenExpr()->expr());\r
245   }\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
253       }\r
254       else {\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
258       }\r
259     }\r
260   }\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
265     }\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
270         Expr tmp = result;\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
274       }\r
275     }\r
276     else if (ctx->opExpr()->ternaryOp() != nullptr) {\r
277       result.type = ExprType::TernaryOperator;\r
278       result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());\r
279     }\r
280   }\r
281   return result;\r
282 }\r
283 Stmt getStmt(TocParser::StmtContext * ctx) {\r
284   Stmt result;\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
289   }\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
296         true,\r
297         std::make_unique<Expr>(getExpr(ei->expr())),\r
298         getBody(ei->body())\r
299       );\r
300     }\r
301     if (ctx->ifStmt()->elseStmt() != nullptr) {\r
302       result._if.elses.emplace_back(\r
303         false,\r
304         nullptr,\r
305         getBody(ctx->ifStmt()->elseStmt()->body())\r
306       );\r
307     }\r
308   }\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
315         getBody(c->body())\r
316       );\r
317     }\r
318   }\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
324     }\r
325     else {\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
328     }\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
332   }\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
337   }\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
342   }\r
343   if (ctx->returnStmt() != nullptr) {\r
344     result.type = StmtType::Return;\r
345     result._return.expr = getExpr(ctx->returnStmt()->expr());\r
346   }\r
347   if (ctx->expr() != nullptr) {\r
348     result.type = StmtType::Expr;\r
349     result._expr = getExpr(ctx->expr());\r
350   }\r
351   return result;\r
352 }