add basic literate lamb (lilamb) parsing
This commit is contained in:
parent
60dd545928
commit
64405f5d5d
1 changed files with 13 additions and 2 deletions
15
lamb.hs
15
lamb.hs
|
@ -4,10 +4,11 @@
|
||||||
|
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
import System.Directory (doesFileExist)
|
import System.Directory (doesFileExist)
|
||||||
|
import System.FilePath (FilePath, splitExtension)
|
||||||
import Interp (evalProgram, evalString, Value(UnitV))
|
import Interp (evalProgram, evalString, 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 :: [String] -> IO (Maybe String)
|
allExist :: [FilePath] -> IO (Maybe FilePath)
|
||||||
allExist [] = return Nothing
|
allExist [] = return Nothing
|
||||||
allExist ("-":xs) = allExist xs
|
allExist ("-":xs) = allExist xs
|
||||||
allExist (x:xs) = do
|
allExist (x:xs) = do
|
||||||
|
@ -15,10 +16,20 @@ allExist (x:xs) = do
|
||||||
if exists then allExist xs
|
if exists then allExist xs
|
||||||
else return $ Just x
|
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 :: String -> IO Value
|
||||||
evalFile path = do
|
evalFile path = do
|
||||||
contents <- if path == "-" then getContents else readFile path
|
contents <- if path == "-" then getContents else readFile path
|
||||||
evalString contents
|
if isLiterate path then
|
||||||
|
evalString . unlines . parseLiterate . lines $ contents
|
||||||
|
else evalString contents
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
args <- getArgs
|
args <- getArgs
|
||||||
|
|
Loading…
Reference in a new issue