Support evaluation across multiple source files

This commit is contained in:
James Eversole 2025-01-01 19:27:04 -06:00
parent a2c459b148
commit fff29199d1

View File

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