]> gitweb.ps.run Git - toc/blob - src/toc.h
Initial commit
[toc] / src / toc.h
1 #pragma once\r
2 \r
3 #include <iostream>\r
4 \r
5 #include "TocParser.h"\r
6 \r
7 void toc(std::ostream & o, TocParser::ProgContext * ctx);\r
8 void toc(std::ostream & o, TocParser::VarDeclContext * ctx);\r
9 void toc(std::ostream & o, TocParser::FuncContext * ctx);\r
10 void toc(std::ostream & o, TocParser::StructDeclContext * ctx);\r
11 void toc(std::ostream & o, TocParser::BodyContext * ctx);\r
12 void toc(std::ostream & o, TocParser::StmtContext * ctx);\r
13 void toc(std::ostream & o, TocParser::IfCondContext * ctx);\r
14 void toc(std::ostream & o, TocParser::WhileLoopContext * ctx);\r
15 void toc(std::ostream & o, TocParser::AssignmentContext * ctx);\r
16 void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx);\r
17 void toc(std::ostream & o, TocParser::ExprContext * ctx);\r
18 void toc(std::ostream & o, TocParser::NonOpExprContext * ctx);\r
19 void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx);\r
20 void toc(std::ostream & o, TocParser::FuncCallContext * ctx);\r
21 void toc(std::ostream & o, TocParser::IdentifierContext * ctx);\r
22 void toc(std::ostream & o, TocParser::LiteralContext * ctx);\r
23 void toc(std::ostream & o, TocParser::SubscriptContext * ctx);\r
24 void toc(std::ostream & o, TocParser::MemberAccessContext * ctx);\r
25 void toc(std::ostream & o, TocParser::ParenExprContext * ctx);\r
26 void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx);\r
27 \r
28 void toc_stub(std::ostream & o, TocParser::FuncContext * ctx);\r
29 void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx);\r
30 \r
31 \r
32 void toc(std::ostream & o, TocParser::ProgContext * ctx) {\r
33   for (auto * decl : ctx->decl()) {\r
34     /**/ if (decl->structDecl() != nullptr) toc_stub(o, decl->structDecl());\r
35     else if (decl->funcDecl()   != nullptr) toc_stub(o, decl->funcDecl()->func());\r
36   }\r
37   for (auto * decl : ctx->decl()) {\r
38     if (decl->varDecl()    != nullptr) {\r
39       toc(o, decl->varDecl());\r
40       o << ";\n";\r
41     }\r
42     else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());\r
43     else if (decl->funcDecl()   != nullptr) toc(o, decl->funcDecl()->func());\r
44   }\r
45 }\r
46 void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {\r
47   o\r
48     << ctx->var()->type()->getText()\r
49     << " "\r
50     << ctx->var()->varName()->getText();\r
51   \r
52   if (ctx->var()->expr() != nullptr) {\r
53     o << " = ";\r
54     toc(o, ctx->var()->expr());\r
55   }\r
56 }\r
57 void toc(std::ostream & o, TocParser::FuncContext * ctx) {\r
58   o\r
59     << ctx->type()->getText()\r
60     << " "\r
61     << ctx->funcName()->getText()\r
62     << "(";\r
63 \r
64   if (ctx->parameter()->firstParameter() != nullptr) {\r
65     o\r
66       << ctx->parameter()->firstParameter()->var()->type()->getText()\r
67       << " "\r
68       << ctx->parameter()->firstParameter()->var()->varName()->getText();\r
69 \r
70     for (auto * par : ctx->parameter()->additionalParameter()) {\r
71       o\r
72         << ", "\r
73         << par->var()->type()->getText()\r
74         << " "\r
75         << par->var()->varName()->getText();\r
76     }\r
77   }\r
78 \r
79   o << ")\n{\n";\r
80 \r
81   toc(o, ctx->body());\r
82 \r
83   o << "}\n";\r
84 }\r
85 void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
86   o\r
87     << "typedef struct "\r
88     << ctx->structName()->getText()\r
89     << "\n{\n";\r
90 \r
91   for (auto * member : ctx->structMember()) {\r
92     if (member->structVar() != nullptr) {\r
93       o\r
94         << member->structVar()->var()->type()->getText()\r
95         << " "\r
96         << member->structVar()->var()->varName()->getText()\r
97         << ";\n";\r
98     }\r
99   }\r
100   o << "} "\r
101     << ctx->structName()->getText()\r
102     << ";\n";\r
103   for (auto * member : ctx->structMember()) {\r
104     if (member->structMethod() != nullptr) {\r
105       o\r
106         << member->structMethod()->func()->type()->getText()\r
107         << " "\r
108         << ctx->structName()->getText()\r
109         << "_"\r
110         << member->structMethod()->func()->funcName()->getText()\r
111         << "("\r
112         << ctx->structName()->getText()\r
113         << " * this";\r
114 \r
115       if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {\r
116         o\r
117           << ", "\r
118           << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()\r
119           << " "\r
120           << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();\r
121 \r
122         for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {\r
123           o\r
124             << ", "\r
125             << par->var()->type()->getText()\r
126             << " "\r
127             << par->var()->varName()->getText();\r
128         }\r
129       }\r
130 \r
131       o << ")\n{\n";\r
132 \r
133       toc(o, member->structMethod()->func()->body());\r
134 \r
135       o << "}\n";\r
136     }\r
137   }\r
138 }\r
139 void toc(std::ostream & o, TocParser::BodyContext * ctx) {\r
140   for (auto * stmt : ctx->stmt()) {\r
141     toc(o, stmt);\r
142     o << "\n";\r
143   }\r
144 }\r
145 void toc(std::ostream & o, TocParser::StmtContext * ctx) {\r
146   /**/ if (ctx->varDecl() != nullptr) toc(o, ctx->varDecl());\r
147   else if (ctx->conditional() != nullptr) toc(o, ctx->conditional()->ifCond());\r
148   else if (ctx->loop() != nullptr) toc(o, ctx->loop()->whileLoop());\r
149   else if (ctx->assignment() != nullptr) toc(o, ctx->assignment());\r
150   else if (ctx->returnStmt() != nullptr) toc(o, ctx->returnStmt());\r
151   else if (ctx->expr() != nullptr) toc(o, ctx->expr());\r
152 \r
153   if (ctx->conditional() == nullptr && ctx->loop() == nullptr)\r
154     o << ";";\r
155 }\r
156 void toc(std::ostream & o, TocParser::IfCondContext * ctx) {\r
157   o << "if (";\r
158   toc(o, ctx->expr());\r
159   o << ")\n{\n";\r
160   toc(o, ctx->body());\r
161   o << "}\n";\r
162 }\r
163 void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {\r
164   o << "while (";\r
165   toc(o, ctx->expr());\r
166   o << ")\n{\n";\r
167   toc(o, ctx->body());\r
168   o << "}\n";\r
169 }\r
170 void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {\r
171   toc(o, ctx->identifier());\r
172   o << " = ";\r
173   toc(o, ctx->expr());\r
174 }\r
175 void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {\r
176   o << "return ";\r
177   toc(o, ctx->expr());\r
178 }\r
179 void toc(std::ostream & o, TocParser::ExprContext * ctx) {\r
180     /**/ if (ctx->funcCall()     != nullptr) toc(o, ctx->funcCall());\r
181     else if (ctx->identifier()   != nullptr) toc(o, ctx->identifier());\r
182     else if (ctx->literal()      != nullptr) toc(o, ctx->literal());\r
183     else if (ctx->subscript()    != nullptr) toc(o, ctx->subscript());\r
184     else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
185     else if (ctx->parenExpr()    != nullptr) toc(o, ctx->parenExpr());\r
186     else if (ctx->operatorExpr() != nullptr) toc(o, ctx->operatorExpr()->binaryOperator());\r
187 }\r
188 void toc(std::ostream & o, TocParser::NonOpExprContext * ctx) {\r
189     /**/ if (ctx->funcCall()     != nullptr) toc(o, ctx->funcCall());\r
190     else if (ctx->identifier()   != nullptr) toc(o, ctx->identifier());\r
191     else if (ctx->literal()      != nullptr) toc(o, ctx->literal());\r
192     else if (ctx->subscript()    != nullptr) toc(o, ctx->subscript());\r
193     else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
194     else if (ctx->parenExpr()    != nullptr) toc(o, ctx->parenExpr());\r
195 }\r
196 void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx) {\r
197     /**/ if (ctx->funcCall()     != nullptr) toc(o, ctx->funcCall());\r
198     else if (ctx->identifier()   != nullptr) toc(o, ctx->identifier());\r
199     else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());\r
200     else if (ctx->parenExpr()    != nullptr) toc(o, ctx->parenExpr());\r
201 }\r
202 void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {\r
203   o\r
204     << ctx->funcName()->getText()\r
205     << "(";\r
206   for (int i = 0; i < ctx->expr().size(); i++) {\r
207     if (i != 0) o << ", ";\r
208     toc(o, ctx->expr(i));\r
209   }\r
210   o << ")";\r
211 }\r
212 void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {\r
213   o << ctx->getText();\r
214 }\r
215 void toc(std::ostream & o, TocParser::LiteralContext * ctx) {\r
216   if (ctx->INTLIT() != nullptr) o << ctx->INTLIT()->getText();\r
217 }\r
218 void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {\r
219   toc(o, ctx->nonSubscriptExpr());\r
220   o << "[";\r
221   toc(o, ctx->expr());\r
222   o << "]";\r
223 }\r
224 void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {\r
225   toc(o, ctx->identifier(0));\r
226   o << ".";\r
227   toc(o, ctx->identifier(1));\r
228 }\r
229 void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {\r
230   o << "(";\r
231   toc(o, ctx->expr());\r
232   o << ")";\r
233 }\r
234 void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx) {\r
235   for (int i = 0; i < ctx->BINARY_OPERATOR().size(); i++) {\r
236     toc(o, ctx->nonOpExpr(i));\r
237     o\r
238       << " "\r
239       << ctx->BINARY_OPERATOR(i)->getText()\r
240       << " ";\r
241     toc(o, ctx->nonOpExpr(i + 1));\r
242   }\r
243 }\r
244 \r
245 void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {\r
246   o\r
247     << ctx->type()->getText()\r
248     << " "\r
249     << ctx->funcName()->getText()\r
250     << "(";\r
251 \r
252   if (ctx->parameter()->firstParameter() != nullptr) {\r
253     o\r
254       << ctx->parameter()->firstParameter()->var()->type()->getText()\r
255       << " "\r
256       << ctx->parameter()->firstParameter()->var()->varName()->getText();\r
257 \r
258     for (auto * par : ctx->parameter()->additionalParameter()) {\r
259       o\r
260         << ", "\r
261         << par->var()->type()->getText()\r
262         << " "\r
263         << par->var()->varName()->getText();\r
264     }\r
265   }\r
266 \r
267   o << ");\n";\r
268 }\r
269 void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {\r
270   o\r
271     << "struct "\r
272     << ctx->structName()->getText()\r
273     << ";\n";\r
274 }\r