Skip to content

Commit

Permalink
build expressions representing either diagrams or IO actions returnin…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Yorgey committed Nov 26, 2013
1 parent 4caeb1a commit e5059df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions diagrams-builder.cabal
Expand Up @@ -47,6 +47,7 @@ library
Diagrams.Builder.Modules
Diagrams.Builder.CmdLine
build-depends: base >=4.2 && < 4.7,
mtl >= 2.1 && < 2.2,
diagrams-lib >=0.6 && < 1.1,
hint ==0.3.*,
directory,
Expand Down
11 changes: 7 additions & 4 deletions src/Diagrams/Builder.hs
Expand Up @@ -38,6 +38,7 @@ module Diagrams.Builder
) where

import Control.Monad (guard, mplus, mzero)
import Control.Monad.Error (catchError)
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
import Crypto.Hash (Digest, MD5,
digestToHexByteString,
Expand Down Expand Up @@ -110,7 +111,8 @@ getHsenvArgv = do
where hsenvArgv = words $ fromMaybe "" (lookup "PACKAGE_DB_FOR_GHC" env)

-- | Interpret a diagram expression based on the contents of a given
-- source file, using some backend to produce a result.
-- source file, using some backend to produce a result. The
-- expression can be of type @Diagram b v@ or @IO (Diagram b v)@.
interpretDiagram
:: forall b v.
( Typeable b, Typeable v
Expand All @@ -127,7 +129,7 @@ interpretDiagram b _ opts m imps dexp = do
args <- liftIO getHsenvArgv
unsafeRunInterpreterWithArgs args $ do
setDiagramImports m imps
d <- interpret dexp (as :: Diagram b v)
d <- interpret dexp (as :: Diagram b v) `catchError` const (interpret dexp (as :: IO (Diagram b v)) >>= liftIO)
return (renderDia b opts d)

-- | Pretty-print an @InterpreterError@.
Expand All @@ -153,8 +155,9 @@ data BuildResult b v x =
-- some extra information.

-- | Build a diagram by writing the given source code to a temporary
-- module and interpreting the given expression. Can return either
-- a parse error if the source does not parse, an interpreter error,
-- module and interpreting the given expression, which can be of
-- type @Diagram b v@ or @IO (Diagram b v)@. Can return either a
-- parse error if the source does not parse, an interpreter error,
-- or the final result.
buildDiagram
:: ( Typeable b, Typeable v
Expand Down

0 comments on commit e5059df

Please sign in to comment.