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 #include <stdint.h> /* replace if stdint.h doesn't exist! */
29 * This implements a hashtable for typedef type keywords which end up
30 * being translated to their full-expressed type. This uses a singly
31 * linked list with a fast hash function.
33 static typedef_node *typedef_table[1024];
37 for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++)
38 typedef_table[i] = NULL;
42 * Fast collisionless hashfunction based off of:
43 * http://www.azillionmonkeys.com/qed/hash.html
46 * The code is licensed under LGPL 2.1 or Paul
47 * Hsieh's derivative license. Stated on his page
50 * The LGPL 2.1 is not necessarily a more liberal license than my
51 * derivative license, but this additional licensing makes the code
52 * available to more developers. Note that this does not give you
53 * multi-licensing rights. You can only use the code under one of
54 * the licenses at a time.
56 * Paul Hsieh derivative license
58 * The derivative content includes raw computer source code, ideas,
59 * opinions, and excerpts whose original source is covered under
60 * another license and transformations of such derivatives.
61 * Note that mere excerpts by themselves (with the exception of raw
62 * source code) are not considered derivative works under this license.
63 * Use and redistribution is limited to the following conditions:
65 * One may not create a derivative work which, in any way, violates the
66 * Paul Hsieh exposition license described above on the original content.
68 * One may not apply a license to a derivative work that precludes anyone
69 * else from using and redistributing derivative content.
71 * One may not attribute any derivative content to authors not involved
72 * in the creation of the content, though an attribution to the author
75 * Paul Hsieh exposition license
77 * The content of all text, figures, tables and displayed layout is
78 * copyrighted by its author and owner Paul Hsieh unless specifically
79 * denoted otherwise. Redistribution is limited to the following conditions:
81 * The redistributor must fully attribute the content's authorship and
82 * make a good faith effort to cite the original location of the original
85 * The content may not be modified via excerpt or otherwise with the
86 * exception of additional citations such as described above without prior
87 * consent of Paul Hsieh.
89 * The content may not be subject to a change in license without prior
90 * consent of Paul Hsieh.
92 * The content may be used for commercial purposes.
95 #if (defined(__GNUC__) && defined(__i386__)) || defined(_MSC_VER)
97 * Unalligned loads are faster if we can do them, otherwise fall back
98 * to safer version below.
100 # define load16(D) (*((const uint16_t*)(D)))
102 # define load16(D) ((((uint32_t)(((const uint8_t*)(D))[1])) << 8) + \
103 (uint32_t)(((const uint8_t*)(D))[0]))
105 unsigned int inline typedef_hash(const char *data) {
106 uint32_t hash = strlen(data);
107 uint32_t size = hash;
111 if (size <= 0|| data == NULL)
118 for (;size > 0; size--) {
119 hash += (load16(data));
120 temp = (load16(data+2) << 11) ^ hash;
121 hash = (hash << 16) ^ temp;
122 data += sizeof(uint16_t) << 1;
129 hash += load16(data);
131 hash ^= ((signed char)data[sizeof(uint16_t)]) << 8;
135 hash += load16(data);
140 hash += (signed char)*data;
146 /* force avalanching of final 127 bits */
157 typedef_node *typedef_find(const char *s) {
158 unsigned int hash = typedef_hash(s);
159 typedef_node *find = typedef_table[hash];
163 int typedef_add(const char *from, const char *to) {
164 unsigned int hash = typedef_hash(to);
165 typedef_node *find = typedef_table[hash];
167 return error(ERROR_PARSE, "typedef for %s already exists\n", to);
169 /* check if the type exists first */
170 if (strncmp(from, "void", sizeof("void")) == 0 ||
171 strncmp(from, "string", sizeof("string")) == 0 ||
172 strncmp(from, "float", sizeof("float")) == 0 ||
173 strncmp(from, "vector", sizeof("vector")) == 0 ||
174 strncmp(from, "entity", sizeof("entity")) == 0) {
176 typedef_table[hash] = mem_a(sizeof(typedef_node));
177 typedef_table[hash]->name = strdup(from);
180 /* search the typedefs for it (typedef-a-typedef?) */
181 typedef_node *find = typedef_table[typedef_hash(from)];
183 typedef_table[hash] = mem_a(sizeof(typedef_node));
184 typedef_table[hash]->name = strdup(find->name);
188 return error(ERROR_PARSE, "cannot typedef %s (not a type)\n", from);