on master help sign in
Create MetadataModule for more refined information about modules

Used in most places to break a module into more orthogonal components. Some of this data needs to be serialized into the interface files.

commit8145e600fc591339bb14e14a4f76f89c23016293 authorEvan Czaplicki <info@elm-lang.org> dateTue 16 Jul 2013, 21:42 (13 years ago) parent621a9c2
3 files changed+28−14
compiler/Language/Elm.hs+5−4 compiler/SourceSyntax/Module.hs+19−7 compiler/Type/Inference.hs+4−3
compiler/Language/Elm.hs+5−4
@@ -21,15 +21,16 @@ import Parse.Module (moduleDef)
2121import SourceSyntax.Module
2222import Text.Blaze.Html (Html)
2323import Text.Parsec (option,optional)
24+import qualified Text.PrettyPrint as P
2425import Paths_Elm
2526
2627-- |This function compiles Elm code to JavaScript. It will return either
2728-- an error message or the compiled JS code.
2829compile :: String -> Either String String
29−compile source = fmap jsModule modul
30− where
31− modul :: Either String (Module () String)
32− modul = buildFromSource False source
30+compile source =
31+ case buildFromSource False source of
32+ Left docs -> Left . unlines $ map P.render docs
33+ Right modul -> Right $ jsModule (modul :: MetadataModule () ())
3334
3435-- |This function extracts the module name of a given source program.
3536moduleName :: String -> Maybe String
compiler/SourceSyntax/Module.hs+19−7
@@ -3,11 +3,16 @@ module SourceSyntax.Module where
33
44import Data.Data
55import Data.List (intercalate)
6−import qualified SourceSyntax.Declaration as Decl
6+import qualified Data.Map as Map
7+
8+import SourceSyntax.Expression
9+import SourceSyntax.Declaration
10+import SourceSyntax.Type
711import System.FilePath (joinPath)
12+import qualified Type.Type as T
813
914data Module tipe var =
10− Module [String] Exports Imports [Decl.Declaration tipe var]
15+ Module [String] Exports Imports [Declaration tipe var]
1116 deriving (Show)
1217
1318type Exports = [String]
@@ -16,8 +21,15 @@ type Imports = [(String, ImportMethod)]
1621data ImportMethod = As String | Importing [String] | Hiding [String]
1722 deriving (Eq, Ord, Show, Data, Typeable)
1823
19−name :: Module a b -> String
20−name (Module names _ _ _) = intercalate "." names
21−
22−path :: Module a b -> FilePath
23−path (Module names _ _ _) = joinPath names
24+data MetadataModule t v = MetadataModule {
25+ names :: [String],
26+ path :: FilePath,
27+ exports :: [String],
28+ imports :: [(String, ImportMethod)],
29+ defs :: [Def t v],
30+ types :: Map.Map String T.Variable,
31+ fixities :: [(Assoc, Int, String)],
32+ aliases :: [(String, [String], Type)],
33+ foreignImports :: [(String, LExpr t v, String, Type)],
34+ foreignExports :: [(String, String, Type)]
35+}
compiler/Type/Inference.hs+4−3
@@ -9,6 +9,7 @@ import qualified Type.Constrain.Declaration as TcDecl
99import qualified Type.Solve as Solve
1010
1111import SourceSyntax.Module
12+import qualified SourceSyntax.Expression as Expr
1213import Text.PrettyPrint
1314import qualified Type.State as TS
1415import Control.Monad.State
@@ -17,11 +18,11 @@ import Transform.SortDefinitions as Sort
1718import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
1819 -- the IO monad. Not sure if that'd be worthwhile.
1920
20−infer :: Module t v -> Either [Doc] (Map.Map String T.Variable)
21−infer (Module _ _ _ decls) = unsafePerformIO $ do
21+infer :: MetadataModule t v -> Either [Doc] (Map.Map String T.Variable)
22+infer modul = unsafePerformIO $ do
2223 env <- Env.initialEnvironment
2324 var <- T.flexibleVar
24− let expr = sortDefs (TcDecl.toExpr decls)
25+ let expr = Expr.dummyLet $ defs modul
2526 constraint <- TcExpr.constrain env expr (T.VarN var)
2627 (env,_,_,errors) <- execStateT (Solve.solve constraint) TS.initialState
2728 if null errors