Expressions and Operators

An expression is anything that evaluates to a value. This can include a single value such as �6� or a compound value built with operators such as �1 + 3�. You can use parentheses to �group� expressions and control the order in which they are evaluated. For example, the following lines will all evaluate to the same value:

a = 10;
a = (5 * 1) * 2;
a = 100 / 10;
a = 100 / (2 * 5);

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. The following mathematical operators are supported:

+            (addition)
-             (subtraction)
*             (multiplication)
/             (division)
%           (modulo)
^             (exponentiation)
unary -    (negation)

Here are some examples:

a = 5 + 2;
b = a * 100;
twentythreepercent = 23 / 100;
neg = -29;
pos = -neg;

Relational Operators

Relational operators allow you to compare how one value relates to another. The following relational operators are supported:

>            (greater-than)
<
            (less-than)
<=
          (less-than or equal to)
>=
          (greater than or equal to)
~=
          (not equal to)
==
          (equal)

All of the relational operators can be applied to any two numbers or any two strings. All other values can only use the == operator to see if they are equal.

Relational operators return Boolean values (true or false). For example:

10 > 20; -- resolves to false

a = 10;
a > 300; -- false

(3 * 200) > 500; -- true
"Brett" ~= "Lorne" -- true

One important point to mention is that the == and ~= operators test for complete equality, which means that any string comparisons done with those operators are case sensitive. For example:

"Jojoba" == "Jojoba"; -- true
"Wildcat" == "wildcat"; -- false
"I like it a lot" == "I like it a LOT"; -- false

"happy" ~= "HaPPy"; -- true

Logical Operators

Logical operators are used to perform Boolean operations on Boolean values. The following logical operators are supported:

and         (only true if both values are true)
or           (true if either value is true)
not          (returns the opposite of the value)

For example:

a = true;
b = false;
c = a and b; -- false
d = a and nil; -- false
e = not b; -- true

Note that only nil and false are considered to be false, and all other values are true.

For example:

iaminvisible = nil;
if iaminvisible then
     -- any lines in here won't happen
     -- because iaminvisible is considered false
     Dialog.Message("You can't see me!", "I am invisible!!!!");
end

if "Brett" then
     -- any lines in here WILL happen, because only nil and false
     -- are considered false...anything else, including strings,
     -- is considered true
     Dialog.Message("What about strings?", "Strings are true.");
end

Concatenation

In Setup Factory scripting, the concatenation operator is two periods (..). It is used to combine two or more strings together. You don�t have to put spaces before and after the periods, but you can if you want to.

For example:

name = "Joe".." Blow"; -- assigns "Joe Blow" to name
b = name .. " is number " .. 1; -- assigns "Joe Blow is number 1" to b

Operator Precedence

Operators are said to have precedence, which is a way of describing the rules that determine which operations in a series of expressions get performed first. A simple example would be the expression 1 + 2 * 3. The multiply (*) operator has higher precedence than the add (+) operator, so this expression is equivalent to 1 + (2 * 3). In other words, the expression 2 * 3 is performed first, and then 1 + 6 is performed, resulting in the final value 7.

You can override the natural order of precedence by using parentheses. For instance, the expression (1 + 2) * 3 resolves to 9. The parentheses make the whole sub-expression �1 + 2� the left value of the multiply (*) operator. Essentially, the sub-expression 1 + 2 is evaluated first, and the result is then used in the expression 3 * 3.

Operator precedence follows the following order, from lowest to highest priority:

and         or
<     >   <=   >=   ~=   ==

..
+            -
*             /          %
not         #          - (unary)
^

Operators are also said to have associativity, which is a way of describing which expressions are performed first when the operators have equal precedence. In the script engine, all binary operators are left associative, which means that whenever two operators have the same precedence, the operation on the left is performed first. The exception is the exponentiation operator (^), which is right-associative.

When in doubt, you can always use explicit parentheses to control precedence. For example:

a + 1 < b/2 + 1

...is the same as:

(a + 1) < ((b/2) + 1)

...and you can use parentheses to change the order of the calculations, too:

a + 1 < b/(2 + 1)

In this last example, instead of 1 being added to half of b, b is divided by 3.