symtab.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* s y m t a b . c
  7. *
  8. * Contains the routine findident, which builds the symbol table and
  9. * searches identifiers
  10. */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "symtab.h"
  14. struct symtab *idtable, *deftable;
  15. struct symtab *
  16. findident(s, mode, table) char *s; struct symtab **table; {
  17. /*
  18. * Look for identifier s in the symboltable referred to by *table.
  19. * If mode = LOOKING, no new entry's will be made.
  20. * If mode = ENTERING, a new entry will be made if s is not in the
  21. * table yet, otherwise an error results
  22. */
  23. register struct symtab *p;
  24. register n;
  25. if (!*table) { /* No entry for this symbol */
  26. if (mode == LOOKING) return (struct symtab *) 0;
  27. /*
  28. * Make new entry
  29. */
  30. p = (struct symtab *) malloc(sizeof *p);
  31. p->s_left = p->s_right = (struct symtab *) 0;
  32. p->s_name = malloc( (unsigned) (strlen(s) + 1));
  33. strcpy(p->s_name,s);
  34. *table = p;
  35. return p;
  36. }
  37. else {
  38. p = *table;
  39. if ((n = strcmp(p->s_name,s)) == 0) { /* This is it! */
  40. if (mode == ENTERING) {
  41. error("Identifier %s redefined",s);
  42. }
  43. return p;
  44. }
  45. /* Binary tree ..... */
  46. if (n < 0) return findident(s,mode,&(p->s_left));
  47. return findident(s,mode,&(p->s_right));
  48. }
  49. /* NOTREACHED */
  50. }