X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/dbc4a22d3c8c4189459f0361cb9da06415ec2dc9..c4231c6faf4e1b4650b075c641b0bb8c053739e4:/src/toc.h diff --git a/src/toc.h b/src/toc.h index 95c24d8..e95ab3e 100644 --- a/src/toc.h +++ b/src/toc.h @@ -4,6 +4,7 @@ #include #include "repr.h" +#include "generic.h" #include "typeInfo.h" template @@ -53,11 +54,46 @@ static std::string namespacePrefix() { return sstr.str(); } +static std::map currentInstantiation; + static Program globalPrg; +static std::shared_ptr globalCtx; + + + +// std::string getPrefix(std::shared_ptr ctx) +// { +// std::string result; +// for (auto it = ctx; it != nullptr; it = it->parent) +// { +// if (it->name.has_value()) +// { +// result = it->name.value() + "_" + result; +// } +// } +// return result; +// } std::ostream & operator<< (std::ostream & out, const Type & t) { - out << vectorStr(t.namespacePrefixes, "_", true) << t.name; + for (auto kv : currentInstantiation) + { + if (t.name == kv.first) + { + out << kv.second; + return out; + } + } + TypeInfo ti = typeType(globalCtx, t); + if (ti.isStruct) + out << "struct "; + auto s = findStruct(t.name, t.namespacePrefixes, globalCtx); + if (s.has_value()) + out << vectorStr(std::get<1>(*s), "_", true) << t.name; + else + out << vectorStr(t.namespacePrefixes, "_", true) << t.name; + if (!t.genericInstantiation.empty()) + out << genericAppendix(t.genericInstantiation); return out; } @@ -67,6 +103,10 @@ std::ostream & operator<< (std::ostream & out, const Variable & v) std::stringstream sstr; std::string s = v.name; + + auto var = findVariable(v.name, namespaces, globalCtx); + if (var.has_value()) + s = vectorStr(std::get<1>(*var), "_", true) + s; for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++) { @@ -92,11 +132,14 @@ std::ostream & operator<< (std::ostream & out, const Variable & v) } std::ostream & operator<< (std::ostream & out, const Body & b) { + b.ctx->parent = globalCtx; + globalCtx = b.ctx; + indent(out); out << "{\n"; indentation += 2; - for (auto v : b.variables) + for (auto v : b.ctx->variables) { indent(out); out << v << ";\n"; @@ -113,6 +156,8 @@ std::ostream & operator<< (std::ostream & out, const Body & b) indent(out, -2); out << "}\n"; + globalCtx = b.ctx->parent; + return out; } std::ostream & operator<< (std::ostream & out, const Expr & e) @@ -120,13 +165,30 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) switch (e.type) { case ExprType::Func: - out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName << "(" << vectorStr(e._func.arguments, ", ") << ")"; break; + { + auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx); + + if (std::get<0>(*f).defined) + out << vectorStr(std::get<1>(*f), "_", true); + + out << e._func.functionName; + if (!e._func.genericInstantiation.empty()) + out << genericAppendix(e._func.genericInstantiation); + out <<"(" << vectorStr(e._func.arguments, ", ") << ")"; break; + } case ExprType::Method: { - TypeInfo ti = typeExpr(globalPrg, namespaces, *e._method.expr); + TypeInfo ti = typeExpr(globalCtx, *e._method.expr); out << vectorStr(ti.type.namespacePrefixes, "_", true) << - ti.type.name << "_" << e._method.methodName << "(" << *e._method.expr << vectorStr(e._method.arguments, ", ") << ")"; break; + ti.type.name << genericAppendix(ti.type.genericInstantiation) << "_" << e._method.methodName; + if (!e._method.genericInstantiation.empty()) + out << genericAppendix(e._method.genericInstantiation); + out << "("; + if (e._method.expr->type == ExprType::Identifier) + out << "&"; + out << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") << + vectorStr(e._method.arguments, ", ") << ")"; break; } case ExprType::Lit: /**/ if (e._lit.type == LitType::Int) out << e._lit._int; @@ -135,9 +197,9 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) else if (e._lit.type == LitType::Bool) out << e._lit._bool; break; case ExprType::Paren: - out << "(" << e._paren.expr << ")"; break; + out << "(" << *e._paren.expr << ")"; break; case ExprType::Dot: - out << *e._dot.expr << "." << e._dot.identifier; break; + out << *e._dot.expr << (e._dot.isPointer ? "->" : ".") << e._dot.identifier; break; case ExprType::PrefixOp: out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break; case ExprType::PostfixOp: @@ -153,7 +215,13 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) case ExprType::Bracket: out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break; case ExprType::Identifier: - out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break; + auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx); + if (v.has_value()) + out << vectorStr(std::get<1>(*v), "_", true); + else + out << vectorStr(e._identifier.namespacePrefixes, "_", true); + + out << e._identifier.identifier; break; } return out; @@ -196,129 +264,244 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s) void tocFunction (std::ostream & out, const Function & f, bool stub) { - out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")"; + if (!stub && !f.defined) return; - if (stub) + if (f.genericTypeNames.empty()) { - out << ";\n"; + out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")"; + + if (stub) + { + out << ";\n"; + } + else + { + out << "\n" << f.body; + } } else { - out << "\n" << f.body; + for (auto instantiation : f.genericInstantiations) + { + for (int i = 0; i < f.genericTypeNames.size(); i++) + { + currentInstantiation[f.genericTypeNames[i]] = instantiation[i]; + } + + out << f.returnType << " " << namespacePrefix() << f.name << genericAppendix(instantiation) << " (" << vectorStr(f.parameters, ", ") << ")"; + + if (stub) + { + out << ";\n"; + } + else + { + out << "\n" << f.body; + } + + currentInstantiation.clear(); + } } } void tocStruct (std::ostream & out, const Struct & s, bool stub) { - out << "struct " << namespacePrefix() << s.name; - if (stub) + if (s.genericTypeNames.empty()) { - out << ";\n"; + out << "struct " << namespacePrefix() << s.name; + if (stub) + { + out << ";\n"; + for (auto m : s.methods) + { + Function f = m; + + f.parameters.insert(f.parameters.begin(), + {"this", + { + namespaces, + s.name, + { + {TypeModifierType::Pointer, false, -1} + } + } + }); + out << f.returnType << " " << + namespacePrefix() << s.name << "_" << f.name << + " (" << vectorStr(f.parameters, ", ") << ");\n"; + } + return; + } + out << "\n{\n"; + indentation += 2; + + for (auto m : s.members) + { + indent(out); + out << m << ";\n"; + } + + indent(out, -2); + out << "};\n"; + for (auto m : s.methods) { Function f = m; - f.parameters.insert(f.parameters.begin(), - {"this", - { - namespaces, - s.name, + {"this", { - {TypeModifierType::Pointer, false, -1} + namespaces, + s.name, + { + {TypeModifierType::Pointer, false, -1} + } } - } - }); + }); out << f.returnType << " " << - namespacePrefix() << s.name << "_" << f.name << - " (" << vectorStr(f.parameters, ", ") << ");\n"; + namespacePrefix() << s.name << "_" << f.name << + " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; } - return; } - out << "\n{\n"; - indentation += 2; - - for (auto m : s.members) + else { - indent(out); - out << m << ";\n"; - } + for (auto instantiation : s.genericInstantiations) + { + for (int i = 0; i < s.genericTypeNames.size(); i++) + { + currentInstantiation[s.genericTypeNames[i]] = instantiation[i]; + } - indent(out, -2); - out << "};\n"; - - for (auto m : s.methods) - { - Function f = m; - f.parameters.insert(f.parameters.begin(), - {"this", + out << "struct " << namespacePrefix() << s.name << genericAppendix(instantiation); + if (stub) + { + out << ";\n"; + for (auto m : s.methods) { - namespaces, - s.name, - { - {TypeModifierType::Pointer, false, -1} - } + Function f = m; + + f.parameters.insert(f.parameters.begin(), + {"this", + { + namespaces, + s.name + genericAppendix(instantiation), + { + {TypeModifierType::Pointer, false, -1} + } + } + }); + out << f.returnType << " " << + namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name << + " (" << vectorStr(f.parameters, ", ") << ");\n"; } - }); - out << f.returnType << " " << - namespacePrefix() << s.name << "_" << f.name << - " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; + return; + } + out << "\n{\n"; + indentation += 2; + + for (auto m : s.members) + { + indent(out); + out << m << ";\n"; + } + + indent(out, -2); + out << "};\n"; + + for (auto m : s.methods) + { + Function f = m; + f.parameters.insert(f.parameters.begin(), + {"this", + { + namespaces, + s.name + genericAppendix(instantiation), + { + {TypeModifierType::Pointer, false, -1} + } + } + }); + out << f.returnType << " " << + namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name << + " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; + } + + currentInstantiation.clear(); + } } } void tocProgram (std::ostream & out, const Program & p) { + globalCtx = p.ctx; + globalPrg = p; - for (auto n : p.namespaces) + for (auto n : p.ctx->namespaces) { tocNamespace(out, n, true); } - for (auto s : p.structs) + out << "\n\n"; + for (auto s : p.ctx->structs) { tocStruct(out, s, true); } - for (auto f : p.functions) + out << "\n\n"; + for (auto f : p.ctx->functions) { tocFunction(out, f, true); } + out << "\n\n"; - for (auto v : p.variables) + for (auto v : p.ctx->variables) { out << v << ";\n"; } - for (auto n : p.namespaces) + out << "\n\n"; + for (auto n : p.ctx->namespaces) { tocNamespace(out, n, false); } - for (auto s : p.structs) + out << "\n\n"; + for (auto s : p.ctx->structs) { tocStruct(out, s, false); } - for (auto f : p.functions) + out << "\n\n"; + for (auto f : p.ctx->functions) { tocFunction(out, f, false); } + out << "\n\n"; } void tocNamespace (std::ostream & out, const Namespace & n, bool stub) { + n.ctx->parent = globalCtx; + globalCtx = n.ctx; + namespaces.push_back(n.name); if (!stub) { - for (auto v : n.variables) + for (auto v : n.ctx->variables) { out << v << ";\n"; } + out << "\n\n"; } - for (auto n : n.namespaces) + for (auto n : n.ctx->namespaces) { tocNamespace(out, n, stub); + out << "\n\n"; } - for (auto s : n.structs) + for (auto s : n.ctx->structs) { tocStruct(out, s, stub); + out << "\n\n"; } - for (auto f : n.functions) + for (auto f : n.ctx->functions) { tocFunction(out, f, stub); + out << "\n\n"; } namespaces.pop_back(); + + globalCtx = n.ctx->parent; } \ No newline at end of file