never executed always true always false
1 {- |
2 Module : NITTA.Frontends.Common
3 Description : Common types and functions for all frontend implementations
4 Copyright : (c) Artur Gogiyan, 2022
5 License : BSD3
6 Maintainer : artur.gogiyan@gmail.com
7 Stability : experimental
8 -}
9 module NITTA.Frontends.Common (
10 FrontendResult (..),
11 TraceVar (..),
12 prettyLog,
13 getTraceVarFormat,
14 ) where
15
16 import Data.HashMap.Strict qualified as HM
17 import Data.Map.Strict qualified as M
18 import Data.Maybe
19 import Data.String.ToString
20 import Data.Text qualified as T
21 import NITTA.Intermediate.DataFlow
22 import Text.Printf
23
24 data FrontendResult v x = FrontendResult
25 { frDataFlow :: DataFlowGraph v x
26 , frTrace :: [TraceVar]
27 , frPrettyLog :: [HM.HashMap v x] -> [HM.HashMap String String]
28 }
29
30 data TraceVar = TraceVar {tvFmt :: Maybe T.Text, tvVar :: T.Text}
31 deriving (Show)
32
33 defaultFmt = T.pack "%.3f"
34
35 prettyLog traceVars hms = map prettyHM hms
36 where
37 prettyHM hm = HM.fromList $ mapMaybe prettyX $ HM.toList hm
38 prettyX (v0, x) = do
39 -- variables names end on #0, #1..., so we trim this suffix
40 let v = takeWhile (/= '#') $ toString v0
41 fmt <- v2fmt M.!? v
42 Just (toString (takeWhile (/= '^') v), printx (T.unpack (getTraceVarFormat fmt)) x)
43 v2fmt = M.fromList $ map (\(TraceVar fmt v) -> (toString v, fmt)) traceVars
44 printx p x
45 | 'f' `elem` p = printf p (fromRational (toRational x) :: Double)
46 | 's' `elem` p = printf p $ show x
47 | otherwise = printf p x
48
49 getTraceVarFormat Nothing = defaultFmt
50 getTraceVarFormat (Just fmt) = fmt