From 2cc49fa9f319641fcef8566fe59048a223647660 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Tue, 8 May 2012 19:19:49 +0100 Subject: [PATCH] Work around weird Fortran unary operator handling. Fortran dislikes unary plus or minus on right right hand side of an arithmetic operator. --- src/ofc/codegen/FortranGenerator.scala | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ofc/codegen/FortranGenerator.scala b/src/ofc/codegen/FortranGenerator.scala index 240123e..86c5b07 100644 --- a/src/ofc/codegen/FortranGenerator.scala +++ b/src/ofc/codegen/FortranGenerator.scala @@ -234,8 +234,22 @@ class FortranGenerator { private def buildNumericComparison(c: NumericComparison[_]) : ExpHolder = buildBinaryOperation(getBinaryOpInfo(c.getOperation), buildExpression(c.getLeft), buildExpression(c.getRight)) - private def buildNumericOperator(o: NumericOperator[_]) : ExpHolder = - buildBinaryOperation(getBinaryOpInfo(o.getOperation), buildExpression(o.getLeft), buildExpression(o.getRight)) + private def buildNumericOperator(o: NumericOperator[_]) : ExpHolder = { + val left = buildExpression(o.getLeft) + var right = buildExpression(o.getRight) + + // Fortran has stupid rules about unary negation on the right hand side of an arithmetic operator. + def isUnaryNegate(expression: Expression[_]) = expression match { + case (l: FloatLiteral) if l.getValue < 0 => true + case (l: IntegerLiteral) if l.getValue < 0 => true + case _ => false + } + + if (isUnaryNegate(o.getRight)) + right = ExpHolder(maxPrec, "(%s)".format(right.exp)) + + buildBinaryOperation(getBinaryOpInfo(o.getOperation), left, right) + } private def processForLoop(stat: ForLoop) { val index = stat.getIndex -- 2.47.3