-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.cpp
83 lines (73 loc) · 2.41 KB
/
SymbolTable.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
//
// Created by WANG on 20/5/2022.
//
#include "SymbolTable.h"
TableSet::TableSet() {
tables.emplace_back(); // 新加一张空表
tables[0].emplace_back("main",Producer,0);
}
int TableSet::mkTable(int fa, const string& producer) {
if(fa == -1) {
tables.emplace_back();
tables.back().emplace_back(producer, Producer);
}
else{
tables.emplace_back(tables[fa].level + 1, fa); // 新建一张表
tables[fa].emplace_back(producer, Producer); // 旧表也加入一条
tables.back().emplace_back(producer, Producer); // 表中第一行一定为程序
}
return (int)tables.size()-1;
}
bool TableSet::enter(int tablePos, const std::string& name, lexType kind, int val) {
int posIter = tablePos;
while(posIter != -1){
int tmp = tables[posIter].findItem(name);
if(tmp != -1) return false;
posIter = tables[posIter].faLink;
}
tables[tablePos].emplace_back(name,kind,val);
return true;
}
pair<int,int> TableSet::lookup(int tablePos, const std::string& name) {
pair<int,int> ans; // first为表下标,second为项下标
int nowPos = tablePos;
while(nowPos != -1){
ans.first = nowPos;
ans.second = tables[nowPos].findItem(name);
if(ans.second == -1){
nowPos = tables[nowPos].faLink;
if(nowPos < 0) break;
}
else return ans;
}
ans = make_pair(-1,-1);
return ans;
}
void TableSet::backPatchProc(int tablePos, int nxq) {
// 回填程序的四元式
tables[tablePos].OneTable[0].val = nxq;
string name = tables[tablePos].OneTable[0].name;
int fa = tables[tablePos].faLink;
if(fa == -1) return;
int a = tables[fa].findItem(name);
if(tables[fa].OneTable[a].kind == Producer)
tables[fa].OneTable[a].val = nxq;
else throw handleError("回填程序四元式未找到");
}
TableItem TableSet::getData(int tableNum,int pos) {
return tables[tableNum].OneTable[pos];
}
void TableSet::print() {
ofstream out("nameTable.out");
for(const auto& x:tables){
out<<"============================================"<<endl;
for(const auto& a:x.OneTable)
out<<a.name<<" "<<a.kind<<" "<<a.val<<" "<<a.address<<endl;
}
}
int TableSet::getLevelDiff(int table1, int table2) {
return tables[table2].level - tables[table1].level;
}
int TableSet::getVarNum(int tablePos) {
return tables[tablePos].variableNum;
}