48# pragma fenv_access(on)
119 return expr !=
nullptr && expr->
ops.size() > 0;
124 return expr !=
nullptr && expr->
ops.size() == 1 && expr->
ops[0].opcode ==
OPCODE_CONST;
131 if (expr ==
nullptr) {
135 for (i = 0; i < expr->
ops.size(); i++) {
151 const double *param_values,
152 int param_values_len,
161#define FAIL_IF(condition) \
163 return EXPR_PYLIKE_FATAL_ERROR; \
176 feclearexcept(FE_ALL_EXCEPT);
178 for (pc = 0; pc >= 0 && pc < expr->
ops.size(); pc++) {
179 switch (ops[pc].opcode) {
183 stack[sp++] = ops[pc].
arg.dval;
187 stack[sp++] = param_values[ops[pc].
arg.ival];
191 stack[sp - 1] = ops[pc].arg.func1(stack[sp - 1]);
195 stack[sp - 2] = ops[pc].arg.func2(stack[sp - 2], stack[sp - 1]);
200 stack[sp - 3] = ops[pc].arg.func3(stack[sp - 3], stack[sp - 2], stack[sp - 1]);
204 FAIL_IF(sp < ops[pc].arg.ival);
205 for (
int j = 1; j < ops[pc].arg.ival; j++, sp--) {
210 FAIL_IF(sp < ops[pc].arg.ival);
211 for (
int j = 1; j < ops[pc].arg.ival; j++, sp--) {
218 pc += ops[pc].jmp_offset;
223 pc += ops[pc].jmp_offset;
230 pc += ops[pc].jmp_offset;
241 if (!ops[pc].arg.func2(stack[sp - 2], stack[sp - 1])) {
243 pc += ops[pc].jmp_offset;
247 stack[sp - 2] = stack[sp - 1];
261 *r_result = stack[0];
264 int flags = fetestexcept(FE_DIVBYZERO | FE_INVALID);
305 return arg *
M_PI / 180.0;
310 return arg * 180.0 /
M_PI;
320 return a * (1.0 -
x) +
b *
x;
325 CLAMP(arg, 0.0, 1.0);
329static double op_clamp3(
double arg,
double minv,
double maxv)
331 CLAMP(arg, minv, maxv);
337 double t = (
x - a) / (
b - a);
339 return t * t * (3.0 - 2.0 * t);
344 return a ? 0.0 : 1.0;
349 return a ==
b ? 1.0 : 0.0;
354 return a !=
b ? 1.0 : 0.0;
359 return a <
b ? 1.0 : 0.0;
364 return a <=
b ? 1.0 : 0.0;
369 return a >
b ? 1.0 : 0.0;
374 return a >=
b ? 1.0 : 0.0;
383 {
"pi",
M_PI}, {
"True", 1.0}, {
"False", 0.0}, {
nullptr, 0.0}};
387 std::variant<UnaryOpFunc, BinaryOpFunc, TernaryOpFunc>
funcptr;
392 if (std::holds_alternative<UnaryOpFunc>(
funcptr)) {
395 if (std::holds_alternative<BinaryOpFunc>(
funcptr)) {
398 if (std::holds_alternative<TernaryOpFunc>(
funcptr)) {
410# pragma function(ceil)
411# pragma function(floor)
450#define MAKE_CHAR2(a, b) (((a) << 8) | (b))
452#define CHECK_ERROR(condition) \
453 if (!(condition)) { \
460#define TOKEN_ID MAKE_CHAR2('I', 'D')
461#define TOKEN_NUMBER MAKE_CHAR2('0', '0')
462#define TOKEN_GE MAKE_CHAR2('>', '=')
463#define TOKEN_LE MAKE_CHAR2('<', '=')
464#define TOKEN_NE MAKE_CHAR2('!', '=')
465#define TOKEN_EQ MAKE_CHAR2('=', '=')
466#define TOKEN_AND MAKE_CHAR2('A', 'N')
467#define TOKEN_OR MAKE_CHAR2('O', 'R')
468#define TOKEN_NOT MAKE_CHAR2('N', 'O')
469#define TOKEN_IF MAKE_CHAR2('I', 'F')
470#define TOKEN_ELSE MAKE_CHAR2('E', 'L')
495 const char *
cur =
nullptr;
515 state->stack_ptr += stack_delta;
521 state->ops.append(op);
522 return &
state->ops.last();
541 std::variant<UnaryOpFunc, BinaryOpFunc, TernaryOpFunc> funcptr)
544 int jmp_gap =
state->ops.size() -
state->last_jmp;
546 feclearexcept(FE_ALL_EXCEPT);
548 if (std::holds_alternative<UnaryOpFunc>(funcptr)) {
551 if (jmp_gap >= 1 && prev_ops[-1].opcode ==
OPCODE_CONST) {
554 volatile double result = func(prev_ops[-1].arg.dval);
556 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
564 else if (std::holds_alternative<BinaryOpFunc>(funcptr)) {
571 volatile double result = func(prev_ops[-2].arg.dval, prev_ops[-1].
arg.dval);
573 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
583 else if (std::holds_alternative<TernaryOpFunc>(funcptr)) {
586 if (jmp_gap >= 3 && prev_ops[-3].opcode ==
OPCODE_CONST &&
591 volatile double result = func(
592 prev_ops[-3].arg.dval, prev_ops[-2].
arg.dval, prev_ops[-1].
arg.dval);
594 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
597 state->stack_ptr -= 2;
616 while (isspace(*
state->cur)) {
621 if (*
state->cur == 0) {
627 if (isdigit(*
state->cur) || (
state->cur[0] ==
'.' && isdigit(
state->cur[1]))) {
628 char *end, *
out =
state->tokenbuf.data();
629 bool is_float =
false;
631 while (isdigit(*
state->cur)) {
635 if (*
state->cur ==
'.') {
639 while (isdigit(*
state->cur)) {
654 while (isdigit(*
state->cur)) {
662 if (!is_float &&
state->tokenbuf[0] ==
'0') {
663 for (
char *p =
state->tokenbuf.data() + 1; *p; p++) {
671 state->tokenval = strtod(
state->tokenbuf.data(), &end);
735 switch (
state->token) {
758 switch (
state->token) {
778 for (i =
state->param_names_len - 1; i >= 0; i--) {
845 switch (
state->token) {
867 switch (
state->token) {
983 int prev_last_jmp =
state->last_jmp;
984 int start =
state->last_jmp =
state->ops.size();
991 const int size =
state->ops.size() - start;
994 std::copy_n(
state->ops.data() + start, size, body.
data());
996 state->ops.resize(start);
997 state->last_jmp = start;
1010 const size_t body_offset =
state->ops.size();
1011 state->ops.resize(body_offset + size);
1012 std::copy_n(body.
data(), size,
state->ops.data() + body_offset);
1027 else if (
state->last_jmp == start) {
1028 state->last_jmp = prev_last_jmp;
1041 const char **param_names,
1042 int param_names_len)
1049 state.param_names_len = param_names_len;
1050 state.param_names = param_names;
1052 state.tokenbuf.resize(strlen(expression) + 1);
1055 ExprPyLike_Parsed *expr = MEM_new<ExprPyLike_Parsed>(
"ExprPyLike_Parsed(empty)");
#define BLI_array_alloca(arr, realsize)
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
void jump(const btVector3 &v=btVector3(0, 0, 0))
#define FAIL_IF(condition)
#define CHECK_ERROR(condition)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
ccl_device_inline float2 fmod(const float2 a, const float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static double op_eq(double a, double b)
static bool parse_and(ExprParseState *state)
static double op_sub(double a, double b)
static void parse_set_jump(ExprParseState *state, int jump)
static bool parse_unary(ExprParseState *state)
static BuiltinOpDef builtin_ops[]
static BinaryOpFunc parse_get_cmp_func(short token)
static bool parse_cmp(ExprParseState *state)
static double op_degrees(double arg)
ExprPyLike_Parsed * BLI_expr_pylike_parse(const char *expression, const char **param_names, int param_names_len)
@ EXPR_PYLIKE_DIV_BY_ZERO
@ EXPR_PYLIKE_FATAL_ERROR
static bool parse_not(ExprParseState *state)
static bool parse_mul(ExprParseState *state)
static bool parse_next_token(ExprParseState *state)
static const char * token_eq_characters
bool BLI_expr_pylike_is_constant(const struct ExprPyLike_Parsed *expr)
double(*)(double) UnaryOpFunc
static bool parse_expr(ExprParseState *state)
static double op_smoothstep(double a, double b, double x)
static double op_lt(double a, double b)
static bool parse_cmp_chain(ExprParseState *state, BinaryOpFunc cur_func)
static double op_ne(double a, double b)
static double op_log2(double a, double b)
static int parse_add_jump(ExprParseState *state, eOpCode code)
eExprPyLike_EvalStatus BLI_expr_pylike_eval(struct ExprPyLike_Parsed *expr, const double *param_values, int param_values_len, double *r_result)
static double op_ge(double a, double b)
static const char * token_characters
void BLI_expr_pylike_free(struct ExprPyLike_Parsed *expr)
static bool parse_add(ExprParseState *state)
static bool parse_or(ExprParseState *state)
static ExprOp * parse_add_op(ExprParseState *state, eOpCode code, int stack_delta)
static KeywordTokenDef keyword_list[]
static double op_le(double a, double b)
static double op_gt(double a, double b)
static double op_clamp3(double arg, double minv, double maxv)
static BuiltinConstDef builtin_consts[]
static double op_div(double a, double b)
static double op_add(double a, double b)
static double op_negate(double arg)
static bool parse_add_func(ExprParseState *state, std::variant< UnaryOpFunc, BinaryOpFunc, TernaryOpFunc > funcptr)
static double op_lerp(double a, double b, double x)
double(*)(double, double) BinaryOpFunc
static int parse_function_args(ExprParseState *state)
bool BLI_expr_pylike_is_valid(const struct ExprPyLike_Parsed *expr)
double(*)(double, double, double) TernaryOpFunc
static double op_not(double a)
static double op_clamp(double arg)
static double op_radians(double arg)
bool BLI_expr_pylike_is_using_param(const struct ExprPyLike_Parsed *expr, int index)
static double op_mul(double a, double b)
std::variant< UnaryOpFunc, BinaryOpFunc, TernaryOpFunc > funcptr
union blender::ExprOp::@230055030022137245332140206007331131374055034155 arg
const char ** param_names