From: Mark Date: Thu, 13 Jul 2023 18:23:20 +0000 (-0600) Subject: correct peek_byte/2 bugs (#1882) X-Git-Tag: v0.9.2~61 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=b051f391455ab10b4f3c29392a7478a0484f633c;p=scryer-prolog.git correct peek_byte/2 bugs (#1882) --- diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 90f927c4..c1b4678e 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -1268,12 +1268,9 @@ impl Stream { } } Stream::InputFile(ref mut file) => { - let mut b = [0u8; 1]; - - match file.read(&mut b)? { - 1 => { - file.stream.get_mut().file.seek(SeekFrom::Current(-1))?; - Ok(b[0]) + match file.peek_byte() { + Some(result) => { + Ok(result?) } _ => Err(std::io::Error::new( ErrorKind::UnexpectedEof, diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 7045eff0..ba67067d 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -2444,7 +2444,11 @@ impl Machine { addr, ); - return Ok(()); + if !self.machine_st.fail { + return Ok(()); + } else { + self.machine_st.fail = false; + } } let addr = match addr { diff --git a/src/parser/char_reader.rs b/src/parser/char_reader.rs index 32854b07..2a1db29f 100644 --- a/src/parser/char_reader.rs +++ b/src/parser/char_reader.rs @@ -128,6 +128,19 @@ impl CharReader { Ok(&self.buf[self.pos..]) } + + pub fn peek_byte(&mut self) -> Option> { + match self.refresh_buffer() { + Ok(_buf) => {} + Err(e) => return Some(Err(e)), + } + + return if let Some(b) = self.buf.get(0).cloned() { + Some(Ok(b)) + } else { + None + }; + } } impl CharRead for CharReader {