-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.cpp
More file actions
127 lines (120 loc) · 3.13 KB
/
Copy pathSymbolTable.cpp
File metadata and controls
127 lines (120 loc) · 3.13 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
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
#include "SymbolTable.hpp"
void SymbolTable::update_top_table()
{
this->top_table.push_back(this->s_table.size());
this->top++;
}
bool SymbolTable::insert_table(table in_table)
{
if (this->check_duplicates(in_table))
{
this->s_table.push_back(in_table);
return true;
}
/* if (this->check_duplicates(in_table))
{
this->s_table.push_back(in_table);
string p1 =in_table.parent;
string p2=this->s_table[this->s_table.size()-2].parent;
/* 进入新的子程序块 */
/* if(p1!=p2)
{
this->top_table.push_back(this->s_table.size()-1);
this->top++;
}
return true;
}*/
else
{
cout << "redeclaration of " << in_table.name << endl;
return false;
}
}
bool SymbolTable::check_duplicates(table in_table)
{
string id_name = in_table.name;
if (0 == this->s_table.size())
{
this->top_table.push_back(0);
return true;
}
int stack_top = this->s_table.size() - 1;
int stack_bot = this->top_table[this->top];
for (int i = stack_top; i >= stack_bot; i--)
{
if (this->s_table[i].name == id_name)
return false;
}
return true;
}
/* void SymbolTable::set_new_top()
{
int stack_top=this->s_table.size()-1;
this->top++;
} */
void SymbolTable::pop_table()
{
int end = this->top_table[this->top];
int start = this->s_table.size() - 1;
for (int i = start; i >= end; i--)
{
this->s_table.pop_back();
}
this->top--;
this->top_table.pop_back();
}
table &SymbolTable::locate_table(string n)
{
int start = this->s_table.size() - 1;
for (int i = start; i >= 0; i--)
{
if (this->s_table[i].is_func || this->s_table[i].is_proc)
{
if(n==this->s_table[i].name)
return s_table[i];
for (int j = 0; j < this->s_table[i].arguments_num; j++)
{
if (this->s_table[i].arguments[j].name == n && this->s_table[i].arguments[j].pass_value)
{
this->s_table[i].is_arg=true;
return this->s_table[i];
}
}
}
else if (this->s_table[i].name == n)
return s_table[i];
}
string err = "err";
this->s_table[0].type = err;
return s_table[0];
}
void SymbolTable::pint_table()
{
int start = this->s_table.size() - 1;
for (int i = 0; i <= start; i++)
{
cout << "name: " << this->s_table[i].name
<< " type: " << this->s_table[i].type
<<" value: "<<this->s_table[i].value
<< endl;
}
}
table &SymbolTable::get_top_table()
{
return this->s_table[this->s_table.size() - 1];
}
table &SymbolTable::get_var_table(int l)
{
return this->s_table[l];
}
/* record_elments SymbolTable::get_records_elment(string name)
{
if(this->s_table[this->s_table.size()-1].records.size()==0)
{
cout<<"err"<<endl;
}
if(this->s_table[this->s_table.size()-1].records.find(name)!=this->s_table[this->s_table.size()-1].records.end())
{
return (this->s_table[this->s_table.size()-1].records[name];
}
} */