#include <stdio.h>
#include <ctype.h>
#include <string.h>

typedef enum {
    TOK_UNKNOWN,
    TOK_OPERATOR
} TokenType;

typedef struct {
    TokenType type;
    char lexeme[3];   
} Token;

static int is_op_start(char c) {
    return c=='!' || c=='=' || c=='<' || c=='>' || c=='+' || c=='-' || c=='*' || c=='/';
}

Token getToken(const char *s, int *i) {
    Token tok;
    tok.type = TOK_UNKNOWN;
    tok.lexeme[0] = '\0';

    while (s[*i] && isspace((unsigned char)s[*i])) (*i)++;

    char c = s[*i];
    if (!c) {
        tok.type = TOK_UNKNOWN;
        return tok;
    }

    if (c=='!' || c=='=' || c=='<' || c=='>') {
        char c2 = s[*i+1];
        if (c=='!' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "!="); *i += 2; return tok; }
        if (c=='=' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "=="); *i += 2; return tok; }
        if (c=='<' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "<="); *i += 2; return tok; }
        if (c=='>' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, ">="); *i += 2; return tok; }

        // If your lexer supports single-char versions of these:
        if (c=='<' ) { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "<");  *i += 1; return tok; }
        if (c=='>' ) { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, ">");  *i += 1; return tok; }


    }


    if (c=='+' || c=='-' || c=='*' || c=='/') {
        tok.type = TOK_OPERATOR;
        tok.lexeme[0] = c;
        tok.lexeme[1] = '\0';
        *i += 1;
        return tok;
    }

    *i += 1;
    return tok;
}



int main() {
    char input[1000];
    int i = 0;

    printf("Enter a simple C code snippet: ");
    fgets(input, sizeof(input), stdin);

    printf("\nTokens Found:\n");

    while (input[i] != '\0') {

        if (isspace(input[i])) {
            i++;
            continue;
        }

        if (
            (input[i] == '+' && input[i+1] == '=') ||
            (input[i] == '-' && input[i+1] == '=') ||
            (input[i] == '*' && input[i+1] == '=') ||
            (input[i] == '/' && input[i+1] == '=') ||
            (input[i] == '=' && input[i+1] == '=') ||
            (input[i] == '!' && input[i+1] == '=') ||
            (input[i] == '>' && input[i+1] == '=') ||
            (input[i] == '<' && input[i+1] == '=')
        ) {
            printf("OPERATOR: %c%c\n", input[i], input[i+1]);
            i += 2;
        }

        else if (input[i] == '+' || input[i] == '-' ||
                 input[i] == '*' || input[i] == '/' ||
                 input[i] == '=' || input[i] == '>' ||
                 input[i] == '<' || input[i] == '!') {
            printf("OPERATOR: %c\n", input[i]);
            i++;
        }
        else {
            i++; 
        }
    }

    return 0;
}