on 254d71f help sign in
99 lines 3.0 kB history raw
1module SourceSyntax.Module where
3import Data.Binary
4import Data.List (intercalate)
5import qualified Data.Map as Map
6import Control.Applicative ((<$>), (<*>))
7import Control.Arrow (second)
9import SourceSyntax.Expression (LExpr)
10import SourceSyntax.Declaration
11import SourceSyntax.Type
12import System.FilePath (joinPath)
13import Control.Monad (liftM)
15import qualified Elm.Internal.Version as Version
17data Module tipe var =
18 Module [String] Exports Imports [Declaration tipe var]
19 deriving (Show)
21type Exports = [String]
23type Imports = [(String, ImportMethod)]
24data ImportMethod = As String | Importing [String] | Hiding [String]
25 deriving (Eq, Ord, Show)
27instance Binary ImportMethod where
28 put (As s) = do put (0 :: Word8)
29 put s
31 put (Importing ss) = do put (1 :: Word8)
32 put ss
34 put (Hiding ss) = do put (2 :: Word8)
35 put ss
37 get = do tag <- getWord8
38 case tag of
39 0 -> liftM As get
40 1 -> liftM Importing get
41 2 -> liftM Hiding get
42 _ -> error "Error reading valid ImportMethod type from serialized string"
44data MetadataModule t v = MetadataModule {
45 names :: [String],
46 path :: FilePath,
47 exports :: [String],
48 imports :: [(String, ImportMethod)],
49 program :: LExpr t v,
50 types :: Map.Map String Type,
51 fixities :: [(Assoc, Int, String)],
52 aliases :: [(String, [String], Type)],
53 datatypes :: [ (String, [String], [(String,[Type])]) ],
54 foreignImports :: [(String, LExpr t v, String, Type)],
55 foreignExports :: [(String, String, Type)]
56} deriving Show
58type Interfaces = Map.Map String ModuleInterface
59type ADT = (String, [String], [(String,[Type])])
61data ModuleInterface = ModuleInterface {
62 iVersion :: Version.Version,
63 iTypes :: Map.Map String Type,
64 iImports :: [(String, ImportMethod)],
65 iAdts :: [ADT],
66 iAliases :: [(String, [String], Type)],
67 iFixities :: [(Assoc, Int, String)]
68} deriving Show
70metaToInterface metaModule =
71 ModuleInterface
72 { iVersion = Version.elmVersion
73 , iTypes = types metaModule
74 , iImports = imports metaModule
75 , iAdts = datatypes metaModule
76 , iAliases = aliases metaModule
77 , iFixities = fixities metaModule
78 }
80instance Binary ModuleInterface where
81 get = ModuleInterface <$> get <*> get <*> get <*> get <*> get <*> get
82 put modul = do
83 put (iVersion modul)
84 put (iTypes modul)
85 put (iImports modul)
86 put (iAdts modul)
87 put (iAliases modul)
88 put (iFixities modul)
91instance Binary Assoc where
92 get = do n <- getWord8
93 return $ case n of
94 0 -> L
95 1 -> N
96 2 -> R
97 _ -> error "Error reading valid associativity from serialized string"
99 put assoc = putWord8 $ case assoc of { L -> 0 ; N -> 1 ; R -> 2 }