// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbp
movq %rdi, %r14
movq 0x38(%rdi), %rdi
movq 0x58(%r14), %rbx
callq 0x15116
movq %rbp, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xad28
testq %rax, %rax
je 0x9144
movq 0x50(%r14), %rbx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x14db6
movq %rax, %r12
testq %rbx, %rbx
je 0x9186
addq $0x40, %r14
leaq 0x48(%rsp), %r13
movq %r14, %rax
movq %r14, 0x10(%rsp)
movq %rbp, 0x40(%rsp)
movq (%rax), %rax
cmpq %r14, %rax
je 0x9186
testq %r12, %r12
je 0x9183
movq %rax, 0x18(%rsp)
movq 0x10(%rax), %r15
movq %r12, %rdi
callq 0x6d66
movq %rax, %rdi
callq 0x6cbc
movl %eax, %ebx
movq %r12, %rdi
callq 0x6d66
cmpl $0x7, %ebx
movq %r15, (%rsp)
movq %rax, %rdi
jne 0x8f74
callq 0x7f96
cmpq 0x10(%r15), %rax
jne 0x919b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %r15, %r14
movq (%r14), %r14
cmpq %r15, %r14
je 0x90c0
movq 0x10(%r14), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
callq *0x10(%rax)
movq %rax, %r15
movq %rax, %rdi
callq 0x6cbc
cmpl $0x1, %eax
jne 0x90da
movq %r15, %rdi
callq 0x6fe4
movq %rax, 0x48(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0xa146
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq (%rsp), %r15
jmp 0x8f1e
callq 0x6cbc
cmpl $0x6, %eax
jne 0x9183
movq %r15, %r14
movq (%r14), %r14
cmpq %r15, %r14
je 0x90b1
movq %r12, %rdi
callq 0x6d66
movq %rax, %rdi
callq 0x6cbc
cmpl $0x6, %eax
jne 0x9168
movq %r12, %rdi
callq 0x6d66
movq %rax, %r15
movq 0x10(%r14), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
callq *0x10(%rax)
movq %rax, %r13
movq (%rax), %rax
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x78f0
movq %rax, %r12
movq %rbx, %rdi
callq 0x6130
testq %r12, %r12
jne 0x9099
pushq $0x40
popq %rdi
callq 0x6260
movq %rax, %r12
movq %rax, %rdi
callq 0x76dc
pushq $0x10
popq %rdi
callq 0x6260
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x6cce
pushq $0x20
popq %rdi
callq 0x6260
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x7554
movq %r15, %rdi
movq %rbp, %rsi
callq 0x782c
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x6cf2
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x6270
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
movq (%r13), %rax
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x78f0
movq %rax, %r12
movq %rbx, %rdi
callq 0x6130
movq 0x40(%rsp), %rbp
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
leaq 0x48(%rsp), %r13
movq (%rsp), %r15
jmp 0x8f85
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
jmp 0x8eba
movq %r12, %rdi
callq 0x6d66
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7d4e
movq %rax, %r12
jmp 0x9125
movq 0x39f0f(%rip), %rdi     # 0x42ff0
leaq 0x260b1(%rip), %rsi     # 0x2f199
callq 0x6240
movq %rax, %rbx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x14d96
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6210
movq %rax, %rdi
callq 0x6140
movq %r13, %rdi
callq 0x6130
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq (%rsp), %r15
leaq 0x20(%rsp), %rdi
callq 0x8746
cmpq %r15, %r14
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
je 0x8eba
jmp 0x9183
movq 0x39ea5(%rip), %rdi     # 0x42ff0
leaq 0x25fee(%rip), %rsi     # 0x2f140
callq 0x6240
addq $0x18, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x6210
jmp 0x917b
movq 0x39e81(%rip), %rdi     # 0x42ff0
leaq 0x26054(%rip), %rsi     # 0x2f1ca
callq 0x6240
movq %rax, %rdi
callq 0x6140
xorl %r12d, %r12d
movq %r12, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x39e4e(%rip), %rdi     # 0x42ff0
leaq 0x25fc1(%rip), %rsi     # 0x2f16a
callq 0x6240
movq %rax, %rbx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x14d96
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6210
movq %rax, %rdi
callq 0x6140
leaq 0x48(%rsp), %rdi
callq 0x6130
jmp 0x9183
movq %rax, %r14
leaq 0x48(%rsp), %rdi
jmp 0x923c
jmp 0x9245
jmp 0x9245
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x6130
jmp 0x9248
jmp 0x9245
movq %rax, %r14
pushq $0x10
popq %rsi
movq %rbx, %rdi
jmp 0x922a
movq %rax, %r14
pushq $0x40
popq %rsi
movq %r12, %rdi
jmp 0x922a
movq %rax, %r14
leaq 0x68(%rsp), %rdi
jmp 0x923c
movq %rax, %r14
pushq $0x20
popq %rsi
movq %rbp, %rdi
callq 0x6270
jmp 0x9252
movq %rax, %r14
leaq 0x88(%rsp), %rdi
callq 0x6130
jmp 0x9252
jmp 0x9245
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x8746
movq %r14, %rdi
callq 0x6460