@@ -20,7 +20,6 @@ import MHSPrelude
2020import Control.Exception (try , SomeException , displayException )
2121import Data.IORef
2222import Data.List (nub )
23- import Data.Text (pack , unpack )
2423import System.IO (putStrLn )
2524
2625import Foreign.C.String (CString , peekCString , peekCStringLen , newCString )
@@ -32,9 +31,7 @@ import Foreign.Storable (poke)
3231
3332import Repl.Context
3433import Repl.Error
35- import Repl.Utils
3634import Repl.Analysis
37- import Repl.Compiler
3835import Repl.Executor
3936
4037--------------------------------------------------------------------------------
@@ -80,54 +77,80 @@ mhsReplFreeCString :: CString -> IO ()
8077mhsReplFreeCString = free
8178
8279--------------------------------------------------------------------------------
83- -- Unified runner
80+ -- Unified runners
8481--------------------------------------------------------------------------------
8582
86- runReplAction
87- :: (ReplCtx -> String -> IO (Either ReplError ReplCtx ))
88- -> ReplHandle -> CString -> CSize -> Ptr CString -> IO CInt
89- runReplAction act h srcPtr srcLen errPtr = do
83+ withHandleInput
84+ :: ReplHandle
85+ -> CString
86+ -> CSize
87+ -> (IORef ReplCtx -> String -> IO a )
88+ -> IO a
89+ withHandleInput h srcPtr srcLen k = do
9090 ref <- deRefStablePtr h
9191 src <- peekSource srcPtr srcLen
92- ctx <- readIORef ref
93- result <- try (act ctx src) :: IO (Either SomeException (Either ReplError ReplCtx ))
94- let normalized = case result of
95- Left ex -> Left (ReplRuntimeError (displayException ex))
96- Right r -> r
97- case normalized of
98- Left e -> writeErrorCString errPtr (prettyReplError e) >> pure c_ERR
99- Right ctx' -> writeIORef ref ctx' >> pure c_OK
92+ k ref src
93+
94+ normalizeResult
95+ :: Either SomeException (Either ReplError a )
96+ -> Either ReplError a
97+ normalizeResult result =
98+ case result of
99+ Left ex -> Left (ReplRuntimeError (displayException ex))
100+ Right val -> val
101+
102+ runStatefulAction
103+ :: (ReplCtx -> String -> IO (Either ReplError ReplCtx ))
104+ -> ReplHandle
105+ -> CString
106+ -> CSize
107+ -> Ptr CString
108+ -> IO CInt
109+ runStatefulAction act h srcPtr srcLen errPtr =
110+ withHandleInput h srcPtr srcLen $ \ ref src -> do
111+ ctx <- readIORef ref
112+ result <- try (act ctx src) :: IO (Either SomeException (Either ReplError ReplCtx ))
113+ case normalizeResult result of
114+ Left err -> writeErrorCString errPtr (prettyReplError err) >> pure c_ERR
115+ Right ctx' -> writeIORef ref ctx' >> pure c_OK
116+
117+ runQueryAction
118+ :: (ReplCtx -> String -> IO (Either ReplError String ))
119+ -> ReplHandle
120+ -> CString
121+ -> CSize
122+ -> Ptr CString
123+ -> IO CInt
124+ runQueryAction act h srcPtr srcLen outPtr =
125+ withHandleInput h srcPtr srcLen $ \ ref src -> do
126+ ctx <- readIORef ref
127+ result <- try (act ctx src) :: IO (Either SomeException (Either ReplError String ))
128+ case normalizeResult result of
129+ Left err -> writeErrorCString outPtr (prettyReplError err) >> pure c_ERR
130+ Right value -> newCString value >>= poke outPtr >> pure c_OK
100131
101132--------------------------------------------------------------------------------
102133-- Public FFI API
103134--------------------------------------------------------------------------------
104135
105136mhsReplDefine :: ReplHandle -> CString -> CSize -> Ptr CString -> IO CInt
106- mhsReplDefine = runReplAction replDefine
137+ mhsReplDefine = runStatefulAction replDefine
107138
108139mhsReplRun :: ReplHandle -> CString -> CSize -> Ptr CString -> IO CInt
109- mhsReplRun = runReplAction replRun
140+ mhsReplRun = runStatefulAction replRun
110141
111142mhsReplExecute :: ReplHandle -> CString -> CSize -> Ptr CString -> IO CInt
112- mhsReplExecute = runReplAction replExecute
143+ mhsReplExecute = runStatefulAction replExecute
113144
114145mhsReplIsComplete :: ReplHandle -> CString -> CSize -> IO CString
115- mhsReplIsComplete h srcPtr srcLen = do
116- ref <- deRefStablePtr h
117- src <- peekSource srcPtr srcLen
118- ctx <- readIORef ref
119- status <- replIsComplete ctx src
120- newCString status
146+ mhsReplIsComplete h srcPtr srcLen =
147+ withHandleInput h srcPtr srcLen $ \ ref src -> do
148+ ctx <- readIORef ref
149+ status <- replIsComplete ctx src
150+ newCString status
121151
122152mhsReplInspect :: ReplHandle -> CString -> CSize -> Ptr CString -> IO CInt
123- mhsReplInspect h srcPtr srcLen resPtr = do
124- ref <- deRefStablePtr h
125- name <- peekSource srcPtr srcLen
126- ctx <- readIORef ref
127- result <- replInspect ctx name
128- case result of
129- Left e -> writeErrorCString resPtr (prettyReplError e) >> pure c_ERR
130- Right info -> newCString info >>= poke resPtr >> pure c_OK
153+ mhsReplInspect = runQueryAction replInspect
131154
132155mhsReplCanParseDefinition :: CString -> CSize -> IO CInt
133156mhsReplCanParseDefinition ptr len = do
0 commit comments