on e0e0b2e help sign in
elk/src/AST/Module.hs
153 lines 3.5 kB history raw
1module AST.Module
2 ( Interfaces
3 , Types, Aliases, ADTs
4 , AdtInfo, CanonicalAdt
5 , SourceModule, ValidModule, CanonicalModule
6 , Module(..), CanonicalBody(..)
7 , Header(..)
8 , Name, nameToString, nameIsNative
9 , Interface(..), toInterface
10 , UserImport, DefaultImport, ImportMethod(..)
11 ) where
13import Control.Applicative ((<$>),(<*>))
14import Data.Binary
15import qualified Data.List as List
16import qualified Data.Map as Map
18import qualified AST.Declaration as Decl
19import qualified AST.Expression.Canonical as Canonical
20import qualified AST.Type as Type
21import qualified AST.Variable as Var
22import qualified Elm.Compiler.Version as Compiler
23import qualified Reporting.Annotation as A
26-- HELPFUL TYPE ALIASES
28type Interfaces = Map.Map Name Interface
30type Types = Map.Map String Type.Canonical
31type Aliases = Map.Map String ([String], Type.Canonical)
32type ADTs = Map.Map String (AdtInfo String)
34type AdtInfo v = ( [String], [(v, [Type.Canonical])] )
35type CanonicalAdt = (Var.Canonical, AdtInfo Var.Canonical)
38-- MODULES
40type SourceModule =
41 Module
42 [UserImport]
43 (Var.Listing (A.Located Var.Value))
44 [Decl.SourceDecl]
47type ValidModule =
48 Module
49 ([DefaultImport], [UserImport])
50 (Var.Listing (A.Located Var.Value))
51 [Decl.ValidDecl]
54type CanonicalModule =
55 Module [Name] [Var.Value] CanonicalBody
58data Module imports exports body = Module
59 { names :: Name
60 , path :: FilePath
61 , exports :: exports
62 , imports :: imports
63 , body :: body
64 }
66data CanonicalBody = CanonicalBody
67 { program :: Canonical.Expr
68 , types :: Types
69 , fixities :: [(Decl.Assoc, Int, String)]
70 , aliases :: Aliases
71 , datatypes :: ADTs
72 , ports :: [String]
73 }
76-- HEADERS
78{-| Basic info needed to identify modules and determine dependencies. -}
79data Header imports = Header
80 { _names :: Name
81 , _exports :: Var.Listing (A.Located Var.Value)
82 , _imports :: imports
83 }
86type Name = [String] -- must be non-empty
89nameToString :: Name -> String
90nameToString =
91 List.intercalate "."
94nameIsNative :: Name -> Bool
95nameIsNative name =
96 case name of
97 "Native" : _ -> True
98 _ -> False
101-- IMPORTs
103type UserImport = A.Located (Name, ImportMethod)
106type DefaultImport = (Name, ImportMethod)
109data ImportMethod = ImportMethod
110 { alias :: Maybe String
111 , exposedVars :: !(Var.Listing Var.Value)
112 }
115-- INTERFACES
117{-| Key facts about a module, used when reading info from .elmi files. -}
118data Interface = Interface
119 { iVersion :: String
120 , iExports :: [Var.Value]
121 , iTypes :: Types
122 , iImports :: [Name]
123 , iAdts :: ADTs
124 , iAliases :: Aliases
125 , iFixities :: [(Decl.Assoc, Int, String)]
126 , iPorts :: [String]
127 }
129toInterface :: CanonicalModule -> Interface
130toInterface modul =
131 let body' = body modul in
132 Interface
133 { iVersion = Compiler.version
134 , iExports = exports modul
135 , iTypes = types body'
136 , iImports = imports modul
137 , iAdts = datatypes body'
138 , iAliases = aliases body'
139 , iFixities = fixities body'
140 , iPorts = ports body'
141 }
143instance Binary Interface where
144 get = Interface <$> get <*> get <*> get <*> get <*> get <*> get <*> get <*> get
145 put modul = do
146 put (iVersion modul)
147 put (iExports modul)
148 put (iTypes modul)
149 put (iImports modul)
150 put (iAdts modul)
151 put (iAliases modul)
152 put (iFixities modul)
153 put (iPorts modul)