]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
More work on second-level representation.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 26 Sep 2012 18:44:07 +0000 (19:44 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 26 Sep 2012 18:44:07 +0000 (19:44 +0100)
OFC/Common.hs
OFC/SecondLevel.hs
OFC/TargetMapping.hs
OFC/TopLevel.hs
src/Main.hs

index 44f77c1ffce5e64a5c62cce7f060e89b45eb3f49..c10c03dfa6bb817ac3ec4f4d5063e61c4cf3b52a 100644 (file)
@@ -1,7 +1,13 @@
 module OFC.Common where
+import Text.PrettyPrint (Doc, renderStyle, Style(..), Mode(..))
 
 data IndexType = 
   FunctionIndex | 
   SpinIndex | 
   SpatialIndex 
   deriving (Show, Eq, Enum, Bounded)
+
+class PrettyPrintable a where
+  toDoc :: a -> Doc
+  prettyPrint :: a -> String
+  prettyPrint = renderStyle Style {mode = PageMode, lineLength=72, ribbonsPerLine=1.5 } . toDoc
index df63c654dd9a02d85b6b4f18afaf2fa77f22a102..ceef1b7d83b1a384b60afbc07126538b021a42ea 100644 (file)
@@ -1,12 +1,32 @@
 module OFC.SecondLevel where
+import OFC.Common
+import OFC.TargetMapping (MappingTable, FortranFunction)
+import OFC.TopLevel (OFL)
+import qualified OFC.TopLevel as TopLevel
 import Data.Complex
+import Text.PrettyPrint
+import Data.Map (Map)
+import qualified Data.Map as Map
 
-data Type = 
+type SymbolTable = Map String SymbolType
+
+data Quantification = 
+  Forall |
+  Bound
+  deriving Show
+
+data SymbolType = 
+  ValueTag ValueType [IndexType] | 
+  IndexTag IndexType Quantification
+  deriving Show
+
+data ValueType = 
   RealType | 
   ComplexType |
   IntegerType |
   Psinc Integer | 
   PsincReciprocal Integer
+  deriving Show
 
 data Expression = 
   IndexedIdentifier String [String] |
@@ -15,10 +35,16 @@ data Expression =
   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 = 
   ConstReal Double |
@@ -29,3 +55,33 @@ data OperatorExpr =
   Multiply OperatorExpr OperatorExpr |
   Sum OperatorExpr String |
   Power OperatorExpr OperatorExpr
+  deriving Show
+
+data OFL2 = OFL2 {
+  symbols :: SymbolTable,
+  assignments :: [Assignment],
+  targetMappings :: MappingTable,
+  outputFunction :: FortranFunction
+} deriving Show
+
+instance PrettyPrintable OFL2 where
+  toDoc ofl2 = text $ show ofl2
+
+emptyOFL2 :: OFL2
+emptyOFL2 = OFL2 { 
+  symbols = Map.empty, 
+  assignments = [], 
+  targetMappings = Map.empty, 
+  outputFunction = error "Output function not defined"
+}
+
+buildSecondLevel :: OFL -> OFL2
+buildSecondLevel ofl = 
+  emptyOFL2 {
+    symbols = buildSymbolTable ofl,
+    targetMappings = TopLevel.getTargetMappings ofl,
+    outputFunction = TopLevel.getOutputFunction ofl
+  }
+
+buildSymbolTable :: OFL -> SymbolTable
+buildSymbolTable _ = error "Building second-level symbol table not yet implemented!"
index 33a115ea773e9e8e43ca239d93686fe56526dfc9..2cbe4dccb7f62b2d168d0143fb51b6db0369bcca 100644 (file)
@@ -1,4 +1,7 @@
 module OFC.TargetMapping where
+import Data.Map (Map)
+
+type MappingTable = Map String TargetType
 
 data TargetType = 
   FortranParameter [FortranParameterProperty] |
index 8045fedbcc7d058dc8b162b15cfc8498be504860..ec2a3970eb18e1d4f9b1b6efbfcee4a170cd7ba7 100644 (file)
@@ -32,7 +32,9 @@ data Expression =
   SpatialDerivative Expression String Integer
   deriving Show
 
-data Assignment = Assign Expression Expression deriving Show
+data Assignment = 
+  Assign Expression Expression 
+  deriving Show
 
 class OFLKeyword a where
   toOFLString :: a -> String
@@ -52,7 +54,6 @@ data SymbolType =
   deriving Show
 
 type SymbolTable = Map String SymbolType
-type MappingTable = Map String TargetType
 
 data OFL = OFL {
   symbols :: SymbolTable,
@@ -62,21 +63,18 @@ data OFL = OFL {
   outputFunction :: FortranFunction
 } deriving Show
 
-toDoc :: OFL -> Doc
-toDoc ofl = vcat [symbolDoc, assignmentsDoc, targetMappingsDoc, outputFunctionDoc]
-  where
-  symbolDoc = text "Symbol table:"
-    $$ nest 1 (vcat [text $ show x | x <- Map.assocs $ symbols ofl])
-  assignmentsDoc = text "Assignments: "
-    $$ nest 1 (vcat [text $ show x | x <- assignments ofl])
-  targetMappingsDoc = text "Target properties: "
-    $$ nest 1 (vcat [text $ show x | x <- Map.assocs $ targetMappings ofl])
-  outputFunctionDoc = text "OutputFunction:"
-    $$ nest 1 (text $ show $ outputFunction ofl)
-
-prettyPrint :: OFL -> String
-prettyPrint ofl = renderStyle Style {mode = PageMode, lineLength=10, ribbonsPerLine=1.5 } $ toDoc ofl
-
+instance PrettyPrintable OFL where
+  toDoc ofl = vcat [symbolDoc, assignmentsDoc, targetMappingsDoc, outputFunctionDoc]
+    where
+    symbolDoc = text "Symbol table:"
+      $$ nest 1 (vcat [text $ show x | x <- Map.assocs $ symbols ofl])
+    assignmentsDoc = text "Assignments: "
+      $$ nest 1 (vcat [text $ show x | x <- assignments ofl])
+    targetMappingsDoc = text "Target properties: "
+      $$ nest 1 (vcat [text $ show x | x <- Map.assocs $ targetMappings ofl])
+    outputFunctionDoc = text "OutputFunction:"
+      $$ nest 1 (text $ show $ outputFunction ofl)
+  
 -- Symbol table manipulators
 addValueDeclaration :: OFL -> String -> BaseType -> [IndexType] -> OFL
 addValueDeclaration ofl name baseType indices = ofl { symbols = symbols' }
@@ -90,6 +88,12 @@ addTargetMapping ofl name properties = result where
     then error $ "error: target binding " ++ show name ++ " conflicts with internally generated identifer."
     else ofl'
 
+getTargetMappings :: OFL -> MappingTable
+getTargetMappings = targetMappings
+
+getOutputFunction :: OFL -> FortranFunction
+getOutputFunction = outputFunction
+
 addIndexDeclaration :: OFL -> String -> IndexType -> OFL
 addIndexDeclaration ofl name indexType = ofl { symbols = symbols' }
   where symbols' = Map.insertWithKey errorOnDuplicate name (IndexTag indexType) (symbols ofl)
index 31fdc9092f13bcc03c2c7c973ffb116844dab960..1b7afd1d2bc0cf40764a95d5803c6b1623ea78ae 100644 (file)
@@ -1,7 +1,9 @@
 module Main (main) where
 import System.Environment (getArgs)
+import OFC.Common
 import OFC.Parser (runOFLParser)
-import OFC.TopLevel (prettyPrint)
+import OFC.TopLevel (OFL)
+import OFC.SecondLevel (buildSecondLevel)
 
 main :: IO()
 main = do 
@@ -18,4 +20,13 @@ processFile filename = do
   let result = runOFLParser contents in
     case result of
       Left err -> putStrLn $ show err
-      Right ofl -> putStrLn $ prettyPrint ofl
+      Right ofl -> printRefinements ofl
+
+printRefinements :: OFL -> IO()
+printRefinements ofl = do
+  let ofl' = buildSecondLevel ofl in
+    do 
+      putStrLn "Top-level representation:"
+      putStrLn $ prettyPrint ofl
+      putStrLn "\nSecond-level representation:"
+      putStrLn $ prettyPrint ofl'