// File: /workspace/github_c_makefile_star2_O2/mahrr[P]raven/src/compiler.c
// Function: pattern
static void
pattern(Parser *parser, Pattern_Context *context, bool subvalue) {
    Debug_Log(parser);

    if (parser->panic_mode) {
        return;
    }

    if (context->cases_count == PATTERN_LIMIT) {
        error_limit(parser, "pattern elements", PATTERN_LIMIT);
        return;
    }

    switch (parser->current.type)
    {
    case TOKEN_IDENTIFIER: {
        Token token = parser->current;

        // Wildcard Pattern (No Binding)
        if (token.length == 1 && token.lexeme[0] == '_') {
            emit_byte(parser, OP_POP); // discard the match value
            advance(parser);           // consume the wildcard '_'
            break;
        }

        // Identifier Pattern
        uint8_t index = variable(parser, "");
        define_variable(parser, index);
        context->bindings_count += 1;
        break;
    }
    case TOKEN_NIL: {
        advance(parser);
        emit_byte(parser, OP_PUSH_NIL);
        emit_bytes(parser, OP_EQ, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);
        break;
    }
    case TOKEN_TRUE: {
        advance(parser);
        emit_byte(parser, OP_PUSH_TRUE);
        emit_bytes(parser, OP_EQ, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);
        break;
    }
    case TOKEN_FALSE: {
        advance(parser);
        emit_byte(parser, OP_PUSH_FALSE);
        emit_bytes(parser, OP_EQ, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);
        break;
    }
    case TOKEN_NUMBER: {
        advance(parser);
        number(parser);
        emit_bytes(parser, OP_EQ, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);
        break;
    }
    case TOKEN_STRING: {
        advance(parser);
        string(parser);
        emit_bytes(parser, OP_EQ, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);
        break;
    }
    case TOKEN_LEFT_PAREN: {
        // Pair Pattern
        advance(parser); // consume '('

        // Check if the recent match value is of type pair.
        emit_bytes(parser, OP_IS_PAIR, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);

        // Index of the recent match value on stack.
        add_dummy_local(parser);
        uint8_t match_value_index = parser->context->local_count - 1;

        // Compile the left side pattern.
        emit_bytes(parser, OP_DUP, OP_CAR);
        pattern(parser, context, true);

        consume(parser, TOKEN_COLON_COLON, "expect '::' in pair pattern");

        // Retrieve the match value to the stack top.
        emit_bytes(parser, OP_GET_LOCAL, match_value_index);

        // Compile the right side pattern.
        emit_byte(parser, OP_CDR);
        pattern(parser, context, true);

        consume(parser, TOKEN_RIGHT_PAREN, "expect closing ')' in pair pattern");
        break;
    }
    case TOKEN_LEFT_BRACKET: {
        // Array Pattern
        advance(parser); // consume '['

        // Check if the recent match value is of type array.
        emit_bytes(parser, OP_IS_ARRAY, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);

        // Index of the recent match value on stack.
        add_dummy_local(parser);
        uint8_t match_value_index = parser->context->local_count - 1;

        // Save a copy of the array length on the stack.
        emit_bytes(parser, OP_DUP, OP_ARRAY_LEN);
        add_dummy_local(parser);
        context->bindings_count += 1;
        uint8_t length_index = parser->context->local_count - 1;

        // Check for empty array pattern.
        if (consume_if(parser, TOKEN_RIGHT_BRACKET)) {
            emit_constant(parser, Num_Value(0));
            emit_byte(parser, OP_NEQ);
            pattern_fail_if_true(parser, context, subvalue);
            break; // switch
        }

        int array_subpatterns_count = 0;
        do {
            // Check that `array_length` >= `array_subpatterns_count`
            emit_bytes(parser, OP_GET_LOCAL, length_index);
            emit_constant(parser, Num_Value(array_subpatterns_count + 1));
            emit_byte(parser, OP_LT);
            pattern_fail_if_true(parser, context, subvalue);

            // Compile array element.
            emit_bytes(parser, OP_GET_LOCAL, match_value_index);
            emit_bytes(parser, OP_ARRAY_PUSH_ELEMENT, (uint8_t)array_subpatterns_count);
            pattern(parser, context, true);

            array_subpatterns_count++;
        } while (consume_if(parser, TOKEN_COMMA));

        // Check that the pattern count equals the array element count.
        emit_bytes(parser, OP_GET_LOCAL, length_index);
        emit_constant(parser, Num_Value(array_subpatterns_count));
        emit_byte(parser, OP_NEQ);
        pattern_fail_if_true(parser, context, subvalue);

        consume(parser, TOKEN_RIGHT_BRACKET, "expect closing ']' for the array pattern");
        break;
    }
    case TOKEN_LEFT_BRACE: {
        // Map Pattern
        advance(parser); // consume '{'

        // Check if the recent match value is of type map.
        emit_bytes(parser, OP_IS_MAP, OP_NOT);
        pattern_fail_if_true(parser, context, subvalue);

        // Check for empty map pattern, it matches all maps.
        if (consume_if(parser, TOKEN_RIGHT_BRACE)) {
            break; // switch
        }

        // The recent match value
        add_dummy_local(parser);
        uint8_t match_value_index = parser->context->local_count - 1;

        do {
            consume(parser, TOKEN_IDENTIFIER, "expect key name for the map pattern");

            RavString *key = object_string(
                &parser->vm->allocator,
                parser->previous.lexeme,
                parser->previous.length
            );

            // OP_MAP_GET sets the X register to true or false depending
            // on whether the map has the key or not
            uint8_t constant_index = make_constant(parser, Obj_Value(key));
            emit_bytes(parser, OP_GET_LOCAL, match_value_index);
            emit_bytes(parser, OP_MAP_PUSH_ELEMENT, constant_index);
            emit_bytes(parser, OP_PUSH_X, OP_NOT);
            pattern_fail_if_true(parser, context, subvalue);

            consume(parser, TOKEN_COLON, "expect ':' after the key name");
            pattern(parser, context, true);
        } while (consume_if(parser, TOKEN_COMMA));

        consume(parser, TOKEN_RIGHT_BRACE, "expect closing '}' after the map pattern");
        break;
    }
    default:
        error_current(parser, "expect beginning of a pattern");
        break;
    }

    Debug_Exit(parser);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpb $0x0, 0x49(%rdi)
je 0x6c74
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x20, 0x80(%rsi)
jne 0x6ca3
leaq 0x2def(%rip), %rsi      # 0x9a79
pushq $0x20
popq %rdx
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5d22
movl %edx, %ebp
movl 0x18(%rbx), %eax
leal -0x28(%rax), %ecx
cmpl $0x9, %ecx
ja 0x6d68
leaq 0x2ab9(%rip), %rax      # 0x9774
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %rbx, %rdi
callq 0x5a44
pushq $0x31
popq %rsi
pushq $0x15
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
movzbl %bpl, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7118
movq %rbx, %rdi
callq 0x7176
movq 0x8(%rbx), %rax
movb 0x2018(%rax), %r12b
decb %r12b
pushq $0x6
popq %rsi
pushq $0x2c
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
pushq $0x1
popq %rbp
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x6c51
leaq 0x2d66(%rip), %rdx      # 0x9a8a
pushq $0x1a
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5bf2
movzbl %r12b, %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5d5f
pushq $0x2d
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x6c51
leaq 0x2d45(%rip), %rdx      # 0x9aa6
pushq $0x29
jmp 0x7101
cmpl $0x4, %eax
je 0x6fbe
cmpl $0xb, %eax
je 0x6fb2
cmpl $0xe, %eax
jne 0x6d91
movq %rbx, %rdi
callq 0x5a44
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x6fcc
leaq 0x2dc5(%rip), %rsi      # 0x9b5d
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5c65
movq %rbx, %rdi
callq 0x5a44
movq %rbx, %rdi
callq 0x6a52
jmp 0x6fd1
cmpl $0x1, 0x28(%rbx)
jne 0x6ffc
movq 0x20(%rbx), %rax
cmpb $0x5f, (%rax)
jne 0x6ffc
pushq $0x7
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5a44
movq %rbx, %rdi
callq 0x5a44
pushq $0x32
popq %rsi
pushq $0x15
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
movzbl %bpl, %r15d
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x7118
movq %rbx, %rdi
callq 0x7176
movq 0x8(%rbx), %rax
movb 0x2018(%rax), %bpl
pushq $0x6
popq %rsi
pushq $0x2e
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
movq %rbx, %rdi
callq 0x7176
incl 0x84(%r14)
movq 0x8(%rbx), %rax
movb 0x2018(%rax), %r12b
pushq $0x2d
popq %rsi
movq %rbx, %rdi
callq 0x5876
testb %al, %al
je 0x7022
movq %rbx, %rdi
xorl %esi, %esi
callq 0x71c6
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
jmp 0x6fe9
movq %rbx, %rdi
callq 0x5a44
pushq $0x33
popq %rsi
pushq $0x15
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
movzbl %bpl, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x7118
pushq $0x2b
popq %rsi
movq %rbx, %rdi
callq 0x5876
testb %al, %al
jne 0x6c65
movq %rbx, %rdi
callq 0x7176
movq 0x8(%rbx), %rax
movb 0x2018(%rax), %al
decb %al
movzbl %al, %r13d
pushq $0x25
popq %r12
movq %rbx, %rdi
pushq $0x2f
popq %rsi
leaq 0x2bfb(%rip), %rdx      # 0x9af2
callq 0x5bf2
movq 0x10(%rbx), %rdi
movq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
callq 0x4de8
movabsq $-0x4000000000000, %rcx # imm = 0xFFFC000000000000
orq %rcx, %rax
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5f52
movl %eax, %r15d
movq %rbx, %rdi
pushq $0x1a
popq %rsi
movl %r13d, %edx
callq 0x5d5f
movzbl %r15b, %edx
movq %rbx, %rdi
pushq $0x30
popq %rsi
callq 0x5d5f
movq %rbx, %rdi
pushq $0x4
popq %rsi
pushq $0x15
popq %rdx
callq 0x5d5f
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x7118
movq %rbx, %rdi
pushq $0x27
popq %rsi
leaq 0x2baa(%rip), %rdx      # 0x9b16
callq 0x5bf2
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0x6c51
movq %rbx, %rdi
movl %r12d, %esi
callq 0x5876
testb %al, %al
jne 0x6eea
leaq 0x2b9b(%rip), %rdx      # 0x9b34
pushq $0x2b
jmp 0x7101
movq %rbx, %rdi
callq 0x5a44
movq %rbx, %rdi
callq 0x6a6f
jmp 0x6fd1
movq %rbx, %rdi
callq 0x5a44
pushq $0x2
jmp 0x6fc8
movq %rbx, %rdi
callq 0x5a44
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
pushq $0xf
popq %rsi
pushq $0x15
popq %rdx
movq %rbx, %rdi
callq 0x5d5f
movzbl %bpl, %edx
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7118
leaq 0x2054(%rip), %rsi      # 0x9057
movq %rbx, %rdi
callq 0x5ab5
movzbl %al, %esi
movq %rbx, %rdi
callq 0x5bc8
incl 0x84(%r14)
jmp 0x6c65
decb %bpl
decb %r12b
movl %r15d, 0x10(%rsp)
xorl %r15d, %r15d
movzbl %r12b, %eax
movl %eax, 0xc(%rsp)
movzbl %bpl, %eax
movl %eax, 0x14(%rsp)
pushq $0x1a
popq %r13
movq %rbx, %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
callq 0x5d5f
leal 0x1(%r15), %r12d
cvtsi2sd %r12d, %xmm0
movq %xmm0, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x71c6
movq %rbx, %rdi
pushq $0x11
popq %rsi
callq 0x5a9e
movq %rbx, %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
callq 0x7118
movq %rbx, %rdi
movl %r13d, %esi
movl 0x14(%rsp), %edx
callq 0x5d5f
movzbl %r15b, %edx
movq %rbx, %rdi
pushq $0x2f
popq %rsi
callq 0x5d5f
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0x6c51
movq %rbx, %rdi
pushq $0x25
popq %rsi
callq 0x5876
movl %r12d, %r15d
testb %al, %al
jne 0x7044
pushq $0x1a
popq %rsi
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq 0x5d5f
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x71c6
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
movq %rbx, %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
callq 0x7118
leaq 0x29ca(%rip), %rdx      # 0x9ac9
pushq $0x2d
popq %rsi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5bf2