-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
510 lines (367 loc) · 14 KB
/
ast.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "ast.h"
namespace Ast {
char ToString(BinaryOperator op) {
switch (op) {
case BinaryOperator::ADD: return '+';
case BinaryOperator::SUB: return '-';
case BinaryOperator::MUL: return '*';
default: return '/';
}
}
char Ast::ToString(UnaryOperator op) {
switch (op) {
case UnaryOperator::PLUS: return '+';
case UnaryOperator::MINUS: return '-';
}
}
Parentheses& Node::AsMutableParentheses() {
return *std::get<ParenthesesPtr>(*this);
}
const Parentheses& Node::AsParentheses() const {
return *std::get<ParenthesesPtr>(*this);
}
UnaryOp& Node::AsMutableUnaryOp() {
return *std::get<UnaryOpPtr>(*this);
}
const UnaryOp& Node::AsUnaryOp() const {
return *std::get<UnaryOpPtr>(*this);
}
BinaryOp& Node::AsMutableBinaryOp() {
return *std::get<BinaryOpPtr>(*this);
}
const BinaryOp& Node::AsBinaryOp() const {
return *std::get<BinaryOpPtr>(*this);
}
Ast::Node Node::OfLiteral(std::string literal) {
return Node(Ast::Literal{std::move(literal)});
}
Ast::Node Node::OfCellParamPtr(CellParamPtr id) {
if(!(*id)->IsValid()) throw FormulaException("invalid position");
return Node(id);
}
Ast::Node Node::OfParentheses(Node token) {
return token.IsCell() || token.IsLiteral() || token.IsParentheses() ?
std::move(token) : Node(std::make_unique<Ast::Parentheses>(std::move(token)));
}
Ast::Node Node::Unary(Ast::Node token, UnaryOperator op) {
return Node(std::make_unique<UnaryOp>(SimplifyUnaryParentheses(std::move(token)), op));
}
Ast::Node Node::UnaryPlus(Node token) {
return Node(std::make_unique<UnaryOp>(SimplifyUnaryParentheses(std::move(token)), UnaryOperator::PLUS));
}
Ast::Node Node::UnaryMinus(Node token) {
return Node(std::make_unique<UnaryOp>(SimplifyUnaryParentheses(std::move(token)), UnaryOperator::MINUS));
}
Ast::Node Node::Binary(Ast::Node lhs, Ast::Node rhs, BinaryOperator op) {
return Node(
std::make_unique<BinaryOp>(
SimplifyBinaryParentheses(op, std::move(lhs), true),
SimplifyBinaryParentheses(op, std::move(rhs), false),
op
)
);
}
Ast::Node Node::BinaryAdd(Node lhs, Node rhs) {
return Node(
std::make_unique<BinaryOp>(
SimplifyBinaryParentheses(BinaryOperator::ADD, std::move(lhs), true),
SimplifyBinaryParentheses(BinaryOperator::ADD, std::move(rhs), false),
BinaryOperator::ADD
)
);
}
Ast::Node Node::BinarySub(Node lhs, Node rhs) {
return Node(
std::make_unique<BinaryOp>(
SimplifyBinaryParentheses(BinaryOperator::SUB, std::move(lhs), true),
SimplifyBinaryParentheses(BinaryOperator::SUB, std::move(rhs), false),
BinaryOperator::SUB
)
);
}
Ast::Node Node::BinaryMul(Node lhs, Node rhs) {
return Node(
std::make_unique<BinaryOp>(
SimplifyBinaryParentheses(BinaryOperator::MUL, std::move(lhs), true),
SimplifyBinaryParentheses(BinaryOperator::MUL, std::move(rhs), false),
BinaryOperator::MUL
)
);
}
Ast::Node Node::BinaryDiv(Node lhs, Node rhs) {
return Node(
std::make_unique<BinaryOp>(
SimplifyBinaryParentheses(BinaryOperator::DIV, std::move(lhs), true),
SimplifyBinaryParentheses(BinaryOperator::DIV, std::move(rhs), false),
BinaryOperator::DIV
)
);
}
IFormula::Value Node::Evaluate(const ISheet& sheet) const {
if (IsLiteral()) {
return AsLiteral().AsDouble();
} else if (IsCell()) {
const CellParam& param = AsCell();
if (param == std::nullopt) return FormulaError(FormulaError::Category::Ref);
const ICell* cell = sheet.GetCell(*param);
if (cell == nullptr) return 0.;
ICell::Value cell_value = cell->GetValue();
if (std::holds_alternative<std::string>(cell_value)) {
const auto& cell_str_value = std::get<std::string>(cell_value);
if (cell_str_value.empty()) return 0.;
try {
// TODO replace with separate method?
size_t idx;
double value = std::stod(cell_str_value, &idx);
if (idx < cell_str_value.size()) {
return FormulaError(FormulaError::Category::Value);
}
return value;
} catch (std::exception& err) {
return FormulaError(FormulaError::Category::Value);
}
} else if (std::holds_alternative<double>(cell_value)) {
return std::get<double>(cell_value);
} else {
return std::get<FormulaError>(cell_value);
}
} else if (IsParentheses()) {
return AsParentheses().GetContent().Evaluate(sheet);
} else if (IsUnaryOp()) {
const auto& unary_op = AsUnaryOp();
IFormula::Value token = unary_op.GetToken().Evaluate(sheet);
if (std::holds_alternative<double>(token)) {
double value = std::get<double>(token);
return unary_op.GetOp() == UnaryOperator::PLUS ? value : -value;
}
return token;
} else if (IsBinaryOp()) {
const auto& binary_op = AsBinaryOp();
IFormula::Value lhs = binary_op.GetLhs().Evaluate(sheet);
if (std::holds_alternative<FormulaError>(lhs)) {
return lhs;
}
IFormula::Value rhs = binary_op.GetRhs().Evaluate(sheet);
if (std::holds_alternative<FormulaError>(rhs)) {
return rhs;
}
double lhs_value = std::get<double>(lhs);
double rhs_value = std::get<double>(rhs);
double result;
switch (binary_op.GetOp()) {
case BinaryOperator::ADD: result = lhs_value + rhs_value; break;
case BinaryOperator::SUB: result = lhs_value - rhs_value; break;
case BinaryOperator::MUL: result = lhs_value * rhs_value; break;
case BinaryOperator::DIV: result = lhs_value / rhs_value; break;
}
if (std::isfinite(result)) {
return result;
} else {
return FormulaError(FormulaError::Category::Div0);
}
}
return 0.;
}
std::string Node::BuildExpression() const {
if (IsLiteral()) {
return AsLiteral().value;
} else if (IsCell()) {
const CellParam& param = AsCell();
return param != std::nullopt ? param->ToString() : "#REF!";
} else if (IsParentheses()) {
return '(' + AsParentheses().GetContent().BuildExpression() + ')';
} else if (IsUnaryOp()) {
const auto& unary_op = AsUnaryOp();
return ToString(unary_op.GetOp()) + unary_op.GetToken().BuildExpression();
} else if (IsBinaryOp()) {
const auto& binary_op = AsBinaryOp();
return binary_op.GetLhs().BuildExpression() + ToString(binary_op.GetOp()) +
binary_op.GetRhs().BuildExpression();
}
}
Ast::Node Node::SimplifyUnaryParentheses(Ast::Node child) {
if (!child.IsParentheses()) return child;
auto& parentheses = child.AsMutableParentheses();
if (parentheses.GetContent().IsBinaryOp()) {
BinaryOperator child_op = parentheses.GetContent().AsBinaryOp().GetOp();
ParenthesesRestrictions restrictions = UNARY_RESTRICTIONS[child_op];
if (restrictions == ALL) return child;
}
return parentheses.Extract();
}
Ast::Node Node::SimplifyBinaryParentheses(BinaryOperator parent_op, Ast::Node child, bool left) {
if (!child.IsParentheses()) return child;
auto& parentheses = child.AsMutableParentheses();
if (parentheses.GetContent().IsBinaryOp()) {
BinaryOperator child_op = parentheses.GetContent().AsBinaryOp().GetOp();
ParenthesesRestrictions restrictions = BINARY_RESTRICTIONS[parent_op][child_op];
if (restrictions == ALL || (restrictions == LEFT && left) || (restrictions == RIGHT && !left)) return child;
}
return parentheses.Extract();
}
TreeBuilder& TreeBuilder::AddLiteral(std::string literal) {
node_stack_.push(Ast::Node::OfLiteral(std::move(literal)));
return *this;
}
TreeBuilder& TreeBuilder::AddCell(const std::string& cell_name) {
const CellParamPtr& cell_param = cell_cache_.GetOrInsert(Position::FromString(cell_name));
node_stack_.push(Ast::Node::OfCellParamPtr(cell_param));
return *this;
}
TreeBuilder& TreeBuilder::AddParentheses() {
Ast::Node content = std::move(node_stack_.top());
node_stack_.pop();
node_stack_.push(Ast::Node::OfParentheses(std::move(content)));
return *this;
}
TreeBuilder& TreeBuilder::AddUnaryOp(UnaryOperator op) {
Ast::Node content = std::move(node_stack_.top());
node_stack_.pop();
node_stack_.push(Ast::Node::Unary(std::move(content), op));
return *this;
}
TreeBuilder& TreeBuilder::AddBinaryOp(BinaryOperator op) {
Ast::Node rhs = std::move(node_stack_.top());
node_stack_.pop();
Ast::Node lhs = std::move(node_stack_.top());
node_stack_.pop();
node_stack_.push(Ast::Node::Binary(std::move(lhs), std::move(rhs), op));
return *this;
}
Ast::Tree TreeBuilder::Build() {
Ast::Node root = std::move(node_stack_.top());
node_stack_.pop();
return Ast::Tree(std::move(root), std::move(cell_cache_));
}
const CellParamPtr& CellParamCache::GetOrInsert(Position position) {
std::map<int, CellParamPtr>& row = cell_params_[position.row];
if (auto col_it = row.find(position.col); col_it != row.end()) {
return col_it->second;
} else {
auto inserted = row.emplace(position.col, std::make_shared<std::optional<Position>>(position));
return inserted.first->second;
}
}
size_t CellParamCache::HandleInsertedRows(int before, int count) {
if (cell_params_.empty()) return 0;
std::vector<int> keys_to_update;
for (auto it = cell_params_.rbegin(); it != cell_params_.rend() && it->first >= before; it++) {
keys_to_update.push_back(it->first);
}
size_t updated_cell_params_count = 0;
for (int key: keys_to_update) {
int updated_row = key + count;
for (auto&[_, ptr]: cell_params_.at(key)) {
CellParam& cell_param = *ptr;
if (cell_param != std::nullopt) {
cell_param->row = updated_row;
updated_cell_params_count++;
}
}
auto entry = cell_params_.extract(key);
entry.key() = updated_row;
cell_params_.insert(std::move(entry));
}
return updated_cell_params_count;
}
size_t CellParamCache::HandleInsertedCols(int before, int count) {
size_t updated_cell_params_count = 0;
std::vector<int> keys_to_update;
for (auto&[_, row]: cell_params_) {
for (auto it = row.rbegin(); it != row.rend() && it->first >= before; it++) {
keys_to_update.push_back(it->first);
}
for (int key: keys_to_update) {
CellParam& param = *row.at(key);
if (param != std::nullopt) {
param->col = key + count;
updated_cell_params_count++;
}
auto entry = row.extract(key);
entry.key() = key + count;
row.insert(std::move(entry));
}
keys_to_update.clear();
}
return updated_cell_params_count;
}
std::pair<size_t, size_t> CellParamCache::HandleDeletedRows(int start, int count) {
size_t deleted_cell_params_count = 0;
size_t updated_cell_params_count = 0;
std::vector<int> keys_to_delete;
std::vector<int> keys_to_update;
auto it = cell_params_.rbegin();
for (; it != cell_params_.rend() && it->first >= start + count; it++) {
keys_to_update.push_back(it->first);
}
for (; it != cell_params_.rend() && it->first >= start; it++) {
keys_to_delete.push_back(it->first);
}
for (int key: keys_to_delete) {
for (auto& [_, ptr]: cell_params_.at(key)) {
ptr->reset();
deleted_cell_params_count++;
}
cell_params_.erase(key);
}
for (int key: keys_to_update) {
int updated_row = key - count;
for (auto&[_, ptr]: cell_params_.at(key)) {
CellParam& cell_param = *ptr;
if (cell_param != std::nullopt) {
cell_param->row = updated_row;
updated_cell_params_count++;
}
}
auto entry = cell_params_.extract(key);
entry.key() = updated_row;
cell_params_.insert(std::move(entry));
}
return std::make_pair(deleted_cell_params_count, updated_cell_params_count);
}
std::pair<size_t, size_t> CellParamCache::HandleDeletedCols(int start, int count) {
size_t deleted_cell_params_count = 0;
size_t updated_cell_params_count = 0;
std::vector<int> keys_to_delete;
std::vector<int> keys_to_update;
for (auto&[_, row]: cell_params_) {
auto it = row.rbegin();
for (; it != row.rend() && it->first >= start + count; it++) {
keys_to_update.push_back(it->first);
}
for (; it != row.rend() && it->first >= start; it++) {
keys_to_delete.push_back(it->first);
}
for (int key: keys_to_delete) {
CellParam& param = *row.at(key);
if (param != std::nullopt) {
param.reset();
deleted_cell_params_count++;
}
row.erase(key);
}
for (int key: keys_to_update) {
CellParam& param = *row.at(key);
if (param != std::nullopt) {
param->col = key - count;
updated_cell_params_count++;
}
auto entry = row.extract(key);
entry.key() = key - count;
row.insert(std::move(entry));
}
keys_to_delete.clear();
keys_to_update.clear();
}
return std::make_pair(deleted_cell_params_count, updated_cell_params_count);
}
std::vector<Position> CellParamCache::GetReferencedCells() const {
std::vector<Position> result;
for (const auto&[row_index, row]: cell_params_) {
for (const auto&[col_index, _]: row) {
result.push_back(Position {row_index, col_index});
}
}
return result;
}
}