diff --git a/src/Main.hs b/src/Main.hs index c937822..56d3431 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,6 +6,7 @@ import Parser (parseTricu) import REPL import Research +import Control.Monad (foldM) import Control.Monad.IO.Class (liftIO) import Text.Megaparsec (runParser) import System.Console.CmdArgs @@ -14,10 +15,8 @@ import qualified Data.Map as Map data TricuArgs = Repl - | Evaluate { file :: Maybe FilePath - , output :: Maybe FilePath - , form :: EvaluatedForm } - | Decode { file :: Maybe FilePath } + | Evaluate { file :: [FilePath], form :: EvaluatedForm } + | Decode { file :: [FilePath] } deriving (Show, Data, Typeable) replMode :: TricuArgs @@ -27,33 +26,27 @@ replMode = Repl &= name "repl" evaluateMode :: TricuArgs -evaluateMode = Evaluate - { file = def &= typ "FILE" - &= help "Optional input file path for evaluation.\n \ - \ Defaults to stdin." +evaluateMode = Evaluate + { file = def &= help "Input file path(s) for evaluation.\n \ + \ Defaults to stdin." &= name "f" - , output = def &= typ "OUTPUT" - &= help "Optional output file path for resulting output.\n \ - \ Defaults to stdout." - &= name "o" - , form = FSL &= typ "FORM" + , form = FSL &= typ "FORM" &= help "Optional output form: (fsl|tree|ast|ternary|ascii).\n \ - \ Defaults to fsl \"Fork Stem Leaf\" form." + \ Defaults to fsl \"Fork Stem Leaf\" form." &= name "t" } - &= help "Evaluate tricu and return the result of the expression in the final line" + &= help "Evaluate tricu and return the result of the final expression." &= explicit &= name "eval" decodeMode :: TricuArgs decodeMode = Decode - { file = def - &= help "Optional input file path to attempt decoding.\n \ - \ Defaults to stdin." - &= name "f" - &= typ "FILE" + { file = def + &= help "Optional input file path(s) to attempt decoding.\n \ + \ Defaults to stdin." + &= name "f" } - &= help "Decode a Tree Calculus value into a string representation" + &= help "Decode a Tree Calculus value into a string representation." &= explicit &= name "decode" @@ -63,29 +56,27 @@ main = do &= help "tricu: Exploring Tree Calculus" &= program "tricu" &= summary "tricu Evaluator and REPL" - case args of Repl -> do putStrLn "Welcome to the tricu REPL" putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`" library <- liftIO $ evaluateFile "./lib/base.tri" repl library - Evaluate { file = maybeFilePath, output = maybeOutputPath, form = form } -> do - result <- case maybeFilePath of - Just filePath -> evaluateFileResult filePath - Nothing -> do + Evaluate { file = filePaths, form = form } -> do + result <- case filePaths of + [] -> do t <- getContents pure $ runTricu t + (filePath:restFilePaths) -> do + initialEnv <- evaluateFile filePath + finalEnv <- foldM evaluateFileWithContext initialEnv restFilePaths + pure $ result finalEnv let fRes = formatResult form result - case maybeOutputPath of - Just outputPath -> do - writeFile outputPath fRes - putStrLn $ "Output to: " ++ outputPath - Nothing -> putStr fRes - Decode { file = maybeFilePath } -> do - value <- case maybeFilePath of - Just filePath -> readFile filePath - Nothing -> getContents + putStr fRes + Decode { file = filePaths } -> do + value <- case filePaths of + [] -> getContents + (filePath:_) -> readFile filePath library <- liftIO $ evaluateFile "./lib/base.tri" putStrLn $ decodeResult $ result $ evalTricu library $ parseTricu value