From 1d57fca6b4b971da0d69c92dbce471dacd811a62 Mon Sep 17 00:00:00 2001 From: darkf Date: Wed, 6 Nov 2013 22:01:06 -0800 Subject: [PATCH] add simple REPL to the driver when no arguments are given --- Lamb.hs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/Lamb.hs b/Lamb.hs index 1235737..cd21c8f 100644 --- a/Lamb.hs +++ b/Lamb.hs @@ -5,7 +5,9 @@ import System.Environment (getArgs) import System.Directory (doesFileExist) 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 allExist :: [FilePath] -> IO (Maybe FilePath) @@ -16,11 +18,31 @@ allExist (x:xs) = do if exists then allExist xs 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 args <- getArgs - exist <- allExist args - case exist of - Just file -> putStrLn $ "error: file " ++ file ++ " doesn't exist" - Nothing -> - initIO >> - mapM_ evalFileV args \ No newline at end of file + case args of + [] -> -- no arguments, launch REPL + initIO >> repl' + _ -> do + exist <- allExist args + case exist of + Just file -> putStrLn $ "error: file " ++ file ++ " doesn't exist" + Nothing -> + initIO >> + mapM_ evalFileV args \ No newline at end of file