MADARA  3.4.1
VariableDecrementNode.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
7 
8 #include <string>
9 #include <sstream>
10 
14  : ComponentNode(context.get_logger()),
15  var_(0),
16  array_(0),
17  value_(value),
18  rhs_(rhs)
19 {
20  var_ = dynamic_cast<VariableNode*>(lhs);
21 
22  if (!var_)
23  array_ = dynamic_cast<CompositeArrayReference*>(lhs);
24 }
25 
27 {
28  // do not clean up record_. Let the context clean that up.
29 }
30 
32 {
33  visitor.visit(*this);
34 }
35 
38 {
40 
41  if (var_)
42  value = var_->item();
43  else if (array_)
44  value = array_->item();
45 
46  return value;
47 }
48 
54 {
55  bool left_child_can_change = false;
56  bool right_child_can_change = false;
58 
59  if (this->var_ != 0 || this->array_ != 0)
60  left_child_can_change = true;
61  else
62  {
64  "madara::expression::VariableDecrementNode: "
65  "KARL COMPILE ERROR: Variable assignment has no variable\n");
66 
68  "madara::expression::VariableDecrementNode: "
69  "KARL COMPILE ERROR: "
70  "Node has no variable left-hand side\n");
71  }
72 
73  if (this->rhs_)
74  {
75  right_value = this->rhs_->prune(right_child_can_change);
76  if (!right_child_can_change && dynamic_cast<LeafNode*>(rhs_) == 0)
77  {
78  delete this->rhs_;
79  this->rhs_ = new LeafNode(*(this->logger_), right_value);
80  }
81  }
82  else
83  {
85  "madara::expression::VariableDecrementNode: "
86  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
87 
89  "madara::expression::VariableDecrementNode: "
90  "KARL COMPILE ERROR: "
91  "Node has no right expression\n");
92  }
93 
94  can_change = left_child_can_change || right_child_can_change;
95 
96  return right_value;
97 }
98 
104 {
106 
107  if (rhs_)
108  rhs = rhs_->evaluate(settings);
109  else
110  rhs = value_;
111 
112  if (var_)
113  {
115  "madara::expression::CompositeAssignmentNode::evaluate: "
116  "Attempting to set variable %s to %s.\n",
117  var_->expand_key().c_str(), rhs.to_string().c_str());
118 
119  knowledge::KnowledgeRecord result(var_->evaluate(settings) - rhs);
120  var_->set(result, settings);
121  return result;
122  }
123  else if (array_)
124  {
126  "madara::expression::CompositeAssignmentNode::evaluate: "
127  "Attempting to set index of var %s to %s.\n",
128  array_->expand_key().c_str(), rhs.to_string().c_str());
129 
130  knowledge::KnowledgeRecord result(array_->evaluate(settings) - rhs);
131  array_->set(result, settings);
132  return result;
133  }
134  else
135  {
137  "madara::expression::CompositeAssignmentNode::evaluate: "
138  "left hand side was neither a variable nor an array reference. "
139  "Check your expression for errors.\n",
140  array_->expand_key().c_str(), rhs.to_string().c_str());
141  }
142 
143  // return the value
144  return rhs;
145 }
146 
147 #endif // _MADARA_NO_KARL_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
An abstract base class defines a simple abstract implementation of an expression tree node.
Definition: ComponentNode.h:37
Defines a terminal node of that references the current value stored in a variable.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
VariableDecrementNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
CompositeArrayReference * array_
variable index holder
Defines a terminal node of that references the current value stored in a variable.
Definition: VariableNode.h:29
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:93
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...