diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 20:49:08 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 20:49:08 +0200 |
commit | dfb3cf0e71686ab12977b172f0b992414878e63d (patch) | |
tree | e5bd2eb2003f4905a863e631c0fa7121850a7622 | |
parent | 4f85f455271ee6035924accebf34352229644a31 (diff) |
refactoring
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 82 |
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 | ||
104 | pattern DColor c a = DDoc (DOColor c a) | 104 | pattern DColor c a = DDoc (DOColor c a) |
105 | 105 | ||
106 | strip :: Doc -> Doc | ||
106 | strip = \case | 107 | strip = \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 | ||
113 | simple :: Doc -> Bool | ||
112 | simple x = case strip x of | 114 | simple x = case strip x of |
113 | DAtom{} -> True | 115 | DAtom{} -> True |
114 | DVar{} -> True | 116 | DVar{} -> True |
115 | _ -> False | 117 | _ -> False |
116 | 118 | ||
119 | instance Show Doc where | ||
120 | show = show . renderDoc | ||
121 | |||
117 | renderDoc :: Doc -> P.Doc | 122 | renderDoc :: Doc -> P.Doc |
118 | renderDoc | 123 | renderDoc |
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 | ||
168 | instance Show Doc where | ||
169 | show = show . renderDoc | ||
170 | |||
171 | -------------------------------------------------------------------------- combinators | 173 | -------------------------------------------------------------------------- combinators |
172 | 174 | ||
175 | a <+> b = DDoc $ DOHSep a b | ||
176 | a </> b = DDoc $ DOSoftSep a b | ||
177 | a <$$> b = DDoc $ DOVCat a b | ||
178 | nest n = DDoc . DONest n | ||
179 | tupled = DDoc . DOTupled | ||
180 | |||
181 | inGreen' = DColor Green | ||
182 | inBlue' = DColor Blue | ||
183 | epar = DColor Underlined | ||
184 | |||
173 | hsep [] = mempty | 185 | hsep [] = mempty |
174 | hsep xs = foldr1 (<+>) xs | 186 | hsep xs = foldr1 (<+>) xs |
187 | |||
175 | vcat [] = mempty | 188 | vcat [] = mempty |
176 | vcat xs = foldr1 (<$$>) xs | 189 | vcat xs = foldr1 (<$$>) xs |
177 | 190 | ||
178 | shVar = DVar | 191 | shVar = DVar |
179 | 192 | ||
180 | shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) | ||
181 | shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b | ||
182 | |||
183 | inGreen' = DColor Green | ||
184 | inBlue' = DColor Blue | ||
185 | epar = DColor Underlined | ||
186 | |||
187 | a <+> b = DDoc $ DOHSep a b | ||
188 | a </> b = DDoc $ DOSoftSep a b | ||
189 | a <$$> b = DDoc $ DOVCat a b | ||
190 | nest n = DDoc . DONest n | ||
191 | tupled = DDoc . DOTupled | ||
192 | |||
193 | pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) | 193 | pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) |
194 | pattern DParen x = DPar "(" x ")" | 194 | pattern DParen x = DPar "(" x ")" |
195 | pattern DBrace x = DPar "{" x "}" | 195 | pattern DBrace x = DPar "{" x "}" |
@@ -208,6 +208,9 @@ shTuple [] = "()" | |||
208 | shTuple [x] = DParen $ DParen x | 208 | shTuple [x] = DParen $ DParen x |
209 | shTuple xs = DParen $ foldr1 (DOp "," (InfixR (-20))) xs | 209 | shTuple xs = DParen $ foldr1 (DOp "," (InfixR (-20))) xs |
210 | 210 | ||
211 | shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) | ||
212 | shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b | ||
213 | |||
211 | shAnn _ True x y | strip y == "Type" = x | 214 | shAnn _ True x y | strip y == "Type" = x |
212 | shAnn s _ x y = DOp s (InfixR (-3)) x y | 215 | shAnn s _ x y = DOp s (InfixR (-3)) x y |
213 | 216 | ||
@@ -219,12 +222,9 @@ shLet' = DOp ":=" (Infix (-4)) | |||
219 | 222 | ||
220 | pattern DLam vs e = DGlueR (-10) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom " ->"))) e | 223 | pattern DLam vs e = DGlueR (-10) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom " ->"))) e |
221 | 224 | ||
222 | hardSpace = DSep (InfixR 11) | ||
223 | dLam vs e = DLam (foldr1 hardSpace vs) e | ||
224 | |||
225 | shLam' x (DFreshName True d) = DFreshName True $ shLam' (DUp 0 x) d | 225 | shLam' x (DFreshName True d) = DFreshName True $ shLam' (DUp 0 x) d |
226 | shLam' x (DLam xs y) = DLam (hardSpace x xs) y | 226 | shLam' x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y |
227 | shLam' x y = dLam [x] y | 227 | shLam' x y = DLam x y |
228 | 228 | ||
229 | 229 | ||
230 | -------------------------------------------------------------------------------- | 230 | -------------------------------------------------------------------------------- |
@@ -232,13 +232,22 @@ shLam' x y = dLam [x] y | |||
232 | class PShow a where | 232 | class PShow a where |
233 | pShow :: a -> Doc | 233 | pShow :: a -> Doc |
234 | 234 | ||
235 | ppShow :: PShow a => a -> String | ||
235 | ppShow = show . pShow | 236 | ppShow = show . pShow |
236 | 237 | ||
237 | -------------------------------------------------------------------------------- | 238 | instance PShow Doc where pShow = id |
239 | instance PShow Int where pShow = fromString . show | ||
240 | instance PShow Integer where pShow = fromString . show | ||
241 | instance PShow Double where pShow = fromString . show | ||
242 | instance PShow Char where pShow = fromString . show | ||
243 | instance PShow () where pShow _ = "()" | ||
238 | 244 | ||
239 | instance PShow Bool where | 245 | instance PShow Bool where |
240 | pShow b = if b then "True" else "False" | 246 | pShow b = if b then "True" else "False" |
241 | 247 | ||
248 | instance (PShow a, PShow b) => PShow (Either a b) where | ||
249 | pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow) | ||
250 | |||
242 | instance (PShow a, PShow b) => PShow (a, b) where | 251 | instance (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 | ||
260 | instance (PShow a, PShow b) => PShow (Either a b) where | ||
261 | pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow) | ||
262 | |||
263 | instance PShow Doc where | ||
264 | pShow x = x | ||
265 | |||
266 | instance PShow Int where pShow = fromString . show | ||
267 | instance PShow Integer where pShow = fromString . show | ||
268 | instance PShow Double where pShow = fromString . show | ||
269 | instance PShow Char where pShow = fromString . show | ||
270 | instance PShow () where pShow _ = "()" | ||
271 | |||
272 | |||
273 | --------------------------------------------------------------------------------- | 269 | --------------------------------------------------------------------------------- |
274 | -- TODO: remove | 270 | -- TODO: remove |
275 | 271 | ||