lookup.c 1.3 KB

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