// File: marxn[P]pupp/src/parser/expression.cpp
// Function: VarExpression::GetVarRef(NodeContext*)
ValueBox * VarExpression::GetVarRef(NodeContext * context)
{
        Variable * var = context->GetVariable(this->VarLayer, this->VarDef->GetVarIndex());

        if(var==NULL)
        {
                cerr<<"pupp runtime error: cannot find variable:"<<this->VarName<<endl;
                return NULL;
        }

        if(this->ExprsList.size()==0)
        {
                return var->GetVBox();
        }

        ValueBox * result = var->GetVBox();

        for(list<list<Expression*>*>::iterator gitor = this->ExprsList.begin(); gitor!=this->ExprsList.end(); gitor++)
        {
            if(result==NULL)
            {
                    return NULL;
            }
            list<Expression*> * ExprList = *gitor;
            
            if(result->GetVal()->GetType()==Array)
            {
                    if(static_cast<ArrayValue*>(result->GetVal())->GetDimensionNum()!=ExprList->size())
                    {
                            cerr<<"pupp runtime error: wrong dimension variable: "<<var->GetVarName()<<endl;
                            return NULL;
                    }

                    vector<long> desc;
                    
                    for(list<Expression*>::iterator i = ExprList->begin(); i!=ExprList->end(); i++)
                    {
                            ConstValue * value = (*i)->Calculate(context);
                            if(value->GetType()!=Integer)
                            {
                                    cerr<<"pupp runtime error: invalid index for variable: "<<var->GetVarName()<<endl;
                                    delete value;
                                    return NULL;
                            }
                            
                            desc.push_back(static_cast<IntegerValue*>(value)->GetValue());
                            delete value;
                    }

                    ArrayValue * value = static_cast<ArrayValue*>(result->GetVal());
                    result = value->GetElementBox(desc);
            }
            else if(result->GetVal()->GetType()==Map)
            {
                    for(list<Expression*>::iterator i = ExprList->begin(); i!=ExprList->end(); i++)
                    {
                            if(result->GetVal()->GetType()!=Map)
                            {
                                    cerr<<"pupp runtime error: Expect a collection."<<endl;
                                    return NULL;
                            }

                            MapValue * local = static_cast<MapValue*>(result->GetVal());
                            
                            ConstValue * key = (*i)->Calculate(context);

                            result = local->FindByKey(key->toString());

                            if(result==NULL)
                            {
                                    MapValue * value = new MapValue();
                                    ValueBox * vb = new ValueBox(value);
                                    KVValue * kv = new KVValue(key, vb);
                                    local->AddKV(kv);

                                    delete kv;
                                    delete vb;
                                    delete value;
                                    
                                    result = local->FindByKey(key->toString());
                            }
                            
                            delete key;
                    }
            }
            else
            {
                    return NULL;
            }
        }
        
        return result;
}

// Assembly:
subq $0x258, %rsp            # imm = 0x258
movq %rdi, 0x248(%rsp)
movq %rsi, 0x240(%rsp)
movq 0x248(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x240(%rsp), %rcx
movq %rcx, 0xe0(%rsp)
movq 0x58(%rax), %rcx
movq %rcx, 0xe8(%rsp)
movq 0x38(%rax), %rdi
callq 0x310b0
movq 0xe0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq %rax, %rdx
callq 0x14950
movq %rax, 0x238(%rsp)
cmpq $0x0, 0x238(%rsp)
jne 0xe5de
movq 0x67a52(%rip), %rdi     # 0x75ff0
leaq 0x50b82(%rip), %rsi     # 0x5f127
callq 0x62d0
movq 0xd8(%rsp), %rsi
movq %rax, %rdi
addq $0x18, %rsi
callq 0x62a0
movq %rax, %rdi
movq 0x679f0(%rip), %rsi     # 0x75fb8
callq 0x6320
movq $0x0, 0x250(%rsp)
jmp 0xee76
movq 0xd8(%rsp), %rdi
addq $0x40, %rdi
callq 0x114d0
cmpq $0x0, %rax
jne 0xe60f
movq 0x238(%rsp), %rdi
callq 0x30a30
movq %rax, 0x250(%rsp)
jmp 0xee76
movq 0x238(%rsp), %rdi
callq 0x30a30
movq 0xd8(%rsp), %rdi
movq %rax, 0x230(%rsp)
addq $0x40, %rdi
callq 0x11500
movq %rax, 0x228(%rsp)
movq 0xd8(%rsp), %rdi
addq $0x40, %rdi
callq 0x11550
movq %rax, 0x220(%rsp)
leaq 0x228(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x11530
testb $0x1, %al
jne 0xe674
jmp 0xee66
cmpq $0x0, 0x230(%rsp)
jne 0xe690
movq $0x0, 0x250(%rsp)
jmp 0xee76
leaq 0x228(%rsp), %rdi
callq 0x11580
movq (%rax), %rax
movq %rax, 0x218(%rsp)
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, %rdi
callq 0x7870
cmpl $0x7, %eax
jne 0xea6b
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, %rdi
callq 0xa600
movq %rax, 0xd0(%rsp)
movq 0x218(%rsp), %rdi
callq 0x115a0
movq %rax, %rcx
movq 0xd0(%rsp), %rax
cmpq %rcx, %rax
je 0xe7b5
movq 0x678e5(%rip), %rdi     # 0x75ff0
leaq 0x50a3f(%rip), %rsi     # 0x5f151
callq 0x62d0
movq %rax, 0xb8(%rsp)
movq 0x238(%rsp), %rsi
leaq 0x1f8(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x309b0
movq 0xb8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x62a0
movq %rax, 0xc8(%rsp)
jmp 0xe75b
movq 0xc8(%rsp), %rdi
movq 0x6784e(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0xe771
leaq 0x1f8(%rsp), %rdi
callq 0x61e0
movq $0x0, 0x250(%rsp)
jmp 0xee76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x61e0
jmp 0xee86
leaq 0x1d0(%rsp), %rdi
callq 0xadc0
movq 0x218(%rsp), %rdi
callq 0x11290
movq %rax, 0x1c8(%rsp)
movq 0x218(%rsp), %rdi
callq 0x112e0
movq %rax, 0x1c0(%rsp)
leaq 0x1c8(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x112c0
testb $0x1, %al
jne 0xe80a
jmp 0xe9e0
leaq 0x1c8(%rsp), %rdi
callq 0x11310
movq (%rax), %rdi
movq 0x240(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, 0xb0(%rsp)
jmp 0xe835
movq 0xb0(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x7870
movl %eax, 0xac(%rsp)
jmp 0xe85b
movl 0xac(%rsp), %eax
cmpl $0x1, %eax
je 0xe960
movq 0x6777e(%rip), %rdi     # 0x75ff0
leaq 0x50907(%rip), %rsi     # 0x5f180
callq 0x62d0
movq %rax, 0xa0(%rsp)
jmp 0xe888
movq 0x238(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x309b0
jmp 0xe89f
movq 0xa0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x62a0
movq %rax, 0x98(%rsp)
jmp 0xe8be
movq 0x98(%rsp), %rdi
movq 0x676eb(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0xe8d4
leaq 0x198(%rsp), %rdi
callq 0x61e0
movq 0x1b8(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, %rax
je 0xe905
movq 0x90(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x250(%rsp)
movl $0x1, 0x194(%rsp)
jmp 0xea35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xea59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x61e0
jmp 0xea59
movq 0x1b8(%rsp), %rdi
callq 0x7fe0
movq %rax, 0x88(%rsp)
jmp 0xe977
movq 0x88(%rsp), %rax
movq %rax, 0x188(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x115d0
jmp 0xe99e
movq 0x1b8(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, %rax
je 0xe9c2
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xe9c4
leaq 0x1c8(%rsp), %rdi
xorl %esi, %esi
callq 0x11380
movq %rax, 0x180(%rsp)
jmp 0xe7d7
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, 0x78(%rsp)
jmp 0xe9f4
movq 0x78(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0xa070
movq %rax, 0x70(%rsp)
jmp 0xea1d
movq 0x70(%rsp), %rax
movq %rax, 0x230(%rsp)
movl $0x0, 0x194(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0xb090
movl 0x194(%rsp), %eax
testl %eax, %eax
je 0xea54
jmp 0xea4f
jmp 0xee76
jmp 0xee48
leaq 0x1d0(%rsp), %rdi
callq 0xb090
jmp 0xee86
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, %rdi
callq 0x7870
cmpl $0x6, %eax
jne 0xee38
movq 0x218(%rsp), %rdi
callq 0x11290
movq %rax, 0x170(%rsp)
movq 0x218(%rsp), %rdi
callq 0x112e0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x112c0
testb $0x1, %al
jne 0xead1
jmp 0xee36
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, %rdi
callq 0x7870
cmpl $0x6, %eax
je 0xeb1e
movq 0x674fe(%rip), %rdi     # 0x75ff0
leaq 0x506b8(%rip), %rsi     # 0x5f1b1
callq 0x62d0
movq %rax, %rdi
movq 0x674b0(%rip), %rsi     # 0x75fb8
callq 0x6320
movq $0x0, 0x250(%rsp)
jmp 0xee76
movq 0x230(%rsp), %rdi
callq 0x7a20
movq %rax, 0x160(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x11310
movq (%rax), %rdi
movq 0x240(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, 0x158(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x158(%rsp), %rsi
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq 0x138(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq *%rax
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x96c0
movq %rax, 0x68(%rsp)
jmp 0xeb9d
movq 0x68(%rsp), %rax
movq %rax, 0x230(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x61e0
cmpq $0x0, 0x230(%rsp)
jne 0xedfa
movl $0x40, %edi
callq 0x62f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x50(%rsp)
callq 0x91d0
jmp 0xebe2
movq 0x50(%rsp), %rax
movq %rax, 0x130(%rsp)
movl $0x10, %edi
callq 0x62f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x48(%rsp)
movq 0x130(%rsp), %rsi
callq 0x78a0
jmp 0xec13
movq 0x48(%rsp), %rax
movq %rax, 0x128(%rsp)
movl $0x20, %edi
callq 0x62f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq 0x158(%rsp), %rsi
movq 0x128(%rsp), %rdx
callq 0x8e80
jmp 0xec4c
movq 0x40(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x160(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x94d0
movq 0x120(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0xec8c
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x128(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0xecb8
movq 0x30(%rsp), %rdi
callq 0x78f0
movq 0x30(%rsp), %rdi
movl $0x10, %esi
callq 0x6300
movq 0x130(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0xecd6
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x160(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x158(%rsp), %rsi
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq 0x100(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq *%rax
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x96c0
movq %rax, 0x20(%rsp)
jmp 0xed17
movq 0x20(%rsp), %rax
movq %rax, 0x230(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x61e0
jmp 0xedfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x61e0
jmp 0xee86
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
movl $0x40, %esi
callq 0x6300
jmp 0xee86
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
movl $0x10, %esi
callq 0x6300
jmp 0xee86
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
movl $0x20, %esi
callq 0x6300
jmp 0xee86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x61e0
jmp 0xee86
movq 0x158(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xee18
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xee1a
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x11380
movq %rax, 0xf8(%rsp)
jmp 0xea9e
jmp 0xee46
movq $0x0, 0x250(%rsp)
jmp 0xee76
jmp 0xee48
jmp 0xee4a
leaq 0x228(%rsp), %rdi
xorl %esi, %esi
callq 0x11600
movq %rax, 0xf0(%rsp)
jmp 0xe63d
movq 0x230(%rsp), %rax
movq %rax, 0x250(%rsp)
movq 0x250(%rsp), %rax
addq $0x258, %rsp            # imm = 0x258
retq
movq 0x1f0(%rsp), %rdi
callq 0x6550
nopw %cs:(%rax,%rax)