|
|
1.1 ! root 1: /* symbol.c: ! 2: General: functions for mapping {string} x {int} -> {int} ! 3: In pass 1: {name} x {viewnum} -> {int file line num} ! 4: In pass 2: {} x {int file line num} -> {slot num} ! 5: Therefore be careful with blank strings ! 6: */ ! 7: ! 8: #include "fdevelop.h" ! 9: ! 10: #define steq(s, n, p) (p->innum == n && ((p->instr == s) || eq(p->instr,s))) ! 11: ! 12: #define SIZE 2053 ! 13: Symbol *head[SIZE]; ! 14: ! 15: int hash(s, n) /* form hash value */ ! 16: register char *s; ! 17: register int n; ! 18: { ! 19: register int hashval; ! 20: ! 21: for (hashval = 0; *s != '\0'; s++) ! 22: hashval = (*s + 31 * hashval) % SIZE; ! 23: hashval = (31 * hashval + 1259 * n) % SIZE; ! 24: return hashval; ! 25: } ! 26: ! 27: Symbol *lookup(s, n) /* return element with s, n */ ! 28: register char *s; ! 29: register int n; ! 30: { ! 31: register Symbol *p; ! 32: for (p = head[hash(s, n)]; p != NULL; p = p->next) ! 33: if (steq(s, n, p)) ! 34: return p; ! 35: return NULL; ! 36: } ! 37: ! 38: insert(s, n, v) /* insert s, n with value v */ ! 39: register char *s; ! 40: register int n; ! 41: int v; ! 42: { ! 43: register Symbol *p; ! 44: char *q; ! 45: int i; ! 46: ! 47: /* fprintf(stderr, "Inserting: |%s|, %d with hash %d\n", ! 48: s, n, hash(s,n)); */ ! 49: if (*s == '\0') ! 50: q = NULLSTR; ! 51: else { ! 52: q = emalloc(strlen(s)+1); ! 53: strcpy(q, s); ! 54: } ! 55: p = (Symbol *) emalloc(sizeof(Symbol)); ! 56: p->instr = q; ! 57: p->innum = n; ! 58: p->outnum = v; ! 59: i = hash(s, n); ! 60: p->next = head[i]; ! 61: head[i] = p; ! 62: } ! 63: ! 64: delete(s, n) /* remove s, n */ ! 65: char *s; ! 66: int n; ! 67: { ! 68: Symbol *p, *pp; ! 69: int i; ! 70: ! 71: /* fprintf(stderr, "Deleting: |%s|, %d with hash %d\n", ! 72: s, n, hash(s,n)); */ ! 73: i = hash(s, n); ! 74: pp = NULL; ! 75: for (p = head[i]; p != NULL; p = p->next) { ! 76: if (steq(s, n, p)) ! 77: break; ! 78: pp = p; ! 79: } ! 80: if (p == NULL) ! 81: error(FATAL, "symtab bug: bad delete"); ! 82: if (p->instr != NULLSTR) ! 83: efree(p->instr); ! 84: if (pp == NULL) { ! 85: head[i] = p->next; ! 86: } else { ! 87: pp->next = p->next; ! 88: } ! 89: efree((char *) p); ! 90: } ! 91: ! 92: opensymtab() /* init table */ ! 93: { ! 94: int i; ! 95: ! 96: for (i = 0; i < SIZE; i++) ! 97: head[i] = NULL; ! 98: } ! 99: ! 100: closesymtab() /* reclaim storage */ ! 101: { ! 102: int i; ! 103: Symbol *p, *np; ! 104: ! 105: for (i = 0; i < SIZE; i++) ! 106: for (p = head[i]; p != NULL; p = np) { ! 107: if (p->instr != NULLSTR) ! 108: efree(p->instr); ! 109: np = p->next; ! 110: efree((char *) p); ! 111: } ! 112: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.