X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/17860defa84c6d8bc0e8bc088a7e09361f17db07..66a27d2fc7c1ad4e97de76d4982168a0fed9920a:/src/toc.h?ds=sidebyside diff --git a/src/toc.h b/src/toc.h index 8bf0e68..095a72c 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,13 +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::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) @@ -69,10 +75,19 @@ std::ostream & operator<< (std::ostream & out, const Type & t) return out; } } - TypeInfo ti = typeType(globalPrg, t); + TypeInfo ti = typeType(globalCtx, t); if (ti.isStruct) out << "struct "; - out << vectorStr(t.namespacePrefixes, "_", true) << t.name; + // 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); @@ -84,7 +99,14 @@ std::ostream & operator<< (std::ostream & out, const Variable & v) std::stringstream sstr; 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; + // nest modifiers, inverted because C defines them + // the opposite direction for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++) { if (m->type == TypeModifierType::Pointer) @@ -143,25 +165,31 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) { case ExprType::Func: { - if (e._func.namespacePrefixes.empty()) - { - TypeInfo ti = typeExpr(globalPrg, namespaces, globalCtx, e); - - } - out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName; + // print function call + 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, globalCtx, *e._method.expr); + // 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) << ti.type.name << genericAppendix(ti.type.genericInstantiation) << "_" << e._method.methodName; if (!e._method.genericInstantiation.empty()) out << genericAppendix(e._method.genericInstantiation); - out << "(&" << *e._method.expr << (e._method.arguments.empty() ? "" : ", ") << + 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: @@ -171,7 +199,7 @@ 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.isPointer ? "->" : ".") << e._dot.identifier; break; case ExprType::PrefixOp: @@ -189,7 +217,14 @@ 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; + // try variable lookup + 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; @@ -232,8 +267,10 @@ 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, ", ") << ")"; @@ -247,10 +284,13 @@ 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]; @@ -273,6 +313,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; @@ -283,6 +324,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", { @@ -314,6 +356,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", { @@ -329,6 +373,7 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body; } } + // generic struct else { for (auto instantiation : s.genericInstantiations) @@ -346,6 +391,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", { @@ -377,6 +423,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", { @@ -396,14 +444,11 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) } } } -void tocProgram (std::ostream & out, const Program & _p) +void tocProgram (std::ostream & out, const Program & p) { - Program p = instantiateGenerics(_p); - globalCtx = p.ctx; - globalPrg = p; - for (auto n : p.namespaces) + for (auto n : p.ctx->namespaces) { tocNamespace(out, n, true); } @@ -424,7 +469,7 @@ void tocProgram (std::ostream & out, const Program & _p) out << v << ";\n"; } out << "\n\n"; - for (auto n : p.namespaces) + for (auto n : p.ctx->namespaces) { tocNamespace(out, n, false); } @@ -456,7 +501,7 @@ void tocNamespace (std::ostream & out, const Namespace & n, bool stub) } out << "\n\n"; } - for (auto n : n.namespaces) + for (auto n : n.ctx->namespaces) { tocNamespace(out, n, stub); out << "\n\n";