// File: baziotis[P]minijava-cpp/parse.cpp
// Function: parse_expr()
static Expression *parse_expr() {
    Expression *e1 = parse_expr_clause();
    assert(e1);
    if (e1->kind == EXPR::UNDEFINED) {
        goto Lerror;
    }
    if (!is_op(token.kind)) {
        // Handle ArrayLength and MessageSend.
        if (match_token(TOK::DOT)) {
            Expression *expr;
            if (is_token(TOK::ID)) {
                const char *id = token.id;
                next_token();
                if(id == length_) {  // ArrayLength
                    expr = new Expression;
                    expr->loc = loc;
                    expr->kind = EXPR::ARR_LEN;
                    expr->e1 = e1;
                } else {  // MessageSend
                    BinaryExpression *be = new BinaryExpression;
                    be->loc = loc;
                    if (!match_token(TOK::LPAR)) {
                        // Maybe don't advance here because of expect_token* ?
                        expect_token_in_rule(TOK::LPAR, "message send expression");
                        be->kind = EXPR::UNDEFINED;
                    } else {
                        be->kind = EXPR::MSG_SEND;
                        be->e1 = e1;
                        be->msd = new MessageSendData;
                        be->msd->id = id;
                        be->msd->expr_list = parse_expr_list();
                        expect_token_in_rule(TOK::RPAR, "message send expression");
                    }
                    expr = be;
                }
            } else {  // Error
                goto Lerror;
            }
            return expr;
        }
        // Simple Primary Expression (or Clause)
        return e1;
    }
    TOK kind;
    BinaryExpression *bin_expr;
    // Note: This is _not_ a loop; We can't have 1 + 2 + 3. But we
    // can have: (1 + 2) + 3
    if (is_op(token.kind)) {
        bin_expr = new BinaryExpression;
        bin_expr->loc = loc;
        bin_expr->e1 = e1;
        kind = token.kind;
        next_token();
        if (kind == TOK::AND_AND) {
            // AndExpression is the only one that can have as operands
            // `Clause`s.
            bin_expr->kind = EXPR::AND;
            bin_expr->e2 = parse_expr_clause();
            assert(bin_expr->e2);
        } else {
            Expression *e2 = parse_expr_primary();
            assert(e2);
            bin_expr->e2 = e2;
            if (e2->kind == EXPR::UNDEFINED) {
                // No type-checking here! We don't start check what the expression is!
                // But, since we got an undefined, we can issue a better message than
                // "expected expression".
                syntax_error("Expected integer expression as a right-hand-side "
                             "expression for operator `", token_name(kind), "`.");
                goto Lerror;
            }
            switch (kind) {
            case TOK::LT:       bin_expr->kind = EXPR::CMP;       break;
            case TOK::PLUS:     bin_expr->kind = EXPR::PLUS;      break;
            case TOK::MINUS:    bin_expr->kind = EXPR::MINUS;     break;
            case TOK::STAR:     bin_expr->kind = EXPR::TIMES;     break;
            case TOK::LBRACKET:
            {
                bin_expr->kind = EXPR::ARR_LOOK;
                expect_token_in_rule(TOK::RBRACKET, "array lookup expression");
            } break;
            default: assert(0);
            }
        }
    }
    return bin_expr;
Lerror:
    Expression *expr = new Expression;
    expr->loc = loc;
    expr->kind = EXPR::UNDEFINED;
    return expr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
callq 0x9fd0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x99ad
jmp 0x99cc
leaq 0x127b8(%rip), %rdi     # 0x1c16c
leaq 0x116dd(%rip), %rsi     # 0x1b098
movl $0xd9, %edx
leaq 0x116fe(%rip), %rcx     # 0x1b0c5
callq 0x20d0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x99db
jmp 0x9d45
leaq 0x1acc6(%rip), %rax     # 0x246a8
movl (%rax), %edi
callq 0xa040
testb $0x1, %al
jne 0x9b92
movl $0x1d, %edi
callq 0x82c0
testb $0x1, %al
jne 0x9a04
jmp 0x9b85
movl $0x8, %edi
callq 0x8070
testb $0x1, %al
jne 0x9a17
jmp 0x9b73
leaq 0x1ac8a(%rip), %rax     # 0x246a8
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
callq 0x4370
movq -0x20(%rbp), %rax
cmpq 0x1b032(%rip), %rax     # 0x24a68
jne 0x9a71
movl $0x18, %edi
callq 0xae40
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
leaq 0x1ac4f(%rip), %rcx     # 0x246a0
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl $0x7, 0x4(%rax)
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x9b71
movl $0x20, %edi
callq 0xae40
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
leaq 0x1ac16(%rip), %rcx     # 0x246a0
movl (%rcx), %ecx
movl %ecx, (%rax)
movl $0x3, %edi
callq 0x82c0
testb $0x1, %al
jne 0x9abf
movl $0x3, %edi
leaq 0x11636(%rip), %rsi     # 0x1b0de
xorl %edx, %edx
callq 0x8300
movq -0x28(%rbp), %rax
movl $0x0, 0x4(%rax)
jmp 0x9b69
movq -0x28(%rbp), %rax
movl $0x8, 0x4(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x20, %edi
callq 0xae40
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
callq 0xb060
jmp 0x9af1
movq -0x78(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
leaq -0x50(%rbp), %rdi
callq 0xa070
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl $0x19, %edi
leaq 0x1159d(%rip), %rsi     # 0x1b0de
xorl %edx, %edx
callq 0x8300
jmp 0x9b69
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x20, %esi
callq 0x2120
jmp 0x9d82
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x9b78
jmp 0x9d45
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x9d75
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x9d75
leaq 0x1ab0f(%rip), %rax     # 0x246a8
movl (%rax), %edi
callq 0xa040
testb $0x1, %al
jne 0x9ba9
jmp 0x9d3b
movl $0x20, %edi
callq 0xae40
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
leaq 0x1aade(%rip), %rcx     # 0x246a0
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq 0x1aacf(%rip), %rax     # 0x246a8
movl (%rax), %eax
movl %eax, -0x54(%rbp)
callq 0x4370
cmpl $0x1f, -0x54(%rbp)
jne 0x9c35
movq -0x60(%rbp), %rax
movl $0x1, 0x4(%rax)
callq 0x9fd0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x9c11
jmp 0x9c30
leaq 0x114de(%rip), %rdi     # 0x1b0f6
leaq 0x11479(%rip), %rsi     # 0x1b098
movl $0x111, %edx            # imm = 0x111
leaq 0x1149a(%rip), %rcx     # 0x1b0c5
callq 0x20d0
jmp 0x9d39
callq 0xa0f0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x9c47
jmp 0x9c66
leaq 0x12525(%rip), %rdi     # 0x1c173
leaq 0x11443(%rip), %rsi     # 0x1b098
movl $0x114, %edx            # imm = 0x114
leaq 0x11464(%rip), %rcx     # 0x1b0c5
callq 0x20d0
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x9c9f
movl -0x54(%rbp), %edi
callq 0xbb40
movq %rax, %rsi
leaq 0x11475(%rip), %rdi     # 0x1b103
leaq 0xffd5(%rip), %rdx      # 0x19c6a
callq 0x9df0
jmp 0x9d45
movl -0x54(%rbp), %eax
addl $-0x20, %eax
movl %eax, %ecx
movq %rcx, -0x80(%rbp)
subl $0x4, %eax
ja 0x9d18
movq -0x80(%rbp), %rax
leaq 0x10e35(%rip), %rcx     # 0x1aaf0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x60(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x9d37
movq -0x60(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x9d37
movq -0x60(%rbp), %rax
movl $0x4, 0x4(%rax)
jmp 0x9d37
movq -0x60(%rbp), %rax
movl $0x5, 0x4(%rax)
jmp 0x9d37
movq -0x60(%rbp), %rax
movl $0x6, 0x4(%rax)
movl $0x1b, %edi
leaq 0x1143f(%rip), %rsi     # 0x1b14e
xorl %edx, %edx
callq 0x8300
jmp 0x9d37
leaq 0x11c63(%rip), %rdi     # 0x1b982
leaq 0x11372(%rip), %rsi     # 0x1b098
movl $0x128, %edx            # imm = 0x128
leaq 0x11393(%rip), %rcx     # 0x1b0c5
callq 0x20d0
jmp 0x9d39
jmp 0x9d3b
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x9d75
movl $0x18, %edi
callq 0xae40
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
leaq 0x1a942(%rip), %rcx     # 0x246a0
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x21c0
nopl (%rax,%rax)