]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add min and max intrinsics to code generator.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Thu, 3 May 2012 16:31:51 +0000 (17:31 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Thu, 3 May 2012 16:31:51 +0000 (17:31 +0100)
src/ofc/codegen/FortranGenerator.scala
src/ofc/codegen/Intrinsic.scala [new file with mode: 0644]

index 9f0ecd4f091a0cefe23e2effe2bdb51580b04a99..059e668ba1071655ef497d718591efe5f4f135c6 100644 (file)
@@ -176,6 +176,7 @@ class FortranGenerator {
       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)
     }
   }
@@ -222,7 +223,12 @@ class FortranGenerator {
     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))
diff --git a/src/ofc/codegen/Intrinsic.scala b/src/ofc/codegen/Intrinsic.scala
new file mode 100644 (file)
index 0000000..a73c7ea
--- /dev/null
@@ -0,0 +1,24 @@
+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
+}