2019-06-09 16:13:13 -07:00
|
|
|
#include "type.hpp"
|
2019-06-09 19:51:53 -07:00
|
|
|
#include "error.hpp"
|
2019-06-09 16:13:13 -07:00
|
|
|
|
|
|
|
namespace lily {
|
|
|
|
type_data::constructor* type_data::create_constructor(const std::string& name,
|
|
|
|
std::vector<type*>&& params) {
|
|
|
|
auto new_constructor = std::make_unique<constructor>();
|
|
|
|
new_constructor->id = constructors.size();
|
|
|
|
new_constructor->parent = this;
|
|
|
|
new_constructor->params = std::move(params);
|
|
|
|
|
|
|
|
constructor* raw_ptr = new_constructor.get();
|
2019-06-09 20:24:44 -07:00
|
|
|
constructors[name] = std::move(new_constructor);
|
2019-06-09 16:13:13 -07:00
|
|
|
return raw_ptr;
|
|
|
|
}
|
2019-06-09 19:51:53 -07:00
|
|
|
|
|
|
|
bool try_unify_pram(type* substitute_with, type* substitute_in) {
|
|
|
|
type_parameter* param = dynamic_cast<type_parameter*>(substitute_in);
|
|
|
|
if(!param) return false;
|
|
|
|
if(!param->actual_type) {
|
|
|
|
param->actual_type = substitute_with;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return substitute_with->unify_with(param->actual_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool type_internal::unify_with(type* other) {
|
|
|
|
type_internal* other_int = dynamic_cast<type_internal*>(other);
|
|
|
|
if(other_int && other_int->type_id == type_id) return true;
|
|
|
|
if(try_unify_pram(this, other)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool type_parameter::unify_with(type* other) {
|
|
|
|
if(this == other) return true;
|
|
|
|
if(!actual_type) {
|
|
|
|
actual_type = other;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(try_unify_pram(this, other)) return true;
|
|
|
|
if(actual_type->unify_with(other)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool type_data::unify_with(type* other) {
|
|
|
|
type_data* other_data = dynamic_cast<type_data*>(other);
|
|
|
|
if(other_data && other_data->type_id == type_id) return true;
|
|
|
|
if(try_unify_pram(this, other)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool type_func::unify_with(type* other) {
|
|
|
|
type_func* other_func = dynamic_cast<type_func*>(other);
|
|
|
|
if(try_unify_pram(this, other)) return true;
|
|
|
|
if(other_func &&
|
|
|
|
left->unify_with(other_func->left) &&
|
|
|
|
right->unify_with(other_func->right)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-09 16:13:13 -07:00
|
|
|
}
|