Commit f51efb3f authored by Andrei Barbu's avatar Andrei Barbu Committed by GitHub

Merge pull request #3 from vaibhavsagar/execStmt

Replace runStmt with execStmt
parents 5f88dcbe 3f9a4226
...@@ -179,7 +179,7 @@ interpret libdir allowedStdin action = runGhc (Just libdir) $ do ...@@ -179,7 +179,7 @@ interpret libdir allowedStdin action = runGhc (Just libdir) $ do
dir <- liftIO getIHaskellDir dir <- liftIO getIHaskellDir
let cmd = printf "IHaskell.IPython.Stdin.fixStdin \"%s\"" dir let cmd = printf "IHaskell.IPython.Stdin.fixStdin \"%s\"" dir
when (allowedStdin && hasSupportLibraries) $ void $ when (allowedStdin && hasSupportLibraries) $ void $
runStmt cmd RunToCompletion execStmt cmd execOptions
initializeItVariable initializeItVariable
...@@ -237,9 +237,9 @@ initializeImports = do ...@@ -237,9 +237,9 @@ initializeImports = do
guard (iHaskellPkgName `isPrefixOf` idString) guard (iHaskellPkgName `isPrefixOf` idString)
displayPkgs = [ pkgName displayPkgs = [ pkgName
| pkgName <- packageNames | pkgName <- packageNames
, Just (x:_) <- [stripPrefix initStr pkgName] , Just (x:_) <- [stripPrefix initStr pkgName]
, pkgName `notElem` broken , pkgName `notElem` broken
, isAlpha x ] , isAlpha x ]
hasIHaskellPackage = not $ null $ filter (== iHaskellPkgName) packageNames hasIHaskellPackage = not $ null $ filter (== iHaskellPkgName) packageNames
...@@ -279,7 +279,7 @@ initializeItVariable :: Interpreter () ...@@ -279,7 +279,7 @@ initializeItVariable :: Interpreter ()
initializeItVariable = initializeItVariable =
-- This is required due to the way we handle `it` in the wrapper statements - if it doesn't exist, -- This is required due to the way we handle `it` in the wrapper statements - if it doesn't exist,
-- the first statement will fail. -- the first statement will fail.
void $ runStmt "let it = ()" RunToCompletion void $ execStmt "let it = ()" execOptions
-- | Publisher for IHaskell outputs. The first argument indicates whether this output is final -- | Publisher for IHaskell outputs. The first argument indicates whether this output is final
-- (true) or intermediate (false). -- (true) or intermediate (false).
...@@ -515,7 +515,7 @@ evalCommand _ (Module contents) state = wrapExecution state $ do ...@@ -515,7 +515,7 @@ evalCommand _ (Module contents) state = wrapExecution state $ do
-- Remember which modules we've loaded before. -- Remember which modules we've loaded before.
importedModules <- getContext importedModules <- getContext
let let
-- Get the dot-delimited pieces of the module name. -- Get the dot-delimited pieces of the module name.
moduleNameOf :: InteractiveImport -> [String] moduleNameOf :: InteractiveImport -> [String]
moduleNameOf (IIDecl decl) = split "." . moduleNameString . unLoc . ideclName $ decl moduleNameOf (IIDecl decl) = split "." . moduleNameString . unLoc . ideclName $ decl
...@@ -694,7 +694,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ ...@@ -694,7 +694,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
let cmd = printf "IHaskellDirectory.setCurrentDirectory \"%s\"" $ let cmd = printf "IHaskellDirectory.setCurrentDirectory \"%s\"" $
replace " " "\\ " $ replace " " "\\ " $
replace "\"" "\\\"" directory replace "\"" "\\\"" directory
runStmt cmd RunToCompletion execStmt cmd execOptions
return mempty return mempty
else return $ displayError $ printf "No such directory: '%s'" directory else return $ displayError $ printf "No such directory: '%s'" directory
cmd -> liftIO $ do cmd -> liftIO $ do
...@@ -711,7 +711,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ ...@@ -711,7 +711,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
outputAccum <- liftIO $ newMVar "" outputAccum <- liftIO $ newMVar ""
-- Start a loop to publish intermediate results. -- Start a loop to publish intermediate results.
let let
-- Compute how long to wait between reading pieces of the output. `threadDelay` takes an -- Compute how long to wait between reading pieces of the output. `threadDelay` takes an
-- argument of microseconds. -- argument of microseconds.
ms = 1000 ms = 1000
...@@ -756,7 +756,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ ...@@ -756,7 +756,7 @@ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
] ]
loop loop
where where
#if MIN_VERSION_base(4,8,0) #if MIN_VERSION_base(4,8,0)
createPipe' = createPipe createPipe' = createPipe
#else #else
...@@ -852,7 +852,7 @@ evalCommand output (Expression expr) state = do ...@@ -852,7 +852,7 @@ evalCommand output (Expression expr) state = do
let widgetExpr = printf "(IHaskell.Display.Widget (%s))" expr :: String let widgetExpr = printf "(IHaskell.Display.Widget (%s))" expr :: String
isWidget <- attempt $ exprType widgetExpr isWidget <- attempt $ exprType widgetExpr
-- Check if this is a template haskell declaration -- Check if this is a template haskell declaration
let declExpr = printf "((id :: IHaskellTH.DecsQ -> IHaskellTH.DecsQ) (%s))" expr :: String let declExpr = printf "((id :: IHaskellTH.DecsQ -> IHaskellTH.DecsQ) (%s))" expr :: String
let anyExpr = printf "((id :: IHaskellPrelude.Int -> IHaskellPrelude.Int) (%s))" expr :: String let anyExpr = printf "((id :: IHaskellPrelude.Int -> IHaskellPrelude.Int) (%s))" expr :: String
isTHDeclaration <- liftM2 (&&) (attempt $ exprType declExpr) (not <$> attempt (exprType anyExpr)) isTHDeclaration <- liftM2 (&&) (attempt $ exprType declExpr) (not <$> attempt (exprType anyExpr))
...@@ -862,7 +862,7 @@ evalCommand output (Expression expr) state = do ...@@ -862,7 +862,7 @@ evalCommand output (Expression expr) state = do
write state $ "Is Declaration: " ++ show isTHDeclaration write state $ "Is Declaration: " ++ show isTHDeclaration
if isTHDeclaration if isTHDeclaration
then then
-- If it typechecks as a DecsQ, we do not want to display the DecsQ, we just want the -- If it typechecks as a DecsQ, we do not want to display the DecsQ, we just want the
-- declaration made. -- declaration made.
do do
...@@ -877,7 +877,7 @@ evalCommand output (Expression expr) state = do ...@@ -877,7 +877,7 @@ evalCommand output (Expression expr) state = do
, evalMsgs = [] , evalMsgs = []
} }
else if canRunDisplay else if canRunDisplay
then then
-- Use the display. As a result, `it` is set to the output. -- Use the display. As a result, `it` is set to the output.
useDisplay displayExpr useDisplay displayExpr
else do else do
...@@ -1119,7 +1119,7 @@ keepingItVariable act = do ...@@ -1119,7 +1119,7 @@ keepingItVariable act = do
gen <- liftIO getStdGen gen <- liftIO getStdGen
let rand = take 20 $ randomRs ('0', '9') gen let rand = take 20 $ randomRs ('0', '9') gen
var name = name ++ rand var name = name ++ rand
goStmt s = runStmt s RunToCompletion goStmt s = execStmt s execOptions
itVariable = var "it_var_temp_" itVariable = var "it_var_temp_"
goStmt $ printf "let %s = it" itVariable goStmt $ printf "let %s = it" itVariable
...@@ -1132,12 +1132,12 @@ data Captured a = CapturedStmt String ...@@ -1132,12 +1132,12 @@ data Captured a = CapturedStmt String
capturedEval :: (String -> IO ()) -- ^ Function used to publish intermediate output. capturedEval :: (String -> IO ()) -- ^ Function used to publish intermediate output.
-> Captured a -- ^ Statement to evaluate. -> Captured a -- ^ Statement to evaluate.
-> Interpreter (String, RunResult) -- ^ Return the output and result. -> Interpreter (String, ExecResult) -- ^ Return the output and result.
capturedEval output stmt = do capturedEval output stmt = do
-- Generate random variable names to use so that we cannot accidentally override the variables by -- Generate random variable names to use so that we cannot accidentally override the variables by
-- using the right names in the terminal. -- using the right names in the terminal.
gen <- liftIO getStdGen gen <- liftIO getStdGen
let let
-- Variable names generation. -- Variable names generation.
rand = take 20 $ randomRs ('0', '9') gen rand = take 20 $ randomRs ('0', '9') gen
var name = name ++ rand var name = name ++ rand
...@@ -1173,16 +1173,16 @@ capturedEval output stmt = do ...@@ -1173,16 +1173,16 @@ capturedEval output stmt = do
, printf "let it = %s" itVariable , printf "let it = %s" itVariable
] ]
goStmt :: String -> Ghc RunResult goStmt :: String -> Ghc ExecResult
goStmt s = runStmt s RunToCompletion goStmt s = execStmt s execOptions
runWithResult (CapturedStmt str) = goStmt str runWithResult (CapturedStmt str) = goStmt str
runWithResult (CapturedIO io) = do runWithResult (CapturedIO io) = do
status <- gcatch (liftIO io >> return NoException) (return . AnyException) status <- gcatch (liftIO io >> return NoException) (return . AnyException)
return $ return $
case status of case status of
NoException -> RunOk [] NoException -> ExecComplete (Right []) 0
AnyException e -> RunException e AnyException e -> ExecComplete (Left e) 0
-- Initialize evaluation context. -- Initialize evaluation context.
results <- forM initStmts goStmt results <- forM initStmts goStmt
...@@ -1222,7 +1222,7 @@ capturedEval output stmt = do ...@@ -1222,7 +1222,7 @@ capturedEval output stmt = do
outputAccum <- liftIO $ newMVar "" outputAccum <- liftIO $ newMVar ""
-- Start a loop to publish intermediate results. -- Start a loop to publish intermediate results.
let let
-- Compute how long to wait between reading pieces of the output. `threadDelay` takes an -- Compute how long to wait between reading pieces of the output. `threadDelay` takes an
-- argument of microseconds. -- argument of microseconds.
ms = 1000 ms = 1000
...@@ -1295,7 +1295,7 @@ evalStatementOrIO publish state cmd = do ...@@ -1295,7 +1295,7 @@ evalStatementOrIO publish state cmd = do
(printed, result) <- capturedEval output cmd (printed, result) <- capturedEval output cmd
case result of case result of
RunOk names -> do ExecComplete (Right names) _ -> do
dflags <- getSessionDynFlags dflags <- getSessionDynFlags
let allNames = map (showPpr dflags) names let allNames = map (showPpr dflags) names
...@@ -1327,8 +1327,8 @@ evalStatementOrIO publish state cmd = do ...@@ -1327,8 +1327,8 @@ evalStatementOrIO publish state cmd = do
-- Return plain and html versions. Previously there was only a plain version. -- Return plain and html versions. Previously there was only a plain version.
text -> Display [plain $ joined ++ "\n" ++ text, html $ htmled ++ mono text] text -> Display [plain $ joined ++ "\n" ++ text, html $ htmled ++ mono text]
RunException exception -> throw exception ExecComplete (Left exception) _ -> throw exception
RunBreak{} -> error "Should not break." ExecBreak{} -> error "Should not break."
-- Read from a file handle until we hit a delimiter or until we've read as many characters as -- Read from a file handle until we hit a delimiter or until we've read as many characters as
-- requested -- requested
......
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