mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-15 11:28:45 +00:00
new symbol table structure
This commit is contained in:
parent
782a48c4ff
commit
25355974a2
5 changed files with 82 additions and 57 deletions
|
|
@ -50,36 +50,78 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
size_t id = 0;
|
||||
|
||||
function_info->declaration = std::move(function_declaration_info);
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
|
||||
return global_info_.functions_.GetId(function_info);
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().declaration = std::move(function_declaration_info);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
global_info_.functions_[id].declaration = std::move(function_declaration_info);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
size_t id = 0;
|
||||
|
||||
function_info->definition = std::move(function_definition_info);
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
|
||||
return global_info_.functions_.GetId(function_info);
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().definition = std::move(function_definition_info);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
global_info_.functions_[id].definition = std::move(function_definition_info);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||
TypeInfo&& type_info) {
|
||||
namespace_stack_.back()->types[type] = std::move(type_info);
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(type);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
id = global_info_.types_.size();
|
||||
namespace_stack_.back()->functions[type] = id;
|
||||
global_info_.types_.push_back(std::move(type_info));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
TypeclassInfo&& typeclass_info) {
|
||||
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||
size_t id = 0;
|
||||
|
||||
return global_info_.functions_.GetId(typeclass_info);
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(typeclass);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
id = global_info_.typeclasses_.size();
|
||||
namespace_stack_.back()->functions[typeclass] = id;
|
||||
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<NamespaceInfo>(path,
|
||||
return FindSomething<NamespaceInfo*>(path,
|
||||
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||
return current_namespace;
|
||||
});
|
||||
|
|
@ -88,22 +130,16 @@ std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const
|
|||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name) {
|
||||
auto result = FindSomething<FunctionInfo>(path,
|
||||
[name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto function_info_iter = current_namespace->functions.find(name);
|
||||
if (function_info_iter == current_namespace->functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return &function_info_iter->second;
|
||||
return function_info_iter->second;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.functions_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||
|
|
@ -112,8 +148,8 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
|||
const std::string& name) {
|
||||
// TODO: remove overhead
|
||||
|
||||
auto result = GlobalInfo::NamespaceVisitor::FindSomething<FunctionInfo>(path,
|
||||
[type, name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
||||
[type, name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
||||
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
||||
|
|
@ -127,65 +163,47 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
|||
continue;
|
||||
}
|
||||
|
||||
return &method_iter->second;
|
||||
return method_iter->second;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.functions_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string type) {
|
||||
auto result = FindSomething<TypeInfo>(path,
|
||||
[type] (NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[type] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto type_info_iter = current_namespace->types.find(type);
|
||||
if (type_info_iter == current_namespace->types.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return &type_info_iter->second;
|
||||
return type_info_iter->second;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.types_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string typeclass) {
|
||||
auto result = FindSomething<TypeclassInfo>(path,
|
||||
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return &typeclass_info_iter->second;
|
||||
return typeclass_info_iter->second;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.typeclasses_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T*>(NamespaceInfo*)> search_func) {
|
||||
std::function<std::optional<T>(NamespaceInfo*)> search_func) {
|
||||
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
NamespaceInfo* current_namespace = nullptr;
|
||||
if (path.has_value()) {
|
||||
|
|
@ -198,7 +216,7 @@ std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
|||
current_namespace = namespace_stack_[i];
|
||||
}
|
||||
|
||||
std::optional<T*> result = search_func(current_namespace);
|
||||
std::optional<T> result = search_func(current_namespace);
|
||||
|
||||
if (result.has_value()) {
|
||||
return result.value();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue