<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Expressions > Expressions |
The basic expressions in Lua are the following:
exp ::= prefixexp
exp ::= nil | false | true
exp ::= Number
exp ::= String
exp ::= function
exp ::= tableconstructor
exp ::= `...?
exp ::= exp binop exp
exp ::= unop exp
prefixexp ::= var | functioncall | `(? exp `)?
Here are some examples:
f() -- adjusted to 0 results
g(f(), x) -- f() is adjusted to 1 result
g(x, f()) -- g gets x plus all results from f()
a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
a,b = ... -- a gets the first vararg parameter, b gets
-- the second (both a and b can get nil if there
-- is no corresponding vararg parameter)
a,b,c = x, f() -- f() is adjusted to 2 results
a,b,c = f() -- f() is adjusted to 3 results
return f() -- returns all results from f()
return ... -- returns all received vararg parameters
return x,y,f() -- returns x, y, and all results from f()
{f()} -- creates a list with all results from f()
{...} -- creates a list with all vararg parameters
{f(), nil} -- f() is adjusted to 1 result
Numbers and literal strings are explained in Lexical Conventions ; variables are explained in Variables ; function definitions are explained in Function Definitions ; function calls are explained in Function Calls ; table constructors are explained in Table Constructors . Vararg expressions, denoted by three dots (’...’), can only be used when directly inside a vararg function; they are explained in Function Definitions .
Binary operators comprise arithmetic operators (see Arithmetic Operators ), relational operators (see Relational Operators ), logical operators (see Logical Operators ), and the concatenation operator (see Concatenation ). Unary operators comprise the unary minus (see Arithmetic Operators ), the unary not (see Logical Operators ), and the unary length operator (see The Length Operator ).
Both function calls and vararg expressions can result in multiple values. If an expression is used as a statement (only possible for function calls (see Function Calls as Statements )), then its return list is adjusted to zero elements, thus discarding all returned values. If an expression is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the call is enclosed in parentheses). In all other contexts, Lua adjusts the result list to one element, discarding all values except the first one.
Any expression enclosed in parentheses always results in only one value. Thus, (f(x,y,z)) is always a single value, even if f returns several values. (The value of (f(x,y,z)) is the first value returned by f or nil if f does not return any values.)