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

Improper handling of negative signs with exponents and modulo #170

Open
jackson-smith-a opened this issue Apr 7, 2023 · 2 comments
Open

Comments

@jackson-smith-a
Copy link

It seems that for some reason -10**2 was incorrectly being parsed as (-10)**2 instead of -(10**2). To fix this the preprocesser replaces all negative signs that don't directly follow "e", "E", or "**" and don't directly precede "=" with - 1 *. However, this changes parsing behavior on operators with greater or equal precedence to multiplication. This only actually matters for exponentiation and modulo, since multiplication and division don't care where the negative goes. Since the preprocesser doesn't check for whitespace after the exponentiation operator, 10 ** -2 is getting turned into 10 ** - 1 * 2 and parsed as (10 ** (-1)) * 2 instead of 10 ** (-2). Since the preprocesser doesn't check for modulo, 5 % -2 is getting turned into 5 % - 1 * 2 and parsed as (5 % (-1)) * 2 (which is 0) instead of 5 % (-2) (which is 1 if you ask JavaScript and -1 if you ask CPython).

This is the exact spot where this is happening: https://github.com/vpython/glowscript/blob/master/lib/compiling/GScompiler.js#L376

@BruceSherwood
Copy link
Member

Thanks very much for this analysis. Unary minus has been a significant challenge.....

@BruceSherwood
Copy link
Member

Thanks again for your report. I've fixed exponentiation but was unable to fix the modulo case (though it must be pretty unusual to use a negative number with modulo). I'm about to update webvpython.org.

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