Expands CLI support with output forms and decoding

Adds CLI options for compiling to a Tree Calculus, AST, Ternary, and
ASCII tree view. Adds CLI command for attempted decoding of a compiled
result to Number/String/List.
This commit is contained in:
James Eversole
2024-12-30 14:19:43 -06:00
parent 8622af9ad2
commit 5e2a4dff50
13 changed files with 131 additions and 57 deletions

View File

@ -1,11 +1,11 @@
module Main where
import Compiler
import Eval (evalTricu, result)
import Eval (evalTricu, result, toAST)
import Library (library)
import Parser (parseTricu)
import REPL (repl)
import Research (T)
import REPL
import Research
import Text.Megaparsec (runParser)
import System.Console.CmdArgs
@ -14,7 +14,11 @@ import qualified Data.Map as Map
data TricuArgs
= Repl
| Compile { file :: FilePath }
| Compile { file :: FilePath, output :: Maybe FilePath, form :: CompiledForm }
| Decode { input :: Maybe FilePath }
deriving (Show, Data, Typeable)
data CompiledForm = TreeCalculus | AST | Ternary | Ascii
deriving (Show, Data, Typeable)
replMode :: TricuArgs
@ -24,14 +28,31 @@ replMode = Repl
&= name "repl"
compileMode :: TricuArgs
compileMode = Compile { file = def &= typ "FILE" &= help "Relative or absolute path to compile" }
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"
}
&= help "Compile a file and return the result of the expression in the final line"
&= explicit
&= name "compile"
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"
main :: IO ()
main = do
args <- cmdArgs $ modes [replMode, compileMode]
args <- cmdArgs $ modes [replMode, compileMode, decodeMode]
&= help "tricu: Exploring Tree Calculus"
&= program "tricu"
&= summary "tricu - compiler and repl"
@ -41,7 +62,22 @@ main = do
putStrLn "Welcome to the tricu REPL"
putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`"
repl library
Compile filePath -> do
Compile { file = filePath, output = maybeOutputPath, form = form } -> do
result <- evaluateFile filePath
print result
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
formatResult :: CompiledForm -> T -> String
formatResult TreeCalculus = show
formatResult AST = show . toAST
formatResult Ternary = toTernaryString
formatResult Ascii = toAscii