2 #ifndef _EVALUATION_VISITOR_CPP_
3 #define _EVALUATION_VISITOR_CPP_
46 void madara::expression::EvaluationVisitor::visit(
49 stack_.push(node.
item());
53 void madara::expression::EvaluationVisitor::visit(
56 stack_.push(node.
item());
60 void madara::expression::EvaluationVisitor::visit(
63 stack_.push(node.
item());
67 void madara::expression::EvaluationVisitor::visit(
70 stack_.push(node.
item());
74 void madara::expression::EvaluationVisitor::visit(
77 stack_.push(node.
item());
81 void madara::expression::EvaluationVisitor::visit(
84 stack_.push(node.
item());
88 void madara::expression::EvaluationVisitor::visit(
91 stack_.push(node.
item());
95 void madara::expression::EvaluationVisitor::visit(
101 void madara::expression::EvaluationVisitor::visit(
104 if (stack_.size() >= 1)
105 stack_.push(-stack_.pop());
108 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
109 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Negate"
110 " requires a right expression"));
112 throw exceptions::KarlException(
"madara::expression::CompositeNegateNode: "
113 "KARL COMPILE ERROR: "
114 "Node has no right expression\n");
119 void madara::expression::EvaluationVisitor::visit(
122 if (stack_.size() >= 1)
127 VariableNode* right =
dynamic_cast<VariableNode*
>(node.
right());
132 new_value = right->dec();
134 stack_.push(new_value);
136 catch (::std::bad_cast&)
138 stack_.push(--old_value);
143 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
144 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Predecrement"
145 " requires a right expression"));
151 void madara::expression::EvaluationVisitor::visit(
154 if (stack_.size() >= 1)
159 VariableNode* right =
dynamic_cast<VariableNode*
>(node.
right());
164 new_value = right->inc();
166 stack_.push(new_value);
168 catch (::std::bad_cast&)
170 stack_.push(++old_value);
175 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
176 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Preincrement"
177 " requires a right expression"));
183 void madara::expression::EvaluationVisitor::visit(
186 if (stack_.size() >= 1)
187 stack_.push(!stack_.pop());
190 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
191 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Not"
192 " requires a right expression"));
198 void madara::expression::EvaluationVisitor::visit(
201 if (stack_.size() >= 2)
202 stack_.push(stack_.pop() + stack_.pop());
205 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
206 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Add"
207 " requires both a left and right expression"));
213 void madara::expression::EvaluationVisitor::visit(
216 if (stack_.size() >= 2)
226 VariableNode* left =
dynamic_cast<VariableNode*
>(node.
left());
230 catch (::std::bad_cast&)
232 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
233 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Assignment"
234 " must have a variable as the left expression\n"));
242 void madara::expression::EvaluationVisitor::visit(
245 if (stack_.size() >= 2)
250 stack_.push(left && right);
254 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
255 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: And"
256 " requires both a left and right expression\n"));
262 void madara::expression::EvaluationVisitor::visit(
265 if (stack_.size() >= 2)
270 stack_.push(left || right);
274 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
275 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Or"
276 " requires both a left and right expression\n"));
282 void madara::expression::EvaluationVisitor::visit(
285 if (stack_.size() >= 2)
292 stack_.push(left_v > right_v ? left_v : right_v);
296 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
297 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: And"
298 " requires both a left and right expression\n"));
304 void madara::expression::EvaluationVisitor::visit(
307 if (stack_.size() >= 2)
314 stack_.push(left_v > right_v ? right_v : left_v);
318 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
319 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: And"
320 " requires both a left and right expression\n"));
326 void madara::expression::EvaluationVisitor::visit(
332 void madara::expression::EvaluationVisitor::visit(
338 void madara::expression::EvaluationVisitor::visit(
341 if (stack_.size() >= 2)
346 stack_.push(left == right);
350 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
351 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Equality"
352 " requires both a left and right expression\n"));
358 void madara::expression::EvaluationVisitor::visit(
361 if (stack_.size() >= 2)
366 stack_.push(left != right);
370 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
371 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Inequality"
372 " requires both a left and right expression\n"));
379 void madara::expression::EvaluationVisitor::visit(
382 if (stack_.size() >= 2)
387 stack_.push(left >= right);
391 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
392 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Greater-than-equal"
393 " requires both a left and right expression\n"));
399 void madara::expression::EvaluationVisitor::visit(
402 if (stack_.size() >= 2)
407 stack_.push(left > right);
411 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
412 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Greater-than"
413 " requires both a left and right expression\n"));
419 void madara::expression::EvaluationVisitor::visit(
422 if (stack_.size() >= 2)
427 stack_.push(left <= right);
431 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
432 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Less-than-equal"
433 " requires both a left and right expression\n"));
439 void madara::expression::EvaluationVisitor::visit(
442 if (stack_.size() >= 2)
447 stack_.push(left < right);
451 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
452 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Less-than"
453 " requires both a left and right expression\n"));
459 void madara::expression::EvaluationVisitor::visit(
462 if (stack_.size() >= 2)
465 stack_.push(stack_.pop() - rhs);
469 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
470 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Subtract"
471 " requires both a left and right expression\n"));
477 void madara::expression::EvaluationVisitor::visit(
480 if (stack_.size() >= 2 && stack_.top())
483 stack_.push(stack_.pop() / rhs);
487 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
488 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Division"
489 " requires both a left and right expression"
490 " (and right must not be 0)\n"));
496 void madara::expression::EvaluationVisitor::visit(
499 if (stack_.size() >= 2)
500 stack_.push(stack_.pop() * stack_.pop());
503 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
504 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Multiply"
505 " requires both a left and right expression\n"));
511 void madara::expression::EvaluationVisitor::visit(
514 if (stack_.size() >= 2 && stack_.top())
517 stack_.push(stack_.pop() / rhs);
521 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
522 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Modulus"
523 " requires both a left and right expression"
524 " (and right must not be 0)\n"));
530 void madara::expression::EvaluationVisitor::visit(
533 if (stack_.size() >= 2)
534 stack_.push(stack_.pop() ? stack_.pop() : 0);
537 MADARA_ERROR(MADARA_LOG_TERMINAL_ERROR,
538 (LM_ERROR, DLINFO
"\nKARL EVAL ERROR: Implies"
539 " requires both a left and right expression"));
545 int64_t madara::expression::EvaluationVisitor::total(
void)
547 if (!stack_.is_empty())
554 void madara::expression::EvaluationVisitor::reset(
void)
virtual ComponentNode * left(void) const
Returns the left expression.
A composite node that encompasses addition of two expressions.
A composite node that performs a logical and.
A composite node that allows for variable assignment.
A composite node that evaluates both left and right expressions regardless of their evaluations.
A composite node that divides a left expression by a right one.
A composite node that compares left and right expressions for equality.
A composite node that iterates until a condition is met.
A composite node that calls a function.
A composite node that compares left and right expressions for greater than or equal to.
A composite node that compares left and right children for greater than.
A composite node that performs an implication (inference rule)
A composite node that compares left and right children for inequality.
A composite node that compares left and right children for less than or equal to.
A composite node that compares left and right children for less than.
A composite node that divides a left expression by a right expression and returns the remainder of th...
A composite node that multiplies a left expression by a right expression.
A composite node that integrally negates a right expression.
A composite node that logically nots a right expression.
A composite node that performs a logical or.
A composite node that decrements a right expression.
A composite node that increments a right expression.
A composite node that evaluates both left and right expressions regardless of their evaluations.
A composite node that encompasses subtraction of a right expression from a left expression.
virtual ComponentNode * right(void) const
Returns the right expression.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable value of the node.
Defines a terminal node that contains a list.
Defines a terminal node of that references the current value stored in a variable.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Composite node that subtracts a variable by some right hand side.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Composite node that divides a variable by some right hand side.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Defines a terminal node of that references the current value stored in a variable.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Composite node that multiplies a variable by some right hand side.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Defines a terminal node of that references the current value stored in a variable.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
This class encapsulates an entry in a KnowledgeBase.
Integer to_integer(void) const
converts the value to an integer.