// File: BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::eval(Token*)
Token* Evaluator::eval(Token* input) {
    if (input->type == TokenType::LIST) {
        List* l = static_cast<List*>(input);
        List* res = new List();
        for (size_t i = 0; i < l->list.size(); i++) {
            if (this->stack_trace.size() < this->max_stack_call) {
                this->stack_trace.push_back(l->list[i]);
                Token* output = this->eval(l->list[i]);
                this->stack_trace.pop_back();
                if (output && output->type != TokenType::ERROR) {
                    res->list.push_back(output);
                } else {
                    res->list.push_back(l->list[i]);
                }
            }

        }

        input = res;
    }


    vector<pair<Token*, function<Token*(Token*)>>> matches_handler;
    for (size_t i = 0; i < this->state.size(); i++) {
        bool compare = compareSignature(input, this->state[i].first);
        if (compare) {
            matches_handler.push_back(this->state[i]);
        }
    }

    Error* err = NULL;
    for (size_t i = 0; i < matches_handler.size(); i++) {
        if (this->stack_trace.size() < this->max_stack_call) {
            this->stack_trace.push_back(input);
            Token* res = matches_handler[i].second(input);
            this->stack_trace.pop_back();

            if (res) {
                if (res->type != TokenType::ERROR) {
                    return res;
                } else {
                    err = static_cast<Error*>(res);
                }
            }
        } else {
            return new Error("stack call limit exceeded", input);
        }
    }

    if (err) {
        return err;
    };

    return input;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x5b28
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x20, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
callq 0x11870
jmp 0x59d1
movq -0xc8(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
callq 0x11a60
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x5b20
movq -0xc0(%rbp), %rdi
addq $0x8, %rdi
callq 0x11a60
movq -0xc0(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jae 0x5b0d
movq -0xc0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rsi
callq 0x11a80
movq -0xd8(%rbp), %rdi
movq %rax, %rsi
callq 0x118b0
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rsi
callq 0x11a80
movq -0xc0(%rbp), %rdi
movq (%rax), %rsi
callq 0x5980
movq -0xc0(%rbp), %rdi
movq %rax, -0x48(%rbp)
addq $0x8, %rdi
callq 0x12020
cmpq $0x0, -0x48(%rbp)
je 0x5adc
movq -0x48(%rbp), %rax
cmpl $0x8, (%rax)
je 0x5adc
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
leaq -0x48(%rbp), %rsi
callq 0x118b0
jmp 0x5b0b
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x20, %esi
callq 0x32c0
jmp 0x5e35
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xe0(%rbp)
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rsi
callq 0x11a80
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0x118b0
jmp 0x5b0d
jmp 0x5b0f
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x59e4
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x11cc0
movq $0x0, -0x68(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, -0xe8(%rbp)
addq $0x20, %rdi
callq 0x12050
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jae 0x5bf6
movq -0xc0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0xf8(%rbp)
addq $0x20, %rdi
movq -0x68(%rbp), %rsi
callq 0x12080
movq -0xf8(%rbp), %rdi
movq (%rax), %rsi
callq 0x9410
movb %al, -0xe9(%rbp)
jmp 0x5b9d
movb -0xe9(%rbp), %al
andb $0x1, %al
movb %al, -0x69(%rbp)
testb $0x1, -0x69(%rbp)
je 0x5be3
movq -0xc0(%rbp), %rdi
addq $0x20, %rdi
movq -0x68(%rbp), %rsi
callq 0x12080
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x120a0
jmp 0x5bd0
jmp 0x5be3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x5e2c
jmp 0x5be5
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x5b39
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x12050
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x5de9
movq -0xc0(%rbp), %rdi
addq $0x8, %rdi
callq 0x11a60
movq -0xc0(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jae 0x5ceb
movq -0xc0(%rbp), %rdi
addq $0x8, %rdi
leaq -0x18(%rbp), %rsi
callq 0x118b0
jmp 0x5c65
movq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x12080
movq %rax, %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x12110
movq %rax, -0x108(%rbp)
jmp 0x5c8b
movq -0xc0(%rbp), %rdi
movq -0x108(%rbp), %rax
movq %rax, -0x88(%rbp)
addq $0x8, %rdi
callq 0x12020
cmpq $0x0, -0x88(%rbp)
je 0x5ce6
movq -0x88(%rbp), %rax
cmpl $0x8, (%rax)
je 0x5cd9
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x5e16
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x5ce6
jmp 0x5dd6
movl $0x30, %edi
callq 0x32b0
movq %rax, -0x110(%rbp)
jmp 0x5cfe
movb $0x1, -0xb2(%rbp)
leaq -0xb1(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x34a0
movq -0x118(%rbp), %rdx
leaq 0x1d6e2(%rip), %rsi     # 0x23408
leaq -0xb0(%rbp), %rdi
callq 0x33b0
jmp 0x5d34
movq -0x110(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0xb0(%rbp), %rsi
callq 0x12150
jmp 0x5d4d
movq -0x110(%rbp), %rax
movb $0x0, -0xb2(%rbp)
movq %rax, -0x8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x31c0
leaq -0xb1(%rbp), %rdi
callq 0x3300
movl $0x1, -0x8c(%rbp)
jmp 0x5e16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x5dac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x31c0
leaq -0xb1(%rbp), %rdi
callq 0x3300
testb $0x1, -0xb2(%rbp)
jne 0x5dc3
jmp 0x5dd4
movq -0x110(%rbp), %rdi
movl $0x30, %esi
callq 0x32c0
jmp 0x5e2c
jmp 0x5dd8
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x5c06
cmpq $0x0, -0x78(%rbp)
je 0x5e04
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x5e16
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x8c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x3e90
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x3e90
movq -0x30(%rbp), %rdi
callq 0x3490
nop