1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "exprtree.h"
5
6 # line 99 "exprtree.dy"
parentfy_ExprTree(ExprTree * et)7 void parentfy_ExprTree(ExprTree * et)
8 {
9 int i;
10
11 for(i=0;i<et->nochild;i++) {
12 et->child[i]->parent = et;
13 et->child[i]->position_in_parent = i;
14 parentfy_ExprTree(et->child[i]);
15 }
16
17
18 }
19
20 # line 112 "exprtree.dy"
declared_ExprTree(ExprTree * et)21 void declared_ExprTree(ExprTree * et)
22 {
23 fprintf(stderr,"Declaring...");
24
25 et->attrib = et->attrib | IS_DECLARED;
26
27 }
28
29
30
31 # line 122 "exprtree.dy"
find_toplevel_name(ExprTree * et)32 void find_toplevel_name(ExprTree * et)
33 {
34 /** et should be an expression or a tag **/
35
36 int i;
37
38 for(i=0;i<et->nochild;i++) {
39 switch(et->child[i]->type) {
40 case ETR_NAME :
41 if( et->position_in_parent == 0 || !(et->parent->type == ETR_REFERENCE || et->parent->type == ETR_STRUCTREF) ) {
42 /* printf("Found as a name -> [%d][%d]",et->parent->position_in_parent,et->parent->type);
43 print_ExprTree(et->child[i]);
44 printf("\n");
45 */
46 et->child[i]->attrib |= IS_TOPLEVEL;
47 } else {
48
49 /* printf("Ignoring name %d,%d",et->parent->position_in_parent,et->parent->type);
50 print_ExprTree(et->child[i]);
51 printf("\n");
52 */
53
54 et->child[i]->attrib &= ~IS_TOPLEVEL;
55
56 }
57 break;
58 default :
59 /*
60 printf("Going into");
61 print_ExprTree(et->child[i]);
62 printf("\n");
63 */
64 find_toplevel_name(et->child[i]);
65 break;
66 }
67 }
68 }
69
70
71 # line 161 "exprtree.dy"
strcat_ExprTree(ExprTree * ExprTree,char * buffer)72 void strcat_ExprTree(ExprTree * ExprTree,char * buffer)
73 {
74 int i;
75
76 switch(ExprTree->type) {
77 case ETR_NUMBER : strcat(buffer,ExprTree->word); return;
78 case ETR_OPERATOR : strcat(buffer,ExprTree->word); return;
79 case ETR_EXPRESSION : strcat(buffer,"(");
80 for(i=0;i< ExprTree->nochild;i++)
81 strcat_ExprTree(ExprTree->child[i],buffer);
82 strcat(buffer,")");
83 return;
84 case ETR_STATEMENT :
85 for(i=0;i< ExprTree->nochild;i++)
86 strcat_ExprTree(ExprTree->child[i],buffer);
87 return;
88 case ETR_NAME : strcat(buffer,ExprTree->word);
89 return;
90 case ETR_ARRAY :
91 strcat_ExprTree(ExprTree->child[0],buffer);
92 strcat(buffer,"[");
93 strcat_ExprTree(ExprTree->child[1],buffer);
94 strcat(buffer,"]");
95 return;
96 case ETR_TAG :
97 for(i=0;i< ExprTree->nochild;i++)
98 strcat_ExprTree(ExprTree->child[i],buffer);
99 return;
100 case ETR_STRUCTREF :
101 strcat_ExprTree(ExprTree->child[0],buffer);
102 strcat(buffer,ExprTree->child[1]->word);
103 strcat_ExprTree(ExprTree->child[2],buffer);
104 return;
105 case ETR_REFERENCE :
106 strcat(buffer,ExprTree->child[0]->word);
107 strcat_ExprTree(ExprTree->child[1],buffer);
108 return;
109 case ETR_METHOD :
110 fprintf(stderr,"So buffer is now [%s]\n",buffer);
111 strcat_ExprTree(ExprTree->child[0],buffer);
112 strcat(buffer,"(");
113 fprintf(stderr,"So buffer is now [%s]\n",buffer);
114 strcat_ExprTree(ExprTree->child[1],buffer);
115
116 /* for(i=0;i<ExprTree->child[1]->nochild;i++)
117 strcat_ExprTree(ExprTree->child[1]->child[i],buffer);
118 */
119
120 strcat(buffer,")");
121 return;
122 case ETR_COMMALIST :
123 for(i=0;i<ExprTree->nochild;i++) {
124 strcat_ExprTree(ExprTree->child[i],buffer);
125 if( i != ExprTree->nochild-1)
126 strcat(buffer,",");
127 }
128 return;
129
130 default :
131 warn("In trying to make Expr string, got unobtainable type!");
132 }
133 }
134
135 # line 224 "exprtree.dy"
print_ExprTree(ExprTree * ExprTree)136 void print_ExprTree(ExprTree * ExprTree)
137 {
138 int i;
139
140 switch(ExprTree->type) {
141 case ETR_NUMBER : printf("#{%s}",ExprTree->word); return;
142 case ETR_OPERATOR : printf("Op{%s}",ExprTree->word); return;
143 case ETR_EXPRESSION : printf("Expression (");
144 for(i=0;i< ExprTree->nochild;i++)
145 print_ExprTree(ExprTree->child[i]);
146 printf(")");
147 return;
148 case ETR_STATEMENT : printf("Top level:");
149 for(i=0;i< ExprTree->nochild;i++)
150 print_ExprTree(ExprTree->child[i]);
151 printf("\n");
152 return;
153 case ETR_NAME : printf("N{%s}",ExprTree->word);
154 for(i=0;i< ExprTree->nochild;i++)
155 print_ExprTree(ExprTree->child[i]);
156 return;
157 case ETR_ARRAY : printf("{ArrayInto{");
158 print_ExprTree(ExprTree->child[0]);
159 printf("} of [");
160 print_ExprTree(ExprTree->child[1]);
161 printf("]}");
162 return;
163 case ETR_TAG :
164 printf("Tag{");
165 for(i=0;i< ExprTree->nochild;i++)
166 print_ExprTree(ExprTree->child[i]);
167 printf("}");
168 return;
169 case ETR_STRUCTREF :
170 printf("struct{%s}(",ExprTree->child[1]->word);
171 print_ExprTree(ExprTree->child[0]);
172 printf(":");
173 print_ExprTree(ExprTree->child[2]);
174 printf(")");
175 return;
176 case ETR_REFERENCE :
177 printf("Ref{");
178 puts(ExprTree->child[0]->word);
179 print_ExprTree(ExprTree->child[1]);
180 printf("}\n");
181 return;
182 case ETR_METHOD :
183 if( ExprTree->attrib & IS_DECLARED )
184 printf("DEC:");
185
186 printf("Method{");
187 for(i=0;i<ExprTree->child[0]->nochild;i++)
188 print_ExprTree(ExprTree->child[0]->child[i]);
189 if( ExprTree->nochild == 1 )
190 printf("}(void)");
191 else {
192 printf("}(");
193 for(i=0;i<ExprTree->child[1]->nochild;i++)
194 print_ExprTree(ExprTree->child[1]->child[i]);
195 printf(")");
196 }
197 return;
198 case ETR_COMMALIST :
199 printf("List{");
200 for(i=0;i<ExprTree->nochild;i++)
201 print_ExprTree(ExprTree->child[i]);
202 printf("}");
203 return;
204
205 default :
206 printf("Unprintable!");
207 }
208 }
209
210
211 /*** declarations ***/
212
213 # line 301 "exprtree.dy"
new_ExprTree_decl_method(ExprTree * name,ExprTree * list)214 ExprTree * new_ExprTree_decl_method(ExprTree * name,ExprTree * list)
215 {
216 ExprTree * out;
217
218 out = new_ExprTree();
219
220 out->type = ETR_DECL_METHOD;
221 out->child[0]=name;
222 out->child[1]=list;
223 out->nochild = 2;
224
225 return out;
226 }
227
228 # line 315 "exprtree.dy"
new_ExprTree_decl_variable(ExprTree * type,ExprTree * name)229 ExprTree * new_ExprTree_decl_variable(ExprTree * type,ExprTree * name)
230 {
231 ExprTree * out;
232
233 out = new_ExprTree();
234
235 out->type = ETR_DECL_VARIABLE;
236 out->child[0]=type;
237 out->child[1]=name;
238 out->nochild = 2;
239
240 return out;
241 }
242
243 # line 329 "exprtree.dy"
add_to_decl_list_ExprTree(ExprTree * list,ExprTree * add)244 ExprTree * add_to_decl_list_ExprTree(ExprTree * list,ExprTree * add)
245 {
246 if( list->type != ETR_DECL_LIST ) {
247 warn("Attempting to add to a non commalist %d",list->type);
248 return list;
249 }
250
251 add_ExprTree(list,add);
252
253 return list;
254 }
255
256 # line 341 "exprtree.dy"
new_ExprTree_decl_list(ExprTree * start)257 ExprTree * new_ExprTree_decl_list(ExprTree * start)
258 {
259 ExprTree * out;
260
261 out = new_ExprTree();
262
263 out->type = ETR_DECL_LIST;
264 add_ExprTree(out,start);
265
266 return out;
267 }
268
269 # line 353 "exprtree.dy"
new_ExprTree_struct_ref(ExprTree * left,ExprTree * ref,ExprTree * right)270 ExprTree * new_ExprTree_struct_ref(ExprTree * left, ExprTree * ref,ExprTree * right)
271 {
272 ExprTree * out;
273
274 out = new_ExprTree();
275
276 out->type = ETR_STRUCTREF;
277 out->child[0]=left;
278 out->child[1]=ref;
279 out->child[2]=right;
280 out->nochild = 3;
281
282 return out;
283 }
284
285 # line 368 "exprtree.dy"
new_ExprTree_ref(char op,ExprTree * right)286 ExprTree * new_ExprTree_ref(char op,ExprTree * right)
287 {
288 ExprTree * out;
289
290 out = new_ExprTree();
291
292 out->type = ETR_REFERENCE;
293 out->child[0]=new_ExprTree_token(op);
294 out->child[1]=right;
295 out->nochild = 2;
296
297 return out;
298 }
299
300 # line 382 "exprtree.dy"
add_to_commalist_ExprTree(ExprTree * list,ExprTree * add)301 ExprTree * add_to_commalist_ExprTree(ExprTree * list,ExprTree * add)
302 {
303 if( list->type != ETR_COMMALIST ) {
304 warn("Attempting to add to a non commalist %d",list->type);
305 return list;
306 }
307
308 add_ExprTree(list,add);
309
310 return list;
311 }
312
313 # line 394 "exprtree.dy"
new_ExprTree_commalist(ExprTree * start)314 ExprTree * new_ExprTree_commalist(ExprTree * start)
315 {
316 ExprTree * out;
317
318 out = new_ExprTree();
319
320 out->type = ETR_COMMALIST;
321 add_ExprTree(out,start);
322
323 return out;
324 }
325
326 # line 406 "exprtree.dy"
new_ExprTree_method(ExprTree * one,ExprTree * other)327 ExprTree * new_ExprTree_method(ExprTree * one,ExprTree * other)
328 {
329 ExprTree * out;
330
331 out = ExprTree_alloc();
332 out->type = ETR_METHOD;
333
334 /* printf("This one %d\n",one->nochild); */
335
336 add_ExprTree(out,one);
337
338 if(other == NULL) {
339 return out;
340 }
341
342 if(other->type != ETR_COMMALIST ) {
343 warn("Attempting to have a non commalist argument for a method");
344 }
345
346 add_ExprTree(out,other);
347
348 return out;
349 }
350
351 # line 430 "exprtree.dy"
new_ExprTree_tag_from_name(ExprTree * name)352 ExprTree * new_ExprTree_tag_from_name(ExprTree * name)
353 {
354
355 ExprTree * out;
356
357 out= new_ExprTree();
358 out->type = ETR_TAG;
359
360 add_ExprTree(out,name);
361
362
363 return out;
364 }
365
366 # line 444 "exprtree.dy"
new_ExprTree_array(ExprTree * tag,ExprTree * expr)367 ExprTree * new_ExprTree_array(ExprTree * tag,ExprTree * expr)
368 {
369 ExprTree * out;
370
371 out = new_ExprTree();
372 out->type = ETR_ARRAY;
373 out->child[0] = tag;
374 out->child[1] = expr;
375 out->nochild = 2;
376 return out;
377 }
378
379 # line 456 "exprtree.dy"
new_ExprTree_binary_expr(ExprTree * left,char op,ExprTree * rgt)380 ExprTree * new_ExprTree_binary_expr(ExprTree * left,char op,ExprTree * rgt)
381 {
382 ExprTree * out;
383
384
385 out = new_ExprTree();
386
387 out->type = ETR_EXPRESSION;
388 out->child[0] = left;
389 out->child[1] = new_ExprTree_token(op);
390 out->child[2] = rgt;
391 out->nochild = 3;
392
393 return out;
394 }
395
396 # line 472 "exprtree.dy"
new_ExprTree_token(char t)397 ExprTree * new_ExprTree_token(char t)
398 {
399 ExprTree * out;
400 char buf[2];
401
402 buf[0] = t;
403 buf[1] = '\0';
404
405 out= new_ExprTree();
406 out->type= ETR_OPERATOR;
407 out->token = t;
408 out->word = stringalloc(buf);
409
410 return out;
411 }
412
413 # line 488 "exprtree.dy"
add_ExprTree(ExprTree * one,ExprTree * child)414 boolean add_ExprTree(ExprTree * one,ExprTree * child)
415 {
416 if( one->nochild+1 > EXPRTREE_MAXCHILD ) {
417 warn("Overflow in ExprTree at %d children",EXPRTREE_MAXCHILD);
418 return FALSE;
419 }
420
421 one->child[one->nochild++] = child;
422 return TRUE;
423 }
424
425 # line 499 "exprtree.dy"
new_ExprTree(void)426 ExprTree * new_ExprTree(void)
427 {
428 int i;
429 ExprTree * out;
430
431 out = ExprTree_alloc();
432 for(i=0;i<128;i++)
433 out->child[i]= NULL;
434
435
436 return out;
437 }
438
439
440
441 # line 440 "exprtree.c"
442 /* Function: hard_link_ExprTree(obj)
443 *
444 * Descrip: Bumps up the reference count of the object
445 * Meaning that multiple pointers can 'own' it
446 *
447 *
448 * Arg: obj [UNKN ] Object to be hard linked [ExprTree *]
449 *
450 * Return [UNKN ] Undocumented return value [ExprTree *]
451 *
452 */
hard_link_ExprTree(ExprTree * obj)453 ExprTree * hard_link_ExprTree(ExprTree * obj)
454 {
455 if( obj == NULL ) {
456 warn("Trying to hard link to a ExprTree object: passed a NULL object");
457 return NULL;
458 }
459 obj->dynamite_hard_link++;
460 return obj;
461 }
462
463
464 /* Function: ExprTree_alloc(void)
465 *
466 * Descrip: Allocates structure: assigns defaults if given
467 *
468 *
469 *
470 * Return [UNKN ] Undocumented return value [ExprTree *]
471 *
472 */
ExprTree_alloc(void)473 ExprTree * ExprTree_alloc(void)
474 {
475 ExprTree * out; /* out is exported at end of function */
476
477
478 /* call ckalloc and see if NULL */
479 if((out=(ExprTree *) ckalloc (sizeof(ExprTree))) == NULL) {
480 warn("ExprTree_alloc failed ");
481 return NULL; /* calling function should respond! */
482 }
483 out->dynamite_hard_link = 1;
484 #ifdef PTHREAD
485 pthread_mutex_init(&(out->dynamite_mutex),NULL);
486 #endif
487 /* child[EXPRTREE_MAXCHILD] is an array: no default possible */
488 out->nochild = 0;
489 out->token = 'u';
490 out->word = NULL;
491 out->type = 0;
492 out->attrib = 0;
493 out->parent = NULL;
494 out->position_in_parent = 0;
495
496
497 return out;
498 }
499
500
501 /* Function: free_ExprTree(obj)
502 *
503 * Descrip: Free Function: removes the memory held by obj
504 * Will chain up to owned members and clear all lists
505 *
506 *
507 * Arg: obj [UNKN ] Object that is free'd [ExprTree *]
508 *
509 * Return [UNKN ] Undocumented return value [ExprTree *]
510 *
511 */
free_ExprTree(ExprTree * obj)512 ExprTree * free_ExprTree(ExprTree * obj)
513 {
514 int return_early = 0;
515
516
517 if( obj == NULL) {
518 warn("Attempting to free a NULL pointer to a ExprTree obj. Should be trappable");
519 return NULL;
520 }
521
522
523 #ifdef PTHREAD
524 assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
525 #endif
526 if( obj->dynamite_hard_link > 1) {
527 return_early = 1;
528 obj->dynamite_hard_link--;
529 }
530 #ifdef PTHREAD
531 assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
532 #endif
533 if( return_early == 1)
534 return NULL;
535 /* Unable to make free function for obj->child[EXPRTREE_MAXCHILD] */
536 if( obj->word != NULL)
537 ckfree(obj->word);
538 /* Unable to make free function for obj->parent */
539
540
541 ckfree(obj);
542 return NULL;
543 }
544
545
546
547 #ifdef _cplusplus
548 }
549 #endif
550