15 #include "ExprConfig.h"
33 std::cerr <<
"KSeExpr Debug Mode Enabled " <<
41 #if defined(SEEXPR_ENABLE_LLVM)
42 if (
char* env = getenv(
"SE_EXPR_EVAL")) {
43 if (
Expression::debugging) std::cerr <<
"Overriding SeExpr Evaluation Default to be " << env << std::endl;
55 : _wantVec(true), _expression(
""), _evaluationStrategy(evaluationStrategy), _context(&
Context::global()),
56 _desiredReturnType(
ExprType().FP(3).Varying()), _parseTree(nullptr), _isValid(false), _parsed(false), _prepped(false),
65 : _wantVec(true), _expression(e), _evaluationStrategy(evaluationStrategy), _context(&context),
66 _desiredReturnType(type), _parseTree(nullptr), _isValid(false), _parsed(false), _prepped(false), _interpreter(nullptr),
79 std::cerr <<
"return slot " <<
_returnSlot << std::endl;
162 int tempStartPos, tempEndPos;
171 #ifdef SEEXPR_PERFORMANCE
178 std::string _parseError;
196 std::cerr <<
"Eval strategy is interpreter" << std::endl;
204 if (dimWanted > dimHave) {
216 std::cerr <<
"Eval strategy is llvm" << std::endl;
233 std::vector<int> lines;
237 if (*
p ==
'\n') lines.push_back(
static_cast<int>(
p - start));
240 lines.push_back(
static_cast<int>(
p - start));
242 std::stringstream sstream;
243 for (
unsigned int i = 0; i <
_errors.size(); i++) {
244 int* bound = std::lower_bound(&*lines.begin(), &*lines.end(),
_errors[i].startPos);
245 int line =
static_cast<int>(bound - &*lines.begin() + 1);
246 int lineStart = line == 1 ? 0 : lines[line - 1];
247 int col =
_errors[i].startPos - lineStart;
248 sstream <<
" Line " << line <<
" Col " << col <<
" - " <<
_errors[i].error << std::endl;
250 _parseError = std::string(sstream.str());
254 std::cerr <<
"ending with isValid " <<
_isValid << std::endl;
255 std::cerr <<
"parse error \n" << _parseError << std::endl;
279 static double noCrash[16] = {};
290 double* destBase =
reinterpret_cast<double**
>(varBlock->
data())[outputVarBlockOffset];
291 for (
size_t i = rangeStart; i < rangeEnd; i++) {
293 const double* f =
evalFP(varBlock);
294 for (
int k = 0; k < dim; k++) {
295 destBase[dim * i + k] = f[k];
static constexpr std::array< int, 514 > p
static void init()
call to define built-in funcs
virtual ExprType prep(bool dontNeedScalar, ExprVarEnvBuilder &envBuilder)
virtual int buildInterpreter(Interpreter *interpreter) const
builds an interpreter. Returns the location index for the evaluated data
const ExprType & type() const
The type of the node.
void addError(const ErrorCode error, const std::vector< std::string > &ids={}) const
Register error. This will allow users and sophisticated editors to highlight where in code problem wa...
bool isVec() const
True if node has a vector result.
bool isLifetimeConstant() const
validity check: type is not an error
std::string toString() const
Stringify the type into a printable string.
bool isFP() const
Direct is predicate checks.
ExprType & Error()
Mutate this into an error type.
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
void reset()
Reset to factory state (one empty environment that is current)
const VarBlockCreator * _varBlockCreator
void evalMultiple(VarBlock *varBlock, int outputVarBlockOffset, size_t rangeStart, size_t rangeEnd) const
Evaluate multiple blocks.
ErrorCode _parseErrorCode
void setExpr(const std::string &e)
static bool debugging
Whether to debug expressions.
std::set< std::string > _vars
bool usesFunc(const std::string &name) const
void parseIfNeeded() const
EvaluationStrategy _evaluationStrategy
Expression(EvaluationStrategy be=Expression::defaultEvaluationStrategy)
void addError(const ErrorCode error, const std::vector< std::string > ids, const int startPos, const int endPos) const
std::set< std::string > _funcs
bool usesVar(const std::string &name) const
Interpreter * _interpreter
std::vector< std::string > _parseErrorIds
const double * evalFP(VarBlock *varBlock=nullptr) const
void prepIfNeeded() const
EvaluationStrategy
Types of evaluation strategies that are available.
void debugPrintParseTree() const
void setContext(const Context &context)
ExprVarEnvBuilder _envBuilder
void debugPrintInterpreter() const
const ExprType & returnType() const
std::vector< std::pair< int, int > > _comments
const Context & context() const
const char * evalStr(VarBlock *varBlock=nullptr) const
std::vector< Error > _errors
void setVarBlockCreator(const VarBlockCreator *varBlockCreator)
ExprType _desiredReturnType
std::vector< std::string > _threadUnsafeFunctionCalls
void setDesiredReturnType(const ExprType &type)
static EvaluationStrategy defaultEvaluationStrategy
What evaluation strategy to use by default.
void debugPrintLLVM() const
LLVMEvaluator * _llvmEvaluator
std::vector< double > d
Double data (constants and evaluated)
int addOp(OpF op)
! adds an operator to the program (pointing to the data at the current location)
void eval(VarBlock *varBlock, bool debug=false)
Evaluate program.
void endOp(bool execute=true)
std::vector< char * > s
constant and evaluated pointer data
int allocFP(int n)
! Allocate a floating point set of data of dimension n
int addOperand(int param)
! Adds an operand. Note this should be done after doing the addOp!
void print(int pc=-1) const
Debug by printing program.
static bool prepLLVM(ExprNode *, ExprType)
static const char * evalStr(VarBlock *)
static const double * evalFP(VarBlock *)
static void evalMultiple(VarBlock *, int, size_t, size_t)
A class that lets you register for the variables used by one or more expressions.
A thread local evaluation context. Just allocate and fill in with data.
int indirectIndex
indirect index to add to pointer based data
std::vector< char * > s
copy of Interpreter's str data
std::vector< double > d
copy of Interpreter's double data
char ** data()
Raw data of the data block pointer (used by compiler)
bool threadSafe
if true, interpreter's data will be copied to this instance before evaluation.
void walk(T_NODE *examinee)
Preorder walk.
static Expression::EvaluationStrategy chooseDefaultEvaluationStrategy()
bool ExprParse(KSeExpr::ExprNode *&parseTree, KSeExpr::ErrorCode &errorCode, std::vector< std::string > &errorIds, int &errorStart, int &errorEnd, std::vector< std::pair< int, int > > &_comments, const KSeExpr::Expression *expr, const char *str, bool wantVec=true)
@ ExpressionIncompatibleTypes
"Expression generated type " << _parseTree->type() << " incompatible with desired type " << _desiredR...