urbit/pkg/hs-urbit/lib/Noun/Lens.hs

50 lines
1.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE MagicHash #-}
2019-07-02 05:51:26 +03:00
module Noun.Lens where
import ClassyPrelude
2019-07-02 05:51:26 +03:00
import Noun.Pill
import Noun
import Noun.Atom
import Control.Lens
2019-07-02 05:51:26 +03:00
import Noun.Jam.Fast (jam, jamBS)
2019-07-03 08:14:39 +03:00
import Noun.Cue.Fast (cue, cueBS)
--------------------------------------------------------------------------------
2019-07-03 08:14:39 +03:00
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right x) = Just x
_CueBytes :: Prism' ByteString Noun
2019-07-03 08:14:39 +03:00
_CueBytes = prism' jamBS (eitherToMaybe . cueBS)
_Cue :: Prism' Atom Noun
2019-07-03 08:14:39 +03:00
_Cue = prism' jam (eitherToMaybe . cue)
loadNoun :: FilePath -> IO (Maybe Noun)
2019-07-03 08:14:39 +03:00
loadNoun = fmap (preview _CueBytes) . readFile
dumpJam :: FilePath -> Noun -> IO ()
2019-07-03 08:14:39 +03:00
dumpJam fp = writeFile fp . view (re _CueBytes)
tryCuePill :: PillFile -> IO ()
tryCuePill pill =
loadNoun (show pill) >>= \case Nothing -> print "nil"
Just (Atom _) -> print "atom"
_ -> print "cell"
2019-07-03 08:14:39 +03:00
tryCueJamPill :: PillFile -> IO ()
tryCueJamPill pill = do
n <- loadNoun (show pill) >>= \case
Nothing -> do print "failure"
pure (Atom 0)
Just (Atom a) -> do print "atom"
pure (Atom a)
Just (Cell h t) -> do print "cell"
pure (Cell h t)
bs <- evaluate (force (jamBS n))
print ("jam size: " <> show (length bs))