git.iko.soy/rowtype-yoga/purescript-native
on 908a6f8 help sign in
186 lines 7.0 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- The main compiler module
13--
14-----------------------------------------------------------------------------
16module Language.PureScript (module P, compile, compile', MonadMake(..), make) where
18import Language.PureScript.Values as P
19import Language.PureScript.Types as P
20import Language.PureScript.Kinds as P
21import Language.PureScript.Declarations as P
22import Language.PureScript.Names as P
23import Language.PureScript.Parser as P
24import Language.PureScript.CodeGen as P
25import Language.PureScript.CodeGen.Common as P
26import Language.PureScript.TypeChecker as P
27import Language.PureScript.Pretty as P
28import Language.PureScript.Sugar as P
29import Language.PureScript.Options as P
30import Language.PureScript.ModuleDependencies as P
31import Language.PureScript.Environment as P
32import Language.PureScript.DeadCodeElimination as P
34import qualified Language.PureScript.Constants as C
36import Data.List (intercalate)
37import Data.Time.Clock
38import Data.Maybe (fromMaybe, mapMaybe)
39import Control.Monad.State.Lazy
40import Control.Applicative ((<$>), (<*>), pure)
41import qualified Data.Map as M
42import System.FilePath (pathSeparator)
44-- |
45-- Compile a collection of modules
46--
47-- The compilation pipeline proceeds as follows:
48--
49-- * Sort the modules based on module dependencies, checking for cyclic dependencies.
50--
51-- * Perform a set of desugaring passes.
52--
53-- * Type check, and elaborate values to include type annotations and type class dictionaries.
54--
55-- * Regroup values to take into account new value dependencies introduced by elaboration.
56--
57-- * Eliminate dead code.
58--
59-- * Generate Javascript, and perform optimization passes.
60--
61-- * Pretty-print the generated Javascript
62--
63compile :: Options -> [Module] -> Either String (String, String, Environment)
64compile = compile' initEnvironment
66compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
67compile' env opts ms = do
68 sorted <- sortModules ms
69 desugared <- desugar sorted
70 (elaborated, env') <- runCheck' env $ forM desugared $ \(Module moduleName' decls exps) -> do
71 modify (\s -> s { checkCurrentModule = Just moduleName' })
72 Module moduleName' <$> typeCheckAll mainModuleIdent moduleName' decls <*> pure exps
73 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
74 let entryPoints = moduleNameFromString `map` optionsModules opts
75 let elim = if null entryPoints then regrouped else eliminateDeadCode entryPoints regrouped
76 let codeGenModules = moduleNameFromString `map` optionsCodeGenModules opts
77 let modulesToCodeGen = if null codeGenModules then elim else filter (\(Module mn _ _) -> mn `elem` codeGenModules) elim
78 let js = mapMaybe (flip (moduleToJs opts) env') modulesToCodeGen
79 let exts = intercalate "\n" . map (`moduleToPs` env') $ modulesToCodeGen
80 js' <- generateMain env' opts js
81 return (prettyPrintJS [wrapExportsContainer opts js'], exts, env')
82 where
83 mainModuleIdent = moduleNameFromString <$> optionsMain opts
85generateMain :: Environment -> Options -> [JS] -> Either String [JS]
86generateMain env opts js =
87 case moduleNameFromString <$> optionsMain opts of
88 Just mmi -> do
89 when ((mmi, Ident C.main) `M.notMember` names env) $
90 Left $ show mmi ++ "." ++ C.main ++ " is undefined"
91 return $ js ++ [JSApp (JSAccessor C.main (JSAccessor (moduleNameToJs mmi) (JSVar C._ps))) []]
92 _ -> return js
94-- |
95-- A type class which collects the IO actions we need to be able to run in "make" mode
96--
97class MonadMake m where
98 -- |
99 -- Get a file timestamp
100 --
101 getTimestamp :: FilePath -> m (Maybe UTCTime)
103 -- |
104 -- Read a file as a string
105 --
106 readTextFile :: FilePath -> m String
108 -- |
109 -- Write a text file
110 --
111 writeTextFile :: FilePath -> String -> m ()
113 -- |
114 -- Report an error
115 --
116 liftError :: Either String a -> m a
119-- |
120-- Compiles in "make" mode, compiling each module separately to a js files and an externs file
121--
122-- If timestamps have not changed, the externs file can be used to provide the module's types without
123-- having to typecheck the module again.
124--
125make :: (Functor m, Monad m, MonadMake m) => Options -> [(FilePath, Module)] -> m ()
126make opts ms = do
127 let filePathMap = M.fromList (map (\(fp, (Module mn _ _)) -> (mn, fp)) ms)
129 sorted <- liftError $ sortModules (map snd ms)
131 marked <- forM sorted $ \m@(Module moduleName' _ _) -> do
132 let filePath = toFileName moduleName'
134 jsFile = "js" ++ pathSeparator : filePath ++ ".js"
135 externsFile = "externs" ++ pathSeparator : filePath ++ ".externs"
136 inputFile = fromMaybe (error "Input file is undefined in make") $ M.lookup moduleName' filePathMap
138 jsTimestamp <- getTimestamp jsFile
139 externsTimestamp <- getTimestamp externsFile
140 inputTimestamp <- getTimestamp inputFile
142 case inputTimestamp < min jsTimestamp externsTimestamp of
143 True -> do
144 externs <- readTextFile externsFile
145 externsModules <- liftError . either (Left . show) Right $ P.runIndentParser externsFile P.parseModules externs
146 case externsModules of
147 [m'@(Module moduleName'' _ _)] | moduleName' == moduleName'' -> return (True, m')
148 _ -> liftError . Left $ "Externs file " ++ externsFile ++ " was invalid"
149 False -> return (False, m)
151 desugared <- liftError $ zip (map fst marked) <$> desugar (map snd marked)
153 go initEnvironment desugared
155 where
156 go :: (Functor m, Monad m, MonadMake m) => Environment -> [(Bool, Module)] -> m ()
157 go _ [] = return ()
158 go env ((True, Module moduleName' typings _) : ms') = do
159 (_, env') <- liftError . runCheck' env $ do
160 modify (\s -> s { checkCurrentModule = Just moduleName' })
161 typeCheckAll Nothing moduleName' typings
163 go env' ms'
164 go env ((False, Module moduleName' decls exps) : ms') = do
165 let filePath = toFileName moduleName'
166 jsFile = "js" ++ pathSeparator : filePath ++ ".js"
167 externsFile = "externs" ++ pathSeparator : filePath ++ ".externs"
169 (elaborated, env') <- liftError . runCheck' env $ do
170 modify (\s -> s { checkCurrentModule = Just moduleName' })
171 typeCheckAll Nothing moduleName' decls
173 regrouped <- liftError . createBindingGroups moduleName' . collapseBindingGroups $ elaborated
175 let mod' = Module moduleName' regrouped exps
176 js = moduleToJs opts mod' env'
177 exts = moduleToPs mod' env'
178 js' = maybe "" (prettyPrintJS . return . wrapExportsContainer opts . return) js
180 writeTextFile jsFile js'
181 writeTextFile externsFile exts
183 go env' ms'
185 toFileName :: ModuleName -> FilePath
186 toFileName (ModuleName ps) = intercalate [pathSeparator] . map runProperName $ ps