From 0a49f4e86d61c14e2d457e03cb56c65c764d8e55 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 11 Sep 2022 20:28:58 +0100 Subject: Better job splitting constnt folding --- app/Main.hs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/Main.hs b/app/Main.hs index 4a3128a..5c07165 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -15,6 +15,7 @@ import System.IO (hPutStr, hPutStrLn, stderr, stdout) data Options = Options { optParse :: Bool, + optAst :: Bool, optHelp :: Bool, optVersion :: Bool } @@ -22,6 +23,7 @@ data Options = Options defOptions = Options { optParse = False, + optAst = False, optHelp = False, optVersion = False } @@ -33,6 +35,11 @@ options = ["parse"] (NoArg (\opts -> opts {optParse = True})) "only parse, reporting any errors", + Option + ['a'] + ["ast"] + (NoArg (\opts -> opts {optAst = True})) + "parse and output the AST", Option ['h'] ["help"] @@ -61,13 +68,13 @@ usage progName errs helpText = usageInfo header options -compileFile :: String -> Bool -> IO () -compileFile filename onlyParse = do +compileFile :: String -> Bool -> Bool -> IO () +compileFile filename onlyParse showAst = do res <- parseFromFile filename case res of Left err -> hPutStrLn stderr (showParserError err) >> exitFailure Right ast -> do - res <- return $ compile ast + res <- return $ if showAst then fmap (\ast -> show ast) (compileToAst ast) else compile ast case res of Right out -> if onlyParse then exitSuccess else hPutStrLn stdout out Left errs -> hPutStr stderr (showErrorList errs) >> exitFailure @@ -82,7 +89,7 @@ main = do when (optHelp opts) $ usage progName [] when (optVersion opts) $ putStrLn (progName ++ " " ++ version) >> exitSuccess case n of - [filename] -> compileFile filename $ optParse opts + [filename] -> compileFile filename (optParse opts) (optAst opts) _ -> usage progName [] where opts = foldl (flip id) defOptions o -- cgit v1.2.3