forked from jd-opensource/arkdecompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexicalenv.cpp
More file actions
215 lines (170 loc) · 5.18 KB
/
Copy pathlexicalenv.cpp
File metadata and controls
215 lines (170 loc) · 5.18 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "lexicalenv.h"
#include <algorithm>
#include <utility>
LexicalEnv::LexicalEnv(size_t capacity)
: expressions_(capacity, nullptr), capacity_(capacity) {
}
bool LexicalEnv::IsFull() {
for (size_t i = 0; i < capacity_; ++i) {
if(expressions_[i] == nullptr){
return false;
}
}
return true;
}
LexicalEnv::LexicalEnv(const LexicalEnv& other)
: expressions_(other.capacity_), capacity_(other.capacity_) {
for (size_t i = 0; i < capacity_; ++i) {
std::cout << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" << std::endl;
if(other.expressions_[i] == nullptr){
std::cout << "null" << std::endl;
}else{
std::cout << "not null" << std::endl;
expressions_[i] = new std::string(*(other.expressions_[i]));
}
std::cout << "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" << std::endl;
}
}
LexicalEnv& LexicalEnv::operator=(const LexicalEnv& other) {
std::cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
if (this != &other) {
expressions_.clear();
capacity_ = other.capacity_;
expressions_.resize(capacity_);
for (size_t i = 0; i < capacity_; ++i) {
expressions_[i] = new std::string(*other.expressions_[i]);
}
}
return *this;
}
LexicalEnv::LexicalEnv(LexicalEnv&& other) noexcept
: expressions_(std::move(other.expressions_)),
capacity_(other.capacity_) {
other.capacity_ = 0;
}
LexicalEnv& LexicalEnv::operator=(LexicalEnv&& other) noexcept {
if (this != &other) {
expressions_ = std::move(other.expressions_);
capacity_ = other.capacity_;
other.capacity_ = 0;
}
return *this;
}
std::string*& LexicalEnv::operator[](size_t index) {
checkIndex(index);
return expressions_[index];
}
const std::string* LexicalEnv::operator[](size_t index) const {
checkIndex(index);
return expressions_[index];
}
std::string* LexicalEnv::get(size_t index) const {
checkIndex(index);
return expressions_[index];
}
void LexicalEnv::set(size_t index, std::string* expr) {
checkIndex(index);
expressions_[index] = expr;
}
size_t LexicalEnv::capacity() const {
return capacity_;
}
size_t LexicalEnv::size() const {
return capacity_;
}
bool LexicalEnv::isValidIndex(size_t index) const {
return index < capacity_;
}
void LexicalEnv::checkIndex(size_t index) const {
if (index >= capacity_) {
throw std::out_of_range("LexicalEnv index out of range");
}
}
LexicalEnvStack::LexicalEnvStack() {
}
LexicalEnvStack::LexicalEnvStack(const LexicalEnvStack& other)
: stack_(other.stack_) {
}
LexicalEnvStack& LexicalEnvStack::operator=(const LexicalEnvStack& other) {
if (this != &other) {
stack_ = other.stack_;
}
return *this;
}
LexicalEnvStack::LexicalEnvStack(LexicalEnvStack&& other) noexcept
: stack_(std::move(other.stack_)) {
}
LexicalEnvStack& LexicalEnvStack::operator=(LexicalEnvStack&& other) noexcept {
if (this != &other) {
stack_ = std::move(other.stack_);
}
return *this;
}
LexicalEnvStack::~LexicalEnvStack() {
}
LexicalEnv* LexicalEnvStack::push(size_t capacity) {
stack_.emplace_back(capacity);
return &stack_.back();
}
void LexicalEnvStack::pop() {
if (stack_.empty()) {
throw std::runtime_error("Cannot pop from empty stack");
}
stack_.pop_back();
}
size_t LexicalEnvStack::size() const {
return stack_.size();
}
bool LexicalEnvStack::empty() const {
return stack_.empty();
}
std::string* LexicalEnvStack::get(size_t A, size_t B) const {
checkIndex(A, B);
size_t actualIndex = stack_.size() - 1 - A;
return stack_[actualIndex].get(B);
}
void LexicalEnvStack::set(size_t A, size_t B, std::string* expr) {
checkIndex(A, B);
size_t actualIndex = stack_.size() - 1 - A;
stack_[actualIndex].set(B, expr);
}
const LexicalEnv& LexicalEnvStack::getLexicalEnv(size_t A) const {
checkStackIndex(A);
size_t actualIndex = stack_.size() - 1 - A;
return stack_[actualIndex];
}
LexicalEnv& LexicalEnvStack::getLexicalEnv(size_t A) {
checkStackIndex(A);
size_t actualIndex = stack_.size() - 1 - A;
return stack_[actualIndex];
}
const LexicalEnv& LexicalEnvStack::top() const {
if (stack_.empty()) {
throw std::runtime_error("Stack is empty");
}
return stack_.back();
}
LexicalEnv& LexicalEnvStack::top() {
if (stack_.empty()) {
throw std::runtime_error("Stack is empty");
}
return stack_.back();
}
void LexicalEnvStack::clear() {
stack_.clear();
}
void LexicalEnvStack::checkIndex(size_t A, size_t B) const {
checkStackIndex(A);
size_t actualIndex = stack_.size() - 1 - A;
if (!stack_[actualIndex].isValidIndex(B)) {
throw std::out_of_range("LexicalEnv index B out of range");
}
}
void LexicalEnvStack::checkStackIndex(size_t A) const {
if (stack_.empty()) {
throw std::runtime_error("Stack is empty");
}
if (A >= stack_.size()) {
throw std::out_of_range("Stack index A out of range");
}
}