MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | NAME |
| 2 | operators |
| 3 | |
| 4 | DESCRIPTION |
| 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 | |
| 83 | expr1 != expr1 Compare values. Valid for strings, numbers, |
| 84 | objects and closures. |
| 85 | |
| 86 | expr1 > expr2 Valid for strings and numbers. |
| 87 | |
| 88 | expr1 >= expr2 Valid for strings and numbers. |
| 89 | |
| 90 | expr1 < expr2 Valid for strings and numbers. |
| 91 | |
| 92 | expr1 <= expr2 Valid for strings and numbers. |
| 93 | |
| 94 | expr1 << expr2 Shift 'expr1' left by 'expr2' bits; the sign |
| 95 | bit is not preserved. |
| 96 | |
| 97 | expr1 >> expr2 Shift 'expr1' right by 'expr2' bits. |
| 98 | This shift preserves the sign of 'expr1'. |
| 99 | |
| 100 | expr1 >>> expr2 Shift 'expr1' right by 'expr2' bits. |
| 101 | This shift does not preserve the sign of 'expr1', |
| 102 | instead it shifts in 0 bits. |
| 103 | |
| 104 | expr1 + expr2 Add 'expr1' and 'expr2'. If numbers, then |
| 105 | arithmetic addition is used. If one of the |
| 106 | expressions are a string, then that string is |
| 107 | concatenated with the other value. |
| 108 | If the expressions are arrays, the result is |
| 109 | the right array appended to the left. |
| 110 | If the expressions are mappings of equal width, |
| 111 | the result is merger of the two mappings. If one |
| 112 | key exists in both mappings, the element from the |
| 113 | right mapping appears in the result. If the two |
| 114 | mappings are of different width, the result is |
| 115 | <expr1> if non-empty, and <expr2> otherwise. |
| 116 | |
| 117 | expr1 - expr2 Subtract 'expr2' from 'expr1'. Valid for |
| 118 | numbers, strings, arrays, mappings. |
| 119 | For arrays and strings, all occurences of the |
| 120 | elements resp. characters in 'expr2' are removed |
| 121 | from 'expr1', and the result is returned. |
| 122 | For mapping, all occurances of elemens in 'expr1' |
| 123 | which have a matching key in 'expr2' are removed, and |
| 124 | the result is returned. |
| 125 | |
| 126 | expr1 * expr2 Multiply 'expr1' with 'expr2'. |
| 127 | If strings or arrays are multiplied with a number |
| 128 | (zero or positive), the result is a repetition of the |
| 129 | original string or array. |
| 130 | |
| 131 | expr1 % expr2 The modulo operator of numeric arguments. |
| 132 | |
| 133 | expr1 / expr2 Integer division. |
| 134 | |
| 135 | ++ var Increment the value of variable 'var', and |
| 136 | return the new value. |
| 137 | |
| 138 | -- var Decrement the value of variable 'var', and |
| 139 | return the new value. |
| 140 | |
| 141 | - var Compute the negative value of 'var'. |
| 142 | |
| 143 | ! var Compute the logical 'not' of an integer. |
| 144 | |
| 145 | ~ var The boolean 'not' of an integer. |
| 146 | |
| 147 | ( type ) var Return the value of <var> converted to <type>. |
| 148 | <type> can be 'string', 'int', 'object', 'float' |
| 149 | or 'int*'. <var> must be of a specific type |
| 150 | for a conversion to take place; if <var> is 'mixed' |
| 151 | or unknown, the cast is purely declarative. |
| 152 | Also, if the declared type of <var> is that of <type>, |
| 153 | the value is not changed. |
| 154 | |
| 155 | NB. The literal number 0 is of unknown type, as |
| 156 | it doubles as 'not initialized' for strings, objects, |
| 157 | and arrays. |
| 158 | |
| 159 | The operator acts like the efuns |
| 160 | to_string(), to_int(), to_object(), to_float() |
| 161 | and to_array(). It is advisable to use the |
| 162 | efuns directly instead of the cast. |
| 163 | |
| 164 | ({ type }) var <var> is now assumed to have the type <type>. |
| 165 | This is purely declarative, the actual value |
| 166 | of <var> is not changed. |
| 167 | |
| 168 | var ++ Increment the value of variable 'var', and |
| 169 | return the old value. |
| 170 | |
| 171 | var -- Decrement the value of variable 'var', and |
| 172 | return the old value. |
| 173 | |
| 174 | expr1[expr2] The array or mapping given by 'expr1' is |
| 175 | indexed by 'expr2'. |
| 176 | |
| 177 | expr1[expr2..expr3] Extracts a |
| 178 | piece from an array or string. |
| 179 | expr2 or expr3 may be omitted, default is the begin |
| 180 | or end of expr1. |
| 181 | Negative numbers for expr2 or expr3 |
| 182 | mean ``count from before the beginning'', i.e. |
| 183 | foo[-2..-1] is an empty array or string. |
| 184 | foo[<2..<1] gives the 2nd and last element of |
| 185 | the array resp. chars of the string. |
| 186 | |
| 187 | expr1->name(...) The symbolic form of call_other(). 'expr1' |
| 188 | gives either an object or a string which is |
| 189 | used as the file_name of an object, and calls |
| 190 | the function 'name' in this object. |
| 191 | |
| 192 | ident::name(...) |
| 193 | Call the inherited function 'name' with the |
| 194 | given parameters in the parent 'ident'. |
| 195 | 'ident' may be given as string containing the |
| 196 | full pathname, or as identifier containing the |
| 197 | pure basename. |
| 198 | If 'ident' is omitted, the last inherited |
| 199 | function of this 'name' is called. |
| 200 | |
| 201 | ({ }) Array constructor. |
| 202 | ([ ]) Mapping constructor. |
| 203 | |
| 204 | NOTE |
| 205 | The closure operators are not described here. |
| 206 | |
| 207 | HISTORY |
| 208 | LDMud 3.2.9 added '>>>', '>>>=', '&&=' and '||='. |
| 209 | LDMud 3.2.10 extended '&' to mappings. |
| 210 | LDMud 3.3 extended '|' and '^' to arrays. |
| 211 | |
| 212 | SEE ALSO |
| 213 | arrays(LPC), alists(LPC), mappings(LPC), closures(LPC) |