X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Futils%2FSerialized.hs;h=26cca6c386f9c255f862b35500a865f7a300d920;hp=9a0e4c5d17bb238f39fdbcb7367944df0b4de421;hb=86add45dbfb6f962b65e371143dd467ae783f9e7;hpb=9bcd95bad83ee937c178970e8b729732e680fe1e diff --git a/compiler/utils/Serialized.hs b/compiler/utils/Serialized.hs index 9a0e4c5..26cca6c 100644 --- a/compiler/utils/Serialized.hs +++ b/compiler/utils/Serialized.hs @@ -24,12 +24,7 @@ import Util import Data.Bits import Data.Word ( Word8 ) -#if __GLASGOW_HASKELL__ > 609 import Data.Data -#else -import Data.Generics -#endif -import Data.Typeable -- | Represents a serialized value of a particular type. Attempts can be made to deserialize it at certain types @@ -86,16 +81,17 @@ deserializeWithData' bytes = deserializeConstr bytes $ \constr_rep bytes -> serializeConstr :: ConstrRep -> [Word8] -> [Word8] serializeConstr (AlgConstr ix) = serializeWord8 1 . serializeInt ix serializeConstr (IntConstr i) = serializeWord8 2 . serializeInteger i -serializeConstr (FloatConstr d) = serializeWord8 3 . serializeDouble d -serializeConstr (StringConstr s) = serializeWord8 4 . serializeString s +serializeConstr (FloatConstr r) = serializeWord8 3 . serializeRational r +serializeConstr (CharConstr c) = serializeWord8 4 . serializeChar c + deserializeConstr :: [Word8] -> (ConstrRep -> [Word8] -> a) -> a deserializeConstr bytes k = deserializeWord8 bytes $ \constr_ix bytes -> case constr_ix of - 1 -> deserializeInt bytes $ \ix -> k (AlgConstr ix) - 2 -> deserializeInteger bytes $ \i -> k (IntConstr i) - 3 -> deserializeDouble bytes $ \d -> k (FloatConstr d) - 4 -> deserializeString bytes $ \s -> k (StringConstr s) + 1 -> deserializeInt bytes $ \ix -> k (AlgConstr ix) + 2 -> deserializeInteger bytes $ \i -> k (IntConstr i) + 3 -> deserializeRational bytes $ \r -> k (FloatConstr r) + 4 -> deserializeChar bytes $ \c -> k (CharConstr c) x -> error $ "deserializeConstr: unrecognised serialized constructor type " ++ show x ++ " in context " ++ show bytes @@ -140,11 +136,11 @@ deserializeInt :: [Word8] -> (Int -> [Word8] -> a) -> a deserializeInt = deserializeFixedWidthNum -serializeDouble :: Double -> [Word8] -> [Word8] -serializeDouble = serializeString . show +serializeRational :: (Real a) => a -> [Word8] -> [Word8] +serializeRational = serializeString . show . toRational -deserializeDouble :: [Word8] -> (Double -> [Word8] -> a) -> a -deserializeDouble bytes k = deserializeString bytes (k . read) +deserializeRational :: (Fractional a) => [Word8] -> (a -> [Word8] -> b) -> b +deserializeRational bytes k = deserializeString bytes (k . fromRational . read) serializeInteger :: Integer -> [Word8] -> [Word8] @@ -154,6 +150,13 @@ deserializeInteger :: [Word8] -> (Integer -> [Word8] -> a) -> a deserializeInteger bytes k = deserializeString bytes (k . read) +serializeChar :: Char -> [Word8] -> [Word8] +serializeChar = serializeString . show + +deserializeChar :: [Word8] -> (Char -> [Word8] -> a) -> a +deserializeChar bytes k = deserializeString bytes (k . read) + + serializeString :: String -> [Word8] -> [Word8] serializeString = serializeList serializeEnum @@ -171,4 +174,5 @@ deserializeList deserialize_element bytes k = deserializeInt bytes $ \len bytes go :: Int -> [Word8] -> ([a] -> [Word8] -> b) -> b go len bytes k | len <= 0 = k [] bytes - | otherwise = deserialize_element bytes (\elt bytes -> go (len - 1) bytes (k . (elt:))) \ No newline at end of file + | otherwise = deserialize_element bytes (\elt bytes -> go (len - 1) bytes (k . (elt:))) +