MADARA  3.4.1
SystemCallFragment.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
7 
10  : SystemCallNode(context, nodes)
11 {
12 }
13 
14 // Dtor
16 
18  void) const
19 {
20  return madara::knowledge::KnowledgeRecord(nodes_.size());
21 }
22 
28 {
29  // user can always change a function, and we have no control over
30  // what it does. Consequently, a function node cannot be pruned out
31  // under any situation
32  can_change = true;
33 
35 
36  for (ComponentNodes::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
37  {
38  bool arg_can_change = false;
39  result = (*i)->prune(arg_can_change);
40 
41  if (!arg_can_change && dynamic_cast<LeafNode*>(*i) == 0)
42  {
43  delete *i;
44  *i = new LeafNode(*(this->logger_), result);
45  }
46  }
47 
48  if (nodes_.size() != 3)
49  {
51  "madara::expression::SystemCallFragment: "
52  "KARL COMPILE ERROR:"
53  "System call fragment"
54  " requires three arguments, e.g."
55  " #fragment ('hello world', 0, 4) will return 'hello'\n");
56 
58  "madara::expression::SystemCallFragment: "
59  "KARL COMPILE ERROR: "
60  "System call fragment"
61  " requires three arguments, e.g."
62  " #fragment ('hello world', 0, 4) will return 'hello'\n");
63  }
64 
65  return result;
66 }
67 
73 {
74  knowledge::KnowledgeRecord return_value;
75 
76  if (nodes_.size() == 3)
77  {
78  // grab the first and last ranges
79  unsigned int first =
80  (unsigned int)nodes_[1]->evaluate(settings).to_integer();
81  unsigned int last =
82  (unsigned int)nodes_[2]->evaluate(settings).to_integer();
83 
85  "madara::expression::SystemCallFragment: "
86  "System call fragment is returning segment [%d-%d].\n",
87  first, last);
88 
89  return nodes_[0]->evaluate(settings).fragment(first, last);
90  }
91 
92  else
93  {
95  "madara::expression::SystemCallFragment: "
96  "KARL RUNTIME ERROR:"
97  "System call fragment"
98  " requires three arguments, e.g."
99  " #fragment ('hello world', 0, 4) will return 'hello'\n");
100 
102  "madara::expression::SystemCallFragment: "
103  "KARL RUNTIME ERROR: "
104  "System call fragment"
105  " requires three arguments, e.g."
106  " #fragment ('hello world', 0, 4) will return 'hello'\n");
107  }
108 
109  return return_value;
110 }
111 
112 // accept a visitor
114  madara::expression::Visitor& visitor) const
115 {
116  visitor.visit(*this);
117 }
118 
119 #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
madara::knowledge::KnowledgeRecord KnowledgeRecord
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
SystemCallFragment(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
virtual ~SystemCallFragment(void)
Destructor.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
Interface for a MADARA system call.
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.
Integer to_integer(void) const
converts the value to an integer.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes