git.iko.soy/rowtype-yoga/purescript-native
on 05dde49 help sign in
74 lines 2.3 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Main
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--
13-----------------------------------------------------------------------------
15module Main where
17import Language.PureScript
18import Data.Maybe (mapMaybe)
19import Data.List (intercalate)
20import System.Console.CmdTheLine
21import Control.Applicative
22import Control.Monad (forM)
23import System.Exit (exitSuccess, exitFailure)
24import qualified Text.Parsec as P
25import qualified System.IO.UTF8 as U
26import qualified Data.Map as M
28compile :: [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
29compile inputFiles outputFile externsFile = do
30 asts <- fmap (fmap concat . sequence) $ forM inputFiles $ \inputFile -> do
31 text <- U.readFile inputFile
32 return $ runIndentParser parseDeclarations text
33 case asts of
34 Left err -> do
35 U.print err
36 exitFailure
37 Right decls ->
38 case check (typeCheckAll decls) of
39 Left typeError -> do
40 U.putStrLn typeError
41 exitFailure
42 Right (_, env) -> do
43 let js = intercalate "; " . map (prettyPrintJS . optimize) . concat . mapMaybe (declToJs) $ decls
44 case outputFile of
45 Just path -> U.writeFile path js
46 Nothing -> U.putStrLn js
47 case externsFile of
48 Nothing -> return ()
49 Just filePath -> U.writeFile filePath $ intercalate "\n" $ mapMaybe (externToPs env) decls
50 exitSuccess
52inputFiles :: Term [FilePath]
53inputFiles = nonEmpty $ posAny [] $ posInfo
54 { posDoc = "The input .ps files" }
56outputFile :: Term (Maybe FilePath)
57outputFile = value $ opt Nothing $ (optInfo [ "o", "output" ])
58 { optDoc = "The output .js file" }
60externsFile :: Term (Maybe FilePath)
61externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
62 { optDoc = "The output .e.ps file" }
64term :: Term (IO ())
65term = compile <$> inputFiles <*> outputFile <*> externsFile
67termInfo :: TermInfo
68termInfo = defTI
69 { termName = "psc"
70 , version = "1.0"
71 , termDoc = "Compiles PureScript to Javascript"
72 }
74main = run (term, termInfo)