diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-08-18 07:44:18 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-08-18 07:44:18 +0100 |
commit | 0f37c89fd65eeb1c0d41e9661de41857db38ffee (patch) | |
tree | 503d875a0dba28a10b34e98ed41a6a4606373582 /src | |
parent | bdb014f3a088da82845642b9688c662bc7afa29f (diff) | |
download | micro-lang-hs-0f37c89fd65eeb1c0d41e9661de41857db38ffee.tar.gz micro-lang-hs-0f37c89fd65eeb1c0d41e9661de41857db38ffee.zip |
Added CLI
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.hs | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/src/Main.hs b/src/Main.hs index 47fd883..8959329 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,21 +1,85 @@ module Main where import Compiler +import Control.Monad (when) import Control.Monad.State (evalState) import qualified Data.Map as Map +import Data.Maybe (fromMaybe) import Error (showErrorList, showParserError) import Lexer (scan) import Parser (parse, parseFromFile) -import System.Exit (exitFailure) +import System.Console.GetOpt +import System.Environment (getProgName) +import System.Environment.Blank (getArgs) +import System.Exit (exitFailure, exitSuccess) import System.IO (hPutStr, hPutStrLn, stderr, stdout) -main :: IO () -main = do - res <- parseFromFile (scan parse) "input" +version = "0.1.0" + +data Options = Options + { optHelp :: Bool, + optVersion :: Bool + } + +defOptions = + Options + { optHelp = False, + optVersion = False + } + +options :: [OptDescr (Options -> Options)] +options = + [ Option + ['h'] + ["help"] + (NoArg (\opts -> opts {optHelp = True})) + "show help and exit", + Option + ['v'] + ["version"] + (NoArg (\opts -> opts {optVersion = True})) + "output version and exit" + ] + +usage :: String -> [String] -> IO () +usage progName errs + | null errs = do + putStrLn helpText + exitSuccess + | otherwise = do + hPutStrLn stderr ("error: " ++ concat errs ++ "Try " ++ progName ++ " -h for more information.") + exitFailure + where + header = + "Usage: " + ++ progName + ++ " [OPTION...] file" + helpText = + usageInfo header options + +compileFile :: String -> IO () +compileFile filename = do + res <- parseFromFile (scan parse) filename case res of Left err -> hPutStrLn stderr (showParserError err) >> exitFailure Right ast -> do res <- return $ evalState (compileAll ast) startState case res of Right _ -> print ast - Left errs -> hPutStr stderr $ showErrorList errs + Left errs -> hPutStr stderr (showErrorList errs) >> exitFailure + +main :: IO () +main = do + progName <- getProgName + argv <- getArgs + case getOpt Permute options argv of + (o, n, []) -> + do + when (optHelp opts) $ usage progName [] + when (optVersion opts) $ putStrLn (progName ++ " " ++ version) >> exitSuccess + case n of + [filename] -> compileFile filename + _ -> usage progName [] + where + opts = foldl (flip id) defOptions o + (_, _, errs) -> usage progName errs |