// File: /workspace/github_c_makefile_star2_O1/masyagin1998[P]GCI/src/lexer/lexer.c
// Function: lexer_next_token
void lexer_next_token(lexer_type_t lexer, struct TOKEN**tok)
{    
    struct POS*cur = &(lexer->cur);
    struct POS cur_next;
    (*tok) = NULL;

    while (!pos_is_eof(cur)) {
        int found_eof = 0;
        while (pos_is_whitespace(cur) || pos_is_newline(cur)) {
            (*cur) = pos_next(cur);
            if (pos_is_eof(cur)) {
                found_eof = 1;
                break;
            }
        }

        if (found_eof) {
            break;
        }

        cur_next = pos_next(cur);

        /* keywords */
        switch (pos_get_code(cur)) {
        case 'f': {
            if (pos_check_keyword(cur, function_keyword, function_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_FUNCTION, function_keyword, function_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'l': {
            if (pos_check_keyword(cur, let_keyword, let_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_LET, let_keyword, let_keyword_len);   
            } else if (pos_check_keyword(cur, len_keyword, len_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_LEN, len_keyword, len_keyword_len);
            }else {
                goto eat_ident;
            }
            break;
        }
        case 'i': {
            if (pos_check_keyword(cur, if_keyword, if_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_IF, if_keyword, if_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'e': {
            if (pos_check_keyword(cur, else_keyword, else_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_ELSE, else_keyword, else_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'w': {
            if (pos_check_keyword(cur, while_keyword, while_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_WHILE, while_keyword, while_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'b': {
            if (pos_check_keyword(cur, break_keyword, break_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_BREAK, break_keyword, break_keyword_len);
            } else {
                goto eat_ident;
            }            
            break;
        }
        case 'c': {
            if (pos_check_keyword(cur, continue_keyword, continue_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_CONTINUE, continue_keyword, continue_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'a': {
            if (pos_check_keyword(cur, append_keyword, append_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_APPEND, append_keyword, append_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'd': {
            if (pos_check_keyword(cur, delete_keyword, delete_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_DELETE, delete_keyword, delete_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'h': {
            if (pos_check_keyword(cur, has_property_keyword, has_property_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_HAS_PROPERTY, has_property_keyword, has_property_keyword_len);
            } else {
                goto eat_ident;
            }
            break;
        }
        case 'r': {
            if (pos_check_keyword(cur, return_keyword, return_keyword_len)) {
                token_read_keyword(tok, cur, TOKEN_TYPE_RETURN, return_keyword, return_keyword_len);
            } else {
                goto eat_ident;
            }            
            break;
        }

            /* operators */
        case '|': {
            if (pos_get_code(&cur_next) == '|') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_OR, cur, &cur_next);
            }
            break;
        }
        case '&': {
            if (pos_get_code(&cur_next) == '&') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_AND, cur, &cur_next);
            }            
            break;
        }
        case '=': {
            if (pos_get_code(&cur_next) == '=') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_EQEQ, cur, &cur_next);
            } else {
                token_read_op(tok, TOKEN_TYPE_EQ, cur, &cur_next);
            }
            break;            
        }
        case '!': {
            if (pos_get_code(&cur_next) == '=') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_NEQ, cur, &cur_next);
            }
            break;            
        }
        case '<': {
            if (pos_get_code(&cur_next) == '=') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_LE, cur, &cur_next);
            } else {
                token_read_op(tok, TOKEN_TYPE_LT, cur, &cur_next);
            }
            break;            
        }
        case '>': {
            if (pos_get_code(&cur_next) == '=') {
                cur_next = pos_next(&cur_next);
                token_read_op(tok, TOKEN_TYPE_GE, cur, &cur_next);
            } else {
                token_read_op(tok, TOKEN_TYPE_GT, cur, &cur_next);
            }            
            break;            
        }
        case '+': {
            token_read_op(tok, TOKEN_TYPE_PLUS, cur, &cur_next);
            break;            
        }
        case '-': {
            token_read_op(tok, TOKEN_TYPE_MINUS, cur, &cur_next);
            break;            
        }
        case '*': {
            token_read_op(tok, TOKEN_TYPE_MUL, cur, &cur_next);
            break;            
        }
        case '/': {
            token_read_op(tok, TOKEN_TYPE_DIV, cur, &cur_next);
            break;            
        }
        case '%': {
            token_read_op(tok, TOKEN_TYPE_MOD, cur, &cur_next);
            break;            
        }
        case '(': {
            token_read_op(tok, TOKEN_TYPE_LPAREN, cur, &cur_next);
            break;            
        }
        case ')': {
            token_read_op(tok, TOKEN_TYPE_RPAREN, cur, &cur_next);
            break;            
        }
        case '[': {
            token_read_op(tok, TOKEN_TYPE_LBRACKET, cur, &cur_next);            
            break;
        }
        case ']': {
            token_read_op(tok, TOKEN_TYPE_RBRACKET, cur, &cur_next);
            break;
        }
        case '{': {
            token_read_op(tok, TOKEN_TYPE_LBRACE, cur, &cur_next);
            break;            
        }
        case '}': {
            token_read_op(tok, TOKEN_TYPE_RBRACE, cur, &cur_next);
            break;            
        }
        case ',': {
            token_read_op(tok, TOKEN_TYPE_COMMA, cur, &cur_next);
            break;            
        }
        case ';': {
            token_read_op(tok, TOKEN_TYPE_SEMI, cur, &cur_next);
            break;            
        }
        case '.': {
            token_read_op(tok, TOKEN_TYPE_DOT, cur, &cur_next);
            break;            
        }
        case ':': {
            token_read_op(tok, TOKEN_TYPE_COLON, cur, &cur_next);
            break;            
        }
        default: {
            if (pos_is_digit(cur)) {
                enum LEXER_CODES r = token_read_number(tok, cur);
                if (r == LEXER_INVALID_TOKEN) {
                    (*tok)->token_type = TOKEN_TYPE_UNKNOWN;
                    (*tok)->group_type = GROUP_TYPE_AUX;
                }
            } else if (pos_is_letter(cur)) {
            eat_ident:
                token_read_ident(tok, cur);
            } else {
                token_read_unknown(tok, cur);
            }
            break;
        }
        }

        (*cur) = (*tok)->frag.following;
        if ((*tok)->token_type == TOKEN_TYPE_UNKNOWN) {
            fprintf(stderr, "%s:%zu:%zu: warning: unknown token ‘‘\n",
                    lexer->program_name,
                    (*tok)->frag.starting.line,
                    (*tok)->frag.starting.pos);
            token_free((*tok));
        } else {
            return;
        }
    }

    /* EOF */
    (*tok) = create_token();

    (*tok)->token_type = TOKEN_TYPE_EOF;
    (*tok)->group_type = GROUP_TYPE_AUX;
    (*tok)->frag.starting = (*cur);
    (*tok)->frag.following = (*cur);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x110(%rdi), %rbx
movq $0x0, (%rsi)
addq $0x10, %r15
leaq 0x8(%rsp), %r12
leaq 0xae61(%rip), %r13      # 0xf448
leaq 0x30(%rsp), %rcx
movq %rcx, %rbp
movq %rbx, %rdi
callq 0x53b8
testl %eax, %eax
jne 0x4cbb
movq %rbx, %rdi
callq 0x53d4
testl %eax, %eax
jne 0x4617
movq %rbx, %rdi
callq 0x53fa
testl %eax, %eax
je 0x4655
movq %r12, %rdi
movq %rbx, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x20(%rbx)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movups %xmm1, 0x10(%rbx)
movups %xmm0, (%rbx)
movq %rbx, %rdi
callq 0x53b8
testl %eax, %eax
je 0x45ff
movl $0x3, %eax
movq %rbp, %rcx
jmp 0x4c9a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %rbx, %rdi
callq 0x53ee
leal -0x21(%rax), %ecx
cmpl $0x1d, %ecx
jbe 0x46b1
addl $-0x5b, %eax
cmpl $0x22, %eax
ja 0x48bc
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq %r14, %rdi
movl $0x1e, %esi
jmp 0x4b2d
leaq 0xad18(%rip), %rdx      # 0xf3d0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %rbp, %rdi
callq 0x53ee
cmpl $0x3d, %eax
jne 0x4c70
movq %r12, %rdi
movq %rbp, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0x10, %esi
jmp 0x4add
movq %r14, %rdi
movl $0x23, %esi
jmp 0x4b2d
movq %rbp, %rdi
callq 0x53ee
cmpl $0x26, %eax
jne 0x4c70
movq %r12, %rdi
movq %rbp, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0xe, %esi
jmp 0x4add
movq %rbp, %r13
movq %rbp, %rdi
callq 0x53ee
cmpl $0x3d, %eax
jne 0x4be5
movq %r12, %rdi
movq %r13, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0x14, %esi
jmp 0x4c01
movq %r14, %rdi
movl $0x1a, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x22, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x1b, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x1c, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x25, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x18, %esi
jmp 0x4b2d
movq %rbp, %r13
movq %rbp, %rdi
callq 0x53ee
cmpl $0x3d, %eax
jne 0x4bef
movq %r12, %rdi
movq %r13, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0xf, %esi
jmp 0x4c01
movq %r14, %rdi
movl $0x19, %esi
jmp 0x4b2d
movq %rbp, %r13
movq %rbp, %rdi
callq 0x53ee
cmpl $0x3d, %eax
jne 0x4bf9
movq %r12, %rdi
movq %r13, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0x13, %esi
jmp 0x4c01
movq %r14, %rdi
movl $0x24, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x16, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x17, %esi
jmp 0x4b2d
movq %rbx, %rdi
callq 0x5425
testl %eax, %eax
je 0x4c15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4eee
cmpl $-0x1, %eax
jne 0x4c70
movq (%r14), %rax
movabsq $0x400000027, %rcx      # imm = 0x400000027
movq %rcx, (%rax)
jmp 0x4c70
movl $0x5, %edx
movq %rbx, %rdi
leaq 0xabe5(%rip), %rsi      # 0xf4e9
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x5, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x4, %edx
leaq 0xabc0(%rip), %rcx      # 0xf4e9
jmp 0x4c5e
movl $0x6, %edx
movq %rbx, %rdi
leaq 0xabc1(%rip), %rsi      # 0xf4fe
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x6, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x7, %edx
leaq 0xab9c(%rip), %rcx      # 0xf4fe
jmp 0x4c5e
movq %r14, %rdi
movl $0x21, %esi
jmp 0x4b2d
movq %r14, %rdi
movl $0x1f, %esi
jmp 0x4b2d
movl $0x8, %edx
movq %rbx, %rdi
leaq 0xab44(%rip), %rsi      # 0xf4d4
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x8, %r8d
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
leaq 0xab22(%rip), %rcx      # 0xf4d4
jmp 0x4c5e
movl $0x5, %edx
movq %rbx, %rdi
leaq 0xab29(%rip), %rsi      # 0xf4ef
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x5, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x5, %edx
leaq 0xab04(%rip), %rcx      # 0xf4ef
jmp 0x4c5e
movl $0x8, %edx
movq %rbx, %rdi
leaq 0xaaf6(%rip), %rsi      # 0xf4f5
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x8, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x6, %edx
leaq 0xaad1(%rip), %rcx      # 0xf4f5
jmp 0x4c5e
movl $0x6, %edx
movq %rbx, %rdi
leaq 0xaae5(%rip), %rsi      # 0xf51d
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x6, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0xb, %edx
leaq 0xaac0(%rip), %rcx      # 0xf51d
jmp 0x4c5e
movl $0x6, %edx
movq %rbx, %rdi
leaq 0xaa94(%rip), %rsi      # 0xf505
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x6, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x8, %edx
leaq 0xaa6f(%rip), %rcx      # 0xf505
jmp 0x4c5e
movq %rbp, %rdi
callq 0x53ee
cmpl $0x7c, %eax
jne 0x4c70
movq %r12, %rdi
movq %rbp, %rsi
callq 0x54d1
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movl $0xd, %esi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0xa95e(%rip), %r13      # 0xf448
jmp 0x4b33
movl $0x3, %edx
movq %rbx, %rdi
leaq 0xa9e2(%rip), %rsi      # 0xf4dd
callq 0x5555
testl %eax, %eax
je 0x4c2e
movl $0x3, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
leaq 0xa9bd(%rip), %rcx      # 0xf4dd
jmp 0x4c5e
movq %r14, %rdi
movl $0x20, %esi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x5187
jmp 0x4c70
movl $0x2, %edx
movq %rbx, %rdi
leaq 0xa995(%rip), %rsi      # 0xf4e1
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x2, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2, %edx
leaq 0xa970(%rip), %rcx      # 0xf4e1
jmp 0x4c5e
movl $0x4, %edx
movq %rbx, %rdi
leaq 0xa95f(%rip), %rsi      # 0xf4e4
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x4, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %edx
leaq 0xa93a(%rip), %rcx      # 0xf4e4
jmp 0x4c5e
movl $0xc, %edx
movq %rbx, %rdi
leaq 0xa94e(%rip), %rsi      # 0xf50c
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0xc, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x9, %edx
leaq 0xa929(%rip), %rcx      # 0xf50c
jmp 0x4c5e
movq %r14, %rdi
movl $0x12, %esi
jmp 0x4c01
movq %r14, %rdi
movl $0x15, %esi
jmp 0x4c01
movq %r14, %rdi
movl $0x11, %esi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x5187
leaq 0xa835(%rip), %r13      # 0xf448
jmp 0x4c70
movq %rbx, %rdi
callq 0x543b
testl %eax, %eax
jne 0x4c65
movq %r14, %rdi
movq %rbx, %rsi
callq 0x51e5
jmp 0x4c70
movl $0x3, %edx
movq %rbx, %rdi
leaq 0xa8dc(%rip), %rsi      # 0xf519
callq 0x5555
testl %eax, %eax
je 0x4c65
movl $0x3, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl $0xa, %edx
leaq 0xa8bb(%rip), %rcx      # 0xf519
callq 0x4e28
jmp 0x4c70
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5091
movq (%r14), %rax
movq 0x50(%rax), %rcx
movq %rcx, 0x20(%rbx)
movups 0x30(%rax), %xmm0
movups 0x40(%rax), %xmm1
movups %xmm1, 0x10(%rbx)
movups %xmm0, (%rbx)
movq (%r14), %rdi
movl $0x1, %eax
cmpl $0x27, (%rdi)
movq %rbp, %rcx
je 0x4ca4
testl %eax, %eax
je 0x45ec
jmp 0x4cb6
movq %r15, %rsi
movq %r14, %rdx
callq 0x14e6
movq %rbp, %rcx
xorl %eax, %eax
jmp 0x4c9a
cmpl $0x3, %eax
jne 0x4d03
xorl %eax, %eax
callq 0x4e0c
movq %rax, (%r14)
movabsq $0x400000026, %rcx      # imm = 0x400000026
movq %rcx, (%rax)
movq 0x20(%rbx), %rcx
movq %rcx, 0x28(%rax)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups %xmm1, 0x18(%rax)
movups %xmm0, 0x8(%rax)
movq (%r14), %rax
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups %xmm1, 0x40(%rax)
movq 0x20(%rbx), %rcx
movq %rcx, 0x50(%rax)
movups %xmm0, 0x30(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq