module Mischief.Live.Interpreter where

import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad
import Control.Monad.Catch
import Data.Data (Proxy (..))
import Data.Foldable (for_)
import Data.Typeable (Typeable)
import Language.Haskell.Interpreter
import Mischief.ECS
import System.Directory (withCurrentDirectory)

data Request where
  Request :: forall a. (Typeable a) => ModuleName -> String -> TMVar (Result a) -> Request
  Reset :: Request

data Result a = Result a | NoResult | AwaitingResult

newtype InterpreterChan = InterpreterChan (TChan Request) deriving anyclass (Typeable InterpreterChan
[HookRel InterpreterChan]
[Hook InterpreterChan]
Set DefaultComponentType
IsExclusive (IsExclusiveRel InterpreterChan)
(Typeable InterpreterChan,
 IsExclusive (IsExclusiveRel InterpreterChan)) =>
Set DefaultComponentType
-> [Hook InterpreterChan]
-> [Hook InterpreterChan]
-> [Hook InterpreterChan]
-> [HookRel InterpreterChan]
-> [HookRel InterpreterChan]
-> [HookRel InterpreterChan]
-> Component InterpreterChan
forall c.
(Typeable c, IsExclusive (IsExclusiveRel c)) =>
Set DefaultComponentType
-> [Hook c]
-> [Hook c]
-> [Hook c]
-> [HookRel c]
-> [HookRel c]
-> [HookRel c]
-> Component c
$crequired :: Set DefaultComponentType
required :: Set DefaultComponentType
$conAdd :: [Hook InterpreterChan]
onAdd :: [Hook InterpreterChan]
$conSet :: [Hook InterpreterChan]
onSet :: [Hook InterpreterChan]
$conRemove :: [Hook InterpreterChan]
onRemove :: [Hook InterpreterChan]
$conAddRel :: [HookRel InterpreterChan]
onAddRel :: [HookRel InterpreterChan]
$conSetRel :: [HookRel InterpreterChan]
onSetRel :: [HookRel InterpreterChan]
$conRemoveRel :: [HookRel InterpreterChan]
onRemoveRel :: [HookRel InterpreterChan]
Component)

interpreterInit :: [FilePath] -> [Extension] -> TChan Request -> IO ()
interpreterInit :: [FilePath] -> [Extension] -> TChan Request -> IO ()
interpreterInit [FilePath]
files [Extension]
ext TChan Request
req = do
  result <- InterpreterT IO () -> IO (Either InterpreterError ())
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
InterpreterT m a -> m (Either InterpreterError a)
runInterpreter (InterpreterT IO () -> IO (Either InterpreterError ()))
-> InterpreterT IO () -> IO (Either InterpreterError ())
forall a b. (a -> b) -> a -> b
$ do
    [OptionVal (InterpreterT IO)] -> InterpreterT IO ()
forall (m :: * -> *). MonadInterpreter m => [OptionVal m] -> m ()
set [Option (InterpreterT IO) [Extension]
forall (m :: * -> *). MonadInterpreter m => Option m [Extension]
languageExtensions Option (InterpreterT IO) [Extension]
-> [Extension] -> OptionVal (InterpreterT IO)
forall (m :: * -> *) a. Option m a -> a -> OptionVal m
:= [Extension]
ext]
    [FilePath] -> InterpreterT IO ()
forall (m :: * -> *). MonadInterpreter m => [FilePath] -> m ()
loadModules [FilePath]
files
    TChan Request -> InterpreterT IO ()
interpreterLoop TChan Request
req

  case result of
    Right ()
_ -> do
      FilePath -> IO ()
putStrLn FilePath
"Reloading Live Code.."
      [FilePath] -> [Extension] -> TChan Request -> IO ()
interpreterInit [FilePath]
files [Extension]
ext TChan Request
req
    Left InterpreterError
e -> do
      FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Live Code Error: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ InterpreterError -> FilePath
forall a. Show a => a -> FilePath
show InterpreterError
e
      [FilePath] -> [Extension] -> TChan Request -> IO ()
interpreterInit [FilePath]
files [Extension]
ext TChan Request
req

interpreterLoop :: TChan Request -> Interpreter ()
interpreterLoop :: TChan Request -> InterpreterT IO ()
interpreterLoop TChan Request
req = do
  request <- IO Request -> InterpreterT IO Request
forall a. IO a -> InterpreterT IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Request -> InterpreterT IO Request)
-> IO Request -> InterpreterT IO Request
forall a b. (a -> b) -> a -> b
$ STM Request -> IO Request
forall a. STM a -> IO a
atomically (STM Request -> IO Request) -> STM Request -> IO Request
forall a b. (a -> b) -> a -> b
$ TChan Request -> STM Request
forall a. TChan a -> STM a
readTChan TChan Request
req

  case request of
    Request FilePath
mod FilePath
f (TMVar (Result a)
v :: TMVar (Result a)) -> do
      -- liftIO $ putStrLn "received request"
      result <- forall a.
Typeable a =>
FilePath -> FilePath -> Interpreter (Result a)
tryRequest @a FilePath
mod FilePath
f
      liftIO $ atomically $ writeTMVar v result

      interpreterLoop req
    Request
Reset -> IO () -> InterpreterT IO ()
forall a. IO a -> InterpreterT IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> InterpreterT IO ()) -> IO () -> InterpreterT IO ()
forall a b. (a -> b) -> a -> b
$ STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TChan Request -> STM ()
tryConsumeResets TChan Request
req

tryConsumeResets :: TChan Request -> STM ()
tryConsumeResets :: TChan Request -> STM ()
tryConsumeResets TChan Request
t = do
  reset <- TChan Request -> STM (Maybe Request)
forall a. TChan a -> STM (Maybe a)
tryPeekTChan TChan Request
t
  case reset of
    Just Request
Reset -> do
      STM Request -> STM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (STM Request -> STM ()) -> STM Request -> STM ()
forall a b. (a -> b) -> a -> b
$ TChan Request -> STM Request
forall a. TChan a -> STM a
readTChan TChan Request
t
      TChan Request -> STM ()
tryConsumeResets TChan Request
t
    Maybe Request
_ -> () -> STM ()
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

tryRequest :: forall a. (Typeable a) => ModuleName -> String -> Interpreter (Result a)
tryRequest :: forall a.
Typeable a =>
FilePath -> FilePath -> Interpreter (Result a)
tryRequest FilePath
mod FilePath
f = do
  InterpreterT IO (Result a)
-> (InterpreterError -> InterpreterT IO (Result a))
-> InterpreterT IO (Result a)
forall e a.
(HasCallStack, Exception e) =>
InterpreterT IO a -> (e -> InterpreterT IO a) -> InterpreterT IO a
forall (m :: * -> *) e a.
(MonadCatch m, HasCallStack, Exception e) =>
m a -> (e -> m a) -> m a
catch
    (a -> Result a
forall a. a -> Result a
Result (a -> Result a) -> InterpreterT IO a -> InterpreterT IO (Result a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> FilePath -> InterpreterT IO a
forall a. Typeable a => FilePath -> FilePath -> Interpreter a
handleRequest FilePath
mod FilePath
f)
    ( \(InterpreterError
e :: InterpreterError) -> do
        IO () -> InterpreterT IO ()
forall a. IO a -> InterpreterT IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> InterpreterT IO ()) -> IO () -> InterpreterT IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Live Code Error: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ InterpreterError -> FilePath
forall a. Show a => a -> FilePath
show InterpreterError
e
        Result a -> InterpreterT IO (Result a)
forall a. a -> InterpreterT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Result a
forall a. Result a
NoResult
    )

handleRequest :: forall a. (Typeable a) => ModuleName -> String -> Interpreter a
handleRequest :: forall a. Typeable a => FilePath -> FilePath -> Interpreter a
handleRequest FilePath
mod FilePath
f = do
  [FilePath] -> InterpreterT IO ()
forall (m :: * -> *). MonadInterpreter m => [FilePath] -> m ()
setTopLevelModules [FilePath
mod]
  FilePath -> a -> InterpreterT IO a
forall (m :: * -> *) a.
(MonadInterpreter m, Typeable a) =>
FilePath -> a -> m a
interpret FilePath
f (a
forall a. Typeable a => a
as :: a)