fixes, part of execute_visitor

This commit is contained in:
ProgramSnail 2023-05-10 23:13:50 +03:00
parent 802b736e15
commit 6e487c8fd9
6 changed files with 79 additions and 47 deletions

View file

@ -37,11 +37,11 @@ public:
} }
bool AddValueRequirement(utils::IdType type, utils::IdType requrement) { bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
return value_manager_.AddTypeRequirement(type, requrement); return value_manager_.AddValueRequirement(type, requrement);
} }
bool EqualValues(utils::IdType first_type, utils::IdType second_type) { bool EqualValues(utils::IdType first_type, utils::IdType second_type) {
return value_manager_.EqualTypes(first_type, second_type); return value_manager_.EqualValues(first_type, second_type);
} }
utils::ValueType GetValueType(utils::IdType value_id) { utils::ValueType GetValueType(utils::IdType value_id) {

View file

@ -319,11 +319,11 @@ public:
return types_.at(type_id).second; return types_.at(type_id).second;
} }
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) { bool EqualValues(utils::IdType first_type, utils::IdType second_type) {
return GetAnyValue(first_type)->Same(*GetAnyValue(second_type)); return GetAnyValue(first_type)->Same(*GetAnyValue(second_type));
} }
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) { bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
return *GetAnyValue(requrement) < *GetAnyValue(type); return *GetAnyValue(requrement) < *GetAnyValue(type);
} }

View file

@ -42,10 +42,10 @@ public:
struct TupleValue { struct TupleValue {
public: public:
explicit TupleValue(ValueManager* value_manager) : value_manager_(value_manager) {} TupleValue() = default;
explicit TupleValue(std::vector<std::pair<std::optional<std::string>, utils::IdType>>&& fields, TupleValue(std::vector<std::pair<std::optional<std::string>, utils::IdType>>&& fields,
ValueManager* value_manager) ValueManager* value_manager)
: fields(std::move(fields)), value_manager_(value_manager) {} : fields(std::move(fields)), value_manager_(value_manager) {}
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
@ -53,29 +53,26 @@ public:
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields; std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
private: private:
ValueManager* value_manager_; ValueManager* value_manager_ = nullptr;
}; };
struct VariantValue { struct VariantValue {
public: public:
explicit VariantValue(ValueManager* value_manager) : value_manager_(value_manager) {} explicit VariantValue() = default;
// TupleValue ?? // TODO: add type?? VariantValue(TupleValue&& value, size_t current_constructor)
VariantValue(utils::IdType value, size_t current_constructor, ValueManager* value_manager) : value(std::move(value)), current_constructor(current_constructor) {}
: value(value), current_constructor(current_constructor), value_manager_(value_manager) {}
public: public:
utils::IdType value; TupleValue value;
size_t current_constructor; size_t current_constructor;
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
private:
ValueManager* value_manager_;
}; };
struct ReferenceToValue { struct ReferenceToValue {
public: public:
explicit ReferenceToValue(ValueManager* value_manager) : value_manager_(value_manager) {} ReferenceToValue() = default;
ReferenceToValue(const std::vector<utils::ReferenceModifier>& references, ReferenceToValue(const std::vector<utils::ReferenceModifier>& references,
utils::IdType value, utils::IdType value,
@ -88,12 +85,12 @@ public:
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
private: private:
ValueManager* value_manager_; ValueManager* value_manager_ = nullptr;
}; };
struct FunctionValue { struct FunctionValue {
public: public:
explicit FunctionValue(ValueManager* value_manager) : value_manager_(value_manager) {} FunctionValue() = default;
template<typename T> template<typename T>
FunctionValue(const T& function, ValueManager* value_manager) FunctionValue(const T& function, ValueManager* value_manager)
@ -105,17 +102,17 @@ public:
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
private: private:
ValueManager* value_manager_; ValueManager* value_manager_ = nullptr;
}; };
struct ArrayValue { struct ArrayValue {
public: public:
explicit ArrayValue(ValueManager* value_manager) : value_manager_(value_manager) {} ArrayValue() = default;
explicit ArrayValue(std::vector<utils::IdType>&& elements, ArrayValue(std::vector<utils::IdType>&& elements,
bool is_constant_size, bool is_constant_size,
ValueManager* value_manager) ValueManager* value_manager)
: elements(std::move(elements)), is_constant_size(is_constant_size), value_manager_(value_manager) {} : elements(std::move(elements)), is_constant_size(is_constant_size), value_manager_(value_manager) {}
public: public:
@ -124,14 +121,14 @@ public:
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
private: private:
ValueManager* value_manager_; ValueManager* value_manager_ = nullptr;
}; };
struct OptionalValue { struct OptionalValue {
public: public:
explicit OptionalValue(ValueManager* value_manager) : value_manager_(value_manager) {} OptionalValue() = default;
explicit OptionalValue(utils::IdType value, ValueManager* value_manager) OptionalValue(std::optional<utils::IdType> value, ValueManager* value_manager)
: value(value), value_manager_(value_manager) {} : value(value), value_manager_(value_manager) {}
std::optional<utils::IdType> GetFieldValue(const std::string& name); std::optional<utils::IdType> GetFieldValue(const std::string& name);
@ -139,7 +136,7 @@ public:
std::optional<utils::IdType> value; std::optional<utils::IdType> value;
private: private:
ValueManager* value_manager_; ValueManager* value_manager_ = nullptr;
}; };
struct Value { // DefinedValue ?? struct Value { // DefinedValue ??
@ -189,9 +186,9 @@ public:
return values_.at(value_id).second; return values_.at(value_id).second;
} }
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) = delete; // TODO bool EqualValues(utils::IdType first_value, utils::IdType second_value) = delete; // TODO
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) = delete; bool AddValueRequirement(utils::IdType value, utils::IdType requrement) = delete;
private: private:
std::vector<std::pair<Value, utils::ValueType>> values_; std::vector<std::pair<Value, utils::ValueType>> values_;

View file

@ -63,11 +63,29 @@ void ExecuteVisitor::Visit(PartitionStatement* node) {
void ExecuteVisitor::Visit(TypeConstructorPatternParameter* node) {} // handled in TypeConstructorPattern void ExecuteVisitor::Visit(TypeConstructorPatternParameter* node) {} // handled in TypeConstructorPattern
// TODO // TODO
// TODO: non-variant constructor patterns
void ExecuteVisitor::Visit(TypeConstructorPattern* node) { void ExecuteVisitor::Visit(TypeConstructorPattern* node) {
// TODO: not only tuples if (!node->constructor->constructor_id_.has_value()) { // checked in typeckeck visitor ??
Visit(node->constructor.get()); // TODO: ?? check is same constructor_id ?? error_handling::HandleRuntimeError("Type constructor pattern constructor name not found", node->base);
for (auto& parameter : node->parameters) { // TODO: pass parameters separately }
Visitor::Visit(parameter.value); // handle TypeConstructorPatternParameter
utils::IdType constructor_id = node->constructor->constructor_id_.value();
// TODO: not only variants
info::value::VariantValue* value_info =
ExtractValue<info::value::VariantValue>(current_value_, node->base);
if (constructor_id != value_info->current_constructor) {
case_matched_ = false;
return;
}
// <--
// extract named parameters ??
for (size_t i = 0; i < node->parameters.size(); ++i) { // not visit if case not matched inside ??
current_value_ = value_info->value.fields[i].second;
Visitor::Visit(node->parameters[i].value);
} }
} }
@ -119,7 +137,7 @@ void ExecuteVisitor::Visit(Match* node) {
utils::ValueType::Tmp); utils::ValueType::Tmp);
} else { } else {
current_value_ = context_manager_.AddValue<info::value::OptionalValue>( current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
info::value::OptionalValue(context_manager_.GetValueManager()), info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
@ -145,7 +163,7 @@ void ExecuteVisitor::Visit(Condition* node) {
Visitor::Visit(node->statements[node->conditions.size()]); Visitor::Visit(node->statements[node->conditions.size()]);
} else { } else {
current_value_ = context_manager_.AddValue<info::value::OptionalValue>( current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
info::value::OptionalValue(context_manager_.GetValueManager()), info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
@ -360,7 +378,7 @@ void ExecuteVisitor::Visit(AccessExpression* node) {
Visitor::Visit(node->id); Visitor::Visit(node->id);
long long index = *ExtractInternalValue<long long>(current_value_, node->base); // TODO: size_t long long index = *ExtractInternalValue<long long>(current_value_, node->base); // TODO: size_t
if (index < 0 || index >= array_value->elements.size()) { if (index < 0 || index >= (long long)array_value->elements.size()) {
error_handling::HandleRuntimeError("Access index out of range", node->base); error_handling::HandleRuntimeError("Access index out of range", node->base);
} }
@ -436,8 +454,14 @@ void ExecuteVisitor::Visit(VariantExpression* node) {
Visitor::Visit(node->expressions[i]); Visitor::Visit(node->expressions[i]);
info::value::OptionalValue* expression_value = ExtractValue<info::value::OptionalValue>(current_value_, node->base); info::value::OptionalValue* expression_value = ExtractValue<info::value::OptionalValue>(current_value_, node->base);
if (expression_value->value.has_value()) { if (expression_value->value.has_value()) {
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields
{{std::nullopt, expression_value->value.value()}};
info::value::TupleValue variant_tuple =
info::value::TupleValue(std::move(fields), context_manager_.GetValueManager());
current_value_ = context_manager_.AddValue<info::value::VariantValue>( current_value_ = context_manager_.AddValue<info::value::VariantValue>(
info::value::VariantValue(expression_value->value.value(), i, context_manager_.GetValueManager()), info::value::VariantValue(std::move(variant_tuple), i),
utils::ValueType::Tmp); utils::ValueType::Tmp);
current_value_ = context_manager_.AddValue<info::value::OptionalValue>( current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
@ -448,7 +472,7 @@ void ExecuteVisitor::Visit(VariantExpression* node) {
} }
current_value_ = context_manager_.AddValue<info::value::OptionalValue>( current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
info::value::OptionalValue(context_manager_.GetValueManager()), info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
@ -581,13 +605,23 @@ void ExecuteVisitor::Visit(VariantName* node) {
for (size_t i = 0; i < node->names.size(); ++i) { for (size_t i = 0; i < node->names.size(); ++i) {
if (i == maybe_variant_value.value()->current_constructor) { if (i == maybe_variant_value.value()->current_constructor) {
// TODO if (fields_count.empty()) ... else ... if (maybe_variant_value.value()->value.fields.empty()) {
current_value_ = context_manager_.AddValue(
info::value::InternalValue(info::value::Unit()),
utils::IsConstModifierToValueType(is_const_definition_.value()));
// TODO: check, that same with typecheck
} else {
current_value_ = context_manager_.AddValue(
maybe_variant_value.value()->value,
utils::IsConstModifierToValueType(is_const_definition_.value()));
}
current_value_ = context_manager_.AddValue( current_value_ = context_manager_.AddValue(
info::value::OptionalValue(maybe_variant_value.value()->value, context_manager_.GetValueManager()), info::value::OptionalValue(current_value_, context_manager_.GetValueManager()),
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ?? utils::IsConstModifierToValueType(is_const_definition_.value()));
} else { } else {
current_value_ = context_manager_.AddValue( current_value_ = context_manager_.AddValue(
info::value::OptionalValue(context_manager_.GetValueManager()), info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
utils::IsConstModifierToValueType(is_const_definition_.value())); utils::IsConstModifierToValueType(is_const_definition_.value()));
} }
@ -722,9 +756,10 @@ void ExecuteVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
break; break;
case 1: case 1:
Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node)); Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node));
if (!context_manager_.EqualValues(current_value_, value)) { // TODO error_handling::HandleInternalError("Unimplemented EqualValues", "ExecuteVisitor.CheckPattern");
case_matched_ = false; // if (!context_manager_.EqualValues(current_value_, value)) { // TODO
} // case_matched_ = false;
// }
break; break;
case 2: case 2:
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get()); Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get());

View file

@ -277,7 +277,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPatternParameter* node) {} // Handle
// TODO // TODO
void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match named constructor void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match named constructor
if (!node->constructor->constructor_id_.has_value()) { if (!node->constructor->constructor_id_.has_value()) {
error_handling::HandleTypecheckError("Type constructor pattern name not found", node->base); error_handling::HandleTypecheckError("Type constructor pattern constructor name not found", node->base);
} }
if (!node->constructor->type_id_.has_value()) { if (!node->constructor->type_id_.has_value()) {

View file

@ -21,7 +21,7 @@ std::optional<utils::IdType> TupleValue::GetFieldValue(const std::string& name)
// //
std::optional<utils::IdType> VariantValue::GetFieldValue(const std::string& name) { std::optional<utils::IdType> VariantValue::GetFieldValue(const std::string& name) {
return value_manager_->GetAnyValue(value)->GetFieldValue(name); // TODO: TupleType ?? return value.GetFieldValue(name);
} }
// //