eval: Move get_tv_number[_chk] to eval/typval.c

This commit is contained in:
ZyX
2016-08-28 08:55:58 +03:00
parent 1b3e13da5b
commit 233b0c93bb
5 changed files with 446 additions and 373 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -50,7 +50,7 @@ int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2,
} }
if (*op == '+' || *op == '-') { if (*op == '+' || *op == '-') {
// nr += nr or nr -= nr // nr += nr or nr -= nr
varnumber_T n = get_tv_number(tv1); varnumber_T n = tv_get_number(tv1);
if (tv2->v_type == VAR_FLOAT) { if (tv2->v_type == VAR_FLOAT) {
float_T f = n; float_T f = n;
@@ -64,9 +64,9 @@ int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2,
tv1->vval.v_float = f; tv1->vval.v_float = f;
} else { } else {
if (*op == '+') { if (*op == '+') {
n += get_tv_number(tv2); n += tv_get_number(tv2);
} else { } else {
n -= get_tv_number(tv2); n -= tv_get_number(tv2);
} }
tv_clear(tv1); tv_clear(tv1);
tv1->v_type = VAR_NUMBER; tv1->v_type = VAR_NUMBER;
@@ -96,7 +96,7 @@ int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2,
} }
const float_T f = (tv2->v_type == VAR_FLOAT const float_T f = (tv2->v_type == VAR_FLOAT
? tv2->vval.v_float ? tv2->vval.v_float
: get_tv_number(tv2)); : tv_get_number(tv2));
if (*op == '+') { if (*op == '+') {
tv1->vval.v_float += f; tv1->vval.v_float += f;
} else { } else {

View File

@@ -17,6 +17,7 @@
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/pos.h" #include "nvim/pos.h"
#include "nvim/charset.h"
// TODO(ZyX-I): Move line_breakcheck out of misc1 // TODO(ZyX-I): Move line_breakcheck out of misc1
#include "nvim/misc1.h" // For line_breakcheck #include "nvim/misc1.h" // For line_breakcheck
@@ -725,7 +726,7 @@ varnumber_T tv_list_find_nr(list_T *const l, const int n, bool *ret_error)
} }
return -1; return -1;
} }
return get_tv_number_chk(&li->li_tv, ret_error); return tv_get_number_chk(&li->li_tv, ret_error);
} }
/// Get list item l[n - 1] as a string /// Get list item l[n - 1] as a string
@@ -1087,7 +1088,7 @@ varnumber_T tv_dict_get_number(dict_T *const d, const char *const key)
if (di == NULL) { if (di == NULL) {
return 0; return 0;
} }
return get_tv_number(&di->di_tv); return tv_get_number(&di->di_tv);
} }
/// Get a string item from a dictionary /// Get a string item from a dictionary
@@ -1971,7 +1972,7 @@ bool tv_equal(typval_T *const tv1, typval_T *const tv2, const bool ic,
/// Check that given value is a number or string /// Check that given value is a number or string
/// ///
/// Error messages are compatible with get_tv_number() previously used for the /// Error messages are compatible with tv_get_number() previously used for the
/// same purpose in buf*() functions. Special values are not accepted (previous /// same purpose in buf*() functions. Special values are not accepted (previous
/// behaviour: silently fail to find buffer). /// behaviour: silently fail to find buffer).
/// ///
@@ -2016,8 +2017,127 @@ bool tv_check_str_or_nr(const typval_T *const tv)
return false; return false;
} }
#define FUNC_ERROR "E703: Using a Funcref as a Number"
static const char *const num_errors[] = {
[VAR_PARTIAL]=N_(FUNC_ERROR),
[VAR_FUNC]=N_(FUNC_ERROR),
[VAR_LIST]=N_("E745: Using a List as a Number"),
[VAR_DICT]=N_("E728: Using a Dictionary as a Number"),
[VAR_FLOAT]=N_("E805: Using a Float as a Number"),
[VAR_UNKNOWN]=N_("E685: using an invalid value as a Number"),
};
#undef FUNC_ERROR
/// Check that given value is a number or can be converted to it
///
/// Error messages are compatible with tv_get_number() previously used for
/// the same purpose.
///
/// @param[in] tv Value to check.
///
/// @return true if everything is OK, false otherwise.
bool tv_check_num(const typval_T *const tv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
switch (tv->v_type) {
case VAR_NUMBER:
case VAR_SPECIAL:
case VAR_STRING: {
return true;
}
case VAR_FUNC:
case VAR_PARTIAL:
case VAR_LIST:
case VAR_DICT:
case VAR_FLOAT:
case VAR_UNKNOWN: {
EMSG(_(num_errors[tv->v_type]));
return false;
}
}
assert(false);
return false;
}
//{{{2 Get //{{{2 Get
/// Get the number value of a VimL object
///
/// @note Use tv_get_number_chk() if you need to determine whether there was an
/// error.
///
/// @param[in] tv Object to get value from.
///
/// @return Number value: vim_str2nr() output for VAR_STRING objects, value
/// for VAR_NUMBER objects, -1 for other types.
varnumber_T tv_get_number(const typval_T *const tv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
bool error = false;
return tv_get_number_chk(tv, &error);
}
/// Get the number value of a VimL object
///
/// @param[in] tv Object to get value from.
/// @param[out] ret_error If type error occurred then `true` will be written
/// to this location. Otherwise it is not touched.
///
/// @note Needs to be initialized to `false` to be
/// useful.
///
/// @return Number value: vim_str2nr() output for VAR_STRING objects, value
/// for VAR_NUMBER objects, -1 (ret_error == NULL) or 0 (otherwise) for
/// other types.
varnumber_T tv_get_number_chk(const typval_T *const tv, bool *const ret_error)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
{
switch (tv->v_type) {
case VAR_FUNC:
case VAR_PARTIAL:
case VAR_LIST:
case VAR_DICT:
case VAR_FLOAT: {
EMSG(_(num_errors[tv->v_type]));
break;
}
case VAR_NUMBER: {
return tv->vval.v_number;
}
case VAR_STRING: {
varnumber_T n = 0;
if (tv->vval.v_string != NULL) {
long nr;
vim_str2nr(tv->vval.v_string, NULL, NULL, STR2NR_ALL, &nr, NULL, 0);
n = (varnumber_T)nr;
}
return n;
}
case VAR_SPECIAL: {
switch (tv->vval.v_special) {
case kSpecialVarTrue: {
return 1;
}
case kSpecialVarFalse:
case kSpecialVarNull: {
return 0;
}
}
break;
}
case VAR_UNKNOWN: {
EMSG2(_(e_intern2), "tv_get_number(UNKNOWN)");
break;
}
}
if (ret_error != NULL) {
*ret_error = true;
}
return (ret_error == NULL ? -1 : 0);
}
/// Get the line number from VimL object /// Get the line number from VimL object
/// ///
/// @param[in] tv Object to get value from. Is expected to be a number or /// @param[in] tv Object to get value from. Is expected to be a number or
@@ -2028,7 +2148,7 @@ bool tv_check_str_or_nr(const typval_T *const tv)
linenr_T tv_get_lnum(const typval_T *const tv) linenr_T tv_get_lnum(const typval_T *const tv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
linenr_T lnum = get_tv_number_chk(tv, NULL); linenr_T lnum = tv_get_number_chk(tv, NULL);
if (lnum == 0) { // No valid number, try using same function as line() does. if (lnum == 0) { // No valid number, try using same function as line() does.
int fnum; int fnum;
pos_T *const fp = var2fpos(tv, true, &fnum); pos_T *const fp = var2fpos(tv, true, &fnum);
@@ -2078,7 +2198,7 @@ float_T tv_get_float(const typval_T *const tv)
break; break;
} }
case VAR_UNKNOWN: { case VAR_UNKNOWN: {
EMSG2(_(e_intern2), "get_tv_float()"); EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
break; break;
} }
} }

View File

@@ -576,7 +576,7 @@ static varnumber_T tv_nr(typval_T *tvs, int *idxp)
} else { } else {
(*idxp)++; (*idxp)++;
bool err = false; bool err = false;
n = (varnumber_T)get_tv_number_chk(&tvs[idx], &err); n = tv_get_number_chk(&tvs[idx], &err);
if (err) { if (err) {
n = 0; n = 0;
} }

View File

@@ -5593,7 +5593,7 @@ int match_add(win_T *wp, const char *const grp, const char *const pat,
if (subli == NULL) { if (subli == NULL) {
goto fail; goto fail;
} }
lnum = get_tv_number_chk(&subli->li_tv, &error); lnum = tv_get_number_chk(&subli->li_tv, &error);
if (error) { if (error) {
goto fail; goto fail;
} }
@@ -5604,13 +5604,13 @@ int match_add(win_T *wp, const char *const grp, const char *const pat,
m->pos.pos[i].lnum = lnum; m->pos.pos[i].lnum = lnum;
subli = subli->li_next; subli = subli->li_next;
if (subli != NULL) { if (subli != NULL) {
col = get_tv_number_chk(&subli->li_tv, &error); col = tv_get_number_chk(&subli->li_tv, &error);
if (error) { if (error) {
goto fail; goto fail;
} }
subli = subli->li_next; subli = subli->li_next;
if (subli != NULL) { if (subli != NULL) {
len = get_tv_number_chk(&subli->li_tv, &error); len = tv_get_number_chk(&subli->li_tv, &error);
if (error) { if (error) {
goto fail; goto fail;
} }
@@ -5810,14 +5810,14 @@ int win_getid(typval_T *argvars)
if (argvars[0].v_type == VAR_UNKNOWN) { if (argvars[0].v_type == VAR_UNKNOWN) {
return curwin->handle; return curwin->handle;
} }
int winnr = get_tv_number(&argvars[0]); int winnr = tv_get_number(&argvars[0]);
win_T *wp; win_T *wp;
if (winnr > 0) { if (winnr > 0) {
if (argvars[1].v_type == VAR_UNKNOWN) { if (argvars[1].v_type == VAR_UNKNOWN) {
wp = firstwin; wp = firstwin;
} else { } else {
tabpage_T *tp = NULL; tabpage_T *tp = NULL;
int tabnr = get_tv_number(&argvars[1]); int tabnr = tv_get_number(&argvars[1]);
FOR_ALL_TABS(tp2) { FOR_ALL_TABS(tp2) {
if (--tabnr == 0) { if (--tabnr == 0) {
tp = tp2; tp = tp2;
@@ -5844,7 +5844,7 @@ int win_getid(typval_T *argvars)
int win_gotoid(typval_T *argvars) int win_gotoid(typval_T *argvars)
{ {
int id = get_tv_number(&argvars[0]); int id = tv_get_number(&argvars[0]);
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
if (wp->handle == id) { if (wp->handle == id) {
@@ -5879,7 +5879,7 @@ void win_id2tabwin(typval_T *argvars, list_T *list)
{ {
int winnr = 1; int winnr = 1;
int tabnr = 1; int tabnr = 1;
int id = get_tv_number(&argvars[0]); handle_T id = (handle_T)tv_get_number(&argvars[0]);
win_get_tabwin(id, &tabnr, &winnr); win_get_tabwin(id, &tabnr, &winnr);
tv_list_append_number(list, tabnr); tv_list_append_number(list, tabnr);
@@ -5888,7 +5888,7 @@ void win_id2tabwin(typval_T *argvars, list_T *list)
win_T * win_id2wp(typval_T *argvars) win_T * win_id2wp(typval_T *argvars)
{ {
int id = get_tv_number(&argvars[0]); int id = tv_get_number(&argvars[0]);
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
if (wp->handle == id) { if (wp->handle == id) {
@@ -5902,7 +5902,7 @@ win_T * win_id2wp(typval_T *argvars)
int win_id2win(typval_T *argvars) int win_id2win(typval_T *argvars)
{ {
int nr = 1; int nr = 1;
int id = get_tv_number(&argvars[0]); int id = tv_get_number(&argvars[0]);
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->handle == id) { if (wp->handle == id) {
@@ -5915,7 +5915,7 @@ int win_id2win(typval_T *argvars)
void win_findbuf(typval_T *argvars, list_T *list) void win_findbuf(typval_T *argvars, list_T *list)
{ {
int bufnr = get_tv_number(&argvars[0]); int bufnr = tv_get_number(&argvars[0]);
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
if (wp->w_buffer->b_fnum == bufnr) { if (wp->w_buffer->b_fnum == bufnr) {