comparison src/eval.c @ 27682:f60d0d823897 v8.2.4367

patch 8.2.4367: calling in_vim9script() multiple times Commit: https://github.com/vim/vim/commit/4525a57afbea3e0885642bbebbc24d5a25c97da9 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 13 11:57:33 2022 +0000 patch 8.2.4367: calling in_vim9script() multiple times Problem: Calling in_vim9script() multiple times. Solution: Call it once and keep the result.
author Bram Moolenaar <Bram@vim.org>
date Sun, 13 Feb 2022 13:00:06 +0100
parents 5c4ab8d4472c
children 3813036f19cb
comparison
equal deleted inserted replaced
27681:fb41bd2ecd26 27682:f60d0d823897
863 char_u *key = NULL; 863 char_u *key = NULL;
864 int len; 864 int len;
865 hashtab_T *ht = NULL; 865 hashtab_T *ht = NULL;
866 int quiet = flags & GLV_QUIET; 866 int quiet = flags & GLV_QUIET;
867 int writing; 867 int writing;
868 int vim9script = in_vim9script();
868 869
869 // Clear everything in "lp". 870 // Clear everything in "lp".
870 CLEAR_POINTER(lp); 871 CLEAR_POINTER(lp);
871 872
872 if (skip || (flags & GLV_COMPILING)) 873 if (skip || (flags & GLV_COMPILING))
877 FNE_INCL_BR | fne_flags); 878 FNE_INCL_BR | fne_flags);
878 return lp->ll_name_end; 879 return lp->ll_name_end;
879 } 880 }
880 881
881 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK. 882 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
882 if (in_vim9script() && at_script_level() 883 if (vim9script && at_script_level()
883 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2])) 884 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
884 { 885 {
885 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name); 886 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
886 return NULL; 887 return NULL;
887 } 888 }
916 } 917 }
917 else 918 else
918 { 919 {
919 lp->ll_name = name; 920 lp->ll_name = name;
920 921
921 if (in_vim9script()) 922 if (vim9script)
922 { 923 {
923 // "a: type" is declaring variable "a" with a type, not "a:". 924 // "a: type" is declaring variable "a" with a type, not "a:".
924 if (p == name + 2 && p[-1] == ':') 925 if (p == name + 2 && p[-1] == ':')
925 { 926 {
926 --p; 927 --p;
994 995
995 // Without [idx] or .key we are done. 996 // Without [idx] or .key we are done.
996 if ((*p != '[' && *p != '.')) 997 if ((*p != '[' && *p != '.'))
997 return p; 998 return p;
998 999
999 if (in_vim9script() && lval_root != NULL) 1000 if (vim9script && lval_root != NULL)
1000 { 1001 {
1001 // using local variable 1002 // using local variable
1002 lp->ll_tv = lval_root; 1003 lp->ll_tv = lval_root;
1003 v = NULL; 1004 v = NULL;
1004 } 1005 }
1016 if (v == NULL) 1017 if (v == NULL)
1017 return NULL; 1018 return NULL;
1018 lp->ll_tv = &v->di_tv; 1019 lp->ll_tv = &v->di_tv;
1019 } 1020 }
1020 1021
1021 if (in_vim9script() && (flags & GLV_NO_DECL) == 0) 1022 if (vim9script && (flags & GLV_NO_DECL) == 0)
1022 { 1023 {
1023 if (!quiet) 1024 if (!quiet)
1024 semsg(_(e_variable_already_declared), lp->ll_name); 1025 semsg(_(e_variable_already_declared), lp->ll_name);
1025 return NULL; 1026 return NULL;
1026 } 1027 }
1059 if (!quiet) 1060 if (!quiet)
1060 emsg(_(e_slice_must_come_last)); 1061 emsg(_(e_slice_must_come_last));
1061 return NULL; 1062 return NULL;
1062 } 1063 }
1063 1064
1064 if (in_vim9script() && lp->ll_valtype == NULL 1065 if (vim9script && lp->ll_valtype == NULL
1065 && v != NULL 1066 && v != NULL
1066 && lp->ll_tv == &v->di_tv 1067 && lp->ll_tv == &v->di_tv
1067 && ht != NULL && ht == get_script_local_ht()) 1068 && ht != NULL && ht == get_script_local_ht())
1068 { 1069 {
1069 svar_T *sv = find_typval_in_script(lp->ll_tv, 0); 1070 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
2610 { 2611 {
2611 if (getnext) 2612 if (getnext)
2612 *arg = eval_next_line(evalarg_used); 2613 *arg = eval_next_line(evalarg_used);
2613 else 2614 else
2614 { 2615 {
2615 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1])) 2616 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2616 { 2617 {
2617 error_white_both(p, 2); 2618 error_white_both(p, 2);
2618 clear_tv(rettv); 2619 clear_tv(rettv);
2619 return FAIL; 2620 return FAIL;
2620 } 2621 }
2622 } 2623 }
2623 2624
2624 /* 2625 /*
2625 * Get the second variable. 2626 * Get the second variable.
2626 */ 2627 */
2627 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2])) 2628 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
2628 { 2629 {
2629 error_white_both(*arg, 2); 2630 error_white_both(*arg, 2);
2630 clear_tv(rettv); 2631 clear_tv(rettv);
2631 return FAIL; 2632 return FAIL;
2632 } 2633 }
2748 } 2749 }
2749 2750
2750 /* 2751 /*
2751 * Get the second variable. 2752 * Get the second variable.
2752 */ 2753 */
2753 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2])) 2754 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
2754 { 2755 {
2755 error_white_both(*arg, 2); 2756 error_white_both(*arg, 2);
2756 clear_tv(rettv); 2757 clear_tv(rettv);
2757 return FAIL; 2758 return FAIL;
2758 } 2759 }
3525 char_u *s; 3526 char_u *s;
3526 char_u *name_start = NULL; 3527 char_u *name_start = NULL;
3527 char_u *start_leader, *end_leader; 3528 char_u *start_leader, *end_leader;
3528 int ret = OK; 3529 int ret = OK;
3529 char_u *alias; 3530 char_u *alias;
3530 static int recurse = 0; 3531 static int recurse = 0;
3532 int vim9script = in_vim9script();
3531 3533
3532 /* 3534 /*
3533 * Initialise variable so that clear_tv() can't mistake this for a 3535 * Initialise variable so that clear_tv() can't mistake this for a
3534 * string and free a string that isn't there. 3536 * string and free a string that isn't there.
3535 */ 3537 */
3537 3539
3538 /* 3540 /*
3539 * Skip '!', '-' and '+' characters. They are handled later. 3541 * Skip '!', '-' and '+' characters. They are handled later.
3540 */ 3542 */
3541 start_leader = *arg; 3543 start_leader = *arg;
3542 if (eval_leader(arg, in_vim9script()) == FAIL) 3544 if (eval_leader(arg, vim9script) == FAIL)
3543 return FAIL; 3545 return FAIL;
3544 end_leader = *arg; 3546 end_leader = *arg;
3545 3547
3546 if (**arg == '.' && (!isdigit(*(*arg + 1)) 3548 if (**arg == '.' && (!isdigit(*(*arg + 1))
3547 #ifdef FEAT_FLOAT 3549 #ifdef FEAT_FLOAT
3612 break; 3614 break;
3613 3615
3614 /* 3616 /*
3615 * Dictionary: #{key: val, key: val} 3617 * Dictionary: #{key: val, key: val}
3616 */ 3618 */
3617 case '#': if (in_vim9script()) 3619 case '#': if (vim9script)
3618 { 3620 {
3619 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE; 3621 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3620 } 3622 }
3621 else if ((*arg)[1] == '{') 3623 else if ((*arg)[1] == '{')
3622 { 3624 {
3629 3631
3630 /* 3632 /*
3631 * Lambda: {arg, arg -> expr} 3633 * Lambda: {arg, arg -> expr}
3632 * Dictionary: {'key': val, 'key': val} 3634 * Dictionary: {'key': val, 'key': val}
3633 */ 3635 */
3634 case '{': if (in_vim9script()) 3636 case '{': if (vim9script)
3635 ret = NOTDONE; 3637 ret = NOTDONE;
3636 else 3638 else
3637 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg); 3639 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
3638 if (ret == NOTDONE) 3640 if (ret == NOTDONE)
3639 ret = eval_dict(arg, rettv, evalarg, FALSE); 3641 ret = eval_dict(arg, rettv, evalarg, FALSE);
3640 break; 3642 break;
3641 3643
3642 /* 3644 /*
3655 * Register contents: @r. 3657 * Register contents: @r.
3656 */ 3658 */
3657 case '@': ++*arg; 3659 case '@': ++*arg;
3658 if (evaluate) 3660 if (evaluate)
3659 { 3661 {
3660 if (in_vim9script() && IS_WHITE_OR_NUL(**arg)) 3662 if (vim9script && IS_WHITE_OR_NUL(**arg))
3661 semsg(_(e_syntax_error_at_str), *arg); 3663 semsg(_(e_syntax_error_at_str), *arg);
3662 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE)) 3664 else if (vim9script && !valid_yank_reg(**arg, FALSE))
3663 emsg_invreg(**arg); 3665 emsg_invreg(**arg);
3664 else 3666 else
3665 { 3667 {
3666 rettv->v_type = VAR_STRING; 3668 rettv->v_type = VAR_STRING;
3667 rettv->vval.v_string = get_reg_contents(**arg, 3669 rettv->vval.v_string = get_reg_contents(**arg,
3675 /* 3677 /*
3676 * nested expression: (expression). 3678 * nested expression: (expression).
3677 * or lambda: (arg) => expr 3679 * or lambda: (arg) => expr
3678 */ 3680 */
3679 case '(': ret = NOTDONE; 3681 case '(': ret = NOTDONE;
3680 if (in_vim9script()) 3682 if (vim9script)
3681 { 3683 {
3682 ret = get_lambda_tv(arg, rettv, TRUE, evalarg); 3684 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3683 if (ret == OK && evaluate) 3685 if (ret == OK && evaluate)
3684 { 3686 {
3685 ufunc_T *ufunc = rettv->vval.v_partial->pt_func; 3687 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3733 ret = FAIL; 3735 ret = FAIL;
3734 else 3736 else
3735 { 3737 {
3736 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 3738 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3737 3739
3738 if (evaluate && in_vim9script() && len == 1 && *s == '_') 3740 if (evaluate && vim9script && len == 1 && *s == '_')
3739 { 3741 {
3740 emsg(_(e_cannot_use_underscore_here)); 3742 emsg(_(e_cannot_use_underscore_here));
3741 ret = FAIL; 3743 ret = FAIL;
3742 } 3744 }
3743 else if (evaluate && in_vim9script() && len > 2 3745 else if (evaluate && vim9script && len > 2
3744 && s[0] == 's' && s[1] == ':') 3746 && s[0] == 's' && s[1] == ':')
3745 { 3747 {
3746 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s); 3748 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3747 ret = FAIL; 3749 ret = FAIL;
3748 } 3750 }
3749 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(') 3751 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
3750 { 3752 {
3751 // "name(..." recursive! 3753 // "name(..." recursive!
3752 *arg = skipwhite(*arg); 3754 *arg = skipwhite(*arg);
3753 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL); 3755 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
3754 } 3756 }
3755 else if (flags & EVAL_CONSTANT) 3757 else if (flags & EVAL_CONSTANT)
3756 ret = FAIL; 3758 ret = FAIL;
3757 else if (evaluate) 3759 else if (evaluate)
3758 { 3760 {
3759 // get the value of "true", "false" or a variable 3761 // get the value of "true", "false" or a variable
3760 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0) 3762 if (len == 4 && vim9script && STRNCMP(s, "true", 4) == 0)
3761 { 3763 {
3762 rettv->v_type = VAR_BOOL; 3764 rettv->v_type = VAR_BOOL;
3763 rettv->vval.v_number = VVAL_TRUE; 3765 rettv->vval.v_number = VVAL_TRUE;
3764 ret = OK; 3766 ret = OK;
3765 } 3767 }
3766 else if (len == 5 && in_vim9script() 3768 else if (len == 5 && vim9script && STRNCMP(s, "false", 5) == 0)
3767 && STRNCMP(s, "false", 5) == 0)
3768 { 3769 {
3769 rettv->v_type = VAR_BOOL; 3770 rettv->v_type = VAR_BOOL;
3770 rettv->vval.v_number = VVAL_FALSE; 3771 rettv->vval.v_number = VVAL_FALSE;
3771 ret = OK; 3772 ret = OK;
3772 } 3773 }
3773 else if (len == 4 && in_vim9script() 3774 else if (len == 4 && vim9script && STRNCMP(s, "null", 4) == 0)
3774 && STRNCMP(s, "null", 4) == 0)
3775 { 3775 {
3776 rettv->v_type = VAR_SPECIAL; 3776 rettv->v_type = VAR_SPECIAL;
3777 rettv->vval.v_number = VVAL_NULL; 3777 rettv->vval.v_number = VVAL_NULL;
3778 ret = OK; 3778 ret = OK;
3779 } 3779 }
3824 char_u *end_leader = *end_leaderp; 3824 char_u *end_leader = *end_leaderp;
3825 int ret = OK; 3825 int ret = OK;
3826 int error = FALSE; 3826 int error = FALSE;
3827 varnumber_T val = 0; 3827 varnumber_T val = 0;
3828 vartype_T type = rettv->v_type; 3828 vartype_T type = rettv->v_type;
3829 int vim9script = in_vim9script();
3829 #ifdef FEAT_FLOAT 3830 #ifdef FEAT_FLOAT
3830 float_T f = 0.0; 3831 float_T f = 0.0;
3831 3832
3832 if (rettv->v_type == VAR_FLOAT) 3833 if (rettv->v_type == VAR_FLOAT)
3833 f = rettv->vval.v_float; 3834 f = rettv->vval.v_float;
3834 else 3835 else
3835 #endif 3836 #endif
3836 { 3837 {
3837 while (VIM_ISWHITE(end_leader[-1])) 3838 while (VIM_ISWHITE(end_leader[-1]))
3838 --end_leader; 3839 --end_leader;
3839 if (in_vim9script() && end_leader[-1] == '!') 3840 if (vim9script && end_leader[-1] == '!')
3840 val = tv2bool(rettv); 3841 val = tv2bool(rettv);
3841 else 3842 else
3842 val = tv_get_number_chk(rettv, &error); 3843 val = tv_get_number_chk(rettv, &error);
3843 } 3844 }
3844 if (error) 3845 if (error)
3859 break; 3860 break;
3860 } 3861 }
3861 #ifdef FEAT_FLOAT 3862 #ifdef FEAT_FLOAT
3862 if (rettv->v_type == VAR_FLOAT) 3863 if (rettv->v_type == VAR_FLOAT)
3863 { 3864 {
3864 if (in_vim9script()) 3865 if (vim9script)
3865 { 3866 {
3866 rettv->v_type = VAR_BOOL; 3867 rettv->v_type = VAR_BOOL;
3867 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE; 3868 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3868 } 3869 }
3869 else 3870 else
3897 } 3898 }
3898 else 3899 else
3899 #endif 3900 #endif
3900 { 3901 {
3901 clear_tv(rettv); 3902 clear_tv(rettv);
3902 if (in_vim9script()) 3903 if (vim9script)
3903 rettv->v_type = type; 3904 rettv->v_type = type;
3904 else 3905 else
3905 rettv->v_type = VAR_NUMBER; 3906 rettv->v_type = VAR_NUMBER;
3906 rettv->vval.v_number = val; 3907 rettv->vval.v_number = val;
3907 } 3908 }
4149 int empty1 = FALSE, empty2 = FALSE; 4150 int empty1 = FALSE, empty2 = FALSE;
4150 typval_T var1, var2; 4151 typval_T var1, var2;
4151 int range = FALSE; 4152 int range = FALSE;
4152 char_u *key = NULL; 4153 char_u *key = NULL;
4153 int keylen = -1; 4154 int keylen = -1;
4154 int vim9 = in_vim9script(); 4155 int vim9script = in_vim9script();
4155 4156
4156 if (check_can_index(rettv, evaluate, verbose) == FAIL) 4157 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4157 return FAIL; 4158 return FAIL;
4158 4159
4159 init_tv(&var1); 4160 init_tv(&var1);
4180 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 4181 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
4181 if (**arg == ':') 4182 if (**arg == ':')
4182 empty1 = TRUE; 4183 empty1 = TRUE;
4183 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive! 4184 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
4184 return FAIL; 4185 return FAIL;
4185 else if (vim9 && **arg == ':') 4186 else if (vim9script && **arg == ':')
4186 { 4187 {
4187 semsg(_(e_white_space_required_before_and_after_str_at_str), 4188 semsg(_(e_white_space_required_before_and_after_str_at_str),
4188 ":", *arg); 4189 ":", *arg);
4189 clear_tv(&var1); 4190 clear_tv(&var1);
4190 return FAIL; 4191 return FAIL;
4193 { 4194 {
4194 int error = FALSE; 4195 int error = FALSE;
4195 4196
4196 #ifdef FEAT_FLOAT 4197 #ifdef FEAT_FLOAT
4197 // allow for indexing with float 4198 // allow for indexing with float
4198 if (vim9 && rettv->v_type == VAR_DICT 4199 if (vim9script && rettv->v_type == VAR_DICT
4199 && var1.v_type == VAR_FLOAT) 4200 && var1.v_type == VAR_FLOAT)
4200 { 4201 {
4201 var1.vval.v_string = typval_tostring(&var1, TRUE); 4202 var1.vval.v_string = typval_tostring(&var1, TRUE);
4202 var1.v_type = VAR_STRING; 4203 var1.v_type = VAR_STRING;
4203 } 4204 }
4204 #endif 4205 #endif
4205 if (vim9 && rettv->v_type == VAR_LIST) 4206 if (vim9script && rettv->v_type == VAR_LIST)
4206 tv_get_number_chk(&var1, &error); 4207 tv_get_number_chk(&var1, &error);
4207 else 4208 else
4208 error = tv_get_string_chk(&var1) == NULL; 4209 error = tv_get_string_chk(&var1) == NULL;
4209 if (error) 4210 if (error)
4210 { 4211 {
4220 *arg = skipwhite_and_linebreak(*arg, evalarg); 4221 *arg = skipwhite_and_linebreak(*arg, evalarg);
4221 if (**arg == ':') 4222 if (**arg == ':')
4222 { 4223 {
4223 range = TRUE; 4224 range = TRUE;
4224 ++*arg; 4225 ++*arg;
4225 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']') 4226 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
4226 { 4227 {
4227 semsg(_(e_white_space_required_before_and_after_str_at_str), 4228 semsg(_(e_white_space_required_before_and_after_str_at_str),
4228 ":", *arg - 1); 4229 ":", *arg - 1);
4229 if (!empty1) 4230 if (!empty1)
4230 clear_tv(&var1); 4231 clear_tv(&var1);