// File: /workspace/github_cpp_makefile_star2_O2/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 0x7d2c
leaq 0x7f15(%rip), %rcx      # 0xfc2e
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x7bb9
jmp 0x7f2c
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 0x7d88
cmpl $0x2, %ebp
sete %cl
cmpq $0x23, %rax
setne %sil
orb %cl, %sil
je 0x7dca
cmpq $0x24, %rax
jne 0x7de0
leaq 0x40(%r14), %rdx
movq %rdx, (%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
jmp 0x7e11
leaq 0x40(%r14), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
pushq $0x7
popq %rdx
movq %r15, %rdi
callq 0xb0d1
movq %rax, %r12
testq %rax, %rax
je 0x7f2c
movq (%rsp), %rdx
movq (%rdx), %rax
cmpq $0x22, %rax
je 0x7e02
cmpq $0x3b, %rax
jne 0x7e46
addq $0x20, %rdx
movq %rdx, (%rsp)
jmp 0x7dd5
leaq 0x40(%r14), %rdx
movq %rdx, (%rsp)
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
jmp 0x7e11
cmpl $0x2, %ebp
jne 0x7efd
cmpq $0x21, %rax
jne 0x7f1a
movb $0x1, %al
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
jmp 0x7e64
addq $0x20, %rdx
movq %rdx, (%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
cmpq $0x21, (%rdx)
jne 0x7e23
leaq 0x7dda(%rip), %rcx      # 0xfbf8
jmp 0x7f21
movq %rsp, %rsi
pushq $0x7
popq %rdx
movq %r15, %rdi
callq 0xb0d1
movq %rax, %r13
testq %rax, %rax
je 0x7f2c
movq (%rsp), %rdx
movq (%rdx), %rax
jmp 0x7e5a
testl %ebp, %ebp
je 0x7f06
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
cmpq $0x21, %rax
jne 0x7ee6
addq $0x20, %rdx
movq %rdx, (%rsp)
movq 0x20(%r15), %rdi
pushq $0x60
popq %rsi
callq 0x7fa2
andl $0x0, (%rax)
movl 0xccf7(%rip), %ecx      # 0x14b78
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, 0xccd5(%rip)      # 0x14b78
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 0x7f2e
movw $0x4, %cx
movw %cx, 0x2(%rax)
movw %cx, 0x12(%rax)
jmp 0x7f2e
cmpl $0x2, %ebp
leaq 0x7d1c(%rip), %rax      # 0xfc0c
leaq 0x7d2a(%rip), %rcx      # 0xfc21
cmoveq %rax, %rcx
jmp 0x7f21
leaq 0x7cd8(%rip), %rcx      # 0xfbdc
jmp 0x7f0d
leaq 0x7cbb(%rip), %rcx      # 0xfbc8
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bb9
jmp 0x7f2c
leaq 0x7ceb(%rip), %rcx      # 0xfc0c
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bb9
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 0x7f48
jmp 0x7f48
movq (%rsp), %rcx
movq %rcx, (%rbx)
movq %rax, %rdi
callq 0x3100