Sign in Join
Wiki copytemporary

Operator: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11An **operator** is a special kind of [[Function|function]]. It can be invoked by placing keywords adjacent to suitable operands.
22
33The word *operator* colloquially refers to the symbol or [[Keyword|keyword]] identifying the function that implements the actual operation. `operator` is also a [[Reserved word|reserved word]] that appears in the course of [[Operator overloading|operator overloading]].
44
55## usage
66
77Most [[Operators|operators]] are binary, that means require two operands. Binary operator symbols appear between two operands, like this:
88
99``` pascal
1010x + 5
1111```
1212
1313The plus character identifies the addition operation (Standard Pascal). `x` and `5` are its operands. This style is called infix notation.
1414
1515Unary operators – those which require only one operand – appear in front of their operands (prefix notation), with the exception of `^`, the de-referencing operator, which appears after its operand (postfix notation). Example:
1616
1717``` pascal
1818-x
1919```
2020
2121Here, the minus character identifies the sign inversion operation (Standard Pascal). Blanks between operator and operand do not harm, confer the following section regarding precedence. However, it is common practice to place unary operator symbols back to back with their operands.
2222
2323Operator symbols always appear explicitly, except the implicit typecast and enumerator operator. Unlike in mathematics, no invisible “times” is assumed between two identifiers.
2424
2525Implicit typecasts occur, where a value has to be stored in a memory location. Note, that *calling* a routine triggers implicit typecasts, too. In order to pass the parameters to the routine, they are stored somewhere. Implicit typecasts are not limited to run-of-the-mill assignment statements.
2626
2727## operator precedence
2828
2929Operands and operators are the building blocks of [[expression|expressions]]. The use of operators is a powerful notational tool, since [[LOC|LOCs]] do not get cluttered by function calls consisting of function identifiers and parentheses, but instead the operands and (ideally) a short symbol achieve the same. Instead of, for example, `sum(x, -8)` the expression `x - 8` evaluates to the same value, while writing five less characters.
3030
3131In order to increase conciseness subexpressions of expressions propagate their intermediate result in a predefined hierarchy. This hierarchy can be superseded by placing parentheses `( )` around subexpressions that shall be treated as *one* expression, so the regular precedence rules apply in the enclosed part remaining uninfluenced from the expression’s rest. Operators that have have higher precedence, bind to operands stronger.
3232
33-<table>
34-<caption>operator precedence</caption>
35-<thead>
36-<tr>
37-<th><p>precedence</p></th>
38-<th><p>operators</p></th>
39-<th><p>category</p></th>
40-</tr>
41-</thead>
42-<tbody>
43-<tr>
44-<td><p>highest<br />
45-(first)</p></td>
46-<td><ul>
47-<li>[[Not|not]] (both, logical and bitwise variant)</li>
48-<li>unary [[Plus|+]] (sign identity, “positive sign”)</li>
49-<li>unary [[Minus|-]] (sign inversion, “negative sign”)</li>
50-<li><code>**</code></li>
51-<li><code>pow</code></li>
52-<li>[[@]]<sup>†</sup></li>
53-<li>[[^]]<sup>†</sup> (de-referencing operator)</li>
54-<li><code>explicit</code> typecasts</li>
55-</ul></td>
56-<td><ul>
57-<li>unary operators (except<br />
58-destination-dependent operators)</li>
59-<li>power</li>
60-</ul></td>
61-</tr>
62-<tr>
63-<td><p>second</p></td>
64-<td><ul>
65-<li>[[*]]</li>
66-<li>[[And|and]] (both, logical and bitwise variant)</li>
67-<li><code>and_then</code></li>
68-<li>[[Slash|/]]</li>
69-<li>[[Div|div]]</li>
70-<li>[[Mod|mod]]</li>
71-<li>[[shl]] (<code>&lt;&lt;</code>)</li>
72-<li><code>shr</code> (<code>&gt;&gt;</code>)</li>
73-<li><code>as</code></li>
74-<li><del><code>is</code> (until FPC 3.3.1, trunk revision 44266)</del></li>
75-</ul></td>
76-<td><ul>
77-<li>multiplication operators</li>
78-<li>conditional typecast</li>
79-</ul></td>
80-</tr>
81-<tr>
82-<td><p>third</p></td>
83-<td><ul>
84-<li>[[Plus|+]]</li>
85-<li>[[Minus|-]]</li>
86-<li>[[Or|or]] (both, logical and bitwise variant)</li>
87-<li><code>or_else</code></li>
88-<li>[[Xor|xor]] (both, logical and bitwise variant)</li>
89-</ul></td>
90-<td><ul>
91-<li>addition operators</li>
92-<li>complex logical operators</li>
93-</ul></td>
94-</tr>
95-<tr>
96-<td><p>fourth</p></td>
97-<td><ul>
98-<li>[[Less than|&lt;]]</li>
99-<li>[[Greater than|&gt;]]</li>
100-<li>[[Equal|=]]</li>
101-<li>[[Not equal|&lt;&gt;]]</li>
102-<li><code>&lt;=</code> (both, ⊆ as well as ≤)</li>
103-<li><code>&gt;=</code></li>
104-<li><code>in</code></li>
105-<li>[[symmetric difference|&gt;&lt;]]</li>
106-<li>[[Is|is]] (since FPC 3.3.1, cf. <a href="https://bugs.freepascal.org/view.php?id=35909">Issue #35909</a>)</li>
107-</ul></td>
108-<td><ul>
109-<li>relational operators</li>
110-<li>inheritance test</li>
111-</ul></td>
112-</tr>
113-<tr>
114-<td><p>lowest<br />
115-(last)</p></td>
116-<td><ul>
117-<li>[[Becomes|:=]] (implicit [[Typecast|typecasts]])</li>
118-<li><code>enumerator</code> (if applicable)</li>
119-</ul></td>
120-<td><ul>
121-<li>destination-dependent conversions</li>
122-</ul></td>
123-</tr>
124-</tbody>
125-</table>
33+operator precedence
34+
35+**precedence**
36+
37+**operators**
38+
39+**category**
40+
41+highest\
42+(first)
43+
44+- [[Not|not]] (both, logical and bitwise variant)
45+- unary [[Plus|+]] (sign identity, “positive sign”)
46+- unary [[Minus|-]] (sign inversion, “negative sign”)
47+- `**`
48+- `pow`
49+- [[@]]<sup>†</sup>
50+- [[^]]<sup>†</sup> (de-referencing operator)
51+- `explicit` typecasts
52+
53+- unary operators (except\
54+ destination-dependent operators)
55+- power
56+
57+second
58+
59+- [[*]]
60+- [[And|and]] (both, logical and bitwise variant)
61+- `and_then`
62+- [[Slash|/]]
63+- [[Div|div]]
64+- [[Mod|mod]]
65+- [[shl]] (`<<`)
66+- `shr` (`>>`)
67+- `as`
68+- ~~`is` (until FPC 3.3.1, trunk revision 44266)~~
69+
70+- multiplication operators
71+- conditional typecast
72+
73+third
74+
75+- [[Plus|+]]
76+- [[Minus|-]]
77+- [[Or|or]] (both, logical and bitwise variant)
78+- `or_else`
79+- [[Xor|xor]] (both, logical and bitwise variant)
80+
81+- addition operators
82+- complex logical operators
83+
84+fourth
85+
86+- [[Less than|&lt;]]
87+- [[Greater than|&gt;]]
88+- [[Equal|=]]
89+- [[Not equal|&lt;&gt;]]
90+- `<=` (both, ⊆ as well as ≤)
91+- `>=`
92+- `in`
93+- [[symmetric difference|&gt;&lt;]]
94+- [[Is|is]] (since FPC 3.3.1, cf. [Issue \#35909](https://bugs.freepascal.org/view.php?id=35909))
95+
96+- relational operators
97+- inheritance test
98+
99+lowest\
100+(last)
101+
102+- [[Becomes|:=]] (implicit [[Typecast|typecasts]])
103+- `enumerator` (if applicable)
104+
105+- destination-dependent conversions
126106
127107<sup>†</sup>) This symbol does not refer to an actual operator, but is (imprecisely) called as one.
128108
129109[[Inc and Dec|Inc and dec]] are pseudo operators: They may be redefined via the operator overloading mechanism, but they can not appear in expressions, like any function could. Therefore their precedence is moot.
130110
131111`Include` and `exclude` are also not operators, but shorthand for common LOCs.
132112
133113`@` and `^` albeit being called operators, are *not* operators, but rather instruct the [[Compiler|compiler]] to interpret an [[Identifier|identifier]] differently than usual. This is not done via any function, but compiler intrinsics.
134114
135115> **Note:** Operator precedence is slightly modified by `{$modeSwitch ISOUnaryMinus}`, which is enabled by default in [[Mode iso|{$mode ISO}]] and [[Mode extendedpascal|{$mode extendedPascal}]]. There, the unary minus is on the same level as other addition operators are.
136116
137117### evaluation order
138118
139119> **Note:** Operator precedence does not infer evaluation order. The compiler may re-arrange expressions according to associative and commutative properties of operators.
140120
141121No assumptions shall be made, and there is no guarantee, that expressions are evaluated from left to right (or the reverse direction). The [[FPC]] for instance, evaluates more “complex” subexpressions first before moving on to “trivial” parts (ratio: avoiding register spilling).
142122
143123If a subexpression *has* to be evaluated first, e.g. a function triggering some side-effects, the expression has to be split up into two separate statements. For example:
144124
145125``` pascal
146126x := foo() + bar();
147127```
148128
149129The compiler may evaluate `foo()` or `bar()` first. If `bar()` ought to be evaluated first at all events, the statement has to be split into two separate ones:
150130
151131``` pascal
152132x := bar();
153133x := x + foo();
154134```
155135
156136> **Note:** The [[Compiler directive|compiler directive]] [[$boolEval|{$boolEval off}]] enables “lazy” evaluation. Parts of expressions may not be evaluated at all.
157137
158138## see also
159139
160140- [“Operators”](https://www.freepascal.org/docs-html/ref/refse88.html) in the FreePascal Reference Guide
161141- Tutorial: [[Basic Pascal Tutorial/Chapter 1/Assignment and Operations|assignment and operations]]
162142- [[management operators]] – a collection of specially supported routines
163143
164144---
165145*From the Free Pascal wiki, page "Operator", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
166146Category: Code

Back to history