git.iko.soy/rowtype-yoga/purescript-native
on 0906d61 help sign in
183 lines 6.8 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, 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 opts ms = do
65 sorted <- sortModules ms
66 desugared <- desugar sorted
67 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName' decls exps) -> do
68 modify (\s -> s { checkCurrentModule = Just moduleName' })
69 Module moduleName' <$> typeCheckAll mainModuleIdent moduleName' decls <*> pure exps
70 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
71 let entryPoints = moduleNameFromString `map` optionsModules opts
72 let elim = if null entryPoints then regrouped else eliminateDeadCode entryPoints regrouped
73 let codeGenModules = moduleNameFromString `map` optionsCodeGenModules opts
74 let modulesToCodeGen = if null codeGenModules then elim else filter (\(Module mn _ _) -> mn `elem` codeGenModules) elim
75 let js = mapMaybe (flip (moduleToJs opts) env) modulesToCodeGen
76 let exts = intercalate "\n" . map (`moduleToPs` env) $ modulesToCodeGen
77 js' <- generateMain env opts js
78 return (prettyPrintJS [wrapExportsContainer opts js'], exts, env)
79 where
80 mainModuleIdent = moduleNameFromString <$> optionsMain opts
82generateMain :: Environment -> Options -> [JS] -> Either String [JS]
83generateMain env opts js =
84 case moduleNameFromString <$> optionsMain opts of
85 Just mmi -> do
86 when ((mmi, Ident C.main) `M.notMember` names env) $
87 Left $ show mmi ++ "." ++ C.main ++ " is undefined"
88 return $ js ++ [JSApp (JSAccessor C.main (JSAccessor (moduleNameToJs mmi) (JSVar C._ps))) []]
89 _ -> return js
91-- |
92-- A type class which collects the IO actions we need to be able to run in "make" mode
93--
94class MonadMake m where
95 -- |
96 -- Get a file timestamp
97 --
98 getTimestamp :: FilePath -> m (Maybe UTCTime)
100 -- |
101 -- Read a file as a string
102 --
103 readTextFile :: FilePath -> m String
105 -- |
106 -- Write a text file
107 --
108 writeTextFile :: FilePath -> String -> m ()
110 -- |
111 -- Report an error
112 --
113 liftError :: Either String a -> m a
116-- |
117-- Compiles in "make" mode, compiling each module separately to a js files and an externs file
118--
119-- If timestamps have not changed, the externs file can be used to provide the module's types without
120-- having to typecheck the module again.
121--
122make :: (Functor m, Monad m, MonadMake m) => Options -> [(FilePath, Module)] -> m ()
123make opts ms = do
124 let filePathMap = M.fromList (map (\(fp, (Module mn _ _)) -> (mn, fp)) ms)
126 sorted <- liftError $ sortModules (map snd ms)
128 marked <- forM sorted $ \m@(Module moduleName' _ _) -> do
129 let filePath = toFileName moduleName'
131 jsFile = "js" ++ pathSeparator : filePath ++ ".js"
132 externsFile = "externs" ++ pathSeparator : filePath ++ ".externs"
133 inputFile = fromMaybe (error "Input file is undefined in make") $ M.lookup moduleName' filePathMap
135 jsTimestamp <- getTimestamp jsFile
136 externsTimestamp <- getTimestamp externsFile
137 inputTimestamp <- getTimestamp inputFile
139 case inputTimestamp < min jsTimestamp externsTimestamp of
140 True -> do
141 externs <- readTextFile externsFile
142 externsModules <- liftError . either (Left . show) Right $ P.runIndentParser externsFile P.parseModules externs
143 case externsModules of
144 [m'@(Module moduleName'' _ _)] | moduleName' == moduleName'' -> return (True, m')
145 _ -> liftError . Left $ "Externs file " ++ externsFile ++ " was invalid"
146 False -> return (False, m)
148 desugared <- liftError $ zip (map fst marked) <$> desugar (map snd marked)
150 go initEnvironment desugared
152 where
153 go :: (Functor m, Monad m, MonadMake m) => Environment -> [(Bool, Module)] -> m ()
154 go _ [] = return ()
155 go env ((True, Module moduleName' typings _) : ms') = do
156 (_, env') <- liftError . runCheck' env $ do
157 modify (\s -> s { checkCurrentModule = Just moduleName' })
158 typeCheckAll Nothing moduleName' typings
160 go env' ms'
161 go env ((False, Module moduleName' decls exps) : ms') = do
162 let filePath = toFileName moduleName'
163 jsFile = "js" ++ pathSeparator : filePath ++ ".js"
164 externsFile = "externs" ++ pathSeparator : filePath ++ ".externs"
166 (elaborated, env') <- liftError . runCheck' env $ do
167 modify (\s -> s { checkCurrentModule = Just moduleName' })
168 typeCheckAll Nothing moduleName' decls
170 regrouped <- liftError . createBindingGroups moduleName' . collapseBindingGroups $ elaborated
172 let mod' = Module moduleName' regrouped exps
173 js = moduleToJs opts mod' env'
174 exts = moduleToPs mod' env'
175 js' = maybe "" (prettyPrintJS . return . wrapExportsContainer opts . return) js
177 writeTextFile jsFile js'
178 writeTextFile externsFile exts
180 go env' ms'
182 toFileName :: ModuleName -> FilePath
183 toFileName (ModuleName ps) = intercalate [pathSeparator] . map runProperName $ ps