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( "startIndex", START_INDEX ); |
78 |
add_res_element( "orientation", ORIENTATION ); |
79 |
|
80 |
is_initialized = 1; // set the initialization boolean to true |
81 |
} |
82 |
|
83 |
|
84 |
/* |
85 |
checks for reserved words. |
86 |
If a reserved word is found, returns the token, |
87 |
else returns 0. |
88 |
*/ |
89 |
|
90 |
int res_word( char* text ){ |
91 |
|
92 |
int matched = 0; |
93 |
int key; // the hash key |
94 |
struct res_element* current_ptr; // points to the current hash element |
95 |
struct defined_element* def_ptr; // points to the current define element |
96 |
|
97 |
if( !is_initialized ){ // initialize the list if not already done |
98 |
|
99 |
initialize_res_list(); |
100 |
} |
101 |
|
102 |
// get the hash key |
103 |
|
104 |
key = hash( text ); |
105 |
current_ptr = reserved[key]; |
106 |
|
107 |
// walk through possible mutiple entries |
108 |
|
109 |
while( current_ptr != NULL ){ |
110 |
|
111 |
matched = !strcmp( text, current_ptr->word ); |
112 |
if( matched ) return current_ptr->token; // search successful |
113 |
|
114 |
current_ptr = current_ptr->next; |
115 |
} |
116 |
|
117 |
// if no keywords turn up, check the word against the list of defines |
118 |
|
119 |
if( defined_list != NULL ){ |
120 |
def_ptr = defined_list[key]; |
121 |
|
122 |
while( def_ptr != NULL ){ |
123 |
|
124 |
matched = !strcmp( text, def_ptr->defined ); |
125 |
if( matched ) return DEFINED; |
126 |
|
127 |
def_ptr = def_ptr->next; |
128 |
} |
129 |
} |
130 |
|
131 |
return 0; //search failed |
132 |
} |
133 |
|
134 |
/* |
135 |
* This is used to test whether a given word is defined |
136 |
* returns 1 if true, 0 if False. |
137 |
*/ |
138 |
|
139 |
int is_defined( char* text ){ |
140 |
|
141 |
int matched = 0; |
142 |
int key; |
143 |
struct defined_element* def_ptr; // points to the current define element |
144 |
|
145 |
key = hash( text ); |
146 |
|
147 |
if( defined_list != NULL ){ |
148 |
def_ptr = defined_list[key]; |
149 |
|
150 |
while( def_ptr != NULL ){ |
151 |
|
152 |
matched = !strcmp( text, def_ptr->defined ); |
153 |
if( matched ) return 1; // search succesful |
154 |
|
155 |
def_ptr = def_ptr->next; |
156 |
} |
157 |
} |
158 |
|
159 |
return 0; //search failed |
160 |
} |
161 |
|
162 |
/* |
163 |
* The next bit returns the text to substitute for any given define. |
164 |
*/ |
165 |
|
166 |
char* get_definition( char* defined ){ |
167 |
int key; |
168 |
int matched = 0; |
169 |
struct defined_element* def_ptr; |
170 |
|
171 |
if( defined_list != NULL ){ |
172 |
|
173 |
key = hash( defined ); |
174 |
def_ptr = defined_list[key]; |
175 |
|
176 |
while( def_ptr != NULL ){ |
177 |
|
178 |
matched = !strcmp( defined, def_ptr->defined ); |
179 |
if( matched ) return def_ptr->definition; |
180 |
|
181 |
def_ptr = def_ptr->next; |
182 |
} |
183 |
} |
184 |
|
185 |
// search failed, therefore there is an error |
186 |
|
187 |
sprintf( painCave.errMsg, "%s was not found in the defined list\n", |
188 |
defined ); |
189 |
painCave.isFatal = 1; |
190 |
simError(); |
191 |
return NULL; |
192 |
} |
193 |
|
194 |
/* |
195 |
* Add a define statement to the hash table |
196 |
*/ |
197 |
|
198 |
void insert_define( char* defined, char* definition ){ |
199 |
|
200 |
int i, key; |
201 |
struct defined_element* element; |
202 |
|
203 |
if( defined_list == NULL ){ |
204 |
|
205 |
defined_list = ( struct defined_element** ) |
206 |
calloc( HASH_SIZE, sizeof( struct defined_element* ) ); |
207 |
|
208 |
for( i=0; i<HASH_SIZE; i++ ){ |
209 |
|
210 |
defined_list[i] = NULL; |
211 |
} |
212 |
} |
213 |
|
214 |
key = hash( defined ); |
215 |
element = (struct defined_element* ) |
216 |
malloc( sizeof( struct defined_element ) ); |
217 |
|
218 |
// fill the element |
219 |
|
220 |
strcpy( element->defined, defined ); |
221 |
strcpy( element->definition, definition ); |
222 |
|
223 |
// add the element to the table |
224 |
|
225 |
element->next = defined_list[key]; |
226 |
defined_list[key] = element; |
227 |
} |
228 |
|
229 |
/* |
230 |
* adds an element to the hash table |
231 |
*/ |
232 |
|
233 |
void add_res_element( char* text, int the_token ){ |
234 |
|
235 |
int key; // the calculated hash key; |
236 |
struct res_element* element; // the element being added |
237 |
|
238 |
key = hash( text ); |
239 |
element = (struct res_element *) malloc( sizeof( struct res_element ) ); |
240 |
|
241 |
// fill the element |
242 |
|
243 |
strcpy( element->word, text ); |
244 |
element->token = the_token; |
245 |
|
246 |
// add the element to the table |
247 |
|
248 |
element->next = reserved[key]; |
249 |
reserved[key] = element; |
250 |
} |
251 |
|
252 |
/* |
253 |
generartes the hash key from the given word. |
254 |
*/ |
255 |
|
256 |
int hash ( char* text ){ |
257 |
|
258 |
register unsigned short int i = 0; // loop counter |
259 |
int key = 0; // the hash key |
260 |
|
261 |
while( text[i] != '\0' ){ |
262 |
|
263 |
key = ( ( key << SHIFT ) + text[i] ) % HASH_SIZE; |
264 |
|
265 |
i++; |
266 |
} |
267 |
|
268 |
if( key < 0 ){ |
269 |
|
270 |
// if the key is less than zero, we've had an overflow error |
271 |
|
272 |
sprintf( painCave.errMsg, |
273 |
"Bass Parse: There has been an overflow error in the hash key."); |
274 |
painCave.isFatal =1; |
275 |
simError(); |
276 |
} |
277 |
|
278 |
return key; |
279 |
} |
280 |
|
281 |
/* |
282 |
function to clean up the memory after we are finished with the lists |
283 |
*/ |
284 |
|
285 |
void kill_lists( ){ |
286 |
|
287 |
struct res_element* current_res_ptr; |
288 |
struct res_element* temp_res_ptr; |
289 |
|
290 |
struct defined_element* current_def_ptr; |
291 |
struct defined_element* temp_def_ptr; |
292 |
|
293 |
register unsigned short int i; |
294 |
|
295 |
if( reserved != NULL ){ |
296 |
|
297 |
for( i=0; i < HASH_SIZE; i++){ |
298 |
|
299 |
current_res_ptr = reserved[i]; |
300 |
|
301 |
while( current_res_ptr != NULL ){ |
302 |
|
303 |
temp_res_ptr = current_res_ptr->next; |
304 |
free( current_res_ptr ); |
305 |
current_res_ptr = temp_res_ptr; |
306 |
} |
307 |
} |
308 |
|
309 |
free( reserved ); |
310 |
} |
311 |
|
312 |
if( defined_list != NULL ){ |
313 |
|
314 |
for( i=0; i < HASH_SIZE; i++){ |
315 |
|
316 |
current_def_ptr = defined_list[i]; |
317 |
|
318 |
while( current_def_ptr != NULL ){ |
319 |
|
320 |
temp_def_ptr = current_def_ptr->next; |
321 |
free( current_def_ptr ); |
322 |
current_def_ptr = temp_def_ptr; |
323 |
} |
324 |
} |
325 |
|
326 |
free( defined_list ); |
327 |
} |
328 |
|
329 |
reserved = NULL; |
330 |
defined_list = NULL; |
331 |
|
332 |
is_initialized = 0; // initialization is now false |
333 |
} |