summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-28 20:49:08 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-28 20:49:08 +0200
commitdfb3cf0e71686ab12977b172f0b992414878e63d (patch)
treee5bd2eb2003f4905a863e631c0fa7121850a7622
parent4f85f455271ee6035924accebf34352229644a31 (diff)
refactoring
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs82
1 files changed, 39 insertions, 43 deletions
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 455a6e7b..5265965c 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -103,17 +103,22 @@ instance Monoid Doc where
103 103
104pattern DColor c a = DDoc (DOColor c a) 104pattern DColor c a = DDoc (DOColor c a)
105 105
106strip :: Doc -> Doc
106strip = \case 107strip = \case
107 DColor _ x -> strip x 108 DColor _ x -> strip x
108 DUp _ x -> strip x 109 DUp _ x -> strip x
109 DFreshName _ x -> strip x 110 DFreshName _ x -> strip x
110 x -> x 111 x -> x
111 112
113simple :: Doc -> Bool
112simple x = case strip x of 114simple x = case strip x of
113 DAtom{} -> True 115 DAtom{} -> True
114 DVar{} -> True 116 DVar{} -> True
115 _ -> False 117 _ -> False
116 118
119instance Show Doc where
120 show = show . renderDoc
121
117renderDoc :: Doc -> P.Doc 122renderDoc :: Doc -> P.Doc
118renderDoc 123renderDoc
119 = render 124 = render
@@ -122,7 +127,7 @@ renderDoc
122 . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) 127 . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z'])
123 . showVars 128 . showVars
124 where 129 where
125 showVars x = case x of 130 showVars = \case
126 DAtom s -> DAtom <$> showVarA s 131 DAtom s -> DAtom <$> showVarA s
127 DDoc d -> DDoc <$> traverse showVars d 132 DDoc d -> DDoc <$> traverse showVars d
128 DOp s pr x y -> DOp s pr <$> showVars x <*> showVars y 133 DOp s pr x y -> DOp s pr <$> showVars x <*> showVars y
@@ -137,20 +142,20 @@ renderDoc
137 addPar :: Int -> Doc -> Doc 142 addPar :: Int -> Doc -> Doc
138 addPar pr x = case x of 143 addPar pr x = case x of
139 DAtom x -> DAtom $ addParA x 144 DAtom x -> DAtom $ addParA x
140 DOp s pr' x y -> paren $ DOp s pr' (addPar (leftPrecedence pr') x) (addPar (rightPrecedence pr') y) 145 DOp s pr' x y -> (if protect then DParen else id)
146 $ DOp s pr' (addPar (leftPrecedence pr') x) (addPar (rightPrecedence pr') y)
141 DColor c x -> DColor c $ addPar pr x 147 DColor c x -> DColor c $ addPar pr x
142 DDoc d -> DDoc $ addPar (-10) <$> d 148 DDoc d -> DDoc $ addPar (-10) <$> d
143 where 149 where
144 addParA (SimpleAtom s) = SimpleAtom s 150 addParA (SimpleAtom s) = SimpleAtom s
145 addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar i d) $ addParA a 151 addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar i d) $ addParA a
146 152
147 paren = if protect then DParen else id 153 protect = case x of
148 where 154 DOp _ f _ _ -> precedence f < pr
149 protect = case x of 155 _ -> False
150 DOp _ f _ _ -> precedence f < pr
151 _ -> False
152 156
153 render x = case x of 157 render :: Doc -> P.Doc
158 render = \case
154 DDoc d -> interpretDocOp $ render <$> d 159 DDoc d -> interpretDocOp $ render <$> d
155 DAtom x -> renderA x 160 DAtom x -> renderA x
156 DOp s _ x y -> case s of 161 DOp s _ x y -> case s of
@@ -165,31 +170,26 @@ renderDoc
165 x <++> "," = x <> P.text "," 170 x <++> "," = x <> P.text ","
166 x <++> s = x P.<+> P.text s 171 x <++> s = x P.<+> P.text s
167 172
168instance Show Doc where
169 show = show . renderDoc
170
171-------------------------------------------------------------------------- combinators 173-------------------------------------------------------------------------- combinators
172 174
175a <+> b = DDoc $ DOHSep a b
176a </> b = DDoc $ DOSoftSep a b
177a <$$> b = DDoc $ DOVCat a b
178nest n = DDoc . DONest n
179tupled = DDoc . DOTupled
180
181inGreen' = DColor Green
182inBlue' = DColor Blue
183epar = DColor Underlined
184
173hsep [] = mempty 185hsep [] = mempty
174hsep xs = foldr1 (<+>) xs 186hsep xs = foldr1 (<+>) xs
187
175vcat [] = mempty 188vcat [] = mempty
176vcat xs = foldr1 (<$$>) xs 189vcat xs = foldr1 (<$$>) xs
177 190
178shVar = DVar 191shVar = DVar
179 192
180shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
181shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b
182
183inGreen' = DColor Green
184inBlue' = DColor Blue
185epar = DColor Underlined
186
187a <+> b = DDoc $ DOHSep a b
188a </> b = DDoc $ DOSoftSep a b
189a <$$> b = DDoc $ DOVCat a b
190nest n = DDoc . DONest n
191tupled = DDoc . DOTupled
192
193pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) 193pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r))
194pattern DParen x = DPar "(" x ")" 194pattern DParen x = DPar "(" x ")"
195pattern DBrace x = DPar "{" x "}" 195pattern DBrace x = DPar "{" x "}"
@@ -208,6 +208,9 @@ shTuple [] = "()"
208shTuple [x] = DParen $ DParen x 208shTuple [x] = DParen $ DParen x
209shTuple xs = DParen $ foldr1 (DOp "," (InfixR (-20))) xs 209shTuple xs = DParen $ foldr1 (DOp "," (InfixR (-20))) xs
210 210
211shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
212shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b
213
211shAnn _ True x y | strip y == "Type" = x 214shAnn _ True x y | strip y == "Type" = x
212shAnn s _ x y = DOp s (InfixR (-3)) x y 215shAnn s _ x y = DOp s (InfixR (-3)) x y
213 216
@@ -219,12 +222,9 @@ shLet' = DOp ":=" (Infix (-4))
219 222
220pattern DLam vs e = DGlueR (-10) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom " ->"))) e 223pattern DLam vs e = DGlueR (-10) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom " ->"))) e
221 224
222hardSpace = DSep (InfixR 11)
223dLam vs e = DLam (foldr1 hardSpace vs) e
224
225shLam' x (DFreshName True d) = DFreshName True $ shLam' (DUp 0 x) d 225shLam' x (DFreshName True d) = DFreshName True $ shLam' (DUp 0 x) d
226shLam' x (DLam xs y) = DLam (hardSpace x xs) y 226shLam' x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
227shLam' x y = dLam [x] y 227shLam' x y = DLam x y
228 228
229 229
230-------------------------------------------------------------------------------- 230--------------------------------------------------------------------------------
@@ -232,13 +232,22 @@ shLam' x y = dLam [x] y
232class PShow a where 232class PShow a where
233 pShow :: a -> Doc 233 pShow :: a -> Doc
234 234
235ppShow :: PShow a => a -> String
235ppShow = show . pShow 236ppShow = show . pShow
236 237
237-------------------------------------------------------------------------------- 238instance PShow Doc where pShow = id
239instance PShow Int where pShow = fromString . show
240instance PShow Integer where pShow = fromString . show
241instance PShow Double where pShow = fromString . show
242instance PShow Char where pShow = fromString . show
243instance PShow () where pShow _ = "()"
238 244
239instance PShow Bool where 245instance PShow Bool where
240 pShow b = if b then "True" else "False" 246 pShow b = if b then "True" else "False"
241 247
248instance (PShow a, PShow b) => PShow (Either a b) where
249 pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow)
250
242instance (PShow a, PShow b) => PShow (a, b) where 251instance (PShow a, PShow b) => PShow (a, b) where
243 pShow (a, b) = tupled [pShow a, pShow b] 252 pShow (a, b) = tupled [pShow a, pShow b]
244 253
@@ -257,19 +266,6 @@ instance PShow a => PShow (Maybe a) where
257--instance (PShow s, PShow a) => PShow (Map s a) where 266--instance (PShow s, PShow a) => PShow (Map s a) where
258-- pShow = braces . vcat . map (\(k, t) -> pShow k <> P.colon <+> pShow t) . Map.toList 267-- pShow = braces . vcat . map (\(k, t) -> pShow k <> P.colon <+> pShow t) . Map.toList
259 268
260instance (PShow a, PShow b) => PShow (Either a b) where
261 pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow)
262
263instance PShow Doc where
264 pShow x = x
265
266instance PShow Int where pShow = fromString . show
267instance PShow Integer where pShow = fromString . show
268instance PShow Double where pShow = fromString . show
269instance PShow Char where pShow = fromString . show
270instance PShow () where pShow _ = "()"
271
272
273--------------------------------------------------------------------------------- 269---------------------------------------------------------------------------------
274-- TODO: remove 270-- TODO: remove
275 271