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