module Language.Literals.Binary where
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
import Data.Bits
b = QuasiQuoter
{ quoteExp = return . LitE . IntegerL . readBinary
, quotePat = return . LitP . IntegerL . readBinary
, quoteType = error "No quasiquoter for types."
, quoteDec = error "No quasiquoter for declarations." }
readBinary :: String -> Integer
readBinary = foldl f 0 where
f x '0' = shift x 1
f x '1' = shift x 1 + 1
f x ' ' = x
f x _ = error "Not a valid binary literal."