home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / sources / c / symbol < prev    next >
Encoding:
Text File  |  1988-12-29  |  3.6 KB  |  140 lines

  1.  
  2. /*
  3.      Little Smalltalk
  4.  
  5.           symbol creation - symbols are never deleted once created.
  6.           timothy a. budd, 10/84
  7. */
  8. /*
  9.      The source code for the Little Smalltalk System may be freely
  10.      copied provided that the source of all files is acknowledged
  11.      and that this condition is copied with each file.
  12.  
  13.      The Little Smalltalk System is distributed without responsibility
  14.      for the performance of the program and without any guarantee of
  15.      maintenance.
  16.  
  17.      All questions concerning Little Smalltalk should be addressed to:
  18.  
  19.           Professor Tim Budd
  20.           Department of Computer Science
  21.           Oregon State University
  22.           Corvallis, Oregon
  23.           97331
  24.           USA
  25. */
  26. # include <stdio.h>
  27. # include "object.h"
  28. # include "symbol.h"
  29.  
  30. /*
  31.      only one copy of symbol values are kept.
  32.      multiple copies of the same symbol point to the same
  33.      location.
  34.      sy_search will find, and if necessary insert, a string into
  35.      this common table 
  36. */
  37.  
  38. extern char x_str[];          /* initialized common string table */
  39. extern symbol *x_tab[];       /* initialized common symbols table */
  40. extern int x_tmax;       /* top of symbols table */
  41. extern char *walloc();        /* routine to allocate a new word */
  42. int ca_sym = 0;               /* symbol allocation counter */
  43.  
  44. /* sy_search performs a binary search of a symbol, is the main interface to
  45. the symbols routines */
  46. symbol *sy_search(word, insert)
  47. char *word;
  48. int  insert;
  49. {    register int i;
  50.      register int j;
  51.      register int k;
  52.      char *p;
  53.      symbol *new_y();
  54.  
  55.      for (i=1; i <= x_tmax; i <<= 1);
  56.      for (i >>= 1, j = i >>1, i--; ; j >>= 1) {
  57.           p = symbol_value(x_tab[i]);
  58.           if (word == p) return(x_tab[i]);
  59.           k = *word - *p;
  60.           if (!k) k = *(word+1) - *(p+1);
  61.           if (!k) k = strcmp(word, p);
  62.           if (!k)
  63.                return(x_tab[i]);
  64.           if (!j) break;
  65.           if (k < 0) i -= j;
  66.           else {
  67.                if ((i += j) > x_tmax) i = x_tmax;
  68.                }
  69.           }
  70.      if (insert) {
  71.           if (k > 0) i++;
  72.           if ((k = ++x_tmax) >= SYMTABMAX)
  73.                cant_happen(12);
  74.           for (; k > i; k--) {
  75.                x_tab[k] = x_tab[k-1];
  76.                }
  77.           /*fprintf(stderr,"adding %s\n", word);*/
  78.           x_tab[i] = new_y(walloc(word));
  79.           x_tab[i]->y_ref_count++; /* make sure not freed */
  80.           return(x_tab[i]);
  81.           }
  82.      else return((symbol *) 0);
  83. }
  84.  
  85. /* w_search performs a search for a word, not a symbol */
  86. char *w_search(word, insert)
  87. char *word;
  88. int insert;
  89. {    symbol *sym;
  90.  
  91.      sym = sy_search(word, insert);
  92.      if (sym)
  93.           return(symbol_value(sym));
  94.      else
  95.           return((char *) 0);
  96. }
  97.  
  98. /*---------------------------------------*/
  99.  
  100. static mstruct *fr_symbol = 0;          /* symbols free list */
  101. static symbol strspace[SYMINITSIZE];    /* initial symbols free list */
  102.  
  103. extern object *o_object;      /* common instance of Object */
  104. extern class *ArrayedCollection;
  105.  
  106. /* sym_init - initialize the symbols routine */
  107. sym_init() {
  108.      int  i;
  109.      symbol *p;
  110.      mstruct   *new;
  111.  
  112.      p = strspace;
  113.      for (i = 0; i < SYMINITSIZE; i++) {
  114.           new = (mstruct *) p;
  115.           new->mlink = fr_symbol;
  116.           fr_symbol = new;
  117.           p++;
  118.           }
  119. }
  120.  
  121. /* new_y is the internal routine for making new symbols */
  122. symbol *new_y(text)
  123. char *text;
  124. {    symbol *new;
  125.  
  126.      if (fr_symbol) {
  127.           new = (symbol *) fr_symbol;
  128.           fr_symbol = fr_symbol->mlink;
  129.           }
  130.      else {
  131.           ca_sym++;
  132.           new = structalloc(symbol);
  133.           }
  134.  
  135.      new->y_ref_count = 0;
  136.      new->y_size = SYMBOLSIZE;
  137.      new->y_value = text;
  138.      return(new);
  139. }
  140.