1-----------------------------------------------------------------------------
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE DoAndIfThenElse #-}
17module Main (main) where
19import qualified Language.PureScript as P
21import Data.List (isSuffixOf)
22import Data.Traversable (traverse)
24import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
27import System.FilePath (pathSeparator)
28import System.Directory (getCurrentDirectory, getTemporaryDirectory, getDirectoryContents, findExecutable)
29import Text.Parsec (ParseError)
30import qualified Paths_purescript as Paths
31import qualified System.IO.UTF8 as U
33preludeFilename :: IO FilePath
34preludeFilename = Paths.getDataFileName "prelude/prelude.purs"
36readInput :: [FilePath] -> IO (Either ParseError [P.Module])
37readInput inputFiles = fmap (fmap concat . sequence) $ forM inputFiles $ \inputFile -> do
38 text <- U.readFile inputFile
39 return $ P.runIndentParser inputFile P.parseModules text
41compile :: P.Options -> [FilePath] -> IO (Either String (String, String, P.Environment))
42compile opts inputFiles = do
43 modules <- readInput inputFiles
46 return (Left $ show parseError)
47 Right ms -> return $ P.compile opts ms
49assert :: FilePath -> P.Options -> FilePath -> (Either String (String, String, P.Environment) -> IO (Maybe String)) -> IO ()
50assert preludeExterns opts inputFile f = do
51 e <- compile opts [preludeExterns, inputFile]
54 Just err -> putStrLn err >> exitFailure
57assertCompiles :: FilePath -> FilePath -> FilePath -> IO ()
58assertCompiles preludeJs preludeExterns inputFile = do
59 putStrLn $ "Assert " ++ inputFile ++ " compiles successfully"
60 let options = P.defaultOptions { P.optionsMain = Just "Main", P.optionsModules = ["Main"], P.optionsCodeGenModules = ["Main"] }
61 assert preludeExterns options inputFile $ either (return . Just) $ \(js, _, _) -> do
62 process <- findNodeProcess
63 result <- traverse (\node -> readProcessWithExitCode node [] (preludeJs ++ js)) process
65 Just (ExitSuccess, out, _) -> putStrLn out >> return Nothing
66 Just (ExitFailure _, _, err) -> return $ Just err
67 Nothing -> return $ Just "Couldn't find node.js executable"
69assertDoesNotCompile :: FilePath -> FilePath -> IO ()
70assertDoesNotCompile preludeExterns inputFile = do
71 putStrLn $ "Assert " ++ inputFile ++ " does not compile"
72 assert preludeExterns P.defaultOptions inputFile $ \e ->
74 Left _ -> return Nothing
75 Right _ -> return $ Just "Should not have compiled"
77findNodeProcess :: IO (Maybe String)
78findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names
79 where names = ["nodejs", "node"]
83 prelude <- preludeFilename
84 putStrLn "Compiling Prelude"
85 preludeResult <- compile P.defaultOptions [prelude]
87 Left err -> putStrLn err >> exitFailure
88 Right (preludeJs, exts, _) -> do
89 tmp <- getTemporaryDirectory
90 let preludeExterns = tmp ++ pathSeparator : "prelude.externs"
91 writeFile preludeExterns exts
92 putStrLn $ "Wrote " ++ preludeExterns
93 cd <- getCurrentDirectory
94 let examples = cd ++ pathSeparator : "examples"
95 let passing = examples ++ pathSeparator : "passing"
96 passingTestCases <- getDirectoryContents passing
97 forM_ passingTestCases $ \inputFile -> when (".purs" `isSuffixOf` inputFile) $
98 assertCompiles preludeJs preludeExterns (passing ++ pathSeparator : inputFile)
99 let failing = examples ++ pathSeparator : "failing"
100 failingTestCases <- getDirectoryContents failing
101 forM_ failingTestCases $ \inputFile -> when (".purs" `isSuffixOf` inputFile) $
102 assertDoesNotCompile preludeExterns (failing ++ pathSeparator : inputFile)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list