Nonstandard operator priorities
cardillan opened this issue · comments
The priority of operators, as currently defined, is somewhat unusual. In Mindcode, we have:
- ternary operator
? :
- assignment
=
,/=
,**=
,-=
,+=
,*=
- exponentiation
**
- unary negation
!
,not
(see also #72 for this) - multiplication
*
,/
,\
,%
- addition
+
,-
- shift
<<
,>>
- inequality comparison
<
,<=
,>
,>=
- equality comparison
==
,!=
,===
- bitwise operation
&
,|
,^
- boolean and
and
,&&
- boolean or
or
,||
- 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):
- unary negation
!
,not
,~
- exponentiation
**
- unary minus
-
(I'll try to make it work while at it) - multiplication
*
,/
,\
,%
- addition
+
,-
- shift
<<
,>>
- bitwise and
&
- bitwise or/xor
|
,^
(this would mean splitting thebinop_bitwise_op
category in two, all others are just reordering of existing terms) - inequality comparison
<
,<=
,>
,>=
- equality comparison
==
,!=
,===
- boolean and
and
,&&
- boolean or
or
,||
- ternary operator
? :
- 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.