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
64 // std::string getPrefix(std::shared_ptr<Context> ctx)
\r
66 // std::string result;
\r
67 // for (auto it = ctx; it != nullptr; it = it->parent)
\r
69 // if (it->name.has_value())
\r
71 // result = it->name.value() + "_" + result;
\r
77 std::ostream & operator<< (std::ostream & out, const Type & t)
\r
79 for (auto kv : currentInstantiation)
\r
81 if (t.name == kv.first)
\r
87 TypeInfo ti = typeType(globalCtx, t);
\r
90 auto s = findStruct(t.name, t.namespacePrefixes, globalCtx);
\r
92 out << vectorStr(std::get<1>(*s), "_", true) << t.name;
\r
94 out << vectorStr(t.namespacePrefixes, "_", true) << t.name;
\r
95 if (!t.genericInstantiation.empty())
\r
96 out << genericAppendix(t.genericInstantiation);
\r
100 std::ostream & operator<< (std::ostream & out, const Variable & v)
\r
102 out << v.type << " ";
\r
104 std::stringstream sstr;
\r
105 std::string s = v.name;
\r
107 auto var = findVariable(v.name, namespaces, globalCtx);
\r
108 if (var.has_value())
\r
109 s = vectorStr(std::get<1>(*var), "_", true) + s;
\r
111 for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
\r
113 if (m->type == TypeModifierType::Pointer)
\r
115 sstr.str(std::string());
\r
116 sstr << "*(" << s << ")";
\r
121 sstr.str(std::string());
\r
122 sstr << "(" << s << ")[";
\r
123 if (m->_staticArray)
\r
124 sstr << m->_arraySize;
\r
133 std::ostream & operator<< (std::ostream & out, const Body & b)
\r
135 b.ctx->parent = globalCtx;
\r
142 for (auto v : b.ctx->variables)
\r
150 for (auto s : b.statements)
\r
159 globalCtx = b.ctx->parent;
\r
163 std::ostream & operator<< (std::ostream & out, const Expr & e)
\r
167 case ExprType::Func:
\r
169 auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);
\r
171 if (std::get<0>(*f).defined)
\r
172 out << vectorStr(std::get<1>(*f), "_", true);
\r
174 out << e._func.functionName;
\r
175 if (!e._func.genericInstantiation.empty())
\r
176 out << genericAppendix(e._func.genericInstantiation);
\r
177 out <<"(" << vectorStr(e._func.arguments, ", ") << ")"; break;
\r
179 case ExprType::Method:
\r
181 TypeInfo ti = typeExpr(globalCtx, *e._method.expr);
\r
183 vectorStr(ti.type.namespacePrefixes, "_", true) <<
\r
184 ti.type.name << genericAppendix(ti.type.genericInstantiation) << "_" << e._method.methodName;
\r
185 if (!e._method.genericInstantiation.empty())
\r
186 out << genericAppendix(e._method.genericInstantiation);
\r
188 if (e._method.expr->type == ExprType::Identifier)
\r
190 out << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") <<
\r
191 vectorStr(e._method.arguments, ", ") << ")"; break;
\r
193 case ExprType::Lit:
\r
194 /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
\r
195 else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
\r
196 else if (e._lit.type == LitType::String) out << e._lit._string;
\r
197 else if (e._lit.type == LitType::Bool) out << e._lit._bool;
\r
199 case ExprType::Paren:
\r
200 out << "(" << *e._paren.expr << ")"; break;
\r
201 case ExprType::Dot:
\r
202 out << *e._dot.expr << (e._dot.isPointer ? "->" : ".") << e._dot.identifier; break;
\r
203 case ExprType::PrefixOp:
\r
204 out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;
\r
205 case ExprType::PostfixOp:
\r
206 out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;
\r
207 case ExprType::BinaryOp:
\r
208 out << *e._binaryOp.lexpr <<
\r
209 " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<
\r
210 *e._binaryOp.rexpr; break;
\r
211 case ExprType::TernaryOp:
\r
212 out << *e._ternaryOp.lexpr <<
\r
213 " ? " << *e._ternaryOp.rexprTrue <<
\r
214 " : " << *e._ternaryOp.rexprFalse; break;
\r
215 case ExprType::Bracket:
\r
216 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
217 case ExprType::Identifier:
\r
218 auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);
\r
220 out << vectorStr(std::get<1>(*v), "_", true);
\r
222 out << vectorStr(e._identifier.namespacePrefixes, "_", true);
\r
224 out << e._identifier.identifier; break;
\r
229 std::ostream & operator<< (std::ostream & out, const Stmt & s)
\r
234 out << "if (" << s._if.condition << ")\n" << s._if.body; break;
\r
235 case StmtType::Switch:
\r
236 out << "switch (" << s._switch.ident << ")\n{\n";
\r
237 for (auto c : s._switch.cases)
\r
240 out << "case " << *c.expr << ": " << c.body << "break;";
\r
245 case StmtType::For:
\r
247 s._for.init << "; " <<
\r
248 *s._for.condition << "; " <<
\r
250 ")\n" << s._for.body; break;
\r
251 case StmtType::While:
\r
252 out << "while (" << s._while.condition << ")\n" << s._while.body; break;
\r
253 case StmtType::Assign:
\r
254 out << s._assign.lexpr << " = " << s._assign.rexpr << ";"; break;
\r
255 case StmtType::Return:
\r
256 out << "return " << s._return.expr << ";"; break;
\r
257 case StmtType::Expr:
\r
258 out << s._expr << ";"; break;
\r
265 void tocFunction (std::ostream & out, const Function & f, bool stub)
\r
267 if (!stub && !f.defined) return;
\r
269 if (f.genericTypeNames.empty())
\r
271 out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";
\r
279 out << "\n" << f.body;
\r
284 for (auto instantiation : f.genericInstantiations)
\r
286 for (int i = 0; i < f.genericTypeNames.size(); i++)
\r
288 currentInstantiation[f.genericTypeNames[i]] = instantiation[i];
\r
291 out << f.returnType << " " << namespacePrefix() << f.name << genericAppendix(instantiation) << " (" << vectorStr(f.parameters, ", ") << ")";
\r
299 out << "\n" << f.body;
\r
302 currentInstantiation.clear();
\r
306 void tocStruct (std::ostream & out, const Struct & s, bool stub)
\r
308 if (s.genericTypeNames.empty())
\r
310 out << "struct " << namespacePrefix() << s.name;
\r
314 for (auto m : s.methods)
\r
318 f.parameters.insert(f.parameters.begin(),
\r
324 {TypeModifierType::Pointer, false, -1}
\r
328 out << f.returnType << " " <<
\r
329 namespacePrefix() << s.name << "_" << f.name <<
\r
330 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
337 for (auto m : s.members)
\r
346 for (auto m : s.methods)
\r
349 f.parameters.insert(f.parameters.begin(),
\r
355 {TypeModifierType::Pointer, false, -1}
\r
359 out << f.returnType << " " <<
\r
360 namespacePrefix() << s.name << "_" << f.name <<
\r
361 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
366 for (auto instantiation : s.genericInstantiations)
\r
368 for (int i = 0; i < s.genericTypeNames.size(); i++)
\r
370 currentInstantiation[s.genericTypeNames[i]] = instantiation[i];
\r
373 out << "struct " << namespacePrefix() << s.name << genericAppendix(instantiation);
\r
377 for (auto m : s.methods)
\r
381 f.parameters.insert(f.parameters.begin(),
\r
385 s.name + genericAppendix(instantiation),
\r
387 {TypeModifierType::Pointer, false, -1}
\r
391 out << f.returnType << " " <<
\r
392 namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<
\r
393 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
400 for (auto m : s.members)
\r
409 for (auto m : s.methods)
\r
412 f.parameters.insert(f.parameters.begin(),
\r
416 s.name + genericAppendix(instantiation),
\r
418 {TypeModifierType::Pointer, false, -1}
\r
422 out << f.returnType << " " <<
\r
423 namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name <<
\r
424 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
427 currentInstantiation.clear();
\r
431 void tocProgram (std::ostream & out, const Program & p)
\r
436 for (auto n : p.ctx->namespaces)
\r
438 tocNamespace(out, n, true);
\r
441 for (auto s : p.ctx->structs)
\r
443 tocStruct(out, s, true);
\r
446 for (auto f : p.ctx->functions)
\r
448 tocFunction(out, f, true);
\r
452 for (auto v : p.ctx->variables)
\r
457 for (auto n : p.ctx->namespaces)
\r
459 tocNamespace(out, n, false);
\r
462 for (auto s : p.ctx->structs)
\r
464 tocStruct(out, s, false);
\r
467 for (auto f : p.ctx->functions)
\r
469 tocFunction(out, f, false);
\r
475 void tocNamespace (std::ostream & out, const Namespace & n, bool stub)
\r
477 n.ctx->parent = globalCtx;
\r
480 namespaces.push_back(n.name);
\r
483 for (auto v : n.ctx->variables)
\r
489 for (auto n : n.ctx->namespaces)
\r
491 tocNamespace(out, n, stub);
\r
494 for (auto s : n.ctx->structs)
\r
496 tocStruct(out, s, stub);
\r
499 for (auto f : n.ctx->functions)
\r
501 tocFunction(out, f, stub);
\r
504 namespaces.pop_back();
\r
506 globalCtx = n.ctx->parent;
\r