mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 23:18:44 +00:00
link_symbols_visitor improvements
This commit is contained in:
parent
496d3819d9
commit
0dc8880c58
5 changed files with 33 additions and 6 deletions
|
|
@ -90,6 +90,8 @@ struct Namespace {
|
|||
std::unordered_map<std::string, Namespace> namespaces;
|
||||
std::unordered_map<std::string, Namespace> variable_namespaces;
|
||||
|
||||
Namespace* parent_namespace = nullptr;;
|
||||
|
||||
std::optional<Modifier> modifier; // modifier => variable namespace
|
||||
std::string type_name;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public:
|
|||
struct Path {
|
||||
std::vector<std::optional<utils::IdType>> path_types;
|
||||
definition::Namespace* result;
|
||||
}
|
||||
};
|
||||
|
||||
void AddImport(definition::Import&& import_info, const std::optional<std::string>& name = std::nullopt);
|
||||
|
||||
|
|
|
|||
|
|
@ -557,6 +557,9 @@ struct TypeExpression {
|
|||
TypeSubExpression type;
|
||||
|
||||
std::optional<size_t> array_size; // if array; 0 - dynamic size
|
||||
|
||||
std::optional<utils::IdType> type_id_;
|
||||
std::optional<utils::IdType> constructor_id_;
|
||||
};
|
||||
|
||||
struct ExtendedScopedAnyType {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
namespace_stack_.push_back(namespace_info);
|
||||
}
|
||||
|
||||
namespace_info->parent_namespace = namespace_stack_.back();
|
||||
|
||||
namespace_info->type_name = name;
|
||||
current_path_.push_back(name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ void LinkSymbolsVisitor::Visit(Namespace* node) {
|
|||
// Type
|
||||
|
||||
// TODO: link internal stages
|
||||
// TODO: find constructors ?
|
||||
void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
||||
std::vector<std::string> path;
|
||||
path.reserve(node->path.size());
|
||||
|
|
@ -58,12 +59,31 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
path.push_back(NameFromTypeSubExpression(path_type));
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> maybe_type = namespace_visitor_.FindType(path, NameFromTypeSubExpression(node->type));
|
||||
node->type_id_ = namespace_visitor_.FindType(path, NameFromTypeSubExpression(node->type));
|
||||
node->type_id_ = namespace_visitor_.FindType(path, NameFromTypeSubExpression(node->type));
|
||||
|
||||
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) {
|
||||
error_handling::HandleTypecheckError("Type or constructor not found");
|
||||
}
|
||||
|
||||
std::optional<info::definition::Namespace*> maybe_type_namespace = namespace_visitor_.FindNamespace(path);
|
||||
if (maybe_type_namespace.has_value()) {
|
||||
info::definition::Namespace* type_namespace = maybe_type_namespace.value();
|
||||
|
||||
for (ssize_t i = (ssize_t)node->path.size(); i >= 0; --i) {
|
||||
info::definition::Namespace* parent_namespace = type_namespace->parent_namespace;
|
||||
|
||||
auto maybe_type = parent_namespace->types.find(type_namespace->type_name); // TODO: make method ??
|
||||
if (maybe_type.has_value()) {
|
||||
node->type_id_ = maybe_type.value();
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found");
|
||||
node->path[i]->
|
||||
}
|
||||
|
||||
if (parent_namespace == nullptr) {
|
||||
error_handling::HandleInternalError("Parent namespace is null in type expression", "LinkSymbolsVisitor");
|
||||
}
|
||||
|
||||
type_namespace = parent_namespace;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue