// File: cphinton42[P]hinton-language/src/parse.cpp
// Function: parse_statement_block(Parsing_Context*, Token**)
internal Block_AST *parse_statement_block(Parsing_Context *ctx, Token **current_ptr)
{
    Token *current = *current_ptr;
    Token *start_section = current;
    Block_AST *result = nullptr;
    defer {
        *current_ptr = current;
    };
    
    if(current->type == Token_Type::open_brace)
    {
        result = construct_ast(ctx->ast_pool, Block_AST, current->line_number, current->line_offset);
        ++current;
        
        // TODO: memory leak
        Dynamic_Array<AST*> statements = {0};
        
        while(true)
        {
            if(current->type == Token_Type::close_brace)
            {
                ++current;
                
                result->statements.count = statements.count;
                result->statements.data = pool_alloc(AST*, statements.count, ctx->ast_pool);
                
                for(u64 i = 0; i < statements.count; ++i)
                {
                    result->statements[i] = statements[i];
                }
                break;
            }
            else
            {
                AST *stmt = parse_statement(ctx, &current);
                if(stmt)
                {
                    array_add(&statements, stmt);
                }
                else
                {
                    return nullptr;
                }
            }
        }
    }
    else
    {
        report_error(ctx, start_section, current, "Expected '{' to begin statement block");
    }
    
    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
leaq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
leaq -0x50(%rbp), %rdx
callq 0x14fc0
movq -0x20(%rbp), %rax
cmpq $0x2c, (%rax)
jne 0x14aa7
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x20, %esi
callq 0xcd80
movq %rax, -0xb0(%rbp)
jmp 0x14906
movq -0xb0(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %ecx
movl $0x1, %esi
callq 0x3c10
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3060
jmp 0x1494b
movq -0x20(%rbp), %rax
cmpq $0x2d, (%rax)
jne 0x14a2c
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x78(%rbp), %rsi
shlq $0x3, %rsi
callq 0xcd80
movq %rax, -0xb8(%rbp)
jmp 0x1498f
movq -0xb8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jae 0x14a2a
movq -0x80(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x18270
movq %rax, -0xc0(%rbp)
jmp 0x149c6
movq -0xc0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x30(%rbp), %rdi
addq $0x10, %rdi
movq -0x80(%rbp), %rsi
callq 0x16fe0
movq %rax, -0xc8(%rbp)
jmp 0x149f1
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x149a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x15db0
jmp 0x14aeb
jmp 0x14aa5
movq -0x10(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x14ff0
movq %rax, -0xd8(%rbp)
jmp 0x14a42
movq -0xd8(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x14a8a
movq -0x88(%rbp), %rsi
movups 0xb218(%rip), %xmm0     # 0x1fc80
movaps %xmm0, -0xa0(%rbp)
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rcx
leaq -0x78(%rbp), %rdi
callq 0x182a0
jmp 0x14a88
jmp 0x14a9e
movq $0x0, -0x8(%rbp)
movl $0x1, -0xa4(%rbp)
jmp 0x14ad5
jmp 0x14aa0
jmp 0x14949
jmp 0x14ac3
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x659b(%rip), %rcx      # 0x1b055
callq 0xc650
jmp 0x14ac1
jmp 0x14ac3
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xa4(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x15db0
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x3100
nopw %cs:(%rax,%rax)