Skip to content

Commit

Permalink
Fix unary expression in FlatModelica::Expression (#7995)
Browse files Browse the repository at this point in the history
- Make unary expressions less greedy when parsing expression so that
  e.g. -x + y is parsed as -x + y and not -(x + y).
  • Loading branch information
perost committed Oct 12, 2021
1 parent 6e08b4d commit ab79a46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions OMEdit/OMEditLIB/FlatModelica/Expression.cpp
Expand Up @@ -1280,7 +1280,7 @@ namespace FlatModelica

if (tok.type == Token::SUB) {
tokenizer.popToken();
auto e = parseExp(tokenizer);
auto e = parsePrimary(tokenizer);

if (e.isNumber()) {
return e.isInteger() ? Expression(-e.intValue()) : Expression(-e.realValue());
Expand All @@ -1289,7 +1289,7 @@ namespace FlatModelica
}
} else if (tok.type == Token::NOT) {
tokenizer.popToken();
auto e = parseExp(tokenizer);
auto e = parsePrimary(tokenizer);

if (e.isBoolean()) {
return Expression(!e.boolValue());
Expand Down
12 changes: 12 additions & 0 deletions OMEdit/Testsuite/Expression/ExpressionTest.cpp
Expand Up @@ -150,6 +150,18 @@ void ExpressionTest::operators_data()
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("result");

QTest::newRow("unary1")
<< "-17"
<< "-17";

QTest::newRow("unary2")
<< "-3 + x"
<< "-2";

QTest::newRow("unary3")
<< "x + -5"
<< "-4";

QTest::newRow("logic1")
<< "x and y or z"
<< "true";
Expand Down

0 comments on commit ab79a46

Please sign in to comment.