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.rstindex 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.hsindex c1b5c0b..ed017c9 100644--- a/exe/RadicleExe.hs+++ b/exe/RadicleExe.hs@@ -7,30 +7,34 @@ import Options.Applicativeimport System.Directory (doesFileExist)import Radicle+import Radicle.Internal.Effects (exitCode)import Radicle.Internal.HttpStorageimport Radicle.Internal.Pretty (putPrettyAnsi)main :: IO ()main = doopts' <- 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 codewhereallOpts = info (opts <**> helper)( fullDescdiff --git a/rad/repl.rad b/rad/repl.radindex 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 repldiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 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 Intderiving (Eq, Show, Generic, Functor)throwErrorHere :: (MonadError (LangError Value) m, HasCallStack) => LangErrorData Value -> m a@@ -158,7 +158,7 @@ errorDataToValue e = case e ofNoMatch -> 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)])wheremakeA = quote . AtommakeVal (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.hsindex 586fccd..f15d67e 100644--- a/src/Radicle/Internal/Effects.hs+++ b/src/Radicle/Internal/Effects.hs@@ -24,6 +24,7 @@ import Radicle.Internal.Cryptoimport Radicle.Internal.Effects.Capabilitiesimport Radicle.Internal.Identifier (Ident(..), unsafeToIdent)import Radicle.Internal.Interpret+import Radicle.Internal.Number (isInt)import Radicle.Internal.Prettyimport Radicle.Internal.PrimFnsimport Radicle.Internal.Type (Type(..))@@ -48,7 +49,7 @@ instance UUID.MonadUUID (InputT IO) whereinstance UUID.MonadUUID m => UUID.MonadUUID (LangT (Bindings (PrimFns m)) m) whereuuid = lift UUID.uuid-repl :: Maybe FilePath -> Text -> Text -> Bindings (PrimFns (InputT IO)) -> IO ()+repl :: Maybe FilePath -> Text -> Text -> Bindings (PrimFns (InputT IO)) -> IO ExitCoderepl histFile preFileName preCode bindings = dolet settings = setComplete completion$ defaultSettings { historyFile = histFile }@@ -56,9 +57,14 @@ repl histFile preFileName preCode bindings = do$ fmap fst $ runLang bindings$ void $ interpretMany preFileName preCodecase 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 ncompletion :: Monad m => CompletionFunc mcompletion = completeWord Nothing ['(', ')', ' ', '\n'] go@@ -148,7 +154,7 @@ replPrimFns sysArgs = fromList $ allDocs $st <- getjoin $ (action >> pure (loop action)) `catchError`(\err -> case err of- LangError _ Exit -> pure (pure nil)+ LangError _ (Exit _) -> pure (pure nil)LangError _ (Impossible _) -> doputPrettyAnsi errthrowError 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.hsindex 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) wherehCloseS = lift . hCloseSopenFileS 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 wherecurrentTime :: m UTCTimediff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 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) whereOtherError t -> "Error:" <+> pretty tSpecialForm 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 codeThrownError i val -> "Exception" <+> pretty i <+> prettyV valPatternMatchError e -> case e ofNoMatch -> "Pattern match(es) are non-exhaustive."diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 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 [] progres @?= 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]whererun stdin' prog =