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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
24 static typedef_node *typedef_table[1024];
28 for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++)
29 typedef_table[i] = NULL;
32 uint32_t typedef_hash(const char *s) {
33 return util_crc32(s, strlen(s), 1024);
36 typedef_node *typedef_find(const char *s) {
37 unsigned int hash = typedef_hash(s);
38 typedef_node *find = typedef_table[hash];
42 void typedef_clear() {
44 for(i = 1024; i > 0; i--) {
45 if(typedef_table[i]) {
46 mem_d(typedef_table[i]->name);
47 mem_d(typedef_table[i]);
52 int typedef_add(lex_file *file, const char *from, const char *to) {
53 unsigned int hash = typedef_hash(to);
54 typedef_node *find = typedef_table[hash];
57 return error(file, ERROR_PARSE, "typedef for %s already exists or conflicts\n", to);
59 /* check if the type exists first */
60 if (strncmp(from, "float", sizeof("float")) == 0 ||
61 strncmp(from, "vector", sizeof("vector")) == 0 ||
62 strncmp(from, "string", sizeof("string")) == 0 ||
63 strncmp(from, "entity", sizeof("entity")) == 0 ||
64 strncmp(from, "void", sizeof("void")) == 0) {
66 typedef_table[hash] = mem_a(sizeof(typedef_node));
67 if (typedef_table[hash])
68 typedef_table[hash]->name = util_strdup(from);
70 return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
73 /* search the typedefs for it (typedef-a-typedef?) */
74 typedef_node *find = typedef_table[typedef_hash(from)];
76 typedef_table[hash] = mem_a(sizeof(typedef_node));
77 if (typedef_table[hash])
78 typedef_table[hash]->name = util_strdup(find->name);
80 return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
84 return error(file, ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from);