#include <stdbool.h>
#include <stdio.h>
#include "lexer.h"
int ch;
int
nextCh(void)
{
    ch = getchar();
    return ch;
}
bool
isWhiteSpace(int ch)
{
    return ch == ' ' || ch == '\t';
}
bool
isDecDigit(int ch)
{
    return ch >= '0' && ch <= '9';
}
bool
isOctDigit(int ch)
{
    return ch >= '0' && ch <= '7';
}
bool
isHexDigit(int ch)
{
    return isDecDigit(ch) || (ch >= 'a' && ch <= 'f') ||
           (ch >= 'A' && ch <= 'F');
}
int
getToken(void)
{
    // init ch, skip white spaces and newlines
    while (ch == 0 || isWhiteSpace(ch) || ch == '\n') {
        nextCh();
    }
    if (ch == EOF) {
        return 0; // EOI
    } else if (isDecDigit(ch)) {
        // parse literal
        if (ch == '0') {
            nextCh();
            if (ch == 'x') {
                nextCh();
                if (isHexDigit(ch)) {
                    while (isHexDigit(ch)) {
                        nextCh();
                    }
                    return 2; // HEX_LITERAL
                }
                return 1; // BAD_TOKEN
            }
            while (isOctDigit(ch)) {
                ch -= '0';
                nextCh();
            }
            return 3; // OCT_LITERAL
        } else if (isDecDigit(ch)) {
            while (isDecDigit(ch)) {
                nextCh();
            }
            return 4; // DEC_LITERAL
        }
    } if (ch == '+') {
        nextCh();
        return 5; // PLUS
    }
    nextCh();
    return 1; // BAD_TOKEN
}