mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-07 23:48:43 +00:00
part of execute_visitor, minor fixes, function & operator fixes
This commit is contained in:
parent
d31979166e
commit
78de51f6f2
6 changed files with 345 additions and 247 deletions
|
|
@ -1,5 +1,8 @@
|
|||
// for clangd
|
||||
#include "../include/execute_visitor.hpp"
|
||||
#include <cwchar>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
|
||||
// TODO
|
||||
|
||||
|
|
@ -7,7 +10,7 @@ namespace interpreter {
|
|||
|
||||
// Sources -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(SourceFile* node) {
|
||||
void ExecuteVisitor::Visit(SourceFile* node) { // never used ??
|
||||
for (auto& statement : node->statements) {
|
||||
Visitor::Visit(statement);
|
||||
}
|
||||
|
|
@ -15,97 +18,48 @@ void ExecuteVisitor::Visit(SourceFile* node) {
|
|||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(NamespaceSources* node) {
|
||||
void ExecuteVisitor::Visit(NamespaceSources* node) { // never used ??
|
||||
for (auto& statement : node->statements) {
|
||||
Visitor::Visit(statement);
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(Namespace* node) {
|
||||
Visit(&node->type);
|
||||
void ExecuteVisitor::Visit(Namespace* node) { // never used ??
|
||||
// Visit(&node->type);
|
||||
Visit(&node->scope);
|
||||
}
|
||||
|
||||
// Definitions -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(ImportStatement* node) {
|
||||
for (auto& symbol : node->symbols) {
|
||||
Visit(&symbol);
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(AliasDefinitionStatement* node) {
|
||||
Visit(&node->type);
|
||||
Visit(node->value.get());
|
||||
}
|
||||
void ExecuteVisitor::Visit(ImportStatement* node) {} // no value
|
||||
void ExecuteVisitor::Visit(AliasDefinitionStatement* node) {} // no value
|
||||
|
||||
void ExecuteVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
Visitor::Visit(node->name);
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(FunctionDeclaration* node) {
|
||||
Visit(&node->name);
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
Visit(node->type.get());
|
||||
}
|
||||
void ExecuteVisitor::Visit(FunctionDeclaration* node) {} // no value
|
||||
|
||||
void ExecuteVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||
Visit(node->definition.get());
|
||||
// Visit(node->definition.get());
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TypeDefinitionStatement* node) {
|
||||
Visit(node->definition.get());
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
void ExecuteVisitor::Visit(TypeDefinitionStatement* node) {} // no value
|
||||
void ExecuteVisitor::Visit(AbstractTypeDefinitionStatement* node) {} // no value
|
||||
|
||||
void ExecuteVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||
Visit(node->type.get());
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||
Visit(node->definition.get());
|
||||
for (auto& requirement : node->requirements) {
|
||||
Visit(requirement.get());
|
||||
}
|
||||
}
|
||||
void ExecuteVisitor::Visit(TypeclassDefinitionStatement* node) {} // no value
|
||||
|
||||
void ExecuteVisitor::Visit(PartitionStatement* node) {
|
||||
Visit(&node->name);
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
|
||||
// Definition parts
|
||||
|
||||
void ExecuteVisitor::Visit(FunctionDefinition* node) {
|
||||
Visit(&node->name);
|
||||
for (auto& argument : node->arguments) {
|
||||
Visit(&argument);
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TypeDefinition* node) {
|
||||
Visit(node->type.get());
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(AnyAnnotatedType* node) {
|
||||
Visit(&node->type);
|
||||
for (auto& typeclass : node->typeclasses) {
|
||||
Visit(typeclass.get());
|
||||
}
|
||||
}
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(TypeConstructorPatternParameter* node) {
|
||||
if (node->name.has_value()) {
|
||||
Visit(&node->name.value());
|
||||
// Visit(&node->name.value());
|
||||
}
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
|
|
@ -145,37 +99,122 @@ void ExecuteVisitor::Visit(Condition* node) {
|
|||
}
|
||||
|
||||
void ExecuteVisitor::Visit(DoWhileLoop* node) {
|
||||
Visitor::Visit(node->statement);
|
||||
Visitor::Visit(node->condition);
|
||||
std::vector<utils::IdType> result;
|
||||
do {
|
||||
Visitor::Visit(node->statement);
|
||||
if (active_loop_control_expression_.has_value()) {
|
||||
if (active_loop_control_expression_.value() == LoopControlExpression::Break) {
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
break;
|
||||
}
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
continue;
|
||||
|
||||
}
|
||||
result.push_back(current_value_);
|
||||
} while(HandleCondition(node->condition, node->base));
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
info::value::ArrayValue(result, false),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(WhileLoop* node) {
|
||||
Visitor::Visit(node->condition);
|
||||
Visitor::Visit(node->statement);
|
||||
std::vector<utils::IdType> result;
|
||||
while(HandleCondition(node->condition, node->base)) {
|
||||
Visitor::Visit(node->statement);
|
||||
if (active_loop_control_expression_.has_value()) {
|
||||
if (active_loop_control_expression_.value() == LoopControlExpression::Break) {
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
break;
|
||||
}
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
continue;
|
||||
|
||||
}
|
||||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
info::value::ArrayValue(result, false),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
void ExecuteVisitor::Visit(ForLoop* node) {
|
||||
Visitor::Visit(node->variable);
|
||||
std::vector<utils::IdType> result;
|
||||
|
||||
// TODO: extend on different interval types (not only array)
|
||||
Visitor::Visit(node->interval);
|
||||
Visitor::Visit(node->statement);
|
||||
info::value::ArrayValue* interval = ExtractValue<info::value::ArrayValue>(current_value_, node->base);
|
||||
|
||||
for (auto& value : interval->elements) {
|
||||
current_value_ = value;
|
||||
Visitor::Visit(node->variable);
|
||||
|
||||
Visitor::Visit(node->statement);
|
||||
if (active_loop_control_expression_.has_value()) {
|
||||
if (active_loop_control_expression_.value() == LoopControlExpression::Break) {
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
break;
|
||||
}
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
continue;
|
||||
|
||||
}
|
||||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
info::value::ArrayValue(result, false),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(LoopLoop* node) {
|
||||
Visitor::Visit(node->statement);
|
||||
std::vector<utils::IdType> result;
|
||||
while(true) {
|
||||
Visitor::Visit(node->statement);
|
||||
if (active_loop_control_expression_.has_value()) {
|
||||
if (active_loop_control_expression_.value() == LoopControlExpression::Break) {
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
break;
|
||||
}
|
||||
active_loop_control_expression_ = std::nullopt;
|
||||
continue;
|
||||
|
||||
}
|
||||
result.push_back(current_value_);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::ArrayValue>(
|
||||
info::value::ArrayValue(result, false),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(Block* node) {
|
||||
for (auto& statement : node->statements) {
|
||||
return_value_ = std::nullopt;
|
||||
Visitor::Visit(statement);
|
||||
if (return_value_.has_value()) {
|
||||
current_value_ = return_value_.value();
|
||||
return;
|
||||
}
|
||||
}
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ScopedStatement* node) {
|
||||
Visitor::Visit(node->statement);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(LoopControlExpression& node) {} // enum
|
||||
void ExecuteVisitor::Visit(LoopControlExpression& node) {
|
||||
active_loop_control_expression_ = node;
|
||||
|
||||
current_value_ = context_manager_.AddValue(info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp); // ??
|
||||
}
|
||||
|
||||
// Operators
|
||||
|
||||
|
|
@ -186,17 +225,35 @@ void ExecuteVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
}
|
||||
|
||||
void ExecuteVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
Visit(&node->operator_name);
|
||||
// Visit(&node->operator_name);
|
||||
Visitor::Visit(node->expression);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ReferenceExpression* node) {
|
||||
// TODO: check, that therie is no references to "Tmp"
|
||||
Visit(node->expression.get());
|
||||
|
||||
utils::ValueType value_type = context_manager_.GetValueType(current_value_);
|
||||
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::ReferenceToValue(node->references, current_value_),
|
||||
value_type);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(AccessExpression* node) {
|
||||
// TODO: extend to other types
|
||||
Visit(node->name.get());
|
||||
info::value::ArrayValue* array_value = ExtractValue<info::value::ArrayValue>(current_value_, node->base);
|
||||
utils::ValueType value_type = context_manager_.GetValueType(current_value_);
|
||||
|
||||
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()) {
|
||||
error_handling::HandleRuntimeError("Access index out of range", node->base);
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.ToModifiedValue(array_value->elements[index], value_type); // needed ??
|
||||
}
|
||||
|
||||
// Other Expressions
|
||||
|
|
@ -212,7 +269,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
}
|
||||
|
||||
Visit(&node->name);
|
||||
// Visit(&node->name);
|
||||
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
|
|
@ -224,24 +281,37 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TupleExpression* node) {
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
|
||||
fields.reserve(node->expressions.size());
|
||||
for (auto& expression : node->expressions) {
|
||||
Visitor::Visit(expression);
|
||||
fields.push_back({std::nullopt, current_value_});
|
||||
}
|
||||
|
||||
current_value_ = context_manager_.AddValue<info::value::TupleValue>(info::value::TupleValue(std::move(fields)),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(VariantExpression* node) {
|
||||
// TODO
|
||||
void ExecuteVisitor::Visit(VariantExpression* node) { // ??
|
||||
for (auto& expression : node->expressions) {
|
||||
Visitor::Visit(expression);
|
||||
info::value::OptionalValue* expression_value = ExtractValue<info::value::OptionalValue>(current_value_, node->base);
|
||||
if (expression_value->value.has_value()) {
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ReturnExpression* node) {
|
||||
Visitor::Visit(node->expression);
|
||||
return_value_ = current_value_;
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TypeConstructorParameter* node) {
|
||||
if (node->name.has_value()) {
|
||||
Visit(&node->name.value());
|
||||
// Visit(&node->name.value());
|
||||
}
|
||||
Visitor::Visit(node->value);
|
||||
}
|
||||
|
|
@ -253,14 +323,10 @@ void ExecuteVisitor::Visit(TypeConstructor* node) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
void ExecuteVisitor::Visit(LambdaFunction* node) {
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
for (auto& argument : node->arguments) {
|
||||
Visit(&argument);
|
||||
}
|
||||
Visitor::Visit(node->expression);
|
||||
error_handling::HandleInternalError("Lambda function are not implemented yet",
|
||||
"ExecuteVisitor.LambdaFunction");
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ArrayExpression* node) {
|
||||
|
|
@ -271,17 +337,9 @@ void ExecuteVisitor::Visit(ArrayExpression* node) {
|
|||
|
||||
// Name
|
||||
|
||||
void ExecuteVisitor::Visit(PartitionName* node) {
|
||||
for (auto& path_namespace : node->path) {
|
||||
Visit(&path_namespace);
|
||||
}
|
||||
|
||||
Visit(&node->name);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(NameExpression* node) {
|
||||
for (auto& name : node->names) {
|
||||
Visit(&name);
|
||||
// Visit(&name);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -298,7 +356,7 @@ void ExecuteVisitor::Visit(VariantName* node) {
|
|||
}
|
||||
|
||||
void ExecuteVisitor::Visit(AnnotatedName* node) {
|
||||
Visit(&node->name);
|
||||
// Visit(&node->name);
|
||||
if (node->type.has_value()) {
|
||||
Visitor::Visit(node->type.value());
|
||||
}
|
||||
|
|
@ -308,85 +366,68 @@ void ExecuteVisitor::Visit(AnnotatedName* node) {
|
|||
|
||||
// Type
|
||||
|
||||
void ExecuteVisitor::Visit(FunctionType* node) {
|
||||
for (auto& type : node->types) {
|
||||
Visitor::Visit(type);
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TupleType* node) {
|
||||
if (node->type.has_value()) {
|
||||
Visit(&node->type.value());
|
||||
}
|
||||
for (auto& entity : node->entities) {
|
||||
if (entity.first.has_value()) {
|
||||
Visit(&entity.first.value());
|
||||
}
|
||||
Visit(entity.second.get());
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(VariantType* node) {
|
||||
if (node->type.has_value()) {
|
||||
Visit(&node->type.value());
|
||||
}
|
||||
for (auto& constructor : node->constructors) {
|
||||
if (std::holds_alternative<Constructor>(constructor)) {
|
||||
Visit(&std::get<Constructor>(constructor));
|
||||
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
|
||||
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(TypeExpression* node) {
|
||||
for (auto& type : node->path) {
|
||||
Visit(&type);
|
||||
}
|
||||
Visit(&node->type);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ExtendedScopedAnyType* node) {
|
||||
Visitor::Visit(node->type);
|
||||
}
|
||||
void ExecuteVisitor::Visit(FunctionType* node) {} // no value
|
||||
void ExecuteVisitor::Visit(TupleType* node) {} // no value
|
||||
void ExecuteVisitor::Visit(VariantType* node) {} // no value
|
||||
void ExecuteVisitor::Visit(TypeExpression* node) {} // no value
|
||||
void ExecuteVisitor::Visit(ExtendedScopedAnyType* node) {} // no value
|
||||
|
||||
// Typeclass
|
||||
|
||||
void ExecuteVisitor::Visit(ParametrizedTypeclass* node) {
|
||||
Visit(&node->typeclass);
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
}
|
||||
void ExecuteVisitor::Visit(ParametrizedTypeclass* node) {} // no value
|
||||
|
||||
// Typeclass & Type -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(ParametrizedType* node) {
|
||||
Visit(&node->type);
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
}
|
||||
void ExecuteVisitor::Visit(ParametrizedType* node) {} // no value
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void ExecuteVisitor::Visit(ExtendedName* node) {}
|
||||
// void ExecuteVisitor::Visit(ExtendedName* node) {}
|
||||
|
||||
void ExecuteVisitor::Visit(std::string* node) {} // std::string
|
||||
// void ExecuteVisitor::Visit(std::string* node) {} // std::string
|
||||
|
||||
void ExecuteVisitor::Visit(FloatNumberLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(FloatNumberLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(NumberLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(NumberLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(StringLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(StringLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(CharLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(CharLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(UnitLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(UnitLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(BoolLiteral* node) {}
|
||||
void ExecuteVisitor::Visit(BoolLiteral* node) {
|
||||
current_value_ = context_manager_.AddValue<info::value::InternalValue>(
|
||||
info::value::InternalValue(node->value),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
bool ExecuteVisitor::HandleCondition(Expression& node, const BaseNode& base_node) {
|
||||
Visitor::Visit(node);
|
||||
return *ExtractInternalValue<bool>(current_value_, base_node);
|
||||
}
|
||||
|
||||
} // namespace interpreter
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue