MADARA  3.2.3
madara::expression::Interpreter Class Reference

Parses incoming expression strings into a parse tree and generates an expression tree from the parse tree. More...

#include <Interpreter.h>

Public Member Functions

 Interpreter ()
 Constructor. More...
 
virtual ~Interpreter ()
 Destructor. More...
 
bool delete_expression (const std::string &expression)
 Attempts to delete an expression from cache. More...
 
ExpressionTree interpret (madara::knowledge::ThreadSafeContext &context, const std::string &input)
 Compiles an expression into an expression tree. More...
 

Static Public Member Functions

static bool is_alphanumeric (char input)
 Checks a character to see if it is alphanumeric. More...
 
static bool is_exponential (char input)
 Checks a character to see if it is in scientific format. More...
 
static bool is_number (char input)
 Checks a character to see if it is a number. More...
 
static bool is_operator (char input)
 Checks a character to see if it is a mathematical operator. More...
 
static bool is_reserved_word (const std::string &input)
 Checks a given input for a reserved word literal. More...
 
static bool is_string_literal (char input)
 Checks a character to see if it is a string literal. More...
 
static bool is_whitespace (char input)
 Checks a character to see if it is whitespace. More...
 

Private Member Functions

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. More...
 
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 More...
 
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. More...
 
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. More...
 
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. More...
 
void precedence_insert (madara::knowledge::ThreadSafeContext &context, Symbol *op,::std::list< Symbol * > &list)
 Inserts a mathematical operator into the tree. More...
 
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. More...
 
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. More...
 
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. More...
 

Private Attributes

ExpressionTreeMap cache_
 Cache of expressions that have been previously compiled. More...
 

Detailed Description

Parses incoming expression strings into a parse tree and generates an expression tree from the parse tree.

This class plays the role of the "interpreter" in the Intepreter pattern. It also uses the Builder pattern to generate the nodes in the expression tree.

Definition at line 42 of file Interpreter.h.

Constructor & Destructor Documentation

madara::expression::Interpreter::Interpreter ( )

Constructor.

Definition at line 5069 of file Interpreter.cpp.

madara::expression::Interpreter::~Interpreter ( )
virtual

Destructor.

Definition at line 5074 of file Interpreter.cpp.

Member Function Documentation

bool madara::expression::Interpreter::delete_expression ( const std::string &  expression)
inline

Attempts to delete an expression from cache.

Parameters
expressionexpression to erase from cache
Returns
true if the expression was deleted

Definition at line 82 of file Interpreter.inl.

void madara::expression::Interpreter::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 
)
private

Handles a parenthesis.

Parameters
contextinterpreter context
inputexpression to compile
icurrent position in expression
lastValidInputlast valid symbol that was read
listlist of symbols in tree that are free
handledwhether or not the pos should be incremented
accumulated_precedencecurrent precedence

Definition at line 6799 of file Interpreter.cpp.

void madara::expression::Interpreter::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 
)
private

extracts precondition, condition, postcondition, and body from input

Parameters
contextinterpreter context
variablename of the variable the for loop is based on
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols to insert for loop into
returnableInputused to interact with lastValidInput if an array reference is found

if we manage to find a delimiter but not its end, we're seeing an array reference with subtraction in it

Definition at line 5080 of file Interpreter.cpp.

void madara::expression::Interpreter::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 
)
private

Handles a parenthesis.

Parameters
contextinterpreter context
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read
handledwhether or not the pos should be incremented
build_argument_listif true, focuses on building a list instead of an expression tree

Definition at line 6878 of file Interpreter.cpp.

madara::expression::ExpressionTree madara::expression::Interpreter::interpret ( madara::knowledge::ThreadSafeContext context,
const std::string &  input 
)

Compiles an expression into an expression tree.

Parameters
contextinterpreter context
inputexpression to compile
Returns
expression tree to evaluate
Exceptions
KarlExceptionfailure during interpret

Definition at line 6977 of file Interpreter.cpp.

bool madara::expression::Interpreter::is_alphanumeric ( char  input)
inlinestatic

Checks a character to see if it is alphanumeric.

Parameters
inputchar to check
Returns
true if the input is alphanumeric

Definition at line 60 of file Interpreter.inl.

bool madara::expression::Interpreter::is_exponential ( char  input)
inlinestatic

Checks a character to see if it is in scientific format.

Parameters
inputchar to check
Returns
true if the input is in scientific exponential format

Definition at line 37 of file Interpreter.inl.

bool madara::expression::Interpreter::is_number ( char  input)
inlinestatic

Checks a character to see if it is a number.

Parameters
inputchar to check
Returns
true if the input is a number

Definition at line 30 of file Interpreter.inl.

bool madara::expression::Interpreter::is_operator ( char  input)
inlinestatic

Checks a character to see if it is a mathematical operator.

Parameters
inputchar to check
Returns
true if the input is an operator

Definition at line 20 of file Interpreter.inl.

bool madara::expression::Interpreter::is_reserved_word ( const std::string &  input)
inlinestatic

Checks a given input for a reserved word literal.

Parameters
inputstring to check
Returns
true if the input is an operator

Definition at line 44 of file Interpreter.inl.

bool madara::expression::Interpreter::is_string_literal ( char  input)
inlinestatic

Checks a character to see if it is a string literal.

Parameters
inputchar to check
Returns
true if the input is an operator

Definition at line 52 of file Interpreter.inl.

bool madara::expression::Interpreter::is_whitespace ( char  input)
inlinestatic

Checks a character to see if it is whitespace.

Parameters
inputchar to check
Returns
true if the input is whitespace

Definition at line 71 of file Interpreter.inl.

void madara::expression::Interpreter::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 
)
private

Inserts a variable into the tree.

Parameters
contextinterpreter context
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read
handledwhether or not the pos should be incremented
build_argument_listif true, focuses on building a list instead of an expression tree

Definition at line 6282 of file Interpreter.cpp.

void madara::expression::Interpreter::number_insert ( madara::knowledge::ThreadSafeContext context,
const std::string &  input,
std::string::size_type &  i,
int &  accumulated_precedence,
::std::list< Symbol * > &  list,
Symbol *&  lastValidInput 
)
private

Inserts a number into the tree.

Parameters
contextthread safe context handle
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read

Definition at line 6020 of file Interpreter.cpp.

void madara::expression::Interpreter::precedence_insert ( madara::knowledge::ThreadSafeContext context,
Symbol op,
::std::list< Symbol * > &  list 
)
private

Inserts a mathematical operator into the tree.

Parameters
contextthread safe context handle
opsymbol to insert
listlist of symbols in tree that are free

Definition at line 6097 of file Interpreter.cpp.

void madara::expression::Interpreter::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 
)
private

Inserts a variable into the tree.

Parameters
openerthe opening string
contextinterpreter context
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read

Definition at line 5687 of file Interpreter.cpp.

void madara::expression::Interpreter::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 
)
private

Inserts a system call into the tree.

Parameters
contextinterpreter context
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read

Definition at line 5719 of file Interpreter.cpp.

void madara::expression::Interpreter::variable_insert ( madara::knowledge::ThreadSafeContext context,
const std::string &  input,
std::string::size_type &  i,
int &  accumulated_precedence,
::std::list< Symbol * > &  list,
Symbol *&  lastValidInput 
)
private

Inserts a variable into the tree.

Parameters
contextinterpreter context
inputexpression to compile
icurrent position in expression
accumulated_precedencecurrent precedence
listlist of symbols in tree that are free
lastValidInputlast valid symbol that was read

Definition at line 5534 of file Interpreter.cpp.

Member Data Documentation

ExpressionTreeMap madara::expression::Interpreter::cache_
private

Cache of expressions that have been previously compiled.

Definition at line 288 of file Interpreter.h.


The documentation for this class was generated from the following files: