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