-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
224 lines (215 loc) · 4.57 KB
/
table.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
符号表相关操作的实现
*/
#include "table.h"
#include "support.h"
#include "global.h"
#pragma warning(disable:4996)
/*
查找操作:若在所给符号表中找到则返回指向该符号表项的指针,否则返回NULL
t:待查找的符号表
name:欲查找的符号
level:欲查找符号所属层数
*/
tableItem* tableFind(Table &t, std::string name, AST_node parent)
{
Table::iterator res = t.find(name);
tableItem *ret = NULL;
if (res == t.end())
{
if (parent == NULL || parent->symTable == NULL)
{
return NULL;
}
return tableFind(*(parent->symTable), name, parent->parent);
}
itemList* list = &(res->second);
ret = &(list->at(0));
return ret;
}
/*
插入操作:将所给的符号及其属性插入到符号表中
t:待插入的符号表
name:待插入的符号
type:待插入符号类型:const、var、procedure、function
attribute:待插入符号属性:integer、char、array,对于函数则记录返回值类型
level:待插入符号所属层数
addr:待插入符号的附加信息地址:对于integer和char记录其值,对于array、function、procedure记录其属性结构所在的地址
*/
tableItem* tableInsert(Table &t, std::string name, LexType type, LexType attribute, int level, const void *addr, int lineNo)
{
Table::iterator res = t.find(name);
itemList* List = NULL;
if (res != t.end())
{
//符号表中已有同名符号
List = &(res->second);
for (itemList::iterator i = List->begin(); i != List->end(); i++)
{
if (i->level == level)
{
//当前层已有同名符号
error(lineNo, "Dulplicate Declaration!");
return NULL;
}
}
//当前层没有同名符号
tableItem* item = (tableItem*)malloc(sizeof(tableItem));
item->type = type;
item->attribute = attribute;
item->level = level;
item->addr = (void *)addr;
item->offset = 0;
List->push_back(*item);
return &(List->back());
}
else
{
//当前符号表中没有同名符号
List = new itemList;
tableItem* item = (tableItem*)malloc(sizeof(tableItem));
item->type = type;
item->attribute = attribute;
item->level = level;
item->addr = (void *)addr;
item->offset = 0;
List->push_back(*item);
t.insert(std::pair<std::string, itemList>::pair(name, *List));
List = &(t.find(name)->second);
return &(List->back());
}
return NULL;
}
void printTable(Table &t)
{
std::map<std::string, itemList>::iterator i = t.begin();
for (; i != t.end(); i++)
{
if (i->second.empty())
{
continue;
}
for (itemList::iterator j = i->second.begin(); j != i->second.end(); j++)
{
std::cout << i->first << ", ";
printItem(*j);
}
}
return;
}
void printItem(tableItem &i)
{
int cnt = 0;
switch (i.type)
{
case CONST:
std::cout << "const, ";
switch (i.attribute)
{
case INT:
std::cout << "int, " << *((int *)(i.addr)) << std::endl;
break;
case CHAR:
std::cout << "char, " << *((char *)i.addr) << std::endl;
break;
default:
break;
}
break;
case VAR:
std::cout << "var, ";
if (i.attribute == INT)
{
std::cout << "int, ," << std::endl;
}
else if (i.attribute == CHAR)
{
std::cout << "char, ," << std::endl;
}
else
{
std::cout << "array, ," << std::endl;
}
break;
case PRO:
cnt = ((procedureTemplet*)i.addr)->args;
std::cout << "procedure, , " << "," << cnt << ", ";
for (int n = 0; n < cnt; n++)
{
if (((procedureTemplet*)i.addr)->types->at(n) == INT)
{
std::cout << "int" << ", ";
}
else
{
std::cout << "char" << ", ";
}
}
std::cout<<", "<<std::endl;
break;
case FUN:
cnt = ((functionTemplet*)i.addr)->args;
std::cout << "function, ";
if (i.attribute == INT)
{
std::cout << "int," << ",";
}
else if (i.attribute == CHAR)
{
std::cout << "char," << ",";
}
std::cout << cnt << ",";
for (int n = 0; n < cnt; n++)
{
if (((functionTemplet*)i.addr)->types->at(n) == INT)
{
std::cout << "int" << ", ";
}
else
{
std::cout << "char" << ", ";
}
}
std::cout << std::endl;
break;
case REFERENCE:
std::cout << "reference, ";
if (i.attribute == INT)
{
std::cout << "int, ," << std::endl;
}
else if (i.attribute == CHAR)
{
std::cout << "char, ," << std::endl;
}
break;
default:
std::cout << std::endl;
break;
}
return;
}
void tableClear(Table &t, int lev)
{
Table::iterator i = t.begin();
for (; i != t.end(); i++)
{
itemList* List = &(i->second);
for (itemList::iterator j = List->begin(); j != List->end(); j++)
{
if (j->level == lev)
{
List->erase(j);
if (List->empty())
{
break;
}
else
{
j = List->begin();
}
}
}
}
return;
}