// File: /workspace/github_cpp_makefile_star2_O3/pixlark[P]Badge/src/lexer.cc
// Function: Lexer::next_token()
Token Lexer::next_token()
{
 reset:
	if (peek() == '\0') {
		Token token = create_token(TOKEN_EOF, 1);
		return token;
	}
	
	if (isspace(peek())) {
		advance();
		goto reset;
	}

	if (peek() == '%') {
		while (peek() != '\0' && peek() != '\n') {
			advance();
		}
		goto reset;
	}
	
	if (peek() == '"') {
		size_t start = cursor;
		advance();
		
		String_Builder builder;
		while (peek() != '"') {
			builder.add(next());
		}
		advance();
		char * s = builder.final_string();
		defer { free(s); };
		
		Token token = create_token(TOKEN_STRING_LITERAL, cursor - start);
		token.values.string = Intern::intern(s); // string literals are technically symbols
		return token;
	}
	
	if (isalpha(peek()) || peek() == '_') {
		String_Builder builder;
		while (isalnum(peek()) || peek() == '_') {
			builder.add(next());
		}
		char * s = builder.final_string();
		defer { free(s); };
		
		for (int i = 0; i < RESERVED_WORDS_COUNT; i++) {
			if (strcmp(s, reserved_words[i]) == 0) {
				return create_token((Token_Kind) (RESERVED_WORDS_BEGIN + i), builder.size());
			}
		}
		
		Token token = create_token(TOKEN_SYMBOL, builder.size());
		token.values.symbol = Intern::intern(s);
		return token;
	}

	if (isdigit(peek())) {
		String_Builder builder;
		while (isdigit(peek())) {
			builder.add(next());
		}
		char * s = builder.final_string();
		defer { free(s); };
		Token token = create_token(TOKEN_INTEGER_LITERAL, builder.size());
		token.values.integer = strtol(s, NULL, 10);
		return token;
	}
	
	switch (peek()) {
	case '+':
	case '-':
	case '*':
	case '/':
	case '.':
	case ',':
	case '(':
	case ')':
	case '{':
	case '}':
	case '@':
	case '$':
	case ':':
	case '\'':
		return create_token((Token_Kind) next(), 1);
		//return Token::with_kind((Token_Kind) next());
	case '=':
		return create_token(read_double_token('=', '=', TOKEN_EQUAL), 1);
	case '!':
		return create_token(read_double_token('!', '=', TOKEN_NOT_EQUAL), 1);
	case '>':
		return create_token(read_double_token('>', '=', TOKEN_GTE), 1);
	case '<':
		return create_token(read_double_token('<', '=', TOKEN_LTE), 1);
	case ']':
		return create_token((Token_Kind) next(), 1);
	case '[':
		advance();
		if (peek() == '-') {
			// Multi-line comment
			while (true) {
				advance();
				if (peek() == '\0') {
					fatal("Unterminated multi-line comment");
				}
				if (peek() == '-') {
					advance();
					if (peek() == ']') {
						advance();
						break;
					}
				}
			}
			goto reset;
		} else {
			return create_token((Token_Kind) '[', 1);
			//return Token::with_kind((Token_Kind) '[');
		}
	default:
		fatal_assoc(Assoc_Allocator::make(source, source_length, cursor, 1, line),
					"Line %d\nMisplaced character %c (%d)", line, peek(), peek());
		/*
	case ';':
		return Token::with_kind(read_double_token(';', ';', TOKEN_DOUBLE_SEMICOLON));*/
	}
	return (Token) {}; // @linter
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq (%rdi), %r13
movq 0x8(%rdi), %r12
movq 0x10(%rdi), %r14
movq 0x18(%rdi), %r8
cmpq %r12, %r14
jae 0x2624
movq %rdi, -0x38(%rbp)
movq %r8, -0x30(%rbp)
movsbl (%r13,%r14), %r15d
testl %r15d, %r15d
je 0x261b
movzbl %r15b, %ebx
movl %r15d, %edi
callq 0x1040
testl %eax, %eax
je 0x2479
cmpl $0xa, %ebx
jne 0x2546
movq -0x30(%rbp), %r8
incq %r8
movq -0x38(%rbp), %rbx
movq %r8, 0x18(%rbx)
jmp 0x254e
cmpl $0x25, %ebx
je 0x2563
cmpl $0x22, %ebx
je 0x26a3
movl %r15d, %edi
callq 0x11b0
testl %eax, %eax
movq -0x38(%rbp), %rbx
jne 0x27a2
cmpb $0x5f, %r15b
je 0x27a2
leal -0x30(%r15), %eax
cmpl $0x9, %eax
jbe 0x28cb
movzbl %r15b, %eax
cmpl $0x5c, %eax
jg 0x2596
cmpl $0x5b, %eax
jne 0x2644
leaq 0x1(%r14), %rax
movq %rax, 0x10(%rbx)
cmpq %r12, %rax
jae 0x29e2
cmpb $0x2d, (%r13,%rax)
jne 0x29e2
movq -0x30(%rbp), %r8
cmpq %r12, %rax
jae 0x2508
cmpb $0xa, (%r13,%rax)
jne 0x2501
incq %r8
movq %r8, 0x18(%rbx)
incq %rax
movq %rax, 0x10(%rbx)
movq %rax, %r14
cmpq %r12, %rax
jae 0x2a78
movzbl (%r13,%r14), %ecx
cmpl $0x2d, %ecx
je 0x252b
movq %r14, %rax
testl %ecx, %ecx
jne 0x24ed
jmp 0x2a78
leaq 0x1(%r14), %rax
movq %rax, 0x10(%rbx)
cmpq %r12, %rax
jae 0x24ed
cmpb $0x5d, (%r13,%rax)
jne 0x24ed
addq $0x2, %r14
jmp 0x2551
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r8
incq %r14
movq %r14, 0x10(%rbx)
cmpq %r12, %r14
jb 0x2439
jmp 0x2624
cmpq %r12, %r14
jae 0x2590
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r8
movzbl (%r13,%r14), %eax
testl %eax, %eax
je 0x2555
cmpl $0xa, %eax
je 0x2555
incq %r14
movq %r14, 0x10(%rbx)
cmpq %r14, %r12
jne 0x2570
jmp 0x2621
movq -0x30(%rbp), %r8
jmp 0x2555
cmpl $0x5d, %eax
je 0x25f2
cmpl $0x7d, %eax
je 0x2660
cmpl $0x7b, %eax
je 0x2660
movl $0x1, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x30(%rbp), %r8
callq 0x17e5
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
xorl %ecx, %ecx
cmpq 0x8(%rbx), %rsi
jae 0x25df
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movsbl (%rcx,%rsi), %ecx
leaq 0x8deb(%rip), %rsi      # 0xb3d1
movl %eax, %edi
movl %ecx, %r8d
xorl %eax, %eax
callq 0x1c52
leaq 0x1(%r14), %rax
movq %rax, 0x10(%rbx)
movl $0x1, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x30(%rbp), %r8
callq 0x17e5
movl %eax, %ebx
movl $0x5d, %r12d
jmp 0x2687
movq -0x30(%rbp), %r8
jmp 0x2624
movq %r12, %r14
decq %r14
movl $0x1, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x17e5
movl %eax, %ebx
movl $0x100, %r12d           # imm = 0x100
jmp 0x2687
addl $-0x21, %eax
cmpl $0x1f, %eax
ja 0x25ad
leaq 0x89bd(%rip), %rcx      # 0xb014
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1(%r14), %rax
movq %rax, 0x10(%rbx)
movslq %r15d, %rax
movl $0x1, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %r12
movq %r14, %rdx
movq -0x30(%rbp), %r8
callq 0x17e5
movl %eax, %ebx
shlq $0x20, %rbx
addq %r12, %rbx
movq %rbx, %rax
movq %r15, %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1(%r14), %rax
movq -0x38(%rbp), %rbx
movq %rax, 0x10(%rbx)
leaq -0x60(%rbp), %r15
movq $0x0, 0x8(%r15)
movq $0x4, 0x10(%r15)
movq 0xd906(%rip), %rax      # 0xffd0
movq %rax, 0x18(%r15)
movq 0xd8f3(%rip), %rax      # 0xffc8
movq %rax, 0x20(%r15)
movl $0x4, %edi
callq 0x1200
movq %rax, (%r15)
movq 0x8(%rbx), %r13
movq 0x10(%rbx), %r12
cmpq %r13, %r12
jae 0x270a
movq %r15, %rcx
movq (%rbx), %r15
movzbl (%r15,%r12), %eax
cmpl $0xa, %eax
je 0x270e
cmpl $0x22, %eax
jne 0x2712
jmp 0x2729
xorl %eax, %eax
jmp 0x271c
incq 0x18(%rbx)
incq %r12
movq %r12, 0x10(%rbx)
movq %rcx, %r15
movsbl %al, %esi
movq %r15, %rdi
callq 0x51c6
jmp 0x26e6
movq 0x18(%rbx), %rax
movq %rax, -0x30(%rbp)
incq %r12
movq %r12, 0x10(%rbx)
movq -0x58(%rbp), %rbx
leaq 0x1(%rbx), %rdi
callq 0x1200
movq %rax, -0x38(%rbp)
movq -0x60(%rbp), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x10b0
movq -0x38(%rbp), %rax
movb $0x0, (%rax,%rbx)
subq %r14, %r12
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rax, %r14
movq %r12, %rcx
movq -0x30(%rbp), %r8
callq 0x17e5
movl %eax, %ebx
movq %r14, %rdi
callq 0x15dd
movq %rax, %r15
movq %r14, %rdi
callq 0x11f8
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
movl $0x118, %r12d           # imm = 0x118
jmp 0x2687
leaq -0x60(%rbp), %r12
movq $0x0, 0x8(%r12)
movq $0x4, 0x10(%r12)
movq 0xd811(%rip), %rax      # 0xffd0
movq %rax, 0x18(%r12)
movq 0xd7fd(%rip), %rax      # 0xffc8
movq %rax, 0x20(%r12)
movl $0x4, %edi
callq 0x1200
movq %rax, (%r12)
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r15
cmpq %r14, %r15
jae 0x280b
movq (%rbx), %rax
movb (%rax,%r15), %r13b
movsbl %r13b, %edi
callq 0x1100
testl %eax, %eax
je 0x281b
cmpb $0xa, %r13b
jne 0x2821
incq 0x18(%rbx)
jmp 0x2821
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x1100
testl %eax, %eax
jne 0x2828
jmp 0x2836
cmpb $0x5f, %r13b
jne 0x2836
incq %r15
movq %r15, 0x10(%rbx)
movsbl %r13b, %esi
movq %r12, %rdi
callq 0x51c6
jmp 0x27de
movq -0x58(%rbp), %r13
leaq 0x1(%r13), %rdi
callq 0x1200
movq %rax, %r12
movq -0x60(%rbp), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x10b0
movq %r12, -0x30(%rbp)
movq %r13, -0x68(%rbp)
movb $0x0, (%r12,%r13)
movl $0x105, %r12d           # imm = 0x105
leaq 0xd4b1(%rip), %r13      # 0xfd20
movq (%r13), %rsi
movq -0x30(%rbp), %rdi
callq 0x1160
testl %eax, %eax
je 0x29b2
movq %r12, %rax
incq %rax
addq $0x8, %r13
movq %rax, %r12
cmpq $0x116, %rax            # imm = 0x116
jne 0x286f
movq -0x68(%rbp), %rcx
subq %rcx, %r15
movq (%rbx), %rdi
movq 0x18(%rbx), %r8
movq %r14, %rsi
movq %r15, %rdx
callq 0x17e5
movl %eax, %ebx
movq -0x30(%rbp), %rdi
callq 0x15dd
movq %rax, %r15
movl $0x116, %r12d           # imm = 0x116
jmp 0x29cd
leaq -0x60(%rbp), %r15
movq $0x0, 0x8(%r15)
movq $0x4, 0x10(%r15)
movq 0xd6ea(%rip), %rax      # 0xffd0
movq %rax, 0x18(%r15)
movq 0xd6d7(%rip), %rax      # 0xffc8
movq %rax, 0x20(%r15)
movl $0x4, %edi
callq 0x1200
movq %rax, (%r15)
movq 0x10(%rbx), %r14
movq (%rbx), %r13
movq 0x8(%rbx), %r12
cmpq %r12, %r14
jae 0x293c
movsbl (%r13,%r14), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x293c
cmpb $0xa, %al
jne 0x2928
incq 0x18(%rbx)
incq %r14
movq %r14, 0x10(%rbx)
movsbl %al, %esi
movq %r15, %rdi
callq 0x51c6
jmp 0x2902
movq -0x58(%rbp), %r15
leaq 0x1(%r15), %rdi
callq 0x1200
movq %rax, -0x30(%rbp)
movq -0x60(%rbp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x10b0
movq -0x30(%rbp), %rax
movb $0x0, (%rax,%r15)
subq %r15, %r14
movq 0x18(%rbx), %r8
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x17e5
movl %eax, %ebx
movq -0x30(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1170
movq %rax, %r14
movq %r15, %rdi
callq 0x11f8
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
movl %r14d, %r15d
movl $0x117, %r12d           # imm = 0x117
jmp 0x2687
movq -0x68(%rbp), %rcx
subq %rcx, %r15
movq (%rbx), %rdi
movq 0x18(%rbx), %r8
movq %r14, %rsi
movq %r15, %rdx
callq 0x17e5
movl %eax, %ebx
movq -0x30(%rbp), %rdi
callq 0x11f8
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
jmp 0x2687
movl $0x1, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x30(%rbp), %r8
callq 0x17e5
movl %eax, %ebx
movl $0x5b, %r12d
jmp 0x2687
movq %rbx, %rdi
movl $0x21, %esi
movl $0x3d, %edx
movl $0x102, %ecx            # imm = 0x102
jmp 0x2a54
movq %rbx, %rdi
movl $0x3d, %esi
movl $0x3d, %edx
movl $0x101, %ecx            # imm = 0x101
jmp 0x2a54
movq %rbx, %rdi
movl $0x3e, %esi
movl $0x3d, %edx
movl $0x103, %ecx            # imm = 0x103
jmp 0x2a54
movq %rbx, %rdi
movl $0x3c, %esi
movl $0x3d, %edx
movl $0x104, %ecx            # imm = 0x104
callq 0x2aee
movl %eax, %r12d
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
decq %rdx
movq 0x18(%rbx), %r8
movl $0x1, %ecx
jmp 0x2680
leaq 0x8932(%rip), %rdi      # 0xb3b1
xorl %eax, %eax
callq 0x1a00
jmp 0x2a98
jmp 0x2ae5
movq %rax, %rbx
movq -0x30(%rbp), %rdi
callq 0x11f8
jmp 0x2aba
movq %rax, %rbx
movq -0x30(%rbp), %rdi
callq 0x11f8
jmp 0x2ac8
jmp 0x2ae5
jmp 0x2ae5
movq %rax, %rbx
movq %r14, %rdi
callq 0x11f8
jmp 0x2ad6
movq %rax, %rbx
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
jmp 0x2add
jmp 0x2ae5
movq %rax, %rbx
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
jmp 0x2add
jmp 0x2ae5
movq %rax, %rbx
movq -0x60(%rbp), %rdi
callq *-0x40(%rbp)
movq %rbx, %rdi
callq 0x11c0
movq %rax, %rdi
callq 0x51b8
nop