| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "node_list.h" |
| 6 |
#include "make_nodes.h" |
| 7 |
#include "simError.h" |
| 8 |
|
| 9 |
/* |
| 10 |
walks to to the top of a stmt_list. Needed when assigning the |
| 11 |
statment list to a block of code. |
| 12 |
*/ |
| 13 |
|
| 14 |
struct node_tag* walk_to_top( struct node_tag* walk_me ){ |
| 15 |
|
| 16 |
while( walk_me->prev_stmt != NULL ){ |
| 17 |
walk_me = walk_me->prev_stmt; |
| 18 |
} |
| 19 |
return walk_me; |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
the next three functions are for creating and initializing the |
| 24 |
assignment statements. |
| 25 |
*/ |
| 26 |
|
| 27 |
struct node_tag* assign_i( char* lhs, int rhs ){ |
| 28 |
|
| 29 |
struct node_tag* the_node; |
| 30 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 31 |
|
| 32 |
the_node->type = ASSIGNMENT_STMT; |
| 33 |
the_node->index = 0; |
| 34 |
the_node->next_stmt = NULL; |
| 35 |
the_node->prev_stmt = NULL; |
| 36 |
the_node->stmt_list = NULL; |
| 37 |
|
| 38 |
the_node->the_data.asmt.type = INT_ASSN; |
| 39 |
the_node->the_data.asmt.identifier = lhs; |
| 40 |
the_node->the_data.asmt.rhs.i_val = rhs; |
| 41 |
|
| 42 |
return the_node; |
| 43 |
} |
| 44 |
|
| 45 |
struct node_tag* assign_d( char* lhs, double rhs ){ |
| 46 |
|
| 47 |
struct node_tag* the_node; |
| 48 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 49 |
|
| 50 |
the_node->type = ASSIGNMENT_STMT; |
| 51 |
the_node->index = 0; |
| 52 |
the_node->next_stmt = NULL; |
| 53 |
the_node->prev_stmt = NULL; |
| 54 |
the_node->stmt_list = NULL; |
| 55 |
|
| 56 |
the_node->the_data.asmt.type = DOUBLE_ASSN; |
| 57 |
the_node->the_data.asmt.identifier = lhs; |
| 58 |
the_node->the_data.asmt.rhs.d_val = rhs; |
| 59 |
|
| 60 |
return the_node; |
| 61 |
} |
| 62 |
|
| 63 |
struct node_tag* assign_s( char* lhs, char* rhs ){ |
| 64 |
|
| 65 |
struct node_tag* the_node; |
| 66 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 67 |
|
| 68 |
the_node->type = ASSIGNMENT_STMT; |
| 69 |
the_node->index = 0; |
| 70 |
the_node->next_stmt = NULL; |
| 71 |
the_node->prev_stmt = NULL; |
| 72 |
the_node->stmt_list = NULL; |
| 73 |
|
| 74 |
the_node->the_data.asmt.type = STR_ASSN; |
| 75 |
the_node->the_data.asmt.identifier = lhs; |
| 76 |
the_node->the_data.asmt.rhs.str_ptr = rhs; |
| 77 |
|
| 78 |
return the_node; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
The next function deals with creating and initializing of the |
| 83 |
members statements used by the bonds, bends, torsions, and rigid bodies. |
| 84 |
*/ |
| 85 |
|
| 86 |
struct node_tag* members( char* list_str ){ |
| 87 |
|
| 88 |
struct node_tag* the_node; |
| 89 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 90 |
|
| 91 |
the_node->type = MEMBERS_STMT; |
| 92 |
the_node->index = 0; |
| 93 |
the_node->next_stmt = NULL; |
| 94 |
the_node->prev_stmt = NULL; |
| 95 |
the_node->stmt_list = NULL; |
| 96 |
|
| 97 |
int nTokens, i; |
| 98 |
char *foo; |
| 99 |
|
| 100 |
nTokens = count_tokens(list_str, " ,;\t"); |
| 101 |
|
| 102 |
if (nTokens < 1) { |
| 103 |
yyerror("can't find any members in this members statement"); |
| 104 |
} |
| 105 |
|
| 106 |
the_node->the_data.mbrs.nMembers = nTokens; |
| 107 |
|
| 108 |
the_node->the_data.mbrs.memberList = (int *) calloc(nTokens, sizeof(int)); |
| 109 |
|
| 110 |
foo = strtok(list_str, " ,;\t"); |
| 111 |
the_node->the_data.mbrs.memberList[0] = atoi( foo ); |
| 112 |
|
| 113 |
for (i = 1; i < nTokens; i++) { |
| 114 |
foo = strtok(NULL, " ,;\t"); |
| 115 |
the_node->the_data.mbrs.memberList[i] = atoi( foo ); |
| 116 |
} |
| 117 |
|
| 118 |
return the_node; |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
This function does the C & I of the constraint statements |
| 123 |
*/ |
| 124 |
|
| 125 |
struct node_tag* constraint( char* list_str ){ |
| 126 |
|
| 127 |
struct node_tag* the_node; |
| 128 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 129 |
|
| 130 |
the_node->type = CONSTRAINT_STMT; |
| 131 |
the_node->index = 0; |
| 132 |
the_node->next_stmt = NULL; |
| 133 |
the_node->prev_stmt = NULL; |
| 134 |
the_node->stmt_list = NULL; |
| 135 |
|
| 136 |
int nTokens; |
| 137 |
char *foo; |
| 138 |
|
| 139 |
nTokens = count_tokens(list_str, " ,;\t"); |
| 140 |
if (nTokens != 1) { |
| 141 |
yyerror("wrong number of arguments given in constraint statement"); |
| 142 |
} |
| 143 |
|
| 144 |
foo = strtok(list_str, " ,;\t"); |
| 145 |
the_node->the_data.cnstr.constraint_val = atof( foo ); |
| 146 |
|
| 147 |
return the_node; |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
This function does the C & I for the orientation statements |
| 152 |
*/ |
| 153 |
|
| 154 |
struct node_tag* orientation( char* list_str ){ |
| 155 |
|
| 156 |
struct node_tag* the_node; |
| 157 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 158 |
|
| 159 |
the_node->type = ORIENTATION_STMT; |
| 160 |
the_node->index = 0; |
| 161 |
the_node->next_stmt = NULL; |
| 162 |
the_node->prev_stmt = NULL; |
| 163 |
the_node->stmt_list = NULL; |
| 164 |
|
| 165 |
int nTokens; |
| 166 |
char *foo; |
| 167 |
|
| 168 |
nTokens = count_tokens(list_str, " ,;\t"); |
| 169 |
if (nTokens != 3) { |
| 170 |
yyerror("wrong number of arguments given in orientation"); |
| 171 |
} |
| 172 |
|
| 173 |
foo = strtok(list_str, " ,;\t"); |
| 174 |
the_node->the_data.ort.phi = atof( foo ); |
| 175 |
|
| 176 |
foo = strtok(NULL, " ,;\t"); |
| 177 |
the_node->the_data.ort.theta = atof( foo ); |
| 178 |
|
| 179 |
foo = strtok(NULL, " ,;\t"); |
| 180 |
the_node->the_data.ort.psi = atof( foo ); |
| 181 |
|
| 182 |
return the_node; |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
This function does the C & I for the position statements |
| 187 |
*/ |
| 188 |
|
| 189 |
struct node_tag* position( char* list_str ){ |
| 190 |
|
| 191 |
struct node_tag* the_node; |
| 192 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 193 |
|
| 194 |
the_node->type = POSITION_STMT; |
| 195 |
the_node->index = 0; |
| 196 |
the_node->next_stmt = NULL; |
| 197 |
the_node->prev_stmt = NULL; |
| 198 |
the_node->stmt_list = NULL; |
| 199 |
|
| 200 |
int nTokens; |
| 201 |
char *foo; |
| 202 |
|
| 203 |
nTokens = count_tokens(list_str, " ,;\t"); |
| 204 |
if (nTokens != 3) { |
| 205 |
yyerror("wrong number of arguments given in position"); |
| 206 |
} |
| 207 |
|
| 208 |
foo = strtok(list_str, " ,;\t"); |
| 209 |
the_node->the_data.pos.x = atof( foo ); |
| 210 |
|
| 211 |
foo = strtok(NULL, " ,;\t"); |
| 212 |
the_node->the_data.pos.y = atof( foo ); |
| 213 |
|
| 214 |
foo = strtok(NULL, " ,;\t"); |
| 215 |
the_node->the_data.pos.z = atof( foo ); |
| 216 |
|
| 217 |
|
| 218 |
return the_node; |
| 219 |
} |
| 220 |
|
| 221 |
/* |
| 222 |
The following six functions initialize the statement nodes |
| 223 |
coresponding to the different code block types. |
| 224 |
*/ |
| 225 |
|
| 226 |
struct node_tag* molecule_blk( struct node_tag* stmt_list ){ |
| 227 |
|
| 228 |
struct node_tag* the_node; |
| 229 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 230 |
|
| 231 |
the_node->type = MOLECULE_HEAD; |
| 232 |
the_node->index = 0; |
| 233 |
the_node->next_stmt = NULL; |
| 234 |
the_node->prev_stmt = NULL; |
| 235 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 236 |
|
| 237 |
return the_node; |
| 238 |
} |
| 239 |
|
| 240 |
struct node_tag* rigidbody_blk( int index, struct node_tag* stmt_list ){ |
| 241 |
|
| 242 |
struct node_tag* the_node; |
| 243 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 244 |
|
| 245 |
the_node->type = RIGIDBODY_HEAD; |
| 246 |
the_node->index = 0; |
| 247 |
the_node->next_stmt = NULL; |
| 248 |
the_node->prev_stmt = NULL; |
| 249 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 250 |
|
| 251 |
return the_node; |
| 252 |
} |
| 253 |
|
| 254 |
struct node_tag* atom_blk( int index, struct node_tag* stmt_list ){ |
| 255 |
|
| 256 |
struct node_tag* the_node; |
| 257 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 258 |
|
| 259 |
the_node->type = ATOM_HEAD; |
| 260 |
the_node->index = index; |
| 261 |
the_node->next_stmt = NULL; |
| 262 |
the_node->prev_stmt = NULL; |
| 263 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 264 |
|
| 265 |
return the_node; |
| 266 |
} |
| 267 |
|
| 268 |
struct node_tag* bond_blk( int index, struct node_tag* stmt_list ){ |
| 269 |
|
| 270 |
struct node_tag* the_node; |
| 271 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 272 |
|
| 273 |
the_node->type = BOND_HEAD; |
| 274 |
the_node->index = index; |
| 275 |
the_node->next_stmt = NULL; |
| 276 |
the_node->prev_stmt = NULL; |
| 277 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 278 |
|
| 279 |
return the_node; |
| 280 |
} |
| 281 |
|
| 282 |
struct node_tag* bend_blk( int index, struct node_tag* stmt_list ){ |
| 283 |
|
| 284 |
struct node_tag* the_node; |
| 285 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 286 |
|
| 287 |
the_node->type = BEND_HEAD; |
| 288 |
the_node->index = index; |
| 289 |
the_node->next_stmt = NULL; |
| 290 |
the_node->prev_stmt = NULL; |
| 291 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 292 |
|
| 293 |
return the_node; |
| 294 |
} |
| 295 |
|
| 296 |
struct node_tag* torsion_blk( int index, struct node_tag* stmt_list ){ |
| 297 |
|
| 298 |
struct node_tag* the_node; |
| 299 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 300 |
|
| 301 |
the_node->type = TORSION_HEAD; |
| 302 |
the_node->index = index; |
| 303 |
the_node->next_stmt = NULL; |
| 304 |
the_node->prev_stmt = NULL; |
| 305 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 306 |
|
| 307 |
return the_node; |
| 308 |
} |
| 309 |
|
| 310 |
struct node_tag* zconstraint_blk( int index, struct node_tag* stmt_list ){ |
| 311 |
|
| 312 |
struct node_tag* the_node; |
| 313 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 314 |
|
| 315 |
the_node->type = ZCONSTRAINT_HEAD; |
| 316 |
the_node->index = index; |
| 317 |
the_node->next_stmt = NULL; |
| 318 |
the_node->prev_stmt = NULL; |
| 319 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 320 |
|
| 321 |
return the_node; |
| 322 |
} |
| 323 |
|
| 324 |
struct node_tag* component_blk( struct node_tag* stmt_list ){ |
| 325 |
|
| 326 |
struct node_tag* the_node; |
| 327 |
the_node = ( struct node_tag* )malloc( sizeof( node ) ); |
| 328 |
|
| 329 |
the_node->type = COMPONENT_HEAD; |
| 330 |
the_node->index = 0; |
| 331 |
the_node->next_stmt = NULL; |
| 332 |
the_node->prev_stmt = NULL; |
| 333 |
the_node->stmt_list = walk_to_top( stmt_list ); |
| 334 |
|
| 335 |
return the_node; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
int count_tokens(char *line, char *delimiters) { |
| 340 |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
| 341 |
|
| 342 |
char *working_line; /* WORKING COPY OF LINE. */ |
| 343 |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
| 344 |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
| 345 |
|
| 346 |
strtok_ptr= working_line= strdup(line); |
| 347 |
|
| 348 |
ntokens=0; |
| 349 |
while (strtok(strtok_ptr,delimiters)!=NULL) |
| 350 |
{ |
| 351 |
ntokens++; |
| 352 |
strtok_ptr=NULL; |
| 353 |
} |
| 354 |
|
| 355 |
free(working_line); |
| 356 |
return(ntokens); |
| 357 |
} |