Extract leading sign when extract UDQ factor

This commit is contained in:
Joakim Hove 2020-10-25 20:46:23 +01:00
parent 0994a1a2fe
commit e9dadcf93e
2 changed files with 45 additions and 3 deletions

View File

@ -83,7 +83,15 @@ UDQParseNode UDQParser::current() const {
UDQASTNode UDQParser::parse_factor() {
double sign = 1.0;
auto current = this->current();
if (current.type == UDQTokenType::binary_op_add || current.type == UDQTokenType::binary_op_sub) {
if (current.type == UDQTokenType::binary_op_sub)
sign = -1.0;
this->next();
current = this->current();
}
if (current.type == UDQTokenType::open_paren) {
this->next();
@ -94,7 +102,7 @@ UDQASTNode UDQParser::parse_factor() {
return UDQASTNode(UDQTokenType::error);
this->next();
return inner_expr;
return sign * inner_expr;
}
if (UDQ::scalarFunc(current.type) || UDQ::elementalUnaryFunc(current.type)) {
@ -109,14 +117,14 @@ UDQASTNode UDQParser::parse_factor() {
return UDQASTNode(UDQTokenType::error);
this->next();
return UDQASTNode(func_node.type, func_node.value, arg_expr);
return sign * UDQASTNode(func_node.type, func_node.value, arg_expr);
} else
return UDQASTNode(UDQTokenType::error);
}
UDQASTNode node(current.type, current.value, current.selector);
this->next();
return node;
return sign * node;
}
UDQASTNode UDQParser::parse_pow() {

View File

@ -2293,3 +2293,37 @@ BOOST_AUTO_TEST_CASE(UDQ_DIV_TEST) {
}
BOOST_AUTO_TEST_CASE(UDQ_LEADING_SIGN) {
std::string deck_string = R"(
SCHEDULE
UDQ
DEFINE FU_VAR1 - 100 + 215 /
DEFINE FU_VAR2 (-100 + 200) / 10 /
DEFINE FU_VAR3 -(100 + 200) * -10 /
DEFINE FU_VAR4 2^-1 /
ASSIGN FU_VAR6 2 /
ASSIGN FU_VAR7 3 /
DEFINE FU_VAR5 (-0.00000041232 * (FU_VAR6 ^ 2)) + (0.0010395 * FU_VAR7) + 0.16504 /
/
)";
auto schedule = make_schedule(deck_string);
UDQState udq_state(0);
SummaryState st(std::chrono::system_clock::now());
const auto& udq = schedule.getUDQConfig(0);
udq.eval(0, st, udq_state);
auto fu_var1 = st.get("FU_VAR1");
auto fu_var2 = st.get("FU_VAR2");
auto fu_var3 = st.get("FU_VAR3");
auto fu_var4 = st.get("FU_VAR4");
auto fu_var5 = st.get("FU_VAR5");
BOOST_CHECK_EQUAL(fu_var1, 115);
BOOST_CHECK_EQUAL(fu_var2, 10);
BOOST_CHECK_EQUAL(fu_var3, 3000);
BOOST_CHECK_EQUAL(fu_var4, 0.5);
BOOST_CHECK_CLOSE(fu_var5, -0.00000041232 * 4 + 0.0010395 * 3 + 0.16504, 1e-5);
}