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