Pristine Ack-5.5
[Ack-5.5.git] / util / topgen / symtab.c
1 /* $Id: symtab.c,v 1.4 1994/06/24 10:42:24 ceriel Exp $ */
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 "symtab.h"
12
13 struct symtab *idtable, *deftable;
14
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     char *malloc();
24     char *strcpy();
25     register struct symtab *p;
26     register n;
27
28     if (!*table) {      /* No entry for this symbol */
29         if (mode == LOOKING) return (struct symtab *) 0;
30         /*
31          * Make new entry
32          */
33         p = (struct symtab *) malloc(sizeof *p);
34         p->s_left = p->s_right = (struct symtab *) 0;
35         p->s_name = malloc( (unsigned) (strlen(s) + 1));
36         strcpy(p->s_name,s);
37         *table = p;
38         return p;
39     }
40     else {
41         p = *table;
42         if ((n = strcmp(p->s_name,s)) == 0) {   /* This is it! */
43             if (mode == ENTERING) {
44                 error("Identifier %s redefined",s);
45             }
46             return p;
47         }
48         /* Binary tree ..... */
49         if (n < 0) return findident(s,mode,&(p->s_left));
50         return findident(s,mode,&(p->s_right));
51     }                           
52     /* NOTREACHED */
53 }