mirror of https://github.com/rui314/chibicc.git
Use hashmap for keyword lookup
This commit is contained in:
parent
655954e301
commit
f6944133d2
10
parse.c
10
parse.c
|
@ -1479,6 +1479,9 @@ 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 HashMap map;
|
||||||
|
|
||||||
|
if (map.capacity == 0) {
|
||||||
static char *kw[] = {
|
static char *kw[] = {
|
||||||
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
|
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
|
||||||
"typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
|
"typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
|
||||||
|
@ -1488,9 +1491,10 @@ static bool is_typename(Token *tok) {
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
||||||
if (equal(tok, kw[i]))
|
hashmap_put(&map, kw[i], (void *)1);
|
||||||
return true;
|
}
|
||||||
return find_typedef(tok);
|
|
||||||
|
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 ")"
|
||||||
|
|
10
tokenize.c
10
tokenize.c
|
@ -156,6 +156,9 @@ static int read_punct(char *p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_keyword(Token *tok) {
|
static bool is_keyword(Token *tok) {
|
||||||
|
static HashMap map;
|
||||||
|
|
||||||
|
if (map.capacity == 0) {
|
||||||
static char *kw[] = {
|
static char *kw[] = {
|
||||||
"return", "if", "else", "for", "while", "int", "sizeof", "char",
|
"return", "if", "else", "for", "while", "int", "sizeof", "char",
|
||||||
"struct", "union", "short", "long", "void", "typedef", "_Bool",
|
"struct", "union", "short", "long", "void", "typedef", "_Bool",
|
||||||
|
@ -167,9 +170,10 @@ static bool is_keyword(Token *tok) {
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
||||||
if (equal(tok, kw[i]))
|
hashmap_put(&map, kw[i], (void *)1);
|
||||||
return true;
|
}
|
||||||
return false;
|
|
||||||
|
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) {
|
||||||
|
|
Loading…
Reference in New Issue