Lua supports an almost conventional set of statements. The conventional commands include assignment, control structures and procedure calls. Non-conventional commands include table constructors, explained in Section 4.5.7, and local variable declarations.
A block is a list of statements, executed sequentially. Any statement can be optionally followed by a semicolon.
block ::= { stat sc } [ ret sc ]For syntactic reasons, a return statement can only be written as the last statement of a block. This restriction also avoids some ``statement not reached'' errors.
sc ::= [ ';' ]
stat ::= varlist1 '=' explist1This statement first evaluates all values on the right side and eventual indices on the left side, and then makes the assignments. Therefore, it can be used to exchange two values, as in
varlist1 ::= var { ',' var }
x, y = y, xBefore the assignment, the list of values is adjusted to the length of the list of variables; see Section 4.3.
var ::= nameA single name can denote a global or a local variable, or a formal parameter.
var ::= var '[' exp1 ']'Square brackets are used to index a table. If var results in a table value, the field indexed by the expression value gets the assigned value. Otherwise, the fallback settable is called, with three parameters: the value of var, the value of expression, and the value being assigned to it; see Section 4.7.
var ::= var '.' nameThe syntax var.NAME is just syntactic sugar for var["NAME"].
The condition expression of a control structure can return any value. All values different from nil are considered true, while nil is considered false. if's, while's and repeat's have the usual meaning.
stat ::= while exp1 do block end | repeat block until exp1 | if exp1 then block { elseif } [ else block ] end
elseif ::= elseif exp1 then block
A return is used to return values from a function. Because a function may return more than one value, the syntax for a return statement is:
ret ::= return explist
stat ::= functioncallEventual returned values are thrown away. Function calls are explained in Section 4.5.8; constructors are the subject of Section 4.5.7.
stat ::= tableconstructor
stat ::= local declist [ init ]If there is an initial assignment, it has the same semantics of a multiple assignment. Otherwise, all variables are initialized with nil.
declist ::= name { ',' name }
init ::= '=' explist1
Next: 4.5 Expressions Up: 4 The Language Previous: 4.3 Adjustment