// File: /workspace/github_cpp_makefile_star2_O3/cphinton42[P]hinton-language/src/parse.cpp
// Function: parse_decl(Parsing_Context*, Token**, Decl_Type)
Decl_AST *parse_decl(Parsing_Context *ctx, Token **current_ptr, Decl_Type decl_type)
{
    Token *current = *current_ptr;
    Token *start_section = current;
    Decl_AST *result = nullptr;
    defer {
        *current_ptr = current;
    };
    
    u32 line_number = current->line_number;
    u32 line_offset = current->line_offset;
    
    if(current->type == Token_Type::ident)
    {
        Token *ident_tok = current;
        ++current;
        Expr_AST *type = nullptr;
        
        bool not_enum = (decl_type != Decl_Type::Enum);
        // TODO: remove require_value, variables should be able to take on default values (e.g. zero initialized)
        bool require_value = (decl_type == Decl_Type::Statement);
        bool expect_expr;
        bool is_constant;
        
        if(current->type == Token_Type::colon && not_enum)
        {
            ++current;
            type = parse_expr(ctx, &current);
            if(!type)
            {
                return nullptr;
            }
            
            if(current->type == Token_Type::equal)
            {
                ++current;
                expect_expr = true;
                is_constant = false;
            }
            else if(current->type == Token_Type::colon)
            {
                ++current;
                expect_expr = true;
                is_constant = true;
            }
            else if(require_value)
            {
                report_error(ctx, start_section, current, "Expected '=' or ':'");
                return nullptr;
            }
            else
            {
                expect_expr = false;
                is_constant = false;
            }
        }
        else if(current->type == Token_Type::colon_eq && not_enum)
        {
            ++current;
            expect_expr = true;
            is_constant = false;
        }
        else if(current->type == Token_Type::double_colon)
        {
            ++current;
            expect_expr = true;
            is_constant = true;
        }
        else if(not_enum)
        {
            report_error(ctx, start_section, current, "Expected ':', ':=', or '::'");
            return nullptr;
        }
        else
        {
            expect_expr = false;
            is_constant = true;
        }
        
        Expr_AST *expr = nullptr;
        
        if(expect_expr)
        {
            if(current->type == Token_Type::semicolon)
            {
                report_error(ctx, start_section, current, "Expected expression");
                return nullptr;
            }
            expr = parse_expr(ctx, &current);
            if(!expr)
            {
                return nullptr;
            }
        }
        
        // TODO: verify that the expression is constant if is_constant?
        
        if(current->type == Token_Type::semicolon)
        {
            ++current;
        }
        else
        {
            if(decl_type == Decl_Type::Enum)
            {
                // TODO: this error is probably a bit off
                // sometimes :: was already seen ?
                report_error(ctx, start_section, current, "Expected ';' or '::'");
            }
            else
            {
                report_error(ctx, start_section, current, "Expected ';'");
            }
            return nullptr;
        }
        
        result = construct_ast(ctx->ast_pool, Decl_AST, line_number, line_offset);
        result->ident = make_ident_ast(*ident_tok);
        result->decl_type = type;
        result->expr = expr;
        
        if(is_constant)
        {
            // TODO: is this the place to do this?
            result->flags |= DECL_FLAG_CONSTANT;
            result->ident.flags |= EXPR_FLAG_CONSTANT;
            
        }
    }
    else
    {
        report_error(ctx, start_section, current, "Expected identifier to begin declaration");
    }
    
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq (%rsi), %r14
movq %r14, (%rsp)
cmpq $0x0, (%r14)
je 0x93a4
leaq 0x88cd(%rip), %rcx      # 0x11c5e
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x9208
jmp 0x95ad
movl %edx, %ebp
movsd 0x8(%r14), %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x20(%r14), %rdx
movq %rdx, (%rsp)
cmpl $0x2, %ebp
sete %cl
movq 0x20(%r14), %rax
cmpq $0x22, %rax
setne %sil
orb %cl, %sil
je 0x9400
cmpl $0x2, %ebp
sete %cl
cmpq $0x23, %rax
setne %sil
orb %cl, %sil
je 0x9444
cmpq $0x24, %rax
jne 0x945a
leaq 0x40(%r14), %rdx
movq %rdx, (%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
jmp 0x948b
leaq 0x40(%r14), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
movq %r15, %rdi
movl $0x7, %edx
callq 0xcbd8
movq %rax, %r12
testq %rax, %rax
je 0x95ad
movq (%rsp), %rdx
movq (%rdx), %rax
cmpq $0x22, %rax
je 0x947c
cmpq $0x3b, %rax
jne 0x94c2
addq $0x20, %rdx
movq %rdx, (%rsp)
jmp 0x944f
leaq 0x40(%r14), %rdx
movq %rdx, (%rsp)
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
jmp 0x948b
cmpl $0x2, %ebp
jne 0x957e
cmpq $0x21, %rax
jne 0x959b
movb $0x1, %al
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
jmp 0x94e0
addq $0x20, %rdx
movq %rdx, (%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
cmpq $0x21, (%rdx)
jne 0x949d
leaq 0x8790(%rip), %rcx      # 0x11c28
jmp 0x95a2
movq %rsp, %rsi
movq %r15, %rdi
movl $0x7, %edx
callq 0xcbd8
movq %rax, %r13
testq %rax, %rax
je 0x95ad
movq (%rsp), %rdx
movq (%rdx), %rax
jmp 0x94d6
testl %ebp, %ebp
je 0x9587
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
cmpq $0x21, %rax
jne 0x9567
addq $0x20, %rdx
movq %rdx, (%rsp)
movq 0x20(%r15), %rdi
movl $0x60, %esi
callq 0x962b
movl $0x0, (%rax)
movl 0xc676(%rip), %ecx      # 0x15b78
leal 0x1(%rcx), %edx
movl %ecx, 0x4(%rax)
movaps 0x10(%rsp), %xmm0
movlps %xmm0, 0x8(%rax)
movups 0x10(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
addl $0x2, %ecx
movl %ecx, 0xc654(%rip)      # 0x15b78
movq 0x8(%r14), %rcx
movl $0x7, 0x10(%rax)
movl %edx, 0x14(%rax)
movq %rcx, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movaps 0x20(%rsp), %xmm1
movups %xmm1, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movq %r12, 0x50(%rax)
movq %r13, 0x58(%rax)
cmpb $0x0, 0x8(%rsp)
je 0x95af
movw $0x4, %cx
movw %cx, 0x2(%rax)
movw %cx, 0x12(%rax)
jmp 0x95af
cmpl $0x2, %ebp
leaq 0x86cb(%rip), %rax      # 0x11c3c
leaq 0x86d9(%rip), %rcx      # 0x11c51
cmoveq %rax, %rcx
jmp 0x95a2
leaq 0x8687(%rip), %rcx      # 0x11c0c
jmp 0x958e
leaq 0x866a(%rip), %rcx      # 0x11bf8
movq %r15, %rdi
movq %r14, %rsi
callq 0x9208
jmp 0x95ad
leaq 0x869a(%rip), %rcx      # 0x11c3c
movq %r15, %rdi
movq %r14, %rsi
callq 0x9208
xorl %eax, %eax
movq (%rsp), %rcx
movq %rcx, (%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x95c9
jmp 0x95c9
movq (%rsp), %rcx
movq %rcx, (%rbx)
movq %rax, %rdi
callq 0x3100