]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add array allocation/deallocation statements.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 1 May 2012 05:27:54 +0000 (06:27 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 1 May 2012 05:27:54 +0000 (06:27 +0100)
src/ofc/codegen/AllocateStatement.scala [new file with mode: 0644]
src/ofc/codegen/DeallocateStatement.scala [new file with mode: 0644]
src/ofc/codegen/FortranGenerator.scala

diff --git a/src/ofc/codegen/AllocateStatement.scala b/src/ofc/codegen/AllocateStatement.scala
new file mode 100644 (file)
index 0000000..b99840a
--- /dev/null
@@ -0,0 +1,15 @@
+package ofc.codegen
+import ofc.LogicError
+
+class AllocateStatement(array: Expression[_], size: Seq[Expression[IntType]]) extends Statement {
+  val arrayType = array.getType match {
+    case (at: ArrayType[_]) => at
+    case _ => throw new LogicError("Can only allocate an array expression.")
+  }
+
+  if (size.length != arrayType.getRank)
+    throw new LogicError("Incorrect rank for array allocation.")
+
+  def getArray : Expression[_] = array
+  def getSize : Seq[Expression[IntType]] = size
+}
diff --git a/src/ofc/codegen/DeallocateStatement.scala b/src/ofc/codegen/DeallocateStatement.scala
new file mode 100644 (file)
index 0000000..41efd47
--- /dev/null
@@ -0,0 +1,11 @@
+package ofc.codegen
+import ofc.LogicError
+
+class DeallocateStatement(array: Expression[_]) extends Statement {
+  array.getType match {
+    case (_: ArrayType[_]) => ()
+    case _ => throw new LogicError("Can only deallocate an array expression.")
+  }
+
+  def getArray : Expression[_] = array
+}
index f424243515105668a59079c3e3eb4300684d1669..080171d36d5476b72bd5b5aa35c963ed1f6d354e 100644 (file)
@@ -143,6 +143,8 @@ class FortranGenerator {
       case (x : ForLoop) => processForLoop(x)
       case (a : Assignment) => processAssignment(a)
       case (i : IfStatement) => processIf(i)
+      case (a : AllocateStatement) => processAllocate(a)
+      case (d : DeallocateStatement) => processDeallocate(d)
       case x => throw new UnimplementedException("Unknown statement type in FORTRAN generator: " + x.toString)
     }
   }
@@ -254,6 +256,14 @@ class FortranGenerator {
     addLine("endif")
   }
 
+  private def processAllocate(allocate: AllocateStatement) {
+    addLine("allocate(%s(%s))".format(buildExpression(allocate.getArray), allocate.getSize.map(buildExpression(_)).mkString(",")))
+  }
+
+  private def processDeallocate(deallocate: DeallocateStatement) {
+    addLine("deallocate(%s)".format(buildExpression(deallocate.getArray)))
+  }
+
   private def addLine(line: String) {
     buffer += "  "*indentLevel + line
   }