-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpr.h
91 lines (67 loc) · 1.76 KB
/
expr.h
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
85
86
87
88
89
90
91
#ifndef EXPR_H_
#define EXPR_H_ 1
#include <cstdio>
#include <forward_list>
#include <memory>
#include <string>
#include "mpreal.h"
#include "terminal.h"
namespace expression {
class Expression {
public:
enum ExpressionType {
kInvalid,
kQuestion,
// Constants
kNumeric,
kString,
kTime,
kInterval,
// Operators
kMinus,
kAdd,
kSubtract,
kMultiply,
kDivide,
kModulus,
kExponentiate,
// Other functions
kCos,
kSin,
kLog,
kHex,
};
typedef std::forward_list<Expression*> List;
Expression(ExpressionType type) : type_(type) {}
Expression(ExpressionType type, Expression* lhs, Expression* rhs = nullptr)
: type_(type), lhs_(lhs), rhs_(rhs) {}
static Expression* CreateNumeric(const std::string& v);
static Expression* CreateTime(const std::string& v);
static Expression* CreateQuestion() { return new Expression(kQuestion); }
ExpressionType Type() const { return type_; }
bool ToString(std::string* result, const Terminal::State* draw_state) const;
bool IsTrivial() const;
private:
struct Value {
Value() : type(kInvalid) {}
Value(const std::string& v) : type(kString), string(v) {}
Value(const mpfr::mpreal& v) : type(kNumeric), numeric(v) {}
Value(ExpressionType type, const mpfr::mpreal& v) : type(type), numeric(v) {}
Value(ExpressionType type) : type(type) {}
enum ExpressionType type;
std::string string;
mpfr::mpreal numeric;
};
Value Eval() const;
ExpressionType type_;
// For kNumeric.
mpfr::mpreal numeric_;
// For kString and kTime.
std::string string_;
// First argument.
std::unique_ptr<Expression> lhs_;
// Second argument.
std::unique_ptr<Expression> rhs_;
};
} // namespace expression
#endif // !EXPR_H_