2 #ifndef _EVALUATION_VISITOR_CPP_ 3 #define _EVALUATION_VISITOR_CPP_ 47 madara::expression::EvaluationVisitor::visit (
50 stack_.push (node.
item ());
55 madara::expression::EvaluationVisitor::visit (
58 stack_.push (node.
item ());
63 madara::expression::EvaluationVisitor::visit (
66 stack_.push (node.
item ());
71 madara::expression::EvaluationVisitor::visit (
74 stack_.push (node.
item ());
79 madara::expression::EvaluationVisitor::visit (
82 stack_.push (node.
item ());
87 madara::expression::EvaluationVisitor::visit (
90 stack_.push (node.
item ());
95 madara::expression::EvaluationVisitor::visit (
98 stack_.push (node.
item ());
103 madara::expression::EvaluationVisitor::visit (
110 madara::expression::EvaluationVisitor::visit (
113 if (stack_.size () >= 1)
114 stack_.push (-stack_.pop ());
118 "\nKARL EVAL ERROR: Negate" \
119 " requires a right expression"));
126 madara::expression::EvaluationVisitor::visit (
129 if (stack_.size () >= 1)
134 VariableNode * right = dynamic_cast <VariableNode *> (node.
right ());
139 new_value = right->dec ();
141 stack_.push (new_value);
143 catch (::std::bad_cast &)
145 stack_.push (--old_value);
151 "\nKARL EVAL ERROR: Predecrement" \
152 " requires a right expression"));
159 madara::expression::EvaluationVisitor::visit (
162 if (stack_.size () >= 1)
167 VariableNode * right = dynamic_cast <VariableNode *> (node.
right ());
172 new_value = right->inc ();
174 stack_.push (new_value);
176 catch (::std::bad_cast &)
178 stack_.push (++old_value);
184 "\nKARL EVAL ERROR: Preincrement" \
185 " requires a right expression"));
192 madara::expression::EvaluationVisitor::visit (
195 if (stack_.size () >= 1)
196 stack_.push (!stack_.pop ());
200 "\nKARL EVAL ERROR: Not" \
201 " requires a right expression"));
208 madara::expression::EvaluationVisitor::visit (
211 if (stack_.size () >= 2)
212 stack_.push (stack_.pop () + stack_.pop ());
216 "\nKARL EVAL ERROR: Add" \
217 " requires both a left and right expression"));
224 madara::expression::EvaluationVisitor::visit (
227 if (stack_.size () >= 2)
237 VariableNode * left = dynamic_cast <VariableNode *> (node.
left ());
241 catch (::std::bad_cast &)
244 "\nKARL EVAL ERROR: Assignment" \
245 " must have a variable as the left expression\n"));
254 madara::expression::EvaluationVisitor::visit (
257 if (stack_.size () >= 2)
262 stack_.push (left && right);
267 "\nKARL EVAL ERROR: And" \
268 " requires both a left and right expression\n"));
275 madara::expression::EvaluationVisitor::visit (
278 if (stack_.size () >= 2)
283 stack_.push (left || right);
288 "\nKARL EVAL ERROR: Or" \
289 " requires both a left and right expression\n"));
296 madara::expression::EvaluationVisitor::visit (
299 if (stack_.size () >= 2)
306 stack_.push (left_v > right_v ? left_v : right_v);
311 "\nKARL EVAL ERROR: And" \
312 " requires both a left and right expression\n"));
319 madara::expression::EvaluationVisitor::visit (
322 if (stack_.size () >= 2)
329 stack_.push (left_v > right_v ? right_v : left_v);
334 "\nKARL EVAL ERROR: And" \
335 " requires both a left and right expression\n"));
342 madara::expression::EvaluationVisitor::visit (
349 madara::expression::EvaluationVisitor::visit (
356 madara::expression::EvaluationVisitor::visit (
359 if (stack_.size () >= 2)
364 stack_.push (left == right);
369 "\nKARL EVAL ERROR: Equality" \
370 " requires both a left and right expression\n"));
377 madara::expression::EvaluationVisitor::visit (
380 if (stack_.size () >= 2)
385 stack_.push (left != right);
390 "\nKARL EVAL ERROR: Inequality" \
391 " requires both a left and right expression\n"));
398 madara::expression::EvaluationVisitor::visit (
401 if (stack_.size () >= 2)
406 stack_.push (left >= right);
411 "\nKARL EVAL ERROR: Greater-than-equal" \
412 " requires both a left and right expression\n"));
419 madara::expression::EvaluationVisitor::visit (
422 if (stack_.size () >= 2)
427 stack_.push (left > right);
432 "\nKARL EVAL ERROR: Greater-than" \
433 " requires both a left and right expression\n"));
440 madara::expression::EvaluationVisitor::visit (
443 if (stack_.size () >= 2)
448 stack_.push (left <= right);
453 "\nKARL EVAL ERROR: Less-than-equal" \
454 " requires both a left and right expression\n"));
461 madara::expression::EvaluationVisitor::visit (
464 if (stack_.size () >= 2)
469 stack_.push (left < right);
474 "\nKARL EVAL ERROR: Less-than" \
475 " requires both a left and right expression\n"));
482 madara::expression::EvaluationVisitor::visit (
485 if (stack_.size () >= 2)
488 stack_.push (stack_.pop () - rhs);
493 "\nKARL EVAL ERROR: Subtract" \
494 " requires both a left and right expression\n"));
501 madara::expression::EvaluationVisitor::visit (
504 if (stack_.size () >= 2 && stack_.top ())
507 stack_.push (stack_.pop () / rhs );
512 "\nKARL EVAL ERROR: Division" \
513 " requires both a left and right expression" \
514 " (and right must not be 0)\n"));
521 madara::expression::EvaluationVisitor::visit (
524 if (stack_.size () >= 2)
525 stack_.push (stack_.pop () * stack_.pop ());
529 "\nKARL EVAL ERROR: Multiply" \
530 " requires both a left and right expression\n"));
537 madara::expression::EvaluationVisitor::visit (
540 if (stack_.size () >= 2 && stack_.top ())
543 stack_.push (stack_.pop () / rhs );
548 "\nKARL EVAL ERROR: Modulus" \
549 " requires both a left and right expression" \
550 " (and right must not be 0)\n"));
557 madara::expression::EvaluationVisitor::visit (
560 if (stack_.size () >= 2)
561 stack_.push (stack_.pop () ? stack_.pop () : 0);
565 "\nKARL EVAL ERROR: Implies" \
566 " requires both a left and right expression"));
573 madara::expression::EvaluationVisitor::total (
void)
575 if (!stack_.is_empty ())
576 return stack_.top ();
583 madara::expression::EvaluationVisitor::reset (
void)
588 #endif // _USE_VISITORS_ This class encapsulates an entry in a KnowledgeBase.
Composite node that divides a variable by some right hand side.
Defines a terminal node of that references the current value stored in a variable.
A composite node that increments a right expression.
Defines a terminal node of that references the current value stored in a variable.
A composite node that encompasses addition of two expressions.
A composite node that compares left and right children for inequality.
Composite node that subtracts a variable by some right hand side.
A composite node that encompasses subtraction of a right expression from a left expression.
A composite node that iterates until a condition is met.
A composite node that divides a left expression by a right expression and returns the remainder of th...
A composite node that compares left and right expressions for greater than or equal to...
A composite node that evaluates both left and right expressions regardless of their evaluations...
A composite node that compares left and right children for less than or equal to. ...
A composite node that multiplies a left expression by a right expression.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
A composite node that performs an implication (inference rule)
A composite node that decrements a right expression.
#define MADARA_ERROR(L, X)
A composite node that evaluates both left and right expressions regardless of their evaluations...
A composite node that calls a function.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
A composite node that performs a logical and.
Composite node that multiplies a variable by some right hand side.
A composite node that compares left and right expressions for equality.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
A composite node that logically nots a right expression.
A composite node that allows for variable assignment.
A composite node that compares left and right children for greater than.
A composite node that integrally negates a right expression.
Integer to_integer(void) const
converts the value to an integer
virtual ComponentNode * left(void) const
Returns the left expression.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable value of the node.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
A composite node that performs a logical or.
A composite node that divides a left expression by a right one.
Defines a terminal node of that references the current value stored in a variable.
#define MADARA_LOG_TERMINAL_ERROR
Used for MADARA errors at the point the error exits the process in question, or when a decision is ma...
Defines a terminal node that contains a list.
virtual ComponentNode * right(void) const
Returns the right expression.
A composite node that compares left and right children for less than.