From aa160950ef426bc959ddce12bd45d2af0cf8dc72 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 14 Nov 2019 17:57:42 +0100 Subject: Handle eof in file IO --- VM.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/VM.hs b/VM.hs index ae9dc6f..7baea0d 100644 --- a/VM.hs +++ b/VM.hs @@ -129,8 +129,11 @@ vmRunBuiltin state "sys-close-file" [RVNum fid] = do hClose (sHandles state ! fid) return (RVList [], state { sHandles = Map.delete fid (sHandles state) }) vmRunBuiltin state "sys-get-char" [RVNum fid] = do - ch <- hGetChar (sHandles state ! fid) - return (RVString [ch], state) + let h = sHandles state ! fid + eof <- hIsEOF h + if eof + then return (RVList [], state) + else hGetChar h >>= \ch -> return (RVString [ch], state) vmRunBuiltin state "sys-put-string" [RVNum fid, RVString str] = do hPutStr (sHandles state ! fid) str return (RVList [], state) -- cgit v1.2.3-70-g09d2