-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbolTable.cpp
More file actions
101 lines (96 loc) · 2.56 KB
/
symbolTable.cpp
File metadata and controls
101 lines (96 loc) · 2.56 KB
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
#include "symbolTable.h"
#include <iostream>
symbolTable::symbolTable()
{
index = 0;
lastIndex = 0;
}
bool symbolTable::add(std::string name, type T,bool paraMark,int newSapceMark,addition add)
{
if (newSapceMark == 2) drop();
int temp = index - 1;
bool success = true;
while (temp >= 0 && std::get<3>(table[temp]) == -1){
if (std::get<0>(table[temp]) == name
&&!(paraMark && (std::get<4>(table[temp])==_int
|| std::get<4>(table[temp]) == _void||std::get<4>(table[temp]) == _char))) {
success = false;
break;
}
temp--;
}
if (1) {
std::vector<type> paraList;
if (newSapceMark == 1) {//deeper layer
//if (index > 0)
lastIndex = index - 1;
//else
// lastIndex = index;
table.push_back(std::make_tuple(name, T, paraList, lastIndex, add));
}
else if (newSapceMark == 2) {//same depth layer
lastIndex = index-1;
table.push_back(std::make_tuple(name, T, paraList, lastIndex, add));
}
else {
table.push_back(std::make_tuple(name, T, paraList, -1, add));
}
if (paraMark) {
std::get<2>(table[lastIndex+1]).push_back(T);
}
index++;
}
/*for (int i = index-1; i >= 0; i--) {
std::cout << std::get<0>(table[i]) << " "
<< std::to_string(std::get<3>(table[i]))<<std::endl;
}
std::cout << "-------------------------\n";*/
return success;
}
int symbolTable::find(std::string name, std::vector<type> typeList)
{
int temp = index-1;
bool findMark = false;
while (temp >= 0) {
if (std::get<0>(table[temp]) == name) {
findMark = true;
break;
}
if (temp>0&&std::get<3>(table[temp]) != -1) {
temp = std::get<3>(table[temp]);
}
else {
temp--;
}
}
if (!findMark) return -1;//no find error
if (std::get<1>(table[temp]) == refun||std::get<1>(table[temp])==norefun) {
if (typeList.size() != std::get<2>(table[temp]).size()) {
return -2;//parameter number no match
}
for (int i = 0; i < typeList.size(); i++) {
if (typeList[i] != std::get<2>(table[temp])[i]) {
return -3;//parameter type no match
}
}
}
if (std::get<1>(table[temp]) == integer && std::get<4>(table[temp]) == _const) return 3;
else if (std::get<1>(table[temp]) == character && std::get<4>(table[temp]) == _const) return 4;
else if (std::get<1>(table[temp]) == integer) return 1;
else if (std::get<1>(table[temp]) == character) return 2;
else if (std::get<1>(table[temp]) == norefun) return 0;
else {
if (std::get<4>(table[temp]) == _int) return 5;
else return 6;
}
}
void symbolTable::drop()
{
index--;
while (index > 1&&std::get<3>(table[index]) == -1) {
table.pop_back();
index--;
}
std::get<3>(table[index]) = -1;
index++;
}