home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / cgrammer / proto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-19  |  3.9 KB  |  211 lines

  1. #include <stdio.h>
  2. #include "tr.h"
  3. #include "li.h"
  4.  
  5. TRnode *tltail = 0;
  6. TRnode *tlhead = 0;
  7.  
  8. void
  9. build_prototypes ()
  10. {
  11.   LInode *liproto;
  12.   
  13.   /*  Loop through the list of trees, building a prototype for each
  14.       baby.  */
  15.   for (liproto = li_head; liproto != 0; liproto = liproto->next)
  16.     {
  17.       int done = 0;
  18.  
  19.       listify (liproto);
  20.       
  21.       if (liproto->comment)
  22.     dump_comment (liproto->comment);
  23.       
  24.       if (0 != liproto->list_decl_spec)
  25.     dump_decl_spec (liproto->list_decl_spec);
  26.       else
  27.     printf ("int ");
  28.       
  29.       if (0 != liproto->list_decl)
  30.     done = dump_decl (liproto->list_decl);
  31.       else
  32.     fprintf (stderr, "ERROR! no Function declaration.");
  33.       
  34.       if (done)
  35.     {
  36.       printf (");\n");
  37.       if (liproto->list_decl_list)
  38.         printf ("/*  The above proto had both types of protos: inline and traditional.  */\n\n");
  39.     }
  40.       else if (0 != liproto->list_decl_list)
  41.     {
  42.       dump_decl_list (liproto->list_decl_list);
  43.     }
  44.       else
  45.     printf (");\n");
  46.  
  47.       printf ("\n");
  48.     }
  49. }
  50.  
  51.  
  52. listify (linode)
  53.      LInode *linode;
  54. {
  55.   
  56.   if ((TRnode *)0 != linode->decl_spec)
  57.     {
  58.       trav_tree (linode->decl_spec);
  59.       linode->list_decl_spec = tlhead;
  60.       tlhead = (TRnode *) 0;
  61.     }
  62.   else
  63.     linode->list_decl_spec = (TRnode *) 0;
  64.  
  65.   if ((TRnode *)0 != linode->decl)
  66.     {
  67.       trav_tree (linode->decl);
  68.       linode->list_decl = tlhead;
  69.       tlhead = (TRnode *) 0;
  70.     }
  71.   else
  72.     linode->list_decl = (TRnode *) 0;
  73.  
  74.   if ((TRnode *)0 != linode->decl_list)
  75.     {
  76.       trav_tree (linode->decl_list);
  77.       linode->list_decl_list = tlhead;
  78.       tlhead = (TRnode *) 0;
  79.     }
  80.   else
  81.     linode->decl_list = (TRnode *) 0;
  82.  
  83.   return;
  84. }
  85.  
  86.  
  87. tladd (node)
  88.      TRnode *node;
  89. {
  90.   if (tlhead == 0)
  91.     {
  92.       tlhead = node;
  93.       tltail = node;
  94.     }
  95.   else
  96.     {
  97.       tltail->list_next = node;
  98.       tltail = node;
  99.     }
  100. }
  101.  
  102.  
  103. trav_tree (anode)
  104.      TRnode *anode;
  105. {
  106.   if (0 != anode->left)
  107.     trav_tree (anode->left);
  108.   
  109.   if (anode->type != NOP)
  110.     tladd (anode);
  111.   
  112.   if (0 != anode->right)
  113.     trav_tree (anode->right);
  114. }
  115.  
  116.  
  117.  
  118. free_prototypes ()
  119. {
  120.   
  121. }
  122.  
  123.  
  124. dump_comment ()
  125. {
  126.   
  127. }
  128.  
  129. dump_decl_spec (node)
  130.      TRnode *node;
  131. {
  132.   TRnode *trnode;
  133.  
  134.   for (trnode = node; trnode != (TRnode *)0; trnode = trnode->list_next)
  135.     {
  136.       printf ("%s ", trnode->pc);
  137.     }
  138. }
  139.  
  140. dump_decl (node)
  141.      TRnode *node;
  142. {
  143.   int struct_last = 0;
  144.   int typed = 0;
  145.  
  146.   while (PTR == node->type)
  147.     {
  148.       printf ("*");
  149.       node = node->list_next;
  150.       if ((TRnode *)0 == node)
  151.     {
  152.       fprintf (stderr, "ACK!!\n");
  153.       exit (5);
  154.     }
  155.     }
  156.  
  157.   printf ("%s (", node->pc);
  158.   node = node->list_next;
  159.   
  160.   if (node && node->type == IDENT)
  161.     return 0;
  162.   while ((TRnode *)0 != node)
  163.     {
  164.  
  165.       printf ("%s", node->pc);
  166.       if (node->type == IDENT && !struct_last && node->list_next != (TRnode *)0)
  167.     printf (", ");
  168.       else 
  169.     printf (" ");
  170.       if (node->type == TYPE)
  171.     typed = 1;
  172.       struct_last = 0;
  173.       if (node->type == LABEL &&
  174.       (0 == strcmp ("struct", node->pc)
  175.        || 0 == strcmp ("union", node->pc)
  176.        || 0 == strcmp ("enum", node->pc)))
  177.     ++struct_last; 
  178.       
  179.       node = node->list_next;
  180.     }
  181.   return typed;
  182. }
  183.  
  184. dump_decl_list (node)
  185.      TRnode *node;
  186. {
  187.   int here = 0;
  188.   int in_struct = 0;
  189.  
  190.   for (; (TRnode *)0 != node; node = node->list_next)
  191.     {
  192.       if ((here == 2 && in_struct)
  193.       || (here && !in_struct))
  194.     {
  195.       printf (", ");
  196.       in_struct = here = 0;
  197.     }
  198.       printf (" %s", node->pc);
  199.       if (node->type == LABEL &&
  200.       (0 == strcmp ("struct", node->pc)
  201.        || 0 == strcmp ("union", node->pc)
  202.        || 0 == strcmp ("enum", node->pc)))
  203.     ++in_struct;
  204.  
  205.       if (node->type == IDENT)
  206.     ++here;
  207.     }
  208.   printf (");\n");
  209. }
  210.  
  211.