blob: bd092dc8cf4513e44e7975cd1a7200d4ca27fded [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001NAME
2 operators
3
4DESCRIPTION
5
6 These are the operators availailable in LPC. They are listed
7 in the order of precedence (low priority first):
8
9
10 expr1 , expr2 Evaluate 'expr1' and then 'expr2'. The
11 returned value is the result of 'expr2'. The
12 returned value of 'expr1' is thrown away.
13
14 var = expr Evaluate 'expr', and assign the value to
15 'var'. The new value of 'var' is the result.
16
17 var += expr Assign the value of 'expr' + 'var' to 'var'.
18 This is mostly equivalent to "var = var + expr".
19
20 var -= expr Similar to '+=' above.
21 var &= expr
22 var |= expr
23 var ^= expr
24 var <<= expr
25 var >>= expr
26 var >>>= expr
27 var *= expr
28 var %= expr
29 var /= expr
30 var &&= expr
31 var ||= expr
32
33 expr1 ? expr2 : expr3
34 Evaluates 'expr1' and branches according to
35 its truth value. If it is true, the 'expr2' is
36 evaluated and returned as result, else
37 'expr3'.
38
39 expr1 || expr2 The result is true if 'expr1' or 'expr2' is
40 true. 'expr2' is not evaluated if 'expr1' was
41 true.
42
43 expr1 && expr2 The result is true i 'expr1' and 'expr2' is
44 true. 'expr2' is not evaluated if 'expr1' was
45 false.
46
47 expr1 | expr2 The result is the bitwise or of 'expr1' and
48 'expr2'.
49 For arrays, the union set is computed: all elements
50 from <expr1> plus all those from <expr2> which
51 are not in <expr1>.
52
53 expr1 ^ expr2 The result is the bitwise xor of 'expr1' and
54 'expr2'.
55 For arrays, the symmetric difference is computed:
56 all elements from <expr1> which are not in <expr2>,
57 plus all those from <expr2> which are not in <expr1>.
58
59 expr1 & expr2 The result is the bitwise and of 'expr1' and
60 'expr2'.
61
62 For arrays and strings, the intersection set
63 (all elements resp. characters from expr1 which
64 which are also in the expr2) is computed.
65 Note: "aab" & "a" -> "aa"
66 but ({ 'a','a','b' }) & ({ 'a' }) -> ({ 'a' })
67 Eventually the array behaviour will be changed
68 to match the string behaviour.
69
70 Intersecting an array with a mapping is equivalent
71 to intersecting the array with the indices of the
72 mapping: array & mapping = array & m_indices(mapping)
73
74 Mappings can be intersected with another mapping
75 or an array. The resulting mapping holds all
76 those entries from the first mapping, which are
77 also mentioned in the second mapping (as index)
78 resp. in the array.
79
80 expr1 == expr2 Compare values. Valid for strings, numbers,
81 objects and closures.
82
Zesstra98e90af2021-05-07 19:18:04 +020083 expr1 != expr2 Compare values. Valid for strings, numbers,
MG Mud User88f12472016-06-24 23:31:02 +020084 objects and closures.
85
Zesstra98e90af2021-05-07 19:18:04 +020086 expr1 in expr2 Check whether 'expr1' is contained in 'expr2':
87 a value in an array, a key in a mapping,
88 a character or substring in a string, or a
89 a byte or byte sequence in a byte sequence.
90
91 expr1 > expr2 Valid for strings and numbers.
MG Mud User88f12472016-06-24 23:31:02 +020092
93 expr1 >= expr2 Valid for strings and numbers.
94
Zesstra98e90af2021-05-07 19:18:04 +020095 expr1 < expr2 Valid for strings and numbers.
MG Mud User88f12472016-06-24 23:31:02 +020096
97 expr1 <= expr2 Valid for strings and numbers.
98
99 expr1 << expr2 Shift 'expr1' left by 'expr2' bits; the sign
100 bit is not preserved.
101
102 expr1 >> expr2 Shift 'expr1' right by 'expr2' bits.
103 This shift preserves the sign of 'expr1'.
104
105 expr1 >>> expr2 Shift 'expr1' right by 'expr2' bits.
106 This shift does not preserve the sign of 'expr1',
107 instead it shifts in 0 bits.
108
109 expr1 + expr2 Add 'expr1' and 'expr2'. If numbers, then
110 arithmetic addition is used. If one of the
111 expressions are a string, then that string is
112 concatenated with the other value.
113 If the expressions are arrays, the result is
114 the right array appended to the left.
115 If the expressions are mappings of equal width,
116 the result is merger of the two mappings. If one
117 key exists in both mappings, the element from the
118 right mapping appears in the result. If the two
119 mappings are of different width, the result is
120 <expr1> if non-empty, and <expr2> otherwise.
121
122 expr1 - expr2 Subtract 'expr2' from 'expr1'. Valid for
123 numbers, strings, arrays, mappings.
Zesstra7ea4a032019-11-26 20:11:40 +0100124 For arrays and strings, all occurrences of the
MG Mud User88f12472016-06-24 23:31:02 +0200125 elements resp. characters in 'expr2' are removed
126 from 'expr1', and the result is returned.
127 For mapping, all occurances of elemens in 'expr1'
128 which have a matching key in 'expr2' are removed, and
129 the result is returned.
130
131 expr1 * expr2 Multiply 'expr1' with 'expr2'.
132 If strings or arrays are multiplied with a number
133 (zero or positive), the result is a repetition of the
134 original string or array.
135
136 expr1 % expr2 The modulo operator of numeric arguments.
137
138 expr1 / expr2 Integer division.
139
140 ++ var Increment the value of variable 'var', and
141 return the new value.
142
143 -- var Decrement the value of variable 'var', and
144 return the new value.
145
146 - var Compute the negative value of 'var'.
147
148 ! var Compute the logical 'not' of an integer.
149
150 ~ var The boolean 'not' of an integer.
151
152 ( type ) var Return the value of <var> converted to <type>.
153 <type> can be 'string', 'int', 'object', 'float'
154 or 'int*'. <var> must be of a specific type
155 for a conversion to take place; if <var> is 'mixed'
156 or unknown, the cast is purely declarative.
157 Also, if the declared type of <var> is that of <type>,
158 the value is not changed.
159
160 NB. The literal number 0 is of unknown type, as
161 it doubles as 'not initialized' for strings, objects,
162 and arrays.
163
164 The operator acts like the efuns
165 to_string(), to_int(), to_object(), to_float()
166 and to_array(). It is advisable to use the
167 efuns directly instead of the cast.
168
169 ({ type }) var <var> is now assumed to have the type <type>.
170 This is purely declarative, the actual value
171 of <var> is not changed.
172
173 var ++ Increment the value of variable 'var', and
174 return the old value.
175
176 var -- Decrement the value of variable 'var', and
177 return the old value.
178
179 expr1[expr2] The array or mapping given by 'expr1' is
180 indexed by 'expr2'.
181
182 expr1[expr2..expr3] Extracts a
183 piece from an array or string.
184 expr2 or expr3 may be omitted, default is the begin
185 or end of expr1.
186 Negative numbers for expr2 or expr3
187 mean ``count from before the beginning'', i.e.
188 foo[-2..-1] is an empty array or string.
189 foo[<2..<1] gives the 2nd and last element of
190 the array resp. chars of the string.
191
192 expr1->name(...) The symbolic form of call_other(). 'expr1'
193 gives either an object or a string which is
194 used as the file_name of an object, and calls
195 the function 'name' in this object.
196
Zesstracbd5f9b2020-08-11 21:08:39 +0200197 expr1.name(...) The operator form of call_strict(). 'expr1'
198 gives either an object or a string which is
199 used as the file name of an object, and calls
200 the function 'name' in this object. Throws
201 an error, if the function does not exist.
202
MG Mud User88f12472016-06-24 23:31:02 +0200203 ident::name(...)
204 Call the inherited function 'name' with the
205 given parameters in the parent 'ident'.
206 'ident' may be given as string containing the
207 full pathname, or as identifier containing the
208 pure basename.
Zesstra98e90af2021-05-07 19:18:04 +0200209 If 'ident' is omitted, the first inherited
MG Mud User88f12472016-06-24 23:31:02 +0200210 function of this 'name' is called.
211
212 ({ }) Array constructor.
213 ([ ]) Mapping constructor.
214
215NOTE
216 The closure operators are not described here.
217
218HISTORY
219 LDMud 3.2.9 added '>>>', '>>>=', '&&=' and '||='.
220 LDMud 3.2.10 extended '&' to mappings.
221 LDMud 3.3 extended '|' and '^' to arrays.
Zesstracbd5f9b2020-08-11 21:08:39 +0200222 LDMud 3.6.2 added '.'.
Zesstra98e90af2021-05-07 19:18:04 +0200223 LDMud 3.6.5 added 'in'.
MG Mud User88f12472016-06-24 23:31:02 +0200224
225SEE ALSO
226 arrays(LPC), alists(LPC), mappings(LPC), closures(LPC)