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:
69 };
70 
71 class LLVMEvaluator;
72 class VarBlock;
73 class VarBlockCreator;
74 
76 class Expression {
77  public:
82  };
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 
106  Expression(const std::string& e,
107  const ExprType& type = ExprType().FP(3),
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 
230 
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 
255 
257  std::string _expression;
258 
260 
263 
264  protected:
267 
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 
307  mutable int _returnSlot;
308 
309  // LLVM evaluation layer
311 
312  // Var block creator
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
int endPos
Error end offset in expression string.
Definition: Expression.h:99
void prepIfNeeded() const
Definition: Expression.h:274
bool usesVar(const std::string &name) const
Definition: Expression.cpp:179
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:35
virtual ~ExprVarRef()
Definition: Expression.h:51
EvaluationStrategy
Types of evaluation strategies that are available.
Definition: Expression.h:79
void setContext(const Context &context)
Definition: Expression.cpp:149
virtual ExprVarRef * resolveVar(const std::string &name) const
Definition: Expression.h:199
std::vector< std::string > _threadUnsafeFunctionCalls
Definition: Expression.h:303
void addError(const std::string &error, const int startPos, const int endPos) const
Definition: Expression.h:205
int startPos
Error start offset in expression string.
Definition: Expression.h:96
void setDesiredReturnType(const ExprType &type)
Definition: Expression.cpp:154
bool usesFunc(const std::string &name) const
Definition: Expression.cpp:184
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
void evalMultiple(VarBlock *varBlock, int outputVarBlockOffset, size_t rangeStart, size_t rangeEnd) const
Evaluate multiple blocks.
Definition: Expression.cpp:313
bool isThreadSafe() const
Definition: Expression.h:162
std::string error
Text of error.
Definition: Expression.h:93
void setThreadUnsafe(const std::string &functionName) const
Definition: Expression.h:166
Represents a parse or type checking error in an expression.
Definition: Expression.h:91
void debugPrintLLVM() const
Definition: Expression.cpp:114
virtual void setType(const ExprType &type)
sets (current) type to given type
Definition: Expression.h:54
ExprType _returnType
Definition: Expression.h:254
const std::string & parseError() const
Definition: Expression.h:140
EvaluationStrategy _evaluationStrategy
Definition: Expression.h:259
const std::vector< std::pair< int, int > > & getComments() const
Definition: Expression.h:147
bool isVec() const
Definition: Expression.cpp:289
double length(const Vec3d &v)
const std::string & getExpr() const
Get the string that this expression is currently set to evaluate.
Definition: Expression.h:122
std::set< std::string > _funcs
Definition: Expression.h:297
const Context & context() const
Definition: Expression.h:216
ExprType _desiredReturnType
Definition: Expression.h:266
static bool debugging
Whether to debug expressions.
Definition: Expression.h:86
A class that lets you register for the variables used by one or more expressions. ...
Definition: VarBlock.h:71
void debugPrintParseTree() const
Definition: Expression.cpp:116
static Context & global()
The global default context of the seexpr.
Definition: Context.cpp:29
void addVar(const char *n) const
add local variable (this is for internal use)
Definition: Expression.h:318
main expression class
Definition: Expression.h:76
Expression & operator=(const Expression &e)
const VarBlockCreator * _varBlockCreator
Definition: Expression.h:313
bool syntaxOK() const
Definition: Expression.cpp:169
std::vector< Error > _errors
Definition: Expression.h:288
void debugPrintInterpreter() const
Definition: Expression.cpp:107
const std::vector< Error > & getErrors() const
Definition: Expression.h:144
const VarBlockCreator * varBlockCreator() const
Definition: Expression.h:231
void addFunc(const char *n) const
add function evaluation (this is for internal use)
Definition: Expression.h:321
const double * evalFP(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:299
< b ></b >< br >< b ></b ></td >< td > vector constructor< br > vector component access n must be
Definition: userdoc.txt:504
const char * evalStr(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:335
bool isValid() const
Definition: Expression.h:133
std::string _parseError
Definition: Expression.h:285
void prep() const
Definition: Expression.cpp:199
std::set< std::string > _vars
Definition: Expression.h:294
void setVarBlockCreator(const VarBlockCreator *varBlockCreator)
Definition: Expression.cpp:159
std::string _expression
Definition: Expression.h:257
void setExpr(const std::string &e)
Definition: Expression.cpp:164
bool isConstant() const
Definition: Expression.cpp:174
const std::vector< std::string > & getThreadUnsafeFunctionCalls() const
Definition: Expression.h:169
ExprVarEnvBuilder _envBuilder
Definition: Expression.h:269
std::vector< std::pair< int, int > > _comments
Definition: Expression.h:291
virtual ExprFunc * resolveFunc(const std::string &name) const
Definition: Expression.h:202
virtual ExprType type() const
returns (current) type
Definition: Expression.h:59
virtual void eval(double *result)=0
returns this variable&#39;s value by setting result
void parseIfNeeded() const
Definition: Expression.h:242
bool wantVec() const
Definition: Expression.h:172
const ExprType & returnType() const
Definition: Expression.cpp:294
Interpreter * _interpreter
Definition: Expression.h:306
ExprVarRef(const ExprType &type)
Definition: Expression.h:49
static EvaluationStrategy defaultEvaluationStrategy
What evaluation strategy to use by default.
Definition: Expression.h:84
const Context * _context
Definition: Expression.h:262
void addComment(int pos, int length)
Definition: Expression.h:210
void parse() const
Definition: Expression.cpp:189
LLVMEvaluator * _llvmEvaluator
Definition: Expression.h:310
abstract class for implementing variable references
Definition: Expression.h:45
Expression(EvaluationStrategy be=Expression::defaultEvaluationStrategy)
Definition: Expression.cpp:85
Error(const std::string &errorIn, const int startPosIn, const int endPosIn)
Definition: Expression.h:101
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:152
ExprNode * _parseTree
Definition: Expression.h:271