14
14
#define NUM_DIRECTIVES 11
15
15
#define NUM_KEYWORDS 16
16
16
17
+ /* Token mapping structure for elegant initialization */
18
+ typedef struct {
19
+ char * name ;
20
+ token_t token ;
21
+ } token_mapping_t ;
22
+
17
23
/* Preprocessor directive hash table using existing shecc hashmap */
18
24
hashmap_t * DIRECTIVE_MAP = NULL ;
19
25
/* C keywords hash table */
@@ -29,41 +35,25 @@ void lex_init_directives()
29
35
30
36
DIRECTIVE_MAP = hashmap_create (16 ); /* Small capacity for directives */
31
37
32
- /* Initialization using indexed for-loop */
38
+ /* Initialization using struct compound literals for elegance */
33
39
directive_tokens_storage =
34
40
arena_alloc (GENERAL_ARENA , NUM_DIRECTIVES * sizeof (token_t ));
35
41
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
+ };
62
51
63
52
/* hashmap insertion */
64
53
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 ]);
67
57
}
68
58
}
69
59
@@ -74,51 +64,34 @@ void lex_init_keywords()
74
64
75
65
KEYWORD_MAP = hashmap_create (32 ); /* Capacity for keywords */
76
66
77
- /* Initialization using indexed for-loop */
67
+ /* Initialization using struct compound literals for elegance */
78
68
keyword_tokens_storage =
79
69
arena_alloc (GENERAL_ARENA , NUM_KEYWORDS * sizeof (token_t ));
80
70
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
+ };
117
90
118
91
/* hashmap insertion */
119
92
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 ]);
122
95
}
123
96
}
124
97
0 commit comments