7 #include "typeInfo.h"
\r
10 std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)
\r
12 std::stringstream sstr;
\r
14 bool putSeparator = false;
\r
17 if (putSeparator) sstr << separator;
\r
18 else putSeparator = true;
\r
21 if (end && !v.empty())
\r
27 std::ostream & operator<< (std::ostream & out, const Type & t);
\r
28 std::ostream & operator<< (std::ostream & out, const Variable & v);
\r
29 std::ostream & operator<< (std::ostream & out, const Body & b);
\r
30 std::ostream & operator<< (std::ostream & out, const Expr & e);
\r
31 std::ostream & operator<< (std::ostream & out, const Stmt & s);
\r
33 void tocFunction (std::ostream & out, const Function & f, bool stub);
\r
34 void tocStruct (std::ostream & out, const Struct & s, bool stub);
\r
35 void tocProgram (std::ostream & out, const Program & p);
\r
36 void tocNamespace (std::ostream & out, const Namespace & n, bool stub);
\r
38 static const int TAB_WIDTH = 2;
\r
39 static int indentation = 0;
\r
40 static void indent(std::ostream & out, int change = 0)
\r
42 indentation += change;
\r
43 out << std::string(indentation, ' ');
\r
46 static std::vector<std::string> namespaces;
\r
47 static std::string namespacePrefix() {
\r
48 std::stringstream sstr;
\r
49 for (auto n : namespaces)
\r
56 static Program globalPrg;
\r
58 std::ostream & operator<< (std::ostream & out, const Type & t)
\r
60 out << vectorStr(t.namespacePrefixes, "_", true) << t.name;
\r
64 std::ostream & operator<< (std::ostream & out, const Variable & v)
\r
66 out << v.type << " ";
\r
68 std::stringstream sstr;
\r
69 std::string s = v.name;
\r
71 for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
\r
73 if (m->type == TypeModifierType::Pointer)
\r
75 sstr.str(std::string());
\r
76 sstr << "*(" << s << ")";
\r
81 sstr.str(std::string());
\r
82 sstr << "(" << s << ")[";
\r
83 if (m->_staticArray)
\r
84 sstr << m->_arraySize;
\r
93 std::ostream & operator<< (std::ostream & out, const Body & b)
\r
99 for (auto v : b.variables)
\r
107 for (auto s : b.statements)
\r
118 std::ostream & operator<< (std::ostream & out, const Expr & e)
\r
122 case ExprType::Func:
\r
123 out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName << "(" << vectorStr(e._func.arguments, ", ") << ")"; break;
\r
124 case ExprType::Method:
\r
126 TypeInfo ti = typeExpr(globalPrg, namespaces, *e._method.expr);
\r
128 vectorStr(ti.type.namespacePrefixes, "_", true) <<
\r
129 ti.type.name << "_" << e._method.methodName << "(" << *e._method.expr << vectorStr(e._method.arguments, ", ") << ")"; break;
\r
131 case ExprType::Lit:
\r
132 /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
\r
133 else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
\r
134 else if (e._lit.type == LitType::String) out << e._lit._string;
\r
135 else if (e._lit.type == LitType::Bool) out << e._lit._bool;
\r
137 case ExprType::Paren:
\r
138 out << "(" << e._paren.expr << ")"; break;
\r
139 case ExprType::Dot:
\r
140 out << *e._dot.expr << "." << e._dot.identifier; break;
\r
141 case ExprType::PrefixOp:
\r
142 out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;
\r
143 case ExprType::PostfixOp:
\r
144 out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;
\r
145 case ExprType::BinaryOp:
\r
146 out << *e._binaryOp.lexpr <<
\r
147 " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<
\r
148 *e._binaryOp.rexpr; break;
\r
149 case ExprType::TernaryOp:
\r
150 out << *e._ternaryOp.lexpr <<
\r
151 " ? " << *e._ternaryOp.rexprTrue <<
\r
152 " : " << *e._ternaryOp.rexprFalse; break;
\r
153 case ExprType::Bracket:
\r
154 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
155 case ExprType::Identifier:
\r
156 out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;
\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 << ")\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.init << "; " <<
\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.lexpr << " = " << s._assign.rexpr << ";"; 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 << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";
\r
207 out << "\n" << f.body;
\r
210 void tocStruct (std::ostream & out, const Struct & s, bool stub)
\r
212 out << "struct " << namespacePrefix() << s.name;
\r
216 for (auto m : s.methods)
\r
220 f.parameters.insert(f.parameters.begin(),
\r
226 {TypeModifierType::Pointer, false, -1}
\r
230 out << f.returnType << " " <<
\r
231 namespacePrefix() << s.name << "_" << f.name <<
\r
232 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
239 for (auto m : s.members)
\r
248 for (auto m : s.methods)
\r
251 f.parameters.insert(f.parameters.begin(),
\r
257 {TypeModifierType::Pointer, false, -1}
\r
261 out << f.returnType << " " <<
\r
262 namespacePrefix() << s.name << "_" << f.name <<
\r
263 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
266 void tocProgram (std::ostream & out, const Program & p)
\r
269 for (auto n : p.namespaces)
\r
271 tocNamespace(out, n, true);
\r
273 for (auto s : p.structs)
\r
275 tocStruct(out, s, true);
\r
277 for (auto f : p.functions)
\r
279 tocFunction(out, f, true);
\r
282 for (auto v : p.variables)
\r
286 for (auto n : p.namespaces)
\r
288 tocNamespace(out, n, false);
\r
290 for (auto s : p.structs)
\r
292 tocStruct(out, s, false);
\r
294 for (auto f : p.functions)
\r
296 tocFunction(out, f, false);
\r
301 void tocNamespace (std::ostream & out, const Namespace & n, bool stub)
\r
303 namespaces.push_back(n.name);
\r
306 for (auto v : n.variables)
\r
311 for (auto n : n.namespaces)
\r
313 tocNamespace(out, n, stub);
\r
315 for (auto s : n.structs)
\r
317 tocStruct(out, s, stub);
\r
319 for (auto f : n.functions)
\r
321 tocFunction(out, f, stub);
\r
323 namespaces.pop_back();
\r