]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lex.c
Cleanups
[xonotic/gmqcc.git] / lex.c
diff --git a/lex.c b/lex.c
index 6c381c9f3cfef5aa27a7829b31f6a0ab8078a74f..7b55dff5033269203a3893804f7a61c89e64b468 100644 (file)
--- a/lex.c
+++ b/lex.c
 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;