Manual:DIL Manual/Operators
The operators of DIL, how tightly each binds, and what each does with each type. Expressions explains failed values and evaluation; Assignment covers :=, which is a statement, not an operator.
Precedence
The grammar of vmc (vme/src/vmc/dilpar.y) has four levels. Operators on a higher line bind more tightly; on one line they are worked out from left to right.
| Level | Operators | Notes |
|---|---|---|
| 1 (tightest) | not |
applies to the single value right after it: a constant, variable, field, call or bracket |
| 2 | * / % and & in |
|
| 3 | , and a minus or plus sign in front of a value | |
| 4 (loosest) | == != < > <= >= $= #= |
$= and #= can't be chained
|
Brackets ( ) override it all. Some consequences, all checked with vmc and in the game:
| Written | Read as | Value |
|---|---|---|
a == 1 and b == 2 |
(a == (1 and b)) == 2 |
false, even with a = 1 and b = 2 |
a == 1 or a == 2 |
(a == (1 or a)) == 2 |
false with a = 2 |
(a == 1) and (b == 2) or (c == 3) |
((a == 1) and (b == 2)) or (c == 3) |
as expected |
| 4 and 1 | (4 and 1) | 3 |
not a == 5 |
(not a) == 5 |
false with a = 0 |
not "x" in sl |
(not "x") in sl |
vmc: "Arg 1 of 'in' not string" |
"a" + "b" in s |
"a" + ("b" in s) |
vmc: "Arg 2 of '+' not string" |
1 < 2 < 3, 3 > 2 > 1 |
(1 < 2) < 3, (3 > 2) > 1 |
true, false |
-a * b |
-(a * b) |
the same number |
So: put every comparison in brackets when it is joined with and or or, and write not (...) with brackets.
A minus sign can't come right after another operator: a + -1, a * -1 and - -a are syntax errors. Write a - 1 or a * (-1). After a comparison or a comma it is fine: if (a == -1), rnd(-5, 5).
Arithmetic
| Operator | Types | Result |
|---|---|---|
+ |
integer + integer | the sum |
+ |
string + string | the two joined: "You have " + itoa(n) + " coins."
|
- * |
integers | difference, product |
/ |
integers | the quotient, cut towards 0: -7 / 2 is -3
|
% |
integers | the remainder, with the sign of the left side: -7 % 3 is -1, 7 % -3 is 1
|
-a, +a |
integer | minus a, a |
- Integers are 32 bits and wrap round without a warning:
2147483647 + 1is-2147483648. - Dividing by 0 with
/or%fails (see Expressions): an integer assigned from it keeps its old value. A division of two constants by 0, such as5 / 0, crashes vmc instead. - There are no numbers with a fraction. For a percentage multiply first:
(part * 100) / whole. +doesn't mix strings and integers:"a" + 5doesn't compile; use itoa(). A join with a failed part fails, but with the string on the left ("Hi " + u.namewhenuis null) it stops the program when assigned or passed to a procedure.- rnd() gives random numbers.
Comparison
| Operator | Types | Meaning |
|---|---|---|
== != |
integers | equal, not equal |
== != |
strings | equal, ignoring case: "Sword" == "sword" is true. An unset string and "" are equal.
|
== != |
unitptr, extraptr, zoneptr, cmdptr | the same unit, extra, zone or command; or both null |
== != |
stringlist, intlist | the same list, not the same contents: two list variables are never equal. sl == null is true when the list is empty.
|
< > <= >= |
integers | less, greater... |
< > <= >= |
strings | alphabetical order, ignoring case: "B" > "a" is true
|
$= |
strings | old form of == for strings; still used in some zones
|
#= |
strings, units, extras, lists | the same object in memory: two strings with the same text are not #=. Obsolete.
|
- Both sides must be of the same type, or one of them
null:if (n == "5")andif (u == exd)don't compile. Units can't be ordered:u < vdoesn't compile. - The result is 1 or 0. With a failed side it is false, for
==and!=alike (with one exception, a bug, on the Expressions page). - For an exact, case-sensitive comparison of strings use strcmp() or strncmp().
Logic
| Operator | Types | Result |
|---|---|---|
and |
any | 1 if both sides are true, else 0 |
or |
any | 1 if either side is true, else 0 |
not |
any | 1 if the value is false, null, empty or failed, else 0 |
- Any type counts as true or false, as in an
if: see Expressions. - Both sides are always worked out, left first.
(u != null) and (u.level > 5)still readsu.level, and a function call on the right is made even when the left side already decides the answer. - A failed side counts as false;
notof a failed value is 1.
Bits
| Operator | Types | Result |
|---|---|---|
& |
integers | the bits set in both: 6 & 3 is 2
|
| integers | SFB_DONE |
- vmc accepts other types here too, but the value then fails when the program runs.
- For flag fields isset(), set() and unset() are clearer:
if (isset(u.flags, UNIT_FL_TRANS)). |binds like+and&like*:1 | 2 * 2is 5.
in
s in x looks for the string s; the left side must be a string. See in.
| Right side | Result |
|---|---|
| string | 1 if s occurs anywhere in it, ignoring case ("key" in "a KEY ring"), else 0. See string in string.
|
| stringlist | the position plus one of the first entry that is s (or the first words of s), ignoring case; 0 if none. See string in stringlist.
|
| extraptr | the first extra with s as one of its names, or null. See string in extraptr.
|
There is no in for an intlist. in binds like *, more tightly than + and the comparisons.
Example
dilbegin integer good_buyer(pc : unitptr, price : integer);
var
pct : integer;
code
{
if (pc == null)
return (FALSE);
pct := (pc.hp * 100) / pc.max_hp;
if ((pc.type == UNIT_ST_PC) and (pct > 50) and
((pc.level >= 10) or (price < 100)))
return (TRUE);
if (not ("merchant" in pc.names))
return (FALSE);
return ((price % 2) == 0);
}
dilend
Brackets around each comparison and around the in after not; a percentage worked out with the multiplication first. If max_hp were 0 the division would fail, pct would keep its old value (0), and the test would just be false.