Operator precedence: Difference between revisions

Content deleted Content added
Rdm (talk | contribs)
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 20: 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.
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.


<lang 6502asm>LDX #$02
<syntaxhighlight lang="6502asm">LDX #$02
LDY #$03
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,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.</lang>
LDA ($20),y ;uses the values store at $20 and $21 as a memory address, and reads the byte at that address + Y.</syntaxhighlight>


The use of parentheses for these modes is required, and nicely illustrates the order of operations. To put it in terms of a hierarchy:
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: Line 235:
See [https://arturo-lang.io/documentation/language/#precedence-and-evaluation Arturo documentation].
See [https://arturo-lang.io/documentation/language/#precedence-and-evaluation Arturo documentation].


<lang rebol>print 2 + 3 * 5 ; same as 2 + (3 * 5)
<syntaxhighlight lang="rebol">print 2 + 3 * 5 ; same as 2 + (3 * 5)
print 3 * 5 + 2 ; same as 3 * (5 + 2)</lang>
print 3 * 5 + 2 ; same as 3 * (5 + 2)</syntaxhighlight>


{{out}}
{{out}}
Line 245: Line 245:
=={{header|AWK}}==
=={{header|AWK}}==
See also: [https://www.gnu.org/software/gawk/manual/html_node/Precedence.html#Precedence gawk-Reference]
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.
# Operators are shown in decreasing order of precedence.
# A blank line separates groups of operators with equal precedence.
# A blank line separates groups of operators with equal precedence.
Line 307: Line 307:
# ^= exponentiation assignment
# ^= exponentiation assignment
# **= exponentiation assignment (not all awk's)
# **= exponentiation assignment (not all awk's)
</syntaxhighlight>
</lang>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
Line 418: Line 418:


Here, <code>2 + 1</code> is evaluated first:
Here, <code>2 + 1</code> is evaluated first:
<lang bqn>3 * 2 + 1</lang>
<syntaxhighlight lang="bqn">3 * 2 + 1</syntaxhighlight>


Here, <code>-˜</code> is evaluated first. <code>˜</code> flips the arguments given to a function.
Here, <code>-˜</code> is evaluated first. <code>˜</code> flips the arguments given to a function.
<lang bqn>3 * 2 -˜ 1</lang>
<syntaxhighlight lang="bqn">3 * 2 -˜ 1</syntaxhighlight>


A precedence table for all BQN syntax is shown below.
A precedence table for all BQN syntax is shown below.
Line 1,612: Line 1,612:


=={{header|Lambdatalk}}==
=={{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.
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}
Such an expression "1+2*3+4" is written {+ 1 {* 2 3} 4}
</syntaxhighlight>
</lang>


=={{header|LIL}}==
=={{header|LIL}}==
Line 1,810: Line 1,810:
=={{header|min}}==
=={{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:
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:
<lang min>42 :my-var
<syntaxhighlight lang="min">42 :my-var
42 "my-var" define</lang>
42 "my-var" define</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
Line 2,190: Line 2,190:
=={{header|Q}}==
=={{header|Q}}==
Operators have equal precedence and expressions are evaluated from right to left.
Operators have equal precedence and expressions are evaluated from right to left.
<lang q>q)3*2+1
<syntaxhighlight lang="q">q)3*2+1
9
9
q)(3*2)+1 / Brackets give the usual order of precedence
q)(3*2)+1 / Brackets give the usual order of precedence
Line 2,196: Line 2,196:
q)x:5
q)x:5
q)(x+5; x:20; x-5)
q)(x+5; x:20; x-5)
25 20 0</lang>
25 20 0</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 2,204: Line 2,204:
=={{header|Racket}}==
=={{header|Racket}}==
Racket uses S-expr for its syntax, operators and functions show no precedences as all code is written as:
Racket uses S-expr for its syntax, operators and functions show no precedences as all code is written as:
<lang Racket>(function arguments ...)</lang>
<syntaxhighlight lang="racket">(function arguments ...)</syntaxhighlight>


function being any function or operator (language or user defined) and arguments are the arguments passed to it.
function being any function or operator (language or user defined) and arguments are the arguments passed to it.
Line 2,340: Line 2,340:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
The next table present operators from higher precedence (Evaluated first) to lower precedence.
The next table present operators from higher precedence (Evaluated first) to lower precedence.


Line 2,362: Line 2,362:


See 3+5*4 # prints 23
See 3+5*4 # prints 23
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 2,454: Line 2,454:
the Seed7 Structured Syntax Description ([http://seed7.sourceforge.net/manual/syntax.htm S7SSD])
the Seed7 Structured Syntax Description ([http://seed7.sourceforge.net/manual/syntax.htm S7SSD])
A S7SSD statement like
A S7SSD statement like
<lang seed7>$ syntax expr: .(). + .() is -> 7;</lang>
<syntaxhighlight lang="seed7">$ syntax expr: .(). + .() is -> 7;</syntaxhighlight>
specifies the syntax of the <code>+</code> operator.
specifies the syntax of the <code>+</code> operator.
The right arrow <code>-&gt;</code> describes the associativity:
The right arrow <code>-&gt;</code> describes the associativity:
Binding of operands from left to right. With <code>7</code> the priority
Binding of operands from left to right. With <code>7</code> the priority
of the <code>+</code> operator is defined. The syntax pattern
of the <code>+</code> operator is defined. The syntax pattern
<lang seed7>.(). + .()</lang>
<syntaxhighlight lang="seed7">.(). + .()</syntaxhighlight>
is introduced and delimited with dots (<code>.</code>). Without dots the pattern is
is introduced and delimited with dots (<code>.</code>). Without dots the pattern is
<pre>() + ()</pre>
<pre>() + ()</pre>
Line 2,519: Line 2,519:


For example:
For example:
<lang ruby>1+2 * 3+4 # means: (1+2) * (3+4)</lang>
<syntaxhighlight lang="ruby">1+2 * 3+4 # means: (1+2) * (3+4)</syntaxhighlight>


See also the [https://trizen.gitbooks.io/sidef-lang/content/syntax_and_semantics/operator_precedence.html documentation] on the precedence of operators.
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: Line 2,574:
Math expressions are usually parenthesized for better readability (by beginners):
Math expressions are usually parenthesized for better readability (by beginners):
<lang smalltalk>5 + b negated. "same as 5 + (b negated); unary > binary"
<syntaxhighlight 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 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"
a bitAnd:1+a abs. "same as a bitAnd:(1+(a abs)); unary > binary > keyword"
Line 2,580: Line 2,580:


"Beginners might be confused by:"
"Beginners might be confused by:"
5 + a * b "same as (5 + a) * b; all binary; therefore left to right"</lang>
5 + a * b "same as (5 + a) * b; all binary; therefore left to right"</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==