SeExpr

Expression.h
Go to the documentation of this file.
1 /*
2 Copyright Disney Enterprises, Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License
6 and the following modification to it: Section 6 Trademarks.
7 deleted and replaced with:
8
9 6. Trademarks. This License does not grant permission to use the
10 trade names, trademarks, service marks, or product names of the
11 Licensor and its affiliates, except as required for reproducing
12 the content of the NOTICE file.
13
14 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #ifndef Expression_h
18 #define Expression_h
19
20 #include <string>
21 #include <map>
22 #include <set>
23 #include <vector>
24 #include <iomanip>
25 #include <stdint.h>
26 #include "ExprConfig.h"
27 #include "Vec.h"
28 #include "Context.h"
29 #include "ExprEnv.h"
30
31 namespace llvm {
32 class ExecutionEngine;
33 class LLVMContext;
34 }
35
36 namespace SeExpr2 {
37
38 class ExprNode;
39 class ExprVarNode;
40 class ExprFunc;
41 class Expression;
42 class Interpreter;
43
45 class ExprVarRef {
46 ExprVarRef() : _type(ExprType().Error().Varying()) {};
47
48 public:
49 ExprVarRef(const ExprType& type) : _type(type) {};
50
51 virtual ~ExprVarRef() {}
52
54 virtual void setType(const ExprType& type) {
55 _type = type;
56 };
57
59 virtual ExprType type() const {
60 return _type;
61 };
62
64 virtual void eval(double* result) = 0;
65 virtual void eval(const char** resultStr) = 0;
66
67 private:
68 ExprType _type;
69 };
70
71 class LLVMEvaluator;
72 class VarBlock;
73 class VarBlockCreator;
74
76 class Expression {
77 public:
79 enum EvaluationStrategy {
80 UseInterpreter,
81 UseLLVM
82 };
84 static EvaluationStrategy defaultEvaluationStrategy;
86 static bool debugging;
87
88 // typedef std::map<std::string, ExprLocalVarRef> LocalVarTable;
89
91 struct Error {
93 std::string error;
94
96 int startPos;
97
99 int endPos;
100
101 Error(const std::string& errorIn, const int startPosIn, const int endPosIn)
102 : error(errorIn), startPos(startPosIn), endPos(endPosIn) {}
103 };
104
105 Expression(EvaluationStrategy be = Expression::defaultEvaluationStrategy);
106 Expression(const std::string& e,
107 const ExprType& type = ExprType().FP(3),
108 EvaluationStrategy be = Expression::defaultEvaluationStrategy,
109 const Context& context = Context::global());
110
111 virtual ~Expression();
112
115 void setDesiredReturnType(const ExprType& type);
116
119 void setExpr(const std::string& e);
120
122 const std::string& getExpr() const { return _expression; }
123
127 bool syntaxOK() const;
128
133 bool isValid() const {
134 prepIfNeeded();
135 return _isValid;
136 }
137
140 const std::string& parseError() const { return _parseError; }
141
144 const std::vector<Error>& getErrors() const { return _errors; }
145
147 const std::vector<std::pair<int, int> >& getComments() const { return _comments; }
148
151 bool isConstant() const;
152
155 bool usesVar(const std::string& name) const;
156
159 bool usesFunc(const std::string& name) const;
160
162 bool isThreadSafe() const { return _threadUnsafeFunctionCalls.size() == 0; }
163
166 void setThreadUnsafe(const std::string& functionName) const { _threadUnsafeFunctionCalls.push_back(functionName); }
167
169 const std::vector<std::string>& getThreadUnsafeFunctionCalls() const { return _threadUnsafeFunctionCalls; }
170
172 bool wantVec() const { return _wantVec; }
173
177 bool isVec() const;
178
182 const ExprType& returnType() const;
183
185 void evalMultiple(VarBlock* varBlock, int outputVarBlockOffset, size_t rangeStart, size_t rangeEnd) const;
186
187 // TODO: make this deprecated
189 const double* evalFP(VarBlock* varBlock = nullptr) const;
190
191 // TODO: make this deprecated
193 const char* evalStr(VarBlock* varBlock = nullptr) const;
194
196 void reset();
197
199 virtual ExprVarRef* resolveVar(const std::string& name) const { return 0; }
200
202 virtual ExprFunc* resolveFunc(const std::string& name) const { return 0; }
203
205 void addError(const std::string& error, const int startPos, const int endPos) const {
206 _errors.push_back(Error(error, startPos, endPos));
207 }
208
210 void addComment(int pos, int length) { _comments.push_back(std::pair<int, int>(pos, length + pos - 1)); }
211
213 // const LocalVarTable& getLocalVars() const {return _localVars;}
214
216 const Context& context() const { return *_context; }
217 void setContext(const Context& context);
218
220 void debugPrintParseTree() const;
221
223 void debugPrintInterpreter() const;
224
226 void debugPrintLLVM() const;
227
229 void setVarBlockCreator(const VarBlockCreator* varBlockCreator);
230
231 const VarBlockCreator* varBlockCreator() const { return _varBlockCreator; }
232
233 private:
235 Expression(const Expression& e);
236 Expression& operator=(const Expression& e);
237
239 void parse() const;
240
242 void parseIfNeeded() const {
243 if (!_parsed) parse();
244 }
245
248 void prep() const;
249
251 bool _wantVec;
252
254 mutable ExprType _returnType;
255
257 std::string _expression;
258
259 EvaluationStrategy _evaluationStrategy;
260
262 const Context* _context;
263
264 protected:
266 mutable ExprType _desiredReturnType;
267
269 mutable ExprVarEnvBuilder _envBuilder;
271 mutable ExprNode* _parseTree;
272
274 void prepIfNeeded() const {
275 if (!_prepped) prep();
276 }
277
278 private:
280 mutable bool _isValid;
282 mutable bool _parsed, _prepped;
283
285 mutable std::string _parseError;
286
288 mutable std::vector<Error> _errors;
289
291 mutable std::vector<std::pair<int, int> > _comments;
292
294 mutable std::set<std::string> _vars;
295
297 mutable std::set<std::string> _funcs;
298
300 // mutable LocalVarTable _localVars;
301
303 mutable std::vector<std::string> _threadUnsafeFunctionCalls;
304
306 mutable Interpreter* _interpreter;
307 mutable int _returnSlot;
308
309 // LLVM evaluation layer
310 mutable LLVMEvaluator* _llvmEvaluator;
311
312 // Var block creator
313 const VarBlockCreator* _varBlockCreator = 0;
314
315 /* internal */ public:
316
318 void addVar(const char* n) const { _vars.insert(n); }
319
321 void addFunc(const char* n) const { _funcs.insert(n); }
322
324 // ExprVarRef* resolveLocalVar(const char* n) const {
325 // LocalVarTable::iterator iter = _localVars.find(n);
326 // if (iter != _localVars.end()) return &iter->second;
327 // return 0;
328 //}
329
332 /* ExprLocalVarRef* getLocalVar(const char* n) const { */
333 /* return &_localVars[n]; */
334 /* } */
335 };
336 }
337
338 #endif
SeExpr2::Expression::Error::startPos
int startPos
Error start offset in expression string.
Definition: Expression.h:96
SeExpr2::Expression::debugPrintInterpreter
void debugPrintInterpreter() const
Definition: Expression.cpp:112
SeExpr2::Expression::usesVar
bool usesVar(const std::string &name) const
Definition: Expression.cpp:184
SeExpr2::Expression::debugging
static bool debugging
Whether to debug expressions.
Definition: Expression.h:86
SeExpr2::Expression::context
const Context & context() const
Definition: Expression.h:216
SeExpr2::Expression::_threadUnsafeFunctionCalls
std::vector< std::string > _threadUnsafeFunctionCalls
Definition: Expression.h:303
llvm
Definition: Expression.h:31
SeExpr2::Expression::isValid
bool isValid() const
Definition: Expression.h:133
SeExpr2::Expression::parseError
const std::string & parseError() const
Definition: Expression.h:140
SeExpr2::Expression::getErrors
const std::vector< Error > & getErrors() const
Definition: Expression.h:144
SeExpr2::Expression::Error::error
std::string error
Text of error.
Definition: Expression.h:93
SeExpr2::Expression::_parseTree
ExprNode * _parseTree
Definition: Expression.h:271
SeExpr2::Expression::parseIfNeeded
void parseIfNeeded() const
Definition: Expression.h:242
SeExpr2::Expression::setVarBlockCreator
void setVarBlockCreator(const VarBlockCreator *varBlockCreator)
Definition: Expression.cpp:164
SeExpr2::ExprVarRef::_type
ExprType _type
Definition: Expression.h:68
SeExpr2::VarBlock
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:33
SeExpr2::Expression::UseInterpreter
Definition: Expression.h:80
SeExpr2::Expression::evalMultiple
void evalMultiple(VarBlock *varBlock, int outputVarBlockOffset, size_t rangeStart, size_t rangeEnd) const
Evaluate multiple blocks.
Definition: Expression.cpp:318
SeExpr2::Expression::_prepped
bool _prepped
Definition: Expression.h:282
SeExpr2::Expression::varBlockCreator
const VarBlockCreator * varBlockCreator() const
Definition: Expression.h:231
SeExpr2::Expression::resolveFunc
virtual ExprFunc * resolveFunc(const std::string &name) const
Definition: Expression.h:202
SeExpr2::Expression::reset
void reset()
Definition: Expression.cpp:132
SeExpr2::Expression::getThreadUnsafeFunctionCalls
const std::vector< std::string > & getThreadUnsafeFunctionCalls() const
Definition: Expression.h:169
SeExpr2::VarBlockCreator
A class that lets you register for the variables used by one or more expressions.
Definition: VarBlock.h:84
SeExpr2::Expression::_comments
std::vector< std::pair< int, int > > _comments
Definition: Expression.h:291
SeExpr2::ExprNode
Definition: ExprNode.h:72
SeExpr2::Context::global
static Context & global()
The global default context of the seexpr.
Definition: Context.cpp:29
SeExpr2::Expression::getComments
const std::vector< std::pair< int, int > > & getComments() const
Definition: Expression.h:147
SeExpr2::Expression::evalStr
const char * evalStr(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:339
SeExpr2::Expression::setExpr
void setExpr(const std::string &e)
Definition: Expression.cpp:169
SeExpr2::Expression::_vars
std::set< std::string > _vars
Definition: Expression.h:294
SeExpr2::ExprVarRef::setType
virtual void setType(const ExprType &type)
sets (current) type to given type
Definition: Expression.h:54
SeExpr2::Expression::_expression
std::string _expression
Definition: Expression.h:257
SeExpr2::Expression::debugPrintParseTree
void debugPrintParseTree() const
Definition: Expression.cpp:121
SeExpr2::Expression::addFunc
void addFunc(const char *n) const
add function evaluation (this is for internal use)
Definition: Expression.h:321
SeExpr2::Expression::_wantVec
bool _wantVec
Definition: Expression.h:251
SeExpr2::Expression::_isValid
bool _isValid
Definition: Expression.h:280
SeExpr2::Expression::wantVec
bool wantVec() const
Definition: Expression.h:172
SeExpr2::Expression::_returnSlot
int _returnSlot
Definition: Expression.h:307
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::Context
Definition: Context.h:24
SeExpr2::Expression::_desiredReturnType
ExprType _desiredReturnType
Definition: Expression.h:266
SeExpr2::length
double length(const Vec3d &v)
Definition: ExprBuiltins.cpp:1062
SeExpr2::ExprFunc
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
SeExpr2::Expression::setThreadUnsafe
void setThreadUnsafe(const std::string &functionName) const
Definition: Expression.h:166
SeExpr2::Expression::prepIfNeeded
void prepIfNeeded() const
Definition: Expression.h:274
SeExpr2::Expression::Expression
Expression(EvaluationStrategy be=Expression::defaultEvaluationStrategy)
Definition: Expression.cpp:90
SeExpr2::Expression::isThreadSafe
bool isThreadSafe() const
Definition: Expression.h:162
SeExpr2
Definition: Context.h:22
SeExpr2::Expression::_errors
std::vector< Error > _errors
Definition: Expression.h:288
SeExpr2::Expression::isConstant
bool isConstant() const
Definition: Expression.cpp:179
SeExpr2::Expression::_evaluationStrategy
EvaluationStrategy _evaluationStrategy
Definition: Expression.h:259
Vec.h
SeExpr2::Expression::_funcs
std::set< std::string > _funcs
Definition: Expression.h:297
SeExpr2::Expression::setContext
void setContext(const Context &context)
Definition: Expression.cpp:154
SeExpr2::ExprVarRef::~ExprVarRef
virtual ~ExprVarRef()
Definition: Expression.h:51
SeExpr2::Expression::parse
void parse() const
Definition: Expression.cpp:194
SeExpr2::Expression::getExpr
const std::string & getExpr() const
Get the string that this expression is currently set to evaluate.
Definition: Expression.h:122
SeExpr2::Expression::~Expression
virtual ~Expression()
Definition: Expression.cpp:107
SeExpr2::ExprVarEnvBuilder
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
SeExpr2::Expression
main expression class
Definition: Expression.h:76
SeExpr2::Expression::Error::Error
Error(const std::string &errorIn, const int startPosIn, const int endPosIn)
Definition: Expression.h:101
SeExpr2::Expression::_parseError
std::string _parseError
Definition: Expression.h:285
be
< b ></b >< br >< b ></b ></td >< td > vector constructor< br > vector component access n must be
Definition: userdoc.txt:484
ExprEnv.h
SeExpr2::Expression::resolveVar
virtual ExprVarRef * resolveVar(const std::string &name) const
Definition: Expression.h:199
SeExpr2::ExprVarRef::eval
virtual void eval(double *result)=0
returns this variable's value by setting result
SeExpr2::Expression::_parsed
bool _parsed
Definition: Expression.h:282
SeExpr2::Expression::returnType
const ExprType & returnType() const
Definition: Expression.cpp:299
SeExpr2::Expression::_envBuilder
ExprVarEnvBuilder _envBuilder
Definition: Expression.h:269
SeExpr2::Expression::_interpreter
Interpreter * _interpreter
Definition: Expression.h:306
SeExpr2::ExprVarRef::ExprVarRef
ExprVarRef()
Definition: Expression.h:46
SeExpr2::Expression::setDesiredReturnType
void setDesiredReturnType(const ExprType &type)
Definition: Expression.cpp:159
SeExpr2::Expression::EvaluationStrategy
EvaluationStrategy
Types of evaluation strategies that are available.
Definition: Expression.h:79
SeExpr2::Expression::defaultEvaluationStrategy
static EvaluationStrategy defaultEvaluationStrategy
What evaluation strategy to use by default.
Definition: Expression.h:84
SeExpr2::LLVMEvaluator
Definition: Evaluator.h:388
SeExpr2::Expression::_context
const Context * _context
Definition: Expression.h:262
SeExpr2::Expression::addComment
void addComment(int pos, int length)
Definition: Expression.h:210
SeExpr2::Expression::Error::endPos
int endPos
Error end offset in expression string.
Definition: Expression.h:99
SeExpr2::Expression::_llvmEvaluator
LLVMEvaluator * _llvmEvaluator
Definition: Expression.h:310
SeExpr2::Expression::operator=
Expression & operator=(const Expression &e)
SeExpr2::Expression::evalFP
const double * evalFP(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:304
SeExpr2::Expression::_returnType
ExprType _returnType
Definition: Expression.h:254
SeExpr2::Expression::addError
void addError(const std::string &error, const int startPos, const int endPos) const
Definition: Expression.h:205
SeExpr2::ExprVarRef
abstract class for implementing variable references
Definition: Expression.h:45
SeExpr2::Expression::_varBlockCreator
const VarBlockCreator * _varBlockCreator
Definition: Expression.h:313
SeExpr2::Expression::prep
void prep() const
Definition: Expression.cpp:204
SeExpr2::ExprVarRef::type
virtual ExprType type() const
returns (current) type
Definition: Expression.h:59
SeExpr2::Expression::addVar
void addVar(const char *n) const
add local variable (this is for internal use)
Definition: Expression.h:318
SeExpr2::Expression::syntaxOK
bool syntaxOK() const
Definition: Expression.cpp:174
SeExpr2::Interpreter
Definition: Interpreter.h:40
SeExpr2::Expression::Error
Represents a parse or type checking error in an expression.
Definition: Expression.h:91
SeExpr2::Expression::debugPrintLLVM
void debugPrintLLVM() const
Definition: Expression.cpp:119
SeExpr2::Expression::UseLLVM
Definition: Expression.h:81
SeExpr2::Expression::usesFunc
bool usesFunc(const std::string &name) const
Definition: Expression.cpp:189
Context.h
SeExpr2::Expression::isVec
bool isVec() const
Definition: Expression.cpp:294
SeExpr2::ExprVarRef::ExprVarRef
ExprVarRef(const ExprType &type)
Definition: Expression.h:49