Home Explore Blog CI



neovim

6th chunk of `runtime/doc/luaref.txt`
de9508d8e24239d5808ce5cad5413a53a76247f7e56e549c0000000100000fa0
 of execution of Lua is called a chunk. A chunk is simply a sequence
of statements, which are executed sequentially. Each statement can be
optionally followed by a semicolon:
>
       chunk ::= {stat [ ; ]}
<
There are no empty statements and thus `;;` is not legal.

Lua handles a chunk as the body of an anonymous function with a variable
number of arguments (see |lua-function-define|). As such, chunks can define
local variables, receive arguments, and return values.

A chunk may be stored in a file or in a string inside the host program. When a
chunk is executed, first it is pre-compiled into instructions for a virtual
machine, and then the compiled code is executed by an interpreter for the
virtual machine.

Chunks may also be pre-compiled into binary form; see program `luac` for
details. Programs in source and compiled forms are interchangeable; Lua
automatically detects the file type and acts accordingly.

------------------------------------------------------------------------------
2.4.2  Blocks                                   *lua-block*

A block is a list of statements; syntactically, a block is the same as a
chunk:
>
       block ::= chunk
<
                                                *lua-do* *lua-end*
A block may be explicitly delimited to produce a single statement:
>
       stat ::= do block end
<
Explicit blocks are useful to control the scope of variable declarations.
Explicit blocks are also sometimes used to add a `return` or `break` statement
in the middle of another block (see |lua-control|).

------------------------------------------------------------------------------
2.4.3  Assignment                               *lua-assign*

Lua allows multiple assignment. Therefore, the syntax for assignment defines a
list of variables on the left side and a list of expressions on the right
side. The elements in both lists are separated by commas:
>
       stat ::= varlist1 = explist1
       varlist1 ::= var { , var }
       explist1 ::= exp { , exp }
<
Expressions are discussed in |lua-expressions|.

Before the assignment, the list of values is adjusted to the length of the
list of variables. If there are more values than needed, the excess values are
thrown away. If there are fewer values than needed, the list is extended with
as many `nil`s as needed. If the list of expressions ends with a function
call, then all values returned by this call enter in the list of values,
before the adjustment (except when the call is enclosed in parentheses; see
|lua-expressions|).

The assignment statement first evaluates all its expressions and only then are
the assignments performed. Thus the code
>lua
       i = 3
       i, a[i] = i+1, 20
<
sets `a[3]` to 20, without affecting `a[4]` because the `i` in `a[i]` is evaluated (to
3) before it is assigned 4. Similarly, the line
>lua
       x, y = y, x
<
exchanges the values of `x` and `y`.

The meaning of assignments to global variables and table fields can be changed
via metatables. An assignment to an indexed variable `t[i] = val` is
equivalent to `settable_event(t,i,val)`. (See |lua-metatable| for a complete
description of the `settable_event` function. This function is not defined or
callable in Lua. We use it here only for explanatory purposes.)

An assignment to a global variable `x = val` is equivalent to the
assignment `_env.x = val`, which in turn is equivalent to
>lua
       settable_event(_env, "x", val)
<
where `_env` is the environment of the running function. (The `_env` variable is
not defined in Lua. We use it here only for explanatory purposes.)

------------------------------------------------------------------------------
2.4.4  Control Structures                               *lua-control*

                                   *lua-if* *lua-then* *lua-else* *lua-elseif*
                                   *lua-while* *lua-repeat* *lua-until*
The control structures `if`, `while`, and `repeat` have the usual meaning and
familiar syntax:
>
       stat ::=  while 

Title: Lua Chunks, Blocks, Assignment, and Control Structures
Summary
This section defines a 'chunk' as a sequence of statements executed sequentially, optionally followed by semicolons. Chunks are treated like anonymous functions and can be pre-compiled. A 'block' is syntactically the same as a chunk and can be explicitly delimited to control variable scope. Lua supports multiple assignments, adjusting value lists with nil if needed, and evaluates expressions before assignments, allowing value swapping. Assignments to global variables and table fields can be altered using metatables and the `settable_event` function. The section also introduces Lua's standard control structures: `if`, `while`, and `repeat`.