// File: /workspace/github_cpp_makefile_star2_O2/CHEN-Zhihan[P]Compiler/Node.cpp
// Function: ExprNode::evaluate()
void ExprNode::evaluate() {
    array<int, 2> stack;
    if (oper == STRING) {
        cerr << "Invalid operation on string" << endl;
        exit(-1);
    }
    switch (oper) {
        case INTEGER: {
            value = dynamic_pointer_cast<IntNode>(op[0])->getValue();
            known = true;
            break;
        }
        case CHARACTER: {
            value = dynamic_pointer_cast<CharNode>(op[0])->getValue();
            known = true;            
            break;
        }
        case BOOL: {
            value = dynamic_pointer_cast<BoolNode>(op[0])->getValue();
            known = true;
            break;
        }
        case VAR: {
            known = false;
            return;
        }
        default: {
            auto left = dynamic_pointer_cast<ExprNode>(op[0]);
            auto right = dynamic_pointer_cast<ExprNode>(op[1]);
            left->evaluate();
            right->evaluate();
            if (not left->known or not right->known) {
                known = false;
                return;
            }
            known = true;
            stack[0] = left->value;
            stack[1] = right->value;
            switch (oper) {
                case '+': {
                    EVAL(+);
                }
                case '-': {
                    EVAL(-);
                }
                case '*': {
                    EVAL(*);
                }
                case '/': {
                    if (stack[1] == 0) {
                        cerr << "Division by 0" << endl;
                        exit(-1);
                    }
                    EVAL(/);
                }
                case '%': {
                    if (stack[1] == 0) {
                        cerr << "Division by 0" << endl;
                        exit(-1);
                    }
                    EVAL(%);
                } case UMINUS: {
                    value = -stack[0];
                    break;
                } case '>': {
                    EVAL(>);
                } case '<': {
                    EVAL(<);
                } case GE: {
                    EVAL(>=);
                } case LE: {
                    EVAL(<=);
                } case EQ: {
                    EVAL(==);
                } case NE: {
                    EVAL(!=);
                } case AND: {
                    EVAL(&&);
                } case OR: {
                    EVAL(||);
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movl 0x8(%rdi), %ecx
leal -0x102(%rcx), %eax
cmpl $0x3, %eax
ja 0xc2d8
leaq 0x73c5(%rip), %rcx      # 0x1367c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x18(%rbx), %rsi
leaq -0x20(%rbp), %r14
movq %r14, %rdi
callq 0xecac
movq (%r14), %rax
movl 0x8(%rax), %eax
jmp 0xc31a
cmpl $-0x5, %ecx
jne 0xc32f
movb $0x0, 0x14(%rbx)
jmp 0xc452
leaq 0x18(%rbx), %rsi
leaq -0x20(%rbp), %r14
movq %r14, %rdi
callq 0xed01
movq (%r14), %rax
movq 0x8(%rax), %rax
movsbl 0x1(%rax), %eax
jmp 0xc31a
leaq 0x18(%rbx), %rsi
leaq -0x20(%rbp), %r14
movq %r14, %rdi
callq 0xed56
movq (%r14), %rax
movzbl 0x8(%rax), %eax
movl %eax, 0x10(%rbx)
leaq -0x18(%rbp), %rdi
callq 0x6b76
movb $0x1, 0x14(%rbx)
jmp 0xc452
leaq 0x18(%rbx), %rsi
leaq -0x20(%rbp), %r14
movq %r14, %rdi
callq 0xedab
leaq 0x28(%rbx), %rsi
leaq -0x30(%rbp), %rdi
callq 0xedab
movq (%r14), %rdi
callq 0xc294
movq -0x30(%rbp), %rdi
callq 0xc294
movq -0x20(%rbp), %rax
cmpb $0x1, 0x14(%rax)
jne 0xc3ad
movq -0x30(%rbp), %rcx
cmpb $0x0, 0x14(%rcx)
je 0xc3ad
movb $0x1, 0x14(%rbx)
movl 0x10(%rax), %eax
movl 0x10(%rcx), %ecx
movl 0x8(%rbx), %edx
leal -0x118(%rdx), %esi
cmpl $0x8, %esi
ja 0xc3b6
leaq 0x7328(%rip), %rdx      # 0x136b8
movslq (%rdx,%rsi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
testl %eax, %eax
setne %al
testl %ecx, %ecx
setne %cl
andb %al, %cl
movzbl %cl, %ecx
jmp 0xc43d
movb $0x0, 0x14(%rbx)
jmp 0xc440
leal -0x25(%rdx), %esi
cmpl $0xa, %esi
ja 0xc3db
leaq 0x72c7(%rip), %rdx      # 0x1368c
movslq (%rdx,%rsi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
testl %ecx, %ecx
je 0xc47e
cltd
idivl %ecx
jmp 0xc43b
cmpl $0x3c, %edx
je 0xc434
cmpl $0x3e, %edx
jne 0xc440
xorl %edx, %edx
cmpl %ecx, %eax
setg %dl
jmp 0xc43b
negl %eax
jmp 0xc430
xorl %edx, %edx
cmpl %ecx, %eax
setge %dl
jmp 0xc43b
xorl %edx, %edx
cmpl %ecx, %eax
sete %dl
jmp 0xc43b
xorl %edx, %edx
cmpl %ecx, %eax
jmp 0xc40e
xorl %edx, %edx
orl %ecx, %eax
setne %dl
jmp 0xc43b
xorl %edx, %edx
cmpl %ecx, %eax
setle %dl
jmp 0xc43b
testl %ecx, %ecx
je 0xc49b
cltd
idivl %ecx
jmp 0xc430
imull %eax, %ecx
jmp 0xc43d
addl %eax, %ecx
jmp 0xc43d
subl %ecx, %eax
movl %eax, %ecx
jmp 0xc43d
xorl %edx, %edx
cmpl %ecx, %eax
setl %dl
movl %edx, %ecx
movl %ecx, 0x10(%rbx)
leaq -0x28(%rbp), %rdi
callq 0x6b76
leaq -0x18(%rbp), %rdi
callq 0x6b76
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movq 0x10b86(%rip), %rdi     # 0x1cfe8
leaq 0x73c4(%rip), %rsi      # 0x1382d
callq 0x31e0
movq %rax, %rdi
callq 0x3130
pushq $-0x1
popq %rdi
callq 0x3260
movq 0x10b63(%rip), %rdi     # 0x1cfe8
leaq 0x73bd(%rip), %rsi      # 0x13849
callq 0x31e0
movq %rax, %rdi
callq 0x3130
jmp 0xc476
movq 0x10b46(%rip), %rdi     # 0x1cfe8
leaq 0x73a0(%rip), %rsi      # 0x13849
callq 0x31e0
movq %rax, %rdi
callq 0x3130
jmp 0xc476
movq %rax, %rbx
leaq -0x28(%rbp), %rdi
callq 0x6b76
leaq -0x18(%rbp), %rdi
callq 0x6b76
movq %rbx, %rdi
callq 0x3390
nop