From: Francis Russell Date: Wed, 18 Jan 2012 19:08:06 +0000 (+0000) Subject: Initial work on generating ONETEP-specific tree structure. X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=5af3fcaa22495fc231ad64047f39ab499da55d92;p=francis%2Fofc.git Initial work on generating ONETEP-specific tree structure. --- diff --git a/src/ofc/generators/Onetep.scala b/src/ofc/generators/Onetep.scala index 90adbad..0e649ee 100644 --- a/src/ofc/generators/Onetep.scala +++ b/src/ofc/generators/Onetep.scala @@ -1,25 +1,11 @@ package ofc.generators import ofc.parser +import ofc.generators.onetep._ import ofc.InvalidInputException -import scala.collection.mutable.HashMap import scala.reflect.Manifest import scala.reflect.Manifest.singleType -trait Matrix -trait FunctionSet - -class SPAM3(name : String) extends Matrix -class PPDFunctionSet(basis : String, data : String) extends FunctionSet -class NamedIndex(name : String) - -class Dictionary { - var matrices = new HashMap[parser.Identifier, Matrix] - var functionSets = new HashMap[parser.Identifier, FunctionSet] - - var indices = new HashMap[parser.Identifier, NamedIndex] -} - class Onetep extends Generator { var dictionary = new Dictionary @@ -27,7 +13,7 @@ class Onetep extends Generator { def acceptInput(program : List[parser.Statement]) = { println(program.mkString("\n") + "\n") buildDictionary(program) - buildDefinition(program) + buildDefinitions(program) } def filterStatements[T <: parser.Statement](statements : List[parser.Statement])(implicit m: Manifest[T]) = @@ -70,10 +56,10 @@ class Onetep extends Generator { } } - def buildNamedIndex(id: parser.Identifier, call : Option[parser.FunctionCall]) { + def buildBindingIndex(id: parser.Identifier, call : Option[parser.FunctionCall]) { call match { case Some(_) => throw new InvalidInputException("Index "+id.name+" cannot have concrete type.") - case None => dictionary.indices += (id -> new NamedIndex(id.name)) + case None => dictionary.indices += (id -> new BindingIndex(id.name)) } } @@ -82,7 +68,6 @@ class Onetep extends Generator { 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) @@ -93,11 +78,22 @@ class Onetep extends Generator { d match { case (id, parser.Matrix()) => buildMatrix(id, targetDeclarationCall) case (id, parser.FunctionSet()) => buildFunctionSet(id, targetDeclarationCall) - case (id, parser.Index()) => buildNamedIndex(id, targetDeclarationCall) + case (id, parser.Index()) => buildBindingIndex(id, targetDeclarationCall) } } } - def buildDefinition(statements : List[parser.Statement]) { + def buildDefinition(definition : parser.Definition) { + println(definition) + val builder = new TreeBuilder(dictionary) + builder(definition.term, definition.expr) + } + + def buildDefinitions(statements : List[parser.Statement]) { + val definitions = filterStatements[parser.Definition](statements) + if (definitions.size != 1) + throw new InvalidInputException("Input file should only contain a single definition.") + else + buildDefinition(definitions.head) } } diff --git a/src/ofc/generators/onetep/Tree.scala b/src/ofc/generators/onetep/Tree.scala new file mode 100644 index 0000000..849bfc6 --- /dev/null +++ b/src/ofc/generators/onetep/Tree.scala @@ -0,0 +1,77 @@ +package ofc.generators.onetep + +import scala.collection.mutable.{HashMap,Set} +import ofc.parser +import ofc.parser.Identifier +import ofc.InvalidInputException + +trait Index +trait SpatialIndex +trait DiscreteIndex + +trait DataSpace +{ + def getIndices() : Set[Index] +} + +trait Matrix extends DataSpace +trait FunctionSet extends DataSpace + +class SPAM3(name : String) extends Matrix { + def getIndices() = Set[Index]() +} + +class PPDFunctionSet(basis : String, data : String) extends FunctionSet { + def getIndices() = Set[Index]() +} + +class Restriction +class Reciprocal +class Pointwise +class Summation + +class BindingIndex(name : String) + +class Dictionary { + var matrices = new HashMap[Identifier, Matrix] + var functionSets = new HashMap[Identifier, FunctionSet] + var indices = new HashMap[Identifier, BindingIndex] + + def getData(id: Identifier) = + matrices.get(id) match { + case Some(mat) => mat + case None => functionSets.get(id) match { + case Some(functionSet) => functionSet + case None => throw new InvalidInputException("Unknown identifier "+id.name) + } + } + + def getIndex(id: Identifier) = + indices.get(id) match { + case Some(index) => index + case None => throw new InvalidInputException("Unknown index "+id.name) + } +} + +class Definition(lhs: DataSpace, rhs: DataSpace) + +class IndexBindings { + var bindings = new HashMap[BindingIndex, Set[Index]] + + def add(binding: BindingIndex, index: Index) = bindings(binding) += index +} + +class TreeBuilder(dictionary : Dictionary) { + + val indexBindings = new IndexBindings + + def apply(lhs: parser.IndexedTerm, rhs: parser.Expression) { + buildIndexedTerm(lhs) + } + + def buildIndexedTerm(term: parser.IndexedTerm) { + val dataSpace = dictionary.getData(term.id) + val indices = for(bindingID <- term.indices) yield dictionary.getIndex(bindingID) + print(indices) + } +}