add simple REPL to the driver when no arguments are given

This commit is contained in:
darkf 2013-11-06 22:01:06 -08:00
parent 0725c9735b
commit 1d57fca6b4
1 changed files with 29 additions and 7 deletions

36
Lamb.hs
View File

@ -5,7 +5,9 @@
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 Interp (evalFileV, initIO, Value(UnitV)) import Control.Monad.IO.Class (liftIO)
import Parser (parseProgram)
import Interp (evalFileV, evalProgram, initIO, interpret, InterpState, Value(UnitV))
-- returns Nothing if all files exist, or Just path for the first one that doesn't -- returns Nothing if all files exist, or Just path for the first one that doesn't
allExist :: [FilePath] -> IO (Maybe FilePath) allExist :: [FilePath] -> IO (Maybe FilePath)
@ -16,11 +18,31 @@ allExist (x:xs) = do
if exists then allExist xs if exists then allExist xs
else return $ Just x else return $ Just x
repl :: InterpState Value
repl = do
liftIO $ putStr ">> "
line <- liftIO getLine
case parseProgram line of
Left err -> do
liftIO $ putStrLn $ "parse error: " ++ show err
repl
Right prg -> do
ev <- evalProgram prg
liftIO $ print ev
repl
repl' :: IO ()
repl' = interpret repl >> return ()
main = do main = do
args <- getArgs args <- getArgs
exist <- allExist args case args of
case exist of [] -> -- no arguments, launch REPL
Just file -> putStrLn $ "error: file " ++ file ++ " doesn't exist" initIO >> repl'
Nothing -> _ -> do
initIO >> exist <- allExist args
mapM_ evalFileV args case exist of
Just file -> putStrLn $ "error: file " ++ file ++ " doesn't exist"
Nothing ->
initIO >>
mapM_ evalFileV args