mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-10 00:48:44 +00:00
.
This commit is contained in:
parent
648f78afa3
commit
496d3819d9
6 changed files with 71 additions and 16 deletions
|
|
@ -18,6 +18,11 @@ public:
|
||||||
friend GlobalInfo;
|
friend GlobalInfo;
|
||||||
NamespaceVisitor() = delete;
|
NamespaceVisitor() = delete;
|
||||||
public:
|
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 AddImport(definition::Import&& import_info, const std::optional<std::string>& name = std::nullopt);
|
||||||
|
|
||||||
void AddEnterNamespace(const std::string& name,
|
void AddEnterNamespace(const std::string& name,
|
||||||
|
|
@ -57,6 +62,8 @@ public:
|
||||||
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
||||||
const std::string& type);
|
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> FindAbstractType(const std::string& abstract_type);
|
||||||
|
|
||||||
std::optional<utils::IdType> FindTypeclass(const std::string& typeclass);
|
std::optional<utils::IdType> FindTypeclass(const std::string& typeclass);
|
||||||
|
|
@ -96,22 +103,22 @@ public:
|
||||||
return NamespaceVisitor(*this);
|
return NamespaceVisitor(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remember about vector realloc
|
// remember about vector realloc
|
||||||
definition::Function* GetFunctionInfo(utils::IdType id) {
|
definition::Function* GetFunctionInfo(utils::IdType id) {
|
||||||
return &functions_[id];
|
return &functions_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remember about vector realloc
|
// remember about vector realloc
|
||||||
definition::Type* GetTypeInfo(utils::IdType id) {
|
definition::Type* GetTypeInfo(utils::IdType id) {
|
||||||
return &types_[id];
|
return &types_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remember about vector realloc
|
// remember about vector realloc
|
||||||
definition::Typeclass* GetTypeclassInfo(utils::IdType id) {
|
definition::Typeclass* GetTypeclassInfo(utils::IdType id) {
|
||||||
return &typeclasses_[id];
|
return &typeclasses_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remember about vector realloc
|
// remember about vector realloc
|
||||||
definition::Constructor* GetConstructorInfo(utils::IdType id) {
|
definition::Constructor* GetConstructorInfo(utils::IdType id) {
|
||||||
return &constructors_[id];
|
return &constructors_[id];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -557,8 +557,6 @@ struct TypeExpression {
|
||||||
TypeSubExpression type;
|
TypeSubExpression type;
|
||||||
|
|
||||||
std::optional<size_t> array_size; // if array; 0 - dynamic size
|
std::optional<size_t> array_size; // if array; 0 - dynamic size
|
||||||
|
|
||||||
utils::IdType type_id_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ExtendedScopedAnyType {
|
struct ExtendedScopedAnyType {
|
||||||
|
|
@ -584,6 +582,8 @@ struct ParametrizedTypeclass {
|
||||||
struct ParametrizedType {
|
struct ParametrizedType {
|
||||||
AnyTypeIdentifier type;
|
AnyTypeIdentifier type;
|
||||||
std::vector<std::unique_ptr<TypeExpression>> parameters;
|
std::vector<std::unique_ptr<TypeExpression>> parameters;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> type_id_; // std::nullopt, if it is namespace without type
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------- Comments [IGNORE] -----------------
|
// ----------------- Comments [IGNORE] -----------------
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
// for clangd
|
// for clangd
|
||||||
#include "error_handling.hpp"
|
|
||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
|
@ -37,12 +36,18 @@ public:
|
||||||
|
|
||||||
// TODO: variable modifiers
|
// TODO: variable modifiers
|
||||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||||
|
// check in previous contexts ??
|
||||||
return contexts_.back().DefineVariable(name, type_id);
|
return contexts_.back().DefineVariable(name, type_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DefineLocalAbstractType() {
|
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
|
||||||
// TODO
|
if (GetLocalAbstractType(name)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
contexts_.back().DefineLocalAbstractType(name, type_id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool RemoveVariable(const std::string& name) {
|
bool RemoveVariable(const std::string& name) {
|
||||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||||
|
|
@ -70,16 +75,34 @@ public:
|
||||||
return std::nullopt;
|
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:
|
private:
|
||||||
class Context {
|
class Context {
|
||||||
public:
|
public:
|
||||||
Context(bool hide_previous) : hide_previous_(hide_previous) {}
|
Context(bool hide_previous) : hide_previous_(hide_previous) {}
|
||||||
|
|
||||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||||
if (variables_.count(name) > 0) {
|
if (local_abstract_types_.count(name) > 0) {
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,10 +120,21 @@ private:
|
||||||
return variable_iter->second;
|
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_; }
|
bool IsFirst() { return hide_previous_; }
|
||||||
private:
|
private:
|
||||||
bool hide_previous_;
|
bool hide_previous_;
|
||||||
std::unordered_map<std::string, utils::IdType> variables_;
|
std::unordered_map<std::string, utils::IdType> variables_;
|
||||||
|
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Context> contexts_;
|
std::vector<Context> contexts_;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
// for clangd
|
// for clangd
|
||||||
#include "../include/find_symbols_visitor.hpp"
|
#include "../include/find_symbols_visitor.hpp"
|
||||||
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
namespace interpreter {
|
namespace interpreter {
|
||||||
|
|
||||||
// Sources -----------------
|
// Sources -----------------
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& a
|
||||||
if (!FindAbstractType(abstract_type).has_value()) {
|
if (!FindAbstractType(abstract_type).has_value()) {
|
||||||
size_t id = global_info_.abstract_types_.size();
|
size_t id = global_info_.abstract_types_.size();
|
||||||
global_info_.name_to_abstract_type_[abstract_type] = id;
|
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");
|
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) {
|
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);
|
auto abstract_type_id_iter = global_info_.name_to_abstract_type_.find(abstract_type);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// for clangd
|
// for clangd
|
||||||
#include "../include/link_symbols_visitor.hpp"
|
#include "../include/link_symbols_visitor.hpp"
|
||||||
#include "../include/error_handling.hpp"
|
#include "../include/error_handling.hpp"
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace interpreter {
|
namespace interpreter {
|
||||||
|
|
||||||
|
|
@ -20,8 +21,13 @@ const std::string& NameFromTypeSubExpression(const TypeSubExpression& type) {
|
||||||
void LinkSymbolsVisitor::Visit(Namespace* node) {
|
void LinkSymbolsVisitor::Visit(Namespace* node) {
|
||||||
// Visitor::Visit(&node->type); // not needed
|
// Visitor::Visit(&node->type); // not needed
|
||||||
|
|
||||||
auto maybe_type = namespace_visitor_.FindType(std::nullopt, node->type); // TODO: find only in local namespace
|
std::optional<utils::IdType> maybe_type = namespace_visitor_.FindLocalType(node->type);
|
||||||
auto maybe_typeclass = namespace_visitor_.FindType(std::nullopt, node->type); // TODO: find only if in global namespace
|
|
||||||
|
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()) {
|
if (maybe_type.has_value() && maybe_typeclass.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Ambigious namespace name (typeclass or type)");
|
error_handling::HandleTypecheckError("Ambigious namespace name (typeclass or type)");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue