blob: 9e4bd22d7254cd0479574a2178b99217ca5e9383 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Minilang.REPL.Purer where
import Control.Monad.Catch
import Control.Monad.Catch.Pure (CatchT (..))
import Control.Monad.State
import Minilang.REPL.Types
data PurerEnv = PurerEnv
{ inputs :: [In]
, outputs :: [Out]
, purerREPL :: REPLEnv
}
newtype PurerREPL a = PurerREPL { runPurer :: CatchT (State PurerEnv) a }
deriving (Functor, Applicative, Monad, MonadState PurerEnv, MonadCatch, MonadThrow)
instance MonadREPL PurerREPL where
input = do
ins <- inputs <$> get
case ins of
[] -> pure EOF
(t:ts) -> modify (\ e -> e { inputs = ts }) >> pure t
output t = modify $ \ e -> e { outputs = t : outputs e }
prompt = pure ()
getEnv = get >>= pure . purerREPL
setEnv e' = modify $ \ e -> e { purerREPL = e' }
load f = pure $ Left $ REPLError $ "cannot load file " <> f <> " in Pure interpreter"
|