Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the auto-introduction of do...end block work for goto statement?

Tags:

lua

The lua doc's comment on label scope:

A goto may jump to any visible label as long as it does not enter into the scope of a local variable

So in my understanding, the following code is problematic:

-- codelist 1
goto a
local x = 42
::a::

But it works well in the lua web shell. The doc continues to say:

Note that you can think of

do
  <...>
  --::a::
  goto a  -- invalid (forward jump into scope of local definition)
  goto b  -- valid (jump out of block)
  <...>
  local x
  <...>
  ::a::
  <...>
  --goto a
  ::b::
end

as equivalent to

do
  <...>
  --::a::
  goto a  -- invalid (jump into nested block prohibited because nested label not even visible here)
  goto b  -- valid (jump out of block)
  <...>
  do
    local x
    <...>
    ::a::
    <...>
    --goto a
  end
  ::b::
end

Why is ::a:: included in the auto-introduced do...end block and why isn't ::b::? Please help me understand, thanks.

Edit: I also found this old post, and it seems that there indeed was a time when codelist 1 was prohibited.

like image 794
SedriX Avatar asked Nov 21 '19 10:11

SedriX


People also ask

Can a goto statement transfer control from a block to an exception?

A GOTO statement cannot transfer control into an exception handler. A GOTO statement cannot transfer control from an exception handler back into the current block (but it can transfer control from an exception handler into an enclosing block). Identifies either a block or a statement (see " plsql_block ::= ", " statement ::= ", and " label " ).

What is the GOTO statement in C?

The GOTO statement allows you to transfer control to a labeled block or statement. The following illustrates the syntax of the GOTO statement: The label_name is the name of a label that identifies the target statement. In the program, you surround the label name with double enclosing angle brackets as shown below:

What is goto label in JavaScript?

Here label is a user-defined identifier which indicates the target statement. The statement immediately followed after ‘label:’ is the destination statement. The ‘label:’ can also appear before the ‘goto label;’ statement in the above syntax. Below are some examples on how to use goto statement:

What are the restrictions of the GOTO statement?

The GOTO statement is subject to the following restrictions. First, you cannot use a GOTO statement to transfer control into an IF, CASE or LOOP statement, the same for sub-block. The following example attempts to transfer control into an IF statement using a GOTO statement:


1 Answers

goto a
local x = 42
::a::

will not cause an error. Not even in 5.2

goto a
local x = 42
::a::
print("oh no")

on the other hand will.

Lua's pre-compilation will only complain if you jump into the scope of a local and acutally do something after the label while still within the local's scope. So you may jump there but you may not do something in this invalid situation.

Same with your second example. ::b:: is the end of the block. Nothing happens within the scope of x after it so it's ok to jump there.

goto b
local x = 42
::a::
print("oh no")
::b::

would be ok.

like image 71
Piglet Avatar answered Nov 27 '22 06:11

Piglet