// File: /workspace/github_c_makefile_star2_O0/Yamsman[P]scc/src/lex.c
// Function: lex_next
void lex_next(lexer *lx) {
	token t = {-1, -1, lx->tgt->loc, 0, NULL};
	lex_target *tgt = lx->tgt;
	s_pos loc = tgt->loc;

	//Use ungotten tokens
	token_n *pre = lx->pre;
	if (pre != NULL) {
		lx->ahead = pre->t;
		lx->pre = pre->next;
		free(pre);
		return;
	}

	//Skip whitespace
	//Count the beginning of a file as a newline
	//When the lexer is reset (when a new context is created) it begins from here
	int nline = (tgt->pos == tgt->buf && tgt->type == TGT_FILE);
reset:	nline |= lex_wspace(lx);
	tgt = lx->tgt;
	t.nline = nline;
	loc = tgt->loc;

	//Process the next character
	t.loc = tgt->loc;
	int noadvc = 0;
	switch (lex_cur(lx)) {
		case '\0':
			//End of stream
			t.type = TOK_END;
			lx->ahead = t;
			return;
		case ';': 	t.type = TOK_SEM; 	break;
		case ':':	t.type = TOK_COL;	break;
		case ',':	t.type = TOK_CMM; 	break;
		case '.':
			if (isdigit(lex_peekc(lx))) goto num_case;
			t.type = TOK_PRD; 
			break;
		case '{':	t.type = TOK_LBR; 	break;
		case '}':	t.type = TOK_RBR; 	break;
		case '(':	t.type = TOK_LPR; 	break;
		case ')':	t.type = TOK_RPR; 	break;
		case '[':	t.type = TOK_LBK;	break;
		case ']':	t.type = TOK_RBK;	break;
		case '"':
			lex_str(lx, &t);
			noadvc = 1;
			break;
		case '\'':
			//Character literal
			lex_advc(lx);

			//Process escape character
			char val = lex_cur(lx);
			if (val == '\\') {
				lex_advc(lx);
				val = lex_eschar(lx);
			} else {
				lex_advc(lx);
			}

			//Check for closing '
			if (lex_cur(lx) == '\'') {
				lex_advc(lx);
			} else {
				c_error(&loc, "Multiple characters in character literal\n");
			}

			//Make integer type
			s_type *ty = type_new(TYPE_INT);
			ty->size = SIZE_CHAR;
			ty->is_signed = 1;
			t.dtype = ty;

			t.dat.ival = val;
			t.type = TOK_CONST;
			noadvc = 1;
			break;
		case '#':
			t.type = TOK_SNS;
			lx->m_exp = 0;
			if (lex_peekc(lx) == '#') {
				lex_advc(lx);
				t.type = TOK_DNS;
				c_error(&loc, "Stray '##' in source\n");
			} else if (!nline && loc.col != 1) {
				c_error(&loc, "Stray '#' in source\n");
			}
			lex_advc(lx); //Advance manually; do not attempt to expand directive
			noadvc = 1;
			lx->m_exp = 1;
			break;
		case '?': 	t.type = TOK_QMK; 	break;
		case '+':
			//if (isdigit(*cur)) goto num_case;
			t.type = TOK_ADD;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_ADD;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '+') {
				t.type = TOK_INC;
				lex_advc(lx);
			}
			break;
		case '-':
			//if (isdigit(*cur)) goto num_case;
			t.type = TOK_SUB;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_SUB;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '-') {
				t.type = TOK_DEC;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '>') {
				t.type = TOK_PTR;
				lex_advc(lx);
			}
			break;
		case '*':
			t.type = TOK_ASR;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_MUL;
				lex_advc(lx);
			}
			break;
		case '/': 
			t.type = TOK_DIV;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_DIV;
				lex_advc(lx);
			}
			break;
		case '%':
			t.type = TOK_MOD;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_MOD;
				lex_advc(lx);
			}
			break;
		case '&':
			t.type = TOK_AND;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_AND;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '&') {
				t.type = TOK_LOGIC_AND;
				lex_advc(lx);
			}
			break;
		case '|':
			t.type = TOK_OR;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_OR;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '|') {
				t.type = TOK_LOGIC_OR;
				lex_advc(lx);
			}
			break;
		case '^':
			t.type = TOK_XOR;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_ASSIGN_XOR;
				lex_advc(lx);
			}
			break;
		case '~':
			t.type = TOK_TLD;
			break;
		case '=':
			t.type = TOK_ASSIGN;
			if (lex_peekc(lx) == '=')
				t.type = TOK_EQ;
			lex_advc(lx);
			break;
		case '!':
			t.type = TOK_NOT;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_NEQ;
				lex_advc(lx);
			}
			break;
		case '<':
			t.type = TOK_LTH;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_LEQ;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '<') {
				t.type = TOK_LSHIFT;
				lex_advc(lx);
				if (lex_peekc(lx) == '=') {
					t.type = TOK_ASSIGN_LSHIFT;
					lex_advc(lx);
				}
			}
			break;
		case '>':
			t.type = TOK_GTH;
			if (lex_peekc(lx) == '=') {
				t.type = TOK_GEQ;
				lex_advc(lx);
			} else if (lex_peekc(lx) == '<') {
				t.type = TOK_RSHIFT;
				lex_advc(lx);
				if (lex_peekc(lx) == '=') {
					t.type = TOK_ASSIGN_RSHIFT;
					lex_advc(lx);
				}
			}
			break;
		case 'a': case 'b': case 'c': case 'd': case 'e':
		case 'f': case 'g': case 'h': case 'i': case 'j':
		case 'k': case 'l': case 'm': case 'n': case 'o':
		case 'p': case 'q': case 'r': case 's': case 't':
		case 'u': case 'v': case 'w': case 'x': case 'y':
		case 'z':
		case 'A': case 'B': case 'C': case 'D': case 'E':
		case 'F': case 'G': case 'H': case 'I': case 'J':
		case 'K': case 'L': case 'M': case 'N': case 'O':
		case 'P': case 'Q': case 'R': case 'S': case 'T':
		case 'U': case 'V': case 'W': case 'X': case 'Y':
		case 'Z': case '_': {
			//Read identifier
			lex_ident(lx, &t);

			//Check if matches keyword
			//String is not freed yet; could be used as macro name
			int kw_id = (long long)map_get(&kw_table, t.dat.sval); 
			if (kw_id > 0)
				t.type = kw_id;

			//If the token is an identifier, free the string
			if (kw_id != 0) {
				free(t.dat.sval);
				t.dat.sval = NULL;
			}
			noadvc = 1;
			} break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			//Read number
num_case:		lex_num(lx, &t);
			t.type = TOK_CONST;
			noadvc = 1;
			break;
		default: 
			//illegal char
			t.type = TOK_END;
			break;
	}
	//Unless the specific case handled it manually, advance to the next char
end:	if (!noadvc)
		lex_advc(lx);
	lx->ahead = t;
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x30(%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x30(%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x8eda
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movq -0x68(%rbp), %rsi
movl $0x30, %edx
callq 0x3160
movq -0x68(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rdi
callq 0x3030
jmp 0x95e2
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x40(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x85(%rbp)
jne 0x8f05
movq -0x40(%rbp), %rax
cmpl $0x0, 0x8(%rax)
sete %al
movb %al, -0x85(%rbp)
movb -0x85(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x6c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x95f0
movl %eax, %ecx
movl -0x6c(%rbp), %eax
orl %ecx, %eax
movl %eax, -0x6c(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rcx
movq %rcx, -0x50(%rbp)
movups 0x28(%rax), %xmm0
movaps %xmm0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rcx
movq %rcx, -0x20(%rbp)
movups 0x28(%rax), %xmm0
movups %xmm0, -0x30(%rbp)
movl $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81b0
movsbl %al, %eax
movl %eax, %ecx
movq %rcx, -0x90(%rbp)
subl $0x7e, %eax
ja 0x95b4
movq -0x90(%rbp), %rax
leaq 0x8b72(%rip), %rcx      # 0x11b04
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
leaq -0x38(%rbp), %rsi
movl $0x30, %edx
callq 0x3160
jmp 0x95e2
movl $0x1, -0x38(%rbp)
jmp 0x95bb
movl $0x2, -0x38(%rbp)
jmp 0x95bb
movl $0x3, -0x38(%rbp)
jmp 0x95bb
callq 0x3240
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movb %al, %cl
movq -0x98(%rbp), %rax
movsbl %cl, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x901b
jmp 0x9597
movl $0x4, -0x38(%rbp)
jmp 0x95bb
movl $0x5, -0x38(%rbp)
jmp 0x95bb
movl $0x6, -0x38(%rbp)
jmp 0x95bb
movl $0x7, -0x38(%rbp)
jmp 0x95bb
movl $0x8, -0x38(%rbp)
jmp 0x95bb
movl $0x9, -0x38(%rbp)
jmp 0x95bb
movl $0xa, -0x38(%rbp)
jmp 0x95bb
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x8cb0
movl $0x1, -0x70(%rbp)
jmp 0x95bb
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
callq 0x81b0
movb %al, -0x71(%rbp)
movsbl -0x71(%rbp), %eax
cmpl $0x5c, %eax
jne 0x90bd
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
callq 0x89c0
movb %al, -0x71(%rbp)
jmp 0x90c6
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
callq 0x81b0
movsbl %al, %eax
cmpl $0x27, %eax
jne 0x90e2
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x90f4
leaq -0x60(%rbp), %rdi
leaq 0x8f7f(%rip), %rsi      # 0x1206c
movb $0x0, %al
callq 0x3880
movl $0x6, %edi
callq 0xf720
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x80(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x80(%rbp), %rax
movq %rax, -0x10(%rbp)
movsbq -0x71(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x33, -0x38(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x95bb
movl $0xd, -0x38(%rbp)
movq -0x8(%rbp), %rax
movl $0x0, 0x68(%rax)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x23, %eax
jne 0x9183
movq -0x8(%rbp), %rdi
callq 0x81f0
movl $0xe, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x8f1c(%rip), %rsi      # 0x12096
movb $0x0, %al
callq 0x3880
jmp 0x91a3
cmpl $0x0, -0x6c(%rbp)
jne 0x91a1
cmpl $0x1, -0x5c(%rbp)
je 0x91a1
leaq -0x60(%rbp), %rdi
leaq 0x8f12(%rip), %rsi      # 0x120ac
movb $0x0, %al
callq 0x3880
jmp 0x91a3
movq -0x8(%rbp), %rdi
callq 0x81f0
movl $0x1, -0x70(%rbp)
movq -0x8(%rbp), %rax
movl $0x1, 0x68(%rax)
jmp 0x95bb
movl $0xf, -0x38(%rbp)
jmp 0x95bb
movl $0x10, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x91f9
movl $0x28, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x921c
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x2b, %eax
jne 0x921a
movl $0x22, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x921c
jmp 0x95bb
movl $0x11, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x924b
movl $0x29, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x9293
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x2d, %eax
jne 0x926e
movl $0x23, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x9291
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3e, %eax
jne 0x928f
movl $0x24, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x9291
jmp 0x9293
jmp 0x95bb
movl $0x12, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x92c0
movl $0x2a, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x13, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x92ed
movl $0x2b, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x14, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x931a
movl $0x2c, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x15, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x9349
movl $0x2d, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x936c
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x26, %eax
jne 0x936a
movl $0x20, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x936c
jmp 0x95bb
movl $0x16, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x939b
movl $0x2e, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x93be
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x7c, %eax
jne 0x93bc
movl $0x1f, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x93be
jmp 0x95bb
movl $0x17, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x93eb
movl $0x2f, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x18, -0x38(%rbp)
jmp 0x95bb
movl $0x27, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x941b
movl $0x1d, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x21, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x9451
movl $0x1e, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x95bb
movl $0x19, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x9480
movl $0x1b, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x94c6
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3c, %eax
jne 0x94c4
movl $0x25, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x94c2
movl $0x30, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x94c4
jmp 0x94c6
jmp 0x95bb
movl $0x1a, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x94f5
movl $0x1c, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x953b
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3c, %eax
jne 0x9539
movl $0x26, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
callq 0x8280
movsbl %al, %eax
cmpl $0x3d, %eax
jne 0x9537
movl $0x31, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
jmp 0x9539
jmp 0x953b
jmp 0x95bb
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x82b0
movq -0x18(%rbp), %rsi
leaq 0xdf5b(%rip), %rdi      # 0x174b0
callq 0x10460
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jle 0x9572
movl -0x84(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x958c
movq -0x18(%rbp), %rdi
callq 0x3030
movq $0x0, -0x18(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x95bb
jmp 0x9597
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x8390
movl $0x33, -0x38(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x95bb
movl $0x0, -0x38(%rbp)
jmp 0x95bd
cmpl $0x0, -0x70(%rbp)
jne 0x95cc
movq -0x8(%rbp), %rdi
callq 0x81f0
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
leaq -0x38(%rbp), %rsi
movl $0x30, %edx
callq 0x3160
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)