// File: nicklausw[P]snesasm/snesasm.cpp
// Function: lexer()
int lexer()
{
  // the crazy part, tokens
  unsigned int counter = 0; // counter again
  int current_token = 0; // vector location value
  bool ct_used = false; // current token usage
  
  // we need the token vector big enough
  // for the first token, obviously
  tokens.resize(1);
  
  
  while (counter < ins.length()) {
    // handle newlines
    if (ins[counter] == '\n') {
      if (ct_used == false) {
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkNL;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkNL;
        current_token++;
      }
      continue;
    }
    
    if (ins[counter] == ',') {
      if (ct_used == false) {
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkCOMMA;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkCOMMA;
        current_token++;
      }
      continue;
    }
    
    if (ins[counter] == ' ') {
      if (ct_used == false) {
        counter++;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
      }
      continue;
    }
    
    
    // no newline means do a token
    
    if (ins[counter] == 0x0D) {
      // CR LF
      counter++;
      continue;
    }
    
    
    if (ins[counter] == '.') {
      // directive
      ct_used = true;
      tokens[current_token].token_type = tkDIR;
      counter++; // skip period
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (ins[counter] == '\"') {
      // quote!
      ct_used = true;
      tokens[current_token].token_type = tkQUOTE;
      
      // quotes get their own append function
      
      unsigned int str_length = ins.length() - 1;
      
      counter++;
  
      while (ins[counter] != '\"') {
        if (counter == str_length) {
          cerr << "error: unclosed quote\n";
          return fail;
        }
    
        tokens[current_token].token_i.append(string(1, ins[counter]));
        counter++;
      }
      
      tokens[current_token].token_i.append(string(1, '\"'));
      
      counter++;
      
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (isalpha(ins[counter])) {
      // opcode
      ct_used = true;
      tokens[current_token].token_type = tkOP;
      
      counter = append_token(counter, current_token);
      
      if (tokens[current_token].token_i[tokens[current_token].token_i.length() - 1] == ':')
        tokens[current_token].token_type = tkLB; // it's a label
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (isdigit(ins[counter]) || ins[counter] == '$' || ins[counter] == '%' || ins[counter] == '-') {
      // number
      ct_used = true;
      tokens[current_token].token_type = tkNUM;
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (ins[counter] == '#' || ins[counter] == '(') {
      // opcode arg
      ct_used = true;
      tokens[current_token].token_type = tkARG;
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else {
      // unknown
      ct_used = true;
      tokens[current_token].token_type = tkUNDEF;
      
      counter = append_token(counter, current_token);
      
      // no need for a counter++ here, it's handled above.
      continue;
    }
    
    // be sure to move on to next symbol.
    counter++;
  }
  
  return success;
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movl $0x0, 0x1c0(%rsp)
movl $0x0, 0x1bc(%rsp)
movb $0x0, 0x1bb(%rsp)
leaq 0xdd7c(%rip), %rdi      # 0x16248
movl $0x1, %esi
callq 0xd260
movl 0x1c0(%rsp), %eax
movq %rax, 0x90(%rsp)
leaq 0xdd7c(%rip), %rdi      # 0x16268
callq 0x22c0
movq %rax, %rcx
movq 0x90(%rsp), %rax
cmpq %rcx, %rax
jae 0x8fe0
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xdd53(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x8646
movb 0x1bb(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x85a4
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdcd8(%rip), %rdi      # 0x16248
callq 0xd260
movl 0xdc79(%rip), %eax      # 0x161f4
movl %eax, 0x8c(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xdcb7(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x8c(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x8641
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdc8d(%rip), %rdi      # 0x16248
callq 0xd260
movb $0x0, 0x1bb(%rsp)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdc47(%rip), %rdi      # 0x16248
callq 0xd260
movl 0xdbe8(%rip), %eax      # 0x161f4
movl %eax, 0x88(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xdc26(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x88(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x84d6
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xdc12(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x2c, %eax
jne 0x8787
movb 0x1bb(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x86e5
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdb97(%rip), %rdi      # 0x16248
callq 0xd260
movl 0xdb3c(%rip), %eax      # 0x161f8
movl %eax, 0x84(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xdb76(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x84(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x8782
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdb4c(%rip), %rdi      # 0x16248
callq 0xd260
movb $0x0, 0x1bb(%rsp)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xdb06(%rip), %rdi      # 0x16248
callq 0xd260
movl 0xdaab(%rip), %eax      # 0x161f8
movl %eax, 0x80(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xdae5(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x80(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x84d6
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xdad1(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x8813
movb 0x1bb(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x87c8
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x880e
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
cltq
imulq $0x28, %rax, %rsi
leaq 0xda69(%rip), %rdi      # 0x16248
callq 0xd260
movb $0x0, 0x1bb(%rsp)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x84d6
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xda45(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x8846
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x84d6
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xda12(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x896e
movb $0x1, 0x1bb(%rsp)
movl 0xd96f(%rip), %eax      # 0x161e4
movl %eax, 0x6c(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd9c0(%rip), %rdi      # 0x16248
movq %rdi, 0x70(%rsp)
callq 0xd300
movl 0x6c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c0(%rsp), %eax
incl %eax
movl %eax, 0x1c0(%rsp)
movl 0x1c0(%rsp), %edi
movl 0x1bc(%rsp), %esi
callq 0xc010
movq 0x70(%rsp), %rdi
movl %eax, 0x1c0(%rsp)
movslq 0x1bc(%rsp), %rsi
callq 0xd300
movq %rax, %rsi
addq $0x8, %rsi
leaq 0x178(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x20d0
movq 0x78(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0xc1b0
jmp 0x8901
movslq 0x1bc(%rsp), %rsi
leaq 0xd938(%rip), %rdi      # 0x16248
callq 0xd300
movq %rax, %rdi
addq $0x8, %rdi
leaq 0x198(%rsp), %rsi
callq 0x2210
leaq 0x198(%rsp), %rdi
callq 0x23a8
leaq 0x178(%rsp), %rdi
callq 0x23a8
jmp 0x84d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x23a8
jmp 0x8ffa
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd8ea(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x22, %eax
jne 0x8c06
movb $0x1, 0x1bb(%rsp)
movl 0xd85f(%rip), %eax      # 0x161fc
movl %eax, 0x68(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd898(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x68(%rsp), %ecx
movl %ecx, (%rax)
leaq 0xd8a6(%rip), %rdi      # 0x16268
callq 0x22c0
subq $0x1, %rax
movl %eax, 0x168(%rsp)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd875(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x22, %eax
je 0x8b2c
movl 0x1c0(%rsp), %eax
cmpl 0x168(%rsp), %eax
jne 0x8a37
movq 0xd5d5(%rip), %rdi      # 0x15ff0
leaq 0x87ad(%rip), %rsi      # 0x111cf
callq 0x21b0
movl $0x1, 0x1c4(%rsp)
jmp 0x8feb
movslq 0x1bc(%rsp), %rsi
leaq 0xd802(%rip), %rdi      # 0x16248
callq 0xd300
addq $0x8, %rax
movq %rax, 0x50(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd804(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
movl %eax, 0x5c(%rsp)
leaq 0x147(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x2330
movl 0x5c(%rsp), %edx
movq 0x60(%rsp), %rcx
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x2130
jmp 0x8a9f
movq 0x50(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x2380
jmp 0x8ab3
leaq 0x148(%rsp), %rdi
callq 0x23a8
leaq 0x147(%rsp), %rdi
callq 0x21d0
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x89e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x8b1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x23a8
leaq 0x147(%rsp), %rdi
callq 0x21d0
jmp 0x8ffa
movslq 0x1bc(%rsp), %rsi
leaq 0xd70d(%rip), %rdi      # 0x16248
callq 0xd300
addq $0x8, %rax
movq %rax, 0x40(%rsp)
leaq 0x11f(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2330
movq 0x48(%rsp), %rcx
leaq 0x120(%rsp), %rdi
movl $0x1, %esi
movl $0x22, %edx
callq 0x2130
jmp 0x8b79
movq 0x40(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x2380
jmp 0x8b8d
leaq 0x120(%rsp), %rdi
callq 0x23a8
leaq 0x11f(%rsp), %rdi
callq 0x21d0
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x84d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x8bf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x23a8
leaq 0x11f(%rsp), %rdi
callq 0x21d0
jmp 0x8ffa
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd652(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %edi
callq 0x22e0
cmpl $0x0, %eax
je 0x8cf1
movb $0x1, 0x1bb(%rsp)
movl 0xd5ae(%rip), %eax      # 0x161e8
movl %eax, 0x34(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd5fb(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x34(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c0(%rsp), %edi
movl 0x1bc(%rsp), %esi
callq 0xc010
movl %eax, 0x1c0(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd5c7(%rip), %rdi      # 0x16248
callq 0xd300
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd5aa(%rip), %rdi      # 0x16248
callq 0xd300
movq %rax, %rdi
addq $0x8, %rdi
callq 0x22c0
movq 0x38(%rsp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x2390
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0x8cec
movl 0xd51e(%rip), %eax      # 0x161ec
movl %eax, 0x30(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd567(%rip), %rdi      # 0x16248
callq 0xd300
movl 0x30(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x84d6
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd567(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %edi
callq 0x2190
cmpl $0x0, %eax
jne 0x8d6e
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd545(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x24, %eax
je 0x8d6e
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd528(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x25, %eax
je 0x8d6e
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd50b(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x8e65
movb $0x1, 0x1bb(%rsp)
movl 0xd464(%rip), %eax      # 0x161e0
movl %eax, 0x1c(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd4b9(%rip), %rdi      # 0x16248
movq %rdi, 0x20(%rsp)
callq 0xd300
movl 0x1c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c0(%rsp), %edi
movl 0x1bc(%rsp), %esi
callq 0xc010
movq 0x20(%rsp), %rdi
movl %eax, 0x1c0(%rsp)
movslq 0x1bc(%rsp), %rsi
callq 0xd300
movq %rax, %rsi
addq $0x8, %rsi
leaq 0xd8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x20d0
movq 0x28(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0xc1b0
jmp 0x8df8
movslq 0x1bc(%rsp), %rsi
leaq 0xd441(%rip), %rdi      # 0x16248
callq 0xd300
movq %rax, %rdi
addq $0x8, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x2210
leaq 0xf8(%rsp), %rdi
callq 0x23a8
leaq 0xd8(%rsp), %rdi
callq 0x23a8
jmp 0x84d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x23a8
jmp 0x8ffa
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd3f3(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x8ea3
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0xd3d6(%rip), %rdi      # 0x16268
callq 0x2390
movsbl (%rax), %eax
cmpl $0x28, %eax
jne 0x8f97
movb $0x1, 0x1bb(%rsp)
movl 0xd33f(%rip), %eax      # 0x161f0
movl %eax, 0x4(%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd384(%rip), %rdi      # 0x16248
movq %rdi, 0x8(%rsp)
callq 0xd300
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c0(%rsp), %edi
movl 0x1bc(%rsp), %esi
callq 0xc010
movq 0x8(%rsp), %rdi
movl %eax, 0x1c0(%rsp)
movslq 0x1bc(%rsp), %rsi
callq 0xd300
movq %rax, %rsi
addq $0x8, %rsi
leaq 0x98(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x20d0
movq 0x10(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0xc1b0
jmp 0x8f2d
movslq 0x1bc(%rsp), %rsi
leaq 0xd30c(%rip), %rdi      # 0x16248
callq 0xd300
movq %rax, %rdi
addq $0x8, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x2210
leaq 0xb8(%rsp), %rdi
callq 0x23a8
leaq 0x98(%rsp), %rdi
callq 0x23a8
jmp 0x84d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x23a8
jmp 0x8ffa
movb $0x1, 0x1bb(%rsp)
movl 0xd2bb(%rip), %eax      # 0x16260
movl %eax, (%rsp)
movslq 0x1bc(%rsp), %rsi
leaq 0xd291(%rip), %rdi      # 0x16248
callq 0xd300
movl (%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c0(%rsp), %edi
movl 0x1bc(%rsp), %esi
callq 0xc010
movl %eax, 0x1c0(%rsp)
jmp 0x84d6
movl $0x0, 0x1c4(%rsp)
movl 0x1c4(%rsp), %eax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
movq 0x170(%rsp), %rdi
callq 0x2320
nopw (%rax,%rax)