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