summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler/DesugaredSource.hs
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-28 04:01:56 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-28 04:01:56 +0200
commit0e21fb5be982ed7e48be455f872fb862ef28b895 (patch)
treee8d99b8a50940c2d886d091fa83de1285b78257e /src/LambdaCube/Compiler/DesugaredSource.hs
parent7e9105793bd0d5ff7197a5860ac5339dea677e0e (diff)
unify Doc types; better expr. pretty print
Diffstat (limited to 'src/LambdaCube/Compiler/DesugaredSource.hs')
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs26
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
24import qualified Data.IntMap as IM 24import qualified Data.IntMap as IM
25import Control.Arrow hiding ((<+>)) 25import Control.Arrow hiding ((<+>))
26import Control.DeepSeq 26import Control.DeepSeq
27import Debug.Trace
27 28
28import LambdaCube.Compiler.Utils 29import LambdaCube.Compiler.Utils
29import LambdaCube.Compiler.DeBruijn 30import LambdaCube.Compiler.DeBruijn
@@ -64,7 +65,7 @@ data SPos = SPos
64 deriving (Eq, Ord) 65 deriving (Eq, Ord)
65 66
66instance PShow SPos where 67instance 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
77instance Eq FileInfo where (==) = (==) `on` fileId 78instance Eq FileInfo where (==) = (==) `on` fileId
78instance Ord FileInfo where compare = compare `on` fileId 79instance Ord FileInfo where compare = compare `on` fileId
79 80
80instance PShow FileInfo where pShowPrec _ = text . filePath 81instance PShow FileInfo where pShow = text . filePath
81instance Show FileInfo where show = ppShow 82instance Show FileInfo where show = ppShow
82 83
83showPos :: FileInfo -> SPos -> Doc 84showPos :: 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
95instance PShow Range where pShowPrec _ (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e 96instance PShow Range where pShow (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e
96instance Show Range where show = ppShow 97instance 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
133instance PShow SI where 134instance 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
138showSI x = case sourceInfo x of 139showSI x = case sourceInfo x of
@@ -155,13 +156,14 @@ pattern SIName si n <- SIName_ si _ n
155instance Eq SIName where (==) = (==) `on` sName 156instance Eq SIName where (==) = (==) `on` sName
156instance Ord SIName where compare = compare `on` sName 157instance Ord SIName where compare = compare `on` sName
157instance Show SIName where show = sName 158instance Show SIName where show = sName
158instance PShow SIName where pShowPrec _ = text . sName 159instance PShow SIName where pShow = text . sName
159 160
160sName (SIName _ s) = s 161sName (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
164getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f 165getFixity_ (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
386trSExp' = trSExp elimVoid 388trSExp' = trSExp elimVoid
387 389
388instance Up a => PShow (SExp' a) where 390instance Up a => PShow (SExp' a) where
389 pShowPrec _ = showDoc_ . sExpDoc 391 pShow = sExpDoc
390 392
391sExpDoc :: Up a => SExp' a -> NDoc 393sExpDoc :: Up a => SExp' a -> NDoc
392sExpDoc = \case 394sExpDoc = \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
407shApp Visible a b = DApp a b
408shApp Hidden a b = DApp a (DOp (Fixity InfixR 20) "@" "" b)
409
404shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b 410shLam 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
429pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") 435pattern Primitive n t = Let n (Just t) (SBuiltin "undefined")
430 436
431instance PShow Stmt where 437instance 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)