lookup.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <string.h>
  6. #include "assert.h"
  7. #include "param.h"
  8. #include "lookup.h"
  9. #include "error.h"
  10. #include "lookup.h"
  11. #include "extern.h"
  12. symbol dumsym; /* dummy to return in case of error */
  13. symbol *lookup(char *name, symtype type, lookupstyle style)
  14. {
  15. symbol *sy_p,**sy_pp;
  16. for (sy_pp = &symhash[hashvalue(name)];(sy_p= *sy_pp) != 0;sy_pp= &sy_p->sy_next) {
  17. if (strcmp(sy_p->sy_name,name)!=0)
  18. continue;
  19. switch(style) {
  20. default:
  21. assert(0);
  22. case justlooking:
  23. case mustexist:
  24. case makeexist:
  25. if (type==symany || type==sy_p->sy_type)
  26. return(sy_p);
  27. continue;
  28. case newsymbol:
  29. error("%s already defined",name);
  30. return(&dumsym);
  31. }
  32. }
  33. switch(style) {
  34. default:
  35. assert(0);
  36. case justlooking:
  37. return((symbol *) 0);
  38. case mustexist:
  39. fatal("%s is unknown symbol",name);
  40. /* NOTREACHED */
  41. case newsymbol:
  42. case makeexist:
  43. NEW(sy_p,symbol);
  44. sy_p->sy_next = 0;
  45. sy_p->sy_name = mystrcpy(name);
  46. assert(type!=symany);
  47. sy_p->sy_type = type;
  48. *sy_pp = sy_p;
  49. return(sy_p);
  50. }
  51. }
  52. unsigned int hashvalue(char *s)
  53. {
  54. unsigned int sum=0;
  55. int i;
  56. for(i=0;*s;s++,i=(i+3)&07)
  57. sum += *s<<i;
  58. return(sum%NSYMHASH);
  59. }