Skip to content

A peer-to-peer stack for code collaboration

Cleanly exit on ctrl-D or (exit!)

Luke Palmerc0154828a66c
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index fa2804e..24a7f7a 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -90,7 +90,7 @@ main = do
docs = ["doc", "doc!", "apropos!", "document", "should-be"]
io =
["print!", "get-line!", "load!", "read-file!", "read-code!", "send-code!"
- , "send-prelude!", "subscribe-to!", "uuid!", "read-line!"]
+ , "send-prelude!", "subscribe-to!", "uuid!", "read-line!", "exit!"]
lens = ["@", "make-lens", "view", "view-ref", "set", "set-ref", "over", "over-ref", "id-lens", "..", "..."]
crypto = ["verify-signature", "default-ecc-curve", "gen-key-pair!", "gen-signature!"]
chainTools =
diff --git a/rad/repl.rad b/rad/repl.rad
index 4c4f823..c08c266 100644
--- a/rad/repl.rad
+++ b/rad/repl.rad
@@ -11,10 +11,15 @@
Type (doc '<name>) for further documentation of <name>."))
+(def read-line-or-exit! (fn []
+ (def line (get-line!))
+ (if (eq? line nil)
+ (exit!)
+ (read line))))
(def repl
{:name "repl"
;; Parse errors are thrown here
- :getter read-line!})
+ :getter read-line-or-exit!})
(subscribe-to! repl print!)
diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hs
index 445893b..eece9c6 100644
--- a/src/Radicle/Internal/Effects.hs
+++ b/src/Radicle/Internal/Effects.hs
@@ -121,7 +121,7 @@ replPrimFns = fromList $ allDocs $
, ( "get-line!"
, [md|Reads a single line of input and returns it as a string.|]
, \case
- [] -> String <$> getLineS
+ [] -> maybe nil toRad <$> getLineS
xs -> throwErrorHere $ WrongNumberOfArgs "get-line!" 0 (length xs)
)
@@ -136,17 +136,20 @@ replPrimFns = fromList $ allDocs $
(Dict m, fn) -> case Map.lookup (Keyword $ unsafeToIdent "getter") m of
Nothing -> throwErrorHere
$ OtherError "subscribe-to!: Expected ':getter' key"
- Just g -> forever (protect go)
+ Just g -> loop go
where
- protect action = do
+ loop action = do
st <- get
- action `catchError`
+ join $ (action >> pure (loop action)) `catchError`
(\err -> case err of
- LangError _ Exit -> throwError err
+ LangError _ Exit -> pure (pure nil)
LangError _ (Impossible _) -> do
putStrS (renderPrettyDef err)
throwError err
- _ -> putStrS (renderPrettyDef err) >> put st)
+ _ -> do
+ putStrS (renderPrettyDef err)
+ put st
+ pure (loop action))
go = do
-- We need to evaluate the getter in the original
-- environment in which it was defined, but the
@@ -202,4 +205,10 @@ replPrimFns = fromList $ allDocs $
[] -> String <$> UUID.uuid
xs -> throwErrorHere $ WrongNumberOfArgs "uuid!" 0 (length xs)
)
+ , ( "exit!"
+ , [md|Exit the interpreter immediately.|]
+ , \case
+ [] -> throwErrorHere Exit
+ xs -> throwErrorHere $ WrongNumberOfArgs "exit!" 0 (length xs)
+ )
]
diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
index da21783..a0a58df 100644
--- a/src/Radicle/Internal/Effects/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -17,13 +17,11 @@ import GHCJS.DOM.XMLHttpRequest
import Radicle.Internal.Core
class (Monad m) => Stdin m where
- getLineS :: m Text
+ getLineS :: m (Maybe Text) -- gives Nothing on EOF
instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang m) where
getLineS = lift getLineS
-instance (MonadException m, Monad m) => Stdin (InputT m) where
- getLineS = getInputLine "rad> " >>= \x -> case x of
- Nothing -> panic "curious about why this would happen"
- Just v -> pure . toS $ v
+instance (MonadException m) => Stdin (InputT m) where
+ getLineS = (fmap.fmap) toS (getInputLine "rad> ")
class (Monad m) => Stdout m where
putStrS :: Text -> m ()
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index b78ece3..1b5d938 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -84,8 +84,8 @@ instance {-# OVERLAPPING #-} Stdin TestLang where
getLineS = do
ws <- lift get
case worldStateStdin ws of
- [] -> throwErrorHere Exit
- h:hs -> lift (put $ ws { worldStateStdin = hs }) >> pure h
+ [] -> pure Nothing
+ h:hs -> lift (put $ ws { worldStateStdin = hs }) >> pure (Just h)
instance {-# OVERLAPPING #-} Stdout TestLang where
putStrS t = lift $