Skip to content

Commit

Permalink
Fix dumping of (x ^ y) ^ z.
Browse files Browse the repository at this point in the history
- Fix algorithm for determining when parentheses are required so that ^
  is correctly handled.
  • Loading branch information
perost committed May 3, 2020
1 parent 3e339e2 commit 97aec02
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 20 deletions.
32 changes: 27 additions & 5 deletions OMCompiler/Compiler/FrontEnd/Dump.mo
Expand Up @@ -2511,11 +2511,11 @@ public function shouldParenthesize
input Boolean inLhs;
output Boolean outShouldParenthesize;
algorithm
outShouldParenthesize := match(inOperand, inOperator, inLhs)
outShouldParenthesize := match inOperand
local
Integer diff;

case (Absyn.UNARY(), _, _) then true;
case Absyn.UNARY() then true;

else
equation
Expand All @@ -2533,9 +2533,10 @@ protected function shouldParenthesize2
input Boolean inLhs;
output Boolean outShouldParenthesize;
algorithm
outShouldParenthesize := match(inPrioDiff, inOperand, inLhs)
case (1, _, _) then true;
case (0, _, false) then not isAssociativeExp(inOperand);
outShouldParenthesize := match inPrioDiff
case 1 then true;
case 0 then if inLhs then isNonAssociativeExp(inOperand) else
not isAssociativeExp(inOperand);
else false;
end match;
end shouldParenthesize2;
Expand Down Expand Up @@ -2568,6 +2569,27 @@ algorithm
end match;
end isAssociativeOp;

protected function isNonAssociativeExp
input Absyn.Exp exp;
output Boolean isNonAssociative;
algorithm
isNonAssociative := match exp
case Absyn.BINARY() then isNonAssociativeOp(exp.op);
else false;
end match;
end isNonAssociativeExp;

protected function isNonAssociativeOp
input Absyn.Operator operator;
output Boolean isNonAssociative;
algorithm
isNonAssociative := match operator
case Absyn.POW() then true;
case Absyn.POW_EW() then true;
else false;
end match;
end isNonAssociativeOp;

public function expPriority
"Returns an integer priority given an expression, which is used by
printOperatorStr to add parentheses around operands when dumping expressions.
Expand Down
38 changes: 31 additions & 7 deletions OMCompiler/Compiler/FrontEnd/Expression.mo
Expand Up @@ -10646,11 +10646,11 @@ public function shouldParenthesize
input Boolean inLhs;
output Boolean outShouldParenthesize;
algorithm
outShouldParenthesize := match(inOperand, inOperator, inLhs)
outShouldParenthesize := match inOperand
local
Integer diff;

case (DAE.UNARY(), _, _) then true;
case DAE.UNARY() then true;

else
equation
Expand All @@ -10668,9 +10668,10 @@ protected function shouldParenthesize2
input Boolean inLhs;
output Boolean outShouldParenthesize;
algorithm
outShouldParenthesize := match(inPrioDiff, inOperand, inLhs)
case (1, _, _) then true;
case (0, _, false) then not isAssociativeExp(inOperand);
outShouldParenthesize := match inPrioDiff
case 1 then true;
case 0 then if inLhs then isNonAssociativeExp(inOperand) else
not isAssociativeExp(inOperand);
else false;
end match;
end shouldParenthesize2;
Expand All @@ -10687,7 +10688,6 @@ algorithm
case DAE.BINARY(operator = op) then isAssociativeOp(op);
case DAE.LBINARY() then true;
else false;

end match;
end isAssociativeExp;

Expand All @@ -10696,7 +10696,7 @@ protected function isAssociativeOp
input DAE.Operator inOperator;
output Boolean outIsAssociative;
algorithm
outIsAssociative := match(inOperator)
outIsAssociative := match inOperator
case DAE.ADD() then true;
case DAE.MUL() then true;
case DAE.ADD_ARR() then true;
Expand All @@ -10706,6 +10706,30 @@ algorithm
end match;
end isAssociativeOp;

protected function isNonAssociativeExp
input DAE.Exp exp;
output Boolean isNonAssociative;
algorithm
isNonAssociative := match exp
case DAE.BINARY() then isNonAssociativeOp(exp.operator);
else false;
end match;
end isNonAssociativeExp;

protected function isNonAssociativeOp
input DAE.Operator inOperator;
output Boolean isNonAssociative;
algorithm
isNonAssociative := match inOperator
case DAE.POW() then true;
case DAE.POW_ARRAY_SCALAR() then true;
case DAE.POW_SCALAR_ARRAY() then true;
case DAE.POW_ARR() then true;
case DAE.POW_ARR2() then true;
else false;
end match;
end isNonAssociativeOp;

public function priority
"Returns an integer priority given an expression, which is used by
ExpressionDumpTpl to add parentheses when dumping expressions. The inLhs
Expand Down
41 changes: 33 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -1754,20 +1754,27 @@ public
output String str;
protected
Integer operand_prio, operator_prio;
Boolean parenthesize = false;
algorithm
str := toString(operand);
operand_prio := priority(operand, lhs);

if operand_prio == 4 then
str := "(" + str + ")";
parenthesize := true;
else
operator_prio := priority(operator, lhs);

if operand_prio > operator_prio or
not lhs and operand_prio == operator_prio and not isAssociativeExp(operand) then
str := "(" + str + ")";
if operand_prio > operator_prio then
parenthesize := true;
elseif operand_prio == operator_prio then
parenthesize := if lhs then isNonAssociativeExp(operand) else not
isAssociativeExp(operand);
end if;
end if;

if parenthesize then
str := "(" + str + ")";
end if;
end operandString;

function operandFlatString
Expand All @@ -1778,20 +1785,27 @@ public
output String str;
protected
Integer operand_prio, operator_prio;
Boolean parenthesize = false;
algorithm
str := toFlatString(operand);
operand_prio := priority(operand, lhs);

if operand_prio == 4 then
str := "(" + str + ")";
parenthesize := true;
else
operator_prio := priority(operator, lhs);

if operand_prio > operator_prio or
not lhs and operand_prio == operator_prio and not isAssociativeExp(operand) then
str := "(" + str + ")";
if operand_prio > operator_prio then
parenthesize := true;
elseif operand_prio == operator_prio then
parenthesize := if lhs then isNonAssociativeExp(operand) else not
isAssociativeExp(operand);
end if;
end if;

if parenthesize then
str := "(" + str + ")";
end if;
end operandFlatString;

function priority
Expand Down Expand Up @@ -1824,6 +1838,17 @@ public
end match;
end isAssociativeExp;

function isNonAssociativeExp
input Expression exp;
output Boolean isAssociative;
algorithm
isAssociative := match exp
case BINARY() then Operator.isNonAssociative(exp.operator);
case LBINARY() then true;
else false;
end match;
end isNonAssociativeExp;

function toDAEOpt
input Option<Expression> exp;
output Option<DAE.Exp> dexp;
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFOperator.mo
Expand Up @@ -306,6 +306,20 @@ public
end match;
end isAssociative;

function isNonAssociative
input Operator op;
output Boolean isNonAssociative;
algorithm
isNonAssociative := match op.op
case Op.POW then true;
case Op.POW_EW then true;
case Op.POW_SCALAR_ARRAY then true;
case Op.POW_ARRAY_SCALAR then true;
case Op.POW_MATRIX then true;
else false;
end match;
end isNonAssociative;

function makeAdd
input Type ty;
output Operator op = OPERATOR(ty, Op.ADD);
Expand Down

0 comments on commit 97aec02

Please sign in to comment.