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:
@@ -13,25 +13,6 @@ static void write_test_file(const char* filename, const char* content) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_token_to_string_keywords(void) {
|
||||
if (strcmp(token_to_string(TOKEN_MODULE), "module") != 0) fail("module");
|
||||
if (strcmp(token_to_string(TOKEN_IMPORT), "import") != 0) fail("import");
|
||||
if (strcmp(token_to_string(TOKEN_VOID), "void") != 0) fail("void");
|
||||
}
|
||||
|
||||
static void test_token_to_string_symbols(void) {
|
||||
if (strcmp(token_to_string(TOKEN_SEMICOLON), "semicolon") != 0) fail("semicolon");
|
||||
if (strcmp(token_to_string(TOKEN_PARENT_OPEN), "paren_open") != 0) fail("paren_open");
|
||||
if (strcmp(token_to_string(TOKEN_PARENT_CLOSE), "paren_close") != 0) fail("paren_close");
|
||||
if (strcmp(token_to_string(TOKEN_BRACKET_OPEN), "bracket_open") != 0) fail("bracket_open");
|
||||
if (strcmp(token_to_string(TOKEN_BRACKET_CLOSE), "bracket_close") != 0) fail("bracket_close");
|
||||
if (strcmp(token_to_string(TOKEN_COMMA), "comma") != 0) fail("comma");
|
||||
}
|
||||
|
||||
static void test_token_to_string_identifier(void) {
|
||||
if (strcmp(token_to_string(TOKEN_IDENTIFIER), "identifier") != 0) fail("identifier");
|
||||
}
|
||||
|
||||
static void test_tokenstream_open_fail(void) {
|
||||
Buffer* buf = buffer_open_file("v0/does_not_exist.c2");
|
||||
if (buf != NULL) fail("expected NULL for non-existent file");
|
||||
|
||||
Reference in New Issue
Block a user