MADARA  3.2.3
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 
64  const std::string &input);
65 
71  static inline bool is_string_literal (char input);
72 
78  static inline bool is_operator (char input);
79 
85  static inline bool is_number (char input);
86 
92  static inline bool is_alphanumeric (char input);
93 
99  static inline bool is_whitespace (char input);
100 
106  static inline bool is_exponential (char input);
107 
113  static inline bool is_reserved_word (const std::string & input);
114 
120  inline bool delete_expression (const std::string & expression);
121 
122  private:
134  void handle_for_loop (
136  std::string &variable,
137  const std::string &input,
138  std::string::size_type &i,
139  int & accumulated_precedence,
140  ::std::list<Symbol *>& list,
141  Symbol *& returnableInput);
142 
152  void variable_insert (
154  const std::string &input,
155  std::string::size_type &i,
156  int & accumulated_precedence,
157  ::std::list<Symbol *>& list,
158  Symbol *& lastValidInput);
159 
170  void string_insert (
171  char opener,
173  const std::string &input,
174  std::string::size_type &i,
175  int & accumulated_precedence,
176  ::std::list<Symbol *>& list,
177  Symbol *& lastValidInput);
178 
188  void system_call_insert (
190  const std::string &input,
191  std::string::size_type &i,
192  int & accumulated_precedence,
193  ::std::list<Symbol *>& list,
194  Symbol *& lastValidInput);
195 
205  void number_insert (
207  const std::string &input,
208  std::string::size_type &i,
209  int & accumulated_precedence,
210  ::std::list<Symbol *>& list,
211  Symbol *& lastValidInput);
212 
219  void precedence_insert (
221  Symbol *op, ::std::list<Symbol *>& list);
222 
236  const std::string &input,
237  std::string::size_type &i,
238  Symbol *& lastValidInput,
239  bool & handled,
240  int & accumulated_precedence,
241  ::std::list<Symbol *>& list,
242  bool build_argument_list = false);
243 
244 
255  void handle_array (
257  const std::string &input,
258  std::string::size_type &i,
259  Symbol *& lastValidInput,
260  bool & handled,
261  int & accumulated_precedence,
262  ::std::list<Symbol *>& list);
263 
276  void handle_parenthesis (
278  const std::string &input,
279  std::string::size_type &i,
280  Symbol *& lastValidInput,
281  bool & handled,
282  int & accumulated_precedence,
283  ::std::list<Symbol *>& list,
284  bool build_argument_list = false);
288  ExpressionTreeMap cache_;
289  };
290  }
291 }
292 
293 #include "Interpreter.inl"
294 
295 #endif // _MADARA_NO_KARL_
296 
297 #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.
static bool is_reserved_word(const std::string &input)
Checks a given input for a reserved word literal.
Definition: Interpreter.inl:44
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:60
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 struct madara::knowledge::tags::string_t string
static bool is_whitespace(char input)
Checks a character to see if it is whitespace.
Definition: Interpreter.inl:71
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:288
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.
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:52
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
Definition: Interpreter.inl:82
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.