on master help sign in
Create MetadataModule for more refined information about modules
8145e60Evan Czaplicki
compiler/Language/Elm.hs+5−4
@@ -21,15 +21,16 @@ import Parse.Module (moduleDef)
21import SourceSyntax.Module
21import SourceSyntax.Module
22import Text.Blaze.Html (Html)
22import Text.Blaze.Html (Html)
23import Text.Parsec (option,optional)
23import Text.Parsec (option,optional)
24+import qualified Text.PrettyPrint as P
24import Paths_Elm
25import Paths_Elm
25
26
26-- |This function compiles Elm code to JavaScript. It will return either
27-- |This function compiles Elm code to JavaScript. It will return either
27-- an error message or the compiled JS code.
28-- an error message or the compiled JS code.
28compile :: String -> Either String String
29compile :: String -> Either String String
29−compile source = fmap jsModule modul
30+compile source =
30− where
31− modul :: Either String (Module () String)
32− modul = buildFromSource False source
31+ case buildFromSource False source of
32+ Left docs -> Left . unlines $ map P.render docs
33+ Right modul -> Right $ jsModule (modul :: MetadataModule () ())
33
34
34-- |This function extracts the module name of a given source program.
35-- |This function extracts the module name of a given source program.
35moduleName :: String -> Maybe String
36moduleName :: String -> Maybe String
compiler/SourceSyntax/Module.hs+19−7
@@ -3,11 +3,16 @@ module SourceSyntax.Module where
3
3
4import Data.Data
4import Data.Data
5import Data.List (intercalate)
5import 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
7import System.FilePath (joinPath)
11import System.FilePath (joinPath)
12+import qualified Type.Type as T
8
13
9data Module tipe var =
14data Module tipe var =
10− Module [String] Exports Imports [Decl.Declaration tipe var]
15+ Module [String] Exports Imports [Declaration tipe var]
11 deriving (Show)
16 deriving (Show)
12
17
13type Exports = [String]
18type Exports = [String]
@@ -16,8 +21,15 @@ type Imports = [(String, ImportMethod)]
16data ImportMethod = As String | Importing [String] | Hiding [String]
21data ImportMethod = As String | Importing [String] | Hiding [String]
17 deriving (Eq, Ord, Show, Data, Typeable)
22 deriving (Eq, Ord, Show, Data, Typeable)
18
23
19−name :: Module a b -> String
24+data MetadataModule t v = MetadataModule {
20−name (Module names _ _ _) = intercalate "." names
25+ names :: [String],
21−
22−path :: Module a b -> FilePath
26+ path :: FilePath,
23−path (Module names _ _ _) = joinPath names
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
9import qualified Type.Solve as Solve
9import qualified Type.Solve as Solve
10
10
11import SourceSyntax.Module
11import SourceSyntax.Module
12+import qualified SourceSyntax.Expression as Expr
12import Text.PrettyPrint
13import Text.PrettyPrint
13import qualified Type.State as TS
14import qualified Type.State as TS
14import Control.Monad.State
15import Control.Monad.State
@@ -17,11 +18,11 @@ import Transform.SortDefinitions as Sort
17import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
18import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
18 -- the IO monad. Not sure if that'd be worthwhile.
19 -- the IO monad. Not sure if that'd be worthwhile.
19
20
20−infer :: Module t v -> Either [Doc] (Map.Map String T.Variable)
21+infer :: MetadataModule t v -> Either [Doc] (Map.Map String T.Variable)
21−infer (Module _ _ _ decls) = unsafePerformIO $ do
22+infer modul = unsafePerformIO $ do
22 env <- Env.initialEnvironment
23 env <- Env.initialEnvironment
23 var <- T.flexibleVar
24 var <- T.flexibleVar
24− let expr = sortDefs (TcDecl.toExpr decls)
25+ let expr = Expr.dummyLet $ defs modul
25 constraint <- TcExpr.constrain env expr (T.VarN var)
26 constraint <- TcExpr.constrain env expr (T.VarN var)
26 (env,_,_,errors) <- execStateT (Solve.solve constraint) TS.initialState
27 (env,_,_,errors) <- execStateT (Solve.solve constraint) TS.initialState
27 if null errors
28 if null errors