A peer-to-peer stack for code collaboration
Some cleanup. (#104)
James Haydon69544ca40b53
diff --git a/package.yaml b/package.yamlindex 3049bb1..49034bd 100644--- a/package.yaml+++ b/package.yaml@@ -70,7 +70,7 @@ tests:- hpackexecutables:- repl:+ radicle:main: Main.hssource-dirs: exe/Replwhen:diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hsindex 4554d4c..86369ee 100644--- a/src/Radicle/Internal.hs+++ b/src/Radicle/Internal.hs@@ -5,12 +5,13 @@ module Radicle.Internalimport Protolude+import Radicle.Internal.CLI as Ximport Radicle.Internal.Core as X+import Radicle.Internal.Interpret as Ximport Radicle.Internal.Parse as Ximport Radicle.Internal.Pretty as Ximport Radicle.Internal.Primops as X-import Radicle.Internal.Subscriber as X-import Radicle.Internal.CLI as X+import Radicle.Internal.Effects as Ximport qualified Text.Megaparsec as Mdiff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Effects.hssimilarity index 95%rename from src/Radicle/Internal/Subscriber.hsrename to src/Radicle/Internal/Effects.hsindex 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 whereimport 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.Interpretimport Radicle.Internal.Prettyimport 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] -> dodiff --git a/src/Radicle/Internal/Subscriber/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hssimilarity index 98%rename from src/Radicle/Internal/Subscriber/Capabilities.hsrename to src/Radicle/Internal/Effects/Capabilities.hsindex 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 whereimport Protoludediff --git a/src/Radicle/Internal/Interpret.hs b/src/Radicle/Internal/Interpret.hsnew file mode 100644index 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 ediff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 1507066..ee562e5 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -9,7 +9,7 @@ import qualified Data.Text as Timport 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 Mimport 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.hsindex 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 Mapimport qualified System.FilePath.Find as FPimport Radicle-import Radicle.Internal.Subscriber.Capabilities+import Radicle.Internal.Effects.Capabilitiesimport Paths_radicle