inhale: avoid monadic pattern matching in pure code

These changes avoid the need for having a MonadFail instance for Decoder.
This commit is contained in:
Peter Simons 2018-09-28 11:47:11 +02:00
parent 59fbcdfba9
commit c5c3fec26c

View File

@ -1178,17 +1178,20 @@ getInt w f = getInt' w >>= maybe (return False) (append . f)
-- @@@@@@@@@ evil beyond evil. there *has* to be a better way -- @@@@@@@@@ evil beyond evil. there *has* to be a better way
inhale :: Int -> Decoder Integer inhale :: Int -> Decoder Integer
inhale 8 = do inhale 8 = do
[b] <- eat 1 x <- eat 1
return $ fromIntegral b case x of
[b] -> return $ fromIntegral b
inhale 16 = do inhale 16 = do
[b0,b1] <- eat 2 x <- eat 2
io $ allocaArray 2 $ \p -> do case x of
[b0,b1] -> io $ allocaArray 2 $ \p -> do
pokeArray p [b0,b1] pokeArray p [b0,b1]
[v] <- peekArray 1 (castPtr p :: Ptr Word16) [v] <- peekArray 1 (castPtr p :: Ptr Word16)
return $ fromIntegral v return $ fromIntegral v
inhale 32 = do inhale 32 = do
[b0,b1,b2,b3] <- eat 4 x <- eat 4
io $ allocaArray 4 $ \p -> do case x of
[b0,b1,b2,b3] -> io $ allocaArray 4 $ \p -> do
pokeArray p [b0,b1,b2,b3] pokeArray p [b0,b1,b2,b3]
[v] <- peekArray 1 (castPtr p :: Ptr Word32) [v] <- peekArray 1 (castPtr p :: Ptr Word32)
return $ fromIntegral v return $ fromIntegral v