Cleanup: BLI_expr_pylike argument ordering

- Order array length after the array.
- Put return argument last.
This commit is contained in:
Campbell Barton 2018-09-19 11:40:40 +10:00
parent 3aea5695bb
commit d44c8ce1cb
4 changed files with 37 additions and 31 deletions

@ -1900,8 +1900,8 @@ ChannelDriver *fcurve_copy_driver(const ChannelDriver *driver)
static ExprPyLike_Parsed *driver_compile_simple_expr_impl(ChannelDriver *driver)
{
/* Prepare parameter names. */
int num_vars = BLI_listbase_count(&driver->variables);
const char **names = BLI_array_alloca(names, num_vars + 1);
int names_len = BLI_listbase_count(&driver->variables);
const char **names = BLI_array_alloca(names, names_len + 1);
int i = 0;
names[i++] = "frame";
@ -1910,14 +1910,14 @@ static ExprPyLike_Parsed *driver_compile_simple_expr_impl(ChannelDriver *driver)
names[i++] = dvar->name;
}
return BLI_expr_pylike_parse(driver->expression, num_vars + 1, names);
return BLI_expr_pylike_parse(driver->expression, names, names_len + 1);
}
static bool driver_evaluate_simple_expr(ChannelDriver *driver, ExprPyLike_Parsed *expr, float *result, float time)
{
/* Prepare parameter values. */
int num_vars = BLI_listbase_count(&driver->variables);
double *vars = BLI_array_alloca(vars, num_vars + 1);
int vars_len = BLI_listbase_count(&driver->variables);
double *vars = BLI_array_alloca(vars, vars_len + 1);
int i = 0;
vars[i++] = time;
@ -1928,7 +1928,7 @@ static bool driver_evaluate_simple_expr(ChannelDriver *driver, ExprPyLike_Parsed
/* Evaluate expression. */
double result_val;
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &result_val, num_vars + 1, vars);
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, vars, vars_len + 1, &result_val);
const char *message;
switch (status) {

@ -52,9 +52,12 @@ void BLI_expr_pylike_free(struct ExprPyLike_Parsed *expr);
bool BLI_expr_pylike_is_valid(struct ExprPyLike_Parsed *expr);
bool BLI_expr_pylike_is_constant(struct ExprPyLike_Parsed *expr);
ExprPyLike_Parsed *BLI_expr_pylike_parse(
const char *expression, int num_params, const char **param_names);
const char *expression,
const char **param_names, int param_names_len);
eExprPyLike_EvalStatus BLI_expr_pylike_eval(
struct ExprPyLike_Parsed *expr, double *result, int num_params, const double *params);
struct ExprPyLike_Parsed *expr,
const double *param_values, int param_values_len,
double *r_result);
#ifdef __cplusplus
}

@ -150,9 +150,12 @@ bool BLI_expr_pylike_is_constant(ExprPyLike_Parsed *expr)
* Evaluate the expression with the given parameters.
* The order and number of parameters must match the names given to parse.
*/
eExprPyLike_EvalStatus BLI_expr_pylike_eval(ExprPyLike_Parsed *expr, double *result, int num_params, const double *params)
eExprPyLike_EvalStatus BLI_expr_pylike_eval(
ExprPyLike_Parsed *expr,
const double *param_values, int param_values_len,
double *r_result)
{
*result = 0.0;
*r_result = 0.0;
if (!BLI_expr_pylike_is_valid(expr)) {
return EXPR_PYLIKE_INVALID;
@ -179,8 +182,8 @@ eExprPyLike_EvalStatus BLI_expr_pylike_eval(ExprPyLike_Parsed *expr, double *res
stack[sp++] = ops[pc].arg.dval;
break;
case OPCODE_PARAMETER:
FAIL_IF(sp >= expr->max_stack || ops[pc].arg.ival >= num_params);
stack[sp++] = params[ops[pc].arg.ival];
FAIL_IF(sp >= expr->max_stack || ops[pc].arg.ival >= param_values_len);
stack[sp++] = param_values[ops[pc].arg.ival];
break;
case OPCODE_FUNC1:
FAIL_IF(sp < 1);
@ -249,7 +252,7 @@ eExprPyLike_EvalStatus BLI_expr_pylike_eval(ExprPyLike_Parsed *expr, double *res
#undef FAIL_IF
*result = stack[0];
*r_result = stack[0];
/* Detect floating point evaluation errors. */
int flags = fetestexcept(FE_DIVBYZERO | FE_INVALID);
@ -412,7 +415,7 @@ static KeywordTokenDef keyword_list[] = {
};
typedef struct SimpleExprParseState {
int param_count;
int param_names_len;
const char **param_names;
/* Original expression */
@ -682,7 +685,7 @@ static bool parse_unary(SimpleExprParseState *state)
case TOKEN_ID:
/* Parameters: search in reverse order in case of duplicate names - the last one should win. */
for (i = state->param_count - 1; i >= 0; i--) {
for (i = state->param_names_len - 1; i >= 0; i--) {
if (STREQ(state->tokenbuf, state->param_names[i])) {
parse_add_op(state, OPCODE_PARAMETER, 1)->arg.ival = i;
return parse_next_token(state);
@ -931,7 +934,7 @@ static bool parse_expr(SimpleExprParseState *state)
* Parse the expression for evaluation later.
* Returns non-NULL even on failure; use is_valid to check.
*/
ExprPyLike_Parsed *BLI_expr_pylike_parse(const char *expression, int num_params, const char **param_names)
ExprPyLike_Parsed *BLI_expr_pylike_parse(const char *expression, const char **param_names, int param_names_len)
{
/* Prepare the parser state. */
SimpleExprParseState state;
@ -939,7 +942,7 @@ ExprPyLike_Parsed *BLI_expr_pylike_parse(const char *expression, int num_params,
state.cur = state.expr = expression;
state.param_count = num_params;
state.param_names_len = param_names_len;
state.param_names = param_names;
state.tokenbuf = MEM_mallocN(strlen(expression) + 1, __func__);

@ -14,7 +14,7 @@ extern "C" {
static void expr_pylike_parse_fail_test(const char *str)
{
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, 0, NULL);
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, NULL, 0);
EXPECT_FALSE(BLI_expr_pylike_is_valid(expr));
@ -23,7 +23,7 @@ static void expr_pylike_parse_fail_test(const char *str)
static void expr_pylike_const_test(const char *str, double value, bool force_const)
{
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, 0, NULL);
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, NULL, 0);
if (force_const) {
EXPECT_TRUE(BLI_expr_pylike_is_constant(expr));
@ -34,7 +34,7 @@ static void expr_pylike_const_test(const char *str, double value, bool force_con
}
double result;
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &result, 0, NULL);
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, NULL, 0, &result);
EXPECT_EQ(status, EXPR_PYLIKE_SUCCESS);
EXPECT_EQ(result, value);
@ -44,8 +44,8 @@ static void expr_pylike_const_test(const char *str, double value, bool force_con
static ExprPyLike_Parsed *parse_for_eval(const char *str, bool nonconst)
{
const char *names[1] = { "x" };
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, 1, names);
const char *names[1] = {"x"};
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, names, ARRAY_SIZE(names));
EXPECT_TRUE(BLI_expr_pylike_is_valid(expr));
@ -59,7 +59,7 @@ static ExprPyLike_Parsed *parse_for_eval(const char *str, bool nonconst)
static void verify_eval_result(ExprPyLike_Parsed *expr, double x, double value)
{
double result;
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &result, 1, &x);
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &x, 1, &result);
EXPECT_EQ(status, EXPR_PYLIKE_SUCCESS);
EXPECT_EQ(result, value);
@ -77,7 +77,7 @@ static void expr_pylike_error_test(const char *str, double x, eExprPyLike_EvalSt
ExprPyLike_Parsed *expr = parse_for_eval(str, false);
double result;
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &result, 1, &x);
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &x, 1, &result);
EXPECT_EQ(status, error);
@ -253,15 +253,15 @@ TEST(expr_pylike, Eval_Ternary1)
TEST(expr_pylike, MultipleArgs)
{
const char* names[3] = { "x", "y", "x" };
double values[3] = { 1.0, 2.0, 3.0 };
const char* names[3] = {"x", "y", "x"};
double values[3] = {1.0, 2.0, 3.0};
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("x*10 + y", 3, names);
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("x*10 + y", names, ARRAY_SIZE(names));
EXPECT_TRUE(BLI_expr_pylike_is_valid(expr));
double result;
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &result, 3, values);
eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, values, 3, &result);
EXPECT_EQ(status, EXPR_PYLIKE_SUCCESS);
EXPECT_EQ(result, 32.0);
@ -291,10 +291,10 @@ TEST_ERROR(Mixed3, "sqrt(x) + 1 / max(0, x)", 1.0, EXPR_PYLIKE_SUCCESS)
TEST(expr_pylike, Error_Invalid)
{
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("", 0, NULL);
ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("", NULL, 0);
double result;
EXPECT_EQ(BLI_expr_pylike_eval(expr, &result, 0, NULL), EXPR_PYLIKE_INVALID);
EXPECT_EQ(BLI_expr_pylike_eval(expr, NULL, 0, &result), EXPR_PYLIKE_INVALID);
BLI_expr_pylike_free(expr);
}
@ -304,7 +304,7 @@ TEST(expr_pylike, Error_ArgumentCount)
ExprPyLike_Parsed *expr = parse_for_eval("x", false);
double result;
EXPECT_EQ(BLI_expr_pylike_eval(expr, &result, 0, NULL), EXPR_PYLIKE_FATAL_ERROR);
EXPECT_EQ(BLI_expr_pylike_eval(expr, NULL, 0, &result), EXPR_PYLIKE_FATAL_ERROR);
BLI_expr_pylike_free(expr);
}