]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add accidentally missed files.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 21 Dec 2011 23:14:50 +0000 (23:14 +0000)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 21 Dec 2011 23:14:50 +0000 (23:14 +0000)
src/ofc/generators/Generator.scala [new file with mode: 0644]
src/ofc/generators/Onetep.scala [new file with mode: 0644]

diff --git a/src/ofc/generators/Generator.scala b/src/ofc/generators/Generator.scala
new file mode 100644 (file)
index 0000000..5f61405
--- /dev/null
@@ -0,0 +1,6 @@
+package ofc.generators
+import ofc.parser.Statement
+
+trait Generator {
+  def acceptInput(program : List[Statement]) : Unit
+}
diff --git a/src/ofc/generators/Onetep.scala b/src/ofc/generators/Onetep.scala
new file mode 100644 (file)
index 0000000..d374434
--- /dev/null
@@ -0,0 +1,71 @@
+package ofc.generators
+
+import ofc.parser
+import ofc.InvalidInputException
+import scala.collection.mutable.HashMap
+import scala.reflect.Manifest
+import scala.reflect.Manifest.singleType
+
+trait Matrix
+trait FunctionSet
+trait Index
+
+class Dictionary {
+  var matrices = new HashMap[parser.Identifier, Matrix]
+  var functionSets = new HashMap[parser.Identifier, FunctionSet]
+  var indices = new HashMap[parser.Identifier, Index]
+}
+
+class Onetep extends Generator {
+  def acceptInput(program : List[parser.Statement]) = {
+    println(program.mkString("\n") + "\n")
+    buildDictionary(program)
+  }
+
+  def filterStatements[T <: parser.Statement](statements : List[parser.Statement])(implicit m: Manifest[T]) =
+    statements.foldLeft(Nil : List[T])((list, item) => item match {
+      case s if (singleType(s) <:< m) =>  s.asInstanceOf[T] :: list
+      case _ => list
+    })
+
+  def getDeclarations(statements : List[parser.Statement]) : Map[parser.Identifier, parser.OFLType] = {
+    def getMappings(dl : parser.DeclarationList) =
+      for (name <- dl.names) yield
+        (name, dl.oflType)
+
+    filterStatements[parser.DeclarationList](statements).flatMap(getMappings(_)).toMap
+  }
+
+  def buildMatrix(id: parser.Identifier, call : Option[parser.FunctionCall]) = {
+    println(id + " " + call)
+  }
+
+  def buildFunctionSet(id: parser.Identifier, call : Option[parser.FunctionCall]) = {
+    println(id + " " + call)
+  }
+
+  def buildIndex(id: parser.Identifier, call : Option[parser.FunctionCall]) = {
+    println(id + " " + call)
+  }
+
+  def buildDictionary(statements : List[parser.Statement]) {
+    val targetDeclarations = filterStatements[parser.TargetAssignment](statements)
+    val declarations = getDeclarations(statements)
+
+    for(d <- declarations) {
+
+      // Find corresponding target-specific declaration if it exists.
+      val targetDeclarationCall = targetDeclarations.filter(_.id == d._1) match {
+        case List(x) => Some(x.value)
+        case List(_,_,_*) => throw new InvalidInputException("Invalid multiple target declarations for symbol " + d._1 + ".")
+        case Nil => None
+      }
+
+      d match {
+        case (id, parser.Matrix()) => buildMatrix(id, targetDeclarationCall)
+        case (id, parser.FunctionSet()) => buildFunctionSet(id, targetDeclarationCall)
+        case (id, parser.Index()) => buildIndex(id, targetDeclarationCall)
+      }
+    }
+  }
+}