From 7fe90404492ea629339e813fb7ced1cc48ac30ca Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Wed, 1 May 2024 22:23:44 +0300 Subject: [PATCH] new typecheck start --- include/nodes/type_nodes.hpp | 17 +++++++- src/nodes/type_nodes.cpp | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/include/nodes/type_nodes.hpp b/include/nodes/type_nodes.hpp index 8e609a3..35850a6 100644 --- a/include/nodes/type_nodes.hpp +++ b/include/nodes/type_nodes.hpp @@ -280,7 +280,7 @@ public: void clear_resolved_generic_names() { resolved_generic_names_.clear(); } - // -- deal with local types + // -- deal with local types // not needed ?? bool add_local_type(const std::string &name); @@ -301,6 +301,21 @@ public: local_name_typeclasses_.clear(); } + // + + enum class UnifyModePolicy { // TODO: proper modes for modifier + Ignore, // all mode differences ignored + ApplyStrongest, // unique > shared, modes changed + CheckLeftIsSubmode, // only check is performed + }; + + // TODO: next iteration of type check + bool unify(TypeProxy left_proxy, TypeProxy right_proxy, + UnifyModePolicy policy); + + bool resolve(TypeProxy generic, + const Type &replacement /* TODO , Mode mode = {}*/); + private: Type *get_type(size_t id) { return &storage_.at(id); } diff --git a/src/nodes/type_nodes.cpp b/src/nodes/type_nodes.cpp index 9f761b7..5939e17 100644 --- a/src/nodes/type_nodes.cpp +++ b/src/nodes/type_nodes.cpp @@ -201,6 +201,84 @@ TypeStorage::get_local_type_requirements(const std::string &name) const { return &local_name_typeclasses_[iter->second]; } +// TODO + +bool TypeStorage::unify(TypeProxy left_proxy, TypeProxy right_proxy, + UnifyModePolicy policy) { + // Type &left = left_id.get(); + // Type &right = right_id.get(); + + // switch (policy) { + // case UnifyModePolicy::Ignore: + // break; + // case UnifyModePolicy::ApplyStrongest: + // left.mode = Mode::choose_min(left.mode, right.mode); + // right.mode = left.mode; + // break; + // case UnifyModePolicy::CheckLeftIsSubmode: + // if (not left.mode.is_submode(right.mode)) { + // return false; + // } + // break; + // } + + // if (const auto *left_generic = get_if(&left.type); + // left_generic != nullptr) { + // // TODO: check if other type contains generic + // std::clog << "left is resolved with policy <" << + // static_cast(policy) << ">\n"; resolve(*left_generic, right, + // left.mode); return true; + // } + + // if (const auto *right_generic = get_if(&right.type); + // right_generic != nullptr) { + // // TODO: check if other type contains generic + // std::clog << "right is resolved with policy <" << + // static_cast(policy) << ">\n"; resolve(*right_generic, left, + // right.mode); return true; + // } + + // if (left.type.index() != right.type.index()) { + // return false; + // } + + // if (holds_alternative(left.type)) { + // const auto &left_types = std::get(left.type).types; + // const auto &right_types = std::get(right.type).types; + + // if (left_types.size() != right_types.size()) { + // return false; + // } + + // bool all_unify_passed = true; + // for (size_t i = 0; i < left_types.size(); ++i) { + // if (not unify(left_types[i], right_types[i], policy)) { + // all_unify_passed = false; + // } + // } + + // return all_unify_passed; + // } + + // TODO + return true; +} + +bool TypeStorage::resolve(TypeProxy generic, + const Type &replacement /* TODO , Mode mode = {}*/) { + error_handling::ensure(generic.get()->is_generic(), "Type should be generic"); + error_handling::ensure(generic.get()->is_generic(), "Type should be generic"); + for (auto &type : storage_) { + if (type.is_generic() && + type.get()->get_name() == generic.get()->get_name()) { + type = replacement; + // type.mode = mode; + } + } +} + +// + Identifier TypeStorage::generate_generic_type_identifier() { Identifier identifier = Identifier(Node(), Identifier::GENERIC_TYPE,