commit 03cbadcc5462829f3190d3483d23eb77f67619c5
parent 8b0b24e7dcbf4303044e85128c4e2c73a6cc1712
Author: James Eversole <james@eversole.co>
Date: Wed, 1 Jan 2025 19:27:04 -0600
Support evaluation across multiple source files
Diffstat:
| M | src/Main.hs | | | 61 | ++++++++++++++++++++++++++----------------------------------- |
1 file changed, 26 insertions(+), 35 deletions(-)
diff --git 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