]> git.xonotic.org Git - xonotic/gmqcc.git/blob - typedef.c
command line argument parsing.
[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 "gmqcc.h"
24 static typedef_node *typedef_table[1024];
25
26 void typedef_init() {
27     int i;
28     for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++)
29         typedef_table[i] = NULL;
30 }
31
32 unsigned int typedef_hash(const char *s) {
33     unsigned int hash = 0;
34     unsigned int size = strlen(s);
35     unsigned int iter;
36     
37     for (iter = 0; iter < size; iter++) {
38         hash += s[iter];
39         hash += (hash << 10);
40         hash ^= (hash >> 6);
41     }
42     hash += (hash << 3);
43     hash ^= (hash >> 11);
44     hash += (hash << 15);
45     
46     return hash % 1024;
47 }
48
49 typedef_node *typedef_find(const char *s) {
50     unsigned int  hash = typedef_hash(s);
51     typedef_node *find = typedef_table[hash];
52     return find;
53 }
54
55 void typedef_clear() {
56     int i;
57     for(i = 1024; i > 0; i--) {
58         if(typedef_table[i]) {
59             mem_d(typedef_table[i]->name);
60             mem_d(typedef_table[i]);
61         }
62     }
63 }
64
65 int typedef_add(struct lex_file *file, const char *from, const char *to) {
66     unsigned int  hash = typedef_hash(to);
67     typedef_node *find = typedef_table[hash];
68     
69     if (find)
70         return error(file, ERROR_PARSE, "typedef for %s already exists or conflicts\n", to);
71     
72     /* check if the type exists first */
73     if (strncmp(from, "float",  sizeof("float"))  == 0 ||
74         strncmp(from, "vector", sizeof("vector")) == 0 ||
75         strncmp(from, "string", sizeof("string")) == 0 ||
76         strncmp(from, "entity", sizeof("entity")) == 0 ||
77         strncmp(from, "void",   sizeof("void"))   == 0) {
78         
79         typedef_table[hash]       = mem_a(sizeof(typedef_node));
80         typedef_table[hash]->name = util_strdup(from);
81         return -100;
82     } else {
83         /* search the typedefs for it (typedef-a-typedef?) */
84         typedef_node *find = typedef_table[typedef_hash(from)];
85         if (find) {
86             typedef_table[hash]       = mem_a(sizeof(typedef_node));
87             typedef_table[hash]->name = util_strdup(find->name);
88             return -100;
89         }
90     }
91     return error(file, ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from);
92 }