SeExpr
BasicExpression.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 * @file BasicExpression.h
18 * @brief A basic expression context for the expression previewer
19 * @author aselle
20 */
21 
22 #ifndef BasicExpression_h
23 #define BasicExpression_h
24 
25 #include <map>
26 #include <SeExpr2/Expression.h>
27 #include <SeExpr2/ExprFunc.h>
28 #include <SeExpr2/ExprNode.h>
29 
31 {
32  public:
33  struct ScalarRef : public SeExpr2::ExprVarRef {
34  double value;
35  ScalarRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(1).Varying()), value(0.0) {}
36  void eval(double* result){result[0]=value;}
37  void eval(const char** result){assert(false);}
38  };
39 
40  struct VectorRef : public SeExpr2::ExprVarRef {
42  VectorRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(3).Varying()), value(0.0) {}
43  void eval(double* result){
44  for(int k=0;k<3;k++) result[k]=value[k];
45  };
46  void eval(const char** result){assert(false);};
47  };
48 
50  {
51  DummyFuncX() :SeExpr2::ExprFuncSimple(true) {}
52  virtual ~DummyFuncX() {}
53 
54  virtual SeExpr2::ExprType prep(SeExpr2::ExprFuncNode* node, bool scalarWanted, SeExpr2::ExprVarEnvBuilder& envBuilder) const
55  {
56  bool valid=true;
57  int nargs = node->numChildren();
58  for(int i=0; i<nargs; i++)
59  valid &= node->checkArg(i, SeExpr2::ExprType().FP(3).Constant(), envBuilder);
60  return valid ? SeExpr2::ExprType().FP(3).Varying() : SeExpr2::ExprType().Error();
61  }
62 
64  return new SeExpr2::ExprFuncNode::Data();
65  }
66 
67  virtual void eval(ArgHandle args) {
68  double* out = &args.outFp;
69  for(int i=0; i<3; i++) out[i] = 0.0;
70  }
71  } dummyFuncX;
73 
74  mutable ScalarRef u;
75  mutable ScalarRef v;
76  mutable VectorRef P;
77 
78  typedef std::map<std::string,VectorRef*> VARMAP;
79  mutable VARMAP varmap;
80  typedef std::map<std::string,bool> FUNCMAP;
81  mutable FUNCMAP funcmap;
82 
83  BasicExpression(const std::string& expr,
84  const SeExpr2::ExprType& type = SeExpr2::ExprType().FP(3));
85  virtual ~BasicExpression();
86 
87  SeExpr2::ExprVarRef* resolveVar(const std::string& name) const;
88  SeExpr2::ExprFunc* resolveFunc(const std::string& name) const;
89  void setExpr(const std::string& str);
90  void clearVars();
91 };
92 
93 
94 #endif
virtual void eval(ArgHandle args)
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:122
virtual SeExpr2::ExprType prep(SeExpr2::ExprFuncNode *node, bool scalarWanted, SeExpr2::ExprVarEnvBuilder &envBuilder) const
</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
std::map< std::string, VectorRef * > VARMAP
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
SeExpr2::ExprVarRef * resolveVar(const std::string &name) const
void eval(const char **result)
std::map< std::string, bool > FUNCMAP
Node that calls a function.
Definition: ExprNode.h:514
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:74
void eval(double *result)
returns this variable&#39;s value by setting result
void eval(const char **result)
bool checkArg(int argIndex, ExprType type, ExprVarEnvBuilder &envBuilder)
Definition: ExprNode.cpp:572
void eval(double *result)
returns this variable&#39;s value by setting result
main expression class
Definition: Expression.h:76
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:112
base class for custom instance data
Definition: ExprNode.h:564
int numChildren() const
Number of children.
Definition: ExprNode.h:114
void setExpr(const std::string &str)
virtual SeExpr2::ExprFuncNode::Data * evalConstant(const SeExpr2::ExprFuncNode *node, ArgHandle args) const
BasicExpression::DummyFuncX dummyFuncX
virtual ~BasicExpression()
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:90
SeExpr2::ExprFunc * resolveFunc(const std::string &name) const
abstract class for implementing variable references
Definition: Expression.h:45
SeExpr2::ExprFunc dummyFunc
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:102
BasicExpression(const std::string &expr, const SeExpr2::ExprType &type=SeExpr2::ExprType().FP(3))
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:152