summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-03 15:08:14 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-03 15:08:14 +0200
commitced2bb3b8d35d0e2faa21adeff04607a2174ae99 (patch)
tree679e0463b3d88b6176130e1b27187610e254addc
parent1b49cc7e377e6c16d6c5bc4cced39dc20b8c0bec (diff)
refactoring
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs71
-rw-r--r--testdata/Builtins.out2
-rw-r--r--testdata/Prelude.out3
-rw-r--r--testdata/language-features/adt/gadt01.lc2
4 files changed, 41 insertions, 37 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
95instance NFData Doc where 95instance NFData Doc where
96 rnf x = rnf $ show x -- TODO 96 rnf x = rnf $ show x -- TODO
97{- 97
98strip :: Doc -> Doc
99strip = \case
100 DFormat _ x -> strip x
101 DUp _ x -> strip x
102 DFreshName _ x -> strip x
103 x -> x
104-}
105instance Show Doc where 98instance 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
114renderDoc :: Doc -> P.Doc 107renderDoc :: Doc -> P.Doc
115renderDoc 108renderDoc
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, [])
diff --git a/testdata/Builtins.out b/testdata/Builtins.out
index 96574724..2e7d6b11 100644
--- a/testdata/Builtins.out
+++ b/testdata/Builtins.out
@@ -938,7 +938,7 @@ allSame
938 \(b :: _) (c :: _) -> case'List 938 \(b :: _) (c :: _) -> case'List
939 \_ -> _ :: _ 939 \_ -> _ :: _
940 (_rhs 'Unit) 940 (_rhs 'Unit)
941 \(d :: _) (e :: _) -> _rhs ('T2 (b ~ d) (allSame (d : e))) 941 \(d :: _) (e :: _) -> _rhs ('T2 (b `'EqCTt` d) (allSame (d : e)))
942 c 942 c
943 a) 943 a)
944 :: forall (f :: _) . [f] -> Type 944 :: forall (f :: _) . [f] -> Type
diff --git a/testdata/Prelude.out b/testdata/Prelude.out
index ead20303..88464b4e 100644
--- a/testdata/Prelude.out
+++ b/testdata/Prelude.out
@@ -191,7 +191,8 @@ isKeyC
191 (_rhs ('Empty "")) 191 (_rhs ('Empty ""))
192 \(d :: _) (e :: _) -> case'RecItem 192 \(d :: _) (e :: _) -> case'RecItem
193 \_ -> _ :: _ 193 \_ -> _ :: _
194 \(f :: _) (g :: _) -> _rhs (primIfThenElse (a == f) (b ~ g) (isKeyC a b e)) 194 \(f :: _) (g :: _) -> _rhs
195 (primIfThenElse (a == f) (b `'EqCTt` g) (isKeyC a b e))
195 d 196 d
196 c 197 c
197fstTup = _rhs (hlistConsCase (_ :: _) \(a :: _) -> \_ -> a) 198fstTup = _rhs (hlistConsCase (_ :: _) \(a :: _) -> \_ -> a)
diff --git a/testdata/language-features/adt/gadt01.lc b/testdata/language-features/adt/gadt01.lc
index b68762ca..741f2dce 100644
--- a/testdata/language-features/adt/gadt01.lc
+++ b/testdata/language-features/adt/gadt01.lc
@@ -14,7 +14,7 @@ data M3 (a :: Type) :: String -> Type where
14 14
15-- g (Value2 x) = x 15-- g (Value2 x) = x
16-- g :: forall a . forall m . M2 a m -> a 16-- g :: forall a . forall m . M2 a m -> a
17g = 'M2Case (\_ _ -> _) (\e @_ -> e) 17g = case'M2 (\_ _ -> _) (\e @_ -> e)
18 18
19h (Value3 x) = x 19h (Value3 x) = x
20 20