-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfixOperation.cpp
More file actions
84 lines (70 loc) · 2.77 KB
/
Copy pathPostfixOperation.cpp
File metadata and controls
84 lines (70 loc) · 2.77 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
#include "PostfixOperation.hpp"
#include "tokenize/LexicalAnalyzer.hpp"
#include "math.h"
#include "BinaryOperationExpression.hpp"
#include "Operation.hpp"
#include "InvalidFormulaException.hpp"
#include <memory>
PostfixOperationEnum PostfixOperationEnum::SIN("sin", sin);
PostfixOperationEnum PostfixOperationEnum::COS("cos", cos);
PostfixOperationEnum PostfixOperationEnum::TAN("tan", tan);
PostfixOperationEnum PostfixOperationEnum::ASIN("asin", asin);
PostfixOperationEnum PostfixOperationEnum::ACOS("acos", acos);
PostfixOperationEnum PostfixOperationEnum::ATAN("atan", atan);
PostfixOperationEnum PostfixOperationEnum::SQRT("sqrt", sqrt);
PostfixOperationEnum PostfixOperationEnum::LOG("log", log);
PostfixOperationEnum PostfixOperationEnum::LOGTEN("logten", log10);
PostfixOperationEnum PostfixOperationEnum::ALL[] = {SIN, COS, TAN, ASIN, ACOS, ATAN, SQRT, LOG, LOGTEN};
PostfixOperationEnum &PostfixOperationEnum::byName(const std::string &name) {
for (auto &obj: PostfixOperationEnum::ALL) {
if (obj.name == name) {
return obj;
}
}
throw InvalidFormulaException("Unknown postfix operator " + name);
}
bool PostfixOperationEnum::match(const std::string &name) {
for (auto &obj: PostfixOperationEnum::ALL) {
if (obj.name == name) {
return true;
}
}
return false;
}
const std::string &PostfixOperationEnum::getName() const {
return name;
}
FuncPtrToTrigonometry PostfixOperationEnum::getFuncPtrOne() const {
return functionPointer;
}
PostfixOperation::PostfixOperation(const std::string &operatorName) : postfixOperationImpl(
PostfixOperationEnum::byName(operatorName)) {
}
std::shared_ptr<Expression> PostfixOperation::add(std::shared_ptr<ASTNode> toAdd) {
std::shared_ptr<Expression> retExp = shared_from_this();
if (!nestedExp) {
nestedExp = std::static_pointer_cast<Expression>(toAdd);
} else if (nestedExp->openForInput()) {
nestedExp = nestedExp->add(toAdd);
} else {
retExp = ASTBuilder::getSelf()->createBinaryOperationExpression(shared_from_this(),
std::static_pointer_cast<Operation>(toAdd), nullptr);
}
return retExp;
}
long double PostfixOperation::resolve(std::map<std::string, long double> vars) const {
return postfixOperationImpl.getFuncPtrOne()(nestedExp->resolve(vars));
}
std::shared_ptr<Expression> PostfixOperation::simplify() {
nestedExp = nestedExp->simplify();
return shared_from_this();
}
bool PostfixOperation::openForInput() const {
return !nestedExp || nestedExp->openForInput();
}
void PostfixOperation::validate() const {
nestedExp->validate();
}
std::string PostfixOperation::toString() const {
return postfixOperationImpl.getName() + nestedExp->toString();
}