// File: marxn[P]pupp/src/parser/expression.cpp
// Function: LambdaExpression::Calculate(NodeContext*)
ConstValue * LambdaExpression::Calculate(NodeContext * context)
{
        list<ClosureVarDesc*> * CopyVars = this->FuncNode->GetCopyList();
        list<Variable *> * cv = new list<Variable *>;

        if(CopyVars!=NULL)
        {
            list<ClosureVarDesc*>::iterator i;
            for(i = CopyVars->begin(); i != CopyVars->end(); i++)
            {
                string varname = (*i)->GetVarName();
                unsigned long layer  = (*i)->GetSrcLayer();
                VariableDef * srcdef = (*i)->GetSrcDef();
                
                Variable * origin = context->GetVariable(layer, srcdef->GetVarIndex());
                if(origin==NULL)
                {
                    cerr<<"pupp runtime error: cannot find variable: "<<varname<<" in the context"<<endl;
                    return NULL;
                }

                if((*i)->IsRef())
                {
                    cv->push_back(origin->CreateVarRef());
                }
                else
                {
                    if(origin->GetVarType()==Func)
                    {
                        ValueBox * vbox = origin->GetVBox();
                        if(vbox==NULL || vbox->GetVal()->GetType()==Null)
                        {
                            return NULL;
                        }
                    }

                    Variable * cl_var = new Variable(origin->GetVarName(), origin->GetVarType());
                    cl_var->SetSource(origin->GetSource());
                    cl_var->SetRef(origin->GetValue());
                    cv->push_back(cl_var);
                }
            }
        }
        FuncValue * ret = new FuncValue(this->FuncNode, cv);

        return ret;
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1a8(%rsp)
movq %rsi, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x18(%rax), %rdi
callq 0x13870
movq %rax, 0x198(%rsp)
movl $0x18, %edi
callq 0x62f0
movq %rax, %rdi
movq %rdi, 0xd0(%rsp)
callq 0x11820
movq 0xd0(%rsp), %rax
movq %rax, 0x190(%rsp)
cmpq $0x0, 0x198(%rsp)
je 0x1071e
leaq 0x188(%rsp), %rdi
callq 0x11830
movq 0x198(%rsp), %rdi
callq 0x11850
movq %rax, 0x180(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x198(%rsp), %rdi
callq 0x118a0
movq %rax, 0x178(%rsp)
leaq 0x188(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x11880
testb $0x1, %al
jne 0x102d6
jmp 0x1071c
leaq 0x188(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0x118d0
movq (%rax), %rsi
leaq 0x158(%rsp), %rdi
callq 0x13640
movq 0xb8(%rsp), %rdi
callq 0x118d0
movq (%rax), %rdi
callq 0x136e0
movq %rax, 0xc0(%rsp)
jmp 0x1031a
movq 0xc0(%rsp), %rax
movq %rax, 0x150(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x118d0
movq (%rax), %rdi
callq 0x136f0
movq %rax, 0xb0(%rsp)
jmp 0x10349
movq 0xb0(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x138(%rsp), %rdi
callq 0x310b0
movq %rax, 0xa8(%rsp)
jmp 0x10390
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rdi
callq 0x14950
movq %rax, 0x90(%rsp)
jmp 0x103b7
movq 0x90(%rsp), %rax
movq %rax, 0x130(%rsp)
cmpq $0x0, 0x130(%rsp)
jne 0x10475
movq 0x65c13(%rip), %rdi     # 0x75ff0
leaq 0x4f020(%rip), %rsi     # 0x5f404
callq 0x62d0
movq %rax, 0x88(%rsp)
jmp 0x103f3
movq 0x88(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x62a0
movq %rax, 0x80(%rsp)
jmp 0x10412
movq 0x80(%rsp), %rdi
leaq 0x4f00e(%rip), %rsi     # 0x5f42f
callq 0x62d0
movq %rax, 0x78(%rsp)
jmp 0x1042d
movq 0x78(%rsp), %rdi
movq 0x65b7f(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x10440
movq $0x0, 0x1b0(%rsp)
movl $0x1, 0x12c(%rsp)
jmp 0x106cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x1070a
leaq 0x188(%rsp), %rdi
callq 0x118d0
movq (%rax), %rdi
callq 0x13670
movb %al, 0x77(%rsp)
jmp 0x10490
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0x1049a
jmp 0x104e1
movq 0x190(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x130(%rsp), %rdi
callq 0x30d80
movq %rax, 0x68(%rsp)
jmp 0x104bb
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rax
movq %rax, 0x120(%rsp)
leaq 0x120(%rsp), %rsi
callq 0x11900
jmp 0x104dc
jmp 0x106c2
movq 0x130(%rsp), %rdi
callq 0x30cb0
movl %eax, 0x5c(%rsp)
jmp 0x104f4
movl 0x5c(%rsp), %eax
cmpl $0x8, %eax
jne 0x10574
movq 0x130(%rsp), %rdi
callq 0x30a30
movq %rax, 0x50(%rsp)
jmp 0x10511
movq 0x50(%rsp), %rax
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
je 0x10556
movq 0x118(%rsp), %rdi
callq 0x7a20
movq %rax, 0x48(%rsp)
jmp 0x1053d
movq 0x48(%rsp), %rdi
callq 0x7870
movl %eax, 0x44(%rsp)
jmp 0x1054d
movl 0x44(%rsp), %eax
cmpl $0x0, %eax
jne 0x10572
movq $0x0, 0x1b0(%rsp)
movl $0x1, 0x12c(%rsp)
jmp 0x106cd
jmp 0x10574
movl $0x38, %edi
callq 0x62f0
movq %rax, 0x38(%rsp)
jmp 0x10585
movb $0x1, 0xef(%rsp)
movq 0x130(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x309b0
jmp 0x105a4
movq 0x130(%rsp), %rdi
callq 0x30cb0
movl %eax, 0x34(%rsp)
jmp 0x105b7
movl 0x34(%rsp), %edx
movq 0x38(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x308d0
jmp 0x105cf
movb $0x0, 0xef(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x61e0
movq 0x38(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x130(%rsp), %rdi
callq 0x30a00
movq %rax, 0x28(%rsp)
jmp 0x10612
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x309e0
jmp 0x10623
movq 0x110(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x130(%rsp), %rdi
callq 0x30bc0
movq %rax, 0x18(%rsp)
jmp 0x10644
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x30a40
jmp 0x10655
movq 0x190(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x11940
jmp 0x1066c
jmp 0x106c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x106a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x61e0
testb $0x1, 0xef(%rsp)
jne 0x106b1
jmp 0x106c0
movq 0x38(%rsp), %rdi
movl $0x38, %esi
callq 0x6300
jmp 0x1070a
movl $0x0, 0x12c(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x61e0
movl 0x12c(%rsp), %eax
testl %eax, %eax
je 0x106ec
jmp 0x106e7
jmp 0x10792
jmp 0x106ee
leaq 0x188(%rsp), %rdi
xorl %esi, %esi
callq 0x11980
movq %rax, 0xe0(%rsp)
jmp 0x102a3
leaq 0x158(%rsp), %rdi
callq 0x61e0
jmp 0x107a2
jmp 0x1071e
movl $0x20, %edi
callq 0x62f0
movq %rax, %rdi
movq 0xc8(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x8(%rsp)
movq 0x18(%rax), %rsi
movq 0x190(%rsp), %rdx
callq 0x310f0
jmp 0x1074e
movq 0x8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x1b0(%rsp)
jmp 0x10792
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x20, %esi
callq 0x6300
jmp 0x107a2
movq 0x1b0(%rsp), %rax
addq $0x1b8, %rsp            # imm = 0x1B8
retq
movq 0x148(%rsp), %rdi
callq 0x6550
nop