// File: jaldhar[P]Lets-Build-a-Simple-Interpreter/calc1.cc
// Function: Interpreter::get_next_token()
Token Interpreter::get_next_token() {
    // Skip leading white space.
    while (_pos < _text.length() && isspace(_text[_pos])) {
        _pos++;
    }

    // is _pos index past the end of _text ?
    // if so, then return ENDOFFILE token because there is no more
    // input left to convert into tokens
    if (_pos >= _text.length()) {
        Token token(TOKENTYPE::ENDOFFILE, 0);
        cerr << token << endl;
        return token;
    }

    // get a character at the position _pos and decide what token to create
    // based on the single character
    char current_char = _text[_pos];

    // if the next characters are digits then convert them to an integer, create
    // an INTEGER token, increment _pos index to point to the next character
    // after the digits, and return the INTEGER token
    if (isdigit(current_char)) {
        long total = 0;
        while (isdigit(_text[_pos])) {
            total *= 10;
            total += (_text[_pos] - '0');
            _pos++;
        }
        Token token(TOKENTYPE::INTEGER, total);
        cerr << token << endl;
        return token;
    }

    if (current_char == '+') {
        Token token(TOKENTYPE::PLUS, '+');
        cerr << token << endl;
        _pos++;
        return token;
    }

    if (current_char == '-') {
        Token token(TOKENTYPE::MINUS, '-');
        cerr << token << endl;
        _pos++;
        return token;
    }

    ostringstream out;
    out << "Error parsing input. Got: " << current_char;
    throw(out.str().c_str());
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x1f0(%rbp)
movq (%rax), %rdi
callq 0x2160
movq -0x1f0(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x1e1(%rbp)
jae 0x25e2
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x21c0
movsbl (%rax), %edi
callq 0x2030
cmpl $0x0, %eax
setne %al
movb %al, -0x1e1(%rbp)
movb -0x1e1(%rbp), %al
testb $0x1, %al
jne 0x25ee
jmp 0x2603
movq -0x1e0(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
jmp 0x258a
movq -0x1e0(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x1f8(%rbp)
movq (%rax), %rdi
callq 0x2160
movq %rax, %rcx
movq -0x1f8(%rbp), %rax
cmpq %rcx, %rax
jb 0x265f
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x2c70
movq 0x29ae(%rip), %rdi      # 0x4ff0
leaq -0x10(%rbp), %rsi
callq 0x23f0
movq %rax, %rdi
movq 0x295b(%rip), %rsi      # 0x4fb0
callq 0x2100
jmp 0x28df
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x21c0
movb (%rax), %al
movb %al, -0x19(%rbp)
movsbl -0x19(%rbp), %edi
callq 0x20d0
cmpl $0x0, %eax
je 0x272c
movq $0x0, -0x28(%rbp)
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x21c0
movsbl (%rax), %edi
callq 0x20d0
cmpl $0x0, %eax
je 0x26f6
movq -0x1e0(%rbp), %rax
imulq $0xa, -0x28(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x21c0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movsbl (%rcx), %ecx
subl $0x30, %ecx
movslq %ecx, %rcx
addq -0x28(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
jmp 0x2691
movq -0x28(%rbp), %rdx
leaq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x2c70
movq 0x28e1(%rip), %rdi      # 0x4ff0
leaq -0x10(%rbp), %rsi
callq 0x23f0
movq %rax, %rdi
movq 0x288e(%rip), %rsi      # 0x4fb0
callq 0x2100
jmp 0x28df
movsbl -0x19(%rbp), %eax
cmpl $0x2b, %eax
jne 0x277f
leaq -0x10(%rbp), %rdi
movl $0x2, %esi
movl $0x2b, %edx
callq 0x2c70
movq 0x28a1(%rip), %rdi      # 0x4ff0
leaq -0x10(%rbp), %rsi
callq 0x23f0
movq %rax, %rdi
movq 0x284e(%rip), %rsi      # 0x4fb0
callq 0x2100
movq -0x1e0(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
jmp 0x28df
movsbl -0x19(%rbp), %eax
cmpl $0x2d, %eax
jne 0x27d2
leaq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x2d, %edx
callq 0x2c70
movq 0x284e(%rip), %rdi      # 0x4ff0
leaq -0x10(%rbp), %rsi
callq 0x23f0
movq %rax, %rdi
movq 0x27fb(%rip), %rsi      # 0x4fb0
callq 0x2100
movq -0x1e0(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
jmp 0x28df
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x2130
movq -0x208(%rbp), %rdi
leaq 0x867(%rip), %rsi       # 0x305a
callq 0x20e0
movq %rax, -0x200(%rbp)
jmp 0x2801
movq -0x200(%rbp), %rdi
movsbl -0x19(%rbp), %esi
callq 0x2110
jmp 0x2813
movb $0x1, -0x1d1(%rbp)
movl $0x8, %edi
callq 0x2080
movq %rax, -0x210(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x20f0
jmp 0x2840
leaq -0x1d0(%rbp), %rdi
callq 0x2070
movq -0x210(%rbp), %rdi
movq %rax, (%rdi)
movb $0x0, -0x1d1(%rbp)
movq 0x2754(%rip), %rsi      # 0x4fb8
xorl %eax, %eax
movl %eax, %edx
callq 0x2190
jmp 0x28fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
jmp 0x28d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
jmp 0x28b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x2090
testb $0x1, -0x1d1(%rbp)
jne 0x28c3
jmp 0x28cf
movq -0x210(%rbp), %rdi
callq 0x20a0
jmp 0x28d1
leaq -0x1a0(%rbp), %rdi
callq 0x2050
jmp 0x28ef
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rdx
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x1a8(%rbp), %rdi
callq 0x21a0
nopl (%rax,%rax)