-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformula.cpp
More file actions
109 lines (90 loc) · 3.01 KB
/
formula.cpp
File metadata and controls
109 lines (90 loc) · 3.01 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
#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 << fe.ToString();
}
FormulaError::FormulaError(Category category) : category_(category) {}
FormulaError::Category FormulaError::GetCategory() const {
return category_;
}
bool FormulaError::operator==(FormulaError rhs) const {
return category_ == rhs.category_;
}
std::string_view FormulaError::ToString() const {
switch (category_) {
case Category::Ref:
return "#REF!"sv;
case Category::Value:
return "#VALUE!"sv;
case Category::Div0:
return "#DIV/0!"sv;
}
return ""sv;
}
namespace {
double NumericOrErrorFromValue(const std::string &str) {
double value = 0;
if (!str.empty()) {
std::istringstream input(str);
if (!(input >> value) || !input.eof()) {
throw FormulaError(FormulaError::Category::Value);
}
}
return value;
}
double NumericOrErrorFromValue(FormulaError error) {
throw FormulaError(error);
}
double NumericOrErrorFromValue(double value) {
return value;
}
double GetValueFromVisit(const CellInterface *cell) {
if (cell) {
return std::visit([](const auto &value) { return NumericOrErrorFromValue(value); },
cell->GetValue());
}
return 0;
}
class Formula : public FormulaInterface {
public:
explicit Formula(std::string expression) : ast_(ParseFormulaAST(expression)) {}
Value Evaluate(const SheetInterface &sheet) const override { //Value = std::variant<double, FormulaError>;
try {
auto cell_by_position = [&sheet](const Position position) -> double {
if (!position.IsValid()) {
throw FormulaError(FormulaError::Category::Ref);
}
const auto *cell = sheet.GetCell(position);
return GetValueFromVisit(cell);
};
return ast_.Execute(cell_by_position);
} catch (FormulaError &error) {
return error;
}
}
std::string GetExpression() const override {
std::ostringstream out;
ast_.PrintFormula(out);
return out.str();
}
std::vector<Position> GetReferencedCells() const override {
std::set<Position> ref_cells;
for (const auto& cell : ast_.GetCells()) {
if (cell.IsValid()) {
ref_cells.emplace(cell);
}
}
return {ref_cells.begin(), ref_cells.end()};
}
private:
FormulaAST ast_;
};
} // namespace
std::unique_ptr<FormulaInterface> ParseFormula(std::string expression) {
return std::make_unique<Formula>(std::move(expression));
}