diff --git a/include/build_visitor.hpp b/include/build_visitor.hpp index 1696010..1c85211 100644 --- a/include/build_visitor.hpp +++ b/include/build_visitor.hpp @@ -126,17 +126,12 @@ private: // Typeclass - void Visit(TypeclassExpression* node) override; void Visit(ParametrizedTypeclass* node) override; - void Visit(TypeclassSubExpression& node) override; // variant - // Typeclass & Type void Visit(ParametrizedType* node) override; - void Visit(TypeSubExpression& node) override; // variant - // Identifiers, constants, etc. ----------------- void Visit(ExtendedName* node) override; diff --git a/include/interpreter_tree.hpp b/include/interpreter_tree.hpp index 249d00c..206e930 100644 --- a/include/interpreter_tree.hpp +++ b/include/interpreter_tree.hpp @@ -69,12 +69,12 @@ using ImportSymbol = AnyIdentifier; // can be extended name struct FunctionDefinition; struct TypeDefinition; -struct AnnotatedAbstractType; +struct AnyAnnotatedType; // TypeIdentifier <-> AbstractTypeIdentifier <-> std::string -using AnnotatedType = AnnotatedAbstractType; +using AnnotatedType = AnyAnnotatedType; -using AnyAnnotatedType = AnnotatedType; +using AnnotatedAbstractType = AnyAnnotatedType; // Flow control ----------------- @@ -225,25 +225,19 @@ struct ExtendedScopedAnyType; // Typeclass struct ParametrizedTypeclass; -struct TypeclassExpression; -using TypeclassSubExpression = std::variant< - std::unique_ptr, - std::unique_ptr>; +// TypeclassSubExpression -> ParametrizedTypeclass // Typeclass & Type struct ParametrizedType; - -using TypeSubExpression = std::variant< - std::unique_ptr, - std::unique_ptr>; +// TypeSubExpression -> ParametrizedType // using FunctionArgument = std::variant< SubExpressionToken, - TypeSubExpression>; + std::unique_ptr>; // Comments [IGNORE] ----------------- // Identifiers, constants, etc. ----------------- @@ -377,9 +371,9 @@ struct TypeDefinition { std::vector> parameters; }; -struct AnnotatedAbstractType { - AbstractTypeIdentifier type; - std::vector> typeclasses; +struct AnyAnnotatedType { + AnyTypeIdentifier type; + std::vector> typeclasses; }; // ----------------- Flow control ----------------- @@ -552,9 +546,16 @@ struct VariantType { std::vector>> constructors; }; +struct ParametrizedType { + AnyTypeIdentifier type; + std::vector> parameters; + + std::optional type_id_; // std::nullopt, if it is namespace without type +}; + struct TypeExpression { - std::vector path; - TypeSubExpression type; + std::vector path; + ParametrizedType type; std::optional array_size; // if array; 0 - dynamic size @@ -569,24 +570,11 @@ struct ExtendedScopedAnyType { // Typeclass ----------------- -struct TypeclassExpression { - TypeclassSubExpression typeclass; - - utils::IdType typeclass_id_; -}; - struct ParametrizedTypeclass { - TypeclassIdentifier typeclass; - std::vector> parameters; -}; - -// Typeclass & Type ----------------- - -struct ParametrizedType { - AnyTypeIdentifier type; + TypeclassIdentifier typeclass; std::vector> parameters; - std::optional type_id_; // std::nullopt, if it is namespace without type + utils::IdType typeclass_id_; }; // ----------------- Comments [IGNORE] ----------------- diff --git a/include/parse_token_types.hpp b/include/parse_token_types.hpp index f14ff53..161398c 100644 --- a/include/parse_token_types.hpp +++ b/include/parse_token_types.hpp @@ -107,18 +107,13 @@ const std::string ExtendedScopedAnyType = "extended_scoped_any_type"; // Typeclass -const std::string TypeclassExpression = "typeclass_expression"; const std::string ParametrizedTypeclass = "parametrized_typeclass"; -const std::string TypeclassSubExpression = "typeclass_subexpression"; - // Typeclass & Type const std::string ParametrizedType = "parametrized_type"; -const std::string TypeSubExpression = "type_subexpression"; - // Identifiers, constants, etc. ----------------- const std::string ExtendedName = "extended_name"; diff --git a/include/print_visitor.hpp b/include/print_visitor.hpp index 803de21..66a2e06 100644 --- a/include/print_visitor.hpp +++ b/include/print_visitor.hpp @@ -99,7 +99,6 @@ private: // Typeclass - void Visit(TypeclassExpression* node) override; void Visit(ParametrizedTypeclass* node) override; // Typeclass & Type diff --git a/include/visitor.hpp b/include/visitor.hpp index dafb924..ebb3241 100644 --- a/include/visitor.hpp +++ b/include/visitor.hpp @@ -123,17 +123,12 @@ protected: // Typeclass - virtual void Visit(TypeclassExpression* node); virtual void Visit(ParametrizedTypeclass* node); - virtual void Visit(TypeclassSubExpression& node); // variant - // Typeclass & Type virtual void Visit(ParametrizedType* node); - virtual void Visit(TypeSubExpression& node); // variant - // Identifiers, constants, etc. ----------------- virtual void Visit(ExtendedName* node); diff --git a/lang-parser b/lang-parser index 8021bf5..1875ec1 160000 --- a/lang-parser +++ b/lang-parser @@ -1 +1 @@ -Subproject commit 8021bf5b433c7b3d5179deba98af60faf47d4457 +Subproject commit 1875ec1f091eaa96d62cc5ceee4c0adc7bb6d4a9 diff --git a/src/build_visitor.cpp b/src/build_visitor.cpp index 5c0ea84..4c8e4af 100644 --- a/src/build_visitor.cpp +++ b/src/build_visitor.cpp @@ -416,7 +416,7 @@ void BuildVisitor::Visit(AnyAnnotatedType* node) { for (size_t i = 0; i + 1 < child_count; ++i) { current_node_ = parse_node.NthNamedChild(i + 1); - node->typeclasses[i] = std::make_unique(); + node->typeclasses[i] = std::make_unique(); Visit(node->typeclasses[i].get()); } } @@ -915,9 +915,9 @@ void BuildVisitor::Visit(FunctionArgument& node) { if (current_node_type == parser::tokens::SubExpressionToken) { node = SubExpressionToken(); Visit(std::get(node)); - } else if (current_node_type == parser::tokens::TypeSubExpression) { - node = TypeSubExpression(); - Visit(std::get(node)); + } else if (current_node_type == parser::tokens::TypeExpression) { + node = std::make_unique(); + Visit(std::get>(node).get()); } else { // error } @@ -1288,7 +1288,7 @@ void BuildVisitor::Visit(TypeExpression* node) { size_t child_count = parse_node.NamedChildCount(); current_node_ = parse_node.ChildByFieldName("type"); - Visit(node->type); + Visit(&node->type); ++excluded_child_count; current_node_ = current_node_.NextSibling(); @@ -1310,7 +1310,7 @@ void BuildVisitor::Visit(TypeExpression* node) { for (size_t i = 0; i + excluded_child_count < child_count; ++i) { current_node_ = parse_node.NthNamedChild(i); - Visit(node->path[i]); + Visit(&node->path[i]); } } @@ -1368,35 +1368,6 @@ void BuildVisitor::Visit(ExtendedScopedAnyType* node) { // Typeclass -void BuildVisitor::Visit(TypeclassExpression* node) { - auto parse_node = current_node_; - - current_node_ = parse_node.ChildByFieldName("typeclass"); - Visit(node->typeclass); - - current_node_ = parse_node; -} - -void BuildVisitor::Visit(TypeclassSubExpression& node) { - auto parse_node = current_node_; - - current_node_ = parse_node.NthNamedChild(0); - - std::string current_node_type = current_node_.GetType(); - - if (current_node_type == parser::tokens::TypeclassIdentifier) { - node = std::make_unique(); - Visit(std::get>(node).get()); - } else if (current_node_type == parser::tokens::ParametrizedTypeclass) { - node = std::make_unique(); - Visit(std::get>(node).get()); - } else { - // error - } - - current_node_ = parse_node; -} - void BuildVisitor::Visit(ParametrizedTypeclass* node) { auto parse_node = current_node_; @@ -1419,28 +1390,6 @@ void BuildVisitor::Visit(ParametrizedTypeclass* node) { // Typeclass & Type ----------------- -void BuildVisitor::Visit(TypeSubExpression& node) { - auto parse_node = current_node_; - - current_node_ = parse_node.NthNamedChild(0); - - std::string current_node_type = current_node_.GetType(); - - if (current_node_type == parser::tokens::TypeIdentifier) { - node = std::make_unique(); - Visit(std::get>(node).get()); - } else if (current_node_type == parser::tokens::AbstractTypeIdentifier) { - node = std::make_unique(); - Visit(std::get>(node).get()); - } else if (current_node_type == parser::tokens::ParametrizedType) { - node = std::make_unique(); - Visit(std::get>(node).get()); - } else { - // error - } - - current_node_ = parse_node; -} void BuildVisitor::Visit(ParametrizedType* node) { auto parse_node = current_node_; diff --git a/src/print_visitor.cpp b/src/print_visitor.cpp index ce3b3eb..a3367f9 100644 --- a/src/print_visitor.cpp +++ b/src/print_visitor.cpp @@ -632,10 +632,10 @@ void PrintVisitor::Visit(TypeExpression* node) { out_ << "] ("; for (auto& type : node->path) { - Visitor::Visit(type); + Visit(&type); out_ << "::"; } - Visitor::Visit(node->type); + Visit(&node->type); out_ << ')'; } @@ -658,12 +658,6 @@ void PrintVisitor::Visit(ExtendedScopedAnyType* node) { // Typeclass -void PrintVisitor::Visit(TypeclassExpression* node) { - out_ << "[TypeclassExpression] ("; - Visitor::Visit(node->typeclass); - out_ << ')'; -} - void PrintVisitor::Visit(ParametrizedTypeclass* node) { out_ << "[ParametrizedTypeclass] ("; Visit(&node->typeclass); diff --git a/src/visitor.cpp b/src/visitor.cpp index 793b921..f5afc4f 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -256,7 +256,7 @@ void Visitor::Visit(FunctionArgument& node) { Visit(std::get(node)); break; case 1: - Visit(std::get(node)); + Visit(std::get>(node).get()); break; default: // error @@ -307,38 +307,6 @@ void Visitor::Visit(AnyType& node) { } } -// Typeclass - -void Visitor::Visit(TypeclassSubExpression& node) { - switch (node.index()) { - case 0: - Visit(std::get>(node).get()); - break; - case 1: - Visit(std::get>(node).get()); - break; - default: - // error - break; - } -} - -// Typeclass & Type - -void Visitor::Visit(TypeSubExpression& node) { - switch (node.index()) { - case 0: - Visit(std::get>(node).get()); - break; - case 1: - Visit(std::get>(node).get()); - break; - default: - // error - break; - } -} - // Identifiers, constants, etc. ----------------- void Visitor::Visit(Literal& node) { @@ -697,9 +665,9 @@ void Visitor::Visit(VariantType* node) { void Visitor::Visit(TypeExpression* node) { for (auto& type : node->path) { - Visit(type); + Visit(&type); } - Visit(node->type); + Visit(&node->type); } void Visitor::Visit(ExtendedScopedAnyType* node) { @@ -708,10 +676,6 @@ void Visitor::Visit(ExtendedScopedAnyType* node) { // Typeclass -void Visitor::Visit(TypeclassExpression* node) { - Visit(node->typeclass); -} - void Visitor::Visit(ParametrizedTypeclass* node) { Visit(&node->typeclass); for (auto& parameter : node->parameters) {