git.iko.soy/rowtype-yoga/purescript-native
on 3fd4ea9 help sign in
70 lines 2.6 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
31import Data.List (intercalate)
32import Control.Monad (when, forM)
33import Control.Monad.State.Lazy
34import Control.Applicative ((<$>))
35import qualified Data.Map as M
37-- |
38-- Compile a collection of modules
39--
40-- The compilation pipeline proceeds as follows:
41--
42-- * Sort the modules based on module dependencies, checking for cyclic dependencies.
43--
44-- * Perform a set of desugaring passes.
45--
46-- * Type check, and elaborate values to include type annotations and type class dictionaries.
47--
48-- * Regroup values to take into account new value dependencies introduced by elaboration
49--
50-- * Generate Javascript, and perform optimization passes.
51--
52-- * Pretty-print the generated Javascript
53--
54compile :: Options -> [Module] -> Either String (String, String, Environment)
55compile opts ms = do
56 sorted <- sortModules ms
57 desugared <- desugar sorted
58 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName decls) -> do
59 modify (\s -> s { checkCurrentModule = Just (ModuleName moduleName) })
60 Module moduleName <$> typeCheckAll (ModuleName moduleName) decls
61 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
62 let js = concatMap (flip (moduleToJs opts) env) $ regrouped
63 let exts = intercalate "\n" . map (flip moduleToPs env) $ regrouped
64 js' <- case () of
65 _ | optionsRunMain opts -> do
66 when ((ModuleName (ProperName "Main"), Ident "main") `M.notMember` (names env)) $
67 Left "Main.main is undefined"
68 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
69 | otherwise -> return js
70 return (prettyPrintJS [(wrapExportsContainer js')], exts, env)