mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
execute_visitor refactoring, type_constructor & type_constructor_patter fixes
This commit is contained in:
parent
f6de515b2c
commit
273ac960fd
2 changed files with 73 additions and 49 deletions
|
|
@ -21,6 +21,8 @@ public:
|
|||
info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager,
|
||||
interpreter::tokens::PartitionStatement* execution_root)
|
||||
: namespace_visitor_(global_info.CreateVisitor()),
|
||||
global_info_(global_info),
|
||||
typeclass_graph_(*global_info.GetTypeclassGraph()),
|
||||
type_context_manager_(type_context_manager),
|
||||
context_manager_(context_manager) {}
|
||||
|
||||
|
|
@ -159,6 +161,8 @@ private:
|
|||
}
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::GlobalInfo& global_info_;
|
||||
info::TypeclassGraph& typeclass_graph_;
|
||||
info::ContextManager<info::type::Type, info::type::TypeManager>& type_context_manager_;
|
||||
info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager_;
|
||||
|
||||
|
|
|
|||
|
|
@ -62,9 +62,7 @@ void ExecuteVisitor::Visit(PartitionStatement* node) {
|
|||
|
||||
void ExecuteVisitor::Visit(TypeConstructorPatternParameter* node) {} // handled in TypeConstructorPattern
|
||||
|
||||
// TODO
|
||||
// TODO: tuples
|
||||
// TODO: non-variant constructor patterns
|
||||
// TODO: check
|
||||
void ExecuteVisitor::Visit(TypeConstructorPattern* node) {
|
||||
if (!node->constructor->constructor_id_.has_value()) { // checked in typeckeck visitor ??
|
||||
error_handling::HandleRuntimeError("Type constructor pattern constructor name not found", node->base);
|
||||
|
|
@ -72,21 +70,33 @@ void ExecuteVisitor::Visit(TypeConstructorPattern* node) {
|
|||
|
||||
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);
|
||||
// only one has value inside
|
||||
auto maybe_variant_value_info = context_manager_.GetValue<info::value::VariantValue>(current_value_);
|
||||
auto maybe_tuple_value_info = context_manager_.GetValue<info::value::TupleValue>(current_value_);
|
||||
|
||||
if (constructor_id != value_info->current_constructor) {
|
||||
case_matched_ = false;
|
||||
return;
|
||||
}
|
||||
if (maybe_variant_value_info.has_value()) {
|
||||
info::value::VariantValue* variant_value_info = maybe_variant_value_info.value();
|
||||
|
||||
// <--
|
||||
if (constructor_id != variant_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);
|
||||
// extract named parameters ??
|
||||
for (size_t i = 0; i < node->parameters.size(); ++i) { // not visit if case not matched inside ??
|
||||
current_value_ = variant_value_info->value.fields[i].second;
|
||||
Visitor::Visit(node->parameters[i].value);
|
||||
}
|
||||
} else if (maybe_tuple_value_info.has_value()) {
|
||||
info::value::TupleValue* tuple_value_info = maybe_tuple_value_info.value();
|
||||
|
||||
// extract named parameters ??
|
||||
for (size_t i = 0; i < node->parameters.size(); ++i) { // not visit if case not matched inside ??
|
||||
current_value_ = tuple_value_info->fields[i].second;
|
||||
Visitor::Visit(node->parameters[i].value);
|
||||
}
|
||||
} else {
|
||||
error_handling::HandleRuntimeError("Wrong value type for type constructor pattern (not variant or tuple)", node->base);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -134,11 +144,11 @@ void ExecuteVisitor::Visit(Match* node) {
|
|||
|
||||
// --- instead of optional return value or throw runtime error ---
|
||||
// if (statement_visited) {
|
||||
// current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
// current_value_ = context_manager_.AddValue(
|
||||
// info::value::OptionalValue(current_value_, context_manager_.GetValueManager()),
|
||||
// utils::ValueType::Tmp);
|
||||
// } else {
|
||||
// current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
// current_value_ = context_manager_.AddValue(
|
||||
// info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
|
||||
// utils::ValueType::Tmp);
|
||||
// }
|
||||
|
|
@ -157,7 +167,7 @@ void ExecuteVisitor::Visit(Condition* node) {
|
|||
Visitor::Visit(node->statements[i]);
|
||||
|
||||
if (node->statements.size() == node->conditions.size()) {
|
||||
current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::OptionalValue(current_value_, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp); // take value type from current_value_ ??
|
||||
}
|
||||
|
|
@ -167,7 +177,7 @@ void ExecuteVisitor::Visit(Condition* node) {
|
|||
if (node->statements.size() > node->conditions.size()) {
|
||||
Visitor::Visit(node->statements[node->conditions.size()]);
|
||||
} else {
|
||||
current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -193,7 +203,7 @@ void ExecuteVisitor::Visit(DoWhileLoop* node) {
|
|||
result.push_back(current_value_);
|
||||
} while(HandleCondition(node->condition, node->base));
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ArrayValue(std::move(result), false, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
|
|
@ -218,7 +228,7 @@ void ExecuteVisitor::Visit(WhileLoop* node) {
|
|||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ArrayValue(std::move(result), false, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
|
|
@ -250,7 +260,7 @@ void ExecuteVisitor::Visit(ForLoop* node) {
|
|||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ArrayValue(std::move(result), false, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
|
|
@ -275,7 +285,7 @@ void ExecuteVisitor::Visit(LoopLoop* node) {
|
|||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ArrayValue(std::move(result), false, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
|
|
@ -298,7 +308,7 @@ void ExecuteVisitor::Visit(Block* node) {
|
|||
}
|
||||
context_manager_.ExitContext();
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -321,7 +331,7 @@ void ExecuteVisitor::Visit(LoopControlExpression& node) {
|
|||
// void ExecuteVisitor::Visit(BinaryOperatorExpression* node) {
|
||||
// context_manager_.EnterContext();
|
||||
//
|
||||
// auto maybe_function_definition = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_).definition;
|
||||
// auto maybe_function_definition = global_info_.GetFunctionInfo(node->function_id_).definition;
|
||||
//
|
||||
// if (maybe_function_definition.has_value()) {
|
||||
// Visitor::Visit(node->left_expression);
|
||||
|
|
@ -399,7 +409,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
// TODO: typeclass functions
|
||||
auto function_declaration = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_.value()).declaration.value(); // checked in type_check_visitor
|
||||
auto function_declaration = global_info_.GetFunctionInfo(node->function_id_.value()).declaration.value(); // checked in type_check_visitor
|
||||
|
||||
for (size_t i = 0; i < node->parameters.size(); ++i) {
|
||||
// TODO: local abstract types, absract types, etc.
|
||||
|
|
@ -408,7 +418,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
// TODO: typeclass functions
|
||||
auto maybe_function_definition = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_.value()).definition;
|
||||
auto maybe_function_definition = global_info_.GetFunctionInfo(node->function_id_.value()).definition;
|
||||
|
||||
if (maybe_function_definition.has_value()) {
|
||||
for (size_t i = 0; i < node->arguments.size(); ++i) {
|
||||
|
|
@ -435,7 +445,7 @@ void ExecuteVisitor::Visit(TupleExpression* node) {
|
|||
fields.push_back({std::nullopt, current_value_});
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::TupleValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::TupleValue(std::move(fields), context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -452,18 +462,18 @@ void ExecuteVisitor::Visit(VariantExpression* node) {
|
|||
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(std::move(variant_tuple), i),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::OptionalValue(current_value_, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::OptionalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::OptionalValue(std::nullopt, context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -476,11 +486,17 @@ void ExecuteVisitor::Visit(ReturnExpression* node) {
|
|||
|
||||
void ExecuteVisitor::Visit(TypeConstructorParameter* node) {} // handled in TypeConstructor
|
||||
|
||||
// TODO: variants, etc.
|
||||
// TODO: check
|
||||
void ExecuteVisitor::Visit(TypeConstructor* node) {
|
||||
// TODO: support for non-tuples
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
|
||||
|
||||
if (!node->constructor->constructor_id_.has_value()) {
|
||||
error_handling::HandleRuntimeError("Type constructor name not found", node->base);
|
||||
}
|
||||
|
||||
utils::IdType constructor_id = node->constructor->constructor_id_.value();
|
||||
info::definition::Constructor constructor_info = global_info_.GetConstructorInfo(constructor_id);
|
||||
|
||||
// Visit(node->constructor.get()); // use parameters from type expression ??
|
||||
fields.reserve(node->parameters.size());
|
||||
for (auto& parameter : node->parameters) {
|
||||
|
|
@ -491,13 +507,17 @@ void ExecuteVisitor::Visit(TypeConstructor* node) {
|
|||
current_value_ });
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::TupleValue>(
|
||||
info::value::TupleValue(std::move(fields), context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
|
||||
// if (constructor_info.order.has_value()) {
|
||||
// // TODO: construct variant value
|
||||
// }
|
||||
if (constructor_info.order.has_value()) { // => variant
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::VariantValue(
|
||||
info::value::TupleValue(std::move(fields), context_manager_.GetValueManager()),
|
||||
constructor_info.order.value()),
|
||||
utils::ValueType::Tmp);
|
||||
} else { // => tuple
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::TupleValue(std::move(fields), context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
|
@ -515,7 +535,7 @@ void ExecuteVisitor::Visit(ArrayExpression* node) {
|
|||
elements.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ArrayValue(std::move(elements), true, context_manager_.GetValueManager()), // maybe size not fixed??
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -674,37 +694,37 @@ void ExecuteVisitor::Visit(ParametrizedType* node) {} // no value
|
|||
// void ExecuteVisitor::Visit(std::string* node) {} // std::string
|
||||
|
||||
void ExecuteVisitor::Visit(FloatNumberLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(NumberLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(StringLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(CharLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(UnitLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(BoolLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
@ -722,7 +742,7 @@ void ExecuteVisitor::CollectTypeContext(const ParametrizedType& type) {
|
|||
return;
|
||||
}
|
||||
|
||||
auto maybe_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type.type_id_.value());
|
||||
auto maybe_type_info = global_info_.GetTypeInfo<info::definition::AnyType>(type.type_id_.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) {
|
||||
error_handling::HandleInternalError("CollectTypeContext unimplemented anything except AnyType", "ExecuteVisitor.CollectTYpeContext");
|
||||
|
|
@ -752,7 +772,7 @@ 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
|
||||
if (!context_manager_.EqualValues(current_value_, value)) {
|
||||
case_matched_ = false;
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue