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 public:
33 double value;
34 ScalarRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(1).Varying()), value(0.0) {}
35 void eval(double* result) { result[0] = value; }
36 void eval(const char** result) { assert(false); }
37 };
38
41 VectorRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(3).Varying()), value(0.0) {}
42 void eval(double* result) {
43 for (int k = 0; k < 3; k++) result[k] = value[k];
44 };
45 void eval(const char** result) {
46 assert(false);
47 };
48 };
49
52 virtual ~DummyFuncX() {}
53
55 bool scalarWanted,
56 SeExpr2::ExprVarEnvBuilder& envBuilder) const {
57 bool valid = true;
58 int nargs = node->numChildren();
59 for (int i = 0; i < nargs; i++)
60 valid &= node->checkArg(i, SeExpr2::ExprType().FP(3).Constant(), envBuilder);
61 return valid ? SeExpr2::ExprType().FP(3).Varying() : SeExpr2::ExprType().Error();
62 }
63
65 return new SeExpr2::ExprFuncNode::Data();
66 }
67
68 virtual void eval(ArgHandle args) {
69 double* out = &args.outFp;
70 for (int i = 0; i < 3; i++) out[i] = 0.0;
71 }
74
75 mutable ScalarRef u;
76 mutable ScalarRef v;
77 mutable VectorRef P;
78
79 typedef std::map<std::string, VectorRef*> VARMAP;
80 mutable VARMAP varmap;
81 typedef std::map<std::string, bool> FUNCMAP;
83
84 BasicExpression(const std::string& expr, 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#endif
std::map< std::string, bool > FUNCMAP
SeExpr2::ExprFunc dummyFunc
SeExpr2::ExprFunc * resolveFunc(const std::string &name) const
virtual ~BasicExpression()
BasicExpression::DummyFuncX dummyFuncX
SeExpr2::ExprVarRef * resolveVar(const std::string &name) const
std::map< std::string, VectorRef * > VARMAP
void setExpr(const std::string &str)
Node that calls a function.
Definition: ExprNode.h:517
bool checkArg(int argIndex, ExprType type, ExprVarEnvBuilder &envBuilder)
Definition: ExprNode.cpp:578
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:74
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
int numChildren() const
Number of children.
Definition: ExprNode.h:114
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:90
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:102
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:122
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:112
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
abstract class for implementing variable references
Definition: Expression.h:45
main expression class
Definition: Expression.h:76
virtual SeExpr2::ExprFuncNode::Data * evalConstant(const SeExpr2::ExprFuncNode *node, ArgHandle args) const
virtual SeExpr2::ExprType prep(SeExpr2::ExprFuncNode *node, bool scalarWanted, SeExpr2::ExprVarEnvBuilder &envBuilder) const
virtual void eval(ArgHandle args)
void eval(double *result)
returns this variable's value by setting result
void eval(const char **result)
void eval(const char **result)
void eval(double *result)
returns this variable's value by setting result
base class for custom instance data
Definition: ExprNode.h:570
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</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")