mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-09 16:38:45 +00:00
fixes, colored errors
This commit is contained in:
parent
4b4756b657
commit
047ead6fa3
11 changed files with 166 additions and 195 deletions
|
|
@ -24,20 +24,22 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
error_handling::HandleTypecheckError("Can't define basic type namespace", base_node);
|
||||
}
|
||||
|
||||
size_t id = global_info_.namespaces_.size();
|
||||
global_info_.namespaces_.emplace_back();
|
||||
definition::Namespace* namespace_info = &global_info_.namespaces_.back();
|
||||
auto current_namespaces = ChooseNamespaces(modifier, namespace_stack_.back());
|
||||
|
||||
utils::IdType id = 0;
|
||||
|
||||
auto namespace_iter = current_namespaces->find(name);
|
||||
if (namespace_iter == current_namespaces->end()) {
|
||||
id = global_info_.namespaces_.size();
|
||||
(*current_namespaces)[name] = id;
|
||||
global_info_.namespaces_.emplace_back();
|
||||
} else {
|
||||
id = namespace_iter->second;
|
||||
}
|
||||
definition::Namespace* namespace_info = &global_info_.namespaces_[id];
|
||||
|
||||
if (modifier.has_value()) {
|
||||
if (modifier.value() == utils::IsConstModifier::Const) {
|
||||
global_info_.namespaces_[namespace_stack_.back()].const_namespaces[name] = id;
|
||||
} else {
|
||||
global_info_.namespaces_[namespace_stack_.back()].var_namespaces[name] = id;
|
||||
}
|
||||
|
||||
namespace_info->modifier = modifier;
|
||||
} else {
|
||||
global_info_.namespaces_[namespace_stack_.back()].namespaces[name] = id;
|
||||
}
|
||||
|
||||
namespace_info->parent_namespace = namespace_stack_.back();
|
||||
|
|
@ -48,36 +50,15 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
current_path_.push_back(name);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
auto current_namespaces = ChooseNamespaces(modifier, i);
|
||||
auto namespace_iter = current_namespaces->find(name);
|
||||
if (namespace_iter != current_namespaces->end()) {
|
||||
namespace_stack_.push_back(namespace_iter->second);
|
||||
current_path_.push_back(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,51 +85,56 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
||||
const std::string& name,
|
||||
definition::FunctionDeclaration&& function_declaration_info) {
|
||||
|
||||
size_t id = 0;
|
||||
const std::string& name,
|
||||
definition::FunctionDeclaration&& function_declaration_info,
|
||||
const interpreter::tokens::BaseNode& base_node) {
|
||||
utils::IdType id = 0;
|
||||
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
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);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
if (global_info_.functions_.back().argument_count != function_declaration_info.argument_types.size()) {
|
||||
error_handling::HandleInternalError("Not same argument count in function definition and declaration",
|
||||
"GlobalInfo.NamespaceVisitor. AddFunctionDeclaration");
|
||||
if (global_info_.functions_[id].argument_count != function_declaration_info.argument_types.size()) {
|
||||
error_handling::HandleTypecheckError("Function declaration: not same argument count in function definition and declaration", base_node);
|
||||
}
|
||||
global_info_.functions_[id].declaration = std::move(function_declaration_info);
|
||||
}
|
||||
|
||||
if (global_info_.functions_[id].declaration.has_value()) {
|
||||
error_handling::HandleTypecheckError("Second function declaration", base_node);
|
||||
}
|
||||
global_info_.functions_[id].declaration = std::move(function_declaration_info);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||
definition::FunctionDefinition&& function_definition_info) {
|
||||
size_t id = 0;
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(
|
||||
const std::string& name,
|
||||
definition::FunctionDefinition&& function_definition_info,
|
||||
const interpreter::tokens::BaseNode& base_node) {
|
||||
utils::IdType id = 0;
|
||||
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
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);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
if (global_info_.functions_.back().argument_count != function_definition_info.argument_names.size() + 1) {
|
||||
error_handling::HandleInternalError("Not same argument count in function definition and declaration", "GlobalInfo");
|
||||
if (global_info_.functions_[id].argument_count != function_definition_info.argument_names.size() + 1) {
|
||||
error_handling::HandleTypecheckError("Function definition: not same argument count in function definition and declaration", base_node);
|
||||
}
|
||||
global_info_.functions_[id].definition = std::move(function_definition_info);
|
||||
}
|
||||
|
||||
if (global_info_.functions_[id].definition.has_value()) {
|
||||
error_handling::HandleTypecheckError("Second function definition", base_node);
|
||||
}
|
||||
global_info_.functions_[id].definition = std::move(function_definition_info);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +147,7 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
|||
error_handling::HandleTypecheckError("Can't redefine basic type", base_node);
|
||||
}
|
||||
|
||||
size_t id = 0;
|
||||
utils::IdType id = 0;
|
||||
|
||||
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
||||
|
||||
|
|
@ -224,17 +210,16 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& a
|
|||
error_handling::HandleTypecheckError("Can't redefine basic type as abstract type", base_node);
|
||||
}
|
||||
|
||||
if (!FindAbstractType(abstract_type).has_value()) {
|
||||
utils::IdType id = global_info_.abstract_types_.size();
|
||||
global_info_.name_to_abstract_type_[abstract_type] = id;
|
||||
global_info_.abstract_types_.push_back(std::move(abstract_type_info));
|
||||
|
||||
return id;
|
||||
if (FindAbstractType(abstract_type).has_value()) {
|
||||
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace",
|
||||
base_node);
|
||||
}
|
||||
|
||||
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace",
|
||||
base_node);
|
||||
return 0;
|
||||
utils::IdType id = global_info_.abstract_types_.size();
|
||||
global_info_.name_to_abstract_type_[abstract_type] = id;
|
||||
global_info_.abstract_types_.push_back(std::move(abstract_type_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
|
|
@ -244,17 +229,15 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& type
|
|||
error_handling::HandleTypecheckError("Can't redefine basic type as typeclass", base_node);
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
return id;
|
||||
if (FindTypeclass(typeclass).has_value()) {
|
||||
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace", base_node);
|
||||
}
|
||||
|
||||
utils::IdType 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", base_node);
|
||||
return 0;
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& constructor,
|
||||
|
|
@ -266,16 +249,15 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& co
|
|||
|
||||
auto constructor_id_iter = global_info_.namespaces_[namespace_stack_.back()].constructors.find(constructor);
|
||||
|
||||
if (constructor_id_iter == global_info_.namespaces_[namespace_stack_.back()].constructors.end()) {
|
||||
size_t id = global_info_.constructors_.size();
|
||||
global_info_.namespaces_[namespace_stack_.back()].constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
|
||||
return id;
|
||||
if (constructor_id_iter != global_info_.namespaces_[namespace_stack_.back()].constructors.end()) {
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace", base_node);
|
||||
}
|
||||
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace", base_node);
|
||||
exit(1);
|
||||
utils::IdType id = global_info_.constructors_.size();
|
||||
global_info_.namespaces_[namespace_stack_.back()].constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::string>& path,
|
||||
|
|
@ -417,8 +399,8 @@ template<typename T>
|
|||
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(utils::IdType)> search_func) {
|
||||
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
utils::IdType current_namespace;
|
||||
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
utils::IdType current_namespace = 0;
|
||||
if (path.has_value()) {
|
||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||
|
||||
|
|
@ -456,4 +438,21 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
|||
return next_namespace;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, utils::IdType>*
|
||||
GlobalInfo::NamespaceVisitor::ChooseNamespaces(std::optional<utils::IsConstModifier> modifier,
|
||||
utils::IdType namespace_id) {
|
||||
std::unordered_map<std::string, utils::IdType>* current_namespaces = nullptr;
|
||||
if (modifier.has_value()) {
|
||||
if (modifier.value() == utils::IsConstModifier::Const) {
|
||||
current_namespaces = &global_info_.namespaces_[namespace_id].const_namespaces;
|
||||
} else {
|
||||
current_namespaces = &global_info_.namespaces_[namespace_id].var_namespaces;
|
||||
}
|
||||
} else {
|
||||
current_namespaces = &global_info_.namespaces_[namespace_id].namespaces;
|
||||
}
|
||||
|
||||
return current_namespaces;
|
||||
}
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue