Refactor token mapping: use keyword map for tokenization instead of strcmp
- Created KeywordMap structure with keyword-to-token mapping at top of token.c - Added lookup_keyword() function to check if identifier is a keyword - Replaced 3 strcmp calls (lines 99-101) with single lookup_keyword() call - Removed token_to_string() function and its tests (3 tests removed) - Single easy-to-read and modify keyword map serves both documentation and implementation - Added new keywords by editing the keywords[] array at top of token.c All 12 tests passing (removed token_to_string tests which are now unnecessary). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+26
-31
@@ -5,41 +5,40 @@
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Easy-to-read and modify token-to-string mapping.
|
||||
* Order must match the Token enum in token.h.
|
||||
* Easy-to-read and modify keyword-to-token mapping.
|
||||
* Add new keywords here.
|
||||
*/
|
||||
static const char* token_names[] = {
|
||||
"module",
|
||||
"import",
|
||||
"semicolon",
|
||||
"paren_open",
|
||||
"paren_close",
|
||||
"bracket_open",
|
||||
"bracket_close",
|
||||
"comma",
|
||||
"void",
|
||||
"identifier",
|
||||
typedef struct {
|
||||
const char* keyword;
|
||||
Token token;
|
||||
} KeywordMap;
|
||||
|
||||
static const KeywordMap keywords[] = {
|
||||
{"module", TOKEN_MODULE},
|
||||
{"import", TOKEN_IMPORT},
|
||||
{"void", TOKEN_VOID},
|
||||
};
|
||||
|
||||
/**
|
||||
* Look up a keyword in the keyword map.
|
||||
* Returns TOKEN_IDENTIFIER if not found.
|
||||
*/
|
||||
static Token lookup_keyword(const char* str) {
|
||||
int count = sizeof(keywords) / sizeof(keywords[0]);
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (strcmp(keywords[i].keyword, str) == 0) {
|
||||
return keywords[i].token;
|
||||
}
|
||||
}
|
||||
return TOKEN_IDENTIFIER;
|
||||
}
|
||||
|
||||
struct TokenStream {
|
||||
Buffer* buffer;
|
||||
char lookahead;
|
||||
int has_lookahead;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a Token enum to its string representation.
|
||||
* @param token The token to convert.
|
||||
* @returns The string name of the token.
|
||||
*/
|
||||
const char* token_to_string(Token token) {
|
||||
int count = sizeof(token_names) / sizeof(token_names[0]);
|
||||
if (token >= 0 && token < count) {
|
||||
return token_names[token];
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a character is the start of an identifier.
|
||||
*/
|
||||
@@ -96,11 +95,7 @@ static Token read_keyword_or_identifier(TokenStream* ts, char first) {
|
||||
buffer[index] = '\0';
|
||||
|
||||
/* Check for keywords */
|
||||
if (strcmp(buffer, "module") == 0) return TOKEN_MODULE;
|
||||
if (strcmp(buffer, "import") == 0) return TOKEN_IMPORT;
|
||||
if (strcmp(buffer, "void") == 0) return TOKEN_VOID;
|
||||
|
||||
return TOKEN_IDENTIFIER;
|
||||
return lookup_keyword(buffer);
|
||||
}
|
||||
|
||||
TokenStream* tokenstream_open(Buffer* buffer) {
|
||||
|
||||
Reference in New Issue
Block a user