diff options
author | tomsmeding <tom.smeding@gmail.com> | 2019-12-13 13:47:31 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2019-12-13 13:47:31 +0100 |
commit | fe57886e5d2122f9ea785012818b3f885e8d6a3f (patch) | |
tree | f6b54226c34b27fd9f9cf3409ab349e2f830207f /Main.hs | |
parent | 3595d3c75503158e4eedaedbac8e81cbbe5ae54b (diff) |
Clean up parseOptions
Diffstat (limited to 'Main.hs')
-rw-r--r-- | Main.hs | 29 |
1 files changed, 14 insertions, 15 deletions
@@ -22,25 +22,24 @@ usage = do putStrLn $ " -irpre Print IR after optimisation, before stackification" putStrLn $ " -ir Print IR after optimisation and stackification" -data Options = Options { optAST :: Bool, optIRPre :: Bool, optIR :: Bool } +data Options = Options { optAST :: Bool, optIRPre :: Bool, optIR :: Bool, optFiles :: [FilePath] } --- TODO: clean this function up -parseOptions' :: (Options, Maybe FilePath) -> [String] -> IO (Options, Maybe FilePath) -parseOptions' pair [] = return pair -parseOptions' _ ("-h":_) = usage >> exitSuccess -parseOptions' (opt, fp) ("-ast":as) = parseOptions' (opt { optAST = True }, fp) as -parseOptions' (opt, fp) ("-irpre":as) = parseOptions' (opt { optIRPre = True }, fp) as -parseOptions' (opt, fp) ("-ir":as) = parseOptions' (opt { optIR = True }, fp) as -parseOptions' _ (('-':a):_) = putStrLn ("Unknown option '" ++ a ++ "'") >> usage >> exitFailure -parseOptions' (opt, Nothing) (f:as) = parseOptions' (opt, Just f) as -parseOptions' (_, Just _) (_:_) = putStrLn "At most one filename argument expected" >> usage >> exitFailure - -parseOptions :: [String] -> IO (Options, Maybe FilePath) -parseOptions = parseOptions' (Options False False False, Nothing) +parseOptions :: Options -> [String] -> IO Options +parseOptions o [] = return o +parseOptions _ ("-h":_) = usage >> exitSuccess +parseOptions o ("-ast":as) = parseOptions (o { optAST = True }) as +parseOptions o ("-irpre":as) = parseOptions (o { optIRPre = True }) as +parseOptions o ("-ir":as) = parseOptions (o { optIR = True }) as +parseOptions _ (('-':a):_) = putStrLn ("Unknown option '" ++ a ++ "'") >> usage >> exitFailure +parseOptions o (f:as) = parseOptions (o { optFiles = optFiles o ++ [f] }) as main :: IO () main = do - (opts, mfname) <- getArgs >>= parseOptions + opts <- getArgs >>= parseOptions (Options False False False []) + mfname <- case optFiles opts of + [] -> return Nothing + [fname] -> return (Just fname) + _ -> putStrLn "At most one filename argument expected" >> usage >> exitFailure source <- maybe getContents readFile mfname prog <- parseProgram mfname source >>= either (die . show) return |