github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/goto.lua (about)

     1  -- ok to jump over local dec. to end of block
     2  x = 1
     3  do
     4      goto l1
     5      local a = 23
     6      x = a
     7      ::l1::;
     8  end
     9  
    10  print(x)
    11  --> =1
    12  
    13  print(load([[
    14    repeat
    15      if x then goto cont end
    16      local xuxu = 10
    17      ::cont::
    18    until xuxu < x
    19  ]]))
    20  --> ~nil\t.*no visible label 'cont'
    21  
    22  -- (bugfix) A return statement prevents back labels
    23  print(load[[
    24    goto L
    25    local a = 1
    26    ::L:: return a
    27  ]])
    28  --> ~nil\t.*no visible label 'L'
    29  
    30  -- Lua 5.4 forbids shadowing labels
    31  
    32  print(load[[
    33    ::label::
    34    print"hello"
    35    ::label::
    36    print"bye"
    37  ]])
    38  --> ~nil\t.*label 'label' already defined at line 1
    39  
    40  print(load[[
    41    ::foo::
    42    do
    43      ::foo::
    44      goto foo
    45    end
    46  ]])
    47  --> ~nil\t.*label 'foo' already defined at line 1
    48  
    49  -- It's OK to reuse a label name that was defined outside the current function
    50  -- scope.
    51  do
    52    ::l2::
    53    local f = function(n)
    54      ::l2::
    55      print(n)
    56      n = n - 1
    57      if n > 0 then
    58        goto l2
    59      end
    60    end
    61    f(2)
    62  end
    63  --> =2
    64  --> =1