2023-04-17 12:09:02 +03:00
|
|
|
// for clangd
|
|
|
|
|
#include "../include/link_symbols_visitor.hpp"
|
2023-04-17 18:56:58 +03:00
|
|
|
#include "../include/error_handling.hpp"
|
2023-04-17 12:09:02 +03:00
|
|
|
|
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
|
|
// Sources -----------------
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(SourceFile* node) {
|
|
|
|
|
for (auto& statement : node->statements) {
|
|
|
|
|
if (std::holds_alternative<Partition>(statement)) {
|
|
|
|
|
Visit(&std::get<Partition>(statement));
|
|
|
|
|
} else if (std::holds_alternative<SourceStatement>(statement)) {
|
|
|
|
|
Visitor::Visit(std::get<SourceStatement>(statement));
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(Sources* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
for (auto& statement : node->statements) {
|
|
|
|
|
Visitor::Visit(statement);
|
|
|
|
|
}
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Namespaces, partitions -----------------
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(Partition* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
// TODO
|
2023-04-17 12:09:02 +03:00
|
|
|
Visit(node->scope.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(Namespace* node) {
|
2023-04-21 14:27:55 +03:00
|
|
|
node->type_id_ = namespace_visitor_.FindType({}, node->type);
|
|
|
|
|
|
|
|
|
|
if (node->name.has_value() && !node->type_id_.has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Variable namespace type not found");
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
namespace_visitor_.EnterNamespace(node->type);
|
2023-04-17 12:09:02 +03:00
|
|
|
Visit(node->scope.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
namespace_visitor_.ExitNamespace();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Definitions -----------------
|
|
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
void LinkSymbolsVisitor::Visit(ImportStatement* node) {}
|
2023-04-17 12:09:02 +03:00
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(AliasDefinitionStatement* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
|
|
|
|
for (size_t i = 0; i <node->parameters.size(); ++i) {
|
|
|
|
|
abstract_types_.DefineType(node->parameters[i], node->parameter_graph_ids_[i]);
|
|
|
|
|
}
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(node->value.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(VariableDefinitionStatement* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
Visitor::Visit(node->name);
|
|
|
|
|
Visitor::Visit(node->value);
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(FunctionDeclaration* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
for (auto& parameter : node->parameters) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(parameter.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(node->type.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(FunctionDefinitionStatement* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
Visit(node->definition.get());
|
|
|
|
|
Visitor::Visit(node->value);
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(TypeDefinitionStatement* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
Visit(node->definition.get());
|
|
|
|
|
Visitor::Visit(node->value);
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(node->type.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
// TODO: can't be used before definition
|
|
|
|
|
abstract_types_.DefineType(node->type->type, node->type->type_graph_id_);
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(TypeclassDefinitionStatement* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
Visit(node->definition.get());
|
|
|
|
|
for (auto& requirement : node->requirements) {
|
|
|
|
|
Visit(requirement.get());
|
|
|
|
|
}
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Definition parts
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(FunctionDefinition* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
for (auto& parameter : node->parameters) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(parameter.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(TypeDefinition* node) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(node->type.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
for (auto& parameter : node->parameters) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(parameter.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flow control -----------------
|
|
|
|
|
|
|
|
|
|
// Statements, expressions, blocks, etc. -----------------
|
|
|
|
|
|
|
|
|
|
// Operators
|
|
|
|
|
|
|
|
|
|
// Other Expressions
|
|
|
|
|
|
2023-04-22 19:30:16 +03:00
|
|
|
// TODO: move to find_symbols_visitor
|
2023-04-17 12:09:02 +03:00
|
|
|
void LinkSymbolsVisitor::Visit(LambdaFunction* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.EnterContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
for (auto& parameter : node->parameters) {
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(parameter.get());
|
2023-04-17 18:56:58 +03:00
|
|
|
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-22 19:30:16 +03:00
|
|
|
////////////
|
|
|
|
|
node->argument_graph_ids_.resize(node->arguments.size());
|
|
|
|
|
for (size_t i = 0; i < node->arguments.size(); ++i) {
|
|
|
|
|
node->argument_graph_ids_[i] = namespace_visitor_.GetAbstractTypeGraph()->AddVertex();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-22 19:30:16 +03:00
|
|
|
node->return_type_graph_id_ = namespace_visitor_.GetAbstractTypeGraph()->AddVertex();
|
|
|
|
|
///////////
|
2023-04-17 12:09:02 +03:00
|
|
|
|
2023-04-22 19:30:16 +03:00
|
|
|
Visitor::Visit(node->expression);
|
|
|
|
|
abstract_types_.ExitContext();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-22 19:30:16 +03:00
|
|
|
// Name
|
2023-04-17 12:09:02 +03:00
|
|
|
|
|
|
|
|
// Type, typeclass, etc. -----------------
|
|
|
|
|
|
|
|
|
|
// Type
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(TypeExpression* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
std::vector<std::string> path;
|
|
|
|
|
path.reserve(node->namespaces.size());
|
2023-04-17 12:09:02 +03:00
|
|
|
|
|
|
|
|
for (auto& type_namespace : node->namespaces) {
|
|
|
|
|
Visitor::Visit(type_namespace);
|
2023-04-17 18:56:58 +03:00
|
|
|
if (std::holds_alternative<std::unique_ptr<std::string>>(type_namespace)) {
|
|
|
|
|
path.push_back(*std::get<std::unique_ptr<std::string>>(type_namespace));
|
|
|
|
|
} else if (std::holds_alternative<std::unique_ptr<ParametrizedType>>(type_namespace)) {
|
|
|
|
|
path.push_back(std::get<std::unique_ptr<ParametrizedType>>(type_namespace)->type_expression->type);
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
std::optional<utils::IdType> maybe_type = namespace_visitor_.FindType(path, node->type);
|
|
|
|
|
std::optional<utils::IdType> maybe_abstract_type = std::nullopt;
|
|
|
|
|
|
|
|
|
|
if (path.size() == 0) {
|
|
|
|
|
maybe_abstract_type = abstract_types_.GetTypeId(node->type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maybe_abstract_type.has_value()) {
|
|
|
|
|
if (maybe_type.has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Ambigious type");
|
|
|
|
|
} else {
|
|
|
|
|
node->type_id_ = maybe_abstract_type.value();
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
2023-04-17 18:56:58 +03:00
|
|
|
} else if (maybe_type.has_value()) {
|
|
|
|
|
node->type_id_ = maybe_type.value();
|
|
|
|
|
} else {
|
|
|
|
|
error_handling::HandleTypecheckError("Type not found");
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
2023-04-17 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 12:09:02 +03:00
|
|
|
// Typeclass
|
|
|
|
|
|
|
|
|
|
void LinkSymbolsVisitor::Visit(TypeclassExpression* node) {
|
2023-04-17 18:56:58 +03:00
|
|
|
std::vector<std::string> path;
|
|
|
|
|
path.reserve(node->namespaces.size());
|
|
|
|
|
|
2023-04-17 12:09:02 +03:00
|
|
|
for (auto& typeclass_namespace : node->namespaces) {
|
|
|
|
|
Visitor::Visit(typeclass_namespace);
|
2023-04-17 18:56:58 +03:00
|
|
|
if (std::holds_alternative<std::unique_ptr<std::string>>(typeclass_namespace)) {
|
|
|
|
|
path.push_back(*std::get<std::unique_ptr<std::string>>(typeclass_namespace));
|
|
|
|
|
} else if (std::holds_alternative<std::unique_ptr<ParametrizedType>>(typeclass_namespace)) {
|
|
|
|
|
path.push_back(std::get<std::unique_ptr<ParametrizedType>>(typeclass_namespace)->type_expression->type);
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
std::optional<utils::IdType> maybe_typeclass = namespace_visitor_.FindType(path, node->typeclass);
|
|
|
|
|
std::optional<utils::IdType> maybe_abstract_typeclass = std::nullopt;
|
2023-04-17 12:09:02 +03:00
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
if (path.size() == 0) {
|
|
|
|
|
maybe_abstract_typeclass = abstract_types_.GetTypeId(node->typeclass);
|
|
|
|
|
}
|
2023-04-17 12:09:02 +03:00
|
|
|
|
2023-04-17 18:56:58 +03:00
|
|
|
if (maybe_abstract_typeclass.has_value()) {
|
|
|
|
|
if (maybe_typeclass.has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Ambigious type");
|
|
|
|
|
} else {
|
|
|
|
|
node->type_id_ = maybe_abstract_typeclass.value();
|
|
|
|
|
}
|
|
|
|
|
} else if (maybe_typeclass.has_value()) {
|
|
|
|
|
node->type_id_ = maybe_typeclass.value();
|
|
|
|
|
} else {
|
|
|
|
|
error_handling::HandleTypecheckError("Type not found");
|
|
|
|
|
}
|
2023-04-17 12:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace interpreter
|