module Minilang.IO where import qualified Data.ByteString as BS import qualified Data.Text as Text import Data.Text.Encoding ( decodeUtf8With, encodeUtf8, ) import Data.Text.Encoding.Error (lenientDecode) import Minilang.Lambda.Eval (eval) import System.IO (Handle) -- | Read a "program" from @hin@ handle, evaluate it and dump the result -- on @hout@. runEval :: Handle -> Handle -> IO () runEval hin hout = do programText <- Text.unpack . decodeUtf8With lenientDecode <$> BS.hGetContents hin let ast = read programText env = mempty result = eval ast env BS.hPut hout (encodeUtf8 (Text.pack (show result) <> "\n"))