Lua Programming/Expressions

As explained before, expressions are pieces of code that have a value and that can be evaluated. They cannot be executed directly (with the exception of function calls), and thus, a script that would contain only the following code, which consists of an expression, would be erroneous:

Code must be comprised of a sequence of statements. These statements can contain expressions which will be values the statement has to manipulate or use to execute the instruction.

Some code examples in this chapter do not constitute valid code, because they consist of only expressions. In the next chapter, statements will be covered and it will be possible to start writing valid code.

Types
To evaluate an expression is to compute it to find its value. The value a given expression evaluates to might be different from one context to another, since it can depend on the environment and stack level. This value will sometimes be a number, sometimes text and the other times any of many other data types, which is why it is said to have a type.

In Lua, and in programming in general, expressions will usually consist of one or more values with zero or more operators. Some operators can only be used with some types (it would be illogical, for example, to try to divide text, while it makes sense to divide numbers). There are two kinds of operators: unary operators and binary operators. Unary operators are operators that only take one value. For example, the unary - operator only takes one number as a parameter: -5, -3, -6, etc. It takes one number as a parameter and negates that number. The binary - operator, however, which is not the same operator, takes two values and subtracts the second from the first: 5 - 3, 8 - 6, 4 - 9, etc.

It is possible to obtain a number's type as a string with the  function:

Numbers
Numbers generally represent quantities, but they can be used for many other things. The number type in Lua works mostly in the same way as real numbers. Numbers can be constructed as integers, decimal numbers, decimal exponents or even in hexadecimal. Here are some valid numbers:


 * 3
 * 3.0
 * 3.1416
 * 314.16e-2
 * 0.31416E1
 * 0xff
 * 0x56

Arithmetic operations
The operators for numbers in Lua are the following:

You probably already know all of these operators (they are the same as basic mathematical operators) except the last. The last is called the modulo operator, and simply calculates the remainder of the division of one number by another. 5 % 3, for example, would give 2 as a result because 2 is the remainder of the division of 5 by 3. The modulo operator is less common than the other operators, but it has multiple uses.

Integers
A new subtype of numbers, integers, was added in Lua 5.3. Numbers can be either integers or floats. Floats are similar to numbers as described above, while integers are numbers with no decimal part. Float division and exponentiation always convert their operands to floats, while all other operators give integers if their two operands were integers. In other cases, with the exception of the floor division operator, the result is a float.

Nil
Nil is the type of the value nil, whose main property is to be different from any other value; it usually represents the absence of a useful value. Some examples of things that have the value nil:
 * value of variables that you access before assigning them a value
 * value you get when trying to access a variable outside of its scope
 * value for any key in a table that hasn't been assigned
 * value returned by  if it can't convert a string to a number

On a more advanced note, purposefully assigning a nil value removes the reference to a variable or table, and allows the garbage collector to reclaim its memory.

Booleans
A boolean value can be either true or false, but nothing else. This is written in Lua as  and , which are reserved keywords. Important to note is that  is a different data type as stated earlier. ,,   are usually associated with boolean values, but can be used with any datatype in Lua.

Essentially, the  operator just negates the boolean value (makes it false if it is true and makes it true if it is false), the   operator returns true if both are true and false if not and the   operator returns true if either of arguments is true and false otherwise. This is however not exactly how they work, as the exact way they work is explained in the table above. In Lua, the values false and nil are both considered to be false in logical expressions, while everything else is considered as true (even 0 and empty strings).

The relational operators introduced in the next chapter do not necessarily take boolean values as operands, but will always give a boolean value as a result.

This can be difficult to reconcile. For added clarity, here are some truth tables or expression-result pairs. Here  can be ,   or  :

This somewhat counterintuitively means that

In addition,

Strings
Strings are sequences of characters that can be used to represent text. They can be written in Lua by being contained in double quotes, single quotes or long brackets, which were covered before in the section about comments (it should be noted that comments and strings have nothing in common other than the fact they can both be delimited by long brackets, preceded by two hyphens in the case of comments). Strings that aren't contained in long brackets will only continue for one line. Because of this, the only way to make a string that contains many lines without using long brackets is to use escape sequences. This is also the only way to insert single or double quotes in certain cases. Escape sequences consist of two things: an escape character, which will always be a backslash ('\') in Lua, and an identifier that identifies the character to be escaped.

Escape sequences are used when putting the character directly in the string would cause a problem. For example, if you have a string of text that is enclosed in double quotes and must contain double quotes, then you need to enclose the string in different characters or to escape the double quotes. Escaping characters in strings delimited by long brackets is not necessary, and this is true for all characters. All characters in a string delimited with long brackets will be taken as-is. The  character is used in string patterns to escape magic characters, but the term escaping is then used in another context.

For convenience, if an opening long string bracket is immediately followed by a new line, that new line will be ignored. Therefore, the two following strings are equivalent:

It is possible to get the length of a string, as a number, by using the unary length operator ('#'):

Concatenation
"In formal language theory and computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball"."

- Wikipedia

The string concatenation operator in Lua is denoted by two dots ('..'). Here is an example of concatenation that concatenates "snow" and "ball" and prints the result:

This code will concatenate "snow" and "ball" and will print the result.

Other types
The four basic types in Lua (numbers, booleans, nil and strings) have been described in the previous sections, but four types are missing: functions, tables, userdata and threads. Functions are pieces of code that can be called, receive values and return values back. Tables are data structures that can be used for data manipulation. Userdata are used internally by applications Lua is embedded in to allow Lua to communicate with that program through objects controlled by the application. Finally, threads are used by coroutines, which allow many functions to run at the same time. These will all be described later, so you only need to keep in mind that there are other data types.

Literals
Literals are notations for representing fixed values in source code. All values can be represented as literals in Lua except threads and userdata. String literals (literals that evaluate to strings), for example, consist of the text that the string must represent enclosed into single quotes, double quotes or long brackets. Number literals, on the other hand, consist the number they represent expressed using decimal notation (ex: ), scientific notation (ex:   and  ) or hexadecimal notation (ex:  ).

Coercion
Coercion is the conversion of a value of one data type to a value of another data type. Lua provides automatic coercion between string and number values. Any arithmetic operation applied to a string will attempt to convert this string to a number. Conversely, whenever a string is expected and a number is used instead, the number will be converted to a string. This applies both to Lua operators and to default functions (functions that are provided with the language).

Coercion of numbers to strings and strings to numbers can also be done manually with the  and   functions. The former accepts a number as an argument and converts it to a string, while the second accepts a string as an argument and converts it to a number (a different base than the default decimal one can optionally be given in the second argument).

Bitwise operations
Since Lua 5.3, bitwise operators are provided to operate on binary numerals (bit patterns). These operators are not used as frequently as the others, so you may skip this section if you do not need them.

The bitwise operators in Lua always operate on integers, converting their operands if this is necessary. They also give integers.

The bitwise AND operation (with operator ) performs logical conjunction on each pair of bits of two binary representations of equal length. For example,  evaluates to 1. We can explain this by looking at the binary representation of these numbers (the subscripts are used to denote the base):

$$(5)_{10} = (0101)_2$$

$$(3)_{10} = (0011)_2$$

$$(1)_{10} = (0001)_2$$

If the bits in a given position in the binary representation of both 5 and 3 are 1 (as is the case for the last bit), then the bit at that position will be 1 in the result; in all other cases, it will be 0.

The bitwise OR operation (with operator ) works in the same way as the bitwise AND, performing logical disjunction instead where it performs logical conjunction. Thus,  will evaluate to 7:

$$(5)_{10} = (0101)_2$$

$$(3)_{10} = (0011)_2$$

$$(7)_{10} = (0111)_2$$

Here, we can see that the bit in each position in the final result was 0 only when the binary representations of the two operands had a 0-bit at that position.

The bitwise XOR operation (with operator ) works like two others, but at a given position, the final bit is only 1 if one, and not both, of the bits in the operands are 1.

$$(5)_{10} = (0101)_2$$

$$(3)_{10} = (0011)_2$$

$$(6)_{10} = (0110)_2$$

This is the same as the previous example, but we can see that the last bit in the result is 0 instead of 1, since the last bit of both operands was 1.

The bitwise NOT operation (with operator ) performs logical negation on each bit of its unique operand, which means that each 0 becomes 1 and that each 1 becomes 0. Thusly,  will evaluate to -8:

$$(7)_{10} = (0111)_2$$

$$(8)_{10} = (1000)_2$$

Here, the first bit became 1 in the result because it was 0 in the operand, and the other bits became 0 because they were all 1.



In addition to these bitwise operators, Lua 5.3 also supports arithmetic bit shifts. The left shift, with operator  and illustrated on left, consists in shifting all bits to the left, by a number of bits that corresponds to the second operand. The right shift, denoted by operator  and illustrated on right, does the same but in the opposite direction.

Operator precedence
Operator precedence works the same way in Lua as it typically does in mathematics. Certain operators will be evaluated before others, and parentheses can be used to arbitrarily change the order in which operations should be executed. The priority in which operators are evaluated is in the list below, from higher to lower priority. Some of these operators were not discussed yet, but they will all be covered at some point in this book.


 * 1) Exponentiation:
 * 2) Unary operators: ,  ,  ,
 * 3) Level 2 mathematical operators: ,  ,  ,
 * 4) Level 1 mathematical operators:  ,
 * 5) Concatenation:
 * 6) Bit shifts:  ,
 * 7) Bitwise AND:
 * 8) Bitwise XOR:
 * 9) Bitwise OR:
 * 10) Relational operators: ,  ,  ,  ,  ,
 * 11) Boolean and:
 * 12) Boolean or:

Quiz
{What will  output? { string (i) }
 * type="{}"}

{What will the expression  return? { - -  +  -  }
 * type=""}
 * and  return their arguments rather than a boolean value.
 * All values different than  and   are considered as 'true' (even 0).
 * returns the first argument, since it is neither  nor.
 * returns the first argument, since it is neither  nor  . 0 is considered as 'true'.

{Which strings are valid? { + +  -  +  +  -  }
 * type="[]"}
 * No escaping for the single quote required, since the string is limited by double quotes.
 * Escaping is necessary here.
 * There is a double quote in the middle of a double quoted string.
 * No escaping for the double quote required, since the string is limited by single quotes.
 * Escaping is optional here.
 * There is a single quote in the middle of a single quoted string.

{Which expressions give the string ? { - +  -  +  }
 * type="[]"}
 * Yields the number 125.
 * Yields the number 35.

{True or false? { - +  }
 * type=""}
 * Beware the operator precedence ( is higher than  )! The expression is equivalent to , which evaluates first to   and thus to.