51 lines
1.6 KiB
C
51 lines
1.6 KiB
C
#include "token.h"
|
|
|
|
KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = {
|
|
{ "(", LParen}, { ")", RParen}, { ",", Comma }, { ".", Dot },
|
|
{ "-", Minus }, { "+", Plus }, { ";", Semicolon }, { "/", Slash}, { "*", Star },
|
|
{ "!", Bang }, { "!=", Bang_Equal }, { "=", Equal }, { "==", Equal_Equal },
|
|
{ ">", Greater }, { ">=", Greater_Equal }, { "<", Less }, { "<=", Less_Equal },
|
|
{ "and", AND }, { "class", CLASS }, { "else", ELSE },
|
|
{ "false", FALSE }, { "for", FOR }, { "fun", FUN },
|
|
{ "if", IF }, { "nil", NIL }, { "or", OR },
|
|
{ "print", PRINT }, { "return", RETURN }, { "super", SUPER },
|
|
{ "this", THIS }, { "true", TRUE }, { "var", VAR }, { "while", WHILE },
|
|
{ "", EndOF }
|
|
};
|
|
|
|
const char* GetLexemeMapping(TokenType);
|
|
|
|
Token* CreateToken(const char* lexeme, int line, TokenType type) {
|
|
Token* token = calloc(1, sizeof(Token));
|
|
|
|
if (!token) {
|
|
fprintf(stderr, "Failed to calloc token. %s", strerror(errno));
|
|
return NULL;
|
|
}
|
|
|
|
if (lexeme) token->lexeme = lexeme;
|
|
else {
|
|
const char* mapping_result = GetLexemeMapping(type);
|
|
|
|
if (!mapping_result) {
|
|
fprintf(stderr, "Failed to get the mapping for %s\n", lexeme);
|
|
free(token);
|
|
return NULL;
|
|
}
|
|
|
|
token->lexeme = mapping_result;
|
|
}
|
|
|
|
token->line = line;
|
|
token->type = type;
|
|
|
|
return token;
|
|
}
|
|
|
|
const char* GetLexemeMapping(TokenType type) {
|
|
for (int i = 0; i < TOKENTYPE_MAPPINGS_COUNT; i++) {
|
|
if (TokenTypeMappings[i].type == type) return TokenTypeMappings[i].lexeme;
|
|
}
|
|
|
|
return NULL;
|
|
} |