aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Helper.hs
diff options
context:
space:
mode:
authorMarvin Borner2022-08-10 18:53:34 +0200
committerMarvin Borner2022-08-10 18:53:34 +0200
commit792534b3888bc1b9c33047f1c312c4e17a720885 (patch)
treeb0401f5c1ca156858fb7de94a5488151be075c89 /src/Helper.hs
parentcba3d7d21241f8db913e6e2733a8edc3a522ee62 (diff)
Added syntactic sugar for strings & chars
Diffstat (limited to 'src/Helper.hs')
-rw-r--r--src/Helper.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/Helper.hs b/src/Helper.hs
index 160ca9a..d653319 100644
--- a/src/Helper.hs
+++ b/src/Helper.hs
@@ -8,6 +8,7 @@ module Helper where
import qualified Control.Monad.State as S
import qualified Data.BitString as Bit
import qualified Data.ByteString as Byte
+import qualified Data.ByteString.Char8 as C
import Data.List
import Text.Megaparsec
@@ -136,8 +137,15 @@ encodeBytes bytes = listify $ map
(encodeByte . Bit.toList . Bit.bitString . Byte.pack . (: []))
(Byte.unpack bytes)
+stringToExpression :: String -> Expression
+stringToExpression = encodeBytes . C.pack
+
+charToExpression :: Char -> Expression
+charToExpression ch = encodeByte $ Bit.toList $ Bit.bitString $ C.pack [ch]
+
encodeStdin :: IO Expression
encodeStdin = do
+ putStrLn "Waiting for stdin eof"
bytes <- Byte.getContents
pure $ encodeBytes bytes
@@ -153,10 +161,10 @@ decodeByte (Abstraction (Application (Application (Bruijn 0) (Abstraction (Abstr
= False : (decodeByte es)
decodeByte (Abstraction (Application (Application (Bruijn 0) (Abstraction (Abstraction (Bruijn 1)))) es))
= True : (decodeByte es)
-decodeByte _ = error "invalid"
+decodeByte _ = error "invalid" -- TODO: Better errors using Maybe
decodeStdout :: Expression -> String
-decodeStdout e = show $ Byte.concat $ map
+decodeStdout e = C.unpack $ Byte.concat $ map
(Bit.realizeBitStringStrict . Bit.fromList . decodeByte)
(unlistify e)