mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-07 15:38:47 +00:00
type structs -> type classes
This commit is contained in:
parent
a512a92f92
commit
648f78afa3
14 changed files with 638 additions and 383 deletions
|
|
@ -31,9 +31,17 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
current_path_.push_back(name);
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
||||
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
error_handling::HandleInternalError("Can't find namespace", "GlobalInfo.NamespaceVisitor.EnterNamespace");
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||
|
|
@ -100,7 +108,7 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::str
|
|||
}
|
||||
|
||||
// TODO: internal types, etc.
|
||||
// TODO: extended constructor names
|
||||
// TODO: extended constructor names (point separated names)
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||
definition::Type&& type_info) {
|
||||
size_t id = 0;
|
||||
|
|
@ -153,38 +161,44 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
|||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
definition::Typeclass&& typeclass_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto typeclass_id_iter = namespace_stack_.back()->typeclasses.find(typeclass);
|
||||
|
||||
if (typeclass_id_iter == namespace_stack_.back()->typeclasses.end()) {
|
||||
id = global_info_.typeclasses_.size();
|
||||
namespace_stack_.back()->typeclasses[typeclass] = id;
|
||||
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace");
|
||||
// TODO: link abstract type with let definitions
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& abstract_type,
|
||||
definition::AbstractType&& abstract_type_info) {
|
||||
if (!FindAbstractType(abstract_type).has_value()) {
|
||||
size_t id = global_info_.abstract_types_.size();
|
||||
global_info_.name_to_abstract_type_[abstract_type] = id;
|
||||
global_info_.abstract_types_.push_b(ack(std::move(abstract_type_info));
|
||||
}
|
||||
|
||||
return id;
|
||||
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace");
|
||||
return 0;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
definition::Typeclass&& typeclass_info) {
|
||||
if (!FindTypeclass(typeclass).has_value()) {
|
||||
size_t id = global_info_.typeclasses_.size();
|
||||
global_info_.name_to_typeclass_[typeclass] = id;
|
||||
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
||||
}
|
||||
|
||||
|
||||
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace");
|
||||
return 0;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& constructor,
|
||||
definition::Constructor&& constructor_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto constructor_id_iter = namespace_stack_.back()->constructors.find(constructor);
|
||||
|
||||
if (constructor_id_iter == namespace_stack_.back()->constructors.end()) {
|
||||
id = global_info_.constructors_.size();
|
||||
size_t id = global_info_.constructors_.size();
|
||||
namespace_stack_.back()->constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace");
|
||||
}
|
||||
|
||||
return id;
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace");
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
|
|
@ -247,19 +261,24 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
|||
});
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& typeclass) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[typeclass] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
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 typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (abstract_type_id_iter != global_info_.name_to_abstract_type_.end()) {
|
||||
return abstract_type_id_iter->second;
|
||||
}
|
||||
|
||||
return typeclass_info_iter->second;
|
||||
});
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(const std::string& typeclass) {
|
||||
auto typeclass_id_iter = global_info_.name_to_typeclass_.find(typeclass);
|
||||
|
||||
if (typeclass_id_iter != global_info_.name_to_typeclass_.end()) {
|
||||
return typeclass_id_iter->second;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue