@@ -1,4 +1,4 @@
1−module Nock.Jam (mat, rub) where
1+module Nock.Jam (jam, cue, mat, rub) where
22
33import Control.Monad.Trans.Class
44import Control.Monad.Trans.State
@@ -28,13 +28,41 @@ parseBits :: Int -> BitParser [Bool]
2828parseBits 0 = pure []
2929parseBits n = do
3030 b <- parseBit
31− (b :) <$> parseBits (n + 1)
31+ (b :) <$> parseBits (n - 1)
3232
3333getOffset :: BitParser Int
3434getOffset = fst <$> get
3535
36−-- cue' :: BitParser Noun
37−-- cue' = do
36+cue :: ByteString -> Noun
37+cue = runGet (runBitGet . withBitOrder LL . flip evalStateT (0, M.empty) $ cue')
38+
39+cue' :: BitParser Noun
40+cue' = do
41+ offset <- getOffset
42+ isNotAtom <- parseBit
43+ case isNotAtom of
44+ False -> do
45+ a' <- rub'
46+ let a = atom a'
47+ (offset, m) <- get
48+ put (offset, M.insert offset a m)
49+ pure a
50+ True -> do
51+ isNotRef <- parseBit
52+ case isNotRef of
53+ False -> do
54+ x <- cue'
55+ y <- cue'
56+ let c = cell x y
57+ (offset, m) <- get
58+ put (offset, M.insert offset c m)
59+ pure c
60+ True -> do
61+ ref <- rub'
62+ (_, m) <- get
63+ case M.lookup (fromIntegral ref) m of
64+ Nothing -> undefined
65+ Just a -> pure a
3866
3967jam :: Noun -> ByteString
4068jam n = runPut . runBitPut . withBitOrder LL $ jam' n