// File: baziotis[P]minijava-cpp/parse.cpp
// Function: parse_method()
static MethodDeclaration *parse_method() {
    MethodDeclaration *method_decl = new MethodDeclaration;
    method_decl->loc = loc;
    Typespec tyspec;
    tyspec.kind = TYSPEC::UNDEFINED;
    Expression *expr = NULL;
    if (!match_token(TOK::PUBLIC)) {
        syntax_error("Expected start of type specifier"
                     " (`int` or `boolean`), found ", token);
        goto Lerror;
    }
    tyspec = parse_typespec();
    method_decl->typespec = tyspec;
    if (tyspec.kind == TYSPEC::UNDEFINED) {
        goto Lerror;
    }
    method_decl->id = token.id;
    if (!expect_token_in_rule(TOK::ID, "method declaration")) {
        goto Lerror;
    }
    if (!match_token(TOK::LPAR)) {
        syntax_error("Expected `(` before parameter list in method declaration with id: ",
                     method_decl->id);
    }
    // Parse FormalParameters
    while (starts_local()) {
        parse_local_and_push(&method_decl->params);
        match_token(TOK::COMMA);
    }
    if (!match_token(TOK::RPAR)) {
        syntax_error("Expected `)` after parameter list in method declaration with id: ",
                     method_decl->id);
    }
    if (!match_token(TOK::LBRACE)) {
        syntax_error("Expected `{` list in method declaration with id: ",
                     method_decl->id);
    }
    // Parse VarDeclarations
    while (starts_local()) {
        parse_local_and_push(&method_decl->vars);
        if (!expect_token_in_rule(TOK::SEMI, "var declaration")) {
            skip_tokens(TOK::SEMI);
        }
    }
    while (starts_statement(token.kind)) {
        parse_stmt_and_push(&method_decl->stmts);
    }
    if (!match_token(TOK::RETURN)) {
        syntax_error("Expected `return` in the end of method with id: ",
                     method_decl->id);
    }
    expr = parse_expr();
    if (expr->is_undefined()) {
        syntax_error("Invalid return expression in method with id: ",
                     method_decl->id);
        // We don't want to make this undefined so that this method
        // goes to typecheck, so that its statements are at least
        // type-checked. That means we need to handle the case
        // where the ret expr is undefined _there_.
    }
    method_decl->ret = expr;
    if (!match_token(TOK::SEMI)) {
        syntax_error("Expected `;` after `return` expression of"
                     " method declaration with id: `", method_decl->id,
                     "` found `", token, "`");
        skip_tokens(TOK::SEMI);
    }
    if (!match_token(TOK::RBRACE)) {
        syntax_error("Expected `}` in the end of method with id: ",
                     method_decl->id, ", found `", token, "`");
    }
    return method_decl;
Lerror:
    method_decl->make_undefined();
    return method_decl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x70, %edi
callq 0xabf0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
callq 0xb520
jmp 0xa5d6
movq -0x80(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
leaq 0x1a0b7(%rip), %rcx     # 0x246a0
movl (%rcx), %ecx
movl %ecx, (%rax)
movl $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0xc, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa657
leaq 0x1a097(%rip), %rax     # 0x246a8
movq (%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x48(%rbp), %esi
movq -0x40(%rbp), %rdx
leaq 0x107a7(%rip), %rdi     # 0x1add5
callq 0x8fe0
jmp 0xa8c1
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x70, %esi
callq 0x2120
jmp 0xa8df
callq 0x8e40
movl %eax, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x10(%rax)
cmpl $0x0, -0x30(%rbp)
jne 0xa692
jmp 0xa8c1
leaq 0x1a00f(%rip), %rax     # 0x246a8
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x8, %edi
leaq 0x10546(%rip), %rsi     # 0x1abf7
xorl %edx, %edx
callq 0x8300
testb $0x1, %al
jne 0xa6c1
jmp 0xa8c1
movl $0x3, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa6e3
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x10c09(%rip), %rdi     # 0x1b2e7
callq 0xa4b0
jmp 0xa6e5
callq 0x8560
testb $0x1, %al
jne 0xa6f0
jmp 0xa709
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x85d0
movl $0x1c, %edi
callq 0x82c0
jmp 0xa6e5
movl $0x19, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa72b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x10c04(%rip), %rdi     # 0x1b32a
callq 0xa4b0
movl $0x7, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa74d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x10c24(%rip), %rdi     # 0x1b36c
callq 0xa4b0
jmp 0xa74f
callq 0x8560
testb $0x1, %al
jne 0xa75a
jmp 0xa78f
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x85d0
movl $0x1e, %edi
leaq 0x10507(%rip), %rsi     # 0x1ac7a
xorl %edx, %edx
callq 0x8300
testb $0x1, %al
jne 0xa78d
movl $0x1e, %edi
movl $0x1, %esi
callq 0x8450
jmp 0xa74f
jmp 0xa791
leaq 0x19f10(%rip), %rax     # 0x246a8
movl (%rax), %edi
callq 0x8610
testb $0x1, %al
jne 0xa7a5
jmp 0xa7b4
movq -0x10(%rbp), %rdi
addq $0x50, %rdi
callq 0x8640
jmp 0xa791
movl $0xe, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa7d6
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x10bcd(%rip), %rdi     # 0x1b39e
callq 0xa4b0
callq 0x9990
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xb580
testb $0x1, %al
jne 0xa7ee
jmp 0xa802
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x10bd2(%rip), %rdi     # 0x1b3cf
callq 0xa4b0
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movl $0x1e, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa86a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x19e7d(%rip), %rax     # 0x246a8
movq (%rax), %rcx
movq %rcx, -0x68(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movl -0x68(%rbp), %ecx
movq -0x60(%rbp), %r8
leaq 0x10bb5(%rip), %rdi     # 0x1b3fd
leaq 0x10bf6(%rip), %rdx     # 0x1b445
leaq 0x104d7(%rip), %r9      # 0x1ad2d
callq 0xa8f0
movl $0x1e, %edi
movl $0x1, %esi
callq 0x8450
movl $0x1a, %edi
callq 0x82c0
testb $0x1, %al
jne 0xa8b7
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x19e21(%rip), %rax     # 0x246a8
movq (%rax), %rcx
movq %rcx, -0x78(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x78(%rbp), %ecx
movq -0x70(%rbp), %r8
leaq 0x10bab(%rip), %rdi     # 0x1b44f
leaq 0x104f5(%rip), %rdx     # 0x1ada0
leaq 0x1047b(%rip), %r9      # 0x1ad2d
callq 0xa8f0
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa8d2
movq -0x10(%rbp), %rdi
callq 0xb5a0
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x21c0
nopl (%rax,%rax)