Operator precedence: Difference between revisions

Add Ecstasy table
m (syntax highlighting fixup automation)
(Add Ecstasy table)
 
(11 intermediate revisions by 6 users not shown)
Line 732:
 
For quick reference, see also [http://cpp.operator-precedence.com this] equivalent, color-coded table.
 
=={{header|C sharp|C#}}==
[http://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/ MSDN documentation]
 
=={{header|Caché ObjectScript}}==
Line 849 ⟶ 852:
| 0 || Range separator || .. || Not a real operator, hardwired into syntax at specific points
|}
 
=={{header|dc}}==
As ''dc'' is a reverse-polish calculator, all operators/commands pop their arguments off the stack. They are simply evaluated in the order they appear in the code (from left to right).
 
=={{header|Delphi}}==
See table at [[#Free Pascal|Free Pascal]].
 
=={{header|Ecstasy}}==
 
Ecstasy's precedence table is based loosely on the C family of languages, but with significant tweaks to support the most common patterns without parenthesis. At a given level of precedence, all evaluation is from left to right.
 
{| class="wikitable"
! Order !! Operator !! Description !! Associativity
|-
| 1 || & || Unary reference-of, based on C syntax || Right-to-left
|-
| 2 || ++ || Post-increment || Left-to-right
|-
| || -- || Post-decrement ||
|-
| || () || Invoke method / call function ||
|-
| || [] || Array access ||
|-
| || ? || Postfix conditional ||
|-
| || . || Access object member ||
|-
| || .new || Postfix object creation ||
|-
| || .as || Postfix type assertion ||
|-
| || .is || Postfix type comparison ||
|-
| 3 || ++ || Pre-increment || Right-to-left
|-
| || -- || Pre-decrement ||
|-
| || + || Unary plus ||
|-
| || - || Unary minus ||
|-
| || ! || Logical NOT ||
|-
| || ~ || Bitwise NOT ||
|-
| 4 || ?: || Conditional "Elvis" || Right-to-left
|-
| 5 || * || Multiply || Left-to-right
|-
| || / || Divide ||
|-
| || % || Modulo ||
|-
| || / || Divide with remainder ||
|-
| 6 || + || Add || Left-to-right
|-
| || - || Subtract ||
|-
| 7 || << || Shift left || Left-to-right
|-
| || >> || Arithmetic shift right ||
|-
| || >>> || Logical shift right ||
|-
| || & || And ||
|-
| || ^ || Xor ||
|-
| || <nowiki>|</nowiki> || Or ||
|-
| 8 || .. || Range/intervale || Left-to-right
|-
| 9 || <- || Assignment || Right-to-left
|-
| 10 || < <= > >= || Relational || Left-to-right
|-
| || <=> || Order ||
|-
| 11 || == || Equality || Left-to-right
|-
| || != || Inequality ||
|-
| 12 || && || Conditional AND || Left-to-right
|-
| 13 || ^^ || Conditional XOR || Left-to-right
|-
| || <nowiki>||</nowiki> || Conditional OR ||
|-
| 14 || ? : || Conditional ternary || Right-to-left
|-
| 15 || : || Conditional ELSE || Right-to-left
|}
 
=={{header|Eiffel}}==
Line 1,302 ⟶ 1,397:
! style="text-align: left" | Associativity
|-
! 910
<small>highest</small>
| <code>f x</code>
| Function application
| Left
|-
! 9
| <code>.</code>
| Function composition
Line 1,309 ⟶ 1,409:
|-
! 8
| <code>^, ^^, **</code>
| Power
| Right
|-
! 7
| <code>*, /, `quot`, `rem`, `div`, `mod`</code>
|
| Left
|-
! 6
| <code>+, -</code>
|
| Left
|-
! 5
| <code>: ++</code>
| Append to list
| Right
|-
! 4
| <code>==, /=, &lt;, &lt;=, &gt;=, &gt; </code>
| Comparisons
| Compare-operators
|
|-
! 4
| <code>&lt;*&gt; &lt;$&gt; </code>
| Functor ops
| Left
|-
! 3
Line 1,344 ⟶ 1,449:
|-
! 1
| <code>&gt;&gt;, &gt;&gt;=</code>
| Monadic ops
|
| Left
|-
! 1
| <code>=&lt;&lt; &lt;&vert;&gt; </code>
|
| Right
|-
! 0
| <code>$, $!, `seq`</code>
|
| Right
Line 1,616 ⟶ 1,721:
Such an expression "1+2*3+4" is written {+ 1 {* 2 3} 4}
</syntaxhighlight>
 
=={{header|Lang}}==
<pre>
# Precedence | Operator | Associativity | Operator name
# ===========|====================|===============|==========================================
# 0 | (opr) | - | Grouping
# | opr1(opr2) | left-to-right | Function calls
# | opr1[opr2] | left-to-right | Get item
# | opr1?.[opr2] | left-to-right | Optional get item
# | opr1::opr2 | left-to-right | Member access [1]
# | opr1?::opr2 | left-to-right | Optional member access [1]
# | opr1->opr2 | left-to-right | Member access pointer [1]
# -----------|--------------------|---------------|------------------------------------------
# 1 | @opr | right-to-left | Length
# | ^opr | | Deep copy
# -----------|--------------------|---------------|------------------------------------------
# 2 | opr1 ** opr2 | right-to-left | Power [2]
# -----------|--------------------|---------------|------------------------------------------
# 3 | +opr | right-to-left | Positive
# | -opr | | Inverse
# | ~opr | | Bitwise not
# | +|opr | | Increment
# | ▲opr | | Increment [Alternative non-ascii symbol]
# | -|opr | | Decrement
# | ▼opr | | Decrement [Alternative non-ascii symbol]
# | !opr | | Not
# -----------|--------------------|---------------|------------------------------------------
# 4 | opr1 * opr2 | left-to-right | Multiplication
# | opr1 / opr2 | | Division
# | opr1 ~/ opr2 | | Truncation division
# | opr1 // opr2 | | Floor division
# | opr1 ^/ opr2 | | Ceil division
# | opr1 % opr2 | | Modulo
# -----------|--------------------|---------------|------------------------------------------
# 5 | opr1 ||| opr2 | left-to-right | Concat
# | opr1 + opr2 | | Addition
# | opr1 - opr2 | | Subtraction
# -----------|--------------------|---------------|------------------------------------------
# 6 | opr1 << opr2 | left-to-right | Left shift
# | opr1 >> opr2 | | Right shift
# | opr1 >>> opr2 | | Right zero shift
# -----------|--------------------|---------------|------------------------------------------
# 7 | opr1 & opr2 | left-to-right | Bitwise and
# -----------|--------------------|---------------|------------------------------------------
# 8 | opr1 ^ opr2 | left-to-right | Bitwsie xor
# -----------|--------------------|---------------|------------------------------------------
# 9 | opr1 | opr2 | left-to-right | Bitwise or
# -----------|--------------------|---------------|------------------------------------------
# 10 | opr1 <=> opr2 | left-to-right | Spaceship
# | opr1 ~~ opr2 | | Instance of
# | opr1 == opr2 | | Equals
# | opr1 != opr2 | | Not equals
# | opr1 =~ opr2 | | Matches
# | opr1 !=~ opr2 | | Not matches
# | opr1 === opr2 | | Strict equals
# | opr1 !== opr2 | | Strict not equals
# | opr1 < opr2 | | Less than
# | opr1 > opr2 | | Greater than
# | opr1 <= opr2 | | Less than or equals
# | opr1 >= opr2 | | Greater than or equals
# -----------|--------------------|---------------|------------------------------------------
# 11 | opr1 && opr2 | left-to-right | And
# -----------|--------------------|---------------|------------------------------------------
# 12 | opr1 || opr2 | left-to-right | Or
# -----------|--------------------|---------------|------------------------------------------
# 13 | opr1 ?: opr2 | left-to-right | Elvis
# | opr1 ?? opr2 | | Null coalescing
# -----------|--------------------|---------------|------------------------------------------
# 14 | opr1 ? opr2 : opr3 | right-to-left | Inline if
# -----------|--------------------|---------------|------------------------------------------
# 15 | opr1 , opr2 | left-to-right | Comma
# -----------|--------------------|---------------|------------------------------------------
# 16 | opr1 = opr2 | right-to-left | Normal assignment
# | opr1 = opr2 | | Translation
# | opr1 ::= opr2 | | Lvalue operation assignment
# | opr1 ?= opr2 | | Condition operator assignment
# | opr1 := opr2 | | Math operator assignment
# | opr1 $= opr2 | | Operation operator assignment
# | opr1 += opr2 | | Addition assignment
# | opr1 -= opr2 | | Subtraction assignment
# | opr1 *= opr2 | | Multiplication assignment
# | opr1 /= opr2 | | Division assignment
# | opr1 ~/= opr2 | | Truncation division assignment
# | opr1 //= opr2 | | Floor division assignment
# | opr1 ^/= opr2 | | Ceil division assignment
# | opr1 **= opr2 | | Power assignment
# | opr1 %= opr2 | | Modulo assignment
# | opr1 >>= opr2 | | Right shift assignment
# | opr1 >>>= opr2 | | Right zero shift assignment
# | opr1 <<= opr2 | | Left shift assignment
# | opr1 &= opr2 | | Bitwise and assignment
# | opr1 ^= opr2 | | Bitwise xor assignment
# | opr1 |= opr2 | | Bitwise or assignment
# | opr1 |||= opr2 | | Concat assignment
# | opr1 ?:= opr2 | | Elvis assignment
# | opr1 ??= opr2 | | Null coalescing assignment
#
# Footnotes:
# 1) The unary operators (@, ^, +, -, ~, +|, ▲, -|, ▼, and !) have a higher binding than the member access operators if they are on the right of the member access operator.
# 2) The unary operators (+, -, ~, +|, ▲, -|, ▼, and !) have a higher binding than the power operator if they are on the right of the power operator.
#
# Some operators have multiple meanings but they keep the same precedence and associativity
</pre>
 
=={{header|LIL}}==
Line 1,867 ⟶ 2,075:
 
=={{header|OCaml}}==
[httphttps://camlocaml.inria.fr/pub/docsorg/manual-ocaml/expr.html#@manual.kwd32ss:precedence-and-associativity This table] contains the precedence and associativity of operators and other expression constructs in OCaml, including user-defined operators.
 
=={{header|Odin}}==
Unary operators have the highest precedence.
 
There are seven precedence levels for binary and ternary operators.
 
{| class="wikitable"
!Precedence !! Operator
|-
|| 7 || <code>*</code> <code>/</code> <code>%</code> <code>%%</code> <code>&</code> <code>&~</code> <code><<</code> <code>>></code>
|-
|| 6 || <code>+</code> <code>-</code> <code><nowiki>|</nowiki></code> <code>~</code> <code>in</code> <code>not_in</code>
|-
|| 5 || <code>==</code> <code>!=</code> <code><</code> <code>></code> <code><=</code> </code>>=</code>
|-
|| 4 || <code>&&</code>
|-
|| 3 || <code><nowiki>||</nowiki></code>
|-
|| 2 || <code>..=</code> <code>..<</code>
|-
|| 1 || <code>or_else</code> <code>?</code> <code>if</code> <code>when</code>
|}
Binary operators of the same precedence associate from left to right. For instance "x / y * z" is the same as "(x / y) * z"
=={{header|Oforth}}==
 
Line 2,081 ⟶ 2,312:
| lowest || || ||
|}
 
=={{header|Plain English}}==
Plain English treats an expression if it contains any of the operators 'plus', 'minus', 'times', 'divided by', or 'then'. Arguments in expressions are passed in by reference. Expressions are evaluated from left to right, ignoring traditional precedence rules.
 
For example, when evaluating 2 + 3 * 4, Plain English will calculate (2 + 3) * 4, and not 2 + (3 * 4).
 
=={{header|PureBasic}}==
162

edits