mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-18 04:48:44 +00:00
part of type deduction done, debugging in proces
This commit is contained in:
parent
a208e2f42d
commit
3815f8259b
6 changed files with 212 additions and 59 deletions
|
|
@ -23,7 +23,8 @@ bool AbstractType::Require(const AbstractType& type) const { // TODO: cache Depe
|
|||
}
|
||||
|
||||
bool AbstractType::DeduceContext(const AbstractType& actual_type,
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
return typeclass_graph_.GetDependenciesSet(graph_id_).count(actual_type.graph_id_) != 0 || graph_id_ == actual_type.graph_id_;
|
||||
}
|
||||
|
||||
|
|
@ -55,9 +56,10 @@ bool DefinedType::Require(const DefinedType& type) const {
|
|||
}
|
||||
|
||||
bool DefinedType::DeduceContext(const DefinedType& actual_type,
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
return type_id_ == actual_type.type_id_
|
||||
&& type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context);
|
||||
&& type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context, type_manager);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name,
|
||||
|
|
@ -115,13 +117,14 @@ bool TupleType::Require(const TupleType& type) const {
|
|||
}
|
||||
|
||||
bool TupleType::DeduceContext(const TupleType& actual_type,
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) 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)) {
|
||||
if (!type_manager_->GetAnyValue(fields_[i].second)->DeduceContext(*type_manager_->GetAnyValue(actual_type.fields_[i].second), context, type_manager)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -215,7 +218,8 @@ bool VariantType::Require(const VariantType& type) const {
|
|||
}
|
||||
|
||||
bool VariantType::DeduceContext(const VariantType& actual_type,
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
if (constructors_.size() != actual_type.constructors_.size()) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -230,7 +234,7 @@ bool VariantType::DeduceContext(const VariantType& actual_type,
|
|||
}
|
||||
|
||||
if (constructors_[i].second.has_value()) {
|
||||
if (!constructors_[i].second.value().DeduceContext(actual_type.constructors_[i].second.value(), context)) {
|
||||
if (!constructors_[i].second.value().DeduceContext(actual_type.constructors_[i].second.value(), context, type_manager)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -286,8 +290,9 @@ bool OptionalType::Require(const OptionalType& type) const {
|
|||
}
|
||||
|
||||
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::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
return type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context, type_manager);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string&,
|
||||
|
|
@ -320,8 +325,9 @@ bool ReferenceToType::Require(const ReferenceToType& type) const {
|
|||
}
|
||||
|
||||
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::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
return references_ == actual_type.references_ && type_manager_->GetAnyValue(type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.type_), context, type_manager);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name,
|
||||
|
|
@ -396,13 +402,14 @@ bool FunctionType::Require(const FunctionType& type) const {
|
|||
}
|
||||
|
||||
bool FunctionType::DeduceContext(const FunctionType& actual_type,
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context) const {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) 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)) {
|
||||
if (!type_manager_->GetAnyValue(argument_types_[i])->DeduceContext(*type_manager_->GetAnyValue(actual_type.argument_types_[i]), context, type_manager)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -457,8 +464,9 @@ bool ArrayType::Require(const ArrayType& type) const {
|
|||
}
|
||||
|
||||
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::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) const {
|
||||
return size_ == actual_type.size_ && type_manager_->GetAnyValue(elements_type_)->DeduceContext(*type_manager_->GetAnyValue(actual_type.elements_type_), context, type_manager);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string&,
|
||||
|
|
@ -570,15 +578,16 @@ bool Type::Require(const Type& type) const { // TODO: check abstract type requir
|
|||
|
||||
// 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 {
|
||||
std::unordered_map<std::string, std::optional<utils::IdType>>& context,
|
||||
TypeManager& type_manager) 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
|
||||
context[type_name] = type_manager.AddAnyValue(Type(actual_type), utils::ValueType::Tmp);
|
||||
// TODO: choose value type ?? (or should be set later ??)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -588,30 +597,38 @@ bool Type::DeduceContext(const Type& actual_type,
|
|||
switch (this_index) {
|
||||
case 0:
|
||||
return std::get<AbstractType>(type_).DeduceContext(std::get<AbstractType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
case 1:
|
||||
return std::get<DefinedType>(type_).DeduceContext(std::get<DefinedType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
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);
|
||||
context,
|
||||
type_manager);
|
||||
case 4:
|
||||
return std::get<VariantType>(type_).DeduceContext(std::get<VariantType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
case 5:
|
||||
return std::get<ReferenceToType>(type_).DeduceContext(std::get<ReferenceToType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
case 6:
|
||||
return std::get<FunctionType>(type_).DeduceContext(std::get<FunctionType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
case 7:
|
||||
return std::get<ArrayType>(type_).DeduceContext(std::get<ArrayType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
case 8:
|
||||
return std::get<OptionalType>(type_).DeduceContext(std::get<OptionalType>(actual_type.type_),
|
||||
context);
|
||||
context,
|
||||
type_manager);
|
||||
default:
|
||||
// error
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue