Author Topic: Fix C operator precedence rules  (Read 9125 times)

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Fix C operator precedence rules
« on: October 25, 2018, 12:49:12 AM »
C's precedence rules are not in fact, there are cases where they can be considered really bad.

These are the current rules in C2:

1. (),.*, ->*
2. !,~
3. *, /, %
4. -, +
5. <<, >>
6. >=, <=, >, <
7. ==, !=
8. &
9. ^
10. |
11. &&
12. ||
13. ?
14. =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
15. ,

I would suggest a flatter and more reasonable precedence order:

1. (),.*, ->*
2. !,~
3. *, /, %
4. <<, >>
5. &, ^, |
6. -, +
7. ==, !=, >=, <=, >, <
8. &&, ||
9. ?
10. =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
11 ,

This means that we cant write something like a || b && c || d && e and expect anything other left-to-right evaluation. In my opinion the operator precedence rules here are allowing hard-to-read code.

We're also removing the common pitfall of a << 3 + b << 2, it's no longer evaluated to (a << (3 + b)) << 2 but instead (a << 3) + (b << 2).

Flattening the bitwise operators follows the same rationale as for logical and/or. An expression such as a | b ^ c & d which has the evaluation of (a | (b ^ (c & d))) might be unexpected for many.

Moving the bitwise operators before comparisons finally allows us for the intuitive code of a & b == 0 instead of the currently required (a & b) == 0. I would say that the standard evaluation of a & (b == 0) is virtually never intended.

We'd be removing three levels of precedence and creating code that's easier to understand and remember. (And the bitwise operator precedence of C should be considered a bug more than anything.)

Other languages with similar precedence rules: Julia, Nim, Zig, Python.

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Fix C operator precedence rules
« Reply #1 on: October 26, 2018, 09:09:48 AM »
I didn't catch this as one of C's improvement points yet (too used to some things ;) ). But looking at the examples it indeed seems a better way.
What i'm missing in the table is the [ ] (index operator), but in other languages it seems to be at number 1.

I'm putting this on the list of improvement points, so what has to be done is:
- modify parser
- add tests
- modify documentation to show differences between C and C2
- enjoy()

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Re: Fix C operator precedence rules
« Reply #2 on: October 26, 2018, 11:24:36 AM »
 :D