diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2019-11-14 17:57:42 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2019-11-14 17:57:42 +0100 |
commit | aa160950ef426bc959ddce12bd45d2af0cf8dc72 (patch) | |
tree | de60752fa56318e6218d6e6583db5bde8d6cba02 | |
parent | 551f74a3f77d5f0b7b5221fa38ef67df5790083f (diff) |
Handle eof in file IO
-rw-r--r-- | VM.hs | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -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) |