// File: cphinton42[P]hinton-language/src/lex.cpp
// Function: report_error(Array<char>, char*, char*, unsigned int, unsigned int, char const*)
internal void report_error(String program_text, byte *start_tok, byte *current, u32 line_number, u32 line_offset, const byte *error_text)
{
    byte *start_program = program_text.data;
    byte *end_program = start_program + program_text.count;
    
    byte *start = start_tok;
    byte *end = current;
    while(start > start_program)
    {
        if(*start == '\n' || *start == '\r')
        {
            ++start;
            break;
        }
        else
        {
            --start;
        }
    }
    while(end < end_program)
    {
        byte b = *end;
        ++end;
        
        if(b == '\r')
        {
            if(end < end_program && *end == '\n')
            {
                ++end;
            }
            break;
        }
        else if(b == '\n')
        {
            break;
        }
    }
    
    // TODO: remove color codes when not outputing to a terminal
    // TODO: perhaps change to highlight in the program text, rather than use an arrow ?
    
    print_err("\x1B[1;31mError\x1B[0m: %d:%d:\n    %s\n", line_number, line_offset, error_text);
    print_err_indented(start, start_tok);
    
    print_err("\x1B[1;33m%.*s\x1B[1;31m%c\x1B[0m", (u32)(current - start_tok), start_tok, *current);
    
    if(current != end)
    {
        print_err_indented(current+1, end, false);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jbe 0xba36
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xba1a
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0xba28
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xba36
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xb9f8
jmp 0xba38
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0xba93
movq -0x48(%rbp), %rax
movb (%rax), %al
movb %al, -0x49(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0xd, %eax
jne 0xba84
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0xba82
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0xba82
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xba93
movsbl -0x49(%rbp), %eax
cmpl $0xa, %eax
jne 0xba8f
jmp 0xba93
jmp 0xba91
jmp 0xba38
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movq 0x10(%rbp), %rcx
leaq 0xe900(%rip), %rdi      # 0x1a3a4
movb $0x0, %al
callq 0x4990
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x1, %edx
callq 0xa210
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movl %eax, %esi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
leaq 0xf2da(%rip), %rdi      # 0x1adb6
movb $0x0, %al
callq 0x4990
movq -0x20(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0xbb00
movq -0x20(%rbp), %rdi
addq $0x1, %rdi
movq -0x48(%rbp), %rsi
xorl %edx, %edx
callq 0xa210
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)