diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 12:41:50 -0400 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 12:41:50 -0400 |
commit | e0e828e2c2f2032d1c9616950d0208f2aab6fcb8 (patch) | |
tree | 8be0532e66d6e2f03d7eaa0efca9700b1bf71386 | |
parent | 596bc33348e2daa5af8a0168f015e850aeccf0fc (diff) |
Add basic scopes
-rw-r--r-- | hash.c | 143 | ||||
-rw-r--r-- | hash.h | 27 | ||||
-rw-r--r-- | node.c | 10 | ||||
-rw-r--r-- | node.h | 6 |
4 files changed, 184 insertions, 2 deletions
@@ -0,0 +1,143 @@ +#include <stdio.h> +#include <stdilb.h> +#include <assert.h> +#include <string.h> + +#include "node.h" +#include "hash.h" + +scope* mkscope(prev) +scope* prev; +{ + int i; + + scope *p = malloc(sizeof(scope)); + assert(p); + + for (i = 0; i < HASH_SIZE; i++) + p->table[i] = NULL; + + p->next = next; + p->function_boundry = 0; + + return p; +} + +void free_scope(s) +scope *s; +{ + int i; + + if (!s) + return; + + for (i = 0; i < HASH_SIZE; i++) { + free_nodes(s->table[i]); + } + + free(s); +} + +/*Copied from Compilers, Aho*/ +#define EOS '\0' +int hashpjw(s) +char* s; +{ + char *p; + unsigned h = 0, g; + + for (p = s; *p != EOS; p++) { + h = (h<<4) + *p; + if (g = h & 0xf0000000) { + h^= g>>24; + h^= g; + } + } + + return h % HASH_SIZE; +} + +scope* push_scope(root); +scope* root; +{ + scope *p = mkscope(); + p->next = root; + return p; +} + +scope* pop_scope(root); +scope *root; +{ + scope *p; + + if (!root) + return NULL; + + p = root->next; + + free_scope(root); + return p; +} + +node* scope_insert(s, name) +scope *s; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return root->table[hash] = list_insert(tmp, name); +} + +node* scope_search(root, name) +scope *root; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return list_search(tmp, name); +} + +node* scope_search_all(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) + if (tmp = scope_search(p, name)) + return tmp; + + return NULL; +} + +node* scope_safe_search(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) { + if (tmp = scope_search(p, name)) + return tmp; + if (p->f) + return NULL + } + + return NULL; +} + +void print_scope(s) +scope *s; +{ + int i; + node_t * tmp; + for (i = 0; i < HASH_SIZE; i++) { + for( tmp=s->table[i]; tmp; tmp = tmp->next) { + fprintf(stderr, "\t%s\n", tmp->name); + } + } +} @@ -0,0 +1,27 @@ +#ifndef SCOPE_H +#define SCOPE_H + +#define HASH_SIZE 211 + +typedef struct hash { + node* table[HASH_SIZE]; + struct hash *prev, *next; + char function_boundry; +} scope; + +scope* mkscope(scope*); +void free_scope(scope*); + +/*stack routines*/ +scope* pop_scope(scope*); +scope* push_scope(scope*); + +/*helpers*/ +node* scope_insert(scope*, char*); +node* scope_search_all(scope*, char*); +node* scope_search(scope*, char*); +node* scope_safe_search_all(scope*, char*); + +/*hash function*/ +int hash_pwj(char*); +#endif @@ -41,3 +41,13 @@ char * str; p->next = root; return p; } + +void free_list(n) +node *n; +{ + node *tmp; + + for(tmp = n; tmp; tmp = n = n->next) { + free(tmp); + } +} @@ -13,7 +13,9 @@ typedef struct node_s { node* mknode(char *); /* helpers */ -node* search(node*, char *); -node* insert(node*, char*); +node* list_search(node*, char *); +node* list_insert(node*, char*); + +void free_list(node*); #endif |