lily/src/type.cpp

62 lines
2.1 KiB
C++

#include "type.hpp"
#include "error.hpp"
namespace lily {
type_data::constructor* type_data::create_constructor(const std::string& name,
std::vector<type*>&& params) {
auto new_constructor = std::unique_ptr<constructor>(new constructor());
new_constructor->id = constructors.size();
new_constructor->parent = this;
new_constructor->params = std::move(params);
constructor* raw_ptr = new_constructor.get();
constructors[name] = std::move(new_constructor);
return raw_ptr;
}
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;
}
}