Commit 704ac8cd authored by Eyal Dechter's avatar Eyal Dechter

Added MonadIO (from transformers package) instance for Interpreter.

parent 1de89fb4
{-# LANGUAGE DoAndIfThenElse, NoOverloadedStrings #-}
{-# LANGUAGE DoAndIfThenElse, NoOverloadedStrings, TypeSynonymInstances #-}
{- | Description : Wrapper around GHC API, exposing a single `evaluate` interface that runs
a statement, declaration, import, or directive.
......@@ -29,6 +30,8 @@ import Control.Monad (guard)
import System.Process
import System.Exit
import Data.Maybe (fromJust)
import qualified Control.Monad.IO.Class as MonadIO (MonadIO, liftIO)
import qualified MonadUtils as MonadUtils (MonadIO, liftIO)
import NameSet
import Name
......@@ -81,6 +84,9 @@ write x = when debug $ liftIO $ hPutStrLn stderr $ "DEBUG: " ++ x
type Interpreter = Ghc
instance MonadIO.MonadIO Interpreter where
liftIO = MonadUtils.liftIO
globalImports :: [String]
globalImports =
[ "import IHaskell.Display"
......@@ -405,8 +411,8 @@ evalCommand _ (Directive SetOpt option) state = do
setOpt _ _ = Nothing
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $
case words cmd of
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $
case words cmd of
"cd":dirs ->
let directory = unwords dirs in do
setCurrentDirectory directory
......@@ -422,13 +428,13 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
std_err = UseHandle handle
}
(_, _, _, process) <- createProcess procSpec
-- Accumulate output from the process.
outputAccum <- liftIO $ newMVar ""
-- Start a loop to publish intermediate results.
let
-- Compute how long to wait between reading pieces of the output.
let
-- Compute how long to wait between reading pieces of the output.
-- `threadDelay` takes an argument of microseconds.
ms = 1000
delay = 100 * ms
......@@ -457,7 +463,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
if not computationDone
then do
-- Write to frontend and repeat.
readMVar outputAccum >>= output
readMVar outputAccum >>= output
loop
else do
out <- readMVar outputAccum
......@@ -470,7 +476,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
html $ printf "<span class='mono'>%s</span>" out ++ htmlErr]
loop
-- This is taken largely from GHCi's info section in InteractiveUI.
evalCommand _ (Directive GetHelp _) state = do
......@@ -687,7 +693,7 @@ evalCommand _ (Declaration decl) state = wrapExecution state $ do
names <- runDecls decl
dflags <- getSessionDynFlags
let boundNames = map (replace ":Interactive." "" . showPpr dflags) names
let boundNames = map (replace ":Interactive." "" . showPpr dflags) names
nonDataNames = filter (not . isUpper . head) boundNames
-- Display the types of all bound names if the option is on.
......@@ -981,7 +987,7 @@ formatType :: String -> [DisplayData]
formatType typeStr = [plain typeStr, html $ formatGetType typeStr]
displayError :: ErrMsg -> [DisplayData]
displayError msg = [plain . fixStdinError . typeCleaner $ msg, html $ formatError msg]
displayError msg = [plain . fixStdinError . typeCleaner $ msg, html $ formatError msg]
fixStdinError :: ErrMsg -> ErrMsg
fixStdinError err =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment