on 499e6d9 help sign in
elk/src/AST/Module.hs
176 lines 4.1 kB history raw
1module AST.Module
2 ( Interfaces, CanonicalInterfaces
3 , Types, Aliases, ADTs
4 , AdtInfo, CanonicalAdt
5 , SourceModule, ValidModule, CanonicalModule, Optimized
6 , Module(..), Body(..)
7 , Header(..)
8 , Name, nameToString, nameIsNative
9 , CanonicalName(..), canonPkg, canonModul
10 , Interface(..), toInterface
11 , UserImport, DefaultImport, ImportMethod(..)
12 ) where
14import Control.Applicative ((<$>),(<*>))
15import Data.Binary
16import qualified Data.List as List
17import qualified Data.Map as Map
19import qualified AST.Declaration as Decl
20import qualified AST.Expression.Canonical as Canonical
21import qualified AST.Expression.Optimized as Optimized
22import qualified AST.Type as Type
23import qualified AST.Variable as Var
24import qualified Docs.AST as Docs
25import qualified Elm.Package as Package
26import qualified Elm.Compiler.Version as Compiler
27import qualified Reporting.Annotation as A
30-- HELPFUL TYPE ALIASES
32type Interfaces = Map.Map Name Interface
33type CanonicalInterfaces = Map.Map CanonicalName Interface
35type Types = Map.Map String Type.Canonical
36type Aliases = Map.Map String ([String], Type.Canonical)
37type ADTs = Map.Map String (AdtInfo String)
39type AdtInfo v = ( [String], [(v, [Type.Canonical])] )
40type CanonicalAdt = (Var.Canonical, AdtInfo Var.Canonical)
43-- MODULES
45type SourceModule =
46 Module
47 String
48 [UserImport]
49 (Var.Listing (A.Located Var.Value))
50 [Decl.SourceDecl]
53type ValidModule =
54 Module
55 String
56 ([DefaultImport], [UserImport])
57 (Var.Listing (A.Located Var.Value))
58 [Decl.ValidDecl]
61type CanonicalModule =
62 Module Docs.Centralized [Name] [Var.Value] (Body Canonical.Expr)
65type Optimized =
66 Module Docs.Centralized [Name] [Var.Value] (Body Optimized.Expr)
69data Module docs imports exports body = Module
70 { names :: Name
71 , path :: FilePath
72 , docs :: A.Located (Maybe docs)
73 , exports :: exports
74 , imports :: imports
75 , body :: body
76 }
79data Body expr = Body
80 { program :: expr
81 , types :: Types
82 , fixities :: [(Decl.Assoc, Int, String)]
83 , aliases :: Aliases
84 , datatypes :: ADTs
85 , ports :: [String]
86 }
89-- HEADERS
91{-| Basic info needed to identify modules and determine dependencies. -}
92data Header imports = Header
93 { _names :: Name
94 , _docs :: A.Located (Maybe String)
95 , _exports :: Var.Listing (A.Located Var.Value)
96 , _imports :: imports
97 }
100type Name = [String] -- must be non-empty
103data CanonicalName =
104 CanonicalName
105 { canonPkg :: Package.Name
106 , canonModul :: Name
107 }
110nameToString :: Name -> String
111nameToString =
112 List.intercalate "."
115nameIsNative :: Name -> Bool
116nameIsNative name =
117 case name of
118 "Native" : _ -> True
119 _ -> False
122-- IMPORTs
124type UserImport = A.Located (Name, ImportMethod)
127type DefaultImport = (Name, ImportMethod)
130data ImportMethod = ImportMethod
131 { alias :: Maybe String
132 , exposedVars :: !(Var.Listing Var.Value)
133 }
136-- INTERFACES
138{-| Key facts about a module, used when reading info from .elmi files. -}
139data Interface = Interface
140 { iVersion :: String
141 , iExports :: [Var.Value]
142 , iTypes :: Types
143 , iImports :: [Name]
144 , iAdts :: ADTs
145 , iAliases :: Aliases
146 , iFixities :: [(Decl.Assoc, Int, String)]
147 , iPorts :: [String]
148 }
151toInterface :: CanonicalModule -> Interface
152toInterface modul =
153 let body' = body modul in
154 Interface
155 { iVersion = Package.versionToString Compiler.version
156 , iExports = exports modul
157 , iTypes = types body'
158 , iImports = imports modul
159 , iAdts = datatypes body'
160 , iAliases = aliases body'
161 , iFixities = fixities body'
162 , iPorts = ports body'
163 }
166instance Binary Interface where
167 get = Interface <$> get <*> get <*> get <*> get <*> get <*> get <*> get <*> get
168 put modul = do
169 put (iVersion modul)
170 put (iExports modul)
171 put (iTypes modul)
172 put (iImports modul)
173 put (iAdts modul)
174 put (iAliases modul)
175 put (iFixities modul)
176 put (iPorts modul)