diff --git a/lamb.hs b/lamb.hs index a331226..3120e55 100644 --- a/lamb.hs +++ b/lamb.hs @@ -4,10 +4,11 @@ import System.Environment (getArgs) import System.Directory (doesFileExist) +import System.FilePath (FilePath, splitExtension) import Interp (evalProgram, evalString, Value(UnitV)) -- returns Nothing if all files exist, or Just path for the first one that doesn't -allExist :: [String] -> IO (Maybe String) +allExist :: [FilePath] -> IO (Maybe FilePath) allExist [] = return Nothing allExist ("-":xs) = allExist xs allExist (x:xs) = do @@ -15,10 +16,20 @@ allExist (x:xs) = do if exists then allExist xs else return $ Just x +isLiterate :: FilePath -> Bool +isLiterate path = snd (splitExtension path) == ".lilamb" + +-- Takes the lines of a literate program and returns the lines for a new executable program +-- from lines beginning with four spaces. +parseLiterate :: [String] -> [String] +parseLiterate lns = [drop 4 line | line <- lns, take 4 line == " "] + evalFile :: String -> IO Value evalFile path = do contents <- if path == "-" then getContents else readFile path - evalString contents + if isLiterate path then + evalString . unlines . parseLiterate . lines $ contents + else evalString contents main = do args <- getArgs