Skip to content

Commit 6345407

Browse files
Fix toString for multary (#12139)
When CAST was involved, parentheses were not printed correctly. Co-authored-by: kabdelhak <38032125+kabdelhak@users.noreply.github.com>
1 parent c4b57fe commit 6345407

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

OMCompiler/Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,16 +1907,16 @@ public
19071907
) + ")";
19081908
case END() then "end";
19091909

1910-
case MULTARY() guard(listEmpty(exp.inv_arguments)) then multaryString(exp.arguments, exp, exp.operator, false);
1910+
case MULTARY() guard(listEmpty(exp.inv_arguments)) then multaryFlatString(exp.arguments, exp, exp.operator, false);
19111911

19121912
case MULTARY() guard(listEmpty(exp.arguments) and Operator.isDashClassification(Operator.getMathClassification(exp.operator)))
1913-
then "-" + multaryString(exp.inv_arguments, exp, exp.operator);
1913+
then "-" + multaryFlatString(exp.inv_arguments, exp, exp.operator);
19141914

1915-
case MULTARY() guard(listEmpty(exp.arguments)) then "1/" + multaryString(exp.inv_arguments, exp, exp.operator);
1915+
case MULTARY() guard(listEmpty(exp.arguments)) then "1/" + multaryFlatString(exp.inv_arguments, exp, exp.operator);
19161916

1917-
case MULTARY() then multaryString(exp.arguments, exp, exp.operator) +
1917+
case MULTARY() then multaryFlatString(exp.arguments, exp, exp.operator) +
19181918
Operator.symbol(Operator.invert(exp.operator)) +
1919-
multaryString(exp.inv_arguments, exp, exp.operator);
1919+
multaryFlatString(exp.inv_arguments, exp, exp.operator);
19201920

19211921
case BINARY() then operandFlatString(exp.exp1, exp, true) +
19221922
Operator.symbol(exp.operator) +
@@ -2049,8 +2049,8 @@ public
20492049
if operand_prio > operator_prio then
20502050
parenthesize := true;
20512051
elseif operand_prio == operator_prio then
2052-
parenthesize := if lhs then isNonAssociativeExp(operand) else not
2053-
isAssociativeExp(operand);
2052+
parenthesize := if lhs then isNonAssociativeExp(operand)
2053+
else not isAssociativeExp(operand);
20542054
end if;
20552055
end if;
20562056

@@ -2063,15 +2063,28 @@ public
20632063
input list<Expression> arguments;
20642064
input Expression exp;
20652065
input Operator operator;
2066-
input Boolean useParanthesis = true;
2066+
input Boolean parenthesize = true;
20672067
output String str;
20682068
algorithm
20692069
str := stringDelimitList(list(operandString(e, exp, false) for e in arguments), Operator.symbol(operator));
2070-
if useParanthesis and listLength(arguments) > 1 then
2070+
if parenthesize and listLength(arguments) > 1 then
20712071
str := "(" + str + ")";
20722072
end if;
20732073
end multaryString;
20742074

2075+
function multaryFlatString
2076+
input list<Expression> arguments;
2077+
input Expression exp;
2078+
input Operator operator;
2079+
input Boolean parenthesize = true;
2080+
output String str;
2081+
algorithm
2082+
str := stringDelimitList(list(operandFlatString(e, exp, false) for e in arguments), Operator.symbol(operator));
2083+
if parenthesize and listLength(arguments) > 1 then
2084+
str := "(" + str + ")";
2085+
end if;
2086+
end multaryFlatString;
2087+
20752088
function priority
20762089
input Expression exp;
20772090
input Boolean lhs;
@@ -2088,6 +2101,9 @@ public
20882101
case RELATION() then 6;
20892102
case RANGE() then 10;
20902103
case IF() then 11;
2104+
case CAST() then priority(exp.exp, lhs);
2105+
case BOX() then priority(exp.exp, lhs);
2106+
case UNBOX() then priority(exp.exp, lhs);
20912107
else 0;
20922108
end match;
20932109
end priority;
@@ -6072,6 +6088,18 @@ public
60726088
then
60736089
json;
60746090

6091+
case MULTARY()
6092+
algorithm
6093+
json := JSON.emptyObject();
6094+
json := JSON.addPair("$kind", JSON.makeString("multary_op"), json);
6095+
json := JSON.addPair("args",
6096+
JSON.makeArray(list(toJSON(a) for a in exp.arguments)), json);
6097+
json := JSON.addPair("inv_args",
6098+
JSON.makeArray(list(toJSON(a) for a in exp.inv_arguments)), json);
6099+
json := JSON.addPair("op", JSON.makeString(Operator.symbol(exp.operator, spacing = "")), json);
6100+
then
6101+
json;
6102+
60756103
case IF()
60766104
algorithm
60776105
json := JSON.emptyObject();

testsuite/simulation/modelica/NBackend/functions/function_annotation_der.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ simulate(function_annotation_der); getErrorString();
8585
// output Real '$fDER_y';
8686
// Real 'y';
8787
// algorithm
88-
// '$fDER_y' := (k * (2.0 * x * $fDER_x) + $fDER_k * x ^ 2.0) + $fDER_m;
88+
// '$fDER_y' := ('k' * (2.0 * 'x' * '$fDER_x') + '$fDER_k' * 'x' ^ 2.0) + '$fDER_m';
8989
// 'y' := 'k' * 'x' ^ 2.0 + 'm';
9090
// annotation(Inline = false);
9191
// end '$fDER1.f'

testsuite/simulation/modelica/NBackend/functions/function_diff.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ simulate(function_diff); getErrorString();
6060
// Real 'b';
6161
// algorithm
6262
// if 'a' >= 0.0 then
63-
// '$fDER_b' := 2.0 * a * $fDER_a;
63+
// '$fDER_b' := 2.0 * 'a' * '$fDER_a';
6464
// 'b' := 'a' ^ 2.0;
6565
// elseif true then
6666
// '$fDER_b' := '$fDER0.f'(-'a', -'$fDER_a');

0 commit comments

Comments
 (0)