Rev | Line | |
---|
[9] | 1 | #include "defs.h"
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | /* TABLE_SIZE is the number of entries in the symbol table. */
|
---|
| 5 | /* TABLE_SIZE must be a power of two. */
|
---|
| 6 |
|
---|
| 7 | #define TABLE_SIZE 1024
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | bucket **symbol_table;
|
---|
| 11 | bucket *first_symbol;
|
---|
| 12 | bucket *last_symbol;
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | int
|
---|
| 16 | hash(name)
|
---|
| 17 | char *name;
|
---|
| 18 | {
|
---|
| 19 | register char *s;
|
---|
| 20 | register int c, k;
|
---|
| 21 |
|
---|
| 22 | assert(name && *name);
|
---|
| 23 | s = name;
|
---|
| 24 | k = *s;
|
---|
| 25 | while (c = *++s)
|
---|
| 26 | k = (31*k + c) & (TABLE_SIZE - 1);
|
---|
| 27 |
|
---|
| 28 | return (k);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | bucket *
|
---|
| 33 | make_bucket(name)
|
---|
| 34 | char *name;
|
---|
| 35 | {
|
---|
| 36 | register bucket *bp;
|
---|
| 37 |
|
---|
| 38 | assert(name);
|
---|
| 39 | bp = (bucket *) MALLOC(sizeof(bucket));
|
---|
| 40 | if (bp == 0) no_space();
|
---|
| 41 | bp->link = 0;
|
---|
| 42 | bp->next = 0;
|
---|
| 43 | bp->name = MALLOC(strlen(name) + 1);
|
---|
| 44 | if (bp->name == 0) no_space();
|
---|
| 45 | bp->tag = 0;
|
---|
| 46 | bp->value = UNDEFINED;
|
---|
| 47 | bp->index = 0;
|
---|
| 48 | bp->prec = 0;
|
---|
| 49 | bp-> class = UNKNOWN;
|
---|
| 50 | bp->assoc = TOKEN;
|
---|
| 51 |
|
---|
| 52 | if (bp->name == 0) no_space();
|
---|
| 53 | strcpy(bp->name, name);
|
---|
| 54 |
|
---|
| 55 | return (bp);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | bucket *
|
---|
| 60 | lookup(name)
|
---|
| 61 | char *name;
|
---|
| 62 | {
|
---|
| 63 | register bucket *bp, **bpp;
|
---|
| 64 |
|
---|
| 65 | bpp = symbol_table + hash(name);
|
---|
| 66 | bp = *bpp;
|
---|
| 67 |
|
---|
| 68 | while (bp)
|
---|
| 69 | {
|
---|
| 70 | if (strcmp(name, bp->name) == 0) return (bp);
|
---|
| 71 | bpp = &bp->link;
|
---|
| 72 | bp = *bpp;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | *bpp = bp = make_bucket(name);
|
---|
| 76 | last_symbol->next = bp;
|
---|
| 77 | last_symbol = bp;
|
---|
| 78 |
|
---|
| 79 | return (bp);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | create_symbol_table()
|
---|
| 84 | {
|
---|
| 85 | register int i;
|
---|
| 86 | register bucket *bp;
|
---|
| 87 |
|
---|
| 88 | symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
|
---|
| 89 | if (symbol_table == 0) no_space();
|
---|
| 90 | for (i = 0; i < TABLE_SIZE; i++)
|
---|
| 91 | symbol_table[i] = 0;
|
---|
| 92 |
|
---|
| 93 | bp = make_bucket("error");
|
---|
| 94 | bp->index = 1;
|
---|
| 95 | bp->class = TERM;
|
---|
| 96 |
|
---|
| 97 | first_symbol = bp;
|
---|
| 98 | last_symbol = bp;
|
---|
| 99 | symbol_table[hash("error")] = bp;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | free_symbol_table()
|
---|
| 104 | {
|
---|
| 105 | FREE(symbol_table);
|
---|
| 106 | symbol_table = 0;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | free_symbols()
|
---|
| 111 | {
|
---|
| 112 | register bucket *p, *q;
|
---|
| 113 |
|
---|
| 114 | for (p = first_symbol; p; p = q)
|
---|
| 115 | {
|
---|
| 116 | q = p->next;
|
---|
| 117 | FREE(p);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.