]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Build preliminary second level representation.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 9 Oct 2012 18:27:19 +0000 (19:27 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 9 Oct 2012 18:27:19 +0000 (19:27 +0100)
OFC/SecondLevel.hs
OFC/TopLevel.hs

index 2b646ae7bd341b2ab875dc7bdc9a8c46e0506212..80d85eb9b8079eec5f3274c7f7e7237e4aea98cd 100644 (file)
@@ -4,8 +4,6 @@ module OFC.SecondLevel
   , ValueType(..)
   , Expression(..) 
   , Assignment(..)
-  , DiagonalOperator(..)
-  , OperatorExpr(..)
   , OFL2
   , buildSecondLevel
   ) where
@@ -37,39 +35,36 @@ data ValueType =
   RealType | 
   ComplexType |
   IntegerType |
-  Psinc Integer | 
-  PsincReciprocal Integer
+  Psinc Integer |
+  PsincReciprocal Integer |
+  RealFunction |
+  ComplexFunction
   deriving Show
 
-data Expression = 
+data Expression =
   IndexedIdentifier String [String] |
-  Upsample |
-  Downsample |
-  Apply DiagonalOperator Expression |
-  Product Expression Expression |
-  InnerProduct Expression Expression
-  deriving Show
-
-data Assignment =
- Assign Expression Expression 
- deriving Show 
-
-data DiagonalOperator = 
-  PositionOperator OperatorExpr |
-  MomentumOperator OperatorExpr
-  deriving Show
-
-data OperatorExpr = 
+  ToMomentum Expression |
+  ToPosition Expression |
+  Upsample Expression |
+  Downsample Expression |
+  Integrate Expression |
   ConstReal Double |
+  ConstInteger Integer |
   ConstComplex (Complex Double) |
-  ConstInteger Integer | 
-  Negate OperatorExpr |
   LatticeComponent String |
-  Multiply OperatorExpr OperatorExpr |
-  Sum OperatorExpr String |
-  Power OperatorExpr OperatorExpr
+  Negate Expression |
+  Sum Expression String |
+  Add Expression Expression |
+  Sub Expression Expression |
+  Mul Expression Expression |
+  Div Expression Expression |
+  Pow Expression Expression
   deriving Show
 
+data Assignment =
+ Assign Expression Expression
+ deriving Show 
+
 data OFL2 = OFL2 {
   symbols :: SymbolTable,
   assignments :: [Assignment],
@@ -94,8 +89,12 @@ emptyOFL2 = OFL2 {
 }
 
 buildSecondLevel :: OFL -> OFL2
-buildSecondLevel ofl = 
-  emptyOFL2 {
+buildSecondLevel ofl = completeOFL2
+  where
+  completeOFL2 = partialOFL2 {
+    assignments = map (buildAssignment ofl partialOFL2) (TopLevel.getAssignments ofl)
+  }
+  partialOFL2 = emptyOFL2 {
     symbols = buildSymbolTable ofl,
     targetMappings = TopLevel.getTargetMappings ofl,
     outputFunction = TopLevel.getOutputFunction ofl
@@ -135,3 +134,27 @@ getTargetValueType (TM.FortranParameter properties) = case TM.findSpace properti
     fortranToValueType TM.FunctionBasis = error "ONETEP function basis cannot be mapped to value types."
     fortranToValueType TM.Integer = IntegerType
     fortranToValueType (TM.Array t _) = fortranToValueType t
+
+buildAssignment :: OFL -> OFL2 -> TopLevel.Assignment -> Assignment
+buildAssignment ofl ofl2 (TopLevel.Assign lhs rhs) = 
+  Assign (buildExpression' lhs) (buildExpression' rhs) 
+  where
+  buildExpression' = buildExpression ofl ofl2
+
+buildExpression :: OFL -> OFL2 -> TopLevel.Expression -> Expression
+buildExpression _ _ (TopLevel.IndexedIdentifier name indices) = IndexedIdentifier name indices
+buildExpression _ _ (TopLevel.ConstReal r) = ConstReal r
+buildExpression _ _ (TopLevel.ConstInteger i) = ConstInteger i
+buildExpression ofl ofl2 (TopLevel.Negate e) = Negate $ buildExpression ofl ofl2 e
+buildExpression ofl ofl2 (TopLevel.Integrate e) = Integrate $ buildExpression ofl ofl2 e
+buildExpression ofl ofl2 (TopLevel.Sum e index) = Sum (buildExpression ofl ofl2 e) index
+buildExpression ofl ofl2 (TopLevel.Multiply e1 e2) = Mul (buildExpression ofl ofl2 e1) (buildExpression ofl ofl2 e2)
+buildExpression ofl ofl2 (TopLevel.Divide e1 e2) = Div (buildExpression ofl ofl2 e1) (buildExpression ofl ofl2 e2)
+buildExpression ofl ofl2 (TopLevel.Add e1 e2) = Add (buildExpression ofl ofl2 e1) (buildExpression ofl ofl2 e2)
+buildExpression ofl ofl2 (TopLevel.Sub e1 e2) = Sub (buildExpression ofl ofl2 e1) (buildExpression ofl ofl2 e2)
+buildExpression ofl ofl2 (TopLevel.Power e1 e2) = Pow (buildExpression ofl ofl2 e1) (buildExpression ofl ofl2 e2)
+buildExpression _ _ (TopLevel.PositionComponent i) = LatticeComponent i
+buildExpression ofl ofl2 (TopLevel.SpatialDerivative e index n) = Mul operator $ ToMomentum (buildExpression ofl ofl2 e)
+  where
+  operator = Pow derivative (ConstInteger n)
+  derivative = Mul (LatticeComponent index) (ConstComplex $ 0.0 :+ (-1.0))
index f9374d8d488f3dcf2a38ecb802dd7bcfa9601986..39f0968f9c4ee82e6ef7bac2bb6564c2c63c6467 100644 (file)
@@ -15,6 +15,7 @@ module OFC.TopLevel
   , getValueSymbols
   , getTargetMappings
   , getTargetType
+  , getAssignments
   , getOutputFunction
   , findUniqueName
   , setOutputFunction
@@ -140,6 +141,9 @@ getTargetType ofl name = case Map.lookup name (targetMappings ofl) of
 getOutputFunction :: OFL -> FortranFunction
 getOutputFunction = outputFunction
 
+getAssignments :: OFL -> [Assignment]
+getAssignments = assignments
+
 addIndexDeclaration :: OFL -> String -> IndexType -> OFL
 addIndexDeclaration ofl name indexType = ofl { symbols = symbols' }
   where symbols' = Map.insertWithKey errorOnDuplicate name (IndexTag indexType) (symbols ofl)