]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add pretty printing for second-level AST.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 10 Oct 2012 15:12:32 +0000 (16:12 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 10 Oct 2012 15:12:32 +0000 (16:12 +0100)
OFC/SecondLevel.hs
OFC/TopLevel.hs

index 80d85eb9b8079eec5f3274c7f7e7237e4aea98cd..c011993319f9c78626574f209db3bd35f4d074d0 100644 (file)
@@ -16,7 +16,7 @@ import qualified OFC.TopLevel as TopLevel
 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
@@ -61,10 +61,38 @@ data Expression =
   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],
@@ -78,7 +106,7 @@ instance PrettyPrintable OFL2 where
     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 { 
index 18ad360361ec5deebb50d331ba8cd3061729ec45..1239152145b300067d9298946a4bda159b4fa607 100644 (file)
@@ -35,7 +35,7 @@ module OFC.TopLevel
 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)
@@ -67,12 +67,12 @@ data Expression =
 
 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
@@ -83,8 +83,8 @@ instance PrettyPrintable Expression where
     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