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