diff --git a/include/type_graph.hpp b/include/type_graph.hpp index 19cfe23..aa67388 100644 --- a/include/type_graph.hpp +++ b/include/type_graph.hpp @@ -93,6 +93,12 @@ public: back_edges_[to].push_back(from); } + void SetEqual(size_t u, size_t v) { + AddEdge(u, v); + AddEdge(v, u); + // TODO: set equality + } + void Calculate() { if (is_calculated_) { return; diff --git a/include/typeclass_graph.hpp b/include/typeclass_graph.hpp index 86093c3..dc50233 100644 --- a/include/typeclass_graph.hpp +++ b/include/typeclass_graph.hpp @@ -1,6 +1,60 @@ #pragma once +#include +#include + +// for calngd +#include "utils.hpp" +#include "interpreter_tree.hpp" +#include "error_handling.hpp" + +namespace info { + class TypeclassGraph { public: + struct TypeclassMethod { + std::string name; + interpreter::tokens::FunctionDeclaration* definition = nullptr; + }; + + struct ParametrizedTypeclass { + utils::IdType typeclass; + std::vector parameter_ids; // ?? + }; + + struct TypeclassVertex { + std::vector>> parameters; + std::vector methods; + std::vector dependencies; + interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr; + }; + + utils::IdType AddTypeclass(const TypeclassVertex& typeclass) { // TODO: universal reference + for (auto& method : typeclass.methods) { + if (method_to_typeclass_.count(method.name) != 0) { + error_handling::HandleTypecheckError(""); + } + } + for (auto& method : typeclass.methods) { + method_to_typeclass_[method.name] = typeclasses_.size(); + } + typeclasses_.push_back(typeclass); + return typeclasses_.size() - 1; + } + std::optional FindMethodTypeclass(const std::string& name) { + auto method_iter = method_to_typeclass_.find(name); + if (method_iter == method_to_typeclass_.end()) { + return std::nullopt; + } + return method_iter->second; + } + + const TypeclassVertex& GetTypeclass(utils::IdType id) { + return typeclasses_.at(id); + } private: + std::unordered_map method_to_typeclass_; + std::vector typeclasses_; }; + +} // namespace info diff --git a/include/utils.hpp b/include/utils.hpp index 4ebcef7..2fb62ae 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,12 +1,13 @@ #pragma once -#include #include #include namespace utils { -using IdType = std::size_t; +using std::size_t; + +using IdType = size_t; enum class ReferenceType { Reference, UniqueReference }; @@ -39,4 +40,42 @@ private: std::unordered_map value_to_id_; }; +class Union { // TODO: recall right algorithm name +public: + Union(size_t n) { + edges_.resize(n); + ranks_.resize(n); + + for (size_t i = 0; i < n; ++i) { + edges_[i] = i; + ranks_[i] = 1; + } + } + + void Connect(size_t u, size_t v) { // TODO: recall choice algorithm + u = GetRoot(u); + v = GetRoot(v); + if (ranks_[v] >= ranks_[u]) { + edges_[u] = v; + ranks_[v] = std::max(ranks_[u] + 1, ranks_[v]); + } else { + edges_[v] = u; + } + } + + bool IsConnected(size_t u, size_t v) { + return GetRoot(u) == GetRoot(v); + } + + size_t GetRoot(size_t v) { + if (edges_[v] == v) { + return v; + } + return edges_[v] = GetRoot(edges_[v]); + } +private: + std::vector edges_; + std::vector ranks_; +}; + } // namespace utils