on 4bec310 help sign in
elk/compiler/Metadata/Prelude.hs
61 lines 2.2 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Metadata.Prelude (interfaces, add) where
4import qualified Data.Map as Map
5import qualified Control.Exception as E
6import System.Exit
7import System.IO
8import SourceSyntax.Module
9import qualified Build.Interface as Interface
10import Build.Utils (getDataFile)
12add :: Bool -> Module def -> Module def
13add noPrelude (Module name exs ims decls) = Module name exs (customIms ++ ims) decls
14 where
15 customIms = if noPrelude then [] else concatMap addModule prelude
17 addModule (n, method) = case lookup n ims of
18 Nothing -> [(n, method)]
19 Just (As _) -> [(n, method)]
20 Just _ -> []
22prelude :: [(String, ImportMethod)]
23prelude = string ++ text ++ map (\n -> (n, Hiding [])) modules
24 where
25 text = map ((,) "Text") [ As "Text", Hiding ["link", "color", "height"] ]
26 string = map ((,) "String") [ As "String", Importing ["show"] ]
27 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Color"
28 , "Graphics.Element", "Graphics.Collage"
29 , "Native.Ports", "Native.Json"
30 ]
32interfaces :: Bool -> IO Interfaces
33interfaces noPrelude =
34 if noPrelude
35 then return Map.empty
36 else safeReadDocs =<< getDataFile "interfaces.data"
38safeReadDocs :: FilePath -> IO Interfaces
39safeReadDocs name =
40 E.catch (readDocs name) $ \err -> do
41 let _ = err :: IOError
42 hPutStrLn stderr $ unlines $
43 [ "Error reading types for standard library from file " ++ name
44 , " If you are using a stable version of Elm, please report an issue at"
45 , " <http://github.com/evancz/Elm/issues> specifying version numbers for"
46 , " Elm and your OS." ]
47 exitFailure
49readDocs :: FilePath -> IO Interfaces
50readDocs filePath = do
51 interfaces <- Interface.load filePath
52 case mapM (Interface.isValid filePath) (interfaces :: [(String, ModuleInterface)]) of
53 Left err -> do
54 hPutStrLn stderr err
55 exitFailure
57 Right [] -> do
58 hPutStrLn stderr "No interfaces found in serialized Prelude!"
59 exitFailure
61 Right ifaces -> return $ Map.fromList ifaces