SeExpr
|
main expression class More...
#include <Expression.h>
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 std::string & | parseError () 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 ExprType & | returnType () 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 ExprVarRef * | resolveVar (const std::string &name) const |
virtual ExprFunc * | resolveFunc (const std::string &name) const |
void | addError (const std::string &error, const int startPos, const int endPos) const |
void | addComment (int pos, int length) |
const Context & | context () const |
void | setContext (const Context &context) |
void | debugPrintParseTree () const |
void | debugPrintInterpreter () const |
void | debugPrintLLVM () const |
void | setVarBlockCreator (const VarBlockCreator *varBlockCreator) |
const VarBlockCreator * | varBlockCreator () 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") != 0 |
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) | |
Expression & | operator= (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 |
std::string | _parseError |
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 |
main expression class
Definition at line 76 of file Expression.h.
Types of evaluation strategies that are available.
Enumerator | |
---|---|
UseInterpreter | |
UseLLVM |
Definition at line 79 of file Expression.h.
Expression::Expression | ( | Expression::EvaluationStrategy | evaluationStrategy = Expression::defaultEvaluationStrategy | ) |
Definition at line 85 of file Expression.cpp.
References SeExpr2::ExprFunc::init().
Expression::Expression | ( | const std::string & | e, |
const ExprType & | type = ExprType().FP(3) , |
||
EvaluationStrategy | be = Expression::defaultEvaluationStrategy , |
||
const Context & | context = Context::global() |
||
) |
Definition at line 92 of file Expression.cpp.
References SeExpr2::ExprFunc::init().
|
virtual |
Definition at line 102 of file Expression.cpp.
References _llvmEvaluator, and reset().
|
private |
No definition by design.
|
inline |
|
inline |
records an error in prep or parse stage
Definition at line 205 of file Expression.h.
References _errors.
Referenced by SeExpr2::ExprNode::addError(), parse(), and SeExpr2::DExpression::resolveVar().
|
inline |
add function evaluation (this is for internal use)
Definition at line 321 of file Expression.h.
References _funcs.
Referenced by SeExpr2::ExprFuncNode::ExprFuncNode().
|
inline |
add local variable (this is for internal use)
Definition at line 318 of file Expression.h.
References _vars.
Referenced by SeExpr2::ExprVarNode::prep().
|
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 216 of file Expression.h.
References _context.
Referenced by setContext().
void Expression::debugPrintInterpreter | ( | ) | const |
Debug printout of interpreter evaluation program
Definition at line 107 of file Expression.cpp.
References _interpreter, _returnSlot, and SeExpr2::Interpreter::print().
void Expression::debugPrintLLVM | ( | ) | const |
Debug printout of LLVM evaluation
Definition at line 114 of file Expression.cpp.
References _llvmEvaluator, and SeExpr2::LLVMEvaluator::debugPrint().
void Expression::debugPrintParseTree | ( | ) | const |
Debug printout of parse tree
Definition at line 116 of file Expression.cpp.
References _desiredReturnType, _parseTree, SeExpr2::ExprType::toString(), SeExpr2::ExprNode::type(), and SeExpr2::Walker< constnode >::walk().
Referenced by prep().
const double * Expression::evalFP | ( | VarBlock * | varBlock = nullptr | ) | const |
Evaluates and returns float (check returnType()!)
Definition at line 299 of file Expression.cpp.
References _evaluationStrategy, _interpreter, _isValid, _llvmEvaluator, _returnSlot, SeExpr2::Interpreter::d, SeExpr2::Interpreter::eval(), SeExpr2::LLVMEvaluator::evalFP(), prepIfNeeded(), and UseInterpreter.
Referenced by SeExpr2::DExpression::eval(), evalMultiple(), main(), and ExprGrapherView::update().
void Expression::evalMultiple | ( | VarBlock * | varBlock, |
int | outputVarBlockOffset, | ||
size_t | rangeStart, | ||
size_t | rangeEnd | ||
) | const |
Evaluate multiple blocks.
Definition at line 313 of file Expression.cpp.
References _desiredReturnType, _evaluationStrategy, _isValid, _llvmEvaluator, SeExpr2::VarBlock::data(), SeExpr2::ExprType::dim(), evalFP(), SeExpr2::LLVMEvaluator::evalMultiple(), f(), SeExpr2::VarBlock::indirectIndex, prepIfNeeded(), and UseInterpreter.
const char * Expression::evalStr | ( | VarBlock * | varBlock = nullptr | ) | const |
Evaluates and returns string (check returnType()!)
Definition at line 335 of file Expression.cpp.
References _evaluationStrategy, _interpreter, _isValid, _llvmEvaluator, _returnSlot, SeExpr2::Interpreter::eval(), SeExpr2::LLVMEvaluator::evalStr(), prepIfNeeded(), SeExpr2::Interpreter::s, and UseInterpreter.
Referenced by SeExpr2::DExpression::eval(), and main().
|
inline |
Get a reference to a list of the ranges where comments occurred
Definition at line 147 of file Expression.h.
References _comments.
Referenced by SeExpr2::findComment().
|
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 144 of file Expression.h.
References _errors.
Referenced by ExprDialog::applyExpression().
|
inline |
Get the string that this expression is currently set to evaluate.
Definition at line 122 of file Expression.h.
References _expression.
Referenced by SeExpr2::findComment(), and SeExpr2::ExprNode::toString().
|
inline |
Returns a list of functions that are not threadSafe
Definition at line 169 of file Expression.h.
References _threadUnsafeFunctionCalls.
bool Expression::isConstant | ( | ) | const |
Check if expression is constant. Expr will be parsed if needed. No binding is required.
Definition at line 174 of file Expression.cpp.
References SeExpr2::ExprType::isLifetimeConstant(), parseIfNeeded(), and returnType().
|
inline |
Returns whether the expression contains and calls to non-threadsafe
Definition at line 162 of file Expression.h.
References _threadUnsafeFunctionCalls.
|
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 133 of file Expression.h.
References _isValid, and prepIfNeeded().
Referenced by ExprDialog::applyExpression(), ExprShortEdit::checkErrors(), main(), prep(), ExprGrapherView::update(), ExprDialog::verifiedAccept(), and ExprDialog::verifiedApply().
bool 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 289 of file Expression.cpp.
References _isValid, _parseTree, _wantVec, SeExpr2::ExprNode::isVec(), and prepIfNeeded().
|
private |
|
private |
Parse, and remember parse error if any
Definition at line 189 of file Expression.cpp.
References _comments, _expression, _parsed, _parseError, _parseTree, _wantVec, addError(), and SeExpr2::ExprParse().
Referenced by parseIfNeeded().
|
inline |
Get parse error (if any). First call syntaxOK or isValid to parse (and optionally bind) the expression.
Definition at line 140 of file Expression.h.
References _parseError.
Referenced by ExprShortEdit::checkErrors(), main(), and prep().
|
inlineprivate |
Parse, but only if not yet parsed
Definition at line 242 of file Expression.h.
References _parsed, and parse().
Referenced by isConstant(), prep(), syntaxOK(), usesFunc(), and usesVar().
|
private |
Prepare expression (bind vars/functions, etc.)
and remember error if any
Definition at line 199 of file Expression.cpp.
References _desiredReturnType, _envBuilder, _errors, _evaluationStrategy, _expression, _interpreter, _isValid, _llvmEvaluator, _parseError, _parseTree, _prepped, _returnSlot, _returnType, SeExpr2::ExprNode::addError(), SeExpr2::Interpreter::addOp(), SeExpr2::Interpreter::addOperand(), SeExpr2::Interpreter::allocFP(), SeExpr2::ExprNode::buildInterpreter(), debugging, debugPrintParseTree(), SeExpr2::ExprType::dim(), SeExpr2::Interpreter::endOp(), SeExpr2::ExprType::Error(), SeExpr2::ExprType::isFP(), isValid(), p, parseError(), parseIfNeeded(), SeExpr2::ExprNode::prep(), SeExpr2::LLVMEvaluator::prepLLVM(), SeExpr2::Interpreter::print(), SeExpr2::ExprType::toString(), SeExpr2::ExprNode::type(), UseInterpreter, and SeExpr2::ExprType::valuesCompatible().
Referenced by prepIfNeeded().
|
inlineprotected |
Prepare, but only if not yet prepped
Definition at line 274 of file Expression.h.
References _prepped, and prep().
Referenced by SeExpr2::DExpression::DExpression(), evalFP(), evalMultiple(), evalStr(), isValid(), isVec(), and returnType().
void Expression::reset | ( | ) |
Reset expr - force reparse/rebind
Definition at line 127 of file Expression.cpp.
References _comments, _envBuilder, _errors, _evaluationStrategy, _funcs, _interpreter, _isValid, _llvmEvaluator, _parsed, _parseError, _parseTree, _prepped, _threadUnsafeFunctionCalls, _vars, SeExpr2::ExprVarEnvBuilder::reset(), and UseInterpreter.
Referenced by setContext(), setDesiredReturnType(), setExpr(), setVarBlockCreator(), and ~Expression().
|
inlinevirtual |
override resolveFunc to add external functions
Reimplemented in BasicExpression.
Definition at line 202 of file Expression.h.
Referenced by SeExpr2::ExprFuncNode::prep().
|
inlinevirtual |
override resolveVar to add external variables
Reimplemented in BasicExpression, and SeExpr2::DExpression.
Definition at line 199 of file Expression.h.
Referenced by SeExpr2::ExprVarNode::prep().
const ExprType & 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 294 of file Expression.cpp.
References _returnType, and prepIfNeeded().
Referenced by isConstant().
void Expression::setContext | ( | const Context & | context | ) |
Definition at line 149 of file Expression.cpp.
void Expression::setDesiredReturnType | ( | const ExprType & | type | ) |
Sets desired return value. This will allow the evaluation to potentially be optimized.
Definition at line 154 of file Expression.cpp.
References _desiredReturnType, and reset().
Referenced by ExprGrapherWidget::update().
void Expression::setExpr | ( | const std::string & | e | ) |
Set expression string to e. This invalidates all parsed state.
Definition at line 164 of file Expression.cpp.
References _expression, and reset().
|
inline |
Internal function where parse tree nodes can register violations in thread safety with the main class.
Definition at line 166 of file Expression.h.
References _threadUnsafeFunctionCalls.
void Expression::setVarBlockCreator | ( | const VarBlockCreator * | varBlockCreator | ) |
Set variable block creator (lifetime of expression must be <= block)
Definition at line 159 of file Expression.cpp.
References _varBlockCreator, and reset().
bool 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 169 of file Expression.cpp.
References _isValid, and parseIfNeeded().
bool 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 184 of file Expression.cpp.
References _funcs, and parseIfNeeded().
bool 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 179 of file Expression.cpp.
References _vars, and parseIfNeeded().
|
inline |
Definition at line 231 of file Expression.h.
References _varBlockCreator.
Referenced by SeExpr2::ExprVarNode::prep().
|
inline |
|
mutableprivate |
Cached comments
Definition at line 291 of file Expression.h.
Referenced by addComment(), getComments(), parse(), and reset().
|
private |
Context for out of band function parameters
Definition at line 262 of file Expression.h.
Referenced by context(), and setContext().
|
mutableprotected |
Computed return type.
Definition at line 266 of file Expression.h.
Referenced by debugPrintParseTree(), SeExpr2::DExpression::eval(), evalMultiple(), prep(), and setDesiredReturnType().
|
mutableprotected |
Variable environment
Definition at line 269 of file Expression.h.
|
mutableprivate |
Cached parse error location {startline,startcolumn,endline,endcolumn}
Definition at line 288 of file Expression.h.
Referenced by addError(), getErrors(), prep(), and reset().
|
private |
Definition at line 259 of file Expression.h.
Referenced by evalFP(), evalMultiple(), evalStr(), prep(), and reset().
|
private |
|
mutableprivate |
Functions used in this expr
Definition at line 297 of file Expression.h.
Referenced by addFunc(), reset(), and usesFunc().
|
mutableprivate |
Definition at line 306 of file Expression.h.
Referenced by debugPrintInterpreter(), evalFP(), evalStr(), prep(), and reset().
|
mutableprivate |
Flag if we are valid or not
Definition at line 280 of file Expression.h.
Referenced by evalFP(), evalMultiple(), evalStr(), isValid(), isVec(), prep(), reset(), and syntaxOK().
|
mutableprivate |
Definition at line 310 of file Expression.h.
Referenced by debugPrintLLVM(), evalFP(), evalMultiple(), evalStr(), prep(), reset(), and ~Expression().
|
mutableprivate |
Flag set once expr is parsed/prepped (parsing is automatic and lazy)
Definition at line 282 of file Expression.h.
Referenced by parse(), parseIfNeeded(), and reset().
|
mutableprivate |
Cached parse error (returned by isValid)
Definition at line 285 of file Expression.h.
Referenced by parse(), parseError(), prep(), and reset().
|
mutableprotected |
Parse tree (null if syntax is bad).
Definition at line 271 of file Expression.h.
Referenced by debugPrintParseTree(), isVec(), parse(), prep(), and reset().
|
mutableprivate |
Definition at line 282 of file Expression.h.
Referenced by prep(), prepIfNeeded(), and reset().
|
mutableprivate |
Definition at line 307 of file Expression.h.
Referenced by debugPrintInterpreter(), evalFP(), evalStr(), and prep().
|
mutableprivate |
Computed return type.
Definition at line 254 of file Expression.h.
Referenced by prep(), and returnType().
|
mutableprivate |
Local variable table Whether or not we have unsafe functions
Definition at line 303 of file Expression.h.
Referenced by getThreadUnsafeFunctionCalls(), isThreadSafe(), reset(), and setThreadUnsafe().
|
private |
Definition at line 313 of file Expression.h.
Referenced by setVarBlockCreator(), and varBlockCreator().
|
mutableprivate |
Variables used in this expr
Definition at line 294 of file Expression.h.
|
private |
True if the expression wants a vector
Definition at line 251 of file Expression.h.
|
static |
Whether to debug expressions.
Definition at line 86 of file Expression.h.
Referenced by SeExpr2::ExprFuncStandard::buildInterpreter(), SeExpr2::chooseDefaultEvaluationStrategy(), and prep().
|
static |
What evaluation strategy to use by default.
Definition at line 84 of file Expression.h.