import OFC.TargetMapping
import OFC.Common
import Text.PrettyPrint
-import Data.List (foldl')
+import Data.List (foldl', intercalate, intersperse)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
SpatialDerivative Expression String Integer
deriving Show
+instance PrettyPrintable Expression where
+ toDoc expression = case expression of
+ IndexedIdentifier name indices -> text $ name ++ case indices of
+ [] -> ""
+ nonNil -> "[" ++ (intercalate ", " nonNil) ++ "]"
+ ConstReal r -> text $ show r
+ ConstInteger i -> text $ show i
+ Negate e -> text "(-" <> toDoc e <> text ")"
+ Integrate e -> functionToDoc "integrate" [toDoc e]
+ Sum e i -> functionToDoc "sum" [toDoc e, text i]
+ Multiply a b -> binaryToDoc "*" a b
+ Divide a b -> binaryToDoc "/" a b
+ Add a b -> binaryToDoc "+" a b
+ Sub a b -> binaryToDoc "-" a b
+ Power a b -> binaryToDoc "^" a b
+ PositionComponent index -> text $ "r[" ++ index ++ "]"
+ SpatialDerivative e index degree -> functionToDoc "diff" [toDoc e, text index, text $ show degree]
+ where
+ binaryToDoc op lhs rhs = hcat [text "(", toDoc lhs, text op, toDoc rhs, text ")"]
+ functionToDoc name params = text (name ++ "(") <> hcat (intersperse (text ", ") params) <> text ")"
+
data Assignment =
Assign Expression Expression
deriving Show
+instance PrettyPrintable Assignment where
+ toDoc (Assign lhs rhs) = hcat [toDoc lhs, text " = ", toDoc rhs]
+
class OFLKeyword a where
toOFLString :: a -> String
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])
+ $$ nest 1 (vcat $ map toDoc $ assignments ofl)
targetMappingsDoc = text "Target properties: "
$$ nest 1 (vcat [text $ show x | x <- Map.assocs $ targetMappings ofl])
outputFunctionDoc = text "OutputFunction:"