Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric Input Error on EOF or Invalid Number – Bug or Implementation Detail? #23

Open
sy-python opened this issue Dec 1, 2024 · 14 comments

Comments

@sy-python
Copy link

Description:
This interpreter, when parsing numeric input, raises an error (ShakespeareRuntimeError) when encountering an invalid number or reaching the end of file (EOF). However, other implementations of the Shakespeare Programming Language (SPL) tend to return 0 in these cases instead of raising an error.

If this is a deliberate implementation choice, please indicate as such in project files. If not, then it is a bug and should be fixed.

@zmbc
Copy link
Owner

zmbc commented Dec 2, 2024

I think I slightly prefer the error behavior from a design standpoint, but would prefer to be consistent with other implementations and especially the language spec. Is this behavior in the spec, and which implementations do this?

@sy-python
Copy link
Author

It's not in the language specification, but also neither is the fact that -1 is returned by the character read input at EoF. The canonical C implementation does it, but it also consumes the whole line from input when requesting a number, so caveat emptor.
Considering the language doesn't have builtin error handling, I personally prefer the non-error approach and in my (very opinionated) fork I have done exactly so.

@zmbc
Copy link
Owner

zmbc commented Dec 6, 2024

It's not in the language specification, but also neither is the fact that -1 is returned by the character read input at EoF. The canonical C implementation does it, but it also consumes the whole line from input when requesting a number, so caveat emptor.

Ah yes, this is ringing a bell. There are a lot of holes in both the spec and the reference implementation in this area. See #5

I think I agree with you that raising a Python error here doesn't make sense. It's unfortunate that there is no "flag" value you could return that is unambiguous -- i.e. if 0 means "bad input" then you can't tell when the user actually entered 0.

Would you be open to adding a note about this to the documentation somewhere, as part of #25?

@sy-python
Copy link
Author

if 0 means "bad input" then you can't tell when the user actually entered 0.

That is why in my main fork, I made it so numeric input doesn't consume newline. That means that you are forced to take a character input between any two numeric inputs, and can check that for eof

@zmbc
Copy link
Owner

zmbc commented Dec 8, 2024

But not invalid number, right? So a program that does one numeric input, one character input, and then one numeric input cannot tell the difference between "0 1" and "a 1". I think this is a fundamental problem with any return value from numeric input being a possible non-error.

@sy-python
Copy link
Author

If you run numeric, character, numeric on "a 1", you get 0 and 97, and there is still " 1" left. Basically, if there is no valid number, nothing is consumed. It does require storing the last byte of input in memory

@zmbc
Copy link
Owner

zmbc commented Dec 9, 2024 via email

@sy-python
Copy link
Author

I don't think there is a way around that. The problem is that the alternative makes numeric input potentially dangerous. Consider working with input like "1\n23\n45\n". If reading a number after eof throws, the only way to work with this input is by avoiding numeric input and parsing this by-character, which defeats the purpose of having two separate input commands.

@zmbc
Copy link
Owner

zmbc commented Dec 9, 2024 via email

@sy-python
Copy link
Author

sy-python commented Dec 9, 2024 via email

@zmbc
Copy link
Owner

zmbc commented Dec 9, 2024 via email

@sy-python
Copy link
Author

I am not sure how do edit the docs, and I did implement the error codes

@zmbc
Copy link
Owner

zmbc commented Dec 13, 2024

Thanks so much! I haven't documented this well, but the docs are in Markdown in the docs subdirectory of the repo, and you should be able to preview changes with mkdocs serve, assuming you've done a dev install with Poetry.

Want to give it a try? Or, I think I can add onto your existing PR :)

@sy-python
Copy link
Author

I will try it tomorrow when I have time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants