Difference between revisions of "Manual:DIL Manual/Expressions"
(XML import) |
(rewritten against the compiler and interpreter; examples compile) |
||
| Line 1: | Line 1: | ||
| − | < | + | An expression works out a value: the right side of an [[Manual:DIL Manual/Assignment|assignment]], the condition of an <code>if</code>, <code>while</code> or <code>wait()</code>, the arguments of a call. Every expression has one of the eight [[Manual:DIL Manual/Data Types|types]], known when vmc compiles it, and vmc refuses one of the wrong type ("Arg 1 of '+' not integer or string", "Assigning incompatible types"). |
| − | + | == What an expression is made of == | |
| − | + | * '''Constants.''' Integers in decimal (<code>42</code>) or hexadecimal (<code>0x2A</code>); a minus sign in front makes them negative. Strings between double quotes, without <code>\"</code> or <code>^</code> inside. Lists in braces, of constants only: <code>{"a", "b"}</code>, <code>{1, 2}</code>. [[Manual:DIL Manual/null|null]]. Macros from the include files, such as <code>TRUE</code>, <code>PULSE_SEC</code>, <code>UNIT_ST_PC</code> or <code>SKI_SEARCH</code>, are replaced by their values before compiling. | |
| − | + | * '''Variables''': the template's own, its arguments, and the [[Manual:DIL Manual/Built-In Variables|built-in variables]] (<code>self</code>, <code>activator</code>, <code>argument</code>...). | |
| − | + | * '''Fields''', with a dot: <code>self.outside.title</code>. Indexed fields take brackets: <code>pc.skills[SKI_SEARCH]</code>, <code>room.exit_to[NORTH]</code>. A list entry or a character of a string takes <code>.[i]</code>: <code>sl.[0]</code>, <code>s.[0]</code>; an extra by name, <code>u.extra.["$data"]</code>. See [[Manual:DIL Manual/Fields|Fields]]. | |
| − | + | * '''Calls''': [[Manual:DIL Manual/Built-In Functions|built-in functions]] (<code>itoa(n)</code>, <code>findroom(s)</code>) and the zone's own functions (<code>max@function(a, b)</code>; see [[Manual:DIL Manual/Functions|Functions]]). | |
| − | + | * '''Operators''' joining these: <code>+ - * / %</code>, <code>== != < > <= >=</code>, <code>and or not</code>, <code>& |</code>, <code>in</code>. See [[Manual:DIL Manual/Operators|Operators]]. | |
| + | * '''Brackets''' to group: <code>(a + b) * 2</code>. | ||
| − | + | A field or a call can't follow a call: <code>findroom(s).title</code> is a syntax error, and so is a DIL function's result passed straight to another DIL function (<code>f(g(x))</code>). Put the first result in a variable. | |
| − | < | + | == Precedence == |
| + | DIL's operators have four levels, from binding most tightly to binding least: | ||
| + | # <code>not</code>, which applies only to the value right after it; | ||
| + | # <code>* / % and & in</code>; | ||
| + | # <code>+ - or |</code>, and a minus sign in front of a value; | ||
| + | # the comparisons <code>== != < > <= >=</code>. | ||
| − | + | This is not the order of C or most other languages: '''<code>and</code> and <code>or</code> bind more tightly than the comparisons'''. vmc accepts | |
| − | + | if (a == 1 and b == 2) | |
| − | + | but reads it as <code>(a == (1 and b)) == 2</code>, which is false even when <code>a</code> is 1 and <code>b</code> is 2. In the same way <code>a == 1 or a == 2</code> is false when <code>a</code> is 2. '''Put every comparison in brackets''': | |
| − | + | if ((a == 1) and (b == 2)) | |
| − | + | The [[Manual:DIL Manual/Operators|Operators]] page has the whole table and more such cases. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Truth == | |
| − | < | + | A condition can be of any type. An integer is true when it isn't 0; a string, unit, extra, zone or command when it isn't null; a list when it isn't empty; a failed value is false. Comparisons, <code>and</code>, <code>or</code> and <code>not</code> give 1 (<code>TRUE</code>) or 0 (<code>FALSE</code>). |
| − | + | A string variable that has been given <code>""</code> is not null, so it counts as true: test text with <code>s != ""</code>. The fields <code>title</code>, <code>descr</code>, <code>outside_descr</code> and <code>inside_descr</code> tested on their own (<code>if (u.title)</code>) currently count the wrong way round, true when they are empty; compare them with <code>""</code> as well. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Failed values == | |
| + | An expression that can't be worked out '''fails''': it has no value, and no error is written. That happens with | ||
| + | * a field of null: <code>u.level</code> when <code>u</code> is null; | ||
| + | * a field the unit doesn't have: <code>u.level</code> when <code>u</code> is a room; | ||
| + | * a division by 0: <code>a / 0</code>, <code>a % 0</code>; | ||
| + | * an index outside a list or string: <code>sl.[9]</code> in a list of three (this one writes "index of stringlist out of bounds" to the zone's log); | ||
| + | * most built-in functions given a failed argument. | ||
| − | + | A failure spreads to the expression around it: | |
| + | * arithmetic and joins with a failed part fail: <code>u.level + 1</code>, <code>u.name + "!"</code>; | ||
| + | * every comparison with a failed side is false. <code>u.level > 5</code> and <code>u.level <= 5</code> are both false, and so are <code>u.outside == null</code> and <code>u.outside != null</code> when <code>u</code> is null. (One exception, a bug: when the right side of a <code>==</code> between units fails, the left side is compared with null, so <code>null == u.outside</code> is true.) | ||
| + | * <code>and</code> and <code>or</code> treat a failed side as false, and <code>not</code> makes it true: <code>not (u.level > 5)</code> is true when <code>u</code> is null; | ||
| + | * an <code>if</code>, <code>while</code> or <code>wait()</code> whose condition fails takes it as false; | ||
| + | * an assignment of a failed value leaves an integer, list or pointer variable as it was, and empties a string variable (see [[Manual:DIL Manual/Assignment|Assignment]]). | ||
| + | Two cases stop the program instead: a string joined to a failed value with the string first (<code>"Hello " + u.name</code>), when it is assigned or passed to a procedure ("Type error in string assignment"); and a function's <code>return</code> of a failed value. | ||
| − | + | So test a unit before using its fields, in a statement of its own: | |
| − | + | if (u == null) | |
| − | + | goto nobody; | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Evaluation == | |
| + | Both sides of every operator are worked out, left side first. '''<code>and</code> and <code>or</code> don't stop early''': in <code>(u != null) and (u.level > 5)</code> the field is read even when <code>u</code> is null (harmless, since it only fails), and in <code>(n > 0) and check@myzone(n)</code> the function is called whatever <code>n</code> is. | ||
| + | |||
| + | The condition of a <code>wait()</code> is worked out again at every event of the kinds it waits for, which can be many times a second near a busy room or fight. Keep it short, and call <code>findunit()</code>, loops or your own functions after the wait instead of in it. | ||
| + | |||
| + | vmc works out the parts of an expression that use only constants when it compiles: <code>PULSE_SEC * 60</code> costs nothing when the program runs. A constant division by 0 (<code>5 / 0</code>) makes vmc itself crash. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin integer can_enter(pc : unitptr, minlvl : integer); | ||
| + | code | ||
| + | { | ||
| + | if (pc == null) | ||
| + | return (FALSE); | ||
| + | if ((pc.type != UNIT_ST_PC) or (pc.level < minlvl)) | ||
| + | return (FALSE); | ||
| + | if (("$banned" in pc.extra) != null) | ||
| + | return (FALSE); | ||
| + | return (TRUE); | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | A function that tells whether a character may pass: null is tested on its own first, each comparison is in brackets, and <code>in</code> on extras gives an extraptr that is compared with <code>null</code>. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/Operators|Operators]] | ||
| + | * [[Manual:DIL Manual/Data Types|Data Types]] | ||
| + | * [[Manual:DIL Manual/Assignment|Assignment]] | ||
| + | * [[Manual:DIL Manual/Fields|Fields]] | ||
| + | * [[Manual:DIL Manual/null|null]] | ||
| + | * [[Manual:DIL Manual/if()|if()]] | ||
Latest revision as of 23:54, 14 November 2026
An expression works out a value: the right side of an assignment, the condition of an if, while or wait(), the arguments of a call. Every expression has one of the eight types, known when vmc compiles it, and vmc refuses one of the wrong type ("Arg 1 of '+' not integer or string", "Assigning incompatible types").
Contents
What an expression is made of
- Constants. Integers in decimal (
42) or hexadecimal (0x2A); a minus sign in front makes them negative. Strings between double quotes, without\"or^inside. Lists in braces, of constants only:{"a", "b"},{1, 2}. null. Macros from the include files, such asTRUE,PULSE_SEC,UNIT_ST_PCorSKI_SEARCH, are replaced by their values before compiling. - Variables: the template's own, its arguments, and the built-in variables (
self,activator,argument...). - Fields, with a dot:
self.outside.title. Indexed fields take brackets:pc.skills[SKI_SEARCH],room.exit_to[NORTH]. A list entry or a character of a string takes.[i]:sl.[0],s.[0]; an extra by name,u.extra.["$data"]. See Fields. - Calls: built-in functions (
itoa(n),findroom(s)) and the zone's own functions (max@function(a, b); see Functions). - Operators joining these:
+ - * / %,== != < > <= >=,and or not,& |,in. See Operators. - Brackets to group:
(a + b) * 2.
A field or a call can't follow a call: findroom(s).title is a syntax error, and so is a DIL function's result passed straight to another DIL function (f(g(x))). Put the first result in a variable.
Precedence
DIL's operators have four levels, from binding most tightly to binding least:
not, which applies only to the value right after it;* / % and & in;+ - or |, and a minus sign in front of a value;- the comparisons
== != < > <= >=.
This is not the order of C or most other languages: and and or bind more tightly than the comparisons. vmc accepts
if (a == 1 and b == 2)
but reads it as (a == (1 and b)) == 2, which is false even when a is 1 and b is 2. In the same way a == 1 or a == 2 is false when a is 2. Put every comparison in brackets:
if ((a == 1) and (b == 2))
The Operators page has the whole table and more such cases.
Truth
A condition can be of any type. An integer is true when it isn't 0; a string, unit, extra, zone or command when it isn't null; a list when it isn't empty; a failed value is false. Comparisons, and, or and not give 1 (TRUE) or 0 (FALSE).
A string variable that has been given "" is not null, so it counts as true: test text with s != "". The fields title, descr, outside_descr and inside_descr tested on their own (if (u.title)) currently count the wrong way round, true when they are empty; compare them with "" as well.
Failed values
An expression that can't be worked out fails: it has no value, and no error is written. That happens with
- a field of null:
u.levelwhenuis null; - a field the unit doesn't have:
u.levelwhenuis a room; - a division by 0:
a / 0,a % 0; - an index outside a list or string:
sl.[9]in a list of three (this one writes "index of stringlist out of bounds" to the zone's log); - most built-in functions given a failed argument.
A failure spreads to the expression around it:
- arithmetic and joins with a failed part fail:
u.level + 1,u.name + "!"; - every comparison with a failed side is false.
u.level > 5andu.level <= 5are both false, and so areu.outside == nullandu.outside != nullwhenuis null. (One exception, a bug: when the right side of a==between units fails, the left side is compared with null, sonull == u.outsideis true.) andandortreat a failed side as false, andnotmakes it true:not (u.level > 5)is true whenuis null;- an
if,whileorwait()whose condition fails takes it as false; - an assignment of a failed value leaves an integer, list or pointer variable as it was, and empties a string variable (see Assignment).
Two cases stop the program instead: a string joined to a failed value with the string first ("Hello " + u.name), when it is assigned or passed to a procedure ("Type error in string assignment"); and a function's return of a failed value.
So test a unit before using its fields, in a statement of its own:
if (u == null) goto nobody;
Evaluation
Both sides of every operator are worked out, left side first. and and or don't stop early: in (u != null) and (u.level > 5) the field is read even when u is null (harmless, since it only fails), and in (n > 0) and check@myzone(n) the function is called whatever n is.
The condition of a wait() is worked out again at every event of the kinds it waits for, which can be many times a second near a busy room or fight. Keep it short, and call findunit(), loops or your own functions after the wait instead of in it.
vmc works out the parts of an expression that use only constants when it compiles: PULSE_SEC * 60 costs nothing when the program runs. A constant division by 0 (5 / 0) makes vmc itself crash.
Example
dilbegin integer can_enter(pc : unitptr, minlvl : integer);
code
{
if (pc == null)
return (FALSE);
if ((pc.type != UNIT_ST_PC) or (pc.level < minlvl))
return (FALSE);
if (("$banned" in pc.extra) != null)
return (FALSE);
return (TRUE);
}
dilend
A function that tells whether a character may pass: null is tested on its own first, each comparison is in brackets, and in on extras gives an extraptr that is compared with null.