From 9c0b5132a8671f83a7e0bc695f87c5e8971ad7e5 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Wed, 21 Dec 2011 23:14:50 +0000 Subject: [PATCH] Add accidentally missed files. --- src/ofc/generators/Generator.scala | 6 +++ src/ofc/generators/Onetep.scala | 71 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/ofc/generators/Generator.scala create mode 100644 src/ofc/generators/Onetep.scala diff --git a/src/ofc/generators/Generator.scala b/src/ofc/generators/Generator.scala new file mode 100644 index 0000000..5f61405 --- /dev/null +++ b/src/ofc/generators/Generator.scala @@ -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 index 0000000..d374434 --- /dev/null +++ b/src/ofc/generators/Onetep.scala @@ -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) + } + } + } +} -- 2.47.3