// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
cmpq $0x0, (%r14)
je 0x9368
leaq 0x89a2(%rip), %rcx      # 0x11ce3
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x91b7
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movq %rcx, (%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, %ebp
movl 0x8(%r14), %edi
movl 0xc(%r14), %r8d
leaq 0x20(%r14), %rdx
movq %rdx, 0x8(%rsp)
cmpl $0x2, %ebp
sete %cl
movq 0x20(%r14), %rax
cmpq $0x22, %rax
setne %sil
orb %cl, %sil
movl %edi, 0x1c(%rsp)
movl %r8d, 0x18(%rsp)
je 0x93c4
cmpl $0x2, %ebp
sete %cl
cmpq $0x23, %rax
setne %sil
orb %cl, %sil
je 0x940a
cmpq $0x24, %rax
jne 0x941a
leaq 0x40(%r14), %rax
movq %rax, 0x8(%rsp)
movb $0x1, %al
xorl %r12d, %r12d
jmp 0x943b
leaq 0x40(%r14), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %r15, %rdi
movl $0x7, %edx
callq 0xcd76
movq %rax, %r12
testq %rax, %rax
je 0x934f
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
cmpq $0x22, %rax
je 0x9430
cmpq $0x3b, %rax
jne 0x9443
addq $0x20, %rdx
movq %rdx, 0x8(%rsp)
movb $0x1, %al
jmp 0x944d
leaq 0x40(%r14), %rax
movq %rax, 0x8(%rsp)
movb $0x1, %al
xorl %r12d, %r12d
jmp 0x944d
cmpl $0x2, %ebp
jne 0x955f
movb $0x1, %al
movl %eax, 0x14(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x9455
addq $0x20, %rdx
movq %rdx, 0x8(%rsp)
movb $0x1, %al
movb $0x1, %cl
movl %ecx, 0x14(%rsp)
jmp 0x9455
testl %ebp, %ebp
je 0x9568
xorl %eax, %eax
movl $0x0, 0x14(%rsp)
testb %al, %al
je 0x9470
movq 0x8(%rsp), %rdx
cmpq $0x21, (%rdx)
jne 0x9475
leaq 0x8842(%rip), %rcx      # 0x11cad
jmp 0x954f
xorl %r13d, %r13d
jmp 0x9493
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
movl $0x7, %edx
callq 0xcd76
movq %rax, %r13
testq %rax, %rax
je 0x934f
movq 0x8(%rsp), %rdx
cmpq $0x21, (%rdx)
jne 0x953a
addq $0x20, %rdx
movq %rdx, 0x8(%rsp)
movq 0x20(%r15), %rdi
movl $0x60, %esi
callq 0x95e6
movl $0x0, (%rax)
movl 0xc6b3(%rip), %ecx      # 0x15b78
leal 0x1(%rcx), %edx
movl %edx, 0xc6aa(%rip)      # 0x15b78
movl %ecx, 0x4(%rax)
movl 0x1c(%rsp), %esi
movl %esi, 0x8(%rax)
movl 0x18(%rsp), %esi
movl %esi, 0xc(%rax)
movq 0x8(%r14), %rsi
movups 0x10(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
addl $0x2, %ecx
movl %ecx, 0xc682(%rip)      # 0x15b78
movl $0x7, 0x10(%rax)
movl %edx, 0x14(%rax)
movq %rsi, 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, 0x14(%rsp)
je 0x9351
movw $0x4, 0x2(%rax)
orb $0x4, 0x12(%rax)
jmp 0x9351
cmpl $0x2, %ebp
leaq 0x877d(%rip), %rax      # 0x11cc1
leaq 0x878b(%rip), %rcx      # 0x11cd6
cmoveq %rax, %rcx
movq %r15, %rdi
movq %r14, %rsi
callq 0x91b7
jmp 0x934f
leaq 0x872b(%rip), %rcx      # 0x11c91
jmp 0x956f
leaq 0x870e(%rip), %rcx      # 0x11c7d
movq %r15, %rdi
movq %r14, %rsi
callq 0x91b7
jmp 0x934f
jmp 0x9583
jmp 0x9583
movq 0x8(%rsp), %rcx
movq %rcx, (%rbx)
movq %rax, %rdi
callq 0x3100