1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
|
5 |
#include "BASSyacc.h" |
6 |
#include "BASS_parse.h" |
7 |
#include "simError.h" |
8 |
#ifdef IS_MPI |
9 |
#define __is_lex__ |
10 |
#include "mpiBASS.h" |
11 |
#endif |
12 |
|
13 |
#define HASH_SIZE 211 // the size of the hash table |
14 |
#define SHIFT 4 // the bit shift for the hash index |
15 |
|
16 |
|
17 |
//*** Global functions, variables, and structures ************ |
18 |
|
19 |
unsigned short is_initialized = 0; // tells whether to init the linked list |
20 |
|
21 |
// reserved word elements for the hash table |
22 |
struct res_element{ |
23 |
char word[100]; //the reserved word |
24 |
int token; //the token to return; |
25 |
struct res_element* next; |
26 |
}; |
27 |
|
28 |
struct res_element** reserved = NULL; //the table of reserved words |
29 |
|
30 |
void initialize_res_list(void); // function to initialize the list |
31 |
|
32 |
int hash ( char* text ); // generates the hash key |
33 |
|
34 |
void add_res_element( char* text, int the_token ); //adds elements to the table |
35 |
|
36 |
// the elements of the defined Hash table |
37 |
struct defined_element{ |
38 |
char defined[100]; |
39 |
char definition[DEFINED_BUFFER_SIZE]; |
40 |
struct defined_element* next; |
41 |
}; |
42 |
|
43 |
struct defined_element** defined_list = NULL; // the defined hash table |
44 |
|
45 |
|
46 |
|
47 |
//*** The Functions ******************************************* |
48 |
|
49 |
|
50 |
/* |
51 |
function to initialize the list of reserved words into memory. |
52 |
*/ |
53 |
|
54 |
void initialize_res_list(){ |
55 |
|
56 |
register unsigned short i; // loop counter |
57 |
|
58 |
reserved = ( struct res_element ** ) |
59 |
calloc( HASH_SIZE, sizeof( struct hash__element* ) ); |
60 |
|
61 |
for( i=0; i < HASH_SIZE; i++ ){ |
62 |
|
63 |
reserved[i] = NULL; |
64 |
} |
65 |
|
66 |
// add the reserved words |
67 |
|
68 |
add_res_element( "molecule", MOLECULE ); |
69 |
add_res_element( "atom", ATOM ); |
70 |
add_res_element( "bond", BOND ); |
71 |
add_res_element( "bend", BEND ); |
72 |
add_res_element( "torsion", TORSION ); |
73 |
add_res_element( "position", POSITION ); |
74 |
add_res_element( "members", MEMBERS ); |
75 |
add_res_element( "constraint", CONSTRAINT ); |
76 |
add_res_element( "component", COMPONENT ); |
77 |
add_res_element( "zConstraint", ZCONSTRAINT ); |
78 |
add_res_element( "orientation", ORIENTATION ); |
79 |
add_res_element( "rigidBody", RIGIDBODY ); |
80 |
add_res_element( "cutoffGroup", CUTOFFGROUP ); |
81 |
|
82 |
is_initialized = 1; // set the initialization boolean to true |
83 |
} |
84 |
|
85 |
|
86 |
/* |
87 |
checks for reserved words. |
88 |
If a reserved word is found, returns the token, |
89 |
else returns 0. |
90 |
*/ |
91 |
|
92 |
int res_word( char* text ){ |
93 |
|
94 |
int matched = 0; |
95 |
int key; // the hash key |
96 |
struct res_element* current_ptr; // points to the current hash element |
97 |
struct defined_element* def_ptr; // points to the current define element |
98 |
|
99 |
if( !is_initialized ){ // initialize the list if not already done |
100 |
|
101 |
initialize_res_list(); |
102 |
} |
103 |
|
104 |
// get the hash key |
105 |
|
106 |
key = hash( text ); |
107 |
current_ptr = reserved[key]; |
108 |
|
109 |
// walk through possible mutiple entries |
110 |
|
111 |
while( current_ptr != NULL ){ |
112 |
|
113 |
matched = !strcmp( text, current_ptr->word ); |
114 |
if( matched ) return current_ptr->token; // search successful |
115 |
|
116 |
current_ptr = current_ptr->next; |
117 |
} |
118 |
|
119 |
// if no keywords turn up, check the word against the list of defines |
120 |
|
121 |
if( defined_list != NULL ){ |
122 |
|
123 |
def_ptr = defined_list[key]; |
124 |
|
125 |
while( def_ptr != NULL ){ |
126 |
|
127 |
matched = !strcmp( text, def_ptr->defined ); |
128 |
if( matched ) return DEFINED; |
129 |
|
130 |
def_ptr = def_ptr->next; |
131 |
} |
132 |
} |
133 |
|
134 |
return 0; //search failed |
135 |
} |
136 |
|
137 |
/* |
138 |
* This is used to test whether a given word is defined |
139 |
* returns 1 if true, 0 if False. |
140 |
*/ |
141 |
|
142 |
int is_defined( char* text ){ |
143 |
|
144 |
int matched = 0; |
145 |
int key; |
146 |
struct defined_element* def_ptr; // points to the current define element |
147 |
|
148 |
key = hash( text ); |
149 |
|
150 |
if( defined_list != NULL ){ |
151 |
def_ptr = defined_list[key]; |
152 |
|
153 |
while( def_ptr != NULL ){ |
154 |
|
155 |
matched = !strcmp( text, def_ptr->defined ); |
156 |
if( matched ) return 1; // search succesful |
157 |
|
158 |
def_ptr = def_ptr->next; |
159 |
} |
160 |
} |
161 |
|
162 |
return 0; //search failed |
163 |
} |
164 |
|
165 |
/* |
166 |
* The next bit returns the text to substitute for any given define. |
167 |
*/ |
168 |
|
169 |
char* get_definition( char* defined ){ |
170 |
int key; |
171 |
int matched = 0; |
172 |
struct defined_element* def_ptr; |
173 |
|
174 |
if( defined_list != NULL ){ |
175 |
|
176 |
key = hash( defined ); |
177 |
def_ptr = defined_list[key]; |
178 |
|
179 |
while( def_ptr != NULL ){ |
180 |
|
181 |
matched = !strcmp( defined, def_ptr->defined ); |
182 |
if( matched ) return def_ptr->definition; |
183 |
|
184 |
def_ptr = def_ptr->next; |
185 |
} |
186 |
} |
187 |
|
188 |
// search failed, therefore there is an error |
189 |
|
190 |
sprintf( painCave.errMsg, "%s was not found in the defined list\n", |
191 |
defined ); |
192 |
painCave.isFatal = 1; |
193 |
simError(); |
194 |
return NULL; |
195 |
} |
196 |
|
197 |
/* |
198 |
* Add a define statement to the hash table |
199 |
*/ |
200 |
|
201 |
void insert_define( char* defined, char* definition ){ |
202 |
|
203 |
int i, key; |
204 |
struct defined_element* element; |
205 |
|
206 |
if( defined_list == NULL ){ |
207 |
|
208 |
defined_list = ( struct defined_element** ) |
209 |
calloc( HASH_SIZE, sizeof( struct defined_element* ) ); |
210 |
|
211 |
for( i=0; i<HASH_SIZE; i++ ){ |
212 |
|
213 |
defined_list[i] = NULL; |
214 |
} |
215 |
} |
216 |
|
217 |
key = hash( defined ); |
218 |
element = (struct defined_element* ) |
219 |
malloc( sizeof( struct defined_element ) ); |
220 |
|
221 |
// fill the element |
222 |
|
223 |
strcpy( element->defined, defined ); |
224 |
strcpy( element->definition, definition ); |
225 |
|
226 |
// add the element to the table |
227 |
|
228 |
element->next = defined_list[key]; |
229 |
defined_list[key] = element; |
230 |
} |
231 |
|
232 |
/* |
233 |
* adds an element to the hash table |
234 |
*/ |
235 |
|
236 |
void add_res_element( char* text, int the_token ){ |
237 |
|
238 |
int key; // the calculated hash key; |
239 |
struct res_element* element; // the element being added |
240 |
|
241 |
key = hash( text ); |
242 |
element = (struct res_element *) malloc( sizeof( struct res_element ) ); |
243 |
|
244 |
// fill the element |
245 |
|
246 |
strcpy( element->word, text ); |
247 |
element->token = the_token; |
248 |
|
249 |
// add the element to the table |
250 |
|
251 |
element->next = reserved[key]; |
252 |
reserved[key] = element; |
253 |
} |
254 |
|
255 |
/* |
256 |
generartes the hash key from the given word. |
257 |
*/ |
258 |
|
259 |
int hash ( char* text ){ |
260 |
|
261 |
register unsigned short int i = 0; // loop counter |
262 |
int key = 0; // the hash key |
263 |
|
264 |
while( text[i] != '\0' ){ |
265 |
|
266 |
key = ( ( key << SHIFT ) + text[i] ) % HASH_SIZE; |
267 |
|
268 |
i++; |
269 |
} |
270 |
|
271 |
if( key < 0 ){ |
272 |
|
273 |
// if the key is less than zero, we've had an overflow error |
274 |
|
275 |
sprintf( painCave.errMsg, |
276 |
"Meta-data parse error: There has been an overflow error in the hash key."); |
277 |
painCave.isFatal =1; |
278 |
simError(); |
279 |
} |
280 |
|
281 |
return key; |
282 |
} |
283 |
|
284 |
/* |
285 |
function to clean up the memory after we are finished with the lists |
286 |
*/ |
287 |
|
288 |
void kill_lists( ){ |
289 |
|
290 |
struct res_element* current_res_ptr; |
291 |
struct res_element* temp_res_ptr; |
292 |
|
293 |
struct defined_element* current_def_ptr; |
294 |
struct defined_element* temp_def_ptr; |
295 |
|
296 |
register unsigned short int i; |
297 |
|
298 |
if( reserved != NULL ){ |
299 |
|
300 |
for( i=0; i < HASH_SIZE; i++){ |
301 |
|
302 |
current_res_ptr = reserved[i]; |
303 |
|
304 |
while( current_res_ptr != NULL ){ |
305 |
|
306 |
temp_res_ptr = current_res_ptr->next; |
307 |
free( current_res_ptr ); |
308 |
current_res_ptr = temp_res_ptr; |
309 |
} |
310 |
} |
311 |
|
312 |
free( reserved ); |
313 |
} |
314 |
|
315 |
if( defined_list != NULL ){ |
316 |
|
317 |
for( i=0; i < HASH_SIZE; i++){ |
318 |
|
319 |
current_def_ptr = defined_list[i]; |
320 |
|
321 |
while( current_def_ptr != NULL ){ |
322 |
|
323 |
temp_def_ptr = current_def_ptr->next; |
324 |
free( current_def_ptr ); |
325 |
current_def_ptr = temp_def_ptr; |
326 |
} |
327 |
} |
328 |
|
329 |
free( defined_list ); |
330 |
} |
331 |
|
332 |
reserved = NULL; |
333 |
defined_list = NULL; |
334 |
|
335 |
is_initialized = 0; // initialization is now false |
336 |
} |