mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-30 10:48:16 +00:00
namespace storage fix, namespace enter fix, maybe other fixes
This commit is contained in:
parent
4882d458f8
commit
4b4756b657
11 changed files with 250 additions and 174 deletions
|
|
@ -271,7 +271,7 @@ void BuildVisitor::Visit(TypeclassDefinitionStatement* node) {
|
|||
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
if (parse_node.PreviousSibling().GetValue() != "var") {
|
||||
if (current_node_.PreviousSibling().GetValue() != "var") {
|
||||
node->function_requirements.push_back(std::make_unique<FunctionDeclaration>());
|
||||
Visit(node->function_requirements.back().get());
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,9 @@ namespace interpreter {
|
|||
|
||||
void FindSymbolsVisitor::Visit(Namespace* node) {
|
||||
namespace_visitor_.AddEnterNamespace(node->type, node->modifier, node->base);
|
||||
|
||||
Visitor::Visit(&node->scope);
|
||||
|
||||
namespace_visitor_.ExitNamespace();
|
||||
}
|
||||
|
||||
|
|
@ -118,6 +120,7 @@ void FindSymbolsVisitor::Visit(TypeDefinitionStatement* node) {
|
|||
|
||||
any_type_info.value = &node->value;
|
||||
any_type_info.parent_namespace = namespace_visitor_.GetCurrentNamespace();
|
||||
any_type_info.modifier = node->modifier;
|
||||
|
||||
std::string type = any_type_info.type.type;
|
||||
|
||||
|
|
@ -160,14 +163,14 @@ void FindSymbolsVisitor::Visit(TypeclassDefinitionStatement* node) {
|
|||
current_info_.reset();
|
||||
}
|
||||
|
||||
info.function_requirements.reserve(node->function_requirements.size());
|
||||
info.function_requirements.resize(node->function_requirements.size());
|
||||
for (size_t i = 0; i < node->function_requirements.size(); ++i) {
|
||||
Visit(node->function_requirements[i].get());
|
||||
info.function_requirements[i] = std::move(std::any_cast<info::definition::FunctionDeclaration>(current_info_));
|
||||
current_info_.reset();
|
||||
}
|
||||
|
||||
info.method_requirements.reserve(node->method_requirements.size());
|
||||
info.method_requirements.resize(node->method_requirements.size());
|
||||
for (size_t i = 0; i < node->method_requirements.size(); ++i) {
|
||||
Visit(node->method_requirements[i].get());
|
||||
info.method_requirements[i] = std::move(std::any_cast<info::definition::FunctionDeclaration>(current_info_));
|
||||
|
|
|
|||
|
|
@ -24,44 +24,71 @@ void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|||
error_handling::HandleTypecheckError("Can't define basic type namespace", base_node);
|
||||
}
|
||||
|
||||
definition::Namespace* namespace_info = nullptr;
|
||||
size_t id = global_info_.namespaces_.size();
|
||||
global_info_.namespaces_.emplace_back();
|
||||
definition::Namespace* namespace_info = &global_info_.namespaces_.back();
|
||||
|
||||
if (modifier.has_value()) {
|
||||
if (modifier.value() == utils::IsConstModifier::Const) {
|
||||
namespace_info = &namespace_stack_.back()->const_namespaces[name];
|
||||
global_info_.namespaces_[namespace_stack_.back()].const_namespaces[name] = id;
|
||||
} else {
|
||||
namespace_info = &namespace_stack_.back()->var_namespaces[name];
|
||||
global_info_.namespaces_[namespace_stack_.back()].var_namespaces[name] = id;
|
||||
}
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
|
||||
namespace_info->modifier = modifier;
|
||||
} else {
|
||||
namespace_info = &namespace_stack_.back()->namespaces[name];
|
||||
namespace_stack_.push_back(namespace_info);
|
||||
global_info_.namespaces_[namespace_stack_.back()].namespaces[name] = id;
|
||||
}
|
||||
|
||||
namespace_info->parent_namespace = namespace_stack_.back();
|
||||
|
||||
namespace_info->type_name = name;
|
||||
|
||||
namespace_stack_.push_back(id);
|
||||
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;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error_handling::HandleInternalError("Can't find namespace",
|
||||
error_handling::HandleInternalError("Can't find namespace " + name,
|
||||
"GlobalInfo.NamespaceVisitor.EnterNamespace");
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||
if (namespace_stack_.size() <= 1) {
|
||||
// error
|
||||
error_handling::HandleInternalError("Can't exit from global namespace",
|
||||
"GlobalInfo.NamespaceVisitor.ExitNamespace");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +100,7 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
namespace_stack_.clear();
|
||||
current_path_.clear();
|
||||
|
||||
namespace_stack_.push_back(&global_info_.global_namespace_);
|
||||
namespace_stack_.push_back(global_info_.GlobalNamespaceId);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
||||
|
|
@ -82,11 +109,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
|||
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
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);
|
||||
|
|
@ -106,11 +133,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::str
|
|||
definition::FunctionDefinition&& function_definition_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
||||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
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);
|
||||
|
|
@ -136,11 +163,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
|||
|
||||
size_t id = 0;
|
||||
|
||||
auto type_id_iter = namespace_stack_.back()->types.find(type);
|
||||
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
||||
|
||||
if (type_id_iter == namespace_stack_.back()->types.end()) {
|
||||
if (type_id_iter == global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
||||
id = global_info_.types_.size();
|
||||
namespace_stack_.back()->types[type] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].types[type] = id;
|
||||
global_info_.types_.push_back(std::move(type_info));
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("More then one type with the same name in namespace", base_node);
|
||||
|
|
@ -221,6 +248,8 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& type
|
|||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -235,16 +264,18 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& co
|
|||
error_handling::HandleTypecheckError("Can't redefine basic type as constructor", base_node);
|
||||
}
|
||||
|
||||
auto constructor_id_iter = namespace_stack_.back()->constructors.find(constructor);
|
||||
auto constructor_id_iter = global_info_.namespaces_[namespace_stack_.back()].constructors.find(constructor);
|
||||
|
||||
if (constructor_id_iter == namespace_stack_.back()->constructors.end()) {
|
||||
if (constructor_id_iter == global_info_.namespaces_[namespace_stack_.back()].constructors.end()) {
|
||||
size_t id = global_info_.constructors_.size();
|
||||
namespace_stack_.back()->constructors[constructor] = id;
|
||||
global_info_.namespaces_[namespace_stack_.back()].constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace", base_node);
|
||||
return 0;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::string>& path,
|
||||
|
|
@ -269,9 +300,9 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::
|
|||
return id;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<definition::Namespace*>(path,
|
||||
[] (definition::Namespace* current_namespace) -> std::optional<definition::Namespace*> {
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
return current_namespace;
|
||||
});
|
||||
}
|
||||
|
|
@ -280,10 +311,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[name] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[name, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto function_info_iter = current_namespace->functions.find(name);
|
||||
if (function_info_iter == current_namespace->functions.end()) {
|
||||
auto function_info_iter = global_info_.namespaces_[current_namespace].functions.find(name);
|
||||
if (function_info_iter == global_info_.namespaces_[current_namespace].functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -299,22 +330,22 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
|||
// TODO: remove overhead
|
||||
|
||||
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
||||
[type, name, modifier] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type, name, modifier, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
|
||||
auto variable_namespace_iter =
|
||||
(modifier == utils::IsConstModifier::Const
|
||||
? current_namespace->const_namespaces.find(type)
|
||||
: current_namespace->var_namespaces.find(type));
|
||||
? global_info_.namespaces_[current_namespace].const_namespaces.find(type)
|
||||
: global_info_.namespaces_[current_namespace].var_namespaces.find(type));
|
||||
if (variable_namespace_iter ==
|
||||
(modifier == utils::IsConstModifier::Const
|
||||
? current_namespace->const_namespaces.end()
|
||||
: current_namespace->var_namespaces.end())) {
|
||||
? global_info_.namespaces_[current_namespace].const_namespaces.end()
|
||||
: global_info_.namespaces_[current_namespace].var_namespaces.end())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto method_iter = variable_namespace_iter->second.functions.find(name);
|
||||
if (method_iter == variable_namespace_iter->second.functions.end()) {
|
||||
auto method_iter = global_info_.namespaces_[variable_namespace_iter->second].functions.find(name);
|
||||
if (method_iter == global_info_.namespaces_[variable_namespace_iter->second].functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -326,10 +357,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[type] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto type_info_iter = current_namespace->types.find(type);
|
||||
if (type_info_iter == current_namespace->types.end()) {
|
||||
auto type_info_iter = global_info_.namespaces_[current_namespace].types.find(type);
|
||||
if (type_info_iter == global_info_.namespaces_[current_namespace].types.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -338,9 +369,9 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
|||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindLocalType(const std::string& type) {
|
||||
auto type_id_iter = namespace_stack_.back()->types.find(type);
|
||||
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
||||
|
||||
if (type_id_iter != namespace_stack_.back()->types.end()) {
|
||||
if (type_id_iter != global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
||||
return type_id_iter->second;
|
||||
}
|
||||
|
||||
|
|
@ -371,10 +402,10 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
|||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& constructor) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[constructor] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
[constructor, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto constructor_info_iter = current_namespace->constructors.find(constructor);
|
||||
if (constructor_info_iter == current_namespace->constructors.end()) {
|
||||
auto constructor_info_iter = global_info_.namespaces_[current_namespace].constructors.find(constructor);
|
||||
if (constructor_info_iter == global_info_.namespaces_[current_namespace].constructors.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -385,9 +416,9 @@ std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
|||
template<typename T>
|
||||
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(definition::Namespace*)> search_func) {
|
||||
std::function<std::optional<T>(utils::IdType)> search_func) {
|
||||
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
definition::Namespace* current_namespace = nullptr;
|
||||
utils::IdType current_namespace;
|
||||
if (path.has_value()) {
|
||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||
|
||||
|
|
@ -410,16 +441,16 @@ std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
definition::Namespace* current_namespace,
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
utils::IdType current_namespace,
|
||||
const std::vector<std::string>& path) {
|
||||
definition::Namespace* next_namespace = current_namespace;
|
||||
utils::IdType 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()) {
|
||||
auto next_namespace_iter = global_info_.namespaces_[next_namespace].namespaces.find(name);
|
||||
if (next_namespace_iter == global_info_.namespaces_[next_namespace].namespaces.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
next_namespace = &next_namespace_iter->second;
|
||||
next_namespace = next_namespace_iter->second;
|
||||
}
|
||||
|
||||
return next_namespace;
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ void LinkSymbolsVisitor::Visit(Namespace* node) {
|
|||
node->link_typeclass_id_ = maybe_typeclass.value();
|
||||
}
|
||||
|
||||
namespace_visitor_.EnterNamespace(node->type);
|
||||
namespace_visitor_.EnterNamespace(node->type, node->modifier);
|
||||
|
||||
Visitor::Visit(&node->scope);
|
||||
|
||||
|
|
@ -64,13 +64,17 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
return;
|
||||
}
|
||||
|
||||
// check made in typecheck visitor ??
|
||||
//////////////////////////////////////////////////////////
|
||||
// abstract / basic / TODO: local abstract type
|
||||
if (path.size() == 0 && namespace_visitor_.FindAbstractType(node->type.type).has_value()) { // TODO
|
||||
return;
|
||||
}
|
||||
// if (path.size() == 0 && (namespace_visitor_.FindAbstractType(node->type.type).has_value()) {
|
||||
// return;
|
||||
// }
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) { // TODO: check, that not bastract types
|
||||
error_handling::HandleTypecheckError("Type or constructor not found", node->base);
|
||||
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) {
|
||||
// check made in typecheck visitor ??
|
||||
return;
|
||||
}
|
||||
|
||||
if (node->constructor_id_.has_value()) {
|
||||
|
|
@ -87,22 +91,18 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
node->type.type_id_ = node->type_id_.value();
|
||||
}
|
||||
|
||||
std::optional<info::definition::Namespace*> maybe_type_namespace = namespace_visitor_.FindNamespace(path);
|
||||
std::optional<utils::IdType> maybe_type_namespace = namespace_visitor_.FindNamespace(path);
|
||||
if (maybe_type_namespace.has_value()) {
|
||||
info::definition::Namespace* type_namespace = maybe_type_namespace.value();
|
||||
info::definition::Namespace* type_namespace = &namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(maybe_type_namespace.value());
|
||||
|
||||
for (ssize_t i = (ssize_t)node->path.size(); i >= 0; --i) {
|
||||
info::definition::Namespace* parent_namespace = type_namespace->parent_namespace;
|
||||
info::definition::Namespace* parent_namespace = &namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(type_namespace->parent_namespace);
|
||||
|
||||
auto type_iter = parent_namespace->types.find(type_namespace->type_name);
|
||||
if (type_iter != parent_namespace->types.end()) {
|
||||
node->path[i].type_id_ = type_iter->second;
|
||||
}
|
||||
|
||||
if (parent_namespace == nullptr) {
|
||||
error_handling::HandleInternalError("Parent namespace is null in type expression", "LinkSymbolsVisitor");
|
||||
}
|
||||
|
||||
type_namespace = parent_namespace;
|
||||
}
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ void LinkSymbolsVisitor::Visit(ParametrizedTypeclass* node) { // TODO: check
|
|||
if (maybe_typeclass.has_value()) {
|
||||
node->typeclass_id_ = maybe_typeclass.value();
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found", node->base);
|
||||
error_handling::HandleTypecheckError("Typeclass not found", node->base);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,21 +45,39 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
|
|||
abstract_type);
|
||||
context_manager_.DefineLocalType(node->type, abstract_type);
|
||||
} else if (node->link_type_id_.has_value()) {
|
||||
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value()->value); // handle error?
|
||||
auto maybe_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) {
|
||||
error_handling::HandleTypecheckError("Namespace type is not AnyType", node->base);
|
||||
}
|
||||
|
||||
Visitor::Visit(*maybe_type_info.value()->value); // handle error?
|
||||
|
||||
context_manager_.EnterVariableContext(
|
||||
utils::ClassInternalVarName, // TODO: different name
|
||||
utils::ClassInternalVarName,
|
||||
context_manager_.AddValue(
|
||||
info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetValueManager()),
|
||||
info::type::DefinedType(node->link_type_id_.value(),
|
||||
current_type_,
|
||||
maybe_type_info.value()->modifier,
|
||||
context_manager_.GetValueManager()),
|
||||
IsConstModifierToValueType(node->modifier.value())));
|
||||
}
|
||||
} else {
|
||||
if (node->link_typeclass_id_.has_value()) { // ??
|
||||
if (node->link_typeclass_id_.has_value()) { // TODO
|
||||
error_handling::HandleTypecheckError("Typeclass can't have not variable namespace", node->base);
|
||||
}
|
||||
|
||||
context_manager_.EnterContext();
|
||||
}
|
||||
namespace_visitor_.EnterNamespace(node->type);
|
||||
|
||||
if (node->link_type_id_.has_value()) {
|
||||
if (type_namespaces_.count(node->link_type_id_.value()) != 0) {
|
||||
error_handling::HandleTypecheckError("Namespaces of one type one in another", node->base);
|
||||
}
|
||||
type_namespaces_.insert(node->link_type_id_.value());
|
||||
}
|
||||
|
||||
namespace_visitor_.EnterNamespace(node->type, node->modifier);
|
||||
|
||||
Visit(&node->scope);
|
||||
|
||||
|
|
@ -67,6 +85,10 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
|
|||
context_manager_.ExitContext();
|
||||
current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||
|
||||
if (node->link_type_id_.has_value()) {
|
||||
type_namespaces_.erase(node->link_type_id_.value());
|
||||
}
|
||||
|
||||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
|
|
@ -79,26 +101,6 @@ void TypeCheckVisitor::Visit(ImportStatement* node) {}
|
|||
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
||||
error_handling::HandleInternalError("Unimplemented",
|
||||
"TypeCheckVisitor.AliasDefinitionStatement");
|
||||
// if (!is_in_statement_) { // do nothing if alias is not applied
|
||||
// current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// context_manager_.EnterContext();
|
||||
//
|
||||
// for (auto& parameter : node->parameters) {
|
||||
// current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||
// parameter,
|
||||
// /*TODO*/{}),
|
||||
// utils::ValueType::Tmp);
|
||||
// context_manager_.DefineLocalType(parameter, current_type_);
|
||||
// }
|
||||
//
|
||||
// Visit(node->value.get());
|
||||
//
|
||||
// context_manager_.ExitContext();
|
||||
//
|
||||
// node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
|
|
@ -152,13 +154,13 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
|||
is_in_statement_ = true;
|
||||
context_manager_.EnterContext();
|
||||
|
||||
const info::definition::Function& function_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_);
|
||||
info::definition::Function& function_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(node->function_id_);
|
||||
|
||||
if (!function_info.declaration.has_value()) {
|
||||
error_handling::HandleTypecheckError("Function defined, but not declared", node->base);
|
||||
}
|
||||
|
||||
const info::definition::FunctionDeclaration& declaration = function_info.declaration.value();
|
||||
info::definition::FunctionDeclaration& declaration = function_info.declaration.value();
|
||||
|
||||
for (auto parameter : declaration.parameters) {
|
||||
std::vector<utils::IdType> requirements;
|
||||
|
|
@ -598,7 +600,7 @@ void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
|
|||
void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
|
||||
// TODO: Check, that type is not abstract ??
|
||||
auto maybe_operator_id = namespace_visitor_.FindFunction(std::nullopt, node->operator_name);
|
||||
const info::definition::Function* operator_info = nullptr;
|
||||
info::definition::Function* operator_info = nullptr;
|
||||
|
||||
node->is_method_ = false;
|
||||
|
||||
|
|
@ -757,7 +759,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
// TODO: better decision ??
|
||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value()->parent_namespace->const_namespaces.at(maybe_type_info.value()->type.type).functions[node->name];
|
||||
std::optional<utils::IdType> maybe_const_function_id = namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(maybe_type_info.value()->parent_namespace).const_namespaces.at(maybe_type_info.value()->type.type)).functions[node->name];
|
||||
|
||||
utils::ValueType expression_value_type = context_manager_.GetValueType(current_type_);
|
||||
|
||||
|
|
@ -768,7 +770,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
|
||||
if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) {
|
||||
// TODO: choose expression value types
|
||||
maybe_function_id = maybe_type_info.value()->parent_namespace->var_namespaces.at(maybe_type_info.value()->type.type).functions[node->name];
|
||||
maybe_function_id = namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(namespace_visitor_.GetGlobalInfo()->GetNamespaceInfo(maybe_type_info.value()->parent_namespace).var_namespaces.at(maybe_type_info.value()->type.type)).functions[node->name];
|
||||
}
|
||||
|
||||
if (maybe_const_function_id.has_value() && maybe_function_id.has_value()) { // TODO: handle on link_types stage
|
||||
|
|
@ -1032,7 +1034,8 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
|
|||
utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_);
|
||||
|
||||
for (size_t i = 1; i < node->names.size(); ++i) {
|
||||
std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i]);
|
||||
std::optional<utils::IdType> maybe_type_value =
|
||||
context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i], type_namespaces_);
|
||||
if (!maybe_type_value.has_value()) {
|
||||
error_handling::HandleTypecheckError("Variable field not found", node->base);
|
||||
}
|
||||
|
|
@ -1251,7 +1254,6 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
|
|||
|
||||
if (maybe_internal_type.has_value()) {
|
||||
// checks made in link_symbols_visitor
|
||||
|
||||
current_type_ = context_manager_.AddValue<info::type::InternalType>(maybe_internal_type.value(), utils::ValueType::Tmp);
|
||||
} else {
|
||||
std::optional<utils::IdType> maybe_local_abstract_type = context_manager_.GetLocalType(node->type.type);
|
||||
|
|
@ -1268,6 +1270,11 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
|
|||
|
||||
Visitor::Visit(*maybe_type_info.value()->value);
|
||||
current_type_ = TypeInContext(current_type_, context);
|
||||
current_type_ = context_manager_.AddValue(info::type::DefinedType(node->type_id_.value(),
|
||||
current_type_,
|
||||
maybe_type_info.value()->modifier,
|
||||
context_manager_.GetValueManager()),
|
||||
utils::ValueType::Tmp);
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found", node->base);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ bool AbstractType::operator>(const AbstractType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -59,8 +60,12 @@ bool DefinedType::operator>(const DefinedType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name) const {
|
||||
return type_manager_->GetAnyValue(type_)->GetFieldType(name);
|
||||
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
if (class_modifier_ == utils::ClassModifier::Struct || type_namespaces.count(type_id_) != 0) {
|
||||
return type_manager_->GetAnyValue(type_)->GetFieldType(name, type_namespaces);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
//
|
||||
|
|
@ -109,7 +114,8 @@ bool TupleType::operator>(const TupleType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
for (size_t i = 0; i < fields_.size(); ++i) { // TODO: optimize??
|
||||
if (fields_[i].first.has_value() && fields_[i].first.value() == name) {
|
||||
return fields_[i].second;
|
||||
|
|
@ -160,9 +166,10 @@ bool VariantType::operator>(const VariantType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
if (current_constructor_.has_value()) {
|
||||
return constructors_.at(current_constructor_.value()).GetFieldType(name);
|
||||
return constructors_.at(current_constructor_.value()).GetFieldType(name, type_namespaces);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
@ -191,7 +198,8 @@ bool OptionalType::operator>(const OptionalType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -219,8 +227,9 @@ bool ReferenceToType::operator>(const ReferenceToType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name) const {
|
||||
return type_manager_->GetAnyValue(type_)->GetFieldType(name);
|
||||
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
return type_manager_->GetAnyValue(type_)->GetFieldType(name, type_namespaces);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
@ -270,7 +279,8 @@ bool FunctionType::operator>(const FunctionType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -299,7 +309,8 @@ bool ArrayType::operator>(const ArrayType& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -405,28 +416,29 @@ bool Type::operator>(const Type& type) const {
|
|||
return type < *this;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> Type::GetFieldType(const std::string& name) const {
|
||||
std::optional<utils::IdType> Type::GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const {
|
||||
size_t index = type_.index();
|
||||
|
||||
switch (index) {
|
||||
case 0:
|
||||
return std::get<AbstractType>(type_).GetFieldType(name);
|
||||
return std::get<AbstractType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 1:
|
||||
return std::get<DefinedType>(type_).GetFieldType(name);
|
||||
return std::get<DefinedType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 2:
|
||||
return std::nullopt;
|
||||
case 3:
|
||||
return std::get<TupleType>(type_).GetFieldType(name);
|
||||
return std::get<TupleType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 4:
|
||||
return std::get<VariantType>(type_).GetFieldType(name);
|
||||
return std::get<VariantType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 5:
|
||||
return std::get<ReferenceToType>(type_).GetFieldType(name);
|
||||
return std::get<ReferenceToType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 6:
|
||||
return std::get<FunctionType>(type_).GetFieldType(name);
|
||||
return std::get<FunctionType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 7:
|
||||
return std::get<ArrayType>(type_).GetFieldType(name);
|
||||
return std::get<ArrayType>(type_).GetFieldType(name, type_namespaces);
|
||||
case 8:
|
||||
return std::get<OptionalType>(type_).GetFieldType(name);
|
||||
return std::get<OptionalType>(type_).GetFieldType(name, type_namespaces);
|
||||
default:
|
||||
// error
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue