diff --git a/src/language/node_processor/AffectationProcessor.hpp b/src/language/node_processor/AffectationProcessor.hpp index f52362c32aea3e43fc44d2cda9a4252fb63ecad9..fc469edae5cd4a36fa87cfdd663cf3e43d3c8c10 100644 --- a/src/language/node_processor/AffectationProcessor.hpp +++ b/src/language/node_processor/AffectationProcessor.hpp @@ -101,7 +101,6 @@ class AffectationExecutor final : public IAffectationExecutor m_lhs = os.str(); } } else { - static_assert(std::is_same_v<OperatorT, language::pluseq_op>, "unexpected operator type"); if constexpr (std::is_same_v<std::string, DataT>) { m_lhs += std::get<std::string>(rhs); } else if constexpr (std::is_arithmetic_v<DataT>) { @@ -173,7 +172,9 @@ class ComponentAffectationExecutor final : public IAffectationExecutor if constexpr (std::is_integral_v<IndexValueT>) { index_value = value; } else { + // LCOV_EXCL_START throw parse_error("unexpected error: invalid index type", std::vector{m_index_expression.begin()}); + // LCOV_EXCL_STOP } }, value_variant); @@ -188,7 +189,6 @@ class ComponentAffectationExecutor final : public IAffectationExecutor m_lhs_array[index_value] = std::to_string(std::get<DataT>(rhs)); } } else { - static_assert(std::is_same_v<OperatorT, language::pluseq_op>, "unexpected operator type"); if constexpr (std::is_same_v<std::string, DataT>) { m_lhs_array[index_value] += std::get<std::string>(rhs); } else { @@ -253,10 +253,12 @@ class AffectationProcessor final : public INodeProcessor Assert(found); DataVariant& value = i_symbol->attributes().value(); + // LCOV_EXCL_START if (array_expression.m_data_type != ASTNodeDataType::vector_t) { throw parse_error("unexpected error: invalid lhs (expecting R^d)", std::vector{array_subscript_expression.begin()}); } + // LCOV_EXCL_STOP auto& index_expression = *array_subscript_expression.children[1]; @@ -291,14 +293,18 @@ class AffectationProcessor final : public INodeProcessor std::make_unique<AffectationExecutorT>(node, std::get<ArrayTypeT>(value), index_expression); break; } + // LCOV_EXCL_START default: { throw parse_error("unexpected error: invalid vector dimension", std::vector{array_subscript_expression.begin()}); } + // LCOV_EXCL_STOP } } else { + // LCOV_EXCL_START throw parse_error("unexpected error: invalid lhs", std::vector{node.children[0]->begin()}); + // LCOV_EXCL_STOP } } }; @@ -328,7 +334,9 @@ class AffectationFromListProcessor final : public INodeProcessor std::is_same_v<T, double>) { v[i] = child_value; } else { + // LCOV_EXCL_START throw parse_error("unexpected error: unexpected right hand side type in affectation", m_node.begin()); + // LCOV_EXCL_STOP } }, children_values[i]); diff --git a/tests/test_AffectationProcessor.cpp b/tests/test_AffectationProcessor.cpp index bbbccadda20615efdd174c42bd61346390ee621d..a821277d95819ebd2a80136d74100f3f6c53201b 100644 --- a/tests/test_AffectationProcessor.cpp +++ b/tests/test_AffectationProcessor.cpp @@ -103,10 +103,49 @@ TEST_CASE("AffectationProcessor", "[language]") SECTION("R") { - CHECK_AFFECTATION_RESULT("R r = -1;", "r", -1.); - CHECK_AFFECTATION_RESULT("R r = true;", "r", 1.); - CHECK_AFFECTATION_RESULT("R r = false;", "r", 0.); - CHECK_AFFECTATION_RESULT("R r = -2.3;", "r", -2.3); + CHECK_AFFECTATION_RESULT("R r = -1;", "r", double{-1}); + CHECK_AFFECTATION_RESULT("R r = true;", "r", double{1}); + CHECK_AFFECTATION_RESULT("R r = false;", "r", double{0}); + CHECK_AFFECTATION_RESULT("R r = -2.3;", "r", double{-2.3}); + } + + SECTION("R^1") + { + CHECK_AFFECTATION_RESULT("R^1 x = -1;", "x", (TinyVector<1>{-1})); + CHECK_AFFECTATION_RESULT("R^1 x = true;", "x", (TinyVector<1>{true})); + CHECK_AFFECTATION_RESULT("R^1 x = false;", "x", (TinyVector<1>{false})); + CHECK_AFFECTATION_RESULT("R^1 x = -2.3;", "x", (TinyVector<1>{-2.3})); + CHECK_AFFECTATION_RESULT("R^1 x; x[0] = -1;", "x", (TinyVector<1>{-1})); + CHECK_AFFECTATION_RESULT("R^1 x; x[0] = true;", "x", (TinyVector<1>{true})); + CHECK_AFFECTATION_RESULT("R^1 x; x[0] = false;", "x", (TinyVector<1>{false})); + CHECK_AFFECTATION_RESULT("R^1 x; x[0] = -2.3;", "x", (TinyVector<1>{-2.3})); + + CHECK_AFFECTATION_RESULT("R^1 x = 0;", "x", (TinyVector<1>{zero})); + CHECK_AFFECTATION_RESULT("R^1 x; x = 0;", "x", (TinyVector<1>{zero})); + } + + SECTION("R^2") + { + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true);", "x", (TinyVector<2>{-1, true})); + CHECK_AFFECTATION_RESULT("R^2 x = (true, false);", "x", (TinyVector<2>{true, false})); + CHECK_AFFECTATION_RESULT("R^2 x = (-0.3, 12);", "x", (TinyVector<2>{-0.3, 12})); + CHECK_AFFECTATION_RESULT("R^2 x; x[0] = -1; x[1] = true;", "x", (TinyVector<2>{-1, true})); + CHECK_AFFECTATION_RESULT("R^2 x; x[0] = true; x[1] = false;", "x", (TinyVector<2>{true, false})); + CHECK_AFFECTATION_RESULT("R^2 x; x[0] = -0.3; x[1] = 12;", "x", (TinyVector<2>{-0.3, 12})); + + CHECK_AFFECTATION_RESULT("R^2 x = 0;", "x", (TinyVector<2>{zero})); + CHECK_AFFECTATION_RESULT("R^2 x; x = 0;", "x", (TinyVector<2>{zero})); + } + + SECTION("R^3") + { + CHECK_AFFECTATION_RESULT("R^3 x = (-1, true, false);", "x", (TinyVector<3>{-1, true, false})); + CHECK_AFFECTATION_RESULT("R^3 x = (-0.3, 12, 6.2);", "x", (TinyVector<3>{-0.3, 12, 6.2})); + CHECK_AFFECTATION_RESULT("R^3 x; x[0] = -1; x[1] = true; x[2] = false;", "x", (TinyVector<3>{-1, true, false})); + CHECK_AFFECTATION_RESULT("R^3 x; x[0] = -0.3; x[1] = 12; x[2] = 6.2;", "x", (TinyVector<3>{-0.3, 12, 6.2})); + + CHECK_AFFECTATION_RESULT("R^3 x = 0;", "x", (TinyVector<3>{zero})); + CHECK_AFFECTATION_RESULT("R^3 x; x = 0;", "x", (TinyVector<3>{zero})); } } @@ -136,6 +175,28 @@ TEST_CASE("AffectationProcessor", "[language]") CHECK_AFFECTATION_RESULT("R r = 3.3; r += false;", "r", 3.3); CHECK_AFFECTATION_RESULT("R r = 2; r += 1.1;", "r", 3.1); } + + SECTION("R^1") + { + CHECK_AFFECTATION_RESULT("R^1 x = -1; R^1 y = 1; x += y;", "x", (TinyVector<1>{-1} + TinyVector<1>{1})); + CHECK_AFFECTATION_RESULT("R^1 x = 2; x[0] += 1;", "x", (TinyVector<1>{2} + TinyVector<1>{1})); + } + + SECTION("R^2") + { + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); R^2 y = (1,3); x += y;", "x", + (TinyVector<2>{-1, true} + TinyVector<2>{1, 3})); + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); x[0] += 2; x[1] += 1;", "x", + (TinyVector<2>{-1, true} + TinyVector<2>{2, 1})); + } + + SECTION("R^3") + { + CHECK_AFFECTATION_RESULT("R^3 x = (-1, true, false); R^3 y = (1,2,3); x += y;", "x", + (TinyVector<3>{-1, true, false} + TinyVector<3>{1, 2, 3})); + CHECK_AFFECTATION_RESULT("R^3 x = (-0.3, 12, 6.2); x[0] += 1; x[1] += -3; x[2] += 1;", "x", + (TinyVector<3>{-0.3, 12, 6.2} + TinyVector<3>{1, -3, 1})); + } } SECTION("-=") @@ -164,6 +225,28 @@ TEST_CASE("AffectationProcessor", "[language]") CHECK_AFFECTATION_RESULT("R r = 3.3; r -= false;", "r", 3.3); CHECK_AFFECTATION_RESULT("R r = 2; r -= 1.1;", "r", (2. - 1.1)); } + + SECTION("R^1") + { + CHECK_AFFECTATION_RESULT("R^1 x = -1; R^1 y = 1; x -= y;", "x", (TinyVector<1>{-1} - TinyVector<1>{1})); + CHECK_AFFECTATION_RESULT("R^1 x = 2; x[0] -= 1;", "x", (TinyVector<1>{2} - TinyVector<1>{1})); + } + + SECTION("R^2") + { + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); R^2 y = (1,3); x -= y;", "x", + (TinyVector<2>{-1, true} - TinyVector<2>{1, 3})); + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); x[0] -= 2; x[1] -= 1;", "x", + (TinyVector<2>{-1, true} - TinyVector<2>{2, 1})); + } + + SECTION("R^3") + { + CHECK_AFFECTATION_RESULT("R^3 x = (-1, true, false); R^3 y = (1,2,3); x-=y;", "x", + (TinyVector<3>{-1, true, false} - TinyVector<3>{1, 2, 3})); + CHECK_AFFECTATION_RESULT("R^3 x = (-0.3, 12, 6.2); x[0] -= 1; x[1] -= -3; x[2] -= 1;", "x", + (TinyVector<3>{-0.3, 12, 6.2} - TinyVector<3>{1, -3, 1})); + } } SECTION("*=") @@ -192,6 +275,26 @@ TEST_CASE("AffectationProcessor", "[language]") CHECK_AFFECTATION_RESULT("R r = 3.3; r *= false;", "r", (3.3 * false)); CHECK_AFFECTATION_RESULT("R r = 2; r *= 1.1;", "r", (2. * 1.1)); } + + SECTION("R^1") + { + CHECK_AFFECTATION_RESULT("R^1 x = 2; x *= 2;", "x", (TinyVector<1>{TinyVector<1>{2} *= 2})); + CHECK_AFFECTATION_RESULT("R^1 x = 2; x[0] *= 1.3;", "x", (TinyVector<1>{2 * 1.3})); + } + + SECTION("R^2") + { + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); x *= 3;", "x", (TinyVector<2>{TinyVector<2>{-1, true} *= 3})); + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); x[0] *= 2; x[1] *= 3;", "x", (TinyVector<2>{-1 * 2, true * 3})); + } + + SECTION("R^3") + { + CHECK_AFFECTATION_RESULT("R^3 x = (-1, true, false); x*=5.2;", "x", + (TinyVector<3>{TinyVector<3>{-1, true, false} *= 5.2})); + CHECK_AFFECTATION_RESULT("R^3 x = (-0.3, 12, 6.2); x[0] *= -1; x[1] *= -3; x[2] *= 2;", "x", + (TinyVector<3>{-0.3 * -1, 12 * -3, 6.2 * 2})); + } } SECTION("/=") @@ -217,6 +320,22 @@ TEST_CASE("AffectationProcessor", "[language]") CHECK_AFFECTATION_RESULT("R r = 1.1; r /= true;", "r", (1.1 / true)); CHECK_AFFECTATION_RESULT("R r = 2; r /= 1.1;", "r", (2. / 1.1)); } + + SECTION("R^1") + { + CHECK_AFFECTATION_RESULT("R^1 x = 2; x[0] /= 1.3;", "x", (TinyVector<1>{2 / 1.3})); + } + + SECTION("R^2") + { + CHECK_AFFECTATION_RESULT("R^2 x = (-1, true); x[0] /= 2; x[1] /= 3;", "x", (TinyVector<2>{-1. / 2., true / 3.})); + } + + SECTION("R^3") + { + CHECK_AFFECTATION_RESULT("R^3 x = (-0.3, 12, 6.2); x[0] /= -1.2; x[1] /= -3.1; x[2] /= 2.4;", "x", + (TinyVector<3>{-0.3 / -1.2, 12 / -3.1, 6.2 / 2.4})); + } } SECTION("errors") diff --git a/tests/test_ListAffectationProcessor.cpp b/tests/test_ListAffectationProcessor.cpp index a888ade21d253e44684c186e57dcd95a6540a189..a5775adbbe0ac60b4168cea884e58ac938a4309b 100644 --- a/tests/test_ListAffectationProcessor.cpp +++ b/tests/test_ListAffectationProcessor.cpp @@ -108,6 +108,13 @@ TEST_CASE("ListAffectationProcessor", "[language]") } } + SECTION("compound R^d from '0'") + { + CHECK_AFFECTATION_RESULT(R"(R^3*R^2*R^1 (x,y,z) = (0,0,0);)", "x", (TinyVector<3>{zero})); + CHECK_AFFECTATION_RESULT(R"(R^3*R^2*R^1 (x,y,z) = (0,0,0);)", "y", (TinyVector<2>{zero})); + CHECK_AFFECTATION_RESULT(R"(R^3*R^2*R^1 (x,y,z) = (0,0,0);)", "z", (TinyVector<1>{zero})); + } + SECTION("compound with subscript values") { CHECK_AFFECTATION_RESULT(R"(R^3 x; (x[0], x[2], x[1]) = (4, 6, 5);)", "x", (TinyVector<3>{4, 5, 6}));