home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_40.arc / DAIMS.ARC / SYMCLASS.CPP < prev    next >
Text File  |  1988-02-10  |  3KB  |  149 lines

  1. #undef TEST
  2. #include <stream.h>
  3. #include <string.h>
  4. #include "matrix.hxx"
  5. #include "strings.hxx"
  6. #include "symclass.hxx"
  7.  
  8. /* 
  9. -*++ sym_tab::sym_tab(): constructor for a symbol table
  10. ** 
  11. ** (*++ history: 
  12. **      7 Dec 87    Bruce Eckel    Creation date
  13. ** ++*)
  14. ** 
  15. ** (*++ detailed: 
  16. ** ++*)
  17. */
  18.  
  19.  sym_tab::sym_tab() { 
  20.     for (int i = 0; i < TBLSZ; tb[i++] = 0) 
  21.       ; 
  22.   }; 
  23.  
  24.  
  25.  
  26. /* 
  27. -*++ sym_tab::~sym_tab(): Destructor for a symbol table
  28. ** 
  29. ** (*++ history: 
  30. **      7 Dec 87    Bruce Eckel    Creation date
  31. ** ++*)
  32. ** 
  33. ** (*++ detailed: 
  34. ** ++*)
  35. */
  36.  
  37.  sym_tab::~sym_tab() {        /* destructor */
  38.   for (int i = 0; i < TBLSZ; i++)
  39.     for (name* n = tb[i]; n; ) {
  40.       name * nxt = n->next;
  41.       delete n->label;
  42.       delete n;
  43.       n = nxt;
  44.     }
  45. }
  46.  
  47.  
  48. /* 
  49. -*++ sym_tab::dump(): dump a symbol table to cout
  50. ** 
  51. ** (*++ history: 
  52. **      7 Dec 87    Bruce Eckel    Creation date
  53. ** ++*)
  54. ** 
  55. ** (*++ detailed: 
  56. ** ++*)
  57. */
  58.  
  59. void sym_tab::dump() {
  60.   for (int i = 0; i < TBLSZ; i++)
  61.     for (name* n=tb[i]; n; n=n->next) {
  62.       cout << n->label << ":\n";
  63.       switch (n->type) {
  64.       case INTEGER : cout << "INTEGER : " << n->value.intval << "\n"; break;
  65.       case DOUBLE : cout << "DOUBLE : " << n->value.doubleval << "\n"; break;
  66. //      case MATRIX : cout << "MATRIX :\n" << n->value.matptr << "\n"; break;
  67.       case KEYWORD : cout << "KEYWORD"; break;
  68.       default: cout << "UNKNOWN TYPE";
  69.       }
  70.     }
  71. }
  72.   
  73.  
  74. /* 
  75. -*++ sym_tab::look(): look up or insert table names
  76. ** 
  77. ** (*++ history: 
  78. **      7 Dec 87    Bruce Eckel    Creation date
  79. ** ++*)
  80. ** 
  81. ** (*++ detailed: 
  82. ** ins == 0 means look up name in table, return 0 if name not found,
  83. ** pointer to name structure containing the string if found  
  84. ** ins != 0 means insert a new name or a new keyword 
  85. ** ins == NEWNAME means insert new name in table 
  86. ** ins == NEWKEYWORD means insert new keyword in table 
  87. ** ++*)
  88. */
  89.  
  90. name* sym_tab::look(char* p, int ins = 0) {
  91.  
  92.   int ii = 0;
  93.   char* pp = p;
  94.   while (*pp) ii = ii<<1 ^ *pp++; /* hash function */
  95.   if (ii < 0) ii = -ii;
  96.   ii %= TBLSZ;
  97.  
  98.   for (name* n=tb[ii]; n; n=n->next)    /* search */
  99.     if (strcmp(p,n->label) == 0) return n;
  100.  
  101.   if (ins == 0) return (name *)0; /* string not found */
  102.  
  103.   name* nn = new name;        /* insert a new name */
  104.   nn->label = new char[strlen(p) + 1];
  105.   strcpy(nn->label,p);
  106.   nn->type = INTEGER;        /* defaults to int */
  107.   nn->value.doubleval = 1;
  108.   nn->value.intval = 1;
  109.   nn->next = tb[ii];
  110.   tb[ii] = nn;
  111.   return nn;
  112. }
  113.  
  114.  
  115. /* 
  116. -*++ sym_tab::remove(): removes label but leaves links in place
  117. ** 
  118. ** (*++ history: 
  119. **      7 Dec 87    Bruce Eckel    Creation date
  120. ** ++*)
  121. ** 
  122. ** (*++ detailed: removes label but leaves links in place.  look() won't find
  123. ** the name,  so if the user wants to try re-defining the variable, s/he can.
  124. ** To do this "neatly", the list would have to be doubly-linked. 
  125. ** ++*)
  126. */
  127.  
  128. void sym_tab::remove(name * nn) 
  129. {
  130.   *(nn->label) = 0;
  131. }
  132.  
  133.  
  134. #ifdef TEST
  135. main()
  136. {
  137.   sym_tab table;
  138.   table.keyword("if");
  139.   table.keyword("then");
  140.   table.keyword("else");
  141.   table.insert("pi")->doubleval = 3.14159;
  142.   table.insert("e")->doubleval = 2.71828;
  143.   table.insert("hello");
  144.   table.look("hello")->doubleval = 7.14;
  145.   table.dump();
  146.   
  147. }
  148. #endif
  149.