aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Binary.hs
diff options
context:
space:
mode:
authorMarvin Borner2022-06-17 21:10:06 +0200
committerMarvin Borner2022-06-17 21:10:06 +0200
commit3a8e9afd461cf648fc6904df64eb76a3a95eeb99 (patch)
tree71ced441ef9c19d3de7d5c178a923eb668745a0d /src/Binary.hs
parentbfc12aff90252dbcd9c40a1d095052ed771d4e56 (diff)
Some binary magic
Diffstat (limited to 'src/Binary.hs')
-rw-r--r--src/Binary.hs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/Binary.hs b/src/Binary.hs
index 4575c49..fbf0a07 100644
--- a/src/Binary.hs
+++ b/src/Binary.hs
@@ -1,14 +1,17 @@
module Binary
( toBinary
, fromBinary
+ , toBitString
+ , fromBitString
) where
import Control.Applicative
+import qualified Data.BitString as Bit
import Data.Char
import Helper
toBinary :: Expression -> String
-toBinary (Bruijn x ) = (replicate x '1') ++ "0"
+toBinary (Bruijn x ) = (replicate (x + 1) '1') ++ "0"
toBinary (Abstraction exp ) = "00" ++ toBinary exp
toBinary (Application exp1 exp2) = "01" ++ (toBinary exp1) ++ (toBinary exp2)
@@ -25,3 +28,24 @@ fromBinary = foldr
showsBin n x = if n == 0
then id
else let (x', b) = divMod x 2 in showsBin (n - 1) x' . (intToDigit b :)
+
+-- TODO: Fix weird endianess things
+padBitList :: [Bool] -> [Bool]
+padBitList lst | length lst `mod` 8 == 0 = lst
+ | otherwise = padBitList ([False] ++ lst)
+
+toBitString :: String -> Bit.BitString
+toBitString = Bit.fromList . padBitList . map
+ (\case
+ '0' -> False
+ '1' -> True
+ )
+
+fromBitString :: Bit.BitString -> String
+fromBitString =
+ map
+ (\case
+ False -> '0'
+ True -> '1'
+ )
+ . Bit.toList