Skip to content

A peer-to-peer stack for code collaboration

Make runLang also return the state.

Julian K. Arni3c50c3c209e8
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index aaeeae4..c8d9a0a 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -179,6 +179,24 @@ data Bindings m = Bindings
, bindingsNextRef :: Int
} deriving (Generic)
+instance Semigroup (Bindings m) where
+ (<>) = mappend
+
+instance Monoid (Bindings m) where
+ mempty = Bindings
+ { bindingsEnv = mempty
+ , bindingsPrimops = mempty
+ , bindingsRefs = mempty
+ , bindingsNextRef = 0
+ }
+ a `mappend` b = Bindings
+ { bindingsEnv = bindingsEnv a <> bindingsEnv b
+ , bindingsPrimops = bindingsPrimops a <> bindingsPrimops b
+ , bindingsRefs = bindingsRefs a
+ <> IntMap.mapKeys (+ bindingsNextRef a) (bindingsRefs b)
+ , bindingsNextRef = bindingsNextRef a + bindingsNextRef b
+ }
+
-- | The environment in which expressions are evaluated.
newtype LangT r m a = LangT
{ fromLangT :: ExceptT (LangError (Value Reference)) (StateT r m) a }
@@ -194,8 +212,9 @@ runLang
:: Monad m
=> Bindings m
-> Lang m a
- -> m (Either (LangError (Value Reference)) a)
-runLang e l = evalStateT (runExceptT $ fromLangT l) e
+ -> m (Either (LangError (Value Reference)) a, Bindings m)
+runLang e l = runStateT (runExceptT $ fromLangT l) e
+
-- | Like 'local' or 'withState'
withEnv :: Monad m => (Bindings m -> Bindings m) -> Lang m a -> Lang m a
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 6ce370a..5acaf8b 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -144,13 +144,13 @@ interpret sourceName expr bnds = do
parsed = runReader (runParserT (valueP <* eof) sourceName expr) primopNames
case parsed of
Left e -> pure . Left $ ParseError e
- Right v -> runLang bnds (makeRefs v >>= eval)
+ Right v -> fst <$> runLang bnds (makeRefs v >>= eval)
-- | Parse and evaluate a Text as multiple expressions.
--
-- Examples:
--
--- >>> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
+-- >>> fmap fst <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
-- Right (Boolean True)
interpretMany :: Monad m => String -> Text -> Lang m (Value Reference)
interpretMany sourceName src = do
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 130fe1d..cce962e 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -33,7 +33,7 @@ repl histFile preCode = do
let settings = setComplete completion
$ defaultSettings { historyFile = Just histFile }
r <- runInputT settings
- $ runLang replBindings
+ $ fmap fst $ runLang replBindings
$ void $ interpretMany "[pre]" preCode
case r of
Left Exit -> pure ()
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 270716c..c76aba9 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -30,7 +30,7 @@ runTestWith bindings inputs action =
, worldStateStdout = []
, worldStateEnv = bindingsEnv bindings
}
- in case runState (runLang bindings $ interpretMany "[test]" action) ws of
+ in case runState (fmap fst $ runLang bindings $ interpretMany "[test]" action) ws of
(val, st) -> (val, reverse $ worldStateStdout st)
-- | Like `runTestWith`, but uses the pureEnv