Skip to content

A peer-to-peer stack for code collaboration

Repl improvements

Julian K. Arni9584e5793cee
diff --git a/package.yaml b/package.yaml
index f64f604..ac17462 100644
--- a/package.yaml
+++ b/package.yaml
@@ -16,6 +16,7 @@ dependencies:
- mmorph
- mtl
- prettyprinter
+- prettyprinter-ansi-terminal
- scientific
- syb
- text
@@ -63,6 +64,7 @@ executables:
source-dirs: repl
dependencies:
- radicle
+ - optparse-applicative
ghc-options: -Wall -threaded
default-extensions:
diff --git a/repl/Main.hs b/repl/Main.hs
index 94327bd..b3dd368 100644
--- a/repl/Main.hs
+++ b/repl/Main.hs
@@ -1,14 +1,59 @@
module Main (main) where
-import System.Environment
+import Data.Semigroup ((<>))
import qualified Data.Text.IO as T
-import Control.Concurrent
+import Options.Applicative
+import System.Environment
-import Radicle
+import Radicle
main :: IO ()
main = do
- [file] <- getArgs
- src <- T.readFile file
- _ <- forkIO $ repl src
- threadDelay maxBound
+ opts' <- execParser allOpts
+ cfgSrc <- T.readFile =<< case configFile opts' of
+ Nothing -> getConfig
+ Just cfg -> pure cfg
+ hist <- case histFile opts' of
+ Nothing -> getHistory
+ Just h -> pure h
+ repl hist cfgSrc
+ where
+ allOpts = info (opts <**> helper)
+ ( fullDesc
+ <> progDesc "Run the radicle REPL"
+ <> header "rad - The radicle REPL"
+ )
+
+-- | Uses XDG_CONFIG_HOME if available.
+getConfig :: IO FilePath
+getConfig = do
+ mCfgHome <- lookupEnv "XDG_CONFIG_HOME"
+ pure $ case mCfgHome of
+ Nothing -> "~/.config/rad/config.rad"
+ Just cfgHome -> cfgHome <> "/rad/config.rad"
+
+-- | Uses XDG_CACHE_HOME if available.
+getHistory :: IO FilePath
+getHistory = do
+ mCfgHome <- lookupEnv "XDG_CACHE_HOME"
+ pure $ case mCfgHome of
+ Nothing -> "/home/jkarni/.cache/rad/history"
+ Just cfgHome -> cfgHome <> "/rad/history"
+
+data Opts = Opts
+ { configFile :: Maybe FilePath
+ , histFile :: Maybe FilePath
+ }
+
+opts :: Parser Opts
+opts = Opts
+ <$> optional (strOption
+ ( long "config"
+ <> metavar "FILE"
+ <> help "rad configuration file"
+ ))
+ <*> optional (strOption
+ ( long "histfile"
+ <> metavar "FILE"
+ <> help "repl history file"
+ ))
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 07c539a..d4973d6 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -32,6 +32,7 @@ data LangError =
| WrongNumberOfArgs Text Int Int
| OtherError Text
| ParseError (Par.ParseError Char Void)
+ | Exit
deriving (Eq, Show, Read, Generic)
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index d0a0511..ef83bd5 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -6,6 +6,7 @@ import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Text
+import Text.Megaparsec.Error (parseErrorPretty)
import Radicle.Internal.Core
@@ -32,6 +33,19 @@ instance Pretty Value where
where
escapeStr = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"
+instance Pretty LangError where
+ pretty v = case v of
+ UnknownIdentifier i -> "Unknown identifier:" <+> pretty i
+ Impossible t -> "This cannot be!" <+> pretty t
+ TypeError t -> "Type error:" <+> pretty t
+ WrongNumberOfArgs t x y -> "Wrong number of args in" <+> pretty t
+ <+> "Expected:" <+> pretty x
+ <+> "Got:" <+> pretty y
+ OtherError t -> "Error:" <+> pretty t
+ ParseError t -> "Parser error:" <+> pretty (parseErrorPretty t)
+ Exit -> "Exit"
+
+
-- | A fast and compact layout. Primarily intended for testing.
--
-- Examples:
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 35e2293..1be67a2 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -10,9 +10,12 @@ import qualified Data.Map as Map
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text as T
+import Data.Text.Prettyprint.Doc (pretty)
+import Data.Text.Prettyprint.Doc.Render.Terminal (putDoc)
import System.Console.Haskeline (CompletionFunc, InputT, completeWord,
- defaultSettings, runInputT,
- setComplete, simpleCompletion)
+ defaultSettings, historyFile,
+ runInputT, setComplete,
+ simpleCompletion)
import Radicle.Internal.Core
import Radicle.Internal.Parse
@@ -22,13 +25,17 @@ import Radicle.Internal.Subscriber.Capabilities
type ReplM m = ( Monad m, Stdout (Lang m), Stdin (Lang m), GetEnv (Lang m)
, SetEnv (Lang m))
-repl :: Text -> IO ()
-repl preCode = do
- let settings = setComplete completion defaultSettings
+repl :: FilePath -> Text -> IO ()
+repl histFile preCode = do
+ let settings = setComplete completion
+ $ defaultSettings { historyFile = Just histFile }
r <- runInputT settings
$ runLang replBindings
$ interpretMany "[pre]" preCode
- print r
+ case r of
+ Left Exit -> return ()
+ Left e -> putDoc $ pretty e
+ Right v -> putDoc $ pretty v
completion :: Monad m => CompletionFunc m
completion = completeWord Nothing ['(', ')', ' ', '\n'] go