diff --git a/src/language/ASTNodeAffectationExpressionBuilder.cpp b/src/language/ASTNodeAffectationExpressionBuilder.cpp index 8c018fe3bc22f60c6d57500abefdb6463c757a22..38e6f9802f90c324df42d30e7ef40ccdc05a8a77 100644 --- a/src/language/ASTNodeAffectationExpressionBuilder.cpp +++ b/src/language/ASTNodeAffectationExpressionBuilder.cpp @@ -162,6 +162,29 @@ ASTNodeAffectationExpressionBuilder::ASTNodeAffectationExpressionBuilder(ASTNode case ASTNodeDataType::string_t: { n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, std::string>>(n); break; + } + case ASTNodeDataType::vector_t: { + switch (data_type.dimension()) { + case 1: { + n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, TinyVector<1>>>(n); + break; + } + case 2: { + n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, TinyVector<2>>>(n); + break; + } + case 3: { + n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, TinyVector<3>>>(n); + break; + } + // LCOV_EXCL_START + default: { + throw parse_error("unexpected error: invalid vector dimension for string affectation", + std::vector{n.children[1]->begin()}); + } + // LCOV_EXCL_STOP + } + break; } // LCOV_EXCL_START default: { diff --git a/src/language/node_processor/AffectationProcessor.hpp b/src/language/node_processor/AffectationProcessor.hpp index 4449fd8cefef8742bd82c7b17b0af08303a4b340..8eeaa8b349cb2063e91dbb673ffc12d9275ac870 100644 --- a/src/language/node_processor/AffectationProcessor.hpp +++ b/src/language/node_processor/AffectationProcessor.hpp @@ -93,15 +93,23 @@ class AffectationExecutor final : public IAffectationExecutor if constexpr (std::is_same_v<OperatorT, language::eq_op>) { if constexpr (std::is_same_v<std::string, DataT>) { m_lhs = std::get<DataT>(rhs); - } else { + } else if constexpr (std::is_arithmetic_v<DataT>) { m_lhs = std::to_string(std::get<DataT>(rhs)); + } else { + std::ostringstream os; + os << std::get<DataT>(rhs) << std::ends; + 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 { + } else if constexpr (std::is_arithmetic_v<DataT>) { m_lhs += std::to_string(std::get<DataT>(rhs)); + } else { + std::ostringstream os; + os << std::get<DataT>(rhs) << std::ends; + m_lhs += os.str(); } } } else { diff --git a/tests/test_AffectationToStringProcessor.cpp b/tests/test_AffectationToStringProcessor.cpp index 57209b308197b42b5fd9d74851aed7c15e79db12..3f692c3bc071990f67629db3ee52d4417743326a 100644 --- a/tests/test_AffectationToStringProcessor.cpp +++ b/tests/test_AffectationToStringProcessor.cpp @@ -57,6 +57,21 @@ TEST_CASE("ASTAffectationToStringProcessor", "[language]") CHECK_AFFECTATION_RESULT(R"(string s; s = -1;)", "s", std::to_string(-1l)); CHECK_AFFECTATION_RESULT(R"(string s; s = true;)", "s", std::to_string(true)); CHECK_AFFECTATION_RESULT(R"(string s; s = 2.3;)", "s", std::to_string(2.3)); + { + std::ostringstream os; + os << TinyVector<1>{13} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^1 x = 13; string s; s = x;)", "s", os.str()); + } + { + std::ostringstream os; + os << TinyVector<2>{2, 3} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^2 x = (2,3); string s; s = x;)", "s", os.str()); + } + { + std::ostringstream os; + os << TinyVector<3>{1, 2, 3} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^3 x = (1,2,3); string s; s = x;)", "s", os.str()); + } } SECTION("+=") @@ -66,5 +81,20 @@ TEST_CASE("ASTAffectationToStringProcessor", "[language]") CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += -1;)", "s", (std::string("foo") + std::to_string(-1l))); CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += true;)", "s", (std::string("foo") + std::to_string(true))); CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += 2.3;)", "s", (std::string("foo") + std::to_string(2.3))); + { + std::ostringstream os; + os << "foo" << TinyVector<1>{13} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^1 x = 13; string s="foo"; s += x;)", "s", os.str()); + } + { + std::ostringstream os; + os << "foo" << TinyVector<2>{2, 3} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^2 x = (2,3); string s="foo"; s += x;)", "s", os.str()); + } + { + std::ostringstream os; + os << "foo" << TinyVector<3>{1, 2, 3} << std::ends; + CHECK_AFFECTATION_RESULT(R"(R^3 x = (1,2,3); string s="foo"; s += x;)", "s", os.str()); + } } }