lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 03/01/15 02:00 PM, Zehao Jin wrote:
Hi all, there was a table constructor question confused me when I was testing with the following cases.
Let's run it thru luac
1.
tb={ 'b' , [1] = 'a' }
$ luac -l -l -
tb={ 'b' , [1] = 'a' }

main <stdin:0,0> (6 instructions at 0x12220c0)
0+ params, 2 slots, 1 upvalue, 0 locals, 4 constants, 0 functions
    1    [1]    NEWTABLE     0 1 1
    2    [1]    LOADK        1 -2    ; "b"
    3    [1]    SETTABLE     0 -3 -4    ; 1 "a"
    4    [1]    SETLIST      0 1 1    ; 1
    5    [1]    SETTABUP     0 -1 0    ; _ENV "tb"
    6    [1]    RETURN       0 1
constants (4) for 0x12220c0:
    1    "tb"
    2    "b"
    3    1
    4    "a"
locals (0) for 0x12220c0:
upvalues (1) for 0x12220c0:
    0    _ENV    1    0

I found that tb[1] is 'b' but not 'a',  I thought 'b' would be override but not.

2.
 tb = { [1] = 'a' , 'b' ,[1]='c'}
$ luac -l -l -
tb = { [1] = 'a' , 'b' ,[1]='c'}

main <stdin:0,0> (7 instructions at 0x222d0c0)
0+ params, 2 slots, 1 upvalue, 0 locals, 5 constants, 0 functions
    1    [1]    NEWTABLE     0 1 2
    2    [1]    SETTABLE     0 -2 -3    ; 1 "a"
    3    [1]    LOADK        1 -4    ; "b"
    4    [1]    SETTABLE     0 -2 -5    ; 1 "c"
    5    [1]    SETLIST      0 1 1    ; 1
    6    [1]    SETTABUP     0 -1 0    ; _ENV "tb"
    7    [1]    RETURN       0 1
constants (5) for 0x222d0c0:
    1    "tb"
    2    1
    3    "a"
    4    "b"
    5    "c"
locals (0) for 0x222d0c0:
upvalues (1) for 0x222d0c0:
    0    _ENV    1    0

 I still got the tb[1]=='b' .

The PIL book tells that {"r","g","b"} is equivalent to {[1]="r",[2]="g",[3]="b"}, so in the above cases, I 
thought tb={ 'b' , [1] = 'a' } would be translated by Lua to tb={ [1] =  'b' , [1] = 'a' } and tb[1] should be 'a',
but that's wrong.

Assigning to the same thing on the same _expression_ is undefined. For example:
$ luac -l -l -
a,a = 1,2

main <stdin:0,0> (4 instructions at 0x222e0c0)
0+ params, 2 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
    1    [1]    LOADK        0 -2    ; 1
    2    [1]    SETTABUP     0 -1 -3    ; _ENV "a" 2
    3    [1]    SETTABUP     0 -1 0    ; _ENV "a"
    4    [1]    RETURN       0 1
constants (3) for 0x222e0c0:
    1    "a"
    2    1
    3    2
locals (0) for 0x222e0c0:
upvalues (1) for 0x222e0c0:
    0    _ENV    1    0

On the other hand, this is actually defined:
local a,a,a = 1,2,3

Due to shadowing, this _expression_ generates 3 locals with the same name, each with a different value, and the last one takes effect. (thus, when scanning locals, you should scan them BACKWARDS, not forwards like most ppl do >.>)

$ luac -l -l -
local a,a,a = 1,2,3

main <stdin:0,0> (4 instructions at 0x14050c0)
0+ params, 3 slots, 1 upvalue, 3 locals, 3 constants, 0 functions
    1    [1]    LOADK        0 -1    ; 1
    2    [1]    LOADK        1 -2    ; 2
    3    [1]    LOADK        2 -3    ; 3
    4    [1]    RETURN       0 1
constants (3) for 0x14050c0:
    1    1
    2    2
    3    3
locals (3) for 0x14050c0:
    0    a    4    5
    1    a    4    5
    2    a    4    5
upvalues (1) for 0x14050c0:
    0    _ENV    1    0
 I searched and got that the table stored in Lua has the array part and hash node part, keys are integers
 which are not less then a specified number N would stored in the array part,so I though keys are all [1] 
would be override by the following assignments,that all I know about this question.
could anyone explain for me? Thanks.

p.s. Forgive my poor English :P






-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.