-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
76 lines (63 loc) · 1.33 KB
/
symbol.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
#include <string.h>
#include <stdlib.h>
#include "table.h"
#include "symbol.h"
#include "util.h"
#define SIZE (109)
struct symbol_ {
char * name;
symbol next;
};
static symbol mksymbol(char *name ,symbol next){
symbol s= checked_malloc(sizeof(*s));
s->name=checked_malloc(strlen(name)+3);// 手动分配内存
memset(s->name,0,sizeof(s->name));
strcpy(s->name,name);
s->next=next;
return s;
}
static symbol hashtable[SIZE];// 处理所有的符号
static unsigned int hash(char *s){
unsigned int ha=0;
for(char* i=s;*i;i++){
ha=ha*65599+*i;
}
return ha;
}
symbol Symbol(char *name ){
int h =hash(name)%SIZE;
symbol syms = hashtable[h];
for(symbol sym=syms;sym;sym=sym->next){
if(!strcmp(sym->name,name)){
return sym;
}
}
symbol sym=mksymbol(name,hashtable[h]);
hashtable[h]=sym;
return sym;
}
// 给定一个 symbol 返回对应的 char *
char * name(symbol sym){
return sym->name;
}
// 符号表的管理
TAB_table S_empty(){
return TAB_empty();
}
// 对hash table 的一层封装
void S_enter(TAB_table t,symbol sym,void* value){
TAB_enter(t,sym,value);
}
void *S_look(TAB_table t,symbol sym){
return TAB_look(t,sym);
}
static struct symbol_ marksym = {"<mark>", 0};
// 最关键的函数block 结构管理
void beginScope(TAB_table t){
S_enter(t,&marksym,NULL);
}
void endScope(TAB_table t){
symbol s;
do s=TAB_pop(t);
while (s != &marksym);
}