on 5c509f2 help sign in
96 lines 2.8 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)
14import qualified Elm.Internal.Version as Version
16data Module tipe var =
17 Module [String] Exports Imports [Declaration tipe var]
18 deriving (Show)
20type Exports = [String]
22type Imports = [(String, ImportMethod)]
23data ImportMethod = As String | Importing [String] | Hiding [String]
24 deriving (Eq, Ord, Show)
26instance Binary ImportMethod where
27 put method =
28 let put' n info = putWord8 n >> put info in
29 case method of
30 As s -> put' 0 s
31 Importing ss -> put' 1 ss
32 Hiding ss -> put' 2 ss
34 get = do tag <- getWord8
35 case tag of
36 0 -> As <$> get
37 1 -> Importing <$> get
38 2 -> Hiding <$> get
39 _ -> error "Error reading valid ImportMethod type from serialized string"
41data MetadataModule t v = MetadataModule {
42 names :: [String],
43 path :: FilePath,
44 exports :: [String],
45 imports :: [(String, ImportMethod)],
46 program :: LExpr t v,
47 types :: Map.Map String Type,
48 fixities :: [(Assoc, Int, String)],
49 aliases :: [Alias],
50 datatypes :: [ADT],
51 ports :: [(String, Type, Maybe (LExpr t v))]
52} deriving Show
54type Interfaces = Map.Map String ModuleInterface
55type ADT = (String, [String], [(String,[Type])], [Derivation])
56type Alias = (String, [String], Type, [Derivation])
58data ModuleInterface = ModuleInterface {
59 iVersion :: Version.Version,
60 iTypes :: Map.Map String Type,
61 iImports :: [(String, ImportMethod)],
62 iAdts :: [ADT],
63 iAliases :: [Alias],
64 iFixities :: [(Assoc, Int, String)]
65} deriving Show
67metaToInterface metaModule =
68 ModuleInterface
69 { iVersion = Version.elmVersion
70 , iTypes = types metaModule
71 , iImports = imports metaModule
72 , iAdts = datatypes metaModule
73 , iAliases = aliases metaModule
74 , iFixities = fixities metaModule
75 }
77instance Binary ModuleInterface where
78 get = ModuleInterface <$> get <*> get <*> get <*> get <*> get <*> get
79 put modul = do
80 put (iVersion modul)
81 put (iTypes modul)
82 put (iImports modul)
83 put (iAdts modul)
84 put (iAliases modul)
85 put (iFixities modul)
88instance Binary Assoc where
89 get = do n <- getWord8
90 return $ case n of
91 0 -> L
92 1 -> N
93 2 -> R
94 _ -> error "Error reading valid associativity from serialized string"
96 put assoc = putWord8 $ case assoc of { L -> 0 ; N -> 1 ; R -> 2 }