-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstant.cpp
More file actions
65 lines (49 loc) · 1.67 KB
/
Copy pathConstant.cpp
File metadata and controls
65 lines (49 loc) · 1.67 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
#include "Constant.hpp"
#include "tokenize/LexicalAnalyzer.hpp"
#include "BinaryOperationExpression.hpp"
#include "InvalidFormulaException.hpp"
ConstantEnum ConstantEnum::PI("pi", 3.14159265358979323846);
ConstantEnum ConstantEnum::E("e", 2.71828182845904523536);
ConstantEnum ConstantEnum::ALL[] = {PI, E};
ConstantEnum::ConstantEnum(const std::string &name, long double val) : name(name), val(val) {
}
ConstantEnum &ConstantEnum::byName(const std::string &name) {
for (auto &obj: ConstantEnum::ALL) {
if (obj.name == name) {
return obj;
}
}
throw InvalidFormulaException("Unknown postfix constant " + name);
}
bool ConstantEnum::match(const std::string &name) {
for (auto &obj: ConstantEnum::ALL) {
if (obj.name == name) {
return true;
}
}
return false;
}
const std::string &ConstantEnum::getName() const {
return name;
}
long double ConstantEnum::getValue() const {
return val;
}
Constant::Constant(const std::string &constantName) : constantImpl(ConstantEnum::byName(constantName)) {
}
std::shared_ptr<Expression> Constant::add(std::shared_ptr<ASTNode> toAdd) {
return ASTBuilder::getSelf()->createBinaryOperationExpression(shared_from_this(), std::static_pointer_cast<Operation>(toAdd),
nullptr);
}
long double Constant::resolve(std::map<std::string, long double> vars) const {
return constantImpl.getValue();
}
std::shared_ptr<Expression> Constant::simplify() {
return shared_from_this();
}
bool Constant::openForInput() const {
return false;
}
std::string Constant::toString() const {
return constantImpl.getName();
}