-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolTable.cpp
67 lines (64 loc) · 1.8 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
#include "SymbolTable.h"
SymbolTable::SymbolTable(){
table.push_back({{}});
}
void SymbolTable::in_scope(){
table[func_depth].push_back({});
scope_depth++;
}
void SymbolTable::out_scope(){
table[func_depth].erase(table[func_depth].begin() + scope_depth);
scope_depth--;
}
void SymbolTable::in_func(){
table.push_back({{}});
func_depth++;
}
void SymbolTable::out_func(){
table.erase(table.begin() + func_depth);
func_depth--;
}
bool SymbolTable::has_var(string& var_name) const{
for(int i = scope_depth; i >= 0; i--){
if(table[func_depth][i].find(var_name) != table[func_depth][i].end()){
return true;
}
}
if(func_depth > 0){
return table[0][0].find(var_name) != table[0][0].end();
}
return false;
}
Program::Object SymbolTable::get_var(string& var_name) const{
for(int i = scope_depth; i >= 0; i--){
if(table[func_depth][i].find(var_name) != table[func_depth][i].end()){
return table[func_depth][i].at(var_name);
}
}
if(func_depth > 0){
// グローバル変数
return table[0][0].at(var_name);
}
return Program::Object();
}
void SymbolTable::set_new_var(string& var_name, const Program::Object& value){
table[func_depth][scope_depth][var_name] = value;
}
void SymbolTable::set_var(string& var_name, const Program::Object& value){
for(int i = scope_depth; i >= 0; i--){
if(table[func_depth][i].find(var_name) != table[func_depth][i].end()){
table[func_depth][i][var_name] = value;
return;
}
}
if(func_depth > 0){
// グローバル変数
table[0][0][var_name] = value;
}
}
int SymbolTable::get_func_depth() const{
return func_depth;
}
int SymbolTable::get_scope_depth() const{
return scope_depth;
}