summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-11-14 17:57:42 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-11-14 17:57:42 +0100
commitaa160950ef426bc959ddce12bd45d2af0cf8dc72 (patch)
treede60752fa56318e6218d6e6583db5bde8d6cba02
parent551f74a3f77d5f0b7b5221fa38ef67df5790083f (diff)
Handle eof in file IO
-rw-r--r--VM.hs7
1 files 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)