Added error for each missing file.

Generalised the allExist function to findMissing.
This commit is contained in:
Anton Golov 2014-05-29 03:31:13 +02:00
parent 02bc968e0c
commit c5a29ef171
1 changed files with 22 additions and 20 deletions

42
Lamb.hs
View File

@ -5,18 +5,18 @@
import System.Environment (getArgs) import System.Environment (getArgs)
import System.Directory (doesFileExist) import System.Directory (doesFileExist)
import System.FilePath (FilePath, splitExtension) import System.FilePath (FilePath, splitExtension)
import Control.Applicative ((<$>))
import Control.Monad (filterM)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Parser (parseProgram) import Parser (parseProgram)
import Interp (evalFileV, evalProgram, initIO, interpret, InterpState, Value) import Interp (evalFileV, evalProgram, initIO, interpret, InterpState, Value)
-- returns Nothing if all files exist, or Just path for the first one that doesn't exists :: FilePath -> IO Bool
allExist :: [FilePath] -> IO (Maybe FilePath) exists "-" = return True
allExist [] = return Nothing exists path = not <$> doesFileExist path
allExist ("-":xs) = allExist xs
allExist (x:xs) = do findMissing :: [FilePath] -> IO [FilePath]
exists <- doesFileExist x findMissing = filterM exists
if exists then allExist xs
else return $ Just x
repl :: InterpState Value repl :: InterpState Value
repl = do repl = do
@ -25,24 +25,26 @@ repl = do
case parseProgram line of case parseProgram line of
Left err -> do Left err -> do
liftIO $ putStrLn $ "parse error: " ++ show err liftIO $ putStrLn $ "parse error: " ++ show err
repl
Right prg -> do Right prg -> do
ev <- evalProgram prg ev <- evalProgram prg
liftIO $ print ev liftIO $ print ev
repl repl
repl' :: IO () repl' :: IO ()
repl' = interpret repl >> return () repl' = interpret repl >> return ()
main = do main = do
args <- getArgs args <- getArgs
case args of if null args
[] -> -- no arguments, launch REPL then do -- no arguments, launch REPL
initIO >> repl' initIO
_ -> do repl'
exist <- allExist args else do
case exist of missing <- findMissing args
Just file -> putStrLn $ "error: file " ++ file ++ " doesn't exist" if null missing
Nothing -> then do
initIO >> initIO
mapM_ evalFileV args mapM_ evalFileV args
else do
let reportMissing file = putStrLn $ "error: file " ++ file ++ " doesn't exist"
mapM_ reportMissing missing