import Data.Complex
import Text.PrettyPrint
import Data.Map (Map)
-import Data.List (foldl')
+import Data.List (foldl', intersperse)
import qualified Data.Map as Map
type SymbolTable = Map String SymbolType
Pow Expression Expression
deriving Show
+instance PrettyPrintable Expression where
+ toDoc expression = case expression of
+ IndexedIdentifier name indices -> text name <> case indices of
+ [] -> empty
+ nonNil -> brackets $ hcat $ punctuate comma (map text nonNil)
+ ToMomentum e -> functionToDoc "to_momentum" [toDoc e]
+ ToPosition e -> functionToDoc "to_position" [toDoc e]
+ Upsample e -> functionToDoc "upsample" [toDoc e]
+ Downsample e -> functionToDoc "downsample" [toDoc e]
+ Integrate e -> functionToDoc "integrate" [toDoc e]
+ ConstReal r -> double r
+ ConstInteger i -> integer i
+ ConstComplex c -> parens $ text ((show $ realPart c) ++ " + i*" ++ (show $ imagPart c) ++ ")")
+ LatticeComponent i -> text $ "lattice[" ++ i ++ "]"
+ Negate e -> hcat [text "(", toDoc e, text ")"]
+ Sum e i -> functionToDoc "sum" [toDoc e, text i]
+ Add a b -> binaryToDoc "+" a b
+ Sub a b -> binaryToDoc "-" a b
+ Mul a b -> binaryToDoc "*" a b
+ Div a b -> binaryToDoc "/" a b
+ Pow a b -> binaryToDoc "^" a b
+ where
+ binaryToDoc op lhs rhs = parens $ hcat [toDoc lhs, text op, toDoc rhs]
+ functionToDoc name params = text name <> (parens $ hcat (intersperse (text ", ") params))
+
data Assignment =
Assign Expression Expression
deriving Show
+instance PrettyPrintable Assignment where
+ toDoc (Assign lhs rhs) = hcat [toDoc lhs, text " = ", toDoc rhs]
+
data OFL2 = OFL2 {
symbols :: SymbolTable,
assignments :: [Assignment],
symbolDoc = text "Symbol table:"
$$ nest 1 (vcat [text $ show x | x <- Map.assocs $ symbols ofl2])
assignmentsDoc = text "Assignments: "
- $$ nest 1 (vcat [text $ show x | x <- assignments ofl2])
+ $$ nest 1 (vcat $ map toDoc $ assignments ofl2)
emptyOFL2 :: OFL2
emptyOFL2 = OFL2 {
import OFC.TargetMapping
import OFC.Common
import Text.PrettyPrint
-import Data.List (foldl', intercalate, intersperse)
+import Data.List (foldl', intersperse)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
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 ")"
+ IndexedIdentifier name indices -> text name <> case indices of
+ [] -> empty
+ nonNil -> brackets $ hcat $ punctuate comma (map text nonNil)
+ ConstReal r -> double r
+ ConstInteger i -> integer i
+ Negate e -> parens $ text "-" <> toDoc e
Integrate e -> functionToDoc "integrate" [toDoc e]
Sum e i -> functionToDoc "sum" [toDoc e, text i]
Multiply 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 ")"
+ binaryToDoc op lhs rhs = parens $ hcat [toDoc lhs, text op, toDoc rhs]
+ functionToDoc name params = text name <> (parens $ hcat (intersperse (text ", ") params))
data Assignment =
Assign Expression Expression