Home Explore Blog CI



neovim

9th chunk of `runtime/doc/luaref.txt`
0f270efb843c16248df4126966a494da1e02d26734c6ffe30000000100000fa0
 *lua-local*

Local variables may be declared anywhere inside a block. The declaration may
include an initial assignment:
>
       stat ::=  local  namelist [ = explist1 ]
       namelist ::= Name { , Name }
<
If present, an initial assignment has the same semantics of a multiple
assignment (see |lua-assign|). Otherwise, all variables are initialized
with `nil`.

A chunk is also a block (see |lua-chunk|), and so local variables can be
declared in a chunk outside any explicit block. The scope of such local
variables extends until the end of the chunk.

The visibility rules for local variables are explained in |lua-visibility|.

==============================================================================
2.5  Expressions                                        *lua-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 )
<
Numbers and literal strings are explained in |lua-lexical|; variables are
explained in |lua-variables|; function definitions are explained in
|lua-function-define|; function calls are explained in |lua-function|;
table constructors are explained in |lua-tableconstructor|. Vararg expressions,
denoted by three dots (`...`), can only be used inside vararg functions;
they are explained in |lua-function-define|.

Binary operators comprise arithmetic operators (see |lua-arithmetic|),
relational operators (see |lua-relational|), logical operators (see
|lua-logicalop|), and the concatenation operator (see |lua-concat|).
Unary operators comprise the unary minus (see |lua-arithmetic|), the unary
`not` (see |lua-logicalop|), and the unary length operator (see |lua-length|).

Both function calls and vararg expressions may result in multiple values. If
the expression is used as a statement (see |lua-funcstatement|)
(only possible for function calls), then its return list is adjusted to zero
elements, thus discarding all returned values. If the 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.

Here are some examples:
>lua
       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 may 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
<
An 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.)

------------------------------------------------------------------------------
2.5.1  Arithmetic Operators                                 *lua-arithmetic*

Lua supports the usual arithmetic operators: the binary `+` (addition),
`-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo)
and `^`

Title: Lua Local Variables, Expressions, and Arithmetic Operators
Summary
This section covers local variable declarations in Lua, including the scope of local variables and how they are initialized. It then outlines the basic expressions in Lua, such as `nil`, booleans, numbers, strings, functions, table constructors, and vararg expressions. It also discusses how function calls and vararg expressions handle multiple values and how Lua adjusts the result list in different contexts. Finally, it lists the arithmetic operators supported by Lua.