// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %rax
cmpb $0x30, (%rax)
jne 0xd9b6
movq 0x20(%rbx), %rax
movzbl (%rax), %eax
cmpl $0x78, %eax
je 0xdaba
cmpl $0x62, %eax
jne 0xd9b6
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movb (%rax), %bpl
movl %ebp, %eax
andb $-0x2, %al
cmpb $0x30, %al
jne 0xdb3a
xorl %r15d, %r15d
leaq 0xba84(%rip), %r14      # 0x193f9
movl %ebp, %eax
andb $-0x2, %al
cmpb $0x30, %al
jne 0xdb51
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movl 0x18(%rbx), %ecx
movl %eax, %edx
subl %ecx, %edx
cmpl $0x42, %edx
jg 0xdb44
addb $-0x30, %bpl
movzbl %bpl, %ecx
leaq (%rcx,%r15,2), %r15
movb (%rax), %bpl
jmp 0xd975
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movsbl (%rax), %edi
callq 0xab7d
testb %al, %al
jne 0xd9ae
movq 0x20(%rbx), %rax
cmpb $0x2e, (%rax)
jne 0xda01
movsbl 0x1(%rax), %edi
callq 0xab7d
testb %al, %al
je 0xda01
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0xd905
movq 0x20(%rbx), %rax
movsbl (%rax), %edi
callq 0xab7d
testb %al, %al
je 0xda01
movq %rbx, %rdi
callq 0xd8a3
jmp 0xd9e7
pushq $0x65
popq %rsi
movq %rbx, %rdi
callq 0xd905
testb %al, %al
jne 0xda1f
pushq $0x45
popq %rsi
movq %rbx, %rdi
callq 0xd905
testb %al, %al
je 0xda75
movq 0x20(%rbx), %rax
movzbl (%rax), %eax
cmpl $0x2d, %eax
je 0xda30
cmpl $0x2b, %eax
jne 0xda3e
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movb (%rax), %al
movsbl %al, %edi
callq 0xab7d
testb %al, %al
je 0xda64
movq 0x20(%rbx), %rax
movsbl (%rax), %edi
callq 0xab7d
testb %al, %al
je 0xda75
movq %rbx, %rdi
callq 0xd8a3
jmp 0xda4a
leaq 0xb9d8(%rip), %rsi      # 0x19443
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdd6b
callq 0x2060
movq %rax, %r14
andl $0x0, (%rax)
movq 0x18(%rbx), %rdi
callq 0x20e0
callq 0x34ce
cmpl $0x22, (%r14)
jne 0xdbac
movq 0x18(%rbx), %rcx
movl 0x20(%rbx), %edx
subl %ecx, %edx
leaq 0xb9b3(%rip), %rsi      # 0x1945b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdd6b
xorps %xmm0, %xmm0
jmp 0xdba7
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movb (%rax), %r15b
leal -0x30(%r15), %eax
cmpb $0xa, %al
setae %al
leal -0x61(%r15), %ecx
cmpb $0x6, %cl
setae %cl
testb %cl, %al
jne 0xdb58
xorl %r14d, %r14d
leal -0x30(%r15), %ebp
cmpb $0xa, %bpl
setae %al
leal -0x61(%r15), %ecx
cmpb $0x6, %cl
setae %cl
testb %cl, %al
jne 0xdb86
movq %rbx, %rdi
callq 0xd8a3
movq 0x20(%rbx), %rax
movl 0x18(%rbx), %ecx
movl %eax, %edx
subl %ecx, %edx
cmpl $0x13, %edx
jge 0xdb75
addb $-0x57, %r15b
cmpb $0xa, %bpl
movzbl %bpl, %ecx
movzbl %r15b, %edx
cmovbl %ecx, %edx
shlq $0x4, %r14
movzbl %dl, %ecx
orq %rcx, %r14
movb (%rax), %r15b
jmp 0xdae3
leaq 0xb8a0(%rip), %r14      # 0x193e1
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0xdd6b
movq %r15, %xmm1
jmp 0xdb8b
leaq 0xb8b6(%rip), %rsi      # 0x19415
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdd6b
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
jmp 0xdbac
leaq 0xb8ae(%rip), %rsi      # 0x1942a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdd6b
movq %r14, %xmm1
punpckldq 0xb22d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xb235(%rip), %xmm1     # 0x18dd0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x34ce
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
popq %r15
popq %rbp
retq