diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-04-14 18:24:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-14 18:24:44 +0800 |
commit | c15939c1f7479be6c1e0a73126b4d62aece28f74 (patch) | |
tree | dd54e88754d71073fb58224fc0ba2891ef0ca7ed /src | |
parent | 7caf0eafd83b5a92f2ff219b3a64ffae4174b9af (diff) | |
parent | 3be966f725bfefd7215acd0aad155c94b813d53f (diff) | |
download | rneovim-c15939c1f7479be6c1e0a73126b4d62aece28f74.tar.gz rneovim-c15939c1f7479be6c1e0a73126b4d62aece28f74.tar.bz2 rneovim-c15939c1f7479be6c1e0a73126b4d62aece28f74.zip |
Merge pull request #23084 from zeertzjq/vim-8.2.1794
vim-patch:8.2.{1794,1798},9.0.1452
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 69 | ||||
-rw-r--r-- | src/nvim/eval/typval.c | 31 |
2 files changed, 73 insertions, 27 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index dab3afb212..d5a794da01 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -2336,6 +2336,7 @@ int eval0(char *arg, typval_T *rettv, exarg_T *eap, evalarg_T *const evalarg) /// Handle top level expression: /// expr2 ? expr1 : expr1 +/// expr2 ?? expr1 /// /// "arg" must point to the first non-white of the expression. /// "arg" is advanced to the next non-white after the recognized expression. @@ -2352,6 +2353,7 @@ int eval1(char **arg, typval_T *rettv, evalarg_T *const evalarg) char *p = *arg; if (*p == '?') { + const bool op_falsy = p[1] == '?'; evalarg_T *evalarg_used = evalarg; evalarg_T local_evalarg; if (evalarg == NULL) { @@ -2365,49 +2367,62 @@ int eval1(char **arg, typval_T *rettv, evalarg_T *const evalarg) if (evaluate) { bool error = false; - if (tv_get_number_chk(rettv, &error) != 0) { + if (op_falsy) { + result = tv2bool(rettv); + } else if (tv_get_number_chk(rettv, &error) != 0) { result = true; } - tv_clear(rettv); + if (error || !op_falsy || !result) { + tv_clear(rettv); + } if (error) { return FAIL; } } // Get the second variable. Recursive! - *arg = skipwhite(*arg + 1); - evalarg_used->eval_flags = result ? orig_flags : orig_flags & ~EVAL_EVALUATE; - if (eval1(arg, rettv, evalarg_used) == FAIL) { - evalarg_used->eval_flags = orig_flags; - return FAIL; - } - - // Check for the ":". - p = *arg; - if (*p != ':') { - emsg(_("E109: Missing ':' after '?'")); - if (evaluate && result) { - tv_clear(rettv); - } - evalarg_used->eval_flags = orig_flags; - return FAIL; + if (op_falsy) { + (*arg)++; } - - // Get the third variable. Recursive! *arg = skipwhite(*arg + 1); - evalarg_used->eval_flags = !result ? orig_flags : orig_flags & ~EVAL_EVALUATE; + evalarg_used->eval_flags = (op_falsy ? !result : result) + ? orig_flags : (orig_flags & ~EVAL_EVALUATE); typval_T var2; if (eval1(arg, &var2, evalarg_used) == FAIL) { - if (evaluate && result) { - tv_clear(rettv); - } evalarg_used->eval_flags = orig_flags; return FAIL; } - if (evaluate && !result) { + if (!op_falsy || !result) { *rettv = var2; } + if (!op_falsy) { + // Check for the ":". + p = *arg; + if (*p != ':') { + emsg(_("E109: Missing ':' after '?'")); + if (evaluate && result) { + tv_clear(rettv); + } + evalarg_used->eval_flags = orig_flags; + return FAIL; + } + + // Get the third variable. Recursive! + *arg = skipwhite(*arg + 1); + evalarg_used->eval_flags = !result ? orig_flags : (orig_flags & ~EVAL_EVALUATE); + if (eval1(arg, &var2, evalarg_used) == FAIL) { + if (evaluate && result) { + tv_clear(rettv); + } + evalarg_used->eval_flags = orig_flags; + return FAIL; + } + if (evaluate && !result) { + *rettv = var2; + } + } + if (evalarg == NULL) { clear_evalarg(&local_evalarg, NULL); } else { @@ -2461,7 +2476,7 @@ static int eval2(char **arg, typval_T *rettv, evalarg_T *const evalarg) while (p[0] == '|' && p[1] == '|') { // Get the second variable. *arg = skipwhite(*arg + 2); - evalarg_used->eval_flags = !result ? orig_flags : orig_flags & ~EVAL_EVALUATE; + evalarg_used->eval_flags = !result ? orig_flags : (orig_flags & ~EVAL_EVALUATE); typval_T var2; if (eval3(arg, &var2, evalarg_used) == FAIL) { return FAIL; @@ -2539,7 +2554,7 @@ static int eval3(char **arg, typval_T *rettv, evalarg_T *const evalarg) while (p[0] == '&' && p[1] == '&') { // Get the second variable. *arg = skipwhite(*arg + 2); - evalarg_used->eval_flags = result ? orig_flags : orig_flags & ~EVAL_EVALUATE; + evalarg_used->eval_flags = result ? orig_flags : (orig_flags & ~EVAL_EVALUATE); typval_T var2; if (eval4(arg, &var2, evalarg_used) == FAIL) { return FAIL; diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 3e67571053..91be41751e 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -4201,3 +4201,34 @@ const char *tv_get_string_buf(const typval_T *const tv, char *const buf) return res != NULL ? res : ""; } + +/// Return true when "tv" is not falsy: non-zero, non-empty string, non-empty +/// list, etc. Mostly like what JavaScript does, except that empty list and +/// empty dictionary are false. +bool tv2bool(const typval_T *const tv) +{ + switch (tv->v_type) { + case VAR_NUMBER: + return tv->vval.v_number != 0; + case VAR_FLOAT: + return tv->vval.v_float != 0.0; + case VAR_PARTIAL: + return tv->vval.v_partial != NULL; + case VAR_FUNC: + case VAR_STRING: + return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; + case VAR_LIST: + return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; + case VAR_DICT: + return tv->vval.v_dict != NULL && tv->vval.v_dict->dv_hashtab.ht_used > 0; + case VAR_BOOL: + return tv->vval.v_bool == kBoolVarTrue; + case VAR_SPECIAL: + return tv->vval.v_special == kSpecialVarNull; + case VAR_BLOB: + return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; + case VAR_UNKNOWN: + break; + } + return false; +} |