5 #include "TocParser.h"
\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
28 void toc_stub(std::ostream & o, TocParser::FuncContext * ctx);
\r
29 void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx);
\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
37 for (auto * decl : ctx->decl()) {
\r
38 if (decl->varDecl() != nullptr) {
\r
39 toc(o, decl->varDecl());
\r
42 else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());
\r
43 else if (decl->funcDecl() != nullptr) toc(o, decl->funcDecl()->func());
\r
46 void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {
\r
48 << ctx->var()->type()->getText()
\r
50 << ctx->var()->varName()->getText();
\r
52 if (ctx->var()->expr() != nullptr) {
\r
54 toc(o, ctx->var()->expr());
\r
57 void toc(std::ostream & o, TocParser::FuncContext * ctx) {
\r
59 << ctx->type()->getText()
\r
61 << ctx->funcName()->getText()
\r
64 if (ctx->parameter()->firstParameter() != nullptr) {
\r
66 << ctx->parameter()->firstParameter()->var()->type()->getText()
\r
68 << ctx->parameter()->firstParameter()->var()->varName()->getText();
\r
70 for (auto * par : ctx->parameter()->additionalParameter()) {
\r
73 << par->var()->type()->getText()
\r
75 << par->var()->varName()->getText();
\r
81 toc(o, ctx->body());
\r
85 void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {
\r
87 << "typedef struct "
\r
88 << ctx->structName()->getText()
\r
91 for (auto * member : ctx->structMember()) {
\r
92 if (member->structVar() != nullptr) {
\r
94 << member->structVar()->var()->type()->getText()
\r
96 << member->structVar()->var()->varName()->getText()
\r
101 << ctx->structName()->getText()
\r
103 for (auto * member : ctx->structMember()) {
\r
104 if (member->structMethod() != nullptr) {
\r
106 << member->structMethod()->func()->type()->getText()
\r
108 << ctx->structName()->getText()
\r
110 << member->structMethod()->func()->funcName()->getText()
\r
112 << ctx->structName()->getText()
\r
115 if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {
\r
118 << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()
\r
120 << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();
\r
122 for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {
\r
125 << par->var()->type()->getText()
\r
127 << par->var()->varName()->getText();
\r
133 toc(o, member->structMethod()->func()->body());
\r
139 void toc(std::ostream & o, TocParser::BodyContext * ctx) {
\r
140 for (auto * stmt : ctx->stmt()) {
\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
153 if (ctx->conditional() == nullptr && ctx->loop() == nullptr)
\r
156 void toc(std::ostream & o, TocParser::IfCondContext * ctx) {
\r
158 toc(o, ctx->expr());
\r
160 toc(o, ctx->body());
\r
163 void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {
\r
165 toc(o, ctx->expr());
\r
167 toc(o, ctx->body());
\r
170 void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {
\r
171 toc(o, ctx->identifier());
\r
173 toc(o, ctx->expr());
\r
175 void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {
\r
177 toc(o, ctx->expr());
\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
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
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
202 void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {
\r
204 << ctx->funcName()->getText()
\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
212 void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {
\r
213 o << ctx->getText();
\r
215 void toc(std::ostream & o, TocParser::LiteralContext * ctx) {
\r
216 if (ctx->INTLIT() != nullptr) o << ctx->INTLIT()->getText();
\r
218 void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {
\r
219 toc(o, ctx->nonSubscriptExpr());
\r
221 toc(o, ctx->expr());
\r
224 void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {
\r
225 toc(o, ctx->identifier(0));
\r
227 toc(o, ctx->identifier(1));
\r
229 void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {
\r
231 toc(o, ctx->expr());
\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
239 << ctx->BINARY_OPERATOR(i)->getText()
\r
241 toc(o, ctx->nonOpExpr(i + 1));
\r
245 void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {
\r
247 << ctx->type()->getText()
\r
249 << ctx->funcName()->getText()
\r
252 if (ctx->parameter()->firstParameter() != nullptr) {
\r
254 << ctx->parameter()->firstParameter()->var()->type()->getText()
\r
256 << ctx->parameter()->firstParameter()->var()->varName()->getText();
\r
258 for (auto * par : ctx->parameter()->additionalParameter()) {
\r
261 << par->var()->type()->getText()
\r
263 << par->var()->varName()->getText();
\r
269 void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {
\r
272 << ctx->structName()->getText()
\r