forked from DlangRen/Programming-in-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_precedence.d
318 lines (243 loc) · 8.73 KB
/
operator_precedence.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
Ddoc
$(DERS_BOLUMU $(IX operator precedence) $(IX precedence, operator) Operator Precedence)
$(P
As we have been using throughout the book, expressions can be $(I chained) with more than one operator. For example, the following line contains four expressions chained with three operators:
)
---
a = b + c * d // three operators: =, +, and *
---
$(P
Operator precedence rules specify the order that the chained operators are executed in and the expressions that they use. Operators are executed according to their precedences: first the higher ones, then the lower ones.
)
$(P
The following is D's operator precedence table. Operators are listed from the highest precedence to the lowest. The ones that are in the same table row have the same precedence. (Line wrapping inside table cells is insignificant; for example, $(C ==) and $(C !is) have the same precedence.) Unless specified otherwise, operators are $(I left-associative).
)
$(P
Some of the terms used in the table are explained later below.
)
<table class="full" border="1" cellpadding="4" cellspacing="0">
<col width="20%"/>
<col width="28%"/>
<col width="52%"/>
<th scope="col">Operators</th>
<th scope="col">Description</th>
<th scope="col">Notes</th>
<tr>
<td> $(C !) </td>
<td class="serif"> Template instantiation</td>
<td class="serif">Cannot be chained
</td></tr>
<tr>
<td> $(C =>) </td>
<td class="serif">Lambda definition</td>
<td class="serif"> Not a real operator; occurs twice in the table; this row is for binding power to the $(I left)
</td></tr>
<tr>
<td> $(C . ++ -- $(PARANTEZ_AC) [) </td>
<td class="serif"> Postfix operators </td>
<td class="serif">$(C $(PARANTEZ_AC)) and $(C [) must be balanced with $(C $(PARANTEZ_KAPA)) and $(C ]), respectively
</td></tr>
<tr>
<td> $(C ^^) </td>
<td class="serif"> Power operator </td>
<td class="serif"> Right-associative
</td></tr>
<tr>
<td> $(C ++ -- * + - ! & ~ cast) </td>
<td class="serif"> Unary operators </td>
<td class="serif">
</td></tr>
<tr>
<td> $(C * / %) </td>
<td class="serif"> Binary operators</td>
<td class="serif">
</td></tr>
<tr>
<td> $(C + - ~) </td>
<td class="serif">Binary operators</td>
<td class="serif"></td>
</tr>
<tr>
<td> $(C << >> >>>) </td>
<td class="serif"> Bit shift operators </td>
<td class="serif">
</td></tr>
<tr>
<td>$(C == != > < >= <= !> !< !>= !<= <> !<> <>= !<>= in !in is !is) </td>
<td class="serif"> Comparison operators </td>
<td class="serif"> Unordered with respect to bitwise operators, cannot be chained
</td></tr>
<tr>
<td> $(C &) </td>
<td class="serif">Bitwise $(I and)</td>
<td class="serif"> Unordered with respect to comparison operators
</td></tr>
<tr>
<td> $(C ^) </td>
<td class="serif"> Bitwise $(I xor)</td>
<td class="serif"> Unordered with respect to comparison operators
</td></tr>
<tr>
<td> $(C |) </td>
<td class="serif"> Bitwise $(I or)</td>
<td class="serif"> Unordered with respect to comparison operators
</td></tr>
<tr>
<td> $(C &&) </td>
<td class="serif">Logical $(I and)</td>
<td class="serif">Short-circuit</td>
</tr>
<tr>
<td>$(C ||)</td>
<td class="serif">Logical $(I or)</td>
<td class="serif">Short-circuit</td>
</tr>
<tr>
<td>$(C ?:)</td>
<td class="serif">Ternary operator</td>
<td class="serif">Right-associative
</td></tr>
<tr>
<td>$(C = -= += = *= %= ^= ^^= ~= <<= >>= >>>=)
</td>
<td class="serif"> Assignment operators </td>
<td class="serif"> Right-associative
</td></tr>
<tr>
<td> $(C =>) </td>
<td class="serif">Lambda definition</td>
<td class="serif"> Not a real operator; occurs twice in the table; this row is for binding power to the $(I right)</td>
</tr>
<tr>
<td> $(C ,) </td>
<td class="serif">Comma operator</td>
<td class="serif">Not to be confused with using comma as a separator (e.g. in parameter lists)</td>
</tr>
<tr>
<td> $(C ..) </td>
<td class="serif">Number range</td>
<td class="serif"> Not a real operator; hardwired into syntax at specific points
</td></tr>
</table>
$(H6 $(IX operator chaining) $(IX chaining, operator) Chaining)
$(P
Let's consider the line from the beginning of the chapter:
)
---
a = b + c * d
---
$(P
Because binary $(C *) has higher precedence than binary $(C +), and binary $(C +) has higher precedence than $(C =), that expression is executed as the following parenthesized equivalent:
)
---
a = (b + (c * d)) // first *, then +, then =
---
$(P
As another example, because postfix $(C .) has higher precedence than unary $(C *), the following expression would first access member $(C ptr) of object $(C o) and then dereference it:
)
---
*o.ptr $(CODE_NOTE dereferences pointer member o.ptr)
*(o.ptr) $(CODE_NOTE equivalent of the above)
(*o).ptr $(CODE_NOTE_WRONG NOT equivalent of the above)
---
$(P
Some operators cannot be chained:
)
---
if (a > b == c) { $(DERLEME_HATASI)
// ...
}
---
$(SHELL
Error: found '==' when expecting '$(PARANTEZ_KAPA)'
)
$(P
The programmer must specify the desired execution order with parentheses:
)
---
if ((a > b) == c) { $(CODE_NOTE compiles)
// ...
}
---
$(H6 $(IX left-associative) $(IX right-associative) $(IX associativity, operator) $(IX operator associativity) Associativity)
$(P
If two operators have the same precedence, then their associativity determines which operator is executed first: the one on the left or the one on the right.
)
$(P
Most operators are $(I left-associative); the one on the left-hand side is executed first:
)
---
10 - 7 - 3;
(10 - 7) - 3; $(CODE_NOTE equivalent of the above (== 0))
10 - (7 - 3); $(CODE_NOTE_WRONG NOT equivalent of the above (== 6))
---
$(P
Some operators are right-associative; the one on the right-hand side is executed first:
)
---
4 ^^ 3 ^^ 2;
4 ^^ (3 ^^ 2); $(CODE_NOTE equivalent of the above (== 262144))
(4 ^^ 3) ^^ 2; $(CODE_NOTE_WRONG NOT equivalent of the above (== 4096))
---
$(H6 Unordered operator groups)
$(P
Precedence between bitwise operators and logical operators are not specified by the language:
)
---
if (a & b == c) { $(DERLEME_HATASI)
// ...
}
---
$(SHELL
Error: b == c must be parenthesized when next to operator &
)
$(P
The programmer must specify the desired execution order with parentheses:
)
---
if ((a & b) == c) { $(CODE_NOTE compiles)
// ...
}
---
$(H6 $(IX =>, operator precedence) The precedence of $(C =>))
$(P
Although $(C =>) is not an operator, it takes part in the table twice to specify how it interacts with operators on its left-hand side and right-hand side.
)
---
l = a => a = 1;
---
$(P
Although both sides of $(C =>) above have an $(C =) operator, $(C =>) has precedence over $(C =) on the left hand side so it $(I binds stronger) to $(C a) as if the programmer wrote the following parentheses:
)
---
l = (a => a = 1);
---
$(P
On the right-hand side, $(C =>) has lower precedence than $(C =), so the $(C a) on the right-hand side binds stronger to $(C =) as if the following extra set of parentheses are specified:
)
---
l = (a => $(HILITE $(PARANTEZ_AC))a = 1$(HILITE $(PARANTEZ_KAPA)));
---
$(P
As a result, the lambda expression does not become just $(C a => a) but includes the rest of the expression as well: $(C a => a = 1), which means $(I given a, produce a = 1). That lambda is then assigned to the variable $(C l).
)
$(P
$(I $(B Note:) This is just an example; otherwise, $(C a = 1) is not a meaningful body for a lambda because the mutation to its parameter $(C a) is seemingly lost and the result of calling the lambda is always 1. (The reason I say "seemingly" is that the assignment operator may have been overloaded for $(C a)'s type and may have side-effects.))
)
$(H6 $(IX , (comma), operator) $(IX comma operator) Comma operator)
$(P
Comma operator is a binary operator. It executes first the left-hand side expression then the right-hand side expression. The value of the left-hand side expression is ignored and the value of the right-hand side expression becomes the value of the comma operator:
)
---
int a = 1;
int b = (foo()$(HILITE ,) bar()$(HILITE ,) ++a + 10);
assert(a == 2);
assert(b == 12);
---
$(P
The two comma operators above chain three expressions. Although the first two expressions ($(C foo()) and $(C bar())) do get executed, their values are ignored and the overall comma expression produces the value of the last expression ($(C ++a + 10)).
)
Macros:
SUBTITLE=Operator Precedence
DESCRIPTION=The precedences and associativity of D operators.
KEYWORDS=d programming lesson book tutorial operator precedence associativity