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
57 static std::shared_ptr<Context> globalCtx;
\r
59 std::ostream & operator<< (std::ostream & out, const Type & t)
\r
61 TypeInfo ti = typeType(globalPrg, t);
\r
64 out << vectorStr(t.namespacePrefixes, "_", true) << t.name;
\r
68 std::ostream & operator<< (std::ostream & out, const Variable & v)
\r
70 out << v.type << " ";
\r
72 std::stringstream sstr;
\r
73 std::string s = v.name;
\r
75 for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
\r
77 if (m->type == TypeModifierType::Pointer)
\r
79 sstr.str(std::string());
\r
80 sstr << "*(" << s << ")";
\r
85 sstr.str(std::string());
\r
86 sstr << "(" << s << ")[";
\r
87 if (m->_staticArray)
\r
88 sstr << m->_arraySize;
\r
97 std::ostream & operator<< (std::ostream & out, const Body & b)
\r
99 b.ctx->parent = globalCtx;
\r
106 for (auto v : b.ctx->variables)
\r
114 for (auto s : b.statements)
\r
123 globalCtx = b.ctx->parent;
\r
127 std::ostream & operator<< (std::ostream & out, const Expr & e)
\r
131 case ExprType::Func:
\r
133 if (e._func.namespacePrefixes.empty())
\r
135 TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, e);
\r
138 out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName << "(" << vectorStr(e._func.arguments, ", ") << ")"; break;
\r
140 case ExprType::Method:
\r
142 TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, *e._method.expr);
\r
144 vectorStr(ti.type.namespacePrefixes, "_", true) <<
\r
145 ti.type.name << "_" << e._method.methodName <<
\r
146 "(&" << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") <<
\r
147 vectorStr(e._method.arguments, ", ") << ")"; break;
\r
149 case ExprType::Lit:
\r
150 /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
\r
151 else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
\r
152 else if (e._lit.type == LitType::String) out << e._lit._string;
\r
153 else if (e._lit.type == LitType::Bool) out << e._lit._bool;
\r
155 case ExprType::Paren:
\r
156 out << "(" << e._paren.expr << ")"; break;
\r
157 case ExprType::Dot:
\r
158 out << *e._dot.expr << "." << e._dot.identifier; break;
\r
159 case ExprType::PrefixOp:
\r
160 out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;
\r
161 case ExprType::PostfixOp:
\r
162 out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;
\r
163 case ExprType::BinaryOp:
\r
164 out << *e._binaryOp.lexpr <<
\r
165 " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<
\r
166 *e._binaryOp.rexpr; break;
\r
167 case ExprType::TernaryOp:
\r
168 out << *e._ternaryOp.lexpr <<
\r
169 " ? " << *e._ternaryOp.rexprTrue <<
\r
170 " : " << *e._ternaryOp.rexprFalse; break;
\r
171 case ExprType::Bracket:
\r
172 out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
\r
173 case ExprType::Identifier:
\r
174 out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;
\r
179 std::ostream & operator<< (std::ostream & out, const Stmt & s)
\r
184 out << "if (" << s._if.condition << ")\n" << s._if.body; break;
\r
185 case StmtType::Switch:
\r
186 out << "switch (" << s._switch.ident << ")\n{\n";
\r
187 for (auto c : s._switch.cases)
\r
190 out << "case " << *c.expr << ": " << c.body << "break;";
\r
195 case StmtType::For:
\r
197 s._for.init << "; " <<
\r
198 *s._for.condition << "; " <<
\r
200 ")\n" << s._for.body; break;
\r
201 case StmtType::While:
\r
202 out << "while (" << s._while.condition << ")\n" << s._while.body; break;
\r
203 case StmtType::Assign:
\r
204 out << s._assign.lexpr << " = " << s._assign.rexpr << ";"; break;
\r
205 case StmtType::Return:
\r
206 out << "return " << s._return.expr << ";"; break;
\r
207 case StmtType::Expr:
\r
208 out << s._expr << ";"; break;
\r
215 void tocFunction (std::ostream & out, const Function & f, bool stub)
\r
217 if (!stub && !f.defined) return;
\r
219 out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";
\r
227 out << "\n" << f.body;
\r
230 void tocStruct (std::ostream & out, const Struct & s, bool stub)
\r
232 out << "struct " << namespacePrefix() << s.name;
\r
236 for (auto m : s.methods)
\r
240 f.parameters.insert(f.parameters.begin(),
\r
246 {TypeModifierType::Pointer, false, -1}
\r
250 out << f.returnType << " " <<
\r
251 namespacePrefix() << s.name << "_" << f.name <<
\r
252 " (" << vectorStr(f.parameters, ", ") << ");\n";
\r
259 for (auto m : s.members)
\r
268 for (auto m : s.methods)
\r
271 f.parameters.insert(f.parameters.begin(),
\r
277 {TypeModifierType::Pointer, false, -1}
\r
281 out << f.returnType << " " <<
\r
282 namespacePrefix() << s.name << "_" << f.name <<
\r
283 " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
\r
286 void tocProgram (std::ostream & out, const Program & p)
\r
291 for (auto n : p.namespaces)
\r
293 tocNamespace(out, n, true);
\r
296 for (auto s : p.structs)
\r
298 tocStruct(out, s, true);
\r
301 for (auto f : p.functions)
\r
303 tocFunction(out, f, true);
\r
307 for (auto v : p.ctx->variables)
\r
312 for (auto n : p.namespaces)
\r
314 tocNamespace(out, n, false);
\r
317 for (auto s : p.structs)
\r
319 tocStruct(out, s, false);
\r
322 for (auto f : p.functions)
\r
324 tocFunction(out, f, false);
\r
330 void tocNamespace (std::ostream & out, const Namespace & n, bool stub)
\r
332 n.ctx->parent = globalCtx;
\r
335 namespaces.push_back(n.name);
\r
338 for (auto v : n.ctx->variables)
\r
344 for (auto n : n.namespaces)
\r
346 tocNamespace(out, n, stub);
\r
349 for (auto s : n.structs)
\r
351 tocStruct(out, s, stub);
\r
354 for (auto f : n.functions)
\r
356 tocFunction(out, f, stub);
\r
359 namespaces.pop_back();
\r
361 globalCtx = n.ctx->parent;
\r