MADARA  3.4.1
CompositeAssignmentNode.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 #ifndef _ASSIGNMENT_NODE_CPP_
4 #define _ASSIGNMENT_NODE_CPP_
5 
6 #ifndef _MADARA_NO_KARL_
7 
8 #include <iostream>
9 
14 
15 // Ctor
16 
19  : CompositeUnaryNode(logger, right)
20 {
21  var_ = dynamic_cast<VariableNode*>(left);
22 
23  if (!var_)
24  array_ = dynamic_cast<CompositeArrayReference*>(left);
25 }
26 
29 {
30  return knowledge::KnowledgeRecord("=");
31 }
32 
35 {
36  bool left_child_can_change = false;
37  bool right_child_can_change = false;
39 
40  if (this->var_ != 0 || this->array_ != 0)
41  left_child_can_change = true;
42  else
43  {
45  "CompositeAssignmentNode: "
46  "KARL COMPILE ERROR (=): "
47  "Assignments must have a variable left hand side.\n");
48 
50  "CompositeAssignmentNode: "
51  "KARL COMPILE ERROR (=): "
52  "Assignments must have a variable left hand side");
53  }
54 
55  if (this->right_)
56  {
57  right_value = this->right_->prune(right_child_can_change);
58  if (!right_child_can_change && dynamic_cast<LeafNode*>(right_) == 0)
59  {
60  delete this->right_;
61  this->right_ = new LeafNode(*(this->logger_), right_value);
62  }
63  }
64  else
65  {
66  // we should never be able to get here but whatever
68  "CompositeAssignmentNode: KARL COMPILE ERROR: "
69  "Assignment has no right expression\n");
70 
72  "CompositeAssignmentNode: KARL COMPILE ERROR: "
73  "Assignment has no right expression\n");
74  }
75 
76  can_change = left_child_can_change || right_child_can_change;
77 
78  return right_value;
79 }
80 
86 {
87  madara::knowledge::KnowledgeRecord rhs = right_->evaluate(settings);
88 
89  // get the value from the right side and set the variable's value with it
90  // madara::knowledge::KnowledgeRecord value = right_->evaluate ();
91  if (var_)
92  {
94  "CompositeAssignmentNode::evaluate: "
95  "Attempting to set variable %s to %s.\n",
96  var_->expand_key().c_str(), rhs.to_string().c_str());
97 
98  var_->set(rhs, settings);
99  }
100  else if (array_)
101  {
103  "CompositeAssignmentNode::evaluate: "
104  "Attempting to set index of var %s to %s.\n",
105  array_->expand_key().c_str(), rhs.to_string().c_str());
106 
107  array_->set(rhs, settings);
108  }
109  else
110  {
112  "left hand side was neither a variable nor an array reference. "
113  "Check your expression for errors.\n");
114  }
115 
116  // return the value
117  return rhs;
118 }
119 
120 // accept a visitor
122 {
123  visitor.visit(*this);
124 }
125 
126 #endif // _MADARA_NO_KARL_
127 
128 #endif /* _ASSIGNMENT_NODE_CPP_ */
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
madara::knowledge::KnowledgeRecord KnowledgeRecord
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
virtual ComponentNode * left(void) const
Returns the left expression.
Defines a terminal node of that references the current value stored in a variable.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
CompositeAssignmentNode(logger::Logger &logger, ComponentNode *left, ComponentNode *right)
Constructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
VariableNode * var_
Left should always be a variable node.
Encapsulates a single expression tree.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
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.
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:165
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:12