// File: /workspace/github_cpp_makefile_star2_O2/ssubrandon[P]460Project2/LexicalAnalyzer.cpp
// Function: LexicalAnalyzer::GetToken()
token_type LexicalAnalyzer::GetToken ()
{
	error_type = 1;
	lexeme = "";
	int state = 1;
	token_type token = NONE;
	map <string, token_type>::iterator itr;
	if (input.fail())
	{
		debug << '\t' << setw(16) << left << "EOF_T" << endl;
		p1file << '\t' << setw(16) << left << "EOF_T" << endl;
		return EOF_T;
	}
	static string valid = "acdrb0.()+-*/'=<>?_\"";
	while (isspace(line[pos]) || line[pos] == ';') // end of line or beginning of comment
		if (line[pos] == ';' || ++pos >= line.length())
		{
			getline (input, line);
			if (input.fail())
			{
				debug << '\t' << setw(16) << left << "EOF_T" << endl;
				p1file << '\t' << setw(16) << left << "EOF_T" << endl;
				return EOF_T;
			}
			linenum++;
			listing << setw(4) << right << linenum << ": " << line << endl;	
			debug << setw(4) << right << linenum << ": " << line << endl;	
			line += ' ';
			pos = 0;
		}
	while (token == NONE)
	{
		char c = line[pos++];
		lexeme += c;
		if (state == 12 && pos == line.length())
		{ // used if end of line found in a string literal
			token = ERROR_T;
			error_type = 2;
			break;	
		}
		if (isalpha(c) && (c != 'a' && c != 'c' && c != 'd' && c != 'r')) 
			c = 'b';
		else if (isdigit(c))
			c = '0';
		int col = 0;
		while (col < valid.length() && valid[col] != c)
			col++;
		state = table[state][col];
		switch (state)
		{
		    case 20: // car, cdr, cadr, cddr, and other list operators
			pos--;
			//cout << lexeme << endl;
			lexeme = lexeme.erase(lexeme.length()-1,1);
			//cout << lexeme << endl;
			token = LISTOP_T;
			break;
		    case 21: // symbol or keyword
			pos--;
			lexeme = lexeme.erase(lexeme.length()-1,1);
			itr = symbols.find (lexeme);
			if (itr != symbols.end())
				token = itr->second;
			else 
				token = IDENT_T;
			break;
		    case 22: // numeric literal
			pos--;
			lexeme = lexeme.erase(lexeme.length()-1,1);
			token = NUMLIT_T;
			break;
		    case 23: // operator/symbol with backup
			pos--;
			lexeme = lexeme.erase(lexeme.length()-1,1);
		    case 24: // operator/symbol without backup
			itr = symbols.find (lexeme);
			if (itr != symbols.end())
				token = itr->second;
			else if (lexeme[lexeme.length()-1] == '?') // allow ? at end of identifier
			{
				pos--;
				lexeme = lexeme.erase(lexeme.length()-1,1);
				token = IDENT_T;
			}
			else
				token = ERROR_T;
			break;
		    case 25: // string literal
			token = STRLIT_T;
			break;
		    case 31: // error with backup
			pos--;
			lexeme = lexeme.erase(lexeme.length()-1,1);
		    case 32: // error
			token = ERROR_T;
		}
		if (token == ERROR_T)
			switch (error_type)
			{
				case 1: ReportError (string("Invalid character found: ") + lexeme);
					break;
				case 2: ReportError (string("Unterminated string literal: ") + lexeme);
					break;
				default: ReportError (string("Unidentified error: ") + lexeme);
			}
	}
	debug << '\t' << setw(16) << left << token_names[token] << lexeme << endl;
	p1file << '\t' << setw(16) << left << token_names[token] << lexeme << endl;
	return token;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movl $0x1, 0x850(%rdi)
leaq 0x830(%rdi), %r14
leaq 0x500f(%rip), %rsi      # 0x8635
movq %r14, %rdi
callq 0x2370
movq 0x400(%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x420(%rbx,%rax)
je 0x36f1
pushq $0x9
popq %r14
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
pushq $0x10
popq %r13
movq %r13, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %r12d      # imm = 0xFFFFFF4F
movl 0x18(%rax,%rcx), %edx
andl %r12d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x4f73(%rip), %r15      # 0x85f8
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
movq %rax, %rdi
callq 0x2130
addq $0x608, %rbx            # imm = 0x608
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq %r13, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
andl 0x18(%rax,%rcx), %r12d
orl $0x20, %r12d
movl %r12d, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
movq %rax, %rdi
callq 0x2130
pushq $0x23
popq %r15
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, -0x48(%rbp)
movb 0x8fa5(%rip), %al       # 0xc6a0
testb %al, %al
je 0x3c7a
leaq 0x400(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq 0x808(%rbx), %r12
leaq 0x200(%rbx), %rax
movq %rax, -0x38(%rbp)
movl 0x82c(%rbx), %r13d
movslq %r13d, %rax
movq (%r12), %rcx
movb (%rcx,%rax), %r14b
movsbl %r14b, %edi
callq 0x2060
testl %eax, %eax
je 0x375d
cmpb $0x3b, %r14b
je 0x3767
incl %r13d
movl %r13d, 0x82c(%rbx)
movslq %r13d, %rax
cmpq %rax, 0x810(%rbx)
ja 0x3727
jmp 0x3767
cmpb $0x3b, %r14b
jne 0x3873
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x2080
movq 0x400(%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x420(%rbx,%rax)
jne 0x3647
movl 0x828(%rbx), %esi
incl %esi
movl %esi, 0x828(%rbx)
movq 0x200(%rbx), %rax
movq -0x18(%rax), %rcx
pushq $0x4
popq %r14
movq %r14, 0x210(%rbx,%rcx)
movq -0x18(%rax), %rax
movl 0x218(%rbx,%rax), %ecx
movl $0xffffff4f, %r13d      # imm = 0xFFFFFF4F
andl %r13d, %ecx
movl $0x80, %edx
orl %edx, %ecx
movl %ecx, 0x218(%rbx,%rax)
movq -0x38(%rbp), %rdi
callq 0x2320
movq %rax, %rdi
movq %rbx, %r15
leaq 0x4f3a(%rip), %rbx      # 0x8723
movq %rbx, %rsi
callq 0x21f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2130
movq (%r15), %rax
movq -0x18(%rax), %rcx
movq %r14, 0x10(%r15,%rcx)
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl %r13d, %ecx
movl $0x80, %edx
orl %edx, %ecx
movl %ecx, 0x18(%r15,%rax)
movl 0x828(%r15), %esi
movq %r15, %rdi
callq 0x2320
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rbx
callq 0x21f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2130
movq %r12, %rdi
pushq $0x20
popq %rsi
callq 0x21b0
andl $0x0, 0x82c(%r15)
xorl %r13d, %r13d
jmp 0x3727
leaq 0x858(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq 0x860(%rbx), %rax
movq %rax, -0x38(%rbp)
pushq $0x1
popq %rax
pushq $-0x1
popq %r15
movl %eax, %r12d
cmpl $-0x1, %r15d
jne 0x3bbf
movslq 0x82c(%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x82c(%rbx)
movq 0x808(%rbx), %rcx
movsbl (%rcx,%rax), %r14d
movq -0x48(%rbp), %rdi
movl %r14d, %esi
callq 0x21b0
cmpl $0xc, %r12d
jne 0x38df
movslq 0x82c(%rbx), %rax
cmpq %rax, 0x810(%rbx)
je 0x3bb1
movl %r14d, %edi
callq 0x22f0
movl %r14d, %ecx
andb $-0x3, %cl
leal -0x30(%r14), %edx
cmpl $0xa, %edx
movl %r14d, %edx
pushq $0x30
popq %rsi
cmovbl %esi, %edx
testl %eax, %eax
movl %edx, %eax
pushq $0x62
popq %rsi
cmovnel %esi, %eax
cmpb $0x61, %cl
cmovel %edx, %eax
cmpb $0x64, %r14b
cmovel %edx, %eax
cmpb $0x72, %r14b
cmovel %edx, %eax
movq 0x8d66(%rip), %rdx      # 0xc688
movq 0x8d57(%rip), %rsi      # 0xc680
movq %rdx, %rdi
negq %rdi
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %r14
leaq (%rdi,%rcx), %r8
cmpq $-0x1, %r8
je 0x3950
movsbl 0x1(%rsi,%rcx), %r8d
incq %rcx
cmpl %r8d, %eax
jne 0x3936
jmp 0x3953
movq %rdx, %rcx
movslq %r12d, %rax
imulq $0x54, %rax, %rax
leaq 0x46ff(%rip), %rdx      # 0x8060
addq %rdx, %rax
movl (%rax,%rcx,4), %r12d
leal -0x14(%r12), %eax
pushq $-0x1
popq %r15
cmpl $0xc, %eax
ja 0x3893
leaq 0x469f(%rip), %rcx      # 0x8020
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
decl 0x82c(%rbx)
movq 0x838(%rbx), %rsi
decq %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
pushq $0x9
popq %r15
jmp 0x3893
decl 0x82c(%rbx)
movq 0x838(%rbx), %rsi
decq %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq 0x4390
pushq $0x1
popq %r15
cmpq -0x38(%rbp), %rax
jne 0x3a90
jmp 0x3893
decl 0x82c(%rbx)
movq 0x838(%rbx), %rsi
decq %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
pushq $0x2
popq %r15
jmp 0x3893
decl 0x82c(%rbx)
movq 0x838(%rbx), %rsi
decq %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
jmp 0x3ae6
decl 0x82c(%rbx)
movq 0x838(%rbx), %rsi
decq %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq 0x4390
cmpq -0x38(%rbp), %rax
je 0x3aa9
movl 0x40(%rax), %r15d
cmpl $0x22, %r15d
jne 0x3893
jmp 0x3ae6
pushq $0x3
popq %r15
jmp 0x3893
movq 0x830(%rbx), %rax
movq 0x838(%rbx), %rsi
cmpb $0x3f, -0x1(%rax,%rsi)
jne 0x3ae6
decq %rsi
decl 0x82c(%rbx)
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2140
pushq $0x1
popq %r15
jmp 0x3893
movl 0x850(%rbx), %eax
cmpl $0x2, %eax
je 0x3b2f
cmpl $0x1, %eax
leaq -0x88(%rbp), %r15
jne 0x3b68
movq %r15, %rdi
leaq 0x4c05(%rip), %rsi      # 0x870c
leaq -0x29(%rbp), %rdx
callq 0x3e7a
leaq -0x68(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x401f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3d18
jmp 0x3b98
leaq -0x88(%rbp), %r15
movq %r15, %rdi
leaq 0x4be6(%rip), %rsi      # 0x8726
leaq -0x29(%rbp), %rdx
callq 0x3e7a
leaq -0x68(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x401f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3d18
jmp 0x3b98
movq %r15, %rdi
leaq 0x4bd2(%rip), %rsi      # 0x8744
leaq -0x29(%rbp), %rdx
callq 0x3e7a
leaq -0x68(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x401f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3d18
movq %r13, %rdi
callq 0x2398
movq %r15, %rdi
callq 0x2398
pushq $0x22
popq %r15
jmp 0x3893
movl $0x2, 0x850(%rbx)
pushq $0x22
popq %r15
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
pushq $0x10
popq %rsi
movq %rsi, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %r13d      # imm = 0xFFFFFF4F
movl 0x18(%rax,%rcx), %edx
andl %r13d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movslq %r15d, %rcx
shlq $0x5, %rcx
leaq 0x8601(%rip), %r12      # 0xc200
addq %rcx, %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x21d0
movq %rax, %rdi
movq -0x48(%rbp), %r14
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2130
addq $0x608, %rbx            # imm = 0x608
movq %rbx, %rdi
pushq $0x9
popq %rsi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
pushq $0x10
popq %rsi
movq %rsi, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
andl 0x18(%rax,%rcx), %r13d
orl $0x20, %r13d
movl %r13d, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r12, %rsi
callq 0x21d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2130
jmp 0x36df
leaq 0x8a1f(%rip), %rdi      # 0xc6a0
callq 0x2350
testl %eax, %eax
je 0x3703
leaq 0x89eb(%rip), %rdi      # 0xc680
leaq 0x4a5b(%rip), %rsi      # 0x86f7
leaq -0x68(%rbp), %rdx
callq 0x3e7a
movq 0x831c(%rip), %rdi      # 0xbfc8
leaq 0x89cd(%rip), %rsi      # 0xc680
leaq 0x8516(%rip), %rdx      # 0xc1d0
callq 0x21c0
leaq 0x89da(%rip), %rdi      # 0xc6a0
callq 0x2170
jmp 0x3703
movq %rax, %rbx
leaq 0x89c6(%rip), %rdi      # 0xc6a0
callq 0x2160
jmp 0x3d0f
jmp 0x3ced
jmp 0x3cfb
jmp 0x3d0c
jmp 0x3ced
jmp 0x3cfb
jmp 0x3d0c
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x2398
jmp 0x3cfe
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2398
jmp 0x3d0f
movq %rax, %rbx
movq %rbx, %rdi
callq 0x2330
nop