Rethink operator precedence #128

Closed
opened 2025-06-30 16:38:18 +02:00 by riki · 0 comments
Owner

I don't like the current precedence of operations in haku (and other languages, for that matter.) I think they result in lots of unnecessary parentheses when doing math, and frankly, for writing math operations, I'd prefer that they always went from left to right, rather than "multiplications first, additions later."

Therefore, I want to flatten the precedence table a bit. Current precedence:

: =
== != < <= > >=
+ -
* /
(function call)

New precedence (including new operators #44 #126 #127)

: =
and or
== != < <= > >=
+ - * / |
.
(function call)

Basically: arithmetic goes from left to right. Comparisons still have lower precedence, because that way is more convenient.

This still makes passing arguments to functions really annoying. Therefore, whitespace around operators should become significant.

  • General case + - * / .: If no space on either side, precedence is higher than (function call). If there is space on either side, precedence is lower than (function call). Unbalanced whitespace is an error.
  • Special case -: If space on the left only, treat as prefix.
  • Special case |: Unaffected by these rules, because it goes tightly into interaction with function calls. Precedence always lower than function calls, and is insensitive to whitespace.

Additionally, in this configuration it should be possible to have a newline before an operator, to carry the expression over to the next line. This should work with all operators except function calls.

I don't like the current precedence of operations in haku (and other languages, for that matter.) I think they result in lots of unnecessary parentheses when doing math, and frankly, for _writing_ math operations, I'd prefer that they always went from left to right, rather than "multiplications first, additions later." Therefore, I want to flatten the precedence table a bit. Current precedence: ``` : = == != < <= > >= + - * / (function call) ``` New precedence (including new operators #44 #126 #127) ``` : = and or == != < <= > >= + - * / | . (function call) ``` Basically: arithmetic goes from left to right. Comparisons still have lower precedence, because that way is more convenient. This still makes passing arguments to functions really annoying. Therefore, whitespace around operators should become significant. - General case `+ - * / .`: If no space on either side, precedence is higher than `(function call)`. If there is space on either side, precedence is lower than `(function call)`. Unbalanced whitespace is an error. - Special case `-`: If space on the left only, treat as prefix. - Special case `|`: Unaffected by these rules, because it goes tightly into interaction with function calls. Precedence always lower than function calls, and is insensitive to whitespace. Additionally, in this configuration it should be possible to have a newline before an operator, to carry the expression over to the next line. This should work with all operators except function calls.
riki added the
area/haku
goal/h2
labels 2025-06-30 16:39:30 +02:00
riki closed this issue 2025-09-03 23:32:05 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: riki/rkgk#128
No description provided.