Lexer (lex.c) requires maintenance

The code needs to be heavily refactored. It'd be better done with a switch, but your repeated use of different lols, such as "lol., lol?, lol!" instead of recognizing "lol" as a token and "!" as a token is an issue in and of itself. The recapitulation that is ` if (strcmp(IdentifierStr, "lol") == 0)` - ` return TOK_RM;` is duress inducing and isn't a healthy environment for developing on top of.

Also, hey you're still alive?



This is taken from this Github Issue and was originally posted by Landon Powell on Github

                /* Returns the next token */
int get_tok(FILE *file) {
    int last_char;
    int IdentifierStr_len;

    /* eat whitespace */
    do {
        last_char = fgetc(file);
    } while (isspace(last_char));

    if (is_valid(last_char)) {
        /* read token */
        IdentifierStr_len = 0;
        do {
            IdentifierStr[IdentifierStr_len] = last_char;
            IdentifierStr_len++;
        } while (is_valid(last_char = fgetc(file)));
        IdentifierStr[IdentifierStr_len] = '\0';

        /* get token type */
        if (strcmp(IdentifierStr, "lol") == 0)
            return TOK_MV_RIGHT;
        else if (strcmp(IdentifierStr, "lol.") == 0)
            return TOK_MV_LEFT;
        else if (strcmp(IdentifierStr, "lol!") == 0)
            return TOK_INCR;
        else if (strcmp(IdentifierStr, "lol?") == 0)
            return TOK_DECR;
        else if (strcmp(IdentifierStr, "qwop") == 0)
            return TOK_OUTPUT;
        else if (strcmp(IdentifierStr, "qwop.") == 0)
            return TOK_INPUT;
        else if (strcmp(IdentifierStr, "qwop!") == 0)
            return TOK_JUMP_OPEN;
        else if (strcmp(IdentifierStr, "qwop?") == 0)
            return TOK_JUMP_CLOSED;
        else if (strcmp(IdentifierStr, "lol-qwop") == 0)
            return TOK_PRINT;
        else if (strcmp(IdentifierStr, "lol-qwop!") == 0)
            return TOK_RM;
        /* comments are ignored */
        return TOK_COMMENT;
    }

    return TOK_EOF;
}
              
Me bio pic
Language:

0 Suggestions