on a950bc5 help sign in
elk/src/AST/Module.hs
167 lines 3.9 kB history raw
1module AST.Module
2 ( Interfaces
3 , Types, Aliases, ADTs
4 , AdtInfo, CanonicalAdt
5 , SourceModule, ValidModule, CanonicalModule, Optimized
6 , Module(..), Body(..)
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.Expression.Optimized as Optimized
21import qualified AST.Type as Type
22import qualified AST.Variable as Var
23import qualified Docs.AST as Docs
24import qualified Elm.Package as Package
25import qualified Elm.Compiler.Version as Compiler
26import qualified Reporting.Annotation as A
29-- HELPFUL TYPE ALIASES
31type Interfaces = Map.Map Name Interface
33type Types = Map.Map String Type.Canonical
34type Aliases = Map.Map String ([String], Type.Canonical)
35type ADTs = Map.Map String (AdtInfo String)
37type AdtInfo v = ( [String], [(v, [Type.Canonical])] )
38type CanonicalAdt = (Var.Canonical, AdtInfo Var.Canonical)
41-- MODULES
43type SourceModule =
44 Module
45 String
46 [UserImport]
47 (Var.Listing (A.Located Var.Value))
48 [Decl.SourceDecl]
51type ValidModule =
52 Module
53 String
54 ([DefaultImport], [UserImport])
55 (Var.Listing (A.Located Var.Value))
56 [Decl.ValidDecl]
59type CanonicalModule =
60 Module Docs.Centralized [Name] [Var.Value] (Body Canonical.Expr)
63type Optimized =
64 Module Docs.Centralized [Name] [Var.Value] (Body Optimized.Expr)
67data Module docs imports exports body = Module
68 { names :: Name
69 , path :: FilePath
70 , docs :: A.Located (Maybe docs)
71 , exports :: exports
72 , imports :: imports
73 , body :: body
74 }
77data Body expr = Body
78 { program :: expr
79 , types :: Types
80 , fixities :: [(Decl.Assoc, Int, String)]
81 , aliases :: Aliases
82 , datatypes :: ADTs
83 , ports :: [String]
84 }
87-- HEADERS
89{-| Basic info needed to identify modules and determine dependencies. -}
90data Header imports = Header
91 { _names :: Name
92 , _docs :: A.Located (Maybe String)
93 , _exports :: Var.Listing (A.Located Var.Value)
94 , _imports :: imports
95 }
98type Name = [String] -- must be non-empty
101nameToString :: Name -> String
102nameToString =
103 List.intercalate "."
106nameIsNative :: Name -> Bool
107nameIsNative name =
108 case name of
109 "Native" : _ -> True
110 _ -> False
113-- IMPORTs
115type UserImport = A.Located (Name, ImportMethod)
118type DefaultImport = (Name, ImportMethod)
121data ImportMethod = ImportMethod
122 { alias :: Maybe String
123 , exposedVars :: !(Var.Listing Var.Value)
124 }
127-- INTERFACES
129{-| Key facts about a module, used when reading info from .elmi files. -}
130data Interface = Interface
131 { iVersion :: String
132 , iExports :: [Var.Value]
133 , iTypes :: Types
134 , iImports :: [Name]
135 , iAdts :: ADTs
136 , iAliases :: Aliases
137 , iFixities :: [(Decl.Assoc, Int, String)]
138 , iPorts :: [String]
139 }
142toInterface :: CanonicalModule -> Interface
143toInterface modul =
144 let body' = body modul in
145 Interface
146 { iVersion = Package.versionToString Compiler.version
147 , iExports = exports modul
148 , iTypes = types body'
149 , iImports = imports modul
150 , iAdts = datatypes body'
151 , iAliases = aliases body'
152 , iFixities = fixities body'
153 , iPorts = ports body'
154 }
157instance Binary Interface where
158 get = Interface <$> get <*> get <*> get <*> get <*> get <*> get <*> get <*> get
159 put modul = do
160 put (iVersion modul)
161 put (iExports modul)
162 put (iTypes modul)
163 put (iImports modul)
164 put (iAdts modul)
165 put (iAliases modul)
166 put (iFixities modul)
167 put (iPorts modul)