blob: b2b3dcc0b97ad4017e82183673254874178bd630 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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.DeBruijn (eval, fromDeBruijn, toDeBruijn)
import Minilang.Lambda.Parser (desugar, parse, pretty, resugar)
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 <- decodeUtf8With lenientDecode <$> BS.hGetContents hin
let parsed = parse programText
let result =
case parsed of
Left err -> Text.pack $ show err
Right ast ->
let evaluate =
pretty
. resugar
. fromDeBruijn
. eval
. toDeBruijn
. desugar
in evaluate ast
BS.hPut
hout
(encodeUtf8 result)
|