mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-10 00:48:44 +00:00
new symbol table structure
This commit is contained in:
parent
782a48c4ff
commit
25355974a2
5 changed files with 82 additions and 57 deletions
|
|
@ -59,8 +59,8 @@ public:
|
||||||
namespace_stack_({&global_info.global_namespace_}) {}
|
namespace_stack_({&global_info.global_namespace_}) {}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T*> FindSomething(const std::optional<std::vector<std::string>>& path,
|
std::optional<T> 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);
|
||||||
|
|
||||||
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
||||||
const std::vector<std::string>& path);
|
const std::vector<std::string>& path);
|
||||||
|
|
@ -74,22 +74,25 @@ public:
|
||||||
return NamespaceVisitor(*this);
|
return NamespaceVisitor(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remember about vector realloc
|
||||||
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
||||||
return functions_.GetValue(id);
|
return &functions_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remember about vector realloc
|
||||||
TypeInfo* GetTypeInfo(utils::IdType id) {
|
TypeInfo* GetTypeInfo(utils::IdType id) {
|
||||||
return types_.GetValue(id);
|
return &types_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remember about vector realloc
|
||||||
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
||||||
return typeclasses_.GetValue(id);
|
return &typeclasses_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
utils::Storage<FunctionInfo*> functions_;
|
std::vector<FunctionInfo> functions_;
|
||||||
utils::Storage<TypeInfo*> types_;
|
std::vector<TypeInfo> types_;
|
||||||
utils::Storage<TypeclassInfo*> typeclasses_;
|
std::vector<TypeclassInfo> typeclasses_;
|
||||||
|
|
||||||
NamespaceInfo global_namespace_;
|
NamespaceInfo global_namespace_;
|
||||||
std::vector<ImportInfo> imports_;
|
std::vector<ImportInfo> imports_;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <endian.h>
|
#include <endian.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -50,36 +50,78 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
||||||
|
|
||||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||||
FunctionDeclarationInfo&& function_declaration_info) {
|
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,
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||||
FunctionDefinitionInfo&& function_definition_info) {
|
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,
|
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||||
TypeInfo&& type_info) {
|
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,
|
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||||
TypeclassInfo&& typeclass_info) {
|
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) {
|
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*> {
|
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||||
return current_namespace;
|
return current_namespace;
|
||||||
});
|
});
|
||||||
|
|
@ -88,22 +130,16 @@ std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const
|
||||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
||||||
const std::optional<std::vector<std::string>>& path,
|
const std::optional<std::vector<std::string>>& path,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
auto result = FindSomething<FunctionInfo>(path,
|
return FindSomething<utils::IdType>(path,
|
||||||
[name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
[name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||||
|
|
||||||
auto function_info_iter = current_namespace->functions.find(name);
|
auto function_info_iter = current_namespace->functions.find(name);
|
||||||
if (function_info_iter == current_namespace->functions.end()) {
|
if (function_info_iter == current_namespace->functions.end()) {
|
||||||
return std::nullopt;
|
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(
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||||
|
|
@ -112,8 +148,8 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
// TODO: remove overhead
|
// TODO: remove overhead
|
||||||
|
|
||||||
auto result = GlobalInfo::NamespaceVisitor::FindSomething<FunctionInfo>(path,
|
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
||||||
[type, name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
[type, name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||||
|
|
||||||
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
||||||
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
||||||
|
|
@ -127,65 +163,47 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return &method_iter->second;
|
return method_iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::nullopt;
|
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(
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
||||||
const std::optional<std::vector<std::string>>& path,
|
const std::optional<std::vector<std::string>>& path,
|
||||||
const std::string type) {
|
const std::string type) {
|
||||||
auto result = FindSomething<TypeInfo>(path,
|
return FindSomething<utils::IdType>(path,
|
||||||
[type] (NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
|
[type] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||||
|
|
||||||
auto type_info_iter = current_namespace->types.find(type);
|
auto type_info_iter = current_namespace->types.find(type);
|
||||||
if (type_info_iter == current_namespace->types.end()) {
|
if (type_info_iter == current_namespace->types.end()) {
|
||||||
return std::nullopt;
|
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(
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||||
const std::optional<std::vector<std::string>>& path,
|
const std::optional<std::vector<std::string>>& path,
|
||||||
const std::string typeclass) {
|
const std::string typeclass) {
|
||||||
auto result = FindSomething<TypeclassInfo>(path,
|
return FindSomething<utils::IdType>(path,
|
||||||
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
|
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||||
|
|
||||||
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||||
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||||
return std::nullopt;
|
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>
|
template<typename T>
|
||||||
std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||||
const std::optional<std::vector<std::string>>& path,
|
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) {
|
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||||
NamespaceInfo* current_namespace = nullptr;
|
NamespaceInfo* current_namespace = nullptr;
|
||||||
if (path.has_value()) {
|
if (path.has_value()) {
|
||||||
|
|
@ -198,7 +216,7 @@ std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||||
current_namespace = namespace_stack_[i];
|
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()) {
|
if (result.has_value()) {
|
||||||
return result.value();
|
return result.value();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue