]> git.xonotic.org Git - xonotic/gmqcc.git/blob - typedef.c
Cleanups
[xonotic/gmqcc.git] / typedef.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <string.h>
24 #include <stdint.h> /* replace if stdint.h doesn't exist! */
25 #include <limits.h>
26 #include "gmqcc.h"
27 static typedef_node *typedef_table[1024];
28 void typedef_init() {
29         int i;
30         for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++)
31                 typedef_table[i] = NULL;
32 }
33
34 unsigned int typedef_hash(const char *s) {
35         unsigned int hash = 0;
36         unsigned int size = strlen(s);
37         unsigned int iter;
38         
39         for (iter = 0; iter < size; iter++) {
40                 hash += s[iter];
41                 hash += (hash << 10);
42                 hash ^= (hash >> 6);
43         }
44         hash += (hash << 3);
45         hash ^= (hash >> 11);
46         hash += (hash << 15);
47         
48         return hash % 1024;
49 }
50
51 typedef_node *typedef_find(const char *s) {
52         unsigned int  hash = typedef_hash(s);
53         typedef_node *find = typedef_table[hash];
54         return find;
55 }
56
57 void typedef_clear() {
58         int i;
59         for(i = 1024; i > 0; i--)
60                 if(typedef_table[i])
61                         mem_d(typedef_table[i]);
62 }
63
64 int typedef_add(const char *from, const char *to) {
65         unsigned int  hash = typedef_hash(to);
66         typedef_node *find = typedef_table[hash];
67         if (find)
68                 return error(ERROR_PARSE, "typedef for %s already exists or conflicts\n", to);
69         
70         /* check if the type exists first */
71         if (strncmp(from, "float",  sizeof("float"))  == 0 ||
72             strncmp(from, "vector", sizeof("vector")) == 0 ||
73             strncmp(from, "string", sizeof("string")) == 0 ||
74             strncmp(from, "entity", sizeof("entity")) == 0 ||
75             strncmp(from, "void",   sizeof("void"))   == 0) {
76                 
77                 typedef_table[hash]       = mem_a(sizeof(typedef_node));
78                 typedef_table[hash]->name = strdup(from);
79                 return -100;
80         } else {
81                 /* search the typedefs for it (typedef-a-typedef?) */
82                 typedef_node *find = typedef_table[typedef_hash(from)];
83                 if (find) {
84                         typedef_table[hash]       = mem_a(sizeof(typedef_node));
85                         typedef_table[hash]->name = strdup(find->name);
86                         return -100;
87                 }
88         }
89         return error(ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from);
90 }