mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-26 16:58:45 +00:00
going to change symbol table structure
This commit is contained in:
parent
9c25cb1c6f
commit
782a48c4ff
11 changed files with 535 additions and 354 deletions
BIN
src/.global_info.cpp.kate-swp
Normal file
BIN
src/.global_info.cpp.kate-swp
Normal file
Binary file not shown.
226
src/global_info.cpp
Normal file
226
src/global_info.cpp
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
// for clangd
|
||||
#include "../include/global_info.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::AddImport(ImportInfo&& import_info,
|
||||
const std::optional<std::string>& name) {
|
||||
if (name.has_value()) {
|
||||
global_info_.usages_[name.value()] = std::move(import_info);
|
||||
} else {
|
||||
global_info_.imports_.push_back(std::move(import_info));
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
||||
const std::optional<NamespaceInfo::Modifier>& modifier,
|
||||
const std::optional<std::string>& variable) {
|
||||
NamespaceInfo* namespace_info = nullptr;
|
||||
if (variable.has_value()) {
|
||||
namespace_info = &namespace_stack_.back()->variable_namespaces[name].emplace_back();
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
|
||||
namespace_info->modifier = modifier;
|
||||
namespace_info->variable = variable;
|
||||
} else {
|
||||
namespace_info = &namespace_stack_.back()->namespaces[name];
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
}
|
||||
|
||||
namespace_info->type_name = name;
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
||||
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||
if (namespace_stack_.size() <= 1) {
|
||||
// error
|
||||
return;
|
||||
}
|
||||
|
||||
namespace_stack_.pop_back();
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
||||
namespace_stack_.clear();
|
||||
namespace_stack_.push_back(&global_info_.global_namespace_);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
|
||||
function_info->declaration = std::move(function_declaration_info);
|
||||
|
||||
return global_info_.functions_.GetId(function_info);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
|
||||
function_info->definition = std::move(function_definition_info);
|
||||
|
||||
return global_info_.functions_.GetId(function_info);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||
TypeInfo&& type_info) {
|
||||
namespace_stack_.back()->types[type] = std::move(type_info);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
TypeclassInfo&& typeclass_info) {
|
||||
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||
|
||||
return global_info_.functions_.GetId(typeclass_info);
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<NamespaceInfo>(path,
|
||||
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||
return current_namespace;
|
||||
});
|
||||
}
|
||||
|
||||
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*> {
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.functions_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type,
|
||||
const std::string& name) {
|
||||
// TODO: remove overhead
|
||||
|
||||
auto result = GlobalInfo::NamespaceVisitor::FindSomething<FunctionInfo>(path,
|
||||
[type, name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||
|
||||
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
||||
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
for (auto& variable_namespace : variable_namespaces_iter->second) {
|
||||
auto method_iter = variable_namespace.functions.find(name);
|
||||
|
||||
if (method_iter == variable_namespace.functions.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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*> {
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
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*> {
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
if (result.has_value()) {
|
||||
return global_info_.typeclasses_.GetId(result.value());
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
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()) {
|
||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||
|
||||
if (!maybe_namespace.has_value()) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
current_namespace = namespace_stack_[i];
|
||||
}
|
||||
|
||||
std::optional<T*> result = search_func(current_namespace);
|
||||
|
||||
if (result.has_value()) {
|
||||
return result.value();
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
NamespaceInfo* current_namespace,
|
||||
const std::vector<std::string>& path) {
|
||||
NamespaceInfo* 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()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
next_namespace = &next_namespace_iter->second;
|
||||
}
|
||||
|
||||
return next_namespace;
|
||||
}
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -7,7 +7,6 @@ namespace interpreter {
|
|||
// Sources -----------------
|
||||
|
||||
void TypeCheckVisitor::Visit(SourceFile* node) {
|
||||
out_ << "[SourceFile] (\n\n";
|
||||
for (auto& statement : node->statements) {
|
||||
if (std::holds_alternative<Partition>(statement)) {
|
||||
Visit(&std::get<Partition>(statement));
|
||||
|
|
@ -17,295 +16,197 @@ void TypeCheckVisitor::Visit(SourceFile* node) {
|
|||
// error
|
||||
}
|
||||
}
|
||||
out_ << "\n)\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(Sources* node) {
|
||||
out_ << "[Sources](\n";
|
||||
for (auto& statement : node->statements) {
|
||||
Visitor::Visit(statement);
|
||||
}
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void TypeCheckVisitor::Visit(Partition* node) {
|
||||
out_ << "[Partition] ";
|
||||
switch (node->name) {
|
||||
case Partition::Test:
|
||||
out_ << "TEST";
|
||||
break;
|
||||
case Partition::Interface:
|
||||
out_ << "INTERFACE";
|
||||
break;
|
||||
case Partition::Core:
|
||||
out_ << "CORE";
|
||||
break;
|
||||
case Partition::Lib:
|
||||
out_ << "LIB";
|
||||
break;
|
||||
case Partition::Module:
|
||||
out_ << "MODULE";
|
||||
break;
|
||||
case Partition::Exe:
|
||||
out_ << "EXE";
|
||||
break;
|
||||
}
|
||||
out_ << " {\n";
|
||||
Visit(node->scope.get());
|
||||
out_ << "}\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(Namespace* node) {
|
||||
out_ << "[Namespace] ";
|
||||
if (node->name.has_value()) {
|
||||
if (node->modifier.has_value()) {
|
||||
switch (node->modifier.value()) {
|
||||
switch (node->modifier.value()) { // TODO
|
||||
case Namespace::Const:
|
||||
out_ << "const ";
|
||||
// TODO
|
||||
break;
|
||||
case Namespace::Var:
|
||||
out_ << "var ";
|
||||
// TODO
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
Visit(&node->name.value());
|
||||
context_manager_.EnterVariableContext(node->name.value().name,
|
||||
global_info_.FindType(std::nullopt, node->type).value()); // TODO ??
|
||||
}
|
||||
Visit(&node->type);
|
||||
out_ << "{\n";
|
||||
// global_info_.FindType(std::nullopt, node->type); // ??
|
||||
|
||||
global_info_.EnterNamespace(node->type);
|
||||
|
||||
Visit(node->scope.get());
|
||||
out_ << "}\n";
|
||||
|
||||
global_info_.ExitNamespace();
|
||||
}
|
||||
|
||||
// Definitions -----------------
|
||||
|
||||
void TypeCheckVisitor::Visit(ImportStatement* node) {
|
||||
if (node->name.has_value()) {
|
||||
out_ << "[Use " << node->name.value() << "] = ";
|
||||
}
|
||||
out_ << "[Import " << node->module_name << "]";
|
||||
if (!node->symbols.empty()) {
|
||||
out_ << " (\n";
|
||||
for (auto& symbol : node->symbols) {
|
||||
Visit(&symbol);
|
||||
out_ << '\n';
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << '\n';
|
||||
void TypeCheckVisitor::Visit(ImportStatement* node) { // TODO
|
||||
// if (node->name.has_value()) {
|
||||
// }
|
||||
// if (!node->symbols.empty()) {
|
||||
// for (auto& symbol : node->symbols) {
|
||||
// Visit(&symbol);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
||||
out_ << "[Alias ";
|
||||
switch (node->modifier) {
|
||||
case AliasDefinitionStatement::Alias:
|
||||
out_ << "alias";
|
||||
break;
|
||||
case AliasDefinitionStatement::Type:
|
||||
out_ << "type";
|
||||
break;
|
||||
case AliasDefinitionStatement::Let:
|
||||
out_ << "let";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
|
||||
|
||||
Visit(&node->type);
|
||||
out_ << "] = (";
|
||||
Visit(node->value.get());
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
out_ << "[Variable ";
|
||||
switch (node->modifier) {
|
||||
case VariableDefinitionStatement::Const:
|
||||
out_ << "const";
|
||||
break;
|
||||
case VariableDefinitionStatement::Var:
|
||||
out_ << "var";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
Visitor::Visit(node->name);
|
||||
out_ << "] = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
||||
out_ << "[FunctionDeclaration ";
|
||||
Visit(&node->name);
|
||||
out_ << "] (";
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
out_ << ") : (";
|
||||
Visit(node->type.get());
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||
out_ << "[Function] (";
|
||||
Visit(node->definition.get());
|
||||
out_ << ") = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
|
||||
out_ << "[Type ";
|
||||
switch (node->modifier) {
|
||||
case TypeDefinitionStatement::Struct:
|
||||
out_ << "struct";
|
||||
break;
|
||||
case TypeDefinitionStatement::Class:
|
||||
out_ << "class";
|
||||
break;
|
||||
}
|
||||
out_ << "] (";
|
||||
Visit(node->definition.get());
|
||||
out_ << ") = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||
out_ << "[AbstractType ";
|
||||
switch (node->modifier) {
|
||||
case AbstractTypeDefinitionStatement::Basic:
|
||||
out_ << "basic";
|
||||
break;
|
||||
case AbstractTypeDefinitionStatement::Abstract:
|
||||
out_ << "abstract";
|
||||
break;
|
||||
}
|
||||
out_ << "] (";
|
||||
Visit(node->type.get());
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||
out_ << "[Typeclass] (";
|
||||
Visit(node->definition.get());
|
||||
if (!node->requirements.empty()) {
|
||||
out_ << ") : (\n";
|
||||
}
|
||||
for (auto& requirement : node->requirements) {
|
||||
out_ << "& ";
|
||||
Visit(requirement.get());
|
||||
out_ << "\n";
|
||||
}
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
// Definition parts
|
||||
|
||||
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
|
||||
out_ << "[FunctionDefinition ";
|
||||
switch (node->modifier) {
|
||||
case FunctionDefinition::Operator:
|
||||
out_ << "operator";
|
||||
break;
|
||||
case FunctionDefinition::Function:
|
||||
out_ << "function";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
Visit(&node->name);
|
||||
out_ << "]";
|
||||
if (!node->parameters.empty()) {
|
||||
out_ << " (";
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
if (!node->arguments.empty()) {
|
||||
out_ << " : (";
|
||||
for (auto& argument : node->arguments) {
|
||||
Visit(&argument);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(TypeDefinition* node) {
|
||||
out_ << "[TypeDefinition] (";
|
||||
Visit(node->type.get());
|
||||
out_ << ')';
|
||||
if (!node->parameters.empty()) {
|
||||
out_ << '(';
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
|
||||
out_ << "[Annotated (Abstract) Type ";
|
||||
Visit(&node->type);
|
||||
out_ << ']';
|
||||
if (!node->typeclasses.empty() > 0) {
|
||||
out_ << " (";
|
||||
for (auto& typeclass : node->typeclasses) {
|
||||
Visitor::Visit(typeclass);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
void TypeCheckVisitor::Visit(MatchCase* node) {
|
||||
out_ << "[MatchCase | ";
|
||||
Visitor::Visit(node->value);
|
||||
if (node->condition.has_value()) {
|
||||
out_ << " ? ";
|
||||
Visitor::Visit(node->condition.value());
|
||||
}
|
||||
if (node->statement.has_value()) {
|
||||
out_ << " -> ";
|
||||
Visitor::Visit(node->statement.value());
|
||||
}
|
||||
out_ << "]\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(Match* node) {
|
||||
out_ << "[Match] (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ") [with] (\n";
|
||||
for (auto& match_case : node->matches) {
|
||||
Visit(&match_case);
|
||||
}
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(Condition* node) {
|
||||
out_ << "[If] (";
|
||||
Visitor::Visit(node->conditions[0]);
|
||||
out_ << ") [then] (\n";
|
||||
Visitor::Visit(node->statements[0]);
|
||||
out_ << ')';
|
||||
for (size_t i = 1; i < node->conditions.size(); ++i) {
|
||||
out_ << " [elif] (";
|
||||
Visitor::Visit(node->conditions[i]);
|
||||
out_ << ") [then] (\n";
|
||||
Visitor::Visit(node->statements[i]);
|
||||
out_ << ')';
|
||||
}
|
||||
if (node->statements.size() > node->conditions.size()) {
|
||||
out_ << " [else] (\n";
|
||||
Visitor::Visit(node->statements[node->conditions.size()]);
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << '\n';
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
|
||||
|
|
@ -657,16 +558,8 @@ void TypeCheckVisitor::Visit(TypeclassExpression* node) {
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void TypeCheckVisitor::Visit(ExtendedName* node) {
|
||||
out_ << "[ExtendedName " << node->name << "] ";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(std::string* node) { // std::string
|
||||
out_ << "[Identifier " << *node << "] ";
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
|
||||
out_ << "[FloatNumber " << node->value << "] ";
|
||||
current_type_ = info::BuiltInTypeInfo::FloatT;
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(NumberLiteral* node) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue