-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.h
160 lines (133 loc) · 3.15 KB
/
scope.h
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
#include <string>
#include <vector>
#include <memory>
#include <iostream>
#include <cstdlib>
using namespace std;
enum Type{
TYPE_INT,
TYPE_PROC,
TYPE_CONST,
};
struct Entry{
string name;
Type type;
int offset;
int var_val;
int is_reference;
int is_array;
int arr_size;
int const_val;
struct Table* procedure_table;
};
struct Table{
string name;
Table *parent;
int procedure_addr;
int mem_size;
vector<Entry> entries;
int num_var; // so luong parameter trong mot procedure
};
struct FindResult{
Entry *entry;
int depth;
};
void init_table(string name);
void make_table(string name);
void table_add_var(string name, bool is_reference,bool is_array,int arr_size);
void table_add_const(string name,int const_val);
void table_add_proc(string name,Table* table);
FindResult find_entry(string name);
Entry *find_entry_procedure(string name,Table *table);
static Table* root_table;
static Table* current_table;
void init_table(string name){
root_table = new Table();
root_table->name = name;
root_table->parent == NULL;
root_table->mem_size = 4;
current_table = root_table;
}
void make_table(string name){
Table* table = new Table();
table->parent = current_table;
table->name = name;
table->mem_size = 4; // 4 thanh ghi dac biet
table_add_proc(name,table);
// cout<<"cha la "<<table->parent->name<<endl;
current_table = table;
}
void table_add_var(string name, bool is_reference,bool is_array,int arr_size){
Entry e;
e.name = name;
e.type = TYPE_INT;
e.is_reference = is_reference;
e.is_array = is_array;
e.arr_size = arr_size;
e.offset = current_table->mem_size;
if (is_array)
current_table->mem_size += arr_size * 1;
else
current_table->mem_size += 1;
current_table->entries.push_back(e);
}
void table_add_const(string name,int const_val){
Entry e;
e.name = name;
e.type = TYPE_CONST;
e.const_val = const_val;
e.is_array = false;
e.is_reference = false;
current_table->entries.push_back(e);
}
void table_add_proc(string name,Table* table){
Entry e;
e.name = name;
e.type = TYPE_PROC;
e.procedure_table = table;
e.is_array = false;
e.is_reference = false;
// cout<<"table_add_proc "<<name<<endl;
// cout<<"current table "<<current_table->name<<endl;
current_table->entries.push_back(e);
}
FindResult find_entry(string name){
Table *table = current_table;
int depth = 0;
FindResult result;
while(table != NULL){
for(int i = 0;i<table->entries.size();i++){
// cout<<table->entries[i].name<<endl;
if(name == table->entries[i].name){
result.entry = &table->entries[i];
result.depth = depth;
return result;
}
}
table = table->parent;
depth +=1;
}
result.entry = NULL;
return result;
}
Entry *find_entry_procedure(string name,Table *table ){
for(int i = 0;i<table->entries.size();i++){
if(name == table->entries[i].name){
// cout<<"Dang tim bang"<<table->name<<endl;
return &table->entries[i];
}
}
return NULL;
}
/*
int main(){
init_table();
cout<<current_table->name;
table_add_var("a",false,false,0);
table_add_var("b",false,false,0);
table_add_var("c",false,false,0);
table_add_var("d",false,false,0);
Entry *e = find("b");
cout<<e->name;
}
*/