mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-26 16:58:45 +00:00
extended name removed, dereference added, unary operators removed, fixes
This commit is contained in:
parent
e62144feac
commit
79bd30c1ee
20 changed files with 101 additions and 274 deletions
|
|
@ -1,10 +1,12 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
// for clangd
|
||||
#include "../include/build_visitor.hpp"
|
||||
#include "../include/parse_token_types.hpp"
|
||||
#include "../include/error_handling.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
|
|
@ -178,7 +180,7 @@ void BuildVisitor::Visit(FunctionDeclaration* node) {
|
|||
node->is_in_interface = false;
|
||||
}
|
||||
|
||||
node->name.name = parse_node.ChildByFieldName("name").GetValue();
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
|
|
@ -371,21 +373,14 @@ void BuildVisitor::Visit(FunctionDefinition* node) {
|
|||
|
||||
auto parse_node = current_node_;
|
||||
|
||||
node->name.name = parse_node.ChildByFieldName("name").GetValue();
|
||||
|
||||
if (parse_node.NthChild(0).GetValue() == "(") {
|
||||
node->modifier = utils::FunctionTypeModifier::Operator;
|
||||
} else {
|
||||
node->modifier = utils::FunctionTypeModifier::Function;
|
||||
}
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
node->arguments.resize(child_count - 1);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(&node->arguments[i]);
|
||||
node->arguments[i] = parse_node.NthNamedChild(i + 1).GetValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -446,10 +441,7 @@ void BuildVisitor::Visit(TypeConstructorPatternParameter* node) {
|
|||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
current_node_ = parse_node.ChildByFieldName("name"),
|
||||
|
||||
node->name.emplace();
|
||||
Visit(&node->name.value());
|
||||
node->name.value() = parse_node.ChildByFieldName("name").GetValue();
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("value"),
|
||||
|
|
@ -489,9 +481,9 @@ void BuildVisitor::Visit(Pattern& node) { // <-> ScopedPattern
|
|||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (current_node_type == parser::tokens::ExtendedName) { // optimize ??
|
||||
node = std::make_unique<ExtendedName>();
|
||||
Visit(std::get<std::unique_ptr<ExtendedName>>(node).get());
|
||||
if (current_node_type == parser::tokens::NameIdentifier) { // optimize ??
|
||||
node = std::make_unique<NameIdentifier>(current_node_.GetValue());
|
||||
// Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::Literal) {
|
||||
node = std::make_unique<Literal>();
|
||||
Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||
|
|
@ -807,9 +799,6 @@ void BuildVisitor::Visit(Expression& node) {
|
|||
} else if (current_node_type == parser::tokens::PrefixedExpression) {
|
||||
node = std::make_unique<PrefixedExpression>();
|
||||
Visit(*std::get<std::unique_ptr<PrefixedExpression>>(node));
|
||||
} else if (current_node_type == parser::tokens::UnaryOperatorExpression) {
|
||||
node = std::make_unique<UnaryOperatorExpression>();
|
||||
Visit(std::get<std::unique_ptr<UnaryOperatorExpression>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::SubExpression) {
|
||||
node = std::make_unique<SubExpression>();
|
||||
Visit(*std::get<std::unique_ptr<SubExpression>>(node));
|
||||
|
|
@ -872,28 +861,29 @@ void BuildVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
|
||||
node->operator_name = parse_node.ChildByFieldName("operator_name").GetValue();
|
||||
|
||||
{ // remove operator prescendence markers
|
||||
{ // remove operator precedence markers
|
||||
size_t operator_size = 0;
|
||||
for (; operator_size < node->operator_name.size() && node->operator_name[operator_size] != '.'; ++operator_size) {}
|
||||
|
||||
node->precedence = utils::MaxOperatorPrecedence - (node->operator_name.size() - operator_size);
|
||||
|
||||
node->operator_name = node->operator_name.substr(0, operator_size);
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("right_expression");
|
||||
Visit(node->right_expression);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
// ??
|
||||
if (std::holds_alternative<std::unique_ptr<BinaryOperatorExpression>>(node->left_expression)
|
||||
&& std::get<std::unique_ptr<BinaryOperatorExpression>>(node->left_expression)->precedence >= node->precedence) {
|
||||
error_handling::HandleParsingError("Operators can't be chained", node->base.start_position, node->base.end_position);
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
SetPosition(node->base, current_node_);
|
||||
|
||||
auto parse_node = current_node_;
|
||||
|
||||
node->operator_name = parse_node.ChildByFieldName("operator_name").GetValue();
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("expression");
|
||||
Visit(node->expression);
|
||||
// ??
|
||||
if (std::holds_alternative<std::unique_ptr<BinaryOperatorExpression>>(node->right_expression)
|
||||
&& std::get<std::unique_ptr<BinaryOperatorExpression>>(node->right_expression)->precedence >= node->precedence) {
|
||||
error_handling::HandleParsingError("Operators can't be chained", node->base.start_position, node->base.end_position);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
|
@ -909,10 +899,10 @@ void BuildVisitor::Visit(ReferenceExpression* node) {
|
|||
node->references.resize(child_count - 1);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
std::string reference = parse_node.NthChild(i).GetValue();
|
||||
if (reference == "~") {
|
||||
if (reference == "^") {
|
||||
node->references[i] = utils::ReferenceModifier::Reference;
|
||||
} else if (reference == "@") {
|
||||
node->references[i] = utils::ReferenceModifier::UniqueReference;
|
||||
} else if (reference == "~") {
|
||||
node->references[i] = utils::ReferenceModifier::Dereference;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -964,8 +954,7 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
|
|||
// no prefix
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("name");
|
||||
Visit(&node->name);
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||
++excluded_child_count;
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
|
@ -1046,9 +1035,7 @@ void BuildVisitor::Visit(TypeConstructorParameter* node) {
|
|||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
current_node_ = parse_node.ChildByFieldName("name");
|
||||
node->name.emplace();
|
||||
Visit(&node->name.value());
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||
|
||||
std::string assignment_modifier = current_node_.NextSibling().GetValue();
|
||||
if (assignment_modifier == "=") {
|
||||
|
|
@ -1109,7 +1096,7 @@ void BuildVisitor::Visit(LambdaFunction* node) {
|
|||
Visit(node->parameters.back().get());
|
||||
} else {
|
||||
node->arguments.emplace_back();
|
||||
Visit(&node->arguments.back());
|
||||
node->arguments.back() = current_node_.GetValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1179,8 +1166,7 @@ void BuildVisitor::Visit(NameExpression* node) {
|
|||
|
||||
node->names.resize(child_count);
|
||||
for (size_t i = 0; i < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(&node->names[i]);
|
||||
node->names[i] = parse_node.NthNamedChild(i).GetValue();
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -1301,9 +1287,8 @@ void BuildVisitor::Visit(TupleType* node) {
|
|||
while (current_node_n < parse_node.NamedChildCount()) {
|
||||
node->entities.emplace_back();
|
||||
|
||||
if (current_node_.GetType() == parser::tokens::ExtendedName) {
|
||||
node->entities.back().first.emplace();
|
||||
Visit(&node->entities.back().first.value());
|
||||
if (current_node_.GetType() == parser::tokens::NameIdentifier) {
|
||||
node->entities.back().first = current_node_.GetValue();
|
||||
|
||||
++current_node_n;
|
||||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
|
@ -1432,7 +1417,7 @@ void BuildVisitor::Visit(ExtendedScopedAnyType* node) {
|
|||
node->references.resize(child_count - 1);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
std::string reference = parse_node.NthChild(i).GetValue();
|
||||
if (reference == "~") {
|
||||
if (reference == "^") {
|
||||
node->references[i] = utils::ReferenceModifier::Reference;
|
||||
} else if (reference == "@") {
|
||||
node->references[i] = utils::ReferenceModifier::UniqueReference;
|
||||
|
|
@ -1498,12 +1483,6 @@ void BuildVisitor::Visit(ParametrizedType* node) {
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void BuildVisitor::Visit(ExtendedName* node) {
|
||||
SetPosition(node->base, current_node_);
|
||||
|
||||
node->name = current_node_.GetValue();
|
||||
}
|
||||
|
||||
// void BuildVisitor::Visit(AnyIdentifier* node) { // std::string
|
||||
// *node = current_node_.GetValue();
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -337,27 +337,6 @@ void ExecuteVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
context_manager_.ExitContext();
|
||||
}
|
||||
|
||||
// TODO: methods??
|
||||
void ExecuteVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
context_manager_.EnterContext();
|
||||
|
||||
auto maybe_function_definition = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_).definition;
|
||||
|
||||
if (maybe_function_definition.has_value()) {
|
||||
Visitor::Visit(node->expression);
|
||||
// TODO: custom argument value types, references, etc.
|
||||
current_value_ = context_manager_.ToModifiedValue(current_value_, utils::ValueType::Const);
|
||||
context_manager_.DefineVariable(maybe_function_definition.value().argument_names[0], current_value_);
|
||||
|
||||
Visit(maybe_function_definition.value().node);
|
||||
} else {
|
||||
// TODO: builtin operators, etc. (imports?)
|
||||
error_handling::HandleRuntimeError("Unary operator definition not found", node->base);
|
||||
}
|
||||
|
||||
context_manager_.ExitContext();
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(ReferenceExpression* node) {
|
||||
// TODO: check, that there is no references to "Tmp"??
|
||||
Visit(node->expression.get());
|
||||
|
|
@ -495,7 +474,7 @@ void ExecuteVisitor::Visit(TypeConstructor* node) {
|
|||
Visitor::Visit(parameter.value);
|
||||
// TODO: copy/move parameters
|
||||
fields.push_back(
|
||||
{ parameter.name.has_value() ? std::optional<std::string>(parameter.name.value().name) : std::nullopt,
|
||||
{ parameter.name.has_value() ? std::optional<std::string>(parameter.name.value()) : std::nullopt,
|
||||
current_value_ });
|
||||
}
|
||||
|
||||
|
|
@ -532,7 +511,7 @@ void ExecuteVisitor::Visit(NameExpression* node) { // TODO: check
|
|||
}
|
||||
|
||||
std::optional<utils::IdType> maybe_variable_value =
|
||||
context_manager_.GetVariableInfo(node->names[0].name);
|
||||
context_manager_.GetVariableInfo(node->names[0]);
|
||||
|
||||
if (!maybe_variable_value.has_value()) {
|
||||
error_handling::HandleRuntimeError("Variable not found", node->base);
|
||||
|
|
@ -544,9 +523,9 @@ void ExecuteVisitor::Visit(NameExpression* node) { // TODO: check
|
|||
utils::ValueType variable_value_type = context_manager_.GetValueType(current_value_);
|
||||
|
||||
for (size_t i = 1; i < node->names.size(); ++i) {
|
||||
std::optional<utils::IdType> maybe_field_value = context_manager_.GetAnyValue(current_value_)->GetFieldValue(node->names[i].name); // TODO
|
||||
std::optional<utils::IdType> maybe_field_value = context_manager_.GetAnyValue(current_value_)->GetFieldValue(node->names[i]); // TODO
|
||||
if (!maybe_field_value.has_value()) {
|
||||
error_handling::HandleRuntimeError("Variable field not found", node->names[i].base);
|
||||
error_handling::HandleRuntimeError("Variable field not found", node->base);
|
||||
}
|
||||
|
||||
current_value_ = maybe_field_value.value();
|
||||
|
|
@ -749,7 +728,7 @@ void ExecuteVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
|
|||
switch (node.index()) {
|
||||
case 0:
|
||||
value = context_manager_.ToModifiedValue(value, utils::ValueType::Const); // ??
|
||||
if (!context_manager_.DefineVariable(std::get<std::unique_ptr<ExtendedName>>(node)->name,
|
||||
if (!context_manager_.DefineVariable(*std::get<std::unique_ptr<NameIdentifier>>(node),
|
||||
value)) {
|
||||
error_handling::HandleRuntimeError("Can't redifine variable", base_node);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ void FindSymbolsVisitor::Visit(FunctionDeclaration* node) {
|
|||
if (was_in_statement) {
|
||||
current_info_ = std::move(info);
|
||||
} else {
|
||||
node->function_id_ = namespace_visitor_.AddFunctionDeclaration(node->name.name, std::move(info));
|
||||
node->function_id_ = namespace_visitor_.AddFunctionDeclaration(node->name, std::move(info));
|
||||
is_in_statement_ = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -86,12 +86,12 @@ void FindSymbolsVisitor::Visit(FunctionDefinitionStatement* node) {
|
|||
|
||||
info.argument_names.resize(definition->arguments.size());
|
||||
for (size_t i = 0; i < definition->arguments.size(); ++i) {
|
||||
info.argument_names[i] = definition->arguments[i].name;
|
||||
info.argument_names[i] = definition->arguments[i];
|
||||
}
|
||||
|
||||
info.node = node;
|
||||
|
||||
node->function_id_ = namespace_visitor_.AddFunctionDefinition(definition->name.name, std::move(info));
|
||||
node->function_id_ = namespace_visitor_.AddFunctionDefinition(definition->name, std::move(info));
|
||||
|
||||
is_in_statement_ = false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,15 +188,6 @@ void PrintVisitor::Visit(PartitionStatement* node) {
|
|||
|
||||
void PrintVisitor::Visit(FunctionDefinition* node) {
|
||||
out_ << "[FunctionDefinition ";
|
||||
switch (node->modifier) {
|
||||
case utils::FunctionTypeModifier::Operator:
|
||||
out_ << "operator";
|
||||
break;
|
||||
case utils::FunctionTypeModifier::Function:
|
||||
out_ << "function";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
Visit(&node->name);
|
||||
out_ << "]";
|
||||
if (!node->arguments.empty()) {
|
||||
|
|
@ -389,24 +380,19 @@ void PrintVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
out_ << ')';
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
out_ << "[UnaryOperator ";
|
||||
Visit(&node->operator_name);
|
||||
out_ << "] (";
|
||||
Visitor::Visit(node->expression);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(ReferenceExpression* node) {
|
||||
out_ << "[ReferenceExpression ";
|
||||
for (auto& reference : node->references) {
|
||||
switch (reference) {
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '~';
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '^';
|
||||
break;
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
out_ << '@';
|
||||
break;
|
||||
case utils::ReferenceModifier::Dereference:
|
||||
out_ << '~';
|
||||
break;
|
||||
}
|
||||
}
|
||||
out_ << "] (";
|
||||
|
|
@ -676,12 +662,15 @@ void PrintVisitor::Visit(ExtendedScopedAnyType* node) {
|
|||
out_ << "[ExtendedScopedAnyType ";
|
||||
for (auto& reference : node->references) {
|
||||
switch (reference) {
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '~';
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '^';
|
||||
break;
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
out_ << '@';
|
||||
break;
|
||||
case utils::ReferenceModifier::Dereference:
|
||||
out_ << '~';
|
||||
break;
|
||||
}
|
||||
}
|
||||
out_ << "] (";
|
||||
|
|
@ -715,10 +704,6 @@ void PrintVisitor::Visit(ParametrizedType* node) {
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void PrintVisitor::Visit(ExtendedName* node) {
|
||||
out_ << "[ExtendedName " << node->name << "] ";
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(std::string* node) { // std::string
|
||||
out_ << "[Identifier " << *node << "] ";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
|||
for (size_t i = 0; i < node->definition->arguments.size(); ++i) {
|
||||
Visitor::Visit(*declaration.argument_types[i]);
|
||||
current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Const); // TODO: var function arguments??
|
||||
if (!context_manager_.DefineVariable(node->definition->arguments[i].name, current_type_)) { // TODO: watch to reference
|
||||
if (!context_manager_.DefineVariable(node->definition->arguments[i], current_type_)) { // TODO: watch to reference
|
||||
error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base);
|
||||
}
|
||||
}
|
||||
|
|
@ -316,7 +316,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
|
|||
for (size_t i = 0; i < node->parameters.size(); ++i) {
|
||||
if (node->parameters[i].name.has_value()) { // TODO: needed??
|
||||
if (!constructor_fields->entities[i].first.has_value()
|
||||
|| constructor_fields->entities[i].first.value().name != node->parameters[i].name.value().name) {
|
||||
|| constructor_fields->entities[i].first.value() != node->parameters[i].name.value()) {
|
||||
error_handling::HandleTypecheckError("Type constructor pattern: name of parameter and name in constructor don't match each other", node->base);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -690,53 +690,13 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
// TODO: can be method ??
|
||||
void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
// TODO: Check, that type is not abstract ??
|
||||
auto maybe_operator_id = namespace_visitor_.FindFunction({}, node->operator_name);
|
||||
|
||||
if (!maybe_operator_id.has_value()) {
|
||||
error_handling::HandleTypecheckError("Operator not found", node->base);
|
||||
}
|
||||
|
||||
const info::definition::Function& operator_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value());
|
||||
|
||||
if (!operator_info.declaration.has_value()) {
|
||||
error_handling::HandleTypecheckError("Operator declaration not found", node->base);
|
||||
}
|
||||
|
||||
if (!operator_info.definition.has_value()) { // TODO: builtin, etc.
|
||||
error_handling::HandleTypecheckError("Operator definition not found", node->base);
|
||||
}
|
||||
|
||||
if (operator_info.argument_count != 2) { // 1 + return type
|
||||
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
|
||||
}
|
||||
|
||||
if (operator_info.declaration->parameters.size() > 0) {
|
||||
error_handling::HandleTypecheckError("Operator with parameters", node->base);
|
||||
}
|
||||
|
||||
Visitor::Visit(*operator_info.declaration.value().argument_types[0]);
|
||||
utils::IdType expression_type = current_type_;
|
||||
|
||||
Visitor::Visit(node->expression);
|
||||
if (!context_manager_.AddValueRequirement(current_type_, expression_type)) {
|
||||
error_handling::HandleTypecheckError("Operator expression has wrong type", node->base);
|
||||
}
|
||||
|
||||
node->function_id_ = maybe_operator_id.value(); // IMPORTANT
|
||||
|
||||
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
|
||||
|
||||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(ReferenceExpression* node) {
|
||||
Visit(node->expression.get());
|
||||
|
||||
current_type_ = context_manager_.AddValue(
|
||||
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()),
|
||||
info::type::ReferenceToType(node->references,
|
||||
current_type_,
|
||||
context_manager_.GetValueManager()),
|
||||
context_manager_.GetValueType(current_type_)); // ??
|
||||
}
|
||||
|
||||
|
|
@ -772,7 +732,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
std::unordered_map<std::string, utils::IdType> context;
|
||||
|
||||
// guaranteed, that name.size() > 0
|
||||
if (node->name.name[0] == '_') { // TODO: manage pointers to function
|
||||
if (node->name[0] == '_') { // TODO: manage pointers to function
|
||||
error_handling::HandleInternalError("Builtin functions/methods weren't implemented yet",
|
||||
"TypeCheckVisitor.FunctionCallExpresssion");
|
||||
}
|
||||
|
|
@ -797,7 +757,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
// TODO: better decision ??
|
||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value()->parent_namespace->const_namespaces.at(maybe_type_info.value()->type.type).functions[node->name.name];
|
||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value()->parent_namespace->const_namespaces.at(maybe_type_info.value()->type.type).functions[node->name];
|
||||
|
||||
utils::ValueType expression_value_type = context_manager_.GetValueType(current_type_);
|
||||
|
||||
|
|
@ -808,7 +768,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
|
||||
if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) {
|
||||
// TODO: choose expression value types
|
||||
maybe_function_id = maybe_type_info.value()->parent_namespace->var_namespaces.at(maybe_type_info.value()->type.type).functions[node->name.name];
|
||||
maybe_function_id = maybe_type_info.value()->parent_namespace->var_namespaces.at(maybe_type_info.value()->type.type).functions[node->name];
|
||||
}
|
||||
|
||||
if (maybe_const_function_id.has_value() && maybe_function_id.has_value()) { // TODO: handle on link_types stage
|
||||
|
|
@ -833,14 +793,14 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
path.push_back(type_expression.type.type);
|
||||
|
||||
maybe_function_id = namespace_visitor_.FindFunction(path, node->name.name);
|
||||
maybe_function_id = namespace_visitor_.FindFunction(path, node->name);
|
||||
|
||||
CollectTypeExpressionContext(*std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()), context);
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
} else {
|
||||
maybe_function_id = namespace_visitor_.FindFunction(std::nullopt, node->name.name);
|
||||
maybe_function_id = namespace_visitor_.FindFunction(std::nullopt, node->name);
|
||||
}
|
||||
|
||||
if (!maybe_function_id.has_value()) {
|
||||
|
|
@ -979,7 +939,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
|||
// TODO: remove variable on move
|
||||
if (node->parameters[i].name.has_value()) {
|
||||
if (!constructor_fields->entities[i].first.has_value()
|
||||
|| constructor_fields->entities[i].first.value().name != node->parameters[i].name.value().name) {
|
||||
|| constructor_fields->entities[i].first.value() != node->parameters[i].name.value()) {
|
||||
error_handling::HandleTypecheckError("Type constructor: name of parameter and name in constructor don't match each other", node->base);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -1060,10 +1020,10 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
|
|||
error_handling::HandleInternalError("Name expression with zero names", "TypeCheckVisitor.NameExpression");
|
||||
}
|
||||
|
||||
auto maybe_variable_type = context_manager_.GetVariableInfo(node->names[0].name);
|
||||
auto maybe_variable_type = context_manager_.GetVariableInfo(node->names[0]);
|
||||
|
||||
if (!maybe_variable_type.has_value()) {
|
||||
error_handling::HandleTypecheckError("Variable not found", node->names[0].base);
|
||||
error_handling::HandleTypecheckError("Variable not found", node->base);
|
||||
}
|
||||
|
||||
current_type_ = maybe_variable_type.value();
|
||||
|
|
@ -1072,9 +1032,9 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
|
|||
utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_);
|
||||
|
||||
for (size_t i = 1; i < node->names.size(); ++i) {
|
||||
std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i].name);
|
||||
std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i]);
|
||||
if (!maybe_type_value.has_value()) {
|
||||
error_handling::HandleTypecheckError("Variable field not found", node->names[i].base);
|
||||
error_handling::HandleTypecheckError("Variable field not found", node->base);
|
||||
}
|
||||
|
||||
current_type_ = maybe_type_value.value();
|
||||
|
|
@ -1240,7 +1200,7 @@ void TypeCheckVisitor::Visit(TupleType* node) {
|
|||
for (auto& entity : node->entities) {
|
||||
Visit(entity.second.get());
|
||||
if (entity.first.has_value()) {
|
||||
fields.push_back({entity.first.value().name, current_type_});
|
||||
fields.push_back({entity.first.value(), current_type_});
|
||||
} else {
|
||||
fields.push_back({std::nullopt, current_type_});
|
||||
}
|
||||
|
|
@ -1437,7 +1397,7 @@ void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
|
|||
switch (node.index()) {
|
||||
case 0:
|
||||
value_type = context_manager_.ToModifiedValue(value_type, utils::ValueType::Const); // ??
|
||||
if (!context_manager_.DefineVariable(std::get<std::unique_ptr<ExtendedName>>(node)->name,
|
||||
if (!context_manager_.DefineVariable(*std::get<std::unique_ptr<NameIdentifier>>(node),
|
||||
value_type)) {
|
||||
error_handling::HandleTypecheckError("Can't redifine variable", base_node);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,15 +261,6 @@ void TypedPrintVisitor::Visit(FunctionDefinition* node) {
|
|||
}
|
||||
|
||||
out_ << ") ";
|
||||
switch (node->modifier) {
|
||||
case utils::FunctionTypeModifier::Operator:
|
||||
out_ << "operator";
|
||||
break;
|
||||
case utils::FunctionTypeModifier::Function:
|
||||
out_ << "function";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
Visit(&node->name);
|
||||
out_ << "]";
|
||||
if (!node->arguments.empty()) {
|
||||
|
|
@ -546,20 +537,6 @@ void TypedPrintVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
out_ << ')';
|
||||
}
|
||||
|
||||
void TypedPrintVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
out_ << "[UnaryOperator : (";
|
||||
|
||||
if (node->base.type_.has_value()) {
|
||||
out_ << context_manager_.GetAnyValue(node->base.type_.value())->GetTypeName();
|
||||
}
|
||||
|
||||
out_ << ") ";
|
||||
Visit(&node->operator_name);
|
||||
out_ << "] (";
|
||||
Visitor::Visit(node->expression);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void TypedPrintVisitor::Visit(ReferenceExpression* node) {
|
||||
out_ << "[ReferenceExpression : (";
|
||||
|
||||
|
|
@ -570,12 +547,15 @@ void TypedPrintVisitor::Visit(ReferenceExpression* node) {
|
|||
out_ << ") ";
|
||||
for (auto& reference : node->references) {
|
||||
switch (reference) {
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '~';
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '^';
|
||||
break;
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
out_ << '@';
|
||||
break;
|
||||
case utils::ReferenceModifier::Dereference:
|
||||
out_ << '~';
|
||||
break;
|
||||
}
|
||||
}
|
||||
out_ << "] (";
|
||||
|
|
@ -957,12 +937,15 @@ void TypedPrintVisitor::Visit(ExtendedScopedAnyType* node) {
|
|||
out_ << ") ";
|
||||
for (auto& reference : node->references) {
|
||||
switch (reference) {
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '~';
|
||||
case utils::ReferenceModifier::Reference:
|
||||
out_ << '^';
|
||||
break;
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
case utils::ReferenceModifier::UniqueReference:
|
||||
out_ << '@';
|
||||
break;
|
||||
case utils::ReferenceModifier::Dereference:
|
||||
out_ << '~';
|
||||
break;
|
||||
}
|
||||
}
|
||||
out_ << "] (";
|
||||
|
|
@ -1002,16 +985,6 @@ void TypedPrintVisitor::Visit(ParametrizedType* node) {
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void TypedPrintVisitor::Visit(ExtendedName* node) {
|
||||
out_ << "[ExtendedName : (";
|
||||
|
||||
if (node->base.type_.has_value()) {
|
||||
out_ << context_manager_.GetAnyValue(node->base.type_.value())->GetTypeName();
|
||||
}
|
||||
|
||||
out_ << ") " << node->name << "] ";
|
||||
}
|
||||
|
||||
void TypedPrintVisitor::Visit(std::string* node) { // std::string
|
||||
out_ << "[Identifier " << *node << "] ";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ void Visitor::Visit(SourceStatement& node) {
|
|||
void Visitor::Visit(Pattern& node) { // <-> ScopedPattern
|
||||
switch (node.index()) {
|
||||
case 0:
|
||||
Visit(std::get<std::unique_ptr<ExtendedName>>(node).get());
|
||||
Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
||||
break;
|
||||
case 1:
|
||||
Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||
|
|
@ -189,9 +189,6 @@ void Visitor::Visit(Expression& node) {
|
|||
Visit(*std::get<std::unique_ptr<PrefixedExpression>>(node));
|
||||
break;
|
||||
case 3:
|
||||
Visit(std::get<std::unique_ptr<UnaryOperatorExpression>>(node).get());
|
||||
break;
|
||||
case 4:
|
||||
Visit(*std::get<std::unique_ptr<SubExpression>>(node));
|
||||
break;
|
||||
default:
|
||||
|
|
@ -477,11 +474,6 @@ void Visitor::Visit(BinaryOperatorExpression* node) {
|
|||
Visit(node->right_expression);
|
||||
}
|
||||
|
||||
void Visitor::Visit(UnaryOperatorExpression* node) {
|
||||
Visit(&node->operator_name);
|
||||
Visit(node->expression);
|
||||
}
|
||||
|
||||
void Visitor::Visit(ReferenceExpression* node) {
|
||||
Visit(node->expression.get());
|
||||
}
|
||||
|
|
@ -662,8 +654,6 @@ void Visitor::Visit(ParametrizedType* node) {
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void Visitor::Visit(ExtendedName* node) {}
|
||||
|
||||
void Visitor::Visit(std::string* node) {} // std::string
|
||||
|
||||
void Visitor::Visit(FloatNumberLiteral* node) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue