2024-12-18 18:55:51 -06:00
|
|
|
module Main where
|
|
|
|
|
2024-12-29 21:49:57 -06:00
|
|
|
import Compiler
|
2024-12-30 14:19:43 -06:00
|
|
|
import Eval (evalTricu, result, toAST)
|
2024-12-29 20:29:41 -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
|
|
|
|
|
|
|
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
|
2024-12-30 14:19:43 -06:00
|
|
|
| Compile { file :: FilePath, output :: Maybe FilePath, form :: CompiledForm }
|
|
|
|
| Decode { input :: Maybe FilePath }
|
|
|
|
deriving (Show, Data, Typeable)
|
|
|
|
|
|
|
|
data CompiledForm = TreeCalculus | AST | Ternary | Ascii
|
2024-12-29 21:49:57 -06:00
|
|
|
deriving (Show, Data, Typeable)
|
|
|
|
|
|
|
|
replMode :: TricuArgs
|
|
|
|
replMode = Repl
|
|
|
|
&= help "Start interactive REPL"
|
|
|
|
&= auto
|
|
|
|
&= name "repl"
|
|
|
|
|
|
|
|
compileMode :: TricuArgs
|
2024-12-30 14:19:43 -06:00
|
|
|
compileMode = Compile
|
|
|
|
{ file = def &= typ "FILE"
|
|
|
|
&= help "Relative or absolute path to file input for compilation" &= name "f"
|
|
|
|
, output = def &= typ "OUTPUT"
|
|
|
|
&= help "Optional output file path for resulting output" &= name "o"
|
|
|
|
, form = TreeCalculus &= typ "FORM"
|
|
|
|
&= help "Output form: (tree|ast|ternary|ascii)"
|
|
|
|
&= name "t"
|
|
|
|
}
|
2024-12-29 21:49:57 -06:00
|
|
|
&= help "Compile a file and return the result of the expression in the final line"
|
|
|
|
&= explicit
|
|
|
|
&= name "compile"
|
|
|
|
|
2024-12-30 14:19:43 -06:00
|
|
|
decodeMode :: TricuArgs
|
|
|
|
decodeMode = Decode
|
|
|
|
{ input = def &= typ "FILE"
|
|
|
|
&= help "Optional file path containing a Tree Calculus value. Defaults to stdin." &= name "f"
|
|
|
|
}
|
|
|
|
&= 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
|
2024-12-30 14:19:43 -06:00
|
|
|
args <- cmdArgs $ modes [replMode, compileMode, decodeMode]
|
2024-12-29 21:49:57 -06:00
|
|
|
&= help "tricu: Exploring Tree Calculus"
|
|
|
|
&= program "tricu"
|
|
|
|
&= summary "tricu - compiler and repl"
|
|
|
|
|
|
|
|
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
|
2024-12-30 14:19:43 -06:00
|
|
|
Compile { file = filePath, output = maybeOutputPath, form = form } -> do
|
2024-12-29 21:49:57 -06:00
|
|
|
result <- evaluateFile filePath
|
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
|
2024-12-29 21:49:57 -06:00
|
|
|
|
2024-12-30 14:19:43 -06:00
|
|
|
formatResult :: CompiledForm -> T -> String
|
|
|
|
formatResult TreeCalculus = show
|
|
|
|
formatResult AST = show . toAST
|
|
|
|
formatResult Ternary = toTernaryString
|
|
|
|
formatResult Ascii = toAscii
|