8 std::ostream & operator<< (std::ostream & out, const std::vector<T> & v) {
\r
11 if (comma) out << ", ";
\r
18 std::ostream & operator<< (std::ostream & out, const Type & t);
\r
19 std::ostream & operator<< (std::ostream & out, const Variable & v);
\r
20 std::ostream & operator<< (std::ostream & out, const Body & b);
\r
21 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o);
\r
22 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o);
\r
23 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o);
\r
24 std::ostream & operator<< (std::ostream & out, const Expr & e);
\r
25 std::ostream & operator<< (std::ostream & out, const Stmt & s);
\r
27 void tocFunction (std::ostream & out, const Function & f, bool stub);
\r
28 void tocStruct (std::ostream & out, const Struct & s, bool stub);
\r
29 void tocProgram (std::ostream & out, const Program & p);
\r
31 static const int TAB_WIDTH = 2;
\r
32 static int indentation = 0;
\r
33 static void indent(std::ostream & out, int change = 0) {
\r
34 indentation += change;
\r
35 out << std::string(indentation, ' ');
\r
38 std::ostream & operator<< (std::ostream & out, const Type & t) {
\r
43 std::ostream & operator<< (std::ostream & out, const Variable & v) {
\r
44 out << v.type << " " << v.name;
\r
48 std::ostream & operator<< (std::ostream & out, const Body & b) {
\r
53 for (auto v : b.variables) {
\r
60 for (auto s : b.statements) {
\r
70 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o) {
\r
71 if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost) {
\r
72 out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;
\r
75 out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];
\r
80 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o) {
\r
81 out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr;
\r
85 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o) {
\r
86 out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;
\r
90 std::ostream & operator<< (std::ostream & out, const Expr & e) {
\r
92 case ExprType::Brackets:
\r
93 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
94 case ExprType::Call:
\r
95 out << e._call.functionName << "(" << e._call.arguments << ")"; break;
\r
97 out << e._dot.name << "." << *e._dot.lexpr; break;
\r
98 case ExprType::Literal:
\r
99 out << e._literal.i; break;
\r
100 case ExprType::Operator:
\r
101 out << e._operator; break;
\r
102 case ExprType::Variable:
\r
103 out << e._variable.name; break;
\r
108 std::ostream & operator<< (std::ostream & out, const Stmt & s) {
\r
110 case StmtType::Assign:
\r
111 out << s._assign.lexpr << "=" << s._assign.rexpr; break;
\r
112 case StmtType::Expr:
\r
113 out << s._expr; break;
\r
115 out << "if (" << s._if.condition << ")\n" << s._if.body; break;
\r
116 case StmtType::Return:
\r
117 out << "return " << s._return.expr; break;
\r
118 case StmtType::While:
\r
119 out << "while (" << s._while.condition << ")\n" << s._while.body; break;
\r
126 void tocFunction (std::ostream & out, const Function & f, bool stub) {
\r
127 out << f.returnType << " " << f.name << " (";
\r
129 bool comma = false;
\r
130 for (auto p : f.parameters) {
\r
131 if (comma) out << ", ";
\r
134 out << p.type << " " << p.name;
\r
143 out << "\n" << f.body;
\r
146 void tocStruct (std::ostream & out, const Struct & s, bool stub) {
\r
147 out << "struct " << s.name;
\r
155 for (auto m : s.members) {
\r
163 void tocProgram (std::ostream & out, const Program & p) {
\r
164 for (auto s : p.structs) {
\r
165 tocStruct(out, s, true);
\r
167 for (auto s : p.structs) {
\r
168 tocStruct(out, s, false);
\r
171 for (auto v : p.variables) {
\r
175 for (auto f : p.functions) {
\r
176 tocFunction(out, f, true);
\r
178 for (auto f : p.functions) {
\r
179 tocFunction(out, f, false);
\r
186 // void toc(std::ostream & o, TocParser::ProgContext * ctx) {
\r
187 // for (auto * decl : ctx->decl()) {
\r
188 // /**/ if (decl->structDecl() != nullptr) toc_stub(o, decl->structDecl());
\r
189 // else if (decl->funcDecl() != nullptr) toc_stub(o, decl->funcDecl()->func());
\r
191 // for (auto * decl : ctx->decl()) {
\r
192 // if (decl->varDecl() != nullptr) {
\r
193 // toc(o, decl->varDecl());
\r
196 // else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());
\r
197 // else if (decl->funcDecl() != nullptr) toc(o, decl->funcDecl()->func());
\r
200 // void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {
\r
202 // << ctx->var()->type()->getText()
\r
204 // << ctx->var()->varName()->getText();
\r
206 // if (ctx->var()->expr() != nullptr) {
\r
208 // toc(o, ctx->var()->expr());
\r
211 // void toc(std::ostream & o, TocParser::FuncContext * ctx) {
\r
213 // << ctx->type()->getText()
\r
215 // << ctx->funcName()->getText()
\r
218 // if (ctx->parameter()->firstParameter() != nullptr) {
\r
220 // << ctx->parameter()->firstParameter()->var()->type()->getText()
\r
222 // << ctx->parameter()->firstParameter()->var()->varName()->getText();
\r
224 // for (auto * par : ctx->parameter()->additionalParameter()) {
\r
227 // << par->var()->type()->getText()
\r
229 // << par->var()->varName()->getText();
\r
233 // out << ")\n{\n";
\r
235 // toc(o, ctx->body());
\r
239 // void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {
\r
241 // << "typedef struct "
\r
242 // << ctx->structName()->getText()
\r
245 // for (auto * member : ctx->structMember()) {
\r
246 // if (member->structVar() != nullptr) {
\r
248 // << member->structVar()->var()->type()->getText()
\r
250 // << member->structVar()->var()->varName()->getText()
\r
255 // << ctx->structName()->getText()
\r
257 // for (auto * member : ctx->structMember()) {
\r
258 // if (member->structMethod() != nullptr) {
\r
260 // << member->structMethod()->func()->type()->getText()
\r
262 // << ctx->structName()->getText()
\r
264 // << member->structMethod()->func()->funcName()->getText()
\r
266 // << ctx->structName()->getText()
\r
269 // if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {
\r
272 // << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()
\r
274 // << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();
\r
276 // for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {
\r
279 // << par->var()->type()->getText()
\r
281 // << par->var()->varName()->getText();
\r
285 // out << ")\n{\n";
\r
287 // toc(o, member->structMethod()->func()->body());
\r
293 // void toc(std::ostream & o, TocParser::BodyContext * ctx) {
\r
294 // for (auto * stmt : ctx->stmt()) {
\r
299 // void toc(std::ostream & o, TocParser::StmtContext * ctx) {
\r
300 // /**/ if (ctx->varDecl() != nullptr) toc(o, ctx->varDecl());
\r
301 // else if (ctx->conditional() != nullptr) toc(o, ctx->conditional()->ifCond());
\r
302 // else if (ctx->loop() != nullptr) toc(o, ctx->loop()->whileLoop());
\r
303 // else if (ctx->assignment() != nullptr) toc(o, ctx->assignment());
\r
304 // else if (ctx->returnStmt() != nullptr) toc(o, ctx->returnStmt());
\r
305 // else if (ctx->expr() != nullptr) toc(o, ctx->expr());
\r
307 // if (ctx->conditional() == nullptr && ctx->loop() == nullptr)
\r
310 // void toc(std::ostream & o, TocParser::IfCondContext * ctx) {
\r
312 // toc(o, ctx->expr());
\r
313 // out << ")\n{\n";
\r
314 // toc(o, ctx->body());
\r
317 // void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {
\r
318 // out << "while (";
\r
319 // toc(o, ctx->expr());
\r
320 // out << ")\n{\n";
\r
321 // toc(o, ctx->body());
\r
324 // void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {
\r
325 // toc(o, ctx->identifier());
\r
327 // toc(o, ctx->expr());
\r
329 // void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {
\r
330 // out << "return ";
\r
331 // toc(o, ctx->expr());
\r
333 // void toc(std::ostream & o, TocParser::ExprContext * ctx) {
\r
334 // /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
\r
335 // else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
\r
336 // else if (ctx->literal() != nullptr) toc(o, ctx->literal());
\r
337 // else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
\r
338 // else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
\r
339 // else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
\r
340 // else if (ctx->operatorExpr() != nullptr) toc(o, ctx->operatorExpr()->binaryOperator());
\r
342 // void toc(std::ostream & o, TocParser::NonOpExprContext * ctx) {
\r
343 // /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
\r
344 // else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
\r
345 // else if (ctx->literal() != nullptr) toc(o, ctx->literal());
\r
346 // else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
\r
347 // else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
\r
348 // else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
\r
350 // void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx) {
\r
351 // /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
\r
352 // else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
\r
353 // else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
\r
354 // else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
\r
356 // void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {
\r
358 // << ctx->funcName()->getText()
\r
360 // for (int i = 0; i < ctx->expr().size(); i++) {
\r
361 // if (i != 0) out << ", ";
\r
362 // toc(o, ctx->expr(i));
\r
366 // void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {
\r
367 // out << ctx->getText();
\r
369 // void toc(std::ostream & o, TocParser::LiteralContext * ctx) {
\r
370 // if (ctx->INTLIT() != nullptr) out << ctx->INTLIT()->getText();
\r
372 // void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {
\r
373 // toc(o, ctx->nonSubscriptExpr());
\r
375 // toc(o, ctx->expr());
\r
378 // void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {
\r
379 // toc(o, ctx->identifier(0));
\r
381 // toc(o, ctx->identifier(1));
\r
383 // void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {
\r
385 // toc(o, ctx->expr());
\r
388 // void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx) {
\r
389 // for (int i = 0; i < ctx->BINARY_OPERATOR().size(); i++) {
\r
390 // toc(o, ctx->nonOpExpr(i));
\r
393 // << ctx->BINARY_OPERATOR(i)->getText()
\r
395 // toc(o, ctx->nonOpExpr(i + 1));
\r
399 // void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {
\r
401 // << ctx->type()->getText()
\r
403 // << ctx->funcName()->getText()
\r
406 // if (ctx->parameter()->firstParameter() != nullptr) {
\r
408 // << ctx->parameter()->firstParameter()->var()->type()->getText()
\r
410 // << ctx->parameter()->firstParameter()->var()->varName()->getText();
\r
412 // for (auto * par : ctx->parameter()->additionalParameter()) {
\r
415 // << par->var()->type()->getText()
\r
417 // << par->var()->varName()->getText();
\r
423 // void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {
\r
426 // << ctx->structName()->getText()
\r