When designing a language, you can tweak precedence and other rules to create an elegant experience. Sometimes, not allowing certain constructs can improve readability, without sacrificing much expressiveness. You only loose similarity to other languages, thus making your language harder to learn.
You established -x**y == -(x**y)
. This makes sense, as this is the convention in maths.
You are considering ++x**y == (++x)**y
. The alternative does not make sense, because x**y
is not an lvalue. However, you may want to consider disallowing increment on an expression level. E.g. Go takes this route. This avoids the problem that x = ++x
is undefined behaviour in many languages. Also, x++**y
is downright ugly ;-)
If you have a static type system and a boolean type, then specifying precedence between **
and !
does not make sense: One is a numeric operator, the other is logical.
If you have no boolean type, or no static type system, then !
should have lower precedence, so !x**y
is !(x**y)
. Exponentiation should bind very tightly, only increment and method calls should have higher precedence. Also, the interpretation (!x)**y
is wonky: Few would expect that, and you should generally follow the principle of least surprise. This would also give !
a higher precedence than unary minus…
I personally am a fan of alphabetic logical operators like not
, and
, or
with very low precedence. This can improve readability.
I am not a fan of bitwise operators like <<
, ~
, &
because bit twiddling is only useful in very few domains and these characters would be valuable for other operators. <<
could be some stream operator, ~
could be string concatenation. Look into Haskell or Perl6 for cute ideas.
If you want to include bitwise operators, then they should relate in precedence to their logical equivalents: !
and ~
might have same precedence. ^
(xor) and |
should have roughly same precedence, and bind slightly more tightly than the logical operators ||
and &&
. &
should have higher precedence than |
, just like &&
usually has higher precedence than ||
.
To avoid suprises, stay near C, as it is one of the most influential languages. You can find a precedence table on Wikipedia. Compare with Perl, Python. Also remember that some languages in the ML family survive without such elaborate precedence tables.
-
has a lower precedence, I know this from the endless array of ACT questions that try to trick you with- x^2
vs(-x)^2
– Daniel Gratzer Sep 26 '13 at 7:50