summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-29 13:10:25 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-29 13:10:25 +0200
commite723bffb57fd2325542df484d0c6dc832673b1ad (patch)
treef2ac8bf440b9c5616c037575fcfef095404c312e /src
parent6005e81103cc9417b8f37a78930b467da5c46c02 (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler.hs41
-rw-r--r--src/LambdaCube/Compiler/Utils.hs47
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
39import Control.Monad.Catch 39import Control.Monad.Catch
40import Control.Exception hiding (catch, bracket, finally, mask) 40import Control.Exception hiding (catch, bracket, finally, mask)
41import Control.Arrow hiding ((<+>)) 41import Control.Arrow hiding ((<+>))
42import System.Directory
43import System.FilePath 42import System.FilePath
44import qualified Data.Text as T
45import qualified Data.Text.IO as TIO
46import qualified Text.Show.Pretty as PP
47--import Debug.Trace 43--import Debug.Trace
48 44
49import LambdaCube.IR as IR 45import LambdaCube.IR as IR
@@ -53,6 +49,7 @@ import LambdaCube.Compiler.Parser (runDefParser, parseLC, DesugarInfo, Module)
53import LambdaCube.Compiler.Infer (inference, GlobalEnv, initEnv) 49import LambdaCube.Compiler.Infer (inference, GlobalEnv, initEnv)
54import LambdaCube.Compiler.CoreToIR 50import LambdaCube.Compiler.CoreToIR
55 51
52import LambdaCube.Compiler.Utils
56import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName) 53import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName)
57import LambdaCube.Compiler.Infer as Exported (Infos, Info(..), listAllInfos, listTypeInfos, listTraceInfos, errorRange, Exp, outputType, boolType, trueExp, unfixlabel) 54import 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
64readFileStrict :: FilePath -> IO String
65readFileStrict = fmap T.unpack . TIO.readFile
66
67readFile' :: FilePath -> IO (Maybe (IO String))
68readFile' fname = do
69 b <- doesFileExist fname
70 return $ if b then Just $ readFileStrict fname else Nothing
71
72instance 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
76prettyShowUnlines :: Show a => a -> String
77prettyShowUnlines = 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
96type MName = String 61type MName = String
97type SName = String 62type SName = String
98type SourceCode = String 63type SourceCode = String
@@ -125,7 +90,7 @@ ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x)
125ioFetch paths' imp n = do 90ioFetch 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
174loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either Doc (FilePath, Module' x)) 137loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either Doc (FilePath, Module' x))
175loadModule ex imp mname_ = do 138loadModule 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 @@
4module LambdaCube.Compiler.Utils where 4module LambdaCube.Compiler.Utils where
5 5
6import qualified Data.IntSet as IS 6import qualified Data.IntSet as IS
7import qualified Data.Text as T
8import qualified Text.Show.Pretty as PP
9import Control.Monad.Catch
10import Control.Monad.Except
11import System.Directory
12import 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
13unfoldNat z s 0 = z 21unfoldNat z s 0 = z
14unfoldNat z s n | n > 0 = s $ unfoldNat z s (n-1) 22unfoldNat z s n | n > 0 = s $ unfoldNat z s (n-1)
15 23
16---------------------- 24------------------------------------------------------- Void data type
17 25
18data Void 26data Void
19 27
@@ -23,7 +31,7 @@ instance Eq Void where x == y = elimVoid x
23elimVoid :: Void -> a 31elimVoid :: Void -> a
24elimVoid v = case v of 32elimVoid 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
29newtype SData a = SData a 37newtype SData a = SData a
@@ -32,7 +40,7 @@ instance Show (SData a) where show _ = "SData"
32instance Eq (SData a) where _ == _ = True 40instance Eq (SData a) where _ == _ = True
33instance Ord (SData a) where _ `compare` _ = EQ 41instance Ord (SData a) where _ `compare` _ = EQ
34 42
35------------------------------------------------------------------------ strongly connected component calculation 43------------------------------------------------------- strongly connected component calculation
36 44
37type Children k = k -> [k] 45type 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
76prettyShowUnlines :: Show a => a -> String
77prettyShowUnlines = 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
96readFileStrict :: FilePath -> IO String
97readFileStrict = fmap T.unpack . TIO.readFile
98
99readFileIfExists :: FilePath -> IO (Maybe (IO String))
100readFileIfExists fname = do
101 b <- doesFileExist fname
102 return $ if b then Just $ readFileStrict fname else Nothing
103
104instance 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