From 0e835d9b8f97087810dddb9743aa84b6431ff3a7 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 12:55:31 -0400 Subject: Rename hash.* -> scope.* --- hash.c | 143 --------------------------------------------------------------- hash.h | 27 ------------ makefile | 8 ++-- pc.y | 2 + scope.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scope.h | 27 ++++++++++++ 6 files changed, 176 insertions(+), 174 deletions(-) delete mode 100644 hash.c delete mode 100644 hash.h create mode 100644 scope.c create mode 100644 scope.h diff --git a/hash.c b/hash.c deleted file mode 100644 index 21ddb1a..0000000 --- a/hash.c +++ /dev/null @@ -1,143 +0,0 @@ -#include -#include -#include -#include - -#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); - } - } -} diff --git a/hash.h b/hash.h deleted file mode 100644 index 8a8cdb1..0000000 --- a/hash.h +++ /dev/null @@ -1,27 +0,0 @@ -#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 diff --git a/makefile b/makefile index 9a2f51a..8dd5899 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,8 @@ FLAGS = -g YACC = yacc LEX = lex -mypc: y.tab.o lex.yy.o tree.o hash.o node.o pc.o - $(CC) $(FLAGS) -o mypc main.o tree.o hash.o node.o y.tab.o lex.yy.o -lfl -ly +mypc: y.tab.o lex.yy.o tree.o scope.o node.o pc.o + $(CC) $(FLAGS) -o mypc main.o tree.o scope.o node.o y.tab.o lex.yy.o -lfl -ly pc.o: main.c pc.h $(CC) $(FLAGS) -c main.c @@ -12,8 +12,8 @@ pc.o: main.c pc.h tree.o: tree.c tree.h $(CC) $(FLAGS) -c tree.c -hash.o: hash.c hash.h - $(CC) $(FLAGS) -c hash.c +scope.o: scope.c scope.h + $(CC) $(FLAGS) -c scope.c node.o: node.c node.h $(CC) $(FLAGS) -c node.c diff --git a/pc.y b/pc.y index 495f64a..e8f2baa 100644 --- a/pc.y +++ b/pc.y @@ -2,6 +2,8 @@ #include #include +#include "node.h" +#include "scope.h" #include "tree.h" #include "y.tab.h" #include "pc.h" diff --git a/scope.c b/scope.c new file mode 100644 index 0000000..32167fc --- /dev/null +++ b/scope.c @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +#include "node.h" +#include "scope.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); + } + } +} diff --git a/scope.h b/scope.h new file mode 100644 index 0000000..8a8cdb1 --- /dev/null +++ b/scope.h @@ -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 -- cgit v1.1