Skip to content

fix: correct unary minus/plus/tilde precedence#1236

Open
jorgsowa wants to merge 3 commits into
mainfrom
fix/unary-minus-precedence
Open

fix: correct unary minus/plus/tilde precedence#1236
jorgsowa wants to merge 3 commits into
mainfrom
fix/unary-minus-precedence

Conversation

@jorgsowa
Copy link
Copy Markdown
Collaborator

Unary -, +, and ~ shared the same precedence key as binary - and + (level 15), placing them below * (level 16). This caused -20 * 5 + 10 to be parsed as -(20 * 5 + 10) instead of (-20) * 5 + 10.

Add u-, u+, u~ keys to the precedence table at the ! level (17) and look them up in resolvePrecedence so unary operators correctly bind tighter than multiplicative operators.

Comment thread src/ast.js
["+", "-", "."],
["*", "/", "%"],
["!"],
["!", "u-", "u+", "u~"], // u- etc. are unary variants; higher than * so -20*5 parses as (-20)*5
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how relevant in practice, but according to https://www.php.net/manual/en/language.operators.precedence.php the unary operators have even higher precedence than instanceof. Would it make sense to reflect this here?
Also, tangentially, are we lacking ++ and --?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I changed the precedence of instanceof. Pre and post increment operators, we can tackle in another branch.

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

Successfully merging this pull request may close these issues.

2 participants