// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
testb $0x1, 0x49(%rax)
je 0xade6
jmp 0xb44c
movq -0x10(%rbp), %rax
cmpl $0x20, 0x80(%rax)
jne 0xae0d
movq -0x8(%rbp), %rdi
leaq 0x3d40(%rip), %rsi      # 0xeb3e
movl $0x20, %edx
callq 0x9580
jmp 0xb44c
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
addl $-0x4, %eax
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x2d, %eax
ja 0xb43c
movq -0x58(%rbp), %rax
leaq 0x394b(%rip), %rcx      # 0xe77c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x20(%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
cmpl $0x1, -0x20(%rbp)
jne 0xae84
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5f, %eax
jne 0xae84
movq -0x8(%rbp), %rdi
movl $0x7, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
callq 0x8dc0
jmp 0xb44c
movq -0x8(%rbp), %rdi
leaq 0x31c8(%rip), %rsi      # 0xe057
callq 0x90d0
movb %al, -0x31(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %esi
callq 0x91f0
movq -0x10(%rbp), %rax
movl 0x84(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x84(%rax)
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
movl $0x2, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movl $0xf, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movl $0xf, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movl $0xf, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
callq 0xa990
movq -0x8(%rbp), %rdi
movl $0xf, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
callq 0xa9d0
movq -0x8(%rbp), %rdi
movl $0xf, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
movl $0x31, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
callq 0xb500
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x2018(%rax), %eax
subl $0x1, %eax
movb %al, -0x32(%rbp)
movq -0x8(%rbp), %rdi
movl $0x6, %esi
movl $0x2c, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0xadc0
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
leaq 0x3ac9(%rip), %rdx      # 0xeb4f
callq 0x9240
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
movzbl -0x32(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movl $0x2d, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0xadc0
movq -0x8(%rbp), %rdi
movl $0x29, %esi
leaq 0x3a9e(%rip), %rdx      # 0xeb6b
callq 0x9240
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
movl $0x32, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
callq 0xb500
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x2018(%rax), %eax
subl $0x1, %eax
movb %al, -0x33(%rbp)
movq -0x8(%rbp), %rdi
movl $0x6, %esi
movl $0x2e, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
callq 0xb500
movq -0x10(%rbp), %rax
movl 0x84(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x84(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x2018(%rax), %eax
subl $0x1, %eax
movb %al, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl $0x2d, %esi
callq 0x8c60
testb $0x1, %al
jne 0xb17c
jmp 0xb1c0
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
xorps %xmm0, %xmm0
callq 0xb600
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0xb5c0
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
jmp 0xb44c
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
movzbl -0x34(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
callq 0xb600
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0xb5c0
movq -0x8(%rbp), %rdi
movl $0x11, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
movzbl -0x33(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl $0x2f, %esi
movzbl %al, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0xadc0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x25, %esi
callq 0x8c60
testb $0x1, %al
jne 0xb1c7
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
movzbl -0x34(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
callq 0xb600
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0xb5c0
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x9000
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
movl $0x2d, %esi
leaq 0x38b5(%rip), %rdx      # 0xeb8e
callq 0x9240
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0x8dc0
movq -0x8(%rbp), %rdi
movl $0x33, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
movl $0x2b, %esi
callq 0x8c60
testb $0x1, %al
jne 0xb328
jmp 0xb32d
jmp 0xb44c
movq -0x8(%rbp), %rdi
callq 0xb500
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x2018(%rax), %eax
subl $0x1, %eax
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rdi
movl $0x2f, %esi
leaq 0x385d(%rip), %rdx      # 0xebb7
callq 0x9240
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x8(%rbp), %rax
movl 0x40(%rax), %edx
callq 0x7880
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
orq %rax, %rsi
callq 0x9970
movb %al, -0x49(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1a, %esi
movzbl -0x39(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movl $0x30, %esi
movzbl -0x49(%rbp), %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x15, %edx
callq 0x9620
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0xb460
movq -0x8(%rbp), %rdi
movl $0x27, %esi
leaq 0x37e3(%rip), %rdx      # 0xebdb
callq 0x9240
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0xadc0
movq -0x8(%rbp), %rdi
movl $0x25, %esi
callq 0x8c60
testb $0x1, %al
jne 0xb34a
movq -0x8(%rbp), %rdi
movl $0x2b, %esi
leaq 0x37c4(%rip), %rdx      # 0xebf9
callq 0x9240
jmp 0xb44c
movq -0x8(%rbp), %rdi
leaq 0x37db(%rip), %rsi      # 0xec22
callq 0x9420
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)