1-----------------------------------------------------------------------------
3-- Module : Language.PureScript
4-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
12-- The main compiler module
14-----------------------------------------------------------------------------
16{-# LANGUAGE DataKinds #-}
17{-# LANGUAGE FlexibleContexts #-}
18{-# LANGUAGE RecordWildCards #-}
19{-# LANGUAGE ScopedTypeVariables #-}
21module Language.PureScript
31import Data.Function (on)
32import Data.List (sortBy, groupBy)
33import Data.Maybe (fromMaybe)
35import Data.Version (Version)
36import qualified Data.Map as M
37import qualified Data.Set as S
39import Control.Applicative
40import Control.Arrow ((&&&))
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
70-- Actions that require implementations when running in "make" mode.
72data MakeActions m = MakeActions {
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.
78 getInputTimestamp :: ModuleName -> m (Either RebuildPolicy (Maybe UTCTime))
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.
84 , getOutputTimestamp :: ModuleName -> m (Maybe UTCTime)
86 -- Read the externs file for a module as a string and also return the actual
88 , readExterns :: ModuleName -> m (FilePath, String)
90 -- Run the code generator for the module and write any required output files.
92 , codegen :: CoreFn.Module CoreFn.Ann -> Environment -> SupplyVar -> Externs -> m ()
94 -- Respond to a progress update.
96 , progress :: String -> m ()
100-- Generated code for an externs file.
105-- A value to be used in the Supply monad.
107type SupplyVar = Integer
110-- Determines when to rebuild a module
113 -- | Never rebuild this module
115 -- | Always rebuild this module
116 | RebuildAlways deriving (Show, Eq, Ord)
119-- Compiles in "make" mode, compiling each module separately to a js files and an externs file
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.
124make :: forall m. (Functor m, Applicative m, Monad m, MonadReader P.Options m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
126 -> [(Either RebuildPolicy FilePath, Module)]
128make MakeActions{..} ms = do
129 (sorted, graph) <- sortModules $ map (importPrim . snd) ms
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
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
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'
158 lift $ codegen renamed env' nextVar exts
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
174 alterErrors = flip catchError $ \(MultipleErrors errs) ->
175 throwError . MultipleErrors $ flip map errs $ \e -> case e of
176 SimpleErrorWrapper (ErrorParsingModule err) -> SimpleErrorWrapper (ErrorParsingExterns err)
179reverseDependencies :: ModuleGraph -> M.Map ModuleName [ModuleName]
180reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
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)
186-- Add an import declaration for a module if it does not already explicitly import it.
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
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])
201version = Paths.version
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list