KSeExpr 6.0.0.0
BasicExpression.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2// SPDX-License-Identifier: LicenseRef-Apache-2.0
3// SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6/*
7 * @file BasicExpression.h
8 * @brief A basic expression context for the expression previewer
9 * @author aselle
10 */
11
12#pragma once
13
14#include <KSeExpr/ExprFunc.h>
15#include <KSeExpr/ExprNode.h>
16#include <KSeExpr/Expression.h>
17#include <map>
18
20{
21public:
23 double value {};
25 : KSeExpr::ExprVarRef(KSeExpr::ExprType().FP(1).Varying())
26 {
27 }
28 void eval(double *result) override
29 {
30 result[0] = value;
31 }
32 void eval(const char **) override
33 {
34 assert(false);
35 }
36 };
37
41 : KSeExpr::ExprVarRef(KSeExpr::ExprType().FP(3).Varying())
42 , value(0.0)
43 {
44 }
45 void eval(double *result) override
46 {
47 for (int k = 0; k < 3; k++)
48 result[k] = value[k];
49 };
50 void eval(const char **) override
51 {
52 assert(false);
53 }
54 };
55
58 : KSeExpr::ExprFuncSimple(true)
59 {
60 }
61
62 ~DummyFuncX() override = default;
64 DummyFuncX &operator=(const DummyFuncX &) = default;
65 DummyFuncX(DummyFuncX &&) = default;
66 DummyFuncX(const DummyFuncX &) = default;
67
69 {
70 bool valid = true;
71 int nargs = node->numChildren();
72 for (int i = 0; i < nargs; i++)
73 valid &= node->checkArg(i, KSeExpr::ExprType().FP(3).Constant(), envBuilder);
74 return valid ? KSeExpr::ExprType().FP(3).Varying() : KSeExpr::ExprType().Error();
75 }
76
81
82 void eval(ArgHandle args) override
83 {
84 double *out = &args.outFp;
85 for (int i = 0; i < 3; i++)
86 out[i] = 0.0;
87 }
90
91 mutable ScalarRef u;
92 mutable ScalarRef v;
93 mutable VectorRef P;
94
95 using VARMAP = std::map<std::string, VectorRef *>;
96 mutable VARMAP varmap;
97 using FUNCMAP = std::map<std::string, bool>;
99
100 BasicExpression(const std::string &expr, const KSeExpr::ExprType &type = KSeExpr::ExprType().FP(3));
101 ~BasicExpression() override;
102
107
108 KSeExpr::ExprVarRef *resolveVar(const std::string &name) const override;
109 KSeExpr::ExprFunc *resolveFunc(const std::string &name) const override;
110 void setExpr(const std::string &str);
111 void clearVars();
112};
BasicExpression(const BasicExpression &)=delete
KSeExpr::ExprVarRef * resolveVar(const std::string &name) const override
BasicExpression & operator=(BasicExpression &&)=delete
KSeExpr::ExprFunc * resolveFunc(const std::string &name) const override
BasicExpression::DummyFuncX dummyFuncX
std::map< std::string, VectorRef * > VARMAP
BasicExpression(BasicExpression &&)=delete
std::map< std::string, bool > FUNCMAP
BasicExpression & operator=(const BasicExpression &)=delete
~BasicExpression() override
void setExpr(const std::string &str)
KSeExpr::ExprFunc dummyFunc
Node that calls a function.
Definition ExprNode.h:654
bool checkArg(int argIndex, const ExprType &type, ExprVarEnvBuilder &envBuilder)
Definition ExprNode.cpp:631
ExprFuncSimple(const bool threadSafe)
Definition ExprFuncX.h:66
Function Definition, used in parse tree and func table.
Definition ExprFunc.h:35
int numChildren() const
Number of children.
Definition ExprNode.h:108
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition ExprType.h:97
ExprType & Constant()
Mutate this into a constant lifetime.
Definition ExprType.h:122
ExprType & Varying()
Mutate this into a varying lifetime.
Definition ExprType.h:134
ExprType & Error()
Mutate this into an error type.
Definition ExprType.h:111
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition ExprEnv.h:181
abstract class for implementing variable references
Definition Expression.h:36
main expression class
Definition Expression.h:67
~DummyFuncX() override=default
DummyFuncX(const DummyFuncX &)=default
DummyFuncX & operator=(const DummyFuncX &)=default
void eval(ArgHandle args) override
KSeExpr::ExprFuncNode::Data * evalConstant(const KSeExpr::ExprFuncNode *, ArgHandle) const override
DummyFuncX & operator=(DummyFuncX &&)=default
KSeExpr::ExprType prep(KSeExpr::ExprFuncNode *node, bool, KSeExpr::ExprVarEnvBuilder &envBuilder) const override
DummyFuncX(DummyFuncX &&)=default
void eval(const char **) override
void eval(double *result) override
returns this variable's value by setting result
void eval(const char **) override
void eval(double *result) override
returns this variable's value by setting result
base class for custom instance data
Definition ExprNode.h:723