big part of type_chack_visitor done

This commit is contained in:
ProgramSnail 2023-05-06 19:26:14 +03:00
parent adccf6feec
commit f7080ba856
7 changed files with 427 additions and 171 deletions

View file

@ -30,6 +30,10 @@ bool AbstractType::operator>(const AbstractType& type) const {
return type < *this;
}
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
std::optional<utils::IdType> DefinedType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -56,6 +60,10 @@ bool DefinedType::operator>(const DefinedType& type) const {
return type < *this;
}
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name) const {
return type_manager_->GetAnyType(type_)->GetFieldType(name);
}
//
std::optional<utils::IdType> TupleType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -102,6 +110,15 @@ bool TupleType::operator>(const TupleType& type) const {
return type < *this;
}
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name) const {
for (size_t i = 0; i < fields_.size(); ++i) { // TODO: optimize??
if (fields_[i].first.has_value() && fields_[i].first.value() == name) {
return fields_[i].second;
}
}
return std::nullopt;
}
//
std::optional<utils::IdType> VariantType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -144,6 +161,13 @@ bool VariantType::operator>(const VariantType& type) const {
return type < *this;
}
std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name) const {
if (current_constructor_.has_value()) {
return constructors_.at(current_constructor_.value()).GetFieldType(name);
}
return std::nullopt;
}
//
std::optional<utils::IdType> OptionalType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -168,6 +192,10 @@ bool OptionalType::operator>(const OptionalType& type) const {
return type < *this;
}
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
std::optional<utils::IdType> ReferenceToType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -192,6 +220,10 @@ bool ReferenceToType::operator>(const ReferenceToType& type) const {
return type < *this;
}
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name) const {
return type_manager_->GetAnyType(type_)->GetFieldType(name);
}
//
std::optional<utils::IdType> FunctionType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -239,6 +271,10 @@ bool FunctionType::operator>(const FunctionType& type) const {
return type < *this;
}
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
std::optional<utils::IdType> ArrayType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -264,6 +300,10 @@ bool ArrayType::operator>(const ArrayType& type) const {
return type < *this;
}
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
@ -286,6 +326,8 @@ std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::strin
return std::get<FunctionType>(type_).InContext(context);
case 7:
return std::get<ArrayType>(type_).InContext(context);
case 8:
return std::get<OptionalType>(type_).InContext(context);
default:
// error
break;
@ -316,6 +358,8 @@ bool Type::Same(const Type& type) const {
return std::get<FunctionType>(type_).Same(std::get<FunctionType>(type.type_));
case 7:
return std::get<ArrayType>(type_).Same(std::get<ArrayType>(type.type_));
case 8:
return std::get<OptionalType>(type_).Same(std::get<OptionalType>(type.type_));
default:
// error
break;
@ -347,6 +391,8 @@ bool Type::operator<(const Type& type) const {
return std::get<FunctionType>(type_) < std::get<FunctionType>(type.type_);
case 7:
return std::get<ArrayType>(type_) < std::get<ArrayType>(type.type_);
case 8:
return std::get<OptionalType>(type_) < std::get<OptionalType>(type.type_);
default:
// error
break;
@ -360,24 +406,63 @@ bool Type::operator>(const Type& type) const {
return type < *this;
}
std::optional<utils::IdType> Type::GetFieldType(const std::string& name) const {
size_t index = type_.index();
switch (index) {
case 0:
return std::get<AbstractType>(type_).GetFieldType(name);
case 1:
return std::get<DefinedType>(type_).GetFieldType(name);
case 2:
return std::nullopt;
case 3:
return std::get<TupleType>(type_).GetFieldType(name);
case 4:
return std::get<VariantType>(type_).GetFieldType(name);
case 5:
return std::get<ReferenceToType>(type_).GetFieldType(name);
case 6:
return std::get<FunctionType>(type_).GetFieldType(name);
case 7:
return std::get<ArrayType>(type_).GetFieldType(name);
case 8:
return std::get<OptionalType>(type_).GetFieldType(name);
default:
// error
break;
}
return std::nullopt;
}
//
template<typename T>
utils::IdType TypeManager::AddType(const T&& type) {
types_.emplace_back(std::forward(type));
utils::IdType TypeManager::AddType(T&& type, utils::ValueType value_type) {
types_.emplace_back({std::forward(type), value_type});
return types_.size() - 1;
}
utils::IdType TypeManager::AddAnyType(Type&& type, utils::ValueType value_type) {
types_.push_back({std::move(type), value_type});
return types_.size() - 1;
}
template<typename T>
std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
if (!std::holds_alternative<T>(types_.at(type_id))) {
if (!std::holds_alternative<T>(types_.at(type_id).second)) {
return std::nullopt;
}
return &std::get<T>(types_.at(type_id));
return &std::get<T>(types_.at(type_id).second);
}
Type* TypeManager::GetAnyType(utils::IdType type_id) {
return &types_.at(type_id);
return &types_.at(type_id).first;
}
utils::ValueType TypeManager::GetValueType(utils::IdType type_id) {
return types_.at(type_id).second;
}
bool TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {