@@ -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))