2024-12-18 18:55:51 -06:00
|
|
|
module Main where
|
|
|
|
|
2024-12-31 10:00:52 -06:00
|
|
|
import Eval (evalTricu, result)
|
2025-01-01 08:17:05 -06:00
|
|
|
import FileEval
|
2024-12-31 10:00:52 -06:00
|
|
|
import Library (library)
|
|
|
|
import Parser (parseTricu)
|
2024-12-30 14:19:43 -06:00
|
|
|
import REPL
|
|
|
|
import Research
|
2024-12-18 18:55:51 -06:00
|
|
|
|
2024-12-31 10:00:52 -06:00
|
|
|
import Text.Megaparsec (runParser)
|
2024-12-29 21:49:57 -06:00
|
|
|
import System.Console.CmdArgs
|
2024-12-18 18:55:51 -06:00
|
|
|
|
2024-12-29 20:29:41 -06:00
|
|
|
import qualified Data.Map as Map
|
|
|
|
|
2024-12-29 21:49:57 -06:00
|
|
|
data TricuArgs
|
|
|
|
= Repl
|
2025-01-01 08:17:05 -06:00
|
|
|
| Evaluate { file :: Maybe FilePath, output :: Maybe FilePath, form :: EvaluatedForm }
|
2024-12-30 14:19:43 -06:00
|
|
|
| Decode { input :: Maybe FilePath }
|
|
|
|
deriving (Show, Data, Typeable)
|
|
|
|
|
2024-12-29 21:49:57 -06:00
|
|
|
replMode :: TricuArgs
|
|
|
|
replMode = Repl
|
|
|
|
&= help "Start interactive REPL"
|
|
|
|
&= auto
|
|
|
|
&= name "repl"
|
|
|
|
|
2025-01-01 08:17:05 -06:00
|
|
|
evaluateMode :: TricuArgs
|
|
|
|
evaluateMode = Evaluate
|
2024-12-30 14:19:43 -06:00
|
|
|
{ file = def &= typ "FILE"
|
2025-01-01 08:17:05 -06:00
|
|
|
&= help "Optional input file path for evaluation.\nDefaults to stdin." &= name "f"
|
2024-12-30 14:19:43 -06:00
|
|
|
, output = def &= typ "OUTPUT"
|
2025-01-01 08:17:05 -06:00
|
|
|
&= help "Optional output file path for resulting output.\nDefaults to stdout." &= name "o"
|
2024-12-31 10:00:52 -06:00
|
|
|
, form = FSL &= typ "FORM"
|
2025-01-01 08:17:05 -06:00
|
|
|
&= help "Optional output form: (fsl|tree|ast|ternary|ascii).\nDefaults to fsl."
|
2024-12-30 14:19:43 -06:00
|
|
|
&= name "t"
|
|
|
|
}
|
2025-01-01 08:17:05 -06:00
|
|
|
&= help "Evaluate a file and return the result of the expression in the final line"
|
2024-12-29 21:49:57 -06:00
|
|
|
&= explicit
|
2025-01-01 08:17:05 -06:00
|
|
|
&= name "eval"
|
2024-12-29 21:49:57 -06:00
|
|
|
|
2024-12-30 14:19:43 -06:00
|
|
|
decodeMode :: TricuArgs
|
|
|
|
decodeMode = Decode
|
|
|
|
{ input = def &= typ "FILE"
|
2025-01-01 08:17:05 -06:00
|
|
|
&= help "Optional file path to attempt decoding.\nDefaults to stdin." &= name "f"
|
2024-12-30 14:19:43 -06:00
|
|
|
}
|
|
|
|
&= help "Decode a Tree Calculus value into a string representation"
|
|
|
|
&= explicit
|
|
|
|
&= name "decode"
|
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
main :: IO ()
|
2024-12-27 19:27:04 -06:00
|
|
|
main = do
|
2025-01-01 08:17:05 -06:00
|
|
|
args <- cmdArgs $ modes [replMode, evaluateMode, decodeMode]
|
2024-12-29 21:49:57 -06:00
|
|
|
&= help "tricu: Exploring Tree Calculus"
|
|
|
|
&= program "tricu"
|
2025-01-01 08:17:05 -06:00
|
|
|
&= summary "tricu Evaluator and REPL"
|
2024-12-29 21:49:57 -06:00
|
|
|
|
|
|
|
case args of
|
|
|
|
Repl -> do
|
|
|
|
putStrLn "Welcome to the tricu REPL"
|
|
|
|
putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`"
|
2024-12-30 08:30:40 -06:00
|
|
|
repl library
|
2025-01-01 08:17:05 -06:00
|
|
|
Evaluate { file = maybeFilePath, output = maybeOutputPath, form = form } -> do
|
|
|
|
result <- case maybeFilePath of
|
|
|
|
Just filePath -> evaluateFile filePath
|
|
|
|
Nothing -> do
|
|
|
|
t <- getContents
|
|
|
|
pure $ runTricu t
|
2024-12-30 14:19:43 -06:00
|
|
|
let fRes = formatResult form result
|
|
|
|
case maybeOutputPath of
|
|
|
|
Just outputPath -> do
|
|
|
|
writeFile outputPath fRes
|
|
|
|
putStrLn $ "Output to: " ++ outputPath
|
|
|
|
Nothing -> putStr fRes
|
|
|
|
Decode { input = maybeInputPath } -> do
|
|
|
|
value <- case maybeInputPath of
|
|
|
|
Just inputPath -> readFile inputPath
|
|
|
|
Nothing -> getContents
|
|
|
|
putStrLn $ decodeResult $ result $ evalTricu library $ parseTricu value
|
2025-01-01 08:17:05 -06:00
|
|
|
|
|
|
|
runTricu :: String -> T
|
|
|
|
runTricu = result . evalTricu Map.empty . parseTricu
|