diff --git a/tests/FixturesForBuiltinT.hpp b/tests/FixturesForBuiltinT.hpp index a51de19d71a513e8399c720f6eca9ff163e06900..d2ff0fcb3b0db1e37a9a5f00487eb8288f1aec3d 100644 --- a/tests/FixturesForBuiltinT.hpp +++ b/tests/FixturesForBuiltinT.hpp @@ -53,4 +53,16 @@ operator/(double, const std::shared_ptr<const double>&) throw std::runtime_error("runtime error rhs"); } +inline std::shared_ptr<const double> +operator<<(const std::shared_ptr<const double>& p_a, const std::shared_ptr<const double>& p_b) +{ + return std::make_shared<double>(static_cast<int>(*p_a) << static_cast<int>(*p_b)); +} + +inline std::shared_ptr<const double> +operator>>(const std::shared_ptr<const double>& p_a, const std::shared_ptr<const double>& p_b) +{ + return std::make_shared<double>(static_cast<int>(*p_a) >> static_cast<int>(*p_b)); +} + #endif // FIXTURES_FOR_BUILTIN_T_HPP diff --git a/tests/test_ASTNodeBinaryOperatorExpressionBuilder.cpp b/tests/test_ASTNodeBinaryOperatorExpressionBuilder.cpp index b99aa045958dc356cb03f2b548e8c632d58b124d..4a9f2bb8d64b047f3355d3f16b81f1e1dada2143 100644 --- a/tests/test_ASTNodeBinaryOperatorExpressionBuilder.cpp +++ b/tests/test_ASTNodeBinaryOperatorExpressionBuilder.cpp @@ -1,6 +1,8 @@ #include <catch2/catch_test_macros.hpp> #include <catch2/matchers/catch_matchers_all.hpp> +#include <FixturesForBuiltinT.hpp> + #include <language/ast/ASTBuilder.hpp> #include <language/ast/ASTModulesImporter.hpp> #include <language/ast/ASTNodeBinaryOperatorExpressionBuilder.hpp> @@ -10,6 +12,11 @@ #include <language/ast/ASTNodeTypeCleaner.hpp> #include <language/ast/ASTSymbolTableBuilder.hpp> #include <language/utils/ASTPrinter.hpp> +#include <language/utils/BasicAffectationRegistrerFor.hpp> +#include <language/utils/BinaryOperatorProcessorBuilder.hpp> +#include <language/utils/DataHandler.hpp> +#include <language/utils/OperatorRepository.hpp> +#include <language/utils/TypeDescriptor.hpp> #include <utils/Demangle.hpp> #include <pegtl/string_input.hpp> @@ -1417,6 +1424,111 @@ x!=y; } } + SECTION("shift") + { +#define CHECK_AST_BUILTIN_SHIFT_EXPRESSION(data, expected_output) \ + { \ + TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + ASTModulesImporter{*ast}; \ + \ + BasicAffectationRegisterFor<EmbeddedData>{ASTNodeDataType::build<ASTNodeDataType::type_id_t>("builtin_t")}; \ + \ + OperatorRepository& repository = OperatorRepository::instance(); \ + \ + repository.addBinaryOperator<language::shift_left_op>( \ + std::make_shared< \ + BinaryOperatorProcessorBuilder<language::shift_left_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ + \ + repository.addBinaryOperator<language::shift_right_op>( \ + std::make_shared< \ + BinaryOperatorProcessorBuilder<language::shift_right_op, std::shared_ptr<const double>, \ + std::shared_ptr<const double>, std::shared_ptr<const double>>>()); \ + \ + SymbolTable& symbol_table = *ast->m_symbol_table; \ + auto [i_symbol, success] = symbol_table.add(builtin_data_type.nameOfTypeId(), ast->begin()); \ + if (not success) { \ + throw UnexpectedError("cannot add '" + builtin_data_type.nameOfTypeId() + "' type for testing"); \ + } \ + \ + i_symbol->attributes().setDataType(ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>()); \ + i_symbol->attributes().setIsInitialized(); \ + i_symbol->attributes().value() = symbol_table.typeEmbedderTable().size(); \ + symbol_table.typeEmbedderTable().add(std::make_shared<TypeDescriptor>(builtin_data_type.nameOfTypeId())); \ + \ + auto [i_symbol_bt_a, success_bt_a] = symbol_table.add("bt_a", ast->begin()); \ + if (not success_bt_a) { \ + throw UnexpectedError("cannot add 'bt_a' of type builtin_t for testing"); \ + } \ + i_symbol_bt_a->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_a->attributes().setIsInitialized(); \ + i_symbol_bt_a->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(3.2))); \ + \ + auto [i_symbol_bt_b, success_bt_b] = symbol_table.add("bt_b", ast->begin()); \ + if (not success_bt_b) { \ + throw UnexpectedError("cannot add 'bt_b' of type builtin_t for testing"); \ + } \ + i_symbol_bt_b->attributes().setDataType(ast_node_data_type_from<std::shared_ptr<const double>>); \ + i_symbol_bt_b->attributes().setIsInitialized(); \ + i_symbol_bt_b->attributes().value() = \ + EmbeddedData(std::make_shared<DataHandler<const double>>(std::make_shared<double>(5.3))); \ + \ + ASTNodeTypeCleaner<language::import_instruction>{*ast}; \ + \ + ASTSymbolTableBuilder{*ast}; \ + ASTNodeDataTypeBuilder{*ast}; \ + \ + ASTNodeDeclarationToAffectationConverter{*ast}; \ + ASTNodeTypeCleaner<language::var_declaration>{*ast}; \ + \ + ASTNodeExpressionBuilder{*ast}; \ + \ + std::stringstream ast_output; \ + ast_output << '\n' << ASTPrinter{*ast, ASTPrinter::Format::raw, {ASTPrinter::Info::exec_type}}; \ + \ + REQUIRE(ast_output.str() == expected_output); \ + } + + SECTION("shift left (builtin)") + { + std::string_view data = R"( +let m : builtin_t; +let n : builtin_t; +n << m; +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) + `-(language::shift_left_op:BinaryExpressionProcessor<language::shift_left_op, std::shared_ptr<double const>, std::shared_ptr<double const>, std::shared_ptr<double const> >) + +-(language::name:n:NameProcessor) + `-(language::name:m:NameProcessor) +)"; + + CHECK_AST_BUILTIN_SHIFT_EXPRESSION(data, result); + } + + SECTION("shift right (builtin)") + { + std::string_view data = R"( +let m : builtin_t; +let n : builtin_t; +n >> m; +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) + `-(language::shift_right_op:BinaryExpressionProcessor<language::shift_right_op, std::shared_ptr<double const>, std::shared_ptr<double const>, std::shared_ptr<double const> >) + +-(language::name:n:NameProcessor) + `-(language::name:m:NameProcessor) +)"; + + CHECK_AST_BUILTIN_SHIFT_EXPRESSION(data, result); + } + } + SECTION("Errors") { SECTION("Invalid binary operator type")