Use hashmap for keyword lookup

This commit is contained in:
Rui Ueyama 2020-09-03 22:55:50 +09:00
parent 655954e301
commit f6944133d2
2 changed files with 32 additions and 24 deletions

26
parse.c
View File

@ -1479,18 +1479,22 @@ static void gvar_initializer(Token **rest, Token *tok, Obj *var) {
// Returns true if a given token represents a type. // Returns true if a given token represents a type.
static bool is_typename(Token *tok) { static bool is_typename(Token *tok) {
static char *kw[] = { static HashMap map;
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
"typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
"const", "volatile", "auto", "register", "restrict", "__restrict",
"__restrict__", "_Noreturn", "float", "double", "typeof", "inline",
"_Thread_local", "__thread",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) if (map.capacity == 0) {
if (equal(tok, kw[i])) static char *kw[] = {
return true; "void", "_Bool", "char", "short", "int", "long", "struct", "union",
return find_typedef(tok); "typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
"const", "volatile", "auto", "register", "restrict", "__restrict",
"__restrict__", "_Noreturn", "float", "double", "typeof", "inline",
"_Thread_local", "__thread",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
hashmap_put(&map, kw[i], (void *)1);
}
return hashmap_get2(&map, tok->loc, tok->len) || find_typedef(tok);
} }
// asm-stmt = "asm" ("volatile" | "inline")* "(" string-literal ")" // asm-stmt = "asm" ("volatile" | "inline")* "(" string-literal ")"

View File

@ -156,20 +156,24 @@ static int read_punct(char *p) {
} }
static bool is_keyword(Token *tok) { static bool is_keyword(Token *tok) {
static char *kw[] = { static HashMap map;
"return", "if", "else", "for", "while", "int", "sizeof", "char",
"struct", "union", "short", "long", "void", "typedef", "_Bool",
"enum", "static", "goto", "break", "continue", "switch", "case",
"default", "extern", "_Alignof", "_Alignas", "do", "signed",
"unsigned", "const", "volatile", "auto", "register", "restrict",
"__restrict", "__restrict__", "_Noreturn", "float", "double",
"typeof", "asm", "_Thread_local", "__thread",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) if (map.capacity == 0) {
if (equal(tok, kw[i])) static char *kw[] = {
return true; "return", "if", "else", "for", "while", "int", "sizeof", "char",
return false; "struct", "union", "short", "long", "void", "typedef", "_Bool",
"enum", "static", "goto", "break", "continue", "switch", "case",
"default", "extern", "_Alignof", "_Alignas", "do", "signed",
"unsigned", "const", "volatile", "auto", "register", "restrict",
"__restrict", "__restrict__", "_Noreturn", "float", "double",
"typeof", "asm", "_Thread_local", "__thread",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
hashmap_put(&map, kw[i], (void *)1);
}
return hashmap_get2(&map, tok->loc, tok->len);
} }
static int read_escaped_char(char **new_pos, char *p) { static int read_escaped_char(char **new_pos, char *p) {