git.iko.soy/rowtype-yoga/purescript-native
on dfaaa0f help sign in
116 lines 3.7 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 qualified Language.PureScript as P
18import System.Console.CmdTheLine
19import Control.Applicative
20import Control.Monad (forM)
21import System.Exit (exitSuccess, exitFailure)
22import qualified System.IO.UTF8 as U
23import Text.Parsec (ParseError)
24import qualified Paths_purescript as Paths
25import Data.Version (showVersion)
27preludeFilename :: IO FilePath
28preludeFilename = Paths.getDataFileName "libraries/prelude/prelude.purs"
30readInput :: Maybe [FilePath] -> IO (Either ParseError [P.Module])
31readInput Nothing = getContents >>= return . P.runIndentParser P.parseModules
32readInput (Just input) = fmap (fmap concat . sequence) $ forM input $ \inputFile -> do
33 text <- U.readFile inputFile
34 return $ P.runIndentParser P.parseModules text
36compile :: P.Options -> Maybe [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
37compile opts input output externs = do
38 modules <- readInput input
39 case modules of
40 Left err -> do
41 U.print err
42 exitFailure
43 Right ms ->
44 case P.compile opts ms of
45 Left err -> do
46 U.putStrLn err
47 exitFailure
48 Right (js, exts, _) -> do
49 case output of
50 Just path -> U.writeFile path js
51 Nothing -> U.putStrLn js
52 case externs of
53 Nothing -> return ()
54 Just filePath -> U.writeFile filePath exts
55 exitSuccess
57useStdIn :: Term Bool
58useStdIn = value . flag $ (optInfo [ "s", "stdin" ])
59 { optDoc = "Read from standard input" }
61inputFiles :: Term [FilePath]
62inputFiles = value $ posAny [] $ posInfo
63 { posDoc = "The input .ps files" }
65outputFile :: Term (Maybe FilePath)
66outputFile = value $ opt Nothing $ (optInfo [ "o", "output" ])
67 { optDoc = "The output .js file" }
69externsFile :: Term (Maybe FilePath)
70externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
71 { optDoc = "The output .e.ps file" }
73tco :: Term Bool
74tco = value $ flag $ (optInfo [ "tco" ])
75 { optDoc = "Perform tail call optimizations" }
77performRuntimeTypeChecks :: Term Bool
78performRuntimeTypeChecks = value $ flag $ (optInfo [ "runtime-type-checks" ])
79 { optDoc = "Generate runtime type checks" }
81noPrelude :: Term Bool
82noPrelude = value $ flag $ (optInfo [ "no-prelude" ])
83 { optDoc = "Omit the Prelude" }
85magicDo :: Term Bool
86magicDo = value $ flag $ (optInfo [ "magic-do" ])
87 { optDoc = "Overload the do keyword to generate efficient code specifically for the Eff monad." }
89runMain :: Term Bool
90runMain = value $ flag $ (optInfo [ "run-main" ])
91 { optDoc = "Generate code to run the main method in the Main module." }
93options :: Term P.Options
94options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain
96stdInOrInputFiles :: FilePath -> Term (Maybe [FilePath])
97stdInOrInputFiles prelude = combine <$> useStdIn <*> (not <$> noPrelude) <*> inputFiles
98 where
99 combine False True input = Just (prelude : input)
100 combine False False input = Just input
101 combine True _ _ = Nothing
103term :: FilePath -> Term (IO ())
104term prelude = compile <$> options <*> stdInOrInputFiles prelude <*> outputFile <*> externsFile
106termInfo :: TermInfo
107termInfo = defTI
108 { termName = "psc"
109 , version = showVersion $ Paths.version
110 , termDoc = "Compiles PureScript to Javascript"
111 }
113main :: IO ()
114main = do
115 prelude <- preludeFilename
116 run (term prelude, termInfo)