-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesis.cpp
More file actions
55 lines (47 loc) · 1.61 KB
/
Copy pathParenthesis.cpp
File metadata and controls
55 lines (47 loc) · 1.61 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
#include "Parenthesis.hpp"
#include "Operation.hpp"
#include "tokenize/LexicalAnalyzer.hpp"
#include "BinaryOperationExpression.hpp"
#include "InvalidFormulaException.hpp"
Parenthesis::Parenthesis(char c) : c(c), closed(false) {
}
std::shared_ptr<Expression> Parenthesis::add(std::shared_ptr<ASTNode> toAdd) {
if (closed) {
auto toAddOp = std::dynamic_pointer_cast<Operation>(toAdd);
if (!toAddOp) {
// TODO: this must never happen. Error is actually LexicalAnalyzer.
throw InvalidFormulaException("Missing opening (");
}
return ASTBuilder::getSelf()->createBinaryOperationExpression(shared_from_this(), toAddOp, nullptr);
}
if (!nestedExp) {
nestedExp = std::static_pointer_cast<Expression>(toAdd);
} else {
if (std::dynamic_pointer_cast<Parenthesis>(toAdd) && !nestedExp->openForInput()) {
closed = true;
} else {
nestedExp = nestedExp->add(toAdd);
}
}
return shared_from_this();
}
long double Parenthesis::resolve(std::map<std::string, long double> vars) const {
return nestedExp->resolve(vars);
}
std::shared_ptr<Expression> Parenthesis::simplify() {
return nestedExp->simplify();
}
bool Parenthesis::openForInput() const {
return !closed;
}
void Parenthesis::validate() const {
if (c == ')') {
throw InvalidFormulaException("Missing opening (");
} else if (!closed) {
throw InvalidFormulaException("Missing closing )");
}
nestedExp->validate();
}
std::string Parenthesis::toString() const {
return nestedExp ? "(" + nestedExp->toString() + ")" : "(?";
}