-
Notifications
You must be signed in to change notification settings - Fork 0
/
symboltable.c
106 lines (93 loc) · 2.39 KB
/
symboltable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "symboltable.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
int DJB_hash(char *str)
{
int hash = 5381;
while (*str)
{
hash += (hash << 5) + (*str++);
}
if(hash > 0)
return hash % 1000;
else return (-hash) % 1000;
}
void init_table(Item ht[]){
int i = 0;
for(;i < 1000;i ++)
ht[i] = NULL;
}
void insert_var_table(Item item, Item ht[], Item st[], int scope){
char* name;
name = (char*)malloc(sizeof(20));
strcpy(name, (const char*)item -> name);
int location = DJB_hash(name);
//printf("%d\n", location);
if(ht[location] == NULL){
ht[location] = item;
item -> nextr = NULL;
}
else{
Item p = ht[location];
ht[location] = item;// add the newest item to the head of the linklist in hash table
item -> nextr = p;
}
if(st[scope] == NULL)
st[scope] = item;
else{
Item p = st[scope];
st[scope] = item;// add the newest item to the head of the linklist in scope table
item -> nextc = p;
}
}
void insert_struct_table(Item item, Item ht[]){
char* name;
name = (char*)malloc(sizeof(20));
strcpy(name, (const char*)item -> name);
int location = DJB_hash(name);
if(ht[location] == NULL){
ht[location] = item;
item -> nextr = NULL;
}
else{
Item p = ht[location];
ht[location] = item;// add the newest item to the head of the linklist
item -> nextr = p;
}
}
void insert_func_table(Item item, Item ht[]){
char* name;
name = (char*)malloc(sizeof(20));
strcpy(name, (const char*)item -> name);
int location = DJB_hash(name);
if(ht[location] == NULL){
ht[location] = item;
item -> nextr = NULL;
}
else{
Item p = ht[location];
ht[location] = item;// add the newest item to the head of the linklist
item -> nextr = p;
}
}
void delete_scope(int scope, Item ht[], Item st[]){//dang qian shan chu de ceng yi ding shi zui nei ceng
if(st[scope] != NULL){
Item it = st[scope];
for( ; it != NULL; it = it -> nextc){// shan chu hash biao zhong dui ying de biao xiang
char *name;
name = (char*)malloc(sizeof(20));
strcpy(name, (const char*)it -> name);
int location = DJB_hash(name);
ht[location] = ht[location] -> nextr;// zui nei ceng yi ding shi zui hou jia ru de, gu zai di yi xiang
}
it = st[scope];
Item q;
while(it != NULL){// destroy the linklist in scope table
q = it -> nextc;
free(it);
it = q;
}
}
st[scope] = NULL;// this is critical!!!
}