-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula.cpp
91 lines (69 loc) · 2.54 KB
/
formula.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
#include "formula.h"
#include "FormulaAST.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <sstream>
using namespace std::literals;
std::ostream& operator<<(std::ostream& output, FormulaError fe) {
return output << "#DIV/0!"; // only need to pass trainer's tests
}
namespace {
class Formula : public FormulaInterface {
public:
explicit Formula(std::string expression)
: ast_(ParseFormulaAST(expression)) {
}
Value Evaluate(const SheetInterface& sheet) const override {
// this object will be used in AST calculations
CellLookup cell_lookup = [&sheet](Position pos) {
double result = 0.0;
if (!pos.IsValid()) {
throw FormulaError(FormulaError::Category::Ref); // if REF pos is invalid
}
if (sheet.GetCell(pos) == nullptr) {
return 0.0; // empty cells return zero
}
CellInterface::Value val = sheet.GetCell(pos)->GetValue();
if (std::holds_alternative<double>(val)) {
result = std::get<double>(val);
}
if (std::holds_alternative<FormulaError>(val)) {
throw std::get<FormulaError>(val);
}
if (std::holds_alternative<std::string>(val)) {
try {
result = std::stod(std::get<std::string>(val)); // cell like "1234" use as number
} catch (std::invalid_argument& err) {
throw FormulaError(FormulaError::Category::Value);
}
}
return result;
};
Value val;
try {
val = ast_.Execute(cell_lookup);
} catch (FormulaError& err) {
val = err;
}
return val;
};
std::string GetExpression() const override {
std::stringstream ss;
ast_.PrintFormula(ss);
return ss.str();
};
std::vector<Position> GetReferencedCells() const {
std::vector<Position> referenced_cells;
for (const auto& cell : ast_.GetCells()) {
referenced_cells.push_back(cell);
}
return referenced_cells;
}
private:
FormulaAST ast_;
};
} // namespace
std::unique_ptr<FormulaInterface> ParseFormula(std::string expression) {
return std::make_unique<Formula>(std::move(expression));
}