diff --git a/src/language/node_processor/AffectationProcessor.hpp b/src/language/node_processor/AffectationProcessor.hpp index cf3f5f65949d571f9cc2b48c402827680c4e9e0e..f408b632facf9de0c1374ab3b2667d4d0cd8a8a4 100644 --- a/src/language/node_processor/AffectationProcessor.hpp +++ b/src/language/node_processor/AffectationProcessor.hpp @@ -153,6 +153,21 @@ class AffectationExecutor final : public IAffectationExecutor } }, rhs); + } else if constexpr (std::is_same_v<TinyMatrix<1>, ValueT>) { + std::visit( + [&](auto&& v) { + using Vi_T = std::decay_t<decltype(v)>; + if constexpr (std::is_convertible_v<Vi_T, double>) { + m_lhs = v; + } else { + // LCOV_EXCL_START + throw UnexpectedError("unexpected rhs type in affectation"); + // LCOV_EXCL_STOP + } + }, + rhs); + } else { + throw UnexpectedError("invalid value type"); } } else { AffOp<OperatorT>().eval(m_lhs, std::get<DataT>(rhs)); @@ -599,14 +614,12 @@ class AffectationToTupleProcessor final : public INodeProcessor os << v; *m_lhs = std::vector<std::string>{os.str()}; } - } else if constexpr (std::is_same_v<ValueT, TinyVector<1>> and std::is_arithmetic_v<T>) { - *m_lhs = std::vector<TinyVector<1>>{TinyVector<1>{static_cast<double>(v)}}; - } else if constexpr (std::is_same_v<ValueT, TinyVector<2>> and std::is_same_v<T, int64_t>) { - Assert(v == 0); - *m_lhs = std::vector<TinyVector<2>>{TinyVector<2>{zero}}; - } else if constexpr (std::is_same_v<ValueT, TinyVector<3>> and std::is_same_v<T, int64_t>) { + } else if constexpr ((std::is_same_v<ValueT, TinyVector<1>> or + std::is_same_v<ValueT, TinyMatrix<1>>)and std::is_arithmetic_v<T>) { + *m_lhs = std::vector<ValueT>{ValueT{static_cast<double>(v)}}; + } else if constexpr ((is_tiny_vector_v<ValueT> or is_tiny_matrix_v<ValueT>)and std::is_same_v<T, int64_t>) { Assert(v == 0); - *m_lhs = std::vector<TinyVector<3>>{TinyVector<3>{zero}}; + *m_lhs = std::vector<ValueT>{ValueT{zero}}; } else { // LCOV_EXCL_START throw ParseError("unexpected error: unexpected right hand side type in affectation", m_node.begin()); diff --git a/src/language/utils/AffectationRegisterForRnxn.cpp b/src/language/utils/AffectationRegisterForRnxn.cpp index 314fb36aaf4d5116418c6366d7b69ad5718091b7..efa95d92bbcfd32af37f6c3f69477d7ee985a560 100644 --- a/src/language/utils/AffectationRegisterForRnxn.cpp +++ b/src/language/utils/AffectationRegisterForRnxn.cpp @@ -21,6 +21,11 @@ AffectationRegisterForRnxn<Dimension>::_register_eq_op() std::vector<std::shared_ptr<const ASTNodeDataType>>{}), std::make_shared<AffectationToTinyMatrixFromListProcessorBuilder< language::eq_op, TinyMatrix<Dimension>>>()); + + repository + .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rnxn), + ASTNodeDataType::build<ASTNodeDataType::int_t>(), + std::make_shared<AffectationToTupleProcessorBuilder<TinyMatrix<Dimension>>>()); } template <> @@ -49,25 +54,25 @@ AffectationRegisterForRnxn<1>::_register_eq_op() language::eq_op>(Rnxn, ASTNodeDataType::build<ASTNodeDataType::double_t>(), std::make_shared<AffectationProcessorBuilder<language::eq_op, TinyMatrix<Dimension>, double>>()); - // repository - // .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rn), - // ASTNodeDataType::build<ASTNodeDataType::bool_t>(), - // std::make_shared<AffectationToTupleProcessorBuilder<TinyVector<Dimension>>>()); - - // repository - // .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rn), - // ASTNodeDataType::build<ASTNodeDataType::unsigned_int_t>(), - // std::make_shared<AffectationToTupleProcessorBuilder<TinyVector<Dimension>>>()); - - // repository - // .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rn), - // ASTNodeDataType::build<ASTNodeDataType::int_t>(), - // std::make_shared<AffectationToTupleProcessorBuilder<TinyVector<Dimension>>>()); - - // repository - // .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rn), - // ASTNodeDataType::build<ASTNodeDataType::double_t>(), - // std::make_shared<AffectationToTupleProcessorBuilder<TinyVector<Dimension>>>()); + repository + .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rnxn), + ASTNodeDataType::build<ASTNodeDataType::bool_t>(), + std::make_shared<AffectationToTupleProcessorBuilder<TinyMatrix<Dimension>>>()); + + repository + .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rnxn), + ASTNodeDataType::build<ASTNodeDataType::unsigned_int_t>(), + std::make_shared<AffectationToTupleProcessorBuilder<TinyMatrix<Dimension>>>()); + + repository + .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rnxn), + ASTNodeDataType::build<ASTNodeDataType::int_t>(), + std::make_shared<AffectationToTupleProcessorBuilder<TinyMatrix<Dimension>>>()); + + repository + .addAffectation<language::eq_op>(ASTNodeDataType::build<ASTNodeDataType::tuple_t>(Rnxn), + ASTNodeDataType::build<ASTNodeDataType::double_t>(), + std::make_shared<AffectationToTupleProcessorBuilder<TinyMatrix<Dimension>>>()); } template <size_t Dimension> diff --git a/tests/test_AffectationToTupleProcessor.cpp b/tests/test_AffectationToTupleProcessor.cpp index 963e931bb0bba74e10063649896e0e11db109601..fccd584c6eb7c164e9b9389d8395f56bc3803ca0 100644 --- a/tests/test_AffectationToTupleProcessor.cpp +++ b/tests/test_AffectationToTupleProcessor.cpp @@ -79,6 +79,23 @@ let s :(string); s = x; let s :(R^1); s = 1.3; )", "s", (std::vector<TinyVector<1>>{TinyVector<1>{1.3}})); + + const std::string A_string = []() -> std::string { + std::ostringstream os; + os << TinyMatrix<2, double>{1, 2, 3, 4}; + return os.str(); + }(); + + CHECK_AFFECTATION_RESULT(R"( +let A :R^2x2, A = (1,2,3,4); +let s :(string); s = A; +)", + "s", (std::vector<std::string>{A_string})); + + CHECK_AFFECTATION_RESULT(R"( +let s :(R^1x1); s = 1.3; +)", + "s", (std::vector<TinyMatrix<1>>{TinyMatrix<1>{1.3}})); } SECTION("Affectations from list") @@ -137,6 +154,45 @@ let x : R^1, x = 1; let t :(R^1); t = (x,2); )", "t", (std::vector<TinyVector<1>>{TinyVector<1>{1}, TinyVector<1>{2}})); + + const std::string A_string = []() -> std::string { + std::ostringstream os; + os << TinyMatrix<2, double>{1, 2, 3, 4}; + return os.str(); + }(); + + CHECK_AFFECTATION_RESULT(R"( +let A : R^2x2, A = (1,2,3,4); +let s : (string); s = (2.,3, A); +)", + "s", (std::vector<std::string>{std::to_string(2.), std::to_string(3), A_string})); + + CHECK_AFFECTATION_RESULT(R"( +let A : R^2x2, A = (1,2,3,4); +let t :(R^2x2); t = (A,0); +)", + "t", (std::vector<TinyMatrix<2>>{TinyMatrix<2>{1, 2, 3, 4}, TinyMatrix<2>{0, 0, 0, 0}})); + + CHECK_AFFECTATION_RESULT(R"( +let t :(R^2x2); t = ((1,2,3,4),0); +)", + "t", (std::vector<TinyMatrix<2>>{TinyMatrix<2>{1, 2, 3, 4}, TinyMatrix<2>{0, 0, 0, 0}})); + + CHECK_AFFECTATION_RESULT(R"( +let t :(R^2x2); t = (0); +)", + "t", (std::vector<TinyMatrix<2>>{TinyMatrix<2>{0, 0, 0, 0}})); + + CHECK_AFFECTATION_RESULT(R"( +let t :(R^3x3); t = 0; +)", + "t", (std::vector<TinyMatrix<3>>{TinyMatrix<3>{0, 0, 0, 0, 0, 0, 0, 0, 0}})); + + CHECK_AFFECTATION_RESULT(R"( +let x : R^1x1, x = 1; +let t :(R^1x1); t = (x,2); +)", + "t", (std::vector<TinyMatrix<1>>{TinyMatrix<1>{1}, TinyMatrix<1>{2}})); } SECTION("Affectations from tuple") @@ -153,6 +209,18 @@ let s :(string); s = x; )", "s", (std::vector<std::string>{x_string})); + const std::string A_string = []() -> std::string { + std::ostringstream os; + os << TinyMatrix<3, double>{1, 2, 3, 4, 5, 6, 7, 8, 9}; + return os.str(); + }(); + + CHECK_AFFECTATION_RESULT(R"( +let A :(R^3x3), A = ((1,2,3,4,5,6,7,8,9)); +let s :(string); s = A; +)", + "s", (std::vector<std::string>{A_string})); + CHECK_AFFECTATION_RESULT(R"( let x :(R), x = (1,2,3); let s :(string); s = x;