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

  1. /*****************************************************
  2. File: ACTIONS.CPP    Copyright 1989 by John M. Dlugosz
  3.    the Actions called from the parser
  4. *****************************************************/
  5.  
  6. #include "usual.hpp"
  7. #include <stream.hpp>
  8. #include "atom.hpp"
  9. #include "node.hpp"
  10. #include "define.hpp"
  11.  
  12. // #define SHORT return 0    
  13. /* short out actions for testing parser only.
  14.    if something suddenly goes wrong, I can stub
  15.    out all the actions to make sure I'm not walking
  16.    on data somewhere.  */
  17. #define SHORT
  18.    // the normal case.
  19.  
  20. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  21.  
  22. static char last_token[81];
  23.  
  24. void get_last_token()
  25. {
  26. /* copy last token from scanner into a nul-terminated string */
  27. int len= 80;  //maximum sig. length
  28. extern char *T_beg, *T_end;  //from the scanner
  29. char* source= T_beg;
  30. char* dest= last_token;
  31.  
  32. //cout << (unsigned)T_beg << "  " << T_end;
  33. //for (int x= 0;  x < 5;  x++)   cout.put(T_beg[x]);
  34.  
  35. while (len-- && source < T_end) *dest++ = *source++;
  36. *dest= '\0';
  37. }
  38.  
  39. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  40.  
  41. /* in a declaration, a storage class is the first thing I get.  This starts
  42.    it off.  Then a ConstVol, a StoreType, and a second ConstVol.  The const
  43.    or volatile keyword may appear before or after the type, with equal
  44.    effect.  The two bits are ORed together for the final result.
  45.    After this, I get one or more calls to Declaration.
  46. */
  47.  
  48. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  49.  
  50. // the type I'm building
  51. struct working_type {
  52.    type_node* this_type;    //the tail
  53.    type_node* root_type;    //the head
  54.    int base_type;
  55.    atom tag_name; // if base_type has a name
  56.    atom name;  //The name of the thing being declared
  57.    int storage_class;
  58.    int const_vol;
  59.    working_type* next;
  60.    } MainType;
  61.  
  62. working_type* Tx= &MainType;
  63. /* this is accessed through a pointer because a declarator can be encounted
  64.    while parsing another declarator.  This lets me stack them.  */
  65.  
  66. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  67.  
  68. static int const_vol_stack[50];
  69. static int const_vol_stacktop= 0;
  70.  
  71. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  72.  
  73. int Declaration (int x,...)
  74. {
  75. /* values for x:  1- global or local def.
  76.                   2- parameters
  77.                   3- struct/union list
  78. */
  79. SHORT;
  80. /* This finishes it off.  A complete declaration has been found. */
  81. Tx->this_type->stuff_primary (Tx->base_type, Tx->tag_name);
  82. Tx->this_type->flags |= Tx->const_vol;
  83. // build the 'thing' from the type_node and the name.
  84. store_thing (Tx->root_type, Tx->name, Tx->storage_class, x);
  85. // Tx->root_type->print();
  86. // cout.put('\n');
  87. return 0;
  88. }
  89.  
  90. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  91.  
  92. int StoreBaseConstVol (int x,...)
  93. {
  94. SHORT;
  95. // the first two calls to ConstVol apply here.
  96. Tx->const_vol =  const_vol_stack[--const_vol_stacktop];
  97. Tx->const_vol |= const_vol_stack[--const_vol_stacktop];
  98. return 0;
  99. }
  100.  
  101. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  102.  
  103. int StoreType (int x,...)
  104. {
  105. SHORT;
  106. Tx->base_type= x;
  107. return 0;
  108. }
  109.  
  110. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  111.  
  112. int StoreTag (int x,...)
  113. {
  114. SHORT;
  115. /* called when a struct, union, or enum is parsed. The tag is the last
  116.    token read.  After this call, the StoreType call is made with 'union'
  117.    or whatever.  */
  118. get_last_token();
  119. Tx->tag_name= atoms[last_token];
  120. return 0;
  121. }
  122.  
  123. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  124.  
  125. int StoreStorage (int x,...)
  126. {
  127. SHORT;
  128. /* this is the first thing called */
  129. Tx->storage_class= x;
  130. return 0;
  131. }
  132.  
  133. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  134.  
  135. int Dname (int x,...)
  136. {
  137. SHORT;
  138. /* if x==1, the last token is the name of a thing being declared.  If
  139.    x==0, there is no name and this is an abstact declarator.  Either
  140.    way, build a type node and store the name.  This overwrites the type 
  141.    node, as it will be the first thing called.  */
  142.  
  143. if (x) {
  144.    get_last_token();
  145.    Tx->name= atoms[last_token];
  146.    }
  147. Tx->this_type= new type_node;
  148. Tx->root_type= Tx->this_type;
  149. return 0;
  150. }
  151.  
  152. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  153.  
  154. int TypeModifier (int x,...)
  155. {
  156. SHORT;
  157. /*  1 t()     2 t[]     3 *t     4 &t        */
  158.  
  159. switch (x) {
  160.    case 1:
  161.       Tx->this_type->primary= type_function;
  162.       // attach parameter list
  163.       Tx->this_type->aggr= completed_def_list;
  164.       break;
  165.    case 2:
  166.       Tx->this_type->primary= type_array;
  167.       // >> attach size
  168.       break;
  169.    case 3:
  170.       Tx->this_type->primary= type_pointer;
  171.       Tx->this_type->flags |= const_vol_stack[--const_vol_stacktop];
  172.       break;
  173.    case 4:
  174.       Tx->this_type->primary= type_reference;
  175.       Tx->this_type->flags |= const_vol_stack[--const_vol_stacktop];
  176.       break;
  177.    }
  178. Tx->this_type->to_what= new type_node;
  179. Tx->this_type= Tx->this_type->to_what;
  180.  
  181. return 0;
  182. }
  183.  
  184. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  185.  
  186. int ConstVol (int x,...)
  187. {
  188. SHORT;
  189. /*  1-const  2-volatile  3-both  */
  190. const_vol_stack[const_vol_stacktop++]= x;
  191. return 0;
  192. }
  193.  
  194. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  195.  
  196. int ReachType (int x,...)
  197. {
  198. SHORT;
  199. /* 0-default  1-near  2-far */
  200. return 0;
  201. }
  202.  
  203. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  204.  
  205. int NestedType (int x, ...)
  206. {
  207. SHORT;
  208. working_type* p;
  209. if (x) {  //start nesting
  210.    p= new working_type;
  211.    p->next= Tx;
  212.    Tx= p;
  213.    }
  214. else {  //restore old type
  215.    p= Tx;
  216.    Tx= Tx->next;
  217.    delete p;
  218.    }
  219. parameter_list (x);  //pass on to DEFINE module
  220. return 0;
  221. }
  222.  
  223. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  224. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  225. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  226.  
  227. int AllDoneNow (int x, ...)
  228. {
  229. SHORT;
  230. cout << "parser complete. \n";
  231. for (int loop= 0;  loop < global_stuff.size();  loop++) {
  232.    global_stuff[loop]->print();
  233.    cout.put ('\n');
  234.    }
  235. return 0;
  236. }
  237.  
  238. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  239.