mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-06 06:58:46 +00:00
new typecheck start
This commit is contained in:
parent
fbe486d25a
commit
7fe9040449
2 changed files with 94 additions and 1 deletions
|
|
@ -280,7 +280,7 @@ public:
|
||||||
|
|
||||||
void clear_resolved_generic_names() { resolved_generic_names_.clear(); }
|
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);
|
bool add_local_type(const std::string &name);
|
||||||
|
|
||||||
|
|
@ -301,6 +301,21 @@ public:
|
||||||
local_name_typeclasses_.clear();
|
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:
|
private:
|
||||||
Type *get_type(size_t id) { return &storage_.at(id); }
|
Type *get_type(size_t id) { return &storage_.at(id); }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -201,6 +201,84 @@ TypeStorage::get_local_type_requirements(const std::string &name) const {
|
||||||
return &local_name_typeclasses_[iter->second];
|
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<GenericType>(&left.type);
|
||||||
|
// left_generic != nullptr) {
|
||||||
|
// // TODO: check if other type contains generic
|
||||||
|
// std::clog << "left is resolved with policy <" <<
|
||||||
|
// static_cast<size_t>(policy) << ">\n"; resolve(*left_generic, right,
|
||||||
|
// left.mode); return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (const auto *right_generic = get_if<GenericType>(&right.type);
|
||||||
|
// right_generic != nullptr) {
|
||||||
|
// // TODO: check if other type contains generic
|
||||||
|
// std::clog << "right is resolved with policy <" <<
|
||||||
|
// static_cast<size_t>(policy) << ">\n"; resolve(*right_generic, left,
|
||||||
|
// right.mode); return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (left.type.index() != right.type.index()) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (holds_alternative<ArrowType>(left.type)) {
|
||||||
|
// const auto &left_types = std::get<ArrowType>(left.type).types;
|
||||||
|
// const auto &right_types = std::get<ArrowType>(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 TypeStorage::generate_generic_type_identifier() {
|
||||||
Identifier identifier =
|
Identifier identifier =
|
||||||
Identifier(Node(), Identifier::GENERIC_TYPE,
|
Identifier(Node(), Identifier::GENERIC_TYPE,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue