git.iko.soy/rowtype-yoga/purescript-native
on 906e209 help sign in
79 lines 3.0 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) 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.DeadCodeElimination as P
33import Data.List (intercalate)
34import Data.Maybe (mapMaybe, fromMaybe)
35import Control.Monad (when, forM)
36import Control.Monad.State.Lazy
37import Control.Applicative ((<$>), (<|>))
38import qualified Data.Map as M
40-- |
41-- Compile a collection of modules
42--
43-- The compilation pipeline proceeds as follows:
44--
45-- * Sort the modules based on module dependencies, checking for cyclic dependencies.
46--
47-- * Perform a set of desugaring passes.
48--
49-- * Type check, and elaborate values to include type annotations and type class dictionaries.
50--
51-- * Regroup values to take into account new value dependencies introduced by elaboration.
52--
53-- * Eliminate dead code.
54--
55-- * Generate Javascript, and perform optimization passes.
56--
57-- * Pretty-print the generated Javascript
58--
59compile :: Options -> [Module] -> Either String (String, String, Environment)
60compile opts ms = do
61 sorted <- sortModules ms
62 desugared <- desugar sorted
63 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName decls) -> do
64 modify (\s -> s { checkCurrentModule = Just moduleName })
65 Module moduleName <$> typeCheckAll mainModuleIdent moduleName decls
66 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
67 let entryPoints = moduleNameFromString `map` optionsModules opts
68 let elim = if null entryPoints then regrouped else eliminateDeadCode env entryPoints regrouped
69 let js = mapMaybe (flip (moduleToJs opts) env) elim
70 let exts = intercalate "\n" . map (flip moduleToPs env) $ elim
71 js' <- case mainModuleIdent of
72 Just mmi -> do
73 when ((mmi, Ident "main") `M.notMember` (names env)) $
74 Left $ (show mmi) ++ ".main is undefined"
75 return $ js ++ [JSApp (JSAccessor "main" (JSAccessor (moduleNameToJs mmi) (JSVar "_ps"))) []]
76 _ -> return js
77 return (prettyPrintJS [(wrapExportsContainer opts js')], exts, env)
78 where
79 mainModuleIdent = moduleNameFromString <$> optionsMain opts