Product Documentation

Expressions

Aug 31, 2016

Expressions compute values from variable and literal values.

Arithmetic Operations

Arithmetic operations are performed on number values. If a string value is used in an arithmetic operation, it is converted to a number -- if that fails, an error is returned.

a + b add a and b
a - b subtract b from a
a * b multiply a and b
a / b divide a by b
a % b modulo = a - math.floor(a/b)*b
a ^ b raise a to the b power; b can be any number
-a negate a

Relational Operations

Relational operations compare two values and return true if the relation is satisfied and false if it is not. Relational operations can be performed between values of any type. If the values are not the same type, false is returned. Numbers are compared in the usual way. Strings are compared using the collating sequence for the current locale.

a == b a is equal to b
a ~= b a is not equal to b
a < b a is less than b
a > b a is greater than b
a <= b a is less than or equal to b
a >= b a is greater than or equal to b

Logical Operations

Logical operations are traditionally performed on Boolean values, but in this language they can be performed on any two values. nil and false is considered false and any other value is considered true. Logical operations use short-cut evaluation, where if the first value determines the result of the operation, the second value is not evaluated.

a and b if a is false or nil then return a else return b
a or b if a is not false and not nil then return a else return b
not a if a is not false or nil return false else return true

The and and or operations can be used for conditional evaluation within an expression:

a or b can be used to provide a default value b if a is uninitialized (nil). This is useful for optional parameters in functions.
a and b or c can be used to chose non-nil b or c based on the condition a. If a is true, then a and b returns b, and b or c returns b. If a is false, then a and b returns false and false or c returns c. This is equivalent to a ? b : c in the C programming language.

Concatenation

String concatenation is s1 .. s2. This creates a new string large enough to hold the contents of s1 and s2 and copies the contents to the new string. An error results if s1 or s2 are not strings. Note that repeated concatenation may have considerable copying overhead. If you build a string of n bytes by concatenating one byte at a time, this will copy n*(n+1)/2 bytes. For better performance, you can put pieces of a string to be concatenated into a table (discussed later) and then use the table.concat()function. An example of this is shown in the COMBINE_HEADERS() example.

Length

The length of a string s is returned by #s. The # operator is also used with array tables, as discussed later.

Precedence

Operator precedence determines the order in which operations are performed in an expression, with higher precedence operations done before those with lower precedence. Precedence order can as usual be overridden by parentheses. For example, in a + b * c, * has higher precedence than +, so the expression is evaluated as a + (b * c).

highest ^
  not # - (unary)
  * / %
  + -
  ..
  = ~= < > <= >=
  and
lowest or

Operations with the same precedence are performed left to right (left associative), except ^ and .. that are performed right to left (right associative). So a^b^c is evaluated as a^(b^c).