MADARA  3.4.1
SystemCallSqrt.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <math.h>
5 
10 
13  : SystemCallNode(context, nodes)
14 {
15 }
16 
17 // Dtor
19 
21  void) const
22 {
23  return madara::knowledge::KnowledgeRecord(nodes_.size());
24 }
25 
30  bool& can_change)
31 {
33 
34  if (nodes_.size() == 1)
35  {
36  bool arg_can_change = false;
38  sqrt(nodes_[0]->prune(arg_can_change).to_double()));
39 
40  if (!arg_can_change && dynamic_cast<LeafNode*>(nodes_[0]) == 0)
41  {
42  delete nodes_[0];
43  nodes_[0] = new LeafNode(*(this->logger_), result);
44  }
45 
46  can_change = can_change || arg_can_change;
47  }
48  else
49  {
51  "madara::expression::SystemCallSleep: "
52  "KARL COMPILE ERROR:"
53  "System call sqrt requires 1 argument,"
54  "e.g., #sqrt (4), which would return 2\n");
55 
56  throw exceptions::KarlException("madara::expression::SystemCallSleep: "
57  "KARL COMPILE ERROR: "
58  "System call sqrt requires 1 argument,"
59  "e.g., #sqrt (4), which would return 2\n");
60  }
61 
62  return result;
63 }
64 
69 {
70  knowledge::KnowledgeRecord return_value;
71 
72  if (nodes_.size() == 1)
73  {
75  "madara::expression::SystemCallSqrt: "
76  "System call sqrt is returning the square root "
77  "of its first argument\n");
78 
79  return_value = knowledge::KnowledgeRecord(
80  sqrt(nodes_[0]->evaluate(settings).to_double()));
81  }
82  else
83  {
85  "madara::expression::SystemCallSqrt: "
86  "KARL RUNTIME ERROR:"
87  "System call sqrt requires 1 argument,"
88  "e.g., #sqrt (4), which would return 2\n");
89 
90  throw exceptions::KarlException("madara::expression::SystemCallSqrt: "
91  "KARL RUNTIME ERROR: "
92  "System call sqrt requires 1 argument,"
93  "e.g., #sqrt (4), which would return 2\n");
94  }
95 
96  return return_value;
97 }
98 
99 // accept a visitor
101  madara::expression::Visitor& visitor) const
102 {
103  visitor.visit(*this);
104 }
105 
106 #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
Interface for a MADARA system call.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
SystemCallSqrt(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual ~SystemCallSqrt(void)
Destructor.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
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.
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