fix: correct unary minus/plus/tilde precedence#1236
Conversation
f9de0f1 to
46bb4a0
Compare
| ["+", "-", "."], | ||
| ["*", "/", "%"], | ||
| ["!"], | ||
| ["!", "u-", "u+", "u~"], // u- etc. are unary variants; higher than * so -20*5 parses as (-20)*5 |
There was a problem hiding this comment.
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 --?
There was a problem hiding this comment.
Good point. I changed the precedence of instanceof. Pre and post increment operators, we can tackle in another branch.
There was a problem hiding this comment.
Hmm, could it be that you moved it too far? instanceof has lower precedence than the unary uperators, but higher precedence than the arithmetic operators (+, -, ., *, /, %).
There was a problem hiding this comment.
That's right. Operator ! should be split away from unary arithmetic operators.
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.