|
Blender
V2.93
|
#include <ctype.h>#include <fenv.h>#include <float.h>#include <math.h>#include <stddef.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "MEM_guardedalloc.h"#include "BLI_alloca.h"#include "BLI_expr_pylike_eval.h"#include "BLI_math_base.h"#include "BLI_utildefines.h"Go to the source code of this file.
Classes | |
| struct | ExprOp |
| struct | ExprPyLike_Parsed |
| struct | BuiltinConstDef |
| struct | BuiltinOpDef |
| struct | KeywordTokenDef |
| struct | ExprParseState |
Macros | |
| #define | FAIL_IF(condition) |
Expression Parser State | |
| #define | MAKE_CHAR2(a, b) (((a) << 8) | (b)) |
| #define | CHECK_ERROR(condition) |
| #define | TOKEN_ID MAKE_CHAR2('I', 'D') |
| #define | TOKEN_NUMBER MAKE_CHAR2('0', '0') |
| #define | TOKEN_GE MAKE_CHAR2('>', '=') |
| #define | TOKEN_LE MAKE_CHAR2('<', '=') |
| #define | TOKEN_NE MAKE_CHAR2('!', '=') |
| #define | TOKEN_EQ MAKE_CHAR2('=', '=') |
| #define | TOKEN_AND MAKE_CHAR2('A', 'N') |
| #define | TOKEN_OR MAKE_CHAR2('O', 'R') |
| #define | TOKEN_NOT MAKE_CHAR2('N', 'O') |
| #define | TOKEN_IF MAKE_CHAR2('I', 'F') |
| #define | TOKEN_ELSE MAKE_CHAR2('E', 'L') |
| typedef struct KeywordTokenDef | KeywordTokenDef |
| typedef struct ExprParseState | ExprParseState |
| static const char * | token_eq_characters = "!=><" |
| static const char * | token_characters = "~`!@#$%^&*+-=/\\?:;<>(){}[]|.,\"'" |
| static KeywordTokenDef | keyword_list [] |
| static ExprOp * | parse_alloc_ops (ExprParseState *state, int count) |
| static ExprOp * | parse_add_op (ExprParseState *state, eOpCode code, int stack_delta) |
| static int | parse_add_jump (ExprParseState *state, eOpCode code) |
| static void | parse_set_jump (ExprParseState *state, int jump) |
| static int | opcode_arg_count (eOpCode code) |
| static bool | parse_add_func (ExprParseState *state, eOpCode code, int args, void *funcptr) |
| static bool | parse_next_token (ExprParseState *state) |
Internal Types | |
| enum | eOpCode { OPCODE_CONST , OPCODE_FUNC1 , OPCODE_FUNC2 , OPCODE_FUNC3 , OPCODE_PARAMETER , OPCODE_MIN , OPCODE_MAX , OPCODE_JMP , OPCODE_JMP_ELSE , OPCODE_JMP_OR , OPCODE_JMP_AND , OPCODE_CMP_CHAIN } |
| typedef enum eOpCode | eOpCode |
| typedef double(* | UnaryOpFunc) (double) |
| typedef double(* | BinaryOpFunc) (double, double) |
| typedef double(* | TernaryOpFunc) (double, double, double) |
| typedef struct ExprOp | ExprOp |
Built-In Operations | |
| typedef struct BuiltinConstDef | BuiltinConstDef |
| typedef struct BuiltinOpDef | BuiltinOpDef |
| static BuiltinConstDef | builtin_consts [] |
| static BuiltinOpDef | builtin_ops [] |
| static double | op_negate (double arg) |
| static double | op_mul (double a, double b) |
| static double | op_div (double a, double b) |
| static double | op_add (double a, double b) |
| static double | op_sub (double a, double b) |
| static double | op_radians (double arg) |
| static double | op_degrees (double arg) |
| static double | op_log2 (double a, double b) |
| static double | op_lerp (double a, double b, double x) |
| static double | op_clamp (double arg) |
| static double | op_clamp3 (double arg, double minv, double maxv) |
| static double | op_smoothstep (double a, double b, double x) |
| static double | op_not (double a) |
| static double | op_eq (double a, double b) |
| static double | op_ne (double a, double b) |
| static double | op_lt (double a, double b) |
| static double | op_le (double a, double b) |
| static double | op_gt (double a, double b) |
| static double | op_ge (double a, double b) |
Simple evaluator for a subset of Python expressions that can be computed using purely double precision floating point values.
Supported subset:
The implementation has no global state and can be used multi-threaded.
Definition in file expr_pylike_eval.c.
| #define CHECK_ERROR | ( | condition | ) |
Definition at line 461 of file expr_pylike_eval.c.
| #define FAIL_IF | ( | condition | ) |
| #define MAKE_CHAR2 | ( | a, | |
| b | |||
| ) | (((a) << 8) | (b)) |
Definition at line 459 of file expr_pylike_eval.c.
| #define TOKEN_AND MAKE_CHAR2('A', 'N') |
Definition at line 475 of file expr_pylike_eval.c.
| #define TOKEN_ELSE MAKE_CHAR2('E', 'L') |
Definition at line 479 of file expr_pylike_eval.c.
| #define TOKEN_EQ MAKE_CHAR2('=', '=') |
Definition at line 474 of file expr_pylike_eval.c.
| #define TOKEN_GE MAKE_CHAR2('>', '=') |
Definition at line 471 of file expr_pylike_eval.c.
| #define TOKEN_ID MAKE_CHAR2('I', 'D') |
Definition at line 469 of file expr_pylike_eval.c.
| #define TOKEN_IF MAKE_CHAR2('I', 'F') |
Definition at line 478 of file expr_pylike_eval.c.
| #define TOKEN_LE MAKE_CHAR2('<', '=') |
Definition at line 472 of file expr_pylike_eval.c.
| #define TOKEN_NE MAKE_CHAR2('!', '=') |
Definition at line 473 of file expr_pylike_eval.c.
| #define TOKEN_NOT MAKE_CHAR2('N', 'O') |
Definition at line 477 of file expr_pylike_eval.c.
| #define TOKEN_NUMBER MAKE_CHAR2('0', '0') |
Definition at line 470 of file expr_pylike_eval.c.
| #define TOKEN_OR MAKE_CHAR2('O', 'R') |
Definition at line 476 of file expr_pylike_eval.c.
Definition at line 96 of file expr_pylike_eval.c.
| typedef struct BuiltinConstDef BuiltinConstDef |
| typedef struct BuiltinOpDef BuiltinOpDef |
| typedef struct ExprParseState ExprParseState |
| typedef struct KeywordTokenDef KeywordTokenDef |
Definition at line 97 of file expr_pylike_eval.c.
Definition at line 95 of file expr_pylike_eval.c.
| enum eOpCode |
| Enumerator | |
|---|---|
| OPCODE_CONST | |
| OPCODE_FUNC1 | |
| OPCODE_FUNC2 | |
| OPCODE_FUNC3 | |
| OPCODE_PARAMETER | |
| OPCODE_MIN | |
| OPCODE_MAX | |
| OPCODE_JMP | |
| OPCODE_JMP_ELSE | |
| OPCODE_JMP_OR | |
| OPCODE_JMP_AND | |
| OPCODE_CMP_CHAIN | |
Definition at line 68 of file expr_pylike_eval.c.
| eExprPyLike_EvalStatus BLI_expr_pylike_eval | ( | ExprPyLike_Parsed * | expr, |
| const double * | param_values, | ||
| int | param_values_len, | ||
| double * | r_result | ||
| ) |
Evaluate the expression with the given parameters. The order and number of parameters must match the names given to parse.
Definition at line 175 of file expr_pylike_eval.c.
References ExprOp::arg, BLI_array_alloca, BLI_expr_pylike_is_valid(), CLAMP_MAX, CLAMP_MIN, ExprOp::dval, EXPR_PYLIKE_DIV_BY_ZERO, EXPR_PYLIKE_FATAL_ERROR, EXPR_PYLIKE_INVALID, EXPR_PYLIKE_MATH_ERROR, EXPR_PYLIKE_SUCCESS, FAIL_IF, ExprOp::ival, ExprPyLike_Parsed::max_stack, OPCODE_CMP_CHAIN, OPCODE_CONST, OPCODE_FUNC1, OPCODE_FUNC2, OPCODE_FUNC3, OPCODE_JMP, OPCODE_JMP_AND, OPCODE_JMP_ELSE, OPCODE_JMP_OR, OPCODE_MAX, OPCODE_MIN, OPCODE_PARAMETER, ExprPyLike_Parsed::ops, and ExprPyLike_Parsed::ops_count.
Referenced by driver_evaluate_simple_expr(), expr_pylike_const_test(), expr_pylike_error_test(), TEST(), and verify_eval_result().
| void BLI_expr_pylike_free | ( | ExprPyLike_Parsed * | expr | ) |
Free the parsed data; NULL argument is ok.
Definition at line 128 of file expr_pylike_eval.c.
References MEM_freeN, and NULL.
Referenced by BKE_driver_invalidate_expression(), driver_compile_simple_expr(), and fcurve_free_driver().
| bool BLI_expr_pylike_is_constant | ( | ExprPyLike_Parsed * | expr | ) |
Check if the parsed expression always evaluates to the same value.
Definition at line 142 of file expr_pylike_eval.c.
References NULL, ExprOp::opcode, OPCODE_CONST, ExprPyLike_Parsed::ops, and ExprPyLike_Parsed::ops_count.
Referenced by expr_pylike_const_test(), and parse_for_eval().
| bool BLI_expr_pylike_is_using_param | ( | ExprPyLike_Parsed * | expr, |
| int | index | ||
| ) |
Check if the parsed expression uses the parameter with the given index.
Definition at line 148 of file expr_pylike_eval.c.
References ExprOp::arg, ExprOp::ival, NULL, ExprOp::opcode, OPCODE_PARAMETER, ExprPyLike_Parsed::ops, and ExprPyLike_Parsed::ops_count.
Referenced by driver_check_simple_expr_depends_on_time(), and TEST().
| bool BLI_expr_pylike_is_valid | ( | ExprPyLike_Parsed * | expr | ) |
Check if the parsing result is valid for evaluation.
Definition at line 136 of file expr_pylike_eval.c.
References NULL, and ExprPyLike_Parsed::ops_count.
Referenced by BKE_driver_has_simple_expression(), BLI_expr_pylike_eval(), driver_try_evaluate_simple_expr(), expr_pylike_const_test(), expr_pylike_parse_fail_test(), parse_for_eval(), and TEST().
| ExprPyLike_Parsed* BLI_expr_pylike_parse | ( | const char * | expression, |
| const char ** | param_names, | ||
| int | param_names_len | ||
| ) |
Compile the expression and return the result.
Parse the expression for evaluation later. Returns non-NULL even on failure; use is_valid to check.
Definition at line 1082 of file expr_pylike_eval.c.
References BLI_assert, ExprPyLike_Parsed::max_stack, MEM_callocN, MEM_freeN, MEM_mallocN, ExprPyLike_Parsed::ops, ExprPyLike_Parsed::ops_count, parse_expr(), parse_next_token(), and state.
Referenced by driver_compile_simple_expr_impl(), expr_pylike_const_test(), expr_pylike_parse_fail_test(), parse_for_eval(), and TEST().
Definition at line 318 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_add().
Definition at line 348 of file expr_pylike_eval.c.
References CLAMP.
Definition at line 354 of file expr_pylike_eval.c.
References CLAMP.
Definition at line 333 of file expr_pylike_eval.c.
References M_PI.
Definition at line 313 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_mul().
Definition at line 372 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 397 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 392 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 387 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 343 of file expr_pylike_eval.c.
References Freestyle::a, and x.
Definition at line 338 of file expr_pylike_eval.c.
References Freestyle::a, and KDL::log().
Definition at line 382 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 308 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_mul().
Definition at line 377 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_get_cmp_func().
Definition at line 303 of file expr_pylike_eval.c.
Referenced by parse_unary().
Definition at line 367 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by parse_not().
Definition at line 328 of file expr_pylike_eval.c.
References M_PI.
Definition at line 360 of file expr_pylike_eval.c.
References Freestyle::a, CLAMP, t, and x.
Definition at line 323 of file expr_pylike_eval.c.
References Freestyle::a.
Referenced by bmo_connect_vert_pair_exec(), bmo_contextual_create_exec(), bridge_loop_pair(), and parse_add().
|
static |
Definition at line 562 of file expr_pylike_eval.c.
References BLI_assert, OPCODE_FUNC1, OPCODE_FUNC2, and OPCODE_FUNC3.
Referenced by parse_unary().
|
static |
Definition at line 900 of file expr_pylike_eval.c.
References CHECK_ERROR, op_add(), op_sub(), OPCODE_FUNC2, parse_add_func(), parse_mul(), parse_next_token(), and state.
Referenced by parse_cmp(), and parse_cmp_chain().
|
static |
Definition at line 578 of file expr_pylike_eval.c.
References ExprOp::arg, BLI_assert, CHECK_ERROR, ExprOp::dval, OPCODE_CONST, OPCODE_FUNC1, OPCODE_FUNC2, OPCODE_FUNC3, parse_add_op(), ExprOp::ptr, result, and state.
Referenced by parse_add(), parse_cmp_chain(), parse_mul(), parse_not(), and parse_unary().
|
static |
Definition at line 548 of file expr_pylike_eval.c.
References parse_add_op(), and state.
Referenced by parse_and(), parse_expr(), and parse_or().
|
static |
Definition at line 533 of file expr_pylike_eval.c.
References CLAMP_MIN, ExprOp::opcode, parse_alloc_ops(), and state.
Referenced by parse_add_func(), parse_add_jump(), parse_cmp_chain(), and parse_unary().
|
static |
Definition at line 520 of file expr_pylike_eval.c.
References count, MEM_reallocN, power_of_2_max_i(), and state.
Referenced by parse_add_op(), and parse_expr().
|
static |
Definition at line 988 of file expr_pylike_eval.c.
References CHECK_ERROR, jump(), OPCODE_JMP_AND, parse_add_jump(), parse_next_token(), parse_not(), parse_set_jump(), state, and TOKEN_AND.
Referenced by parse_or().
|
static |
Definition at line 962 of file expr_pylike_eval.c.
References CHECK_ERROR, parse_add(), parse_cmp_chain(), parse_get_cmp_func(), parse_next_token(), and state.
Referenced by parse_not().
|
static |
Definition at line 942 of file expr_pylike_eval.c.
References ExprOp::arg, CHECK_ERROR, ExprOp::func2, jump(), OPCODE_CMP_CHAIN, OPCODE_FUNC2, parse_add(), parse_add_func(), parse_add_op(), parse_get_cmp_func(), parse_next_token(), parse_set_jump(), and state.
Referenced by parse_cmp().
|
static |
Definition at line 1018 of file expr_pylike_eval.c.
References CHECK_ERROR, MEM_freeN, MEM_mallocN, OPCODE_JMP, OPCODE_JMP_ELSE, parse_add_jump(), parse_alloc_ops(), parse_next_token(), parse_or(), parse_set_jump(), size(), state, TOKEN_ELSE, and TOKEN_IF.
Referenced by BLI_expr_pylike_parse(), parse_function_args(), and parse_unary().
|
static |
Definition at line 761 of file expr_pylike_eval.c.
References parse_expr(), parse_next_token(), and state.
Referenced by parse_unary().
|
static |
Definition at line 922 of file expr_pylike_eval.c.
References NULL, op_eq(), op_ge(), op_gt(), op_le(), op_lt(), op_ne(), TOKEN_EQ, TOKEN_GE, TOKEN_LE, and TOKEN_NE.
Referenced by parse_cmp(), and parse_cmp_chain().
|
static |
Definition at line 878 of file expr_pylike_eval.c.
References CHECK_ERROR, op_div(), op_mul(), OPCODE_FUNC2, parse_add_func(), parse_next_token(), parse_unary(), and state.
Referenced by parse_add().
|
static |
Definition at line 654 of file expr_pylike_eval.c.
References CHECK_ERROR, ELEM, keyword_list, MAKE_CHAR2, KeywordTokenDef::name, state, STREQ, KeywordTokenDef::token, token_characters, token_eq_characters, TOKEN_ID, and TOKEN_NUMBER.
Referenced by BLI_expr_pylike_parse(), parse_add(), parse_and(), parse_cmp(), parse_cmp_chain(), parse_expr(), parse_function_args(), parse_mul(), parse_not(), parse_or(), and parse_unary().
|
static |
Definition at line 977 of file expr_pylike_eval.c.
References CHECK_ERROR, op_not(), OPCODE_FUNC1, parse_add_func(), parse_cmp(), parse_next_token(), state, and TOKEN_NOT.
Referenced by parse_and().
|
static |
Definition at line 1003 of file expr_pylike_eval.c.
References CHECK_ERROR, jump(), OPCODE_JMP_OR, parse_add_jump(), parse_and(), parse_next_token(), parse_set_jump(), state, and TOKEN_OR.
Referenced by parse_expr().
|
static |
Definition at line 555 of file expr_pylike_eval.c.
Referenced by parse_and(), parse_cmp_chain(), parse_expr(), and parse_or().
|
static |
Definition at line 795 of file expr_pylike_eval.c.
References ExprOp::arg, builtin_consts, builtin_ops, CHECK_ERROR, ExprOp::dval, ExprOp::ival, BuiltinConstDef::name, BuiltinOpDef::name, op_negate(), opcode_arg_count(), OPCODE_CONST, OPCODE_FUNC1, OPCODE_MAX, OPCODE_MIN, OPCODE_PARAMETER, parse_add_func(), parse_add_op(), parse_expr(), parse_function_args(), parse_next_token(), state, STREQ, TOKEN_ID, TOKEN_NUMBER, and BuiltinConstDef::value.
Referenced by parse_mul().
|
static |
|
static |
Definition at line 423 of file expr_pylike_eval.c.
Referenced by parse_unary().
|
static |
Definition at line 489 of file expr_pylike_eval.c.
Referenced by denoise_func(), merge_func(), and parse_next_token().
|
static |
Definition at line 482 of file expr_pylike_eval.c.
Referenced by parse_next_token().
|
static |
Definition at line 481 of file expr_pylike_eval.c.
Referenced by parse_next_token().