summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2017-06-14 23:04:40 +0100
committerCsaba Hruska <csaba.hruska@gmail.com>2017-06-14 23:04:40 +0100
commit0ed4c201ecd4a0700e582fdb0284abedcf4b87aa (patch)
tree48adecf6e1b82de4f4b93abb8c03e0bbd588bceb
parent30792e3d67a11787a691493683c703a4767ce96d (diff)
add more typesig
-rw-r--r--refactor.plan2
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs2
-rw-r--r--src/LambdaCube/Compiler/Utils.hs9
3 files changed, 11 insertions, 2 deletions
diff --git a/refactor.plan b/refactor.plan
index 9d08dc97..92a46638 100644
--- a/refactor.plan
+++ b/refactor.plan
@@ -2,7 +2,7 @@ goals:
2 simple, clean API and dependency tree 2 simple, clean API and dependency tree
3 3
4stage 1: 4stage 1:
5ok - LambdaCube.Compiler.Utils 5typesig - LambdaCube.Compiler.Utils
6import LambdaCube.Compiler.Pretty 6import LambdaCube.Compiler.Pretty
7import LambdaCube.Compiler.DeBruijn 7import LambdaCube.Compiler.DeBruijn
8 8
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 05fdd096..317c4080 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -82,12 +82,14 @@ data DocAtom
82 = SimpleAtom String 82 = SimpleAtom String
83 | ComplexAtom String Int Doc DocAtom 83 | ComplexAtom String Int Doc DocAtom
84 84
85mapDocAtom :: (String -> Int -> Doc -> Doc) -> DocAtom -> DocAtom
85mapDocAtom f (SimpleAtom s) = SimpleAtom s 86mapDocAtom f (SimpleAtom s) = SimpleAtom s
86mapDocAtom f (ComplexAtom s i d a) = ComplexAtom s i (f s i d) $ mapDocAtom f a 87mapDocAtom f (ComplexAtom s i d a) = ComplexAtom s i (f s i d) $ mapDocAtom f a
87 88
88instance IsString Doc where 89instance IsString Doc where
89 fromString = text 90 fromString = text
90 91
92
91text = DText 93text = DText
92pattern DText s = DAtom (SimpleAtom s) 94pattern DText s = DAtom (SimpleAtom s)
93 95
diff --git a/src/LambdaCube/Compiler/Utils.hs b/src/LambdaCube/Compiler/Utils.hs
index 2834497b..02a90ee7 100644
--- a/src/LambdaCube/Compiler/Utils.hs
+++ b/src/LambdaCube/Compiler/Utils.hs
@@ -29,6 +29,7 @@ dropIndex i xs = take i xs ++ drop (i+1) xs
29iterateN :: Int -> (a -> a) -> a -> a 29iterateN :: Int -> (a -> a) -> a -> a
30iterateN n f e = iterate f e !! n 30iterateN n f e = iterate f e !! n
31 31
32foldlrev :: Foldable t => (a -> b -> a) -> a -> t b -> a
32foldlrev f = foldr (flip f) 33foldlrev f = foldr (flip f)
33 34
34------------------------------------------------------- Void data type 35------------------------------------------------------- Void data type
@@ -61,6 +62,7 @@ scc key children revChildren
61 revPostOrderWalk :: Children k -> [k] -> (IS.IntSet, [k]) 62 revPostOrderWalk :: Children k -> [k] -> (IS.IntSet, [k])
62 revPostOrderWalk children = collect IS.empty [] . map Visit where 63 revPostOrderWalk children = collect IS.empty [] . map Visit where
63 64
65 collect :: IS.IntSet -> [k] -> [Task k] -> (IS.IntSet, [k])
64 collect s acc [] = (s, acc) 66 collect s acc [] = (s, acc)
65 collect s acc (Return h: t) = collect s (h: acc) t 67 collect s acc (Return h: t) = collect s (h: acc) t
66 collect s acc (Visit h: t) 68 collect s acc (Visit h: t)
@@ -70,10 +72,12 @@ scc key children revChildren
70 revMapWalk :: Children k -> IS.IntSet -> [k] -> [[k]] 72 revMapWalk :: Children k -> IS.IntSet -> [k] -> [[k]]
71 revMapWalk children = f [] 73 revMapWalk children = f []
72 where 74 where
75 f :: [[k]] -> IS.IntSet -> [k] -> [[k]]
73 f acc s [] = acc 76 f acc s [] = acc
74 f acc s (h:t) = f (c: acc) s' t 77 f acc s (h:t) = f (c: acc) s' t
75 where (s', c) = collect s [] [h] 78 where (s', c) = collect s [] [h]
76 79
80 collect :: IS.IntSet -> [k] -> [k] -> (IS.IntSet, [k])
77 collect s acc [] = (s, acc) 81 collect s acc [] = (s, acc)
78 collect s acc (h:t) 82 collect s acc (h:t)
79 | not (key h `IS.member` s) = collect s acc t 83 | not (key h `IS.member` s) = collect s acc t
@@ -84,15 +88,18 @@ scc key children revChildren
84prettyShowUnlines :: Show a => a -> String 88prettyShowUnlines :: Show a => a -> String
85prettyShowUnlines = goPP 0 . PP.ppShow 89prettyShowUnlines = goPP 0 . PP.ppShow
86 where 90 where
91 goPP :: Int -> String -> String
87 goPP _ [] = [] 92 goPP _ [] = []
88 goPP n ('"':xs) | isMultilineString xs = "\"\"\"\n" ++ indent ++ go xs where 93 goPP n ('"':xs) | isMultilineString xs = "\"\"\"\n" ++ indent ++ go xs where
89 indent = replicate n ' ' 94 indent = replicate n ' ' :: String
95 go :: String -> String
90 go ('\\':'n':xs) = "\n" ++ indent ++ go xs 96 go ('\\':'n':xs) = "\n" ++ indent ++ go xs
91 go ('\\':c:xs) = '\\':c:go xs 97 go ('\\':c:xs) = '\\':c:go xs
92 go ('"':xs) = "\n" ++ indent ++ "\"\"\"" ++ goPP n xs 98 go ('"':xs) = "\n" ++ indent ++ "\"\"\"" ++ goPP n xs
93 go (x:xs) = x : go xs 99 go (x:xs) = x : go xs
94 goPP n (x:xs) = x : goPP (if x == '\n' then 0 else n+1) xs 100 goPP n (x:xs) = x : goPP (if x == '\n' then 0 else n+1) xs
95 101
102 isMultilineString :: String -> Bool
96 isMultilineString ('\\':'n':xs) = True 103 isMultilineString ('\\':'n':xs) = True
97 isMultilineString ('\\':c:xs) = isMultilineString xs 104 isMultilineString ('\\':c:xs) = isMultilineString xs
98 isMultilineString ('"':xs) = False 105 isMultilineString ('"':xs) = False