2019-07-01 07:47:21 +03:00
|
|
|
{-# LANGUAGE MagicHash #-}
|
|
|
|
|
2019-07-02 05:51:26 +03:00
|
|
|
module Noun.Lens where
|
2019-07-01 07:47:21 +03:00
|
|
|
|
|
|
|
import ClassyPrelude
|
2019-07-02 05:51:26 +03:00
|
|
|
import Noun.Pill
|
|
|
|
import Noun
|
|
|
|
import Noun.Atom
|
2019-07-01 07:47:21 +03:00
|
|
|
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-01 07:47:21 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-03 08:14:39 +03:00
|
|
|
eitherToMaybe (Left _) = Nothing
|
|
|
|
eitherToMaybe (Right x) = Just x
|
|
|
|
|
2019-07-01 07:47:21 +03:00
|
|
|
_CueBytes :: Prism' ByteString Noun
|
2019-07-03 08:14:39 +03:00
|
|
|
_CueBytes = prism' jamBS (eitherToMaybe . cueBS)
|
2019-07-01 07:47:21 +03:00
|
|
|
|
|
|
|
_Cue :: Prism' Atom Noun
|
2019-07-03 08:14:39 +03:00
|
|
|
_Cue = prism' jam (eitherToMaybe . cue)
|
2019-07-01 07:47:21 +03:00
|
|
|
|
|
|
|
loadNoun :: FilePath -> IO (Maybe Noun)
|
2019-07-03 08:14:39 +03:00
|
|
|
loadNoun = fmap (preview _CueBytes) . readFile
|
2019-07-01 07:47:21 +03:00
|
|
|
|
|
|
|
dumpJam :: FilePath -> Noun -> IO ()
|
2019-07-03 08:14:39 +03:00
|
|
|
dumpJam fp = writeFile fp . view (re _CueBytes)
|
2019-07-01 07:47:21 +03:00
|
|
|
|
|
|
|
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))
|