// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x8
popq %rdi
callq 0x3200
movabsq $0x400000005, %rcx      # imm = 0x400000005
movq %rcx, (%rax)
movq %rax, -0x48(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x7d15(%rip), %rsi      # 0xc287
leaq -0x88(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x3100
leaq -0x88(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x88(%rbp), %rdi
callq 0x3140
movq %r15, -0x40(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x7cde(%rip), %rsi      # 0xc291
leaq -0x68(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x3100
leaq -0x68(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x68(%rbp), %rdi
callq 0x3140
leaq -0x38(%rbp), %r12
movq %r15, (%r12)
pushq $0x20
popq %rdi
callq 0x3200
movq %rax, %r15
movl $0x1, (%rax)
leaq 0x8(%rax), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
andq $0x0, 0x18(%rax)
leaq -0x48(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
leaq -0x40(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
movq %r13, %rdi
movq %r12, %rsi
callq 0x74b0
movq %r15, (%rbx)
andq $0x0, 0x10(%rbx)
movq %r14, 0x8(%rbx)
leaq 0x1bcc(%rip), %rax      # 0x6200
movq %rax, 0x20(%rbx)
leaq 0x1c6d(%rip), %rax      # 0x62ac
movq %rax, 0x18(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0x466a
jmp 0x4671
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x3140
jmp 0x4674
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r15, %rdi
callq 0x3210
movq %rbx, %rdi
callq 0x3320
nop