blob: a9f7112f2deaa7375287b24f29638e898690e830 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200971 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Ernie Rael64885642023-10-04 20:16:22 +0200988#ifdef LOG_LOCKVAR
989typedef struct flag_string_S
990{
991 int flag;
992 char *str;
993} flag_string_T;
994
995 static char *
996flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
997{
998 char *p = buf;
999 *p = NUL;
1000 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1001 {
1002 if ((fstring->flag & flags) != 0)
1003 {
1004 size_t len = STRLEN(fstring->str);
1005 if (n > p - buf + len + 7)
1006 {
1007 STRCAT(p, fstring->str);
1008 p += len;
1009 STRCAT(p, " ");
1010 ++p;
1011 }
1012 else
1013 {
1014 STRCAT(buf, "...");
1015 break;
1016 }
1017 }
1018 }
1019 return buf;
1020}
1021
1022flag_string_T glv_flag_strings[] = {
1023 { GLV_QUIET, "QUIET" },
1024 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1025 { GLV_READ_ONLY, "READ_ONLY" },
1026 { GLV_NO_DECL, "NO_DECL" },
1027 { GLV_COMPILING, "COMPILING" },
1028 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1029 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1030 { 0, NULL }
1031};
1032#endif
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
Ernie Raelee865f32023-09-29 19:53:55 +02001035 * Fill in "lp" using "root". This is used in a special case when
1036 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1037 *
1038 * This is typically called with "lval_root" as "root". For a class, find
1039 * the name from lp in the class from root, fill in lval_T if found. For a
1040 * complex type, list/dict use it as the result; just put the root into
1041 * ll_tv.
1042 *
1043 * "lval_root" is a hack used during run-time/instr-execution to provide the
1044 * starting point for "get_lval()" to traverse a chain of indexes. In some
1045 * cases get_lval sees a bare name and uses this function to populate the
1046 * lval_T.
1047 *
1048 * For setting up "lval_root" (currently only used with lockvar)
1049 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1050 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1051 */
1052 static void
Ernie Rael64885642023-10-04 20:16:22 +02001053get_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001056 ch_log(NULL, "LKVAR: get_lval_root(): name %s", lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001057#endif
Ernie Rael64885642023-10-04 20:16:22 +02001058 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001059 {
Ernie Rael64885642023-10-04 20:16:22 +02001060 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001061 {
1062 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001063 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001064 int m_idx;
1065 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1066 lp->ll_name_end - lp->ll_name, &m_idx);
1067 if (m != NULL)
1068 {
1069 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001070 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001071 lp->ll_oi = m_idx;
1072 lp->ll_valtype = m->ocm_type;
1073 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1074#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001075 ch_log(NULL, "LKVAR: ... class member %s.%s",
1076 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001077#endif
1078 return;
1079 }
1080 }
1081 }
1082
1083#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001084 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001085#endif
Ernie Rael64885642023-10-04 20:16:22 +02001086 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001087 lp->ll_is_root = TRUE;
1088}
1089
1090/*
Ernie Rael64885642023-10-04 20:16:22 +02001091 * Check if the class has permission to access the member.
1092 * Returns OK or FAIL.
1093 */
1094 static int
1095get_lval_check_access(
1096 class_T *cl_exec, // executing class, NULL if :def or script level
1097 class_T *cl, // class which contains the member
1098 ocmember_T *om, // member being accessed
1099 char_u *p, // char after member name
1100 int flags) // GLV flags to check if writing to lval
1101{
1102#ifdef LOG_LOCKVAR
1103 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1104 (void*)cl_exec, (void*)cl, *p);
1105#endif
1106 if (cl_exec == NULL || cl_exec != cl)
1107 {
1108 switch (om->ocm_access)
1109 {
1110 case VIM_ACCESS_PRIVATE:
1111 semsg(_(e_cannot_access_private_variable_str),
1112 om->ocm_name, cl->class_name);
1113 return FAIL;
1114 case VIM_ACCESS_READ:
1115 // If [idx] or .key following, read only OK.
1116 if (*p == '[' || *p == '.')
1117 break;
1118 if ((flags & GLV_READ_ONLY) == 0)
1119 {
1120 semsg(_(e_variable_is_not_writable_str),
1121 om->ocm_name, cl->class_name);
1122 return FAIL;
1123 }
1124 break;
1125 case VIM_ACCESS_ALL:
1126 break;
1127 }
1128 }
1129 return OK;
1130}
1131
1132/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 * Get an lval: variable, Dict item or List item that can be assigned a value
1134 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1135 * "name.key", "name.key[expr]" etc.
1136 * Indexing only works if "name" is an existing List or Dictionary.
1137 * "name" points to the start of the name.
1138 * If "rettv" is not NULL it points to the value to be assigned.
1139 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1140 * wrong; must end in space or cmd separator.
1141 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001142 * flags:
1143 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001144 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001145 * GLV_NO_AUTOLOAD: do not use script autoloading
1146 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001148 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001151 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001152get_lval(
1153 char_u *name,
1154 typval_T *rettv,
1155 lval_T *lp,
1156 int unlet,
1157 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 int flags, // GLV_ values
1159 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001161 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 char_u *expr_start, *expr_end;
1163 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001164 dictitem_T *v;
1165 typval_T var1;
1166 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001170 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001171 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001172 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001173 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001174 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175
Ernie Raelee865f32023-09-29 19:53:55 +02001176#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001177 if (lval_root == NULL)
1178 ch_log(NULL,
1179 "LKVAR: get_lval(): name %s, lval_root (nil)", name);
1180 else
1181 ch_log(NULL,
1182 "LKVAR: get_lval(): name %s, lr_tv %p lr_is_arg %d",
1183 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
1184 char buf[80];
1185 ch_log(NULL, "LKVAR: ...: GLV flags %s",
1186 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001187#endif
1188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001190 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001192 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001194 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001196 lp->ll_name_end = find_name_end(name, NULL, NULL,
1197 FNE_INCL_BR | fne_flags);
1198 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 }
1200
Bram Moolenaara749a422022-02-12 19:52:25 +00001201 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001202 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001203 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1204 {
1205 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1206 return NULL;
1207 }
1208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001209 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001210 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (expr_start != NULL)
1213 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001214 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001215 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 && *p != '[' && *p != '.')
1217 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001218 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 return NULL;
1220 }
1221
1222 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1223 if (lp->ll_exp_name == NULL)
1224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225 // Report an invalid expression in braces, unless the
1226 // expression evaluation has been cancelled due to an
1227 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (!aborting() && !quiet)
1229 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001230 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001231 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 return NULL;
1233 }
1234 }
1235 lp->ll_name = lp->ll_exp_name;
1236 }
1237 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 lp->ll_name = name;
1240
Bram Moolenaar4525a572022-02-13 11:57:33 +00001241 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001243 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001244 // However, "g:[key]" is indexing a dictionary.
1245 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001246 {
1247 --p;
1248 lp->ll_name_end = p;
1249 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001250 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001251 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001252 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001253
Bram Moolenaar9510d222022-09-11 15:14:05 +01001254 if (is_scoped_variable(name))
1255 {
1256 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1257 return NULL;
1258 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001259 if (VIM_ISWHITE(*p))
1260 {
1261 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1262 return NULL;
1263 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001264 if (tp == p + 1 && !quiet)
1265 {
1266 semsg(_(e_white_space_required_after_str_str), ":", p);
1267 return NULL;
1268 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001269 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001270 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001271 semsg(_(e_using_type_not_in_script_context_str), p);
1272 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001273 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001274 if (vim9script && (flags & GLV_NO_DECL) &&
1275 !(flags & GLV_FOR_LOOP))
1276 {
1277 // Using a type and not in a "var" declaration.
1278 semsg(_(e_trailing_characters_str), p);
1279 return NULL;
1280 }
1281
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001282
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001283 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001284 lp->ll_type = parse_type(&tp,
1285 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1286 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001287 if (lp->ll_type == NULL && !quiet)
1288 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001289 lp->ll_name_end = tp;
1290 }
Ernie Raelee865f32023-09-29 19:53:55 +02001291 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 }
1293 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001294 if (lp->ll_name == NULL)
1295 return p;
1296
Bram Moolenaar62aec932022-01-29 21:45:34 +00001297 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001298 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001299 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001300
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001301 if (import != NULL)
1302 {
1303 ufunc_T *ufunc;
1304 type_T *type;
1305
Bram Moolenaar753885b2022-08-24 16:30:36 +01001306 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001307 lp->ll_sid = import->imp_sid;
1308 lp->ll_name = skipwhite(p + 1);
1309 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1310 lp->ll_name_end = p;
1311
1312 // check the item is exported
1313 cc = *p;
1314 *p = NUL;
1315 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001316 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001317 {
1318 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001319 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001320 }
1321 *p = cc;
1322 }
1323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001325 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001326 if ((*p != '[' && *p != '.'))
Ernie Raelee865f32023-09-29 19:53:55 +02001327 {
1328 if (lval_root != NULL)
Ernie Rael64885642023-10-04 20:16:22 +02001329 get_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332
Bram Moolenaar4525a572022-02-13 11:57:33 +00001333 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001334 {
1335 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001336 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001337 v = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001338 cl_exec = lval_root->lr_cl_exec;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001339 }
1340 else
1341 {
1342 cc = *p;
1343 *p = NUL;
1344 // When we would write to the variable pass &ht and prevent autoload.
1345 writing = !(flags & GLV_READ_ONLY);
1346 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001347 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001348 if (v == NULL && !quiet)
1349 semsg(_(e_undefined_variable_str), lp->ll_name);
1350 *p = cc;
1351 if (v == NULL)
1352 return NULL;
1353 lp->ll_tv = &v->di_tv;
1354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355
Bram Moolenaar4525a572022-02-13 11:57:33 +00001356 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001357 {
1358 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001359 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001360 return NULL;
1361 }
1362
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 /*
1364 * Loop until no more [idx] or .key is following.
1365 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001366 var1.v_type = VAR_UNKNOWN;
1367 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001368 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001370 vartype_T v_type = lp->ll_tv->v_type;
1371
1372 if (*p == '.' && v_type != VAR_DICT
1373 && v_type != VAR_OBJECT
1374 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001375 {
1376 if (!quiet)
1377 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1378 return NULL;
1379 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001380 if (v_type != VAR_LIST
1381 && v_type != VAR_DICT
1382 && v_type != VAR_BLOB
1383 && v_type != VAR_OBJECT
1384 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001387 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001390
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001391 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001392 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001393 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001394 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001395 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001396 r = rettv_blob_alloc(lp->ll_tv);
1397 if (r == FAIL)
1398 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001403 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001406
Bram Moolenaar4525a572022-02-13 11:57:33 +00001407 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001408 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001409 && lp->ll_tv == &v->di_tv
1410 && ht != NULL && ht == get_script_local_ht())
1411 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001412 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001413
1414 // Vim9 script local variable: get the type
1415 if (sv != NULL)
1416 lp->ll_valtype = sv->sv_type;
1417 }
1418
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 len = -1;
1420 if (*p == '.')
1421 {
1422 key = p + 1;
1423 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1424 ;
1425 if (len == 0)
1426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001428 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001430 }
1431 p = key + len;
1432 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001433 else
1434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001435 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001436 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 if (*p == ':')
1438 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001439 else
1440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001441 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001442 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001444 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001446 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001447 clear_tv(&var1);
1448 return NULL;
1449 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001450 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001451 }
1452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001453 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001454 if (*p == ':')
1455 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001456 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001459 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001460 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001462 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001463 if (rettv != NULL
1464 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001465 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001467 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001470 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001471 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 }
1474 p = skipwhite(p + 1);
1475 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001477 else
1478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001480 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001481 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001482 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001483 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001485 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001486 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001488 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001489 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 clear_tv(&var2);
1491 return NULL;
1492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001496 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001497 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001498
Bram Moolenaar8c711452005-01-14 21:53:12 +00001499 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001502 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001503 clear_tv(&var1);
1504 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001505 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001506 }
1507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001508 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 ++p;
1510 }
1511
Bram Moolenaard505d172022-12-18 21:42:55 +00001512 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001513 {
1514 if (len == -1)
1515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001516 // "[key]": get key from "var1"
1517 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001518 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001519 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001520 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 }
1523 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001524 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001525 lp->ll_object = NULL;
1526 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001527
1528 // a NULL dict is equivalent with an empty dict
1529 if (lp->ll_tv->vval.v_dict == NULL)
1530 {
1531 lp->ll_tv->vval.v_dict = dict_alloc();
1532 if (lp->ll_tv->vval.v_dict == NULL)
1533 {
1534 clear_tv(&var1);
1535 return NULL;
1536 }
1537 ++lp->ll_tv->vval.v_dict->dv_refcount;
1538 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001540
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001541 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001543 // When assigning to a scope dictionary check that a function and
1544 // variable name is valid (only variable name unless it is l: or
1545 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001546 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001547 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001548 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001549
1550 if (len != -1)
1551 {
1552 prevval = key[len];
1553 key[len] = NUL;
1554 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001555 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001556 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001557 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001558 && (rettv->v_type == VAR_FUNC
1559 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001560 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001561 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001562 if (len != -1)
1563 key[len] = prevval;
1564 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001565 {
1566 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001567 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001568 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001569 }
1570
Bram Moolenaar10c65862020-10-08 21:16:42 +02001571 if (lp->ll_valtype != NULL)
1572 // use the type of the member
1573 lp->ll_valtype = lp->ll_valtype->tt_member;
1574
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001575 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001576 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001577 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001578 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001579 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001580 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001581 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001582 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001583 return NULL;
1584 }
1585
Bram Moolenaar31b81602019-02-10 22:14:27 +01001586 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001587 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001589 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001590 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001591 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001592 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001593 }
1594 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001596 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001598 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001600 p = NULL;
1601 break;
1602 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001603 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001604 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001605 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1606 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001607 {
1608 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001609 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001610 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001611
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001612 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001613 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001614 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001615 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001617 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1618
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001619 /*
1620 * Get the number and item for the only or first index of the List.
1621 */
1622 if (empty1)
1623 lp->ll_n1 = 0;
1624 else
1625 // is number or string
1626 lp->ll_n1 = (long)tv_get_number(&var1);
1627 clear_tv(&var1);
1628
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001629 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001631 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001632 return NULL;
1633 }
1634 if (lp->ll_range && !lp->ll_empty2)
1635 {
1636 lp->ll_n2 = (long)tv_get_number(&var2);
1637 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001638 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1639 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001640 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001641 }
1642 lp->ll_blob = lp->ll_tv->vval.v_blob;
1643 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001644 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001646 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001647 {
1648 /*
1649 * Get the number and item for the only or first index of the List.
1650 */
1651 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001652 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001653 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001654 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001655 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001656 clear_tv(&var1);
1657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001659 lp->ll_object = NULL;
1660 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001661 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001662 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1663 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001664 if (lp->ll_li == NULL)
1665 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001666 clear_tv(&var2);
1667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001668 }
1669
Bram Moolenaar10c65862020-10-08 21:16:42 +02001670 if (lp->ll_valtype != NULL)
1671 // use the type of the member
1672 lp->ll_valtype = lp->ll_valtype->tt_member;
1673
Bram Moolenaar8c711452005-01-14 21:53:12 +00001674 /*
1675 * May need to find the item or absolute index for the second
1676 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001677 * When no index given: "lp->ll_empty2" is TRUE.
1678 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001679 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001680 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001682 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001683 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001684 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001685 if (check_range_index_two(lp->ll_list,
1686 &lp->ll_n1, lp->ll_li,
1687 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001688 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001689 }
1690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001692 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001693 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001694 {
Ernie Raelee865f32023-09-29 19:53:55 +02001695 lp->ll_dict = NULL;
1696 lp->ll_list = NULL;
1697
1698 class_T *cl;
1699 if (v_type == VAR_OBJECT && lp->ll_tv->vval.v_object != NULL)
1700 {
1701 cl = lp->ll_tv->vval.v_object->obj_class;
1702 lp->ll_object = lp->ll_tv->vval.v_object;
1703 }
1704 else
1705 {
1706 cl = lp->ll_tv->vval.v_class;
1707 lp->ll_object = NULL;
1708 }
1709 lp->ll_class = cl;
1710
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001711 // TODO: what if class is NULL?
1712 if (cl != NULL)
1713 {
1714 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001715
1716 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001717 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001718 // First look for a function with this name.
1719 // round 1: class functions (skipped for an object)
1720 // round 2: object methods
1721 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001722 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001723 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001724 int m_idx;
1725 ufunc_T *fp;
1726
1727 fp = method_lookup(cl,
1728 round == 1 ? VAR_CLASS : VAR_OBJECT,
1729 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001730 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001731 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001732 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001733 lp->ll_ufunc = fp;
1734 lp->ll_valtype = fp->uf_func_type;
1735 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001736 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001737 }
1738 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001739
Ernie Raelee865f32023-09-29 19:53:55 +02001740 // TODO: dont' check access if inside class
1741 // TODO: is GLV_READ_ONLY the right thing to use
1742 // for class/object member access?
1743 // Probably in some cases. Need inside class check
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001744 if (lp->ll_valtype == NULL)
1745 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001746 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001747 ocmember_T *om
1748 = member_lookup(cl, v_type, key, p - key, &m_idx);
1749 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001750 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001751 {
Ernie Rael64885642023-10-04 20:16:22 +02001752 if (get_lval_check_access(cl_exec, cl, om,
1753 p, flags) == FAIL)
1754 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001755
1756 lp->ll_valtype = om->ocm_type;
1757
1758 if (v_type == VAR_OBJECT)
1759 lp->ll_tv = ((typval_T *)(
1760 lp->ll_tv->vval.v_object + 1)) + m_idx;
1761 else
1762 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001763 }
1764 }
1765
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001766 if (lp->ll_valtype == NULL)
1767 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001768 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001769 return NULL;
1770 }
1771 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 }
1774
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001775 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 return p;
1778}
1779
1780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001782 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785{
1786 vim_free(lp->ll_exp_name);
1787 vim_free(lp->ll_newkey);
1788}
1789
1790/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001791 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001793 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1794 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797set_var_lval(
1798 lval_T *lp,
1799 char_u *endp,
1800 typval_T *rettv,
1801 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001802 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1803 char_u *op,
1804 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001805{
1806 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001807 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001808
1809 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001811 cc = *endp;
1812 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001813 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001814 return;
1815
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001816 if (lp->ll_blob != NULL)
1817 {
1818 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001819
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001820 if (op != NULL && *op != '=')
1821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001822 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001823 return;
1824 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001825 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001826 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001827
1828 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1829 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001830 if (lp->ll_empty2)
1831 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1832
Bram Moolenaar68452172021-04-12 21:21:02 +02001833 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1834 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001835 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001836 }
1837 else
1838 {
1839 val = (int)tv_get_number_chk(rettv, &error);
1840 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001841 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001842 }
1843 }
1844 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001846 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001848 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1849 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001850 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001851 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001852 *endp = cc;
1853 return;
1854 }
1855
Bram Moolenaarff697e62019-02-12 22:28:33 +01001856 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001857 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001858 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001859 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001860 {
1861 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001862 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1863 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001864 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001865 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001866 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001867 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001870 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001871 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001872 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1873 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001874 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001875 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001876 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001877 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001878 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001880 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001881 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001882 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001884 else if (lp->ll_range)
1885 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001886 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1887 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001888 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001889 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001890 return;
1891 }
1892
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001893 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1894 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 }
1896 else
1897 {
1898 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001899 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001901 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1902 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001903 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001904 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001905 return;
1906 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001907
1908 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001909 && check_typval_arg_type(lp->ll_valtype, rettv,
1910 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001911 return;
1912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (lp->ll_newkey != NULL)
1914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 if (op != NULL && *op != '=')
1916 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001917 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 return;
1919 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001920 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1921 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001922 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001924 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001925 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 if (di == NULL)
1927 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001928 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1929 {
1930 vim_free(di);
1931 return;
1932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 else if (op != NULL && *op != '=')
1936 {
1937 tv_op(lp->ll_tv, rettv, op);
1938 return;
1939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 /*
1944 * Assign the value to the variable or list item.
1945 */
1946 if (copy)
1947 copy_tv(rettv, lp->ll_tv);
1948 else
1949 {
1950 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001951 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 }
1954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955}
1956
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001958 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1959 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 * Returns OK or FAIL.
1961 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001965 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966 char_u numbuf[NUMBUFLEN];
1967 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001968 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001970 // Can't do anything with a Funcref or Dict on the right.
1971 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001972 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001973 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1974 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 {
1976 switch (tv1->v_type)
1977 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001978 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001979 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 case VAR_DICT:
1982 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001983 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001984 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001985 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001986 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001988 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001989 case VAR_CLASS:
1990 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 break;
1992
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001993 case VAR_BLOB:
1994 if (*op != '+' || tv2->v_type != VAR_BLOB)
1995 break;
1996 // BLOB += BLOB
1997 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1998 {
1999 blob_T *b1 = tv1->vval.v_blob;
2000 blob_T *b2 = tv2->vval.v_blob;
2001 int i, len = blob_len(b2);
2002 for (i = 0; i < len; i++)
2003 ga_append(&b1->bv_ga, blob_get(b2, i));
2004 }
2005 return OK;
2006
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007 case VAR_LIST:
2008 if (*op != '+' || tv2->v_type != VAR_LIST)
2009 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002010 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002011 if (tv2->vval.v_list != NULL)
2012 {
2013 if (tv1->vval.v_list == NULL)
2014 {
2015 tv1->vval.v_list = tv2->vval.v_list;
2016 ++tv1->vval.v_list->lv_refcount;
2017 }
2018 else
2019 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 return OK;
2022
2023 case VAR_NUMBER:
2024 case VAR_STRING:
2025 if (tv2->v_type == VAR_LIST)
2026 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002027 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002029 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002030 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002031 if (tv2->v_type == VAR_FLOAT)
2032 {
2033 float_T f = n;
2034
Bram Moolenaarff697e62019-02-12 22:28:33 +01002035 if (*op == '%')
2036 break;
2037 switch (*op)
2038 {
2039 case '+': f += tv2->vval.v_float; break;
2040 case '-': f -= tv2->vval.v_float; break;
2041 case '*': f *= tv2->vval.v_float; break;
2042 case '/': f /= tv2->vval.v_float; break;
2043 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002044 clear_tv(tv1);
2045 tv1->v_type = VAR_FLOAT;
2046 tv1->vval.v_float = f;
2047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002049 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002050 switch (*op)
2051 {
2052 case '+': n += tv_get_number(tv2); break;
2053 case '-': n -= tv_get_number(tv2); break;
2054 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002055 case '/': n = num_divide(n, tv_get_number(tv2),
2056 &failed); break;
2057 case '%': n = num_modulus(n, tv_get_number(tv2),
2058 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002059 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002060 clear_tv(tv1);
2061 tv1->v_type = VAR_NUMBER;
2062 tv1->vval.v_number = n;
2063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 }
2065 else
2066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002067 if (tv2->v_type == VAR_FLOAT)
2068 break;
2069
Bram Moolenaarff697e62019-02-12 22:28:33 +01002070 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002071 s = tv_get_string(tv1);
2072 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 clear_tv(tv1);
2074 tv1->v_type = VAR_STRING;
2075 tv1->vval.v_string = s;
2076 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002077 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002078
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002079 case VAR_FLOAT:
2080 {
2081 float_T f;
2082
Bram Moolenaarff697e62019-02-12 22:28:33 +01002083 if (*op == '%' || *op == '.'
2084 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002085 && tv2->v_type != VAR_NUMBER
2086 && tv2->v_type != VAR_STRING))
2087 break;
2088 if (tv2->v_type == VAR_FLOAT)
2089 f = tv2->vval.v_float;
2090 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002091 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002092 switch (*op)
2093 {
2094 case '+': tv1->vval.v_float += f; break;
2095 case '-': tv1->vval.v_float -= f; break;
2096 case '*': tv1->vval.v_float *= f; break;
2097 case '/': tv1->vval.v_float /= f; break;
2098 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002099 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002100 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 }
2102 }
2103
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002104 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 return FAIL;
2106}
2107
2108/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109 * Evaluate the expression used in a ":for var in expr" command.
2110 * "arg" points to "var".
2111 * Set "*errp" to TRUE for an error, FALSE otherwise;
2112 * Return a pointer that holds the info. Null when there is an error.
2113 */
2114 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115eval_for_line(
2116 char_u *arg,
2117 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002118 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002119 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120{
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002122 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 typval_T tv;
2125 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002126 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002128 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002130 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131 if (fi == NULL)
2132 return NULL;
2133
Bram Moolenaar404557e2021-07-05 21:41:48 +02002134 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2135 &fi->fi_semicolon, FALSE);
2136 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137 return fi;
2138
Bram Moolenaar404557e2021-07-05 21:41:48 +02002139 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002140 if (expr[0] != 'i' || expr[1] != 'n'
2141 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002142 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002143 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2144 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2145 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002146 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 return fi;
2148 }
2149
2150 if (skip)
2151 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002152 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2153 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154 {
2155 *errp = FALSE;
2156 if (!skip)
2157 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002158 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002159 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002160 l = tv.vval.v_list;
2161 if (l == NULL)
2162 {
2163 // a null list is like an empty list: do nothing
2164 clear_tv(&tv);
2165 }
2166 else
2167 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002169 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002171 // No need to increment the refcount, it's already set for
2172 // the list being used in "tv".
2173 fi->fi_list = l;
2174 list_add_watch(l, &fi->fi_lw);
2175 fi->fi_lw.lw_item = l->lv_first;
2176 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002177 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002178 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002179 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002180 fi->fi_bi = 0;
2181 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002182 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002183 typval_T btv;
2184
2185 // Make a copy, so that the iteration still works when the
2186 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002188 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002189 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002190 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002191 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002192 else if (tv.v_type == VAR_STRING)
2193 {
2194 fi->fi_byte_idx = 0;
2195 fi->fi_string = tv.vval.v_string;
2196 tv.vval.v_string = NULL;
2197 if (fi->fi_string == NULL)
2198 fi->fi_string = vim_strsave((char_u *)"");
2199 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002200 else
2201 {
Sean Dewar80d73952021-08-04 19:25:54 +02002202 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002203 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002204 }
2205 }
2206 }
2207 if (skip)
2208 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002209 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002210
2211 return fi;
2212}
2213
2214/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002215 * Used when looping over a :for line, skip the "in expr" part.
2216 */
2217 void
2218skip_for_lines(void *fi_void, evalarg_T *evalarg)
2219{
2220 forinfo_T *fi = (forinfo_T *)fi_void;
2221 int i;
2222
2223 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002224 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002225}
2226
2227/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002228 * Use the first item in a ":for" list. Advance to the next.
2229 * Assign the values to the variable (list). "arg" points to the first one.
2230 * Return TRUE when a valid item was found, FALSE when at end of list or
2231 * something wrong.
2232 */
2233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002235{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002236 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002238 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002239 ? (ASSIGN_FINAL
2240 // first round: error if variable exists
2241 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002242 | ASSIGN_NO_MEMBER_TYPE
2243 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002244 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002246 int skip_assign = in_vim9script() && arg[0] == '_'
2247 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002248
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002249 if (fi->fi_blob != NULL)
2250 {
2251 typval_T tv;
2252
2253 if (fi->fi_bi >= blob_len(fi->fi_blob))
2254 return FALSE;
2255 tv.v_type = VAR_NUMBER;
2256 tv.v_lock = VAR_FIXED;
2257 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2258 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002259 if (skip_assign)
2260 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002261 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002262 fi->fi_varcount, flag, NULL) == OK;
2263 }
2264
2265 if (fi->fi_string != NULL)
2266 {
2267 typval_T tv;
2268 int len;
2269
2270 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2271 if (len == 0)
2272 return FALSE;
2273 tv.v_type = VAR_STRING;
2274 tv.v_lock = VAR_FIXED;
2275 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2276 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002277 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002278 if (skip_assign)
2279 result = TRUE;
2280 else
2281 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002282 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002283 vim_free(tv.vval.v_string);
2284 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002285 }
2286
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002287 item = fi->fi_lw.lw_item;
2288 if (item == NULL)
2289 result = FALSE;
2290 else
2291 {
2292 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002293 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002294 if (skip_assign)
2295 result = TRUE;
2296 else
2297 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002298 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 }
2300 return result;
2301}
2302
2303/*
2304 * Free the structure used to store info used by ":for".
2305 */
2306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002307free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002308{
Bram Moolenaar33570922005-01-25 22:26:29 +00002309 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002310
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002311 if (fi == NULL)
2312 return;
2313 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002314 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002315 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002316 list_unref(fi->fi_list);
2317 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002318 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002319 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002320 else
2321 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 vim_free(fi);
2323}
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002326set_context_for_expression(
2327 expand_T *xp,
2328 char_u *arg,
2329 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002331 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002335 if (cmdidx == CMD_let || cmdidx == CMD_var
2336 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 {
2338 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002341 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002343 {
2344 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002345 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002346 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002347 break;
2348 }
2349 return;
2350 }
2351 }
2352 else
2353 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2354 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 while ((xp->xp_pattern = vim_strpbrk(arg,
2356 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2357 {
2358 c = *xp->xp_pattern;
2359 if (c == '&')
2360 {
2361 c = xp->xp_pattern[1];
2362 if (c == '&')
2363 {
2364 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002365 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002370 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2371 xp->xp_pattern += 2;
2372
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375 else if (c == '$')
2376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 xp->xp_context = EXPAND_ENV_VARS;
2379 }
2380 else if (c == '=')
2381 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002382 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 xp->xp_context = EXPAND_EXPRESSION;
2384 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002385 else if (c == '#'
2386 && xp->xp_context == EXPAND_EXPRESSION)
2387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002389 break;
2390 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002391 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 && xp->xp_context == EXPAND_FUNCTIONS
2393 && vim_strchr(xp->xp_pattern, '(') == NULL)
2394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002395 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 break;
2397 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002398 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002400 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2403 if (c == '\\' && xp->xp_pattern[1] != NUL)
2404 ++xp->xp_pattern;
2405 xp->xp_context = EXPAND_NOTHING;
2406 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002407 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002409 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2411 /* skip */ ;
2412 xp->xp_context = EXPAND_NOTHING;
2413 }
2414 else if (c == '|')
2415 {
2416 if (xp->xp_pattern[1] == '|')
2417 {
2418 ++xp->xp_pattern;
2419 xp->xp_context = EXPAND_EXPRESSION;
2420 }
2421 else
2422 xp->xp_context = EXPAND_COMMANDS;
2423 }
2424 else
2425 xp->xp_context = EXPAND_EXPRESSION;
2426 }
2427 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002428 // Doesn't look like something valid, expand as an expression
2429 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 arg = xp->xp_pattern;
2432 if (*arg != NUL)
2433 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2434 /* skip */ ;
2435 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002436
2437 // ":exe one two" completes "two"
2438 if ((cmdidx == CMD_execute
2439 || cmdidx == CMD_echo
2440 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002441 || cmdidx == CMD_echomsg
2442 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002443 && xp->xp_context == EXPAND_EXPRESSION)
2444 {
2445 for (;;)
2446 {
2447 char_u *n = skiptowhite(arg);
2448
2449 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2450 break;
2451 arg = skipwhite(n);
2452 }
2453 }
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 xp->xp_pattern = arg;
2456}
2457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002459 * Return TRUE if "pat" matches "text".
2460 * Does not use 'cpo' and always uses 'magic'.
2461 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002462 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002463pattern_match(char_u *pat, char_u *text, int ic)
2464{
2465 int matches = FALSE;
2466 char_u *save_cpo;
2467 regmatch_T regmatch;
2468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002469 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002470 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002471 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002472 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2473 if (regmatch.regprog != NULL)
2474 {
2475 regmatch.rm_ic = ic;
2476 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2477 vim_regfree(regmatch.regprog);
2478 }
2479 p_cpo = save_cpo;
2480 return matches;
2481}
2482
2483/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002484 * Handle a name followed by "(". Both for just "name(arg)" and for
2485 * "expr->name(arg)".
2486 * Returns OK or FAIL.
2487 */
2488 static int
2489eval_func(
2490 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002491 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002492 char_u *name,
2493 int name_len,
2494 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002495 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002496 typval_T *basetv) // "expr" for "expr->name(arg)"
2497{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002498 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002499 char_u *s = name;
2500 int len = name_len;
2501 partial_T *partial;
2502 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002503 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002504 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002505
2506 if (!evaluate)
2507 check_vars(s, len);
2508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002509 // If "s" is the name of a variable of type VAR_FUNC
2510 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002511 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002512 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002514 // Need to make a copy, in case evaluating the arguments makes
2515 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002516 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002517 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002518 ret = FAIL;
2519 else
2520 {
2521 funcexe_T funcexe;
2522
2523 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002524 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002525 funcexe.fe_firstline = curwin->w_cursor.lnum;
2526 funcexe.fe_lastline = curwin->w_cursor.lnum;
2527 funcexe.fe_evaluate = evaluate;
2528 funcexe.fe_partial = partial;
2529 funcexe.fe_basetv = basetv;
2530 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002531 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002532 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002533 }
2534 vim_free(s);
2535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002536 // If evaluate is FALSE rettv->v_type was not set in
2537 // get_func_tv, but it's needed in handle_subscript() to parse
2538 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002539 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2540 {
2541 rettv->vval.v_string = NULL;
2542 rettv->v_type = VAR_FUNC;
2543 }
2544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002545 // Stop the expression evaluation when immediately
2546 // aborting on error, or when an interrupt occurred or
2547 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002548 if (evaluate && aborting())
2549 {
2550 if (ret == OK)
2551 clear_tv(rettv);
2552 ret = FAIL;
2553 }
2554 return ret;
2555}
2556
2557/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002558 * After a NL, skip over empty lines and comment-only lines.
2559 */
2560 static char_u *
2561newline_skip_comments(char_u *arg)
2562{
2563 char_u *p = arg + 1;
2564
2565 for (;;)
2566 {
2567 p = skipwhite(p);
2568
2569 if (*p == NUL)
2570 break;
2571 if (vim9_comment_start(p))
2572 {
2573 char_u *nl = vim_strchr(p, NL);
2574
2575 if (nl == NULL)
2576 break;
2577 p = nl;
2578 }
2579 if (*p != NL)
2580 break;
2581 ++p; // skip another NL
2582 }
2583 return p;
2584}
2585
2586/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002587 * Get the next line source line without advancing. But do skip over comment
2588 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002589 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002590 */
2591 static char_u *
2592getline_peek_skip_comments(evalarg_T *evalarg)
2593{
2594 for (;;)
2595 {
2596 char_u *next = getline_peek(evalarg->eval_getline,
2597 evalarg->eval_cookie);
2598 char_u *p;
2599
2600 if (next == NULL)
2601 break;
2602 p = skipwhite(next);
2603 if (*p != NUL && !vim9_comment_start(p))
2604 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002605 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002606 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002607 }
2608 return NULL;
2609}
2610
2611/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002612 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2613 * comment) and there is a next line, return the next line (skipping blanks)
2614 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002615 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2616 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617 * "arg" must point somewhere inside a line, not at the start.
2618 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002619 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002620eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2621{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002622 char_u *p = skipwhite(arg);
2623
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002625 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002627 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2628 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002629 && (*p == NUL || *p == NL
2630 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002631 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002632 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002633
Bram Moolenaare442d592022-05-05 12:20:28 +01002634 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002635 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002636 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002637 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002638 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002639 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002640
Bram Moolenaar9d489562020-07-30 20:08:50 +02002641 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002642 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002643 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002644 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002645 }
2646 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002647 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648}
2649
2650/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002651 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002652 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002653 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002654 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002655eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002656{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002657 garray_T *gap = &evalarg->eval_ga;
2658 char_u *line;
2659
Bram Moolenaar39be4982022-05-06 21:51:50 +01002660 if (arg != NULL)
2661 {
2662 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002663 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002664 // Truncate before a trailing comment, so that concatenating the lines
2665 // won't turn the rest into a comment.
2666 if (*skipwhite(arg) == '#')
2667 *arg = NUL;
2668 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002669
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002670 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002671 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2672 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002673 else
2674 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002675 if (line == NULL)
2676 return NULL;
2677
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002678 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002679 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2680 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002681 char_u *p = skipwhite(line);
2682
2683 // Going to concatenate the lines after parsing. For an empty or
2684 // comment line use an empty string.
2685 if (*p == NUL || vim9_comment_start(p))
2686 {
2687 vim_free(line);
2688 line = vim_strsave((char_u *)"");
2689 }
2690
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002691 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2692 ++gap->ga_len;
2693 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002694 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002695 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002696 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002697 evalarg->eval_tofree = line;
2698 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002699
2700 // Advanced to the next line, "arg" no longer points into the previous
2701 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002702 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002703 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002704}
2705
Bram Moolenaare6b53242020-07-01 17:28:33 +02002706/*
2707 * Call eval_next_non_blank() and get the next line if needed.
2708 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002709 char_u *
2710skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2711{
2712 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002713 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002714
Bram Moolenaar962d7212020-07-04 14:15:00 +02002715 if (evalarg == NULL)
2716 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002717 eval_next_non_blank(p, evalarg, &getnext);
2718 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002719 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002720 return p;
2721}
2722
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002723/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002724 * The "eval" functions have an "evalarg" argument: When NULL or
2725 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2726 * parsed but not executed. The functions may return OK, but the rettv will be
2727 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
2729
2730/*
2731 * Handle zero level expression.
2732 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002734 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * Return OK or FAIL.
2737 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739eval0(
2740 char_u *arg,
2741 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002742 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002743 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002745 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2746}
2747
2748/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002749 * If "arg" is a simple function call without arguments then call it and return
2750 * the result. Otherwise return NOTDONE.
2751 */
2752 int
2753may_call_simple_func(
2754 char_u *arg,
2755 typval_T *rettv)
2756{
2757 char_u *parens = (char_u *)strstr((char *)arg, "()");
2758 int r = NOTDONE;
2759
2760 // If the expression is "FuncName()" then we can skip a lot of overhead.
2761 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2762 {
2763 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2764
2765 if (to_name_end(p, TRUE) == parens)
2766 r = call_simple_func(arg, (int)(parens - arg), rettv);
2767 }
2768 return r;
2769}
2770
2771/*
2772 * Handle zero level expression with optimization for a simple function call.
2773 * Same arguments and return value as eval0().
2774 */
2775 int
2776eval0_simple_funccal(
2777 char_u *arg,
2778 typval_T *rettv,
2779 exarg_T *eap,
2780 evalarg_T *evalarg)
2781{
2782 int r = may_call_simple_func(arg, rettv);
2783
2784 if (r == NOTDONE)
2785 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2786 return r;
2787}
2788
2789/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002790 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2791 * expression and don't check what comes after the expression.
2792 */
2793 int
2794eval0_retarg(
2795 char_u *arg,
2796 typval_T *rettv,
2797 exarg_T *eap,
2798 evalarg_T *evalarg,
2799 char_u **retarg)
2800{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 int ret;
2802 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002803 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002804 int did_emsg_before = did_emsg;
2805 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002806 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002807 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002811
Bram Moolenaar79481362022-06-27 20:15:10 +01002812 if (ret != FAIL)
2813 {
2814 expr_end = p;
2815 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002816
Bram Moolenaar79481362022-06-27 20:15:10 +01002817 // In Vim9 script a command block is not split at NL characters for
2818 // commands using an expression argument. Skip over a '#' comment to
2819 // check for a following NL. Require white space before the '#'.
2820 if (in_vim9script() && p > expr_end && retarg == NULL)
2821 while (*p == '#')
2822 {
2823 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002824
Bram Moolenaar79481362022-06-27 20:15:10 +01002825 if (nl == NULL)
2826 break;
2827 p = skipwhite(nl + 1);
2828 if (eap != NULL && *p != NUL)
2829 eap->nextcmd = p;
2830 check_for_end = FALSE;
2831 }
2832
2833 if (check_for_end)
2834 end_error = !ends_excmd2(arg, p);
2835 }
2836
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002837 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 /*
2842 * Report the invalid expression unless the expression evaluation has
2843 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002844 * exception, or we already gave a more specific error.
2845 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002847 if (!aborting()
2848 && did_emsg == did_emsg_before
2849 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002850 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002851 {
2852 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002853 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002854 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002855 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002856 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002857
zeertzjqf2588b62023-05-05 17:22:35 +01002858 if (eap != NULL && p != NULL)
2859 {
2860 // Some of the expression may not have been consumed.
2861 // Only execute a next command if it cannot be a "||" operator.
2862 // The next command may be "catch".
2863 char_u *nextcmd = check_nextcmd(p);
2864 if (nextcmd != NULL && *nextcmd != '|')
2865 eap->nextcmd = nextcmd;
2866 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002867 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002869
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002870 if (retarg != NULL)
2871 *retarg = p;
2872 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002873 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return ret;
2876}
2877
2878/*
2879 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002880 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002881 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 *
2883 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002884 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002886 * Note: "rettv.v_lock" is not set.
2887 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * Return OK or FAIL.
2889 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002890 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002893 char_u *p;
2894 int getnext;
2895
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002896 CLEAR_POINTER(rettv);
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /*
2899 * Get the first variable.
2900 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002901 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return FAIL;
2903
Bram Moolenaar793648f2020-06-26 21:28:25 +02002904 p = eval_next_non_blank(*arg, evalarg, &getnext);
2905 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002907 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908 int result;
2909 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002910 evalarg_T *evalarg_used = evalarg;
2911 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002912 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002913 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002914 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915
2916 if (evalarg == NULL)
2917 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002918 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002919 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002921 orig_flags = evalarg_used->eval_flags;
2922 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002924 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002925 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002926 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002927 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002928 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002929 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002930 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002931 clear_tv(rettv);
2932 return FAIL;
2933 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002934 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002935 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002938 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002940 int error = FALSE;
2941
Bram Moolenaar13106602020-10-04 16:06:05 +02002942 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002943 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002944 else if (vim9script)
2945 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002946 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002948 if (error || !op_falsy || !result)
2949 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002950 if (error)
2951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953
2954 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002955 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002957 if (op_falsy)
2958 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002959 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002960 {
mityu4ac198c2021-05-28 17:52:40 +02002961 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002962 clear_tv(rettv);
2963 return FAIL;
2964 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002965 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002966 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002967 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002968 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002969 {
2970 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002972 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002973 if (!op_falsy || !result)
2974 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002976 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002978 /*
2979 * Check for the ":".
2980 */
2981 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2982 if (*p != ':')
2983 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002984 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002985 if (evaluate && result)
2986 clear_tv(rettv);
2987 evalarg_used->eval_flags = orig_flags;
2988 return FAIL;
2989 }
2990 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002991 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002992 else
2993 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002994 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002995 {
2996 error_white_both(p, 1);
2997 clear_tv(rettv);
2998 evalarg_used->eval_flags = orig_flags;
2999 return FAIL;
3000 }
3001 *arg = p;
3002 }
3003
3004 /*
3005 * Get the third variable. Recursive!
3006 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003007 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003008 {
mityu4ac198c2021-05-28 17:52:40 +02003009 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003010 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003011 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003012 return FAIL;
3013 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003014 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3015 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003016 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003017 if (eval1(arg, &var2, evalarg_used) == FAIL)
3018 {
3019 if (evaluate && result)
3020 clear_tv(rettv);
3021 evalarg_used->eval_flags = orig_flags;
3022 return FAIL;
3023 }
3024 if (evaluate && !result)
3025 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003027
3028 if (evalarg == NULL)
3029 clear_evalarg(&local_evalarg, NULL);
3030 else
3031 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
3034 return OK;
3035}
3036
3037/*
3038 * Handle first level expression:
3039 * expr2 || expr2 || expr2 logical OR
3040 *
3041 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003042 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 *
3044 * Return OK or FAIL.
3045 */
3046 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003049 char_u *p;
3050 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
3052 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003053 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 return FAIL;
3057
3058 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003059 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003061 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003062 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003064 evalarg_T *evalarg_used = evalarg;
3065 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003066 int evaluate;
3067 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003068 long result = FALSE;
3069 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003070 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003071 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003072
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003073 if (evalarg == NULL)
3074 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003075 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003076 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003078 orig_flags = evalarg_used->eval_flags;
3079 evaluate = orig_flags & EVAL_EVALUATE;
3080 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003081 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003082 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003083 result = tv_get_bool_chk(rettv, &error);
3084 else if (tv_get_number_chk(rettv, &error) != 0)
3085 result = TRUE;
3086 clear_tv(rettv);
3087 if (error)
3088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090
3091 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003092 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003094 while (p[0] == '|' && p[1] == '|')
3095 {
3096 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003097 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003098 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003099 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003100 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003101 {
3102 error_white_both(p, 2);
3103 clear_tv(rettv);
3104 return FAIL;
3105 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003106 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003107 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003108
3109 /*
3110 * Get the second variable.
3111 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003112 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003113 {
mityu4ac198c2021-05-28 17:52:40 +02003114 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003115 clear_tv(rettv);
3116 return FAIL;
3117 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003118 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3119 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003120 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003121 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003122 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003123
3124 /*
3125 * Compute the result.
3126 */
3127 if (evaluate && !result)
3128 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003129 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003130 result = tv_get_bool_chk(&var2, &error);
3131 else if (tv_get_number_chk(&var2, &error) != 0)
3132 result = TRUE;
3133 clear_tv(&var2);
3134 if (error)
3135 return FAIL;
3136 }
3137 if (evaluate)
3138 {
3139 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003140 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003141 rettv->v_type = VAR_BOOL;
3142 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003143 }
3144 else
3145 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003146 rettv->v_type = VAR_NUMBER;
3147 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003148 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003149 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003150
3151 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003153
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003154 if (evalarg == NULL)
3155 clear_evalarg(&local_evalarg, NULL);
3156 else
3157 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159
3160 return OK;
3161}
3162
3163/*
3164 * Handle second level expression:
3165 * expr3 && expr3 && expr3 logical AND
3166 *
3167 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003168 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 *
3170 * Return OK or FAIL.
3171 */
3172 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003175 char_u *p;
3176 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003179 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003181 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return FAIL;
3183
3184 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003185 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003187 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003188 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003190 evalarg_T *evalarg_used = evalarg;
3191 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003192 int orig_flags;
3193 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003194 long result = TRUE;
3195 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003196 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003197 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003198
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003199 if (evalarg == NULL)
3200 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003201 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003202 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003204 orig_flags = evalarg_used->eval_flags;
3205 evaluate = orig_flags & EVAL_EVALUATE;
3206 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003207 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003208 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003209 result = tv_get_bool_chk(rettv, &error);
3210 else if (tv_get_number_chk(rettv, &error) == 0)
3211 result = FALSE;
3212 clear_tv(rettv);
3213 if (error)
3214 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216
3217 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003218 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003220 while (p[0] == '&' && p[1] == '&')
3221 {
3222 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003223 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003224 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003225 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003226 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003227 {
3228 error_white_both(p, 2);
3229 clear_tv(rettv);
3230 return FAIL;
3231 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003232 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003233 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003234
3235 /*
3236 * Get the second variable.
3237 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003238 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003239 {
mityu4ac198c2021-05-28 17:52:40 +02003240 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003241 clear_tv(rettv);
3242 return FAIL;
3243 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003244 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3245 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003246 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003247 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003248 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003250
3251 /*
3252 * Compute the result.
3253 */
3254 if (evaluate && result)
3255 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003256 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003257 result = tv_get_bool_chk(&var2, &error);
3258 else if (tv_get_number_chk(&var2, &error) == 0)
3259 result = FALSE;
3260 clear_tv(&var2);
3261 if (error)
3262 return FAIL;
3263 }
3264 if (evaluate)
3265 {
3266 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003267 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003268 rettv->v_type = VAR_BOOL;
3269 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003270 }
3271 else
3272 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003273 rettv->v_type = VAR_NUMBER;
3274 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003275 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003276 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003277
3278 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003280
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003281 if (evalarg == NULL)
3282 clear_evalarg(&local_evalarg, NULL);
3283 else
3284 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286
3287 return OK;
3288}
3289
3290/*
3291 * Handle third level expression:
3292 * var1 == var2
3293 * var1 =~ var2
3294 * var1 != var2
3295 * var1 !~ var2
3296 * var1 > var2
3297 * var1 >= var2
3298 * var1 < var2
3299 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003300 * var1 is var2
3301 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 *
3303 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003304 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 *
3306 * Return OK or FAIL.
3307 */
3308 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003309eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003312 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003313 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003315 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
3317 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003318 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003320 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return FAIL;
3322
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003323 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003324
Bram Moolenaar696ba232020-07-29 21:20:41 +02003325 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003330 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003332 typval_T var2;
3333 int ic;
3334 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003335 int evaluate = evalarg == NULL
3336 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003337 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003338
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003339 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003340 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003341 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003342 p = *arg;
3343 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003344 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3345 {
mityu4ac198c2021-05-28 17:52:40 +02003346 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003347 clear_tv(rettv);
3348 return FAIL;
3349 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003350
Bram Moolenaar696ba232020-07-29 21:20:41 +02003351 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3352 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003353 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003354 clear_tv(rettv);
3355 return FAIL;
3356 }
3357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (p[len] == '?')
3360 {
3361 ic = TRUE;
3362 ++len;
3363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003364 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else if (p[len] == '#')
3366 {
3367 ic = FALSE;
3368 ++len;
3369 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003370 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003372 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /*
3375 * Get the second variable.
3376 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003377 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3378 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003379 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003380 clear_tv(rettv);
3381 return FAIL;
3382 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003383 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003384 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return FAIL;
3388 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003389 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003390 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003391 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003392
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003393 // use the line of the comparison for messages
3394 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003395 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003396 {
3397 ret = FAIL;
3398 clear_tv(rettv);
3399 }
3400 else
3401 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003402 clear_tv(&var2);
3403 return ret;
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406
3407 return OK;
3408}
3409
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003410/*
3411 * Make a copy of blob "tv1" and append blob "tv2".
3412 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 void
3414eval_addblob(typval_T *tv1, typval_T *tv2)
3415{
3416 blob_T *b1 = tv1->vval.v_blob;
3417 blob_T *b2 = tv2->vval.v_blob;
3418 blob_T *b = blob_alloc();
3419 int i;
3420
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003421 if (b == NULL)
3422 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003424 for (i = 0; i < blob_len(b1); i++)
3425 ga_append(&b->bv_ga, blob_get(b1, i));
3426 for (i = 0; i < blob_len(b2); i++)
3427 ga_append(&b->bv_ga, blob_get(b2, i));
3428
3429 clear_tv(tv1);
3430 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431}
3432
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003433/*
3434 * Make a copy of list "tv1" and append list "tv2".
3435 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 int
3437eval_addlist(typval_T *tv1, typval_T *tv2)
3438{
3439 typval_T var3;
3440
3441 // concatenate Lists
3442 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3443 {
3444 clear_tv(tv1);
3445 clear_tv(tv2);
3446 return FAIL;
3447 }
3448 clear_tv(tv1);
3449 *tv1 = var3;
3450 return OK;
3451}
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003454 * Handle the bitwise left/right shift operator expression:
3455 * var1 << var2
3456 * var1 >> var2
3457 *
3458 * "arg" must point to the first non-white of the expression.
3459 * "arg" is advanced to just after the recognized expression.
3460 *
3461 * Return OK or FAIL.
3462 */
3463 static int
3464eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3465{
3466 /*
3467 * Get the first expression.
3468 */
3469 if (eval6(arg, rettv, evalarg) == FAIL)
3470 return FAIL;
3471
3472 /*
3473 * Repeat computing, until no '<<' or '>>' is following.
3474 */
3475 for (;;)
3476 {
3477 char_u *p;
3478 int getnext;
3479 exprtype_T type;
3480 int evaluate;
3481 typval_T var2;
3482 int vim9script;
3483
3484 p = eval_next_non_blank(*arg, evalarg, &getnext);
3485 if (p[0] == '<' && p[1] == '<')
3486 type = EXPR_LSHIFT;
3487 else if (p[0] == '>' && p[1] == '>')
3488 type = EXPR_RSHIFT;
3489 else
3490 return OK;
3491
3492 // Handle a bitwise left or right shift operator
3493 if (rettv->v_type != VAR_NUMBER)
3494 {
3495 // left operand should be a number
3496 emsg(_(e_bitshift_ops_must_be_number));
3497 clear_tv(rettv);
3498 return FAIL;
3499 }
3500
3501 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3502 vim9script = in_vim9script();
3503 if (getnext)
3504 {
3505 *arg = eval_next_line(*arg, evalarg);
3506 p = *arg;
3507 }
3508 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3509 {
3510 error_white_both(*arg, 2);
3511 clear_tv(rettv);
3512 return FAIL;
3513 }
3514
3515 /*
3516 * Get the second variable.
3517 */
3518 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3519 {
3520 error_white_both(p, 2);
3521 clear_tv(rettv);
3522 return FAIL;
3523 }
3524 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3525 if (eval6(arg, &var2, evalarg) == FAIL)
3526 {
3527 clear_tv(rettv);
3528 return FAIL;
3529 }
3530
3531 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3532 {
3533 // right operand should be a positive number
3534 if (var2.v_type != VAR_NUMBER)
3535 emsg(_(e_bitshift_ops_must_be_number));
3536 else
dundargocc57b5bc2022-11-02 13:30:51 +00003537 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003538 clear_tv(rettv);
3539 clear_tv(&var2);
3540 return FAIL;
3541 }
3542
3543 if (evaluate)
3544 {
3545 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3546 // shifting more bits than we have always results in zero
3547 rettv->vval.v_number = 0;
3548 else if (type == EXPR_LSHIFT)
3549 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003550 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003551 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003552 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003553 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003554 }
3555
3556 clear_tv(&var2);
3557 }
3558
3559 return OK;
3560}
3561
3562/*
3563 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003564 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003566 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003567 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 *
3569 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003570 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 *
3572 * Return OK or FAIL.
3573 */
3574 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003575eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003578 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003580 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return FAIL;
3582
3583 /*
3584 * Repeat computing, until no '+', '-' or '.' is following.
3585 */
3586 for (;;)
3587 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003588 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003589 int getnext;
3590 char_u *p;
3591 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003592 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003593 int concat;
3594 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003595 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003596
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003597 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003598 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003599 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003600 p = eval_next_non_blank(*arg, evalarg, &getnext);
3601 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003602 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003603 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3604 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003606 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3607 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003608
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003609 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003610 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003611 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003612 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003613 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003614 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003615 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003616 {
mityu4ac198c2021-05-28 17:52:40 +02003617 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003618 clear_tv(rettv);
3619 return FAIL;
3620 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003621 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003622 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003623 if ((op != '+' || (rettv->v_type != VAR_LIST
3624 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003625 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003626 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003627 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003628 int error = FALSE;
3629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // For "list + ...", an illegal use of the first operand as
3631 // a number cannot be determined before evaluating the 2nd
3632 // operand: if this is also a list, all is ok.
3633 // For "something . ...", "something - ..." or "non-list + ...",
3634 // we know that the first operand needs to be a string or number
3635 // without evaluating the 2nd operand. So check before to avoid
3636 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003637 if (op != '.')
3638 tv_get_number_chk(rettv, &error);
3639 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003640 {
3641 clear_tv(rettv);
3642 return FAIL;
3643 }
3644 }
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 /*
3647 * Get the second variable.
3648 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003649 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003650 {
mityu89dcb4d2021-05-28 13:50:17 +02003651 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003652 clear_tv(rettv);
3653 return FAIL;
3654 }
3655 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003656 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return FAIL;
3660 }
3661
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003662 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 /*
3665 * Compute the result.
3666 */
3667 if (op == '.')
3668 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003669 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3670 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003671 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003672
Bram Moolenaar2e086612020-08-25 22:37:48 +02003673 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003674 || var2.v_type == VAR_CHANNEL
3675 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003676 semsg(_(e_using_invalid_value_as_string_str),
3677 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003678 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003679 {
3680 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3681 var2.vval.v_float);
3682 s2 = buf2;
3683 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003684 else
3685 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687 {
3688 clear_tv(rettv);
3689 clear_tv(&var2);
3690 return FAIL;
3691 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003692 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 clear_tv(rettv);
3694 rettv->v_type = VAR_STRING;
3695 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003697 else if (op == '+' && rettv->v_type == VAR_BLOB
3698 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003700 else if (op == '+' && rettv->v_type == VAR_LIST
3701 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003702 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 else
3707 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003708 int error = FALSE;
3709 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003710 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003712 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003714 f1 = rettv->vval.v_float;
3715 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003716 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003719 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720 if (error)
3721 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003722 // This can only happen for "list + non-list" or
3723 // "blob + non-blob". For "non-list + ..." or
3724 // "something - ...", we returned before evaluating the
3725 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003727 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 return FAIL;
3729 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 if (var2.v_type == VAR_FLOAT)
3731 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003732 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 if (var2.v_type == VAR_FLOAT)
3734 {
3735 f2 = var2.vval.v_float;
3736 n2 = 0;
3737 }
3738 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003741 if (error)
3742 {
3743 clear_tv(rettv);
3744 clear_tv(&var2);
3745 return FAIL;
3746 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003747 if (rettv->v_type == VAR_FLOAT)
3748 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003750 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003753 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3754 {
3755 if (op == '+')
3756 f1 = f1 + f2;
3757 else
3758 f1 = f1 - f2;
3759 rettv->v_type = VAR_FLOAT;
3760 rettv->vval.v_float = f1;
3761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 {
3764 if (op == '+')
3765 n1 = n1 + n2;
3766 else
3767 n1 = n1 - n2;
3768 rettv->v_type = VAR_NUMBER;
3769 rettv->vval.v_number = n1;
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 }
3775 return OK;
3776}
3777
3778/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003779 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * * number multiplication
3781 * / number division
3782 * % number modulo
3783 *
3784 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003785 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 *
3787 * Return OK or FAIL.
3788 */
3789 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003790eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003791 char_u **arg,
3792 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003793 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003796 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003799 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003801 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return FAIL;
3803
3804 /*
3805 * Repeat computing, until no '*', '/' or '%' is following.
3806 */
3807 for (;;)
3808 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003809 int evaluate;
3810 int getnext;
3811 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003812 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003813 int op;
3814 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003815 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003816 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003817
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003818 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003819 p = eval_next_non_blank(*arg, evalarg, &getnext);
3820 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003821 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003823
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003824 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003825 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003826 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003827 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003828 {
3829 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3830 {
mityu4ac198c2021-05-28 17:52:40 +02003831 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003832 clear_tv(rettv);
3833 return FAIL;
3834 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003835 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003838 f1 = 0;
3839 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003840 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003841 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003843 if (rettv->v_type == VAR_FLOAT)
3844 {
3845 f1 = rettv->vval.v_float;
3846 use_float = TRUE;
3847 n1 = 0;
3848 }
3849 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003850 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003852 if (error)
3853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 else
3856 n1 = 0;
3857
3858 /*
3859 * Get the second variable.
3860 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003861 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3862 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003863 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003864 clear_tv(rettv);
3865 return FAIL;
3866 }
3867 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003868 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003871 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873 if (var2.v_type == VAR_FLOAT)
3874 {
3875 if (!use_float)
3876 {
3877 f1 = n1;
3878 use_float = TRUE;
3879 }
3880 f2 = var2.vval.v_float;
3881 n2 = 0;
3882 }
3883 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003886 clear_tv(&var2);
3887 if (error)
3888 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 if (use_float)
3890 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 /*
3894 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003895 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003897 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003899 if (op == '*')
3900 f1 = f1 * f2;
3901 else if (op == '/')
3902 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003903#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003905 if (f2 == 0.0)
3906 {
3907 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003908 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003909 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003910 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003911 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003912 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003913 }
3914 else
3915 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003916#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003917 // We rely on the floating point library to handle divide
3918 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003920#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003923 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003924 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003925 return FAIL;
3926 }
3927 rettv->v_type = VAR_FLOAT;
3928 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930 else
3931 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003932 int failed = FALSE;
3933
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003934 if (op == '*')
3935 n1 = n1 * n2;
3936 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003937 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003939 n1 = num_modulus(n1, n2, &failed);
3940 if (failed)
3941 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003942
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003943 rettv->v_type = VAR_NUMBER;
3944 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 }
3948
3949 return OK;
3950}
3951
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003952/*
3953 * Handle a type cast before a base level expression.
3954 * "arg" must point to the first non-white of the expression.
3955 * "arg" is advanced to just after the recognized expression.
3956 * Return OK or FAIL.
3957 */
3958 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003959eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003960 char_u **arg,
3961 typval_T *rettv,
3962 evalarg_T *evalarg,
3963 int want_string) // after "." operator
3964{
3965 type_T *want_type = NULL;
3966 garray_T type_list; // list of pointers to allocated types
3967 int res;
3968 int evaluate = evalarg == NULL ? 0
3969 : (evalarg->eval_flags & EVAL_EVALUATE);
3970
3971 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003972 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3973 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003974 {
3975 ++*arg;
3976 ga_init2(&type_list, sizeof(type_T *), 10);
3977 want_type = parse_type(arg, &type_list, TRUE);
3978 if (want_type == NULL && (evaluate || **arg != '>'))
3979 {
3980 clear_type_list(&type_list);
3981 return FAIL;
3982 }
3983
3984 if (**arg != '>')
3985 {
3986 if (*skipwhite(*arg) == '>')
3987 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3988 else
3989 emsg(_(e_missing_gt));
3990 clear_type_list(&type_list);
3991 return FAIL;
3992 }
3993 ++*arg;
3994 *arg = skipwhite_and_linebreak(*arg, evalarg);
3995 }
3996
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003997 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003998
3999 if (want_type != NULL && evaluate)
4000 {
4001 if (res == OK)
4002 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004003 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4004 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004005
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004006 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004007 {
4008 if (want_type == &t_bool && actual != &t_bool
4009 && (actual->tt_flags & TTFLAG_BOOL_OK))
4010 {
4011 int n = tv2bool(rettv);
4012
4013 // can use "0" and "1" for boolean in some places
4014 clear_tv(rettv);
4015 rettv->v_type = VAR_BOOL;
4016 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4017 }
4018 else
4019 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004020 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004021
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004022 res = check_type(want_type, actual, TRUE, where);
4023 }
4024 }
4025 }
4026 clear_type_list(&type_list);
4027 }
4028
4029 return res;
4030}
4031
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004032 int
4033eval_leader(char_u **arg, int vim9)
4034{
4035 char_u *s = *arg;
4036 char_u *p = *arg;
4037
4038 while (*p == '!' || *p == '-' || *p == '+')
4039 {
4040 char_u *n = skipwhite(p + 1);
4041
4042 // ++, --, -+ and +- are not accepted in Vim9 script
4043 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4044 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004045 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004046 return FAIL;
4047 }
4048 p = n;
4049 }
4050 *arg = p;
4051 return OK;
4052}
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004055 * Check for a predefined value "true", "false" and "null.*".
4056 * Return OK when recognized.
4057 */
4058 int
4059handle_predefined(char_u *s, int len, typval_T *rettv)
4060{
4061 switch (len)
4062 {
4063 case 4: if (STRNCMP(s, "true", 4) == 0)
4064 {
4065 rettv->v_type = VAR_BOOL;
4066 rettv->vval.v_number = VVAL_TRUE;
4067 return OK;
4068 }
4069 if (STRNCMP(s, "null", 4) == 0)
4070 {
4071 rettv->v_type = VAR_SPECIAL;
4072 rettv->vval.v_number = VVAL_NULL;
4073 return OK;
4074 }
4075 break;
4076 case 5: if (STRNCMP(s, "false", 5) == 0)
4077 {
4078 rettv->v_type = VAR_BOOL;
4079 rettv->vval.v_number = VVAL_FALSE;
4080 return OK;
4081 }
4082 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004083 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4084 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004085#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004086 rettv->v_type = VAR_JOB;
4087 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004088#else
4089 rettv->v_type = VAR_SPECIAL;
4090 rettv->vval.v_number = VVAL_NULL;
4091#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004092 return OK;
4093 }
4094 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004095 case 9:
4096 if (STRNCMP(s, "null_", 5) != 0)
4097 break;
4098 if (STRNCMP(s + 5, "list", 4) == 0)
4099 {
4100 rettv->v_type = VAR_LIST;
4101 rettv->vval.v_list = NULL;
4102 return OK;
4103 }
4104 if (STRNCMP(s + 5, "dict", 4) == 0)
4105 {
4106 rettv->v_type = VAR_DICT;
4107 rettv->vval.v_dict = NULL;
4108 return OK;
4109 }
4110 if (STRNCMP(s + 5, "blob", 4) == 0)
4111 {
4112 rettv->v_type = VAR_BLOB;
4113 rettv->vval.v_blob = NULL;
4114 return OK;
4115 }
4116 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004117 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4118 {
4119 rettv->v_type = VAR_CLASS;
4120 rettv->vval.v_class = NULL;
4121 return OK;
4122 }
4123 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004124 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4125 {
4126 rettv->v_type = VAR_STRING;
4127 rettv->vval.v_string = NULL;
4128 return OK;
4129 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004130 if (STRNCMP(s, "null_object", 11) == 0)
4131 {
4132 rettv->v_type = VAR_OBJECT;
4133 rettv->vval.v_object = NULL;
4134 return OK;
4135 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004136 break;
4137 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004138 if (STRNCMP(s, "null_channel", 12) == 0)
4139 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004141 rettv->v_type = VAR_CHANNEL;
4142 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004143#else
4144 rettv->v_type = VAR_SPECIAL;
4145 rettv->vval.v_number = VVAL_NULL;
4146#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004147 return OK;
4148 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004149 if (STRNCMP(s, "null_partial", 12) == 0)
4150 {
4151 rettv->v_type = VAR_PARTIAL;
4152 rettv->vval.v_partial = NULL;
4153 return OK;
4154 }
4155 break;
4156 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4157 {
4158 rettv->v_type = VAR_FUNC;
4159 rettv->vval.v_string = NULL;
4160 return OK;
4161 }
4162 break;
4163 }
4164 return FAIL;
4165}
4166
4167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * Handle sixth level expression:
4169 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004170 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004171 * "string" string constant
4172 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * &option-name option value
4174 * @r register contents
4175 * identifier variable value
4176 * function() function call
4177 * $VAR environment variable
4178 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004179 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004181 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004182 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 *
4184 * Also handle:
4185 * ! in front logical NOT
4186 * - in front unary minus
4187 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004188 * trailing [] subscript in String or List
4189 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004190 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 *
4192 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004193 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004198eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004199 char_u **arg,
4200 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004201 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004204 int evaluate = evalarg != NULL
4205 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int len;
4207 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004208 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u *start_leader, *end_leader;
4210 int ret = OK;
4211 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004212 static int recurse = 0;
4213 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004222 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 */
4224 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004225 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 end_leader = *arg;
4228
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004229 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004230 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004231 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004232 ++*arg;
4233 return FAIL;
4234 }
4235
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004236 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004237 // and crash. With MSVC the stack is smaller.
4238 if (recurse ==
4239#ifdef _MSC_VER
4240 300
4241#else
4242 1000
4243#endif
4244 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004245 {
4246 semsg(_(e_expression_too_recursive_str), *arg);
4247 return FAIL;
4248 }
4249 ++recurse;
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 switch (**arg)
4252 {
4253 /*
4254 * Number constant.
4255 */
4256 case '0':
4257 case '1':
4258 case '2':
4259 case '3':
4260 case '4':
4261 case '5':
4262 case '6':
4263 case '7':
4264 case '8':
4265 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004266 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004267
4268 // Apply prefixed "-" and "+" now. Matters especially when
4269 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004270 if (ret == OK && evaluate && end_leader > start_leader
4271 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004272 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 break;
4274
4275 /*
4276 * String constant: "string".
4277 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004278 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 break;
4280
4281 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004282 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004284 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 break;
4286
4287 /*
4288 * List: [expr, expr]
4289 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004290 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 break;
4292
4293 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004294 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004295 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004296 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004297 {
4298 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4299 }
4300 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004301 {
4302 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004303 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004304 }
4305 else
4306 ret = NOTDONE;
4307 break;
4308
4309 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004310 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004311 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004312 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004313 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004314 ret = NOTDONE;
4315 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004316 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004317 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004318 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004319 break;
4320
4321 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004322 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004324 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 break;
4326
4327 /*
4328 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004329 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 */
LemonBoy2eaef102022-05-06 13:14:50 +01004331 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4332 ret = eval_interp_string(arg, rettv, evaluate);
4333 else
4334 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336
4337 /*
4338 * Register contents: @r.
4339 */
4340 case '@': ++*arg;
4341 if (evaluate)
4342 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004343 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004344 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004345 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004346 emsg_invreg(**arg);
4347 else
4348 {
4349 rettv->v_type = VAR_STRING;
4350 rettv->vval.v_string = get_reg_contents(**arg,
4351 GREG_EXPR_SRC);
4352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (**arg != NUL)
4355 ++*arg;
4356 break;
4357
4358 /*
4359 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004360 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004362 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004364 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004365 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004366 if (ret == OK && evaluate)
4367 {
4368 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4369
Bram Moolenaara9931532021-06-12 15:58:16 +02004370 // Compile it here to get the return type. The return
4371 // type is optional, when it's missing use t_unknown.
4372 // This is recognized in compile_return().
4373 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4374 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004375 if (compile_def_function(ufunc, FALSE,
4376 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004377 {
4378 clear_tv(rettv);
4379 ret = FAIL;
4380 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004381 }
4382 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004383 if (ret == NOTDONE)
4384 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004385 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004386 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004387
Bram Moolenaar9215f012020-06-27 21:18:00 +02004388 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004389 if (**arg == ')')
4390 ++*arg;
4391 else if (ret == OK)
4392 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004393 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004394 clear_tv(rettv);
4395 ret = FAIL;
4396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 break;
4399
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 break;
4402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004403
4404 if (ret == NOTDONE)
4405 {
4406 /*
4407 * Must be a variable or function name.
4408 * Can also be a curly-braces kind of name: {expr}.
4409 */
4410 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004411 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 if (alias != NULL)
4413 s = alias;
4414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004415 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004416 ret = FAIL;
4417 else
4418 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004419 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4420
Bram Moolenaar4525a572022-02-13 11:57:33 +00004421 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004422 {
4423 emsg(_(e_cannot_use_underscore_here));
4424 ret = FAIL;
4425 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004426 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004427 && s[0] == 's' && s[1] == ':')
4428 {
4429 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4430 ret = FAIL;
4431 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004432 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004433 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004434 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004435 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004436 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004439 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004440 // get the value of "true", "false", etc. or a variable
4441 ret = FAIL;
4442 if (vim9script)
4443 ret = handle_predefined(s, len, rettv);
4444 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004445 {
4446 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004447 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004448 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004449 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004451 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004452 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004453 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004454 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004455 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004456 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004458 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 }
4460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004461 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4462 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004463 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004464 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
4467 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4468 */
4469 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004470 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004471
4472 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004473 return ret;
4474}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004476/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004477 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004478 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004479 * Adjusts "end_leaderp" until it is at "start_leader".
4480 */
4481 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004482eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004483 typval_T *rettv,
4484 int numeric_only,
4485 char_u *start_leader,
4486 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004487{
4488 char_u *end_leader = *end_leaderp;
4489 int ret = OK;
4490 int error = FALSE;
4491 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004492 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004493 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004494 float_T f = 0.0;
4495
4496 if (rettv->v_type == VAR_FLOAT)
4497 f = rettv->vval.v_float;
4498 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004499 {
4500 while (VIM_ISWHITE(end_leader[-1]))
4501 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004502 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004503 val = tv2bool(rettv);
4504 else
4505 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004506 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004507 if (error)
4508 {
4509 clear_tv(rettv);
4510 ret = FAIL;
4511 }
4512 else
4513 {
4514 while (end_leader > start_leader)
4515 {
4516 --end_leader;
4517 if (*end_leader == '!')
4518 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004519 if (numeric_only)
4520 {
4521 ++end_leader;
4522 break;
4523 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004524 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004525 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004526 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004527 {
4528 rettv->v_type = VAR_BOOL;
4529 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4530 }
4531 else
4532 f = !f;
4533 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004534 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004535 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004536 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004537 type = VAR_BOOL;
4538 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004539 }
4540 else if (*end_leader == '-')
4541 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004542 if (rettv->v_type == VAR_FLOAT)
4543 f = -f;
4544 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004545 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004546 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004547 type = VAR_NUMBER;
4548 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004549 }
4550 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004551 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004554 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 else
4557 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004558 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004559 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004560 rettv->v_type = type;
4561 else
4562 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004563 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004566 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return ret;
4568}
4569
4570/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004571 * Call the function referred to in "rettv".
4572 */
4573 static int
4574call_func_rettv(
4575 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004576 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004577 typval_T *rettv,
4578 int evaluate,
4579 dict_T *selfdict,
4580 typval_T *basetv)
4581{
4582 partial_T *pt = NULL;
4583 funcexe_T funcexe;
4584 typval_T functv;
4585 char_u *s;
4586 int ret;
4587
4588 // need to copy the funcref so that we can clear rettv
4589 if (evaluate)
4590 {
4591 functv = *rettv;
4592 rettv->v_type = VAR_UNKNOWN;
4593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004595 if (functv.v_type == VAR_PARTIAL)
4596 {
4597 pt = functv.vval.v_partial;
4598 s = partial_name(pt);
4599 }
4600 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004601 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004602 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004603 if (s == NULL || *s == NUL)
4604 {
4605 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004606 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004607 goto theend;
4608 }
4609 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004610 }
4611 else
4612 s = (char_u *)"";
4613
Bram Moolenaara80faa82020-04-12 19:37:17 +02004614 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004615 funcexe.fe_firstline = curwin->w_cursor.lnum;
4616 funcexe.fe_lastline = curwin->w_cursor.lnum;
4617 funcexe.fe_evaluate = evaluate;
4618 funcexe.fe_partial = pt;
4619 funcexe.fe_selfdict = selfdict;
4620 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004621 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004622
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004623theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // Clear the funcref afterwards, so that deleting it while
4625 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004626 if (evaluate)
4627 clear_tv(&functv);
4628
4629 return ret;
4630}
4631
4632/*
4633 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004634 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004635 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4636 */
4637 static int
4638eval_lambda(
4639 char_u **arg,
4640 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004641 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004643{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004644 int evaluate = evalarg != NULL
4645 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004646 typval_T base = *rettv;
4647 int ret;
4648
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004649 rettv->v_type = VAR_UNKNOWN;
4650
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004651 if (**arg == '{')
4652 {
4653 // ->{lambda}()
4654 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4655 }
4656 else
4657 {
4658 // ->(lambda)()
4659 ++*arg;
4660 ret = eval1(arg, rettv, evalarg);
4661 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004662 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004663 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004664 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004665 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004666 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004667 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4668 && rettv->v_type != VAR_PARTIAL)
4669 {
4670 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4671 return FAIL;
4672 }
4673 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004674 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004675 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004676 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004677
4678 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004679 {
4680 if (verbose)
4681 {
4682 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004683 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004684 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004685 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004686 }
4687 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004688 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004689 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004690 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004691 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004692
4693 // Clear the funcref afterwards, so that deleting it while
4694 // evaluating the arguments is possible (see test55).
4695 if (evaluate)
4696 clear_tv(&base);
4697
4698 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004699}
4700
4701/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004702 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004703 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004704 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4705 */
4706 static int
4707eval_method(
4708 char_u **arg,
4709 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004710 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004712{
4713 char_u *name;
4714 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004715 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004716 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004717 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004718 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004719 int evaluate = evalarg != NULL
4720 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004721
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004722 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004723
Bram Moolenaarac92e252019-08-03 21:58:38 +02004724 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004725 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004726 if (alias != NULL)
4727 name = alias;
4728
4729 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004730 {
4731 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004732 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004733 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004734 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004735 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004736 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004737 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004738
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004739 // If there is no "(" immediately following, but there is further on,
4740 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4741 // Does not handle anything where "(" is part of the expression.
4742 *arg = skipwhite(*arg);
4743
4744 if (**arg != '(' && alias == NULL
4745 && (paren = vim_strchr(*arg, '(')) != NULL)
4746 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004747 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004748
4749 // Truncate the name a the "(". Avoid trying to get another line
4750 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004751 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004752 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4753 if (evalarg != NULL)
4754 {
4755 getline = evalarg->eval_getline;
4756 evalarg->eval_getline = NULL;
4757 }
4758
4759 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004760 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004761 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004762 *arg = name + len;
4763 ret = FAIL;
4764 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004765 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004766 {
4767 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004768 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004769 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004770
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004771 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004772 if (getline != NULL)
4773 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004774 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004775
4776 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004777 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004778 *arg = skipwhite(*arg);
4779
4780 if (**arg != '(')
4781 {
4782 if (verbose)
4783 semsg(_(e_missing_parenthesis_str), name);
4784 ret = FAIL;
4785 }
4786 else if (VIM_ISWHITE((*arg)[-1]))
4787 {
4788 if (verbose)
4789 emsg(_(e_no_white_space_allowed_before_parenthesis));
4790 ret = FAIL;
4791 }
4792 else
4793 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004794 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004795 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004796 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004797
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004798 // Clear the funcref afterwards, so that deleting it while
4799 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004800 if (evaluate)
4801 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004802 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004803
Bram Moolenaarac92e252019-08-03 21:58:38 +02004804 return ret;
4805}
4806
4807/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004808 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4809 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4811 */
4812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004813eval_index(
4814 char_u **arg,
4815 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004816 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004819 int evaluate = evalarg != NULL
4820 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004822 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004825 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004826 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004828 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4829 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004831 init_tv(&var1);
4832 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004833 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 /*
4836 * dict.name
4837 */
4838 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004839 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004841 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004843 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 }
4845 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 /*
4848 * something[idx]
4849 *
4850 * Get the (first) variable from inside the [].
4851 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004852 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004853 if (**arg == ':')
4854 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004855 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004857 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004858 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004859 semsg(_(e_white_space_required_before_and_after_str_at_str),
4860 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004861 clear_tv(&var1);
4862 return FAIL;
4863 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004864 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004866 int error = FALSE;
4867
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004868 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004869 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004870 && var1.v_type == VAR_FLOAT)
4871 {
4872 var1.vval.v_string = typval_tostring(&var1, TRUE);
4873 var1.v_type = VAR_STRING;
4874 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004875
Bram Moolenaar4525a572022-02-13 11:57:33 +00004876 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004877 tv_get_number_chk(&var1, &error);
4878 else
4879 error = tv_get_string_chk(&var1) == NULL;
4880 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004881 {
4882 // not a number or string
4883 clear_tv(&var1);
4884 return FAIL;
4885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004887
4888 /*
4889 * Get the second variable from inside the [:].
4890 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004891 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892 if (**arg == ':')
4893 {
4894 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004895 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004896 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004897 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004898 semsg(_(e_white_space_required_before_and_after_str_at_str),
4899 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004900 if (!empty1)
4901 clear_tv(&var1);
4902 return FAIL;
4903 }
4904 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 if (**arg == ']')
4906 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004907 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004909 if (!empty1)
4910 clear_tv(&var1);
4911 return FAIL;
4912 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004913 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 if (!empty1)
4917 clear_tv(&var1);
4918 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004919 return FAIL;
4920 }
4921 }
4922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004924 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 if (**arg != ']')
4926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004927 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004928 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929 clear_tv(&var1);
4930 if (range)
4931 clear_tv(&var2);
4932 return FAIL;
4933 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004934 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 }
4936
4937 if (evaluate)
4938 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004939 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004940 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004942
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004943 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004946 clear_tv(&var2);
4947 return res;
4948 }
4949 return OK;
4950}
4951
4952/*
4953 * Check if "rettv" can have an [index] or [sli:ce]
4954 */
4955 int
4956check_can_index(typval_T *rettv, int evaluate, int verbose)
4957{
4958 switch (rettv->v_type)
4959 {
4960 case VAR_FUNC:
4961 case VAR_PARTIAL:
4962 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004963 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004964 return FAIL;
4965 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004966 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004967 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004968 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004969 case VAR_BOOL:
4970 case VAR_SPECIAL:
4971 case VAR_JOB:
4972 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004973 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004974 case VAR_CLASS:
4975 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004976 if (verbose)
4977 emsg(_(e_cannot_index_special_variable));
4978 return FAIL;
4979 case VAR_UNKNOWN:
4980 case VAR_ANY:
4981 case VAR_VOID:
4982 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004984 emsg(_(e_cannot_index_special_variable));
4985 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004987 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004989 case VAR_STRING:
4990 case VAR_LIST:
4991 case VAR_DICT:
4992 case VAR_BLOB:
4993 break;
4994 case VAR_NUMBER:
4995 if (in_vim9script())
4996 emsg(_(e_cannot_index_number));
4997 break;
4998 }
4999 return OK;
5000}
5001
5002/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005003 * slice() function
5004 */
5005 void
5006f_slice(typval_T *argvars, typval_T *rettv)
5007{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005008 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005009 && ((argvars[0].v_type != VAR_STRING
5010 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005011 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005012 && check_for_list_arg(argvars, 0) == FAIL)
5013 || check_for_number_arg(argvars, 1) == FAIL
5014 || check_for_opt_number_arg(argvars, 2) == FAIL))
5015 return;
5016
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005017 if (check_can_index(argvars, TRUE, FALSE) != OK)
5018 return;
5019
5020 copy_tv(argvars, rettv);
5021 eval_index_inner(rettv, TRUE, argvars + 1,
5022 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5023 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005024}
5025
5026/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005027 * Apply index or range to "rettv".
5028 * "var1" is the first index, NULL for [:expr].
5029 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005030 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5031 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005032 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5033 */
5034 int
5035eval_index_inner(
5036 typval_T *rettv,
5037 int is_range,
5038 typval_T *var1,
5039 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005040 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005041 char_u *key,
5042 int keylen,
5043 int verbose)
5044{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005045 varnumber_T n1, n2 = 0;
5046 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005047
5048 n1 = 0;
5049 if (var1 != NULL && rettv->v_type != VAR_DICT)
5050 n1 = tv_get_number(var1);
5051
5052 if (is_range)
5053 {
5054 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005056 if (verbose)
5057 emsg(_(e_cannot_slice_dictionary));
5058 return FAIL;
5059 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005060 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005061 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005062 else
5063 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005064 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005065
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005066 switch (rettv->v_type)
5067 {
5068 case VAR_UNKNOWN:
5069 case VAR_ANY:
5070 case VAR_VOID:
5071 case VAR_FUNC:
5072 case VAR_PARTIAL:
5073 case VAR_FLOAT:
5074 case VAR_BOOL:
5075 case VAR_SPECIAL:
5076 case VAR_JOB:
5077 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005078 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005079 case VAR_CLASS:
5080 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 break; // not evaluating, skipping over subscript
5082
5083 case VAR_NUMBER:
5084 case VAR_STRING:
5085 {
5086 char_u *s = tv_get_string(rettv);
5087
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005089 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005090 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005091 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005092 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005093 else
5094 s = char_from_string(s, n1);
5095 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005096 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // The resulting variable is a substring. If the indexes
5099 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 if (n1 < 0)
5101 {
5102 n1 = len + n1;
5103 if (n1 < 0)
5104 n1 = 0;
5105 }
5106 if (n2 < 0)
5107 n2 = len + n2;
5108 else if (n2 >= len)
5109 n2 = len;
5110 if (n1 >= len || n2 < 0 || n1 > n2)
5111 s = NULL;
5112 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005113 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 }
5115 else
5116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005117 // The resulting variable is a string of a single
5118 // character. If the index is too big or negative the
5119 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 if (n1 >= len || n1 < 0)
5121 s = NULL;
5122 else
5123 s = vim_strnsave(s + n1, 1);
5124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 clear_tv(rettv);
5126 rettv->v_type = VAR_STRING;
5127 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005128 }
5129 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005131 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005132 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5133 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005134 break;
5135
5136 case VAR_LIST:
5137 if (var1 == NULL)
5138 n1 = 0;
5139 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005140 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005141 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005142 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005143 return FAIL;
5144 break;
5145
5146 case VAR_DICT:
5147 {
5148 dictitem_T *item;
5149 typval_T tmp;
5150
5151 if (key == NULL)
5152 {
5153 key = tv_get_string_chk(var1);
5154 if (key == NULL)
5155 return FAIL;
5156 }
5157
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005158 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005159
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005160 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005161 {
5162 if (verbose)
5163 {
5164 if (keylen > 0)
5165 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005166 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005167 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005168 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005169 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005170
5171 copy_tv(&item->di_tv, &tmp);
5172 clear_tv(rettv);
5173 *rettv = tmp;
5174 }
5175 break;
5176 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 return OK;
5178}
5179
5180/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005181 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005182 */
5183 char_u *
5184partial_name(partial_T *pt)
5185{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005186 if (pt != NULL)
5187 {
5188 if (pt->pt_name != NULL)
5189 return pt->pt_name;
5190 if (pt->pt_func != NULL)
5191 return pt->pt_func->uf_name;
5192 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005193 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005194}
5195
Bram Moolenaarddecc252016-04-06 22:59:37 +02005196 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005197partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005198{
5199 int i;
5200
5201 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005202 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005203 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005204 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005205 if (pt->pt_name != NULL)
5206 {
5207 func_unref(pt->pt_name);
5208 vim_free(pt->pt_name);
5209 }
5210 else
5211 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005212
Bram Moolenaar54656012021-06-09 20:50:46 +02005213 // "out_up" is no longer used, decrement refcount on partial that owns it.
5214 partial_unref(pt->pt_outer.out_up_partial);
5215
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005216 // Using pt_outer from another partial.
5217 partial_unref(pt->pt_outer_partial);
5218
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005219 // Decrease the reference count for the context of a closure. If down
5220 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005221 if (pt->pt_funcstack != NULL)
5222 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005223 --pt->pt_funcstack->fs_refcount;
5224 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005225 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005226 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005227 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5228 if (pt->pt_loopvars[i] != NULL)
5229 {
5230 --pt->pt_loopvars[i]->lvs_refcount;
5231 loopvars_check_refcount(pt->pt_loopvars[i]);
5232 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005233
Bram Moolenaarddecc252016-04-06 22:59:37 +02005234 vim_free(pt);
5235}
5236
5237/*
5238 * Unreference a closure: decrement the reference count and free it when it
5239 * becomes zero.
5240 */
5241 void
5242partial_unref(partial_T *pt)
5243{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005244 if (pt == NULL)
5245 return;
5246
5247 int done = FALSE;
5248
5249 if (--pt->pt_refcount <= 0)
5250 partial_free(pt);
5251
5252 // If the reference count goes down to one, the funcstack may be the
5253 // only reference and can be freed if no other partials reference it.
5254 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005255 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005256 // careful: if the funcstack is freed it may contain this partial
5257 // and it gets freed as well
5258 if (pt->pt_funcstack != NULL)
5259 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005260
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005261 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005262 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005263 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005264
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005265 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5266 if (pt->pt_loopvars[depth] != NULL
5267 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005268 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005269 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005270 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005271}
5272
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005273/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005274 * Return the next (unique) copy ID.
5275 * Used for serializing nested structures.
5276 */
5277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005278get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005279{
5280 current_copyID += COPYID_INC;
5281 return current_copyID;
5282}
5283
5284/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285 * Garbage collection for lists and dictionaries.
5286 *
5287 * We use reference counts to be able to free most items right away when they
5288 * are no longer used. But for composite items it's possible that it becomes
5289 * unused while the reference count is > 0: When there is a recursive
5290 * reference. Example:
5291 * :let l = [1, 2, 3]
5292 * :let d = {9: l}
5293 * :let l[1] = d
5294 *
5295 * Since this is quite unusual we handle this with garbage collection: every
5296 * once in a while find out which lists and dicts are not referenced from any
5297 * variable.
5298 *
5299 * Here is a good reference text about garbage collection (refers to Python
5300 * but it applies to all reference-counting mechanisms):
5301 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005302 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005303
5304/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005305 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005306 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005307 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005308 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005309 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005310garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005312 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314 buf_T *buf;
5315 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005316 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005317 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005319 if (!testing)
5320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005322 want_garbage_collect = FALSE;
5323 may_garbage_collect = FALSE;
5324 garbage_collect_at_exit = FALSE;
5325 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005326
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005327 // The execution stack can grow big, limit the size.
5328 if (exestack.ga_maxlen - exestack.ga_len > 500)
5329 {
5330 size_t new_len;
5331 char_u *pp;
5332 int n;
5333
5334 // Keep 150% of the current size, with a minimum of the growth size.
5335 n = exestack.ga_len / 2;
5336 if (n < exestack.ga_growsize)
5337 n = exestack.ga_growsize;
5338
5339 // Don't make it bigger though.
5340 if (exestack.ga_len + n < exestack.ga_maxlen)
5341 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005342 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005343 pp = vim_realloc(exestack.ga_data, new_len);
5344 if (pp == NULL)
5345 return FAIL;
5346 exestack.ga_maxlen = exestack.ga_len + n;
5347 exestack.ga_data = pp;
5348 }
5349 }
5350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // We advance by two because we add one for items referenced through
5352 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005353 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 /*
5356 * 1. Go through all accessible variables and mark all lists and dicts
5357 * with copyID.
5358 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // Don't free variables in the previous_funccal list unless they are only
5361 // referenced through previous_funccal. This must be first, because if
5362 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005363 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005366 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005368 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005369 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5371 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005374 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005375 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5376 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005377 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005378 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005379 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005380 abort = abort || set_ref_in_item(
5381 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005382#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005383 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005384 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5385 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005386 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005387 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005388 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5389 NULL, NULL);
5390#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005391
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005393 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5395 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005397 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005403 abort = abort || set_ref_in_functions(copyID);
5404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005407
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005408 // funcstacks keep variables for closures
5409 abort = abort || set_ref_in_funcstacks(copyID);
5410
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005411 // loopvars keep variables for loop blocks
5412 abort = abort || set_ref_in_loopvars(copyID);
5413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005415 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005416
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005417 // callbacks in buffers
5418 abort = abort || set_ref_in_buffers(copyID);
5419
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005420 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5421 abort = abort || set_ref_in_insexpand_funcs(copyID);
5422
5423 // 'operatorfunc' callback
5424 abort = abort || set_ref_in_opfunc(copyID);
5425
5426 // 'tagfunc' callback
5427 abort = abort || set_ref_in_tagfunc(copyID);
5428
5429 // 'imactivatefunc' and 'imstatusfunc' callbacks
5430 abort = abort || set_ref_in_im_funcs(copyID);
5431
Bram Moolenaar1dced572012-04-05 16:54:08 +02005432#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005434#endif
5435
Bram Moolenaardb913952012-06-29 12:54:53 +02005436#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005437 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005438#endif
5439
5440#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005441 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005442#endif
5443
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005444#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005445 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005446 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005447#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005448#ifdef FEAT_NETBEANS_INTG
5449 abort = abort || set_ref_in_nb_channel(copyID);
5450#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005451
Bram Moolenaare3188e22016-05-31 21:13:04 +02005452#ifdef FEAT_TIMERS
5453 abort = abort || set_ref_in_timer(copyID);
5454#endif
5455
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005456#ifdef FEAT_QUICKFIX
5457 abort = abort || set_ref_in_quickfix(copyID);
5458#endif
5459
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005460#ifdef FEAT_TERMINAL
5461 abort = abort || set_ref_in_term(copyID);
5462#endif
5463
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005464#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005465 abort = abort || set_ref_in_popups(copyID);
5466#endif
5467
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005468 abort = abort || set_ref_in_classes(copyID);
5469
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005470 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005471 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 /*
5473 * 2. Free lists and dictionaries that are not referenced.
5474 */
5475 did_free = free_unref_items(copyID);
5476
5477 /*
5478 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005480 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005481 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005482 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005483 else if (p_verbose > 0)
5484 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005485 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005487
5488 return did_free;
5489}
5490
5491/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005492 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005493 */
5494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005496{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005497 int did_free = FALSE;
5498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // Let all "free" functions know that we are here. This means no
5500 // dictionaries, lists, channels or jobs are to be freed, because we will
5501 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005502 in_free_unref_items = TRUE;
5503
5504 /*
5505 * PASS 1: free the contents of the items. We don't free the items
5506 * themselves yet, so that it is possible to decrement refcount counters
5507 */
5508
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005509 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005510 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005511
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005512 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005513 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005514
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005515 // Go through the list of objects and free items without this copyID.
5516 did_free |= object_free_nonref(copyID);
5517
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005518 // Go through the list of classes and free items without this copyID.
5519 did_free |= class_free_nonref(copyID);
5520
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005521#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005522 // Go through the list of jobs and free items without the copyID. This
5523 // must happen before doing channels, because jobs refer to channels, but
5524 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005525 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005528 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5529#endif
5530
5531 /*
5532 * PASS 2: free the items themselves.
5533 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005534 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005535 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005536
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // Go through the list of jobs and free items without the copyID. This
5539 // must happen before doing channels, because jobs refer to channels, but
5540 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005541 free_unused_jobs(copyID, COPYID_MASK);
5542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005544 free_unused_channels(copyID, COPYID_MASK);
5545#endif
5546
5547 in_free_unref_items = FALSE;
5548
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005549 return did_free;
5550}
5551
5552/*
5553 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554 * "list_stack" is used to add lists to be marked. Can be NULL.
5555 *
5556 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005557 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005558 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005559set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005560{
5561 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005562 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005563 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 hashtab_T *cur_ht;
5565 ht_stack_T *ht_stack = NULL;
5566 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005567
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005568 cur_ht = ht;
5569 for (;;)
5570 {
5571 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // Mark each item in the hashtab. If the item contains a hashtab
5574 // it is added to ht_stack, if it contains a list it is added to
5575 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005577 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005578 if (!HASHITEM_EMPTY(hi))
5579 {
5580 --todo;
5581 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5582 &ht_stack, list_stack);
5583 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005584 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005585
5586 if (ht_stack == NULL)
5587 break;
5588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005589 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005590 cur_ht = ht_stack->ht;
5591 tempitem = ht_stack;
5592 ht_stack = ht_stack->prev;
5593 free(tempitem);
5594 }
5595
5596 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005597}
5598
Dominique Pelle748b3082022-01-08 12:41:16 +00005599#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5600 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005601/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005602 * Mark a dict and its items with "copyID".
5603 * Returns TRUE if setting references failed somehow.
5604 */
5605 int
5606set_ref_in_dict(dict_T *d, int copyID)
5607{
5608 if (d != NULL && d->dv_copyID != copyID)
5609 {
5610 d->dv_copyID = copyID;
5611 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5612 }
5613 return FALSE;
5614}
Dominique Pelle748b3082022-01-08 12:41:16 +00005615#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005616
5617/*
5618 * Mark a list and its items with "copyID".
5619 * Returns TRUE if setting references failed somehow.
5620 */
5621 int
5622set_ref_in_list(list_T *ll, int copyID)
5623{
5624 if (ll != NULL && ll->lv_copyID != copyID)
5625 {
5626 ll->lv_copyID = copyID;
5627 return set_ref_in_list_items(ll, copyID, NULL);
5628 }
5629 return FALSE;
5630}
5631
5632/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005633 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005634 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5635 *
5636 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005637 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005638 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005639set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005640{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 listitem_T *li;
5642 int abort = FALSE;
5643 list_T *cur_l;
5644 list_stack_T *list_stack = NULL;
5645 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005646
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 cur_l = l;
5648 for (;;)
5649 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005650 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005651 // Mark each item in the list. If the item contains a hashtab
5652 // it is added to ht_stack, if it contains a list it is added to
5653 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005654 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5655 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5656 ht_stack, &list_stack);
5657 if (list_stack == NULL)
5658 break;
5659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 cur_l = list_stack->list;
5662 tempitem = list_stack;
5663 list_stack = list_stack->prev;
5664 free(tempitem);
5665 }
5666
5667 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005668}
5669
5670/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005671 * Mark the partial in callback 'cb' with "copyID".
5672 */
5673 int
5674set_ref_in_callback(callback_T *cb, int copyID)
5675{
5676 typval_T tv;
5677
5678 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5679 return FALSE;
5680
5681 tv.v_type = VAR_PARTIAL;
5682 tv.vval.v_partial = cb->cb_partial;
5683 return set_ref_in_item(&tv, copyID, NULL, NULL);
5684}
5685
5686/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005687 * Mark the dict "dd" with "copyID".
5688 * Also see set_ref_in_item().
5689 */
5690 static int
5691set_ref_in_item_dict(
5692 dict_T *dd,
5693 int copyID,
5694 ht_stack_T **ht_stack,
5695 list_stack_T **list_stack)
5696{
5697 if (dd == NULL || dd->dv_copyID == copyID)
5698 return FALSE;
5699
5700 // Didn't see this dict yet.
5701 dd->dv_copyID = copyID;
5702 if (ht_stack == NULL)
5703 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5704
5705 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5706 if (newitem == NULL)
5707 return TRUE;
5708
5709 newitem->ht = &dd->dv_hashtab;
5710 newitem->prev = *ht_stack;
5711 *ht_stack = newitem;
5712
5713 return FALSE;
5714}
5715
5716/*
5717 * Mark the list "ll" with "copyID".
5718 * Also see set_ref_in_item().
5719 */
5720 static int
5721set_ref_in_item_list(
5722 list_T *ll,
5723 int copyID,
5724 ht_stack_T **ht_stack,
5725 list_stack_T **list_stack)
5726{
5727 if (ll == NULL || ll->lv_copyID == copyID)
5728 return FALSE;
5729
5730 // Didn't see this list yet.
5731 ll->lv_copyID = copyID;
5732 if (list_stack == NULL)
5733 return set_ref_in_list_items(ll, copyID, ht_stack);
5734
5735 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5736 if (newitem == NULL)
5737 return TRUE;
5738
5739 newitem->list = ll;
5740 newitem->prev = *list_stack;
5741 *list_stack = newitem;
5742
5743 return FALSE;
5744}
5745
5746/*
5747 * Mark the partial "pt" with "copyID".
5748 * Also see set_ref_in_item().
5749 */
5750 static int
5751set_ref_in_item_partial(
5752 partial_T *pt,
5753 int copyID,
5754 ht_stack_T **ht_stack,
5755 list_stack_T **list_stack)
5756{
5757 if (pt == NULL || pt->pt_copyID == copyID)
5758 return FALSE;
5759
5760 // Didn't see this partial yet.
5761 pt->pt_copyID = copyID;
5762
5763 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5764
5765 if (pt->pt_dict != NULL)
5766 {
5767 typval_T dtv;
5768
5769 dtv.v_type = VAR_DICT;
5770 dtv.vval.v_dict = pt->pt_dict;
5771 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5772 }
5773
5774 for (int i = 0; i < pt->pt_argc; ++i)
5775 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5776 ht_stack, list_stack);
5777 // pt_funcstack is handled in set_ref_in_funcstacks()
5778 // pt_loopvars is handled in set_ref_in_loopvars()
5779
5780 return abort;
5781}
5782
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005783#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005784/*
5785 * Mark the job "pt" with "copyID".
5786 * Also see set_ref_in_item().
5787 */
5788 static int
5789set_ref_in_item_job(
5790 job_T *job,
5791 int copyID,
5792 ht_stack_T **ht_stack,
5793 list_stack_T **list_stack)
5794{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005795 typval_T dtv;
5796
5797 if (job == NULL || job->jv_copyID == copyID)
5798 return FALSE;
5799
5800 job->jv_copyID = copyID;
5801 if (job->jv_channel != NULL)
5802 {
5803 dtv.v_type = VAR_CHANNEL;
5804 dtv.vval.v_channel = job->jv_channel;
5805 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5806 }
5807 if (job->jv_exit_cb.cb_partial != NULL)
5808 {
5809 dtv.v_type = VAR_PARTIAL;
5810 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5811 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5812 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005813
5814 return FALSE;
5815}
5816
5817/*
5818 * Mark the channel "ch" with "copyID".
5819 * Also see set_ref_in_item().
5820 */
5821 static int
5822set_ref_in_item_channel(
5823 channel_T *ch,
5824 int copyID,
5825 ht_stack_T **ht_stack,
5826 list_stack_T **list_stack)
5827{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005828 typval_T dtv;
5829
5830 if (ch == NULL || ch->ch_copyID == copyID)
5831 return FALSE;
5832
5833 ch->ch_copyID = copyID;
5834 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5835 {
5836 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5837 jq != NULL; jq = jq->jq_next)
5838 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5839 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5840 cq = cq->cq_next)
5841 if (cq->cq_callback.cb_partial != NULL)
5842 {
5843 dtv.v_type = VAR_PARTIAL;
5844 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5845 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5846 }
5847 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5848 {
5849 dtv.v_type = VAR_PARTIAL;
5850 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5851 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5852 }
5853 }
5854 if (ch->ch_callback.cb_partial != NULL)
5855 {
5856 dtv.v_type = VAR_PARTIAL;
5857 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5858 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5859 }
5860 if (ch->ch_close_cb.cb_partial != NULL)
5861 {
5862 dtv.v_type = VAR_PARTIAL;
5863 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5864 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5865 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005866
5867 return FALSE;
5868}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005869#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005870
5871/*
5872 * Mark the class "cl" with "copyID".
5873 * Also see set_ref_in_item().
5874 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005875 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005876set_ref_in_item_class(
5877 class_T *cl,
5878 int copyID,
5879 ht_stack_T **ht_stack,
5880 list_stack_T **list_stack)
5881{
5882 int abort = FALSE;
5883
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005884 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005885 return FALSE;
5886
5887 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005888 if (cl->class_members_tv != NULL)
5889 {
5890 // The "class_members_tv" table is allocated only for regular classes
5891 // and not for interfaces.
5892 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5893 abort = abort || set_ref_in_item(
5894 &cl->class_members_tv[i],
5895 copyID, ht_stack, list_stack);
5896 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005897
5898 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5899 abort = abort || set_ref_in_func(NULL,
5900 cl->class_class_functions[i], copyID);
5901
5902 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5903 abort = abort || set_ref_in_func(NULL,
5904 cl->class_obj_methods[i], copyID);
5905
5906 return abort;
5907}
5908
5909/*
5910 * Mark the object "cl" with "copyID".
5911 * Also see set_ref_in_item().
5912 */
5913 static int
5914set_ref_in_item_object(
5915 object_T *obj,
5916 int copyID,
5917 ht_stack_T **ht_stack,
5918 list_stack_T **list_stack)
5919{
5920 int abort = FALSE;
5921
5922 if (obj == NULL || obj->obj_copyID == copyID)
5923 return FALSE;
5924
5925 obj->obj_copyID = copyID;
5926
5927 // The typval_T array is right after the object_T.
5928 typval_T *mtv = (typval_T *)(obj + 1);
5929 for (int i = 0; !abort
5930 && i < obj->obj_class->class_obj_member_count; ++i)
5931 abort = abort || set_ref_in_item(mtv + i, copyID,
5932 ht_stack, list_stack);
5933
5934 return abort;
5935}
5936
5937/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005938 * Mark all lists, dicts and other container types referenced through typval
5939 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005940 * "list_stack" is used to add lists to be marked. Can be NULL.
5941 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5942 *
5943 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005944 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946set_ref_in_item(
5947 typval_T *tv,
5948 int copyID,
5949 ht_stack_T **ht_stack,
5950 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005952 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005954 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005955 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005956 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005957 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5958 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005959
5960 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005961 return set_ref_in_item_list(tv->vval.v_list, copyID,
5962 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005963
5964 case VAR_FUNC:
5965 {
5966 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5967 break;
5968 }
5969
5970 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005971 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5972 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005973
5974 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005975#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005976 return set_ref_in_item_job(tv->vval.v_job, copyID,
5977 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005978#else
5979 break;
5980#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005981
5982 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005983#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005984 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005985 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005986#else
5987 break;
5988#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005989
5990 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005991 return set_ref_in_item_class(tv->vval.v_class, copyID,
5992 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005993
5994 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005995 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005996 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005997
5998 case VAR_UNKNOWN:
5999 case VAR_ANY:
6000 case VAR_VOID:
6001 case VAR_BOOL:
6002 case VAR_SPECIAL:
6003 case VAR_NUMBER:
6004 case VAR_FLOAT:
6005 case VAR_STRING:
6006 case VAR_BLOB:
6007 case VAR_INSTR:
6008 // Types that do not contain any other item
6009 break;
6010 }
6011
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006012 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013}
6014
Bram Moolenaar8c711452005-01-14 21:53:12 +00006015/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 * Return a string with the string representation of a variable.
6017 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006020 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006021 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006022 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006023 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6024 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006025 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006027 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006028echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006029 typval_T *tv,
6030 char_u **tofree,
6031 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006032 int copyID,
6033 int echo_style,
6034 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006035 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006037 static int recurse = 0;
6038 char_u *r = NULL;
6039
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006042 if (!did_echo_string_emsg)
6043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006044 // Only give this message once for a recursive call to avoid
6045 // flooding the user with errors. And stop iterating over lists
6046 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006047 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006048 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006049 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006051 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006052 }
6053 ++recurse;
6054
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 switch (tv->v_type)
6056 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006057 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006058 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006059 {
6060 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006061 r = tv->vval.v_string;
6062 if (r == NULL)
6063 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006064 }
6065 else
6066 {
6067 *tofree = string_quote(tv->vval.v_string, FALSE);
6068 r = *tofree;
6069 }
6070 break;
6071
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006073 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006074 char_u buf[MAX_FUNC_NAME_LEN];
6075
6076 if (echo_style)
6077 {
LemonBoya5d35902022-04-29 21:15:02 +01006078 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6079 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006080 buf, MAX_FUNC_NAME_LEN);
6081 if (r == buf)
6082 {
6083 r = vim_strsave(buf);
6084 *tofree = r;
6085 }
6086 else
6087 *tofree = NULL;
6088 }
6089 else
6090 {
6091 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6092 : make_ufunc_name_readable(
6093 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6094 TRUE);
6095 r = *tofree;
6096 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006099
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006100 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006101 {
6102 partial_T *pt = tv->vval.v_partial;
6103 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006104 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006105 garray_T ga;
6106 int i;
6107 char_u *tf;
6108
6109 ga_init2(&ga, 1, 100);
6110 ga_concat(&ga, (char_u *)"function(");
6111 if (fname != NULL)
6112 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006113 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006114 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006115 && vim_isupper(fname[1]))
6116 {
6117 ga_concat(&ga, (char_u *)"'g:");
6118 ga_concat(&ga, fname + 1);
6119 }
6120 else
6121 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006122 vim_free(fname);
6123 }
6124 if (pt != NULL && pt->pt_argc > 0)
6125 {
6126 ga_concat(&ga, (char_u *)", [");
6127 for (i = 0; i < pt->pt_argc; ++i)
6128 {
6129 if (i > 0)
6130 ga_concat(&ga, (char_u *)", ");
6131 ga_concat(&ga,
6132 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6133 vim_free(tf);
6134 }
6135 ga_concat(&ga, (char_u *)"]");
6136 }
6137 if (pt != NULL && pt->pt_dict != NULL)
6138 {
6139 typval_T dtv;
6140
6141 ga_concat(&ga, (char_u *)", ");
6142 dtv.v_type = VAR_DICT;
6143 dtv.vval.v_dict = pt->pt_dict;
6144 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6145 vim_free(tf);
6146 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006147 // terminate with ')' and a NUL
6148 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006149
6150 *tofree = ga.ga_data;
6151 r = *tofree;
6152 break;
6153 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006154
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006155 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006156 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006157 break;
6158
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006160 if (tv->vval.v_list == NULL)
6161 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006162 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006163 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006164 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006165 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006166 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6167 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006168 {
6169 *tofree = NULL;
6170 r = (char_u *)"[...]";
6171 }
6172 else
6173 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006174 int old_copyID = tv->vval.v_list->lv_copyID;
6175
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006176 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006177 *tofree = list2string(tv, copyID, restore_copyID);
6178 if (restore_copyID)
6179 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006180 r = *tofree;
6181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006183
Bram Moolenaar8c711452005-01-14 21:53:12 +00006184 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006185 if (tv->vval.v_dict == NULL)
6186 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006187 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006188 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006189 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006190 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006191 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6192 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006193 {
6194 *tofree = NULL;
6195 r = (char_u *)"{...}";
6196 }
6197 else
6198 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006199 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006200
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006202 *tofree = dict2string(tv, copyID, restore_copyID);
6203 if (restore_copyID)
6204 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006205 r = *tofree;
6206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006210 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006211 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006212 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006213 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006214 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006215 break;
6216
Bram Moolenaar835dc632016-02-07 14:27:38 +01006217 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006218 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006219#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006220 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006221 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6222 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006223 if (composite_val)
6224 {
6225 *tofree = string_quote(r, FALSE);
6226 r = *tofree;
6227 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006228#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006230
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006231 case VAR_INSTR:
6232 *tofree = NULL;
6233 r = (char_u *)"instructions";
6234 break;
6235
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006236 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006237 {
6238 class_T *cl = tv->vval.v_class;
6239 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6240 r = *tofree = alloc(len);
6241 vim_snprintf((char *)r, len, "class %s",
6242 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6243 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006244 break;
6245
6246 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006247 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006248 garray_T ga;
6249 ga_init2(&ga, 1, 50);
6250 ga_concat(&ga, (char_u *)"object of ");
6251 object_T *obj = tv->vval.v_object;
6252 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6253 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6254 : cl->class_name);
6255 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006256 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006257 ga_concat(&ga, (char_u *)" {");
6258 for (int i = 0; i < cl->class_obj_member_count; ++i)
6259 {
6260 if (i > 0)
6261 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006262 ocmember_T *m = &cl->class_obj_members[i];
6263 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006264 ga_concat(&ga, (char_u *)": ");
6265 char_u *tf = NULL;
6266 ga_concat(&ga, echo_string_core(
6267 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006268 &tf, numbuf, copyID, echo_style,
6269 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006270 vim_free(tf);
6271 }
6272 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006273 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006274
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006275 *tofree = r = ga.ga_data;
6276 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006277 break;
6278
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006279 case VAR_FLOAT:
6280 *tofree = NULL;
6281 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6282 r = numbuf;
6283 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006284
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006285 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006286 case VAR_SPECIAL:
6287 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006288 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006289 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006290 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006291
Bram Moolenaar8502c702014-06-17 12:51:16 +02006292 if (--recurse == 0)
6293 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006295}
6296
6297/*
6298 * Return a string with the string representation of a variable.
6299 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6300 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006301 * Does not put quotes around strings, as ":echo" displays values.
6302 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6303 * May return NULL.
6304 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006305 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006306echo_string(
6307 typval_T *tv,
6308 char_u **tofree,
6309 char_u *numbuf,
6310 int copyID)
6311{
6312 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6313}
6314
6315/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006316 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6317 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006318 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006319 */
6320 int
6321buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6322{
6323 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006324 char_u *t;
6325 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006326
6327 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6328 return -1;
6329
6330 if (lnum > buf->b_ml.ml_line_count)
6331 lnum = buf->b_ml.ml_line_count;
6332
6333 str = ml_get_buf(buf, lnum, FALSE);
6334 if (str == NULL)
6335 return -1;
6336
6337 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006338 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006339
Bram Moolenaar91458462021-01-13 20:08:38 +01006340 // count the number of characters
6341 t = str;
6342 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6343 t += mb_ptr2len(t);
6344
6345 // In insert mode, when the cursor is at the end of a non-empty line,
6346 // byteidx points to the NUL character immediately past the end of the
6347 // string. In this case, add one to the character count.
6348 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6349 count++;
6350
6351 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006352}
6353
6354/*
6355 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006356 * byte index. Works only for loaded buffers. Returns -1 on failure.
6357 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006358 */
6359 int
6360buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6361{
6362 char_u *str;
6363 char_u *t;
6364
6365 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6366 return -1;
6367
6368 if (lnum > buf->b_ml.ml_line_count)
6369 lnum = buf->b_ml.ml_line_count;
6370
6371 str = ml_get_buf(buf, lnum, FALSE);
6372 if (str == NULL)
6373 return -1;
6374
6375 // Convert the character offset to a byte offset
6376 t = str;
6377 while (*t != NUL && --charidx > 0)
6378 t += mb_ptr2len(t);
6379
Bram Moolenaar91458462021-01-13 20:08:38 +01006380 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006381}
6382
6383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006385 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388var2fpos(
6389 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006390 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006391 int *fnum, // set to fnum for '0, 'A, etc.
6392 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006394 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006396 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006398 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006399 if (varp->v_type == VAR_LIST)
6400 {
6401 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006402 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006403 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006404 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006405
6406 l = varp->vval.v_list;
6407 if (l == NULL)
6408 return NULL;
6409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006410 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006411 pos.lnum = list_find_nr(l, 0L, &error);
6412 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006413 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006414 if (charcol)
6415 len = (long)mb_charlen(ml_get(pos.lnum));
6416 else
6417 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006418
Bram Moolenaarec65d772020-08-20 22:29:12 +02006419 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006421 li = list_find(l, 1L);
6422 if (li != NULL && li->li_tv.v_type == VAR_STRING
6423 && li->li_tv.vval.v_string != NULL
6424 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006425 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006426 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006427 }
6428 else
6429 {
6430 pos.col = list_find_nr(l, 1L, &error);
6431 if (error)
6432 return NULL;
6433 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006435 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006436 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006438 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006440 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006441 pos.coladd = list_find_nr(l, 2L, &error);
6442 if (error)
6443 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006444
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006445 return &pos;
6446 }
6447
Bram Moolenaarc5809432021-03-27 21:23:30 +01006448 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6449 return NULL;
6450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006451 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006452 if (name == NULL)
6453 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006454
6455 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006456 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006457 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006458 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006459 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006460 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006461 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006462 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006463 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006464 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006465 pos = VIsual;
6466 else
6467 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006468 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006469 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006470 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006472 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006473 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6475 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006476 pos = *pp;
6477 }
6478 if (pos.lnum != 0)
6479 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006480 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006481 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6482 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006484
Bram Moolenaara5525202006-03-02 22:52:09 +00006485 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006486
Bram Moolenaar477933c2007-07-17 14:32:23 +00006487 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006488 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006489 // the "w_valid" flags are not reset when moving the cursor, but they
6490 // do matter for update_topline() and validate_botline().
6491 check_cursor_moved(curwin);
6492
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006493 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006494 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006495 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006496 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // In silent Ex mode topline is zero, but that's not a valid line
6498 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006499 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006500 return &pos;
6501 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006503 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006504 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006505 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006506 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006507 return &pos;
6508 }
6509 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006510 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006512 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
6514 pos.lnum = curbuf->b_ml.ml_line_count;
6515 pos.col = 0;
6516 }
6517 else
6518 {
6519 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006520 if (charcol)
6521 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6522 else
6523 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525 return &pos;
6526 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006527 if (in_vim9script())
6528 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 return NULL;
6530}
6531
6532/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006533 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006534 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006535 * Note that the column is passed on as-is, the caller may want to decrement
6536 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006537 * If "charcol" is TRUE use the column as the character index instead of the
6538 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006539 * Return FAIL when conversion is not possible, doesn't check the position for
6540 * validity.
6541 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006543list2fpos(
6544 typval_T *arg,
6545 pos_T *posp,
6546 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006547 colnr_T *curswantp,
6548 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006549{
6550 list_T *l = arg->vval.v_list;
6551 long i = 0;
6552 long n;
6553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6555 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006556 if (arg->v_type != VAR_LIST
6557 || l == NULL
6558 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006559 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006560 return FAIL;
6561
6562 if (fnump != NULL)
6563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006565 if (n < 0)
6566 return FAIL;
6567 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006569 *fnump = n;
6570 }
6571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006573 if (n < 0)
6574 return FAIL;
6575 posp->lnum = n;
6576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006578 if (n < 0)
6579 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006580 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006581 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006582 if (charcol)
6583 {
6584 buf_T *buf;
6585
6586 // Get the text for the specified line in a loaded buffer
6587 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6588 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6589 return FAIL;
6590
Bram Moolenaar79f23442022-10-10 12:42:57 +01006591 n = buf_charidx_to_byteidx(buf,
6592 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006593 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006594 posp->col = n;
6595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006596 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006597 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006598 posp->coladd = 0;
6599 else
6600 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006601
Bram Moolenaar493c1782014-05-28 14:34:46 +02006602 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006603 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006604
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006605 return OK;
6606}
6607
6608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 * Get the length of an environment variable name.
6610 * Advance "arg" to the first character after the name.
6611 * Return 0 for error.
6612 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 char_u *p;
6617 int len;
6618
6619 for (p = *arg; vim_isIDc(*p); ++p)
6620 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006621 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 return 0;
6623
6624 len = (int)(p - *arg);
6625 *arg = p;
6626 return len;
6627}
6628
6629/*
6630 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006631 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 * Return 0 if something is wrong.
6633 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637 char_u *p;
6638 int len;
6639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006640 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006642 {
6643 if (*p == ':')
6644 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // "s:" is start of "s:var", but "n:" is not and can be used in
6646 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006647 len = (int)(p - *arg);
6648 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6649 || len > 1)
6650 break;
6651 }
6652 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006653 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 return 0;
6655
6656 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006657 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 return len;
6660}
6661
6662/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006663 * Get the length of the name of a variable or function.
6664 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006666 * Return -1 if curly braces expansion failed.
6667 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 * If the name contains 'magic' {}'s, expand them and return the
6669 * expanded name in an allocated string via 'alias' - caller must free.
6670 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672get_name_len(
6673 char_u **arg,
6674 char_u **alias,
6675 int evaluate,
6676 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 char_u *p;
6680 char_u *expr_start;
6681 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006683 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6686 && (*arg)[2] == (int)KE_SNR)
6687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006688 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 *arg += 3;
6690 return get_id_len(arg) + 3;
6691 }
6692 len = eval_fname_script(*arg);
6693 if (len > 0)
6694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 *arg += len;
6697 }
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006700 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006702 p = find_name_end(*arg, &expr_start, &expr_end,
6703 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 if (expr_start != NULL)
6705 {
6706 char_u *temp_string;
6707
6708 if (!evaluate)
6709 {
6710 len += (int)(p - *arg);
6711 *arg = skipwhite(p);
6712 return len;
6713 }
6714
6715 /*
6716 * Include any <SID> etc in the expanded string:
6717 * Thus the -len here.
6718 */
6719 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6720 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006721 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 *alias = temp_string;
6723 *arg = skipwhite(p);
6724 return (int)STRLEN(temp_string);
6725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006728 // Only give an error when there is something, otherwise it will be
6729 // reported at a higher level.
6730 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006731 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 return len;
6734}
6735
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736/*
6737 * Find the end of a variable or function name, taking care of magic braces.
6738 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6739 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006740 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006741 * Return a pointer to just after the name. Equal to "arg" if there is no
6742 * valid name.
6743 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006745find_name_end(
6746 char_u *arg,
6747 char_u **expr_start,
6748 char_u **expr_end,
6749 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006751 int mb_nest = 0;
6752 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006754 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006755 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006757 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006759 *expr_start = NULL;
6760 *expr_end = NULL;
6761 }
6762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006763 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006764 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006765 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006766 return arg;
6767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006768 for (p = arg; *p != NUL
6769 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006770 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006771 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006772 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006774 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006776 if (*p == '\'')
6777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006778 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006779 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006780 ;
6781 if (*p == NUL)
6782 break;
6783 }
6784 else if (*p == '"')
6785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006787 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006788 if (*p == '\\' && p[1] != NUL)
6789 ++p;
6790 if (*p == NUL)
6791 break;
6792 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006793 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006795 // "s:" is start of "s:var", but "n:" is not and can be used in
6796 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006797 len = (int)(p - arg);
6798 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006799 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006800 break;
6801 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 if (*p == '[')
6806 ++br_nest;
6807 else if (*p == ']')
6808 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006810
zeertzjqa93d9cd2023-05-02 16:25:47 +01006811 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 if (*p == '{')
6814 {
6815 mb_nest++;
6816 if (expr_start != NULL && *expr_start == NULL)
6817 *expr_start = p;
6818 }
6819 else if (*p == '}')
6820 {
6821 mb_nest--;
6822 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6823 *expr_end = p;
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827
6828 return p;
6829}
6830
6831/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006832 * Expands out the 'magic' {}'s in a variable/function name.
6833 * Note that this can call itself recursively, to deal with
6834 * constructs like foo{bar}{baz}{bam}
6835 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6836 * "in_start" ^
6837 * "expr_start" ^
6838 * "expr_end" ^
6839 * "in_end" ^
6840 *
6841 * Returns a new allocated string, which the caller must free.
6842 * Returns NULL for failure.
6843 */
6844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845make_expanded_name(
6846 char_u *in_start,
6847 char_u *expr_start,
6848 char_u *expr_end,
6849 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006850{
6851 char_u c1;
6852 char_u *retval = NULL;
6853 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006854
6855 if (expr_end == NULL || in_end == NULL)
6856 return NULL;
6857 *expr_start = NUL;
6858 *expr_end = NUL;
6859 c1 = *in_end;
6860 *in_end = NUL;
6861
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006862 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006863 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006864 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006865 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6866 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006867 if (retval != NULL)
6868 {
6869 STRCPY(retval, in_start);
6870 STRCAT(retval, temp_result);
6871 STRCAT(retval, expr_end + 1);
6872 }
6873 }
6874 vim_free(temp_result);
6875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006876 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006877 *expr_start = '{';
6878 *expr_end = '}';
6879
6880 if (retval != NULL)
6881 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006882 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006883 if (expr_start != NULL)
6884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006885 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006886 temp_result = make_expanded_name(retval, expr_start,
6887 expr_end, temp_result);
6888 vim_free(retval);
6889 retval = temp_result;
6890 }
6891 }
6892
6893 return retval;
6894}
6895
6896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006903 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006904}
6905
6906/*
6907 * Return TRUE if character "c" can be used as the first character in a
6908 * variable or function name (excluding '{' and '}').
6909 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006912{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006913 return ASCII_ISALPHA(c) || c == '_';
6914}
6915
6916/*
6917 * Return TRUE if character "c" can be used as the first character of a
6918 * dictionary key.
6919 */
6920 int
6921eval_isdictc(int c)
6922{
6923 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
6926/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006927 * Handle:
6928 * - expr[expr], expr[expr:expr] subscript
6929 * - ".name" lookup
6930 * - function call with Funcref variable: func(expr)
6931 * - method call: var->method()
6932 *
6933 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006934 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006935 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006937handle_subscript(
6938 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006939 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006940 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006941 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006942 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006943{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006944 int evaluate = evalarg != NULL
6945 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006946 int ret = OK;
6947 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006948 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006949 int getnext;
6950 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006951
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006952 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006953 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006954 // When at the end of the line and ".name" or "->{" or "->X" follows in
6955 // the next line then consume the line break.
6956 p = eval_next_non_blank(*arg, evalarg, &getnext);
6957 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006958 && ((*p == '.'
6959 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6960 || rettv->v_type == VAR_CLASS
6961 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006962 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6963 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6964 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006965 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006966 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006967 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006968 check_white = FALSE;
6969 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006970
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006971 if (rettv->v_type == VAR_ANY)
6972 {
6973 char_u *exp_name;
6974 int cc;
6975 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006976 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006977 type_T *type;
6978
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006979 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006980 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006981 if (**arg != '.')
6982 {
6983 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006984 semsg(_(e_expected_dot_after_name_str),
6985 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006986 ret = FAIL;
6987 break;
6988 }
6989 ++*arg;
6990 if (IS_WHITE_OR_NUL(**arg))
6991 {
6992 if (verbose)
6993 emsg(_(e_no_white_space_allowed_after_dot));
6994 ret = FAIL;
6995 break;
6996 }
6997
6998 // isolate the name
6999 exp_name = *arg;
7000 while (eval_isnamec(**arg))
7001 ++*arg;
7002 cc = **arg;
7003 **arg = NUL;
7004
7005 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007006 evalarg == NULL ? NULL : evalarg->eval_cctx,
7007 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007008 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007009
7010 if (idx < 0 && ufunc == NULL)
7011 {
7012 ret = FAIL;
7013 break;
7014 }
7015 if (idx >= 0)
7016 {
7017 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7018 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7019
7020 copy_tv(sv->sv_tv, rettv);
7021 }
7022 else
7023 {
7024 rettv->v_type = VAR_FUNC;
7025 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7026 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007027 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007028 }
7029
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007030 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7031 || rettv->v_type == VAR_PARTIAL))
7032 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007033 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007034 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7035 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007036
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007037 // Stop the expression evaluation when immediately aborting on
7038 // error, or when an interrupt occurred or an exception was thrown
7039 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007040 if (aborting())
7041 {
7042 if (ret == OK)
7043 clear_tv(rettv);
7044 ret = FAIL;
7045 }
7046 dict_unref(selfdict);
7047 selfdict = NULL;
7048 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007049 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007050 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007051 if (in_vim9script())
7052 *arg = skipwhite(p + 2);
7053 else
7054 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007055 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007056 {
zeertzjqc481ad32023-03-11 16:18:51 +00007057 emsg(_(e_no_white_space_allowed_before_parenthesis));
7058 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007059 }
zeertzjqc481ad32023-03-11 16:18:51 +00007060 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7061 // expr->{lambda}() or expr->(lambda)()
7062 ret = eval_lambda(arg, rettv, evalarg, verbose);
7063 else
7064 // expr->name()
7065 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007066 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007067 // "." is ".name" lookup when we found a dict or when evaluating and
7068 // scriptversion is at least 2, where string concatenation is "..".
7069 else if (**arg == '['
7070 || (**arg == '.' && (rettv->v_type == VAR_DICT
7071 || (!evaluate
7072 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007073 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007074 {
7075 dict_unref(selfdict);
7076 if (rettv->v_type == VAR_DICT)
7077 {
7078 selfdict = rettv->vval.v_dict;
7079 if (selfdict != NULL)
7080 ++selfdict->dv_refcount;
7081 }
7082 else
7083 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007084 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007085 {
7086 clear_tv(rettv);
7087 ret = FAIL;
7088 }
7089 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007090 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7091 || rettv->v_type == VAR_OBJECT))
7092 {
7093 // class member: SomeClass.varname
7094 // class method: SomeClass.SomeMethod()
7095 // class constructor: SomeClass.new()
7096 // object member: someObject.varname
7097 // object method: someObject.SomeMethod()
7098 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7099 {
7100 clear_tv(rettv);
7101 ret = FAIL;
7102 }
7103 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007104 else
7105 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007106 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007108 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7109 // Don't do this when "Func" is already a partial that was bound
7110 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007111 if (selfdict != NULL
7112 && (rettv->v_type == VAR_FUNC
7113 || (rettv->v_type == VAR_PARTIAL
7114 && (rettv->vval.v_partial->pt_auto
7115 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007116 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007117
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007118 dict_unref(selfdict);
7119 return ret;
7120}
7121
7122/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 * Make a copy of an item.
7124 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007125 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007126 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7127 * reference to an already copied list/dict can be used.
7128 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007130 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007131item_copy(
7132 typval_T *from,
7133 typval_T *to,
7134 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007135 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007136 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137{
7138 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007139 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007143 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 }
7146 ++recurse;
7147
7148 switch (from->v_type)
7149 {
7150 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007151 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 case VAR_STRING:
7153 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007154 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007155 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007156 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007157 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007158 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007159 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007160 case VAR_CLASS:
7161 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162 copy_tv(from, to);
7163 break;
7164 case VAR_LIST:
7165 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007166 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007167 if (from->vval.v_list == NULL)
7168 to->vval.v_list = NULL;
7169 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007171 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 to->vval.v_list = from->vval.v_list->lv_copylist;
7173 ++to->vval.v_list->lv_refcount;
7174 }
7175 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007176 to->vval.v_list = list_copy(from->vval.v_list,
7177 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 if (to->vval.v_list == NULL)
7179 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007181 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007183 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 case VAR_DICT:
7185 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007186 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 if (from->vval.v_dict == NULL)
7188 to->vval.v_dict = NULL;
7189 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007191 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7193 ++to->vval.v_dict->dv_refcount;
7194 }
7195 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007196 to->vval.v_dict = dict_copy(from->vval.v_dict,
7197 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 if (to->vval.v_dict == NULL)
7199 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007201 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007202 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007204 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 }
7207 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209}
7210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 void
7212echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7213{
7214 char_u *tofree;
7215 char_u numbuf[NUMBUFLEN];
7216 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7217
7218 if (*atstart)
7219 {
7220 *atstart = FALSE;
7221 // Call msg_start() after eval1(), evaluating the expression
7222 // may cause a message to appear.
7223 if (with_space)
7224 {
7225 // Mark the saved text as finishing the line, so that what
7226 // follows is displayed on a new line when scrolling back
7227 // at the more prompt.
7228 msg_sb_eol();
7229 msg_start();
7230 }
7231 }
7232 else if (with_space)
7233 msg_puts_attr(" ", echo_attr);
7234
7235 if (p != NULL)
7236 for ( ; *p != NUL && !got_int; ++p)
7237 {
7238 if (*p == '\n' || *p == '\r' || *p == TAB)
7239 {
7240 if (*p != TAB && *needclr)
7241 {
7242 // remove any text still there from the command
7243 msg_clr_eos();
7244 *needclr = FALSE;
7245 }
7246 msg_putchar_attr(*p, echo_attr);
7247 }
7248 else
7249 {
7250 if (has_mbyte)
7251 {
7252 int i = (*mb_ptr2len)(p);
7253
7254 (void)msg_outtrans_len_attr(p, i, echo_attr);
7255 p += i - 1;
7256 }
7257 else
7258 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7259 }
7260 }
7261 vim_free(tofree);
7262}
7263
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 * ":echo expr1 ..." print each argument separated with a space, add a
7266 * newline at the end.
7267 * ":echon expr1 ..." print each argument plain.
7268 */
7269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007274 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int needclr = TRUE;
7276 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007277 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007278 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007279 evalarg_T evalarg;
7280
Bram Moolenaare707c882020-07-01 13:07:37 +02007281 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 if (eap->skip)
7284 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007285 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007287 // If eval1() causes an error message the text from the command may
7288 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007289 need_clr_eos = needclr;
7290
Bram Moolenaar68db9962021-05-09 23:19:22 +02007291 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007292 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 /*
7295 * Report the invalid expression unless the expression evaluation
7296 * has been cancelled due to an aborting error, an interrupt, or an
7297 * exception.
7298 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007299 if (!aborting() && did_emsg == did_emsg_before
7300 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007301 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007302 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 break;
7304 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007305 need_clr_eos = FALSE;
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007308 {
7309 if (rettv.v_type == VAR_VOID)
7310 {
7311 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7312 break;
7313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007315 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007317 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 arg = skipwhite(arg);
7319 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007320 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007321 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
7323 if (eap->skip)
7324 --emsg_skip;
7325 else
7326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007327 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (needclr)
7329 msg_clr_eos();
7330 if (eap->cmdidx == CMD_echo)
7331 msg_end();
7332 }
7333}
7334
7335/*
7336 * ":echohl {name}".
7337 */
7338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007339ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007341 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342}
7343
7344/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007345 * Returns the :echo attribute
7346 */
7347 int
7348get_echo_attr(void)
7349{
7350 return echo_attr;
7351}
7352
7353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 * ":execute expr1 ..." execute the result of an expression.
7355 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007356 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007358 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * Each gets spaces around each argument and a newline at the end for
7360 * echo commands
7361 */
7362 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007363ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 int ret = OK;
7368 char_u *p;
7369 garray_T ga;
7370 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007371 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 ga_init2(&ga, 1, 80);
7374
7375 if (eap->skip)
7376 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007377 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007379 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007380 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 if (!eap->skip)
7384 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007385 char_u buf[NUMBUFLEN];
7386
7387 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007388 {
7389 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7390 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007391 semsg(_(e_using_invalid_value_as_string_str),
7392 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007393 p = NULL;
7394 }
7395 else
7396 p = tv_get_string_buf(&rettv, buf);
7397 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007398 else
7399 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007400 if (p == NULL)
7401 {
7402 clear_tv(&rettv);
7403 ret = FAIL;
7404 break;
7405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 len = (int)STRLEN(p);
7407 if (ga_grow(&ga, len + 2) == FAIL)
7408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 ret = FAIL;
7411 break;
7412 }
7413 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 ga.ga_len += len;
7417 }
7418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 arg = skipwhite(arg);
7421 }
7422
7423 if (ret != FAIL && ga.ga_data != NULL)
7424 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007425 // use the first line of continuation lines for messages
7426 SOURCING_LNUM = start_lnum;
7427
Bram Moolenaar37fef162022-08-29 18:16:32 +01007428 if (eap->cmdidx == CMD_echomsg
7429 || eap->cmdidx == CMD_echowindow
7430 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007432 // Mark the already saved text as finishing the line, so that what
7433 // follows is displayed on a new line when scrolling back at the
7434 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007435 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007436 }
7437
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007438 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007439 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007440 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007441 out_flush();
7442 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007443 else if (eap->cmdidx == CMD_echowindow)
7444 {
7445#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007446 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007447#endif
7448 msg_attr(ga.ga_data, echo_attr);
7449#ifdef HAS_MESSAGE_WINDOW
7450 end_echowindow();
7451#endif
7452 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007453 else if (eap->cmdidx == CMD_echoconsole)
7454 {
7455 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7456 ui_write((char_u *)"\r\n", 2, TRUE);
7457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 else if (eap->cmdidx == CMD_echoerr)
7459 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007460 int save_did_emsg = did_emsg;
7461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007462 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (!force_abort)
7465 did_emsg = save_did_emsg;
7466 }
7467 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007468 {
7469 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7470
7471 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7472 sticky_cmdmod_flags = cmdmod.cmod_flags
7473 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 do_cmdline((char_u *)ga.ga_data,
7475 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007476 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479
7480 ga_clear(&ga);
7481
7482 if (eap->skip)
7483 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007484 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485}
7486
7487/*
7488 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7489 * "arg" points to the "&" or '+' when called, to "option" when returning.
7490 * Returns NULL when no option name found. Otherwise pointer to the char
7491 * after the option name.
7492 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007493 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007494find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 char_u *p = *arg;
7497
7498 ++p;
7499 if (*p == 'g' && p[1] == ':')
7500 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007501 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 p += 2;
7503 }
7504 else if (*p == 'l' && p[1] == ':')
7505 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007506 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 p += 2;
7508 }
7509 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007510 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
7512 if (!ASCII_ISALPHA(*p))
7513 return NULL;
7514 *arg = p;
7515
7516 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007517 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else
7519 while (ASCII_ISALPHA(*p))
7520 ++p;
7521 return p;
7522}
7523
7524/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007525 * Display script name where an item was last set.
7526 * Should only be invoked when 'verbose' is non-zero.
7527 */
7528 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007529last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007530{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007531 char_u *p;
7532
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007533 if (script_ctx.sc_sid == 0)
7534 return;
7535
7536 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7537 if (p == NULL)
7538 return;
7539
7540 verbose_enter();
7541 msg_puts(_("\n\tLast set from "));
7542 msg_puts((char *)p);
7543 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007544 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007545 msg_puts(_(line_msg));
7546 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007547 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007548 verbose_leave();
7549 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007550}
7551
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007552#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554/*
7555 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007556 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 * "flags" can be "g" to do a global substitute.
7558 * Returns an allocated string, NULL for error.
7559 */
7560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561do_string_sub(
7562 char_u *str,
7563 char_u *pat,
7564 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007565 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007566 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 int sublen;
7569 regmatch_T regmatch;
7570 int i;
7571 int do_all;
7572 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007573 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 garray_T ga;
7575 char_u *ret;
7576 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007577 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007579 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007581 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
7583 ga_init2(&ga, 1, 200);
7584
7585 do_all = (flags[0] == 'g');
7586
7587 regmatch.rm_ic = p_ic;
7588 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7589 if (regmatch.regprog != NULL)
7590 {
7591 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007592 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007595 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007596 if (regmatch.startp[0] == regmatch.endp[0])
7597 {
7598 if (zero_width == regmatch.startp[0])
7599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007600 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007601 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007602 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7603 (size_t)i);
7604 ga.ga_len += i;
7605 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007606 continue;
7607 }
7608 zero_width = regmatch.startp[0];
7609 }
7610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 /*
7612 * Get some space for a temporary buffer to do the substitution
7613 * into. It will contain:
7614 * - The text up to where the match is.
7615 * - The substituted text.
7616 * - The text after the match.
7617 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007618 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007619 if (sublen <= 0)
7620 {
7621 ga_clear(&ga);
7622 break;
7623 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007624 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7626 {
7627 ga_clear(&ga);
7628 break;
7629 }
7630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007631 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 i = (int)(regmatch.startp[0] - tail);
7633 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007634 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007635 (void)vim_regsub(&regmatch, sub, expr,
7636 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7637 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007639 tail = regmatch.endp[0];
7640 if (*tail == NUL)
7641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (!do_all)
7643 break;
7644 }
7645
7646 if (ga.ga_data != NULL)
7647 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7648
Bram Moolenaar473de612013-06-08 18:19:48 +02007649 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651
7652 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7653 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007654 if (p_cpo == empty_option)
7655 p_cpo = save_cpo;
7656 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007658 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007659 // If it's still empty it was changed and restored, need to restore in
7660 // the complicated way.
7661 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007662 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007663 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
7666 return ret;
7667}