home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 5.ddi / PARSER.ZIP / NODE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-25  |  3.8 KB  |  166 lines

  1. /*****************************************************
  2. file: NODE.CPP       Copyright 1989 by John M. Dlugosz
  3.    dealings with nodes
  4. *****************************************************/
  5.  
  6. #include "usual.hpp"
  7. #include <stream.hpp>
  8. #include <assert.h>
  9. #include "atom.hpp"
  10. #include "node.hpp"
  11.  
  12. extern "C" void* malloc (unsigned size);
  13. extern "C" void free (void*);
  14. extern "C" void* realloc (void*, unsigned);
  15.  
  16. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  17.  
  18. node::node()
  19. {
  20. flavor= nf_base;
  21. }
  22.  
  23. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  24.  
  25. node::~node()
  26. {
  27. }
  28.  
  29. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  30.  
  31. void node::print()
  32. {
  33. cout << "\nerror: base class node printed.\n";
  34. }
  35.  
  36. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  37. /*   type_node                              */
  38. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  39.  
  40. type_node::~type_node()
  41. {
  42. if (to_what) delete to_what;
  43. }
  44.  
  45. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  46.  
  47. type_node::type_node ()
  48. {
  49. flavor= nf_type;
  50. tag= -1;
  51. to_what= NULL;
  52. flags= 0;
  53. primary= -1;  //not filled in yet
  54. }
  55.  
  56. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  57.  
  58. void type_node::print()
  59. {
  60. static char* typenames[]= {
  61.    "void", //0
  62.    "char", //1
  63.    "int",  //2
  64.    "long", //3
  65.    "float",  //4
  66.    "double", //5
  67.    "long double", //6
  68.       "enum ",  //7
  69.       "class ", //8
  70.       "union ", //9
  71.    "pointer to ",  //10
  72.    "reference to ",//11
  73.    "array of ",    //12
  74.    "function ",    //13
  75.    };
  76.  
  77. if (isConst()) cout << "const ";
  78. if (isVol()) cout << "volotile ";
  79. if (primary < 10) {  // a simple type
  80.    if (isUnsigned()) cout << "unsigned ";
  81.    cout << typenames[primary];
  82.    if (primary >= 7) {
  83.       cout << (tag > -1 ? atoms[tag] : "<no name>");
  84.       cout.put (' ');
  85.       }
  86.    }
  87. else {  //something fancy
  88.    cout << typenames[primary];
  89.    if (primary == 12) {
  90.       // >> print array dimention
  91.       }
  92.    else if (primary == 13) {
  93.       cout.put('(');
  94.       int max= aggr->size();
  95.       for (int loop= 0;  loop < max;  loop++) {
  96.          (*aggr)[loop]->type->print();
  97.          if (loop < max-1) cout << ", ";
  98.          }
  99.       cout << ") returning ";
  100.       }
  101.    if (secondary()) to_what->print();
  102.    else cout << "**unknown type**";
  103.    }
  104. }
  105.  
  106. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  107.  
  108. void type_node::stuff_primary (int x, atom tagname)
  109. {
  110. /* given the 'x' parameter from the grammer production, stick the right
  111.    values into the 'primary' and set the 'unsigned' flag if needed. */
  112.  
  113. static int lookup[15]= { 0,  1,1,1,2,2,3,3,4,5,6,0,7,8,9};
  114. assert (x >= 0 && x < 15);
  115. primary= lookup[x];
  116. tag= tagname;  //harmless if not needed
  117. if (x == 3 || x==5) flags|=16;  //mark as unsigned
  118. }
  119.    
  120. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  121. /*    def_node                              */
  122. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  123.  
  124. def_node::def_node (atom n, int store, type_node* t)
  125. {
  126. flavor= nf_def;
  127. name= n;
  128. storage_class= store;
  129. type=t;
  130. }
  131.  
  132. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  133.  
  134. void def_node::print()
  135. {
  136. static char* names[]= {"", "static ", "extern ", "typedef ", "auto ", "register " };
  137. cout << names[storage_class] << atoms[name] << " is ";
  138. type->print();
  139. }
  140.  
  141. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  142. /*   node list                              */
  143. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  144.  
  145. node_list::node_list()
  146. {
  147. count= 0;
  148. list= malloc ((capacity=8) * sizeof (node*));
  149. }
  150.  
  151. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  152.  
  153. node** node_list::access (int x)
  154. {
  155. if (x >= capacity) {
  156.    capacity += capacity/2;
  157.    list= realloc (list, capacity * sizeof (node*));
  158.    }
  159. return list+x;
  160. }
  161.  
  162. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  163. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  164. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  165.  
  166.