Operator precedence: Difference between revisions

Add Ecstasy table
(Standardize Pascal, allegedly adding new external links [spam detection false positive])
(Add Ecstasy table)
 
(13 intermediate revisions by 8 users not shown)
Line 20:
There are two types of indirect addressing: Indirect X and Indirect Y. These differ in that the indirect X mode applies the offset before looking up the address.
 
<langsyntaxhighlight lang="6502asm">LDX #$02
LDY #$03
 
LDA ($20,x) ;uses the values stored at $20+x and $21+x as a memory address, and reads the byte at that address.
LDA ($20),y ;uses the values store at $20 and $21 as a memory address, and reads the byte at that address + Y.</langsyntaxhighlight>
 
The use of parentheses for these modes is required, and nicely illustrates the order of operations. To put it in terms of a hierarchy:
Line 235:
See [https://arturo-lang.io/documentation/language/#precedence-and-evaluation Arturo documentation].
 
<langsyntaxhighlight lang="rebol">print 2 + 3 * 5 ; same as 2 + (3 * 5)
print 3 * 5 + 2 ; same as 3 * (5 + 2)</langsyntaxhighlight>
 
{{out}}
Line 245:
=={{header|AWK}}==
See also: [https://www.gnu.org/software/gawk/manual/html_node/Precedence.html#Precedence gawk-Reference]
<syntaxhighlight lang="awk">
<lang AWK>
# Operators are shown in decreasing order of precedence.
# A blank line separates groups of operators with equal precedence.
Line 307:
# ^= exponentiation assignment
# **= exponentiation assignment (not all awk's)
</syntaxhighlight>
</lang>
 
=={{header|BASIC256}}==
Line 418:
 
Here, <code>2 + 1</code> is evaluated first:
<syntaxhighlight lang ="bqn">3 * 2 + 1</langsyntaxhighlight>
 
Here, <code>-˜</code> is evaluated first. <code>˜</code> flips the arguments given to a function.
<syntaxhighlight lang ="bqn">3 * 2 -˜ 1</langsyntaxhighlight>
 
A precedence table for all BQN syntax is shown below.
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,486 ⟶ 1,591:
Here's an informal treatment of the grammar:
 
Conjunctions require a left and right argument, either of which may be a noun or a verb. If one argument is omitted the conjunction is curried with the remaining argument, forming an adverb (if the right argument is omitted, the precedence of the conjunction is treated as lower than the precedence of a verb -- this might be thought of as a consequence of the "long left scope" of conjunctions extending as far as possible).
 
Adverbs require a single left argument, which may be a noun or a verb.
Line 1,612 ⟶ 1,717:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
As with Common Lisp and Scheme, Lambdatalk uses s-expressions so there is no need for operator precedence.
Such an expression "1+2*3+4" is written {+ 1 {* 2 3} 4}
</syntaxhighlight>
</lang>
 
=={{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,810 ⟶ 2,018:
=={{header|min}}==
min is a stack language that uses postfix notation, so for the most part, all operators have the same precedence. Sigils are a small exception. However, they de-sugar to postfix. For example, the following two lines are equivalent:
<langsyntaxhighlight lang="min">42 :my-var
42 "my-var" define</langsyntaxhighlight>
 
=={{header|Nim}}==
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}}==
Line 2,190 ⟶ 2,426:
=={{header|Q}}==
Operators have equal precedence and expressions are evaluated from right to left.
<langsyntaxhighlight lang="q">q)3*2+1
9
q)(3*2)+1 / Brackets give the usual order of precedence
Line 2,196 ⟶ 2,432:
q)x:5
q)(x+5; x:20; x-5)
25 20 0</langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 2,204 ⟶ 2,440:
=={{header|Racket}}==
Racket uses S-expr for its syntax, operators and functions show no precedences as all code is written as:
<syntaxhighlight lang Racket="racket">(function arguments ...)</langsyntaxhighlight>
 
function being any function or operator (language or user defined) and arguments are the arguments passed to it.
Line 2,340 ⟶ 2,576:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
The next table present operators from higher precedence (Evaluated first) to lower precedence.
 
Line 2,362 ⟶ 2,598:
 
See 3+5*4 # prints 23
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 2,454 ⟶ 2,690:
the Seed7 Structured Syntax Description ([http://seed7.sourceforge.net/manual/syntax.htm S7SSD])
A S7SSD statement like
<langsyntaxhighlight lang="seed7">$ syntax expr: .(). + .() is -> 7;</langsyntaxhighlight>
specifies the syntax of the <code>+</code> operator.
The right arrow <code>-&gt;</code> describes the associativity:
Binding of operands from left to right. With <code>7</code> the priority
of the <code>+</code> operator is defined. The syntax pattern
<syntaxhighlight lang ="seed7">.(). + .()</langsyntaxhighlight>
is introduced and delimited with dots (<code>.</code>). Without dots the pattern is
<pre>() + ()</pre>
Line 2,519 ⟶ 2,755:
 
For example:
<langsyntaxhighlight lang="ruby">1+2 * 3+4 # means: (1+2) * (3+4)</langsyntaxhighlight>
 
See also the [https://trizen.gitbooks.io/sidef-lang/content/syntax_and_semantics/operator_precedence.html documentation] on the precedence of operators.
Line 2,574 ⟶ 2,810:
Math expressions are usually parenthesized for better readability (by beginners):
<langsyntaxhighlight lang="smalltalk">5 + b negated. "same as 5 + (b negated); unary > binary"
a abs - b sqrt "same as (a abs) - (b sqrt); unary > binary"
a bitAnd:1+a abs. "same as a bitAnd:(1+(a abs)); unary > binary > keyword"
Line 2,580 ⟶ 2,816:
 
"Beginners might be confused by:"
5 + a * b "same as (5 + a) * b; all binary; therefore left to right"</langsyntaxhighlight>
 
=={{header|Standard ML}}==
162

edits