8 #include "typeInfo.h"
\r
10 template<typename T>
\r
11 std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)
\r
13 std::stringstream sstr;
\r
15 bool putSeparator = false;
\r
18 if (putSeparator) sstr << separator;
\r
19 else putSeparator = true;
\r
22 if (end && !v.empty())
\r
28 std::ostream & operator<< (std::ostream & out, const Type & t);
\r
29 std::ostream & operator<< (std::ostream & out, const Variable & v);
\r
30 std::ostream & operator<< (std::ostream & out, const Body & b);
\r
31 std::ostream & operator<< (std::ostream & out, const Expr & e);
\r
32 std::ostream & operator<< (std::ostream & out, const Stmt & s);
\r
34 void tocFunction (std::ostream & out, const Function & f, bool stub);
\r
35 void tocStruct (std::ostream & out, const Struct & s, bool stub);
\r
36 void tocProgram (std::ostream & out, const Program & p);
\r
37 void tocNamespace (std::ostream & out, const Namespace & n, bool stub);
\r
39 static const int TAB_WIDTH = 2;
\r
40 static int indentation = 0;
\r
41 static void indent(std::ostream & out, int change = 0)
\r
43 indentation += change;
\r
44 out << std::string(indentation, ' ');
\r
47 static std::vector<std::string> namespaces;
\r
48 static std::string namespacePrefix() {
\r
49 std::stringstream sstr;
\r
50 for (auto n : namespaces)
\r
57 static std::map<std::string, Type> currentInstantiation;
\r
59 static Program globalPrg;
\r
60 static std::shared_ptr<Context> globalCtx;
\r
62 std::ostream & operator<< (std::ostream & out, const Type & t)
\r
64 for (auto kv : currentInstantiation)
\r
66 if (t.name == kv.first)
\r
72 TypeInfo ti = typeType(globalPrg, t);
\r
75 out << vectorStr(t.namespacePrefixes, "_", true) << t.name;
\r
76 if (!t.genericInstantiation.empty())
\r
77 out << genericAppendix(t.genericInstantiation);
\r
81 std::ostream & operator<< (std::ostream & out, const Variable & v)
\r
83 out << v.type << " ";
\r
85 std::stringstream sstr;
\r
86 std::string s = v.name;
\r
88 for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
\r
90 if (m->type == TypeModifierType::Pointer)
\r
92 sstr.str(std::string());
\r
93 sstr << "*(" << s << ")";
\r
98 sstr.str(std::string());
\r
99 sstr << "(" << s << ")[";
\r
100 if (m->_staticArray)
\r
101 sstr << m->_arraySize;
\r
110 std::ostream & operator<< (std::ostream & out, const Body & b)
\r
112 b.ctx->parent = globalCtx;
\r
119 for (auto v : b.ctx->variables)
\r
127 for (auto s : b.statements)
\r
136 globalCtx = b.ctx->parent;
\r
140 std::ostream & operator<< (std::ostream & out, const Expr & e)
\r
144 case ExprType::Func:
\r
146 if (e._func.namespacePrefixes.empty())
\r
148 TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, e);
\r
151 out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName;
\r
152 if (!e._func.genericInstantiation.empty())
\r
153 out << genericAppendix(e._func.genericInstantiation);
\r
154 out <<"(" << vectorStr(e._func.arguments, ", ") << ")"; break;
\r
156 case ExprType::Method:
\r
158 TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, *e._method.expr);
\r
160 vectorStr(ti.type.namespacePrefixes, "_", true) <<
\r
161 ti.type.name << genericAppendix(ti.type.genericInstantiation) << "_" << e._method.methodName;
\r
162 if (!e._method.genericInstantiation.empty())
\r
163 out << genericAppendix(e._method.genericInstantiation);
\r
164 out << "(&" << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") <<
\r
165 vectorStr(e._method.arguments, ", ") << ")"; break;
\r
167 case ExprType::Lit:
\r
168 /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
\r
169 else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
\r
170 else if (e._lit.type == LitType::String) out << e._lit._string;
\r
171 else if (e._lit.type == LitType::Bool) out << e._lit._bool;
\r
173 case ExprType::Paren:
\r
174 out << "(" << e._paren.expr << ")"; break;
\r
175 case ExprType::Dot:
\r
176 out << *e._dot.expr << (e._dot.isPointer ? "->" : ".") << e._dot.identifier; break;
\r
177 case ExprType::PrefixOp:
\r
178 out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;
\r
179 case ExprType::PostfixOp:
\r
180 out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;
\r
181 case ExprType::BinaryOp:
\r
182 out << *e._binaryOp.lexpr <<
\r
183 " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<
\r
184 *e._binaryOp.rexpr; break;
\r
185 case ExprType::TernaryOp:
\r
186 out << *e._ternaryOp.lexpr <<
\r
187 " ? " << *e._ternaryOp.rexprTrue <<
\r
188 " : " << *e._ternaryOp.rexprFalse; break;
\r
189 case ExprType::Bracket:
\r
190 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
191 case ExprType::Identifier:
\r
192 out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;
\r
197 std::ostream & operator<< (std::ostream & out, const Stmt & s)
\r
202 out << "if (" << s._if.condition << ")\n" << s._if.body; break;
\r
203 case StmtType::Switch:
\r
204 out << "switch (" << s._switch.ident << ")\n{\n";
\r
205 for (auto c : s._switch.cases)
\r
208 out << "case " << *c.expr << ": " << c.body << "break;";
\r
213 case StmtType::For:
\r
215 s._for.init << "; " <<
\r
216 *s._for.condition << "; " <<
\r
218 ")\n" << s._for.body; break;
\r
219 case StmtType::While:
\r
220 out << "while (" << s._while.condition << ")\n" << s._while.body; break;
\r
221 case StmtType::Assign:
\r
222 out << s._assign.lexpr << " = " << s._assign.rexpr << ";"; break;
\r
223 case StmtType::Return:
\r
224 out << "return " << s._return.expr << ";"; break;
\r
225 case StmtType::Expr:
\r
226 out << s._expr << ";"; break;
\r
233 void tocFunction (std::ostream & out, const Function & f, bool stub)
\r
235 if (!stub && !f.defined) return;
\r
237 if (f.genericTypeNames.empty())
\r
239 out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";
\r
247 out << "\n" << f.body;
\r
252 for (auto instantiation : f.genericInstantiations)
\r
254 for (int i = 0; i < f.genericTypeNames.size(); i++)
\r
256 currentInstantiation[f.genericTypeNames[i]] = instantiation[i];
\r
259 out << f.returnType << " " << namespacePrefix() << f.name << genericAppendix(instantiation) << " (" << vectorStr(f.parameters, ", ") << ")";
\r
267 out << "\n" << f.body;
\r
270 currentInstantiation.clear();
\r
274 void tocStruct (std::ostream & out, const Struct & s, bool stub)
\r
276 if (s.genericTypeNames.empty())
\r
278 out << "struct " << namespacePrefix() << s.name;
\r
282 for (auto m : s.methods)
\r
286 f.parameters.insert(f.parameters.begin(),
\r
292 {TypeModifierType::Pointer, false, -1}
\r
296 out << f.returnType << " " <<
\r
297 namespacePrefix() << s.name << "_" << f.name <<
\r
298 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
305 for (auto m : s.members)
\r
314 for (auto m : s.methods)
\r
317 f.parameters.insert(f.parameters.begin(),
\r
323 {TypeModifierType::Pointer, false, -1}
\r
327 out << f.returnType << " " <<
\r
328 namespacePrefix() << s.name << "_" << f.name <<
\r
329 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
334 for (auto instantiation : s.genericInstantiations)
\r
336 for (int i = 0; i < s.genericTypeNames.size(); i++)
\r
338 currentInstantiation[s.genericTypeNames[i]] = instantiation[i];
\r
341 out << "struct " << namespacePrefix() << s.name << genericAppendix(instantiation);
\r
345 for (auto m : s.methods)
\r
349 f.parameters.insert(f.parameters.begin(),
\r
353 s.name + genericAppendix(instantiation),
\r
355 {TypeModifierType::Pointer, false, -1}
\r
359 out << f.returnType << " " <<
\r
360 namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<
\r
361 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
368 for (auto m : s.members)
\r
377 for (auto m : s.methods)
\r
380 f.parameters.insert(f.parameters.begin(),
\r
384 s.name + genericAppendix(instantiation),
\r
386 {TypeModifierType::Pointer, false, -1}
\r
390 out << f.returnType << " " <<
\r
391 namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<
\r
392 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
395 currentInstantiation.clear();
\r
399 void tocProgram (std::ostream & out, const Program & _p)
\r
401 Program p = instantiateGenerics(_p);
\r
406 for (auto n : p.namespaces)
\r
408 tocNamespace(out, n, true);
\r
411 for (auto s : p.ctx->structs)
\r
413 tocStruct(out, s, true);
\r
416 for (auto f : p.ctx->functions)
\r
418 tocFunction(out, f, true);
\r
422 for (auto v : p.ctx->variables)
\r
427 for (auto n : p.namespaces)
\r
429 tocNamespace(out, n, false);
\r
432 for (auto s : p.ctx->structs)
\r
434 tocStruct(out, s, false);
\r
437 for (auto f : p.ctx->functions)
\r
439 tocFunction(out, f, false);
\r
445 void tocNamespace (std::ostream & out, const Namespace & n, bool stub)
\r
447 n.ctx->parent = globalCtx;
\r
450 namespaces.push_back(n.name);
\r
453 for (auto v : n.ctx->variables)
\r
459 for (auto n : n.namespaces)
\r
461 tocNamespace(out, n, stub);
\r
464 for (auto s : n.ctx->structs)
\r
466 tocStruct(out, s, stub);
\r
469 for (auto f : n.ctx->functions)
\r
471 tocFunction(out, f, stub);
\r
474 namespaces.pop_back();
\r
476 globalCtx = n.ctx->parent;
\r