summaryrefslogtreecommitdiff
path: root/src/grasp.hs
blob: fb8f866ec5f44dc7f7e3991a86d446b3dd3dcf38 (plain)
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


import qualified System.Environment as Env

import System.Console.GetOpt( OptDescr )
import qualified System.Console.GetOpt as GetOpt

import Control.Exception( ErrorCall(..), Handler(..), catches )

import qualified Control.Monad as Monad

import Grasp.Parser
import Grasp.Interpreter




data Flag = Testing
    deriving (Eq, Show)




usageString :: String
usageString = "Usage: grasp [--testing] <program file>"



options :: [OptDescr Flag]
options =
	[ GetOpt.Option ['t'] ["testing"] (GetOpt.NoArg Testing) "Enables before and after showing of program graph" ]



program :: IO ()
program = do
    (options, arguments, errors) <- Env.getArgs >>= return . GetOpt.getOpt GetOpt.Permute options
    
    Monad.when (length options > 1) (error usageString)
    Monad.when (length arguments /= 1) (error usageString)
    Monad.when (length errors /= 0) (error usageString)

    fileContents <- readFile (head arguments)

    case (parseGrasp fileContents) of
        Left x -> putStrLn (show x)
        Right x -> if (Testing `elem` options)
        	        then do
        	        	putStrLn "Input:"
        	        	putStrLn (show x)
        	        	y <- grasp x
        	        	putStrLn "\nOutput:"
        	        	putStrLn (show y)
        	        else grasp x >> return ()



main = catches program
               [ Handler ((\e -> putStrLn . show $ e) :: ErrorCall -> IO ()) ]