lua icon indicating copy to clipboard operation
lua copied to clipboard

[BUG] Local Variable Scope Should Include Loop Condition

Open soh-cah-toa opened this issue 14 years ago • 0 comments

There's an example in Programming Lua, 2nd Edition that looks like (with a few subtle modifications):

local x = 10000
local sqr = x / 2

repeat
    sqr = (sqr + x / sqr) / 2
    local err = math.abs(sqr^2 - x)

    print(x)

    x = x - 1
until err < x / 10000

The output should be:

10000
9999
9998
9997
9996
9995
9994
9993
9992

However, parrot-lua spits out a runtime error:

10000
lua.pbc: example.lua:9: attempt to compare nil with number
stack traceback:
        example.lua:9: in main chunk
        [PIR]: in function 'docall'
        [PIR]: in function 'handle_script'
        [PIR]: in function 'main'

In Lua, the scope of local variables declared inside a loop includes the condition as well. However, it seems that parrot-lua does not know about this common mistake.

Note that this behavior is new in Lua 5.1.

soh-cah-toa avatar Oct 19 '11 22:10 soh-cah-toa