Skip to content

A peer-to-peer stack for code collaboration

Some cleanup. (#104)

James Haydon69544ca40b53
diff --git a/package.yaml b/package.yaml
index 3049bb1..49034bd 100644
--- a/package.yaml
+++ b/package.yaml
@@ -70,7 +70,7 @@ tests:
- hpack
executables:
- repl:
+ radicle:
main: Main.hs
source-dirs: exe/Repl
when:
diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hs
index 4554d4c..86369ee 100644
--- a/src/Radicle/Internal.hs
+++ b/src/Radicle/Internal.hs
@@ -5,12 +5,13 @@ module Radicle.Internal
import Protolude
+import Radicle.Internal.CLI as X
import Radicle.Internal.Core as X
+import Radicle.Internal.Interpret as X
import Radicle.Internal.Parse as X
import Radicle.Internal.Pretty as X
import Radicle.Internal.Primops as X
-import Radicle.Internal.Subscriber as X
-import Radicle.Internal.CLI as X
+import Radicle.Internal.Effects as X
import qualified Text.Megaparsec as M
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Effects.hs
similarity index 95%
rename from src/Radicle/Internal/Subscriber.hs
rename to src/Radicle/Internal/Effects.hs
index ab7dfa2..0a27858 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Effects.hs
@@ -1,4 +1,4 @@
-module Radicle.Internal.Subscriber where
+module Radicle.Internal.Effects where
import Protolude hiding (TypeError)
@@ -12,10 +12,11 @@ import System.Console.Haskeline (CompletionFunc, InputT, completeWord,
simpleCompletion)
import Radicle.Internal.Core
-import Radicle.Internal.Parse
+import Radicle.Internal.Interpret
import Radicle.Internal.Pretty
import Radicle.Internal.Primops
-import Radicle.Internal.Subscriber.Capabilities
+import Radicle.Internal.Effects.Capabilities
+
type ReplM m =
@@ -74,7 +75,7 @@ replPrimops = Map.fromList $ first toIdent <$>
, ("get-line!", \args -> case args of
[] -> String <$> getLineS
- xs -> throwError $ WrongNumberOfArgs "get-line!" 0 (length xs))
+ xs -> throwError $ WrongNumberOfArgs "get-line!" 0 (length xs))
, ("subscribe-to!", \args -> case args of
[x, v] -> do
diff --git a/src/Radicle/Internal/Subscriber/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
similarity index 98%
rename from src/Radicle/Internal/Subscriber/Capabilities.hs
rename to src/Radicle/Internal/Effects/Capabilities.hs
index 485e78d..e993845 100644
--- a/src/Radicle/Internal/Subscriber/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -3,7 +3,7 @@
-- The intent is that any set of primops may wear on their sleaves (i.e.
-- constraints) what effects they do.
{-# LANGUAGE CPP #-}
-module Radicle.Internal.Subscriber.Capabilities where
+module Radicle.Internal.Effects.Capabilities where
import Protolude
diff --git a/src/Radicle/Internal/Interpret.hs b/src/Radicle/Internal/Interpret.hs
new file mode 100644
index 0000000..536de91
--- /dev/null
+++ b/src/Radicle/Internal/Interpret.hs
@@ -0,0 +1,61 @@
+module Radicle.Internal.Interpret where
+
+import Protolude
+
+import qualified Data.Map.Strict as Map
+import Text.Megaparsec (eof, runParserT)
+
+import Radicle.Internal.Core
+import Radicle.Internal.Parse
+
+-- | Parse and evaluate a Text.
+--
+-- Examples:
+--
+-- >>> import Control.Monad.Identity
+-- >>> import Radicle.Internal.Primops
+-- >>> runIdentity $ interpret "test" "((lambda (x) x) #t)" pureEnv
+-- Right (Boolean True)
+--
+-- >>> import Control.Monad.Identity
+-- >>> runIdentity $ interpret "test" "(#t #f)" pureEnv
+-- Left (TypeError "Trying to apply a non-function")
+interpret
+ :: Monad m
+ => Text -- ^ Name of source file (for error reporting)
+ -> Text -- ^ Source code to be interpreted
+ -> Bindings m -- ^ Bindings to be used
+ -> m (Either (LangError Value) Value)
+interpret sourceName expr bnds = do
+ let primopNames = Map.keys (bindingsPrimops bnds)
+ parsed = runReader (runParserT (spaceConsumer *> valueP <* eof) (toS sourceName) expr) primopNames
+ case parsed of
+ Left e -> pure . Left $ ParseError e
+ Right v -> fst <$> runLang bnds (eval v)
+
+-- | Parse and evaluate a Text as multiple expressions.
+--
+-- The first argument is the name of the source file to be used for error
+-- reporting.
+--
+-- Examples:
+--
+-- >>> import Radicle.Internal.Primops
+-- >>> fmap fst <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
+-- Right (Boolean True)
+interpretMany
+ :: Monad m
+ => Text -- ^ Name of source file (for error reporting)
+ -> Text -- ^ Source code to be interpreted
+ -> Lang m Value
+interpretMany sourceName src = do
+ primopNames <- gets $ Map.keys . bindingsPrimops
+ let parsed = parseValues sourceName src primopNames
+ case partitionEithers parsed of
+ ([], vs) -> do
+ es <- mapM eval vs
+ case lastMay es of
+ Just e -> pure e
+ _ -> throwError
+ $ OtherError "InterpretMany should be called with at least one expression."
+ (e:_, _) -> throwError $ ParseError e
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 1507066..ee562e5 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -9,7 +9,7 @@ import qualified Data.Text as T
import GHC.Exts (IsString(..))
import Text.Megaparsec (ParsecT, State(..), between, choice,
defaultTabWidth, eof, initialPos, manyTill,
- runParserT, runParserT', sepBy, try, (<?>))
+ runParserT', sepBy, try, (<?>))
import qualified Text.Megaparsec as M
import Text.Megaparsec.Char (char, satisfy, space1)
import qualified Text.Megaparsec.Char.Lexer as L
@@ -110,58 +110,6 @@ valueP = do
-- * Utilities
--- | Parse and evaluate a Text.
---
--- Examples:
---
--- >>> import Control.Monad.Identity
--- >>> import Radicle.Internal.Primops
--- >>> runIdentity $ interpret "test" "((lambda (x) x) #t)" pureEnv
--- Right (Boolean True)
---
--- >>> import Control.Monad.Identity
--- >>> runIdentity $ interpret "test" "(#t #f)" pureEnv
--- Left (TypeError "Trying to apply a non-function")
-interpret
- :: Monad m
- => Text -- ^ Name of source file (for error reporting)
- -> Text -- ^ Source code to be interpreted
- -> Bindings m -- ^ Bindings to be used
- -> m (Either (LangError Value) Value)
-interpret sourceName expr bnds = do
- let primopNames = Map.keys (bindingsPrimops bnds)
- parsed = runReader (runParserT (spaceConsumer *> valueP <* eof) (toS sourceName) expr) primopNames
- case parsed of
- Left e -> pure . Left $ ParseError e
- Right v -> fst <$> runLang bnds (eval v)
-
--- | Parse and evaluate a Text as multiple expressions.
---
--- The first argument is the name of the source file to be used for error
--- reporting.
---
--- Examples:
---
--- >>> import Radicle.Internal.Primops
--- >>> fmap fst <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
--- Right (Boolean True)
-interpretMany
- :: Monad m
- => Text -- ^ Name of source file (for error reporting)
- -> Text -- ^ Source code to be interpreted
- -> Lang m Value
-interpretMany sourceName src = do
- primopNames <- gets $ Map.keys . bindingsPrimops
- let parsed = parseValues sourceName src primopNames
- case partitionEithers parsed of
- ([], vs) -> do
- es <- mapM eval vs
- case lastMay es of
- Just e -> pure e
- _ -> throwError
- $ OtherError "InterpretMany should be called with at least one expression."
- (e:_, _) -> throwError $ ParseError e
-
-- | Parse a Text as a series of values.
--
-- Note that parsing continues even if one value fails to parse.
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index d6aafce..b41ef44 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -8,7 +8,7 @@ import qualified Data.Map.Strict as Map
import qualified System.FilePath.Find as FP
import Radicle
-import Radicle.Internal.Subscriber.Capabilities
+import Radicle.Internal.Effects.Capabilities
import Paths_radicle