2019-08-22 02:49:08 +03:00
|
|
|
{-# OPTIONS_GHC -O2 #-}
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Fast implementation of `cue :: Atom -> Maybe Noun`.
|
|
|
|
|
|
|
|
Implementation is based on the approach used in `flat`.
|
|
|
|
-}
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.Noun.Cue (cue, cueExn, cueBS, cueBSExn, DecodeErr) where
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
import ClassyPrelude
|
2019-07-12 00:41:09 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
import Urbit.Atom
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Noun.Core
|
2019-07-12 04:16:40 +03:00
|
|
|
|
2019-07-12 22:24:44 +03:00
|
|
|
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
|
2020-01-23 12:22:30 +03:00
|
|
|
import Data.Function ((&))
|
2019-07-12 22:24:44 +03:00
|
|
|
import Foreign.Ptr (Ptr, castPtr, plusPtr, ptrToWordPtr)
|
2019-07-03 04:15:48 +03:00
|
|
|
import Foreign.Storable (peek)
|
2019-07-12 00:41:09 +03:00
|
|
|
import GHC.Prim (ctz#)
|
|
|
|
import GHC.Word (Word(..))
|
2019-07-03 04:15:48 +03:00
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
2019-07-12 00:41:09 +03:00
|
|
|
import Text.Printf (printf)
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-03 04:15:48 +03:00
|
|
|
import qualified Data.ByteString.Unsafe as BS
|
|
|
|
import qualified Data.HashTable.IO as H
|
|
|
|
import qualified Data.Vector.Primitive as VP
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
|
2019-07-03 04:15:48 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
cueBS :: ByteString -> Either DecodeErr Noun
|
2019-07-12 00:41:09 +03:00
|
|
|
cueBS = doGet dNoun
|
2019-07-03 04:15:48 +03:00
|
|
|
|
2019-08-28 14:45:49 +03:00
|
|
|
cueBSExn :: MonadIO m => ByteString -> m Noun
|
2019-07-21 22:56:18 +03:00
|
|
|
cueBSExn bs =
|
|
|
|
cueBS bs & \case
|
|
|
|
Left e -> throwIO e
|
|
|
|
Right x -> pure x
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
cue :: Atom -> Either DecodeErr Noun
|
2020-01-23 12:22:30 +03:00
|
|
|
cue = cueBS . atomBytes
|
2019-07-03 04:15:48 +03:00
|
|
|
|
2019-08-28 14:45:49 +03:00
|
|
|
cueExn :: MonadIO m => Atom -> m Noun
|
2020-01-23 12:22:30 +03:00
|
|
|
cueExn = cueBSExn . atomBytes
|
2019-07-17 08:32:36 +03:00
|
|
|
|
2019-07-03 04:15:48 +03:00
|
|
|
|
2019-07-03 08:14:39 +03:00
|
|
|
-- Debugging -------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE debugM #-}
|
|
|
|
debugM :: Monad m => String -> m ()
|
|
|
|
debugM _ = pure ()
|
|
|
|
|
|
|
|
{-# INLINE debugMId #-}
|
|
|
|
debugMId :: (Monad m, Show a) => String -> m a -> m a
|
|
|
|
debugMId _ a = a
|
|
|
|
|
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-|
|
|
|
|
The decoder state.
|
|
|
|
|
|
|
|
- An array of words (internal structure of our atoms).
|
|
|
|
- A pointer to the word *after* the last word in the array.
|
|
|
|
- A pointer into the current word of that array.
|
|
|
|
- A bit-offset into that word.
|
|
|
|
-}
|
|
|
|
data S = S
|
|
|
|
{ currPtr :: {-# UNPACK #-} !(Ptr Word)
|
|
|
|
, usedBits :: {-# UNPACK #-} !Word
|
|
|
|
, pos :: {-# UNPACK #-} !Word
|
|
|
|
} deriving (Show,Eq,Ord)
|
|
|
|
|
|
|
|
type Env = (Ptr Word, S)
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
data DecodeErr
|
2019-07-19 03:52:53 +03:00
|
|
|
= InfiniteCue Env
|
2019-06-29 04:46:33 +03:00
|
|
|
| BadEncoding Env String
|
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
data GetResult a = GetResult {-# UNPACK #-} !S !a
|
2019-07-03 08:14:39 +03:00
|
|
|
deriving (Show, Functor)
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
newtype Get a = Get
|
|
|
|
{ runGet :: Ptr Word
|
2019-07-12 00:41:09 +03:00
|
|
|
-> H.BasicHashTable Word Noun
|
2019-06-29 04:46:33 +03:00
|
|
|
-> S
|
|
|
|
-> IO (GetResult a)
|
|
|
|
}
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
doGet :: Get a -> ByteString -> Either DecodeErr a
|
2019-07-03 04:15:48 +03:00
|
|
|
doGet m bs =
|
2019-07-12 22:24:44 +03:00
|
|
|
unsafePerformIO $ try $ BS.unsafeUseAsCStringLen bs $ \(ptr, len) -> do
|
2019-07-03 04:15:48 +03:00
|
|
|
let endPtr = ptr `plusPtr` len
|
2019-07-05 01:40:36 +03:00
|
|
|
let sz = max 50
|
|
|
|
$ min 10_000_000
|
|
|
|
$ length bs `div` 6
|
|
|
|
tbl <- H.newSized sz
|
2019-07-03 04:15:48 +03:00
|
|
|
GetResult _ r <- runGet m endPtr tbl (S (castPtr ptr) 0 0)
|
|
|
|
pure r
|
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
instance Exception DecodeErr
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
instance Functor Get where
|
|
|
|
fmap f g = Get $ \end tbl s -> do
|
|
|
|
GetResult s' a <- runGet g end tbl s
|
|
|
|
return $ GetResult s' (f a)
|
|
|
|
{-# INLINE fmap #-}
|
|
|
|
|
|
|
|
instance Applicative Get where
|
|
|
|
pure x = Get (\_ _ s -> return $ GetResult s x)
|
|
|
|
{-# INLINE pure #-}
|
|
|
|
|
|
|
|
Get f <*> Get g = Get $ \end tbl s1 -> do
|
|
|
|
GetResult s2 f' <- f end tbl s1
|
|
|
|
GetResult s3 g' <- g end tbl s2
|
|
|
|
return $ GetResult s3 (f' g')
|
|
|
|
{-# INLINE (<*>) #-}
|
|
|
|
|
|
|
|
Get f *> Get g = Get $ \end tbl s1 -> do
|
|
|
|
GetResult s2 _ <- f end tbl s1
|
|
|
|
g end tbl s2
|
|
|
|
{-# INLINE (*>) #-}
|
|
|
|
|
|
|
|
instance Monad Get where
|
|
|
|
return = pure
|
|
|
|
{-# INLINE return #-}
|
|
|
|
|
|
|
|
(>>) = (*>)
|
|
|
|
{-# INLINE (>>) #-}
|
|
|
|
|
|
|
|
Get x >>= f = Get $ \end tbl s -> do
|
|
|
|
GetResult s' x' <- x end tbl s
|
|
|
|
runGet (f x') end tbl s'
|
|
|
|
{-# INLINE (>>=) #-}
|
|
|
|
|
2019-07-03 08:14:39 +03:00
|
|
|
fail msg = Get $ \end tbl s -> do
|
2019-06-29 04:46:33 +03:00
|
|
|
badEncoding end s msg
|
|
|
|
{-# INLINE fail #-}
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
instance MonadIO Get where
|
|
|
|
liftIO io = Get $ \end tbl s -> GetResult s <$> io
|
|
|
|
{-# INLINE liftIO #-}
|
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE badEncoding #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
badEncoding :: Ptr Word -> S -> String -> IO a
|
2019-07-10 00:57:48 +03:00
|
|
|
badEncoding !endPtr s msg = throwIO $ BadEncoding (endPtr,s) msg
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE getPos #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
getPos :: Get Word
|
|
|
|
getPos = Get $ \_ _ s ->
|
|
|
|
pure (GetResult s (pos s))
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE insRef #-}
|
2019-07-12 00:41:09 +03:00
|
|
|
insRef :: Word -> Noun -> Get ()
|
2019-07-12 22:24:44 +03:00
|
|
|
insRef !pos !now = Get $ \_ tbl s -> do
|
2019-06-29 04:46:33 +03:00
|
|
|
H.insert tbl pos now
|
|
|
|
pure $ GetResult s ()
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE getRef #-}
|
2019-07-12 00:41:09 +03:00
|
|
|
getRef :: Word -> Get Noun
|
2019-07-12 22:24:44 +03:00
|
|
|
getRef !ref = Get $ \x tbl s -> do
|
2019-06-29 04:46:33 +03:00
|
|
|
H.lookup tbl ref >>= \case
|
2019-07-03 08:14:39 +03:00
|
|
|
Nothing -> runGet (fail ("Invalid Reference: " <> show ref)) x tbl s
|
2019-06-29 04:46:33 +03:00
|
|
|
Just no -> pure (GetResult s no)
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE advance #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
advance :: Word -> Get ()
|
2019-07-03 08:14:39 +03:00
|
|
|
advance 0 = debugM "advance: 0" >> pure ()
|
2019-07-12 22:24:44 +03:00
|
|
|
advance !n = Get $ \_ _ s -> do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugM ("advance: " <> show n)
|
2019-06-29 04:46:33 +03:00
|
|
|
let newUsed = n + usedBits s
|
|
|
|
newS = s { pos = pos s + n
|
|
|
|
, usedBits = newUsed `mod` 64
|
|
|
|
, currPtr = plusPtr (currPtr s)
|
2019-07-03 08:14:39 +03:00
|
|
|
(8 * (fromIntegral (newUsed `div` 64)))
|
2019-06-29 04:46:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pure (GetResult newS ())
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE guardInfinite #-}
|
|
|
|
guardInfinite :: Ptr Word -> Ptr Word -> S -> IO ()
|
|
|
|
guardInfinite end cur s =
|
|
|
|
when (cur >= (end `plusPtr` 16)) $ do
|
|
|
|
throwIO (InfiniteCue (end, s))
|
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
-- TODO Should this be (>= end) or (> end)?
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE peekCurWord #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
peekCurWord :: Get Word
|
2019-07-12 22:24:44 +03:00
|
|
|
peekCurWord = Get $ \end _ s -> do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "peekCurWord" $ do
|
2019-07-19 03:52:53 +03:00
|
|
|
guardInfinite end (currPtr s) s
|
2019-06-29 04:46:33 +03:00
|
|
|
if ptrToWordPtr (currPtr s) >= ptrToWordPtr end
|
|
|
|
then pure (GetResult s 0)
|
|
|
|
else GetResult s <$> peek (currPtr s)
|
|
|
|
|
|
|
|
-- TODO Same question as above.
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE peekNextWord #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
peekNextWord :: Get Word
|
2019-07-12 22:24:44 +03:00
|
|
|
peekNextWord = Get $ \end _ s -> do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "peekNextWord" $ do
|
|
|
|
let pTarget = currPtr s `plusPtr` 8
|
2019-07-19 03:52:53 +03:00
|
|
|
guardInfinite end pTarget s
|
2019-07-03 08:14:39 +03:00
|
|
|
if ptrToWordPtr pTarget >= ptrToWordPtr end
|
2019-06-29 04:46:33 +03:00
|
|
|
then pure (GetResult s 0)
|
2019-07-03 08:14:39 +03:00
|
|
|
else GetResult s <$> peek pTarget
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE peekUsedBits #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
peekUsedBits :: Get Word
|
2019-07-03 08:14:39 +03:00
|
|
|
peekUsedBits =
|
|
|
|
debugMId "peekUsedBits" $ do
|
2019-07-12 22:24:44 +03:00
|
|
|
Get $ \_ _ s -> pure (GetResult s (usedBits s))
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Get a bit.
|
|
|
|
|
|
|
|
- Peek the current word.
|
|
|
|
- Right-shift by the bit-offset.
|
|
|
|
- Mask the high bits.
|
|
|
|
-}
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dBit #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
dBit :: Get Bool
|
|
|
|
dBit = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "dBit" $ do
|
|
|
|
wor <- peekCurWord
|
|
|
|
use <- fromIntegral <$> peekUsedBits
|
|
|
|
advance 1
|
|
|
|
pure (0 /= shiftR wor use .&. 1)
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dWord #-}
|
2019-07-03 04:15:48 +03:00
|
|
|
dWord :: Get Word
|
|
|
|
dWord = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "dWord" $ do
|
|
|
|
res <- peekWord
|
|
|
|
advance 64
|
|
|
|
pure res
|
2019-07-03 04:15:48 +03:00
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
{-|
|
|
|
|
Get n bits, where n > 64:
|
|
|
|
|
|
|
|
- Get (n/64) words.
|
|
|
|
- Advance by n bits.
|
|
|
|
- Calculate an offset (equal to the current bit-offset)
|
|
|
|
- Calculate the length (equal to n)
|
|
|
|
- Construct a bit-vector using the buffer*length*offset.
|
|
|
|
-}
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dAtomBits #-}
|
2019-07-03 04:15:48 +03:00
|
|
|
dAtomBits :: Word -> Get Atom
|
2019-07-10 00:57:48 +03:00
|
|
|
dAtomBits !(fromIntegral -> bits) = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId ("dAtomBits(" <> show bits <> ")") $ do
|
2020-01-23 12:22:30 +03:00
|
|
|
fmap wordsAtom $
|
2019-07-12 22:24:44 +03:00
|
|
|
VP.generateM bufSize $ \i -> do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugM (show i)
|
|
|
|
if (i == lastIdx && numExtraBits /= 0)
|
|
|
|
then dWordBits (fromIntegral numExtraBits)
|
|
|
|
else dWord
|
2019-07-03 04:15:48 +03:00
|
|
|
where
|
|
|
|
bufSize = numFullWords + min 1 numExtraBits
|
|
|
|
lastIdx = bufSize - 1
|
|
|
|
numFullWords = bits `div` 64
|
|
|
|
numExtraBits = bits `mod` 64
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
In order to peek at the next Word64:
|
|
|
|
|
|
|
|
- If we are past the end of the buffer:
|
|
|
|
- Return zero.
|
|
|
|
- If the bit-offset is zero:
|
|
|
|
- Just peek.
|
|
|
|
- If we are pointing to the last word:
|
|
|
|
- Peek and right-shift by the bit offset.
|
|
|
|
- Otherwise,
|
|
|
|
- Peek the current word *and* the next word.
|
|
|
|
- Right-shift the current word by the bit-offset.
|
|
|
|
- Left-shift the next word by the bit-offset.
|
|
|
|
- Binary or the resulting two words.
|
|
|
|
-}
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE peekWord #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
peekWord :: Get Word
|
|
|
|
peekWord = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "peekWord" $ do
|
2019-06-29 04:46:33 +03:00
|
|
|
off <- peekUsedBits
|
|
|
|
cur <- peekCurWord
|
2019-07-03 08:14:39 +03:00
|
|
|
nex <- peekNextWord
|
|
|
|
let res = swiz off (cur, nex)
|
|
|
|
debugM ("\t" <> (take 10 $ reverse $ printf "%b" (fromIntegral res :: Integer)) <> "..")
|
|
|
|
pure res
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE swiz #-}
|
2019-07-03 08:14:39 +03:00
|
|
|
swiz :: Word -> (Word, Word) -> Word
|
2019-07-10 00:57:48 +03:00
|
|
|
swiz !(fromIntegral -> off) (!low, !hig) =
|
2019-07-03 08:14:39 +03:00
|
|
|
(.|.) (shiftR low off) (shiftL hig (64-off))
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE takeLowBits #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
takeLowBits :: Word -> Word -> Word
|
2019-07-10 00:57:48 +03:00
|
|
|
takeLowBits 64 !wor = wor
|
|
|
|
takeLowBits !wid !wor = (2^wid - 1) .&. wor
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Make a word from the next n bits (where n <= 64).
|
|
|
|
|
|
|
|
- Peek at the next word.
|
|
|
|
- Mask the n lowest bits from the word.
|
|
|
|
- Advance by that number of bits.
|
|
|
|
- Return the word.
|
|
|
|
-}
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dWordBits #-}
|
2019-06-29 04:46:33 +03:00
|
|
|
dWordBits :: Word -> Get Word
|
2019-07-10 00:57:48 +03:00
|
|
|
dWordBits !n = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId ("dWordBits(" <> show n <> ")") $ do
|
2019-06-29 04:46:33 +03:00
|
|
|
w <- peekWord
|
|
|
|
advance n
|
2019-07-03 08:14:39 +03:00
|
|
|
debugM ("dWordBits: " <> show (takeLowBits n w))
|
2019-06-29 04:46:33 +03:00
|
|
|
pure (takeLowBits n w)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Fast Cue --------------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
Get the exponent-prefix of an atom:
|
|
|
|
|
|
|
|
- Peek at the next word.
|
|
|
|
- Calculate the number of least-significant bits in that word (there's
|
|
|
|
a primitive for this).
|
|
|
|
- Advance by that number of bits.
|
|
|
|
- Return the number of bits
|
|
|
|
-}
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dExp #-}
|
2019-07-02 05:43:10 +03:00
|
|
|
dExp :: Get Word
|
|
|
|
dExp = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "dExp" $ do
|
2019-07-02 05:43:10 +03:00
|
|
|
W# w <- peekWord
|
|
|
|
let res = W# (ctz# w)
|
2019-07-03 08:14:39 +03:00
|
|
|
advance (res+1)
|
2019-07-02 05:43:10 +03:00
|
|
|
pure res
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dAtomLen #-}
|
2019-07-02 05:43:10 +03:00
|
|
|
dAtomLen :: Get Word
|
|
|
|
dAtomLen = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "dAtomLen" $ do
|
|
|
|
dExp >>= \case
|
|
|
|
0 -> pure 0
|
|
|
|
e -> do p <- dWordBits (e-1)
|
|
|
|
pure (2^(e-1) .|. p)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dRef #-}
|
2019-07-02 05:43:10 +03:00
|
|
|
dRef :: Get Word
|
2019-07-03 08:14:39 +03:00
|
|
|
dRef = debugMId "dRef" (dAtomLen >>= dWordBits)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dAtom #-}
|
2019-07-02 05:43:10 +03:00
|
|
|
dAtom :: Get Atom
|
|
|
|
dAtom = do
|
2019-07-03 08:14:39 +03:00
|
|
|
debugMId "dAtom" $ do
|
|
|
|
dAtomLen >>= \case
|
|
|
|
0 -> pure 0
|
|
|
|
n -> dAtomBits n
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
{-# INLINE dCell #-}
|
2019-07-12 00:41:09 +03:00
|
|
|
dCell :: Get Noun
|
|
|
|
dCell = Cell <$> dNoun <*> dNoun
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Get a Noun.
|
|
|
|
|
|
|
|
- Get a bit
|
|
|
|
- If it's zero, get an atom.
|
|
|
|
- Otherwise, get another bit.
|
|
|
|
- If it's zero, get a cell.
|
|
|
|
- If it's one, get an atom.
|
|
|
|
-}
|
2019-07-12 00:41:09 +03:00
|
|
|
dNoun :: Get Noun
|
2019-07-02 05:43:10 +03:00
|
|
|
dNoun = do
|
|
|
|
p <- getPos
|
|
|
|
|
|
|
|
let yield r = insRef p r >> pure r
|
|
|
|
|
|
|
|
dBit >>= \case
|
2019-07-03 08:14:39 +03:00
|
|
|
False -> do debugM "It's an atom"
|
2019-07-12 00:41:09 +03:00
|
|
|
(Atom <$> dAtom) >>= yield
|
2019-07-02 05:43:10 +03:00
|
|
|
True -> dBit >>= \case
|
2019-07-03 08:14:39 +03:00
|
|
|
False -> do debugM "It's a cell"
|
|
|
|
dCell >>= yield
|
|
|
|
True -> do debugM "It's a backref"
|
|
|
|
dRef >>= getRef
|