@@ -1,6 +1,6 @@
1{-# LANGUAGE FlexibleInstances, IncoherentInstances #-}
1{-# LANGUAGE FlexibleInstances, IncoherentInstances #-}
3−module Idris.IdeSlave(receiveMessage, sendMessage, IdeSlaveCommand(..)) where
3+module Idris.IdeSlave(receiveMessage, sendMessage, IdeSlaveCommand(..), sexpToCommand, SExp(..)) where
5import Text.Printf
5import Text.Printf
6import Numeric
6import Numeric
@@ -84,38 +84,31 @@ sexpToCommand (List [SymbolAtom "interpret", StringAtom cmd]) = Just (Interpret
84sexpToCommand (SymbolAtom "version") = Just Version
84sexpToCommand (SymbolAtom "version") = Just Version
85sexpToCommand _ = Nothing
85sexpToCommand _ = Nothing
87−receiveMessage :: IO (Maybe IdeSlaveCommand)
87+receiveMessage :: IO (SExp)
88receiveMessage
88receiveMessage
89 = do x <- getLine
89 = do x <- getLine
90 return (receiveString x)
90 return (receiveString x)
92−receiveString :: String -> Maybe IdeSlaveCommand
92+receiveString :: String -> SExp
93receiveString x =
93receiveString x =
94 case readHex (take 6 x) of
94 case readHex (take 6 x) of
95 ((num, ""):_) ->
95 ((num, ""):_) ->
96 let msg = drop 6 x in
96 let msg = drop 6 x in
97 if (length msg) /= (num - 1)
97 if (length msg) /= (num - 1)
98 then error "bad input length"
98 then error "bad input length"
99− else parseSExpToCommand msg
99+ else (case parseSExp msg of
100+ Left _ -> error "parse failure"
100 _ -> error "readHex failed"
102 _ -> error "readHex failed"
102−parseSExpToCommand :: String -> Maybe IdeSlaveCommand
104+sendMessage :: SExpable a => Integer -> Either a a -> IO ()
103−parseSExpToCommand msg =
105+sendMessage id s =
104− case parseSExp msg of
107+ case s of
105− Left _ -> error "parse failure"
108+ Left err -> putStrLn $ conv (List [SymbolAtom "abort", toSExp err])
106− Right r -> sexpToCommand r
109+ Right succ -> putStrLn $ conv (List [SymbolAtom "ok", toSExp succ])
107−
110+ where conv sexp =
108−sendMessage :: SExpable a => a -> IO ()
111+ let str = sExpToString (List [SymbolAtom "return", sexp, IntegerAtom id]) in (getHexLength str) ++ str
110− do let str = convertToSExpString s
111− putStrLn $ (getHexLength str) ++ str
113−convertToSExpString :: SExpable a => a -> String
114−convertToSExpString s =
115− let str = sExpToString (toSExp s) in
116− if (str !! 0) == '('
118− else "(" ++ str ++ ")"
120getHexLength :: String -> String
113getHexLength :: String -> String
121getHexLength s = printf "%06x" (1 + (length s))
114getHexLength s = printf "%06x" (1 + (length s))