-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParsenode.cpp
167 lines (127 loc) · 3.66 KB
/
Parsenode.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Parsenode.h"
double Ternopnode::eval(const Context& cc) {
return bool(pred->eval(cc)) ? trueval->eval(cc) : falseval->eval(cc);
}
double Binopnode::eval(const Context& cc) {
double ll = left->eval(cc);
double rr = right->eval(cc);
switch (op) {
case '+':
return ll + rr;
case '-':
return ll - rr;
case '*':
return ll * rr;
case '/':
return ll / rr;
case '%':
return static_cast<int>(ll) % static_cast<int>(rr);
case '&':
return static_cast<double>(bool(ll) && bool(rr));
case '|':
return static_cast<double>(bool(ll) || bool(rr));
// case '=': return ll==rr; // !!! Shouldn't compare
// doubles with ==
case 'g':
return static_cast<double>(ll >= rr);
case 'l':
return static_cast<double>(ll <= rr);
// case 'n': return ll!=rr; // !!! Shouldn't compare
// doubles with ==
case '<':
return static_cast<double>(ll < rr);
case '>':
return static_cast<double>(ll > rr);
default:
throw std::runtime_error(
std::string { "unrecognized binary operator " } + op);
}
}
Binopnode::Binopnode(char _o, ParsenodePtr _l, ParsenodePtr _r) :
op(_o), left(std::move(_l)), right(std::move(_r)) {
}
Ternopnode::Ternopnode(ParsenodePtr _p, ParsenodePtr _t, ParsenodePtr _f) :
pred(std::move(_p)), trueval(std::move(_t)), falseval(std::move(_f)) {
}
Unopnode::Unopnode(char _o, ParsenodePtr _c) :
op(_o), child(std::move(_c)) {
}
double Unopnode::eval(const Context& cc) {
if (op == '!')
return static_cast<double>(!static_cast<bool>(child->eval(cc)));
if (op == '-')
return -child->eval(cc);
if (op == '+')
return child->eval(cc);
throw std::runtime_error(std::string("unrecognized unary operator ") + op);
}
Numnode::Numnode(double _v) :
val(_v) {
}
double Numnode::eval(const Context& /*cc*/) {
return val;
}
double Idnode::eval(const Context& cc) {
//add RND, srand, rand?!!!
static const double DEG = 180 / M_PI;
if (name == "acos")
return acos(pp[0]->eval(cc));
if (name == "acosd")
return DEG * acos(pp[0]->eval(cc));
if (name == "asin")
return asin(pp[0]->eval(cc));
if (name == "asind")
return DEG * asin(pp[0]->eval(cc));
if (name == "atan")
return atan(pp[0]->eval(cc));
if (name == "atand")
return DEG * atan(pp[0]->eval(cc));
if (name == "atan2")
return atan2(pp[0]->eval(cc), pp[1]->eval(cc));
if (name == "atan2d")
return DEG * atan2(pp[0]->eval(cc), pp[1]->eval(cc));
if (name == "ceil")
return ceil(pp[0]->eval(cc));
if (name == "cos")
return cos(pp[0]->eval(cc));
if (name == "cosd")
return cos(pp[0]->eval(cc) / DEG);
if (name == "cosh")
return cosh(pp[0]->eval(cc));
if (name == "exp")
return exp(pp[0]->eval(cc));
if (name == "fabs")
return fabs(pp[0]->eval(cc));
if (name == "floor")
return floor(pp[0]->eval(cc));
if (name == "fmod")
return fmod(pp[0]->eval(cc), pp[1]->eval(cc));
if (name == "log")
return log(pp[0]->eval(cc));
if (name == "log10")
return log10(pp[0]->eval(cc));
if (name == "pow")
return pow(pp[0]->eval(cc), pp[1]->eval(cc));
if (name == "sin")
return sin(pp[0]->eval(cc));
if (name == "sind")
return sin(pp[0]->eval(cc) / DEG);
if (name == "sinh")
return sinh(pp[0]->eval(cc));
if (name == "sqrt")
return sqrt(pp[0]->eval(cc));
if (name == "tan")
return tan(pp[0]->eval(cc));
if (name == "tand")
return tan(pp[0]->eval(cc) / DEG);
auto ii = cc.constants.find(name);
if (ii != cc.constants.end())
return ii->second;
auto jj = cc.expressions.find(name);
if (jj != cc.expressions.end())
return jj->second->eval(cc);
throw std::runtime_error(name + ": undefined identifier.");
}
Idnode::Idnode(std::string _n, std::vector<ParsenodePtr> _p) :
name(std::move(_n)), pp(std::move(_p)) {
}