1-----------------------------------------------------------------------------
3-- Module : Language.PureScript
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
12-- The main compiler module
14-----------------------------------------------------------------------------
16module Language.PureScript (module P, compile) 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.Maybe (mapMaybe)
38import Control.Monad.State.Lazy
39import Control.Applicative ((<$>), (<*>), pure)
40import qualified Data.Map as M
43-- Compile a collection of modules
45-- The compilation pipeline proceeds as follows:
47-- * Sort the modules based on module dependencies, checking for cyclic dependencies.
49-- * Perform a set of desugaring passes.
51-- * Type check, and elaborate values to include type annotations and type class dictionaries.
53-- * Regroup values to take into account new value dependencies introduced by elaboration.
55-- * Eliminate dead code.
57-- * Generate Javascript, and perform optimization passes.
59-- * Pretty-print the generated Javascript
61compile :: Options -> [Module] -> Either String (String, String, Environment)
63 sorted <- sortModules ms
64 desugared <- desugar sorted
65 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName' decls exps) -> do
66 modify (\s -> s { checkCurrentModule = Just moduleName' })
67 Module moduleName' <$> typeCheckAll mainModuleIdent moduleName' decls <*> pure exps
68 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
69 let entryPoints = moduleNameFromString `map` optionsModules opts
70 let elim = if null entryPoints then regrouped else eliminateDeadCode entryPoints regrouped
71 let codeGenModules = moduleNameFromString `map` optionsCodeGenModules opts
72 let modulesToCodeGen = if null codeGenModules then elim else filter (\(Module mn _ _) -> mn `elem` codeGenModules) elim
73 let js = mapMaybe (flip (moduleToJs opts) env) modulesToCodeGen
74 let exts = intercalate "\n" . map (`moduleToPs` env) $ modulesToCodeGen
75 js' <- case mainModuleIdent of
77 when ((mmi, Ident C.main) `M.notMember` names env) $
78 Left $ show mmi ++ "." ++ C.main ++ " is undefined"
79 return $ js ++ [JSApp (JSAccessor C.main (JSAccessor (moduleNameToJs mmi) (JSVar C._ps))) []]
81 return (prettyPrintJS [wrapExportsContainer opts js'], exts, env)
83 mainModuleIdent = moduleNameFromString <$> optionsMain opts
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