mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
fixes, part of execute_visitor
This commit is contained in:
parent
802b736e15
commit
6e487c8fd9
6 changed files with 79 additions and 47 deletions
|
|
@ -37,11 +37,11 @@ public:
|
|||
}
|
||||
|
||||
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) {
|
||||
return value_manager_.EqualTypes(first_type, second_type);
|
||||
return value_manager_.EqualValues(first_type, second_type);
|
||||
}
|
||||
|
||||
utils::ValueType GetValueType(utils::IdType value_id) {
|
||||
|
|
|
|||
|
|
@ -319,11 +319,11 @@ public:
|
|||
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));
|
||||
}
|
||||
|
||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
|
||||
bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
|
||||
return *GetAnyValue(requrement) < *GetAnyValue(type);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -42,10 +42,10 @@ public:
|
|||
|
||||
struct TupleValue {
|
||||
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,
|
||||
ValueManager* value_manager)
|
||||
TupleValue(std::vector<std::pair<std::optional<std::string>, utils::IdType>>&& fields,
|
||||
ValueManager* value_manager)
|
||||
: fields(std::move(fields)), value_manager_(value_manager) {}
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct VariantValue {
|
||||
public:
|
||||
explicit VariantValue(ValueManager* value_manager) : value_manager_(value_manager) {}
|
||||
explicit VariantValue() = default;
|
||||
|
||||
// TupleValue ?? // TODO: add type??
|
||||
VariantValue(utils::IdType value, size_t current_constructor, ValueManager* value_manager)
|
||||
: value(value), current_constructor(current_constructor), value_manager_(value_manager) {}
|
||||
VariantValue(TupleValue&& value, size_t current_constructor)
|
||||
: value(std::move(value)), current_constructor(current_constructor) {}
|
||||
|
||||
public:
|
||||
utils::IdType value;
|
||||
TupleValue value;
|
||||
size_t current_constructor;
|
||||
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name);
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
};
|
||||
|
||||
struct ReferenceToValue {
|
||||
public:
|
||||
explicit ReferenceToValue(ValueManager* value_manager) : value_manager_(value_manager) {}
|
||||
ReferenceToValue() = default;
|
||||
|
||||
ReferenceToValue(const std::vector<utils::ReferenceModifier>& references,
|
||||
utils::IdType value,
|
||||
|
|
@ -88,12 +85,12 @@ public:
|
|||
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name);
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionValue {
|
||||
public:
|
||||
explicit FunctionValue(ValueManager* value_manager) : value_manager_(value_manager) {}
|
||||
FunctionValue() = default;
|
||||
|
||||
template<typename T>
|
||||
FunctionValue(const T& function, ValueManager* value_manager)
|
||||
|
|
@ -105,17 +102,17 @@ public:
|
|||
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name);
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
struct ArrayValue {
|
||||
public:
|
||||
explicit ArrayValue(ValueManager* value_manager) : value_manager_(value_manager) {}
|
||||
ArrayValue() = default;
|
||||
|
||||
explicit ArrayValue(std::vector<utils::IdType>&& elements,
|
||||
bool is_constant_size,
|
||||
ValueManager* value_manager)
|
||||
ArrayValue(std::vector<utils::IdType>&& elements,
|
||||
bool is_constant_size,
|
||||
ValueManager* value_manager)
|
||||
: elements(std::move(elements)), is_constant_size(is_constant_size), value_manager_(value_manager) {}
|
||||
|
||||
public:
|
||||
|
|
@ -124,14 +121,14 @@ public:
|
|||
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name);
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct OptionalValue {
|
||||
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) {}
|
||||
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name);
|
||||
|
|
@ -139,7 +136,7 @@ public:
|
|||
std::optional<utils::IdType> value;
|
||||
|
||||
private:
|
||||
ValueManager* value_manager_;
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct Value { // DefinedValue ??
|
||||
|
|
@ -189,9 +186,9 @@ public:
|
|||
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:
|
||||
std::vector<std::pair<Value, utils::ValueType>> values_;
|
||||
|
|
|
|||
|
|
@ -63,11 +63,29 @@ void ExecuteVisitor::Visit(PartitionStatement* node) {
|
|||
void ExecuteVisitor::Visit(TypeConstructorPatternParameter* node) {} // handled in TypeConstructorPattern
|
||||
|
||||
// TODO
|
||||
// TODO: non-variant constructor patterns
|
||||
void ExecuteVisitor::Visit(TypeConstructorPattern* node) {
|
||||
// TODO: not only tuples
|
||||
Visit(node->constructor.get()); // TODO: ?? check is same constructor_id ??
|
||||
for (auto& parameter : node->parameters) { // TODO: pass parameters separately
|
||||
Visitor::Visit(parameter.value); // handle TypeConstructorPatternParameter
|
||||
if (!node->constructor->constructor_id_.has_value()) { // checked in typeckeck visitor ??
|
||||
error_handling::HandleRuntimeError("Type constructor pattern constructor name not found", node->base);
|
||||
}
|
||||
|
||||
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);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -145,7 +163,7 @@ void ExecuteVisitor::Visit(Condition* node) {
|
|||
Visitor::Visit(node->statements[node->conditions.size()]);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -360,7 +378,7 @@ void ExecuteVisitor::Visit(AccessExpression* node) {
|
|||
Visitor::Visit(node->id);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -436,8 +454,14 @@ void ExecuteVisitor::Visit(VariantExpression* node) {
|
|||
Visitor::Visit(node->expressions[i]);
|
||||
info::value::OptionalValue* expression_value = ExtractValue<info::value::OptionalValue>(current_value_, node->base);
|
||||
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>(
|
||||
info::value::VariantValue(expression_value->value.value(), i, context_manager_.GetValueManager()),
|
||||
info::value::VariantValue(std::move(variant_tuple), i),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
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>(
|
||||
info::value::OptionalValue(context_manager_.GetValueManager()),
|
||||
info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
|
|
@ -581,13 +605,23 @@ void ExecuteVisitor::Visit(VariantName* node) {
|
|||
|
||||
for (size_t i = 0; i < node->names.size(); ++i) {
|
||||
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(
|
||||
info::value::OptionalValue(maybe_variant_value.value()->value, context_manager_.GetValueManager()),
|
||||
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
|
||||
info::value::OptionalValue(current_value_, context_manager_.GetValueManager()),
|
||||
utils::IsConstModifierToValueType(is_const_definition_.value()));
|
||||
} else {
|
||||
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()));
|
||||
}
|
||||
|
||||
|
|
@ -722,9 +756,10 @@ void ExecuteVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
|
|||
break;
|
||||
case 1:
|
||||
Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||
if (!context_manager_.EqualValues(current_value_, value)) { // TODO
|
||||
case_matched_ = false;
|
||||
}
|
||||
error_handling::HandleInternalError("Unimplemented EqualValues", "ExecuteVisitor.CheckPattern");
|
||||
// if (!context_manager_.EqualValues(current_value_, value)) { // TODO
|
||||
// case_matched_ = false;
|
||||
// }
|
||||
break;
|
||||
case 2:
|
||||
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get());
|
||||
|
|
|
|||
|
|
@ -277,7 +277,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPatternParameter* node) {} // Handle
|
|||
// TODO
|
||||
void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match named constructor
|
||||
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()) {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ std::optional<utils::IdType> TupleValue::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);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue