Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #34 from psylinse/multiline-eval
Browse files Browse the repository at this point in the history
Support multiline eval.
  • Loading branch information
heyLu committed Oct 4, 2014
2 parents 78bc206 + 9047776 commit 9066dd5
Show file tree
Hide file tree
Showing 5 changed files with 3,332 additions and 3,174 deletions.
27 changes: 25 additions & 2 deletions haskell/ReplSession.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import System.Process
import System.Directory (getDirectoryContents)
import Data.List (isSuffixOf)
import Control.Monad (liftM)
import Language.Haskell.Exts

data ReplSession = ReplSession {
replIn :: Handle,
Expand All @@ -20,11 +21,33 @@ data ReplSession = ReplSession {

evalInSession :: String -> ReplSession -> IO (Either String String)
evalInSession cmd session@(ReplSession input out err _) = do
clearHandle out 0
sendCommand (":{\n" ++ prepareCode cmd ++ "\n") session
clearHandle out 10
clearHandle err 0
sendCommand (cmd ++ "\n") session
sendCommand ":}\n" session
readEvalOutput session

prepareCode :: String -> String
prepareCode code = case parseCode code of
[] -> code
decls -> if isFunDecl $ head decls
then prependLet decls
else code

isFunDecl :: Decl -> Bool
isFunDecl (TypeSig {}) = True
isFunDecl (FunBind {}) = True
isFunDecl (PatBind {}) = True
isFunDecl _ = False

prependLet :: [Decl] -> String
prependLet = prettyPrint . letStmt

parseCode :: String -> [Decl]
parseCode code = case parseFileContents code of
(ParseOk (Module _ _ _ _ _ _ decls)) -> decls
(ParseFailed {}) -> []

readEvalOutput :: ReplSession -> IO (Either String String)
readEvalOutput (ReplSession _ out err _) = do
output <- readUntil out ("--EvalFinished\n" `isSuffixOf`)
Expand Down
Loading

0 comments on commit 9066dd5

Please sign in to comment.