on bdeb36e help sign in
98 lines 2.9 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 def =
17 Module [String] Exports Imports [def]
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 =
42 MetadataModule
43 { names :: [String]
44 , path :: FilePath
45 , exports :: [String]
46 , imports :: [(String, ImportMethod)]
47 , program :: LExpr
48 , types :: Map.Map String Type
49 , fixities :: [(Assoc, Int, String)]
50 , aliases :: [Alias]
51 , datatypes :: [ADT]
52 , sendPorts :: [(String, LExpr, Type)]
53 , recvPorts :: [(String, LExpr, Type)]
54 } deriving Show
56type Interfaces = Map.Map String ModuleInterface
57type ADT = (String, [String], [(String,[Type])], [Derivation])
58type Alias = (String, [String], Type, [Derivation])
60data ModuleInterface = ModuleInterface {
61 iVersion :: Version.Version,
62 iTypes :: Map.Map String Type,
63 iImports :: [(String, ImportMethod)],
64 iAdts :: [ADT],
65 iAliases :: [Alias],
66 iFixities :: [(Assoc, Int, String)]
67} deriving Show
69metaToInterface metaModule =
70 ModuleInterface
71 { iVersion = Version.elmVersion
72 , iTypes = types metaModule
73 , iImports = imports metaModule
74 , iAdts = datatypes metaModule
75 , iAliases = aliases metaModule
76 , iFixities = fixities metaModule
77 }
79instance Binary ModuleInterface where
80 get = ModuleInterface <$> get <*> get <*> get <*> get <*> get <*> get
81 put modul = do
82 put (iVersion modul)
83 put (iTypes modul)
84 put (iImports modul)
85 put (iAdts modul)
86 put (iAliases modul)
87 put (iFixities modul)
90instance Binary Assoc where
91 get = do n <- getWord8
92 return $ case n of
93 0 -> L
94 1 -> N
95 2 -> R
96 _ -> error "Error reading valid associativity from serialized string"
98 put assoc = putWord8 $ case assoc of { L -> 0 ; N -> 1 ; R -> 2 }