From c8b37bf43c61c2fc42ec6ba4ad57f631a59fc2d4 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Sat, 19 Jan 2008 22:30:36 +0000 Subject: [PATCH] Support multiple -e flags --- compiler/ghci/InteractiveUI.hs | 21 +++++++++++---------- compiler/main/Main.hs | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/compiler/ghci/InteractiveUI.hs b/compiler/ghci/InteractiveUI.hs index f4c27d2..fdf32dc 100644 --- a/compiler/ghci/InteractiveUI.hs +++ b/compiler/ghci/InteractiveUI.hs @@ -280,8 +280,9 @@ findEditor = do return "" #endif -interactiveUI :: Session -> [(FilePath, Maybe Phase)] -> Maybe String -> IO () -interactiveUI session srcs maybe_expr = do +interactiveUI :: Session -> [(FilePath, Maybe Phase)] -> Maybe [String] + -> IO () +interactiveUI session srcs maybe_exprs = do -- HACK! If we happen to get into an infinite loop (eg the user -- types 'let x=x in x' at the prompt), then the thread will block -- on a blackhole, and become unreachable during GC. The GC will @@ -297,7 +298,7 @@ interactiveUI session srcs maybe_expr = do -- Initialise buffering for the *interpreted* I/O system initInterpBuffering session - when (isNothing maybe_expr) $ do + when (isNothing maybe_exprs) $ do -- Only for GHCi (not runghc and ghc -e): -- Turn buffering off for the compiled program's stdout/stderr @@ -328,7 +329,7 @@ interactiveUI session srcs maybe_expr = do default_editor <- findEditor - startGHCi (runGHCi srcs maybe_expr) + startGHCi (runGHCi srcs maybe_exprs) GHCiState{ progname = "", args = [], prompt = "%s> ", @@ -351,8 +352,8 @@ interactiveUI session srcs maybe_expr = do return () -runGHCi :: [(FilePath, Maybe Phase)] -> Maybe String -> GHCi () -runGHCi paths maybe_expr = do +runGHCi :: [(FilePath, Maybe Phase)] -> Maybe [String] -> GHCi () +runGHCi paths maybe_exprs = do let read_dot_files = not opt_IgnoreDotGhci when (read_dot_files) $ do @@ -390,7 +391,7 @@ runGHCi paths maybe_expr = do when (not (null paths)) $ do ok <- ghciHandle (\e -> do showException e; return Failed) $ loadModule paths - when (isJust maybe_expr && failed ok) $ + when (isJust maybe_exprs && failed ok) $ io (exitWith (ExitFailure 1)) -- if verbosity is greater than 0, or we are connected to a @@ -399,7 +400,7 @@ runGHCi paths maybe_expr = do dflags <- getDynFlags let show_prompt = verbosity dflags > 0 || is_tty - case maybe_expr of + case maybe_exprs of Nothing -> do #if defined(mingw32_HOST_OS) @@ -415,9 +416,9 @@ runGHCi paths maybe_expr = do #endif -- enter the interactive loop interactiveLoop is_tty show_prompt - Just expr -> do + Just exprs -> do -- just evaluate the expression we were given - enqueueCommands [expr] + enqueueCommands exprs let handleEval (ExitException code) = io (exitWith code) handleEval e = handler e runCommands' handleEval (return Nothing) diff --git a/compiler/main/Main.hs b/compiler/main/Main.hs index d41e590..730c43e 100644 --- a/compiler/main/Main.hs +++ b/compiler/main/Main.hs @@ -179,7 +179,7 @@ main = DoMkDependHS -> doMkDependHS session (map fst srcs) StopBefore p -> oneShot dflags p srcs DoInteractive -> interactiveUI session srcs Nothing - DoEval expr -> interactiveUI session srcs (Just expr) + DoEval exprs -> interactiveUI session srcs $ Just $ reverse exprs dumpFinalStats dflags exitWith ExitSuccess @@ -316,7 +316,7 @@ data CmdLineMode -- StopBefore StopLn is the default | DoMake -- ghc --make | DoInteractive -- ghc --interactive - | DoEval String -- ghc -e + | DoEval [String] -- ghc -e foo -e bar => DoEval ["bar", "foo"] deriving (Show) isInteractiveMode, isInterpretiveMode :: CmdLineMode -> Bool @@ -386,7 +386,7 @@ mode_flags = , ( "S" , PassFlag (setMode (StopBefore As))) , ( "-make" , PassFlag (setMode DoMake)) , ( "-interactive" , PassFlag (setMode DoInteractive)) - , ( "e" , HasArg (\s -> setMode (DoEval s) "-e")) + , ( "e" , HasArg (\s -> updateMode (updateDoEval s) "-e")) -- -fno-code says to stop after Hsc but don't generate any code. , ( "fno-code" , PassFlag (\f -> do setMode (StopBefore HCc) f @@ -395,12 +395,19 @@ mode_flags = ] setMode :: CmdLineMode -> String -> ModeM () -setMode m flag = do +setMode m flag = updateMode (\_ -> m) flag + +updateDoEval :: String -> CmdLineMode -> CmdLineMode +updateDoEval expr (DoEval exprs) = DoEval (expr : exprs) +updateDoEval expr _ = DoEval [expr] + +updateMode :: (CmdLineMode -> CmdLineMode) -> String -> ModeM () +updateMode f flag = do (old_mode, old_flag, flags) <- getCmdLineState if notNull old_flag && flag /= old_flag then throwDyn (UsageError ("cannot use `" ++ old_flag ++ "' with `" ++ flag ++ "'")) - else putCmdLineState (m, flag, flags) + else putCmdLineState (f old_mode, flag, flags) addFlag :: String -> ModeM () addFlag s = do -- 1.7.10.4