What is the difference between a piece and a block in lua

what's the difference between a piece and a block in lua? I don’t understand what a piece is!

+4
source share
4 answers

Was asked and reviewed here: http://lua-users.org/lists/lua-l/2012-06/threads.html#00723

From guide 5.2:

The unit of execution of Lua is called a piece. Syntactically, a block is just a block: chunk ::= block

From the mouth of Roberto:

The fact that a block is a block does not mean that any block is a Piece. Pieces do not nest (unlike blocks). A block is the most remote block that you download for "download".

+5
source

A block is an independently executed sequence of statements. A block is just a sequence of statements. The difference is that a piece can be executed independently of other pieces.

All pieces are blocks (sequences of statements), but not all blocks are pieces.

A block is basically a Lua function; you can call it with some parameters and it will return 0 or more values. This is what I mean by “independently executable”: the instructions inside the piece will be executed in order. But as soon as you exit a piece, which piece you perform is up to you.

+6
source

A block may be a piece of code. However, a block usually means zero or more statements belonging, for example, to an if or function.

Quote from the official Lua link :

[...] A block is a list of operators; syntactically, the block matches the block [...]

If you look at the Lua grammar , you will see that they are the same:

 chunk ::= {stat [`;´]} [laststat [`;´]] block ::= chunk 
+3
source

A block is a sub-part of Chunk, they can be the same as in the code example 02: for example, 01:

  if condition1 then block1 elseif condition2 then block2 elseif condition3 then block3 else block4 end 

here we have one piece starting with
if condition 1, then for the last end but this code has four blocks, each condition has a separate block. this example has one piece and four blocks.

Example 02:

  /////////////////////////// 01 ////////////////////// for variable = beginning, end, step do block end /////////////////////////// 02 ////////////////////// function Name() block end /////////////////////////// 03 ////////////////////// if condition then block end 

in example 02, the piece and block are the same, but they are always not the same. read for yourself.

http://www.lua.org/manual/2.5/manual.html

4.4.1 - Blocks

A block is a list of statements that are executed sequentially. Any instruction may be followed by a semicolon:

block: = {stat sc} [ret] sc: = [';']

2 - Environment and pieces

The unit of execution of Lua is called a piece. The syntax for the pieces is:

chunk: = {stat | function} [ret]

this is useful?????

+1
source

All Articles