blob: 923e7a43d9bfe130bb40f1ae6ad9f8ef28c4f953 [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 Moolenaar5409f5d2020-06-24 18:37:35 +020025static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
26static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
27static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
28static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010029static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020030static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010031static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
32static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
33static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000034
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020035static 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 +020036
Bram Moolenaare21c1582019-03-02 11:57:09 +010037/*
38 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010039 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010040 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020041 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010042num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010043{
44 varnumber_T result;
45
Bram Moolenaar99880f92021-01-20 21:23:14 +010046 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010047 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010048 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010049 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010050 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010051 if (failed != NULL)
52 *failed = TRUE;
53 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010054 if (n1 == 0)
55 result = VARNUM_MIN; // similar to NaN
56 else if (n1 < 0)
57 result = -VARNUM_MAX;
58 else
59 result = VARNUM_MAX;
60 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010061 else if (n1 == VARNUM_MIN && n2 == -1)
62 {
63 // specific case: trying to do VARNUM_MIN / -1 results in a positive
64 // number that doesn't fit in varnumber_T and causes an FPE
65 result = VARNUM_MAX;
66 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010067 else
68 result = n1 / n2;
69
70 return result;
71}
72
73/*
74 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010076 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020077 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010078num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010079{
Bram Moolenaar99880f92021-01-20 21:23:14 +010080 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010081 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 if (failed != NULL)
84 *failed = TRUE;
85 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010086 return (n2 == 0) ? 0 : (n1 % n2);
87}
88
Bram Moolenaar33570922005-01-25 22:26:29 +000089/*
90 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000091 */
92 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010093eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000094{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020095 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020096 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000097}
98
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000099#if defined(EXITFREE) || defined(PROTO)
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100104 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200105 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000106
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200107 // autoloaded script names
108 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000109
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100110 // unreferenced lists, tuples and dicts
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200111 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200112
113 // functions not garbage collected
114 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000115}
116#endif
117
Bram Moolenaare6b53242020-07-01 17:28:33 +0200118 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200119fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
120{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100121 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200122 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000123
124 if (eap == NULL)
125 return;
126
127 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100128 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200129 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100130 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200132 }
133}
134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135/*
136 * Top level evaluation function, returning a boolean.
137 * Sets "error" to TRUE if there was an error.
138 * Return TRUE or FALSE.
139 */
140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100141eval_to_bool(
142 char_u *arg,
143 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200144 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100145 int skip, // only parse, don't execute
146 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100151 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200152
Bram Moolenaar37c83712020-06-30 21:18:36 +0200153 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155 if (skip)
156 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100157 if (use_simple_function)
158 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
159 else
160 r = eval0(arg, &tv, eap, &evalarg);
161 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 else
164 {
165 *error = FALSE;
166 if (!skip)
167 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200168 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200169 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200170 else
171 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 }
174 }
175 if (skip)
176 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200177 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180}
181
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182/*
183 * Call eval1() and give an error message if not done at a lower level.
184 */
185 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200186eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100187{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100188 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189 int ret;
190 int did_emsg_before = did_emsg;
191 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200192 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
195
196 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 if (ret == FAIL)
198 {
199 // Report the invalid expression unless the expression evaluation has
200 // been cancelled due to an aborting error, an interrupt, or an
201 // exception, or we already gave a more specific error.
202 // Also check called_emsg for when using assert_fails().
203 if (!aborting() && did_emsg == did_emsg_before
204 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200205 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200207 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 return ret;
209}
210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200212 * Return whether a typval is a valid expression to pass to eval_expr_typval()
213 * or eval_expr_to_bool(). An empty string returns FALSE;
214 */
215 int
216eval_expr_valid_arg(typval_T *tv)
217{
218 return tv->v_type != VAR_UNKNOWN
219 && (tv->v_type != VAR_STRING
220 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
221}
222
223/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100224 * When calling eval_expr_typval() many times we only need one funccall_T.
225 * Returns NULL when no funccall_T is to be used.
226 * When returning non-NULL remove_funccal() must be called later.
227 */
228 funccall_T *
229eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
230{
231 if (expr->v_type != VAR_PARTIAL)
232 return NULL;
233
234 partial_T *partial = expr->vval.v_partial;
235 if (partial == NULL)
236 return NULL;
237 if (partial->pt_func == NULL
238 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
239 return NULL;
240
241 return create_funccal(partial->pt_func, rettv);
242}
243
244/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200245 * Evaluate a partial.
246 * Pass arguments "argv[argc]".
247 * "fc_arg" is from eval_expr_get_funccal() or NULL;
248 * Return the result in "rettv" and OK or FAIL.
249 */
250 static int
251eval_expr_partial(
252 typval_T *expr,
253 typval_T *argv,
254 int argc,
255 funccall_T *fc_arg,
256 typval_T *rettv)
257{
258 partial_T *partial = expr->vval.v_partial;
259
260 if (partial == NULL)
261 return FAIL;
262
263 if (partial->pt_func != NULL
264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
265 {
266 funccall_T *fc = fc_arg != NULL ? fc_arg
267 : create_funccal(partial->pt_func, rettv);
268 int r;
269
270 if (fc == NULL)
271 return FAIL;
272
273 // Shortcut to call a compiled function with minimal overhead.
Yegappan Lakshmanan481992c2024-12-06 18:35:12 +0100274 if (partial->pt_obj != NULL)
275 partial->pt_obj->obj_refcount++;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200276 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
Yegappan Lakshmanan481992c2024-12-06 18:35:12 +0100277 partial, partial->pt_obj, fc, rettv);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200278 if (fc_arg == NULL)
279 remove_funccal();
280 if (r == FAIL)
281 return FAIL;
282 }
283 else
284 {
285 char_u *s = partial_name(partial);
286 funcexe_T funcexe;
287
288 if (s == NULL || *s == NUL)
289 return FAIL;
290
291 CLEAR_FIELD(funcexe);
292 funcexe.fe_evaluate = TRUE;
293 funcexe.fe_partial = partial;
294 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
295 return FAIL;
296 }
297
298 return OK;
299}
300
301/*
302 * Evaluate an expression which is a function.
303 * Pass arguments "argv[argc]".
304 * Return the result in "rettv" and OK or FAIL.
305 */
306 static int
307eval_expr_func(
308 typval_T *expr,
309 typval_T *argv,
310 int argc,
311 typval_T *rettv)
312{
313 funcexe_T funcexe;
314 char_u buf[NUMBUFLEN];
315 char_u *s;
316
317 if (expr->v_type == VAR_FUNC)
318 s = expr->vval.v_string;
319 else
320 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
321 if (s == NULL || *s == NUL)
322 return FAIL;
323
324 CLEAR_FIELD(funcexe);
325 funcexe.fe_evaluate = TRUE;
326 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
327 return FAIL;
328
329 return OK;
330}
331
332/*
333 * Evaluate an expression, which is a string.
334 * Return the result in "rettv" and OK or FAIL.
335 */
336 static int
337eval_expr_string(
338 typval_T *expr,
339 typval_T *rettv)
340{
341 char_u *s;
342 char_u buf[NUMBUFLEN];
343
344 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
345 if (s == NULL)
346 return FAIL;
347
348 s = skipwhite(s);
349 if (eval1_emsg(&s, rettv, NULL) == FAIL)
350 return FAIL;
351
352 if (*skipwhite(s) != NUL) // check for trailing chars after expr
353 {
354 clear_tv(rettv);
355 semsg(_(e_invalid_expression_str), s);
356 return FAIL;
357 }
358
359 return OK;
360}
361
362/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 * Evaluate an expression, which can be a function, partial or string.
364 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200365 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100366 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 * Return the result in "rettv" and OK or FAIL.
368 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200369 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100370eval_expr_typval(
371 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200372 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100373 typval_T *argv,
374 int argc,
375 funccall_T *fc_arg,
376 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100377{
zeertzjqad0c4422023-08-17 22:15:47 +0200378 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200379 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
John Marriottbd4614f2024-11-18 20:25:21 +0100380 if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200381 return exe_typval_instr(expr, rettv);
John Marriottbd4614f2024-11-18 20:25:21 +0100382 if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200383 return eval_expr_func(expr, argv, argc, rettv);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200384
John Marriottbd4614f2024-11-18 20:25:21 +0100385 return eval_expr_string(expr, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100386}
387
388/*
389 * Like eval_to_bool() but using a typval_T instead of a string.
390 * Works for string, funcref and partial.
391 */
392 int
393eval_expr_to_bool(typval_T *expr, int *error)
394{
395 typval_T rettv;
396 int res;
397
zeertzjqad0c4422023-08-17 22:15:47 +0200398 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100399 {
400 *error = TRUE;
401 return FALSE;
402 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200403 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100404 clear_tv(&rettv);
405 return res;
406}
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408/*
409 * Top level evaluation function, returning a string. If "skip" is TRUE,
410 * only parsing to "nextcmd" is done, without reporting errors. Return
411 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
412 */
413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100414eval_to_string_skip(
415 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200416 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100417 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
Bram Moolenaar33570922005-01-25 22:26:29 +0000419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200421 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaar37c83712020-06-30 21:18:36 +0200423 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 if (skip)
425 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200426 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 retval = NULL;
428 else
429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100430 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000431 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 }
433 if (skip)
434 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200435 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 return retval;
438}
439
440/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100441 * Initialize "evalarg" for use.
442 */
443 void
444init_evalarg(evalarg_T *evalarg)
445{
446 CLEAR_POINTER(evalarg);
447 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
448}
449
450/*
451 * If "evalarg->eval_tofree" is not NULL free it later.
452 * Caller is expected to overwrite "evalarg->eval_tofree" next.
453 */
454 static void
455free_eval_tofree_later(evalarg_T *evalarg)
456{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000457 if (evalarg->eval_tofree == NULL)
458 return;
459
460 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
461 ((char_u **)evalarg->eval_tofree_ga.ga_data)
462 [evalarg->eval_tofree_ga.ga_len++]
463 = evalarg->eval_tofree;
464 else
465 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100466}
467
468/*
469 * After using "evalarg" filled from "eap": free the memory.
470 */
471 void
472clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
473{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000474 if (evalarg == NULL)
475 return;
476
477 garray_T *etga = &evalarg->eval_tofree_ga;
478
479 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100480 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000481 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100482 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000483 // We may need to keep the original command line, e.g. for
484 // ":let" it has the variable names. But we may also need
485 // the new one, "nextcmd" points into it. Keep both.
486 vim_free(eap->cmdline_tofree);
487 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100488
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000489 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
490 {
491 // "nextcmd" points into the last line in eval_tofree_ga,
492 // need to keep it around.
493 --etga->ga_len;
494 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
495 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100496 }
497 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000498 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100499 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000500 else
501 vim_free(evalarg->eval_tofree);
502 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100503 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000504
505 ga_clear_strings(etga);
506 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100507}
508
509/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000510 * Skip over an expression at "*pp".
511 * Return FAIL for an error, OK otherwise.
512 */
513 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200514skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000515{
Bram Moolenaar33570922005-01-25 22:26:29 +0000516 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000517
518 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200519 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000520}
521
522/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200523 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 * If in Vim9 script and line breaks are encountered, the lines are
525 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200526 * "arg" is advanced to just after the expression.
527 * "start" is set to the start of the expression, "end" to just after the end.
528 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200529 * Return FAIL for an error, OK otherwise.
530 */
531 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200532skip_expr_concatenate(
533 char_u **arg,
534 char_u **start,
535 char_u **end,
536 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200537{
538 typval_T rettv;
539 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200540 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200541 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200542 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200543 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200544 int evaluate = evalarg == NULL
545 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200546
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200547 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200548 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200549 {
550 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200552 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200553 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200555 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200556 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200557
558 // Don't evaluate the expression.
559 if (evalarg != NULL)
560 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200561 *arg = skipwhite(*arg);
562 res = eval1(arg, &rettv, evalarg);
563 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200564 if (evalarg != NULL)
565 evalarg->eval_flags = save_flags;
566
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200567 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200568 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200569 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200570 if (evalarg->eval_ga.ga_len == 1)
571 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200572 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200573 ga_clear(gap);
574 gap->ga_itemsize = 0;
575 }
576 else
577 {
578 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200579 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200580
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200581 // Line breaks encountered, concatenate all the lines.
582 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200583 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200584
585 // free the lines only when using getsourceline()
586 if (evalarg->eval_cookie != NULL)
587 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200588 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200589 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200590 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100591 // later. Also free "eval_tofree" later if needed.
592 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200593 evalarg->eval_tofree =
594 ((char_u **)gap->ga_data)[gap->ga_len - 1];
595 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200596 ga_clear_strings(gap);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200597 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200598 }
599 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200600 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200601 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200602
603 // free lines that were explicitly marked for freeing
604 ga_clear_strings(freegap);
605 }
606
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200607 gap->ga_itemsize = 0;
608 if (p == NULL)
609 return FAIL;
610 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200611 vim_free(evalarg->eval_tofree_lambda);
612 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200613 // Compute "end" relative to the end.
614 *end = *start + STRLEN(*start) - endoff;
615 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200616 }
617
618 return res;
619}
620
621/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100622 * Convert "tv" to a string.
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100623 * When "join_list" is TRUE convert a List or a Tuple into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100624 * Returns an allocated string (NULL when out of memory).
625 */
626 char_u *
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200627typval2string(typval_T *tv, int join_list)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100628{
629 garray_T ga;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100630 char_u *retval = NULL;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100631
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100632 if (join_list && (tv->v_type == VAR_LIST || tv->v_type == VAR_TUPLE))
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100634 if (tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100635 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100636 ga_init2(&ga, sizeof(char), 80);
637 if (tv->vval.v_list != NULL)
638 {
639 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE,
640 0);
641 if (tv->vval.v_list->lv_len > 0)
642 ga_append(&ga, NL);
643 }
644 ga_append(&ga, NUL);
645 retval = (char_u *)ga.ga_data;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100646 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100647 else
648 {
649 // tuple
650 ga_init2(&ga, sizeof(char), 80);
651 if (tv->vval.v_tuple != NULL)
652 {
653 tuple_join(&ga, tv->vval.v_tuple, (char_u *)"\n", TRUE, FALSE,
654 0);
655 if (TUPLE_LEN(tv->vval.v_tuple) > 0)
656 ga_append(&ga, NL);
657 }
658 ga_append(&ga, NUL);
659 retval = (char_u *)ga.ga_data;
660 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100661 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100662 else if (tv->v_type == VAR_LIST
663 || tv->v_type == VAR_TUPLE
664 || tv->v_type == VAR_DICT)
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200665 {
666 char_u *tofree;
667 char_u numbuf[NUMBUFLEN];
668
669 retval = tv2string(tv, &tofree, numbuf, 0);
670 // Make a copy if we have a value but it's not in allocated memory.
671 if (retval != NULL && tofree == NULL)
672 retval = vim_strsave(retval);
673 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100674 else
675 retval = vim_strsave(tv_get_string(tv));
676 return retval;
677}
678
679/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200680 * Top level evaluation function, returning a string. Does not handle line
681 * breaks.
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100682 * When "join_list" is TRUE convert a List and a Tuple into a sequence of
683 * lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * Return pointer to allocated memory, or NULL for failure.
685 */
686 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100687eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100688 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200689 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100690 exarg_T *eap,
691 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100695 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100698 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100699 if (use_simple_function)
700 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
701 else
702 r = eval0(arg, &tv, NULL, &evalarg);
703 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 retval = NULL;
705 else
706 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200707 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000708 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100710 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 return retval;
713}
714
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100715 char_u *
716eval_to_string(
717 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200718 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100719 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100720{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200721 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100722}
723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000725 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100726 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200727 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730eval_to_string_safe(
731 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000732 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100733 int keep_script_version,
734 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200737 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200738 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200739 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar9530b582022-01-22 13:39:08 +0000741 if (!keep_script_version)
742 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200743 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000744 if (use_sandbox)
745 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100746 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200747 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100748 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000749 if (use_sandbox)
750 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100751 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200752 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200753 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200754 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 return retval;
756}
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758/*
759 * Top level evaluation function, returning a number.
760 * Evaluates "expr" silently.
761 * Returns -1 for an error.
762 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200763 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100764eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
Bram Moolenaar33570922005-01-25 22:26:29 +0000766 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000768 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100769 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 ++emsg_off;
772
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100773 if (use_simple_function)
774 r = may_call_simple_func(expr, &rettv);
775 if (r == NOTDONE)
776 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
777 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 retval = -1;
779 else
780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100781 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 --emsg_off;
785
786 return retval;
787}
788
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000789/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000790 * Top level evaluation function.
791 * Returns an allocated typval_T with the result.
792 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000793 */
794 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200795eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000796{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100797 return eval_expr_ext(arg, eap, FALSE);
798}
799
800 typval_T *
801eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
802{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000803 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200804 evalarg_T evalarg;
805
806 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000807
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200808 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100809 if (tv != NULL)
810 {
811 int r = NOTDONE;
812
813 if (use_simple_function)
814 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
815 if (r == NOTDONE)
816 r = eval0(arg, tv, eap, &evalarg);
817
818 if (r == FAIL)
819 VIM_CLEAR(tv);
820 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000821
Bram Moolenaar37c83712020-06-30 21:18:36 +0200822 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823 return tv;
824}
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000827 * "*arg" points to what can be a function name in the form of "import.Name" or
828 * "Funcref". Return the name of the function. Set "tofree" to something that
829 * was allocated.
830 * If "verbose" is FALSE no errors are given.
831 * Return NULL for any failure.
832 */
833 static char_u *
834deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000835 char_u **arg,
836 char_u **tofree,
837 evalarg_T *evalarg,
838 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000839{
840 typval_T ref;
841 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100842 int save_flags = 0;
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100843 int evaluate = evalarg != NULL
844 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000845
846 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100847 if (evalarg != NULL)
848 {
849 // need to evaluate this to get an import, like in "a.Func"
850 save_flags = evalarg->eval_flags;
851 evalarg->eval_flags |= EVAL_EVALUATE;
852 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100853 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100854 {
855 dictitem_T *v;
856
857 // If <SID>VarName was used it would not be found, try another way.
858 v = find_var_also_in_script(name, NULL, FALSE);
859 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100860 {
861 name = NULL;
862 goto theend;
863 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100864 copy_tv(&v->di_tv, &ref);
865 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 if (*skipwhite(*arg) != NUL)
867 {
868 if (verbose)
869 semsg(_(e_trailing_characters_str), *arg);
870 name = NULL;
871 }
872 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
873 {
874 name = ref.vval.v_string;
875 ref.vval.v_string = NULL;
876 *tofree = name;
877 }
878 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
879 {
880 if (ref.vval.v_partial->pt_argc > 0
881 || ref.vval.v_partial->pt_dict != NULL)
882 {
883 if (verbose)
884 emsg(_(e_cannot_use_partial_here));
885 name = NULL;
886 }
887 else
888 {
889 name = vim_strsave(partial_name(ref.vval.v_partial));
890 *tofree = name;
891 }
892 }
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100893 else if (evaluate)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000894 {
895 if (verbose)
896 semsg(_(e_not_callable_type_str), name);
897 name = NULL;
898 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100899
900theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000901 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100902 if (evalarg != NULL)
903 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000904 return name;
905}
906
907/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100908 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200909 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
910 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914call_vim_function(
915 char_u *func,
916 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200917 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200918 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000920 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200921 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000922 char_u *arg;
923 char_u *name;
924 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000925 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100927 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200928 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000929 funcexe.fe_firstline = curwin->w_cursor.lnum;
930 funcexe.fe_lastline = curwin->w_cursor.lnum;
931 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000932
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000933 // The name might be "import.Func" or "Funcref". We don't know, we need to
934 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000935 // autoload script has errors. Guess that when there is a dot in the name
936 // showing errors is the right choice.
937 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000938 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000939 if (ignore_errors)
940 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000941 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000942 if (ignore_errors)
943 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000944 if (name == NULL)
945 name = func;
946
947 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
948
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949 if (ret == FAIL)
950 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000951 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952
953 return ret;
954}
955
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100956/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100957 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000958 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
959 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000960 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000961 */
962 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963call_func_retstr(
964 char_u *func,
965 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200966 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000967{
968 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000969 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000970
Bram Moolenaarded27a12018-08-01 19:06:03 +0200971 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000972 return NULL;
973
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100974 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 return retval;
977}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000978
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000979/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100980 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000981 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000982 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100983 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000984 */
985 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986call_func_retlist(
987 char_u *func,
988 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200989 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000990{
991 typval_T rettv;
992
Bram Moolenaarded27a12018-08-01 19:06:03 +0200993 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000994 return NULL;
995
996 if (rettv.v_type != VAR_LIST)
997 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100998 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
999 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001000 clear_tv(&rettv);
1001 return NULL;
1002 }
1003
1004 return rettv.vval.v_list;
1005}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaare70dd112022-01-21 16:31:11 +00001007#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001009 * Evaluate "arg", which is 'foldexpr'.
1010 * Note: caller must set "curwin" to match "arg".
1011 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
1012 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 */
1014 int
Bram Moolenaare70dd112022-01-21 16:31:11 +00001015eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaare70dd112022-01-21 16:31:11 +00001017 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00001018 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001019 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +00001021 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001022 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001023 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001025 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001026 current_sctx = wp->w_p_script_ctx[WV_FDE];
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001029 if (use_sandbox)
1030 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001031 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001033
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001034 // Evaluate the expression. If the expression is "FuncName()" call the
1035 // function directly.
1036 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 retval = 0;
1038 else
1039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 if (tv.v_type == VAR_NUMBER)
1042 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001043 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 retval = 0;
1045 else
1046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 // If the result is a string, check if there is a non-digit before
1048 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001050 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 *cp = *s++;
1052 retval = atol((char *)s);
1053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
1056 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001057 if (use_sandbox)
1058 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001059 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001060 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001061 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001063 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
1065#endif
1066
Ernie Rael64885642023-10-04 20:16:22 +02001067#ifdef LOG_LOCKVAR
1068typedef struct flag_string_S
1069{
1070 int flag;
1071 char *str;
1072} flag_string_T;
1073
1074 static char *
1075flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1076{
1077 char *p = buf;
1078 *p = NUL;
1079 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1080 {
1081 if ((fstring->flag & flags) != 0)
1082 {
1083 size_t len = STRLEN(fstring->str);
1084 if (n > p - buf + len + 7)
1085 {
1086 STRCAT(p, fstring->str);
1087 p += len;
1088 STRCAT(p, " ");
1089 ++p;
1090 }
1091 else
1092 {
1093 STRCAT(buf, "...");
1094 break;
1095 }
1096 }
1097 }
1098 return buf;
1099}
1100
1101flag_string_T glv_flag_strings[] = {
1102 { GLV_QUIET, "QUIET" },
1103 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1104 { GLV_READ_ONLY, "READ_ONLY" },
1105 { GLV_NO_DECL, "NO_DECL" },
1106 { GLV_COMPILING, "COMPILING" },
1107 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1108 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1109 { 0, NULL }
1110};
1111#endif
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113/*
Ernie Raelee865f32023-09-29 19:53:55 +02001114 * Fill in "lp" using "root". This is used in a special case when
1115 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1116 *
1117 * This is typically called with "lval_root" as "root". For a class, find
1118 * the name from lp in the class from root, fill in lval_T if found. For a
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001119 * complex type, list/tuple/dict use it as the result; just put the root into
Ernie Raelee865f32023-09-29 19:53:55 +02001120 * ll_tv.
1121 *
1122 * "lval_root" is a hack used during run-time/instr-execution to provide the
1123 * starting point for "get_lval()" to traverse a chain of indexes. In some
1124 * cases get_lval sees a bare name and uses this function to populate the
1125 * lval_T.
1126 *
1127 * For setting up "lval_root" (currently only used with lockvar)
1128 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1129 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1130 */
1131 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001132fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001133{
1134#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001135 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1136 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001137#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001138 if (lr->lr_tv == NULL)
1139 return;
Ernie Rael64885642023-10-04 20:16:22 +02001140 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001141 {
Ernie Rael64885642023-10-04 20:16:22 +02001142 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001143 {
1144 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001145 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001146 int m_idx;
1147 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1148 lp->ll_name_end - lp->ll_name, &m_idx);
1149 if (m != NULL)
1150 {
1151 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001152 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001153 lp->ll_oi = m_idx;
1154 lp->ll_valtype = m->ocm_type;
1155 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1156#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001157 ch_log(NULL, "LKVAR: ... class member %s.%s",
1158 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001159#endif
1160 return;
1161 }
1162 }
1163 }
1164
1165#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001166 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001167#endif
Ernie Rael64885642023-10-04 20:16:22 +02001168 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001169 lp->ll_is_root = TRUE;
1170}
1171
1172/*
Ernie Rael64885642023-10-04 20:16:22 +02001173 * Check if the class has permission to access the member.
1174 * Returns OK or FAIL.
1175 */
1176 static int
1177get_lval_check_access(
1178 class_T *cl_exec, // executing class, NULL if :def or script level
1179 class_T *cl, // class which contains the member
1180 ocmember_T *om, // member being accessed
1181 char_u *p, // char after member name
1182 int flags) // GLV flags to check if writing to lval
1183{
1184#ifdef LOG_LOCKVAR
1185 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1186 (void*)cl_exec, (void*)cl, *p);
1187#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001188 if (cl_exec != NULL && cl_exec == cl)
1189 return OK;
1190
1191 char *msg = NULL;
1192 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001193 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001194 case VIM_ACCESS_PRIVATE:
1195 msg = e_cannot_access_protected_variable_str;
1196 break;
1197 case VIM_ACCESS_READ:
1198 // If [idx] or .key following, read only OK.
1199 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001200 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001201 if ((flags & GLV_READ_ONLY) == 0)
1202 {
1203 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001204 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001205 if (om->ocm_type->tt_type == VAR_OBJECT)
1206 semsg(_(e_enumvalue_str_cannot_be_modified),
1207 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001208 else
1209 msg = e_variable_is_not_writable_str;
1210 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001211 else
1212 msg = e_variable_is_not_writable_str;
1213 }
1214 break;
1215 case VIM_ACCESS_ALL:
1216 break;
1217 }
1218 if (msg != NULL)
1219 {
1220 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1221 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001222 }
1223 return OK;
1224}
1225
1226/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001227 * Get lval information for a variable imported from script "imp_sid". On
1228 * success, updates "lp" with the variable name, type, script ID and typval.
1229 * The variable name starts at or after "p".
1230 * If "rettv" is not NULL it points to the value to be assigned. This used to
1231 * match the rhs and lhs types.
1232 * Returns a pointer to the character after the variable name if the imported
1233 * variable is valid and writable.
1234 * Returns NULL if the variable is not exported or typval is not found or the
1235 * rhs type doesn't match the lhs type or the variable is not writable.
1236 */
1237 static char_u *
1238get_lval_imported(
1239 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001240 scid_T imp_sid,
1241 char_u *p,
1242 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001243 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001244{
1245 ufunc_T *ufunc;
1246 type_T *type = NULL;
1247 int cc;
1248 int rc = FAIL;
1249
1250 p = skipwhite(p);
1251
1252 import_check_sourced_sid(&imp_sid);
1253 lp->ll_sid = imp_sid;
1254 lp->ll_name = p;
1255 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1256 lp->ll_name_end = p;
1257
1258 // check the item is exported
1259 cc = *p;
1260 *p = NUL;
1261 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1262 TRUE) == -1)
1263 goto failed;
1264
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001265 // Get the typval for the exported item
1266 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1267 if (ht == NULL)
1268 goto failed;
1269
1270 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1271 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001272 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001273 goto success;
1274
1275 *dip = di;
1276
1277 // Check whether the variable is writable.
1278 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1279 if (sv != NULL && sv->sv_const != 0)
1280 {
1281 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1282 goto failed;
1283 }
1284
1285 // check whether variable is locked
1286 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1287 goto failed;
1288
1289 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001290 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001291
1292success:
1293 rc = OK;
1294
1295failed:
1296 *p = cc;
1297 return rc == OK ? p : NULL;
1298}
1299
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001300typedef enum {
1301 GLV_FAIL,
1302 GLV_OK,
1303 GLV_STOP
1304} glv_status_T;
1305
1306/*
1307 * Get an Dict lval variable that can be assigned a value to: "name",
1308 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1309 * "name" points to the start of the name.
1310 * If "rettv" is not NULL it points to the value to be assigned.
1311 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1312 * wrong; must end in space or cmd separator.
1313 *
1314 * flags:
1315 * GLV_QUIET: do not give error messages
1316 * GLV_READ_ONLY: will not change the variable
1317 * GLV_NO_AUTOLOAD: do not use script autoloading
1318 *
1319 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1320 * failure. Returns GLV_STOP to stop processing the characters following
1321 * 'key_end'.
1322 */
1323 static int
1324get_lval_dict_item(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001325 lval_T *lp,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001326 char_u *name,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001327 char_u *key,
1328 int len,
1329 char_u **key_end,
1330 typval_T *var1,
1331 int flags,
1332 int unlet,
1333 typval_T *rettv)
1334{
1335 int quiet = flags & GLV_QUIET;
1336 char_u *p = *key_end;
1337
1338 if (len == -1)
1339 {
1340 // "[key]": get key from "var1"
1341 key = tv_get_string_chk(var1); // is number or string
1342 if (key == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001343 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001344 }
1345 lp->ll_list = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001346 lp->ll_list = NULL;
1347 lp->ll_blob = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001348 lp->ll_object = NULL;
1349 lp->ll_class = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001350 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001351
1352 // a NULL dict is equivalent with an empty dict
1353 if (lp->ll_tv->vval.v_dict == NULL)
1354 {
1355 lp->ll_tv->vval.v_dict = dict_alloc();
1356 if (lp->ll_tv->vval.v_dict == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001357 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001358 ++lp->ll_tv->vval.v_dict->dv_refcount;
1359 }
1360 lp->ll_dict = lp->ll_tv->vval.v_dict;
1361
1362 lp->ll_di = dict_find(lp->ll_dict, key, len);
1363
1364 // When assigning to a scope dictionary check that a function and
1365 // variable name is valid (only variable name unless it is l: or
1366 // g: dictionary). Disallow overwriting a builtin function.
1367 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1368 {
1369 int prevval;
1370
1371 if (len != -1)
1372 {
1373 prevval = key[len];
1374 key[len] = NUL;
1375 }
1376 else
1377 prevval = 0; // avoid compiler warning
1378 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1379 && (rettv->v_type == VAR_FUNC
1380 || rettv->v_type == VAR_PARTIAL)
1381 && var_wrong_func_name(key, lp->ll_di == NULL))
1382 || !valid_varname(key, -1, TRUE);
1383 if (len != -1)
1384 key[len] = prevval;
1385 if (wrong)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001386 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001387 }
1388
1389 if (lp->ll_valtype != NULL)
1390 // use the type of the member
1391 lp->ll_valtype = lp->ll_valtype->tt_member;
1392
1393 if (lp->ll_di == NULL)
1394 {
1395 // Can't add "v:" or "a:" variable.
1396 if (lp->ll_dict == get_vimvar_dict()
1397 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1398 {
1399 semsg(_(e_illegal_variable_name_str), name);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001400 return GLV_FAIL;
1401 }
1402
1403 // Key does not exist in dict: may need to add it.
1404 if (*p == '[' || *p == '.' || unlet)
1405 {
1406 if (!quiet)
1407 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001408 return GLV_FAIL;
1409 }
1410 if (len == -1)
1411 lp->ll_newkey = vim_strsave(key);
1412 else
1413 lp->ll_newkey = vim_strnsave(key, len);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001414 if (lp->ll_newkey == NULL)
1415 p = NULL;
1416
1417 *key_end = p;
1418 return GLV_STOP;
1419 }
1420 // existing variable, need to check if it can be changed
1421 else if ((flags & GLV_READ_ONLY) == 0
1422 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1423 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001424 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001425
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001426 lp->ll_tv = &lp->ll_di->di_tv;
1427
1428 return GLV_OK;
1429}
1430
1431/*
1432 * Get an blob lval variable that can be assigned a value to: "name",
1433 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1434 *
1435 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1436 * blob index. If the first index is not specified in a range, then 'empty1'
1437 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1438 * invalid indexes.
1439 *
1440 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1441 */
1442 static int
1443get_lval_blob(
1444 lval_T *lp,
1445 typval_T *var1,
1446 typval_T *var2,
1447 int empty1,
1448 int quiet)
1449{
1450 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1451
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001452 lp->ll_list = NULL;
1453 lp->ll_dict = NULL;
1454 lp->ll_object = NULL;
1455 lp->ll_class = NULL;
1456 lp->ll_tuple = NULL;
1457
1458 // Get the number and item for the only or first index of a List or Tuple.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001459 if (empty1)
1460 lp->ll_n1 = 0;
1461 else
1462 // is number or string
1463 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001464
1465 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001466 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001467 if (lp->ll_range && !lp->ll_empty2)
1468 {
1469 lp->ll_n2 = (long)tv_get_number(var2);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001470 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1471 return FAIL;
1472 }
1473
1474 if (!lp->ll_range)
1475 // Indexing a single byte in a blob. So the rhs type is a
1476 // number.
1477 lp->ll_valtype = &t_number;
1478
1479 lp->ll_blob = lp->ll_tv->vval.v_blob;
1480 lp->ll_tv = NULL;
1481
1482 return OK;
1483}
1484
1485/*
1486 * Get a List lval variable that can be assigned a value to: "name",
1487 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1488 *
1489 * 'var1' specifies the starting List index and 'var2' specifies the ending
1490 * List index. If the first index is not specified in a range, then 'empty1'
1491 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1492 * invalid indexes.
1493 *
1494 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1495 */
1496 static int
1497get_lval_list(
1498 lval_T *lp,
1499 typval_T *var1,
1500 typval_T *var2,
1501 int empty1,
1502 int flags,
1503 int quiet)
1504{
1505 /*
1506 * Get the number and item for the only or first index of the List.
1507 */
1508 if (empty1)
1509 lp->ll_n1 = 0;
1510 else
1511 // is number or string
1512 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001513
1514 lp->ll_dict = NULL;
1515 lp->ll_object = NULL;
1516 lp->ll_class = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001517 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001518 lp->ll_list = lp->ll_tv->vval.v_list;
1519 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1520 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1521 if (lp->ll_li == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001522 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001523
1524 if (lp->ll_valtype != NULL && !lp->ll_range)
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001525 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001526 // use the type of the member
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001527 if (lp->ll_valtype->tt_member != NULL)
1528 lp->ll_valtype = lp->ll_valtype->tt_member;
1529 else
1530 // If the LHS member type is not known (VAR_ANY), then get it from
1531 // the list item (after indexing)
1532 lp->ll_valtype = typval2type(&lp->ll_li->li_tv, get_copyID(),
1533 &lp->ll_type_list, TVTT_DO_MEMBER);
1534
1535 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001536
1537 /*
1538 * May need to find the item or absolute index for the second
1539 * index of a range.
1540 * When no index given: "lp->ll_empty2" is TRUE.
1541 * Otherwise "lp->ll_n2" is set to the second index.
1542 */
1543 if (lp->ll_range && !lp->ll_empty2)
1544 {
1545 lp->ll_n2 = (long)tv_get_number(var2);
1546 // is number or string
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001547 if (check_range_index_two(lp->ll_list,
1548 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1549 return FAIL;
1550 }
1551
1552 lp->ll_tv = &lp->ll_li->li_tv;
1553
1554 return OK;
1555}
1556
1557/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001558 * Get a tuple lval variable that can be assigned a value to: "name",
1559 * "na{me}", "name[expr]", "name[expr][expr]", etc.
1560 *
1561 * 'idx' specifies the tuple index.
1562 * If 'quiet' is TRUE, then error messages are not displayed for an invalid
1563 * index.
1564 *
1565 * The typval is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1566 * failure.
1567 */
1568 static int
1569get_lval_tuple(
1570 lval_T *lp,
1571 typval_T *idx,
1572 int quiet)
1573{
1574 // is number or string
1575 lp->ll_n1 = (long)tv_get_number(idx);
1576
1577 lp->ll_list = NULL;
1578 lp->ll_dict = NULL;
1579 lp->ll_blob = NULL;
1580 lp->ll_object = NULL;
1581 lp->ll_class = NULL;
1582
1583 lp->ll_tuple = lp->ll_tv->vval.v_tuple;
1584 lp->ll_tv = tuple_find(lp->ll_tuple, lp->ll_n1);
1585 if (lp->ll_tv == NULL)
1586 {
1587 if (!quiet)
1588 semsg(_(e_tuple_index_out_of_range_nr), lp->ll_n1);
1589 return GLV_FAIL;
1590 }
1591
1592 // use the type of the member
1593 if (lp->ll_valtype != NULL)
1594 {
1595 if (lp->ll_valtype != NULL
1596 && lp->ll_valtype->tt_type == VAR_TUPLE
1597 && lp->ll_valtype->tt_argcount == 1)
1598 {
1599 // a variadic tuple or a single item tuple
1600 if (lp->ll_valtype->tt_flags & TTFLAG_VARARGS)
1601 lp->ll_valtype = lp->ll_valtype->tt_args[0]->tt_member;
1602 else
1603 lp->ll_valtype = lp->ll_valtype->tt_args[0];
1604 }
1605 else
1606 // If the LHS member type is not known (VAR_ANY), then get it from
1607 // the tuple item (after indexing)
1608 lp->ll_valtype = typval2type(lp->ll_tv, get_copyID(),
1609 &lp->ll_type_list, TVTT_DO_MEMBER);
1610 }
1611
1612 return GLV_OK;
1613}
1614
1615/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001616 * Get a class or object lval method in class "cl". The 'key' argument points
1617 * to the method name and 'key_end' points to the character after 'key'.
1618 * 'v_type' is VAR_CLASS or VAR_OBJECT.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001619 *
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001620 * The method index, method function pointer and method type are returned in
1621 * "lp".
1622 */
1623 static void
1624get_lval_oc_method(
1625 lval_T *lp,
1626 class_T *cl,
1627 char_u *key,
1628 char_u *key_end,
1629 vartype_T v_type)
1630{
1631 // Look for a method with this name.
1632 // round 1: class functions (skipped for an object)
1633 // round 2: object methods
1634 for (int round = v_type == VAR_OBJECT ? 2 : 1; round <= 2; ++round)
1635 {
1636 int m_idx;
1637 ufunc_T *fp;
1638
1639 fp = method_lookup(cl, round == 1 ? VAR_CLASS : VAR_OBJECT,
1640 key, key_end - key, &m_idx);
1641 lp->ll_oi = m_idx;
1642 if (fp != NULL)
1643 {
1644 lp->ll_ufunc = fp;
1645 lp->ll_valtype = fp->uf_func_type;
1646 break;
1647 }
1648 }
1649}
1650
1651/*
1652 * Get a class or object lval variable in class "cl". The "key" argument
1653 * points to the variable name and "key_end" points to the character after
1654 * "key". "v_type" is VAR_CLASS or VAR_OBJECT. "cl_exec" is the class that is
1655 * executing, or NULL.
1656 *
1657 * The variable index, typval and type are returned in "lp". Returns FAIL if
1658 * the variable is not writable. Otherwise returns OK.
1659 */
1660 static int
1661get_lval_oc_variable(
1662 lval_T *lp,
1663 class_T *cl,
1664 char_u *key,
1665 char_u *key_end,
1666 vartype_T v_type,
1667 class_T *cl_exec,
1668 int flags)
1669{
1670 int m_idx;
1671 ocmember_T *om;
1672
1673 om = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1674 lp->ll_oi = m_idx;
1675 if (om == NULL)
1676 return OK;
1677
1678 // Check variable is accessible
1679 if (get_lval_check_access(cl_exec, cl, om, key_end, flags) == FAIL)
1680 return FAIL;
1681
1682 // When lhs is used to modify the variable, check it is not a read-only
1683 // variable.
1684 if ((flags & GLV_READ_ONLY) == 0 && (*key_end != '.' && *key_end != '[')
1685 && oc_var_check_ro(cl, om))
1686 return FAIL;
1687
1688 lp->ll_valtype = om->ocm_type;
1689
1690 if (v_type == VAR_OBJECT)
1691 lp->ll_tv = ((typval_T *)(lp->ll_tv->vval.v_object + 1)) + m_idx;
1692 else
1693 lp->ll_tv = &cl->class_members_tv[m_idx];
1694
1695 return OK;
1696}
1697
1698/*
1699 * Get a Class or Object lval variable or method that can be assigned a value
1700 * to: "name", "name.key", "name.key[expr]" etc.
1701 *
1702 * The 'key' argument points to the member name and 'key_end' points to the
1703 * character after 'key'. 'v_type' is VAR_CLASS or VAR_OBJECT. 'cl_exec' is
1704 * the class that is executing, or NULL. If 'quiet' is TRUE, then error
1705 * messages are not displayed for invalid indexes.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001706 *
1707 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1708 * failure.
1709 */
1710 static int
1711get_lval_class_or_obj(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001712 lval_T *lp,
1713 char_u *key,
1714 char_u *key_end,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001715 vartype_T v_type,
1716 class_T *cl_exec,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001717 int flags,
1718 int quiet)
1719{
1720 lp->ll_dict = NULL;
1721 lp->ll_list = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001722 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001723
1724 class_T *cl;
1725 if (v_type == VAR_OBJECT)
1726 {
1727 if (lp->ll_tv->vval.v_object == NULL)
1728 {
1729 if (!quiet)
1730 emsg(_(e_using_null_object));
1731 return FAIL;
1732 }
1733 cl = lp->ll_tv->vval.v_object->obj_class;
1734 lp->ll_object = lp->ll_tv->vval.v_object;
1735 }
1736 else
1737 {
1738 cl = lp->ll_tv->vval.v_class;
1739 lp->ll_object = NULL;
1740 }
1741 lp->ll_class = cl;
1742
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001743 if (cl == NULL)
1744 // TODO: what if class is NULL?
1745 return OK;
1746
1747 lp->ll_valtype = NULL;
1748
1749 if (flags & GLV_PREFER_FUNC)
1750 get_lval_oc_method(lp, cl, key, key_end, v_type);
1751
1752 // Look for object/class member variable
1753 if (lp->ll_valtype == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001754 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001755 if (get_lval_oc_variable(lp, cl, key, key_end, v_type, cl_exec, flags)
1756 == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001757 return FAIL;
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001758 }
1759
1760 if (lp->ll_valtype == NULL)
1761 {
1762 member_not_found_msg(cl, v_type, key, key_end - key);
1763 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001764 }
1765
1766 return OK;
1767}
1768
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001769/*
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001770 * Check whether dot (".") is allowed after the variable "name" with type
1771 * "v_type". Only Dict, Class and Object types support a dot after the name.
1772 * Returns TRUE if dot is allowed after the name.
1773 */
1774 static int
1775dot_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1776{
1777 if (v_type != VAR_DICT && v_type != VAR_OBJECT && v_type != VAR_CLASS)
1778 {
1779 if (!quiet)
1780 semsg(_(e_dot_not_allowed_after_str_str),
1781 vartype_name(v_type), name);
1782 return FALSE;
1783 }
1784
1785 return TRUE;
1786}
1787
1788/*
1789 * Check whether left bracket ("[") is allowed after the variable "name" with
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001790 * type "v_type". Only Dict, List, Tuple and Blob types support a bracket
1791 * after the variable name. Returns TRUE if bracket is allowed after the name.
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001792 */
1793 static int
1794bracket_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1795{
1796 if (v_type == VAR_CLASS || v_type == VAR_OBJECT)
1797 {
1798 if (!quiet)
1799 semsg(_(e_index_not_allowed_after_str_str),
1800 vartype_name(v_type), name);
1801 return FALSE;
1802 }
1803
1804 return TRUE;
1805}
1806
1807/*
1808 * Check whether the variable "name" with type "v_type" can be followed by an
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001809 * index. Only Dict, List, Tuple, Blob, Object and Class types support
1810 * indexing. Returns TRUE if indexing is allowed after the name.
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001811 */
1812 static int
1813index_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1814{
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001815 if (v_type != VAR_LIST
1816 && v_type != VAR_TUPLE
1817 && v_type != VAR_DICT
1818 && v_type != VAR_BLOB
1819 && v_type != VAR_OBJECT
1820 && v_type != VAR_CLASS)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001821 {
1822 if (!quiet)
1823 semsg(_(e_index_not_allowed_after_str_str),
1824 vartype_name(v_type), name);
1825 return FALSE;
1826 }
1827
1828 return TRUE;
1829}
1830
1831/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001832 * Get the lval of a list/tuple/dict/blob/object/class subitem starting at "p".
1833 * Loop until no more [idx] or .key is following.
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001834 *
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001835 * If "rettv" is not NULL it points to the value to be assigned.
1836 * "unlet" is TRUE for ":unlet".
1837 *
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001838 * Returns a pointer to the character after the subscript on success or NULL on
1839 * failure.
1840 */
1841 static char_u *
1842get_lval_subscript(
1843 lval_T *lp,
1844 char_u *p,
1845 char_u *name,
1846 typval_T *rettv,
1847 hashtab_T *ht,
1848 dictitem_T *v,
1849 int unlet,
1850 int flags, // GLV_ values
1851 class_T *cl_exec)
1852{
1853 int vim9script = in_vim9script();
1854 int quiet = flags & GLV_QUIET;
1855 char_u *key = NULL;
1856 int len;
1857 typval_T var1;
1858 typval_T var2;
1859 int empty1 = FALSE;
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001860 int rc = FAIL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001861
1862 /*
1863 * Loop until no more [idx] or .key is following.
1864 */
1865 var1.v_type = VAR_UNKNOWN;
1866 var2.v_type = VAR_UNKNOWN;
1867
1868 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1869 {
1870 vartype_T v_type = lp->ll_tv->v_type;
1871
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001872 if (*p == '.' && !dot_allowed_after_type(name, v_type, quiet))
1873 goto done;
1874
1875 if (*p == '[' && !bracket_allowed_after_type(name, v_type, quiet))
1876 goto done;
1877
1878 if (!index_allowed_after_type(name, v_type, quiet))
1879 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001880
1881 // A NULL list/blob works like an empty list/blob, allocate one now.
1882 int r = OK;
1883 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1884 r = rettv_list_alloc(lp->ll_tv);
1885 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1886 r = rettv_blob_alloc(lp->ll_tv);
1887 if (r == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001888 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001889
1890 if (lp->ll_range)
1891 {
1892 if (!quiet)
1893 emsg(_(e_slice_must_come_last));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001894 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001895 }
1896#ifdef LOG_LOCKVAR
1897 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1898 vartype_name(v_type));
1899#endif
1900
1901 if (vim9script && lp->ll_valtype == NULL
1902 && v != NULL
1903 && lp->ll_tv == &v->di_tv
1904 && ht != NULL && ht == get_script_local_ht())
1905 {
1906 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1907
1908 // Vim9 script local variable: get the type
1909 if (sv != NULL)
1910 {
1911 lp->ll_valtype = sv->sv_type;
1912#ifdef LOG_LOCKVAR
1913 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1914 vartype_name(lp->ll_valtype->tt_type));
1915#endif
1916 }
1917 }
1918
1919 len = -1;
1920 if (*p == '.')
1921 {
1922 key = p + 1;
1923
1924 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1925 ;
1926 if (len == 0)
1927 {
1928 if (!quiet)
1929 emsg(_(e_cannot_use_empty_key_for_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001930 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001931 }
1932 p = key + len;
1933 }
1934 else
1935 {
1936 // Get the index [expr] or the first index [expr: ].
1937 p = skipwhite(p + 1);
1938 if (*p == ':')
1939 empty1 = TRUE;
1940 else
1941 {
1942 empty1 = FALSE;
1943 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001944 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001945 if (tv_get_string_chk(&var1) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001946 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001947 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001948 p = skipwhite(p);
1949 }
1950
1951 // Optionally get the second index [ :expr].
1952 if (*p == ':')
1953 {
1954 if (v_type == VAR_DICT)
1955 {
1956 if (!quiet)
1957 emsg(_(e_cannot_slice_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001958 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001959 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001960 if (v_type == VAR_TUPLE)
1961 {
1962 if (!quiet)
1963 emsg(_(e_cannot_slice_tuple));
1964 goto done;
1965 }
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001966 if (rettv != NULL
1967 && !(rettv->v_type == VAR_LIST
1968 && rettv->vval.v_list != NULL)
1969 && !(rettv->v_type == VAR_BLOB
1970 && rettv->vval.v_blob != NULL))
1971 {
1972 if (!quiet)
1973 emsg(_(e_slice_requires_list_or_blob_value));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001974 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001975 }
1976 p = skipwhite(p + 1);
1977 if (*p == ']')
1978 lp->ll_empty2 = TRUE;
1979 else
1980 {
1981 lp->ll_empty2 = FALSE;
1982 // recursive!
1983 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001984 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001985 if (tv_get_string_chk(&var2) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001986 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001987 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001988 }
1989 lp->ll_range = TRUE;
1990 }
1991 else
1992 lp->ll_range = FALSE;
1993
1994 if (*p != ']')
1995 {
1996 if (!quiet)
1997 emsg(_(e_missing_closing_square_brace));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001998 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001999 }
2000
2001 // Skip to past ']'.
2002 ++p;
2003 }
2004#ifdef LOG_LOCKVAR
2005 if (len == -1)
2006 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
2007 empty1 ? ":" : (char*)tv_get_string(&var1));
2008 else
2009 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
2010#endif
2011
2012 if (v_type == VAR_DICT)
2013 {
2014 glv_status_T glv_status;
2015
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02002016 glv_status = get_lval_dict_item(lp, name, key, len, &p, &var1,
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002017 flags, unlet, rettv);
2018 if (glv_status == GLV_FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002019 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002020 if (glv_status == GLV_STOP)
2021 break;
2022 }
2023 else if (v_type == VAR_BLOB)
2024 {
2025 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002026 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002027
2028 break;
2029 }
2030 else if (v_type == VAR_LIST)
2031 {
2032 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002033 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002034 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002035 else if (v_type == VAR_TUPLE)
2036 {
2037 if (get_lval_tuple(lp, &var1, quiet) == FAIL)
2038 goto done;
2039 }
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002040 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
2041 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02002042 if (get_lval_class_or_obj(lp, key, p, v_type, cl_exec, flags,
2043 quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002044 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002045 }
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002046
2047 clear_tv(&var1);
2048 clear_tv(&var2);
2049 var1.v_type = VAR_UNKNOWN;
2050 var2.v_type = VAR_UNKNOWN;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002051 }
2052
Yegappan Lakshmanan038be272025-03-26 18:46:21 +01002053 if (lp->ll_tuple != NULL && (flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002054 {
2055 if (!quiet)
2056 emsg(_(e_tuple_is_immutable));
2057 goto done;
2058 }
2059
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002060 rc = OK;
2061
2062done:
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002063 clear_tv(&var1);
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002064 clear_tv(&var2);
2065 return rc == OK ? p : NULL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002066}
2067
2068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 * Get an lval: variable, Dict item or List item that can be assigned a value
2070 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2071 * "name.key", "name.key[expr]" etc.
2072 * Indexing only works if "name" is an existing List or Dictionary.
2073 * "name" points to the start of the name.
2074 * If "rettv" is not NULL it points to the value to be assigned.
2075 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2076 * wrong; must end in space or cmd separator.
2077 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002078 * flags:
2079 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002080 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002081 * GLV_NO_AUTOLOAD: do not use script autoloading
2082 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002088get_lval(
2089 char_u *name,
2090 typval_T *rettv,
2091 lval_T *lp,
2092 int unlet,
2093 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 int flags, // GLV_ values
2095 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 char_u *expr_start, *expr_end;
2099 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002100 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01002101 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002102 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02002103 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00002104 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02002105 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106
Ernie Raelee865f32023-09-29 19:53:55 +02002107#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002108 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002109 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02002110 else
Ernie Rael4c8da022023-10-11 21:35:11 +02002111 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
2112 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02002113 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02002114 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02002115 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02002116#endif
2117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002118 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02002119 CLEAR_POINTER(lp);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002120 ga_init2(&lp->ll_type_list, sizeof(type_T *), 10);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002122 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002124 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02002126 lp->ll_name_end = find_name_end(name, NULL, NULL,
2127 FNE_INCL_BR | fne_flags);
2128 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 }
2130
Bram Moolenaara749a422022-02-12 19:52:25 +00002131 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00002132 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00002133 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
2134 {
2135 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
2136 return NULL;
2137 }
2138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002140 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 if (expr_start != NULL)
2143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01002145 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 && *p != '[' && *p != '.')
2147 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002148 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
2150 }
2151
2152 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2153 if (lp->ll_exp_name == NULL)
2154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002155 // Report an invalid expression in braces, unless the
2156 // expression evaluation has been cancelled due to an
2157 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 if (!aborting() && !quiet)
2159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002160 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002161 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 return NULL;
2163 }
2164 }
2165 lp->ll_name = lp->ll_exp_name;
Christian Brabandt06774a22025-03-26 19:25:57 +01002166 if (lp->ll_name != NULL)
2167 lp->ll_name_end = lp->ll_name + STRLEN(lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 }
2169 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 lp->ll_name = name;
2172
Bram Moolenaar4525a572022-02-13 11:57:33 +00002173 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002175 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002176 // However, "g:[key]" is indexing a dictionary.
2177 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002178 {
2179 --p;
2180 lp->ll_name_end = p;
2181 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002182 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002183 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002184 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02002185
Bram Moolenaar9510d222022-09-11 15:14:05 +01002186 if (is_scoped_variable(name))
2187 {
2188 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2189 return NULL;
2190 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002191 if (VIM_ISWHITE(*p))
2192 {
2193 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2194 return NULL;
2195 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002196 if (tp == p + 1 && !quiet)
2197 {
2198 semsg(_(e_white_space_required_after_str_str), ":", p);
2199 return NULL;
2200 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002201 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002202 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002203 semsg(_(e_using_type_not_in_script_context_str), p);
2204 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002205 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002206 if (vim9script && (flags & GLV_NO_DECL) &&
2207 !(flags & GLV_FOR_LOOP))
2208 {
2209 // Using a type and not in a "var" declaration.
2210 semsg(_(e_trailing_characters_str), p);
2211 return NULL;
2212 }
2213
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002214
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002215 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002216 lp->ll_type = parse_type(&tp,
2217 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2218 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002219 if (lp->ll_type == NULL && !quiet)
2220 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002221 lp->ll_name_end = tp;
2222 }
Ernie Raelee865f32023-09-29 19:53:55 +02002223 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 }
2225 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002226 if (lp->ll_name == NULL)
2227 return p;
2228
Bram Moolenaar62aec932022-01-29 21:45:34 +00002229 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002230 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002231 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002232 if (import != NULL)
2233 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002234 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002235 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002236 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002237 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002238 }
2239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240
Ernie Rael0f058d12023-10-14 11:25:04 +02002241 // Without [idx] or .key we are done.
2242 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002243 {
2244 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002245 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002247 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248
Ernie Rael0f058d12023-10-14 11:25:04 +02002249 if (vim9script && lval_root != NULL)
2250 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002251 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002252 {
2253 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002254 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002255 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002256 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002257 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002258 {
2259 cc = *p;
2260 *p = NUL;
2261 // When we would write to the variable pass &ht and prevent autoload.
2262 writing = !(flags & GLV_READ_ONLY);
2263 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002264 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002265 if (v == NULL && !quiet)
2266 semsg(_(e_undefined_variable_str), lp->ll_name);
2267 *p = cc;
2268 if (v == NULL)
2269 return NULL;
2270 lp->ll_tv = &v->di_tv;
2271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272
Bram Moolenaar4525a572022-02-13 11:57:33 +00002273 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002274 {
2275 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002276 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002277 return NULL;
2278 }
2279
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002280 // If the next character is a "." or a "[", then process the subitem.
2281 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2282 if (p == NULL)
2283 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002285 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2286 {
2287 where_T where = WHERE_INIT;
2288
2289 // In a vim9 script, do type check and make sure the variable is
2290 // writable.
2291 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2292 return NULL;
2293 }
2294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 return p;
2297}
2298
2299/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002300 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304{
2305 vim_free(lp->ll_exp_name);
2306 vim_free(lp->ll_newkey);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002307 clear_type_list(&lp->ll_type_list);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308}
2309
2310/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002311 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002313 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2314 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002317set_var_lval(
2318 lval_T *lp,
2319 char_u *endp,
2320 typval_T *rettv,
2321 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002322 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2323 char_u *op,
2324 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325{
2326 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328
2329 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002331 cc = *endp;
2332 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002333 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002334 return;
2335
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336 if (lp->ll_blob != NULL)
2337 {
2338 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002339
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002340 if (op != NULL && *op != '=')
2341 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002342 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002343 return;
2344 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002345 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002346 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002347
2348 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2349 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002350 if (lp->ll_empty2)
2351 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2352
Bram Moolenaar68452172021-04-12 21:21:02 +02002353 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2354 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002355 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002356 }
2357 else
2358 {
2359 val = (int)tv_get_number_chk(rettv, &error);
2360 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002361 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002362 }
2363 }
2364 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002366 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002368 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2369 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002370 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002371 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002372 *endp = cc;
2373 return;
2374 }
2375
Bram Moolenaarff697e62019-02-12 22:28:33 +01002376 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002377 di = NULL;
Christian Brabandt06774a22025-03-26 19:25:57 +01002378 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002379 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002380 {
Ernie Raele75fde62023-12-21 17:18:54 +01002381 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002382 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002383 clear_tv(&tv);
2384 return;
2385 }
2386
Bram Moolenaar79518e22017-02-17 16:31:35 +01002387 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002388 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2389 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002390 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002391 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002392 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002393 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002396 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002397 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002398 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2399 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002400 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002401 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002402 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002403 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002404 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002406 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002407 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002408 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002409 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 else if (lp->ll_range)
2411 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002412 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2413 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002414 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002415 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002416 return;
2417 }
2418
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002419 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2420 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 }
2422 else
2423 {
2424 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002425 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002427 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2428 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002429 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002430 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002431 return;
2432 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002433
2434 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002435 && check_typval_arg_type(lp->ll_valtype, rettv,
2436 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002437 return;
2438
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (lp->ll_newkey != NULL)
2440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 if (op != NULL && *op != '=')
2442 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002443 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 return;
2445 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002446 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2447 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002448 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002450 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002451 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 if (di == NULL)
2453 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002454 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2455 {
2456 vim_free(di);
2457 return;
2458 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002460 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 else if (op != NULL && *op != '=')
2462 {
2463 tv_op(lp->ll_tv, rettv, op);
2464 return;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 /*
2470 * Assign the value to the variable or list item.
2471 */
2472 if (copy)
2473 copy_tv(rettv, lp->ll_tv);
2474 else
2475 {
2476 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002477 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481}
2482
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002484 * Handle "blob1 += blob2".
2485 * Returns OK or FAIL.
2486 */
2487 static int
2488tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2489{
2490 if (*op != '+' || tv2->v_type != VAR_BLOB)
2491 return FAIL;
2492
2493 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002494 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002495 return OK;
2496
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002497 if (tv1->vval.v_blob == NULL)
2498 {
2499 tv1->vval.v_blob = tv2->vval.v_blob;
2500 ++tv1->vval.v_blob->bv_refcount;
2501 return OK;
2502 }
2503
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002504 blob_T *b1 = tv1->vval.v_blob;
2505 blob_T *b2 = tv2->vval.v_blob;
2506 int len = blob_len(b2);
2507
2508 for (int i = 0; i < len; i++)
2509 ga_append(&b1->bv_ga, blob_get(b2, i));
2510
2511 return OK;
2512}
2513
2514/*
2515 * Handle "list1 += list2".
2516 * Returns OK or FAIL.
2517 */
2518 static int
2519tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2520{
2521 if (*op != '+' || tv2->v_type != VAR_LIST)
2522 return FAIL;
2523
2524 // List += List
2525 if (tv2->vval.v_list == NULL)
2526 return OK;
2527
2528 if (tv1->vval.v_list == NULL)
2529 {
2530 tv1->vval.v_list = tv2->vval.v_list;
2531 ++tv1->vval.v_list->lv_refcount;
2532 }
2533 else
2534 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2535
2536 return OK;
2537}
2538
2539/*
2540 * Handle number operations:
2541 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2542 *
2543 * Returns OK or FAIL.
2544 */
2545 static int
2546tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2547{
2548 varnumber_T n;
2549 int failed = FALSE;
2550
2551 n = tv_get_number(tv1);
2552 if (tv2->v_type == VAR_FLOAT)
2553 {
2554 float_T f = n;
2555
2556 if (*op == '%')
2557 return FAIL;
2558 switch (*op)
2559 {
2560 case '+': f += tv2->vval.v_float; break;
2561 case '-': f -= tv2->vval.v_float; break;
2562 case '*': f *= tv2->vval.v_float; break;
2563 case '/': f /= tv2->vval.v_float; break;
2564 }
2565 clear_tv(tv1);
2566 tv1->v_type = VAR_FLOAT;
2567 tv1->vval.v_float = f;
2568 }
2569 else
2570 {
2571 switch (*op)
2572 {
2573 case '+': n += tv_get_number(tv2); break;
2574 case '-': n -= tv_get_number(tv2); break;
2575 case '*': n *= tv_get_number(tv2); break;
2576 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2577 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2578 }
2579 clear_tv(tv1);
2580 tv1->v_type = VAR_NUMBER;
2581 tv1->vval.v_number = n;
2582 }
2583
2584 return failed ? FAIL : OK;
2585}
2586
2587/*
2588 * Handle "str1 .= str2"
2589 * Returns OK or FAIL.
2590 */
2591 static int
2592tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2593{
2594 char_u numbuf[NUMBUFLEN];
2595 char_u *s;
2596
2597 if (tv2->v_type == VAR_FLOAT)
2598 return FAIL;
2599
2600 // str .= str
2601 s = tv_get_string(tv1);
2602 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2603 clear_tv(tv1);
2604 tv1->v_type = VAR_STRING;
2605 tv1->vval.v_string = s;
2606
2607 return OK;
2608}
2609
2610/*
2611 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2612 * and "tv1 .= tv2"
2613 * Returns OK or FAIL.
2614 */
2615 static int
2616tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2617{
2618 if (tv2->v_type == VAR_LIST)
2619 return FAIL;
2620
2621 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2622 return tv_op_number(tv1, tv2, op);
2623
2624 return tv_op_string(tv1, tv2, op);
2625}
2626
2627/*
2628 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2629 * Returns OK or FAIL.
2630 */
2631 static int
2632tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2633{
2634 float_T f;
2635
2636 if (*op == '%' || *op == '.'
2637 || (tv2->v_type != VAR_FLOAT
2638 && tv2->v_type != VAR_NUMBER
2639 && tv2->v_type != VAR_STRING))
2640 return FAIL;
2641
2642 if (tv2->v_type == VAR_FLOAT)
2643 f = tv2->vval.v_float;
2644 else
2645 f = tv_get_number(tv2);
2646 switch (*op)
2647 {
2648 case '+': tv1->vval.v_float += f; break;
2649 case '-': tv1->vval.v_float -= f; break;
2650 case '*': tv1->vval.v_float *= f; break;
2651 case '/': tv1->vval.v_float /= f; break;
2652 }
2653
2654 return OK;
2655}
2656
2657/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002658 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2659 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 * Returns OK or FAIL.
2661 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002663tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002665 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002666 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002667 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2668 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2669 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002671 semsg(_(e_wrong_variable_type_for_str_equal), op);
2672 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 }
2674
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002675 int retval = FAIL;
2676 switch (tv1->v_type)
2677 {
2678 case VAR_UNKNOWN:
2679 case VAR_ANY:
2680 case VAR_VOID:
2681 case VAR_DICT:
2682 case VAR_FUNC:
2683 case VAR_PARTIAL:
2684 case VAR_BOOL:
2685 case VAR_SPECIAL:
2686 case VAR_JOB:
2687 case VAR_CHANNEL:
2688 case VAR_INSTR:
2689 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002690 case VAR_CLASS:
2691 case VAR_TYPEALIAS:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002692 case VAR_TUPLE:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002693 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002694
2695 case VAR_BLOB:
2696 retval = tv_op_blob(tv1, tv2, op);
2697 break;
2698
2699 case VAR_LIST:
2700 retval = tv_op_list(tv1, tv2, op);
2701 break;
2702
2703 case VAR_NUMBER:
2704 case VAR_STRING:
2705 retval = tv_op_nr_or_string(tv1, tv2, op);
2706 break;
2707
2708 case VAR_FLOAT:
2709 retval = tv_op_float(tv1, tv2, op);
2710 break;
2711 }
2712
2713 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002714 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002715
2716 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717}
2718
2719/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002720 * Evaluate the expression used in a ":for var in expr" command.
2721 * "arg" points to "var".
2722 * Set "*errp" to TRUE for an error, FALSE otherwise;
2723 * Return a pointer that holds the info. Null when there is an error.
2724 */
2725 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002726eval_for_line(
2727 char_u *arg,
2728 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002729 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002730 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731{
Bram Moolenaar33570922005-01-25 22:26:29 +00002732 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002733 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002735 typval_T tv;
2736 list_T *l;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002737 tuple_T *tuple;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002738 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002740 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002742 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743 if (fi == NULL)
2744 return NULL;
2745
Bram Moolenaar404557e2021-07-05 21:41:48 +02002746 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2747 &fi->fi_semicolon, FALSE);
2748 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749 return fi;
2750
Bram Moolenaar404557e2021-07-05 21:41:48 +02002751 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002752 if (expr[0] != 'i' || expr[1] != 'n'
2753 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002755 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2756 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2757 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002758 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759 return fi;
2760 }
2761
2762 if (skip)
2763 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002764 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2765 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766 {
2767 *errp = FALSE;
2768 if (!skip)
2769 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002770 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002771 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002772 l = tv.vval.v_list;
2773 if (l == NULL)
2774 {
2775 // a null list is like an empty list: do nothing
2776 clear_tv(&tv);
2777 }
2778 else
2779 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002781 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002783 // No need to increment the refcount, it's already set for
2784 // the list being used in "tv".
2785 fi->fi_list = l;
2786 list_add_watch(l, &fi->fi_lw);
2787 fi->fi_lw.lw_item = l->lv_first;
2788 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002789 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002790 else if (tv.v_type == VAR_TUPLE)
2791 {
2792 tuple = tv.vval.v_tuple;
2793 if (tuple == NULL)
2794 {
2795 // a null tuple is like an empty tuple: do nothing
2796 clear_tv(&tv);
2797 }
2798 else
2799 {
2800 // No need to increment the refcount, it's already set for
2801 // the tuple being used in "tv".
2802 fi->fi_tuple = tuple;
2803 fi->fi_tuple_idx = 0;
2804 }
2805 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002806 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002807 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002808 fi->fi_bi = 0;
2809 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002810 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002811 typval_T btv;
2812
2813 // Make a copy, so that the iteration still works when the
2814 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002816 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002817 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002818 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002819 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002820 else if (tv.v_type == VAR_STRING)
2821 {
2822 fi->fi_byte_idx = 0;
2823 fi->fi_string = tv.vval.v_string;
2824 tv.vval.v_string = NULL;
2825 if (fi->fi_string == NULL)
2826 fi->fi_string = vim_strsave((char_u *)"");
2827 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 else
2829 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002830 emsg(_(e_string_list_tuple_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002831 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 }
2833 }
2834 }
2835 if (skip)
2836 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002837 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002838
2839 return fi;
2840}
2841
2842/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002843 * Used when looping over a :for line, skip the "in expr" part.
2844 */
2845 void
2846skip_for_lines(void *fi_void, evalarg_T *evalarg)
2847{
2848 forinfo_T *fi = (forinfo_T *)fi_void;
2849 int i;
2850
2851 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002852 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002853}
2854
2855/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856 * Use the first item in a ":for" list. Advance to the next.
2857 * Assign the values to the variable (list). "arg" points to the first one.
2858 * Return TRUE when a valid item was found, FALSE when at end of list or
2859 * something wrong.
2860 */
2861 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002862next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002864 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002866 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002867 ? (ASSIGN_FINAL
2868 // first round: error if variable exists
2869 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002870 | ASSIGN_NO_MEMBER_TYPE
2871 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002872 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002874 int skip_assign = in_vim9script() && arg[0] == '_'
2875 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002877 if (fi->fi_blob != NULL)
2878 {
2879 typval_T tv;
2880
2881 if (fi->fi_bi >= blob_len(fi->fi_blob))
2882 return FALSE;
2883 tv.v_type = VAR_NUMBER;
2884 tv.v_lock = VAR_FIXED;
2885 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2886 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002887 if (skip_assign)
2888 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002889 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002890 fi->fi_varcount, flag, NULL) == OK;
2891 }
2892
2893 if (fi->fi_string != NULL)
2894 {
2895 typval_T tv;
2896 int len;
2897
2898 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2899 if (len == 0)
2900 return FALSE;
2901 tv.v_type = VAR_STRING;
2902 tv.v_lock = VAR_FIXED;
2903 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2904 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002905 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002906 if (skip_assign)
2907 result = TRUE;
2908 else
2909 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002910 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002911 vim_free(tv.vval.v_string);
2912 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002913 }
2914
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002915 if (fi->fi_tuple != NULL)
2916 {
2917 typval_T tv;
2918
2919 if (fi->fi_tuple_idx >= TUPLE_LEN(fi->fi_tuple))
2920 return FALSE;
2921
2922 copy_tv(TUPLE_ITEM(fi->fi_tuple, fi->fi_tuple_idx), &tv);
2923 ++fi->fi_tuple_idx;
2924 ++fi->fi_bi;
2925 if (skip_assign)
2926 return TRUE;
2927 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2928 fi->fi_varcount, flag, NULL) == OK;
2929 }
2930
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 item = fi->fi_lw.lw_item;
2932 if (item == NULL)
2933 result = FALSE;
2934 else
2935 {
2936 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002937 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002938 if (skip_assign)
2939 result = TRUE;
2940 else
2941 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002942 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 }
2944 return result;
2945}
2946
2947/*
2948 * Free the structure used to store info used by ":for".
2949 */
2950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952{
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002954
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002955 if (fi == NULL)
2956 return;
2957 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002960 list_unref(fi->fi_list);
2961 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002962 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002963 blob_unref(fi->fi_blob);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002964 else if (fi->fi_tuple != NULL)
2965 tuple_unref(fi->fi_tuple);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002966 else
2967 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968 vim_free(fi);
2969}
2970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972set_context_for_expression(
2973 expand_T *xp,
2974 char_u *arg,
2975 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002977 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002981 if (cmdidx == CMD_let || cmdidx == CMD_var
2982 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983 {
2984 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002985 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002987 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002988 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002989 {
2990 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002991 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002992 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993 break;
2994 }
2995 return;
2996 }
2997 }
2998 else
2999 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3000 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 while ((xp->xp_pattern = vim_strpbrk(arg,
3002 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3003 {
3004 c = *xp->xp_pattern;
3005 if (c == '&')
3006 {
3007 c = xp->xp_pattern[1];
3008 if (c == '&')
3009 {
3010 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003011 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003016 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3017 xp->xp_pattern += 2;
3018
3019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021 else if (c == '$')
3022 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003023 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 xp->xp_context = EXPAND_ENV_VARS;
3025 }
3026 else if (c == '=')
3027 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003028 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 xp->xp_context = EXPAND_EXPRESSION;
3030 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003031 else if (c == '#'
3032 && xp->xp_context == EXPAND_EXPRESSION)
3033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003034 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02003035 break;
3036 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003037 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 && xp->xp_context == EXPAND_FUNCTIONS
3039 && vim_strchr(xp->xp_pattern, '(') == NULL)
3040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 break;
3043 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003044 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003046 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
3048 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3049 if (c == '\\' && xp->xp_pattern[1] != NUL)
3050 ++xp->xp_pattern;
3051 xp->xp_context = EXPAND_NOTHING;
3052 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003053 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3057 /* skip */ ;
3058 xp->xp_context = EXPAND_NOTHING;
3059 }
3060 else if (c == '|')
3061 {
3062 if (xp->xp_pattern[1] == '|')
3063 {
3064 ++xp->xp_pattern;
3065 xp->xp_context = EXPAND_EXPRESSION;
3066 }
3067 else
3068 xp->xp_context = EXPAND_COMMANDS;
3069 }
3070 else
3071 xp->xp_context = EXPAND_EXPRESSION;
3072 }
3073 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003074 // Doesn't look like something valid, expand as an expression
3075 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 arg = xp->xp_pattern;
3078 if (*arg != NUL)
3079 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3080 /* skip */ ;
3081 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01003082
3083 // ":exe one two" completes "two"
3084 if ((cmdidx == CMD_execute
3085 || cmdidx == CMD_echo
3086 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01003087 || cmdidx == CMD_echomsg
3088 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01003089 && xp->xp_context == EXPAND_EXPRESSION)
3090 {
3091 for (;;)
3092 {
3093 char_u *n = skiptowhite(arg);
3094
3095 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
3096 break;
3097 arg = skipwhite(n);
3098 }
3099 }
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 xp->xp_pattern = arg;
3102}
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003105 * Return TRUE if "pat" matches "text".
3106 * Does not use 'cpo' and always uses 'magic'.
3107 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003108 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003109pattern_match(char_u *pat, char_u *text, int ic)
3110{
3111 int matches = FALSE;
3112 char_u *save_cpo;
3113 regmatch_T regmatch;
3114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003116 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01003117 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003118 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3119 if (regmatch.regprog != NULL)
3120 {
3121 regmatch.rm_ic = ic;
3122 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3123 vim_regfree(regmatch.regprog);
3124 }
3125 p_cpo = save_cpo;
3126 return matches;
3127}
3128
3129/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003130 * Handle a name followed by "(". Both for just "name(arg)" and for
3131 * "expr->name(arg)".
3132 * Returns OK or FAIL.
3133 */
3134 static int
3135eval_func(
3136 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02003137 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003138 char_u *name,
3139 int name_len,
3140 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003141 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003142 typval_T *basetv) // "expr" for "expr->name(arg)"
3143{
Bram Moolenaar32e35112020-05-14 22:41:15 +02003144 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003145 char_u *s = name;
3146 int len = name_len;
3147 partial_T *partial;
3148 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003149 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003150 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003151
3152 if (!evaluate)
3153 check_vars(s, len);
3154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003155 // If "s" is the name of a variable of type VAR_FUNC
3156 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003157 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003158 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003160 // Need to make a copy, in case evaluating the arguments makes
3161 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003162 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01003163 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003164 ret = FAIL;
3165 else
3166 {
3167 funcexe_T funcexe;
3168
3169 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02003170 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003171 funcexe.fe_firstline = curwin->w_cursor.lnum;
3172 funcexe.fe_lastline = curwin->w_cursor.lnum;
3173 funcexe.fe_evaluate = evaluate;
3174 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003175 if (partial != NULL)
3176 {
3177 funcexe.fe_object = partial->pt_obj;
3178 if (funcexe.fe_object != NULL)
3179 ++funcexe.fe_object->obj_refcount;
3180 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003181 funcexe.fe_basetv = basetv;
3182 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003183 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003184 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003185 }
3186 vim_free(s);
3187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003188 // If evaluate is FALSE rettv->v_type was not set in
3189 // get_func_tv, but it's needed in handle_subscript() to parse
3190 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003191 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3192 {
3193 rettv->vval.v_string = NULL;
3194 rettv->v_type = VAR_FUNC;
3195 }
3196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003197 // Stop the expression evaluation when immediately
3198 // aborting on error, or when an interrupt occurred or
3199 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003200 if (evaluate && aborting())
3201 {
3202 if (ret == OK)
3203 clear_tv(rettv);
3204 ret = FAIL;
3205 }
3206 return ret;
3207}
3208
3209/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003210 * After a NL, skip over empty lines and comment-only lines.
3211 */
3212 static char_u *
3213newline_skip_comments(char_u *arg)
3214{
3215 char_u *p = arg + 1;
3216
3217 for (;;)
3218 {
3219 p = skipwhite(p);
3220
3221 if (*p == NUL)
3222 break;
3223 if (vim9_comment_start(p))
3224 {
3225 char_u *nl = vim_strchr(p, NL);
3226
3227 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003228 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003229 p = nl;
3230 }
3231 if (*p != NL)
3232 break;
3233 ++p; // skip another NL
3234 }
3235 return p;
3236}
3237
3238/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003239 * Get the next line source line without advancing. But do skip over comment
3240 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003241 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003242 */
3243 static char_u *
3244getline_peek_skip_comments(evalarg_T *evalarg)
3245{
3246 for (;;)
3247 {
3248 char_u *next = getline_peek(evalarg->eval_getline,
3249 evalarg->eval_cookie);
3250 char_u *p;
3251
3252 if (next == NULL)
3253 break;
3254 p = skipwhite(next);
3255 if (*p != NUL && !vim9_comment_start(p))
3256 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003257 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003258 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003259 }
3260 return NULL;
3261}
3262
3263/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003264 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3265 * comment) and there is a next line, return the next line (skipping blanks)
3266 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003267 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3268 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003269 * "arg" must point somewhere inside a line, not at the start.
3270 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003271 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003272eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3273{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003274 char_u *p = skipwhite(arg);
3275
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003276 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003277 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003278 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003279 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3280 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003281 && (*p == NUL || *p == NL
3282 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003283 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003284 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003285
Bram Moolenaare442d592022-05-05 12:20:28 +01003286 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003287 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003288 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003289 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003290 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003291 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292
Bram Moolenaar9d489562020-07-30 20:08:50 +02003293 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003294 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003295 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003296 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003297 }
3298 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003299 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003300}
3301
3302/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003303 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003304 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003305 *
3306 * If "arg" is not NULL, then the caller should assign the return value to
3307 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003308 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003309 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003310eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003311{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003312 garray_T *gap = &evalarg->eval_ga;
3313 char_u *line;
3314
Bram Moolenaar39be4982022-05-06 21:51:50 +01003315 if (arg != NULL)
3316 {
3317 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003318 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003319 // Truncate before a trailing comment, so that concatenating the lines
3320 // won't turn the rest into a comment.
3321 if (*skipwhite(arg) == '#')
3322 *arg = NUL;
3323 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003324
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003325 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003326 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3327 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003328 else
3329 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003330 if (line == NULL)
3331 return NULL;
3332
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003333 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003334 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3335 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003336 char_u *p = skipwhite(line);
3337
3338 // Going to concatenate the lines after parsing. For an empty or
3339 // comment line use an empty string.
3340 if (*p == NUL || vim9_comment_start(p))
3341 {
3342 vim_free(line);
3343 line = vim_strsave((char_u *)"");
3344 }
3345
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003346 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3347 ++gap->ga_len;
3348 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003349 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003350 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003351 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003352 evalarg->eval_tofree = line;
3353 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003354
3355 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003356 // line. The caller assigns the return value to "arg".
3357 // If "arg" is NULL, then the return value is discarded. In that case,
3358 // "arg" still points to the previous line. So don't reset
3359 // "eval_using_cmdline".
3360 if (arg != NULL)
3361 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003362 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003363}
3364
Bram Moolenaare6b53242020-07-01 17:28:33 +02003365/*
3366 * Call eval_next_non_blank() and get the next line if needed.
3367 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003368 char_u *
3369skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3370{
3371 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003372 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003373
Bram Moolenaar962d7212020-07-04 14:15:00 +02003374 if (evalarg == NULL)
3375 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003376 eval_next_non_blank(p, evalarg, &getnext);
3377 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003378 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003379 return p;
3380}
3381
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003382/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003383 * The "eval" functions have an "evalarg" argument: When NULL or
3384 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3385 * parsed but not executed. The functions may return OK, but the rettv will be
3386 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
3388
3389/*
3390 * Handle zero level expression.
3391 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003392 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003393 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003394 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 * Return OK or FAIL.
3396 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003398eval0(
3399 char_u *arg,
3400 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003401 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003402 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003404 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3405}
3406
3407/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003408 * If "arg" is a simple function call without arguments then call it and return
3409 * the result. Otherwise return NOTDONE.
3410 */
3411 int
3412may_call_simple_func(
3413 char_u *arg,
3414 typval_T *rettv)
3415{
3416 char_u *parens = (char_u *)strstr((char *)arg, "()");
3417 int r = NOTDONE;
3418
3419 // If the expression is "FuncName()" then we can skip a lot of overhead.
3420 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3421 {
3422 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3423
3424 if (to_name_end(p, TRUE) == parens)
zeertzjqc1ed7882024-08-01 22:46:54 +02003425 r = call_simple_func(arg, (size_t)(parens - arg), rettv);
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003426 }
3427 return r;
3428}
3429
3430/*
3431 * Handle zero level expression with optimization for a simple function call.
3432 * Same arguments and return value as eval0().
3433 */
3434 int
3435eval0_simple_funccal(
3436 char_u *arg,
3437 typval_T *rettv,
3438 exarg_T *eap,
3439 evalarg_T *evalarg)
3440{
3441 int r = may_call_simple_func(arg, rettv);
3442
3443 if (r == NOTDONE)
3444 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3445 return r;
3446}
3447
3448/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003449 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3450 * expression and don't check what comes after the expression.
3451 */
3452 int
3453eval0_retarg(
3454 char_u *arg,
3455 typval_T *rettv,
3456 exarg_T *eap,
3457 evalarg_T *evalarg,
3458 char_u **retarg)
3459{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int ret;
3461 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003462 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003463 int did_emsg_before = did_emsg;
3464 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003465 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003466 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003469 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003470
Bram Moolenaar79481362022-06-27 20:15:10 +01003471 if (ret != FAIL)
3472 {
3473 expr_end = p;
3474 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003475
Bram Moolenaar79481362022-06-27 20:15:10 +01003476 // In Vim9 script a command block is not split at NL characters for
3477 // commands using an expression argument. Skip over a '#' comment to
3478 // check for a following NL. Require white space before the '#'.
3479 if (in_vim9script() && p > expr_end && retarg == NULL)
3480 while (*p == '#')
3481 {
3482 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003483
Bram Moolenaar79481362022-06-27 20:15:10 +01003484 if (nl == NULL)
3485 break;
3486 p = skipwhite(nl + 1);
3487 if (eap != NULL && *p != NUL)
3488 eap->nextcmd = p;
3489 check_for_end = FALSE;
3490 }
3491
3492 if (check_for_end)
3493 end_error = !ends_excmd2(arg, p);
3494 }
3495
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003496 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /*
3501 * Report the invalid expression unless the expression evaluation has
3502 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003503 * exception, or we already gave a more specific error.
3504 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003506 if (!aborting()
3507 && did_emsg == did_emsg_before
3508 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003509 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003510 {
3511 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003512 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003513 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003514 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003515 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003516
zeertzjqf2588b62023-05-05 17:22:35 +01003517 if (eap != NULL && p != NULL)
3518 {
3519 // Some of the expression may not have been consumed.
3520 // Only execute a next command if it cannot be a "||" operator.
3521 // The next command may be "catch".
3522 char_u *nextcmd = check_nextcmd(p);
3523 if (nextcmd != NULL && *nextcmd != '|')
3524 eap->nextcmd = nextcmd;
3525 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003526 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003528
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003529 if (retarg != NULL)
3530 *retarg = p;
3531 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003532 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return ret;
3535}
3536
3537/*
3538 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003539 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003540 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 *
3542 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003543 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003545 * Note: "rettv.v_lock" is not set.
3546 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * Return OK or FAIL.
3548 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003549 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003550eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003552 char_u *p;
3553 int getnext;
3554
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003555 CLEAR_POINTER(rettv);
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /*
3558 * Get the first variable.
3559 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003560 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 return FAIL;
3562
Bram Moolenaar793648f2020-06-26 21:28:25 +02003563 p = eval_next_non_blank(*arg, evalarg, &getnext);
3564 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003566 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003567 int result;
3568 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003569 evalarg_T *evalarg_used = evalarg;
3570 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003571 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003572 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003573 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003574
3575 if (evalarg == NULL)
3576 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003577 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003578 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003579 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003580 orig_flags = evalarg_used->eval_flags;
3581 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003582
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003583 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003584 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003585 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003586 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003587 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003588 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003589 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003590 clear_tv(rettv);
3591 return FAIL;
3592 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003593 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003594 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003597 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599 int error = FALSE;
3600
Bram Moolenaar13106602020-10-04 16:06:05 +02003601 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003602 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003603 else if (vim9script)
3604 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003605 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003607 if (error || !op_falsy || !result)
3608 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 if (error)
3610 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
3613 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003614 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003616 if (op_falsy)
3617 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003618 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003619 {
mityu4ac198c2021-05-28 17:52:40 +02003620 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003621 clear_tv(rettv);
3622 return FAIL;
3623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003624 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003625 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003626 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003627 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003628 {
3629 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003631 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003632 if (!op_falsy || !result)
3633 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003635 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003637 /*
3638 * Check for the ":".
3639 */
3640 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3641 if (*p != ':')
3642 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003643 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003644 if (evaluate && result)
3645 clear_tv(rettv);
3646 evalarg_used->eval_flags = orig_flags;
3647 return FAIL;
3648 }
3649 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003650 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003651 else
3652 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003653 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003654 {
3655 error_white_both(p, 1);
3656 clear_tv(rettv);
3657 evalarg_used->eval_flags = orig_flags;
3658 return FAIL;
3659 }
3660 *arg = p;
3661 }
3662
3663 /*
3664 * Get the third variable. Recursive!
3665 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003666 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003667 {
mityu4ac198c2021-05-28 17:52:40 +02003668 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003669 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003670 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003671 return FAIL;
3672 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003673 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3674 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003675 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003676 if (eval1(arg, &var2, evalarg_used) == FAIL)
3677 {
3678 if (evaluate && result)
3679 clear_tv(rettv);
3680 evalarg_used->eval_flags = orig_flags;
3681 return FAIL;
3682 }
3683 if (evaluate && !result)
3684 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003686
3687 if (evalarg == NULL)
3688 clear_evalarg(&local_evalarg, NULL);
3689 else
3690 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 return OK;
3694}
3695
3696/*
3697 * Handle first level expression:
3698 * expr2 || expr2 || expr2 logical OR
3699 *
3700 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003701 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 *
3703 * Return OK or FAIL.
3704 */
3705 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003706eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003708 char_u *p;
3709 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003712 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003714 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 return FAIL;
3716
3717 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003718 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003720 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003721 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003723 evalarg_T *evalarg_used = evalarg;
3724 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003725 int evaluate;
3726 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003727 long result = FALSE;
3728 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003729 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003730 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003731
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003732 if (evalarg == NULL)
3733 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003734 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003735 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003736 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003737 orig_flags = evalarg_used->eval_flags;
3738 evaluate = orig_flags & EVAL_EVALUATE;
3739 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003740 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003741 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003742 result = tv_get_bool_chk(rettv, &error);
3743 else if (tv_get_number_chk(rettv, &error) != 0)
3744 result = TRUE;
3745 clear_tv(rettv);
3746 if (error)
3747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
3750 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003751 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003753 while (p[0] == '|' && p[1] == '|')
3754 {
3755 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003756 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003757 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003758 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003759 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003760 {
3761 error_white_both(p, 2);
3762 clear_tv(rettv);
3763 return FAIL;
3764 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003765 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003766 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003767
3768 /*
3769 * Get the second variable.
3770 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003771 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003772 {
mityu4ac198c2021-05-28 17:52:40 +02003773 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003774 clear_tv(rettv);
3775 return FAIL;
3776 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003777 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3778 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003779 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003780 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003782
3783 /*
3784 * Compute the result.
3785 */
3786 if (evaluate && !result)
3787 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003788 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003789 result = tv_get_bool_chk(&var2, &error);
3790 else if (tv_get_number_chk(&var2, &error) != 0)
3791 result = TRUE;
3792 clear_tv(&var2);
3793 if (error)
3794 return FAIL;
3795 }
3796 if (evaluate)
3797 {
3798 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003799 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003800 rettv->v_type = VAR_BOOL;
3801 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003802 }
3803 else
3804 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003805 rettv->v_type = VAR_NUMBER;
3806 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003807 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003808 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003809
3810 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003812
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003813 if (evalarg == NULL)
3814 clear_evalarg(&local_evalarg, NULL);
3815 else
3816 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818
3819 return OK;
3820}
3821
3822/*
3823 * Handle second level expression:
3824 * expr3 && expr3 && expr3 logical AND
3825 *
3826 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003827 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 *
3829 * Return OK or FAIL.
3830 */
3831 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003832eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003834 char_u *p;
3835 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003838 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003840 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return FAIL;
3842
3843 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003844 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003846 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003847 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003849 evalarg_T *evalarg_used = evalarg;
3850 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003851 int orig_flags;
3852 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003853 long result = TRUE;
3854 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003855 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003856 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003857
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003858 if (evalarg == NULL)
3859 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003860 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003861 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003862 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003863 orig_flags = evalarg_used->eval_flags;
3864 evaluate = orig_flags & EVAL_EVALUATE;
3865 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003866 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003867 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003868 result = tv_get_bool_chk(rettv, &error);
3869 else if (tv_get_number_chk(rettv, &error) == 0)
3870 result = FALSE;
3871 clear_tv(rettv);
3872 if (error)
3873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875
3876 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003877 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003879 while (p[0] == '&' && p[1] == '&')
3880 {
3881 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003882 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003883 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003884 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003885 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003886 {
3887 error_white_both(p, 2);
3888 clear_tv(rettv);
3889 return FAIL;
3890 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003891 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003892 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003893
3894 /*
3895 * Get the second variable.
3896 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003897 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003898 {
mityu4ac198c2021-05-28 17:52:40 +02003899 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003900 clear_tv(rettv);
3901 return FAIL;
3902 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003903 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3904 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003905 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003906 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003907 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003909
3910 /*
3911 * Compute the result.
3912 */
3913 if (evaluate && result)
3914 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003915 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003916 result = tv_get_bool_chk(&var2, &error);
3917 else if (tv_get_number_chk(&var2, &error) == 0)
3918 result = FALSE;
3919 clear_tv(&var2);
3920 if (error)
3921 return FAIL;
3922 }
3923 if (evaluate)
3924 {
3925 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003926 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003927 rettv->v_type = VAR_BOOL;
3928 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003929 }
3930 else
3931 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003932 rettv->v_type = VAR_NUMBER;
3933 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003934 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003935 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003936
3937 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003939
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003940 if (evalarg == NULL)
3941 clear_evalarg(&local_evalarg, NULL);
3942 else
3943 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945
3946 return OK;
3947}
3948
3949/*
3950 * Handle third level expression:
3951 * var1 == var2
3952 * var1 =~ var2
3953 * var1 != var2
3954 * var1 !~ var2
3955 * var1 > var2
3956 * var1 >= var2
3957 * var1 < var2
3958 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003959 * var1 is var2
3960 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 *
3962 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003963 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 *
3965 * Return OK or FAIL.
3966 */
3967 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003968eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003971 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003972 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003974 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003977 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003979 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return FAIL;
3981
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003982 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003983
Bram Moolenaar696ba232020-07-29 21:20:41 +02003984 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003987 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003989 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003991 typval_T var2;
3992 int ic;
3993 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003994 int evaluate = evalarg == NULL
3995 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003996 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003997
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003998 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003999 {
Bram Moolenaare442d592022-05-05 12:20:28 +01004000 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02004001 p = *arg;
4002 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004003 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4004 {
mityu4ac198c2021-05-28 17:52:40 +02004005 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004006 clear_tv(rettv);
4007 return FAIL;
4008 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02004009
Bram Moolenaar696ba232020-07-29 21:20:41 +02004010 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
4011 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004012 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02004013 clear_tv(rettv);
4014 return FAIL;
4015 }
4016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (p[len] == '?')
4019 {
4020 ic = TRUE;
4021 ++len;
4022 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 else if (p[len] == '#')
4025 {
4026 ic = FALSE;
4027 ++len;
4028 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004029 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004031 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033 /*
4034 * Get the second variable.
4035 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004036 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
4037 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02004038 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004039 clear_tv(rettv);
4040 return FAIL;
4041 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02004042 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004043 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return FAIL;
4047 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004048 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01004049 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004050 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01004051
Bram Moolenaar1b1df952022-03-10 20:01:50 +00004052 // use the line of the comparison for messages
4053 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004054 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004055 {
4056 ret = FAIL;
4057 clear_tv(rettv);
4058 }
4059 else
4060 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01004061 clear_tv(&var2);
4062 return ret;
4063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065
4066 return OK;
4067}
4068
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004069/*
4070 * Make a copy of blob "tv1" and append blob "tv2".
4071 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 void
4073eval_addblob(typval_T *tv1, typval_T *tv2)
4074{
4075 blob_T *b1 = tv1->vval.v_blob;
4076 blob_T *b2 = tv2->vval.v_blob;
4077 blob_T *b = blob_alloc();
4078 int i;
4079
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004080 if (b == NULL)
4081 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004083 for (i = 0; i < blob_len(b1); i++)
4084 ga_append(&b->bv_ga, blob_get(b1, i));
4085 for (i = 0; i < blob_len(b2); i++)
4086 ga_append(&b->bv_ga, blob_get(b2, i));
4087
4088 clear_tv(tv1);
4089 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090}
4091
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004092/*
4093 * Make a copy of list "tv1" and append list "tv2".
4094 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 int
4096eval_addlist(typval_T *tv1, typval_T *tv2)
4097{
4098 typval_T var3;
4099
4100 // concatenate Lists
4101 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
4102 {
4103 clear_tv(tv1);
4104 clear_tv(tv2);
4105 return FAIL;
4106 }
4107 clear_tv(tv1);
4108 *tv1 = var3;
4109 return OK;
4110}
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004113 * Make a copy of tuple "tv1" and append tuple "tv2".
4114 */
4115 int
4116eval_addtuple(typval_T *tv1, typval_T *tv2)
4117{
4118 int vim9script = in_vim9script();
4119 typval_T var3;
4120
4121 if (vim9script && tv1->vval.v_tuple != NULL && tv2->vval.v_tuple != NULL
4122 && tv1->vval.v_tuple->tv_type != NULL
4123 && tv2->vval.v_tuple->tv_type != NULL)
4124 {
4125 if (!check_tuples_addable(tv1->vval.v_tuple->tv_type,
4126 tv2->vval.v_tuple->tv_type))
4127 return FAIL;
4128 }
4129
4130 // concatenate tuples
4131 if (tuple_concat(tv1->vval.v_tuple, tv2->vval.v_tuple, &var3) == FAIL)
4132 {
4133 clear_tv(tv1);
4134 clear_tv(tv2);
4135 return FAIL;
4136 }
4137 clear_tv(tv1);
4138 *tv1 = var3;
4139 return OK;
4140}
4141
4142/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004143 * Left or right shift the number "tv1" by the number "tv2" and store the
4144 * result in "tv1".
4145 *
4146 * Return OK or FAIL.
4147 */
4148 static int
4149eval_shift_number(typval_T *tv1, typval_T *tv2, int shift_type)
4150{
4151 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
4152 {
4153 // right operand should be a positive number
4154 if (tv2->v_type != VAR_NUMBER)
4155 emsg(_(e_bitshift_ops_must_be_number));
4156 else
4157 emsg(_(e_bitshift_ops_must_be_positive));
4158 clear_tv(tv1);
4159 clear_tv(tv2);
4160 return FAIL;
4161 }
4162
4163 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
4164 // shifting more bits than we have always results in zero
4165 tv1->vval.v_number = 0;
4166 else if (shift_type == EXPR_LSHIFT)
4167 tv1->vval.v_number =
4168 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
4169 else
4170 tv1->vval.v_number =
4171 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
4172
4173 return OK;
4174}
4175
4176/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004177 * Handle fourth level expression (bitwise left/right shift operators):
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004178 * var1 << var2
4179 * var1 >> var2
4180 *
4181 * "arg" must point to the first non-white of the expression.
4182 * "arg" is advanced to just after the recognized expression.
4183 *
4184 * Return OK or FAIL.
4185 */
4186 static int
4187eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
4188{
4189 /*
4190 * Get the first expression.
4191 */
4192 if (eval6(arg, rettv, evalarg) == FAIL)
4193 return FAIL;
4194
4195 /*
4196 * Repeat computing, until no '<<' or '>>' is following.
4197 */
4198 for (;;)
4199 {
4200 char_u *p;
4201 int getnext;
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004202 exprtype_T exprtype;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004203 int evaluate;
4204 typval_T var2;
4205 int vim9script;
4206
4207 p = eval_next_non_blank(*arg, evalarg, &getnext);
4208 if (p[0] == '<' && p[1] == '<')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004209 exprtype = EXPR_LSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004210 else if (p[0] == '>' && p[1] == '>')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004211 exprtype = EXPR_RSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004212 else
4213 return OK;
4214
4215 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004216 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
4217 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004218 {
4219 // left operand should be a number
4220 emsg(_(e_bitshift_ops_must_be_number));
4221 clear_tv(rettv);
4222 return FAIL;
4223 }
4224
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004225 vim9script = in_vim9script();
4226 if (getnext)
4227 {
4228 *arg = eval_next_line(*arg, evalarg);
4229 p = *arg;
4230 }
4231 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4232 {
4233 error_white_both(*arg, 2);
4234 clear_tv(rettv);
4235 return FAIL;
4236 }
4237
4238 /*
4239 * Get the second variable.
4240 */
4241 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
4242 {
4243 error_white_both(p, 2);
4244 clear_tv(rettv);
4245 return FAIL;
4246 }
4247 *arg = skipwhite_and_linebreak(p + 2, evalarg);
4248 if (eval6(arg, &var2, evalarg) == FAIL)
4249 {
4250 clear_tv(rettv);
4251 return FAIL;
4252 }
4253
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004254 if (evaluate)
4255 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004256 if (eval_shift_number(rettv, &var2, exprtype) == FAIL)
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004257 return FAIL;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004258 }
4259
4260 clear_tv(&var2);
4261 }
4262
4263 return OK;
4264}
4265
4266/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004267 * Concatenate strings "tv1" and "tv2" and store the result in "tv1".
4268 */
4269 static int
4270eval_concat_str(typval_T *tv1, typval_T *tv2)
4271{
4272 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4273 char_u *s1 = tv_get_string_buf(tv1, buf1);
4274 char_u *s2 = NULL;
4275 char_u *p;
4276 int vim9script = in_vim9script();
4277
4278 if (vim9script && (tv2->v_type == VAR_VOID
4279 || tv2->v_type == VAR_CHANNEL
4280 || tv2->v_type == VAR_JOB))
4281 semsg(_(e_using_invalid_value_as_string_str),
4282 vartype_name(tv2->v_type));
4283 else if (vim9script && tv2->v_type == VAR_FLOAT)
4284 {
4285 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4286 tv2->vval.v_float);
4287 s2 = buf2;
4288 }
4289 else
4290 s2 = tv_get_string_buf_chk(tv2, buf2);
4291 if (s2 == NULL) // type error ?
4292 {
4293 clear_tv(tv1);
4294 clear_tv(tv2);
4295 return FAIL;
4296 }
4297
4298 p = concat_str(s1, s2);
4299 clear_tv(tv1);
4300 tv1->v_type = VAR_STRING;
4301 tv1->vval.v_string = p;
4302
4303 return OK;
4304}
4305
4306/*
4307 * Add or subtract numbers "tv1" and "tv2" and store the result in "tv1".
4308 * The numbers can be whole numbers or floats.
4309 */
4310 static int
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004311eval_addsub_number(typval_T *tv1, typval_T *tv2, int op)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004312{
4313 int error = FALSE;
4314 varnumber_T n1, n2;
4315 float_T f1 = 0, f2 = 0;
4316
4317 if (tv1->v_type == VAR_FLOAT)
4318 {
4319 f1 = tv1->vval.v_float;
4320 n1 = 0;
4321 }
4322 else
4323 {
4324 n1 = tv_get_number_chk(tv1, &error);
4325 if (error)
4326 {
4327 // This can only happen for "list + non-list" or
4328 // "blob + non-blob". For "non-list + ..." or
4329 // "something - ...", we returned before evaluating the
4330 // 2nd operand.
4331 clear_tv(tv1);
4332 clear_tv(tv2);
4333 return FAIL;
4334 }
4335 if (tv2->v_type == VAR_FLOAT)
4336 f1 = n1;
4337 }
4338 if (tv2->v_type == VAR_FLOAT)
4339 {
4340 f2 = tv2->vval.v_float;
4341 n2 = 0;
4342 }
4343 else
4344 {
4345 n2 = tv_get_number_chk(tv2, &error);
4346 if (error)
4347 {
4348 clear_tv(tv1);
4349 clear_tv(tv2);
4350 return FAIL;
4351 }
4352 if (tv1->v_type == VAR_FLOAT)
4353 f2 = n2;
4354 }
4355 clear_tv(tv1);
4356
4357 // If there is a float on either side the result is a float.
4358 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4359 {
4360 if (op == '+')
4361 f1 = f1 + f2;
4362 else
4363 f1 = f1 - f2;
4364 tv1->v_type = VAR_FLOAT;
4365 tv1->vval.v_float = f1;
4366 }
4367 else
4368 {
4369 if (op == '+')
4370 n1 = n1 + n2;
4371 else
4372 n1 = n1 - n2;
4373 tv1->v_type = VAR_NUMBER;
4374 tv1->vval.v_number = n1;
4375 }
4376
4377 return OK;
4378}
4379
4380/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004381 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004382 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004384 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004385 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004388 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004393eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004396 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004398 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 /*
4402 * Repeat computing, until no '+', '-' or '.' is following.
4403 */
4404 for (;;)
4405 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004406 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004407 int getnext;
4408 char_u *p;
4409 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004410 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004411 int concat;
4412 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004413 int vim9script = in_vim9script();
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004414 long op_lnum = SOURCING_LNUM;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004415
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004416 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004417 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004418 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004419 p = eval_next_non_blank(*arg, evalarg, &getnext);
4420 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004421 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004422 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4423 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004425 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4426 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004427
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004428 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004429 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004430 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004431 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004432 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004433 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004434 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004435 {
mityu4ac198c2021-05-28 17:52:40 +02004436 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004437 clear_tv(rettv);
4438 return FAIL;
4439 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004440 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004441 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004442 if ((op != '+' || (rettv->v_type != VAR_LIST
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004443 && rettv->v_type != VAR_TUPLE
4444 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004446 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004448 int error = FALSE;
4449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // For "list + ...", an illegal use of the first operand as
4451 // a number cannot be determined before evaluating the 2nd
4452 // operand: if this is also a list, all is ok.
4453 // For "something . ...", "something - ..." or "non-list + ...",
4454 // we know that the first operand needs to be a string or number
4455 // without evaluating the 2nd operand. So check before to avoid
4456 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004457 if (op != '.')
4458 tv_get_number_chk(rettv, &error);
4459 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004460 {
4461 clear_tv(rettv);
4462 return FAIL;
4463 }
4464 }
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 /*
4467 * Get the second variable.
4468 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004469 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004470 {
mityu89dcb4d2021-05-28 13:50:17 +02004471 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004472 clear_tv(rettv);
4473 return FAIL;
4474 }
4475 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004476 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 return FAIL;
4480 }
4481
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004482 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 /*
4485 * Compute the result.
4486 */
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004487 // use the line of the operation for messages
4488 SOURCING_LNUM = op_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (op == '.')
4490 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004491 if (eval_concat_str(rettv, &var2) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004494 else if (op == '+' && rettv->v_type == VAR_BLOB
4495 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004497 else if (op == '+' && rettv->v_type == VAR_LIST
4498 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004503 else if (op == '+' && rettv->v_type == VAR_TUPLE
4504 && var2.v_type == VAR_TUPLE)
4505 {
4506 if (eval_addtuple(rettv, &var2) == FAIL)
4507 return FAIL;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else
4510 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004511 if (eval_addsub_number(rettv, &var2, op) == FAIL)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004514 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 }
4517 return OK;
4518}
4519
4520/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004521 * Multiply or divide or compute the modulo of numbers "tv1" and "tv2" and
4522 * store the result in "tv1". The numbers can be whole numbers or floats.
4523 */
4524 static int
4525eval_multdiv_number(typval_T *tv1, typval_T *tv2, int op)
4526{
4527 varnumber_T n1, n2;
4528 float_T f1, f2;
4529 int error;
4530 int use_float = FALSE;
4531
4532 f1 = 0;
4533 f2 = 0;
4534 error = FALSE;
4535 if (tv1->v_type == VAR_FLOAT)
4536 {
4537 f1 = tv1->vval.v_float;
4538 use_float = TRUE;
4539 n1 = 0;
4540 }
4541 else
4542 n1 = tv_get_number_chk(tv1, &error);
4543 clear_tv(tv1);
4544 if (error)
4545 {
4546 clear_tv(tv2);
4547 return FAIL;
4548 }
4549
4550 if (tv2->v_type == VAR_FLOAT)
4551 {
4552 if (!use_float)
4553 {
4554 f1 = n1;
4555 use_float = TRUE;
4556 }
4557 f2 = tv2->vval.v_float;
4558 n2 = 0;
4559 }
4560 else
4561 {
4562 n2 = tv_get_number_chk(tv2, &error);
4563 clear_tv(tv2);
4564 if (error)
4565 return FAIL;
4566 if (use_float)
4567 f2 = n2;
4568 }
4569
4570 /*
4571 * Compute the result.
4572 * When either side is a float the result is a float.
4573 */
4574 if (use_float)
4575 {
4576 if (op == '*')
4577 f1 = f1 * f2;
4578 else if (op == '/')
4579 {
4580#ifdef VMS
4581 // VMS crashes on divide by zero, work around it
4582 if (f2 == 0.0)
4583 {
4584 if (f1 == 0)
4585 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
4586 else if (f1 < 0)
4587 f1 = -1 * __F_FLT_MAX;
4588 else
4589 f1 = __F_FLT_MAX;
4590 }
4591 else
4592 f1 = f1 / f2;
4593#else
4594 // We rely on the floating point library to handle divide
4595 // by zero to result in "inf" and not a crash.
4596 f1 = f1 / f2;
4597#endif
4598 }
4599 else
4600 {
4601 emsg(_(e_cannot_use_percent_with_float));
4602 return FAIL;
4603 }
4604 tv1->v_type = VAR_FLOAT;
4605 tv1->vval.v_float = f1;
4606 }
4607 else
4608 {
4609 int failed = FALSE;
4610
4611 if (op == '*')
4612 n1 = n1 * n2;
4613 else if (op == '/')
4614 n1 = num_divide(n1, n2, &failed);
4615 else
4616 n1 = num_modulus(n1, n2, &failed);
4617 if (failed)
4618 return FAIL;
4619
4620 tv1->v_type = VAR_NUMBER;
4621 tv1->vval.v_number = n1;
4622 }
4623
4624 return OK;
4625}
4626
4627/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004628 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 * * number multiplication
4630 * / number division
4631 * % number modulo
4632 *
4633 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004634 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 *
4636 * Return OK or FAIL.
4637 */
4638 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004639eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004640 char_u **arg,
4641 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004642 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004646 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004648 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return FAIL;
4650
4651 /*
4652 * Repeat computing, until no '*', '/' or '%' is following.
4653 */
4654 for (;;)
4655 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004656 int evaluate;
4657 int getnext;
4658 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004659 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004660 int op;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004661
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004662 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004663 p = eval_next_non_blank(*arg, evalarg, &getnext);
4664 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004665 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004667
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004668 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004669 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004670 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004671 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004672 {
4673 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4674 {
mityu4ac198c2021-05-28 17:52:40 +02004675 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004676 clear_tv(rettv);
4677 return FAIL;
4678 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004679 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 /*
4683 * Get the second variable.
4684 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004685 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4686 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004687 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004688 clear_tv(rettv);
4689 return FAIL;
4690 }
4691 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004692 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return FAIL;
4694
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004695 if (evaluate)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004696 // Compute the result.
4697 if (eval_multdiv_number(rettv, &var2, op) == FAIL)
4698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700
4701 return OK;
4702}
4703
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004704/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004705 * Handle seventh level expression:
4706 * a type cast before a base level expression.
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004707 * "arg" must point to the first non-white of the expression.
4708 * "arg" is advanced to just after the recognized expression.
4709 * Return OK or FAIL.
4710 */
4711 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004712eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004713 char_u **arg,
4714 typval_T *rettv,
4715 evalarg_T *evalarg,
4716 int want_string) // after "." operator
4717{
4718 type_T *want_type = NULL;
4719 garray_T type_list; // list of pointers to allocated types
4720 int res;
4721 int evaluate = evalarg == NULL ? 0
4722 : (evalarg->eval_flags & EVAL_EVALUATE);
4723
4724 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004725 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4726 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004727 {
4728 ++*arg;
4729 ga_init2(&type_list, sizeof(type_T *), 10);
4730 want_type = parse_type(arg, &type_list, TRUE);
4731 if (want_type == NULL && (evaluate || **arg != '>'))
4732 {
4733 clear_type_list(&type_list);
4734 return FAIL;
4735 }
4736
4737 if (**arg != '>')
4738 {
4739 if (*skipwhite(*arg) == '>')
4740 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4741 else
4742 emsg(_(e_missing_gt));
4743 clear_type_list(&type_list);
4744 return FAIL;
4745 }
4746 ++*arg;
4747 *arg = skipwhite_and_linebreak(*arg, evalarg);
4748 }
4749
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004750 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004751
4752 if (want_type != NULL && evaluate)
4753 {
4754 if (res == OK)
4755 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004756 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4757 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004758
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004759 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004760 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004761 if (want_type->tt_type == VAR_BOOL
4762 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004763 && (actual->tt_flags & TTFLAG_BOOL_OK))
4764 {
4765 int n = tv2bool(rettv);
4766
4767 // can use "0" and "1" for boolean in some places
4768 clear_tv(rettv);
4769 rettv->v_type = VAR_BOOL;
4770 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4771 }
4772 else
4773 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004774 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004775
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004776 res = check_type(want_type, actual, TRUE, where);
4777 }
4778 }
4779 }
4780 clear_type_list(&type_list);
4781 }
4782
4783 return res;
4784}
4785
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004786 int
4787eval_leader(char_u **arg, int vim9)
4788{
4789 char_u *s = *arg;
4790 char_u *p = *arg;
4791
4792 while (*p == '!' || *p == '-' || *p == '+')
4793 {
4794 char_u *n = skipwhite(p + 1);
4795
4796 // ++, --, -+ and +- are not accepted in Vim9 script
4797 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4798 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004799 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004800 return FAIL;
4801 }
4802 p = n;
4803 }
4804 *arg = p;
4805 return OK;
4806}
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004809 * Check for a predefined value "true", "false" and "null.*".
4810 * Return OK when recognized.
4811 */
4812 int
4813handle_predefined(char_u *s, int len, typval_T *rettv)
4814{
4815 switch (len)
4816 {
4817 case 4: if (STRNCMP(s, "true", 4) == 0)
4818 {
4819 rettv->v_type = VAR_BOOL;
4820 rettv->vval.v_number = VVAL_TRUE;
4821 return OK;
4822 }
4823 if (STRNCMP(s, "null", 4) == 0)
4824 {
4825 rettv->v_type = VAR_SPECIAL;
4826 rettv->vval.v_number = VVAL_NULL;
4827 return OK;
4828 }
4829 break;
4830 case 5: if (STRNCMP(s, "false", 5) == 0)
4831 {
4832 rettv->v_type = VAR_BOOL;
4833 rettv->vval.v_number = VVAL_FALSE;
4834 return OK;
4835 }
4836 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004837 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4838 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004840 rettv->v_type = VAR_JOB;
4841 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004842#else
4843 rettv->v_type = VAR_SPECIAL;
4844 rettv->vval.v_number = VVAL_NULL;
4845#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004846 return OK;
4847 }
4848 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004849 case 9:
4850 if (STRNCMP(s, "null_", 5) != 0)
4851 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004852 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004853 if (STRNCMP(s + 5, "list", 4) == 0)
4854 {
4855 rettv->v_type = VAR_LIST;
4856 rettv->vval.v_list = NULL;
4857 return OK;
4858 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004859 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004860 if (STRNCMP(s + 5, "dict", 4) == 0)
4861 {
4862 rettv->v_type = VAR_DICT;
4863 rettv->vval.v_dict = NULL;
4864 return OK;
4865 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004866 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004867 if (STRNCMP(s + 5, "blob", 4) == 0)
4868 {
4869 rettv->v_type = VAR_BLOB;
4870 rettv->vval.v_blob = NULL;
4871 return OK;
4872 }
4873 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004874 case 10:
4875 if (STRNCMP(s, "null_", 5) != 0)
4876 break;
4877 // null_class
4878 if (STRNCMP(s + 5, "class", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004879 {
4880 rettv->v_type = VAR_CLASS;
4881 rettv->vval.v_class = NULL;
4882 return OK;
4883 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004884 if (STRNCMP(s + 5, "tuple", 5) == 0)
4885 {
4886 rettv->v_type = VAR_TUPLE;
4887 rettv->vval.v_tuple = NULL;
4888 return OK;
4889 }
4890 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004891 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4892 {
4893 rettv->v_type = VAR_STRING;
4894 rettv->vval.v_string = NULL;
4895 return OK;
4896 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004897 if (STRNCMP(s, "null_object", 11) == 0)
4898 {
4899 rettv->v_type = VAR_OBJECT;
4900 rettv->vval.v_object = NULL;
4901 return OK;
4902 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004903 break;
4904 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004905 if (STRNCMP(s, "null_channel", 12) == 0)
4906 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004907#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004908 rettv->v_type = VAR_CHANNEL;
4909 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004910#else
4911 rettv->v_type = VAR_SPECIAL;
4912 rettv->vval.v_number = VVAL_NULL;
4913#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004914 return OK;
4915 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004916 if (STRNCMP(s, "null_partial", 12) == 0)
4917 {
4918 rettv->v_type = VAR_PARTIAL;
4919 rettv->vval.v_partial = NULL;
4920 return OK;
4921 }
4922 break;
4923 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4924 {
4925 rettv->v_type = VAR_FUNC;
4926 rettv->vval.v_string = NULL;
4927 return OK;
4928 }
4929 break;
4930 }
4931 return FAIL;
4932}
4933
4934/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004935 * Handle register contents: @r.
4936 */
4937 static void
4938eval9_reg_contents(
4939 char_u **arg,
4940 typval_T *rettv,
4941 int evaluate)
4942{
4943 int vim9script = in_vim9script();
4944
4945 ++*arg; // skip '@'
4946 if (evaluate)
4947 {
4948 if (vim9script && IS_WHITE_OR_NUL(**arg))
4949 semsg(_(e_syntax_error_at_str), *arg);
4950 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4951 emsg_invreg(**arg);
4952 else
4953 {
4954 rettv->v_type = VAR_STRING;
4955 rettv->vval.v_string = get_reg_contents(**arg,
4956 GREG_EXPR_SRC);
4957 }
4958 }
4959 if (**arg != NUL)
4960 ++*arg;
4961}
4962
4963/*
4964 * Handle a nested expression: (expression) or lambda: (arg) => expr
4965 */
4966 static int
4967eval9_nested_expr(
4968 char_u **arg,
4969 typval_T *rettv,
4970 evalarg_T *evalarg,
4971 int evaluate)
4972{
4973 int ret = NOTDONE;
4974 int vim9script = in_vim9script();
4975
4976 if (vim9script)
4977 {
4978 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4979 if (ret == OK && evaluate)
4980 {
4981 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4982
4983 // Compile it here to get the return type. The return
4984 // type is optional, when it's missing use t_unknown.
4985 // This is recognized in compile_return().
4986 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4987 ufunc->uf_ret_type = &t_unknown;
4988 if (compile_def_function(ufunc, FALSE,
4989 get_compile_type(ufunc), NULL) == FAIL)
4990 {
4991 clear_tv(rettv);
4992 ret = FAIL;
4993 }
4994 }
4995 }
4996 if (ret == NOTDONE)
4997 {
4998 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004999 if (**arg == ')')
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005000 // empty tuple
5001 ret = eval_tuple(arg, rettv, evalarg, TRUE);
5002 else
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005003 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005004 ret = eval1(arg, rettv, evalarg); // recursive!
5005
5006 *arg = skipwhite_and_linebreak(*arg, evalarg);
5007
5008 if (**arg == ',')
5009 // tuple
5010 ret = eval_tuple(arg, rettv, evalarg, TRUE);
5011 else if (**arg == ')')
5012 ++*arg;
5013 else if (ret == OK)
5014 {
5015 emsg(_(e_missing_closing_paren));
5016 clear_tv(rettv);
5017 ret = FAIL;
5018 }
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005019 }
5020 }
5021
5022 return ret;
5023}
5024
5025/*
5026* Handle be a variable or function name.
5027* Can also be a curly-braces kind of name: {expr}.
5028*/
5029 static int
5030eval9_var_func_name(
5031 char_u **arg,
5032 typval_T *rettv,
5033 evalarg_T *evalarg,
5034 int evaluate,
5035 char_u **name_start)
5036{
5037 char_u *s;
5038 int len;
5039 char_u *alias;
5040 int ret = OK;
5041 int vim9script = in_vim9script();
5042
5043 s = *arg;
5044 len = get_name_len(arg, &alias, evaluate, TRUE);
5045 if (alias != NULL)
5046 s = alias;
5047
5048 if (len <= 0)
5049 ret = FAIL;
5050 else
5051 {
5052 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
5053
5054 if (evaluate && vim9script && len == 1 && *s == '_')
5055 {
5056 emsg(_(e_cannot_use_underscore_here));
5057 ret = FAIL;
5058 }
5059 else if (evaluate && vim9script && len > 2
5060 && s[0] == 's' && s[1] == ':')
5061 {
5062 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
5063 ret = FAIL;
5064 }
5065 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
5066 {
5067 // "name(..." recursive!
5068 *arg = skipwhite(*arg);
5069 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
5070 }
5071 else if (evaluate)
5072 {
5073 // get the value of "true", "false", etc. or a variable
5074 ret = FAIL;
5075 if (vim9script)
5076 ret = handle_predefined(s, len, rettv);
5077 if (ret == FAIL)
5078 {
5079 *name_start = s;
5080 ret = eval_variable(s, len, 0, rettv, NULL,
5081 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
5082 }
5083 }
5084 else
5085 {
5086 // skip the name
5087 check_vars(s, len);
5088 ret = OK;
5089 }
5090 }
5091 vim_free(alias);
5092
5093 return ret;
5094}
5095
5096/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01005097 * Handle eighth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005099 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005100 * "string" string constant
5101 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 * &option-name option value
5103 * @r register contents
5104 * identifier variable value
5105 * function() function call
5106 * $VAR environment variable
5107 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005108 * [expr, expr] List
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005109 * (expr, expr) Tuple
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005111 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02005112 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 *
5114 * Also handle:
5115 * ! in front logical NOT
5116 * - in front unary minus
5117 * + in front unary plus (ignored)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005118 * trailing [] subscript in String or List or Tuple
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02005120 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 *
5122 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02005123 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 *
5125 * Return OK or FAIL.
5126 */
5127 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005128eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005129 char_u **arg,
5130 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005131 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005134 int evaluate = evalarg != NULL
5135 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005136 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 char_u *start_leader, *end_leader;
5138 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005139 static int recurse = 0;
5140 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02005149 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005152 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01005153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 end_leader = *arg;
5155
Keith Thompson184f71c2024-01-04 21:19:04 +01005156 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005157 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005158 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005159 ++*arg;
5160 return FAIL;
5161 }
5162
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005163 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00005164 // and crash. With MSVC the stack is smaller.
5165 if (recurse ==
5166#ifdef _MSC_VER
5167 300
5168#else
5169 1000
5170#endif
5171 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005172 {
5173 semsg(_(e_expression_too_recursive_str), *arg);
5174 return FAIL;
5175 }
5176 ++recurse;
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 switch (**arg)
5179 {
5180 /*
5181 * Number constant.
5182 */
5183 case '0':
5184 case '1':
5185 case '2':
5186 case '3':
5187 case '4':
5188 case '5':
5189 case '6':
5190 case '7':
5191 case '8':
5192 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005193 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005194
5195 // Apply prefixed "-" and "+" now. Matters especially when
5196 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005197 if (ret == OK && evaluate && end_leader > start_leader
5198 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005199 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201
5202 /*
5203 * String constant: "string".
5204 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01005205 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01005211 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 break;
5213
5214 /*
5215 * List: [expr, expr]
5216 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005217 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219
5220 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005221 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005222 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005223 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005224 break;
5225
5226 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005227 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005228 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005230 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005231 ret = NOTDONE;
5232 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005233 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005234 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005235 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 break;
5237
5238 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005241 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 break;
5243
5244 /*
5245 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005246 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 */
LemonBoy2eaef102022-05-06 13:14:50 +01005248 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5249 ret = eval_interp_string(arg, rettv, evaluate);
5250 else
5251 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 break;
5253
5254 /*
5255 * Register contents: @r.
5256 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005257 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 break;
5259
5260 /*
5261 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005262 * or lambda: (arg) => expr
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005263 * or tuple
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005265 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 break;
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 break;
5270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271
5272 if (ret == NOTDONE)
5273 {
5274 /*
5275 * Must be a variable or function name.
5276 * Can also be a curly-braces kind of name: {expr}.
5277 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005278 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 }
5280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5282 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005284 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5288 */
5289 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005290 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005291
5292 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005293 return ret;
5294}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005296/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005297 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005298 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005299 * Adjusts "end_leaderp" until it is at "start_leader".
5300 */
5301 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005302eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005303 typval_T *rettv,
5304 int numeric_only,
5305 char_u *start_leader,
5306 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005307{
5308 char_u *end_leader = *end_leaderp;
5309 int ret = OK;
5310 int error = FALSE;
5311 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005312 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005313 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005314 float_T f = 0.0;
5315
5316 if (rettv->v_type == VAR_FLOAT)
5317 f = rettv->vval.v_float;
5318 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005319 {
5320 while (VIM_ISWHITE(end_leader[-1]))
5321 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005322 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005323 val = tv2bool(rettv);
5324 else
5325 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005326 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005327 if (error)
5328 {
5329 clear_tv(rettv);
5330 ret = FAIL;
5331 }
5332 else
5333 {
5334 while (end_leader > start_leader)
5335 {
5336 --end_leader;
5337 if (*end_leader == '!')
5338 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005339 if (numeric_only)
5340 {
5341 ++end_leader;
5342 break;
5343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005344 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005345 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005346 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005347 {
5348 rettv->v_type = VAR_BOOL;
5349 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5350 }
5351 else
5352 f = !f;
5353 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005354 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005355 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005356 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005357 type = VAR_BOOL;
5358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005359 }
5360 else if (*end_leader == '-')
5361 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005362 if (rettv->v_type == VAR_FLOAT)
5363 f = -f;
5364 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005365 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005366 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005367 type = VAR_NUMBER;
5368 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005369 }
5370 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005371 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005374 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005376 else
5377 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005378 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005379 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005380 rettv->v_type = type;
5381 else
5382 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005383 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005386 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return ret;
5388}
5389
5390/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005391 * Call the function referred to in "rettv".
5392 */
5393 static int
5394call_func_rettv(
5395 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005396 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005397 typval_T *rettv,
5398 int evaluate,
5399 dict_T *selfdict,
5400 typval_T *basetv)
5401{
5402 partial_T *pt = NULL;
5403 funcexe_T funcexe;
5404 typval_T functv;
5405 char_u *s;
5406 int ret;
5407
5408 // need to copy the funcref so that we can clear rettv
5409 if (evaluate)
5410 {
5411 functv = *rettv;
5412 rettv->v_type = VAR_UNKNOWN;
5413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005415 if (functv.v_type == VAR_PARTIAL)
5416 {
5417 pt = functv.vval.v_partial;
5418 s = partial_name(pt);
5419 }
5420 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005421 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005422 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005423 if (s == NULL || *s == NUL)
5424 {
5425 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005426 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005427 goto theend;
5428 }
5429 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005430 }
5431 else
5432 s = (char_u *)"";
5433
Bram Moolenaara80faa82020-04-12 19:37:17 +02005434 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005435 funcexe.fe_firstline = curwin->w_cursor.lnum;
5436 funcexe.fe_lastline = curwin->w_cursor.lnum;
5437 funcexe.fe_evaluate = evaluate;
5438 funcexe.fe_partial = pt;
5439 funcexe.fe_selfdict = selfdict;
5440 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005441 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005442
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005443theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // Clear the funcref afterwards, so that deleting it while
5445 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005446 if (evaluate)
5447 clear_tv(&functv);
5448
5449 return ret;
5450}
5451
5452/*
5453 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005454 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005455 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5456 */
5457 static int
5458eval_lambda(
5459 char_u **arg,
5460 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005461 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005463{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005464 int evaluate = evalarg != NULL
5465 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005466 typval_T base = *rettv;
5467 int ret;
5468
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005469 rettv->v_type = VAR_UNKNOWN;
5470
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005471 if (**arg == '{')
5472 {
5473 // ->{lambda}()
5474 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5475 }
5476 else
5477 {
5478 // ->(lambda)()
5479 ++*arg;
5480 ret = eval1(arg, rettv, evalarg);
5481 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005482 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005483 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005484 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005485 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005486 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005487 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5488 && rettv->v_type != VAR_PARTIAL)
5489 {
5490 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5491 return FAIL;
5492 }
5493 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005494 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005495 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005496 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005497
5498 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005499 {
5500 if (verbose)
5501 {
5502 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005503 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005504 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005505 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005506 }
5507 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005508 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005509 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005510 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005511 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005512
5513 // Clear the funcref afterwards, so that deleting it while
5514 // evaluating the arguments is possible (see test55).
5515 if (evaluate)
5516 clear_tv(&base);
5517
5518 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005519}
5520
5521/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005522 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005523 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005524 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5525 */
5526 static int
5527eval_method(
5528 char_u **arg,
5529 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005530 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005532{
5533 char_u *name;
5534 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005535 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005536 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005537 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005538 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005539 int evaluate = evalarg != NULL
5540 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005541
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005542 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005543
Bram Moolenaarac92e252019-08-03 21:58:38 +02005544 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005545 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005546 if (alias != NULL)
5547 name = alias;
5548
5549 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005550 {
5551 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005552 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005553 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005554 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005555 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005556 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005557 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005558
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005559 // If there is no "(" immediately following, but there is further on,
5560 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5561 // Does not handle anything where "(" is part of the expression.
5562 *arg = skipwhite(*arg);
5563
5564 if (**arg != '(' && alias == NULL
5565 && (paren = vim_strchr(*arg, '(')) != NULL)
5566 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005567 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005568
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005569 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005570 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005571 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005572 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5573 if (evalarg != NULL)
5574 {
5575 getline = evalarg->eval_getline;
5576 evalarg->eval_getline = NULL;
5577 }
5578
5579 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005580 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005581 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005582 *arg = name + len;
5583 ret = FAIL;
5584 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005585 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005586 {
5587 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005588 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005589 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005590
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005591 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005592 if (getline != NULL)
5593 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005594 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005595
5596 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005597 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005598 *arg = skipwhite(*arg);
5599
5600 if (**arg != '(')
5601 {
5602 if (verbose)
5603 semsg(_(e_missing_parenthesis_str), name);
5604 ret = FAIL;
5605 }
5606 else if (VIM_ISWHITE((*arg)[-1]))
5607 {
5608 if (verbose)
5609 emsg(_(e_no_white_space_allowed_before_parenthesis));
5610 ret = FAIL;
5611 }
5612 else
5613 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005614 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005615 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005616 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005617
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005618 // Clear the funcref afterwards, so that deleting it while
5619 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005620 if (evaluate)
5621 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005622 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005623
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005624 if (alias != NULL)
5625 vim_free(alias);
5626
Bram Moolenaarac92e252019-08-03 21:58:38 +02005627 return ret;
5628}
5629
5630/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005631 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5632 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5634 */
5635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636eval_index(
5637 char_u **arg,
5638 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005639 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005642 int evaluate = evalarg != NULL
5643 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005648 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005649 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005651 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5652 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005654 init_tv(&var1);
5655 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 /*
5659 * dict.name
5660 */
5661 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005662 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005664 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005666 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 }
5668 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 /*
5671 * something[idx]
5672 *
5673 * Get the (first) variable from inside the [].
5674 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005675 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 if (**arg == ':')
5677 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005678 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005680 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005681 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005682 semsg(_(e_white_space_required_before_and_after_str_at_str),
5683 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005684 clear_tv(&var1);
5685 return FAIL;
5686 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005687 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005688 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005689 int error = FALSE;
5690
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005691 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005692 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005693 && var1.v_type == VAR_FLOAT)
5694 {
5695 var1.vval.v_string = typval_tostring(&var1, TRUE);
5696 var1.v_type = VAR_STRING;
5697 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005698
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005699 if (vim9script && (rettv->v_type == VAR_LIST
5700 || rettv->v_type == VAR_TUPLE))
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005701 tv_get_number_chk(&var1, &error);
5702 else
5703 error = tv_get_string_chk(&var1) == NULL;
5704 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005705 {
5706 // not a number or string
5707 clear_tv(&var1);
5708 return FAIL;
5709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711
5712 /*
5713 * Get the second variable from inside the [:].
5714 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005715 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 if (**arg == ':')
5717 {
5718 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005719 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005720 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005721 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005722 semsg(_(e_white_space_required_before_and_after_str_at_str),
5723 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005724 if (!empty1)
5725 clear_tv(&var1);
5726 return FAIL;
5727 }
5728 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 if (**arg == ']')
5730 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005731 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005733 if (!empty1)
5734 clear_tv(&var1);
5735 return FAIL;
5736 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005737 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005740 if (!empty1)
5741 clear_tv(&var1);
5742 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 return FAIL;
5744 }
5745 }
5746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005748 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 if (**arg != ']')
5750 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005752 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 clear_tv(&var1);
5754 if (range)
5755 clear_tv(&var2);
5756 return FAIL;
5757 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005758 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 }
5760
5761 if (evaluate)
5762 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005763 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005764 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005765 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005766
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005767 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005770 clear_tv(&var2);
5771 return res;
5772 }
5773 return OK;
5774}
5775
5776/*
5777 * Check if "rettv" can have an [index] or [sli:ce]
5778 */
5779 int
5780check_can_index(typval_T *rettv, int evaluate, int verbose)
5781{
5782 switch (rettv->v_type)
5783 {
5784 case VAR_FUNC:
5785 case VAR_PARTIAL:
5786 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005787 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005788 return FAIL;
5789 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005790 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005791 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005792 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005793 case VAR_BOOL:
5794 case VAR_SPECIAL:
5795 case VAR_JOB:
5796 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005797 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005798 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005799 if (verbose)
5800 emsg(_(e_cannot_index_special_variable));
5801 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005802 case VAR_CLASS:
5803 case VAR_TYPEALIAS:
5804 if (verbose)
5805 check_typval_is_value(rettv);
5806 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005807 case VAR_UNKNOWN:
5808 case VAR_ANY:
5809 case VAR_VOID:
5810 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005812 emsg(_(e_cannot_index_special_variable));
5813 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005815 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005817 case VAR_STRING:
5818 case VAR_LIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005819 case VAR_TUPLE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005820 case VAR_DICT:
5821 case VAR_BLOB:
5822 break;
5823 case VAR_NUMBER:
5824 if (in_vim9script())
5825 emsg(_(e_cannot_index_number));
5826 break;
5827 }
5828 return OK;
5829}
5830
5831/*
5832 * Apply index or range to "rettv".
5833 * "var1" is the first index, NULL for [:expr].
5834 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005835 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5836 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005837 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5838 */
5839 int
5840eval_index_inner(
5841 typval_T *rettv,
5842 int is_range,
5843 typval_T *var1,
5844 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005845 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005846 char_u *key,
5847 int keylen,
5848 int verbose)
5849{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005850 varnumber_T n1, n2 = 0;
5851 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005852
5853 n1 = 0;
5854 if (var1 != NULL && rettv->v_type != VAR_DICT)
5855 n1 = tv_get_number(var1);
5856
5857 if (is_range)
5858 {
5859 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005861 if (verbose)
5862 emsg(_(e_cannot_slice_dictionary));
5863 return FAIL;
5864 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005865 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005866 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005867 else
5868 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005869 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005870
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005871 switch (rettv->v_type)
5872 {
5873 case VAR_UNKNOWN:
5874 case VAR_ANY:
5875 case VAR_VOID:
5876 case VAR_FUNC:
5877 case VAR_PARTIAL:
5878 case VAR_FLOAT:
5879 case VAR_BOOL:
5880 case VAR_SPECIAL:
5881 case VAR_JOB:
5882 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005883 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005884 case VAR_CLASS:
5885 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005886 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005887 break; // not evaluating, skipping over subscript
5888
5889 case VAR_NUMBER:
5890 case VAR_STRING:
5891 {
5892 char_u *s = tv_get_string(rettv);
5893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005895 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005896 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005897 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005898 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005899 else
5900 s = char_from_string(s, n1);
5901 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005902 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005904 // The resulting variable is a substring. If the indexes
5905 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 if (n1 < 0)
5907 {
5908 n1 = len + n1;
5909 if (n1 < 0)
5910 n1 = 0;
5911 }
5912 if (n2 < 0)
5913 n2 = len + n2;
5914 else if (n2 >= len)
5915 n2 = len;
5916 if (n1 >= len || n2 < 0 || n1 > n2)
5917 s = NULL;
5918 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005919 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 }
5921 else
5922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 // The resulting variable is a string of a single
5924 // character. If the index is too big or negative the
5925 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 if (n1 >= len || n1 < 0)
5927 s = NULL;
5928 else
5929 s = vim_strnsave(s + n1, 1);
5930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 clear_tv(rettv);
5932 rettv->v_type = VAR_STRING;
5933 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005934 }
5935 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005937 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005938 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5939 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005940 break;
5941
5942 case VAR_LIST:
5943 if (var1 == NULL)
5944 n1 = 0;
5945 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005946 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005947 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005948 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005949 return FAIL;
5950 break;
5951
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005952 case VAR_TUPLE:
5953 if (var1 == NULL)
5954 n1 = 0;
5955 if (var2 == NULL)
5956 n2 = VARNUM_MAX;
5957 if (tuple_slice_or_index(rettv->vval.v_tuple,
5958 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
5959 return FAIL;
5960 break;
5961
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005962 case VAR_DICT:
5963 {
5964 dictitem_T *item;
5965 typval_T tmp;
5966
5967 if (key == NULL)
5968 {
5969 key = tv_get_string_chk(var1);
5970 if (key == NULL)
5971 return FAIL;
5972 }
5973
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005974 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005975
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005976 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005977 {
5978 if (verbose)
5979 {
5980 if (keylen > 0)
5981 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005982 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005983 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005984 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005985 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005986
5987 copy_tv(&item->di_tv, &tmp);
5988 clear_tv(rettv);
5989 *rettv = tmp;
5990 }
5991 break;
5992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 return OK;
5994}
5995
5996/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005997 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005998 */
5999 char_u *
6000partial_name(partial_T *pt)
6001{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02006002 if (pt != NULL)
6003 {
6004 if (pt->pt_name != NULL)
6005 return pt->pt_name;
6006 if (pt->pt_func != NULL)
6007 return pt->pt_func->uf_name;
6008 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02006009 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006010}
6011
Bram Moolenaarddecc252016-04-06 22:59:37 +02006012 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006013partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006014{
6015 int i;
6016
6017 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006018 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006019 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006020 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006021 if (pt->pt_name != NULL)
6022 {
6023 func_unref(pt->pt_name);
6024 vim_free(pt->pt_name);
6025 }
6026 else
6027 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02006028 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006029
Bram Moolenaar54656012021-06-09 20:50:46 +02006030 // "out_up" is no longer used, decrement refcount on partial that owns it.
6031 partial_unref(pt->pt_outer.out_up_partial);
6032
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006033 // Using pt_outer from another partial.
6034 partial_unref(pt->pt_outer_partial);
6035
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006036 // Decrease the reference count for the context of a closure. If down
6037 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006038 if (pt->pt_funcstack != NULL)
6039 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006040 --pt->pt_funcstack->fs_refcount;
6041 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006042 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006043 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01006044 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
6045 if (pt->pt_loopvars[i] != NULL)
6046 {
6047 --pt->pt_loopvars[i]->lvs_refcount;
6048 loopvars_check_refcount(pt->pt_loopvars[i]);
6049 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006050
Bram Moolenaarddecc252016-04-06 22:59:37 +02006051 vim_free(pt);
6052}
6053
6054/*
6055 * Unreference a closure: decrement the reference count and free it when it
6056 * becomes zero.
6057 */
6058 void
6059partial_unref(partial_T *pt)
6060{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006061 if (pt == NULL)
6062 return;
6063
6064 int done = FALSE;
6065
6066 if (--pt->pt_refcount <= 0)
6067 partial_free(pt);
6068
6069 // If the reference count goes down to one, the funcstack may be the
6070 // only reference and can be freed if no other partials reference it.
6071 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006072 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006073 // careful: if the funcstack is freed it may contain this partial
6074 // and it gets freed as well
6075 if (pt->pt_funcstack != NULL)
6076 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01006077
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006078 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006079 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006080 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01006081
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006082 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
6083 if (pt->pt_loopvars[depth] != NULL
6084 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01006085 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006086 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006087 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02006088}
6089
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006091 * Return a textual representation of a string in "tv".
6092 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6093 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6094 * strings as "string()", otherwise does not put quotes around strings.
6095 * May return NULL.
6096 */
6097 static char_u *
6098string_tv2string(
6099 typval_T *tv,
6100 char_u **tofree,
6101 int echo_style,
6102 int composite_val)
6103{
6104 char_u *r = NULL;
6105
6106 if (echo_style && !composite_val)
6107 {
6108 *tofree = NULL;
6109 r = tv->vval.v_string;
6110 if (r == NULL)
6111 r = (char_u *)"";
6112 }
6113 else
6114 {
6115 *tofree = string_quote(tv->vval.v_string, FALSE);
6116 r = *tofree;
6117 }
6118
6119 return r;
6120}
6121
6122/*
6123 * Return a textual representation of a function in "tv".
6124 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6125 * When "echo_style" is FALSE, put quotes around the function name as
6126 * "function()", otherwise does not put quotes around function name.
6127 * May return NULL.
6128 */
6129 static char_u *
6130func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
6131{
6132 char_u *r = NULL;
6133 char_u buf[MAX_FUNC_NAME_LEN];
6134
6135 if (echo_style)
6136 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006137 *tofree = NULL;
6138
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006139 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006140 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006141 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006142 {
6143 r = make_ufunc_name_readable(tv->vval.v_string, buf,
6144 MAX_FUNC_NAME_LEN);
6145 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006146 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006147 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006148 }
6149 else
6150 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006151 char_u *s = NULL;
6152
6153 if (tv->vval.v_string != NULL)
6154 s = make_ufunc_name_readable(tv->vval.v_string, buf,
6155 MAX_FUNC_NAME_LEN);
6156
6157 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006158 }
6159
6160 return r;
6161}
6162
6163/*
Ernie Rael9d779c52024-07-07 20:41:44 +02006164 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
6165 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6166 * When "echo_style" is FALSE, put quotes around the function name as
6167 * "function()", otherwise does not put quotes around function name.
6168 * May return NULL.
6169 */
6170 static char_u *
6171method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
6172{
6173 char_u buf[MAX_FUNC_NAME_LEN];
6174 partial_T *pt = tv->vval.v_partial;
6175
6176 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
6177 pt->pt_func->uf_script_ctx.sc_sid,
6178 pt->pt_func->uf_class->class_name,
6179 pt->pt_func->uf_name);
6180 if (len >= sizeof(buf))
6181 {
6182 if (echo_style)
6183 {
6184 *tofree = NULL;
6185 return (char_u *)"function()";
6186 }
6187 else
6188 return *tofree = string_quote((char_u*)"", TRUE);
6189 }
6190
6191 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
6192}
6193
6194/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006195 * Return a textual representation of a partial in "tv".
6196 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6197 * "numbuf" is used for a number. May return NULL.
6198 */
6199 static char_u *
6200partial_tv2string(
6201 typval_T *tv,
6202 char_u **tofree,
6203 char_u *numbuf,
6204 int copyID)
6205{
6206 char_u *r = NULL;
6207 partial_T *pt;
6208 char_u *fname;
6209 garray_T ga;
6210 int i;
6211 char_u *tf;
6212
6213 pt = tv->vval.v_partial;
6214 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
6215
6216 ga_init2(&ga, 1, 100);
6217 ga_concat(&ga, (char_u *)"function(");
6218 if (fname != NULL)
6219 {
6220 // When using uf_name prepend "g:" for a global function.
6221 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
6222 && vim_isupper(fname[1]))
6223 {
6224 ga_concat(&ga, (char_u *)"'g:");
6225 ga_concat(&ga, fname + 1);
6226 }
6227 else
6228 ga_concat(&ga, fname);
6229 vim_free(fname);
6230 }
6231 if (pt != NULL && pt->pt_argc > 0)
6232 {
6233 ga_concat(&ga, (char_u *)", [");
6234 for (i = 0; i < pt->pt_argc; ++i)
6235 {
6236 if (i > 0)
6237 ga_concat(&ga, (char_u *)", ");
6238 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6239 vim_free(tf);
6240 }
6241 ga_concat(&ga, (char_u *)"]");
6242 }
6243 if (pt != NULL && pt->pt_dict != NULL)
6244 {
6245 typval_T dtv;
6246
6247 ga_concat(&ga, (char_u *)", ");
6248 dtv.v_type = VAR_DICT;
6249 dtv.vval.v_dict = pt->pt_dict;
6250 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6251 vim_free(tf);
6252 }
6253 // terminate with ')' and a NUL
6254 ga_concat_len(&ga, (char_u *)")", 2);
6255
6256 *tofree = ga.ga_data;
6257 r = *tofree;
6258
6259 return r;
6260}
6261
6262/*
6263 * Return a textual representation of a List in "tv".
6264 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6265 * When "copyID" is not zero replace recursive lists with "...". When
6266 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6267 * May return NULL.
6268 */
6269 static char_u *
6270list_tv2string(
6271 typval_T *tv,
6272 char_u **tofree,
6273 int copyID,
6274 int restore_copyID)
6275{
6276 char_u *r = NULL;
6277
6278 if (tv->vval.v_list == NULL)
6279 {
6280 // NULL list is equivalent to empty list.
6281 *tofree = NULL;
6282 r = (char_u *)"[]";
6283 }
6284 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6285 && tv->vval.v_list->lv_len > 0)
6286 {
6287 *tofree = NULL;
6288 r = (char_u *)"[...]";
6289 }
6290 else
6291 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006292 int old_copyID;
6293 if (restore_copyID)
6294 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006295
6296 tv->vval.v_list->lv_copyID = copyID;
6297 *tofree = list2string(tv, copyID, restore_copyID);
6298 if (restore_copyID)
6299 tv->vval.v_list->lv_copyID = old_copyID;
6300 r = *tofree;
6301 }
6302
6303 return r;
6304}
6305
6306/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006307 * Return a textual representation of a Tuple in "tv".
6308 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6309 * When "copyID" is not zero replace recursive lists with "...". When
6310 * "restore_copyID" is FALSE, repeated items in tuples are replaced with "...".
6311 * May return NULL.
6312 */
6313 static char_u *
6314tuple_tv2string(
6315 typval_T *tv,
6316 char_u **tofree,
6317 int copyID,
6318 int restore_copyID)
6319{
6320 tuple_T *tuple = tv->vval.v_tuple;
6321 char_u *r = NULL;
6322
6323 if (tuple == NULL)
6324 {
6325 // NULL tuple is equivalent to an empty tuple.
6326 *tofree = NULL;
6327 r = (char_u *)"()";
6328 }
6329 else if (copyID != 0 && tuple->tv_copyID == copyID
6330 && tuple->tv_items.ga_len > 0)
6331 {
6332 *tofree = NULL;
6333 r = (char_u *)"(...)";
6334 }
6335 else
6336 {
6337 int old_copyID;
6338 if (restore_copyID)
6339 old_copyID = tuple->tv_copyID;
6340
6341 tuple->tv_copyID = copyID;
6342 *tofree = tuple2string(tv, copyID, restore_copyID);
6343 if (restore_copyID)
6344 tuple->tv_copyID = old_copyID;
6345 r = *tofree;
6346 }
6347
6348 return r;
6349}
6350
6351/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006352 * Return a textual representation of a Dict in "tv".
6353 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6354 * When "copyID" is not zero replace recursive dicts with "...".
6355 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6356 * replaced with "...". May return NULL.
6357 */
6358 static char_u *
6359dict_tv2string(
6360 typval_T *tv,
6361 char_u **tofree,
6362 int copyID,
6363 int restore_copyID)
6364{
6365 char_u *r = NULL;
6366
6367 if (tv->vval.v_dict == NULL)
6368 {
6369 // NULL dict is equivalent to empty dict.
6370 *tofree = NULL;
6371 r = (char_u *)"{}";
6372 }
6373 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6374 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6375 {
6376 *tofree = NULL;
6377 r = (char_u *)"{...}";
6378 }
6379 else
6380 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006381 int old_copyID;
6382 if (restore_copyID)
6383 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006384
6385 tv->vval.v_dict->dv_copyID = copyID;
6386 *tofree = dict2string(tv, copyID, restore_copyID);
6387 if (restore_copyID)
6388 tv->vval.v_dict->dv_copyID = old_copyID;
6389 r = *tofree;
6390 }
6391
6392 return r;
6393}
6394
6395/*
6396 * Return a textual representation of a job or a channel in "tv".
6397 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6398 * "numbuf" is used for a number.
6399 * When "composite_val" is FALSE, put quotes around strings as "string()",
6400 * otherwise does not put quotes around strings.
6401 * May return NULL.
6402 */
6403 static char_u *
6404jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006405 typval_T *tv UNUSED,
6406 char_u **tofree UNUSED,
6407 char_u *numbuf UNUSED,
6408 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006409{
6410 char_u *r = NULL;
6411
6412#ifdef FEAT_JOB_CHANNEL
6413 *tofree = NULL;
6414
6415 if (tv->v_type == VAR_JOB)
6416 r = job_to_string_buf(tv, numbuf);
6417 else
6418 r = channel_to_string_buf(tv, numbuf);
6419
6420 if (composite_val)
6421 {
6422 *tofree = string_quote(r, FALSE);
6423 r = *tofree;
6424 }
6425#endif
6426
6427 return r;
6428}
6429
6430/*
6431 * Return a textual representation of a class in "tv".
6432 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6433 * May return NULL.
6434 */
6435 static char_u *
6436class_tv2string(typval_T *tv, char_u **tofree)
6437{
6438 char_u *r = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01006439 size_t rsize;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006440 class_T *cl = tv->vval.v_class;
John Marriottbd4614f2024-11-18 20:25:21 +01006441 char_u *class_name = (char_u *)"[unknown]";
6442 size_t class_namelen = 9;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006443 char *s = "class";
John Marriottbd4614f2024-11-18 20:25:21 +01006444 size_t slen = 5;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006445
John Marriottbd4614f2024-11-18 20:25:21 +01006446 if (cl != NULL)
6447 {
6448 class_name = cl->class_name;
6449 class_namelen = STRLEN(cl->class_name);
6450 if (IS_INTERFACE(cl))
6451 {
6452 s = "interface";
6453 slen = 9;
6454 }
6455 else if (IS_ENUM(cl))
6456 {
6457 s = "enum";
6458 slen = 4;
6459 }
6460 }
6461
6462 rsize = slen + 1 + class_namelen + 1;
6463 r = *tofree = alloc(rsize);
6464 if (r != NULL)
6465 vim_snprintf((char *)r, rsize, "%s %s", s, (char *)class_name);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006466
6467 return r;
6468}
6469
6470/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006471 * Return a textual representation of an Object in "tv".
6472 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6473 * When "copyID" is not zero replace recursive object with "...".
6474 * When "restore_copyID" is FALSE, repeated items in the object are
6475 * replaced with "...". May return NULL.
6476 */
6477 static char_u *
6478object_tv2string(
6479 typval_T *tv,
6480 char_u **tofree,
6481 int copyID,
6482 int restore_copyID,
6483 char_u *numbuf,
6484 int echo_style,
6485 int composite_val)
6486{
6487 char_u *r = NULL;
6488
6489 object_T *obj = tv->vval.v_object;
6490 if (obj == NULL || obj->obj_class == NULL)
6491 {
6492 *tofree = NULL;
6493 r = (char_u *)"object of [unknown]";
6494 }
6495 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006496 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006497 {
John Marriottbd4614f2024-11-18 20:25:21 +01006498 size_t n = 25 + STRLEN((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006499 r = alloc(n);
6500 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006501 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006502 obj->obj_class->class_name);
6503 *tofree = r;
6504 }
6505 else
6506 {
6507 int old_copyID;
6508 if (restore_copyID)
6509 old_copyID = obj->obj_copyID;
6510
6511 obj->obj_copyID = copyID;
6512 *tofree = object2string(obj, numbuf, copyID, echo_style,
6513 restore_copyID, composite_val);
6514 if (restore_copyID)
6515 obj->obj_copyID = old_copyID;
6516 r = *tofree;
6517 }
6518
6519 return r;
6520}
6521
6522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Return a string with the string representation of a variable.
6524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006526 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006527 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006528 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006529 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006530 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6531 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006532 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006534 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006535echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536 typval_T *tv,
6537 char_u **tofree,
6538 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006539 int copyID,
6540 int echo_style,
6541 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006542 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 static int recurse = 0;
6545 char_u *r = NULL;
6546
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006549 if (!did_echo_string_emsg)
6550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // Only give this message once for a recursive call to avoid
6552 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006553 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006554 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006555 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006558 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006559 }
6560 ++recurse;
6561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 switch (tv->v_type)
6563 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006564 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006565 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006566 break;
6567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006569 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006571
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006572 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006573 if (tv->vval.v_partial == NULL
6574 || tv->vval.v_partial->pt_obj == NULL)
6575 r = partial_tv2string(tv, tofree, numbuf, copyID);
6576 else
6577 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006578 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006579
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006580 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006581 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006582 break;
6583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006585 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006587
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006588 case VAR_TUPLE:
6589 r = tuple_tv2string(tv, tofree, copyID, restore_copyID);
6590 break;
6591
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006593 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006597 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006598 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006600 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006601 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006602 break;
6603
Bram Moolenaar835dc632016-02-07 14:27:38 +01006604 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006605 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006606 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006608
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006609 case VAR_INSTR:
6610 *tofree = NULL;
6611 r = (char_u *)"instructions";
6612 break;
6613
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006614 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006615 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006616 break;
6617
6618 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006619 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6620 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006621 break;
6622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006623 case VAR_FLOAT:
6624 *tofree = NULL;
6625 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6626 r = numbuf;
6627 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006628
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006629 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006630 case VAR_SPECIAL:
6631 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006632 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006633 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006634
6635 case VAR_TYPEALIAS:
6636 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6637 r = *tofree;
6638 if (r == NULL)
6639 r = (char_u *)"";
6640 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642
Bram Moolenaar8502c702014-06-17 12:51:16 +02006643 if (--recurse == 0)
6644 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006645 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006646}
6647
6648/*
6649 * Return a string with the string representation of a variable.
6650 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6651 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006652 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006653 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006654 * May return NULL.
6655 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006657echo_string(
6658 typval_T *tv,
6659 char_u **tofree,
6660 char_u *numbuf,
6661 int copyID)
6662{
6663 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6664}
6665
6666/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006667 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6668 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006669 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006670 */
6671 int
6672buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6673{
6674 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006675 char_u *t;
6676 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006677
6678 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6679 return -1;
6680
6681 if (lnum > buf->b_ml.ml_line_count)
6682 lnum = buf->b_ml.ml_line_count;
6683
6684 str = ml_get_buf(buf, lnum, FALSE);
6685 if (str == NULL)
6686 return -1;
6687
6688 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006689 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006690
Bram Moolenaar91458462021-01-13 20:08:38 +01006691 // count the number of characters
6692 t = str;
6693 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6694 t += mb_ptr2len(t);
6695
6696 // In insert mode, when the cursor is at the end of a non-empty line,
6697 // byteidx points to the NUL character immediately past the end of the
6698 // string. In this case, add one to the character count.
6699 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6700 count++;
6701
6702 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006703}
6704
6705/*
6706 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006707 * byte index. Works only for loaded buffers. Returns -1 on failure.
6708 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006709 */
6710 int
6711buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6712{
6713 char_u *str;
6714 char_u *t;
6715
6716 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6717 return -1;
6718
6719 if (lnum > buf->b_ml.ml_line_count)
6720 lnum = buf->b_ml.ml_line_count;
6721
6722 str = ml_get_buf(buf, lnum, FALSE);
6723 if (str == NULL)
6724 return -1;
6725
6726 // Convert the character offset to a byte offset
6727 t = str;
6728 while (*t != NUL && --charidx > 0)
6729 t += mb_ptr2len(t);
6730
Bram Moolenaar91458462021-01-13 20:08:38 +01006731 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006732}
6733
6734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006736 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006739var2fpos(
6740 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006741 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006742 int *fnum, // set to fnum for '0, 'A, etc.
6743 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006745 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006747 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006749 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006750 if (varp->v_type == VAR_LIST)
6751 {
6752 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006753 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006754 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006755 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006756
6757 l = varp->vval.v_list;
6758 if (l == NULL)
6759 return NULL;
6760
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006761 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006762 pos.lnum = list_find_nr(l, 0L, &error);
6763 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006764 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006765 if (charcol)
6766 len = (long)mb_charlen(ml_get(pos.lnum));
6767 else
John Marriottbfcc8952024-03-11 22:04:45 +01006768 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006769
Bram Moolenaarec65d772020-08-20 22:29:12 +02006770 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006771 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006772 li = list_find(l, 1L);
6773 if (li != NULL && li->li_tv.v_type == VAR_STRING
6774 && li->li_tv.vval.v_string != NULL
6775 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006776 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006777 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006778 }
6779 else
6780 {
6781 pos.col = list_find_nr(l, 1L, &error);
6782 if (error)
6783 return NULL;
6784 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006787 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006788 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006789 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006791 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006792 pos.coladd = list_find_nr(l, 2L, &error);
6793 if (error)
6794 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006795
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006796 return &pos;
6797 }
6798
Bram Moolenaarc5809432021-03-27 21:23:30 +01006799 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6800 return NULL;
6801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006802 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 if (name == NULL)
6804 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006805
6806 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006807 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006808 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006809 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006810 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006811 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006812 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006813 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006814 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006815 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006816 pos = VIsual;
6817 else
6818 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006819 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006820 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006821 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006823 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006824 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6826 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006827 pos = *pp;
6828 }
6829 if (pos.lnum != 0)
6830 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006831 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006832 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6833 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006835
Bram Moolenaara5525202006-03-02 22:52:09 +00006836 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006837
Bram Moolenaar477933c2007-07-17 14:32:23 +00006838 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006839 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006840 // the "w_valid" flags are not reset when moving the cursor, but they
6841 // do matter for update_topline() and validate_botline().
6842 check_cursor_moved(curwin);
6843
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006844 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006845 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006846 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006847 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006848 // In silent Ex mode topline is zero, but that's not a valid line
6849 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006850 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006851 return &pos;
6852 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006854 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006855 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006856 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006857 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006858 return &pos;
6859 }
6860 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006861 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006863 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
6865 pos.lnum = curbuf->b_ml.ml_line_count;
6866 pos.col = 0;
6867 }
6868 else
6869 {
6870 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006871 if (charcol)
6872 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6873 else
John Marriottbfcc8952024-03-11 22:04:45 +01006874 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 return &pos;
6877 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006878 if (in_vim9script())
6879 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 return NULL;
6881}
6882
6883/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006884 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006885 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006886 * Note that the column is passed on as-is, the caller may want to decrement
6887 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006888 * If "charcol" is TRUE use the column as the character index instead of the
6889 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006890 * Return FAIL when conversion is not possible, doesn't check the position for
6891 * validity.
6892 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006894list2fpos(
6895 typval_T *arg,
6896 pos_T *posp,
6897 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006898 colnr_T *curswantp,
6899 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006900{
6901 list_T *l = arg->vval.v_list;
6902 long i = 0;
6903 long n;
6904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006905 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6906 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006907 if (arg->v_type != VAR_LIST
6908 || l == NULL
6909 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006910 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006911 return FAIL;
6912
6913 if (fnump != NULL)
6914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006916 if (n < 0)
6917 return FAIL;
6918 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006920 *fnump = n;
6921 }
6922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006924 if (n < 0)
6925 return FAIL;
6926 posp->lnum = n;
6927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006929 if (n < 0)
6930 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006931 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006932 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006933 if (charcol)
6934 {
6935 buf_T *buf;
6936
6937 // Get the text for the specified line in a loaded buffer
6938 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6939 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6940 return FAIL;
6941
Bram Moolenaar79f23442022-10-10 12:42:57 +01006942 n = buf_charidx_to_byteidx(buf,
6943 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006944 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006945 posp->col = n;
6946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006948 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006949 posp->coladd = 0;
6950 else
6951 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006952
Bram Moolenaar493c1782014-05-28 14:34:46 +02006953 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006955
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006956 return OK;
6957}
6958
6959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * Get the length of an environment variable name.
6961 * Advance "arg" to the first character after the name.
6962 * Return 0 for error.
6963 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 char_u *p;
6968 int len;
6969
6970 for (p = *arg; vim_isIDc(*p); ++p)
6971 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 return 0;
6974
6975 len = (int)(p - *arg);
6976 *arg = p;
6977 return len;
6978}
6979
6980/*
6981 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006982 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 * Return 0 if something is wrong.
6984 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 char_u *p;
6989 int len;
6990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006993 {
6994 if (*p == ':')
6995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // "s:" is start of "s:var", but "n:" is not and can be used in
6997 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006998 len = (int)(p - *arg);
6999 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
7000 || len > 1)
7001 break;
7002 }
7003 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007004 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 return 0;
7006
7007 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007008 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 return len;
7011}
7012
7013/*
Bram Moolenaara7043832005-01-21 11:56:39 +00007014 * Get the length of the name of a variable or function.
7015 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017 * Return -1 if curly braces expansion failed.
7018 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 * If the name contains 'magic' {}'s, expand them and return the
7020 * expanded name in an allocated string via 'alias' - caller must free.
7021 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023get_name_len(
7024 char_u **arg,
7025 char_u **alias,
7026 int evaluate,
7027 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 char_u *p;
7031 char_u *expr_start;
7032 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007034 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
7037 && (*arg)[2] == (int)KE_SNR)
7038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007039 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 *arg += 3;
7041 return get_id_len(arg) + 3;
7042 }
7043 len = eval_fname_script(*arg);
7044 if (len > 0)
7045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007046 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 *arg += len;
7048 }
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007051 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007053 p = find_name_end(*arg, &expr_start, &expr_end,
7054 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 if (expr_start != NULL)
7056 {
7057 char_u *temp_string;
7058
7059 if (!evaluate)
7060 {
7061 len += (int)(p - *arg);
7062 *arg = skipwhite(p);
7063 return len;
7064 }
7065
7066 /*
7067 * Include any <SID> etc in the expanded string:
7068 * Thus the -len here.
7069 */
7070 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
7071 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007072 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 *alias = temp_string;
7074 *arg = skipwhite(p);
7075 return (int)STRLEN(temp_string);
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007079 // Only give an error when there is something, otherwise it will be
7080 // reported at a higher level.
7081 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007082 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 return len;
7085}
7086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007087/*
7088 * Find the end of a variable or function name, taking care of magic braces.
7089 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7090 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007091 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092 * Return a pointer to just after the name. Equal to "arg" if there is no
7093 * valid name.
7094 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096find_name_end(
7097 char_u *arg,
7098 char_u **expr_start,
7099 char_u **expr_end,
7100 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007102 int mb_nest = 0;
7103 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007105 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02007106 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007108 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007110 *expr_start = NULL;
7111 *expr_end = NULL;
7112 }
7113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007115 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007116 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007117 return arg;
7118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 for (p = arg; *p != NUL
7120 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007121 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02007122 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007123 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007124 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007125 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007127 if (*p == '\'')
7128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007129 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007130 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007131 ;
7132 if (*p == NUL)
7133 break;
7134 }
7135 else if (*p == '"')
7136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007137 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007138 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007139 if (*p == '\\' && p[1] != NUL)
7140 ++p;
7141 if (*p == NUL)
7142 break;
7143 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007144 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007146 // "s:" is start of "s:var", but "n:" is not and can be used in
7147 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007148 len = (int)(p - arg);
7149 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007150 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007151 break;
7152 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156 if (*p == '[')
7157 ++br_nest;
7158 else if (*p == ']')
7159 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007161
zeertzjqa93d9cd2023-05-02 16:25:47 +01007162 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164 if (*p == '{')
7165 {
7166 mb_nest++;
7167 if (expr_start != NULL && *expr_start == NULL)
7168 *expr_start = p;
7169 }
7170 else if (*p == '}')
7171 {
7172 mb_nest--;
7173 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7174 *expr_end = p;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178
7179 return p;
7180}
7181
7182/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007183 * Expands out the 'magic' {}'s in a variable/function name.
7184 * Note that this can call itself recursively, to deal with
7185 * constructs like foo{bar}{baz}{bam}
7186 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7187 * "in_start" ^
7188 * "expr_start" ^
7189 * "expr_end" ^
7190 * "in_end" ^
7191 *
7192 * Returns a new allocated string, which the caller must free.
7193 * Returns NULL for failure.
7194 */
7195 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196make_expanded_name(
7197 char_u *in_start,
7198 char_u *expr_start,
7199 char_u *expr_end,
7200 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007201{
7202 char_u c1;
7203 char_u *retval = NULL;
7204 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007205
7206 if (expr_end == NULL || in_end == NULL)
7207 return NULL;
7208 *expr_start = NUL;
7209 *expr_end = NUL;
7210 c1 = *in_end;
7211 *in_end = NUL;
7212
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007213 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007214 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007215 {
John Marriottbd4614f2024-11-18 20:25:21 +01007216 size_t retvalsize = (size_t)(expr_start - in_start)
7217 + STRLEN(temp_result)
7218 + (size_t)(in_end - expr_end) + 1;
7219
7220 retval = alloc(retvalsize);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007221 if (retval != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01007222 vim_snprintf((char *)retval, retvalsize, "%s%s%s",
7223 in_start, temp_result, expr_end + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007224 }
7225 vim_free(temp_result);
7226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007227 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007228 *expr_start = '{';
7229 *expr_end = '}';
7230
7231 if (retval != NULL)
7232 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007233 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007234 if (expr_start != NULL)
7235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007236 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007237 temp_result = make_expanded_name(retval, expr_start,
7238 expr_end, temp_result);
7239 vim_free(retval);
7240 retval = temp_result;
7241 }
7242 }
7243
7244 return retval;
7245}
7246
7247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007249 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007254 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007255}
7256
7257/*
7258 * Return TRUE if character "c" can be used as the first character in a
7259 * variable or function name (excluding '{' and '}').
7260 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007262eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007263{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007264 return ASCII_ISALPHA(c) || c == '_';
7265}
7266
7267/*
7268 * Return TRUE if character "c" can be used as the first character of a
7269 * dictionary key.
7270 */
7271 int
7272eval_isdictc(int c)
7273{
7274 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007278 * Handle:
7279 * - expr[expr], expr[expr:expr] subscript
7280 * - ".name" lookup
7281 * - function call with Funcref variable: func(expr)
7282 * - method call: var->method()
7283 *
7284 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007285 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007286 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288handle_subscript(
7289 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007290 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007292 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007293 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007294{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007295 int evaluate = evalarg != NULL
7296 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007297 int ret = OK;
7298 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007299 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007300 int getnext;
7301 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007302
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007303 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007304 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007305 // When at the end of the line and ".name" or "->{" or "->X" follows in
7306 // the next line then consume the line break.
7307 p = eval_next_non_blank(*arg, evalarg, &getnext);
7308 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007309 && ((*p == '.'
7310 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7311 || rettv->v_type == VAR_CLASS
7312 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007313 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7314 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7315 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007316 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007317 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007318 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007319 check_white = FALSE;
7320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007321
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007322 if (rettv->v_type == VAR_ANY)
7323 {
7324 char_u *exp_name;
7325 int cc;
7326 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007327 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007328 type_T *type;
7329
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007330 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007331 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007332 if (**arg != '.')
7333 {
7334 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007335 semsg(_(e_expected_dot_after_name_str),
7336 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007337 ret = FAIL;
7338 break;
7339 }
7340 ++*arg;
7341 if (IS_WHITE_OR_NUL(**arg))
7342 {
7343 if (verbose)
7344 emsg(_(e_no_white_space_allowed_after_dot));
7345 ret = FAIL;
7346 break;
7347 }
7348
7349 // isolate the name
7350 exp_name = *arg;
7351 while (eval_isnamec(**arg))
7352 ++*arg;
7353 cc = **arg;
7354 **arg = NUL;
7355
7356 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007357 evalarg == NULL ? NULL : evalarg->eval_cctx,
7358 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007359 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007360
7361 if (idx < 0 && ufunc == NULL)
7362 {
7363 ret = FAIL;
7364 break;
7365 }
7366 if (idx >= 0)
7367 {
7368 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7369 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7370
7371 copy_tv(sv->sv_tv, rettv);
7372 }
7373 else
7374 {
7375 rettv->v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01007376 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007377 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007378 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007379 }
7380
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007381 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7382 || rettv->v_type == VAR_PARTIAL))
7383 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007384 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007385 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7386 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007387
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007388 // Stop the expression evaluation when immediately aborting on
7389 // error, or when an interrupt occurred or an exception was thrown
7390 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007391 if (aborting())
7392 {
7393 if (ret == OK)
7394 clear_tv(rettv);
7395 ret = FAIL;
7396 }
7397 dict_unref(selfdict);
7398 selfdict = NULL;
7399 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007400 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007401 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007402 if (in_vim9script())
7403 *arg = skipwhite(p + 2);
7404 else
7405 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007406 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007407 {
zeertzjqc481ad32023-03-11 16:18:51 +00007408 emsg(_(e_no_white_space_allowed_before_parenthesis));
7409 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007410 }
zeertzjqc481ad32023-03-11 16:18:51 +00007411 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7412 // expr->{lambda}() or expr->(lambda)()
7413 ret = eval_lambda(arg, rettv, evalarg, verbose);
7414 else
7415 // expr->name()
7416 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007417 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007418 // "." is ".name" lookup when we found a dict or when evaluating and
7419 // scriptversion is at least 2, where string concatenation is "..".
7420 else if (**arg == '['
7421 || (**arg == '.' && (rettv->v_type == VAR_DICT
7422 || (!evaluate
7423 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007424 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007425 {
7426 dict_unref(selfdict);
7427 if (rettv->v_type == VAR_DICT)
7428 {
7429 selfdict = rettv->vval.v_dict;
7430 if (selfdict != NULL)
7431 ++selfdict->dv_refcount;
7432 }
7433 else
7434 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007435 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007436 {
7437 clear_tv(rettv);
7438 ret = FAIL;
7439 }
7440 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007441 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7442 || rettv->v_type == VAR_OBJECT))
7443 {
7444 // class member: SomeClass.varname
7445 // class method: SomeClass.SomeMethod()
7446 // class constructor: SomeClass.new()
7447 // object member: someObject.varname
7448 // object method: someObject.SomeMethod()
7449 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7450 {
7451 clear_tv(rettv);
7452 ret = FAIL;
7453 }
7454 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007455 else
7456 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007457 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007459 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7460 // Don't do this when "Func" is already a partial that was bound
7461 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007462 if (selfdict != NULL
7463 && (rettv->v_type == VAR_FUNC
7464 || (rettv->v_type == VAR_PARTIAL
7465 && (rettv->vval.v_partial->pt_auto
7466 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007467 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007469 dict_unref(selfdict);
7470 return ret;
7471}
7472
7473/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 * Make a copy of an item.
7475 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007476 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7478 * reference to an already copied list/dict can be used.
7479 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482item_copy(
7483 typval_T *from,
7484 typval_T *to,
7485 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007486 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488{
7489 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007490 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007494 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007495 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496 }
7497 ++recurse;
7498
7499 switch (from->v_type)
7500 {
7501 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007502 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 case VAR_STRING:
7504 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007505 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007506 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007507 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007508 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007509 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007510 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007511 case VAR_CLASS:
7512 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007513 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 copy_tv(from, to);
7515 break;
7516 case VAR_LIST:
7517 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007518 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007519 if (from->vval.v_list == NULL)
7520 to->vval.v_list = NULL;
7521 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007523 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007524 to->vval.v_list = from->vval.v_list->lv_copylist;
7525 ++to->vval.v_list->lv_refcount;
7526 }
7527 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007528 to->vval.v_list = list_copy(from->vval.v_list,
7529 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007530 if (to->vval.v_list == NULL)
7531 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007533 case VAR_TUPLE:
7534 to->v_type = VAR_TUPLE;
7535 to->v_lock = 0;
7536 if (from->vval.v_tuple == NULL)
7537 to->vval.v_tuple = NULL;
7538 else if (copyID != 0 && from->vval.v_tuple->tv_copyID == copyID)
7539 {
7540 // use the copy made earlier
7541 to->vval.v_tuple = from->vval.v_tuple->tv_copytuple;
7542 ++to->vval.v_tuple->tv_refcount;
7543 }
7544 else
7545 to->vval.v_tuple = tuple_copy(from->vval.v_tuple,
7546 deep, top, copyID);
7547 if (to->vval.v_tuple == NULL)
7548 ret = FAIL;
7549 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007550 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007552 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007553 case VAR_DICT:
7554 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007556 if (from->vval.v_dict == NULL)
7557 to->vval.v_dict = NULL;
7558 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007560 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007561 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7562 ++to->vval.v_dict->dv_refcount;
7563 }
7564 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007565 to->vval.v_dict = dict_copy(from->vval.v_dict,
7566 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007567 if (to->vval.v_dict == NULL)
7568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007570 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007571 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007573 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007574 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 }
7576 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007578}
7579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 void
7581echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7582{
7583 char_u *tofree;
7584 char_u numbuf[NUMBUFLEN];
7585 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7586
7587 if (*atstart)
7588 {
7589 *atstart = FALSE;
7590 // Call msg_start() after eval1(), evaluating the expression
7591 // may cause a message to appear.
7592 if (with_space)
7593 {
7594 // Mark the saved text as finishing the line, so that what
7595 // follows is displayed on a new line when scrolling back
7596 // at the more prompt.
7597 msg_sb_eol();
7598 msg_start();
7599 }
7600 }
7601 else if (with_space)
7602 msg_puts_attr(" ", echo_attr);
7603
7604 if (p != NULL)
7605 for ( ; *p != NUL && !got_int; ++p)
7606 {
7607 if (*p == '\n' || *p == '\r' || *p == TAB)
7608 {
7609 if (*p != TAB && *needclr)
7610 {
7611 // remove any text still there from the command
7612 msg_clr_eos();
7613 *needclr = FALSE;
7614 }
7615 msg_putchar_attr(*p, echo_attr);
7616 }
7617 else
7618 {
7619 if (has_mbyte)
7620 {
7621 int i = (*mb_ptr2len)(p);
7622
7623 (void)msg_outtrans_len_attr(p, i, echo_attr);
7624 p += i - 1;
7625 }
7626 else
7627 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7628 }
7629 }
7630 vim_free(tofree);
7631}
7632
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 * ":echo expr1 ..." print each argument separated with a space, add a
7635 * newline at the end.
7636 * ":echon expr1 ..." print each argument plain.
7637 */
7638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007643 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 int needclr = TRUE;
7645 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007646 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007647 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007648 evalarg_T evalarg;
7649
Bram Moolenaare707c882020-07-01 13:07:37 +02007650 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652 if (eap->skip)
7653 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007654 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007656 // If eval1() causes an error message the text from the command may
7657 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007658 need_clr_eos = needclr;
7659
Bram Moolenaar68db9962021-05-09 23:19:22 +02007660 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007661 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
7663 /*
7664 * Report the invalid expression unless the expression evaluation
7665 * has been cancelled due to an aborting error, an interrupt, or an
7666 * exception.
7667 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007668 if (!aborting() && did_emsg == did_emsg_before
7669 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007670 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 break;
7673 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674 need_clr_eos = FALSE;
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007677 {
7678 if (rettv.v_type == VAR_VOID)
7679 {
7680 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7681 break;
7682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007685
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 arg = skipwhite(arg);
7688 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007689 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007690 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691
7692 if (eap->skip)
7693 --emsg_skip;
7694 else
7695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (needclr)
7698 msg_clr_eos();
7699 if (eap->cmdidx == CMD_echo)
7700 msg_end();
7701 }
7702}
7703
7704/*
7705 * ":echohl {name}".
7706 */
7707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007708ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007710 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007714 * Returns the :echo attribute
7715 */
7716 int
7717get_echo_attr(void)
7718{
7719 return echo_attr;
7720}
7721
7722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 * ":execute expr1 ..." execute the result of an expression.
7724 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007725 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007727 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 * Each gets spaces around each argument and a newline at the end for
7729 * echo commands
7730 */
7731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 int ret = OK;
7737 char_u *p;
7738 garray_T ga;
7739 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007740 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
7742 ga_init2(&ga, 1, 80);
7743
7744 if (eap->skip)
7745 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007746 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007748 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007749 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 if (!eap->skip)
7753 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007754 char_u buf[NUMBUFLEN];
7755
7756 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007757 {
7758 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7759 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007760 semsg(_(e_using_invalid_value_as_string_str),
7761 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007762 p = NULL;
7763 }
7764 else
7765 p = tv_get_string_buf(&rettv, buf);
7766 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007767 else
7768 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007769 if (p == NULL)
7770 {
7771 clear_tv(&rettv);
7772 ret = FAIL;
7773 break;
7774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 len = (int)STRLEN(p);
7776 if (ga_grow(&ga, len + 2) == FAIL)
7777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 ret = FAIL;
7780 break;
7781 }
7782 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 ga.ga_len += len;
7786 }
7787
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 arg = skipwhite(arg);
7790 }
7791
7792 if (ret != FAIL && ga.ga_data != NULL)
7793 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007794 // use the first line of continuation lines for messages
7795 SOURCING_LNUM = start_lnum;
7796
Bram Moolenaar37fef162022-08-29 18:16:32 +01007797 if (eap->cmdidx == CMD_echomsg
7798 || eap->cmdidx == CMD_echowindow
7799 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007801 // Mark the already saved text as finishing the line, so that what
7802 // follows is displayed on a new line when scrolling back at the
7803 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007804 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007805 }
7806
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007807 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007808 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007809 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007810 out_flush();
7811 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007812 else if (eap->cmdidx == CMD_echowindow)
7813 {
7814#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007815 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007816#endif
7817 msg_attr(ga.ga_data, echo_attr);
7818#ifdef HAS_MESSAGE_WINDOW
7819 end_echowindow();
7820#endif
7821 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007822 else if (eap->cmdidx == CMD_echoconsole)
7823 {
7824 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7825 ui_write((char_u *)"\r\n", 2, TRUE);
7826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else if (eap->cmdidx == CMD_echoerr)
7828 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007829 int save_did_emsg = did_emsg;
7830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007831 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007832 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (!force_abort)
7834 did_emsg = save_did_emsg;
7835 }
7836 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007837 {
7838 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7839
7840 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7841 sticky_cmdmod_flags = cmdmod.cmod_flags
7842 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007844 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007845 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848
7849 ga_clear(&ga);
7850
7851 if (eap->skip)
7852 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007853 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
7857 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7858 * "arg" points to the "&" or '+' when called, to "option" when returning.
7859 * Returns NULL when no option name found. Otherwise pointer to the char
7860 * after the option name.
7861 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007862 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007863find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 char_u *p = *arg;
7866
7867 ++p;
7868 if (*p == 'g' && p[1] == ':')
7869 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007870 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 p += 2;
7872 }
7873 else if (*p == 'l' && p[1] == ':')
7874 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007875 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 p += 2;
7877 }
7878 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007879 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 if (!ASCII_ISALPHA(*p))
7882 return NULL;
7883 *arg = p;
7884
7885 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007886 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 else
7888 while (ASCII_ISALPHA(*p))
7889 ++p;
7890 return p;
7891}
7892
7893/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007894 * Display script name where an item was last set.
7895 * Should only be invoked when 'verbose' is non-zero.
7896 */
7897 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007898last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007899{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007900 char_u *p;
7901
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007902 if (script_ctx.sc_sid == 0)
7903 return;
7904
7905 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7906 if (p == NULL)
7907 return;
7908
7909 verbose_enter();
7910 msg_puts(_("\n\tLast set from "));
7911 msg_puts((char *)p);
7912 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007913 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007914 msg_puts(_(line_msg));
7915 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007916 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007917 verbose_leave();
7918 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007919}
7920
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007921#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923/*
7924 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007925 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 * "flags" can be "g" to do a global substitute.
7927 * Returns an allocated string, NULL for error.
7928 */
7929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007930do_string_sub(
7931 char_u *str,
John Marriottbd4614f2024-11-18 20:25:21 +01007932 size_t len,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933 char_u *pat,
7934 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007935 typval_T *expr,
John Marriottbd4614f2024-11-18 20:25:21 +01007936 char_u *flags,
7937 size_t *ret_len) // length of returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 garray_T ga;
7941 char_u *ret;
7942 char_u *save_cpo;
7943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007944 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007946 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
7948 ga_init2(&ga, 1, 200);
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 regmatch.rm_ic = p_ic;
7951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7952 if (regmatch.regprog != NULL)
7953 {
John Marriottbd4614f2024-11-18 20:25:21 +01007954 char_u *tail = str;
7955 char_u *end = str + len;
7956 int do_all = (flags[0] == 'g');
7957 int sublen;
7958 int i;
7959 char_u *zero_width = NULL;
7960
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007963 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007964 if (regmatch.startp[0] == regmatch.endp[0])
7965 {
7966 if (zero_width == regmatch.startp[0])
7967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007968 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007969 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007970 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7971 (size_t)i);
7972 ga.ga_len += i;
7973 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007974 continue;
7975 }
7976 zero_width = regmatch.startp[0];
7977 }
7978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 /*
7980 * Get some space for a temporary buffer to do the substitution
7981 * into. It will contain:
7982 * - The text up to where the match is.
7983 * - The substituted text.
7984 * - The text after the match.
7985 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007986 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007987 if (sublen <= 0)
7988 {
7989 ga_clear(&ga);
7990 break;
7991 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007992 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7994 {
7995 ga_clear(&ga);
7996 break;
7997 }
7998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007999 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 i = (int)(regmatch.startp[0] - tail);
8001 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008002 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008003 (void)vim_regsub(&regmatch, sub, expr,
8004 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
8005 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02008007 tail = regmatch.endp[0];
8008 if (*tail == NUL)
8009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (!do_all)
8011 break;
8012 }
8013
8014 if (ga.ga_data != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01008015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
John Marriottbd4614f2024-11-18 20:25:21 +01008017 ga.ga_len += (int)(end - tail);
8018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
Bram Moolenaar473de612013-06-08 18:19:48 +02008020 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022
John Marriottbd4614f2024-11-18 20:25:21 +01008023 if (ga.ga_data != NULL)
8024 {
8025 str = (char_u *)ga.ga_data;
8026 len = (size_t)ga.ga_len;
8027 }
8028 ret = vim_strnsave(str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008030 if (p_cpo == empty_option)
8031 p_cpo = save_cpo;
8032 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008034 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008035 // If it's still empty it was changed and restored, need to restore in
8036 // the complicated way.
8037 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008038 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008039 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
John Marriottbd4614f2024-11-18 20:25:21 +01008042 if (ret_len != NULL)
8043 *ret_len = len;
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 return ret;
8046}