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