This commit is contained in:
2024-09-28 15:16:53 +02:00
parent 0dccdc0a95
commit 18c54613aa
8 changed files with 0 additions and 0 deletions

35
a3/apl.hs Normal file
View File

@ -0,0 +1,35 @@
module Main (main) where
import APL.Eval (Val (..), eval, runEval)
import APL.Parser (parseAPL)
import System.Environment
( getArgs,
getProgName,
)
import System.Exit (ExitCode (..), exitWith)
import System.IO (hPutStrLn, stderr, stdout)
stringVal :: Val -> String
stringVal (ValBool b) = show b
stringVal (ValInt x) = show x
stringVal ValFun {} = "#<fun>"
main :: IO ()
main = do
args <- getArgs
case args of
[fname] -> do
s <- readFile fname
case parseAPL fname s of
Left err -> hPutStrLn stderr err
Right e -> case runEval (eval e) of
Left err -> hPutStrLn stderr err
Right v -> hPutStrLn stdout $ stringVal v
_ -> do
prog <- getProgName
failure $ "Usage: " ++ prog ++ " FILE"
pure ()
where
failure s = do
hPutStrLn stderr s
exitWith $ ExitFailure 1