chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

char-ready?

Open mnieper opened this issue 4 years ago • 10 comments

char-ready? generally returns #t although the next (read-char) invocation would block.

mnieper avatar Sep 15 '20 08:09 mnieper

Yes, this is a known limitation... I thought it was documented somewhere.

Although looking at sexp_char_ready_p now I see nothing preventing us checking for a full char...

ashinn avatar Sep 15 '20 09:09 ashinn

It would be nice if this could be made to work in 80% of the cases. In the remaining 20%, I think one can safely return #f to make it conformant. (Always returning #f is also conformant to the spec, I think, but pretty useless and may break existing programs.)

Am Di., 15. Sept. 2020 um 11:01 Uhr schrieb Alex Shinn < [email protected]>:

Yes, this is a known limitation... I thought it was documented somewhere.

Although looking at sexp_char_ready_p now I see nothing preventing us checking for a full char...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ashinn/chibi-scheme/issues/704#issuecomment-692575544, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHDTQ7RHYCG64R7OU5TAXTSF4UNBANCNFSM4RMZHLEQ .

mnieper avatar Sep 15 '20 09:09 mnieper

Did this arise from a real-world program?

I'm not a fan of char-ready? in general. If it errs towards #f, then a program waiting for a character to be ready may get stuck, never making progress, even though a character is ready. If it errs towards #t, then a program may block when it shouldn't.

Better to use threads.

ashinn avatar Sep 19 '20 14:09 ashinn

Yes, from a real-world program. A user-entered command is being parsed. As soon as the command parser (reading char by char) finds an error, the rest of the line shall be skipped. I'm currently using char-ready?. When it returns #t, I do a read-line to munch the rest of the line.

Am Sa., 19. Sept. 2020 um 16:37 Uhr schrieb Alex Shinn < [email protected]>:

Did this arise from a real-world program?

I'm not a fan of char-ready? in general. If it errs towards #f, then a program waiting for a character to be ready may get stuck, never making progress, even though a character is ready. If it errs towards #t, then a program may block when it shouldn't.

Better to use threads.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ashinn/chibi-scheme/issues/704#issuecomment-695221013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHDTQ5F66D4I4L3PENF37TSGS62ZANCNFSM4RMZHLEQ .

mnieper avatar Sep 19 '20 14:09 mnieper

And this actually involved an incomplete utf8 char that incorrectly blocked your program? That should be impossible for the user to input manually.

ashinn avatar Sep 19 '20 14:09 ashinn

I'm not saying that my approach is the best one. I could also use peek-char to see whether the next read would be a newline (which would mean that an extra read-line would be extraneous).

So it's not crucial to fix char-ready?, although the current implementation seems to contradict the spec unless you interpret "not to hang" very liberally. :)

mnieper avatar Sep 19 '20 14:09 mnieper

PS So what I would like to do is kind of flushing the input port (where the text is entered line-wise). The idea is to call read-line until char-ready? returns #f.

mnieper avatar Sep 19 '20 14:09 mnieper

Having flush-input-port as a primitive could be a useful complement to RnRS flush-output-port. It could call poll() and read() in a loop until there's no data left. Since we're discarding the data, it doesn't matter whether we have a byte port or a character port. It can ignore character encoding.

I tend to agree with Alex that char-ready? is a troublesome primitive, not only in Scheme but any language that has it. byte-ready? would be simpler, but could be subsumed into a generic poll that can wait on multiple ports at once.

lassik avatar Sep 20 '20 10:09 lassik

Common Lisp has a standard clear-input function: http://www.lispworks.com/documentation/HyperSpec/Body/f_clear_.htm#clear-input

lassik avatar Sep 20 '20 13:09 lassik

Sorry, I may have misunderstood the problem. The current limitation is that if u8-ready? we assume char-ready?.

If your bug is not this case, can you elaborate? You just describe using char-ready? to check and then read to the end of the line, not what the apparent bug is.

ashinn avatar Sep 22 '20 07:09 ashinn