case (c: NumericComparison[_]) => buildNumericComparison(c)
case (c: NumericOperator[_]) => buildNumericOperator(c)
case (c: Conversion[_,_]) => buildConversion(c)
+ case (i: Intrinsic[_]) => buildIntrinsic(i)
case x => throw new UnimplementedException("Unknown expression type in FORTRAN generator: " + x.toString)
}
}
case (_: ComplexType) => ExpHolder(maxPrec, "cmplx(%s)".format(buildExpression(c.getExpression)))
case _ => throw new UnimplementedException("Fortran generator cannot handle conversion.")
}
-
+
+ private def buildIntrinsic(i: Intrinsic[_]) : ExpHolder = i match {
+ case (m: Min[_]) => ExpHolder(maxPrec, "min(%s, %s)".format(buildExpression(m.getLeft), buildExpression(m.getRight)))
+ case (m: Max[_]) => ExpHolder(maxPrec, "max(%s, %s)".format(buildExpression(m.getLeft), buildExpression(m.getRight)))
+ case _ => throw new UnimplementedException("Unknown intrinsic in Fortran generator: "+i)
+ }
private def buildNumericComparison(c: NumericComparison[_]) : ExpHolder =
buildBinaryOperation(getBinaryOpInfo(c.getOperation), buildExpression(c.getLeft), buildExpression(c.getRight))
--- /dev/null
+package ofc.codegen
+import ofc.LogicError
+
+abstract class Intrinsic[T <: Type] extends Expression[T]
+
+class Min[T <: Type](a: Expression[T], b: Expression[T])(implicit isNumeric: HasProperty[T, Numeric]) extends Intrinsic[T] {
+ if (a.getType != b.getType)
+ throw new LogicError("Parameters to min must have matching types.")
+
+ def getType = a.getType
+ def foreach[U](f: Expression[_] => U) = List(a, b).foreach(f)
+ def getLeft = a
+ def getRight = b
+}
+
+class Max[T <: Type](a: Expression[T], b: Expression[T])(implicit isNumeric: HasProperty[T, Numeric]) extends Intrinsic[T] {
+ if (a.getType != b.getType)
+ throw new LogicError("Parameters to max must have matching types.")
+
+ def getType = a.getType
+ def foreach[U](f: Expression[_] => U) = List(a, b).foreach(f)
+ def getLeft = a
+ def getRight = b
+}