You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
Hello, I stumbled across this bug on https://glowscript.org/ and wanted to mention that it also applies to the division operation, which is much more common to do with a negative. Example:
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 into10 ** - 1 * 2
and parsed as(10 ** (-1)) * 2
instead of10 ** (-2)
. Since the preprocesser doesn't check for modulo,5 % -2
is getting turned into5 % - 1 * 2
and parsed as(5 % (-1)) * 2
(which is0
) instead of5 % (-2)
(which is1
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
The text was updated successfully, but these errors were encountered: