MADARA  3.4.1
VariableIncrementNode.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  context_(context)
20 {
21  var_ = dynamic_cast<VariableNode*>(lhs);
22 
23  if (!var_)
24  array_ = dynamic_cast<CompositeArrayReference*>(lhs);
25 }
26 
28 {
29  // do not clean up record_. Let the context clean that up.
30 }
31 
33 {
34  visitor.visit(*this);
35 }
36 
39 {
41 
42  if (var_)
43  value = var_->item();
44  else if (array_)
45  value = array_->item();
46 
47  return value;
48 }
49 
55 {
56  bool left_child_can_change = false;
57  bool right_child_can_change = false;
59 
60  if (this->var_ != 0 || this->array_ != 0)
61  left_child_can_change = true;
62  else
63  {
65  "madara::expression::VariableIncrementNode: "
66  "KARL COMPILE ERROR: Variable assignment has no variable\n");
67 
69  "madara::expression::VariableIncrementNode: "
70  "KARL COMPILE ERROR: "
71  "Node has no variable left-hand side\n");
72  }
73 
74  if (this->rhs_)
75  {
76  right_value = this->rhs_->prune(right_child_can_change);
77  if (!right_child_can_change && dynamic_cast<LeafNode*>(rhs_) == 0)
78  {
79  delete this->rhs_;
80  this->rhs_ = new LeafNode(*(this->logger_), right_value);
81  }
82  }
83  else
84  {
86  "madara::expression::VariableIncrementNode: "
87  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
88 
90  "madara::expression::VariableIncrementNode: "
91  "KARL COMPILE ERROR: "
92  "Node has no right expression\n");
93  }
94 
95  can_change = left_child_can_change || right_child_can_change;
96 
97  return right_value;
98 }
99 
105 {
107 
108  if (rhs_)
109  rhs = rhs_->evaluate(settings);
110  else
111  rhs = value_;
112 
113  if (var_)
114  {
116  "madara::expression::CompositeAssignmentNode::evaluate: "
117  "Attempting to set variable %s to %s.\n",
118  var_->expand_key().c_str(), rhs.to_string().c_str());
119 
120  knowledge::KnowledgeRecord result(var_->evaluate(settings) + rhs);
121  var_->set(result, settings);
122  return result;
123  }
124  else if (array_)
125  {
127  "madara::expression::CompositeAssignmentNode::evaluate: "
128  "Attempting to set index of var %s to %s.\n",
129  array_->expand_key().c_str(), rhs.to_string().c_str());
130 
131  knowledge::KnowledgeRecord result(array_->evaluate(settings) + rhs);
132  array_->set(result, settings);
133  return result;
134  }
135  else
136  {
138  "madara::expression::CompositeAssignmentNode::evaluate: "
139  "left hand side was neither a variable nor an array reference. "
140  "Check your expression for errors.\n",
141  array_->expand_key().c_str(), rhs.to_string().c_str());
142  }
143 
144  // return the value
145  return rhs;
146 }
147 
148 #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
const ThreadSafeContext * context_
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 void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
VariableIncrementNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.
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...