diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-03 15:08:14 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-03 15:08:14 +0200 |
commit | ced2bb3b8d35d0e2faa21adeff04607a2174ae99 (patch) | |
tree | 679e0463b3d88b6176130e1b27187610e254addc /src | |
parent | 1b49cc7e377e6c16d6c5bc4cced39dc20b8c0bec (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs index 6f8fdb20..62d044c5 100644 --- a/src/LambdaCube/Compiler/Pretty.hs +++ b/src/LambdaCube/Compiler/Pretty.hs | |||
@@ -94,14 +94,7 @@ instance Monoid Doc where | |||
94 | 94 | ||
95 | instance NFData Doc where | 95 | instance NFData Doc where |
96 | rnf x = rnf $ show x -- TODO | 96 | rnf x = rnf $ show x -- TODO |
97 | {- | 97 | |
98 | strip :: Doc -> Doc | ||
99 | strip = \case | ||
100 | DFormat _ x -> strip x | ||
101 | DUp _ x -> strip x | ||
102 | DFreshName _ x -> strip x | ||
103 | x -> x | ||
104 | -} | ||
105 | instance Show Doc where | 98 | instance Show Doc where |
106 | show = ($ "") . P.displayS . P.renderPretty 0.4 200 . renderDoc | 99 | show = ($ "") . P.displayS . P.renderPretty 0.4 200 . renderDoc |
107 | 100 | ||
@@ -114,7 +107,8 @@ simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc | |||
114 | renderDoc :: Doc -> P.Doc | 107 | renderDoc :: Doc -> P.Doc |
115 | renderDoc | 108 | renderDoc |
116 | = render | 109 | = render |
117 | . addPar False (Infix (-10)) | 110 | . addPar (Infix (-10)) |
111 | . namespace False | ||
118 | . flip runReader freeNames | 112 | . flip runReader freeNames |
119 | . flip evalStateT freshNames | 113 | . flip evalStateT freshNames |
120 | . showVars | 114 | . showVars |
@@ -156,28 +150,18 @@ renderDoc | |||
156 | showVarA (SimpleAtom s) = pure $ SimpleAtom s | 150 | showVarA (SimpleAtom s) = pure $ SimpleAtom s |
157 | showVarA (ComplexAtom s i d a) = ComplexAtom s i <$> showVars d <*> showVarA a | 151 | showVarA (ComplexAtom s i d a) = ComplexAtom s i <$> showVars d <*> showVarA a |
158 | 152 | ||
159 | addPar :: Bool -> Fixity -> Doc -> Doc | 153 | namespace :: Bool -> Doc -> Doc |
160 | addPar tn pr x = case x of | 154 | namespace tn x = case x of |
161 | DAtom x -> DAtom $ addParA x | 155 | DAtom x -> DAtom $ namespaceA x |
162 | DText "'List" `DApp` x -> addPar tn pr $ DBracket x | 156 | DText "'List" `DApp` x -> namespace tn $ DBracket x |
163 | DOp0 s f -> DParen $ DOp0 s f | 157 | DInfix pr' x op y -> DInfix pr' (namespace tn x) (namespaceA op) (namespace tn y) |
164 | DOp0 s f `DApp` x `DApp` y -> addPar tn pr $ DOp (addBackquotes s) f x y | 158 | DPreOp pr' op y -> DPreOp pr' (namespaceA op) (namespace tn y) |
165 | -- DOpL s f x -> DParen $ DOpL s f $ addPar tn (InfixL $ leftPrecedence f) x | 159 | DFormat c x -> DFormat c $ namespace tn x |
166 | -- DOpR s f x -> DParen $ DOpR s f $ addPar tn (InfixR $ rightPrecedence f) x | 160 | DTypeNamespace c x -> namespace c x |
167 | DInfix pr' x op y -> (if protect then DParen else id) | 161 | DDocOp x d -> DDocOp x $ namespace tn <$> d |
168 | $ DInfix pr' (addPar tn (InfixL $ leftPrecedence pr') x) (addParA op) (addPar tn (InfixR $ rightPrecedence pr') y) | ||
169 | DPreOp pr' op y -> (if protect then DParen else id) | ||
170 | $ DPreOp pr' (addParA op) (addPar tn (Infix pr') y) | ||
171 | DFormat c x -> DFormat c $ addPar tn pr x | ||
172 | DTypeNamespace c x -> addPar c pr x | ||
173 | DDocOp x d -> DDocOp x $ addPar tn (Infix (-10)) <$> d | ||
174 | where | 162 | where |
175 | addParA (SimpleAtom s) = SimpleAtom $ switch tn s | 163 | namespaceA (SimpleAtom s) = SimpleAtom $ switch tn s |
176 | addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar tn (Infix i) d) $ addParA a | 164 | namespaceA (ComplexAtom s i d a) = ComplexAtom s i (namespace tn d) $ namespaceA a |
177 | |||
178 | addBackquotes "'EqCTt" = "~" | ||
179 | addBackquotes s@(c:_) | isAlpha c || c == '_' || c == '\'' = '`': s ++ "`" | ||
180 | addBackquotes s = s | ||
181 | 165 | ||
182 | switch True ('`': '\'': cs@(c: _)) | isUpper c = '`': cs | 166 | switch True ('`': '\'': cs@(c: _)) | isUpper c = '`': cs |
183 | switch True ('\'': cs@(c: _)) | isUpper c {- && last cs /= '\'' -} = cs | 167 | switch True ('\'': cs@(c: _)) | isUpper c {- && last cs /= '\'' -} = cs |
@@ -185,12 +169,31 @@ renderDoc | |||
185 | switch True cs@(c:_) | isUpper c = '\'': cs | 169 | switch True cs@(c:_) | isUpper c = '\'': cs |
186 | switch _ x = x | 170 | switch _ x = x |
187 | 171 | ||
188 | protect = case x of | 172 | addPar :: Fixity -> Doc -> Doc |
189 | DInfix f _ _ _ -> precedence f < precedence pr | 173 | addPar pr x = case x of |
190 | DPreOp f _ _ -> case pr of | 174 | DAtom x -> DAtom $ addParA x |
175 | DOp0 s f -> DParen $ DOp0 s f | ||
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 | ||
178 | -- DOpR s f x -> DParen $ DOpR s f $ addPar (InfixR $ rightPrecedence f) x | ||
179 | DInfix pr' x op y -> (if precedence pr' < precedence pr then DParen else id) | ||
180 | $ DInfix pr' (addPar (InfixL $ leftPrecedence pr') x) (addParA op) (addPar (InfixR $ rightPrecedence pr') y) | ||
181 | DPreOp pr' op y -> (if protect pr' then DParen else id) | ||
182 | $ DPreOp pr' (addParA op) (addPar (Infix pr') y) | ||
183 | DFormat c x -> DFormat c $ addPar pr x | ||
184 | DTypeNamespace c x -> DTypeNamespace c $ addPar pr x | ||
185 | DDocOp x d -> DDocOp x $ addPar (Infix (-10)) <$> d | ||
186 | where | ||
187 | addParA (SimpleAtom s) = SimpleAtom s | ||
188 | addParA (ComplexAtom s i d a) = ComplexAtom s i (addPar (Infix i) d) $ addParA a | ||
189 | |||
190 | addBackquotes "EqCTt" = "~" | ||
191 | addBackquotes s@(c:_) | isAlpha c || c == '_' || c == '\'' = '`': s ++ "`" | ||
192 | addBackquotes s = s | ||
193 | |||
194 | protect f = case pr of | ||
191 | InfixL pr -> f < pr | 195 | InfixL pr -> f < pr |
192 | _ -> False | 196 | _ -> False |
193 | _ -> False | ||
194 | 197 | ||
195 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) | 198 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) |
196 | getApps x = (x, []) | 199 | getApps x = (x, []) |