This commit is contained in:
ProgramSnail 2023-05-02 16:16:55 +03:00
parent 648f78afa3
commit 496d3819d9
6 changed files with 71 additions and 16 deletions

View file

@ -18,6 +18,11 @@ public:
friend GlobalInfo;
NamespaceVisitor() = delete;
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);
void AddEnterNamespace(const std::string& name,
@ -57,6 +62,8 @@ public:
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
const std::string& type);
std::optional<utils::IdType> FindLocalType(const std::string& type);
std::optional<utils::IdType> FindAbstractType(const std::string& abstract_type);
std::optional<utils::IdType> FindTypeclass(const std::string& typeclass);
@ -96,22 +103,22 @@ public:
return NamespaceVisitor(*this);
}
// TODO: remember about vector realloc
// remember about vector realloc
definition::Function* GetFunctionInfo(utils::IdType id) {
return &functions_[id];
}
// TODO: remember about vector realloc
// remember about vector realloc
definition::Type* GetTypeInfo(utils::IdType id) {
return &types_[id];
}
// TODO: remember about vector realloc
// remember about vector realloc
definition::Typeclass* GetTypeclassInfo(utils::IdType id) {
return &typeclasses_[id];
}
// TODO: remember about vector realloc
// remember about vector realloc
definition::Constructor* GetConstructorInfo(utils::IdType id) {
return &constructors_[id];
}

View file

@ -557,8 +557,6 @@ struct TypeExpression {
TypeSubExpression type;
std::optional<size_t> array_size; // if array; 0 - dynamic size
utils::IdType type_id_;
};
struct ExtendedScopedAnyType {
@ -584,6 +582,8 @@ struct ParametrizedTypeclass {
struct ParametrizedType {
AnyTypeIdentifier type;
std::vector<std::unique_ptr<TypeExpression>> parameters;
std::optional<utils::IdType> type_id_; // std::nullopt, if it is namespace without type
};
// ----------------- Comments [IGNORE] -----------------

View file

@ -6,7 +6,6 @@
#include <unordered_map>
// for clangd
#include "error_handling.hpp"
#include "types.hpp"
#include "utils.hpp"
@ -37,13 +36,19 @@ public:
// TODO: variable modifiers
bool DefineVariable(const std::string& name, utils::IdType type_id) {
// check in previous contexts ??
return contexts_.back().DefineVariable(name, type_id);
}
bool DefineLocalAbstractType() {
// TODO
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (GetLocalAbstractType(name)) {
return false;
}
contexts_.back().DefineLocalAbstractType(name, type_id);
return true;
}
bool RemoveVariable(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
if (contexts_[i].RemoveVariable(name)) {
@ -70,16 +75,34 @@ public:
return std::nullopt;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetLocalAbstractType(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
private:
class Context {
public:
Context(bool hide_previous) : hide_previous_(hide_previous) {}
bool DefineVariable(const std::string& name, utils::IdType type_id) {
if (variables_.count(name) > 0) {
if (local_abstract_types_.count(name) > 0) {
return false;
}
variables_[name] = type_id;
local_abstract_types_[name] = type_id;
return true;
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (local_abstract_types_.count(name) > 0) {
return false;
}
local_abstract_types_[name] = type_id;
return true;
}
@ -97,10 +120,21 @@ private:
return variable_iter->second;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
auto local_abstract_type_iter = local_abstract_types_.find(name);
if (local_abstract_type_iter == local_abstract_types_.end()) {
return std::nullopt;
}
return local_abstract_type_iter->second;
}
bool IsFirst() { return hide_previous_; }
private:
bool hide_previous_;
std::unordered_map<std::string, utils::IdType> variables_;
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
};
std::vector<Context> contexts_;

View file

@ -1,8 +1,6 @@
// for clangd
#include "../include/find_symbols_visitor.hpp"
// TODO
namespace interpreter {
// Sources -----------------

View file

@ -167,7 +167,7 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& a
if (!FindAbstractType(abstract_type).has_value()) {
size_t id = global_info_.abstract_types_.size();
global_info_.name_to_abstract_type_[abstract_type] = id;
global_info_.abstract_types_.push_b(ack(std::move(abstract_type_info));
global_info_.abstract_types_.push_back(std::move(abstract_type_info));
}
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace");
@ -261,6 +261,16 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
});
}
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindLocalType(const std::string& type) {
auto type_id_iter = namespace_stack_.back()->types.find(type);
if (type_id_iter != namespace_stack_.back()->types.end()) {
return type_id_iter->second;
}
return std::nullopt;
}
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindAbstractType(const std::string& abstract_type) {
auto abstract_type_id_iter = global_info_.name_to_abstract_type_.find(abstract_type);

View file

@ -1,6 +1,7 @@
// for clangd
#include "../include/link_symbols_visitor.hpp"
#include "../include/error_handling.hpp"
#include <optional>
namespace interpreter {
@ -20,8 +21,13 @@ const std::string& NameFromTypeSubExpression(const TypeSubExpression& type) {
void LinkSymbolsVisitor::Visit(Namespace* node) {
// Visitor::Visit(&node->type); // not needed
auto maybe_type = namespace_visitor_.FindType(std::nullopt, node->type); // TODO: find only in local namespace
auto maybe_typeclass = namespace_visitor_.FindType(std::nullopt, node->type); // TODO: find only if in global namespace
std::optional<utils::IdType> maybe_type = namespace_visitor_.FindLocalType(node->type);
std::optional<utils::IdType> maybe_typeclass;
if (namespace_visitor_.GetCurrentPath().size() == 0) {
maybe_typeclass = namespace_visitor_.FindTypeclass(node->type);
}
if (maybe_type.has_value() && maybe_typeclass.has_value()) {
error_handling::HandleTypecheckError("Ambigious namespace name (typeclass or type)");
}