-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
66 lines (49 loc) · 1.97 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-
Project: VUT FIT FLP 1. Project (Functional) - SIMPLIFY-BKG
Author: Dominik Harmim <xharmi00@stud.fit.vutbr.cz>
Year: 2020
Module: Main
Description: The main program. Performs input and output operations.
Initiates the computation.
-}
module Main (main) where
import Parser (parseCFG)
import Simplification (simplify1, simplify2)
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)
import Types (CFG(..))
-- The main program.
main :: IO ()
main = do
(action, input) <- processArgs =<< getArgs
either die action $ parseCFG input
-- Processes input arguments and returns an action to be performed and an
-- input file.
processArgs :: [String] -> IO (CFG -> IO (), String)
processArgs [option] = processOptions option =<< getContents
processArgs [option, inputFile] = processOptions option =<< readFile inputFile
processArgs _ = die "Excepting an option argument and optionally an input \
\file: simplify-bkg (-i|-1|-2) [input-file]"
-- Processes input options and returns an action to be performed and an
-- input file.
processOptions :: String -> String -> IO (CFG -> IO (), String)
processOptions option input = case option of
"-i" -> return (printCFG, input)
"-1" -> return (printCFGSimplify1, input)
"-2" -> return (printCFGSimplify2, input)
_ -> die $ "Unknown option: " ++ option
-- Prints the context-free grammar to the standard output.
printCFG :: CFG -> IO ()
printCFG = putStr . show
-- Prints the context-free grammar to the standard output after the 'simplify1'
-- function has been performed.
printCFGSimplify1 :: CFG -> IO ()
printCFGSimplify1 = printCFG . simplify1
-- Prints the context-free grammar to the standard output after the 'simplify2'
-- function has been performed.
printCFGSimplify2 :: CFG -> IO ()
printCFGSimplify2 = printCFG . simplify2
-- Terminates the program with an error code and an error message.
die :: String -> IO a
die str = hPutStrLn stderr str >> exitFailure