From 6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 11 Feb 2020 12:24:56 +0100 Subject: Rename and cleanup --- sem3/osc/mm11/regn/symtab.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sem3/osc/mm11/regn/symtab.c (limited to 'sem3/osc/mm11/regn/symtab.c') diff --git a/sem3/osc/mm11/regn/symtab.c b/sem3/osc/mm11/regn/symtab.c new file mode 100644 index 0000000..8103203 --- /dev/null +++ b/sem3/osc/mm11/regn/symtab.c @@ -0,0 +1,50 @@ +#include "symtab.h" +#include +#include + +unsigned int hash(char *s) { + uint32_t hv = 0; + for( int i = 0; s[i] != '\0'; i++ ) { + // take MSB 6 bits of hv and xors with LSB of s[i] + uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f); + + // Push those back on hv + hv = (hv << 4) | v; + } + // Return appropriate size + return hv % HASHSIZE; +} + + +symnode_t *sym_insert(char *var) { + unsigned int index = hash(var); + + // Save old value + symnode_t *oldSym = symbolarray[index]; + + // Make new + symbolarray[index] = malloc(sizeof(symnode_t)); + if( symbolarray[index] == NULL ) { + // If malloc failed + symbolarray[index] = oldSym; + return NULL; + } + + // Link old one + symbolarray[index]->next = oldSym; + symbolarray[index]->name = var; + + return symbolarray[index]; +} + +symnode_t *sym_lookup(char *var) { + unsigned int index = hash(var); + symnode_t *n = symbolarray[index]; + + // Look trough list + while(n != NULL && strcmp(n->name, var) != 0) { + n = n->next; + } + + return n; +} -- cgit v1.2.3