Skip to content

A peer-to-peer stack for code collaboration

'exit!' gets an argument

Thomas Scholtes6eaeb4f24f74
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 9736350..8b24949 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -67,7 +67,7 @@ label for the exception, the second can be any value.
``exit!``
~~~~~~~~~
-Exit the interpreter immediately.
+Exit the interpreter immediately with the given exit code.
``read``
~~~~~~~~
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hs
index c1b5c0b..ed017c9 100644
--- a/exe/RadicleExe.hs
+++ b/exe/RadicleExe.hs
@@ -7,30 +7,34 @@ import Options.Applicative
import System.Directory (doesFileExist)
import Radicle
+import Radicle.Internal.Effects (exitCode)
import Radicle.Internal.HttpStorage
import Radicle.Internal.Pretty (putPrettyAnsi)
main :: IO ()
main = do
opts' <- execParser allOpts
- if sourceFile opts' == "-"
- then do
- src <- getContents
- let prog = interpretMany (toS $ sourceFile opts') src
- bindings <- createBindings (toS <$> scriptArgs opts')
- (result, _state) <- runLang bindings prog
- case result of
- Left (LangError _ Exit) -> pure ()
- Left e -> do putPrettyAnsi e
- exitWith (ExitFailure 1)
- Right v -> putPrettyAnsi v
- else do
- src <- ignoreShebang <$> readSource (sourceFile opts')
- hist <- case histFile opts' of
- Nothing -> getHistoryFile
- Just h -> pure h
- bindings <- createBindings (toS <$> scriptArgs opts')
- repl (Just hist) (toS $ sourceFile opts') src bindings
+ code <-
+ if sourceFile opts' == "-"
+ then do
+ src <- getContents
+ let prog = interpretMany (toS $ sourceFile opts') src
+ bindings <- createBindings (toS <$> scriptArgs opts')
+ (result, _state) <- runLang bindings prog
+ case result of
+ Left (LangError _ (Exit n)) -> pure (exitCode n)
+ Left e -> do putPrettyAnsi e
+ pure $ ExitFailure 1
+ Right v -> do putPrettyAnsi v
+ pure ExitSuccess
+ else do
+ src <- ignoreShebang <$> readSource (sourceFile opts')
+ hist <- case histFile opts' of
+ Nothing -> getHistoryFile
+ Just h -> pure h
+ bindings <- createBindings (toS <$> scriptArgs opts')
+ repl (Just hist) (toS $ sourceFile opts') src bindings
+ exitWith code
where
allOpts = info (opts <**> helper)
( fullDesc
diff --git a/rad/repl.rad b/rad/repl.rad
index ad09dbe..cd650ac 100644
--- a/rad/repl.rad
+++ b/rad/repl.rad
@@ -5,7 +5,7 @@
(def read-line-or-exit! (fn []
(def line (get-line!))
(if (eq? line nil)
- (exit!)
+ (exit! 0)
(read-many line))))
(def repl
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 2b50672..f58d5b7 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -86,7 +86,7 @@ data LangErrorData r =
| SendError Text
| PatternMatchError PatternMatchError
-- | Raised if the effectful @exit!@ primitive is evaluated.
- | Exit
+ | Exit Int
deriving (Eq, Show, Generic, Functor)
throwErrorHere :: (MonadError (LangError Value) m, HasCallStack) => LangErrorData Value -> m a
@@ -158,7 +158,7 @@ errorDataToValue e = case e of
NoMatch -> makeVal ( "non-exhaustive-pattern-matches", [])
BadBindings p -> makeVal ( "bad-pattern", [("bad-pattern", p)])
SendError se -> makeVal ( "send-error", [("info", String se)] )
- Exit -> makeVal ("exit", [])
+ Exit code -> makeVal ("exit", [("code", toRad code)])
where
makeA = quote . Atom
makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys (Keyword . Ident) . GhcExts.fromList $ v)
diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hs
index 586fccd..f15d67e 100644
--- a/src/Radicle/Internal/Effects.hs
+++ b/src/Radicle/Internal/Effects.hs
@@ -24,6 +24,7 @@ import Radicle.Internal.Crypto
import Radicle.Internal.Effects.Capabilities
import Radicle.Internal.Identifier (Ident(..), unsafeToIdent)
import Radicle.Internal.Interpret
+import Radicle.Internal.Number (isInt)
import Radicle.Internal.Pretty
import Radicle.Internal.PrimFns
import Radicle.Internal.Type (Type(..))
@@ -48,7 +49,7 @@ instance UUID.MonadUUID (InputT IO) where
instance UUID.MonadUUID m => UUID.MonadUUID (LangT (Bindings (PrimFns m)) m) where
uuid = lift UUID.uuid
-repl :: Maybe FilePath -> Text -> Text -> Bindings (PrimFns (InputT IO)) -> IO ()
+repl :: Maybe FilePath -> Text -> Text -> Bindings (PrimFns (InputT IO)) -> IO ExitCode
repl histFile preFileName preCode bindings = do
let settings = setComplete completion
$ defaultSettings { historyFile = histFile }
@@ -56,9 +57,14 @@ repl histFile preFileName preCode bindings = do
$ fmap fst $ runLang bindings
$ void $ interpretMany preFileName preCode
case r of
- Left (LangError _ Exit) -> pure ()
- Left e -> putPrettyAnsi e
- Right () -> pure ()
+ Left (LangError _ (Exit n)) -> pure (exitCode n)
+ Left e -> do putPrettyAnsi e
+ pure $ ExitFailure 1
+ Right () -> pure ExitSuccess
+
+exitCode :: Int -> ExitCode
+exitCode 0 = ExitSuccess
+exitCode n = ExitFailure n
completion :: Monad m => CompletionFunc m
completion = completeWord Nothing ['(', ')', ' ', '\n'] go
@@ -148,7 +154,7 @@ replPrimFns sysArgs = fromList $ allDocs $
st <- get
join $ (action >> pure (loop action)) `catchError`
(\err -> case err of
- LangError _ Exit -> pure (pure nil)
+ LangError _ (Exit _) -> pure (pure nil)
LangError _ (Impossible _) -> do
putPrettyAnsi err
throwError err
@@ -308,10 +314,12 @@ replPrimFns sysArgs = fromList $ allDocs $
v -> throwErrorHere $ TypeError "wait-for-process!" 0 TProcHandle v
)
, ( "exit!"
- , "Exit the interpreter immediately."
- , \case
- [] -> throwErrorHere Exit
- xs -> throwErrorHere $ WrongNumberOfArgs "exit!" 0 (length xs)
+ , "Exit the interpreter immediately with the given exit code."
+ , oneArg "exit!" $ \case
+ Number q -> case isInt q of
+ Left _ -> throwErrorHere $ OtherError $ "exit!: number is not an integer"
+ Right n -> throwErrorHere $ Exit n
+ v -> throwErrorHere $ TypeError "exit!" 0 TNumber v
)
, ( "now!"
, "Returns a timestamp for the current Coordinated Universal Time (UTC), right now, formatted according to ISO 8601."
diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
index b5b87c9..42574d7 100644
--- a/src/Radicle/Internal/Effects/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -101,11 +101,6 @@ instance System m => System (InputT m) where
hCloseS = lift . hCloseS
openFileS f mode = lift $ openFileS f mode
-class (Monad m) => Exit m where
- exitS :: m ()
-instance Exit m => Exit (Lang m) where
- exitS = lift exitS
-
class Monad m => CurrentTime m where
currentTime :: m UTCTime
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index 1cc8337..c7e9415 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -115,7 +115,7 @@ instance PrettyV r => PrettyV (LangErrorData r) where
OtherError t -> "Error:" <+> pretty t
SpecialForm f t -> "Error using special form" <+> pretty f <> ":" <+> pretty t <> "."
ParseError t -> "Parser error:" <+> pretty (parseErrorPretty t)
- Exit -> "Exit"
+ Exit code -> "Exit with code " <+> pretty code
ThrownError i val -> "Exception" <+> pretty i <+> prettyV val
PatternMatchError e -> case e of
NoMatch -> "Pattern match(es) are non-exhaustive."
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 7a4af63..a546316 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -698,6 +698,11 @@ test_repl_primops =
let prog = [s| (uuid? (uuid!)) |]
res <- run [] prog
res @?= Right (Boolean True)
+
+ , testCase "'exit!' throws an Exit error with the provided code" $ do
+ let prog = [s|(exit! 5)|]
+ Left (LangError _ errorData) <- run [] prog
+ errorData @?= Exit 5
]
where
run stdin' prog =