diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-29 13:10:25 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-29 13:10:25 +0200 |
commit | e723bffb57fd2325542df484d0c6dc832673b1ad (patch) | |
tree | f2ac8bf440b9c5616c037575fcfef095404c312e /src | |
parent | 6005e81103cc9417b8f37a78930b467da5c46c02 (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler.hs | 41 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Utils.hs | 47 |
2 files changed, 46 insertions, 42 deletions
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs index 31a5d4a2..ecb33167 100644 --- a/src/LambdaCube/Compiler.hs +++ b/src/LambdaCube/Compiler.hs | |||
@@ -39,11 +39,7 @@ import Control.DeepSeq | |||
39 | import Control.Monad.Catch | 39 | import Control.Monad.Catch |
40 | import Control.Exception hiding (catch, bracket, finally, mask) | 40 | import Control.Exception hiding (catch, bracket, finally, mask) |
41 | import Control.Arrow hiding ((<+>)) | 41 | import Control.Arrow hiding ((<+>)) |
42 | import System.Directory | ||
43 | import System.FilePath | 42 | import System.FilePath |
44 | import qualified Data.Text as T | ||
45 | import qualified Data.Text.IO as TIO | ||
46 | import qualified Text.Show.Pretty as PP | ||
47 | --import Debug.Trace | 43 | --import Debug.Trace |
48 | 44 | ||
49 | import LambdaCube.IR as IR | 45 | import LambdaCube.IR as IR |
@@ -53,6 +49,7 @@ import LambdaCube.Compiler.Parser (runDefParser, parseLC, DesugarInfo, Module) | |||
53 | import LambdaCube.Compiler.Infer (inference, GlobalEnv, initEnv) | 49 | import LambdaCube.Compiler.Infer (inference, GlobalEnv, initEnv) |
54 | import LambdaCube.Compiler.CoreToIR | 50 | import LambdaCube.Compiler.CoreToIR |
55 | 51 | ||
52 | import LambdaCube.Compiler.Utils | ||
56 | import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName) | 53 | import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName) |
57 | import LambdaCube.Compiler.Infer as Exported (Infos, Info(..), listAllInfos, listTypeInfos, listTraceInfos, errorRange, Exp, outputType, boolType, trueExp, unfixlabel) | 54 | import LambdaCube.Compiler.Infer as Exported (Infos, Info(..), listAllInfos, listTypeInfos, listTraceInfos, errorRange, Exp, outputType, boolType, trueExp, unfixlabel) |
58 | 55 | ||
@@ -61,38 +58,6 @@ import Paths_lambdacube_compiler (getDataDir) | |||
61 | 58 | ||
62 | -------------------------------------------------------------------------------- | 59 | -------------------------------------------------------------------------------- |
63 | 60 | ||
64 | readFileStrict :: FilePath -> IO String | ||
65 | readFileStrict = fmap T.unpack . TIO.readFile | ||
66 | |||
67 | readFile' :: FilePath -> IO (Maybe (IO String)) | ||
68 | readFile' fname = do | ||
69 | b <- doesFileExist fname | ||
70 | return $ if b then Just $ readFileStrict fname else Nothing | ||
71 | |||
72 | instance MonadMask m => MonadMask (ExceptT e m) where | ||
73 | mask f = ExceptT $ mask $ \u -> runExceptT $ f (mapExceptT u) | ||
74 | uninterruptibleMask = error "not implemented: uninterruptibleMask for ExcpetT" | ||
75 | |||
76 | prettyShowUnlines :: Show a => a -> String | ||
77 | prettyShowUnlines = goPP 0 . PP.ppShow | ||
78 | where | ||
79 | goPP _ [] = [] | ||
80 | goPP n ('"':xs) | isMultilineString xs = "\"\"\"\n" ++ indent ++ go xs where | ||
81 | indent = replicate n ' ' | ||
82 | go ('\\':'n':xs) = "\n" ++ indent ++ go xs | ||
83 | go ('\\':c:xs) = '\\':c:go xs | ||
84 | go ('"':xs) = "\n" ++ indent ++ "\"\"\"" ++ goPP n xs | ||
85 | go (x:xs) = x : go xs | ||
86 | goPP n (x:xs) = x : goPP (if x == '\n' then 0 else n+1) xs | ||
87 | |||
88 | isMultilineString ('\\':'n':xs) = True | ||
89 | isMultilineString ('\\':c:xs) = isMultilineString xs | ||
90 | isMultilineString ('"':xs) = False | ||
91 | isMultilineString (x:xs) = isMultilineString xs | ||
92 | isMultilineString [] = False | ||
93 | |||
94 | -------------------------------------------------------------------------------- | ||
95 | |||
96 | type MName = String | 61 | type MName = String |
97 | type SName = String | 62 | type SName = String |
98 | type SourceCode = String | 63 | type SourceCode = String |
@@ -125,7 +90,7 @@ ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x) | |||
125 | ioFetch paths' imp n = do | 90 | ioFetch paths' imp n = do |
126 | preludePath <- (</> "lc") <$> liftIO getDataDir | 91 | preludePath <- (</> "lc") <$> liftIO getDataDir |
127 | let paths = paths' ++ [preludePath] | 92 | let paths = paths' ++ [preludePath] |
128 | find ((x, mn): xs) = liftIO (readFile' x) >>= maybe (find xs) (\src -> return $ Right (x, mn, liftIO src)) | 93 | find ((x, mn): xs) = liftIO (readFileIfExists x) >>= maybe (find xs) (\src -> return $ Right (x, mn, liftIO src)) |
129 | find [] = return $ Left $ "can't find" <+> either (("lc file" <+>) . text) (("module" <+>) . text) n | 94 | find [] = return $ Left $ "can't find" <+> either (("lc file" <+>) . text) (("module" <+>) . text) n |
130 | <+> "in path" <+> hsep (map text (paths' ++ ["<<installed-prelude-path>>"]{-todo-})) | 95 | <+> "in path" <+> hsep (map text (paths' ++ ["<<installed-prelude-path>>"]{-todo-})) |
131 | find $ nubBy ((==) `on` fst) $ map (first normalise . lcModuleFile) paths | 96 | find $ nubBy ((==) `on` fst) $ map (first normalise . lcModuleFile) paths |
@@ -169,8 +134,6 @@ data Modules x = Modules | |||
169 | , nextMId :: !Int | 134 | , nextMId :: !Int |
170 | } | 135 | } |
171 | 136 | ||
172 | (<&>) = flip (<$>) | ||
173 | |||
174 | loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either Doc (FilePath, Module' x)) | 137 | loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either Doc (FilePath, Module' x)) |
175 | loadModule ex imp mname_ = do | 138 | loadModule ex imp mname_ = do |
176 | r <- ask >>= \fetch -> fetch imp mname_ | 139 | r <- ask >>= \fetch -> fetch imp mname_ |
diff --git a/src/LambdaCube/Compiler/Utils.hs b/src/LambdaCube/Compiler/Utils.hs index 71e4081b..f59f7a7b 100644 --- a/src/LambdaCube/Compiler/Utils.hs +++ b/src/LambdaCube/Compiler/Utils.hs | |||
@@ -4,6 +4,14 @@ | |||
4 | module LambdaCube.Compiler.Utils where | 4 | module LambdaCube.Compiler.Utils where |
5 | 5 | ||
6 | import qualified Data.IntSet as IS | 6 | import qualified Data.IntSet as IS |
7 | import qualified Data.Text as T | ||
8 | import qualified Text.Show.Pretty as PP | ||
9 | import Control.Monad.Catch | ||
10 | import Control.Monad.Except | ||
11 | import System.Directory | ||
12 | import qualified Data.Text.IO as TIO | ||
13 | |||
14 | ------------------------------------------------------- general functions | ||
7 | 15 | ||
8 | (<&>) = flip (<$>) | 16 | (<&>) = flip (<$>) |
9 | 17 | ||
@@ -13,7 +21,7 @@ iterateN n f e = iterate f e !! n | |||
13 | unfoldNat z s 0 = z | 21 | unfoldNat z s 0 = z |
14 | unfoldNat z s n | n > 0 = s $ unfoldNat z s (n-1) | 22 | unfoldNat z s n | n > 0 = s $ unfoldNat z s (n-1) |
15 | 23 | ||
16 | ---------------------- | 24 | ------------------------------------------------------- Void data type |
17 | 25 | ||
18 | data Void | 26 | data Void |
19 | 27 | ||
@@ -23,7 +31,7 @@ instance Eq Void where x == y = elimVoid x | |||
23 | elimVoid :: Void -> a | 31 | elimVoid :: Void -> a |
24 | elimVoid v = case v of | 32 | elimVoid v = case v of |
25 | 33 | ||
26 | ---------------------- | 34 | ------------------------------------------------------- supplementary data wrapper |
27 | 35 | ||
28 | -- supplementary data: data with no semantic relevance | 36 | -- supplementary data: data with no semantic relevance |
29 | newtype SData a = SData a | 37 | newtype SData a = SData a |
@@ -32,7 +40,7 @@ instance Show (SData a) where show _ = "SData" | |||
32 | instance Eq (SData a) where _ == _ = True | 40 | instance Eq (SData a) where _ == _ = True |
33 | instance Ord (SData a) where _ `compare` _ = EQ | 41 | instance Ord (SData a) where _ `compare` _ = EQ |
34 | 42 | ||
35 | ------------------------------------------------------------------------ strongly connected component calculation | 43 | ------------------------------------------------------- strongly connected component calculation |
36 | 44 | ||
37 | type Children k = k -> [k] | 45 | type Children k = k -> [k] |
38 | 46 | ||
@@ -63,5 +71,38 @@ scc key children revChildren | |||
63 | | not (key h `IS.member` s) = collect s acc t | 71 | | not (key h `IS.member` s) = collect s acc t |
64 | | otherwise = collect (IS.delete (key h) s) (h: acc) (children h ++ t) | 72 | | otherwise = collect (IS.delete (key h) s) (h: acc) (children h ++ t) |
65 | 73 | ||
74 | ------------------------------------------------------- wrapped pretty show | ||
75 | |||
76 | prettyShowUnlines :: Show a => a -> String | ||
77 | prettyShowUnlines = goPP 0 . PP.ppShow | ||
78 | where | ||
79 | goPP _ [] = [] | ||
80 | goPP n ('"':xs) | isMultilineString xs = "\"\"\"\n" ++ indent ++ go xs where | ||
81 | indent = replicate n ' ' | ||
82 | go ('\\':'n':xs) = "\n" ++ indent ++ go xs | ||
83 | go ('\\':c:xs) = '\\':c:go xs | ||
84 | go ('"':xs) = "\n" ++ indent ++ "\"\"\"" ++ goPP n xs | ||
85 | go (x:xs) = x : go xs | ||
86 | goPP n (x:xs) = x : goPP (if x == '\n' then 0 else n+1) xs | ||
87 | |||
88 | isMultilineString ('\\':'n':xs) = True | ||
89 | isMultilineString ('\\':c:xs) = isMultilineString xs | ||
90 | isMultilineString ('"':xs) = False | ||
91 | isMultilineString (x:xs) = isMultilineString xs | ||
92 | isMultilineString [] = False | ||
93 | |||
94 | ------------------------------------------------------- file handling | ||
95 | |||
96 | readFileStrict :: FilePath -> IO String | ||
97 | readFileStrict = fmap T.unpack . TIO.readFile | ||
98 | |||
99 | readFileIfExists :: FilePath -> IO (Maybe (IO String)) | ||
100 | readFileIfExists fname = do | ||
101 | b <- doesFileExist fname | ||
102 | return $ if b then Just $ readFileStrict fname else Nothing | ||
103 | |||
104 | instance MonadMask m => MonadMask (ExceptT e m) where | ||
105 | mask f = ExceptT $ mask $ \u -> runExceptT $ f (mapExceptT u) | ||
106 | uninterruptibleMask = error "not implemented: uninterruptibleMask for ExcpetT" | ||
66 | 107 | ||
67 | 108 | ||