// File: BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::eq()
pair<Token*, function<Token*(Token*)>> Evaluator::eq() {
    Token* op = new BinaryOperator(BinaryOperatorType::EQ);
    Token* var1 = new Variable("L");
    Token* var2 = new Variable("R");
    List* signature = new List();
    signature->list.push_back(op);
    signature->list.push_back(var1);
    signature->list.push_back(var2);
    Token* token = signature;
    auto res = make_pair(token, [this](Token* input) {
            List* list = static_cast<List*>(input);

            Token* a_token = this->eval(list->list[1]);
            Token* b_token = this->eval(list->list[2]);

            if (a_token->type != b_token->type) {
                if (a_token->type == TokenType::VARIABLE
                        || b_token->type == TokenType::VARIABLE) {
                    Token* t = new Error("cannot check equality of variable", input);
                    return t;
                }
            }

            if (a_token->type == TokenType::NUMBER) {
                Number* a = static_cast<Number*>(a_token);
                Number* b = static_cast<Number*>(b_token);

                Token* t = new Boolean(a->value == b->value);
                return t;
            }

            if (a_token->type == TokenType::ATOM) {
                Atom* a = static_cast<Atom*>(a_token);
                Atom* b = static_cast<Atom*>(b_token);

                Token* t = new Boolean(a->str == b->str);
                return t;
            }

            if (a_token->type == TokenType::BOOLEAN) {
                Boolean* a = static_cast<Boolean*>(a_token);
                Boolean* b = static_cast<Boolean*>(b_token);

                Token* t = new Boolean(a->value == b->value);
                return t;
            }

            Token* t = new Error("unsupported equality", input);
            return t;
    });

    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 $0x7, %esi
callq 0x11920
jmp 0x4f52
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 0x1e3f7(%rip), %rsi     # 0x23387
leaq -0x50(%rbp), %rdi
callq 0x33b0
jmp 0x4f9b
movq -0xe0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x11830
jmp 0x4fad
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 0x1e5b9(%rip), %rsi     # 0x235c0
leaq -0x80(%rbp), %rdi
callq 0x33b0
jmp 0x5012
movq -0xf0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x11830
jmp 0x5024
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 0x5069
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 0x51f0
movq -0xd0(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq %rdx, -0xa0(%rbp)
leaq -0xa8(%rbp), %rsi
callq 0x5220
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 0x51e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x5158
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 0x5169
jmp 0x517a
movq -0xe0(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x51e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x519f
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 0x51b6
jmp 0x51c7
movq -0xf0(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x51e6
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