Operators
Operators are worked out by the five commands that calculate: set, if,
while, until and return. Every operator has
to be a separate word, with spaces around it; see Expressions.
Arithmetic
Section titled “Arithmetic”| Example | Operator | Result |
|---|---|---|
%a + %b |
Addition | |
%a - %b |
Subtraction | |
%a * %b |
Multiplication | |
%a / %b |
Division | Whole numbers only: the remainder is dropped, 7 / 2 is 3. Dividing by 0 gives 0. |
%a % %b |
Remainder | What is left over after dividing: 7 % 2 is 1. By 0 it gives 0. |
abs %a |
Absolute value | The value without its sign: abs -5 is 5. |
Arithmetic works on whole numbers. A value that is not a number (hello, N/A) counts as 0.
Comparison
Section titled “Comparison”Comparisons give -1 (#true) or 0 (#false).
| Example | Operator | True when |
|---|---|---|
%a = %b |
Equal | Both are the same. When either is not a number they are compared as text, ignoring case. |
%a <> %b |
Not equal | They are not the same, by the same rule. |
%a < %b |
Less than | %a is smaller. |
%a > %b |
Greater than | %a is larger. |
%a <= %b |
Less than or equal | Also written =<. |
%a >= %b |
Greater than or equal | Also written =>. |
%a in %b |
Contained in | The text %a appears inside %b, ignoring case. |
%a notin %b |
Not contained in | It does not. |
<, >, <= and >= are always false when either side is not a number: hello > 33 is false, and so is
hello < 33. That makes a quick test for “is this a number?”:
set %cnt helloif ! ( %cnt > 0 || %cnt < 1 ) set %cnt 0 ; %cnt was not a numberdisplay ok %cnthaltLogical
Section titled “Logical”| Example | Operator | True when |
|---|---|---|
%a && %b |
And | Both are true. |
%a || %b |
Or | At least one is true. |
! %a |
Not | %a is false (it turns -1 into 0 and 0 into -1). |
These are meant for true/false values (-1 and 0), such as the results of comparisons. On other numbers
they work bit by bit (! 5 is -6), which is rarely what you want. Both sides of && and || are always
worked out.
Text and lines
Section titled “Text and lines”These are handled while the line is read, before the command runs, so they work with every command, not just the five above.
| Example | Operator | What it does |
|---|---|---|
%a , %b |
Join | Joins the two values into one word: with %a = A and %b = B, %a , %b is AB. |
%a . %b |
Glue a name | Glues the word on the left to the value on the right, then reads the result as a variable. See below. |
+ at the start of a line |
Continue | The line continues the previous one, joined without a space. |
Joining works on any word, even command names: h , a , l , t is halt.
Building variable names with .
Section titled “Building variable names with .”. works from right to left: the right side is replaced by its value first, then glued to the left word, and
the result is read as a variable. That builds variable names from other values, a little like arrays:
set %a 3set %item3 swordset %x %item . %a ; %item . 3 -> %item3 -> swordset %y %item , %a ; "%item" has no value, so N/A , 3 -> N/A3display ok %x %yhaltset %slot1 swordset %slot2 shieldset %slot3 helmetfor %i 1 3{ set %name %slot . %i ; %slot1, %slot2, %slot3 display ok %name}haltA . or , only works as a separate word. To put a full stop or a comma into text, use #DOT
or glue the pieces with ,.
Order of operations
Section titled “Order of operations”Within each pair of parentheses, starting with the innermost, operators are worked out in this order. Operators on the same row are worked out from left to right.
| Order | Operators |
|---|---|
| 1 | ! and abs, from right to left |
| 2 | * / % |
| 3 | + - |
| 4 | = <> < > <= >= in notin |
| 5 | && |
| 6 | || |
Before any of this, while the line is read, . (right to left) and then , (left to right) have already been
applied, and variables have been replaced by their values.
set %r 5 + 3 * 2 ; 5 + ( 3 * 2 ) = 11set %r 8 - 3 - 2 ; ( 8 - 3 ) - 2 = 3set %r ( 1 + 2 ) * 2 * 4 ; 24set %a 1set %b 2if %a = 1 && %b = 2 ; ( %a = 1 ) && ( %b = 2 ) display ok Both are true.if ! ( %a = 1 ) ; parentheses needed: ! %a = 1 means ( ! %a ) = 1 display ok Not shown: %a is 1.haltBased on the EasyUO wiki, edited for EasyUO Reforged. This text is available under the GNU Free Documentation License 1.2.