cardillan / mindcode

A high level language for Mindustry Logic and Mindustry Schematics.

Home Page:http://mindcode.herokuapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nonstandard operator priorities

cardillan opened this issue · comments

The priority of operators, as currently defined, is somewhat unusual. In Mindcode, we have:

  1. ternary operator ? :
  2. assignment =, /=, **=, -=, +=, *=
  3. exponentiation **
  4. unary negation !, not (see also #72 for this)
  5. multiplication *, /, \, %
  6. addition +, -
  7. shift <<, >>
  8. inequality comparison <, <=, >, >=
  9. equality comparison ==, !=, ===
  10. bitwise operation &, |, ^
  11. boolean and and, &&
  12. boolean or or, ||
  13. unary minus - (this isn't working at the moment, but let's ignore it)

Some of this can be a bit surprising. Compared to other languages, both the ternary operator and assignments are way too high. The most obvious example I stumbled upon is a = b > c ? 0 : 1, which gets compiled to a wee bit unusual a = b > (c ? 0 : 1). I don't use assignments as expressions much, but it would in all probability behave in similarly unexpected ways.

I propose changing the order to a more common one (I tried to do it according to Ruby):

  1. unary negation !, not, ~
  2. exponentiation **
  3. unary minus - (I'll try to make it work while at it)
  4. multiplication *, /, \, %
  5. addition +, -
  6. shift <<, >>
  7. bitwise and &
  8. bitwise or/xor |, ^ (this would mean splitting the binop_bitwise_op category in two, all others are just reordering of existing terms)
  9. inequality comparison <, <=, >, >=
  10. equality comparison ==, !=, ===
  11. boolean and and, &&
  12. boolean or or, ||
  13. ternary operator ? :
  14. assignment =, /=, **=, -=, +=, *=

@francois, as this is again a code-breaking change (hopefully last from me for some while), would you be ok with me implementing this?

LGTM, go ahead. I would also appreciate if you changed the README / HTML to document the Mindcode operator precedence rules. I am not experimented enough and got my priorities wrong.

I will update the syntax document as well. I thought about expanding it a bit anyway.