Skip to content
Snippets Groups Projects
Commit 60362405 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add R^d -> string affectations and corresponding tests

parent 20c8864a
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -162,6 +162,29 @@ ASTNodeAffectationExpressionBuilder::ASTNodeAffectationExpressionBuilder(ASTNode ...@@ -162,6 +162,29 @@ ASTNodeAffectationExpressionBuilder::ASTNodeAffectationExpressionBuilder(ASTNode
case ASTNodeDataType::string_t: { case ASTNodeDataType::string_t: {
n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, std::string>>(n); n.m_node_processor = std::make_unique<AffectationProcessor<OperatorT, std::string, std::string>>(n);
break; 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 // LCOV_EXCL_START
default: { default: {
......
...@@ -93,15 +93,23 @@ class AffectationExecutor final : public IAffectationExecutor ...@@ -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<OperatorT, language::eq_op>) {
if constexpr (std::is_same_v<std::string, DataT>) { if constexpr (std::is_same_v<std::string, DataT>) {
m_lhs = std::get<DataT>(rhs); m_lhs = std::get<DataT>(rhs);
} else { } else if constexpr (std::is_arithmetic_v<DataT>) {
m_lhs = std::to_string(std::get<DataT>(rhs)); 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 { } else {
static_assert(std::is_same_v<OperatorT, language::pluseq_op>, "unexpected operator type"); static_assert(std::is_same_v<OperatorT, language::pluseq_op>, "unexpected operator type");
if constexpr (std::is_same_v<std::string, DataT>) { if constexpr (std::is_same_v<std::string, DataT>) {
m_lhs += std::get<std::string>(rhs); 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)); 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 { } else {
......
...@@ -57,6 +57,21 @@ TEST_CASE("ASTAffectationToStringProcessor", "[language]") ...@@ -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 = -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 = true;)", "s", std::to_string(true));
CHECK_AFFECTATION_RESULT(R"(string s; s = 2.3;)", "s", std::to_string(2.3)); 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("+=") SECTION("+=")
...@@ -66,5 +81,20 @@ TEST_CASE("ASTAffectationToStringProcessor", "[language]") ...@@ -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 += -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 += 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))); 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());
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment