From e25b703805a6d5b20ddcb70e406830ca8d16cd22 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Fri, 21 Feb 2014 23:51:29 +1100 Subject: Revised programming style --- fractran.hs | 15 ++++----------- makefile | 4 ++-- thue.hs | 15 ++++----------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/fractran.hs b/fractran.hs index 7f3946f..964a095 100644 --- a/fractran.hs +++ b/fractran.hs @@ -1,16 +1,11 @@ import System.Environment( getArgs ) import Data.Typeable -import qualified Control.Exception +import Control.Exception( ErrorCall(..), Handler(..), catches ) import Fractran.Parser import Fractran.Interpreter -data FractranException = FractranException { errString :: String } - deriving (Show, Typeable) - -instance Control.Exception.Exception FractranException - usageString :: String @@ -22,7 +17,7 @@ program :: IO () program = do args <- getArgs fileContents <- if (length args /= 1) - then Control.Exception.throw (FractranException usageString) + then error usageString else readFile (head args) case (parseFractran fileContents) of @@ -31,8 +26,6 @@ program = do -main = Control.Exception.catch program handler - where - handler :: FractranException -> IO () - handler err = putStrLn (errString err) +main = catches program + [ Handler ((\e -> putStrLn . show $ e) :: ErrorCall -> IO ()) ] diff --git a/makefile b/makefile index 961af1a..b702844 100644 --- a/makefile +++ b/makefile @@ -21,8 +21,8 @@ testprog: ghc --make test.hs -o ${OUTPUTDIR}/test fractranprog: - ghc -XDeriveDataTypeable --make fractran.hs -o ${OUTPUTDIR}/fractran + ghc --make fractran.hs -o ${OUTPUTDIR}/fractran thueprog: - ghc -XDeriveDataTypeable --make thue.hs -o ${OUTPUTDIR}/thue + ghc --make thue.hs -o ${OUTPUTDIR}/thue diff --git a/thue.hs b/thue.hs index 6630e6d..7bc375d 100644 --- a/thue.hs +++ b/thue.hs @@ -1,16 +1,11 @@ import System.Environment( getArgs ) import Data.Typeable -import qualified Control.Exception +import Control.Exception( ErrorCall(..), Handler(..), catches ) import Thue.Parser import Thue.Interpreter -data ThueException = ThueException { errString :: String } - deriving (Show, Typeable) - -instance Control.Exception.Exception ThueException - usageString :: String @@ -22,7 +17,7 @@ program :: IO () program = do args <- getArgs fileContents <- if (length args /= 1) - then Control.Exception.throw (ThueException usageString) + then error usageString else readFile (head args) case (parseThue fileContents) of @@ -31,8 +26,6 @@ program = do -main = Control.Exception.catch program handler - where - handler :: ThueException -> IO () - handler err = putStrLn (errString err) +main = catches program + [ Handler ((\e -> putStrLn . show $ e) :: ErrorCall -> IO ()) ] -- cgit