X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=lex.c;h=7b55dff5033269203a3893804f7a61c89e64b468;hb=bd5ba9e0fe0e9bdaea3f869f55d8f40651a2d0fb;hp=6c381c9f3cfef5aa27a7829b31f6a0ab8078a74f;hpb=c3f08b289bff35de3f5988fa9131bb5bf2dc6001;p=xonotic%2Fgmqcc.git diff --git a/lex.c b/lex.c index 6c381c9..7b55dff 100644 --- a/lex.c +++ b/lex.c @@ -34,15 +34,7 @@ static const char *const lex_keywords[] = { "do", "else", "if", "while", "break", "continue", "return", "goto", - "for", "typedef", - - /* types */ - "int", - "void", - "string", - "float", - "vector", - "entity", + "for", "typedef" }; struct lex_file *lex_open(FILE *fp) { @@ -60,13 +52,11 @@ struct lex_file *lex_open(FILE *fp) { return lex; } -int lex_close(struct lex_file *file) { - int ret = -1; - if (file) { - ret = fclose(file->file); - mem_d(file); - } - return ret; +void lex_close(struct lex_file *file) { + if (!file) return; + + fclose(file->file); /* may already be closed */ + mem_d(file); } static void lex_addch(int ch, struct lex_file *file) { @@ -290,12 +280,27 @@ int lex_token(struct lex_file *file) { if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it]))) return it; + /* try a type? */ + #define TEST_TYPE(X) \ + do { \ + if (!strncmp(X, "float", sizeof("float"))) \ + return TOKEN_FLOAT; \ + if (!strncmp(X, "vector", sizeof("vector"))) \ + return TOKEN_VECTOR; \ + if (!strncmp(X, "string", sizeof("string"))) \ + return TOKEN_STRING; \ + if (!strncmp(X, "entity", sizeof("entity"))) \ + return TOKEN_ENTITY; \ + if (!strncmp(X, "void" , sizeof("void"))) \ + return TOKEN_VOID; \ + } while(0) + + TEST_TYPE(file->lastok); + /* try the hashtable for typedefs? */ if (typedef_find(file->lastok)) - for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++) - if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it]))) - return it; - + TEST_TYPE(typedef_find(file->lastok)->name); + return LEX_IDENT; } return ch;