mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
eval: Fix duplicate code in trig/math functions #3035
The same error checking/recovery code was duplicated among the f_acos, f_asin, f_atan, f_ceil, f_cos, f_cosh, f_exp, f_floor, f_log, f_log10, f_round, f_sin, f_sinh, f_sqrt, f_tan, f_tanh and f_trunc functions. This commit moves that code into a wrapper function. `trunc` is not in C90, but it is in C99, which is what neovim targets, so we use it here.
This commit is contained in:
parent
14ae3c0cbd
commit
7732bec9b8
156
src/nvim/eval.c
156
src/nvim/eval.c
@ -7088,7 +7088,7 @@ static int non_zero_arg(typval_T *argvars)
|
|||||||
* Get the float value of "argvars[0]" into "f".
|
* Get the float value of "argvars[0]" into "f".
|
||||||
* Returns FAIL when the argument is not a Number or Float.
|
* Returns FAIL when the argument is not a Number or Float.
|
||||||
*/
|
*/
|
||||||
static int get_float_arg(typval_T *argvars, float_T *f)
|
static inline int get_float_arg(typval_T *argvars, float_T *f)
|
||||||
{
|
{
|
||||||
if (argvars[0].v_type == VAR_FLOAT) {
|
if (argvars[0].v_type == VAR_FLOAT) {
|
||||||
*f = argvars[0].vval.v_float;
|
*f = argvars[0].vval.v_float;
|
||||||
@ -7102,14 +7102,27 @@ static int get_float_arg(typval_T *argvars, float_T *f)
|
|||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply a floating point C function on a typval with one float_T.
|
||||||
|
static inline void float_op_wrapper(typval_T *argvars, typval_T *rettv,
|
||||||
|
float_T (*function)(float_T))
|
||||||
|
{
|
||||||
|
float_T f;
|
||||||
|
|
||||||
|
rettv->v_type = VAR_FLOAT;
|
||||||
|
if (get_float_arg(argvars, &f) == OK) {
|
||||||
|
rettv->vval.v_float = function(f);
|
||||||
|
} else {
|
||||||
|
rettv->vval.v_float = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "abs(expr)" function
|
* "abs(expr)" function
|
||||||
*/
|
*/
|
||||||
static void f_abs(typval_T *argvars, typval_T *rettv)
|
static void f_abs(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
if (argvars[0].v_type == VAR_FLOAT) {
|
if (argvars[0].v_type == VAR_FLOAT) {
|
||||||
rettv->v_type = VAR_FLOAT;
|
float_op_wrapper(argvars, rettv, &fabs);
|
||||||
rettv->vval.v_float = fabs(argvars[0].vval.v_float);
|
|
||||||
} else {
|
} else {
|
||||||
varnumber_T n;
|
varnumber_T n;
|
||||||
int error = FALSE;
|
int error = FALSE;
|
||||||
@ -7129,13 +7142,7 @@ static void f_abs(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_acos(typval_T *argvars, typval_T *rettv)
|
static void f_acos(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &acos);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = acos(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -7289,13 +7296,7 @@ static void f_argv(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_asin(typval_T *argvars, typval_T *rettv)
|
static void f_asin(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &asin);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = asin(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -7303,13 +7304,7 @@ static void f_asin(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_atan(typval_T *argvars, typval_T *rettv)
|
static void f_atan(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &atan);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = atan(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -7638,13 +7633,7 @@ static void f_call(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_ceil(typval_T *argvars, typval_T *rettv)
|
static void f_ceil(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &ceil);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = ceil(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -7848,13 +7837,7 @@ static void f_copy(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_cos(typval_T *argvars, typval_T *rettv)
|
static void f_cos(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &cos);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = cos(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -7862,13 +7845,7 @@ static void f_cos(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_cosh(typval_T *argvars, typval_T *rettv)
|
static void f_cosh(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &cosh);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = cosh(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -8267,13 +8244,7 @@ static void f_exists(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_exp(typval_T *argvars, typval_T *rettv)
|
static void f_exp(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &exp);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = exp(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -8752,13 +8723,7 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_floor(typval_T *argvars, typval_T *rettv)
|
static void f_floor(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &floor);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = floor(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -11313,13 +11278,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact)
|
|||||||
*/
|
*/
|
||||||
static void f_log(typval_T *argvars, typval_T *rettv)
|
static void f_log(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &log);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = log(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -11327,13 +11286,7 @@ static void f_log(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_log10(typval_T *argvars, typval_T *rettv)
|
static void f_log10(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &log10);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = log10(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -12763,13 +12716,7 @@ theend:
|
|||||||
*/
|
*/
|
||||||
static void f_round(typval_T *argvars, typval_T *rettv)
|
static void f_round(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &round);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = round(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// "rpcnotify()" function
|
// "rpcnotify()" function
|
||||||
@ -13900,13 +13847,7 @@ static void f_simplify(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_sin(typval_T *argvars, typval_T *rettv)
|
static void f_sin(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &sin);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = sin(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -13914,13 +13855,7 @@ static void f_sin(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_sinh(typval_T *argvars, typval_T *rettv)
|
static void f_sinh(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &sinh);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = sinh(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// struct used in the array that's given to qsort()
|
/// struct used in the array that's given to qsort()
|
||||||
@ -14391,13 +14326,7 @@ static void f_split(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_sqrt(typval_T *argvars, typval_T *rettv)
|
static void f_sqrt(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &sqrt);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = sqrt(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -15281,13 +15210,7 @@ static void f_test(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_tan(typval_T *argvars, typval_T *rettv)
|
static void f_tan(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &tan);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = tan(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -15295,13 +15218,7 @@ static void f_tan(typval_T *argvars, typval_T *rettv)
|
|||||||
*/
|
*/
|
||||||
static void f_tanh(typval_T *argvars, typval_T *rettv)
|
static void f_tanh(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &tanh);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
rettv->vval.v_float = tanh(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -15449,14 +15366,7 @@ error:
|
|||||||
*/
|
*/
|
||||||
static void f_trunc(typval_T *argvars, typval_T *rettv)
|
static void f_trunc(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
float_T f;
|
float_op_wrapper(argvars, rettv, &trunc);
|
||||||
|
|
||||||
rettv->v_type = VAR_FLOAT;
|
|
||||||
if (get_float_arg(argvars, &f) == OK)
|
|
||||||
/* trunc() is not in C90, use floor() or ceil() instead. */
|
|
||||||
rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
|
|
||||||
else
|
|
||||||
rettv->vval.v_float = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user