mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-07 15:38:47 +00:00
namespace storage fix, namespace enter fix, maybe other fixes
This commit is contained in:
parent
4882d458f8
commit
4b4756b657
11 changed files with 250 additions and 174 deletions
|
|
@ -24,44 +24,71 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
error_handling::HandleTypecheckError("Can't define basic type namespace", base_node);
|
||||
}
|
||||
|
||||
definition::Namespace* namespace_info = nullptr;
|
||||
size_t id = global_info_.namespaces_.size();
|
||||
global_info_.namespaces_.emplace_back();
|
||||
definition::Namespace* namespace_info = &global_info_.namespaces_.back();
|
||||
|
||||
if (modifier.has_value()) {
|
||||
if (modifier.value() == utils::IsConstModifier::Const) {
|
||||
namespace_info = &namespace_stack_.back()->const_namespaces[name];
|
||||
global_info_.namespaces_[namespace_stack_.back()].const_namespaces[name] = id;
|
||||
} else {
|
||||
namespace_info = &namespace_stack_.back()->var_namespaces[name];
|
||||
global_info_.namespaces_[namespace_stack_.back()].var_namespaces[name] = id;
|
||||
}
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
|
||||
namespace_info->modifier = modifier;
|
||||
} else {
|
||||
namespace_info = &namespace_stack_.back()->namespaces[name];
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
global_info_.namespaces_[namespace_stack_.back()].namespaces[name] = id;
|
||||
}
|
||||
|
||||
namespace_info->parent_namespace = namespace_stack_.back();
|
||||
|
||||
namespace_info->type_name = name;
|
||||
|
||||
namespace_stack_.push_back(id);
|
||||
current_path_.push_back(name);
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
auto namespace_iter = namespace_stack_[i]->namespaces.find(name);
|
||||
if (namespace_iter != namespace_stack_[i]->namespaces.end()) {
|
||||
namespace_stack_.push_back(&namespace_iter->second);
|
||||
current_path_.push_back(name);
|
||||
return;
|
||||
// better code ??
|
||||
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name,
|
||||
std::optional<utils::IsConstModifier> modifier) {
|
||||
|
||||
if (!modifier.has_value()) {
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
auto namespace_iter = global_info_.namespaces_[namespace_stack_[i]].namespaces.find(name);
|
||||
if (namespace_iter != global_info_.namespaces_[namespace_stack_[i]].namespaces.end()) {
|
||||
namespace_stack_.push_back(namespace_iter->second);
|
||||
current_path_.push_back(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (modifier.has_value() && modifier.value() == utils::IsConstModifier::Const) {
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
auto const_namespace_iter = global_info_.namespaces_[namespace_stack_[i]].const_namespaces.find(name);
|
||||
if (const_namespace_iter != global_info_.namespaces_[namespace_stack_[i]].const_namespaces.end()) {
|
||||
namespace_stack_.push_back(const_namespace_iter->second);
|
||||
current_path_.push_back(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
auto var_namespace_iter = global_info_.namespaces_[namespace_stack_[i]].var_namespaces.find(name);
|
||||
if (var_namespace_iter != global_info_.namespaces_[namespace_stack_[i]].var_namespaces.end()) {
|
||||
namespace_stack_.push_back(var_namespace_iter->second);
|
||||
current_path_.push_back(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error_handling::HandleInternalError("Can't find namespace",
|
||||
error_handling::HandleInternalError("Can't find namespace " + name,
|
||||
"GlobalInfo.NamespaceVisitor.EnterNamespace");
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||
if (namespace_stack_.size() <= 1) {
|
||||
// error
|
||||
error_handling::HandleInternalError("Can't exit from global namespace",
|
||||
"GlobalInfo.NamespaceVisitor.ExitNamespace");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +100,7 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
namespace_stack_.clear();
|
||||
current_path_.clear();
|
||||
|
||||
namespace_stack_.push_back(&global_info_.global_namespace_);
|
||||
namespace_stack_.push_back(global_info_.GlobalNamespaceId);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
||||
|
|
@ -82,11 +109,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
|||
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().argument_count = function_declaration_info.argument_types.size(); // add return type
|
||||
global_info_.functions_.back().declaration = std::move(function_declaration_info);
|
||||
|
|
@ -106,11 +133,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::str
|
|||
definition::FunctionDefinition&& function_definition_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().argument_count = function_definition_info.argument_names.size() + 1;
|
||||
global_info_.functions_.back().definition = std::move(function_definition_info);
|
||||
|
|
@ -136,11 +163,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
|||
|
||||
size_t id = 0;
|
||||
|
||||
auto type_id_iter = namespace_stack_.back()->types.find(type);
|
||||
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
||||
|
||||
if (type_id_iter == namespace_stack_.back()->types.end()) {
|
||||
if (type_id_iter == global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
||||
id = global_info_.types_.size();
|
||||
namespace_stack_.back()->types[type] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].types[type] = id;
|
||||
global_info_.types_.push_back(std::move(type_info));
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("More then one type with the same name in namespace", base_node);
|
||||
|
|
@ -221,6 +248,8 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& type
|
|||
size_t id = global_info_.typeclasses_.size();
|
||||
global_info_.name_to_typeclass_[typeclass] = id;
|
||||
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -235,16 +264,18 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& co
|
|||
error_handling::HandleTypecheckError("Can't redefine basic type as constructor", base_node);
|
||||
}
|
||||
|
||||
auto constructor_id_iter = namespace_stack_.back()->constructors.find(constructor);
|
||||
auto constructor_id_iter = global_info_.namespaces_[namespace_stack_.back()].constructors.find(constructor);
|
||||
|
||||
if (constructor_id_iter == namespace_stack_.back()->constructors.end()) {
|
||||
if (constructor_id_iter == global_info_.namespaces_[namespace_stack_.back()].constructors.end()) {
|
||||
size_t id = global_info_.constructors_.size();
|
||||
namespace_stack_.back()->constructors[constructor] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace", base_node);
|
||||
return 0;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::string>& path,
|
||||
|
|
@ -269,9 +300,9 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::
|
|||
return id;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<definition::Namespace*>(path,
|
||||
[] (definition::Namespace* current_namespace) -> std::optional<definition::Namespace*> {
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
return current_namespace;
|
||||
});
|
||||
}
|
||||
|
|
@ -280,10 +311,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[name] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[name, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto function_info_iter = current_namespace->functions.find(name);
|
||||
if (function_info_iter == current_namespace->functions.end()) {
|
||||
auto function_info_iter = global_info_.namespaces_[current_namespace].functions.find(name);
|
||||
if (function_info_iter == global_info_.namespaces_[current_namespace].functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -299,22 +330,22 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
|||
// TODO: remove overhead
|
||||
|
||||
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
||||
[type, name, modifier] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type, name, modifier, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
|
||||
auto variable_namespace_iter =
|
||||
(modifier == utils::IsConstModifier::Const
|
||||
? current_namespace->const_namespaces.find(type)
|
||||
: current_namespace->var_namespaces.find(type));
|
||||
? global_info_.namespaces_[current_namespace].const_namespaces.find(type)
|
||||
: global_info_.namespaces_[current_namespace].var_namespaces.find(type));
|
||||
if (variable_namespace_iter ==
|
||||
(modifier == utils::IsConstModifier::Const
|
||||
? current_namespace->const_namespaces.end()
|
||||
: current_namespace->var_namespaces.end())) {
|
||||
? global_info_.namespaces_[current_namespace].const_namespaces.end()
|
||||
: global_info_.namespaces_[current_namespace].var_namespaces.end())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto method_iter = variable_namespace_iter->second.functions.find(name);
|
||||
if (method_iter == variable_namespace_iter->second.functions.end()) {
|
||||
auto method_iter = global_info_.namespaces_[variable_namespace_iter->second].functions.find(name);
|
||||
if (method_iter == global_info_.namespaces_[variable_namespace_iter->second].functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -326,10 +357,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[type] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto type_info_iter = current_namespace->types.find(type);
|
||||
if (type_info_iter == current_namespace->types.end()) {
|
||||
auto type_info_iter = global_info_.namespaces_[current_namespace].types.find(type);
|
||||
if (type_info_iter == global_info_.namespaces_[current_namespace].types.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -338,9 +369,9 @@ 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);
|
||||
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
||||
|
||||
if (type_id_iter != namespace_stack_.back()->types.end()) {
|
||||
if (type_id_iter != global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
||||
return type_id_iter->second;
|
||||
}
|
||||
|
||||
|
|
@ -371,10 +402,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& constructor) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[constructor] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[constructor, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto constructor_info_iter = current_namespace->constructors.find(constructor);
|
||||
if (constructor_info_iter == current_namespace->constructors.end()) {
|
||||
auto constructor_info_iter = global_info_.namespaces_[current_namespace].constructors.find(constructor);
|
||||
if (constructor_info_iter == global_info_.namespaces_[current_namespace].constructors.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -385,9 +416,9 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
|||
template<typename T>
|
||||
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(definition::Namespace*)> search_func) {
|
||||
std::function<std::optional<T>(utils::IdType)> search_func) {
|
||||
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
definition::Namespace* current_namespace = nullptr;
|
||||
utils::IdType current_namespace;
|
||||
if (path.has_value()) {
|
||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||
|
||||
|
|
@ -410,16 +441,16 @@ std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
definition::Namespace* current_namespace,
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
utils::IdType current_namespace,
|
||||
const std::vector<std::string>& path) {
|
||||
definition::Namespace* next_namespace = current_namespace;
|
||||
utils::IdType next_namespace = current_namespace;
|
||||
for (auto& name : path) {
|
||||
auto next_namespace_iter = next_namespace->namespaces.find(name);
|
||||
if (next_namespace_iter == next_namespace->namespaces.end()) {
|
||||
auto next_namespace_iter = global_info_.namespaces_[next_namespace].namespaces.find(name);
|
||||
if (next_namespace_iter == global_info_.namespaces_[next_namespace].namespaces.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
next_namespace = &next_namespace_iter->second;
|
||||
next_namespace = next_namespace_iter->second;
|
||||
}
|
||||
|
||||
return next_namespace;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue