summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/REPL/Pure.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2025-10-28 20:14:16 +0100
committerArnaud Bailly <arnaud@pankzsoft.com>2025-10-28 20:14:16 +0100
commit1d1f0efc7de1eaa0c8a6b503892c2f950008fde6 (patch)
treebd2e2ea5963d8a208d302854502f580d8ffd73cb /lambda-calcul/haskell/src/Minilang/REPL/Pure.hs
parenta210419fd60c00d011f70fb1b968031621e4c01a (diff)
downloadlambda-nantes-1d1f0efc7de1eaa0c8a6b503892c2f950008fde6.tar.gz
wip: port REPL from minilang
Diffstat (limited to 'lambda-calcul/haskell/src/Minilang/REPL/Pure.hs')
-rw-r--r--lambda-calcul/haskell/src/Minilang/REPL/Pure.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/lambda-calcul/haskell/src/Minilang/REPL/Pure.hs b/lambda-calcul/haskell/src/Minilang/REPL/Pure.hs
new file mode 100644
index 0000000..23c45e1
--- /dev/null
+++ b/lambda-calcul/haskell/src/Minilang/REPL/Pure.hs
@@ -0,0 +1,42 @@
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+{-# LANGUAGE LambdaCase #-}
+
+module Minilang.REPL.Pure where
+
+import Control.Exception (throw)
+import Control.Monad.Catch
+import Control.Monad.Catch.Pure (CatchT (..))
+import Control.Monad.State
+import Data.Text hiding (replicate)
+import Minilang.REPL.Types
+
+data PureEnv = PureEnv
+ { inputText :: [Text],
+ outputText :: [Text],
+ pureREPL :: REPLEnv
+ }
+
+newtype PureREPL a = PureREPL {runPure :: CatchT (State PureEnv) a}
+ deriving (Functor, Applicative, Monad, MonadState PureEnv, MonadCatch)
+
+instance MonadREPL PureREPL where
+ input = do
+ ins <- inputText <$> get
+ case ins of
+ [] -> pure EOF
+ (t : ts) -> modify (\e -> e {inputText = ts}) >> pure (interpret t)
+
+ output t = modify $ \e -> e {outputText = render t : outputText e}
+ prompt = pure ()
+ getEnv = get >>= pure . pureREPL
+ setEnv e' = modify $ \e -> e {pureREPL = e'}
+ load f = pure $ Left $ REPLError $ "cannot load file " <> f <> " in Pure interpreter"
+
+render :: Out -> Text
+render = \case
+ Msg t -> t
+ Bye -> "Bye!"
+ other -> pack (show other)
+
+instance MonadThrow PureREPL where
+ throwM = throw