diff options
Diffstat (limited to 'src/LambdaCube/Compiler/Pretty.hs')
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs index f9702b94..db31ee2e 100644 --- a/src/LambdaCube/Compiler/Pretty.hs +++ b/src/LambdaCube/Compiler/Pretty.hs | |||
@@ -16,6 +16,7 @@ module LambdaCube.Compiler.Pretty | |||
16 | import Data.Maybe | 16 | import Data.Maybe |
17 | import Data.String | 17 | import Data.String |
18 | import Data.Char | 18 | import Data.Char |
19 | import Data.Monoid | ||
19 | --import qualified Data.Set as Set | 20 | --import qualified Data.Set as Set |
20 | --import qualified Data.Map as Map | 21 | --import qualified Data.Map as Map |
21 | import Control.Applicative | 22 | import Control.Applicative |
@@ -153,8 +154,22 @@ renderDoc | |||
153 | showVarA (SimpleAtom s) = pure $ SimpleAtom s | 154 | showVarA (SimpleAtom s) = pure $ SimpleAtom s |
154 | showVarA (ComplexAtom s i d a) = ComplexAtom s i <$> showVars d <*> showVarA a | 155 | showVarA (ComplexAtom s i d a) = ComplexAtom s i <$> showVars d <*> showVarA a |
155 | 156 | ||
157 | getTup (DText "HCons" `DApp` x `DApp` (getTup -> Just xs)) = Just $ x: xs | ||
158 | getTup (DText "HNil") = Just [] | ||
159 | getTup _ = Nothing | ||
160 | |||
161 | getList (DOp0 ":" _ `DApp` x `DApp` (getList -> Just xs)) = Just $ x: xs | ||
162 | getList (DText "Nil") = Just [] | ||
163 | getList _ = Nothing | ||
164 | |||
165 | shTick True = DPreOp 20 (SimpleAtom "'") | ||
166 | shTick False = id | ||
167 | |||
156 | namespace :: Bool -> Doc -> Doc | 168 | namespace :: Bool -> Doc -> Doc |
157 | namespace tn x = case x of | 169 | namespace tn x = case x of |
170 | (getTup -> Just xs) -> shTick tn $ namespace tn $ shTuple xs | ||
171 | (getList -> Just xs) -> shTick tn $ namespace tn $ shList xs | ||
172 | DText "'HList" `DApp` (getList -> Just xs) -> shTick (not tn) $ namespace tn $ shTuple xs | ||
158 | DAtom x -> DAtom $ namespaceA x | 173 | DAtom x -> DAtom $ namespaceA x |
159 | DText "'List" `DApp` x -> namespace tn $ DBracket x | 174 | DText "'List" `DApp` x -> namespace tn $ DBracket x |
160 | DInfix pr' x op y -> DInfix pr' (namespace tn x) (namespaceA op) (namespace tn y) | 175 | DInfix pr' x op y -> DInfix pr' (namespace tn x) (namespaceA op) (namespace tn y) |
@@ -198,25 +213,39 @@ renderDoc | |||
198 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) | 213 | getApps (DApp (getApps -> (n, xs)) x) = (n, x: xs) |
199 | getApps x = (x, []) | 214 | getApps x = (x, []) |
200 | 215 | ||
216 | getSemis (DSemi x (getSemis -> (xs, n))) = (x: xs, n) | ||
217 | getSemis x = ([], x) | ||
218 | |||
219 | getCommas (DComma x (getCommas -> xs)) = x: xs | ||
220 | getCommas x = [x] | ||
221 | |||
201 | render :: Doc -> P.Doc | 222 | render :: Doc -> P.Doc |
202 | render = snd . render' | 223 | render = snd . render' |
203 | where | 224 | where |
204 | render' = \case | 225 | render' = \case |
205 | DText "Nil" -> rtext "[]" | ||
206 | DText "'Nil" -> rtext "'[]" | ||
207 | DAtom x -> renderA x | 226 | DAtom x -> renderA x |
208 | DFormat c x -> second c $ render' x | 227 | DFormat c x -> second c $ render' x |
209 | DDocOp f d -> (('\0', '\0'), f $ render <$> d) | 228 | DDocOp f d -> (('\0', '\0'), f $ render <$> d) |
210 | DPreOp _ op y -> renderA op <++> render' y | 229 | DPreOp _ op y -> renderA' op <++> render' y |
211 | DSep (InfixR 11) a b -> gr $ render' a <+++> render' b | 230 | DSep (InfixR 11) a b -> gr $ render' a <+++> render' b |
212 | x@DApp{} -> case getApps x of | 231 | x@DApp{} -> case getApps x of |
213 | (n, reverse -> xs) -> ((\xs -> (fst $ head xs, snd $ last xs)) *** P.nest 2 . P.sep) (unzip $ render' n: (render' <$> xs)) | 232 | (n, reverse -> xs) -> ((\xs -> (fst $ head xs, snd $ last xs)) *** P.nest 2 . P.sep) (unzip $ render' n: (render' <$> xs)) |
233 | x@DComma{} -> case getCommas x of | ||
234 | x: xs -> ((\xs -> (fst $ head xs, snd $ last xs)) *** P.cat) (unzip $ render' x: (second ("," P.<+>) . render' <$> xs)) | ||
235 | x@DSemi{} -> case getSemis x of | ||
236 | (xs, n) -> ((\xs -> (fst $ head xs, snd $ last xs)) *** P.sep) (unzip $ (second (<> ";") . render' <$> xs) ++ [render' n]) | ||
214 | DInfix _ x op y -> gr $ render' x <+++> renderA op <++> render' y | 237 | DInfix _ x op y -> gr $ render' x <+++> renderA op <++> render' y |
215 | 238 | ||
239 | renderA' (SimpleAtom s) = rtext s | ||
240 | renderA' x = gr $ renderA'' x | ||
241 | |||
242 | renderA'' (SimpleAtom s) = rtext s | ||
243 | renderA'' (ComplexAtom s _ d a) = rtext s <+++> render' d <+++> renderA'' a | ||
244 | |||
216 | renderA (SimpleAtom s) = rtext s | 245 | renderA (SimpleAtom s) = rtext s |
217 | renderA (ComplexAtom s _ d a) = rtext s <++> render' d <++> renderA a | 246 | renderA (ComplexAtom s _ d a) = rtext s <++> render' d <++> renderA a |
218 | 247 | ||
219 | gr = second (P.nest 2. P.group) | 248 | gr = second (P.nest 2 . P.group) |
220 | 249 | ||
221 | rtext "" = (('\0', '\0'), mempty) | 250 | rtext "" = (('\0', '\0'), mempty) |
222 | rtext s@(h:_) = ((h, last s), P.text s) | 251 | rtext s@(h:_) = ((h, last s), P.text s) |
@@ -224,12 +253,12 @@ renderDoc | |||
224 | ((lx, rx), x) <++> ((ly, ry), y) = ((lx, ry), z) | 253 | ((lx, rx), x) <++> ((ly, ry), y) = ((lx, ry), z) |
225 | where | 254 | where |
226 | z | sep rx ly = x P.<+> y | 255 | z | sep rx ly = x P.<+> y |
227 | | otherwise = x P.<> y | 256 | | otherwise = x <> y |
228 | 257 | ||
229 | ((lx, rx), x) <+++> ((ly, ry), y) = ((lx, ry), z) | 258 | ((lx, rx), x) <+++> ((ly, ry), y) = ((lx, ry), z) |
230 | where | 259 | where |
231 | z | sep rx ly = x P.<> P.line P.<> y | 260 | z | sep rx ly = x <> P.line <> y |
232 | | otherwise = x P.<> y | 261 | | otherwise = x <> y |
233 | 262 | ||
234 | sep x y | 263 | sep x y |
235 | | x == '\0' || y == '\0' = False | 264 | | x == '\0' || y == '\0' = False |
@@ -291,6 +320,7 @@ infixl 4 `DApp` | |||
291 | 320 | ||
292 | pattern DAt x = DGlue (InfixR 20) (DText "@") x | 321 | pattern DAt x = DGlue (InfixR 20) (DText "@") x |
293 | pattern DApp x y = DSep (InfixL 10) x y | 322 | pattern DApp x y = DSep (InfixL 10) x y |
323 | pattern DSemi x y = DOp ";" (InfixR (-19)) x y | ||
294 | pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => . | 324 | pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => . |
295 | pattern DCstr x y = DOp "~" (Infix (-2)) x y | 325 | pattern DCstr x y = DOp "~" (Infix (-2)) x y |
296 | pattern DAnn x y = DOp "::" (Infix (-3)) x (DTypeNamespace True y) | 326 | pattern DAnn x y = DOp "::" (Infix (-3)) x (DTypeNamespace True y) |
@@ -317,12 +347,16 @@ shTuple [] = "()" | |||
317 | shTuple [x] = DParen $ DParen x | 347 | shTuple [x] = DParen $ DParen x |
318 | shTuple xs = DParen $ foldr1 DComma xs | 348 | shTuple xs = DParen $ foldr1 DComma xs |
319 | 349 | ||
350 | shList [] = "[]" | ||
351 | shList xs = DBracket $ foldr1 DComma xs | ||
352 | |||
320 | shAnn = DAnn | 353 | shAnn = DAnn |
321 | 354 | ||
322 | shArr = DArr | 355 | shArr = DArr |
323 | 356 | ||
324 | 357 | ||
325 | pattern DForall s vs e = DArr_ s (DPreOp 10 (SimpleAtom "forall") vs) e | 358 | pattern DForall s vs e = DArr_ s (DPreOp 10 (SimpleAtom "forall") vs) e |
359 | pattern DContext' vs e = DArr_ "->" (DAt vs) e | ||
326 | pattern DContext vs e = DArr_ "=>" vs e | 360 | pattern DContext vs e = DArr_ "=>" vs e |
327 | pattern DParContext vs e = DContext (DParen vs) e | 361 | pattern DParContext vs e = DContext (DParen vs) e |
328 | pattern DLam vs e = DPreOp (-10) (ComplexAtom "\\" 11 vs (SimpleAtom "->")) e | 362 | pattern DLam vs e = DPreOp (-10) (ComplexAtom "\\" 11 vs (SimpleAtom "->")) e |