on 577fc17 help sign in
elk/compiler/Metadata/Prelude.hs
66 lines 2.4 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 AST.Module as Module
9import qualified Build.Interface as Interface
10import Build.Utils (getDataFile)
12add :: Bool -> Module exs body -> Module exs body
13add noPrelude (Module name path exs ims decls) =
14 Module name path exs (customIms ++ ims) decls
15 where
16 customIms = if noPrelude then [] else concatMap addModule prelude
18 addModule (n, method) = case lookup n ims of
19 Nothing -> [(n, method)]
20 Just (As _) -> [(n, method)]
21 Just _ -> []
23prelude :: [(String, ImportMethod)]
24prelude = text : string : map (\n -> (n, Module.open)) modules
25 where
26 string = ("String", Module.importing ["show"])
27 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Color"
28 , "Graphics.Element", "Graphics.Collage"
29 , "Native.Ports", "Native.Json"
30 ]
31 text = ("Text", Module.importing textImports)
32 textImports =
33 [ "toText", "leftAligned", "rightAligned", "centered", "justified"
34 , "plainText", "asText", "typeface", "monospace", "bold", "italic"
35 ]
37interfaces :: Bool -> IO Interfaces
38interfaces noPrelude =
39 if noPrelude
40 then return Map.empty
41 else safeReadDocs =<< getDataFile "interfaces.data"
43safeReadDocs :: FilePath -> IO Interfaces
44safeReadDocs name =
45 E.catch (readDocs name) $ \err -> do
46 let _ = err :: IOError
47 hPutStrLn stderr $ unlines $
48 [ "Error reading types for standard library from file " ++ name
49 , " If you are using a stable version of Elm, please report an issue at"
50 , " <http://github.com/elm-lang/Elm/issues> specifying version numbers for"
51 , " Elm and your OS." ]
52 exitFailure
54readDocs :: FilePath -> IO Interfaces
55readDocs filePath = do
56 interfaces <- Interface.load filePath
57 case mapM (Interface.isValid filePath) (interfaces :: [(String, Module.Interface)]) of
58 Left err -> do
59 hPutStrLn stderr err
60 exitFailure
62 Right [] -> do
63 hPutStrLn stderr "No interfaces found in serialized Prelude!"
64 exitFailure
66 Right ifaces -> return $ Map.fromList ifaces