Skip to content

Improper handling of negative signs with exponents and modulo #170

Open
@jackson-smith-a

Description

@jackson-smith-a

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions