// File: /workspace/github_c_makefile_star2_O1/sisshiki1969[P]monocc/src/tokenize.c
// Function: tokenize
void tokenize(char *file, char *p, bool is_main) {
  Token head;
  head.next = NULL;
  Token *cur = &head;
  FileInfo *fi = new_file_info(file, p);
  TokContext *ctx = new_tok_context(fi, cur, p);

  while (*p) {
    skip_whitespace(&p);

    if (*p == '#') {
      char *line_end = strchr(p++, '\n');
      skip_whitespace(&p);
      int len = 0;
      while (is_ident_char(*(p + len))) len++;

      if (is_reserved(p, len, "include")) {
        p += len;
        skip_whitespace(&p);
        if (*p == '"') {
          // get directory name of the current file.
          char *cur_dir;
          char *f = strrchr(file, '/');
          if (!f)
            cur_dir = "./";
          else {
            cur_dir = calloc(1, f - file + 2);
            strncpy(cur_dir, file, f - file + 1);
          }
          // fprintf(stderr, "directory: %s\n", cur_dir);
          // concat the include file name to the directory name.
          char *end = strchr(p + 1, '"');
          int len = end - p - 1;
          char *file_name = calloc(1, strlen(cur_dir) + len + 1);
          strcpy(file_name, cur_dir);
          strncpy(file_name + strlen(cur_dir), p + 1, len);

          // fprintf(stderr, "include <%s>\n", file_name);
          char *text = read_file(file_name);
          tokenize(file_name, text, false);
        }
      } else if (is_reserved(p, len, "define")) {
        // fprintf(stderr, "macro define: ");
        p += len;
        skip_whitespace(&p);
        if (!isalpha(*p) && *p != '_')
          error_at_char(fi, p, "Expected identifier.");
        int len = 0;
        while (is_ident_char(p[len])) len++;

        Token *head = cur;
        Token *tok = new_token(TK_IDENT, cur, p, len);
        p += len;

        ctx->p = p;
        ctx->cur = head;
        // print_token(stderr, tok);
        head->next = NULL;

        if (read_if(ctx, '(')) {
          int i = 0;
          // read params of function-like macro
          // fprintf(stderr, " ( ");
          skip_whitespace(&ctx->p);
          // function macro
          while (*ctx->p != ')') {
            Token *param_token = read_token(ctx);
            param_token->num_val = i++;
            // print_token(stderr, param_token);

            skip_whitespace(&ctx->p);
            if (*ctx->p != ',') break;
            ctx->p++;
          }
          if (!read_if(ctx, ')')) error_at_char(fi, ctx->p, "Expected ')'");
          // fprintf(stderr, " ) ");
        }

        // fprintf(stderr, "=>");
        Token *params = head->next;
        head->next = NULL;
        ctx->cur = head;

        skip_whitespace(&ctx->p);
        int i = 0;
        while (*ctx->p != '\n' && i < 50) {
          read_token(ctx);
          // print_token(stderr, read_token(ctx));
          skip_whitespace(&ctx->p);
          i++;
        }
        new_macro(tok, params, head->next);
        head->next = NULL;

        // fprintf(stderr, "\n");
      }

      p = line_end + 1;
      continue;
    }

    // TokContext *ctx = new_tok_context(fi, cur, p);
    ctx->p = p;
    ctx->cur = cur;
    while (*ctx->p != '\n' && *ctx->p != 0) {
      read_token(ctx);
    }

    while (*ctx->p == '\n') ctx->p++;
    cur = ctx->cur;
    p = ctx->p;
  }

  if (!token) {
    token = head.next;
  } else {
    Token *cursor = token;
    while (cursor->next) cursor = cursor->next;
    cursor->next = head.next;
  }
  if (is_main) new_token(TK_EOF, ctx->cur, ctx->p, 1);
  fi->end = ctx->p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, 0x24(%rsp)
movq %rsi, %r15
leaq 0x48(%rsp), %r14
movq $0x0, 0x8(%r14)
movq %rdi, 0x28(%rsp)
callq 0x95c0
movq %rax, %rbx
movl $0x1, %edi
movl $0x18, %esi
callq 0x10d0
movq %rax, %r13
movq %rbx, 0x10(%rsp)
movq %rbx, (%rax)
movq %r14, 0x8(%rsp)
movq %r14, 0x8(%rax)
movq %r15, 0x10(%rax)
cmpb $0x0, (%r15)
je 0x9c40
movq %r13, 0x18(%rsp)
movzbl (%r15), %eax
cmpl $0x20, %eax
je 0x9820
cmpl $0x9, %eax
jne 0x9825
incq %r15
jmp 0x9812
cmpb $0x23, (%r15)
jne 0x9bf0
movq %r15, %rdi
movl $0xa, %esi
callq 0x10a0
movq %r15, %rcx
movq %rax, 0x40(%rsp)
incq %r15
leaq 0x2(%rcx), %rbx
movq %rbx, %r14
movzbl (%r15), %eax
cmpl $0x20, %eax
je 0x985c
cmpl $0x9, %eax
jne 0x986a
incq %r15
incq %rcx
incq %r14
incq %rbx
jmp 0x984e
movq %rcx, %r12
callq 0x11c0
movq %r12, %rcx
movq %rax, 0x38(%rsp)
movq (%rax), %rsi
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rcx, %rbp
movq %r14, %rdx
movq %rbx, %r12
incq %rcx
movsbq 0x1(%rbp), %rax
incl %r13d
incq %r14
incq %rbx
cmpq $0x5f, %rax
je 0x9883
movzwl (%rsi,%rax,2), %eax
andl $0x8, %eax
jne 0x9883
movq %rsi, 0x30(%rsp)
movq %rdx, %r14
movq %rcx, %rbx
movq %r15, %rdi
movl %r13d, %esi
leaq 0x4064(%rip), %rdx      # 0xd928
callq 0x9588
testb %al, %al
je 0x98e9
movq 0x18(%rsp), %r13
movzbl -0x1(%r14), %eax
cmpl $0x20, %eax
je 0x98e1
cmpl $0x9, %eax
jne 0x992c
incq %r12
incq %r14
jmp 0x98d2
movq %r15, %rdi
movl %r13d, %esi
leaq 0x403d(%rip), %rdx      # 0xd933
callq 0x9588
testb %al, %al
movq 0x18(%rsp), %r13
je 0x9bd9
movq %r14, %rdx
movq 0x30(%rsp), %rcx
movzbl (%rbx), %eax
cmpl $0x20, %eax
je 0x9921
cmpl $0x9, %eax
jne 0x99f3
incq %rbx
incq %rbp
incq %rdx
jmp 0x9910
cmpb $0x22, -0x1(%r14)
jne 0x9bd9
movq %r14, %rbp
movq 0x28(%rsp), %r14
movq %r14, %rdi
movl $0x2f, %esi
callq 0x10b0
leaq 0x3fdd(%rip), %r15      # 0xd930
testq %rax, %rax
je 0x9980
movq %rax, %rbx
subq %r14, %rbx
leaq 0x2(%rbx), %rsi
movl $0x1, %edi
callq 0x10d0
movq %rax, %r15
incq %rbx
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1040
movq %rbp, %rdi
movl $0x22, %esi
callq 0x10a0
movq %rax, %rbx
subl %r12d, %ebx
movq %r15, %rdi
callq 0x1090
movslq %ebx, %r12
leaq (%rax,%r12), %rsi
incq %rsi
movl $0x1, %edi
callq 0x10d0
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x1060
movq %r15, %rdi
callq 0x1090
leaq (%rax,%r13), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1040
movq %r13, %rdi
callq 0x560c
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x97ad
movq 0x18(%rsp), %r13
jmp 0x9bd9
movq %rbx, %r12
movsbq (%rbx), %rax
cmpq $0x5f, %rax
je 0x9a27
movzwl (%rcx,%rax,2), %eax
andl $0x400, %eax            # imm = 0x400
jne 0x9a27
movq %rdx, %rbx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x3928(%rip), %rdx      # 0xd345
xorl %eax, %eax
callq 0x5230
movq %rbx, %rdx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %rdx, %r14
movsbq 0x1(%rbp), %rcx
incq %rbp
incl %r15d
incq %rdx
cmpq $0x5f, %rcx
je 0x9a35
movzwl (%rax,%rcx,2), %ecx
andl $0x8, %ecx
jne 0x9a35
movl $0x1, %edi
movl $0x30, %esi
callq 0x10d0
movq %rax, %rbx
movl $0x0, (%rax)
movq %r12, 0x20(%rax)
movl %r15d, 0x28(%rax)
movq 0x8(%rsp), %rax
movq %rbx, 0x8(%rax)
movq %rbp, 0x10(%r13)
movq %rax, 0x8(%r13)
movq $0x0, 0x8(%rax)
cmpb $0x28, (%rbp)
jne 0x9b25
movq %r14, 0x10(%r13)
movzbl (%r14), %eax
cmpl $0x20, %eax
je 0x9aaa
cmpl $0x9, %eax
jne 0x9aaf
incq %r14
jmp 0x9a98
movq 0x10(%r13), %rax
cmpb $0x29, (%rax)
je 0x9afc
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x9cb6
movq %r14, 0x10(%rax)
movq 0x10(%r13), %rax
incq %rax
movzbl -0x1(%rax), %ecx
cmpl $0x20, %ecx
je 0x9adc
cmpl $0x9, %ecx
jne 0x9ae2
movq %rax, 0x10(%r13)
jmp 0x9acb
movq 0x10(%r13), %rax
cmpb $0x2c, (%rax)
jne 0x9afc
incq %r14
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r13)
cmpb $0x29, 0x1(%rax)
jne 0x9abb
movq 0x10(%r13), %rax
cmpb $0x29, (%rax)
jne 0x9b0e
incq %rax
movq %rax, 0x10(%r13)
jmp 0x9b25
movq 0x10(%r13), %rsi
movq 0x10(%rsp), %rdi
leaq 0x3e1c(%rip), %rdx      # 0xd93a
xorl %eax, %eax
callq 0x5230
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r14
movq $0x0, 0x8(%rax)
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rax
incq %rax
movzbl -0x1(%rax), %ecx
cmpl $0x9, %ecx
je 0x9b4f
cmpl $0x20, %ecx
jne 0x9b55
movq %rax, 0x10(%r13)
jmp 0x9b3e
movq 0x10(%r13), %rax
cmpb $0xa, (%rax)
je 0x9b9a
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x9cb6
movq 0x10(%r13), %rax
incq %rax
movzbl -0x1(%rax), %ecx
cmpl $0x20, %ecx
je 0x9b7e
cmpl $0x9, %ecx
jne 0x9b84
movq %rax, 0x10(%r13)
jmp 0x9b6d
movq 0x10(%r13), %rax
cmpb $0xa, (%rax)
je 0x9b9a
leal 0x1(%r15), %eax
cmpl $0x31, %r15d
movl %eax, %r15d
jb 0x9b61
movq 0x8(%rsp), %r12
movq 0x8(%r12), %r15
movl $0x1, %edi
movl $0x20, %esi
callq 0x10d0
movq %rbx, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %r15, 0x18(%rax)
movq 0x96ba(%rip), %rcx      # 0x13280
movq %rcx, (%rax)
movq %rax, 0x96b0(%rip)      # 0x13280
movq $0x0, 0x8(%r12)
movq 0x40(%rsp), %rax
movq %rax, %r15
incq %r15
cmpb $0x0, 0x1(%rax)
jne 0x9812
jmp 0x9c40
movq %r15, 0x10(%r13)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rax
movzbl (%rax), %eax
testl %eax, %eax
je 0x9c17
cmpl $0xa, %eax
je 0x9c17
movq %r13, %rdi
callq 0x9cb6
jmp 0x9bfd
movq 0x10(%r13), %r15
decq %r15
cmpb $0xa, 0x1(%r15)
leaq 0x1(%r15), %r15
je 0x9c1e
movq %r15, 0x10(%r13)
movq 0x8(%r13), %rax
movq %rax, 0x8(%rsp)
cmpb $0x0, (%r15)
jne 0x9812
leaq 0x95e1(%rip), %rax      # 0x13228
movq (%rax), %rcx
testq %rcx, %rcx
je 0x9c5f
movq %rcx, %rax
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x9c4f
addq $0x8, %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
cmpb $0x0, 0x24(%rsp)
je 0x9c9a
movq 0x8(%r13), %rbx
movq 0x10(%r13), %r14
movl $0x1, %edi
movl $0x30, %esi
callq 0x10d0
movl $0x41, (%rax)
movq %r14, 0x20(%rax)
movl $0x1, 0x28(%rax)
movq %rax, 0x8(%rbx)
movq 0x10(%r13), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x18(%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq