diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-03 15:37:02 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-03 15:37:02 +0200 |
commit | 57d10e734217c6eeaaec61131861bfd2861a4eb0 (patch) | |
tree | 1ced06f37ea0902999341ea80bc646cdba7e5db4 /src | |
parent | ced2bb3b8d35d0e2faa21adeff04607a2174ae99 (diff) |
fix pretty print parens
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs index 62d044c5..fe917fa6 100644 --- a/src/LambdaCube/Compiler/Pretty.hs +++ b/src/LambdaCube/Compiler/Pretty.hs | |||
@@ -107,7 +107,7 @@ simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc | |||
107 | renderDoc :: Doc -> P.Doc | 107 | renderDoc :: Doc -> P.Doc |
108 | renderDoc | 108 | renderDoc |
109 | = render | 109 | = render |
110 | . addPar (Infix (-10)) | 110 | . addPar (-10, -10) |
111 | . namespace False | 111 | . namespace False |
112 | . flip runReader freeNames | 112 | . flip runReader freeNames |
113 | . flip evalStateT freshNames | 113 | . flip evalStateT freshNames |
@@ -169,32 +169,29 @@ renderDoc | |||
169 | switch True cs@(c:_) | isUpper c = '\'': cs | 169 | switch True cs@(c:_) | isUpper c = '\'': cs |
170 | switch _ x = x | 170 | switch _ x = x |
171 | 171 | ||
172 | addPar :: Fixity -> Doc -> Doc | 172 | addPar :: (Int, Int) -> Doc -> Doc |
173 | addPar pr x = case x of | 173 | addPar pr@(prl, prr) x = case x of |
174 | DAtom x -> DAtom $ addParA x | 174 | DAtom x -> DAtom $ addParA x |
175 | DOp0 s f -> DParen $ DOp0 s f | 175 | DOp0 s f -> DParen $ DOp0 s f |
176 | DOp0 s f `DApp` x `DApp` y -> addPar pr $ DOp (addBackquotes s) f x y | 176 | DOp0 s f `DApp` x `DApp` y -> addPar pr $ DOp (addBackquotes s) f x y |
177 | -- DOpL s f x -> DParen $ DOpL s f $ addPar (InfixL $ leftPrecedence f) x | 177 | DInfix pr' x op y |
178 | -- DOpR s f x -> DParen $ DOpR s f $ addPar (InfixR $ rightPrecedence f) x | 178 | | precedence pr' < prl || precedence pr' < prr |
179 | DInfix pr' x op y -> (if precedence pr' < precedence pr then DParen else id) | 179 | -> DParen $ DInfix pr' (addPar (-20, leftPrecedence pr') x) (addParA op) (addPar (rightPrecedence pr', -20) y) |
180 | $ DInfix pr' (addPar (InfixL $ leftPrecedence pr') x) (addParA op) (addPar (InfixR $ rightPrecedence pr') y) | 180 | | otherwise -> DInfix pr' (addPar (prl, leftPrecedence pr') x) (addParA op) (addPar (rightPrecedence pr', prr) y) |
181 | DPreOp pr' op y -> (if protect pr' then DParen else id) | 181 | DPreOp pr' op y |
182 | $ DPreOp pr' (addParA op) (addPar (Infix pr') y) | 182 | | pr' < prr -> DParen $ DPreOp pr' (addParA op) (addPar (pr', -20) y) |
183 | | otherwise -> DPreOp pr' (addParA op) (addPar (pr', prr) y) | ||
183 | DFormat c x -> DFormat c $ addPar pr x | 184 | DFormat c x -> DFormat c $ addPar pr x |
184 | DTypeNamespace c x -> DTypeNamespace c $ addPar pr x | 185 | DTypeNamespace c x -> DTypeNamespace c $ addPar pr x |
185 | DDocOp x d -> DDocOp x $ addPar (Infix (-10)) <$> d | 186 | DDocOp x d -> DDocOp x $ addPar (-10, -10) <$> d |
186 | where | 187 | where |
187 | addParA (SimpleAtom s) = SimpleAtom s | 188 | addParA (SimpleAtom s) = SimpleAtom s |
188 | addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar (Infix i) d) $ addParA a | 189 | addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar (i, i) d) $ addParA a |
189 | 190 | ||
190 | addBackquotes "EqCTt" = "~" | 191 | addBackquotes "EqCTt" = "~" |
191 | addBackquotes s@(c:_) | isAlpha c || c == '_' || c == '\'' = '`': s ++ "`" | 192 | addBackquotes s@(c:_) | isAlpha c || c == '_' || c == '\'' = '`': s ++ "`" |
192 | addBackquotes s = s | 193 | addBackquotes s = s |
193 | 194 | ||
194 | protect f = case pr of | ||
195 | InfixL pr -> f < pr | ||
196 | _ -> False | ||
197 | |||
198 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) | 195 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) |
199 | getApps x = (x, []) | 196 | getApps x = (x, []) |
200 | 197 | ||