symtab.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "main.h"
  15. struct symtab *idtable, *deftable;
  16. struct symtab *findident(char *s, int mode, struct symtab **table)
  17. {
  18. /*
  19. * Look for identifier s in the symboltable referred to by *table.
  20. * If mode = LOOKING, no new entry's will be made.
  21. * If mode = ENTERING, a new entry will be made if s is not in the
  22. * table yet, otherwise an error results
  23. */
  24. struct symtab *p;
  25. int n;
  26. if (!*table) { /* No entry for this symbol */
  27. if (mode == LOOKING) return (struct symtab *) 0;
  28. /*
  29. * Make new entry
  30. */
  31. p = (struct symtab *) malloc(sizeof *p);
  32. p->s_left = p->s_right = (struct symtab *) 0;
  33. p->s_name = malloc( (unsigned) (strlen(s) + 1));
  34. strcpy(p->s_name,s);
  35. *table = p;
  36. return p;
  37. }
  38. else {
  39. p = *table;
  40. if ((n = strcmp(p->s_name,s)) == 0) { /* This is it! */
  41. if (mode == ENTERING) {
  42. error("Identifier %s redefined",s);
  43. }
  44. return p;
  45. }
  46. /* Binary tree ..... */
  47. if (n < 0) return findident(s,mode,&(p->s_left));
  48. return findident(s,mode,&(p->s_right));
  49. }
  50. /* NOTREACHED */
  51. }