// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, -0x38(%rbp)
cmpl $0x1, (%rsi)
jne 0x43b5
pushq $0x20
popq %rdi
callq 0x3200
movq %rax, %r14
movl $0x1, (%rax)
addq $0x8, %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
andq $0x0, 0x18(%r14)
leaq 0x8(%rbx), %rax
movq %rax, -0x68(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rax
subq %rsi, %rax
sarq $0x3, %rax
cmpq %rax, %r13
jae 0x43ba
movq 0x10(%rbx), %rax
subq 0x8(%rbx), %rax
sarq $0x3, %rax
movl (%rbx), %ecx
cmpq %rcx, %rax
jae 0x43ac
addq %r12, %rsi
movq -0x68(%rbp), %rdi
callq 0x74b0
movq 0x8(%r15), %rax
movq (%rax,%r12), %rsi
movq %rbx, %rdi
callq 0x42ea
movq %rax, -0x60(%rbp)
addq $-0x8, 0x10(%rbx)
testq %rax, %rax
je 0x439c
cmpl $0x8, (%rax)
leaq -0x60(%rbp), %rsi
jne 0x43a3
movq 0x8(%r15), %rsi
addq %r12, %rsi
movq -0x40(%rbp), %rdi
callq 0x74b0
incq %r13
addq $0x8, %r12
jmp 0x4342
movq %r15, %r14
jmp 0x43be
movq %r14, -0x38(%rbp)
xorps %xmm0, %xmm0
leaq -0x60(%rbp), %r15
andq $0x0, 0x10(%r15)
movaps %xmm0, (%r15)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%rbx), %rcx
movq 0x28(%rbx), %rax
subq %rcx, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %rax, %r13
jae 0x4414
movq (%rcx,%r12), %rsi
movq %r14, %rdi
callq 0x5adb
testb %al, %al
je 0x440b
movq 0x20(%rbx), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x7644
incq %r13
addq $0x28, %r12
jmp 0x43d4
leaq 0x8(%rbx), %rax
movq %rax, -0x40(%rbp)
pushq $0x8
popq %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %rax, %r13
jae 0x448a
movq 0x10(%rbx), %rax
subq 0x8(%rbx), %rax
sarq $0x3, %rax
movl (%rbx), %ecx
cmpq %rcx, %rax
jae 0x4498
movq -0x40(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x74b0
movq -0x60(%rbp), %rdi
addq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x7680
movq %rax, %r15
addq $-0x8, 0x10(%rbx)
testq %rax, %rax
je 0x4481
cmpl $0x8, (%r15)
movq %r15, %r12
jne 0x44d9
incq %r13
addq $0x28, %r14
jmp 0x4426
testq %r12, %r12
jne 0x4493
movq -0x38(%rbp), %r12
movq %r12, %r15
jmp 0x44d9
pushq $0x30
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x7e4e(%rip), %rsi      # 0xc2f8
leaq -0x88(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x3100
movq -0x38(%rbp), %rdx
leaq -0x88(%rbp), %rsi
movq %r15, %rdi
callq 0x76a6
leaq -0x88(%rbp), %rdi
callq 0x3140
leaq -0x60(%rbp), %rdi
callq 0x38c4
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x3140
jmp 0x4508
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r15, %rdi
callq 0x3210
jmp 0x451e
jmp 0x451b
jmp 0x451b
jmp 0x451b
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x38c4
movq %rbx, %rdi
callq 0x3320
nop