git.iko.soy/rowtype-yoga/purescript-native
on 446c911 help sign in
72 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 input <- fmap concat $ mapM U.readFile inputFiles
30 let ast = runIndentParser 0 parseDeclarations input
31 case ast of
32 Left err -> do
33 U.print err
34 exitFailure
35 Right decls -> do
36 case check (typeCheckAll decls) of
37 Left typeError -> do
38 U.putStrLn typeError
39 exitFailure
40 Right (_, env) -> do
41 let js = intercalate "\n" $ mapMaybe declToJs decls
42 case outputFile of
43 Just path -> U.writeFile path js
44 Nothing -> U.putStrLn js
45 case externsFile of
46 Nothing -> return ()
47 Just filePath -> U.writeFile filePath $ intercalate "\n" $ map (uncurry externToPs) $ M.toList $ names env
48 exitSuccess
50inputFiles :: Term [FilePath]
51inputFiles = nonEmpty $ posAny [] $ posInfo
52 { posDoc = "The input .ps files" }
54outputFile :: Term (Maybe FilePath)
55outputFile = value $ opt Nothing $ (optInfo [ "o", "output" ])
56 { optDoc = "The output .js file" }
58externsFile :: Term (Maybe FilePath)
59externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
60 { optDoc = "The output .e.ps file" }
62term :: Term (IO ())
63term = compile <$> inputFiles <*> outputFile <*> externsFile
65termInfo :: TermInfo
66termInfo = defTI
67 { termName = "psc"
68 , version = "1.0"
69 , termDoc = "Compiles PureScript to Javascript"
70 }
72main = run (term, termInfo)