git.iko.soy/rowtype-yoga/purescript-native
on 1efe77b help sign in
171 lines 6.6 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 :: (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 desugared <- liftError $ do
127 sorted <- sortModules (map snd ms)
128 desugar sorted
129 go initEnvironment filePathMap desugared
131 where
132 go :: (Monad m, MonadMake m) => Environment -> M.Map ModuleName FilePath -> [Module] -> m ()
133 go _ _ [] = return ()
134 go env filePathMap (Module moduleName' decls exps : ms') = do
135 let filePath = toFileName moduleName'
136 let jsFile = "js" ++ pathSeparator : filePath ++ ".js"
137 externsFile = "externs" ++ pathSeparator : filePath ++ ".externs"
139 jsTimestamp <- getTimestamp jsFile
140 externsTimestamp <- getTimestamp externsFile
142 let inputFile = fromMaybe (error "Input file is undefined in make") $ M.lookup moduleName' filePathMap
143 inputTimestamp <- getTimestamp inputFile
145 env' <- case () of
146 _ | inputTimestamp < min jsTimestamp externsTimestamp -> do
147 externs <- readTextFile externsFile
148 externsModules <- liftError . either (Left . show) Right $ P.runIndentParser externsFile P.parseModules externs
149 case externsModules of
150 [Module moduleName'' typings _] | moduleName' == moduleName'' -> do
151 (_, env') <- liftError . runCheck' env $ do
152 modify (\s -> s { checkCurrentModule = Just moduleName' })
153 typeCheckAll Nothing moduleName' typings
154 return env'
155 _ -> liftError . Left $ "Externs file " ++ externsFile ++ " was invalid"
156 | otherwise -> do
157 (elaborated, env') <- liftError . runCheck' env $ do
158 modify (\s -> s { checkCurrentModule = Just moduleName' })
159 typeCheckAll Nothing moduleName' decls
160 regrouped <- liftError . createBindingGroups moduleName' . collapseBindingGroups $ elaborated
161 let mod' = Module moduleName' regrouped exps
162 js = moduleToJs opts mod' env'
163 exts = moduleToPs mod' env'
164 js' = maybe "" (prettyPrintJS . return . wrapExportsContainer opts . return) js
165 writeTextFile jsFile js'
166 writeTextFile externsFile exts
167 return env'
168 go env' filePathMap ms'
170 toFileName :: ModuleName -> FilePath
171 toFileName (ModuleName ps) = intercalate [pathSeparator] . map runProperName $ ps