2013-10-20 22:55:30 +00:00
|
|
|
-- Driver for the Lamb programming language
|
|
|
|
-- Copyright (c) 2013 darkf
|
|
|
|
-- Licensed under the terms of the zlib license, see LICENSE for details
|
|
|
|
|
|
|
|
import System.Environment (getArgs)
|
|
|
|
import System.Directory (doesFileExist)
|
2013-10-26 02:34:31 +00:00
|
|
|
import System.FilePath (FilePath, splitExtension)
|
2013-11-07 06:01:06 +00:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import Parser (parseProgram)
|
2013-11-09 10:02:00 +00:00
|
|
|
import Interp (evalFileV, evalProgram, initIO, interpret, InterpState, Value)
|
2013-10-20 22:55:30 +00:00
|
|
|
|
|
|
|
-- returns Nothing if all files exist, or Just path for the first one that doesn't
|
2013-10-26 02:34:31 +00:00
|
|
|
allExist :: [FilePath] -> IO (Maybe FilePath)
|
2013-10-20 22:55:30 +00:00
|
|
|
allExist [] = return Nothing
|
2013-10-26 01:16:19 +00:00
|
|
|
allExist ("-":xs) = allExist xs
|
2013-10-20 22:55:30 +00:00
|
|
|
allExist (x:xs) = do
|
|
|
|
exists <- doesFileExist x
|
|
|
|
if exists then allExist xs
|
|
|
|
else return $ Just x
|
|
|
|
|
2013-11-07 06:01:06 +00:00
|
|
|
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 ()
|
|
|
|
|
2013-10-20 22:55:30 +00:00
|
|
|
main = do
|
|
|
|
args <- getArgs
|
2013-11-07 06:01:06 +00:00
|
|
|
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
|