From: Francis Russell Date: Tue, 3 Jan 2012 19:18:56 +0000 (+0000) Subject: Pattern match ONETEP-specific initialisers. X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=41f7be195e2d2e2fd0bf0465c21063176155ad58;p=francis%2Fofc.git Pattern match ONETEP-specific initialisers. --- diff --git a/src/ofc/generators/Onetep.scala b/src/ofc/generators/Onetep.scala index d374434..5ffe37b 100644 --- a/src/ofc/generators/Onetep.scala +++ b/src/ofc/generators/Onetep.scala @@ -36,16 +36,35 @@ class Onetep extends Generator { filterStatements[parser.DeclarationList](statements).flatMap(getMappings(_)).toMap } - def buildMatrix(id: parser.Identifier, call : Option[parser.FunctionCall]) = { - println(id + " " + call) + def buildMatrix(id: parser.Identifier, call : Option[parser.FunctionCall]) { + import parser._ + + call match { + case Some(FunctionCall(matType, params)) => (matType, params) match { + case (Identifier("SPAM3"), ParameterList(StringParameter(name))) => println(name) + case _ => throw new InvalidInputException("Unknown usage of type: "+matType.name) + } + case _ => throw new InvalidInputException("Undefined concrete type for matrix: "+id.name) + } } - def buildFunctionSet(id: parser.Identifier, call : Option[parser.FunctionCall]) = { - println(id + " " + call) + def buildFunctionSet(id: parser.Identifier, call : Option[parser.FunctionCall]) { + import parser._ + + call match { + case Some(FunctionCall(fSetType, params)) => (fSetType, params) match { + case (Identifier("PPDFunctionSet"), ParameterList(StringParameter(basis), StringParameter(data))) => println(basis) + case _ => throw new InvalidInputException("Unknown usage of type: "+fSetType.name) + } + case _ => throw new InvalidInputException("Undefined concrete type for function set: "+id.name) + } } - def buildIndex(id: parser.Identifier, call : Option[parser.FunctionCall]) = { - println(id + " " + call) + def buildIndex(id: parser.Identifier, call : Option[parser.FunctionCall]) { + call match { + case Some(_) => throw new InvalidInputException("Index "+id.name+" cannot have concrete type.") + case None => () + } } def buildDictionary(statements : List[parser.Statement]) { diff --git a/src/ofc/parser/Parser.scala b/src/ofc/parser/Parser.scala index b22ea74..2a7b2bd 100644 --- a/src/ofc/parser/Parser.scala +++ b/src/ofc/parser/Parser.scala @@ -51,12 +51,12 @@ class Parser extends JavaTokenParsers { def target : Parser[Target] = "target"~!identifier ^^ (x => new Target(x._2)) def specifics : Parser[TargetAssignment] = identifier~("is"~>functionCall) ^^ (x => new TargetAssignment(x._1, x._2)) def functionCall : Parser[FunctionCall] = identifier~("("~>repsep(functionParameter, ",")<~")") ^^ - (x => new FunctionCall(x._1, new ParameterList(x._2))) + (x => new FunctionCall(x._1, new ParameterList(x._2 : _*))) def functionParameter : Parser[Parameter] = stringParameter | numericParameter | parameterList def stringParameter : Parser[StringParameter] = stringLiteral ^^ (x => new StringParameter(x.slice(1, x.length-1))) def numericParameter : Parser[NumericParameter] = floatingPointNumber ^^ (x => new NumericParameter(x.toDouble)) - def parameterList : Parser[ParameterList] = "["~>repsep(functionParameter, ",")<~"]" ^^ (x => new ParameterList(x)) + def parameterList : Parser[ParameterList] = "["~>repsep(functionParameter, ",")<~"]" ^^ (x => new ParameterList(x : _*)) def parseProgram(in: Reader) : List[Statement] = parseAll(program, in) match { diff --git a/src/ofc/parser/Statement.scala b/src/ofc/parser/Statement.scala index 01a1fbb..d794c1f 100644 --- a/src/ofc/parser/Statement.scala +++ b/src/ofc/parser/Statement.scala @@ -56,7 +56,7 @@ case class FunctionCall(name: Identifier, params: ParameterList) { } sealed abstract class Parameter -case class ParameterList(params: List[Parameter]) extends Parameter { +case class ParameterList(params: Parameter*) extends Parameter { override def toString : String = params.mkString("[", ", ", "]") } case class StringParameter(s: String) extends Parameter {