// File: 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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xd0(%rbp)
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl $0x8, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
movl $0x4, %esi
callq 0x11920
jmp 0x5e92
movq -0xb8(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0xe0(%rbp)
movb $0x1, -0x52(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x34a0
movq -0xd8(%rbp), %rdx
leaq 0x1d4c7(%rip), %rsi     # 0x23397
leaq -0x50(%rbp), %rdi
callq 0x33b0
jmp 0x5edb
movq -0xe0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x11830
jmp 0x5eed
movb $0x0, -0x52(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x31c0
leaq -0x51(%rbp), %rdi
callq 0x3300
movq -0xe0(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0xf0(%rbp)
movb $0x1, -0x82(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x34a0
movq -0xe8(%rbp), %rdx
leaq 0x1d45a(%rip), %rsi     # 0x233a1
leaq -0x80(%rbp), %rdi
callq 0x33b0
jmp 0x5f52
movq -0xf0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x11830
jmp 0x5f64
movb $0x0, -0x82(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x31c0
leaq -0x81(%rbp), %rdi
callq 0x3300
movq -0xf0(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x20, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
callq 0x11870
jmp 0x5fa9
movq -0xf8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
leaq -0x18(%rbp), %rsi
callq 0x118b0
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
leaq -0x30(%rbp), %rsi
callq 0x118b0
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
leaq -0x60(%rbp), %rsi
callq 0x118b0
movq -0xc0(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6130
movq -0xd0(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq %rdx, -0xa0(%rbp)
leaq -0xa8(%rbp), %rsi
callq 0x6160
movq -0xc8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x8, %esi
callq 0x32c0
jmp 0x6126
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x6098
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x31c0
leaq -0x51(%rbp), %rdi
callq 0x3300
testb $0x1, -0x52(%rbp)
jne 0x60a9
jmp 0x60ba
movq -0xe0(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x6126
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x60df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x31c0
leaq -0x81(%rbp), %rdi
callq 0x3300
testb $0x1, -0x82(%rbp)
jne 0x60f6
jmp 0x6107
movq -0xf0(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x6126
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x20, %esi
callq 0x32c0
movq -0x20(%rbp), %rdi
callq 0x3490
nop