Skip to content

Commit d5d6682

Browse files
committed
Modernize lexer with compound literals
Adapt changes from commit 191c6ac to the compound-literal branch. This replaces verbose index-based array initialization with cleaner compound literal arrays: - Convert directive initialization to use array compound literal - Convert keyword initialization to use array compound literal
1 parent 5e3a6f8 commit d5d6682

File tree

1 file changed

+41
-68
lines changed

1 file changed

+41
-68
lines changed

src/lexer.c

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#define NUM_DIRECTIVES 11
1515
#define NUM_KEYWORDS 16
1616

17+
/* Token mapping structure for elegant initialization */
18+
typedef struct {
19+
char *name;
20+
token_t token;
21+
} token_mapping_t;
22+
1723
/* Preprocessor directive hash table using existing shecc hashmap */
1824
hashmap_t *DIRECTIVE_MAP = NULL;
1925
/* C keywords hash table */
@@ -29,41 +35,25 @@ void lex_init_directives()
2935

3036
DIRECTIVE_MAP = hashmap_create(16); /* Small capacity for directives */
3137

32-
/* Initialization using indexed for-loop */
38+
/* Initialization using struct compound literals for elegance */
3339
directive_tokens_storage =
3440
arena_alloc(GENERAL_ARENA, NUM_DIRECTIVES * sizeof(token_t));
3541

36-
char *names[NUM_DIRECTIVES];
37-
token_t token_values[NUM_DIRECTIVES];
38-
39-
/* Populate arrays using index-based assignments for compatibility */
40-
names[0] = "#define";
41-
token_values[0] = T_cppd_define;
42-
names[1] = "#elif";
43-
token_values[1] = T_cppd_elif;
44-
names[2] = "#else";
45-
token_values[2] = T_cppd_else;
46-
names[3] = "#endif";
47-
token_values[3] = T_cppd_endif;
48-
names[4] = "#error";
49-
token_values[4] = T_cppd_error;
50-
names[5] = "#if";
51-
token_values[5] = T_cppd_if;
52-
names[6] = "#ifdef";
53-
token_values[6] = T_cppd_ifdef;
54-
names[7] = "#ifndef";
55-
token_values[7] = T_cppd_ifndef;
56-
names[8] = "#include";
57-
token_values[8] = T_cppd_include;
58-
names[9] = "#pragma";
59-
token_values[9] = T_cppd_pragma;
60-
names[10] = "#undef";
61-
token_values[10] = T_cppd_undef;
42+
/* Use array compound literal for directive mappings */
43+
token_mapping_t directives[] = {
44+
{"#define", T_cppd_define}, {"#elif", T_cppd_elif},
45+
{"#else", T_cppd_else}, {"#endif", T_cppd_endif},
46+
{"#error", T_cppd_error}, {"#if", T_cppd_if},
47+
{"#ifdef", T_cppd_ifdef}, {"#ifndef", T_cppd_ifndef},
48+
{"#include", T_cppd_include}, {"#pragma", T_cppd_pragma},
49+
{"#undef", T_cppd_undef},
50+
};
6251

6352
/* hashmap insertion */
6453
for (int i = 0; i < NUM_DIRECTIVES; i++) {
65-
directive_tokens_storage[i] = token_values[i];
66-
hashmap_put(DIRECTIVE_MAP, names[i], &directive_tokens_storage[i]);
54+
directive_tokens_storage[i] = directives[i].token;
55+
hashmap_put(DIRECTIVE_MAP, directives[i].name,
56+
&directive_tokens_storage[i]);
6757
}
6858
}
6959

@@ -74,51 +64,34 @@ void lex_init_keywords()
7464

7565
KEYWORD_MAP = hashmap_create(32); /* Capacity for keywords */
7666

77-
/* Initialization using indexed for-loop */
67+
/* Initialization using struct compound literals for elegance */
7868
keyword_tokens_storage =
7969
arena_alloc(GENERAL_ARENA, NUM_KEYWORDS * sizeof(token_t));
8070

81-
char *names[NUM_KEYWORDS];
82-
token_t token_values[NUM_KEYWORDS];
83-
84-
/* Populate arrays using index-based assignments for compatibility */
85-
names[0] = "if";
86-
token_values[0] = T_if;
87-
names[1] = "while";
88-
token_values[1] = T_while;
89-
names[2] = "for";
90-
token_values[2] = T_for;
91-
names[3] = "do";
92-
token_values[3] = T_do;
93-
names[4] = "else";
94-
token_values[4] = T_else;
95-
names[5] = "return";
96-
token_values[5] = T_return;
97-
names[6] = "typedef";
98-
token_values[6] = T_typedef;
99-
names[7] = "enum";
100-
token_values[7] = T_enum;
101-
names[8] = "struct";
102-
token_values[8] = T_struct;
103-
names[9] = "sizeof";
104-
token_values[9] = T_sizeof;
105-
names[10] = "switch";
106-
token_values[10] = T_switch;
107-
names[11] = "case";
108-
token_values[11] = T_case;
109-
names[12] = "break";
110-
token_values[12] = T_break;
111-
names[13] = "default";
112-
token_values[13] = T_default;
113-
names[14] = "continue";
114-
token_values[14] = T_continue;
115-
names[15] = "union";
116-
token_values[15] = T_union;
71+
/* Use array compound literal for keyword mappings */
72+
token_mapping_t keywords[] = {
73+
{"if", T_if},
74+
{"while", T_while},
75+
{"for", T_for},
76+
{"do", T_do},
77+
{"else", T_else},
78+
{"return", T_return},
79+
{"typedef", T_typedef},
80+
{"enum", T_enum},
81+
{"struct", T_struct},
82+
{"sizeof", T_sizeof},
83+
{"switch", T_switch},
84+
{"case", T_case},
85+
{"break", T_break},
86+
{"default", T_default},
87+
{"continue", T_continue},
88+
{"union", T_union},
89+
};
11790

11891
/* hashmap insertion */
11992
for (int i = 0; i < NUM_KEYWORDS; i++) {
120-
keyword_tokens_storage[i] = token_values[i];
121-
hashmap_put(KEYWORD_MAP, names[i], &keyword_tokens_storage[i]);
93+
keyword_tokens_storage[i] = keywords[i].token;
94+
hashmap_put(KEYWORD_MAP, keywords[i].name, &keyword_tokens_storage[i]);
12295
}
12396
}
12497

0 commit comments

Comments
 (0)