summaryrefslogtreecommitdiffhomepage
path: root/Runnable.hs
blob: 90e01af1801aed3227d72b00236f6b7f0c81888f (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
{-# OPTIONS -fglasgow-exts #-}
module Runnable where

import Control.Monad
import Text.Read
import Text.ParserCombinators.ReadPrec
import Config (runnableTypes)
import Commands

data Runnable = forall r . (Exec r,Show r, Read r) => Run r

instance Show Runnable where
    show (Run a) = "Run " ++ show a

instance Exec Runnable where
     run (Run a) = run a
     rate (Run a) = rate a
     alias (Run a) = alias a

instance Read Runnable where
    readPrec = readRunnable

-- read an existential as any of hidden types ts
class ReadAsAnyOf ts ex where
    readAsAnyOf :: ts -> ReadPrec ex

instance ReadAsAnyOf () ex where 
    readAsAnyOf ~() = mzero

instance (Read t, Show t, Exec t, ReadAsAnyOf ts Runnable) => ReadAsAnyOf (t,ts) Runnable where 
    readAsAnyOf ~(t,ts) = r t `mplus` readAsAnyOf ts
              where r ty = do { m <- readPrec; return (Run (m `asTypeOf` ty)) }



readRunnable :: ReadPrec Runnable
readRunnable = prec 10 $ do
                 Ident "Run" <- lexP
                 parens $ readAsAnyOf runnableTypes


-- | Reads the configuration files or quits with an error
readConfig :: FilePath -> IO Runnable
readConfig f = 
    do s <- readFile f
       case reads s of
         [(config,_)] -> return config
         [] -> error ("Corrupt config file: " ++ f)
         _ -> error ("Some problem occured. Aborting...")