modifications, part of type deduction system

This commit is contained in:
ProgramSnail 2023-07-02 18:28:16 +03:00
parent 238180b778
commit 0d3dd248da
4 changed files with 203 additions and 71 deletions

View file

@ -18,12 +18,13 @@ bool AbstractType::Same(const AbstractType& type) const {
return graph_id_ == type.graph_id_;
}
bool AbstractType::operator<(const AbstractType& type) const { // TODO: cache DependenciesSet
bool AbstractType::Require(const AbstractType& type) const { // TODO: cache DependenciesSet
return typeclass_graph_.GetDependenciesSet(graph_id_).count(type.graph_id_) != 0 || graph_id_ == type.graph_id_;
}
bool AbstractType::operator>(const AbstractType& type) const {
return type < *this;
bool AbstractType::DeduceContext(const AbstractType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
return typeclass_graph_.GetDependenciesSet(graph_id_).count(actual_type.graph_id_) != 0 || graph_id_ == actual_type.graph_id_;
}
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string&,
@ -48,13 +49,15 @@ bool DefinedType::Same(const DefinedType& type) const {
&& type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
}
bool DefinedType::operator<(const DefinedType& type) const {
bool DefinedType::Require(const DefinedType& type) const {
return type_id_ == type.type_id_
&& *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
&& type_manager_->GetAnyValue(type_)->Require(*type_manager_->GetAnyValue(type.type_));
}
bool DefinedType::operator>(const DefinedType& type) const {
return type < *this;
bool DefinedType::DeduceContext(const DefinedType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
return type_id_ == actual_type.type_id_
&& type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context);
}
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name,
@ -97,13 +100,13 @@ bool TupleType::Same(const TupleType& type) const {
return true;
}
bool TupleType::operator<(const TupleType& type) const {
bool TupleType::Require(const TupleType& type) const {
if (fields_.size() != type.fields_.size()) {
return false;
}
for (size_t i = 0; i < fields_.size(); ++i) {
if (!(*type_manager_->GetAnyValue(fields_[i].second) < *type_manager_->GetAnyValue(type.fields_[i].second))) {
if (!type_manager_->GetAnyValue(fields_[i].second)->Require(*type_manager_->GetAnyValue(type.fields_[i].second))) {
return false;
}
}
@ -111,8 +114,19 @@ bool TupleType::operator<(const TupleType& type) const {
return true;
}
bool TupleType::operator>(const TupleType& type) const {
return type < *this;
bool TupleType::DeduceContext(const TupleType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
if (fields_.size() != actual_type.fields_.size()) {
return false;
}
for (size_t i = 0; i < fields_.size(); ++i) {
if (!type_manager_->GetAnyValue(fields_[i].second)->DeduceContext(*type_manager_->GetAnyValue(actual_type.fields_[i].second), context)) {
return false;
}
}
return true;
}
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name,
@ -158,7 +172,6 @@ bool VariantType::Same(const VariantType& type) const {
}
for (size_t i = 0; i < constructors_.size(); ++i) {
if (constructors_[i].first != type.constructors_[i].first) { // TODO: decide
return false;
}
@ -177,22 +190,53 @@ bool VariantType::Same(const VariantType& type) const {
return true;
}
bool VariantType::operator<(const VariantType& type) const {
bool VariantType::Require(const VariantType& type) const {
if (constructors_.size() != type.constructors_.size()) {
return false;
}
for (size_t i = 0; i < constructors_.size(); ++i) {
if (!(constructors_[i] < constructors_[i])) {
if (constructors_[i].first != type.constructors_[i].first) { // TODO: decide
return false;
}
if (constructors_[i].second.has_value() != type.constructors_[i].second.has_value()) {
return false;
}
if (constructors_[i].second.has_value()) {
if (!constructors_[i].second.value().Require(type.constructors_[i].second.value())) {
return false;
}
}
}
return true;
}
bool VariantType::operator>(const VariantType& type) const {
return type < *this;
bool VariantType::DeduceContext(const VariantType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
if (constructors_.size() != actual_type.constructors_.size()) {
return false;
}
for (size_t i = 0; i < constructors_.size(); ++i) {
if (constructors_[i].first != actual_type.constructors_[i].first) { // TODO: decide
return false;
}
if (constructors_[i].second.has_value() != actual_type.constructors_[i].second.has_value()) {
return false;
}
if (constructors_[i].second.has_value()) {
if (!constructors_[i].second.value().DeduceContext(actual_type.constructors_[i].second.value(), context)) {
return false;
}
}
}
return true;
}
std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name,
@ -237,12 +281,13 @@ bool OptionalType::Same(const OptionalType& type) const {
return type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
}
bool OptionalType::operator<(const OptionalType& type) const {
return *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
bool OptionalType::Require(const OptionalType& type) const {
return type_manager_->GetAnyValue(type_)->Require(*type_manager_->GetAnyValue(type.type_));
}
bool OptionalType::operator>(const OptionalType& type) const {
return type < *this;
bool OptionalType::DeduceContext(const OptionalType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
return type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context);
}
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string&,
@ -270,12 +315,13 @@ bool ReferenceToType::Same(const ReferenceToType& type) const {
return references_ == type.references_ && type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
}
bool ReferenceToType::operator<(const ReferenceToType& type) const {
return references_ == type.references_ && *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
bool ReferenceToType::Require(const ReferenceToType& type) const {
return references_ == type.references_ && type_manager_->GetAnyValue(type_)->Require(*type_manager_->GetAnyValue(type.type_));
}
bool ReferenceToType::operator>(const ReferenceToType& type) const {
return type < *this;
bool ReferenceToType::DeduceContext(const ReferenceToType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
return references_ == actual_type.references_ && type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context);
}
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name,
@ -335,13 +381,13 @@ bool FunctionType::Same(const FunctionType& type) const {
return true;
}
bool FunctionType::operator<(const FunctionType& type) const {
bool FunctionType::Require(const FunctionType& type) const {
if (argument_types_.size() != type.argument_types_.size()) {
return false;
}
for (size_t i = 0; i < argument_types_.size(); ++i) {
if (!(*type_manager_->GetAnyValue(argument_types_[i]) < *type_manager_->GetAnyValue(type.argument_types_[i]))) {
if (!type_manager_->GetAnyValue(argument_types_[i])->Require(*type_manager_->GetAnyValue(type.argument_types_[i]))) {
return false;
}
}
@ -349,8 +395,19 @@ bool FunctionType::operator<(const FunctionType& type) const {
return true;
}
bool FunctionType::operator>(const FunctionType& type) const {
return type < *this;
bool FunctionType::DeduceContext(const FunctionType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
if (argument_types_.size() != actual_type.argument_types_.size()) {
return false;
}
for (size_t i = 0; i < argument_types_.size(); ++i) {
if (!type_manager_->GetAnyValue(argument_types_[i])->DeduceContext(*type_manager_->GetAnyValue(actual_type.argument_types_[i]), context)) {
return false;
}
}
return true;
}
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string&,
@ -395,12 +452,13 @@ bool ArrayType::Same(const ArrayType& type) const {
return size_ == type.size_ && type_manager_->GetAnyValue(elements_type_)->Same(*type_manager_->GetAnyValue(type.elements_type_));
}
bool ArrayType::operator<(const ArrayType& type) const {
return size_ == type.size_ && *type_manager_->GetAnyValue(elements_type_) < *type_manager_->GetAnyValue(type.elements_type_);
bool ArrayType::Require(const ArrayType& type) const {
return size_ == type.size_ && type_manager_->GetAnyValue(elements_type_)->Require(*type_manager_->GetAnyValue(type.elements_type_));
}
bool ArrayType::operator>(const ArrayType& type) const {
return type < *this;
bool ArrayType::DeduceContext(const ArrayType& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
return size_ == actual_type.size_ && type_manager_->GetAnyValue(elements_type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.elements_type_), context);
}
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string&,
@ -477,30 +535,30 @@ bool Type::Same(const Type& type) const {
return false;
}
bool Type::operator<(const Type& type) const {
bool Type::Require(const Type& type) const { // TODO: check abstract type requirements for not abstract types
size_t this_index = type_.index();
size_t type_index = type.type_.index();
if (this_index == type_index) {
switch (this_index) {
case 0:
return std::get<AbstractType>(type_) < std::get<AbstractType>(type.type_);
return std::get<AbstractType>(type_).Require(std::get<AbstractType>(type.type_));
case 1:
return std::get<DefinedType>(type_) < std::get<DefinedType>(type.type_);
return std::get<DefinedType>(type_).Require(std::get<DefinedType>(type.type_));
case 2:
return std::get<InternalType>(type_) == std::get<InternalType>(type.type_);
case 3:
return std::get<TupleType>(type_) < std::get<TupleType>(type.type_);
return std::get<TupleType>(type_).Require(std::get<TupleType>(type.type_));
case 4:
return std::get<VariantType>(type_) < std::get<VariantType>(type.type_);
return std::get<VariantType>(type_).Require(std::get<VariantType>(type.type_));
case 5:
return std::get<ReferenceToType>(type_) < std::get<ReferenceToType>(type.type_);
return std::get<ReferenceToType>(type_).Require(std::get<ReferenceToType>(type.type_));
case 6:
return std::get<FunctionType>(type_) < std::get<FunctionType>(type.type_);
return std::get<FunctionType>(type_).Require(std::get<FunctionType>(type.type_));
case 7:
return std::get<ArrayType>(type_) < std::get<ArrayType>(type.type_);
return std::get<ArrayType>(type_).Require(std::get<ArrayType>(type.type_));
case 8:
return std::get<OptionalType>(type_) < std::get<OptionalType>(type.type_);
return std::get<OptionalType>(type_).Require(std::get<OptionalType>(type.type_));
default:
// error
break;
@ -510,8 +568,57 @@ bool Type::operator<(const Type& type) const {
return false;
}
bool Type::operator>(const Type& type) const {
return type < *this;
// TODO: check abstract type requirements for not abstract types
bool Type::DeduceContext(const Type& actual_type,
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
size_t this_index = type_.index();
size_t type_index = actual_type.type_.index();
if (this_index == 0) {
std::string type_name = std::get<AbstractType>(type_).GetName();
if (context.count(type_name) != 0) { // is abstract type
// context[type_name] = // TODO: actual_type.id_; ??
// TODO: fixes
}
}
error_handling::HandleGeneralError("Type::DeduceContext: not implemented");
if (this_index == type_index) {
switch (this_index) {
case 0:
return std::get<AbstractType>(type_).DeduceContext(std::get<AbstractType>(actual_type.type_),
context);
case 1:
return std::get<DefinedType>(type_).DeduceContext(std::get<DefinedType>(actual_type.type_),
context);
case 2:
return std::get<InternalType>(type_) == std::get<InternalType>(actual_type.type_);
case 3:
return std::get<TupleType>(type_).DeduceContext(std::get<TupleType>(actual_type.type_),
context);
case 4:
return std::get<VariantType>(type_).DeduceContext(std::get<VariantType>(actual_type.type_),
context);
case 5:
return std::get<ReferenceToType>(type_).DeduceContext(std::get<ReferenceToType>(actual_type.type_),
context);
case 6:
return std::get<FunctionType>(type_).DeduceContext(std::get<FunctionType>(actual_type.type_),
context);
case 7:
return std::get<ArrayType>(type_).DeduceContext(std::get<ArrayType>(actual_type.type_),
context);
case 8:
return std::get<OptionalType>(type_).DeduceContext(std::get<OptionalType>(actual_type.type_),
context);
default:
// error
break;
}
}
return false;
}
std::optional<utils::IdType> Type::GetFieldType(const std::string& name,