module Language.Haskell.Liquid.Interactive.Handler (
    -- * Initial state for server
    initial

    -- * Command handler
  , handler
  ) where

import Prelude hiding (error)
import Control.Concurrent.MVar
import Language.Haskell.Liquid.Interactive.Types
import Language.Haskell.Liquid.Liquid

------------------------------------------------------------------------------
handler :: MVar State -> Command -> IO Response
------------------------------------------------------------------------------
handler :: MVar State -> Command -> IO Response
handler MVar State
r = MVar State -> (State -> IO (State, Response)) -> IO Response
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar State
r ((State -> IO (State, Response)) -> IO Response)
-> (Command -> State -> IO (State, Response))
-> Command
-> IO Response
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Command -> State -> IO (State, Response)
runLiquid'

runLiquid' :: Command -> State -> IO (State, Response)
runLiquid' :: Command -> State -> IO (State, Response)
runLiquid' Command
cfg State
s = do
  let mE :: MbEnv
mE    = State -> MbEnv
sMbEnv State
s
  let n :: Int
n     = State -> Int
sCount State
s
  (ExitCode
c, MbEnv
mE') <- MbEnv -> Command -> IO (ExitCode, MbEnv)
runLiquid MbEnv
mE Command
cfg
  let s' :: State
s'    = Int -> MbEnv -> State
State (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MbEnv
mE'
  (State, Response) -> IO (State, Response)
forall (m :: * -> *) a. Monad m => a -> m a
return      (State
s', (ExitCode -> Status
status ExitCode
c, Int
n))

------------------------------------------------------------------------------
initial :: State
------------------------------------------------------------------------------
initial :: State
initial = Int -> MbEnv -> State
State Int
0 MbEnv
forall a. Maybe a
Nothing