-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformula.cpp
More file actions
89 lines (77 loc) · 1.75 KB
/
formula.cpp
File metadata and controls
89 lines (77 loc) · 1.75 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
#include "common.h"
#include "formula.h"
#include "FormulaAST.h"
using namespace std::literals;
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;
default:
assert(false);
return ""sv;
}
}
std::ostream& operator<<(std::ostream& output, FormulaError fe) {
return output << fe.ToString();
}
namespace {
class Formula : public FormulaInterface {
public:
explicit Formula(std::string expression)
: ast_([&expression]() {
try {
return ParseFormulaAST(expression);
}
catch (std::exception& e) {
throw FormulaException(e.what());
}
}())
{
Position prev = Position::NONE;
for (auto& cell : ast_.GetCells()) {
if (cell.IsValid() && !(cell == prev)) {
referenced_cells_.push_back(std::move(cell));
prev = cell;
}
}
}
Value Evaluate(const SheetInterface& sheet) const override
{
try {
return ast_.Execute(sheet);
}
catch (FormulaError& e) {
return e;
}
}
std::string GetExpression() const override
{
std::stringstream oss;
ast_.PrintFormula(oss);
return oss.str();
}
std::vector<Position> GetReferencedCells() const override {
return referenced_cells_;
}
private:
FormulaAST ast_;
std::vector<Position> referenced_cells_;
};
} // namespace
std::unique_ptr<FormulaInterface> ParseFormula(std::string expression) {
return std::make_unique<Formula>(std::move(expression));
}