mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-09 16:38:45 +00:00
type_check_visitor part
This commit is contained in:
parent
d13faf104d
commit
94ef8702fb
11 changed files with 496 additions and 288 deletions
|
|
@ -5,17 +5,6 @@
|
|||
|
||||
namespace interpreter {
|
||||
|
||||
const std::string& NameFromTypeSubExpression(const TypeSubExpression& type) {
|
||||
if (std::holds_alternative<std::unique_ptr<std::string>>(type)) {
|
||||
return *std::get<std::unique_ptr<std::string>>(type);
|
||||
} else if (std::holds_alternative<std::unique_ptr<ParametrizedType>>(type)) {
|
||||
return std::get<std::unique_ptr<ParametrizedType>>(type)->type;
|
||||
}
|
||||
|
||||
error_handling::HandleInternalError("Empty variant", "NameFromTypeSubExpression");
|
||||
exit(1); // TODO: better decision ??
|
||||
}
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void LinkSymbolsVisitor::Visit(Namespace* node) {
|
||||
|
|
@ -49,23 +38,25 @@ void LinkSymbolsVisitor::Visit(Namespace* node) {
|
|||
|
||||
// Type
|
||||
|
||||
// TODO: link internal stages
|
||||
// TODO: find constructors ?
|
||||
void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
||||
std::vector<std::string> path;
|
||||
path.reserve(node->path.size());
|
||||
|
||||
for (auto& path_type : node->path) {
|
||||
path.push_back(NameFromTypeSubExpression(path_type));
|
||||
path.push_back(path_type.type);
|
||||
}
|
||||
|
||||
node->type_id_ = namespace_visitor_.FindType(path, NameFromTypeSubExpression(node->type));
|
||||
node->type_id_ = namespace_visitor_.FindType(path, NameFromTypeSubExpression(node->type));
|
||||
node->type_id_ = namespace_visitor_.FindType(path, node->type.type);
|
||||
node->constructor_id_ = namespace_visitor_.FindConstructor(path, node->type.type);
|
||||
|
||||
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) {
|
||||
error_handling::HandleTypecheckError("Type or constructor not found");
|
||||
}
|
||||
|
||||
if (node->type_id_.has_value()) {
|
||||
node->type.type_id_ = node->type_id_.value();
|
||||
}
|
||||
|
||||
std::optional<info::definition::Namespace*> maybe_type_namespace = namespace_visitor_.FindNamespace(path);
|
||||
if (maybe_type_namespace.has_value()) {
|
||||
info::definition::Namespace* type_namespace = maybe_type_namespace.value();
|
||||
|
|
@ -73,9 +64,9 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
for (ssize_t i = (ssize_t)node->path.size(); i >= 0; --i) {
|
||||
info::definition::Namespace* parent_namespace = type_namespace->parent_namespace;
|
||||
|
||||
auto maybe_type = parent_namespace->types.find(type_namespace->type_name); // TODO: make method ??
|
||||
if (maybe_type.has_value()) {
|
||||
node->path[i]->
|
||||
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) {
|
||||
|
|
@ -89,18 +80,8 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
|
||||
// Typeclass
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeclassExpression* node) { // TODO: check
|
||||
std::string typeclass;
|
||||
|
||||
if (std::holds_alternative<std::unique_ptr<std::string>>(node->typeclass)) {
|
||||
typeclass = *std::get<std::unique_ptr<std::string>>(node->typeclass);
|
||||
} else if (std::holds_alternative<std::unique_ptr<ParametrizedTypeclass>>(node->typeclass)) {
|
||||
typeclass = std::get<std::unique_ptr<ParametrizedTypeclass>>(node->typeclass)->typeclass;
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> maybe_typeclass = namespace_visitor_.FindTypeclass(typeclass);
|
||||
void LinkSymbolsVisitor::Visit(ParametrizedTypeclass* node) { // TODO: check
|
||||
std::optional<utils::IdType> maybe_typeclass = namespace_visitor_.FindTypeclass(node->typeclass);
|
||||
|
||||
if (maybe_typeclass.has_value()) {
|
||||
node->typeclass_id_ = maybe_typeclass.value();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue