X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/e8e6aebbb892a72b287f1378901f855246da15a2..HEAD:/src/toc.h diff --git a/src/toc.h b/src/toc.h index e95ab3e..c31e12b 100644 --- a/src/toc.h +++ b/src/toc.h @@ -7,6 +7,7 @@ #include "generic.h" #include "typeInfo.h" +// print a generic vector with specified separator, optionally printing the separator at the end aswell template std::string vectorStr (const std::vector & v, const std::string & separator, bool end = false) { @@ -54,28 +55,18 @@ static std::string namespacePrefix() { return sstr.str(); } +// mapping from generic typenames (which are just names) +// to actual instantiated types static std::map currentInstantiation; -static Program globalPrg; +// set current context so that lookups can be made correctly 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) { + // if the typename equals one of the current generic instantiations + // print instantiated type instead for (auto kv : currentInstantiation) { if (t.name == kv.first) @@ -87,28 +78,29 @@ std::ostream & operator<< (std::ostream & out, const Type & t) TypeInfo ti = typeType(globalCtx, t); if (ti.isStruct) out << "struct "; + // try finding type in current context auto s = findStruct(t.name, t.namespacePrefixes, globalCtx); + // print prefix for either found type or the specified + // prefix if type is not found (shouldn't happen) if (s.has_value()) out << vectorStr(std::get<1>(*s), "_", true) << t.name; else out << vectorStr(t.namespacePrefixes, "_", true) << t.name; + + // print generic appendix if (!t.genericInstantiation.empty()) out << genericAppendix(t.genericInstantiation); return out; } -std::ostream & operator<< (std::ostream & out, const Variable & v) -{ - out << v.type << " "; +std::string generateModifiers (std::string s, std::vector modifiers) +{ 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++) + // apply modifiers, inverted because C defines them + // the opposite direction + for (auto m = modifiers.rbegin(); m != modifiers.rend(); m++) { if (m->type == TypeModifierType::Pointer) { @@ -126,7 +118,23 @@ std::ostream & operator<< (std::ostream & out, const Variable & v) s = sstr.str(); } } - out << s; + + return s; +} + +std::ostream & operator<< (std::ostream & out, const Variable & v) +{ + out << v.type << " "; + + std::string s = v.name; + + // lookup variable and change name to reflect containing namespace + auto var = findVariable(v.name, namespaces, globalCtx); + if (var.has_value()) + s = vectorStr(std::get<1>(*var), "_", true) + s; + + // apply modifiers in C fashion + out << generateModifiers(s, v.type.modifiers); return out; } @@ -166,6 +174,7 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) { case ExprType::Func: { + // print function call auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx); if (std::get<0>(*f).defined) @@ -178,6 +187,8 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) } case ExprType::Method: { + // get TypeInfo on the Expression that the method is called on + // then print method call TypeInfo ti = typeExpr(globalCtx, *e._method.expr); out << vectorStr(ti.type.namespacePrefixes, "_", true) << @@ -215,6 +226,7 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) case ExprType::Bracket: out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break; case ExprType::Identifier: + // try variable lookup auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx); if (v.has_value()) out << vectorStr(std::get<1>(*v), "_", true); @@ -264,11 +276,13 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s) void tocFunction (std::ostream & out, const Function & f, bool stub) { - if (!stub && !f.defined) return; + // for a function that is not defined, only the stub can be printed + if (!f.defined && !stub) return; + // regular function if (f.genericTypeNames.empty()) { - out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")"; + out << f.returnType << " " << generateModifiers(namespacePrefix() + f.name, f.returnType.modifiers) << " (" << vectorStr(f.parameters, ", ") << ")"; if (stub) { @@ -279,16 +293,21 @@ void tocFunction (std::ostream & out, const Function & f, bool stub) out << "\n" << f.body; } } + // generic function else { + // print one instance per instantiation for (auto instantiation : f.genericInstantiations) { + // set global type mapping 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, ", ") << ")"; + out << f.returnType << " " << + generateModifiers(namespacePrefix() + f.name + genericAppendix(instantiation), f.returnType.modifiers) << + " (" << vectorStr(f.parameters, ", ") << ")"; if (stub) { @@ -305,6 +324,7 @@ void tocFunction (std::ostream & out, const Function & f, bool stub) } void tocStruct (std::ostream & out, const Struct & s, bool stub) { + // regular struct if (s.genericTypeNames.empty()) { out << "struct " << namespacePrefix() << s.name; @@ -315,6 +335,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) { Function f = m; + // add implicit this parameter f.parameters.insert(f.parameters.begin(), {"this", { @@ -326,7 +347,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) } }); out << f.returnType << " " << - namespacePrefix() << s.name << "_" << f.name << + generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) << " (" << vectorStr(f.parameters, ", ") << ");\n"; } return; @@ -346,6 +367,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) for (auto m : s.methods) { Function f = m; + + // add implicit this parameter f.parameters.insert(f.parameters.begin(), {"this", { @@ -357,10 +380,11 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) } }); out << f.returnType << " " << - namespacePrefix() << s.name << "_" << f.name << - " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; + generateModifiers(namespacePrefix() + s.name + "_" + f.name, f.returnType.modifiers) << + " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; } } + // generic struct else { for (auto instantiation : s.genericInstantiations) @@ -378,6 +402,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) { Function f = m; + // add implicit this parameter f.parameters.insert(f.parameters.begin(), {"this", { @@ -389,7 +414,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) } }); out << f.returnType << " " << - namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name << + generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) << " (" << vectorStr(f.parameters, ", ") << ");\n"; } return; @@ -409,6 +434,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) for (auto m : s.methods) { Function f = m; + + // add implicit this parameter f.parameters.insert(f.parameters.begin(), {"this", { @@ -420,8 +447,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) } }); out << f.returnType << " " << - namespacePrefix() << s.name << genericAppendix(instantiation) << "_" << f.name << - " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; + generateModifiers(namespacePrefix() + s.name + genericAppendix(instantiation) + "_" + f.name, f.returnType.modifiers) << + " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; } currentInstantiation.clear(); @@ -432,7 +459,6 @@ void tocProgram (std::ostream & out, const Program & p) { globalCtx = p.ctx; - globalPrg = p; for (auto n : p.ctx->namespaces) { tocNamespace(out, n, true);