|
2 | 2 |
|
3 | 3 | #include "calculator.hpp"
|
4 | 4 |
|
5 |
| -#include <fmt/core.h> |
| 5 | +#include <string> |
6 | 6 |
|
7 | 7 | using namespace std;
|
8 | 8 |
|
9 |
| -shared_ptr<Expr> Expr::operator+(const shared_ptr<Expr>& other) const { |
10 |
| - return make_shared<Eval>(shared_from_this(), other, "+", |
11 |
| - [](int a, int b) { return a + b; }); |
| 9 | +shared_ptr<expression> expression::operator+( |
| 10 | + const shared_ptr<expression>& other) const { |
| 11 | + return make_shared<evaluation>(shared_from_this(), other, "+", |
| 12 | + [](int a, int b) { return a + b; }); |
12 | 13 | }
|
13 | 14 |
|
14 |
| -shared_ptr<Expr> Expr::operator-(const shared_ptr<Expr>& other) const { |
15 |
| - return make_shared<Eval>(shared_from_this(), other, "-", |
16 |
| - [](int a, int b) { return a - b; }); |
| 15 | +shared_ptr<expression> expression::operator-( |
| 16 | + const shared_ptr<expression>& other) const { |
| 17 | + return make_shared<evaluation>(shared_from_this(), other, "-", |
| 18 | + [](int a, int b) { return a - b; }); |
17 | 19 | }
|
18 | 20 |
|
19 |
| -shared_ptr<Expr> Expr::operator*(const shared_ptr<Expr>& other) const { |
20 |
| - return make_shared<Eval>(shared_from_this(), other, "*", |
21 |
| - [](int a, int b) { return a * b; }); |
| 21 | +shared_ptr<expression> expression::operator*( |
| 22 | + const shared_ptr<expression>& other) const { |
| 23 | + return make_shared<evaluation>(shared_from_this(), other, "*", |
| 24 | + [](int a, int b) { return a * b; }); |
22 | 25 | }
|
23 | 26 |
|
24 |
| -shared_ptr<Expr> Expr::operator/(const shared_ptr<Expr>& other) const { |
25 |
| - return make_shared<Eval>(shared_from_this(), other, "/", |
26 |
| - [](int a, int b) { return a / b; }); |
| 27 | +shared_ptr<expression> expression::operator/( |
| 28 | + const shared_ptr<expression>& other) const { |
| 29 | + return make_shared<evaluation>(shared_from_this(), other, "/", |
| 30 | + [](int a, int b) { return a / b; }); |
27 | 31 | }
|
28 | 32 |
|
29 |
| -Expr::~Expr() {} |
| 33 | +expression::~expression() {} |
30 | 34 |
|
31 |
| -Lit::Lit(int data) : data_(data) {} |
| 35 | +literal::literal(int data) : data_(data) {} |
32 | 36 |
|
33 |
| -shared_ptr<const Expr> Lit::simplify() const { |
| 37 | +shared_ptr<const expression> literal::simplify() const { |
34 | 38 | return shared_from_this();
|
35 | 39 | }
|
36 | 40 |
|
37 |
| -string Lit::to_string() const { |
38 |
| - return fmt::format("{}", data_); |
| 41 | +literal::operator string() const { |
| 42 | + return to_string(data_); |
39 | 43 | }
|
40 | 44 |
|
41 |
| -int Lit::data() const { |
| 45 | +int literal::data() const { |
42 | 46 | return data_;
|
43 | 47 | }
|
44 | 48 |
|
45 |
| -Lit::~Lit() {} |
| 49 | +literal::~literal() {} |
46 | 50 |
|
47 |
| -Var::Var(string data) : data_(data) {} |
| 51 | +variable::variable(string data) : data_(data) {} |
48 | 52 |
|
49 |
| -shared_ptr<const Expr> Var::simplify() const { |
| 53 | +shared_ptr<const expression> variable::simplify() const { |
50 | 54 | return shared_from_this();
|
51 | 55 | }
|
52 | 56 |
|
53 |
| -string Var::to_string() const { |
| 57 | +variable::operator string() const { |
54 | 58 | return data_;
|
55 | 59 | }
|
56 | 60 |
|
57 |
| -Var::~Var() {} |
| 61 | +variable::~variable() {} |
58 | 62 |
|
59 |
| -Eval::Eval(shared_ptr<const Expr> left, |
60 |
| - shared_ptr<const Expr> right, |
61 |
| - string token, |
62 |
| - function<int(int, int)> func) |
| 63 | +evaluation::evaluation(shared_ptr<const expression> left, |
| 64 | + shared_ptr<const expression> right, |
| 65 | + string token, |
| 66 | + function<int(int, int)> func) |
63 | 67 | : left_(left), right_(right), token_(token), func_(func) {}
|
64 | 68 |
|
65 |
| -shared_ptr<const Expr> Eval::simplify() const { |
66 |
| - auto lp = dynamic_pointer_cast<const Lit>(left_->simplify()); |
67 |
| - auto rp = dynamic_pointer_cast<const Lit>(right_->simplify()); |
| 69 | +shared_ptr<const expression> evaluation::simplify() const { |
| 70 | + auto lp = dynamic_pointer_cast<const literal>(left_->simplify()); |
| 71 | + auto rp = dynamic_pointer_cast<const literal>(right_->simplify()); |
68 | 72 |
|
69 | 73 | // Do nothing because no simplification is needed.
|
70 | 74 | if (lp == nullptr || rp == nullptr) {
|
71 | 75 | return shared_from_this();
|
72 | 76 | }
|
73 | 77 |
|
74 |
| - return make_shared<Lit>(func_(lp->data(), rp->data())); |
| 78 | + return make_shared<literal>(func_(lp->data(), rp->data())); |
75 | 79 | }
|
76 | 80 |
|
77 |
| -string Eval::to_string() const { |
78 |
| - return fmt::format("({} {} {})", left_->to_string(), token_, |
79 |
| - right_->to_string()); |
| 81 | +evaluation::operator string() const { |
| 82 | + return string(*left_) + " " + token_ + " " + string(*right_); |
80 | 83 | }
|
81 | 84 |
|
82 |
| -Eval::~Eval() {} |
| 85 | +evaluation::~evaluation() {} |
0 commit comments