9 std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)
\r
14 if (comma) out << ", ";
\r
21 std::ostream & operator<< (std::ostream & out, const Type & t);
\r
22 std::ostream & operator<< (std::ostream & out, const Variable & v);
\r
23 std::ostream & operator<< (std::ostream & out, const Body & b);
\r
24 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o);
\r
25 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o);
\r
26 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o);
\r
27 std::ostream & operator<< (std::ostream & out, const Expr & e);
\r
28 std::ostream & operator<< (std::ostream & out, const Stmt & s);
\r
30 void tocFunction (std::ostream & out, const Function & f, bool stub);
\r
31 void tocStruct (std::ostream & out, const Struct & s, bool stub);
\r
32 void tocProgram (std::ostream & out, const Program & p);
\r
34 static const int TAB_WIDTH = 2;
\r
35 static int indentation = 0;
\r
36 static void indent(std::ostream & out, int change = 0)
\r
38 indentation += change;
\r
39 out << std::string(indentation, ' ');
\r
42 std::ostream & operator<< (std::ostream & out, const Type & t)
\r
48 std::ostream & operator<< (std::ostream & out, const Variable & v)
\r
50 out << v.type << " ";
\r
52 std::stringstream sstr;
\r
53 std::string s = v.name;
\r
55 for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
\r
57 if (m->type == TypeModifierType::Pointer)
\r
59 sstr.str(std::string());
\r
60 sstr << "*(" << s << ")";
\r
65 sstr.str(std::string());
\r
66 sstr << "(" << s << ")[";
\r
67 if (m->_staticArray)
\r
68 sstr << m->_arraySize;
\r
77 std::ostream & operator<< (std::ostream & out, const Body & b)
\r
83 for (auto v : b.variables)
\r
91 for (auto s : b.statements)
\r
102 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o)
\r
104 if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost)
\r
106 out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;
\r
110 out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];
\r
115 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o)
\r
117 out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr;
\r
121 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o)
\r
123 out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;
\r
127 std::ostream & operator<< (std::ostream & out, const Expr & e)
\r
129 if (e.parenthesized)
\r
134 case ExprType::Func:
\r
135 out << e._func.functionName << "(" << e._func.arguments << ")"; break;
\r
136 case ExprType::Lit:
\r
137 /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
\r
138 else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
\r
139 else if (e._lit.type == LitType::String) out << e._lit._string;
\r
140 else if (e._lit.type == LitType::Bool) out << e._lit._bool;
\r
142 case ExprType::Identifier:
\r
143 out << e._identifier.name; break;
\r
144 case ExprType::Brackets:
\r
145 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
146 case ExprType::Dot:
\r
147 out << *e._dot.expr << "." << e._dot.ident.name; break;
\r
148 case ExprType::UnaryOperator:
\r
149 out << e._unaryOperator; break;
\r
150 case ExprType::BinaryOperator:
\r
151 out << e._binaryOperator; break;
\r
152 case ExprType::TernaryOperator:
\r
153 out << e._ternaryOperator; break;
\r
156 if (e.parenthesized)
\r
161 std::ostream & operator<< (std::ostream & out, const Stmt & s)
\r
166 out << "if (" << s._if.condition << ")\n" << s._if.body; break;
\r
167 case StmtType::Switch:
\r
168 out << "switch (" << s._switch.ident.name << ")\n{\n";
\r
169 for (auto c : s._switch.cases)
\r
172 out << "case " << *c.expr << ": " << c.body << "break;";
\r
177 case StmtType::For:
\r
179 s._for.varName << " = " << *s._for.initValue << "; " <<
\r
180 *s._for.condition << "; " <<
\r
182 ")\n" << s._for.body; break;
\r
183 case StmtType::While:
\r
184 out << "while (" << s._while.condition << ")\n" << s._while.body; break;
\r
185 case StmtType::Assign:
\r
186 out << s._assign.name << " = " << s._assign.expr << ";"; break;
\r
187 case StmtType::Return:
\r
188 out << "return " << s._return.expr << ";"; break;
\r
189 case StmtType::Expr:
\r
190 out << s._expr << ";"; break;
\r
197 void tocFunction (std::ostream & out, const Function & f, bool stub)
\r
199 out << f.returnType << " " << f.name << " (" << f.parameters << ")";
\r
207 out << "\n" << f.body;
\r
210 void tocStruct (std::ostream & out, const Struct & s, bool stub)
\r
212 out << "struct " << s.name;
\r
216 for (auto m : s.methods)
\r
218 m.parameters.insert(m.parameters.begin(),
\r
221 {{TypeModifierType::Pointer, false, -1}}}});
\r
222 out << m.returnType << " " <<
\r
223 s.name << "_" << m.name <<
\r
224 " (" << m.parameters << ");\n";
\r
231 for (auto m : s.members)
\r
240 for (auto m : s.methods)
\r
242 m.parameters.insert(m.parameters.begin(),
\r
245 {{TypeModifierType::Pointer, false, -1}}}});
\r
246 out << m.returnType << " " << s.name << "_" << m.name << " (" << m.parameters << ")\n" << m.body;
\r
249 void tocProgram (std::ostream & out, const Program & p)
\r
251 for (auto s : p.structs)
\r
253 tocStruct(out, s, true);
\r
255 for (auto f : p.functions)
\r
257 tocFunction(out, f, true);
\r
260 for (auto v : p.variables)
\r
264 for (auto s : p.structs)
\r
266 tocStruct(out, s, false);
\r
268 for (auto f : p.functions)
\r
270 tocFunction(out, f, false);
\r