diff --git a/lang/builders/include/builders_utils.hpp b/lang/builders/include/builders_utils.hpp index 4e35dab..b4b79cf 100644 --- a/lang/builders/include/builders_utils.hpp +++ b/lang/builders/include/builders_utils.hpp @@ -7,7 +7,7 @@ namespace builders { // using Code = parser::ParseTree; -using Exprs = nodes::ExpressionStorage; +using Exprs = nodes::ExprStorage; using Types = nodes::TypeStorage; using Names = names::NameTree; diff --git a/lang/builders/include/expression_builders.hpp b/lang/builders/include/expression_builders.hpp index 3243c26..0996877 100644 --- a/lang/builders/include/expression_builders.hpp +++ b/lang/builders/include/expression_builders.hpp @@ -24,12 +24,11 @@ namespace builders { // --- flow control template <> -struct BuilderTask - : public BuilderTaskBase { - using BuilderTaskBase::BuilderTaskBase; +struct BuilderTask : public BuilderTaskBase { + using BuilderTaskBase::BuilderTaskBase; - nodes::ExpressionProxy operator()(const ParserNode &parser_node, - const Arguments &args) override; + nodes::Expr operator()(const ParserNode &parser_node, + const Arguments &args) override; }; template <> @@ -104,12 +103,12 @@ struct BuilderTask ? nodes::Container::BLOCK : nodes::Container::ARRAY; - std::vector expressions; + std::vector expressions; auto current_node = parser_node.nth_named_child(0); while (!current_node.is_null()) { - expressions.push_back(Run(current_node)); + expressions.push_back(Run(current_node)); current_node = current_node.next_named_sibling(); } @@ -182,7 +181,7 @@ struct BuilderTask return nodes::ModifierExpression( build_node(parser_node), build_modifier(parser_node.nth_child(modifier_pos)), - Run(parser_node.nth_named_child(0))); + Run(parser_node.nth_named_child(0))); } }; diff --git a/lang/builders/include/type_builders.hpp b/lang/builders/include/type_builders.hpp index 4a3c958..6cebea5 100644 --- a/lang/builders/include/type_builders.hpp +++ b/lang/builders/include/type_builders.hpp @@ -5,22 +5,22 @@ namespace builders { -nodes::TypeProxy build_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); -nodes::TypeProxy build_variant_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_variant_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); -nodes::TypeProxy build_tuple_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_tuple_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); -nodes::TypeProxy build_reference_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_reference_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); -nodes::TypeProxy build_modified_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_modified_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); -nodes::TypeProxy build_simple_type(parser::ParseTree::Node parse_node, - nodes::TypeStorage &type_storage); +nodes::Type build_simple_type(parser::ParseTree::Node parse_node, + nodes::TypeStorage &type_storage); } // namespace builders diff --git a/lang/builders/src/expression_builders.cpp b/lang/builders/src/expression_builders.cpp index e6781c7..d4cfef2 100644 --- a/lang/builders/src/expression_builders.cpp +++ b/lang/builders/src/expression_builders.cpp @@ -10,9 +10,8 @@ namespace builders { -nodes::ExpressionProxy -BuilderTask::operator()(const ParserNode &parser_node, - const Arguments &) { +nodes::Expr BuilderTask::operator()(const ParserNode &parser_node, + const Arguments &) { tokens::Type type = tokens::string_to_type(parser_node.get_type()); auto maybe_parenthesis = parser_node.previous_sibling(); @@ -20,20 +19,20 @@ BuilderTask::operator()(const ParserNode &parser_node, (!maybe_parenthesis.is_null() && !maybe_parenthesis.is_named() && maybe_parenthesis.get_value() == "("); - std::optional expr; + std::optional expr; switch (type) { // --- flow control case tokens::Type::MATCH: expr = Run(parser_node); break; case tokens::Type::CONDITION: - return state().add_expression({build_node(parser_node), - Run(parser_node), - is_scoped}); + return state().add_expr({build_node(parser_node), + Run(parser_node), + is_scoped}); expr = Run(parser_node); break; case tokens::Type::LOOP: - return state().add_expression( + return state().add_expr( {build_node(parser_node), Run(parser_node), is_scoped}); expr = Run(parser_node); break; @@ -141,7 +140,7 @@ BuilderTask::operator()(const ParserNode &parser_node, utils::Assert(expr.has_value(), "Expression should have value"); - return state().add_expression( + return state().add_expr( {build_node(parser_node), expr.value(), is_scoped}); } @@ -174,13 +173,11 @@ BuilderTask::operator()(const ParserNode &parser_node, build_node(parser_node), case_type == ":=" ? nodes::Match::Case::PATTERN_VALUE : nodes::Match::Case::VALUE_PATTERN, - Run(parser_node.nth_named_child(0)), - condition_node.has_value() - ? Run(condition_node.value()) - : std::optional(), - expression_node.has_value() - ? Run(expression_node.value()) - : std::optional()); + Run(parser_node.nth_named_child(0)), + condition_node.has_value() ? Run(condition_node.value()) + : std::optional(), + expression_node.has_value() ? Run(expression_node.value()) + : std::optional()); } // expression case+ @@ -195,10 +192,9 @@ BuilderTask::operator()(const ParserNode &parser_node, current_node = current_node.next_named_sibling(); } - return nodes::Match( - build_node(parser_node), - Run(parser_node.nth_named_child(0)), - std::move(cases)); + return nodes::Match(build_node(parser_node), + Run(parser_node.nth_named_child(0)), + std::move(cases)); } // ('??' | 'if') expression _do_ expression (('!!' | 'elif') expression _do_ @@ -208,14 +204,14 @@ BuilderTask::operator()(const ParserNode &parser_node, const Arguments &) { size_t named_child_count = parser_node.named_child_count(); - std::vector> cases; + std::vector> cases; auto current_node = parser_node.nth_named_child(0); auto next_node = current_node.next_named_sibling(); while (!current_node.is_null() && !next_node.is_null()) { - cases.push_back({Run(current_node), - Run(next_node)}); + cases.push_back( + {Run(current_node), Run(next_node)}); current_node = next_node.next_named_sibling(); if (current_node.is_null()) { break; @@ -226,9 +222,8 @@ BuilderTask::operator()(const ParserNode &parser_node, return nodes::Condition( build_node(parser_node), std::move(cases), named_child_count % 2 == 1 - ? Run( - parser_node.nth_named_child(named_child_count - 1)) - : std::optional()); + ? Run(parser_node.nth_named_child(named_child_count - 1)) + : std::optional()); } // ('@' | 'for') (expression | expression ':' expression)? _do_ expression @@ -237,23 +232,20 @@ nodes::Loop BuilderTask::operator()(const ParserNode &parser_node, size_t named_child_count = parser_node.named_child_count(); if (named_child_count == 1) { // body - return nodes::Loop( - build_node(parser_node), - Run(parser_node.nth_named_child(0))); + return nodes::Loop(build_node(parser_node), + Run(parser_node.nth_named_child(0))); } else if (named_child_count == 2) { // condition, // body - return nodes::Loop( - build_node(parser_node), - Run(parser_node.nth_named_child(0)), - Run(parser_node.nth_named_child(1))); + return nodes::Loop(build_node(parser_node), + Run(parser_node.nth_named_child(0)), + Run(parser_node.nth_named_child(1))); } else if (named_child_count == 3) { // variable, // interval, // body - return nodes::Loop( - build_node(parser_node), - Run(parser_node.nth_named_child(0)), - Run(parser_node.nth_named_child(1)), - Run(parser_node.nth_named_child(2))); + return nodes::Loop(build_node(parser_node), + Run(parser_node.nth_named_child(0)), + Run(parser_node.nth_named_child(1)), + Run(parser_node.nth_named_child(2))); } else { error_handling::handle_parsing_error("Unex" "prec" @@ -283,14 +275,13 @@ nodes::Loop BuilderTask::operator()(const ParserNode &parser_node, nodes::NameExpression BuilderTask::operator()( const ParserNode &parser_node, const Arguments &) { - std::vector, nodes::ExpressionProxy>> - arguments; + std::vector, nodes::Expr>> arguments; - arguments.emplace_back(std::nullopt, Run( - parser_node.nth_named_child(0))); + arguments.emplace_back(std::nullopt, + Run(parser_node.nth_named_child(0))); - arguments.emplace_back(std::nullopt, Run( - parser_node.nth_named_child(1))); + arguments.emplace_back(std::nullopt, + Run(parser_node.nth_named_child(1))); return nodes::NameExpression( build_node(parser_node), @@ -308,14 +299,13 @@ BuilderTask::operator()( "m" "e"); - std::vector, nodes::ExpressionProxy>> - arguments; + std::vector, nodes::Expr>> arguments; - arguments.emplace_back(std::nullopt, Run( - name_node.previous_named_sibling())); + arguments.emplace_back(std::nullopt, + Run(name_node.previous_named_sibling())); - arguments.emplace_back(std::nullopt, Run( - name_node.next_named_sibling())); + arguments.emplace_back(std::nullopt, + Run(name_node.next_named_sibling())); return nodes::NameExpression(build_node(parser_node), build_operator(name_node), std::move(arguments), @@ -334,14 +324,13 @@ BuilderTask::operator()(const ParserNode &parser_node, const Arguments &) { std::string modifier = parser_node.nth_child(0).get_value(); - return nodes::Return( - build_node(parser_node), - modifier == "re" - "tu" - "rn" - ? nodes::Return::RETURN - : nodes::Return::BRING, - Run(parser_node.nth_named_child(0))); + return nodes::Return(build_node(parser_node), + modifier == "re" + "tu" + "rn" + ? nodes::Return::RETURN + : nodes::Return::BRING, + Run(parser_node.nth_named_child(0))); } // _var_let_ (simple_name_identifier | placeholder) @@ -362,10 +351,9 @@ BuilderTask::operator()(const ParserNode &parser_node, // expression '[' expression ']' nodes::Access BuilderTask::operator()( const ParserNode &parser_node, const Arguments &) { - return nodes::Access( - build_node(parser_node), nodes::Access::ARRAY, - Run(parser_node.nth_named_child(0)), - Run(parser_node.nth_named_child(1))); + return nodes::Access(build_node(parser_node), nodes::Access::ARRAY, + Run(parser_node.nth_named_child(0)), + Run(parser_node.nth_named_child(1))); } // expression '.' number_literal @@ -373,8 +361,8 @@ nodes::Access BuilderTask::operator()( const ParserNode &parser_node, const Arguments &) { return nodes::Access( build_node(parser_node), nodes::Access::TUPLE, - Run(parser_node.nth_named_child(0)), - state().add_expression( + Run(parser_node.nth_named_child(0)), + state().add_expr( {build_node(parser_node.nth_named_child(1)), build_index_literal(parser_node.nth_named_child(1)), false})); } @@ -408,7 +396,7 @@ void build_arguments_until_end( } else { arguments.emplace_back( std::move(last_annotation), - BuilderTask{executor}(current_node, {})); + BuilderTask{executor}(current_node, {})); last_annotation = std::nullopt; } current_node = current_node.next_named_sibling(); @@ -422,7 +410,7 @@ BuilderTask::operator()( const ParserNode &parser_node, const Arguments &) { std::vector arguments; - std::optional prefix; + std::optional prefix; bool is_point_call = false; @@ -447,8 +435,7 @@ BuilderTask::operator()( prefix_node = current_node; } else { is_point_call = true; - arguments.emplace_back(std::nullopt, - Run(current_node)); + arguments.emplace_back(std::nullopt, Run(current_node)); } } @@ -459,7 +446,7 @@ BuilderTask::operator()( build_node(parser_node), build_identifier(name_node), std::move(arguments), prefix_node.has_value() ? build_type(prefix_node.value(), state()) - : nodes::MaybeTypeProxy(), + : nodes::MaybeType(), is_point_call, false); } @@ -500,7 +487,7 @@ BuilderTask::operator()(const ParserNode &parser_node, current_node = current_node.next_named_sibling(); return nodes::Lambda(build_node(parser_node), std::move(arguments), - Run(current_node)); + Run(current_node)); } } // namespace builders diff --git a/lang/builders/src/statement_builders.cpp b/lang/builders/src/statement_builders.cpp index ea391e6..ebb1a8e 100644 --- a/lang/builders/src/statement_builders.cpp +++ b/lang/builders/src/statement_builders.cpp @@ -132,9 +132,8 @@ BuilderTask::operator()(const ParserNode &parser_node, nodes::Constraint BuilderTask::operator()(const ParserNode &parser_node, const Arguments &) { - return nodes::Constraint( - build_node(parser_node), - Run(parser_node.nth_named_child(0))); + return nodes::Constraint(build_node(parser_node), + Run(parser_node.nth_named_child(0))); } parser::ParseTree::Node collect_symbol_doc_nodes( @@ -244,9 +243,9 @@ BuilderTask::operator()(const ParserNode &parser_node, } } - nodes::MaybeTypeProxy type = - type_node.has_value() ? build_type(type_node.value(), state()) - : nodes::MaybeTypeProxy(); + nodes::MaybeType type = type_node.has_value() + ? build_type(type_node.value(), state()) + : nodes::MaybeType(); std::unordered_set annotations; @@ -453,9 +452,8 @@ nodes::FunctionDefinition BuilderTask::operator()( std::move(constraints), return_modifier, is_method, name_prefix, build_identifier(name_node), std::move(arguments), are_annotations_same_to_names, - expression_node.has_value() - ? Run(expression_node.value()) - : std::optional()); + expression_node.has_value() ? Run(expression_node.value()) + : std::optional()); } } // namespace builders diff --git a/lang/builders/src/type_builders.cpp b/lang/builders/src/type_builders.cpp index 352b995..eded975 100644 --- a/lang/builders/src/type_builders.cpp +++ b/lang/builders/src/type_builders.cpp @@ -8,8 +8,8 @@ namespace builders { -nodes::TypeProxy build_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { +nodes::Type build_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { tokens::Type type = tokens::string_to_type(parser_node.get_type()); // TODO: for better formatted printing @@ -37,10 +37,9 @@ nodes::TypeProxy build_type(parser::ParseTree::Node parser_node, exit(1); // unreachable } -std::vector -collect_parameters(parser::ParseTree::Node first_node, - nodes::TypeStorage &type_storage) { - std::vector parameters; +std::vector collect_parameters(parser::ParseTree::Node first_node, + nodes::TypeStorage &type_storage) { + std::vector parameters; std::optional current_annotation; @@ -61,10 +60,10 @@ collect_parameters(parser::ParseTree::Node first_node, return parameters; } -nodes::TypeProxy build_container_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage, - builtin::Type container) { - std::vector parameters = +nodes::Type build_container_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage, + builtin::Type container) { + std::vector parameters = collect_parameters(parser_node.nth_named_child(0), type_storage); return type_storage.add_container_of(std::move(parameters), container, @@ -72,31 +71,30 @@ nodes::TypeProxy build_container_type(parser::ParseTree::Node parser_node, } // '|'? annotation? type ('|' annotation? type)+ -nodes::TypeProxy build_variant_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { +nodes::Type build_variant_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { return build_container_type(parser_node, type_storage, builtin::Type::VARIANT); } // '&'? annotation? type ('&' annotation? type)+ -nodes::TypeProxy build_tuple_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { +nodes::Type build_tuple_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { return build_container_type(parser_node, type_storage, builtin::Type::TUPLE); } // _reference_ type -nodes::TypeProxy build_reference_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { - nodes::TypeProxy type = - build_type(parser_node.nth_named_child(0), type_storage); +nodes::Type build_reference_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { + nodes::Type type = build_type(parser_node.nth_named_child(0), type_storage); type.get()->set_modifier(build_modifier(parser_node.nth_child(0))); return type; } // type ('?' | '!') -nodes::TypeProxy build_modified_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { - std::vector parameters; +nodes::Type build_modified_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { + std::vector parameters; parameters.push_back( build_type(parser_node.nth_named_child(0), type_storage)); @@ -118,22 +116,22 @@ nodes::TypeProxy build_modified_type(parser::ParseTree::Node parser_node, break; } - return type_storage.add_type( - nodes::Type(nodes::Identifier(build_node(parser_node), - nodes::Identifier::SIMPLE_TYPE, identifier), - std::move(parameters))); + return type_storage.add_type(nodes::TypeData( + nodes::Identifier(build_node(parser_node), nodes::Identifier::SIMPLE_TYPE, + identifier), + std::move(parameters))); } // type_identifier ('[' (annotation? type)+ ']')? -nodes::TypeProxy build_simple_type(parser::ParseTree::Node parser_node, - nodes::TypeStorage &type_storage) { +nodes::Type build_simple_type(parser::ParseTree::Node parser_node, + nodes::TypeStorage &type_storage) { auto name_node = parser_node.child_by_field_name("name"); - std::vector parameters = + std::vector parameters = collect_parameters(name_node.next_named_sibling(), type_storage); return type_storage.add_type( - nodes::Type(build_identifier(name_node), std::move(parameters))); + nodes::TypeData(build_identifier(name_node), std::move(parameters))); } } // namespace builders diff --git a/lang/nodes/include/basic_nodes.hpp b/lang/nodes/include/basic_nodes.hpp index 4f2b85e..ee0b86f 100644 --- a/lang/nodes/include/basic_nodes.hpp +++ b/lang/nodes/include/basic_nodes.hpp @@ -196,6 +196,10 @@ struct unicode { class Literal : public Node { public: + using Type = + std::variant; + template Literal(Node node, T &&value) : Node(node), value_(std::forward(value)) {} @@ -213,14 +217,12 @@ public: return std::nullopt; } - auto get_any() { return &value_; } + Type &get_any() { return value_; } - auto get_any() const { return &value_; } + const Type &get_any() const { return value_; } private: - std::variant - value_; + Type value_; }; class Identifier : public Node { diff --git a/lang/nodes/include/expression_nodes.hpp b/lang/nodes/include/expression_nodes.hpp index 47a624e..ab4d584 100644 --- a/lang/nodes/include/expression_nodes.hpp +++ b/lang/nodes/include/expression_nodes.hpp @@ -13,52 +13,50 @@ class TypedNode : public Node { public: TypedNode(Node node) : Node(node) {} - void set_expression_type(TypeProxy expression_type) { + void set_expression_type(Type expression_type) { expression_type_ = expression_type; } - std::optional get_expression_type() const { - return expression_type_; - } + std::optional get_expression_type() const { return expression_type_; } protected: - std::optional expression_type_; + std::optional expression_type_; }; -class Expression; -class ExpressionStorage; +class ExprData; +class ExprStorage; -class ExpressionProxy { - friend ExpressionStorage; +class Expr { + friend ExprStorage; public: - ExpressionProxy() = delete; + Expr() = delete; - Expression *get(); + ExprData *get(); - const Expression *get() const; + const ExprData *get() const; private: - ExpressionProxy(ExpressionStorage &expression_storage, size_t id) + Expr(ExprStorage &expression_storage, size_t id) : expression_storage_(&expression_storage), id_(id) {} private: - ExpressionStorage *expression_storage_; + ExprStorage *expression_storage_; size_t id_; }; namespace utils { -inline std::optional -proxy_to_expr_optional(std::optional &proxy) { +inline std::optional +proxy_to_expr_optional(std::optional &proxy) { if (proxy.has_value()) { return proxy.value().get(); } return std::nullopt; } -inline std::optional -proxy_to_expr_optional(const std::optional &proxy) { +inline std::optional +proxy_to_expr_optional(const std::optional &proxy) { if (proxy.has_value()) { return proxy.value().get(); } @@ -78,50 +76,50 @@ public: VALUE_PATTERN, }; - Case(Node node, CaseType case_type, ExpressionProxy value, - std::optional condition = std::nullopt, - std::optional expression = std::nullopt) + Case(Node node, CaseType case_type, Expr value, + std::optional condition = std::nullopt, + std::optional expression = std::nullopt) : TypedNode(node), case_type_(case_type), value_(value), condition_(condition), expression_(expression) {} CaseType case_type() const { return case_type_; } - Expression *get_value() { return value_.get(); } + ExprData *get_value() { return value_.get(); } - const Expression *get_value() const { return value_.get(); } + const ExprData *get_value() const { return value_.get(); } - std::optional get_condition() { + std::optional get_condition() { return utils::proxy_to_expr_optional(condition_); } - std::optional get_condition() const { + std::optional get_condition() const { return utils::proxy_to_expr_optional(condition_); } - std::optional get_expression() { + std::optional get_expression() { return utils::proxy_to_expr_optional(expression_); } - std::optional get_expression() const { + std::optional get_expression() const { return utils::proxy_to_expr_optional(expression_); } private: CaseType case_type_; - ExpressionProxy value_; - std::optional condition_; - std::optional expression_; + Expr value_; + std::optional condition_; + std::optional expression_; }; - Match(Node node, ExpressionProxy value, std::vector &&cases) + Match(Node node, Expr value, std::vector &&cases) : TypedNode(node), value_(value), cases_(std::move(cases)) {} - Match(Node node, ExpressionProxy value, const std::vector &cases) + Match(Node node, Expr value, const std::vector &cases) : TypedNode(node), value_(value), cases_(cases) {} - Expression *get_value() { return value_.get(); } + ExprData *get_value() { return value_.get(); } - const Expression *get_value() const { return value_.get(); } + const ExprData *get_value() const { return value_.get(); } size_t cases_size() const { return cases_.size(); } @@ -130,44 +128,41 @@ public: const Case *get_case(size_t id) const { return &cases_.at(id); } private: - ExpressionProxy value_; + Expr value_; std::vector cases_; }; class Condition : public Node { public: - Condition(Node node, - std::vector> &&cases, - std::optional else_case = std::nullopt) + Condition(Node node, std::vector> &&cases, + std::optional else_case = std::nullopt) : Node(node), cases_(std::move(cases)), else_case_(else_case) {} - Condition( - Node node, - const std::vector> &cases, - std::optional else_case = std::nullopt) + Condition(Node node, const std::vector> &cases, + std::optional else_case = std::nullopt) : Node(node), cases_(cases), else_case_(else_case) {} size_t cases_size() const { return cases_.size(); } - std::pair get_case(size_t id) { + std::pair get_case(size_t id) { return {cases_.at(id).first.get(), cases_[id].second.get()}; } - std::pair get_case(size_t id) const { + std::pair get_case(size_t id) const { return {cases_.at(id).first.get(), cases_[id].second.get()}; } - std::optional get_else_case() { + std::optional get_else_case() { return utils::proxy_to_expr_optional(else_case_); } - std::optional get_else_case() const { + std::optional get_else_case() const { return utils::proxy_to_expr_optional(else_case_); } private: - std::vector> cases_; - std::optional else_case_; + std::vector> cases_; + std::optional else_case_; }; class Loop : public TypedNode { @@ -179,58 +174,57 @@ public: }; // LOOP - Loop(Node node, ExpressionProxy expression) + Loop(Node node, Expr expression) : TypedNode(node), loop_type_(LOOP), expression_(expression) {} // WHILE - Loop(Node node, ExpressionProxy condition, ExpressionProxy expression) + Loop(Node node, Expr condition, Expr expression) : TypedNode(node), loop_type_(WHILE), expression_(expression), condition_(condition) {} // FOR - Loop(Node node, ExpressionProxy variable, ExpressionProxy interval, - ExpressionProxy expression) + Loop(Node node, Expr variable, Expr interval, Expr expression) : TypedNode(node), loop_type_(FOR), expression_(expression), variable_(variable), interval_(interval) {} LoopType get_type() const { return loop_type_; } - Expression *get_expression() { return expression_.get(); } + ExprData *get_expression() { return expression_.get(); } - const Expression *get_expression() const { return expression_.get(); } + const ExprData *get_expression() const { return expression_.get(); } - std::optional get_condition() { + std::optional get_condition() { return utils::proxy_to_expr_optional(condition_); } - std::optional get_condition() const { + std::optional get_condition() const { return utils::proxy_to_expr_optional(condition_); } - std::optional get_variable() { + std::optional get_variable() { return utils::proxy_to_expr_optional(variable_); } - std::optional get_variable() const { + std::optional get_variable() const { return utils::proxy_to_expr_optional(variable_); } - std::optional get_interval() { + std::optional get_interval() { return utils::proxy_to_expr_optional(interval_); } - std::optional get_interval() const { + std::optional get_interval() const { return utils::proxy_to_expr_optional(interval_); } private: LoopType loop_type_; - ExpressionProxy expression_; + Expr expression_; - std::optional condition_; - std::optional variable_; - std::optional interval_; + std::optional condition_; + std::optional variable_; + std::optional interval_; }; // --- containers @@ -242,29 +236,27 @@ public: ARRAY, }; - Container(Node node, ContainerType type, - std::vector &&expressions) + Container(Node node, ContainerType type, std::vector &&expressions) : TypedNode(node), container_type_(type), expressions_(std::move(expressions)) {} - Container(Node node, ContainerType type, - const std::vector &expressions) + Container(Node node, ContainerType type, const std::vector &expressions) : TypedNode(node), container_type_(type), expressions_(expressions) {} ContainerType get_type() const { return container_type_; } size_t expressions_size() const { return expressions_.size(); } - Expression *get_expression(size_t id) { return expressions_.at(id).get(); } + ExprData *get_expression(size_t id) { return expressions_.at(id).get(); } - const Expression *get_expression(size_t id) const { + const ExprData *get_expression(size_t id) const { return expressions_.at(id).get(); } private: ContainerType container_type_; - std::vector expressions_; + std::vector expressions_; }; // --- modifiers @@ -276,19 +268,19 @@ public: BRING, }; - Return(Node node, ReturnType type, ExpressionProxy expression) + Return(Node node, ReturnType type, Expr expression) : Node(node), return_type_(type), expression_(expression) {} ReturnType get_type() const { return return_type_; } - Expression *get_expression() { return expression_.get(); } + ExprData *get_expression() { return expression_.get(); } - const Expression *get_expression() const { return expression_.get(); } + const ExprData *get_expression() const { return expression_.get(); } private: ReturnType return_type_; - ExpressionProxy expression_; + Expr expression_; }; class NameDefinition : public TypedNode { @@ -323,25 +315,24 @@ public: TUPLE, // only number literal index allowed }; - Access(Node node, AccessType type, ExpressionProxy value, - ExpressionProxy index) + Access(Node node, AccessType type, Expr value, Expr index) : TypedNode(node), access_type_(type), value_(value), index_(index) {} AccessType get_type() const { return access_type_; } - Expression *get_value() { return value_.get(); } + ExprData *get_value() { return value_.get(); } - const Expression *get_value() const { return value_.get(); } + const ExprData *get_value() const { return value_.get(); } - Expression *get_index() { return index_.get(); } + ExprData *get_index() { return index_.get(); } - const Expression *get_index() const { return index_.get(); } + const ExprData *get_index() const { return index_.get(); } private: AccessType access_type_; - ExpressionProxy value_; - ExpressionProxy index_; + Expr value_; + Expr index_; }; class LoopControl : public TypedNode { @@ -362,25 +353,24 @@ private: class ModifierExpression : public TypedNode { public: - ModifierExpression(Node node, Modifier modifier, ExpressionProxy expression) + ModifierExpression(Node node, Modifier modifier, Expr expression) : TypedNode(node), modifier_(modifier), expression_(expression) {} Modifier get_modifier() const { return modifier_; } - Expression *get_expression() { return expression_.get(); } + ExprData *get_expression() { return expression_.get(); } - const Expression *get_expression() const { return expression_.get(); } + const ExprData *get_expression() const { return expression_.get(); } private: Modifier modifier_; - ExpressionProxy expression_; + Expr expression_; }; // --- other -using AnnotatedArgument = - std::pair, ExpressionProxy>; +using AnnotatedArgument = std::pair, Expr>; class NameExpression : public TypedNode { public: @@ -392,7 +382,7 @@ public: NameExpression(Node node, Identifier &&name, std::vector &&arguments, - std::optional &&prefix, bool is_point_call = false, + std::optional &&prefix, bool is_point_call = false, bool is_operator_call = false) : TypedNode(node), name_(std::move(name)), arguments_(std::move(arguments)), prefix_(std::move(prefix)), @@ -402,14 +392,14 @@ public: const Identifier *get_name() const { return &name_; } - std::optional get_prefix() { + std::optional get_prefix() { if (prefix_.has_value()) { return prefix_.value().get(); } return std::nullopt; } - std::optional get_prefix() const { + std::optional get_prefix() const { if (prefix_.has_value()) { return prefix_.value().get(); } @@ -418,11 +408,11 @@ public: size_t arguments_size() const { return arguments_.size(); } - Expression *get_argument_value(size_t id) { + ExprData *get_argument_value(size_t id) { return arguments_.at(id).second.get(); } - const Expression *get_argument_value(size_t id) const { + const ExprData *get_argument_value(size_t id) const { return arguments_.at(id).second.get(); } @@ -448,7 +438,7 @@ private: Identifier name_; // universal function call syntax std::vector arguments_; - std::optional prefix_; + std::optional prefix_; // for static methods bool is_point_call_ = false; // x.f ... or f x ... bool is_operator_call_ = false; // ... operator ... @@ -456,28 +446,27 @@ private: class Constructor : public TypedNode { public: - Constructor(Node node, TypeProxy type, - std::vector &&arguments) + Constructor(Node node, Type type, std::vector &&arguments) : TypedNode(node), constructor_type_(type), arguments_(std::move(arguments)) {} - Constructor(Node node, TypeProxy type, + Constructor(Node node, Type type, const std::vector &arguments) : TypedNode(node), constructor_type_(type), arguments_(arguments) {} - Type *get_type() { return constructor_type_.get(); } + TypeData *get_type() { return constructor_type_.get(); } - const Type *get_type() const { return constructor_type_.get(); } + const TypeData *get_type() const { return constructor_type_.get(); } - TypeProxy get_type_proxy() const { return constructor_type_; } + Type get_type_proxy() const { return constructor_type_; } size_t arguments_size() const { return arguments_.size(); } - Expression *get_argument_value(size_t id) { + ExprData *get_argument_value(size_t id) { return arguments_.at(id).second.get(); } - const Expression *get_argument_value(size_t id) const { + const ExprData *get_argument_value(size_t id) const { return arguments_.at(id).second.get(); } @@ -496,20 +485,17 @@ public: } private: - TypeProxy constructor_type_; - std::vector, ExpressionProxy>> - arguments_; + Type constructor_type_; + std::vector, Expr>> arguments_; }; class Lambda : public TypedNode { public: - Lambda(Node node, std::vector &&arguments, - ExpressionProxy expression) + Lambda(Node node, std::vector &&arguments, Expr expression) : TypedNode(node), arguments_(std::move(arguments)), expression_(expression) {} - Lambda(Node node, const std::vector &arguments, - ExpressionProxy expression) + Lambda(Node node, const std::vector &arguments, Expr expression) : TypedNode(node), arguments_(arguments), expression_(expression) {} size_t arguments_size() const { return arguments_.size(); } @@ -518,16 +504,16 @@ public: const Identifier *get_argument(size_t id) const { return &arguments_.at(id); } - Expression *get_expression() { return expression_.get(); } + ExprData *get_expression() { return expression_.get(); } - const Expression *get_expression() const { return expression_.get(); } + const ExprData *get_expression() const { return expression_.get(); } private: std::vector arguments_; - ExpressionProxy expression_; + Expr expression_; }; -class Expression : public Node { +class ExprData : public Node { public: using Type = std::variant< // --- flow control @@ -563,7 +549,7 @@ public: public: template - Expression(Node node, T &&expression, bool is_scoped) + ExprData(Node node, T &&expression, bool is_scoped) : Node(node), expression_(std::forward(expression)), is_scoped_(is_scoped) {} @@ -581,9 +567,9 @@ public: return std::nullopt; } - auto get_any() { return &expression_; } + Type &get_any() { return expression_; } - auto get_any() const { return &expression_; } + const Type &get_any() const { return expression_; } bool is_scoped() const { return is_scoped_; } @@ -593,27 +579,22 @@ private: bool is_scoped_ = false; }; -class ExpressionStorage { - friend ExpressionProxy; +class ExprStorage { + friend Expr; public: - ExpressionProxy add_expression(const Expression &expression) { - storage_.push_back(expression); - return ExpressionProxy(*this, storage_.size() - 1); - } - - ExpressionProxy add_expression(Expression &&expression) { + Expr add_expr(ExprData expression) { storage_.push_back(std::move(expression)); - return ExpressionProxy(*this, storage_.size() - 1); + return Expr(*this, storage_.size() - 1); } private: - Expression *get_expression(size_t id) { return &storage_.at(id); } + ExprData *get_expr(size_t id) { return &storage_.at(id); } - const Expression *get_expression(size_t id) const { return &storage_.at(id); } + const ExprData *get_expr(size_t id) const { return &storage_.at(id); } private: - std::vector storage_; + std::vector storage_; }; } // namespace nodes diff --git a/lang/nodes/include/statement_nodes.hpp b/lang/nodes/include/statement_nodes.hpp index 011f8a0..ea933c1 100644 --- a/lang/nodes/include/statement_nodes.hpp +++ b/lang/nodes/include/statement_nodes.hpp @@ -62,15 +62,15 @@ private: class Constraint : public Node { public: - Constraint(Node node, ExpressionProxy expression) + Constraint(Node node, Expr expression) : Node(node), expression_(expression) {} - Expression *get_expression() { return expression_.get(); } + ExprData *get_expression() { return expression_.get(); } - const Expression *get_expression() const { return expression_.get(); } + const ExprData *get_expression() const { return expression_.get(); } private: - ExpressionProxy expression_; + Expr expression_; }; class FunctionDefinition : public Node { @@ -89,7 +89,7 @@ public: : annotation_(annotation), name_(name), before_modifier_(before_modifier), after_modifier_(after_modifier) {} - Argument(const std::optional &annotation, TypeProxy type, + Argument(const std::optional &annotation, Type type, Modifier before_modifier = Modifier::NONE) : annotation_(annotation), type_(type), before_modifier_(before_modifier), @@ -99,36 +99,36 @@ public: // // add type with same annotation and same before_modifier - bool update_type_from(const Argument &other_argument) { - if (type_.has_value() || !other_argument.type_.has_value()) { + bool update_type_from(const Argument &other) { + if (type_.has_value() || !other.type_.has_value()) { return false; } if (annotation_.has_value() && - (!other_argument.annotation_.has_value() || - annotation_.value() != other_argument.annotation_.value())) { + (!other.annotation_.has_value() || + annotation_.value() != other.annotation_.value())) { return false; } - if (before_modifier_ != other_argument.before_modifier_) { + if (before_modifier_ != other.before_modifier_) { return false; } - if (after_modifier_ != other_argument.after_modifier_) { + if (after_modifier_ != other.after_modifier_) { return false; } - annotation_ = other_argument.annotation_; - type_ = other_argument.type_; - before_modifier_ = other_argument.before_modifier_; - after_modifier_ = other_argument.after_modifier_; + annotation_ = other.annotation_; + type_ = other.type_; + before_modifier_ = other.before_modifier_; + after_modifier_ = other.after_modifier_; return true; } // check, that argument modifiers are none or same to different from type // modifiers - bool add_type(const std::optional &annotation, TypeProxy type, + bool add_type(const std::optional &annotation, Type type, nodes::Modifier before_modifier = Modifier::NONE) { if (type_.has_value()) { return false; @@ -205,21 +205,21 @@ public: // - std::optional get_type() { + std::optional get_type() { if (type_.has_value()) { return type_.value().get(); } return std::nullopt; } - std::optional get_type() const { + std::optional get_type() const { if (type_.has_value()) { return type_.value().get(); } return std::nullopt; } - std::optional get_type_proxy() const { + std::optional get_type_proxy() const { if (type_.has_value()) { return type_.value(); } @@ -235,7 +235,7 @@ public: private: std::optional annotation_; std::optional name_; // no name for output arguments - std::optional type_; // no type if it is deduced + std::optional type_; // no type if it is deduced Modifier before_modifier_ = Modifier::NONE; // in, out, ref, none // sync with type Modifier after_modifier_ = @@ -248,7 +248,7 @@ public: const std::optional &name_prefix, const Identifier &name, std::vector &&arguments, bool are_annotations_same_to_names, - std::optional expression) + std::optional expression) : Node(node), docs_(std::move(docs)), constraints_(std::move(constraints)), return_modifier_(return_modifier), is_method_(is_method), name_(name), full_name_(name), @@ -304,14 +304,14 @@ public: // - std::optional get_expression() { + std::optional get_expression() { if (expression_.has_value()) { return expression_.value().get(); } return std::nullopt; } - std::optional get_expression() const { + std::optional get_expression() const { if (expression_.has_value()) { return expression_.value().get(); } @@ -326,9 +326,9 @@ public: // - bool is_same_to(const FunctionDefinition &other_function_definition) const; + bool is_same_to(const FunctionDefinition &other) const; - CombineResult combine(FunctionDefinition &&other_function_definition); + CombineResult combine(FunctionDefinition &&other); private: SymbolDocs docs_; @@ -339,15 +339,14 @@ private: Identifier full_name_; std::vector arguments_; bool are_annotations_same_to_names_; // needed for easier prinitng process - std::optional expression_; + std::optional expression_; }; // refactor ?? class TypeDefinition : public Node { public: TypeDefinition(Node node, SymbolDocs &&docs, bool is_on_heap, const Identifier &name, std::vector &&typeclasses, - std::vector &&arguments, - std::optional type) + std::vector &&arguments, std::optional type) : Node(node), docs_(std::move(docs)), is_on_heap_(is_on_heap), name_(name), typeclasses_(typeclasses), arguments_(std::move(arguments)), type_(std::move(type)) {} @@ -378,7 +377,7 @@ public: // - std::optional get_type() const { + std::optional get_type() const { if (type_.has_value()) { return type_.value(); } @@ -391,9 +390,9 @@ public: // - bool is_same_to(const TypeDefinition &other_type_definition) const; + bool is_same_to(const TypeDefinition &other) const; - CombineResult combine(TypeDefinition &&other_type_definition); + CombineResult combine(TypeDefinition &&other); private: SymbolDocs docs_; @@ -401,11 +400,14 @@ private: Identifier name_; std::vector typeclasses_; std::vector arguments_; - std::optional type_; + std::optional type_; }; class Statement : public Node { public: + using Type = std::variant; + // Statement(const Statement &) = default; // Statement(Statement &&) = default; // Statement &operator=(const Statement &) = default; @@ -413,33 +415,32 @@ public: template Statement(Node node, T &&statement) - : Node(node), expression_(std::forward(statement)) {} + : Node(node), expr_(std::forward(statement)) {} template std::optional get() { - if (std::holds_alternative(expression_)) { - return &std::get(expression_); + if (std::holds_alternative(expr_)) { + return &std::get(expr_); } return std::nullopt; } template std::optional get() const { - if (std::holds_alternative(expression_)) { - return &std::get(expression_); + if (std::holds_alternative(expr_)) { + return &std::get(expr_); } return std::nullopt; } - auto get_any() { return &expression_; } + Type &get_any() { return expr_; } - auto get_any() const { return &expression_; } + const Type &get_any() const { return expr_; } - bool is_same_to(const Statement &other_statement) const; + bool is_same_to(const Statement &other) const; - CombineResult combine(Statement &&other_statement); + CombineResult combine(Statement &&other); private: - std::variant - expression_; + Type expr_; }; } // namespace nodes diff --git a/lang/nodes/include/type_nodes.hpp b/lang/nodes/include/type_nodes.hpp index 35850a6..72451f3 100644 --- a/lang/nodes/include/type_nodes.hpp +++ b/lang/nodes/include/type_nodes.hpp @@ -16,23 +16,23 @@ namespace nodes { -class Type; +class TypeData; class TypeStorage; -class TypeProxy { +class Type { friend TypeStorage; public: - Type *get(); + TypeData *get(); - const Type *get() const; + const TypeData *get() const; - bool operator==(const TypeProxy &other) const; + bool operator==(const Type &other) const; - bool operator!=(const TypeProxy &) const = default; + bool operator!=(const Type &) const = default; private: - TypeProxy(TypeStorage &type_storage, size_t id) + Type(TypeStorage &type_storage, size_t id) : type_storage_(&type_storage), id_(id) {} private: @@ -40,29 +40,30 @@ private: size_t id_; }; -using MaybeTypeProxy = std::optional; -using TypeProxies = std::vector; +using MaybeType = std::optional; +using Types = std::vector; -class Type { +class TypeData { public: - Type(Identifier &&identifier, Modifier modifier = nodes::Modifier::CONST, - const std::optional &annotation = std::nullopt) + TypeData(Identifier &&identifier, Modifier modifier = nodes::Modifier::CONST, + const std::optional &annotation = std::nullopt) : name_(std::move(identifier)), modifier_(modifier), annotation_(annotation) {} - Type(const Identifier &identifier, Modifier modifier = nodes::Modifier::CONST, - const std::optional &annotation = std::nullopt) + TypeData(const Identifier &identifier, + Modifier modifier = nodes::Modifier::CONST, + const std::optional &annotation = std::nullopt) : name_(identifier), modifier_(modifier), annotation_(annotation) {} - Type(Identifier &&identifier, std::vector &¶meters, - Modifier modifier = nodes::Modifier::CONST, - const std::optional &annotation = std::nullopt) + TypeData(Identifier &&identifier, std::vector &¶meters, + Modifier modifier = nodes::Modifier::CONST, + const std::optional &annotation = std::nullopt) : name_(std::move(identifier)), parameters_(std::move(parameters)), modifier_(modifier), annotation_(annotation) {} - Type(const Identifier &identifier, std::vector &¶meters, - Modifier modifier = nodes::Modifier::CONST, - const std::optional &annotation = std::nullopt) + TypeData(const Identifier &identifier, std::vector &¶meters, + Modifier modifier = nodes::Modifier::CONST, + const std::optional &annotation = std::nullopt) : name_(identifier), parameters_(std::move(parameters)), modifier_(modifier), annotation_(annotation) {} @@ -76,16 +77,16 @@ public: size_t parameters_size() const { return parameters_.size(); } - TypeProxy get_parameter_proxy(size_t id) const { return parameters_.at(id); } + Type get_parameter_proxy(size_t id) const { return parameters_.at(id); } - Type *get_parameter(size_t id) { return get_parameter_proxy(id).get(); } + TypeData *get_parameter(size_t id) { return get_parameter_proxy(id).get(); } - const Type *get_parameter(size_t id) const { + const TypeData *get_parameter(size_t id) const { return get_parameter_proxy(id).get(); } // TODO: cache with map ?? - std::optional + std::optional get_parameter_proxy_by_name(const std::string &name) const { const auto it = std::find_if( parameters_.begin(), parameters_.end(), [&name](const auto ¶meter) { @@ -99,16 +100,17 @@ public: return std::nullopt; } - std::optional + std::optional get_parameter_by_name(const std::string &name) const { const auto proxy = get_parameter_proxy_by_name(name); return proxy.has_value() ? proxy.value().get() - : std::optional{}; + : std::optional{}; } - std::optional get_parameter_by_name(const std::string &name) { + std::optional get_parameter_by_name(const std::string &name) { auto proxy = get_parameter_proxy_by_name(name); - return proxy.has_value() ? proxy.value().get() : std::optional{}; + return proxy.has_value() ? proxy.value().get() + : std::optional{}; } // @@ -169,7 +171,7 @@ public: // - bool operator==(const Type &other) const { + bool operator==(const TypeData &other) const { if (name_ != other.name_ || modifier_ != other.modifier_ || parameters_.size() != other.parameters_.size()) { return false; @@ -184,11 +186,11 @@ public: return true; } - bool operator!=(const Type &other) const { return !(*this == other); } + bool operator!=(const TypeData &other) const { return !(*this == other); } // - bool operator<(const Type &other) const { + bool operator<(const TypeData &other) const { if (name_ != other.name_) { return *name_.get() < *other.name_.get(); } @@ -210,11 +212,11 @@ public: return true; } - bool operator>(const Type &other) const { return other < *this; } + bool operator>(const TypeData &other) const { return other < *this; } - bool operator<=(const Type &other) const { return !operator>(other); } + bool operator<=(const TypeData &other) const { return !operator>(other); } - bool operator>=(const Type &other) const { return !operator<(other); } + bool operator>=(const TypeData &other) const { return !operator<(other); } // is parameters count check necessary ?? builtin::Type to_builtin() const { @@ -236,47 +238,45 @@ public: private: Identifier name_; - std::vector parameters_; + std::vector parameters_; Modifier modifier_ = Modifier::NONE; std::optional annotation_; }; class TypeStorage { - friend TypeProxy; + friend Type; public: - TypeProxy primitive(builtin::Type type); + Type primitive(builtin::Type type); - TypeProxy add_array_of(TypeProxy type, Node node = Node()); + Type add_array_of(Type type, Node node = Node()); - TypeProxy add_error_of(TypeProxy type, Node node = Node()); + Type add_error_of(Type type, Node node = Node()); - nodes::TypeProxy add_container_of(std::vector &¶meters, - builtin::Type container, - Node node = Node()); + nodes::Type add_container_of(std::vector &¶meters, + builtin::Type container, Node node = Node()); - nodes::TypeProxy add_modification_of(TypeProxy type, Modifier modifier); + nodes::Type add_modification_of(Type type, Modifier modifier); - TypeProxy add_type(const Type &type) { + Type add_type(const TypeData &type) { storage_.push_back(type); - return TypeProxy(*this, storage_.size() - 1); + return Type(*this, storage_.size() - 1); } - TypeProxy add_type(Type &&type) { + Type add_type(TypeData &&type) { storage_.push_back(std::move(type)); - return TypeProxy(*this, storage_.size() - 1); + return Type(*this, storage_.size() - 1); } // -- deal with generic types (variable types in this case) bool resolve_all_generic_types(); - TypeProxy add_generic_type() { - return add_type(Type(generate_generic_type_identifier())); + Type add_generic_type() { + return add_type(TypeData(generate_generic_type_identifier())); } - bool resolve_generic_name(const std::string &name, - const TypeProxy &actulal_type); + bool resolve_generic_name(const std::string &name, const Type &actulal_type); void clear_resolved_generic_names() { resolved_generic_names_.clear(); } @@ -310,16 +310,15 @@ public: }; // TODO: next iteration of type check - bool unify(TypeProxy left_proxy, TypeProxy right_proxy, - UnifyModePolicy policy); + bool unify(Type left_proxy, Type right_proxy, UnifyModePolicy policy); - bool resolve(TypeProxy generic, - const Type &replacement /* TODO , Mode mode = {}*/); + bool resolve(Type generic, + const TypeData &replacement /* TODO , Mode mode = {}*/); private: - Type *get_type(size_t id) { return &storage_.at(id); } + TypeData *get_type(size_t id) { return &storage_.at(id); } - const Type *get_type(size_t id) const { return &storage_.at(id); } + const TypeData *get_type(size_t id) const { return &storage_.at(id); } Identifier generate_generic_type_identifier(); @@ -336,7 +335,7 @@ private: std::vector> local_name_typeclasses_; // storage for all types - std::vector storage_; + std::vector storage_; }; } // namespace nodes diff --git a/lang/nodes/src/expression_nodes.cpp b/lang/nodes/src/expression_nodes.cpp index f6ca6f5..29a4cee 100644 --- a/lang/nodes/src/expression_nodes.cpp +++ b/lang/nodes/src/expression_nodes.cpp @@ -2,12 +2,8 @@ namespace nodes { -Expression *ExpressionProxy::get() { - return expression_storage_->get_expression(id_); -} +ExprData *Expr::get() { return expression_storage_->get_expr(id_); } -const Expression *ExpressionProxy::get() const { - return expression_storage_->get_expression(id_); -} +const ExprData *Expr::get() const { return expression_storage_->get_expr(id_); } }; // namespace nodes diff --git a/lang/nodes/src/statement_nodes.cpp b/lang/nodes/src/statement_nodes.cpp index 3a13ad9..22d4733 100644 --- a/lang/nodes/src/statement_nodes.cpp +++ b/lang/nodes/src/statement_nodes.cpp @@ -6,9 +6,8 @@ namespace nodes { -bool FunctionDefinition::is_same_to( - const FunctionDefinition &other_function_definition) const { - if (name_ != other_function_definition.name_) { +bool FunctionDefinition::is_same_to(const FunctionDefinition &other) const { + if (name_ != other.name_) { return false; } return true; @@ -20,93 +19,81 @@ bool FunctionDefinition::is_same_to( // name 'arg1 'arg2 'arg3 : Type1 Type2 Type3 -> Type4 = ... // function // definition (with types) // name 'arg1 'arg2 -> 'arg3 = ... // function definition (without types) -CombineResult -FunctionDefinition::combine(FunctionDefinition &&other_function_definition) { +CombineResult FunctionDefinition::combine(FunctionDefinition &&other) { // names should be the same - if (name_ != other_function_definition.name_) { + if (name_ != other.name_) { return CombineResult::DIFFERENT_NAME_ERROR; } // modifiers should be the same - if (return_modifier_ != other_function_definition.return_modifier_) { + if (return_modifier_ != other.return_modifier_) { return CombineResult::DIFFERNENT_MODIFIER_ERROR; } - if (is_method_ != other_function_definition.is_method_) { + if (is_method_ != other.is_method_) { return CombineResult::DIFFERNENT_MODIFIER_ERROR; // other error type ?? } - if (are_annotations_same_to_names_ != - other_function_definition.are_annotations_same_to_names_) { + if (are_annotations_same_to_names_ != other.are_annotations_same_to_names_) { return CombineResult::ARGUMENTS_ERROR; } // only one definition should have constraints - if (!constraints_.empty() && - !other_function_definition.constraints_.empty()) { + if (!constraints_.empty() && !other.constraints_.empty()) { return CombineResult::MORE_THEN_ONE_CONSTRAINTS_ERROR; } // only one definition should have expression (body) - if (expression_.has_value() && - other_function_definition.expression_.has_value()) { + if (expression_.has_value() && other.expression_.has_value()) { return CombineResult::MORE_THEN_ONE_DEFINITION_BODY_ERROR; } // only one definition should have documentation if (docs_.get_description().has_value() && - other_function_definition.docs_.get_description().has_value()) { + other.docs_.get_description().has_value()) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } if (docs_.annotations_info_size() > 0 && - other_function_definition.docs_.annotations_info_size() > 0) { + other.docs_.annotations_info_size() > 0) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } // check, that function definitions have same named arguments - for (size_t i = 0; i < std::max(arguments_.size(), - other_function_definition.arguments_.size()); + for (size_t i = 0; i < std::max(arguments_.size(), other.arguments_.size()); ++i) { - if (i < arguments_.size() && - i < other_function_definition.arguments_.size()) { + if (i < arguments_.size() && i < other.arguments_.size()) { // annotations should be the same if ((!arguments_[i].get_annotation().has_value() && - !other_function_definition.arguments_[i] - .get_annotation() - .has_value()) || + !other.arguments_[i].get_annotation().has_value()) || (arguments_[i].get_annotation().has_value() && - other_function_definition.arguments_[i] - .get_annotation() - .has_value() && + other.arguments_[i].get_annotation().has_value() && *arguments_[i].get_annotation().value() != - *other_function_definition.arguments_[i] - .get_annotation() - .value())) { + *other.arguments_[i].get_annotation().value())) { return CombineResult::ARGUMENTS_ERROR; } // argument names should be the same if ((!arguments_[i].get_name().has_value() && - !other_function_definition.arguments_[i].get_name().has_value()) || + !other.arguments_[i].get_name().has_value()) || (arguments_[i].get_name().has_value() && - other_function_definition.arguments_[i].get_name().has_value() && + other.arguments_[i].get_name().has_value() && arguments_[i].get_name().value() != - other_function_definition.arguments_[i].get_name().value())) { + other.arguments_[i].get_name().value())) { return CombineResult::ARGUMENTS_ERROR; } // types should be the same (if present in both definitions) if (arguments_[i].get_type().has_value() && - other_function_definition.arguments_[i].get_type().has_value() && + other.arguments_[i].get_type().has_value() && *arguments_[i].get_type().value() != - *other_function_definition.arguments_[i].get_type().value()) { + *other.arguments_[i].get_type().value()) { return CombineResult::ARGUMENTS_ERROR; } // argument modifiers should be the same if (arguments_[i].get_before_modifier() != - other_function_definition.arguments_[i].get_before_modifier() || + other.arguments_[i].get_before_modifier() || arguments_[i].get_after_modifier() != - other_function_definition.arguments_[i].get_after_modifier()) { + other.arguments_[i].get_after_modifier()) { return CombineResult::ARGUMENTS_ERROR; } } else if (i < arguments_.size()) { @@ -121,14 +108,12 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) { } } else { // i < other_function_definition.size() // annotations should be the same - if (other_function_definition.arguments_[i] - .get_annotation() - .has_value()) { + if (other.arguments_[i].get_annotation().has_value()) { return CombineResult::ARGUMENTS_ERROR; } // names should be the same - if (other_function_definition.arguments_[i].get_name().has_value()) { + if (other.arguments_[i].get_name().has_value()) { return CombineResult::ARGUMENTS_ERROR; } } @@ -136,29 +121,28 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) { // combine docs // all docs should be in one definition - if (other_function_definition.docs_.get_description().has_value() || - other_function_definition.docs_.annotations_info_size() > 0) { + if (other.docs_.get_description().has_value() || + other.docs_.annotations_info_size() > 0) { if (docs_.annotations_info_size() > 0 || docs_.get_description().has_value()) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } - docs_ = std::move(other_function_definition.docs_); + docs_ = std::move(other.docs_); } - if (!other_function_definition.constraints_.empty()) { - constraints_ = std::move(other_function_definition.constraints_); + if (!other.constraints_.empty()) { + constraints_ = std::move(other.constraints_); } - if (other_function_definition.expression_.has_value()) { - expression_ = other_function_definition.expression_; + if (other.expression_.has_value()) { + expression_ = other.expression_; } - for (size_t i = 0; i < other_function_definition.arguments_.size(); ++i) { + for (size_t i = 0; i < other.arguments_.size(); ++i) { if (i < arguments_.size()) { - if (other_function_definition.arguments_[i].get_type().has_value()) { - if (arguments_[i].update_type_from( - other_function_definition.arguments_[i])) { + if (other.arguments_[i].get_type().has_value()) { + if (arguments_[i].update_type_from(other.arguments_[i])) { error_handling::handle_internal_error( "Function arguments are not properly checked before merging " "during combination", @@ -166,16 +150,15 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) { } } } else { - arguments_.push_back(std::move(other_function_definition.arguments_[i])); + arguments_.push_back(std::move(other.arguments_[i])); } } return CombineResult::OK; } -bool TypeDefinition::is_same_to( - const TypeDefinition &other_type_definition) const { - if (name_ != other_type_definition.name_) { +bool TypeDefinition::is_same_to(const TypeDefinition &other) const { + if (name_ != other.name_) { return false; } return true; @@ -185,87 +168,85 @@ bool TypeDefinition::is_same_to( // Type[...] 'A 'B 'C; // declare type / define typeclass (statement without // args in this case) // Type[...] 'A 'B 'C = ... // define type -CombineResult TypeDefinition::combine(TypeDefinition &&other_type_definition) { +CombineResult TypeDefinition::combine(TypeDefinition &&other) { // name should be same - if (name_ != other_type_definition.name_) { + if (name_ != other.name_) { return CombineResult::DIFFERENT_NAME_ERROR; } // modifier should be the same - if (is_on_heap_ != other_type_definition.is_on_heap_) { + if (is_on_heap_ != other.is_on_heap_) { return CombineResult::DIFFERNENT_MODIFIER_ERROR; } // typeclasses should be the same - if (typeclasses_.size() != other_type_definition.typeclasses_.size()) { + if (typeclasses_.size() != other.typeclasses_.size()) { return CombineResult::ARGUMENTS_ERROR; } for (size_t i = 0; i < typeclasses_.size(); ++i) { - if (typeclasses_[i] != other_type_definition.typeclasses_[i]) { + if (typeclasses_[i] != other.typeclasses_[i]) { return CombineResult::ARGUMENTS_ERROR; } } // arguments should be the same - if (arguments_.size() != other_type_definition.arguments_.size()) { + if (arguments_.size() != other.arguments_.size()) { return CombineResult::ARGUMENTS_ERROR; } for (size_t i = 0; i < arguments_.size(); ++i) { - if (arguments_[i] != other_type_definition.arguments_[i]) { + if (arguments_[i] != other.arguments_[i]) { return CombineResult::ARGUMENTS_ERROR; } } // only one definition should have documentation if (docs_.get_description().has_value() && - other_type_definition.docs_.get_description().has_value()) { + other.docs_.get_description().has_value()) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } if (docs_.annotations_info_size() > 0 && - other_type_definition.docs_.annotations_info_size() > 0) { + other.docs_.annotations_info_size() > 0) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } // only one type should define type / body - if (type_.has_value() && other_type_definition.type_.has_value()) { + if (type_.has_value() && other.type_.has_value()) { return CombineResult::MORE_THEN_ONE_DEFINITION_BODY_ERROR; } // combine docs // all docs should be in one definition - if (other_type_definition.docs_.get_description().has_value() || - other_type_definition.docs_.annotations_info_size() > 0) { + if (other.docs_.get_description().has_value() || + other.docs_.annotations_info_size() > 0) { if (docs_.annotations_info_size() > 0 || docs_.get_description().has_value()) { return CombineResult::MORE_THEN_ONE_DOCS_ERROR; } - docs_ = std::move(other_type_definition.docs_); + docs_ = std::move(other.docs_); } - if (other_type_definition.type_.has_value()) { - type_ = std::move(other_type_definition.type_); + if (other.type_.has_value()) { + type_ = std::move(other.type_); } return CombineResult::OK; } -bool Statement::is_same_to(const Statement &other_statement) const { - if (expression_.index() != other_statement.expression_.index()) { +bool Statement::is_same_to(const Statement &other) const { + if (expr_.index() != other.expr_.index()) { return false; } - switch (expression_.index()) { + switch (expr_.index()) { case 0: // Import return false; case 1: // TypeDefinition - return std::get(expression_) - .is_same_to( - std::move(std::get(other_statement.expression_))); + return std::get(expr_).is_same_to( + std::move(std::get(other.expr_))); case 2: // FunctionDefinition - return std::get(expression_) - .is_same_to(std::move( - std::get(other_statement.expression_))); + return std::get(expr_).is_same_to( + std::move(std::get(other.expr_))); case 3: // Extra return false; case 4: // EmptyLines @@ -280,22 +261,20 @@ bool Statement::is_same_to(const Statement &other_statement) const { exit(1); } -CombineResult Statement::combine(Statement &&other_statement) { - if (expression_.index() != other_statement.expression_.index()) { +CombineResult Statement::combine(Statement &&other) { + if (expr_.index() != other.expr_.index()) { return CombineResult::DIFFERENT_STATEMENT_TYPES; } - switch (expression_.index()) { + switch (expr_.index()) { case 0: // Import return CombineResult::STATEMENTS_CANT_BE_COMBINED_ERROR; case 1: // TypeDefinition - return std::get(expression_) - .combine( - std::move(std::get(other_statement.expression_))); + return std::get(expr_).combine( + std::move(std::get(other.expr_))); case 2: // FunctionDefinition - return std::get(expression_) - .combine(std::move( - std::get(other_statement.expression_))); + return std::get(expr_).combine( + std::move(std::get(other.expr_))); case 3: // Extra return CombineResult::STATEMENTS_CANT_BE_COMBINED_ERROR; case 4: // EmptyLines diff --git a/lang/nodes/src/type_nodes.cpp b/lang/nodes/src/type_nodes.cpp index 6ee941b..4e7548d 100644 --- a/lang/nodes/src/type_nodes.cpp +++ b/lang/nodes/src/type_nodes.cpp @@ -2,60 +2,59 @@ namespace nodes { -Type *TypeProxy::get() { return type_storage_->get_type(id_); } +TypeData *Type::get() { return type_storage_->get_type(id_); } -const Type *TypeProxy::get() const { return type_storage_->get_type(id_); } +const TypeData *Type::get() const { return type_storage_->get_type(id_); } -bool TypeProxy::operator==(const TypeProxy &other) const { +bool Type::operator==(const Type &other) const { return *get() == *other.get(); } // -TypeProxy TypeStorage::primitive(builtin::Type type) { +Type TypeStorage::primitive(builtin::Type type) { auto iter = primitive_type_ids_.find(type); if (iter != primitive_type_ids_.end()) { - return TypeProxy(*this, iter->second); + return Type(*this, iter->second); } else { primitive_type_ids_[type] = storage_.size(); - return add_type(Type(Identifier(Node(), Identifier::SIMPLE_TYPE, - builtin::types::to_string(type)))); + return add_type(TypeData(Identifier(Node(), Identifier::SIMPLE_TYPE, + builtin::types::to_string(type)))); } } -TypeProxy TypeStorage::add_array_of(TypeProxy type, Node node) { +Type TypeStorage::add_array_of(Type type, Node node) { if (type.type_storage_ != this) { error_handling::handle_general_error( "TypeStorage: Can't add array of type from another type " "storage"); } - std::vector parameters; + std::vector parameters; parameters.push_back(type); - return add_type(Type(Identifier(node, Identifier::SIMPLE_TYPE, - builtin::types::ARRAY_IDENTIFIER), - std::move(parameters))); + return add_type(TypeData(Identifier(node, Identifier::SIMPLE_TYPE, + builtin::types::ARRAY_IDENTIFIER), + std::move(parameters))); } -TypeProxy TypeStorage::add_error_of(TypeProxy type, Node node) { +Type TypeStorage::add_error_of(Type type, Node node) { if (type.type_storage_ != this) { error_handling::handle_general_error( "TypeStorage: Can't add error of type from another type " "storage"); } - std::vector parameters; + std::vector parameters; parameters.push_back(type); - return add_type(Type(Identifier(node, Identifier::SIMPLE_TYPE, - builtin::types::ERROR_IDENTIFIER), - std::move(parameters))); + return add_type(TypeData(Identifier(node, Identifier::SIMPLE_TYPE, + builtin::types::ERROR_IDENTIFIER), + std::move(parameters))); } -nodes::TypeProxy -TypeStorage::add_container_of(std::vector &¶meters, - builtin::Type container, Node node) { +nodes::Type TypeStorage::add_container_of(std::vector &¶meters, + builtin::Type container, Node node) { for (auto ¶meter : parameters) { if (parameter.type_storage_ != this) { error_handling::handle_general_error( @@ -66,20 +65,19 @@ TypeStorage::add_container_of(std::vector &¶meters, } return add_type( - nodes::Type(nodes::Identifier(node, nodes::Identifier::SIMPLE_TYPE, - builtin::types::to_string(container)), - std::move(parameters))); + nodes::TypeData(nodes::Identifier(node, nodes::Identifier::SIMPLE_TYPE, + builtin::types::to_string(container)), + std::move(parameters))); } -nodes::TypeProxy TypeStorage::add_modification_of(TypeProxy type, - Modifier modifier) { +nodes::Type TypeStorage::add_modification_of(Type type, Modifier modifier) { if (type.type_storage_ != this) { error_handling::handle_general_error( "TypeStorage: Can't add modification of type from another type " "storage"); } - Type type_copy = *type.get(); + TypeData type_copy = *type.get(); type_copy.set_modifier(modifier); return add_type(std::move(type_copy)); @@ -124,7 +122,7 @@ bool TypeStorage::resolve_all_generic_types() { } bool TypeStorage::resolve_generic_name(const std::string &name, - const TypeProxy &actulal_type) { + const Type &actulal_type) { if (actulal_type.type_storage_ != this) { error_handling::handle_general_error( "TypeStorage: Can't resolve generic type to type from another type " @@ -203,7 +201,7 @@ TypeStorage::get_local_type_requirements(const std::string &name) const { // TODO -bool TypeStorage::unify(TypeProxy /*left_proxy*/, TypeProxy /*right_proxy*/, +bool TypeStorage::unify(Type /*left_proxy*/, Type /*right_proxy*/, UnifyModePolicy /*policy*/) { // Type &left = left_id.get(); // Type &right = right_id.get(); @@ -265,13 +263,12 @@ bool TypeStorage::unify(TypeProxy /*left_proxy*/, TypeProxy /*right_proxy*/, throw std::exception(); } -bool TypeStorage::resolve(TypeProxy generic, - const Type &replacement /* TODO , Mode mode = {}*/) { +bool TypeStorage::resolve( + Type generic, const TypeData &replacement /* TODO , Mode mode = {}*/) { error_handling::ensure(generic.get()->is_generic(), "Type should be generic"); error_handling::ensure(generic.get()->is_generic(), "Type should be generic"); for (auto &type : storage_) { - if (type.is_generic() && - type.get_name() == generic.get()->get_name()) { + if (type.is_generic() && type.get_name() == generic.get()->get_name()) { type = replacement; // type.mode = mode; } diff --git a/lang/printers/include/expression_printers.hpp b/lang/printers/include/expression_printers.hpp index 2a6836b..eca995c 100644 --- a/lang/printers/include/expression_printers.hpp +++ b/lang/printers/include/expression_printers.hpp @@ -4,7 +4,7 @@ #include "expression_nodes.hpp" namespace printers { -void print(const nodes::Expression &expression, printers::Printer &printer); +void print(const nodes::ExprData &expression, printers::Printer &printer); // --- flow control diff --git a/lang/printers/include/type_printers.hpp b/lang/printers/include/type_printers.hpp index 840d67a..28bc71e 100644 --- a/lang/printers/include/type_printers.hpp +++ b/lang/printers/include/type_printers.hpp @@ -5,7 +5,7 @@ namespace printers { -void print(const nodes::Type &type, printers::Printer &printer); +void print(const nodes::TypeData &type, printers::Printer &printer); // void print_tuple_type(const nodes::TupleType &type, printers::Printer // &printer); diff --git a/lang/printers/src/basic_printers.cpp b/lang/printers/src/basic_printers.cpp index de018d6..61b57dd 100644 --- a/lang/printers/src/basic_printers.cpp +++ b/lang/printers/src/basic_printers.cpp @@ -86,7 +86,7 @@ void print(const nodes::Modifier &modifier, Printer &printer, } void print(const nodes::Literal &literal, Printer &printer) { - switch (literal.get_any()->index()) { + switch (literal.get_any().index()) { case 0: // float printer.print(std::to_string(*literal.get().value())); return; diff --git a/lang/printers/src/expression_printers.cpp b/lang/printers/src/expression_printers.cpp index 659e2f1..f81a87e 100644 --- a/lang/printers/src/expression_printers.cpp +++ b/lang/printers/src/expression_printers.cpp @@ -8,7 +8,7 @@ namespace printers { namespace utils { -bool is_block_expression(const nodes::Expression &expression) { +bool is_block_expression(const nodes::ExprData &expression) { return expression.get().has_value() && expression.get().value()->get_type() == nodes::Container::BLOCK; @@ -16,13 +16,13 @@ bool is_block_expression(const nodes::Expression &expression) { } // namespace utils -void print(const nodes::Expression &expression, printers::Printer &printer) { +void print(const nodes::ExprData &expression, printers::Printer &printer) { if (expression.is_scoped()) { printer.print("("); } std::visit([&printer](const auto &arg) -> void { print(arg, printer); }, - *expression.get_any()); + expression.get_any()); if (expression.is_scoped()) { printer.print(")"); diff --git a/lang/printers/src/statement_printers.cpp b/lang/printers/src/statement_printers.cpp index a063340..0647b08 100644 --- a/lang/printers/src/statement_printers.cpp +++ b/lang/printers/src/statement_printers.cpp @@ -19,7 +19,7 @@ namespace printers { void print(const nodes::Statement &statement, Printer &printer) { std::visit([&printer](const auto &arg) -> void { print(arg, printer); }, - *statement.get_any()); + statement.get_any()); if (not statement.get().has_value()) { printer.new_indent_line(); diff --git a/lang/printers/src/type_printers.cpp b/lang/printers/src/type_printers.cpp index 4d0ad1e..a558e7c 100644 --- a/lang/printers/src/type_printers.cpp +++ b/lang/printers/src/type_printers.cpp @@ -6,7 +6,7 @@ namespace printers { // TODO: better printing format for builtin types -void print(const nodes::Type &type, printers::Printer &printer) { +void print(const nodes::TypeData &type, printers::Printer &printer) { if (type.has_annotation()) { print_annotation(*type.get_annotation().value(), printer); printer.space(); diff --git a/lang/type_check/include/expression_type_check.hpp b/lang/type_check/include/expression_type_check.hpp index 256997d..9194214 100644 --- a/lang/type_check/include/expression_type_check.hpp +++ b/lang/type_check/include/expression_type_check.hpp @@ -8,10 +8,10 @@ namespace type_check { template <> -struct CheckTask : public CheckTaskBase { - using CheckTaskBase::CheckTaskBase; +struct CheckTask : public CheckTaskBase { + using CheckTaskBase::CheckTaskBase; - Result operator()(const nodes::Expression &expr, + Result operator()(const nodes::ExprData &expr, const Arguments &args) override; }; diff --git a/lang/type_check/include/type_check_utils.hpp b/lang/type_check/include/type_check_utils.hpp index 0048436..7cb3574 100644 --- a/lang/type_check/include/type_check_utils.hpp +++ b/lang/type_check/include/type_check_utils.hpp @@ -17,14 +17,14 @@ class State { public: struct VariableInfo { - nodes::TypeProxy type; + nodes::Type type; nodes::NameDefinition::Modifier modifier; }; public: State(Log &log) : log_(log) {} - bool insert_variable(const std::string &name, nodes::TypeProxy type, + bool insert_variable(const std::string &name, nodes::Type type, nodes::NameDefinition::Modifier modifier) { Log::Context logc(log_, utils::Log::Area::kTypeCheck); @@ -49,7 +49,7 @@ public: return std::nullopt; } - bool bring_type(nodes::TypeProxy type) { + bool bring_type(nodes::Type type) { Log::Context logc(log_, utils::Log::Area::kTypeCheck); if (contexts_.empty()) { @@ -65,7 +65,7 @@ public: return true; } - bool return_type(nodes::TypeProxy type) { + bool return_type(nodes::Type type) { Log::Context logc(log_, utils::Log::Area::kTypeCheck); if (contexts_.empty()) { @@ -91,7 +91,7 @@ private: // TODO: argument for property is returned type should be merged // returns brought type, return type is merged with next context or with // brought type in last context - nodes::MaybeTypeProxy exit_context() { + nodes::MaybeType exit_context() { Log::Context logc(log_, utils::Log::Area::kTypeCheck); if (contexts_.empty()) { @@ -143,8 +143,8 @@ public: // } public: - nodes::MaybeTypeProxy brought_type; - nodes::MaybeTypeProxy returned_type; + nodes::MaybeType brought_type; + nodes::MaybeType returned_type; std::unordered_map variables; private: @@ -159,7 +159,7 @@ private: // -using Exprs = nodes::ExpressionStorage; +using Exprs = nodes::ExprStorage; using Types = nodes::TypeStorage; using Names = names::NameTree; @@ -183,20 +183,20 @@ public: return copy; } - Arguments expect(nodes::TypeProxies types) const { + Arguments expect(nodes::Types types) const { Arguments copy(*this); copy.expected_types_ = types; return copy; } - Arguments expect(nodes::MaybeTypeProxy type) const { + Arguments expect(nodes::MaybeType type) const { Arguments copy(*this); - copy.expected_types_ = (type.has_value() ? nodes::TypeProxies{type.value()} - : nodes::TypeProxies{}); + copy.expected_types_ = + (type.has_value() ? nodes::Types{type.value()} : nodes::Types{}); return copy; } - Arguments pass(nodes::MaybeTypeProxy type) const { + Arguments pass(nodes::MaybeType type) const { Arguments copy(*this); copy.passed_type_ = type; return copy; @@ -214,16 +214,16 @@ public: return copy; } - nodes::TypeProxies get_expected() const { return expected_types_; }; + nodes::Types get_expected() const { return expected_types_; }; - nodes::MaybeTypeProxy get_passed() const { return passed_type_; }; + nodes::MaybeType get_passed() const { return passed_type_; }; // TODO: add check, that there is no passed type for some nodes ?? // TODO: arguments builder ?? private: - nodes::TypeProxies expected_types_; - nodes::MaybeTypeProxy passed_type_; + nodes::Types expected_types_; + nodes::MaybeType passed_type_; }; // @@ -231,7 +231,7 @@ private: class ContextHolder { public: ContextHolder(State &state, const nodes::Node &node, - nodes::MaybeTypeProxy *context_exit_type) + nodes::MaybeType *context_exit_type) : state_(state), context_exit_type_(context_exit_type) { state.enter_context(node); } @@ -252,7 +252,7 @@ public: private: State &state_; - nodes::MaybeTypeProxy *context_exit_type_; + nodes::MaybeType *context_exit_type_; }; // @@ -262,18 +262,18 @@ public: // for invalid type static Result invalid() { return Result(); } - explicit Result(nodes::TypeProxy type) : type_(type) {} + explicit Result(nodes::Type type) : type_(type) {} // - nodes::TypeProxy &get() { + nodes::Type &get() { utils::Assert(type_.has_value(), "Access to invalid type in TypeCheckResult"); return type_.value(); } - const nodes::TypeProxy &get() const { + const nodes::Type &get() const { utils::Assert(type_.has_value(), "Access to invalid type in TypeCheckResult"); @@ -281,7 +281,7 @@ public: return type_.value(); } - void set(nodes::TypeProxy type) { type_ = type; } + void set(nodes::Type type) { type_ = type; } // @@ -291,15 +291,15 @@ private: Result() = default; private: - nodes::MaybeTypeProxy type_ = {}; + nodes::MaybeType type_ = {}; }; using MaybeResult = std::optional; // -nodes::TypeProxy check_same_to_pass_type_in_arguments( - nodes::TypeProxy type, const Arguments &arguments, const nodes::Node &node, +nodes::Type check_same_to_pass_type_in_arguments( + nodes::Type type, const Arguments &arguments, const nodes::Node &node, Executor &executor, const std::string &message = "Different type with passed one", bool handle_errors = true); @@ -310,13 +310,12 @@ nodes::TypeProxy check_same_to_pass_type_in_arguments( // const std::string &message = "Type can't be passed to this node"); Result type_same_to_expected( - nodes::TypeProxy type, const Arguments &argumensr, const nodes::Node &node, + nodes::Type type, const Arguments &argumensr, const nodes::Node &node, Executor &executor, const std::string &message = "Different type with expected one", bool handle_errors = true); -Result type_check_from_arguments(nodes::TypeProxy type, - const Arguments &arguments, +Result type_check_from_arguments(nodes::Type type, const Arguments &arguments, const nodes::Node &node, Executor &executor, bool handle_errors = true); @@ -328,16 +327,16 @@ std::optional find_name_definition(const std::string &name, const nodes::Node &node, Executor &executor, bool handle_errors = true); -nodes::MaybeTypeProxy unfold_user_defined_type(nodes::TypeProxy type, - const nodes::Node &node, - Executor &executor, - bool handle_errors = true); +nodes::MaybeType unfold_user_defined_type(nodes::Type type, + const nodes::Node &node, + Executor &executor, + bool handle_errors = true); -nodes::MaybeTypeProxy get_field_type_by_name(nodes::TypeProxy type, - const std::string &field, - const nodes::Node &node, - Executor &executor, - bool handle_errors = true); +nodes::MaybeType get_field_type_by_name(nodes::Type type, + const std::string &field, + const nodes::Node &node, + Executor &executor, + bool handle_errors = true); void type_check_error(const std::string &message, const nodes::Node &node, Executor &executor, bool handle_error = true); @@ -353,7 +352,8 @@ template struct CheckTask { template struct CheckTaskBase : public Task { using Task::Task; - template Result Run(const OtherN &node, const Arguments &args) { + template + Result Run(const OtherN &node, const Arguments &args) { CheckTask task(this->executor); return task(node, args); } diff --git a/lang/type_check/src/basic_type_check.cpp b/lang/type_check/src/basic_type_check.cpp index 9283b10..6a06cbc 100644 --- a/lang/type_check/src/basic_type_check.cpp +++ b/lang/type_check/src/basic_type_check.cpp @@ -2,9 +2,9 @@ namespace type_check { -nodes::TypeProxy get_literal_type(const nodes::Literal &literal, - Executor &executor) { - switch (literal.get_any()->index()) { +nodes::Type get_literal_type(const nodes::Literal &literal, + Executor &executor) { + switch (literal.get_any().index()) { case 0: // float return executor.state().primitive(builtin::Type::FLOAT); case 1: // double diff --git a/lang/type_check/src/expression_type_check.cpp b/lang/type_check/src/expression_type_check.cpp index 7cf6b34..8a7bbb0 100644 --- a/lang/type_check/src/expression_type_check.cpp +++ b/lang/type_check/src/expression_type_check.cpp @@ -11,13 +11,13 @@ namespace type_check { -Result CheckTask::operator()(const nodes::Expression &expr, - const Arguments &arguments) { +Result CheckTask::operator()(const nodes::ExprData &expr, + const Arguments &arguments) { return std::visit( [this, &arguments](const auto &node) -> Result { return Run(node, arguments); }, - *expr.get_any()); + expr.get_any()); } // --- flow control @@ -43,7 +43,7 @@ Result CheckTask::operator()(const nodes::Match &expr, Run(*current_case->get_value(), Arguments{} .expect_builtin(builtin::Type::BOOL, executor) - .pass(value_result.is_invalid() ? nodes::MaybeTypeProxy{} + .pass(value_result.is_invalid() ? nodes::MaybeType{} : expression_result.value().get())); // TODO: use type modifiers ?? @@ -60,7 +60,7 @@ Result CheckTask::operator()(const nodes::Match &expr, Run(*current_case->get_condition().value(), Arguments{}.expect(expression_result.has_value() ? expression_result.value().get() - : nodes::MaybeTypeProxy{})); + : nodes::MaybeType{})); if (!expression_result.has_value() && !case_result.is_invalid()) { expression_result = std::move(case_result); @@ -99,7 +99,7 @@ Result CheckTask::operator()(const nodes::Condition &expr, Run(*expr.get_case(i).first, Arguments{}.expect(expression_result.has_value() ? expression_result.value().get() - : nodes::MaybeTypeProxy{})); + : nodes::MaybeType{})); if (!expression_result.has_value() && !case_result.is_invalid()) { expression_result = std::move(case_result); @@ -110,7 +110,7 @@ Result CheckTask::operator()(const nodes::Condition &expr, Run(*expr.get_else_case().value(), Arguments{}.expect(expression_result.has_value() ? expression_result.value().get() - : nodes::MaybeTypeProxy{})); + : nodes::MaybeType{})); } if (!expression_result.has_value()) { @@ -216,7 +216,7 @@ Result CheckTask::CheckArray(const nodes::Container &expr, Result CheckTask::CheckBlock(const nodes::Container &expr, const Arguments &args) { - nodes::MaybeTypeProxy context_exit_type; + nodes::MaybeType context_exit_type; { ContextHolder context_holder( @@ -601,7 +601,7 @@ Result CheckTask::operator()(const nodes::Constructor &expr, // TODO: check that is not typeclass ?? - nodes::TypeProxy type = type_definition->get_type().value(); + nodes::Type type = type_definition->get_type().value(); // TODO: work with different parametric types: tuple, variant, ... @@ -770,7 +770,7 @@ Result CheckTask::operator()(const nodes::Constructor &expr, chosen_variant_option.value()))); } else { // TODO: error, if there is more then one possible variant in // answer - nodes::TypeProxies possible_options; + nodes::Types possible_options; for (size_t i = 0; i < type.get()->parameters_size(); ++i) { possible_options.push_back(type.get()->get_parameter_proxy(i)); } diff --git a/lang/type_check/src/type_check_utils.cpp b/lang/type_check/src/type_check_utils.cpp index 21a121c..173cc39 100644 --- a/lang/type_check/src/type_check_utils.cpp +++ b/lang/type_check/src/type_check_utils.cpp @@ -6,10 +6,9 @@ namespace type_check { // pass type -> compare types, return bool // no pass type -> return type -nodes::TypeProxy check_same_to_pass_type_in_arguments( - nodes::TypeProxy type, const Arguments &arguments, - const nodes::Node & /*node*/, Executor &executor, - const std::string &message, bool handle_errors) { +nodes::Type check_same_to_pass_type_in_arguments( + nodes::Type type, const Arguments &arguments, const nodes::Node & /*node*/, + Executor &executor, const std::string &message, bool handle_errors) { Log::Context logc(executor.log(), utils::Log::Area::kTypeCheck); if (not arguments.get_passed().has_value()) { @@ -38,7 +37,7 @@ bool check_no_pass_type_in_arguments(const Arguments &arguments, return true; } -Result type_same_to_expected(nodes::TypeProxy type, const Arguments &arguments, +Result type_same_to_expected(nodes::Type type, const Arguments &arguments, const nodes::Node & /*node*/, Executor &executor, const std::string &message, bool handle_errors) { Log::Context logc(executor.log(), utils::Log::Area::kTypeCheck); @@ -52,7 +51,7 @@ Result type_same_to_expected(nodes::TypeProxy type, const Arguments &arguments, // TODO: use 'can cast to' (for modifiers), instead '==' bool all_not_expected = std::all_of( expected.begin(), expected.end(), - [type](nodes::TypeProxy expected_type) { return type != expected_type; }); + [type](nodes::Type expected_type) { return type != expected_type; }); if (all_not_expected and handle_errors) { logc.Error({{message}} /* TODO: node */); @@ -61,7 +60,7 @@ Result type_same_to_expected(nodes::TypeProxy type, const Arguments &arguments, return Result{expected.front()}; // any can be choosen } -Result type_check_from_arguments(nodes::TypeProxy /*type*/, +Result type_check_from_arguments(nodes::Type /*type*/, const Arguments & /*arguments*/, const nodes::Node & /*node*/, Executor &executor, bool /*handle_errors*/) { @@ -119,9 +118,10 @@ find_name_definition(const std::string &name, const nodes::Node &node, "Node in name tree is not name definition", handle_errors); } -std::optional -unfold_user_defined_type(nodes::TypeProxy type, const nodes::Node &node, - Executor &executor, bool handle_errors) { +std::optional unfold_user_defined_type(nodes::Type type, + const nodes::Node &node, + Executor &executor, + bool handle_errors) { Log::Context logc(executor.log(), utils::Log::Area::kTypeCheck); const auto maybe_type_definition = find_type_definition( @@ -147,11 +147,11 @@ unfold_user_defined_type(nodes::TypeProxy type, const nodes::Node &node, return maybe_type_definition.value()->get_type().value(); } -std::optional get_field_type_by_name(nodes::TypeProxy type, - const std::string &field, - const nodes::Node &node, - Executor &executor, - bool handle_errors) { +std::optional get_field_type_by_name(nodes::Type type, + const std::string &field, + const nodes::Node &node, + Executor &executor, + bool handle_errors) { Log::Context logc(executor.log(), utils::Log::Area::kTypeCheck); switch (type.get()->to_builtin()) {