Skip to content

Commit 9a7ccb2

Browse files
authored
Merge pull request #5450 from tonhuisman/bugfix/Calculation-of-functions-discards-next-operator
[Bugfix] Calculation of functions discarded the next operator
2 parents 8a9d3b1 + 7214126 commit 9a7ccb2

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/src/Helpers/Rules_calculate.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ CalculateReturnCode RulesCalculate_t::RPNCalculate(char *token)
315315
ESPEASY_RULES_FLOAT_TYPE first = pop();
316316

317317
ret = push(apply_operator(token[0], first, second));
318-
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate operator %c: 1: %.2f 2: %.2f"), token[0], first, second));
318+
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate operator %c: 1: %.4f 2: %.4f"), token[0], first, second));
319319

320320
// FIXME TD-er: Regardless whether it is an error, all code paths return ret;
321321
// if (isError(ret)) { return ret; }
@@ -324,7 +324,7 @@ CalculateReturnCode RulesCalculate_t::RPNCalculate(char *token)
324324
ESPEASY_RULES_FLOAT_TYPE first = pop();
325325

326326
ret = push(apply_unary_operator(token[0], first));
327-
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate unary %d: 1: %.2f"), token[0], first));
327+
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate unary %d: 1: %.4f"), token[0], first));
328328

329329
// FIXME TD-er: Regardless whether it is an error, all code paths return ret;
330330
// if (isError(ret)) { return ret; }
@@ -337,14 +337,14 @@ CalculateReturnCode RulesCalculate_t::RPNCalculate(char *token)
337337
ESPEASY_RULES_FLOAT_TYPE first = pop();
338338

339339
ret = push(apply_quinary_operator(token[0], first, second, third, fourth, fifth));
340-
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate quinary %d: 1: %.2f 2: %.2f 3: %.2f 4: %.2f 5: %.2f"), token[0], first, second, third, fourth, fifth));
340+
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate quinary %d: 1: %.4f 2: %.4f 3: %.4f 4: %.4f 5: %.4f"), token[0], first, second, third, fourth, fifth));
341341

342342
} else {
343343
// Fetch next if there is any
344344
ESPEASY_RULES_FLOAT_TYPE value{};
345345
if (validDoubleFromString(token, value)) {
346346

347-
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate push value: %.2f token: %s"), value, token));
347+
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate push value: %.4f token: %s"), value, token));
348348
// } else {
349349
// addLog(LOG_LEVEL_INFO, strformat(F("RPNCalculate unknown token: %s"), token));
350350
}
@@ -540,15 +540,14 @@ CalculateReturnCode RulesCalculate_t::doCalculate(const char *input, ESPEASY_RUL
540540
*(TokenPos) = 0; // Mark end of token string
541541
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate popping stack sl: %u token: %s sc: %d"), sl, token, sc));
542542
if (sc == '(') {
543-
ESPEASY_RULES_FLOAT_TYPE first = pop(); // Get last value from stack
544-
error = push(first); // push back
545-
error = push(first); // Push as a result of ()
546-
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate pop&push 2x last value: %.2f sl: %u"), first, sl));
543+
// const auto first = pop(); // Get last value from stack
544+
// push(first); // push back
545+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate at ( last value: %.4f sl: %u"), first, sl));
547546
} else {
548547
error = RPNCalculate(token);
549-
// ESPEASY_RULES_FLOAT_TYPE first = pop(); // Get last value from stack
550-
// error = push(first); // push back
551-
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate last value on stack: %.2f sl: %u"), first, sl));
548+
// const auto first = pop(); // Get last value from stack
549+
// push(first); // push back
550+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate last value on stack: %.4f sl: %u"), first, sl));
552551
}
553552
TokenPos = token;
554553

@@ -567,8 +566,14 @@ CalculateReturnCode RulesCalculate_t::doCalculate(const char *input, ESPEASY_RUL
567566
pe = true;
568567
if (sl > 1) {
569568
sc = stack[sl - 2];
570-
if (!(is_operator(sc) || is_unary_operator(sc) || is_quinary_operator(sc))) {
571-
sc = '\0';
569+
if (is_operator(sc)) { // Not a function call
570+
// Don't touch
571+
} else if (is_unary_operator(sc) || is_quinary_operator(sc)) { // Function call, so process the function too
572+
*TokenPos = sc;
573+
++TokenPos;
574+
stack[sl - 2] = '\0'; // Don't process again on stack wind-down
575+
} else {
576+
sc = '\0'; // Reset
572577
}
573578
}
574579
}
@@ -606,6 +611,7 @@ CalculateReturnCode RulesCalculate_t::doCalculate(const char *input, ESPEASY_RUL
606611
++strpos;
607612
}
608613

614+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate final stack content 0x%s"), formatToHex_array(reinterpret_cast<const uint8_t*>(stack), sl + 1).c_str()));
609615
// When there are no more tokens to read:
610616
// While there are still operator tokens in the stack:
611617
while (sl > 0)
@@ -617,8 +623,12 @@ CalculateReturnCode RulesCalculate_t::doCalculate(const char *input, ESPEASY_RUL
617623
}
618624

619625
*(TokenPos) = 0; // Mark end of token string
626+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate closing up stack sl: %u token: %s sc: %d"), sl, token, sc));
620627
error = RPNCalculate(token);
621628
TokenPos = token;
629+
// const auto first = pop(); // Get last value from stack
630+
// push(first); // push back
631+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate closing, last value on stack: %.4f sl: %u"), first, sl));
622632

623633
if (isError(error)) { return error; }
624634
*TokenPos = sc;
@@ -629,6 +639,9 @@ CalculateReturnCode RulesCalculate_t::doCalculate(const char *input, ESPEASY_RUL
629639
*(TokenPos) = 0; // Mark end of token string
630640
error = RPNCalculate(token);
631641
TokenPos = token;
642+
// const auto first = pop(); // Get last value from stack
643+
// push(first); // push back
644+
// addLog(LOG_LEVEL_INFO, strformat(F("doCalculate final value on stack: %.4f sl: %u"), first, sl));
632645

633646
if (isError(error))
634647
{

0 commit comments

Comments
 (0)