MADARA  3.1.8
Interpreter.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_KNOWLEDGE_INTERPRETER_H_
4 #define _MADARA_KNOWLEDGE_INTERPRETER_H_
5 
6 #ifndef _MADARA_NO_KARL_
7 
16 #include <string>
17 #include <list>
18 #include <map>
19 
23 
24 namespace madara
25 {
26  namespace expression
27  {
28  // Forward declaration.
29  class Symbol;
30 
31  typedef std::map <std::string, ExpressionTree> ExpressionTreeMap;
32 
43  {
44  public:
48  Interpreter ();
49 
53  virtual ~Interpreter ();
54 
63  const std::string &input);
64 
70  static inline bool is_string_literal (char input);
71 
77  static inline bool is_operator (char input);
78 
84  static inline bool is_number (char input);
85 
91  static inline bool is_alphanumeric (char input);
92 
98  static inline bool is_whitespace (char input);
99 
105  static inline bool is_exponential (char input);
106 
112  inline bool delete_expression (const std::string & expression);
113 
114  private:
126  void handle_for_loop (
128  std::string &variable,
129  const std::string &input,
130  std::string::size_type &i,
131  int & accumulated_precedence,
132  ::std::list<Symbol *>& list,
133  Symbol *& returnableInput);
134 
144  void variable_insert (
146  const std::string &input,
147  std::string::size_type &i,
148  int & accumulated_precedence,
149  ::std::list<Symbol *>& list,
150  Symbol *& lastValidInput);
151 
162  void string_insert (
163  char opener,
165  const std::string &input,
166  std::string::size_type &i,
167  int & accumulated_precedence,
168  ::std::list<Symbol *>& list,
169  Symbol *& lastValidInput);
170 
180  void system_call_insert (
182  const std::string &input,
183  std::string::size_type &i,
184  int & accumulated_precedence,
185  ::std::list<Symbol *>& list,
186  Symbol *& lastValidInput);
187 
197  void number_insert (
199  const std::string &input,
200  std::string::size_type &i,
201  int & accumulated_precedence,
202  ::std::list<Symbol *>& list,
203  Symbol *& lastValidInput);
204 
211  void precedence_insert (
213  Symbol *op, ::std::list<Symbol *>& list);
214 
228  const std::string &input,
229  std::string::size_type &i,
230  Symbol *& lastValidInput,
231  bool & handled,
232  int & accumulated_precedence,
233  ::std::list<Symbol *>& list,
234  bool build_argument_list = false);
235 
236 
247  void handle_array (
249  const std::string &input,
250  std::string::size_type &i,
251  Symbol *& lastValidInput,
252  bool & handled,
253  int & accumulated_precedence,
254  ::std::list<Symbol *>& list);
255 
268  void handle_parenthesis (
270  const std::string &input,
271  std::string::size_type &i,
272  Symbol *& lastValidInput,
273  bool & handled,
274  int & accumulated_precedence,
275  ::std::list<Symbol *>& list,
276  bool build_argument_list = false);
280  ExpressionTreeMap cache_;
281  };
282  }
283 }
284 
285 #include "Interpreter.inl"
286 
287 #endif // _MADARA_NO_KARL_
288 
289 #endif /* _MADARA_KNOWLEDGE_INTERPRETER_H_ */
ExpressionTree interpret(madara::knowledge::ThreadSafeContext &context, const std::string &input)
Compiles an expression into an expression tree.
Encapsulates a MADARA KaRL expression into an evaluatable tree.
This class stores variables and their values for use by any entity needing state information in a thr...
void precedence_insert(madara::knowledge::ThreadSafeContext &context, Symbol *op,::std::list< Symbol * > &list)
Inserts a mathematical operator into the tree.
void main_loop(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, Symbol *&lastValidInput, bool &handled, int &accumulated_precedence,::std::list< Symbol * > &list, bool build_argument_list=false)
Inserts a variable into the tree.
static bool is_alphanumeric(char input)
Checks a character to see if it is alphanumeric.
Definition: Interpreter.inl:53
static bool is_number(char input)
Checks a character to see if it is a number.
Definition: Interpreter.inl:30
void number_insert(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, int &accumulated_precedence,::std::list< Symbol * > &list, Symbol *&lastValidInput)
Inserts a number into the tree.
void string_insert(char opener, madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, int &accumulated_precedence,::std::list< Symbol * > &list, Symbol *&lastValidInput)
Inserts a variable into the tree.
static bool is_whitespace(char input)
Checks a character to see if it is whitespace.
Definition: Interpreter.inl:64
Abstract base class of all parse tree nodes.
Parses incoming expression strings into a parse tree and generates an expression tree from the parse ...
Definition: Interpreter.h:42
ExpressionTreeMap cache_
Cache of expressions that have been previously compiled.
Definition: Interpreter.h:280
void handle_parenthesis(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, Symbol *&lastValidInput, bool &handled, int &accumulated_precedence,::std::list< Symbol * > &list, bool build_argument_list=false)
Handles a parenthesis.
static bool is_exponential(char input)
Checks a character to see if it is in scientific format.
Definition: Interpreter.inl:37
void system_call_insert(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, int &accumulated_precedence,::std::list< Symbol * > &list, Symbol *&lastValidInput)
Inserts a system call into the tree.
static constexpr struct madara::knowledge::tags::string_t string
void handle_for_loop(madara::knowledge::ThreadSafeContext &context, std::string &variable, const std::string &input, std::string::size_type &i, int &accumulated_precedence,::std::list< Symbol * > &list, Symbol *&returnableInput)
extracts precondition, condition, postcondition, and body from input
void handle_array(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, Symbol *&lastValidInput, bool &handled, int &accumulated_precedence,::std::list< Symbol * > &list)
Handles a parenthesis.
std::map< std::string, ExpressionTree > ExpressionTreeMap
Definition: Interpreter.h:29
Copyright (c) 2015 Carnegie Mellon University.
static bool is_string_literal(char input)
Checks a character to see if it is a string literal.
Definition: Interpreter.inl:45
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
Definition: Interpreter.inl:75
static bool is_operator(char input)
Checks a character to see if it is a mathematical operator.
Definition: Interpreter.inl:20
virtual ~Interpreter()
Destructor.
void variable_insert(madara::knowledge::ThreadSafeContext &context, const std::string &input, std::string::size_type &i, int &accumulated_precedence,::std::list< Symbol * > &list, Symbol *&lastValidInput)
Inserts a variable into the tree.