-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryOperationExpression.cpp
More file actions
58 lines (48 loc) · 1.96 KB
/
Copy pathBinaryOperationExpression.cpp
File metadata and controls
58 lines (48 loc) · 1.96 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
#include "BinaryOperationExpression.hpp"
#include "tokenize/LexicalAnalyzer.hpp"
#include "InvalidFormulaException.hpp"
BinaryOperationExpression::BinaryOperationExpression(std::shared_ptr<Expression> op1, std::shared_ptr<Operation> op)
: op1(op1), op(op) {
}
BinaryOperationExpression::BinaryOperationExpression(std::shared_ptr<Expression> op1, std::shared_ptr<Operation> op,
std::shared_ptr<Expression> op2)
: op1(op1), op(op), op2(op2) {
}
std::shared_ptr<Expression> BinaryOperationExpression::add(std::shared_ptr<ASTNode> toAdd) {
if (op2 && op2->openForInput()) {
op2->add(toAdd);
return shared_from_this();
}
if (auto toAddOp = std::dynamic_pointer_cast<Operation>(toAdd)) {
if (op->getPrecedence() < toAddOp->getPrecedence()) {
auto binOp2 = ASTBuilder::getSelf()->createBinaryOperationExpression(op2, toAddOp, nullptr);
return ASTBuilder::getSelf()->createBinaryOperationExpression(op1, op, binOp2);
} else {
return ASTBuilder::getSelf()->createBinaryOperationExpression(shared_from_this(), toAddOp, nullptr);
}
} else {
op2 = std::static_pointer_cast<Expression>(toAdd);
return shared_from_this();
}
}
long double BinaryOperationExpression::resolve(std::map<std::string, long double> vars) const {
return op->resolve(vars, op1, op2);
}
std::shared_ptr<Expression> BinaryOperationExpression::simplify() {
op1 = op1->simplify();
op2 = op2->simplify();
return shared_from_this();
}
void BinaryOperationExpression::validate() const {
if (!op2) {
throw InvalidFormulaException("op2 must not be null");
}
op1->validate();
op2->validate();
}
bool BinaryOperationExpression::openForInput() const {
return !op2 || op2->openForInput();
}
std::string BinaryOperationExpression::toString() const {
return "(" + op1->toString() + op->toString() + op2->toString() + ")";
}