lookup.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef NORCSID
  6. static char rcsid[]= "$Id$";
  7. #endif
  8. #include <string.h>
  9. #include "assert.h"
  10. #include "param.h"
  11. #include "lookup.h"
  12. #include "extern.h"
  13. unsigned int hashvalue(char *s);
  14. symbol dumsym; /* dummy to return in case of error */
  15. symbol *lookup(char *name, symtype type, lookupstyle style)
  16. {
  17. symbol *sy_p,**sy_pp;
  18. for (sy_pp = &symhash[hashvalue(name)];(sy_p= *sy_pp) != 0;sy_pp= &sy_p->sy_next) {
  19. if (strcmp(sy_p->sy_name,name)!=0)
  20. continue;
  21. switch(style) {
  22. default:
  23. assert(0);
  24. case justlooking:
  25. case mustexist:
  26. case makeexist:
  27. if (type==symany || type==sy_p->sy_type)
  28. return(sy_p);
  29. continue;
  30. case newsymbol:
  31. error("%s already defined",name);
  32. return(&dumsym);
  33. }
  34. }
  35. switch(style) {
  36. default:
  37. assert(0);
  38. case justlooking:
  39. return((symbol *) 0);
  40. case mustexist:
  41. fatal("%s is unknown symbol",name);
  42. /* NOTREACHED */
  43. case newsymbol:
  44. case makeexist:
  45. NEW(sy_p,symbol);
  46. sy_p->sy_next = 0;
  47. sy_p->sy_name = mystrcpy(name);
  48. assert(type!=symany);
  49. sy_p->sy_type = type;
  50. *sy_pp = sy_p;
  51. return(sy_p);
  52. }
  53. }
  54. unsigned int hashvalue(char *s)
  55. {
  56. unsigned int sum=0;
  57. int i;
  58. for(i=0;*s;s++,i=(i+3)&07)
  59. sum += *s<<i;
  60. return(sum%NSYMHASH);
  61. }