diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 04:01:56 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 04:01:56 +0200 |
commit | 0e21fb5be982ed7e48be455f872fb862ef28b895 (patch) | |
tree | e8d99b8a50940c2d886d091fa83de1285b78257e /src/LambdaCube/Compiler/DesugaredSource.hs | |
parent | 7e9105793bd0d5ff7197a5860ac5339dea677e0e (diff) |
unify Doc types; better expr. pretty print
Diffstat (limited to 'src/LambdaCube/Compiler/DesugaredSource.hs')
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs index 0511988a..e588403a 100644 --- a/src/LambdaCube/Compiler/DesugaredSource.hs +++ b/src/LambdaCube/Compiler/DesugaredSource.hs | |||
@@ -24,6 +24,7 @@ import qualified Data.Set as Set | |||
24 | import qualified Data.IntMap as IM | 24 | import qualified Data.IntMap as IM |
25 | import Control.Arrow hiding ((<+>)) | 25 | import Control.Arrow hiding ((<+>)) |
26 | import Control.DeepSeq | 26 | import Control.DeepSeq |
27 | import Debug.Trace | ||
27 | 28 | ||
28 | import LambdaCube.Compiler.Utils | 29 | import LambdaCube.Compiler.Utils |
29 | import LambdaCube.Compiler.DeBruijn | 30 | import LambdaCube.Compiler.DeBruijn |
@@ -64,7 +65,7 @@ data SPos = SPos | |||
64 | deriving (Eq, Ord) | 65 | deriving (Eq, Ord) |
65 | 66 | ||
66 | instance PShow SPos where | 67 | instance PShow SPos where |
67 | pShowPrec _ (SPos r c) = pShow r <> ":" <> pShow c | 68 | pShow (SPos r c) = pShow r <> ":" <> pShow c |
68 | 69 | ||
69 | -------------------------------------------------------------------------------- file info | 70 | -------------------------------------------------------------------------------- file info |
70 | 71 | ||
@@ -77,7 +78,7 @@ data FileInfo = FileInfo | |||
77 | instance Eq FileInfo where (==) = (==) `on` fileId | 78 | instance Eq FileInfo where (==) = (==) `on` fileId |
78 | instance Ord FileInfo where compare = compare `on` fileId | 79 | instance Ord FileInfo where compare = compare `on` fileId |
79 | 80 | ||
80 | instance PShow FileInfo where pShowPrec _ = text . filePath | 81 | instance PShow FileInfo where pShow = text . filePath |
81 | instance Show FileInfo where show = ppShow | 82 | instance Show FileInfo where show = ppShow |
82 | 83 | ||
83 | showPos :: FileInfo -> SPos -> Doc | 84 | showPos :: FileInfo -> SPos -> Doc |
@@ -92,7 +93,7 @@ instance NFData Range where | |||
92 | rnf Range{} = () | 93 | rnf Range{} = () |
93 | 94 | ||
94 | -- short version | 95 | -- short version |
95 | instance PShow Range where pShowPrec _ (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e | 96 | instance PShow Range where pShow (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e |
96 | instance Show Range where show = ppShow | 97 | instance Show Range where show = ppShow |
97 | 98 | ||
98 | -- long version | 99 | -- long version |
@@ -131,8 +132,8 @@ instance Monoid SI where | |||
131 | mappend _ r@RangeSI{} = r | 132 | mappend _ r@RangeSI{} = r |
132 | 133 | ||
133 | instance PShow SI where | 134 | instance PShow SI where |
134 | pShowPrec _ (NoSI ds) = hsep $ map text $ Set.toList ds | 135 | pShow (NoSI ds) = hsep $ map text $ Set.toList ds |
135 | pShowPrec _ (RangeSI r) = pShow r | 136 | pShow (RangeSI r) = pShow r |
136 | 137 | ||
137 | -- long version | 138 | -- long version |
138 | showSI x = case sourceInfo x of | 139 | showSI x = case sourceInfo x of |
@@ -155,13 +156,14 @@ pattern SIName si n <- SIName_ si _ n | |||
155 | instance Eq SIName where (==) = (==) `on` sName | 156 | instance Eq SIName where (==) = (==) `on` sName |
156 | instance Ord SIName where compare = compare `on` sName | 157 | instance Ord SIName where compare = compare `on` sName |
157 | instance Show SIName where show = sName | 158 | instance Show SIName where show = sName |
158 | instance PShow SIName where pShowPrec _ = text . sName | 159 | instance PShow SIName where pShow = text . sName |
159 | 160 | ||
160 | sName (SIName _ s) = s | 161 | sName (SIName _ s) = s |
161 | 162 | ||
162 | --appName f (SIName si n) = SIName si $ f n | 163 | --appName f (SIName si n) = SIName si $ f n |
163 | 164 | ||
164 | getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f | 165 | getFixity_ (SIName_ _ f _) = f |
166 | --getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f | ||
165 | 167 | ||
166 | ------------- | 168 | ------------- |
167 | 169 | ||
@@ -386,13 +388,14 @@ trSExp' :: SExp -> SExp' a | |||
386 | trSExp' = trSExp elimVoid | 388 | trSExp' = trSExp elimVoid |
387 | 389 | ||
388 | instance Up a => PShow (SExp' a) where | 390 | instance Up a => PShow (SExp' a) where |
389 | pShowPrec _ = showDoc_ . sExpDoc | 391 | pShow = sExpDoc |
390 | 392 | ||
391 | sExpDoc :: Up a => SExp' a -> NDoc | 393 | sExpDoc :: Up a => SExp' a -> NDoc |
392 | sExpDoc = \case | 394 | sExpDoc = \case |
393 | SGlobal ns -> shAtom $ sName ns | 395 | SGlobal ns -> shAtom $ sName ns |
394 | SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b) | 396 | SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b) |
395 | TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a) | 397 | TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a) |
398 | SGlobal op `SAppV` a `SAppV` b | Just p <- getFixity_ op -> DOp p (pShow a) (sName op) (pShow b) | ||
396 | SApp h a b -> shApp h (sExpDoc a) (sExpDoc b) | 399 | SApp h a b -> shApp h (sExpDoc a) (sExpDoc b) |
397 | Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t) | 400 | Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t) |
398 | SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b) | 401 | SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b) |
@@ -401,6 +404,9 @@ sExpDoc = \case | |||
401 | SVar _ i -> shVar i | 404 | SVar _ i -> shVar i |
402 | SLit _ l -> shAtom $ show l | 405 | SLit _ l -> shAtom $ show l |
403 | 406 | ||
407 | shApp Visible a b = DApp a b | ||
408 | shApp Hidden a b = DApp a (DOp (Fixity InfixR 20) "@" "" b) | ||
409 | |||
404 | shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b | 410 | shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b |
405 | where | 411 | where |
406 | lam = case h of | 412 | lam = case h of |
@@ -408,7 +414,7 @@ shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b | |||
408 | _ -> shLam' | 414 | _ -> shLam' |
409 | 415 | ||
410 | p = case h of | 416 | p = case h of |
411 | BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0) | 417 | BMeta -> shAnn ":" True (inBlue' $ DVar 0) |
412 | BLam h -> vpar h | 418 | BLam h -> vpar h |
413 | BPi h -> vpar h | 419 | BPi h -> vpar h |
414 | 420 | ||
@@ -429,7 +435,7 @@ data Stmt | |||
429 | pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") | 435 | pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") |
430 | 436 | ||
431 | instance PShow Stmt where | 437 | instance PShow Stmt where |
432 | pShowPrec p = \case | 438 | pShow = \case |
433 | Let n ty e -> text (sName n) </> "=" <+> maybe (pShow e) (\ty -> pShow e </> "::" <+> pShow ty) ty | 439 | Let n ty e -> text (sName n) </> "=" <+> maybe (pShow e) (\ty -> pShow e </> "::" <+> pShow ty) ty |
434 | Data n ps ty cs -> "data" <+> text (sName n) | 440 | Data n ps ty cs -> "data" <+> text (sName n) |
435 | PrecDef n i -> "precedence" <+> text (sName n) <+> text (show i) | 441 | PrecDef n i -> "precedence" <+> text (sName n) <+> text (show i) |