on master help sign in
make this a bit more useful, and use same protocol as emacs side :)
commita3c8020d50def27d7e1eb01d0ec8e10a00e9b90e authorHannes Mehnert <hannes@mehnert.org> dateFri 12 Apr 2013, 16:41 (14 years ago) parentb7710bc
2 files changed+31−36
src/Idris/IdeSlave.hs+14−21 src/Idris/REPL.hs+17−15
src/Idris/IdeSlave.hs+14−21
@@ -1,6 +1,6 @@
11{-# LANGUAGE FlexibleInstances, IncoherentInstances #-}
22
3−module Idris.IdeSlave(receiveMessage, sendMessage, IdeSlaveCommand(..)) where
3+module Idris.IdeSlave(receiveMessage, sendMessage, IdeSlaveCommand(..), sexpToCommand, SExp(..)) where
44
55import Text.Printf
66import Numeric
@@ -84,38 +84,31 @@ sexpToCommand (List [SymbolAtom "interpret", StringAtom cmd]) = Just (Interpret
8484sexpToCommand (SymbolAtom "version") = Just Version
8585sexpToCommand _ = Nothing
8686
87−receiveMessage :: IO (Maybe IdeSlaveCommand)
87+receiveMessage :: IO (SExp)
8888receiveMessage
8989 = do x <- getLine
9090 return (receiveString x)
9191
92−receiveString :: String -> Maybe IdeSlaveCommand
92+receiveString :: String -> SExp
9393receiveString x =
9494 case readHex (take 6 x) of
9595 ((num, ""):_) ->
9696 let msg = drop 6 x in
9797 if (length msg) /= (num - 1)
9898 then error "bad input length"
99− else parseSExpToCommand msg
99+ else (case parseSExp msg of
100+ Left _ -> error "parse failure"
101+ Right r -> r)
100102 _ -> error "readHex failed"
101103
102−parseSExpToCommand :: String -> Maybe IdeSlaveCommand
103−parseSExpToCommand msg =
104− case parseSExp msg of
105− Left _ -> error "parse failure"
106− Right r -> sexpToCommand r
107−
108−sendMessage :: SExpable a => a -> IO ()
109−sendMessage s =
110− do let str = convertToSExpString s
111− putStrLn $ (getHexLength str) ++ str
112−
113−convertToSExpString :: SExpable a => a -> String
114−convertToSExpString s =
115− let str = sExpToString (toSExp s) in
116− if (str !! 0) == '('
117− then str
118− else "(" ++ str ++ ")"
104+sendMessage :: SExpable a => Integer -> Either a a -> IO ()
105+sendMessage id s =
106+ -- return value id
107+ case s of
108+ Left err -> putStrLn $ conv (List [SymbolAtom "abort", toSExp err])
109+ Right succ -> putStrLn $ conv (List [SymbolAtom "ok", toSExp succ])
110+ where conv sexp =
111+ let str = sExpToString (List [SymbolAtom "return", sexp, IntegerAtom id]) in (getHexLength str) ++ str
119112
120113getHexLength :: String -> String
121114getHexLength s = printf "%06x" (1 + (length s))
src/Idris/REPL.hs+17−15
@@ -84,21 +84,23 @@ repl orig mods
8484-- | Run the IdeSlave
8585ideslave :: IState -> [FilePath] -> Idris ()
8686ideslave orig mods
87− = do idrisCatch (do x <- liftIO $ receiveMessage
88− case x of
89− Nothing -> do liftIO $ sendMessage (Just "did not understand")
90− Just (Interpret cmd) -> do i <- getIState
91− let fn = case mods of
92− (f:_) -> f
93− _ -> ""
94− case parseCmd i cmd of
95− Left err -> do liftIO $ sendMessage (show err)
96− Right cmd -> do idrisCatch (do xs <- process fn cmd
97− liftIO $ sendMessage xs)
98− (\e -> do liftIO $ sendMessage (show e))
99− Just y -> do liftIO $ sendMessage (Just "understood you!")
100− liftIO $ sendMessage (Just "Hello world"))
101− (\e -> do liftIO $ sendMessage (show e))
87+ = do idrisCatch
88+ (do x <- liftIO $ receiveMessage
89+ case x of
90+ (List [(SymbolAtom "emacs-rex"), cmd, (IntegerAtom id)]) ->
91+ case sexpToCommand cmd of
92+ Nothing -> do liftIO $ sendMessage id (Left "did not understand")
93+ Just (Interpret cmd) ->
94+ do i <- getIState
95+ let fn = case mods of
96+ (f:_) -> f
97+ _ -> ""
98+ case parseCmd i cmd of
99+ Left err -> do liftIO $ sendMessage id (Left (show err))
100+ Right cmd -> do idrisCatch (do xs <- process fn cmd
101+ liftIO $ sendMessage id (Right xs))
102+ (\e -> do liftIO $ sendMessage id (Left (show e))))
103+ (\e -> do liftIO $ sendMessage 0 (Left (show e)))
102104 ideslave orig mods
103105
104106-- | The prompt consists of the currently loaded modules, or "Idris" if there are none