X-Git-Url: http://git.tomasm.cz/fp.git/blobdiff_plain/2df2a815f76afa2daee4c7c5494e15795876003e..2d2af9eded0d0e4968cf21746f3772f61caecd73:/src/Main.hs diff --git a/src/Main.hs b/src/Main.hs index 4de3762..4ddfe2d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -2,14 +2,16 @@ module Main where -import Data.Text as T (Text) +import Data.Text as T (Text, pack) import qualified Data.Text.IO as T import Data.Attoparsec.Text import qualified Data.Map as M import Control.Applicative +import Control.Monad import System.Environment import Lambda +import Lambda.Parser.Fancy data Definition = Definition String Term @@ -23,24 +25,38 @@ parseDefinition = do t <- parseTerm return $! Definition name t -parseFile :: Text -> ([Definition], [Term]) +parseImport :: Parser String +parseImport = do + string (T.pack "import ") + many1 letter + +parseFile :: Text -> ([FilePath], [Definition], [Term]) parseFile txt = case cnt of (Right t) -> t (Left e) -> error e where cnt = parseOnly (parserF <* endOfInput) txt parserF = do + imports <- many (parseImport <* char '\n') defs <- many (parseDefinition <* char '\n') terms <- many (parseTerm <* char '\n') - return $! (defs, terms) + return (imports, defs, terms) -printEval :: Term -> IO () -printEval t = do +printEval :: (Term -> Term) -> Term -> IO () +printEval def t = do putStrLn $ show t ++ ":" - putStrLn $ " " ++ show (reduce t) + putStrLn $ " " ++ show (reduce $ def t) putStrLn "" +makeDefTerm :: [Definition] -> Term -> Term +makeDefTerm (Definition name dTerm : ds) t = App (Lambda name (makeDefTerm ds t)) dTerm +makeDefTerm [] t = t + main :: IO () main = do [filename] <- getArgs - (defs, terms) <- parseFile <$> T.readFile filename - mapM_ printEval terms + (imports, defs, terms) <- parseFile <$> T.readFile filename + importDefs <- forM imports $ \ file -> do + (_, idefs, _) <- parseFile <$> T.readFile (file ++ ".lc") + return idefs + let defT = makeDefTerm $ concat importDefs ++ defs + mapM_ (printEval defT) terms