git.iko.soy/rowtype-yoga/purescript-native
on c92c7a6 help sign in
201 lines 7.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript
4-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
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-----------------------------------------------------------------------------
16{-# LANGUAGE DataKinds #-}
17{-# LANGUAGE FlexibleContexts #-}
18{-# LANGUAGE RecordWildCards #-}
19{-# LANGUAGE ScopedTypeVariables #-}
21module Language.PureScript
22 ( module P
23 , RebuildPolicy(..)
24 , MakeActions(..)
25 , SupplyVar()
26 , Externs()
27 , make
28 , version
29 ) where
31import Data.Function (on)
32import Data.List (sortBy, groupBy)
33import Data.Maybe (fromMaybe)
34import Data.Time.Clock
35import Data.Version (Version)
36import qualified Data.Map as M
37import qualified Data.Set as S
39import Control.Applicative
40import Control.Arrow ((&&&))
41import Control.Monad
42import Control.Monad.Error.Class (MonadError(..))
43import Control.Monad.Reader
44import Control.Monad.Writer
45import Control.Monad.Supply.Class (fresh)
47import Language.PureScript.AST as P
48import Language.PureScript.Comments as P
49import Language.PureScript.CodeGen.Externs (moduleToPs)
50import Language.PureScript.Environment as P
51import Language.PureScript.Errors as P
52import Language.PureScript.Kinds as P
53import Language.PureScript.Linter as P
54import Language.PureScript.ModuleDependencies as P
55import Language.PureScript.Names as P
56import Language.PureScript.Options as P
57import Language.PureScript.Parser as P
58import Language.PureScript.Pretty as P
59import Language.PureScript.Renamer as P
60import Language.PureScript.Sugar as P
61import Control.Monad.Supply as P
62import Language.PureScript.TypeChecker as P
63import Language.PureScript.Types as P
64import qualified Language.PureScript.CoreFn as CoreFn
65import qualified Language.PureScript.Constants as C
67import qualified Paths_purescript as Paths
69-- |
70-- Actions that require implementations when running in "make" mode.
71--
72data MakeActions m = MakeActions {
73 -- |
74 -- Get the timestamp for the input file(s) for a module. If there are multiple
75 -- files (.purs and foreign files, for example) the timestamp should be for
76 -- the most recently modified file.
77 --
78 getInputTimestamp :: ModuleName -> m (Either RebuildPolicy (Maybe UTCTime))
79 -- |
80 -- Get the timestamp for the output files for a module. This should be the
81 -- timestamp for the oldest modified file, or Nothing if any of the required
82 -- output files are missing.
83 --
84 , getOutputTimestamp :: ModuleName -> m (Maybe UTCTime)
85 -- |
86 -- Read the externs file for a module as a string and also return the actual
87 -- path for the file.
88 , readExterns :: ModuleName -> m (FilePath, String)
89 -- |
90 -- Run the code generator for the module and write any required output files.
91 --
92 , codegen :: CoreFn.Module CoreFn.Ann -> Environment -> SupplyVar -> Externs -> m ()
93 -- |
94 -- Respond to a progress update.
95 --
96 , progress :: String -> m ()
97 }
99-- |
100-- Generated code for an externs file.
101--
102type Externs = String
104-- |
105-- A value to be used in the Supply monad.
106--
107type SupplyVar = Integer
109-- |
110-- Determines when to rebuild a module
111--
112data RebuildPolicy
113 -- | Never rebuild this module
114 = RebuildNever
115 -- | Always rebuild this module
116 | RebuildAlways deriving (Show, Eq, Ord)
118-- |
119-- Compiles in "make" mode, compiling each module separately to a js files and an externs file
120--
121-- If timestamps have not changed, the externs file can be used to provide the module's types without
122-- having to typecheck the module again.
123--
124make :: forall m. (Functor m, Applicative m, Monad m, MonadReader P.Options m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
125 => MakeActions m
126 -> [(Either RebuildPolicy FilePath, Module)]
127 -> m Environment
128make MakeActions{..} ms = do
129 (sorted, graph) <- sortModules $ map (importPrim . snd) ms
130 mapM_ lint sorted
131 toRebuild <- foldM (\s (Module _ moduleName' _ _) -> do
132 inputTimestamp <- getInputTimestamp moduleName'
133 outputTimestamp <- getOutputTimestamp moduleName'
134 return $ case (inputTimestamp, outputTimestamp) of
135 (Right (Just t1), Just t2) | t1 < t2 -> s
136 (Left RebuildNever, Just _) -> s
137 _ -> S.insert moduleName' s) S.empty sorted
139 marked <- rebuildIfNecessary (reverseDependencies graph) toRebuild sorted
140 (desugared, nextVar) <- runSupplyT 0 $ zip (map fst marked) <$> desugar (map snd marked)
141 evalSupplyT nextVar $ go initEnvironment desugared
142 where
144 go :: Environment -> [(Bool, Module)] -> SupplyT m Environment
145 go env [] = return env
146 go env ((False, m) : ms') = do
147 (_, env') <- lift . runCheck' env $ typeCheckModule Nothing m
148 go env' ms'
149 go env ((True, m@(Module coms moduleName' _ exps)) : ms') = do
150 lift $ progress $ "Compiling " ++ runModuleName moduleName'
151 (Module _ _ elaborated _, env') <- lift . runCheck' env $ typeCheckModule Nothing m
152 regrouped <- createBindingGroups moduleName' . collapseBindingGroups $ elaborated
153 let mod' = Module coms moduleName' regrouped exps
154 corefn = CoreFn.moduleToCoreFn env' mod'
155 [renamed] = renameInModules [corefn]
156 exts = moduleToPs mod' env'
157 nextVar <- fresh
158 lift $ codegen renamed env' nextVar exts
159 go env' ms'
161 rebuildIfNecessary :: M.Map ModuleName [ModuleName] -> S.Set ModuleName -> [Module] -> m [(Bool, Module)]
162 rebuildIfNecessary _ _ [] = return []
163 rebuildIfNecessary graph toRebuild (m@(Module _ moduleName' _ _) : ms') | moduleName' `S.member` toRebuild = do
164 let deps = fromMaybe [] $ moduleName' `M.lookup` graph
165 toRebuild' = toRebuild `S.union` S.fromList deps
166 (:) (True, m) <$> rebuildIfNecessary graph toRebuild' ms'
167 rebuildIfNecessary graph toRebuild (Module _ moduleName' _ _ : ms') = do
168 (path, externs) <- readExterns moduleName'
169 externsModules <- fmap (map snd) . alterErrors $ P.parseModulesFromFiles id [(path, externs)]
170 case externsModules of
171 [m'@(Module _ moduleName'' _ _)] | moduleName'' == moduleName' -> (:) (False, m') <$> rebuildIfNecessary graph toRebuild ms'
172 _ -> throwError . errorMessage . InvalidExternsFile $ path
173 where
174 alterErrors = flip catchError $ \(MultipleErrors errs) ->
175 throwError . MultipleErrors $ flip map errs $ \e -> case e of
176 SimpleErrorWrapper (ErrorParsingModule err) -> SimpleErrorWrapper (ErrorParsingExterns err)
177 _ -> e
179reverseDependencies :: ModuleGraph -> M.Map ModuleName [ModuleName]
180reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
181 where
182 combine :: (Ord a) => [(a, b)] -> M.Map a [b]
183 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
185-- |
186-- Add an import declaration for a module if it does not already explicitly import it.
187--
188addDefaultImport :: ModuleName -> Module -> Module
189addDefaultImport toImport m@(Module coms mn decls exps) =
190 if isExistingImport `any` decls || mn == toImport then m
191 else Module coms mn (ImportDeclaration toImport Implicit Nothing : decls) exps
192 where
193 isExistingImport (ImportDeclaration mn' _ _) | mn' == toImport = True
194 isExistingImport (PositionedDeclaration _ _ d) = isExistingImport d
195 isExistingImport _ = False
197importPrim :: Module -> Module
198importPrim = addDefaultImport (ModuleName [ProperName C.prim])
200version :: Version
201version = Paths.version