15 #include <Teuchos_MathExpr.hpp>
23 : Teuchos::Reader(Teuchos::MathExpr::ask_reader_tables()) {
33 case Teuchos::MathExpr::TOK_NAME: {
34 std::string&
result = Teuchos::make_any_ref<std::string>(result_any);
38 case Teuchos::MathExpr::TOK_CONST: {
48 case Teuchos::MathExpr::PROD_PROGRAM: {
49 swap(result, rhs.at(1));
52 case Teuchos::MathExpr::PROD_NO_STATEMENTS:
53 case Teuchos::MathExpr::PROD_NO_EXPR:
54 case Teuchos::MathExpr::PROD_NEXT_STATEMENT: {
57 case Teuchos::MathExpr::PROD_ASSIGN: {
58 std::string
const& name = Teuchos::any_ref_cast<std::string>(rhs.at(0));
62 case Teuchos::MathExpr::PROD_YES_EXPR:
63 case Teuchos::MathExpr::PROD_EXPR:
64 case Teuchos::MathExpr::PROD_TERNARY_DECAY:
65 case Teuchos::MathExpr::PROD_OR_DECAY:
66 case Teuchos::MathExpr::PROD_AND_DECAY:
67 case Teuchos::MathExpr::PROD_ADD_SUB_DECAY:
68 case Teuchos::MathExpr::PROD_MUL_DIV_DECAY:
69 case Teuchos::MathExpr::PROD_POW_DECAY:
70 case Teuchos::MathExpr::PROD_NEG_DECAY:
71 case Teuchos::MathExpr::PROD_SOME_ARGS:
72 swap(result, rhs.at(0));
74 case Teuchos::MathExpr::PROD_TERNARY:
75 this->
ternary_op(result, rhs.at(0), rhs.at(3), rhs.at(6));
77 case Teuchos::MathExpr::PROD_OR:
80 case Teuchos::MathExpr::PROD_AND:
83 case Teuchos::MathExpr::PROD_GT:
86 case Teuchos::MathExpr::PROD_LT:
89 case Teuchos::MathExpr::PROD_GEQ:
92 case Teuchos::MathExpr::PROD_LEQ:
95 case Teuchos::MathExpr::PROD_EQ:
98 case Teuchos::MathExpr::PROD_BOOL_PARENS:
99 swap(result, rhs.at(2));
101 case Teuchos::MathExpr::PROD_ADD:
104 case Teuchos::MathExpr::PROD_SUB:
107 case Teuchos::MathExpr::PROD_MUL:
110 case Teuchos::MathExpr::PROD_DIV:
113 case Teuchos::MathExpr::PROD_POW:
116 case Teuchos::MathExpr::PROD_CALL: {
117 std::string
const& name = Teuchos::any_ref_cast<std::string>(rhs.at(0));
120 "symbol \"" << name <<
"\" being called doesn't exist!");
122 std::vector<Teuchos::any>& args = Teuchos::any_ref_cast<std::vector<Teuchos::any>>(rhs.at(4));
123 func(name, result, args);
126 case Teuchos::MathExpr::PROD_NO_ARGS: {
127 result = std::vector<Teuchos::any>{};
130 case Teuchos::MathExpr::PROD_FIRST_ARG: {
131 std::vector<Teuchos::any>& args = Teuchos::make_any_ref<std::vector<Teuchos::any>>(
result);
133 swap(args.back(), rhs.at(0));
136 case Teuchos::MathExpr::PROD_NEXT_ARG: {
137 swap(result, rhs.at(0));
138 std::vector<Teuchos::any>& args = Teuchos::any_ref_cast<std::vector<Teuchos::any>>(
result);
140 swap(args.back(), rhs.at(3));
143 case Teuchos::MathExpr::PROD_NEG:
144 this->
neg_op(result, rhs.at(2));
146 case Teuchos::MathExpr::PROD_VAL_PARENS:
147 swap(result, rhs.at(2));
149 case Teuchos::MathExpr::PROD_CONST:
150 swap(result, rhs.at(0));
152 case Teuchos::MathExpr::PROD_VAR:
153 std::string
const& name = Teuchos::any_ref_cast<std::string>(rhs.at(0));
156 "symbol " << name <<
" being referenced doesn't exist!");
165 this->
inspect_arg(cond, cond_is_many, cond_is_bool);
167 "Ternary condition is not of boolean type!");
173 "Boolean values in ternary operator not yet supported");
176 auto host_cond_value = Kokkos::create_mirror_view(cond_value);
177 Kokkos::deep_copy(host_cond_value, cond_value);
178 if (host_cond_value()) {
184 if (!is_many[0] && !is_many[1]) {
186 }
else if (!is_many[0] && is_many[1]) {
188 }
else if (is_many[0] && !is_many[1]) {
190 }
else if (is_many[0] && is_many[1]) {
221 "Left argument to '" <<
get_op_syntax(code) <<
"' is " << (is_bool[0] ?
"" :
"not") <<
" boolean!");
223 "Right argument to '" <<
get_op_syntax(code) <<
"' is " << (is_bool[0] ?
"" :
"not") <<
" boolean!");
224 if (!is_many[0] && !is_many[1]) {
226 }
else if (!is_many[0] && is_many[1]) {
228 }
else if (is_many[0] && !is_many[1]) {
230 }
else if (is_many[0] && is_many[1]) {
240 "Can't negate a boolean");
virtual void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void make_constant(Teuchos::any &result, double const &value)=0
BinaryOpCode
Denotes the native binary operators in the Teuchos::MathExpr language.
virtual void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
std::function< void(std::string const &name, Teuchos::any &, std::vector< Teuchos::any > &rhs)> Function
The type of user-defined functions which are callable in the math language.
virtual void many_neg_op(Teuchos::any &result, Teuchos::any &right)=0
virtual void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool)=0
void ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
Executes the ternary operator, e.g. (a > b) ? a : b.
void at_reduce(Teuchos::any &result, int prod, std::vector< Teuchos::any > &rhs) override
Called at every reduced production in the math language.
void binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
Executes a binary operator.
void set(std::string const &name, Function const &value)
Registers an EvalBase::Function, binding it to a name and making it callable.
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we're performing.
virtual void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
std::map< std::string, Teuchos::any > symbol_map
Stores all current symbols including variables and functions.
static const char * get_op_syntax(BinaryOpCode code)
virtual void single_neg_op(Teuchos::any &result, Teuchos::any &right)=0
virtual void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
void neg_op(Teuchos::any &result, Teuchos::any &right)
Executes the only native unary operator in the math language, numeric negation via a minus sign...
void at_shift(Teuchos::any &result, int token, std::string &text) override
Called at every parsed token in the math language.
virtual void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0