// File: /workspace/github_cpp_makefile_star2_O1/BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::def()
pair<Token*, function<Token*(Token*)>> Evaluator::def() {
    Token* op = new BinaryOperator(BinaryOperatorType::DEF);
    Token* condition = new Variable("CONDITION");
    Token* result = new Variable("RESULT");
    List* signature = new List();
    signature->list.push_back(op);
    signature->list.push_back(condition);
    signature->list.push_back(result);
    Token* token = signature;
    auto res = make_pair(token, [this](Token* input) {
            List* list = static_cast<List*>(input);

            Token* condition_token = list->list[1];
            Token* result_token = list->list[2];

            auto new_rule = make_pair(condition_token, [condition_token, result_token, this](Token* raw_input) {
                    vector<pair<Variable*, Token*>> arguments;

                    Token* input = NULL;
                    if (raw_input->type != TokenType::LIST) {
                        input = raw_input;
                    } else {
                        List* input_list = static_cast<List*>(raw_input);
                        List* res = new List();

                        for (size_t i = 0; i < input_list->list.size(); i++) {
                            res->list.push_back(this->eval(input_list->list[i]));
                        }

                        input = res;
                    }


                    if (condition_token->type == TokenType::VARIABLE) {
                        if (input->type == TokenType::VARIABLE) {
                            Token* t = new Error("cannot subtitute variable with variable", input);
                            return t;
                        }
                        Variable* var = static_cast<Variable*>(condition_token);
                        arguments.push_back(make_pair(var, input));
                    } else if (condition_token->type == TokenType::LIST) {
                        List* params = static_cast<List*>(condition_token);
                        List* args = static_cast<List*>(input);

                        for (size_t i = 0; i < params->list.size(); i++) {
                            if (params->list[i]->type == TokenType::VARIABLE) {
                                if (args->list[i]->type == TokenType::VARIABLE) {
                                    Token* t = new Error("cannot subtitute variable with variable", input);
                                    return t;
                                }
                                Variable* var = static_cast<Variable*>(params->list[i]);
                                arguments.push_back(make_pair(var, args->list[i]));
                            }
                        }
                    } else {
                        // if there are no variable at all then there's nothing
                        // to subtitute
                        return result_token;
                    }

                    Token* result_list = subtitute_variable(result_token, arguments);
                    Token* res = this->eval(result_list);
                    return res;
            });

            this->state.push_back(new_rule);

            Token* res = new Boolean(true);
            return res;
    });

    return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl $0x8, %edi
callq 0x31b0
movabsq $0x400000005, %rcx      # imm = 0x400000005
movq %rcx, (%rax)
movq %rax, -0x38(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x68(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0xa6b0(%rip), %rsi      # 0xf30b
leaq 0xa6b2(%rip), %rdx      # 0xf314
leaq -0x78(%rbp), %rdi
callq 0x3ba8
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x4ca6
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x30(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x48(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0xa64e(%rip), %rsi      # 0xf315
leaq 0xa64d(%rip), %rdx      # 0xf31b
leaq -0x58(%rbp), %rdi
callq 0x3ba8
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x4d12
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x28(%rbp)
movl $0x20, %edi
callq 0x31b0
movq %rax, %r15
movl $0x1, (%rax)
leaq 0x8(%rax), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x4d53
movq -0x38(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x4d5f
leaq -0x38(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x4d77
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x4d83
leaq -0x30(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x4d9b
movq -0x28(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x4da7
leaq -0x28(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq %r15, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movups %xmm0, 0x18(%rbx)
movq %r14, 0x8(%rbx)
leaq 0x2ec2(%rip), %rax      # 0x7c82
movq %rax, 0x20(%rbx)
leaq 0x2f8f(%rip), %rax      # 0x7d5a
movq %rax, 0x18(%rbx)
movq %rbx, %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x4e10
movq -0x48(%rbp), %rsi
jmp 0x4e03
jmp 0x4e0d
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x4e10
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x4e10
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop