add basic literate lamb (lilamb) parsing

This commit is contained in:
darkf 2013-10-25 19:34:31 -07:00
parent 60dd545928
commit 64405f5d5d
1 changed files with 13 additions and 2 deletions

15
lamb.hs
View File

@ -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