From 2b8bd5497018bf801f08ac4ff52866bf3ef0ad25 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Wed, 10 Oct 2012 15:16:35 +0100 Subject: [PATCH] Add pretty printing for top-level AST. --- OFC/TopLevel.hs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/OFC/TopLevel.hs b/OFC/TopLevel.hs index 39f0968..18ad360 100644 --- a/OFC/TopLevel.hs +++ b/OFC/TopLevel.hs @@ -35,7 +35,7 @@ module OFC.TopLevel 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) @@ -65,10 +65,34 @@ data Expression = 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 @@ -102,7 +126,7 @@ instance PrettyPrintable OFL 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]) + $$ 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:" -- 2.47.3