KSeExpr  4.0.4.0
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
KSeExpr::Expression Class Reference

main expression class More...

#include <Expression.h>

Inheritance diagram for KSeExpr::Expression:
BasicExpression KSeExpr::DExpression

Classes

struct  Error
 Represents a parse or type checking error in an expression. More...
 

Public Types

enum  EvaluationStrategy { UseInterpreter , UseLLVM }
 Types of evaluation strategies that are available. More...
 

Public Member Functions

 Expression (EvaluationStrategy be=Expression::defaultEvaluationStrategy)
 
 Expression (const std::string &e, const ExprType &type=ExprType().FP(3), EvaluationStrategy be=Expression::defaultEvaluationStrategy, const Context &context=Context::global())
 
virtual ~Expression ()
 
void setDesiredReturnType (const ExprType &type)
 
void setExpr (const std::string &e)
 
const std::string & getExpr () const
 Get the string that this expression is currently set to evaluate. More...
 
bool syntaxOK () const
 
bool isValid () const
 
const ErrorCodeparseError () const
 
const std::vector< std::string > & parseErrorArgs () const
 
const std::vector< Error > & getErrors () const
 
const std::vector< std::pair< int, int > > & getComments () const
 
bool isConstant () const
 
bool usesVar (const std::string &name) const
 
bool usesFunc (const std::string &name) const
 
bool isThreadSafe () const
 
void setThreadUnsafe (const std::string &functionName) const
 
const std::vector< std::string > & getThreadUnsafeFunctionCalls () const
 
bool wantVec () const
 
bool isVec () const
 
const ExprTypereturnType () const
 
void evalMultiple (VarBlock *varBlock, int outputVarBlockOffset, size_t rangeStart, size_t rangeEnd) const
 Evaluate multiple blocks. More...
 
const double * evalFP (VarBlock *varBlock=nullptr) const
 
const char * evalStr (VarBlock *varBlock=nullptr) const
 
void reset ()
 
virtual ExprVarRefresolveVar (const std::string &) const
 
virtual ExprFuncresolveFunc (const std::string &) const
 
void addError (const ErrorCode error, const std::vector< std::string > ids, const int startPos, const int endPos) const
 
void addComment (int pos, int length)
 
const Contextcontext () const
 
void setContext (const Context &context)
 
void debugPrintParseTree () const
 
void debugPrintInterpreter () const
 
void debugPrintLLVM () const
 
void setVarBlockCreator (const VarBlockCreator *varBlockCreator)
 
const VarBlockCreatorvarBlockCreator () const
 
void addVar (const char *n) const
 add local variable (this is for internal use) More...
 
void addFunc (const char *n) const
 add function evaluation (this is for internal use) More...
 

Static Public Attributes

static EvaluationStrategy defaultEvaluationStrategy = chooseDefaultEvaluationStrategy()
 What evaluation strategy to use by default. More...
 
static bool debugging = getenv("SE_EXPR_DEBUG") != nullptr
 Whether to debug expressions. More...
 

Protected Member Functions

void prepIfNeeded () const
 

Protected Attributes

ExprType _desiredReturnType
 
ExprVarEnvBuilder _envBuilder
 
ExprNode_parseTree
 

Private Member Functions

 Expression (const Expression &e)
 
Expressionoperator= (const Expression &e)
 
void parse () const
 
void parseIfNeeded () const
 
void prep () const
 

Private Attributes

bool _wantVec
 
ExprType _returnType
 
std::string _expression
 
EvaluationStrategy _evaluationStrategy
 
const Context_context
 
bool _isValid
 
bool _parsed
 
bool _prepped
 
ErrorCode _parseErrorCode
 
std::vector< std::string > _parseErrorIds
 
std::vector< Error_errors
 
std::vector< std::pair< int, int > > _comments
 
std::set< std::string > _vars
 
std::set< std::string > _funcs
 
std::vector< std::string > _threadUnsafeFunctionCalls
 
Interpreter_interpreter
 
int _returnSlot
 
LLVMEvaluator_llvmEvaluator
 
const VarBlockCreator_varBlockCreator = 0
 

Detailed Description

main expression class

Definition at line 67 of file Expression.h.

Member Enumeration Documentation

◆ EvaluationStrategy

Types of evaluation strategies that are available.

Enumerator
UseInterpreter 
UseLLVM 

Definition at line 70 of file Expression.h.

Constructor & Destructor Documentation

◆ Expression() [1/3]

KSeExpr::Expression::Expression ( Expression::EvaluationStrategy  evaluationStrategy = Expression::defaultEvaluationStrategy)

Definition at line 54 of file Expression.cpp.

References KSeExpr::ExprFunc::init().

◆ Expression() [2/3]

KSeExpr::Expression::Expression ( const std::string &  e,
const ExprType type = ExprType().FP(3),
EvaluationStrategy  be = Expression::defaultEvaluationStrategy,
const Context context = Context::global() 
)

Definition at line 61 of file Expression.cpp.

References KSeExpr::ExprFunc::init().

◆ ~Expression()

KSeExpr::Expression::~Expression ( )
virtual

Definition at line 71 of file Expression.cpp.

References _llvmEvaluator, and reset().

◆ Expression() [3/3]

KSeExpr::Expression::Expression ( const Expression e)
private

No definition by design.

Member Function Documentation

◆ addComment()

void KSeExpr::Expression::addComment ( int  pos,
int  length 
)
inline

records a comment

Definition at line 212 of file Expression.h.

References _comments, and KSeExpr::length().

◆ addError()

void KSeExpr::Expression::addError ( const ErrorCode  error,
const std::vector< std::string >  ids,
const int  startPos,
const int  endPos 
) const
inline

records an error in prep or parse stage

Definition at line 207 of file Expression.h.

References _errors.

Referenced by parse(), and KSeExpr::DExpression::resolveVar().

◆ addFunc()

void KSeExpr::Expression::addFunc ( const char *  n) const
inline

add function evaluation (this is for internal use)

Definition at line 326 of file Expression.h.

References _funcs.

Referenced by KSeExpr::ExprFuncNode::ExprFuncNode().

◆ addVar()

void KSeExpr::Expression::addVar ( const char *  n) const
inline

add local variable (this is for internal use)

Definition at line 323 of file Expression.h.

References _vars.

Referenced by KSeExpr::ExprVarNode::prep().

◆ context()

const Context& KSeExpr::Expression::context ( ) const
inline

Returns a read only map of local variables that were set An immutable reference to access context parameters from say ExprFuncX's

Definition at line 218 of file Expression.h.

References _context.

Referenced by setContext().

◆ debugPrintInterpreter()

void KSeExpr::Expression::debugPrintInterpreter ( ) const

Debug printout of interpreter evaluation program

Definition at line 76 of file Expression.cpp.

References _interpreter, _returnSlot, and KSeExpr::Interpreter::print().

◆ debugPrintLLVM()

void KSeExpr::Expression::debugPrintLLVM ( ) const

Debug printout of LLVM evaluation

Definition at line 83 of file Expression.cpp.

References _llvmEvaluator, and KSeExpr::LLVMEvaluator::debugPrint().

◆ debugPrintParseTree()

void KSeExpr::Expression::debugPrintParseTree ( ) const

Debug printout of parse tree

Definition at line 85 of file Expression.cpp.

References _desiredReturnType, _parseTree, KSeExpr::ExprType::toString(), KSeExpr::ExprNode::type(), and KSeExpr::Walker< constnode >::walk().

Referenced by prep().

◆ evalFP()

const double * KSeExpr::Expression::evalFP ( VarBlock varBlock = nullptr) const

◆ evalMultiple()

void KSeExpr::Expression::evalMultiple ( VarBlock varBlock,
int  outputVarBlockOffset,
size_t  rangeStart,
size_t  rangeEnd 
) const

◆ evalStr()

const char * KSeExpr::Expression::evalStr ( VarBlock varBlock = nullptr) const

◆ getComments()

const std::vector<std::pair<int, int> >& KSeExpr::Expression::getComments ( ) const
inline

Get a reference to a list of the ranges where comments occurred

Definition at line 149 of file Expression.h.

References _comments.

Referenced by KSeExpr::findComment().

◆ getErrors()

const std::vector<Error>& KSeExpr::Expression::getErrors ( ) const
inline

Get a reference to a list of parse errors in the expression. The error structure gives location information as well as the errors itself.

Definition at line 146 of file Expression.h.

References _errors.

◆ getExpr()

const std::string& KSeExpr::Expression::getExpr ( ) const
inline

Get the string that this expression is currently set to evaluate.

Definition at line 116 of file Expression.h.

References _expression.

Referenced by KSeExpr::findComment(), and KSeExpr::ExprNode::toString().

◆ getThreadUnsafeFunctionCalls()

const std::vector<std::string>& KSeExpr::Expression::getThreadUnsafeFunctionCalls ( ) const
inline

Returns a list of functions that are not threadSafe

Definition at line 171 of file Expression.h.

References _threadUnsafeFunctionCalls.

◆ isConstant()

bool KSeExpr::Expression::isConstant ( ) const

Check if expression is constant. Expr will be parsed if needed. No binding is required.

Definition at line 144 of file Expression.cpp.

References KSeExpr::ExprType::isLifetimeConstant(), parseIfNeeded(), and returnType().

◆ isThreadSafe()

bool KSeExpr::Expression::isThreadSafe ( ) const
inline

Returns whether the expression contains and calls to non-threadsafe

Definition at line 164 of file Expression.h.

References _threadUnsafeFunctionCalls.

◆ isValid()

bool KSeExpr::Expression::isValid ( ) const
inline

Check if expression is valid. Expr will be parsed if needed. Variables and functions will also be bound. If this returns false, the error message can be accessed via parseError()

Definition at line 127 of file Expression.h.

References _isValid, and prepIfNeeded().

◆ isVec()

bool KSeExpr::Expression::isVec ( ) const

Determine if expression computes a vector (may be false even if wantVec is true). Expr will be parsed and variables and functions will be bound if needed.

Definition at line 259 of file Expression.cpp.

References _isValid, _parseTree, _wantVec, KSeExpr::ExprNode::isVec(), and prepIfNeeded().

◆ operator=()

Expression& KSeExpr::Expression::operator= ( const Expression e)
private

◆ parse()

void KSeExpr::Expression::parse ( ) const
private

Parse, and remember parse error if any

Definition at line 159 of file Expression.cpp.

References _comments, _expression, _parsed, _parseErrorCode, _parseErrorIds, _parseTree, _wantVec, addError(), and KSeExpr::ExprParse().

Referenced by parseIfNeeded().

◆ parseError()

const ErrorCode& KSeExpr::Expression::parseError ( ) const
inline

Get parse error (if any). First call syntaxOK or isValid to parse (and optionally bind) the expression.

Definition at line 134 of file Expression.h.

References _parseErrorCode.

◆ parseErrorArgs()

const std::vector<std::string>& KSeExpr::Expression::parseErrorArgs ( ) const
inline

Get parse error message's arguments. First call syntaxOK or isValid to parse (and optionally bind) the expression. NOTE: These strings are NOT translatable.

Definition at line 142 of file Expression.h.

References _parseErrorIds.

◆ parseIfNeeded()

void KSeExpr::Expression::parseIfNeeded ( ) const
inlineprivate

Parse, but only if not yet parsed

Definition at line 244 of file Expression.h.

References _parsed, and parse().

Referenced by isConstant(), prep(), syntaxOK(), usesFunc(), and usesVar().

◆ prep()

void KSeExpr::Expression::prep ( ) const
private

◆ prepIfNeeded()

void KSeExpr::Expression::prepIfNeeded ( ) const
inlineprotected

Prepare, but only if not yet prepped

Definition at line 276 of file Expression.h.

References _prepped, and prep().

Referenced by KSeExpr::DExpression::DExpression(), evalFP(), evalMultiple(), evalStr(), isValid(), isVec(), and returnType().

◆ reset()

void KSeExpr::Expression::reset ( )

◆ resolveFunc()

virtual ExprFunc* KSeExpr::Expression::resolveFunc ( const std::string &  ) const
inlinevirtual

override resolveFunc to add external functions

Reimplemented in BasicExpression.

Definition at line 204 of file Expression.h.

Referenced by KSeExpr::ExprFuncNode::prep().

◆ resolveVar()

virtual ExprVarRef* KSeExpr::Expression::resolveVar ( const std::string &  ) const
inlinevirtual

override resolveVar to add external variables

Reimplemented in BasicExpression, and KSeExpr::DExpression.

Definition at line 201 of file Expression.h.

Referenced by KSeExpr::ExprVarNode::prep().

◆ returnType()

const ExprType & KSeExpr::Expression::returnType ( ) const

Return the return type of the expression. Currently may not match the type set in setReturnType. Expr will be parsed and variables and functions will be bound if needed.

Definition at line 264 of file Expression.cpp.

References _returnType, and prepIfNeeded().

Referenced by isConstant().

◆ setContext()

void KSeExpr::Expression::setContext ( const Context context)

Definition at line 119 of file Expression.cpp.

References _context, context(), and reset().

◆ setDesiredReturnType()

void KSeExpr::Expression::setDesiredReturnType ( const ExprType type)

Sets desired return value. This will allow the evaluation to potentially be optimized.

Definition at line 124 of file Expression.cpp.

References _desiredReturnType, and reset().

◆ setExpr()

void KSeExpr::Expression::setExpr ( const std::string &  e)

Set expression string to e. This invalidates all parsed state.

Definition at line 134 of file Expression.cpp.

References _expression, and reset().

◆ setThreadUnsafe()

void KSeExpr::Expression::setThreadUnsafe ( const std::string &  functionName) const
inline

Internal function where parse tree nodes can register violations in thread safety with the main class.

Definition at line 168 of file Expression.h.

References _threadUnsafeFunctionCalls.

◆ setVarBlockCreator()

void KSeExpr::Expression::setVarBlockCreator ( const VarBlockCreator varBlockCreator)

Set variable block creator (lifetime of expression must be <= block)

Definition at line 129 of file Expression.cpp.

References _varBlockCreator, and reset().

◆ syntaxOK()

bool KSeExpr::Expression::syntaxOK ( ) const

Check expression syntax. Expr will be parsed if needed. If this returns false, the error message can be accessed via parseError()

Definition at line 139 of file Expression.cpp.

References _isValid, and parseIfNeeded().

◆ usesFunc()

bool KSeExpr::Expression::usesFunc ( const std::string &  name) const

Determine whether expression uses a particular function. Expr will be parsed if needed. No binding is required.

Definition at line 154 of file Expression.cpp.

References _funcs, and parseIfNeeded().

◆ usesVar()

bool KSeExpr::Expression::usesVar ( const std::string &  name) const

Determine whether expression uses a particular external variable. Expr will be parsed if needed. No binding is required.

Definition at line 149 of file Expression.cpp.

References _vars, and parseIfNeeded().

◆ varBlockCreator()

const VarBlockCreator* KSeExpr::Expression::varBlockCreator ( ) const
inline

Definition at line 233 of file Expression.h.

References _varBlockCreator.

Referenced by KSeExpr::ExprVarNode::prep().

◆ wantVec()

bool KSeExpr::Expression::wantVec ( ) const
inline

Get wantVec setting

Definition at line 174 of file Expression.h.

References _wantVec.

Member Data Documentation

◆ _comments

std::vector<std::pair<int, int> > KSeExpr::Expression::_comments
mutableprivate

Cached comments

Definition at line 296 of file Expression.h.

Referenced by addComment(), getComments(), parse(), and reset().

◆ _context

const Context* KSeExpr::Expression::_context
private

Context for out of band function parameters

Definition at line 264 of file Expression.h.

Referenced by context(), and setContext().

◆ _desiredReturnType

ExprType KSeExpr::Expression::_desiredReturnType
mutableprotected

Computed return type.

Definition at line 268 of file Expression.h.

Referenced by debugPrintParseTree(), KSeExpr::DExpression::eval(), evalMultiple(), prep(), and setDesiredReturnType().

◆ _envBuilder

ExprVarEnvBuilder KSeExpr::Expression::_envBuilder
mutableprotected

Variable environment

Definition at line 271 of file Expression.h.

Referenced by prep(), and reset().

◆ _errors

std::vector<Error> KSeExpr::Expression::_errors
mutableprivate

Cached parse error location {startline,startcolumn,endline,endcolumn}

Definition at line 293 of file Expression.h.

Referenced by addError(), getErrors(), prep(), and reset().

◆ _evaluationStrategy

EvaluationStrategy KSeExpr::Expression::_evaluationStrategy
private

Definition at line 261 of file Expression.h.

Referenced by evalFP(), evalMultiple(), evalStr(), prep(), and reset().

◆ _expression

std::string KSeExpr::Expression::_expression
private

The expression.

Definition at line 259 of file Expression.h.

Referenced by getExpr(), parse(), prep(), and setExpr().

◆ _funcs

std::set<std::string> KSeExpr::Expression::_funcs
mutableprivate

Functions used in this expr

Definition at line 302 of file Expression.h.

Referenced by addFunc(), reset(), and usesFunc().

◆ _interpreter

Interpreter* KSeExpr::Expression::_interpreter
mutableprivate

Interpreter

Definition at line 311 of file Expression.h.

Referenced by debugPrintInterpreter(), evalFP(), evalStr(), prep(), and reset().

◆ _isValid

bool KSeExpr::Expression::_isValid
mutableprivate

Flag if we are valid or not

Definition at line 282 of file Expression.h.

Referenced by evalFP(), evalMultiple(), evalStr(), isValid(), isVec(), prep(), reset(), and syntaxOK().

◆ _llvmEvaluator

LLVMEvaluator* KSeExpr::Expression::_llvmEvaluator
mutableprivate

Definition at line 315 of file Expression.h.

Referenced by debugPrintLLVM(), evalFP(), evalMultiple(), evalStr(), prep(), reset(), and ~Expression().

◆ _parsed

bool KSeExpr::Expression::_parsed
mutableprivate

Flag set once expr is parsed/prepped (parsing is automatic and lazy)

Definition at line 284 of file Expression.h.

Referenced by parse(), parseIfNeeded(), and reset().

◆ _parseErrorCode

ErrorCode KSeExpr::Expression::_parseErrorCode
mutableprivate

Cached parse error (returned by isValid)

Definition at line 287 of file Expression.h.

Referenced by parse(), parseError(), and reset().

◆ _parseErrorIds

std::vector<std::string> KSeExpr::Expression::_parseErrorIds
mutableprivate

Cached parse error components (returned by isValid)

Definition at line 290 of file Expression.h.

Referenced by parse(), parseErrorArgs(), and reset().

◆ _parseTree

ExprNode* KSeExpr::Expression::_parseTree
mutableprotected

Parse tree (null if syntax is bad).

Definition at line 273 of file Expression.h.

Referenced by debugPrintParseTree(), isVec(), parse(), prep(), and reset().

◆ _prepped

bool KSeExpr::Expression::_prepped
private

Definition at line 284 of file Expression.h.

Referenced by prep(), prepIfNeeded(), and reset().

◆ _returnSlot

int KSeExpr::Expression::_returnSlot
mutableprivate

Definition at line 312 of file Expression.h.

Referenced by debugPrintInterpreter(), evalFP(), evalStr(), and prep().

◆ _returnType

ExprType KSeExpr::Expression::_returnType
mutableprivate

Computed return type.

Definition at line 256 of file Expression.h.

Referenced by prep(), and returnType().

◆ _threadUnsafeFunctionCalls

std::vector<std::string> KSeExpr::Expression::_threadUnsafeFunctionCalls
mutableprivate

Local variable table Whether or not we have unsafe functions

Definition at line 308 of file Expression.h.

Referenced by getThreadUnsafeFunctionCalls(), isThreadSafe(), reset(), and setThreadUnsafe().

◆ _varBlockCreator

const VarBlockCreator* KSeExpr::Expression::_varBlockCreator = 0
private

Definition at line 318 of file Expression.h.

Referenced by setVarBlockCreator(), and varBlockCreator().

◆ _vars

std::set<std::string> KSeExpr::Expression::_vars
mutableprivate

Variables used in this expr

Definition at line 299 of file Expression.h.

Referenced by addVar(), reset(), and usesVar().

◆ _wantVec

bool KSeExpr::Expression::_wantVec
private

True if the expression wants a vector

Definition at line 253 of file Expression.h.

Referenced by isVec(), parse(), and wantVec().

◆ debugging

bool KSeExpr::Expression::debugging = getenv("SE_EXPR_DEBUG") != nullptr
static

Whether to debug expressions.

Definition at line 77 of file Expression.h.

Referenced by KSeExpr::ExprFuncStandard::buildInterpreter(), KSeExpr::chooseDefaultEvaluationStrategy(), and prep().

◆ defaultEvaluationStrategy

Expression::EvaluationStrategy KSeExpr::Expression::defaultEvaluationStrategy = chooseDefaultEvaluationStrategy()
static

What evaluation strategy to use by default.

Definition at line 75 of file Expression.h.


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