65 lines
3.2 KiB
C++
65 lines
3.2 KiB
C++
#ifndef MALACHSCRIPT_BOUNDNAMESPACESTRINGIFIER_HPP
|
|
#define MALACHSCRIPT_BOUNDNAMESPACESTRINGIFIER_HPP
|
|
|
|
#include "BoundNamespace.hpp"
|
|
|
|
template <typename K, typename V> static std::vector<std::pair<K, V>> mapToVector(const std::unordered_map<K, V>& map) {
|
|
auto vec = std::vector<std::pair<K, V>>(map.begin(), map.end());
|
|
std::reverse(vec.begin(), vec.end());
|
|
return vec;
|
|
};
|
|
|
|
namespace MalachScript::Binder {
|
|
class BoundNamespaceStringifier {
|
|
static void StringifyField(const MalachScript::Identifier& identifier, const BoundVariable* field,
|
|
std::stringstream& stream, const std::string& prefix, bool isLast) {
|
|
stream << prefix;
|
|
stream << (isLast ? "└──" : "├──");
|
|
stream << identifier << ": " << field->GetType()->GetIdentifier();
|
|
}
|
|
static void StringifyType(const MalachScript::Identifier& identifier, const BoundType* type,
|
|
std::stringstream& stream, const std::string& prefix, bool isLast) {
|
|
stream << prefix;
|
|
stream << (isLast ? "└──" : "├──");
|
|
stream << "class " << identifier << " (size: " << type->GetSize() << ")";
|
|
auto types = mapToVector(type->GetTypes());
|
|
for (size_t i = 0; i < types.size(); i++) {
|
|
stream << std::endl;
|
|
StringifyType(types[i].first, types[i].second, stream, prefix + (isLast ? " " : "│ "),
|
|
i == types.size() - 1);
|
|
}
|
|
auto fields = mapToVector(type->GetFieldsLookup());
|
|
for (size_t i = 0; i < fields.size(); i++) {
|
|
stream << std::endl;
|
|
StringifyField(fields[i].first, fields[i].second, stream, prefix + (isLast ? " " : "│ "),
|
|
i == fields.size() - 1);
|
|
}
|
|
}
|
|
static void StringifyNamespace(const MalachScript::Identifier& identifier, const BoundNamespace* boundNamespace,
|
|
std::stringstream& stream, const std::string& prefix, bool isLast) {
|
|
stream << prefix;
|
|
stream << (isLast ? "└──" : "├──");
|
|
stream << "namespace " << identifier;
|
|
auto types = mapToVector(boundNamespace->GetTypes());
|
|
for (size_t i = 0; i < types.size(); i++) {
|
|
stream << std::endl;
|
|
StringifyType(types[i].first, types[i].second, stream, prefix + (isLast ? " " : "│ "),
|
|
i == types.size() - 1);
|
|
}
|
|
auto namespaces = mapToVector(boundNamespace->GetNamespaces());
|
|
for (size_t i = 0; i < namespaces.size(); i++) {
|
|
stream << std::endl;
|
|
StringifyNamespace(namespaces[i].first, namespaces[i].second, stream,
|
|
prefix + (isLast ? " " : "│ "), i == namespaces.size() - 1);
|
|
}
|
|
}
|
|
|
|
public:
|
|
static void Stringify(const BoundNamespace* ns, std::stringstream& stream) {
|
|
StringifyNamespace("global"_id, ns, stream, "", true);
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // MALACHSCRIPT_BOUNDNAMESPACESTRINGIFIER_HPP
|