Simple expressions are:
exp ::= '(' exp ')'Numbers (numerical constants) and string literals are explained in Section 4.1. Variables are explained in Section 4.4.2.
exp ::= nil
exp ::= 'number'
exp ::= 'literal'
exp ::= var
Lua supports the usual arithmetic operators. These operators are the binary +, -, *, / and ^ (exponentiation), and the unary -. If the operands are numbers, or strings that can be converted to numbers, according to the rules given in Section 4.2, all operations but exponentiation have the usual meaning. Otherwise, the fallback ``arith'' is called; see Section 4.7. An exponentiation always calls this fallback. The standard mathematical library redefines this fallback, giving the expected meaning to exponentiation; see Section 6.3.
Lua offers the following relational operators:
< > <= >= ~= ==
All return nil as false and 1 as true.
Equality first compares the types of its operands. If they are different, the result is nil. Otherwise, their values are compared. Numbers and strings are compared in the usual way. Tables, CFunctions, and functions are compared by reference, that is, two tables are considered equal only if they are the same table. The operator ~= is exactly the negation of equality (=).
The other operators work as follows. If both arguments are numbers, they are compared as such. Otherwise, if both arguments can be converted to strings, their values are compared using lexicographical order. Otherwise, the fallback ``order'' is called; see Section 4.7.
All logical operators, like control structures,
consider nil as false and anything else as true.
The logical operators are:
and or not
The operators and and or use short-cut evaluation,
that is,
the second operand is evaluated only if necessary.
Lua offers a string concatenation operator, denoted by ``..''. If operands are strings or numbers, they are converted to strings according to the rules in Section 4.2. Otherwise, the fallback ``concat'' is called; see Section 4.7.
Operator precedence follows the table below,
from the lower to the higher priority:
and or
< > <= >= ~= =
..
+ -
* /
not - (unary)
^
All binary operators are left associative, except for ^,
which is right associative.
The general syntax for constructors is:
tableconstructor ::= '{' fieldlist '}'
fieldlist ::= lfieldlist | ffieldlist | lfieldlist ';' ffieldlist
lfieldlist ::= [ lfieldlist1 ]
ffieldlist ::= [ ffieldlist1 ]
The form lfieldlist1 is used to initialize lists.
lfieldlist1 ::= exp { ',' exp } [ ',' ]The expressions in the list are assigned to consecutive numerical indexes, starting with 1. As an example:
a = {"v1", "v2", 34}is equivalent to:
temp = {} temp[1] = "v1" temp[2] = "v2" temp[3] = 34 a = temp
The next form initializes named fields in a table.
ffieldlist1 ::= ffield { ',' ffield } [ ',' ]As an example:
ffield ::= name '=' exp
a = {x = 1, y = 3}is equivalent to:
temp = {} temp.x = 1 temp.y = 3 a = temp
functioncall ::= var realParamsHere, var can be any variable (global, local, indexed, etc). If its type is function or CFunction, this function is called. Otherwise, the fallback ``function'' is called, having as first parameter the value of var, and then the original call parameters.
The form:
functioncall ::= var ':' name realParamscan be used to call ``methods''. A call var:name(...) is syntactic sugar for
var.name(var, ...)except that var is evaluated only once.
realParams ::= '(' [ explist1 ] ')'All argument expressions are evaluated before the call; then the list of arguments is adjusted to the length of the list of parameters (see Section 4.3); finally, this list is assigned to the formal parameters. A call of the form f{...} is syntactic sugar for f({...}), that is, the parameter list is a single new table.
realParams ::= tableconstructor
explist1 ::= exp1 { ',' exp1 }
Because a function can return any number of results (see Section 4.4.3), the number of results must be adjusted before used. If the function is called as an statement (see Section 4.4.4), its return list is adjusted to 0. If the function is called in a place that needs a single value (syntactically denoted by the non-terminal exp1), its return list is adjusted to 1. If the function is called in a place that can hold many values (syntactically denoted by the non-terminal exp), no adjustment is done.