// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/src/pk_compiler.c
// Function: eatNumber
static void eatNumber(Compiler* compiler) {

#define IS_HEX_CHAR(c)            \
  (('0' <= (c) && (c) <= '9')  || \
   ('a' <= (c) && (c) <= 'f'))

#define IS_BIN_CHAR(c) (((c) == '0') || ((c) == '1'))

  Var value = VAR_NULL; // The number value.
  char c = *compiler->token_start;

  // Binary literal.
  if (c == '0' && peekChar(compiler) == 'b') {
    eatChar(compiler); // Consume '0b'

    uint64_t bin = 0;
    c = peekChar(compiler);
    if (!IS_BIN_CHAR(c)) {
      lexError(compiler, "Invalid binary literal.");

    } else {
      do {
        // Consume the next digit.
        c = peekChar(compiler);
        if (!IS_BIN_CHAR(c)) break;
        eatChar(compiler);

        // Check the length of the binary literal.
        int length = (int)(compiler->current_char - compiler->token_start);
        if (length > STR_BIN_BUFF_SIZE - 2) { // -2: '-\0' 0b is in both side.
          lexError(compiler, "Binary literal is too long.");
          break;
        }

        // "Append" the next digit at the end.
        bin = (bin << 1) | (c - '0');

      } while (true);
    }
    value = VAR_NUM((double)bin);

  } else if (c == '0' && peekChar(compiler) == 'x') {
    eatChar(compiler); // Consume '0x'

    uint64_t hex = 0;
    c = peekChar(compiler);

    // The first digit should be either hex digit.
    if (!IS_HEX_CHAR(c)) {
      lexError(compiler, "Invalid hex literal.");

    } else {
      do {
        // Consume the next digit.
        c = peekChar(compiler);
        if (!IS_HEX_CHAR(c)) break;
        eatChar(compiler);

        // Check the length of the binary literal.
        int length = (int)(compiler->current_char - compiler->token_start);
        if (length > STR_HEX_BUFF_SIZE - 2) { // -2: '-\0' 0x is in both side.
          lexError(compiler, "Hex literal is too long.");
          break;
        }

        // "Append" the next digit at the end.
        uint8_t append_val = ('0' <= c && c <= '9')
          ? (uint8_t)(c - '0')
          : (uint8_t)((c - 'a') + 10);
        hex = (hex << 4) | append_val;

      } while (true);

      value = VAR_NUM((double)hex);
    }

  } else { // Regular number literal.
    while (utilIsDigit(peekChar(compiler))) {
      eatChar(compiler);
    }

    if (peekChar(compiler) == '.' && utilIsDigit(peekNextChar(compiler))) {
      matchChar(compiler, '.');
      while (utilIsDigit(peekChar(compiler))) {
        eatChar(compiler);
      }
    }

    // Parse if in scientific notation format (MeN == M * 10 ** N).
    if (matchChar(compiler, 'e') || matchChar(compiler, 'E')) {

      if (peekChar(compiler) == '+' || peekChar(compiler) == '-') {
        eatChar(compiler);
      }

      if (!utilIsDigit(peekChar(compiler))) {
        lexError(compiler, "Invalid number literal.");

      } else { // Eat the exponent.
        while (utilIsDigit(peekChar(compiler))) eatChar(compiler);
      }
    }

    errno = 0;
    value = VAR_NUM(atof(compiler->token_start));
    if (errno == ERANGE) {
      const char* start = compiler->token_start;
      int len = (int)(compiler->current_char - start);
      lexError(compiler, "Number literal is too large (%.*s).", len, start);
      value = VAR_NUM(0);
    }
  }

  setNextValueToken(compiler, TK_NUMBER, value);
#undef IS_BIN_CHAR
#undef IS_HEX_CHAR
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %r8
movq 0x20(%rdi), %rax
cmpb $0x30, (%r8)
movzbl (%rax), %ecx
jne 0x120ee
cmpl $0x78, %ecx
je 0x12221
cmpl $0x62, %ecx
jne 0x120ee
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb 0x1(%rax), %cl
movl %ecx, %edx
andb $-0x2, %dl
cmpb $0x30, %dl
jne 0x122a4
leaq 0x2(%rax), %rdx
subl %r8d, %eax
addl $0x2, %eax
xorl %r14d, %r14d
leaq 0xb35a(%rip), %rsi      # 0x1d419
movq %rdx, 0x20(%rbx)
cmpl $0x42, %eax
jg 0x122d4
addb $-0x30, %cl
movzbl %cl, %ecx
leaq (%rcx,%r14,2), %r14
movb (%rdx), %cl
movl %ecx, %edi
andb $-0x2, %dil
incq %rdx
incl %eax
cmpb $0x30, %dil
je 0x120bf
jmp 0x122de
movsbl %cl, %edi
jmp 0x1210b
movq 0x20(%rbx), %rax
movb (%rax), %cl
incq %rax
movq %rax, 0x20(%rbx)
cmpb $0xa, %cl
jne 0x12108
incl 0x28(%rbx)
movsbl (%rax), %edi
callq 0xef1d
testb %al, %al
jne 0x120f3
movq 0x20(%rbx), %rax
cmpb $0x2e, (%rax)
jne 0x12165
movsbl 0x1(%rax), %edi
callq 0xef1d
testb %al, %al
je 0x12165
movq 0x20(%rbx), %rcx
movb (%rcx), %al
cmpb $0x2e, %al
jne 0x1213f
leaq 0x1(%rcx), %rax
movq %rax, 0x20(%rbx)
movb 0x1(%rcx), %al
movsbl %al, %edi
callq 0xef1d
testb %al, %al
je 0x12165
movq 0x20(%rbx), %rax
movb (%rax), %cl
incq %rax
movq %rax, 0x20(%rbx)
cmpb $0xa, %cl
jne 0x12160
incl 0x28(%rbx)
movsbl (%rax), %edi
jmp 0x12142
movq 0x20(%rbx), %rax
movzbl (%rax), %ecx
orl $0x20, %ecx
cmpl $0x65, %ecx
jne 0x121d9
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movzbl 0x1(%rax), %ecx
cmpl $0x2d, %ecx
je 0x1218a
cmpl $0x2b, %ecx
jne 0x12195
leaq 0x2(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb 0x2(%rax), %cl
movsbl %cl, %edi
callq 0xef1d
testb %al, %al
je 0x121c8
movq 0x20(%rbx), %rax
movsbl (%rax), %edi
callq 0xef1d
testb %al, %al
je 0x121d9
movq 0x20(%rbx), %rax
movb (%rax), %cl
incq %rax
movq %rax, 0x20(%rbx)
cmpb $0xa, %cl
jne 0x121a5
incl 0x28(%rbx)
jmp 0x121a5
leaq 0xb294(%rip), %rsi      # 0x1d463
movq %rbx, %rdi
xorl %eax, %eax
callq 0x124de
callq 0x2060
movq %rax, %r14
movl $0x0, (%rax)
movq 0x18(%rbx), %rdi
callq 0x20e0
callq 0x8f9f
cmpl $0x22, (%r14)
jne 0x12304
movq 0x18(%rbx), %rcx
movl 0x20(%rbx), %edx
subl %ecx, %edx
leaq 0xb26c(%rip), %rsi      # 0x1d47b
movq %rbx, %rdi
xorl %eax, %eax
callq 0x124de
xorps %xmm0, %xmm0
jmp 0x122ff
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb 0x1(%rax), %cl
leal -0x30(%rcx), %esi
cmpb $0xa, %sil
setb %dil
setae %dl
leal -0x61(%rcx), %r9d
cmpb $0x6, %r9b
setae %r9b
testb %r9b, %dl
jne 0x122b0
leaq 0x2(%rax), %rdx
subl %r8d, %eax
addl $0x2, %eax
xorl %r14d, %r14d
movq %rdx, 0x20(%rbx)
cmpl $0x13, %eax
jge 0x122cd
addb $-0x57, %cl
movzbl %sil, %esi
movzbl %cl, %ecx
testb $0x1, %dil
cmovnel %esi, %ecx
shlq $0x4, %r14
movzbl %cl, %ecx
orq %rcx, %r14
movb (%rdx), %cl
leal -0x30(%rcx), %esi
cmpb $0xa, %sil
setb %dil
leal -0x61(%rcx), %r8d
cmpb $0x6, %r8b
setb %r8b
orb %dil, %r8b
incq %rdx
incl %eax
testb %r8b, %r8b
jne 0x12258
jmp 0x122de
leaq 0xb156(%rip), %rsi      # 0x1d401
xorl %r14d, %r14d
jmp 0x122d4
leaq 0xb17e(%rip), %rsi      # 0x1d435
movq %rbx, %rdi
xorl %eax, %eax
callq 0x124de
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
jmp 0x12304
leaq 0xb176(%rip), %rsi      # 0x1d44a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x124de
movq %r14, %xmm1
punpckldq 0xaaf5(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xaafd(%rip), %xmm1     # 0x1cdf0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x8f9f
movl $0x48, 0x70(%rbx)
movq 0x18(%rbx), %rcx
movq %rcx, 0x78(%rbx)
movl 0x20(%rbx), %edx
subl %ecx, %edx
movl %edx, 0x80(%rbx)
movl 0x28(%rbx), %ecx
movl %ecx, 0x84(%rbx)
movq %rax, 0x88(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
retq