KSeExpr  4.0.4.0
BasicExpression.cpp
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.cpp
8  * @brief A basic expression context for the expression previewer
9  * @author aselle
10  */
11 
12 #include "BasicExpression.h"
13 
14 BasicExpression::BasicExpression(const std::string &expr, const KSeExpr::ExprType &type)
15  : Expression(expr, type)
16  , dummyFunc(dummyFuncX, 0, 16)
17 {
18 }
19 
21 {
22  clearVars();
23 }
24 
25 template<class T_MAP> void deleteAndClear(T_MAP &map)
26 {
27  for (const auto& i: map)
28  delete i.second;
29  map.clear();
30 }
31 
33 {
35  funcmap.clear();
36 }
37 
38 void BasicExpression::setExpr(const std::string &str)
39 {
40  clearVars();
41  Expression::setExpr(str);
42 }
43 
44 KSeExpr::ExprVarRef *BasicExpression::resolveVar(const std::string &name) const
45 {
46  if (name == "u")
47  return &u;
48  else if (name == "v")
49  return &v;
50  else if (name == "P")
51  return &P;
52  else {
53  // make a variable to resolve any unknown
54  auto i = varmap.find(name);
55  if (i != varmap.end())
56  return i->second;
57  else {
58  varmap[name] = new VectorRef();
59  return varmap[name];
60  }
61  }
62 }
63 
64 KSeExpr::ExprFunc *BasicExpression::resolveFunc(const std::string &name) const
65 {
66  // check if it is builtin so we get proper behavior
67  if (KSeExpr::ExprFunc::lookup(name))
68  return nullptr;
69 
70  funcmap[name] = true;
71  return &dummyFunc;
72 }
void deleteAndClear(T_MAP &map)
KSeExpr::ExprVarRef * resolveVar(const std::string &name) const override
KSeExpr::ExprFunc * resolveFunc(const std::string &name) const override
~BasicExpression() override
BasicExpression(const std::string &expr, const KSeExpr::ExprType &type=KSeExpr::ExprType().FP(3))
void setExpr(const std::string &str)
KSeExpr::ExprFunc dummyFunc
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:35
static const ExprFunc * lookup(const std::string &name)
Lookup a builtin function by name.
Definition: ExprFunc.cpp:116
abstract class for implementing variable references
Definition: Expression.h:36