-
Notifications
You must be signed in to change notification settings - Fork 432
/
Expression.cpp
440 lines (377 loc) · 8.3 KB
/
Expression.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "Expression.hpp"
#include "Parser.hpp"
namespace pdal
{
namespace expr
{
Node::Node(NodeType type) : m_type(type)
{}
Node::~Node()
{}
NodeType Node::type() const
{ return m_type; }
//
// NotNode
//
NotNode::NotNode(NodeType type, NodePtr sub) :
LogicalNode(type), m_sub(std::move(sub))
{}
std::string NotNode::print() const
{
return "!(" + m_sub->print() + ")";
}
Utils::StatusWithReason NotNode::prepare(PointLayoutPtr l)
{
return m_sub->prepare(l);
}
Result NotNode::eval(PointRef& p) const
{
return !(m_sub->eval(p).m_bval);
}
//
// UnMathNode
//
UnMathNode::UnMathNode(NodeType type, NodePtr sub) :
ValueNode(type), m_sub(std::move(sub))
{}
std::string UnMathNode::print() const
{
return "-(" + m_sub->print() + ")";
}
Utils::StatusWithReason UnMathNode::prepare(PointLayoutPtr l)
{
return m_sub->prepare(l);
}
Result UnMathNode::eval(PointRef& p) const
{
return -(m_sub->eval(p).m_dval);
}
//
// BinMathNode
//
BinMathNode::BinMathNode(NodeType type, NodePtr left, NodePtr right) :
ValueNode(type), m_left(std::move(left)), m_right(std::move(right))
{}
std::string BinMathNode::print() const
{
std::string s;
switch (type())
{
case NodeType::Add:
s = "+";
break;
case NodeType::Subtract:
s = "-";
break;
case NodeType::Multiply:
s = "*";
break;
case NodeType::Divide:
s = "/";
break;
default:
break;
}
return "(" + m_left->print() + " " + s + " " + m_right->print() + ")";
}
Utils::StatusWithReason BinMathNode::prepare(PointLayoutPtr l)
{
auto status = m_left->prepare(l);
if (status)
status = m_right->prepare(l);
return status;
}
Result BinMathNode::eval(PointRef& p) const
{
double l = m_left->eval(p).m_dval;
double r = m_right->eval(p).m_dval;
switch (type())
{
case NodeType::Add:
return l + r;
case NodeType::Subtract:
return l - r;
case NodeType::Multiply:
return l * r;
case NodeType::Divide:
if (r == 0)
return std::numeric_limits<double>::quiet_NaN();
return l / r;
default:
break;
}
assert(false);
return 0.0;
}
//
// Bool node
//
BoolNode::BoolNode(NodeType type, NodePtr left, NodePtr right) :
LogicalNode(type), m_left(std::move(left)), m_right(std::move(right))
{}
std::string BoolNode::print() const
{
std::string s;
switch (type())
{
case NodeType::And:
s = "&&";
break;
case NodeType::Or:
s = "||";
break;
default:
break;
}
return "(" + m_left->print() + " " + s + " " + m_right->print() + ")";
}
Utils::StatusWithReason BoolNode::prepare(PointLayoutPtr l)
{
auto status = m_left->prepare(l);
if (status)
status = m_right->prepare(l);
return status;
}
Result BoolNode::eval(PointRef& p) const
{
bool l = m_left->eval(p).m_bval;
bool r = m_right->eval(p).m_bval;
switch (type())
{
case NodeType::And:
return l && r;
case NodeType::Or:
return l || r;
default:
break;
}
assert(false);
return false;
}
//
// CompareNode
//
CompareNode::CompareNode(NodeType type, NodePtr left, NodePtr right) :
LogicalNode(type), m_left(std::move(left)), m_right(std::move(right))
{}
Utils::StatusWithReason CompareNode::prepare(PointLayoutPtr l)
{
auto status = m_left->prepare(l);
if (status)
status = m_right->prepare(l);
return status;
}
std::string CompareNode::print() const
{
std::string s;
switch (type())
{
case NodeType::Equal:
s = "==";
break;
case NodeType::NotEqual:
s = "!=";
break;
case NodeType::Greater:
s = ">";
break;
case NodeType::GreaterEqual:
s = ">=";
break;
case NodeType::Less:
s = "<";
break;
case NodeType::LessEqual:
s = "<=";
break;
default:
break;
}
return "(" + m_left->print() + s + m_right->print() + ")";
}
Result CompareNode::eval(PointRef& p) const
{
double l = m_left->eval(p).m_dval;
double r = m_right->eval(p).m_dval;
switch (type())
{
case NodeType::Equal:
return l == r;
case NodeType::NotEqual:
return l != r;
case NodeType::Less:
return l < r;
case NodeType::LessEqual:
return l <= r;
case NodeType::Greater:
return l > r;
case NodeType::GreaterEqual:
return l >= r;
default:
break;
}
assert(false);
return false;
}
//
// ConstValueNode
//
ConstValueNode::ConstValueNode(double d) : ValueNode(NodeType::Value), m_val(d)
{}
std::string ConstValueNode::print() const
{
return std::to_string(m_val);
}
Utils::StatusWithReason ConstValueNode::prepare(PointLayoutPtr l)
{
return true;
}
Result ConstValueNode::eval(PointRef&) const
{
return m_val;
}
double ConstValueNode::value() const
{
return m_val;
}
//
// ConstLogicalNode
//
ConstLogicalNode::ConstLogicalNode(bool b) :
LogicalNode(NodeType::Value), m_val(b)
{}
std::string ConstLogicalNode::print() const
{
return m_val ? "true" : "false";
}
Utils::StatusWithReason ConstLogicalNode::prepare(PointLayoutPtr l)
{
return true;
}
Result ConstLogicalNode::eval(PointRef&) const
{
return m_val;
}
bool ConstLogicalNode::value() const
{
return m_val;
}
//
// Var Node
//
VarNode::VarNode(const std::string& s) : ValueNode(NodeType::Identifier),
m_name(s), m_id(Dimension::Id::Unknown)
{}
std::string VarNode::print() const
{
return m_name;
}
Result VarNode::eval(PointRef& p) const
{
return p.getFieldAs<double>(m_id);
}
Utils::StatusWithReason VarNode::prepare(PointLayoutPtr l)
{
m_id = l->findDim(m_name);
if (m_id == Dimension::Id::Unknown)
return { -1, "Unknown dimension '" + m_name + "' in assigment." };
return true;
}
//
// Expression
//
Expression::Expression()
{}
// This is a strange copy ctor that ignores the source. At this point we
// don't need it to do anything, but we do need the an expression to
// be copyable. In order to copy, we'd actually have to deep-copy the
// nodes, but there is no way to do that right now.
Expression::Expression(const Expression& expr)
{}
Expression::~Expression()
{}
// This is a strange copy ctor that ignores the source. At this point we
// don't need it to do anything, but we do need the an expression to
// be copyable. In order to copy, we'd actually have to deep-copy the
// nodes, but there is no way to do that right now.
Expression& Expression::operator=(const Expression& expr)
{
clear();
return *this;
}
void Expression::clear()
{
std::stack<NodePtr> empty;
m_nodes.swap(empty);
m_error.clear();
}
bool Expression::parse(const std::string& s)
{
clear();
Parser p(*this);
bool ok = p.parse(s);
if (!ok)
m_error = p.error();
return ok;
}
std::string Expression::error() const
{
return m_error;
}
std::string Expression::print() const
{
return m_nodes.top()->print();
}
NodePtr Expression::popNode()
{
NodePtr n(std::move(m_nodes.top()));
m_nodes.pop();
return n;
}
void Expression::pushNode(NodePtr node)
{
m_nodes.push(std::move(node));
}
Utils::StatusWithReason Expression::prepare(PointLayoutPtr layout)
{
/**
if (m_nodes.size())
return m_nodes.top()->prepare(layout);
**/
if (m_nodes.size())
{
Node *top = m_nodes.top().get();
auto status = top->prepare(layout);
std::cerr << top->print() << "!\n";
if (status)
{
if (top->isValue())
status =
{ -1, "Expression evalutes to a value, not a boolean." };
else
{
ConstLogicalNode *n = dynamic_cast<ConstLogicalNode *>(top);
if (n)
{
if (n->value())
status = { -1, "Expression is always true." };
else
status = { -1, "Expression is always false." };
}
}
}
return status;
}
return true;
}
bool Expression::eval(PointRef& p) const
{
return m_nodes.top()->eval(p).m_bval;
}
std::ostream& operator<<(std::ostream& out, const Expression& expr)
{
out << expr.print();
return out;
}
} // namespace expr
} // namespace pdal