[Git][ghc/ghc][wip/run-mode] ghc: Introduce --run mode

Ben Gamari gitlab at gitlab.haskell.org
Fri Apr 3 22:57:36 UTC 2020



Ben Gamari pushed to branch wip/run-mode at Glasgow Haskell Compiler / GHC


Commits:
bffce3fd by Ben Gamari at 2020-04-03T18:57:29-04:00
ghc: Introduce --run mode

As described in #18011, this

- - - - -


6 changed files:

- docs/users_guide/using.rst
- ghc/Main.hs
- + testsuite/tests/driver/RunMode.hs
- + testsuite/tests/driver/RunMode.stdout
- + testsuite/tests/driver/RunMode/Test.hs
- testsuite/tests/driver/all.T


Changes:

=====================================
docs/users_guide/using.rst
=====================================
@@ -265,6 +265,17 @@ The available mode flags are:
     Interactive mode, which is also available as :program:`ghci`. Interactive
     mode is described in more detail in :ref:`ghci`.
 
+.. ghc-flag:: --run
+    :shortdesc: Run a Haskell program.
+    :type: mode
+    :category: modes
+
+    .. index::
+       single: run mode
+       single: GHCi
+
+    Run a script's ``main`` binding using the bytecode interpreter.
+
 .. ghc-flag:: --make
     :shortdesc: Build a multi-module Haskell program, automatically figuring out
         dependencies. Likely to be much easier, and faster, than using


=====================================
ghc/Main.hs
=====================================
@@ -161,6 +161,7 @@ main' postLoadMode dflags0 args flagWarnings = do
          = case postLoadMode of
                DoInteractive   -> (CompManager, HscInterpreted, LinkInMemory)
                DoEval _        -> (CompManager, HscInterpreted, LinkInMemory)
+               DoRun           -> (CompManager, HscInterpreted, LinkInMemory)
                DoMake          -> (CompManager, dflt_target,    LinkBinary)
                DoBackpack      -> (CompManager, dflt_target,    LinkBinary)
                DoMkDependHS    -> (MkDepend,    dflt_target,    LinkBinary)
@@ -172,6 +173,7 @@ main' postLoadMode dflags0 args flagWarnings = do
                          ghcLink   = link,
                          verbosity = case postLoadMode of
                                          DoEval _ -> 0
+                                         DoRun    -> 0
                                          _other   -> 1
                         }
 
@@ -185,6 +187,7 @@ main' postLoadMode dflags0 args flagWarnings = do
       -- a great story for the moment.
       dflags2  | DoInteractive <- postLoadMode = def_ghci_flags
                | DoEval _      <- postLoadMode = def_ghci_flags
+               | DoRun         <- postLoadMode = def_ghci_flags
                | otherwise                     = dflags1
         where def_ghci_flags = dflags1 `gopt_set` Opt_ImplicitImportQualified
                                        `gopt_set` Opt_IgnoreOptimChanges
@@ -271,6 +274,7 @@ main' postLoadMode dflags0 args flagWarnings = do
        DoInteractive          -> ghciUI hsc_env dflags6 srcs Nothing
        DoEval exprs           -> ghciUI hsc_env dflags6 srcs $ Just $
                                    reverse exprs
+       DoRun                  -> ghciUI hsc_env dflags6 srcs (Just ["main"])
        DoAbiHash              -> abiHash (map fst srcs)
        ShowPackages           -> liftIO $ showPackages dflags6
        DoFrontend f           -> doFrontend f srcs
@@ -488,15 +492,17 @@ data PostLoadMode
   | DoBackpack              -- ghc --backpack foo.bkp
   | DoInteractive           -- ghc --interactive
   | DoEval [String]         -- ghc -e foo -e bar => DoEval ["bar", "foo"]
+  | DoRun                   -- ghc --run
   | DoAbiHash               -- ghc --abi-hash
   | ShowPackages            -- ghc --show-packages
   | DoFrontend ModuleName   -- ghc --frontend Plugin.Module
 
-doMkDependHSMode, doMakeMode, doInteractiveMode,
+doMkDependHSMode, doMakeMode, doInteractiveMode, doRunMode,
   doAbiHashMode, showPackagesMode :: Mode
 doMkDependHSMode = mkPostLoadMode DoMkDependHS
 doMakeMode = mkPostLoadMode DoMake
 doInteractiveMode = mkPostLoadMode DoInteractive
+doRunMode = mkPostLoadMode DoRun
 doAbiHashMode = mkPostLoadMode DoAbiHash
 showPackagesMode = mkPostLoadMode ShowPackages
 
@@ -557,11 +563,13 @@ needsInputsMode _               = False
 isLinkMode :: PostLoadMode -> Bool
 isLinkMode (StopBefore StopLn) = True
 isLinkMode DoMake              = True
+isLinkMode DoRun               = True
 isLinkMode DoInteractive       = True
 isLinkMode (DoEval _)          = True
 isLinkMode _                   = False
 
 isCompManagerMode :: PostLoadMode -> Bool
+isCompManagerMode DoRun         = True
 isCompManagerMode DoMake        = True
 isCompManagerMode DoInteractive = True
 isCompManagerMode (DoEval _)    = True
@@ -643,6 +651,7 @@ mode_flags =
   , defFlag "E"            (PassFlag (setMode (stopBeforeMode anyHsc)))
   , defFlag "C"            (PassFlag (setMode (stopBeforeMode HCc)))
   , defFlag "S"            (PassFlag (setMode (stopBeforeMode (As False))))
+  , defFlag "-run"         (PassFlag (setMode doRunMode))
   , defFlag "-make"        (PassFlag (setMode doMakeMode))
   , defFlag "-backpack"    (PassFlag (setMode doBackpackMode))
   , defFlag "-interactive" (PassFlag (setMode doInteractiveMode))


=====================================
testsuite/tests/driver/RunMode.hs
=====================================
@@ -0,0 +1,4 @@
+import Test
+
+main :: IO ()
+main = test


=====================================
testsuite/tests/driver/RunMode.stdout
=====================================
@@ -0,0 +1 @@
+hello


=====================================
testsuite/tests/driver/RunMode/Test.hs
=====================================
@@ -0,0 +1,4 @@
+module Test where
+
+test :: IO ()
+test = putStrLn "hello"


=====================================
testsuite/tests/driver/all.T
=====================================
@@ -279,3 +279,4 @@ test('T16737',
 
 test('T17143', exit_code(1), run_command, ['{compiler} T17143.hs -S -fno-code'])
 test('T17786', unless(opsys('mingw32'), skip), makefile_test, [])
+test('RunMode', extra_files(['RunMode/Test.hs']), run_command, ['{compiler} --run -iRunMode/ -ignore-dot-ghci RunMode.hs'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bffce3fda867c9736dd4c07f60b004047fbdf5a2

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bffce3fda867c9736dd4c07f60b004047fbdf5a2
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200403/2c773d84/attachment-0001.html>


More information about the ghc-commits mailing list