blob: 9a140c16609566e860bb4a3b30b9a1146f33d228 [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
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200110 // unreferenced lists and dicts
111 (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 Lakshmananbce51d92024-04-15 19:19:52 +0200623 * When "join_list" is TRUE convert a List 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;
630 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100631
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200632 if (join_list && tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000634 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100635 if (tv->vval.v_list != NULL)
636 {
637 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
638 if (tv->vval.v_list->lv_len > 0)
639 ga_append(&ga, NL);
640 }
641 ga_append(&ga, NUL);
642 retval = (char_u *)ga.ga_data;
643 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200644 else if (tv->v_type == VAR_LIST || tv->v_type == VAR_DICT)
645 {
646 char_u *tofree;
647 char_u numbuf[NUMBUFLEN];
648
649 retval = tv2string(tv, &tofree, numbuf, 0);
650 // Make a copy if we have a value but it's not in allocated memory.
651 if (retval != NULL && tofree == NULL)
652 retval = vim_strsave(retval);
653 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100654 else
655 retval = vim_strsave(tv_get_string(tv));
656 return retval;
657}
658
659/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200660 * Top level evaluation function, returning a string. Does not handle line
661 * breaks.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200662 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * Return pointer to allocated memory, or NULL for failure.
664 */
665 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100666eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100667 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200668 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100669 exarg_T *eap,
670 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100674 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100675 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100677 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100678 if (use_simple_function)
679 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
680 else
681 r = eval0(arg, &tv, NULL, &evalarg);
682 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 retval = NULL;
684 else
685 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200686 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100689 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 return retval;
692}
693
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100694 char_u *
695eval_to_string(
696 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200697 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100698 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100699{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200700 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100701}
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000704 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100705 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200706 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709eval_to_string_safe(
710 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000711 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100712 int keep_script_version,
713 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200716 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200717 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200718 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar9530b582022-01-22 13:39:08 +0000720 if (!keep_script_version)
721 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200722 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000723 if (use_sandbox)
724 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100725 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200726 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100727 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000728 if (use_sandbox)
729 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100730 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200731 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200732 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200733 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 return retval;
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Top level evaluation function, returning a number.
739 * Evaluates "expr" silently.
740 * Returns -1 for an error.
741 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200742 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100743eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaar33570922005-01-25 22:26:29 +0000745 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200746 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000747 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100748 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750 ++emsg_off;
751
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100752 if (use_simple_function)
753 r = may_call_simple_func(expr, &rettv);
754 if (r == NOTDONE)
755 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
756 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 retval = -1;
758 else
759 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100760 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000761 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 --emsg_off;
764
765 return retval;
766}
767
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000768/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000769 * Top level evaluation function.
770 * Returns an allocated typval_T with the result.
771 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000772 */
773 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200774eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000775{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100776 return eval_expr_ext(arg, eap, FALSE);
777}
778
779 typval_T *
780eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
781{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000782 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200783 evalarg_T evalarg;
784
785 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000786
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200787 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100788 if (tv != NULL)
789 {
790 int r = NOTDONE;
791
792 if (use_simple_function)
793 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
794 if (r == NOTDONE)
795 r = eval0(arg, tv, eap, &evalarg);
796
797 if (r == FAIL)
798 VIM_CLEAR(tv);
799 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000800
Bram Moolenaar37c83712020-06-30 21:18:36 +0200801 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000802 return tv;
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000806 * "*arg" points to what can be a function name in the form of "import.Name" or
807 * "Funcref". Return the name of the function. Set "tofree" to something that
808 * was allocated.
809 * If "verbose" is FALSE no errors are given.
810 * Return NULL for any failure.
811 */
812 static char_u *
813deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000814 char_u **arg,
815 char_u **tofree,
816 evalarg_T *evalarg,
817 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000818{
819 typval_T ref;
820 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100821 int save_flags = 0;
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100822 int evaluate = evalarg != NULL
823 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000824
825 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100826 if (evalarg != NULL)
827 {
828 // need to evaluate this to get an import, like in "a.Func"
829 save_flags = evalarg->eval_flags;
830 evalarg->eval_flags |= EVAL_EVALUATE;
831 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100832 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100833 {
834 dictitem_T *v;
835
836 // If <SID>VarName was used it would not be found, try another way.
837 v = find_var_also_in_script(name, NULL, FALSE);
838 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100839 {
840 name = NULL;
841 goto theend;
842 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100843 copy_tv(&v->di_tv, &ref);
844 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000845 if (*skipwhite(*arg) != NUL)
846 {
847 if (verbose)
848 semsg(_(e_trailing_characters_str), *arg);
849 name = NULL;
850 }
851 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
852 {
853 name = ref.vval.v_string;
854 ref.vval.v_string = NULL;
855 *tofree = name;
856 }
857 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
858 {
859 if (ref.vval.v_partial->pt_argc > 0
860 || ref.vval.v_partial->pt_dict != NULL)
861 {
862 if (verbose)
863 emsg(_(e_cannot_use_partial_here));
864 name = NULL;
865 }
866 else
867 {
868 name = vim_strsave(partial_name(ref.vval.v_partial));
869 *tofree = name;
870 }
871 }
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100872 else if (evaluate)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000873 {
874 if (verbose)
875 semsg(_(e_not_callable_type_str), name);
876 name = NULL;
877 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100878
879theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000880 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100881 if (evalarg != NULL)
882 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000883 return name;
884}
885
886/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100887 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200888 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
889 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000890 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893call_vim_function(
894 char_u *func,
895 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200896 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200897 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200900 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000901 char_u *arg;
902 char_u *name;
903 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000904 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200907 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000908 funcexe.fe_firstline = curwin->w_cursor.lnum;
909 funcexe.fe_lastline = curwin->w_cursor.lnum;
910 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000911
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000912 // The name might be "import.Func" or "Funcref". We don't know, we need to
913 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000914 // autoload script has errors. Guess that when there is a dot in the name
915 // showing errors is the right choice.
916 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000917 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000918 if (ignore_errors)
919 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000920 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000921 if (ignore_errors)
922 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000923 if (name == NULL)
924 name = func;
925
926 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
927
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000928 if (ret == FAIL)
929 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000930 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000931
932 return ret;
933}
934
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100936 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000937 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
938 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000939 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000940 */
941 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100942call_func_retstr(
943 char_u *func,
944 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200945 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000946{
947 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000948 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949
Bram Moolenaarded27a12018-08-01 19:06:03 +0200950 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000951 return NULL;
952
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100953 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 return retval;
956}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000957
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000958/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100959 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000960 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000961 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100962 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000963 */
964 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100965call_func_retlist(
966 char_u *func,
967 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200968 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000969{
970 typval_T rettv;
971
Bram Moolenaarded27a12018-08-01 19:06:03 +0200972 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000973 return NULL;
974
975 if (rettv.v_type != VAR_LIST)
976 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100977 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
978 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000979 clear_tv(&rettv);
980 return NULL;
981 }
982
983 return rettv.vval.v_list;
984}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
Bram Moolenaare70dd112022-01-21 16:31:11 +0000986#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100988 * Evaluate "arg", which is 'foldexpr'.
989 * Note: caller must set "curwin" to match "arg".
990 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
991 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 */
993 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000994eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000996 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200998 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +00001000 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001001 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001002 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001004 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001005 current_sctx = wp->w_p_script_ctx[WV_FDE];
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001008 if (use_sandbox)
1009 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001010 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001012
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001013 // Evaluate the expression. If the expression is "FuncName()" call the
1014 // function directly.
1015 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 retval = 0;
1017 else
1018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001019 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 if (tv.v_type == VAR_NUMBER)
1021 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001022 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 retval = 0;
1024 else
1025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 // If the result is a string, check if there is a non-digit before
1027 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001028 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001029 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 *cp = *s++;
1031 retval = atol((char *)s);
1032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001036 if (use_sandbox)
1037 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001038 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001039 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001040 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001042 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044#endif
1045
Ernie Rael64885642023-10-04 20:16:22 +02001046#ifdef LOG_LOCKVAR
1047typedef struct flag_string_S
1048{
1049 int flag;
1050 char *str;
1051} flag_string_T;
1052
1053 static char *
1054flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1055{
1056 char *p = buf;
1057 *p = NUL;
1058 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1059 {
1060 if ((fstring->flag & flags) != 0)
1061 {
1062 size_t len = STRLEN(fstring->str);
1063 if (n > p - buf + len + 7)
1064 {
1065 STRCAT(p, fstring->str);
1066 p += len;
1067 STRCAT(p, " ");
1068 ++p;
1069 }
1070 else
1071 {
1072 STRCAT(buf, "...");
1073 break;
1074 }
1075 }
1076 }
1077 return buf;
1078}
1079
1080flag_string_T glv_flag_strings[] = {
1081 { GLV_QUIET, "QUIET" },
1082 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1083 { GLV_READ_ONLY, "READ_ONLY" },
1084 { GLV_NO_DECL, "NO_DECL" },
1085 { GLV_COMPILING, "COMPILING" },
1086 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1087 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1088 { 0, NULL }
1089};
1090#endif
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092/*
Ernie Raelee865f32023-09-29 19:53:55 +02001093 * Fill in "lp" using "root". This is used in a special case when
1094 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1095 *
1096 * This is typically called with "lval_root" as "root". For a class, find
1097 * the name from lp in the class from root, fill in lval_T if found. For a
1098 * complex type, list/dict use it as the result; just put the root into
1099 * ll_tv.
1100 *
1101 * "lval_root" is a hack used during run-time/instr-execution to provide the
1102 * starting point for "get_lval()" to traverse a chain of indexes. In some
1103 * cases get_lval sees a bare name and uses this function to populate the
1104 * lval_T.
1105 *
1106 * For setting up "lval_root" (currently only used with lockvar)
1107 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1108 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1109 */
1110 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001111fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001112{
1113#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001114 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1115 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001116#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001117 if (lr->lr_tv == NULL)
1118 return;
Ernie Rael64885642023-10-04 20:16:22 +02001119 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001120 {
Ernie Rael64885642023-10-04 20:16:22 +02001121 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001122 {
1123 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001124 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001125 int m_idx;
1126 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1127 lp->ll_name_end - lp->ll_name, &m_idx);
1128 if (m != NULL)
1129 {
1130 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001131 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001132 lp->ll_oi = m_idx;
1133 lp->ll_valtype = m->ocm_type;
1134 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1135#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001136 ch_log(NULL, "LKVAR: ... class member %s.%s",
1137 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001138#endif
1139 return;
1140 }
1141 }
1142 }
1143
1144#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001145 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001146#endif
Ernie Rael64885642023-10-04 20:16:22 +02001147 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001148 lp->ll_is_root = TRUE;
1149}
1150
1151/*
Ernie Rael64885642023-10-04 20:16:22 +02001152 * Check if the class has permission to access the member.
1153 * Returns OK or FAIL.
1154 */
1155 static int
1156get_lval_check_access(
1157 class_T *cl_exec, // executing class, NULL if :def or script level
1158 class_T *cl, // class which contains the member
1159 ocmember_T *om, // member being accessed
1160 char_u *p, // char after member name
1161 int flags) // GLV flags to check if writing to lval
1162{
1163#ifdef LOG_LOCKVAR
1164 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1165 (void*)cl_exec, (void*)cl, *p);
1166#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001167 if (cl_exec != NULL && cl_exec == cl)
1168 return OK;
1169
1170 char *msg = NULL;
1171 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001172 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001173 case VIM_ACCESS_PRIVATE:
1174 msg = e_cannot_access_protected_variable_str;
1175 break;
1176 case VIM_ACCESS_READ:
1177 // If [idx] or .key following, read only OK.
1178 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001179 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001180 if ((flags & GLV_READ_ONLY) == 0)
1181 {
1182 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001183 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001184 if (om->ocm_type->tt_type == VAR_OBJECT)
1185 semsg(_(e_enumvalue_str_cannot_be_modified),
1186 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001187 else
1188 msg = e_variable_is_not_writable_str;
1189 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001190 else
1191 msg = e_variable_is_not_writable_str;
1192 }
1193 break;
1194 case VIM_ACCESS_ALL:
1195 break;
1196 }
1197 if (msg != NULL)
1198 {
1199 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1200 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001201 }
1202 return OK;
1203}
1204
1205/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001206 * Get lval information for a variable imported from script "imp_sid". On
1207 * success, updates "lp" with the variable name, type, script ID and typval.
1208 * The variable name starts at or after "p".
1209 * If "rettv" is not NULL it points to the value to be assigned. This used to
1210 * match the rhs and lhs types.
1211 * Returns a pointer to the character after the variable name if the imported
1212 * variable is valid and writable.
1213 * Returns NULL if the variable is not exported or typval is not found or the
1214 * rhs type doesn't match the lhs type or the variable is not writable.
1215 */
1216 static char_u *
1217get_lval_imported(
1218 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001219 scid_T imp_sid,
1220 char_u *p,
1221 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001222 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001223{
1224 ufunc_T *ufunc;
1225 type_T *type = NULL;
1226 int cc;
1227 int rc = FAIL;
1228
1229 p = skipwhite(p);
1230
1231 import_check_sourced_sid(&imp_sid);
1232 lp->ll_sid = imp_sid;
1233 lp->ll_name = p;
1234 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1235 lp->ll_name_end = p;
1236
1237 // check the item is exported
1238 cc = *p;
1239 *p = NUL;
1240 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1241 TRUE) == -1)
1242 goto failed;
1243
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001244 // Get the typval for the exported item
1245 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1246 if (ht == NULL)
1247 goto failed;
1248
1249 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1250 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001251 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001252 goto success;
1253
1254 *dip = di;
1255
1256 // Check whether the variable is writable.
1257 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1258 if (sv != NULL && sv->sv_const != 0)
1259 {
1260 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1261 goto failed;
1262 }
1263
1264 // check whether variable is locked
1265 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1266 goto failed;
1267
1268 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001269 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001270
1271success:
1272 rc = OK;
1273
1274failed:
1275 *p = cc;
1276 return rc == OK ? p : NULL;
1277}
1278
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001279typedef enum {
1280 GLV_FAIL,
1281 GLV_OK,
1282 GLV_STOP
1283} glv_status_T;
1284
1285/*
1286 * Get an Dict lval variable that can be assigned a value to: "name",
1287 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1288 * "name" points to the start of the name.
1289 * If "rettv" is not NULL it points to the value to be assigned.
1290 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1291 * wrong; must end in space or cmd separator.
1292 *
1293 * flags:
1294 * GLV_QUIET: do not give error messages
1295 * GLV_READ_ONLY: will not change the variable
1296 * GLV_NO_AUTOLOAD: do not use script autoloading
1297 *
1298 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1299 * failure. Returns GLV_STOP to stop processing the characters following
1300 * 'key_end'.
1301 */
1302 static int
1303get_lval_dict_item(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001304 lval_T *lp,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001305 char_u *name,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001306 char_u *key,
1307 int len,
1308 char_u **key_end,
1309 typval_T *var1,
1310 int flags,
1311 int unlet,
1312 typval_T *rettv)
1313{
1314 int quiet = flags & GLV_QUIET;
1315 char_u *p = *key_end;
1316
1317 if (len == -1)
1318 {
1319 // "[key]": get key from "var1"
1320 key = tv_get_string_chk(var1); // is number or string
1321 if (key == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001322 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001323 }
1324 lp->ll_list = NULL;
1325 lp->ll_object = NULL;
1326 lp->ll_class = NULL;
1327
1328 // a NULL dict is equivalent with an empty dict
1329 if (lp->ll_tv->vval.v_dict == NULL)
1330 {
1331 lp->ll_tv->vval.v_dict = dict_alloc();
1332 if (lp->ll_tv->vval.v_dict == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001333 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001334 ++lp->ll_tv->vval.v_dict->dv_refcount;
1335 }
1336 lp->ll_dict = lp->ll_tv->vval.v_dict;
1337
1338 lp->ll_di = dict_find(lp->ll_dict, key, len);
1339
1340 // When assigning to a scope dictionary check that a function and
1341 // variable name is valid (only variable name unless it is l: or
1342 // g: dictionary). Disallow overwriting a builtin function.
1343 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1344 {
1345 int prevval;
1346
1347 if (len != -1)
1348 {
1349 prevval = key[len];
1350 key[len] = NUL;
1351 }
1352 else
1353 prevval = 0; // avoid compiler warning
1354 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1355 && (rettv->v_type == VAR_FUNC
1356 || rettv->v_type == VAR_PARTIAL)
1357 && var_wrong_func_name(key, lp->ll_di == NULL))
1358 || !valid_varname(key, -1, TRUE);
1359 if (len != -1)
1360 key[len] = prevval;
1361 if (wrong)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001362 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001363 }
1364
1365 if (lp->ll_valtype != NULL)
1366 // use the type of the member
1367 lp->ll_valtype = lp->ll_valtype->tt_member;
1368
1369 if (lp->ll_di == NULL)
1370 {
1371 // Can't add "v:" or "a:" variable.
1372 if (lp->ll_dict == get_vimvar_dict()
1373 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1374 {
1375 semsg(_(e_illegal_variable_name_str), name);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001376 return GLV_FAIL;
1377 }
1378
1379 // Key does not exist in dict: may need to add it.
1380 if (*p == '[' || *p == '.' || unlet)
1381 {
1382 if (!quiet)
1383 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001384 return GLV_FAIL;
1385 }
1386 if (len == -1)
1387 lp->ll_newkey = vim_strsave(key);
1388 else
1389 lp->ll_newkey = vim_strnsave(key, len);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001390 if (lp->ll_newkey == NULL)
1391 p = NULL;
1392
1393 *key_end = p;
1394 return GLV_STOP;
1395 }
1396 // existing variable, need to check if it can be changed
1397 else if ((flags & GLV_READ_ONLY) == 0
1398 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1399 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001400 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001401
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001402 lp->ll_tv = &lp->ll_di->di_tv;
1403
1404 return GLV_OK;
1405}
1406
1407/*
1408 * Get an blob lval variable that can be assigned a value to: "name",
1409 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1410 *
1411 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1412 * blob index. If the first index is not specified in a range, then 'empty1'
1413 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1414 * invalid indexes.
1415 *
1416 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1417 */
1418 static int
1419get_lval_blob(
1420 lval_T *lp,
1421 typval_T *var1,
1422 typval_T *var2,
1423 int empty1,
1424 int quiet)
1425{
1426 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1427
1428 // Get the number and item for the only or first index of the List.
1429 if (empty1)
1430 lp->ll_n1 = 0;
1431 else
1432 // is number or string
1433 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001434
1435 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001436 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001437 if (lp->ll_range && !lp->ll_empty2)
1438 {
1439 lp->ll_n2 = (long)tv_get_number(var2);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001440 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1441 return FAIL;
1442 }
1443
1444 if (!lp->ll_range)
1445 // Indexing a single byte in a blob. So the rhs type is a
1446 // number.
1447 lp->ll_valtype = &t_number;
1448
1449 lp->ll_blob = lp->ll_tv->vval.v_blob;
1450 lp->ll_tv = NULL;
1451
1452 return OK;
1453}
1454
1455/*
1456 * Get a List lval variable that can be assigned a value to: "name",
1457 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1458 *
1459 * 'var1' specifies the starting List index and 'var2' specifies the ending
1460 * List index. If the first index is not specified in a range, then 'empty1'
1461 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1462 * invalid indexes.
1463 *
1464 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1465 */
1466 static int
1467get_lval_list(
1468 lval_T *lp,
1469 typval_T *var1,
1470 typval_T *var2,
1471 int empty1,
1472 int flags,
1473 int quiet)
1474{
1475 /*
1476 * Get the number and item for the only or first index of the List.
1477 */
1478 if (empty1)
1479 lp->ll_n1 = 0;
1480 else
1481 // is number or string
1482 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001483
1484 lp->ll_dict = NULL;
1485 lp->ll_object = NULL;
1486 lp->ll_class = NULL;
1487 lp->ll_list = lp->ll_tv->vval.v_list;
1488 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1489 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1490 if (lp->ll_li == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001491 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001492
1493 if (lp->ll_valtype != NULL && !lp->ll_range)
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001494 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001495 // use the type of the member
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001496 if (lp->ll_valtype->tt_member != NULL)
1497 lp->ll_valtype = lp->ll_valtype->tt_member;
1498 else
1499 // If the LHS member type is not known (VAR_ANY), then get it from
1500 // the list item (after indexing)
1501 lp->ll_valtype = typval2type(&lp->ll_li->li_tv, get_copyID(),
1502 &lp->ll_type_list, TVTT_DO_MEMBER);
1503
1504 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001505
1506 /*
1507 * May need to find the item or absolute index for the second
1508 * index of a range.
1509 * When no index given: "lp->ll_empty2" is TRUE.
1510 * Otherwise "lp->ll_n2" is set to the second index.
1511 */
1512 if (lp->ll_range && !lp->ll_empty2)
1513 {
1514 lp->ll_n2 = (long)tv_get_number(var2);
1515 // is number or string
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001516 if (check_range_index_two(lp->ll_list,
1517 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1518 return FAIL;
1519 }
1520
1521 lp->ll_tv = &lp->ll_li->li_tv;
1522
1523 return OK;
1524}
1525
1526/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001527 * Get a class or object lval method in class "cl". The 'key' argument points
1528 * to the method name and 'key_end' points to the character after 'key'.
1529 * 'v_type' is VAR_CLASS or VAR_OBJECT.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001530 *
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001531 * The method index, method function pointer and method type are returned in
1532 * "lp".
1533 */
1534 static void
1535get_lval_oc_method(
1536 lval_T *lp,
1537 class_T *cl,
1538 char_u *key,
1539 char_u *key_end,
1540 vartype_T v_type)
1541{
1542 // Look for a method with this name.
1543 // round 1: class functions (skipped for an object)
1544 // round 2: object methods
1545 for (int round = v_type == VAR_OBJECT ? 2 : 1; round <= 2; ++round)
1546 {
1547 int m_idx;
1548 ufunc_T *fp;
1549
1550 fp = method_lookup(cl, round == 1 ? VAR_CLASS : VAR_OBJECT,
1551 key, key_end - key, &m_idx);
1552 lp->ll_oi = m_idx;
1553 if (fp != NULL)
1554 {
1555 lp->ll_ufunc = fp;
1556 lp->ll_valtype = fp->uf_func_type;
1557 break;
1558 }
1559 }
1560}
1561
1562/*
1563 * Get a class or object lval variable in class "cl". The "key" argument
1564 * points to the variable name and "key_end" points to the character after
1565 * "key". "v_type" is VAR_CLASS or VAR_OBJECT. "cl_exec" is the class that is
1566 * executing, or NULL.
1567 *
1568 * The variable index, typval and type are returned in "lp". Returns FAIL if
1569 * the variable is not writable. Otherwise returns OK.
1570 */
1571 static int
1572get_lval_oc_variable(
1573 lval_T *lp,
1574 class_T *cl,
1575 char_u *key,
1576 char_u *key_end,
1577 vartype_T v_type,
1578 class_T *cl_exec,
1579 int flags)
1580{
1581 int m_idx;
1582 ocmember_T *om;
1583
1584 om = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1585 lp->ll_oi = m_idx;
1586 if (om == NULL)
1587 return OK;
1588
1589 // Check variable is accessible
1590 if (get_lval_check_access(cl_exec, cl, om, key_end, flags) == FAIL)
1591 return FAIL;
1592
1593 // When lhs is used to modify the variable, check it is not a read-only
1594 // variable.
1595 if ((flags & GLV_READ_ONLY) == 0 && (*key_end != '.' && *key_end != '[')
1596 && oc_var_check_ro(cl, om))
1597 return FAIL;
1598
1599 lp->ll_valtype = om->ocm_type;
1600
1601 if (v_type == VAR_OBJECT)
1602 lp->ll_tv = ((typval_T *)(lp->ll_tv->vval.v_object + 1)) + m_idx;
1603 else
1604 lp->ll_tv = &cl->class_members_tv[m_idx];
1605
1606 return OK;
1607}
1608
1609/*
1610 * Get a Class or Object lval variable or method that can be assigned a value
1611 * to: "name", "name.key", "name.key[expr]" etc.
1612 *
1613 * The 'key' argument points to the member name and 'key_end' points to the
1614 * character after 'key'. 'v_type' is VAR_CLASS or VAR_OBJECT. 'cl_exec' is
1615 * the class that is executing, or NULL. If 'quiet' is TRUE, then error
1616 * messages are not displayed for invalid indexes.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001617 *
1618 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1619 * failure.
1620 */
1621 static int
1622get_lval_class_or_obj(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001623 lval_T *lp,
1624 char_u *key,
1625 char_u *key_end,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001626 vartype_T v_type,
1627 class_T *cl_exec,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001628 int flags,
1629 int quiet)
1630{
1631 lp->ll_dict = NULL;
1632 lp->ll_list = NULL;
1633
1634 class_T *cl;
1635 if (v_type == VAR_OBJECT)
1636 {
1637 if (lp->ll_tv->vval.v_object == NULL)
1638 {
1639 if (!quiet)
1640 emsg(_(e_using_null_object));
1641 return FAIL;
1642 }
1643 cl = lp->ll_tv->vval.v_object->obj_class;
1644 lp->ll_object = lp->ll_tv->vval.v_object;
1645 }
1646 else
1647 {
1648 cl = lp->ll_tv->vval.v_class;
1649 lp->ll_object = NULL;
1650 }
1651 lp->ll_class = cl;
1652
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001653 if (cl == NULL)
1654 // TODO: what if class is NULL?
1655 return OK;
1656
1657 lp->ll_valtype = NULL;
1658
1659 if (flags & GLV_PREFER_FUNC)
1660 get_lval_oc_method(lp, cl, key, key_end, v_type);
1661
1662 // Look for object/class member variable
1663 if (lp->ll_valtype == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001664 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001665 if (get_lval_oc_variable(lp, cl, key, key_end, v_type, cl_exec, flags)
1666 == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001667 return FAIL;
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001668 }
1669
1670 if (lp->ll_valtype == NULL)
1671 {
1672 member_not_found_msg(cl, v_type, key, key_end - key);
1673 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001674 }
1675
1676 return OK;
1677}
1678
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001679/*
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001680 * Check whether dot (".") is allowed after the variable "name" with type
1681 * "v_type". Only Dict, Class and Object types support a dot after the name.
1682 * Returns TRUE if dot is allowed after the name.
1683 */
1684 static int
1685dot_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1686{
1687 if (v_type != VAR_DICT && v_type != VAR_OBJECT && v_type != VAR_CLASS)
1688 {
1689 if (!quiet)
1690 semsg(_(e_dot_not_allowed_after_str_str),
1691 vartype_name(v_type), name);
1692 return FALSE;
1693 }
1694
1695 return TRUE;
1696}
1697
1698/*
1699 * Check whether left bracket ("[") is allowed after the variable "name" with
1700 * type "v_type". Only Dict, List and Blob types support a bracket after the
1701 * variable name. Returns TRUE if bracket is allowed after the name.
1702 */
1703 static int
1704bracket_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1705{
1706 if (v_type == VAR_CLASS || v_type == VAR_OBJECT)
1707 {
1708 if (!quiet)
1709 semsg(_(e_index_not_allowed_after_str_str),
1710 vartype_name(v_type), name);
1711 return FALSE;
1712 }
1713
1714 return TRUE;
1715}
1716
1717/*
1718 * Check whether the variable "name" with type "v_type" can be followed by an
1719 * index. Only Dict, List, Blob, Object and Class types support indexing.
1720 * Returns TRUE if indexing is allowed after the name.
1721 */
1722 static int
1723index_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1724{
1725 if (v_type != VAR_LIST && v_type != VAR_DICT && v_type != VAR_BLOB &&
1726 v_type != VAR_OBJECT && v_type != VAR_CLASS)
1727 {
1728 if (!quiet)
1729 semsg(_(e_index_not_allowed_after_str_str),
1730 vartype_name(v_type), name);
1731 return FALSE;
1732 }
1733
1734 return TRUE;
1735}
1736
1737/*
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001738 * Get the lval of a list/dict/blob/object/class subitem starting at "p". Loop
1739 * until no more [idx] or .key is following.
1740 *
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001741 * If "rettv" is not NULL it points to the value to be assigned.
1742 * "unlet" is TRUE for ":unlet".
1743 *
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001744 * Returns a pointer to the character after the subscript on success or NULL on
1745 * failure.
1746 */
1747 static char_u *
1748get_lval_subscript(
1749 lval_T *lp,
1750 char_u *p,
1751 char_u *name,
1752 typval_T *rettv,
1753 hashtab_T *ht,
1754 dictitem_T *v,
1755 int unlet,
1756 int flags, // GLV_ values
1757 class_T *cl_exec)
1758{
1759 int vim9script = in_vim9script();
1760 int quiet = flags & GLV_QUIET;
1761 char_u *key = NULL;
1762 int len;
1763 typval_T var1;
1764 typval_T var2;
1765 int empty1 = FALSE;
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001766 int rc = FAIL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001767
1768 /*
1769 * Loop until no more [idx] or .key is following.
1770 */
1771 var1.v_type = VAR_UNKNOWN;
1772 var2.v_type = VAR_UNKNOWN;
1773
1774 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1775 {
1776 vartype_T v_type = lp->ll_tv->v_type;
1777
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001778 if (*p == '.' && !dot_allowed_after_type(name, v_type, quiet))
1779 goto done;
1780
1781 if (*p == '[' && !bracket_allowed_after_type(name, v_type, quiet))
1782 goto done;
1783
1784 if (!index_allowed_after_type(name, v_type, quiet))
1785 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001786
1787 // A NULL list/blob works like an empty list/blob, allocate one now.
1788 int r = OK;
1789 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1790 r = rettv_list_alloc(lp->ll_tv);
1791 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1792 r = rettv_blob_alloc(lp->ll_tv);
1793 if (r == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001794 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001795
1796 if (lp->ll_range)
1797 {
1798 if (!quiet)
1799 emsg(_(e_slice_must_come_last));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001800 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001801 }
1802#ifdef LOG_LOCKVAR
1803 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1804 vartype_name(v_type));
1805#endif
1806
1807 if (vim9script && lp->ll_valtype == NULL
1808 && v != NULL
1809 && lp->ll_tv == &v->di_tv
1810 && ht != NULL && ht == get_script_local_ht())
1811 {
1812 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1813
1814 // Vim9 script local variable: get the type
1815 if (sv != NULL)
1816 {
1817 lp->ll_valtype = sv->sv_type;
1818#ifdef LOG_LOCKVAR
1819 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1820 vartype_name(lp->ll_valtype->tt_type));
1821#endif
1822 }
1823 }
1824
1825 len = -1;
1826 if (*p == '.')
1827 {
1828 key = p + 1;
1829
1830 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1831 ;
1832 if (len == 0)
1833 {
1834 if (!quiet)
1835 emsg(_(e_cannot_use_empty_key_for_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001836 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001837 }
1838 p = key + len;
1839 }
1840 else
1841 {
1842 // Get the index [expr] or the first index [expr: ].
1843 p = skipwhite(p + 1);
1844 if (*p == ':')
1845 empty1 = TRUE;
1846 else
1847 {
1848 empty1 = FALSE;
1849 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001850 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001851 if (tv_get_string_chk(&var1) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001852 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001853 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001854 p = skipwhite(p);
1855 }
1856
1857 // Optionally get the second index [ :expr].
1858 if (*p == ':')
1859 {
1860 if (v_type == VAR_DICT)
1861 {
1862 if (!quiet)
1863 emsg(_(e_cannot_slice_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001864 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001865 }
1866 if (rettv != NULL
1867 && !(rettv->v_type == VAR_LIST
1868 && rettv->vval.v_list != NULL)
1869 && !(rettv->v_type == VAR_BLOB
1870 && rettv->vval.v_blob != NULL))
1871 {
1872 if (!quiet)
1873 emsg(_(e_slice_requires_list_or_blob_value));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001874 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001875 }
1876 p = skipwhite(p + 1);
1877 if (*p == ']')
1878 lp->ll_empty2 = TRUE;
1879 else
1880 {
1881 lp->ll_empty2 = FALSE;
1882 // recursive!
1883 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001884 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001885 if (tv_get_string_chk(&var2) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001886 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001887 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001888 }
1889 lp->ll_range = TRUE;
1890 }
1891 else
1892 lp->ll_range = FALSE;
1893
1894 if (*p != ']')
1895 {
1896 if (!quiet)
1897 emsg(_(e_missing_closing_square_brace));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001898 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001899 }
1900
1901 // Skip to past ']'.
1902 ++p;
1903 }
1904#ifdef LOG_LOCKVAR
1905 if (len == -1)
1906 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1907 empty1 ? ":" : (char*)tv_get_string(&var1));
1908 else
1909 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
1910#endif
1911
1912 if (v_type == VAR_DICT)
1913 {
1914 glv_status_T glv_status;
1915
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001916 glv_status = get_lval_dict_item(lp, name, key, len, &p, &var1,
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001917 flags, unlet, rettv);
1918 if (glv_status == GLV_FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001919 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001920 if (glv_status == GLV_STOP)
1921 break;
1922 }
1923 else if (v_type == VAR_BLOB)
1924 {
1925 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001926 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001927
1928 break;
1929 }
1930 else if (v_type == VAR_LIST)
1931 {
1932 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001933 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001934 }
1935 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
1936 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001937 if (get_lval_class_or_obj(lp, key, p, v_type, cl_exec, flags,
1938 quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001939 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001940 }
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001941
1942 clear_tv(&var1);
1943 clear_tv(&var2);
1944 var1.v_type = VAR_UNKNOWN;
1945 var2.v_type = VAR_UNKNOWN;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001946 }
1947
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001948 rc = OK;
1949
1950done:
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001951 clear_tv(&var1);
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001952 clear_tv(&var2);
1953 return rc == OK ? p : NULL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001954}
1955
1956/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 * Get an lval: variable, Dict item or List item that can be assigned a value
1958 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1959 * "name.key", "name.key[expr]" etc.
1960 * Indexing only works if "name" is an existing List or Dictionary.
1961 * "name" points to the start of the name.
1962 * If "rettv" is not NULL it points to the value to be assigned.
1963 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1964 * wrong; must end in space or cmd separator.
1965 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001966 * flags:
1967 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001968 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001969 * GLV_NO_AUTOLOAD: do not use script autoloading
1970 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976get_lval(
1977 char_u *name,
1978 typval_T *rettv,
1979 lval_T *lp,
1980 int unlet,
1981 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 int flags, // GLV_ values
1983 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 char_u *expr_start, *expr_end;
1987 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001988 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001989 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001990 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001991 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001992 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001993 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994
Ernie Raelee865f32023-09-29 19:53:55 +02001995#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001996 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001997 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001998 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001999 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
2000 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02002001 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02002002 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02002003 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02002004#endif
2005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002006 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02002007 CLEAR_POINTER(lp);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002008 ga_init2(&lp->ll_type_list, sizeof(type_T *), 10);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002010 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002012 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02002014 lp->ll_name_end = find_name_end(name, NULL, NULL,
2015 FNE_INCL_BR | fne_flags);
2016 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 }
2018
Bram Moolenaara749a422022-02-12 19:52:25 +00002019 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00002020 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00002021 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
2022 {
2023 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
2024 return NULL;
2025 }
2026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002028 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (expr_start != NULL)
2031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01002033 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 && *p != '[' && *p != '.')
2035 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002036 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 return NULL;
2038 }
2039
2040 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2041 if (lp->ll_exp_name == NULL)
2042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Report an invalid expression in braces, unless the
2044 // expression evaluation has been cancelled due to an
2045 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 if (!aborting() && !quiet)
2047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002048 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002049 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 return NULL;
2051 }
2052 }
2053 lp->ll_name = lp->ll_exp_name;
2054 }
2055 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 lp->ll_name = name;
2058
Bram Moolenaar4525a572022-02-13 11:57:33 +00002059 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002061 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002062 // However, "g:[key]" is indexing a dictionary.
2063 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002064 {
2065 --p;
2066 lp->ll_name_end = p;
2067 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002068 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002069 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002070 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02002071
Bram Moolenaar9510d222022-09-11 15:14:05 +01002072 if (is_scoped_variable(name))
2073 {
2074 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2075 return NULL;
2076 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002077 if (VIM_ISWHITE(*p))
2078 {
2079 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2080 return NULL;
2081 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002082 if (tp == p + 1 && !quiet)
2083 {
2084 semsg(_(e_white_space_required_after_str_str), ":", p);
2085 return NULL;
2086 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002087 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002088 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002089 semsg(_(e_using_type_not_in_script_context_str), p);
2090 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002091 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002092 if (vim9script && (flags & GLV_NO_DECL) &&
2093 !(flags & GLV_FOR_LOOP))
2094 {
2095 // Using a type and not in a "var" declaration.
2096 semsg(_(e_trailing_characters_str), p);
2097 return NULL;
2098 }
2099
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002100
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002101 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002102 lp->ll_type = parse_type(&tp,
2103 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2104 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002105 if (lp->ll_type == NULL && !quiet)
2106 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002107 lp->ll_name_end = tp;
2108 }
Ernie Raelee865f32023-09-29 19:53:55 +02002109 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 }
2111 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002112 if (lp->ll_name == NULL)
2113 return p;
2114
Bram Moolenaar62aec932022-01-29 21:45:34 +00002115 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002116 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002117 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002118 if (import != NULL)
2119 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002120 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002121 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002122 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002123 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002124 }
2125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126
Ernie Rael0f058d12023-10-14 11:25:04 +02002127 // Without [idx] or .key we are done.
2128 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002129 {
2130 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002131 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002133 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134
Ernie Rael0f058d12023-10-14 11:25:04 +02002135 if (vim9script && lval_root != NULL)
2136 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002137 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002138 {
2139 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002140 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002141 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002142 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002143 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002144 {
2145 cc = *p;
2146 *p = NUL;
2147 // When we would write to the variable pass &ht and prevent autoload.
2148 writing = !(flags & GLV_READ_ONLY);
2149 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002150 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002151 if (v == NULL && !quiet)
2152 semsg(_(e_undefined_variable_str), lp->ll_name);
2153 *p = cc;
2154 if (v == NULL)
2155 return NULL;
2156 lp->ll_tv = &v->di_tv;
2157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158
Bram Moolenaar4525a572022-02-13 11:57:33 +00002159 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002160 {
2161 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002162 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002163 return NULL;
2164 }
2165
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002166 // If the next character is a "." or a "[", then process the subitem.
2167 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2168 if (p == NULL)
2169 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002171 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2172 {
2173 where_T where = WHERE_INIT;
2174
2175 // In a vim9 script, do type check and make sure the variable is
2176 // writable.
2177 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2178 return NULL;
2179 }
2180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 return p;
2183}
2184
2185/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190{
2191 vim_free(lp->ll_exp_name);
2192 vim_free(lp->ll_newkey);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002193 clear_type_list(&lp->ll_type_list);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194}
2195
2196/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002197 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002199 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2200 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203set_var_lval(
2204 lval_T *lp,
2205 char_u *endp,
2206 typval_T *rettv,
2207 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002208 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2209 char_u *op,
2210 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211{
2212 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002213 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214
2215 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002217 cc = *endp;
2218 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002219 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002220 return;
2221
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002222 if (lp->ll_blob != NULL)
2223 {
2224 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002225
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002226 if (op != NULL && *op != '=')
2227 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002228 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002229 return;
2230 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002231 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002232 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002233
2234 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2235 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002236 if (lp->ll_empty2)
2237 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2238
Bram Moolenaar68452172021-04-12 21:21:02 +02002239 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2240 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002241 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002242 }
2243 else
2244 {
2245 val = (int)tv_get_number_chk(rettv, &error);
2246 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002247 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002248 }
2249 }
2250 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002252 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002253
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002254 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2255 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002256 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002257 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002258 *endp = cc;
2259 return;
2260 }
2261
Bram Moolenaarff697e62019-02-12 22:28:33 +01002262 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002263 di = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01002264 if (eval_variable(lp->ll_name, (int)(lp->ll_name_end - lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002265 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002266 {
Ernie Raele75fde62023-12-21 17:18:54 +01002267 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002268 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002269 clear_tv(&tv);
2270 return;
2271 }
2272
Bram Moolenaar79518e22017-02-17 16:31:35 +01002273 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002274 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2275 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002276 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002277 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002278 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002279 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002282 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002283 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002284 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2285 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002286 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002287 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002288 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002289 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002290 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002292 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002293 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002294 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002295 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 else if (lp->ll_range)
2297 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002298 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2299 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002300 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002301 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002302 return;
2303 }
2304
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002305 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2306 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 }
2308 else
2309 {
2310 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002311 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002313 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2314 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002315 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002316 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002317 return;
2318 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002319
2320 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002321 && check_typval_arg_type(lp->ll_valtype, rettv,
2322 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002323 return;
2324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (lp->ll_newkey != NULL)
2326 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 if (op != NULL && *op != '=')
2328 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002329 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 return;
2331 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002332 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2333 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002334 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002337 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (di == NULL)
2339 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002340 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2341 {
2342 vim_free(di);
2343 return;
2344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 else if (op != NULL && *op != '=')
2348 {
2349 tv_op(lp->ll_tv, rettv, op);
2350 return;
2351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 /*
2356 * Assign the value to the variable or list item.
2357 */
2358 if (copy)
2359 copy_tv(rettv, lp->ll_tv);
2360 else
2361 {
2362 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002363 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367}
2368
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002370 * Handle "blob1 += blob2".
2371 * Returns OK or FAIL.
2372 */
2373 static int
2374tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2375{
2376 if (*op != '+' || tv2->v_type != VAR_BLOB)
2377 return FAIL;
2378
2379 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002380 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002381 return OK;
2382
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002383 if (tv1->vval.v_blob == NULL)
2384 {
2385 tv1->vval.v_blob = tv2->vval.v_blob;
2386 ++tv1->vval.v_blob->bv_refcount;
2387 return OK;
2388 }
2389
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002390 blob_T *b1 = tv1->vval.v_blob;
2391 blob_T *b2 = tv2->vval.v_blob;
2392 int len = blob_len(b2);
2393
2394 for (int i = 0; i < len; i++)
2395 ga_append(&b1->bv_ga, blob_get(b2, i));
2396
2397 return OK;
2398}
2399
2400/*
2401 * Handle "list1 += list2".
2402 * Returns OK or FAIL.
2403 */
2404 static int
2405tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2406{
2407 if (*op != '+' || tv2->v_type != VAR_LIST)
2408 return FAIL;
2409
2410 // List += List
2411 if (tv2->vval.v_list == NULL)
2412 return OK;
2413
2414 if (tv1->vval.v_list == NULL)
2415 {
2416 tv1->vval.v_list = tv2->vval.v_list;
2417 ++tv1->vval.v_list->lv_refcount;
2418 }
2419 else
2420 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2421
2422 return OK;
2423}
2424
2425/*
2426 * Handle number operations:
2427 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2428 *
2429 * Returns OK or FAIL.
2430 */
2431 static int
2432tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2433{
2434 varnumber_T n;
2435 int failed = FALSE;
2436
2437 n = tv_get_number(tv1);
2438 if (tv2->v_type == VAR_FLOAT)
2439 {
2440 float_T f = n;
2441
2442 if (*op == '%')
2443 return FAIL;
2444 switch (*op)
2445 {
2446 case '+': f += tv2->vval.v_float; break;
2447 case '-': f -= tv2->vval.v_float; break;
2448 case '*': f *= tv2->vval.v_float; break;
2449 case '/': f /= tv2->vval.v_float; break;
2450 }
2451 clear_tv(tv1);
2452 tv1->v_type = VAR_FLOAT;
2453 tv1->vval.v_float = f;
2454 }
2455 else
2456 {
2457 switch (*op)
2458 {
2459 case '+': n += tv_get_number(tv2); break;
2460 case '-': n -= tv_get_number(tv2); break;
2461 case '*': n *= tv_get_number(tv2); break;
2462 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2463 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2464 }
2465 clear_tv(tv1);
2466 tv1->v_type = VAR_NUMBER;
2467 tv1->vval.v_number = n;
2468 }
2469
2470 return failed ? FAIL : OK;
2471}
2472
2473/*
2474 * Handle "str1 .= str2"
2475 * Returns OK or FAIL.
2476 */
2477 static int
2478tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2479{
2480 char_u numbuf[NUMBUFLEN];
2481 char_u *s;
2482
2483 if (tv2->v_type == VAR_FLOAT)
2484 return FAIL;
2485
2486 // str .= str
2487 s = tv_get_string(tv1);
2488 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2489 clear_tv(tv1);
2490 tv1->v_type = VAR_STRING;
2491 tv1->vval.v_string = s;
2492
2493 return OK;
2494}
2495
2496/*
2497 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2498 * and "tv1 .= tv2"
2499 * Returns OK or FAIL.
2500 */
2501 static int
2502tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2503{
2504 if (tv2->v_type == VAR_LIST)
2505 return FAIL;
2506
2507 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2508 return tv_op_number(tv1, tv2, op);
2509
2510 return tv_op_string(tv1, tv2, op);
2511}
2512
2513/*
2514 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2515 * Returns OK or FAIL.
2516 */
2517 static int
2518tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2519{
2520 float_T f;
2521
2522 if (*op == '%' || *op == '.'
2523 || (tv2->v_type != VAR_FLOAT
2524 && tv2->v_type != VAR_NUMBER
2525 && tv2->v_type != VAR_STRING))
2526 return FAIL;
2527
2528 if (tv2->v_type == VAR_FLOAT)
2529 f = tv2->vval.v_float;
2530 else
2531 f = tv_get_number(tv2);
2532 switch (*op)
2533 {
2534 case '+': tv1->vval.v_float += f; break;
2535 case '-': tv1->vval.v_float -= f; break;
2536 case '*': tv1->vval.v_float *= f; break;
2537 case '/': tv1->vval.v_float /= f; break;
2538 }
2539
2540 return OK;
2541}
2542
2543/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002544 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2545 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 * Returns OK or FAIL.
2547 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002548 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002551 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002552 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002553 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2554 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2555 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002557 semsg(_(e_wrong_variable_type_for_str_equal), op);
2558 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 }
2560
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002561 int retval = FAIL;
2562 switch (tv1->v_type)
2563 {
2564 case VAR_UNKNOWN:
2565 case VAR_ANY:
2566 case VAR_VOID:
2567 case VAR_DICT:
2568 case VAR_FUNC:
2569 case VAR_PARTIAL:
2570 case VAR_BOOL:
2571 case VAR_SPECIAL:
2572 case VAR_JOB:
2573 case VAR_CHANNEL:
2574 case VAR_INSTR:
2575 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002576 case VAR_CLASS:
2577 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002578 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002579
2580 case VAR_BLOB:
2581 retval = tv_op_blob(tv1, tv2, op);
2582 break;
2583
2584 case VAR_LIST:
2585 retval = tv_op_list(tv1, tv2, op);
2586 break;
2587
2588 case VAR_NUMBER:
2589 case VAR_STRING:
2590 retval = tv_op_nr_or_string(tv1, tv2, op);
2591 break;
2592
2593 case VAR_FLOAT:
2594 retval = tv_op_float(tv1, tv2, op);
2595 break;
2596 }
2597
2598 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002599 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002600
2601 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002602}
2603
2604/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002605 * Evaluate the expression used in a ":for var in expr" command.
2606 * "arg" points to "var".
2607 * Set "*errp" to TRUE for an error, FALSE otherwise;
2608 * Return a pointer that holds the info. Null when there is an error.
2609 */
2610 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611eval_for_line(
2612 char_u *arg,
2613 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002614 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002615 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002616{
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002618 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002619 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 typval_T tv;
2621 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002622 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002624 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002625
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002626 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002627 if (fi == NULL)
2628 return NULL;
2629
Bram Moolenaar404557e2021-07-05 21:41:48 +02002630 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2631 &fi->fi_semicolon, FALSE);
2632 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002633 return fi;
2634
Bram Moolenaar404557e2021-07-05 21:41:48 +02002635 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002636 if (expr[0] != 'i' || expr[1] != 'n'
2637 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002638 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002639 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2640 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2641 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002642 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002643 return fi;
2644 }
2645
2646 if (skip)
2647 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002648 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2649 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002650 {
2651 *errp = FALSE;
2652 if (!skip)
2653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002654 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002655 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002656 l = tv.vval.v_list;
2657 if (l == NULL)
2658 {
2659 // a null list is like an empty list: do nothing
2660 clear_tv(&tv);
2661 }
2662 else
2663 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002665 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002667 // No need to increment the refcount, it's already set for
2668 // the list being used in "tv".
2669 fi->fi_list = l;
2670 list_add_watch(l, &fi->fi_lw);
2671 fi->fi_lw.lw_item = l->lv_first;
2672 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002673 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002674 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002675 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002676 fi->fi_bi = 0;
2677 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002678 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002679 typval_T btv;
2680
2681 // Make a copy, so that the iteration still works when the
2682 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002684 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002685 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002686 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002687 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002688 else if (tv.v_type == VAR_STRING)
2689 {
2690 fi->fi_byte_idx = 0;
2691 fi->fi_string = tv.vval.v_string;
2692 tv.vval.v_string = NULL;
2693 if (fi->fi_string == NULL)
2694 fi->fi_string = vim_strsave((char_u *)"");
2695 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002696 else
2697 {
Sean Dewar80d73952021-08-04 19:25:54 +02002698 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002699 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002700 }
2701 }
2702 }
2703 if (skip)
2704 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002705 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706
2707 return fi;
2708}
2709
2710/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002711 * Used when looping over a :for line, skip the "in expr" part.
2712 */
2713 void
2714skip_for_lines(void *fi_void, evalarg_T *evalarg)
2715{
2716 forinfo_T *fi = (forinfo_T *)fi_void;
2717 int i;
2718
2719 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002720 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002721}
2722
2723/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724 * Use the first item in a ":for" list. Advance to the next.
2725 * Assign the values to the variable (list). "arg" points to the first one.
2726 * Return TRUE when a valid item was found, FALSE when at end of list or
2727 * something wrong.
2728 */
2729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002730next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002732 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002734 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002735 ? (ASSIGN_FINAL
2736 // first round: error if variable exists
2737 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002738 | ASSIGN_NO_MEMBER_TYPE
2739 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002740 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002741 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002742 int skip_assign = in_vim9script() && arg[0] == '_'
2743 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002744
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002745 if (fi->fi_blob != NULL)
2746 {
2747 typval_T tv;
2748
2749 if (fi->fi_bi >= blob_len(fi->fi_blob))
2750 return FALSE;
2751 tv.v_type = VAR_NUMBER;
2752 tv.v_lock = VAR_FIXED;
2753 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2754 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002755 if (skip_assign)
2756 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002757 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002758 fi->fi_varcount, flag, NULL) == OK;
2759 }
2760
2761 if (fi->fi_string != NULL)
2762 {
2763 typval_T tv;
2764 int len;
2765
2766 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2767 if (len == 0)
2768 return FALSE;
2769 tv.v_type = VAR_STRING;
2770 tv.v_lock = VAR_FIXED;
2771 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2772 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002773 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002774 if (skip_assign)
2775 result = TRUE;
2776 else
2777 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002778 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002779 vim_free(tv.vval.v_string);
2780 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002781 }
2782
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002783 item = fi->fi_lw.lw_item;
2784 if (item == NULL)
2785 result = FALSE;
2786 else
2787 {
2788 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002789 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002790 if (skip_assign)
2791 result = TRUE;
2792 else
2793 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002794 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795 }
2796 return result;
2797}
2798
2799/*
2800 * Free the structure used to store info used by ":for".
2801 */
2802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002803free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804{
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002807 if (fi == NULL)
2808 return;
2809 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002810 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002812 list_unref(fi->fi_list);
2813 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002814 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002815 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002816 else
2817 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818 vim_free(fi);
2819}
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002822set_context_for_expression(
2823 expand_T *xp,
2824 char_u *arg,
2825 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002827 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002831 if (cmdidx == CMD_let || cmdidx == CMD_var
2832 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 {
2834 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002835 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002838 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 {
2840 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002841 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002842 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002843 break;
2844 }
2845 return;
2846 }
2847 }
2848 else
2849 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2850 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 while ((xp->xp_pattern = vim_strpbrk(arg,
2852 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2853 {
2854 c = *xp->xp_pattern;
2855 if (c == '&')
2856 {
2857 c = xp->xp_pattern[1];
2858 if (c == '&')
2859 {
2860 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002861 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002866 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2867 xp->xp_pattern += 2;
2868
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871 else if (c == '$')
2872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 xp->xp_context = EXPAND_ENV_VARS;
2875 }
2876 else if (c == '=')
2877 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002878 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 xp->xp_context = EXPAND_EXPRESSION;
2880 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002881 else if (c == '#'
2882 && xp->xp_context == EXPAND_EXPRESSION)
2883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002885 break;
2886 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002887 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 && xp->xp_context == EXPAND_FUNCTIONS
2889 && vim_strchr(xp->xp_pattern, '(') == NULL)
2890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 break;
2893 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002894 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002896 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
2898 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2899 if (c == '\\' && xp->xp_pattern[1] != NUL)
2900 ++xp->xp_pattern;
2901 xp->xp_context = EXPAND_NOTHING;
2902 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002903 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002905 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2907 /* skip */ ;
2908 xp->xp_context = EXPAND_NOTHING;
2909 }
2910 else if (c == '|')
2911 {
2912 if (xp->xp_pattern[1] == '|')
2913 {
2914 ++xp->xp_pattern;
2915 xp->xp_context = EXPAND_EXPRESSION;
2916 }
2917 else
2918 xp->xp_context = EXPAND_COMMANDS;
2919 }
2920 else
2921 xp->xp_context = EXPAND_EXPRESSION;
2922 }
2923 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002924 // Doesn't look like something valid, expand as an expression
2925 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 arg = xp->xp_pattern;
2928 if (*arg != NUL)
2929 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2930 /* skip */ ;
2931 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002932
2933 // ":exe one two" completes "two"
2934 if ((cmdidx == CMD_execute
2935 || cmdidx == CMD_echo
2936 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002937 || cmdidx == CMD_echomsg
2938 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002939 && xp->xp_context == EXPAND_EXPRESSION)
2940 {
2941 for (;;)
2942 {
2943 char_u *n = skiptowhite(arg);
2944
2945 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2946 break;
2947 arg = skipwhite(n);
2948 }
2949 }
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 xp->xp_pattern = arg;
2952}
2953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002955 * Return TRUE if "pat" matches "text".
2956 * Does not use 'cpo' and always uses 'magic'.
2957 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002958 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002959pattern_match(char_u *pat, char_u *text, int ic)
2960{
2961 int matches = FALSE;
2962 char_u *save_cpo;
2963 regmatch_T regmatch;
2964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002965 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002966 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002967 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002968 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2969 if (regmatch.regprog != NULL)
2970 {
2971 regmatch.rm_ic = ic;
2972 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2973 vim_regfree(regmatch.regprog);
2974 }
2975 p_cpo = save_cpo;
2976 return matches;
2977}
2978
2979/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002980 * Handle a name followed by "(". Both for just "name(arg)" and for
2981 * "expr->name(arg)".
2982 * Returns OK or FAIL.
2983 */
2984 static int
2985eval_func(
2986 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002987 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002988 char_u *name,
2989 int name_len,
2990 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002991 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002992 typval_T *basetv) // "expr" for "expr->name(arg)"
2993{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002994 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002995 char_u *s = name;
2996 int len = name_len;
2997 partial_T *partial;
2998 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002999 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003000 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003001
3002 if (!evaluate)
3003 check_vars(s, len);
3004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003005 // If "s" is the name of a variable of type VAR_FUNC
3006 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003007 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003008 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 // Need to make a copy, in case evaluating the arguments makes
3011 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003012 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01003013 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003014 ret = FAIL;
3015 else
3016 {
3017 funcexe_T funcexe;
3018
3019 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02003020 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003021 funcexe.fe_firstline = curwin->w_cursor.lnum;
3022 funcexe.fe_lastline = curwin->w_cursor.lnum;
3023 funcexe.fe_evaluate = evaluate;
3024 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003025 if (partial != NULL)
3026 {
3027 funcexe.fe_object = partial->pt_obj;
3028 if (funcexe.fe_object != NULL)
3029 ++funcexe.fe_object->obj_refcount;
3030 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003031 funcexe.fe_basetv = basetv;
3032 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003033 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003034 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003035 }
3036 vim_free(s);
3037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003038 // If evaluate is FALSE rettv->v_type was not set in
3039 // get_func_tv, but it's needed in handle_subscript() to parse
3040 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003041 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3042 {
3043 rettv->vval.v_string = NULL;
3044 rettv->v_type = VAR_FUNC;
3045 }
3046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003047 // Stop the expression evaluation when immediately
3048 // aborting on error, or when an interrupt occurred or
3049 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003050 if (evaluate && aborting())
3051 {
3052 if (ret == OK)
3053 clear_tv(rettv);
3054 ret = FAIL;
3055 }
3056 return ret;
3057}
3058
3059/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003060 * After a NL, skip over empty lines and comment-only lines.
3061 */
3062 static char_u *
3063newline_skip_comments(char_u *arg)
3064{
3065 char_u *p = arg + 1;
3066
3067 for (;;)
3068 {
3069 p = skipwhite(p);
3070
3071 if (*p == NUL)
3072 break;
3073 if (vim9_comment_start(p))
3074 {
3075 char_u *nl = vim_strchr(p, NL);
3076
3077 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003078 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003079 p = nl;
3080 }
3081 if (*p != NL)
3082 break;
3083 ++p; // skip another NL
3084 }
3085 return p;
3086}
3087
3088/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003089 * Get the next line source line without advancing. But do skip over comment
3090 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003091 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003092 */
3093 static char_u *
3094getline_peek_skip_comments(evalarg_T *evalarg)
3095{
3096 for (;;)
3097 {
3098 char_u *next = getline_peek(evalarg->eval_getline,
3099 evalarg->eval_cookie);
3100 char_u *p;
3101
3102 if (next == NULL)
3103 break;
3104 p = skipwhite(next);
3105 if (*p != NUL && !vim9_comment_start(p))
3106 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003107 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003108 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003109 }
3110 return NULL;
3111}
3112
3113/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003114 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3115 * comment) and there is a next line, return the next line (skipping blanks)
3116 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003117 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3118 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003119 * "arg" must point somewhere inside a line, not at the start.
3120 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003121 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003122eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3123{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003124 char_u *p = skipwhite(arg);
3125
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003127 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003128 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003129 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3130 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003131 && (*p == NUL || *p == NL
3132 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003133 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003134 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003135
Bram Moolenaare442d592022-05-05 12:20:28 +01003136 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003137 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003138 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003139 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003140 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003141 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142
Bram Moolenaar9d489562020-07-30 20:08:50 +02003143 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003145 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003146 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003147 }
3148 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003149 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003150}
3151
3152/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003153 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003154 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003155 *
3156 * If "arg" is not NULL, then the caller should assign the return value to
3157 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003158 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003159 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003160eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003161{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003162 garray_T *gap = &evalarg->eval_ga;
3163 char_u *line;
3164
Bram Moolenaar39be4982022-05-06 21:51:50 +01003165 if (arg != NULL)
3166 {
3167 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003168 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003169 // Truncate before a trailing comment, so that concatenating the lines
3170 // won't turn the rest into a comment.
3171 if (*skipwhite(arg) == '#')
3172 *arg = NUL;
3173 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003174
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003175 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003176 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3177 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003178 else
3179 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003180 if (line == NULL)
3181 return NULL;
3182
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003183 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003184 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3185 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003186 char_u *p = skipwhite(line);
3187
3188 // Going to concatenate the lines after parsing. For an empty or
3189 // comment line use an empty string.
3190 if (*p == NUL || vim9_comment_start(p))
3191 {
3192 vim_free(line);
3193 line = vim_strsave((char_u *)"");
3194 }
3195
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003196 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3197 ++gap->ga_len;
3198 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003199 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003200 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003201 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003202 evalarg->eval_tofree = line;
3203 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003204
3205 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003206 // line. The caller assigns the return value to "arg".
3207 // If "arg" is NULL, then the return value is discarded. In that case,
3208 // "arg" still points to the previous line. So don't reset
3209 // "eval_using_cmdline".
3210 if (arg != NULL)
3211 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003212 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003213}
3214
Bram Moolenaare6b53242020-07-01 17:28:33 +02003215/*
3216 * Call eval_next_non_blank() and get the next line if needed.
3217 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003218 char_u *
3219skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3220{
3221 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003222 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003223
Bram Moolenaar962d7212020-07-04 14:15:00 +02003224 if (evalarg == NULL)
3225 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003226 eval_next_non_blank(p, evalarg, &getnext);
3227 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003228 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003229 return p;
3230}
3231
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003232/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003233 * The "eval" functions have an "evalarg" argument: When NULL or
3234 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3235 * parsed but not executed. The functions may return OK, but the rettv will be
3236 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 */
3238
3239/*
3240 * Handle zero level expression.
3241 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003243 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003244 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * Return OK or FAIL.
3246 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248eval0(
3249 char_u *arg,
3250 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003251 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003252 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003254 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3255}
3256
3257/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003258 * If "arg" is a simple function call without arguments then call it and return
3259 * the result. Otherwise return NOTDONE.
3260 */
3261 int
3262may_call_simple_func(
3263 char_u *arg,
3264 typval_T *rettv)
3265{
3266 char_u *parens = (char_u *)strstr((char *)arg, "()");
3267 int r = NOTDONE;
3268
3269 // If the expression is "FuncName()" then we can skip a lot of overhead.
3270 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3271 {
3272 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3273
3274 if (to_name_end(p, TRUE) == parens)
zeertzjqc1ed7882024-08-01 22:46:54 +02003275 r = call_simple_func(arg, (size_t)(parens - arg), rettv);
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003276 }
3277 return r;
3278}
3279
3280/*
3281 * Handle zero level expression with optimization for a simple function call.
3282 * Same arguments and return value as eval0().
3283 */
3284 int
3285eval0_simple_funccal(
3286 char_u *arg,
3287 typval_T *rettv,
3288 exarg_T *eap,
3289 evalarg_T *evalarg)
3290{
3291 int r = may_call_simple_func(arg, rettv);
3292
3293 if (r == NOTDONE)
3294 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3295 return r;
3296}
3297
3298/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003299 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3300 * expression and don't check what comes after the expression.
3301 */
3302 int
3303eval0_retarg(
3304 char_u *arg,
3305 typval_T *rettv,
3306 exarg_T *eap,
3307 evalarg_T *evalarg,
3308 char_u **retarg)
3309{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 int ret;
3311 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003312 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003313 int did_emsg_before = did_emsg;
3314 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003315 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003316 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003319 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003320
Bram Moolenaar79481362022-06-27 20:15:10 +01003321 if (ret != FAIL)
3322 {
3323 expr_end = p;
3324 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003325
Bram Moolenaar79481362022-06-27 20:15:10 +01003326 // In Vim9 script a command block is not split at NL characters for
3327 // commands using an expression argument. Skip over a '#' comment to
3328 // check for a following NL. Require white space before the '#'.
3329 if (in_vim9script() && p > expr_end && retarg == NULL)
3330 while (*p == '#')
3331 {
3332 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003333
Bram Moolenaar79481362022-06-27 20:15:10 +01003334 if (nl == NULL)
3335 break;
3336 p = skipwhite(nl + 1);
3337 if (eap != NULL && *p != NUL)
3338 eap->nextcmd = p;
3339 check_for_end = FALSE;
3340 }
3341
3342 if (check_for_end)
3343 end_error = !ends_excmd2(arg, p);
3344 }
3345
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003346 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /*
3351 * Report the invalid expression unless the expression evaluation has
3352 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003353 * exception, or we already gave a more specific error.
3354 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003356 if (!aborting()
3357 && did_emsg == did_emsg_before
3358 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003359 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003360 {
3361 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003362 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003363 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003364 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003365 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003366
zeertzjqf2588b62023-05-05 17:22:35 +01003367 if (eap != NULL && p != NULL)
3368 {
3369 // Some of the expression may not have been consumed.
3370 // Only execute a next command if it cannot be a "||" operator.
3371 // The next command may be "catch".
3372 char_u *nextcmd = check_nextcmd(p);
3373 if (nextcmd != NULL && *nextcmd != '|')
3374 eap->nextcmd = nextcmd;
3375 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003376 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003378
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003379 if (retarg != NULL)
3380 *retarg = p;
3381 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003382 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return ret;
3385}
3386
3387/*
3388 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003389 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003390 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 *
3392 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003393 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003395 * Note: "rettv.v_lock" is not set.
3396 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 * Return OK or FAIL.
3398 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003399 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003400eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003402 char_u *p;
3403 int getnext;
3404
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003405 CLEAR_POINTER(rettv);
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /*
3408 * Get the first variable.
3409 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003410 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 return FAIL;
3412
Bram Moolenaar793648f2020-06-26 21:28:25 +02003413 p = eval_next_non_blank(*arg, evalarg, &getnext);
3414 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003416 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003417 int result;
3418 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003419 evalarg_T *evalarg_used = evalarg;
3420 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003421 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003422 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003423 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003424
3425 if (evalarg == NULL)
3426 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003427 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003428 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003430 orig_flags = evalarg_used->eval_flags;
3431 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003432
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003433 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003434 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003435 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003436 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003437 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003438 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003439 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003440 clear_tv(rettv);
3441 return FAIL;
3442 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003443 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003444 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003449 int error = FALSE;
3450
Bram Moolenaar13106602020-10-04 16:06:05 +02003451 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003452 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003453 else if (vim9script)
3454 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003455 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003457 if (error || !op_falsy || !result)
3458 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459 if (error)
3460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462
3463 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003464 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003466 if (op_falsy)
3467 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003468 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003469 {
mityu4ac198c2021-05-28 17:52:40 +02003470 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003471 clear_tv(rettv);
3472 return FAIL;
3473 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003474 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003475 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003476 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003477 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003478 {
3479 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003481 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003482 if (!op_falsy || !result)
3483 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003485 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003487 /*
3488 * Check for the ":".
3489 */
3490 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3491 if (*p != ':')
3492 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003493 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003494 if (evaluate && result)
3495 clear_tv(rettv);
3496 evalarg_used->eval_flags = orig_flags;
3497 return FAIL;
3498 }
3499 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003500 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003501 else
3502 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003503 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003504 {
3505 error_white_both(p, 1);
3506 clear_tv(rettv);
3507 evalarg_used->eval_flags = orig_flags;
3508 return FAIL;
3509 }
3510 *arg = p;
3511 }
3512
3513 /*
3514 * Get the third variable. Recursive!
3515 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003516 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003517 {
mityu4ac198c2021-05-28 17:52:40 +02003518 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003519 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003520 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003521 return FAIL;
3522 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003523 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3524 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003525 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003526 if (eval1(arg, &var2, evalarg_used) == FAIL)
3527 {
3528 if (evaluate && result)
3529 clear_tv(rettv);
3530 evalarg_used->eval_flags = orig_flags;
3531 return FAIL;
3532 }
3533 if (evaluate && !result)
3534 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003536
3537 if (evalarg == NULL)
3538 clear_evalarg(&local_evalarg, NULL);
3539 else
3540 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542
3543 return OK;
3544}
3545
3546/*
3547 * Handle first level expression:
3548 * expr2 || expr2 || expr2 logical OR
3549 *
3550 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003551 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 *
3553 * Return OK or FAIL.
3554 */
3555 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003556eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003558 char_u *p;
3559 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003562 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003564 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 return FAIL;
3566
3567 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003568 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003570 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003571 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003573 evalarg_T *evalarg_used = evalarg;
3574 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003575 int evaluate;
3576 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003577 long result = FALSE;
3578 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003579 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003580 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003581
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003582 if (evalarg == NULL)
3583 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003584 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003585 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003586 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003587 orig_flags = evalarg_used->eval_flags;
3588 evaluate = orig_flags & EVAL_EVALUATE;
3589 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003590 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003591 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003592 result = tv_get_bool_chk(rettv, &error);
3593 else if (tv_get_number_chk(rettv, &error) != 0)
3594 result = TRUE;
3595 clear_tv(rettv);
3596 if (error)
3597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003601 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003603 while (p[0] == '|' && p[1] == '|')
3604 {
3605 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003606 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003607 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003608 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003609 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003610 {
3611 error_white_both(p, 2);
3612 clear_tv(rettv);
3613 return FAIL;
3614 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003615 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003616 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003617
3618 /*
3619 * Get the second variable.
3620 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003621 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003622 {
mityu4ac198c2021-05-28 17:52:40 +02003623 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003624 clear_tv(rettv);
3625 return FAIL;
3626 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003627 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3628 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003629 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003630 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003631 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003632
3633 /*
3634 * Compute the result.
3635 */
3636 if (evaluate && !result)
3637 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003638 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003639 result = tv_get_bool_chk(&var2, &error);
3640 else if (tv_get_number_chk(&var2, &error) != 0)
3641 result = TRUE;
3642 clear_tv(&var2);
3643 if (error)
3644 return FAIL;
3645 }
3646 if (evaluate)
3647 {
3648 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003649 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003650 rettv->v_type = VAR_BOOL;
3651 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003652 }
3653 else
3654 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003655 rettv->v_type = VAR_NUMBER;
3656 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003657 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003658 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003659
3660 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003662
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003663 if (evalarg == NULL)
3664 clear_evalarg(&local_evalarg, NULL);
3665 else
3666 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 return OK;
3670}
3671
3672/*
3673 * Handle second level expression:
3674 * expr3 && expr3 && expr3 logical AND
3675 *
3676 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003677 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 *
3679 * Return OK or FAIL.
3680 */
3681 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003682eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003684 char_u *p;
3685 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003688 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003690 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
3692
3693 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003694 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003696 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003697 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003699 evalarg_T *evalarg_used = evalarg;
3700 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003701 int orig_flags;
3702 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003703 long result = TRUE;
3704 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003705 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003706 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003707
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003708 if (evalarg == NULL)
3709 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003710 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003711 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003712 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003713 orig_flags = evalarg_used->eval_flags;
3714 evaluate = orig_flags & EVAL_EVALUATE;
3715 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003716 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003717 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003718 result = tv_get_bool_chk(rettv, &error);
3719 else if (tv_get_number_chk(rettv, &error) == 0)
3720 result = FALSE;
3721 clear_tv(rettv);
3722 if (error)
3723 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725
3726 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003727 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003729 while (p[0] == '&' && p[1] == '&')
3730 {
3731 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003732 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003733 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003734 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003735 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003736 {
3737 error_white_both(p, 2);
3738 clear_tv(rettv);
3739 return FAIL;
3740 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003741 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003742 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003743
3744 /*
3745 * Get the second variable.
3746 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003747 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003748 {
mityu4ac198c2021-05-28 17:52:40 +02003749 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003750 clear_tv(rettv);
3751 return FAIL;
3752 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003753 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3754 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003755 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003756 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003757 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003759
3760 /*
3761 * Compute the result.
3762 */
3763 if (evaluate && result)
3764 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003765 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003766 result = tv_get_bool_chk(&var2, &error);
3767 else if (tv_get_number_chk(&var2, &error) == 0)
3768 result = FALSE;
3769 clear_tv(&var2);
3770 if (error)
3771 return FAIL;
3772 }
3773 if (evaluate)
3774 {
3775 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003776 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003777 rettv->v_type = VAR_BOOL;
3778 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003779 }
3780 else
3781 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003782 rettv->v_type = VAR_NUMBER;
3783 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003784 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003785 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003786
3787 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003789
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003790 if (evalarg == NULL)
3791 clear_evalarg(&local_evalarg, NULL);
3792 else
3793 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 return OK;
3797}
3798
3799/*
3800 * Handle third level expression:
3801 * var1 == var2
3802 * var1 =~ var2
3803 * var1 != var2
3804 * var1 !~ var2
3805 * var1 > var2
3806 * var1 >= var2
3807 * var1 < var2
3808 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003809 * var1 is var2
3810 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 *
3812 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003813 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 *
3815 * Return OK or FAIL.
3816 */
3817 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003818eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003821 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003822 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003824 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003827 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003829 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return FAIL;
3831
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003832 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003833
Bram Moolenaar696ba232020-07-29 21:20:41 +02003834 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003837 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003839 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003841 typval_T var2;
3842 int ic;
3843 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003844 int evaluate = evalarg == NULL
3845 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003846 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003847
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003848 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003849 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003850 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003851 p = *arg;
3852 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003853 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3854 {
mityu4ac198c2021-05-28 17:52:40 +02003855 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003856 clear_tv(rettv);
3857 return FAIL;
3858 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003859
Bram Moolenaar696ba232020-07-29 21:20:41 +02003860 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3861 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003862 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003863 clear_tv(rettv);
3864 return FAIL;
3865 }
3866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (p[len] == '?')
3869 {
3870 ic = TRUE;
3871 ++len;
3872 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else if (p[len] == '#')
3875 {
3876 ic = FALSE;
3877 ++len;
3878 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003879 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003881 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883 /*
3884 * Get the second variable.
3885 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003886 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3887 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003888 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003889 clear_tv(rettv);
3890 return FAIL;
3891 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003892 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003893 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return FAIL;
3897 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003898 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003899 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003900 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003901
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003902 // use the line of the comparison for messages
3903 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003904 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003905 {
3906 ret = FAIL;
3907 clear_tv(rettv);
3908 }
3909 else
3910 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003911 clear_tv(&var2);
3912 return ret;
3913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915
3916 return OK;
3917}
3918
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003919/*
3920 * Make a copy of blob "tv1" and append blob "tv2".
3921 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 void
3923eval_addblob(typval_T *tv1, typval_T *tv2)
3924{
3925 blob_T *b1 = tv1->vval.v_blob;
3926 blob_T *b2 = tv2->vval.v_blob;
3927 blob_T *b = blob_alloc();
3928 int i;
3929
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003930 if (b == NULL)
3931 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003933 for (i = 0; i < blob_len(b1); i++)
3934 ga_append(&b->bv_ga, blob_get(b1, i));
3935 for (i = 0; i < blob_len(b2); i++)
3936 ga_append(&b->bv_ga, blob_get(b2, i));
3937
3938 clear_tv(tv1);
3939 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940}
3941
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003942/*
3943 * Make a copy of list "tv1" and append list "tv2".
3944 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 int
3946eval_addlist(typval_T *tv1, typval_T *tv2)
3947{
3948 typval_T var3;
3949
3950 // concatenate Lists
3951 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3952 {
3953 clear_tv(tv1);
3954 clear_tv(tv2);
3955 return FAIL;
3956 }
3957 clear_tv(tv1);
3958 *tv1 = var3;
3959 return OK;
3960}
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02003963 * Left or right shift the number "tv1" by the number "tv2" and store the
3964 * result in "tv1".
3965 *
3966 * Return OK or FAIL.
3967 */
3968 static int
3969eval_shift_number(typval_T *tv1, typval_T *tv2, int shift_type)
3970{
3971 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3972 {
3973 // right operand should be a positive number
3974 if (tv2->v_type != VAR_NUMBER)
3975 emsg(_(e_bitshift_ops_must_be_number));
3976 else
3977 emsg(_(e_bitshift_ops_must_be_positive));
3978 clear_tv(tv1);
3979 clear_tv(tv2);
3980 return FAIL;
3981 }
3982
3983 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3984 // shifting more bits than we have always results in zero
3985 tv1->vval.v_number = 0;
3986 else if (shift_type == EXPR_LSHIFT)
3987 tv1->vval.v_number =
3988 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
3989 else
3990 tv1->vval.v_number =
3991 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
3992
3993 return OK;
3994}
3995
3996/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01003997 * Handle fourth level expression (bitwise left/right shift operators):
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003998 * var1 << var2
3999 * var1 >> var2
4000 *
4001 * "arg" must point to the first non-white of the expression.
4002 * "arg" is advanced to just after the recognized expression.
4003 *
4004 * Return OK or FAIL.
4005 */
4006 static int
4007eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
4008{
4009 /*
4010 * Get the first expression.
4011 */
4012 if (eval6(arg, rettv, evalarg) == FAIL)
4013 return FAIL;
4014
4015 /*
4016 * Repeat computing, until no '<<' or '>>' is following.
4017 */
4018 for (;;)
4019 {
4020 char_u *p;
4021 int getnext;
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004022 exprtype_T exprtype;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004023 int evaluate;
4024 typval_T var2;
4025 int vim9script;
4026
4027 p = eval_next_non_blank(*arg, evalarg, &getnext);
4028 if (p[0] == '<' && p[1] == '<')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004029 exprtype = EXPR_LSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004030 else if (p[0] == '>' && p[1] == '>')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004031 exprtype = EXPR_RSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004032 else
4033 return OK;
4034
4035 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004036 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
4037 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004038 {
4039 // left operand should be a number
4040 emsg(_(e_bitshift_ops_must_be_number));
4041 clear_tv(rettv);
4042 return FAIL;
4043 }
4044
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004045 vim9script = in_vim9script();
4046 if (getnext)
4047 {
4048 *arg = eval_next_line(*arg, evalarg);
4049 p = *arg;
4050 }
4051 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4052 {
4053 error_white_both(*arg, 2);
4054 clear_tv(rettv);
4055 return FAIL;
4056 }
4057
4058 /*
4059 * Get the second variable.
4060 */
4061 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
4062 {
4063 error_white_both(p, 2);
4064 clear_tv(rettv);
4065 return FAIL;
4066 }
4067 *arg = skipwhite_and_linebreak(p + 2, evalarg);
4068 if (eval6(arg, &var2, evalarg) == FAIL)
4069 {
4070 clear_tv(rettv);
4071 return FAIL;
4072 }
4073
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004074 if (evaluate)
4075 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004076 if (eval_shift_number(rettv, &var2, exprtype) == FAIL)
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004077 return FAIL;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004078 }
4079
4080 clear_tv(&var2);
4081 }
4082
4083 return OK;
4084}
4085
4086/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004087 * Concatenate strings "tv1" and "tv2" and store the result in "tv1".
4088 */
4089 static int
4090eval_concat_str(typval_T *tv1, typval_T *tv2)
4091{
4092 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4093 char_u *s1 = tv_get_string_buf(tv1, buf1);
4094 char_u *s2 = NULL;
4095 char_u *p;
4096 int vim9script = in_vim9script();
4097
4098 if (vim9script && (tv2->v_type == VAR_VOID
4099 || tv2->v_type == VAR_CHANNEL
4100 || tv2->v_type == VAR_JOB))
4101 semsg(_(e_using_invalid_value_as_string_str),
4102 vartype_name(tv2->v_type));
4103 else if (vim9script && tv2->v_type == VAR_FLOAT)
4104 {
4105 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4106 tv2->vval.v_float);
4107 s2 = buf2;
4108 }
4109 else
4110 s2 = tv_get_string_buf_chk(tv2, buf2);
4111 if (s2 == NULL) // type error ?
4112 {
4113 clear_tv(tv1);
4114 clear_tv(tv2);
4115 return FAIL;
4116 }
4117
4118 p = concat_str(s1, s2);
4119 clear_tv(tv1);
4120 tv1->v_type = VAR_STRING;
4121 tv1->vval.v_string = p;
4122
4123 return OK;
4124}
4125
4126/*
4127 * Add or subtract numbers "tv1" and "tv2" and store the result in "tv1".
4128 * The numbers can be whole numbers or floats.
4129 */
4130 static int
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004131eval_addsub_number(typval_T *tv1, typval_T *tv2, int op)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004132{
4133 int error = FALSE;
4134 varnumber_T n1, n2;
4135 float_T f1 = 0, f2 = 0;
4136
4137 if (tv1->v_type == VAR_FLOAT)
4138 {
4139 f1 = tv1->vval.v_float;
4140 n1 = 0;
4141 }
4142 else
4143 {
4144 n1 = tv_get_number_chk(tv1, &error);
4145 if (error)
4146 {
4147 // This can only happen for "list + non-list" or
4148 // "blob + non-blob". For "non-list + ..." or
4149 // "something - ...", we returned before evaluating the
4150 // 2nd operand.
4151 clear_tv(tv1);
4152 clear_tv(tv2);
4153 return FAIL;
4154 }
4155 if (tv2->v_type == VAR_FLOAT)
4156 f1 = n1;
4157 }
4158 if (tv2->v_type == VAR_FLOAT)
4159 {
4160 f2 = tv2->vval.v_float;
4161 n2 = 0;
4162 }
4163 else
4164 {
4165 n2 = tv_get_number_chk(tv2, &error);
4166 if (error)
4167 {
4168 clear_tv(tv1);
4169 clear_tv(tv2);
4170 return FAIL;
4171 }
4172 if (tv1->v_type == VAR_FLOAT)
4173 f2 = n2;
4174 }
4175 clear_tv(tv1);
4176
4177 // If there is a float on either side the result is a float.
4178 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4179 {
4180 if (op == '+')
4181 f1 = f1 + f2;
4182 else
4183 f1 = f1 - f2;
4184 tv1->v_type = VAR_FLOAT;
4185 tv1->vval.v_float = f1;
4186 }
4187 else
4188 {
4189 if (op == '+')
4190 n1 = n1 + n2;
4191 else
4192 n1 = n1 - n2;
4193 tv1->v_type = VAR_NUMBER;
4194 tv1->vval.v_number = n1;
4195 }
4196
4197 return OK;
4198}
4199
4200/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004201 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004202 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004204 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004205 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 *
4207 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004208 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004213eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004216 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004218 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220
4221 /*
4222 * Repeat computing, until no '+', '-' or '.' is following.
4223 */
4224 for (;;)
4225 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004226 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004227 int getnext;
4228 char_u *p;
4229 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004230 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004231 int concat;
4232 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004233 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004234
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004235 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004236 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004237 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004238 p = eval_next_non_blank(*arg, evalarg, &getnext);
4239 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004240 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004241 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4242 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004244 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4245 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004246
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004247 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004248 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004249 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004250 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004251 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004252 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004253 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004254 {
mityu4ac198c2021-05-28 17:52:40 +02004255 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004256 clear_tv(rettv);
4257 return FAIL;
4258 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004259 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004260 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004261 if ((op != '+' || (rettv->v_type != VAR_LIST
4262 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004263 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004264 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004266 int error = FALSE;
4267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004268 // For "list + ...", an illegal use of the first operand as
4269 // a number cannot be determined before evaluating the 2nd
4270 // operand: if this is also a list, all is ok.
4271 // For "something . ...", "something - ..." or "non-list + ...",
4272 // we know that the first operand needs to be a string or number
4273 // without evaluating the 2nd operand. So check before to avoid
4274 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004275 if (op != '.')
4276 tv_get_number_chk(rettv, &error);
4277 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 {
4279 clear_tv(rettv);
4280 return FAIL;
4281 }
4282 }
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 /*
4285 * Get the second variable.
4286 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004287 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004288 {
mityu89dcb4d2021-05-28 13:50:17 +02004289 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004290 clear_tv(rettv);
4291 return FAIL;
4292 }
4293 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004294 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return FAIL;
4298 }
4299
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004300 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 /*
4303 * Compute the result.
4304 */
4305 if (op == '.')
4306 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004307 if (eval_concat_str(rettv, &var2) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004310 else if (op == '+' && rettv->v_type == VAR_BLOB
4311 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004313 else if (op == '+' && rettv->v_type == VAR_LIST
4314 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004315 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004317 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 else
4320 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004321 if (eval_addsub_number(rettv, &var2, op) == FAIL)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 }
4327 return OK;
4328}
4329
4330/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004331 * Multiply or divide or compute the modulo of numbers "tv1" and "tv2" and
4332 * store the result in "tv1". The numbers can be whole numbers or floats.
4333 */
4334 static int
4335eval_multdiv_number(typval_T *tv1, typval_T *tv2, int op)
4336{
4337 varnumber_T n1, n2;
4338 float_T f1, f2;
4339 int error;
4340 int use_float = FALSE;
4341
4342 f1 = 0;
4343 f2 = 0;
4344 error = FALSE;
4345 if (tv1->v_type == VAR_FLOAT)
4346 {
4347 f1 = tv1->vval.v_float;
4348 use_float = TRUE;
4349 n1 = 0;
4350 }
4351 else
4352 n1 = tv_get_number_chk(tv1, &error);
4353 clear_tv(tv1);
4354 if (error)
4355 {
4356 clear_tv(tv2);
4357 return FAIL;
4358 }
4359
4360 if (tv2->v_type == VAR_FLOAT)
4361 {
4362 if (!use_float)
4363 {
4364 f1 = n1;
4365 use_float = TRUE;
4366 }
4367 f2 = tv2->vval.v_float;
4368 n2 = 0;
4369 }
4370 else
4371 {
4372 n2 = tv_get_number_chk(tv2, &error);
4373 clear_tv(tv2);
4374 if (error)
4375 return FAIL;
4376 if (use_float)
4377 f2 = n2;
4378 }
4379
4380 /*
4381 * Compute the result.
4382 * When either side is a float the result is a float.
4383 */
4384 if (use_float)
4385 {
4386 if (op == '*')
4387 f1 = f1 * f2;
4388 else if (op == '/')
4389 {
4390#ifdef VMS
4391 // VMS crashes on divide by zero, work around it
4392 if (f2 == 0.0)
4393 {
4394 if (f1 == 0)
4395 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
4396 else if (f1 < 0)
4397 f1 = -1 * __F_FLT_MAX;
4398 else
4399 f1 = __F_FLT_MAX;
4400 }
4401 else
4402 f1 = f1 / f2;
4403#else
4404 // We rely on the floating point library to handle divide
4405 // by zero to result in "inf" and not a crash.
4406 f1 = f1 / f2;
4407#endif
4408 }
4409 else
4410 {
4411 emsg(_(e_cannot_use_percent_with_float));
4412 return FAIL;
4413 }
4414 tv1->v_type = VAR_FLOAT;
4415 tv1->vval.v_float = f1;
4416 }
4417 else
4418 {
4419 int failed = FALSE;
4420
4421 if (op == '*')
4422 n1 = n1 * n2;
4423 else if (op == '/')
4424 n1 = num_divide(n1, n2, &failed);
4425 else
4426 n1 = num_modulus(n1, n2, &failed);
4427 if (failed)
4428 return FAIL;
4429
4430 tv1->v_type = VAR_NUMBER;
4431 tv1->vval.v_number = n1;
4432 }
4433
4434 return OK;
4435}
4436
4437/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004438 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * * number multiplication
4440 * / number division
4441 * % number modulo
4442 *
4443 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004444 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 *
4446 * Return OK or FAIL.
4447 */
4448 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004449eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004450 char_u **arg,
4451 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004452 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004453 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004456 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004458 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
4460
4461 /*
4462 * Repeat computing, until no '*', '/' or '%' is following.
4463 */
4464 for (;;)
4465 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004466 int evaluate;
4467 int getnext;
4468 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004469 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004470 int op;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004471
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004472 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004473 p = eval_next_non_blank(*arg, evalarg, &getnext);
4474 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004475 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004477
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004478 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004479 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004480 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004481 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004482 {
4483 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4484 {
mityu4ac198c2021-05-28 17:52:40 +02004485 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004486 clear_tv(rettv);
4487 return FAIL;
4488 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004489 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 /*
4493 * Get the second variable.
4494 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004495 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4496 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004497 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004498 clear_tv(rettv);
4499 return FAIL;
4500 }
4501 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004502 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 return FAIL;
4504
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004505 if (evaluate)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004506 // Compute the result.
4507 if (eval_multdiv_number(rettv, &var2, op) == FAIL)
4508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510
4511 return OK;
4512}
4513
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004514/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004515 * Handle seventh level expression:
4516 * a type cast before a base level expression.
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004517 * "arg" must point to the first non-white of the expression.
4518 * "arg" is advanced to just after the recognized expression.
4519 * Return OK or FAIL.
4520 */
4521 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004522eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004523 char_u **arg,
4524 typval_T *rettv,
4525 evalarg_T *evalarg,
4526 int want_string) // after "." operator
4527{
4528 type_T *want_type = NULL;
4529 garray_T type_list; // list of pointers to allocated types
4530 int res;
4531 int evaluate = evalarg == NULL ? 0
4532 : (evalarg->eval_flags & EVAL_EVALUATE);
4533
4534 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004535 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4536 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004537 {
4538 ++*arg;
4539 ga_init2(&type_list, sizeof(type_T *), 10);
4540 want_type = parse_type(arg, &type_list, TRUE);
4541 if (want_type == NULL && (evaluate || **arg != '>'))
4542 {
4543 clear_type_list(&type_list);
4544 return FAIL;
4545 }
4546
4547 if (**arg != '>')
4548 {
4549 if (*skipwhite(*arg) == '>')
4550 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4551 else
4552 emsg(_(e_missing_gt));
4553 clear_type_list(&type_list);
4554 return FAIL;
4555 }
4556 ++*arg;
4557 *arg = skipwhite_and_linebreak(*arg, evalarg);
4558 }
4559
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004560 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004561
4562 if (want_type != NULL && evaluate)
4563 {
4564 if (res == OK)
4565 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004566 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4567 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004568
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004569 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004570 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004571 if (want_type->tt_type == VAR_BOOL
4572 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004573 && (actual->tt_flags & TTFLAG_BOOL_OK))
4574 {
4575 int n = tv2bool(rettv);
4576
4577 // can use "0" and "1" for boolean in some places
4578 clear_tv(rettv);
4579 rettv->v_type = VAR_BOOL;
4580 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4581 }
4582 else
4583 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004584 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004585
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004586 res = check_type(want_type, actual, TRUE, where);
4587 }
4588 }
4589 }
4590 clear_type_list(&type_list);
4591 }
4592
4593 return res;
4594}
4595
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004596 int
4597eval_leader(char_u **arg, int vim9)
4598{
4599 char_u *s = *arg;
4600 char_u *p = *arg;
4601
4602 while (*p == '!' || *p == '-' || *p == '+')
4603 {
4604 char_u *n = skipwhite(p + 1);
4605
4606 // ++, --, -+ and +- are not accepted in Vim9 script
4607 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4608 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004609 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004610 return FAIL;
4611 }
4612 p = n;
4613 }
4614 *arg = p;
4615 return OK;
4616}
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004619 * Check for a predefined value "true", "false" and "null.*".
4620 * Return OK when recognized.
4621 */
4622 int
4623handle_predefined(char_u *s, int len, typval_T *rettv)
4624{
4625 switch (len)
4626 {
4627 case 4: if (STRNCMP(s, "true", 4) == 0)
4628 {
4629 rettv->v_type = VAR_BOOL;
4630 rettv->vval.v_number = VVAL_TRUE;
4631 return OK;
4632 }
4633 if (STRNCMP(s, "null", 4) == 0)
4634 {
4635 rettv->v_type = VAR_SPECIAL;
4636 rettv->vval.v_number = VVAL_NULL;
4637 return OK;
4638 }
4639 break;
4640 case 5: if (STRNCMP(s, "false", 5) == 0)
4641 {
4642 rettv->v_type = VAR_BOOL;
4643 rettv->vval.v_number = VVAL_FALSE;
4644 return OK;
4645 }
4646 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004647 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4648 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004650 rettv->v_type = VAR_JOB;
4651 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004652#else
4653 rettv->v_type = VAR_SPECIAL;
4654 rettv->vval.v_number = VVAL_NULL;
4655#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004656 return OK;
4657 }
4658 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004659 case 9:
4660 if (STRNCMP(s, "null_", 5) != 0)
4661 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004662 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004663 if (STRNCMP(s + 5, "list", 4) == 0)
4664 {
4665 rettv->v_type = VAR_LIST;
4666 rettv->vval.v_list = NULL;
4667 return OK;
4668 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004669 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004670 if (STRNCMP(s + 5, "dict", 4) == 0)
4671 {
4672 rettv->v_type = VAR_DICT;
4673 rettv->vval.v_dict = NULL;
4674 return OK;
4675 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004676 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004677 if (STRNCMP(s + 5, "blob", 4) == 0)
4678 {
4679 rettv->v_type = VAR_BLOB;
4680 rettv->vval.v_blob = NULL;
4681 return OK;
4682 }
4683 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004684 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4685 {
4686 rettv->v_type = VAR_CLASS;
4687 rettv->vval.v_class = NULL;
4688 return OK;
4689 }
4690 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004691 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4692 {
4693 rettv->v_type = VAR_STRING;
4694 rettv->vval.v_string = NULL;
4695 return OK;
4696 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004697 if (STRNCMP(s, "null_object", 11) == 0)
4698 {
4699 rettv->v_type = VAR_OBJECT;
4700 rettv->vval.v_object = NULL;
4701 return OK;
4702 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004703 break;
4704 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004705 if (STRNCMP(s, "null_channel", 12) == 0)
4706 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004708 rettv->v_type = VAR_CHANNEL;
4709 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004710#else
4711 rettv->v_type = VAR_SPECIAL;
4712 rettv->vval.v_number = VVAL_NULL;
4713#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004714 return OK;
4715 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004716 if (STRNCMP(s, "null_partial", 12) == 0)
4717 {
4718 rettv->v_type = VAR_PARTIAL;
4719 rettv->vval.v_partial = NULL;
4720 return OK;
4721 }
4722 break;
4723 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4724 {
4725 rettv->v_type = VAR_FUNC;
4726 rettv->vval.v_string = NULL;
4727 return OK;
4728 }
4729 break;
4730 }
4731 return FAIL;
4732}
4733
4734/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004735 * Handle register contents: @r.
4736 */
4737 static void
4738eval9_reg_contents(
4739 char_u **arg,
4740 typval_T *rettv,
4741 int evaluate)
4742{
4743 int vim9script = in_vim9script();
4744
4745 ++*arg; // skip '@'
4746 if (evaluate)
4747 {
4748 if (vim9script && IS_WHITE_OR_NUL(**arg))
4749 semsg(_(e_syntax_error_at_str), *arg);
4750 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4751 emsg_invreg(**arg);
4752 else
4753 {
4754 rettv->v_type = VAR_STRING;
4755 rettv->vval.v_string = get_reg_contents(**arg,
4756 GREG_EXPR_SRC);
4757 }
4758 }
4759 if (**arg != NUL)
4760 ++*arg;
4761}
4762
4763/*
4764 * Handle a nested expression: (expression) or lambda: (arg) => expr
4765 */
4766 static int
4767eval9_nested_expr(
4768 char_u **arg,
4769 typval_T *rettv,
4770 evalarg_T *evalarg,
4771 int evaluate)
4772{
4773 int ret = NOTDONE;
4774 int vim9script = in_vim9script();
4775
4776 if (vim9script)
4777 {
4778 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4779 if (ret == OK && evaluate)
4780 {
4781 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4782
4783 // Compile it here to get the return type. The return
4784 // type is optional, when it's missing use t_unknown.
4785 // This is recognized in compile_return().
4786 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4787 ufunc->uf_ret_type = &t_unknown;
4788 if (compile_def_function(ufunc, FALSE,
4789 get_compile_type(ufunc), NULL) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 ret = FAIL;
4793 }
4794 }
4795 }
4796 if (ret == NOTDONE)
4797 {
4798 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
4799 ret = eval1(arg, rettv, evalarg); // recursive!
4800
4801 *arg = skipwhite_and_linebreak(*arg, evalarg);
4802 if (**arg == ')')
4803 ++*arg;
4804 else if (ret == OK)
4805 {
4806 emsg(_(e_missing_closing_paren));
4807 clear_tv(rettv);
4808 ret = FAIL;
4809 }
4810 }
4811
4812 return ret;
4813}
4814
4815/*
4816* Handle be a variable or function name.
4817* Can also be a curly-braces kind of name: {expr}.
4818*/
4819 static int
4820eval9_var_func_name(
4821 char_u **arg,
4822 typval_T *rettv,
4823 evalarg_T *evalarg,
4824 int evaluate,
4825 char_u **name_start)
4826{
4827 char_u *s;
4828 int len;
4829 char_u *alias;
4830 int ret = OK;
4831 int vim9script = in_vim9script();
4832
4833 s = *arg;
4834 len = get_name_len(arg, &alias, evaluate, TRUE);
4835 if (alias != NULL)
4836 s = alias;
4837
4838 if (len <= 0)
4839 ret = FAIL;
4840 else
4841 {
4842 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4843
4844 if (evaluate && vim9script && len == 1 && *s == '_')
4845 {
4846 emsg(_(e_cannot_use_underscore_here));
4847 ret = FAIL;
4848 }
4849 else if (evaluate && vim9script && len > 2
4850 && s[0] == 's' && s[1] == ':')
4851 {
4852 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4853 ret = FAIL;
4854 }
4855 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
4856 {
4857 // "name(..." recursive!
4858 *arg = skipwhite(*arg);
4859 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
4860 }
4861 else if (evaluate)
4862 {
4863 // get the value of "true", "false", etc. or a variable
4864 ret = FAIL;
4865 if (vim9script)
4866 ret = handle_predefined(s, len, rettv);
4867 if (ret == FAIL)
4868 {
4869 *name_start = s;
4870 ret = eval_variable(s, len, 0, rettv, NULL,
4871 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
4872 }
4873 }
4874 else
4875 {
4876 // skip the name
4877 check_vars(s, len);
4878 ret = OK;
4879 }
4880 }
4881 vim_free(alias);
4882
4883 return ret;
4884}
4885
4886/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004887 * Handle eighth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004889 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004890 * "string" string constant
4891 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 * &option-name option value
4893 * @r register contents
4894 * identifier variable value
4895 * function() function call
4896 * $VAR environment variable
4897 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004898 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004900 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004901 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 *
4903 * Also handle:
4904 * ! in front logical NOT
4905 * - in front unary minus
4906 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004907 * trailing [] subscript in String or List
4908 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004909 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 *
4911 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004912 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 *
4914 * Return OK or FAIL.
4915 */
4916 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004917eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004918 char_u **arg,
4919 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004920 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004923 int evaluate = evalarg != NULL
4924 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004925 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 char_u *start_leader, *end_leader;
4927 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004928 static int recurse = 0;
4929 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004932 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004938 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
4940 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004941 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 end_leader = *arg;
4944
Keith Thompson184f71c2024-01-04 21:19:04 +01004945 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004946 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004947 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004948 ++*arg;
4949 return FAIL;
4950 }
4951
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004952 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004953 // and crash. With MSVC the stack is smaller.
4954 if (recurse ==
4955#ifdef _MSC_VER
4956 300
4957#else
4958 1000
4959#endif
4960 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004961 {
4962 semsg(_(e_expression_too_recursive_str), *arg);
4963 return FAIL;
4964 }
4965 ++recurse;
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 switch (**arg)
4968 {
4969 /*
4970 * Number constant.
4971 */
4972 case '0':
4973 case '1':
4974 case '2':
4975 case '3':
4976 case '4':
4977 case '5':
4978 case '6':
4979 case '7':
4980 case '8':
4981 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004982 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004983
4984 // Apply prefixed "-" and "+" now. Matters especially when
4985 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004986 if (ret == OK && evaluate && end_leader > start_leader
4987 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004988 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 break;
4990
4991 /*
4992 * String constant: "string".
4993 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004994 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 break;
4996
4997 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01005000 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 break;
5002
5003 /*
5004 * List: [expr, expr]
5005 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005006 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008
5009 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005010 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005011 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005012 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005013 break;
5014
5015 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005016 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005017 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005019 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005020 ret = NOTDONE;
5021 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005022 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005023 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005024 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 break;
5026
5027 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005028 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005030 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032
5033 /*
5034 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005035 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 */
LemonBoy2eaef102022-05-06 13:14:50 +01005037 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5038 ret = eval_interp_string(arg, rettv, evaluate);
5039 else
5040 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042
5043 /*
5044 * Register contents: @r.
5045 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005046 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048
5049 /*
5050 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005051 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005053 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059
5060 if (ret == NOTDONE)
5061 {
5062 /*
5063 * Must be a variable or function name.
5064 * Can also be a curly-braces kind of name: {expr}.
5065 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005066 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 }
5068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5070 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005071 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005072 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5076 */
5077 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005078 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005079
5080 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005081 return ret;
5082}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005083
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005084/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005085 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005086 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005087 * Adjusts "end_leaderp" until it is at "start_leader".
5088 */
5089 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005090eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005091 typval_T *rettv,
5092 int numeric_only,
5093 char_u *start_leader,
5094 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005095{
5096 char_u *end_leader = *end_leaderp;
5097 int ret = OK;
5098 int error = FALSE;
5099 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005100 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005101 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005102 float_T f = 0.0;
5103
5104 if (rettv->v_type == VAR_FLOAT)
5105 f = rettv->vval.v_float;
5106 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005107 {
5108 while (VIM_ISWHITE(end_leader[-1]))
5109 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005110 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005111 val = tv2bool(rettv);
5112 else
5113 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005114 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005115 if (error)
5116 {
5117 clear_tv(rettv);
5118 ret = FAIL;
5119 }
5120 else
5121 {
5122 while (end_leader > start_leader)
5123 {
5124 --end_leader;
5125 if (*end_leader == '!')
5126 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005127 if (numeric_only)
5128 {
5129 ++end_leader;
5130 break;
5131 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005132 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005133 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005134 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005135 {
5136 rettv->v_type = VAR_BOOL;
5137 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5138 }
5139 else
5140 f = !f;
5141 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005142 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005143 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005144 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005145 type = VAR_BOOL;
5146 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005147 }
5148 else if (*end_leader == '-')
5149 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005150 if (rettv->v_type == VAR_FLOAT)
5151 f = -f;
5152 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005153 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005154 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005155 type = VAR_NUMBER;
5156 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005157 }
5158 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005159 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005162 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 else
5165 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005166 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005167 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005168 rettv->v_type = type;
5169 else
5170 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005171 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005174 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return ret;
5176}
5177
5178/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005179 * Call the function referred to in "rettv".
5180 */
5181 static int
5182call_func_rettv(
5183 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005184 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005185 typval_T *rettv,
5186 int evaluate,
5187 dict_T *selfdict,
5188 typval_T *basetv)
5189{
5190 partial_T *pt = NULL;
5191 funcexe_T funcexe;
5192 typval_T functv;
5193 char_u *s;
5194 int ret;
5195
5196 // need to copy the funcref so that we can clear rettv
5197 if (evaluate)
5198 {
5199 functv = *rettv;
5200 rettv->v_type = VAR_UNKNOWN;
5201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005203 if (functv.v_type == VAR_PARTIAL)
5204 {
5205 pt = functv.vval.v_partial;
5206 s = partial_name(pt);
5207 }
5208 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005209 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005210 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005211 if (s == NULL || *s == NUL)
5212 {
5213 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005214 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005215 goto theend;
5216 }
5217 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005218 }
5219 else
5220 s = (char_u *)"";
5221
Bram Moolenaara80faa82020-04-12 19:37:17 +02005222 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005223 funcexe.fe_firstline = curwin->w_cursor.lnum;
5224 funcexe.fe_lastline = curwin->w_cursor.lnum;
5225 funcexe.fe_evaluate = evaluate;
5226 funcexe.fe_partial = pt;
5227 funcexe.fe_selfdict = selfdict;
5228 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005229 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005230
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005231theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Clear the funcref afterwards, so that deleting it while
5233 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005234 if (evaluate)
5235 clear_tv(&functv);
5236
5237 return ret;
5238}
5239
5240/*
5241 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005242 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005243 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5244 */
5245 static int
5246eval_lambda(
5247 char_u **arg,
5248 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005249 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005251{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005252 int evaluate = evalarg != NULL
5253 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005254 typval_T base = *rettv;
5255 int ret;
5256
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005257 rettv->v_type = VAR_UNKNOWN;
5258
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005259 if (**arg == '{')
5260 {
5261 // ->{lambda}()
5262 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5263 }
5264 else
5265 {
5266 // ->(lambda)()
5267 ++*arg;
5268 ret = eval1(arg, rettv, evalarg);
5269 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005270 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005271 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005272 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005273 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005274 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005275 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5276 && rettv->v_type != VAR_PARTIAL)
5277 {
5278 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5279 return FAIL;
5280 }
5281 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005282 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005283 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005284 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005285
5286 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005287 {
5288 if (verbose)
5289 {
5290 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005291 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005292 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005293 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005294 }
5295 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005296 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005297 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005298 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005299 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005300
5301 // Clear the funcref afterwards, so that deleting it while
5302 // evaluating the arguments is possible (see test55).
5303 if (evaluate)
5304 clear_tv(&base);
5305
5306 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005307}
5308
5309/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005310 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005311 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005312 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5313 */
5314 static int
5315eval_method(
5316 char_u **arg,
5317 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005318 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005320{
5321 char_u *name;
5322 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005323 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005324 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005325 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005326 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005327 int evaluate = evalarg != NULL
5328 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005329
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005330 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005331
Bram Moolenaarac92e252019-08-03 21:58:38 +02005332 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005333 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005334 if (alias != NULL)
5335 name = alias;
5336
5337 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005338 {
5339 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005340 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005341 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005342 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005343 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005344 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005345 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005346
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005347 // If there is no "(" immediately following, but there is further on,
5348 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5349 // Does not handle anything where "(" is part of the expression.
5350 *arg = skipwhite(*arg);
5351
5352 if (**arg != '(' && alias == NULL
5353 && (paren = vim_strchr(*arg, '(')) != NULL)
5354 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005355 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005356
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005357 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005358 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005359 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005360 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5361 if (evalarg != NULL)
5362 {
5363 getline = evalarg->eval_getline;
5364 evalarg->eval_getline = NULL;
5365 }
5366
5367 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005368 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005369 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005370 *arg = name + len;
5371 ret = FAIL;
5372 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005373 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005374 {
5375 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005376 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005377 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005378
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005379 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005380 if (getline != NULL)
5381 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005382 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005383
5384 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005385 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005386 *arg = skipwhite(*arg);
5387
5388 if (**arg != '(')
5389 {
5390 if (verbose)
5391 semsg(_(e_missing_parenthesis_str), name);
5392 ret = FAIL;
5393 }
5394 else if (VIM_ISWHITE((*arg)[-1]))
5395 {
5396 if (verbose)
5397 emsg(_(e_no_white_space_allowed_before_parenthesis));
5398 ret = FAIL;
5399 }
5400 else
5401 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005402 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005403 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005404 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005405
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005406 // Clear the funcref afterwards, so that deleting it while
5407 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005408 if (evaluate)
5409 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005410 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005411
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005412 if (alias != NULL)
5413 vim_free(alias);
5414
Bram Moolenaarac92e252019-08-03 21:58:38 +02005415 return ret;
5416}
5417
5418/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005419 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5420 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5422 */
5423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424eval_index(
5425 char_u **arg,
5426 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005427 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005430 int evaluate = evalarg != NULL
5431 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005436 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005437 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005439 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5440 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005442 init_tv(&var1);
5443 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 /*
5447 * dict.name
5448 */
5449 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005450 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005452 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005454 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 }
5456 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 /*
5459 * something[idx]
5460 *
5461 * Get the (first) variable from inside the [].
5462 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005463 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 if (**arg == ':')
5465 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005466 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005468 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005469 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005470 semsg(_(e_white_space_required_before_and_after_str_at_str),
5471 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005475 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005476 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005477 int error = FALSE;
5478
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005479 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005480 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005481 && var1.v_type == VAR_FLOAT)
5482 {
5483 var1.vval.v_string = typval_tostring(&var1, TRUE);
5484 var1.v_type = VAR_STRING;
5485 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005486
Bram Moolenaar4525a572022-02-13 11:57:33 +00005487 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005488 tv_get_number_chk(&var1, &error);
5489 else
5490 error = tv_get_string_chk(&var1) == NULL;
5491 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005492 {
5493 // not a number or string
5494 clear_tv(&var1);
5495 return FAIL;
5496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
5499 /*
5500 * Get the second variable from inside the [:].
5501 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005502 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503 if (**arg == ':')
5504 {
5505 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005506 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005507 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005508 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005509 semsg(_(e_white_space_required_before_and_after_str_at_str),
5510 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005511 if (!empty1)
5512 clear_tv(&var1);
5513 return FAIL;
5514 }
5515 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 if (**arg == ']')
5517 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005518 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005520 if (!empty1)
5521 clear_tv(&var1);
5522 return FAIL;
5523 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005524 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005526 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005527 if (!empty1)
5528 clear_tv(&var1);
5529 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 return FAIL;
5531 }
5532 }
5533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005535 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 if (**arg != ']')
5537 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005538 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005539 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 clear_tv(&var1);
5541 if (range)
5542 clear_tv(&var2);
5543 return FAIL;
5544 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005545 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 }
5547
5548 if (evaluate)
5549 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005550 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005551 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005552 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005553
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005554 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005557 clear_tv(&var2);
5558 return res;
5559 }
5560 return OK;
5561}
5562
5563/*
5564 * Check if "rettv" can have an [index] or [sli:ce]
5565 */
5566 int
5567check_can_index(typval_T *rettv, int evaluate, int verbose)
5568{
5569 switch (rettv->v_type)
5570 {
5571 case VAR_FUNC:
5572 case VAR_PARTIAL:
5573 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005574 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005575 return FAIL;
5576 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005577 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005578 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005579 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005580 case VAR_BOOL:
5581 case VAR_SPECIAL:
5582 case VAR_JOB:
5583 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005584 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005585 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005586 if (verbose)
5587 emsg(_(e_cannot_index_special_variable));
5588 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005589 case VAR_CLASS:
5590 case VAR_TYPEALIAS:
5591 if (verbose)
5592 check_typval_is_value(rettv);
5593 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005594 case VAR_UNKNOWN:
5595 case VAR_ANY:
5596 case VAR_VOID:
5597 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005599 emsg(_(e_cannot_index_special_variable));
5600 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005602 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005604 case VAR_STRING:
5605 case VAR_LIST:
5606 case VAR_DICT:
5607 case VAR_BLOB:
5608 break;
5609 case VAR_NUMBER:
5610 if (in_vim9script())
5611 emsg(_(e_cannot_index_number));
5612 break;
5613 }
5614 return OK;
5615}
5616
5617/*
5618 * Apply index or range to "rettv".
5619 * "var1" is the first index, NULL for [:expr].
5620 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005621 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5622 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005623 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5624 */
5625 int
5626eval_index_inner(
5627 typval_T *rettv,
5628 int is_range,
5629 typval_T *var1,
5630 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005631 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005632 char_u *key,
5633 int keylen,
5634 int verbose)
5635{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005636 varnumber_T n1, n2 = 0;
5637 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005638
5639 n1 = 0;
5640 if (var1 != NULL && rettv->v_type != VAR_DICT)
5641 n1 = tv_get_number(var1);
5642
5643 if (is_range)
5644 {
5645 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005647 if (verbose)
5648 emsg(_(e_cannot_slice_dictionary));
5649 return FAIL;
5650 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005651 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005652 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005653 else
5654 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005655 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005656
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005657 switch (rettv->v_type)
5658 {
5659 case VAR_UNKNOWN:
5660 case VAR_ANY:
5661 case VAR_VOID:
5662 case VAR_FUNC:
5663 case VAR_PARTIAL:
5664 case VAR_FLOAT:
5665 case VAR_BOOL:
5666 case VAR_SPECIAL:
5667 case VAR_JOB:
5668 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005669 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005670 case VAR_CLASS:
5671 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005672 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005673 break; // not evaluating, skipping over subscript
5674
5675 case VAR_NUMBER:
5676 case VAR_STRING:
5677 {
5678 char_u *s = tv_get_string(rettv);
5679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005681 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005682 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005683 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005684 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005685 else
5686 s = char_from_string(s, n1);
5687 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005688 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005690 // The resulting variable is a substring. If the indexes
5691 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 if (n1 < 0)
5693 {
5694 n1 = len + n1;
5695 if (n1 < 0)
5696 n1 = 0;
5697 }
5698 if (n2 < 0)
5699 n2 = len + n2;
5700 else if (n2 >= len)
5701 n2 = len;
5702 if (n1 >= len || n2 < 0 || n1 > n2)
5703 s = NULL;
5704 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005705 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 }
5707 else
5708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005709 // The resulting variable is a string of a single
5710 // character. If the index is too big or negative the
5711 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 if (n1 >= len || n1 < 0)
5713 s = NULL;
5714 else
5715 s = vim_strnsave(s + n1, 1);
5716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 clear_tv(rettv);
5718 rettv->v_type = VAR_STRING;
5719 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005720 }
5721 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005723 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005724 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5725 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005726 break;
5727
5728 case VAR_LIST:
5729 if (var1 == NULL)
5730 n1 = 0;
5731 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005732 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005733 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005734 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005735 return FAIL;
5736 break;
5737
5738 case VAR_DICT:
5739 {
5740 dictitem_T *item;
5741 typval_T tmp;
5742
5743 if (key == NULL)
5744 {
5745 key = tv_get_string_chk(var1);
5746 if (key == NULL)
5747 return FAIL;
5748 }
5749
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005750 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005751
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005752 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005753 {
5754 if (verbose)
5755 {
5756 if (keylen > 0)
5757 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005758 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005759 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005760 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005761 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005762
5763 copy_tv(&item->di_tv, &tmp);
5764 clear_tv(rettv);
5765 *rettv = tmp;
5766 }
5767 break;
5768 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 return OK;
5770}
5771
5772/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005773 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005774 */
5775 char_u *
5776partial_name(partial_T *pt)
5777{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005778 if (pt != NULL)
5779 {
5780 if (pt->pt_name != NULL)
5781 return pt->pt_name;
5782 if (pt->pt_func != NULL)
5783 return pt->pt_func->uf_name;
5784 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005785 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005786}
5787
Bram Moolenaarddecc252016-04-06 22:59:37 +02005788 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005789partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005790{
5791 int i;
5792
5793 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005794 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005795 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005796 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005797 if (pt->pt_name != NULL)
5798 {
5799 func_unref(pt->pt_name);
5800 vim_free(pt->pt_name);
5801 }
5802 else
5803 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005804 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005805
Bram Moolenaar54656012021-06-09 20:50:46 +02005806 // "out_up" is no longer used, decrement refcount on partial that owns it.
5807 partial_unref(pt->pt_outer.out_up_partial);
5808
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005809 // Using pt_outer from another partial.
5810 partial_unref(pt->pt_outer_partial);
5811
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005812 // Decrease the reference count for the context of a closure. If down
5813 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005814 if (pt->pt_funcstack != NULL)
5815 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005816 --pt->pt_funcstack->fs_refcount;
5817 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005818 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005819 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005820 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5821 if (pt->pt_loopvars[i] != NULL)
5822 {
5823 --pt->pt_loopvars[i]->lvs_refcount;
5824 loopvars_check_refcount(pt->pt_loopvars[i]);
5825 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005826
Bram Moolenaarddecc252016-04-06 22:59:37 +02005827 vim_free(pt);
5828}
5829
5830/*
5831 * Unreference a closure: decrement the reference count and free it when it
5832 * becomes zero.
5833 */
5834 void
5835partial_unref(partial_T *pt)
5836{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005837 if (pt == NULL)
5838 return;
5839
5840 int done = FALSE;
5841
5842 if (--pt->pt_refcount <= 0)
5843 partial_free(pt);
5844
5845 // If the reference count goes down to one, the funcstack may be the
5846 // only reference and can be freed if no other partials reference it.
5847 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005848 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005849 // careful: if the funcstack is freed it may contain this partial
5850 // and it gets freed as well
5851 if (pt->pt_funcstack != NULL)
5852 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005853
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005854 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005855 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005856 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005857
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005858 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5859 if (pt->pt_loopvars[depth] != NULL
5860 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005861 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005862 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005863 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005864}
5865
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005867 * Return a textual representation of a string in "tv".
5868 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5869 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5870 * strings as "string()", otherwise does not put quotes around strings.
5871 * May return NULL.
5872 */
5873 static char_u *
5874string_tv2string(
5875 typval_T *tv,
5876 char_u **tofree,
5877 int echo_style,
5878 int composite_val)
5879{
5880 char_u *r = NULL;
5881
5882 if (echo_style && !composite_val)
5883 {
5884 *tofree = NULL;
5885 r = tv->vval.v_string;
5886 if (r == NULL)
5887 r = (char_u *)"";
5888 }
5889 else
5890 {
5891 *tofree = string_quote(tv->vval.v_string, FALSE);
5892 r = *tofree;
5893 }
5894
5895 return r;
5896}
5897
5898/*
5899 * Return a textual representation of a function in "tv".
5900 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5901 * When "echo_style" is FALSE, put quotes around the function name as
5902 * "function()", otherwise does not put quotes around function name.
5903 * May return NULL.
5904 */
5905 static char_u *
5906func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5907{
5908 char_u *r = NULL;
5909 char_u buf[MAX_FUNC_NAME_LEN];
5910
5911 if (echo_style)
5912 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005913 *tofree = NULL;
5914
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005915 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005916 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005917 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005918 {
5919 r = make_ufunc_name_readable(tv->vval.v_string, buf,
5920 MAX_FUNC_NAME_LEN);
5921 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005922 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005923 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005924 }
5925 else
5926 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005927 char_u *s = NULL;
5928
5929 if (tv->vval.v_string != NULL)
5930 s = make_ufunc_name_readable(tv->vval.v_string, buf,
5931 MAX_FUNC_NAME_LEN);
5932
5933 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005934 }
5935
5936 return r;
5937}
5938
5939/*
Ernie Rael9d779c52024-07-07 20:41:44 +02005940 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
5941 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5942 * When "echo_style" is FALSE, put quotes around the function name as
5943 * "function()", otherwise does not put quotes around function name.
5944 * May return NULL.
5945 */
5946 static char_u *
5947method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5948{
5949 char_u buf[MAX_FUNC_NAME_LEN];
5950 partial_T *pt = tv->vval.v_partial;
5951
5952 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
5953 pt->pt_func->uf_script_ctx.sc_sid,
5954 pt->pt_func->uf_class->class_name,
5955 pt->pt_func->uf_name);
5956 if (len >= sizeof(buf))
5957 {
5958 if (echo_style)
5959 {
5960 *tofree = NULL;
5961 return (char_u *)"function()";
5962 }
5963 else
5964 return *tofree = string_quote((char_u*)"", TRUE);
5965 }
5966
5967 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
5968}
5969
5970/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005971 * Return a textual representation of a partial in "tv".
5972 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5973 * "numbuf" is used for a number. May return NULL.
5974 */
5975 static char_u *
5976partial_tv2string(
5977 typval_T *tv,
5978 char_u **tofree,
5979 char_u *numbuf,
5980 int copyID)
5981{
5982 char_u *r = NULL;
5983 partial_T *pt;
5984 char_u *fname;
5985 garray_T ga;
5986 int i;
5987 char_u *tf;
5988
5989 pt = tv->vval.v_partial;
5990 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5991
5992 ga_init2(&ga, 1, 100);
5993 ga_concat(&ga, (char_u *)"function(");
5994 if (fname != NULL)
5995 {
5996 // When using uf_name prepend "g:" for a global function.
5997 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5998 && vim_isupper(fname[1]))
5999 {
6000 ga_concat(&ga, (char_u *)"'g:");
6001 ga_concat(&ga, fname + 1);
6002 }
6003 else
6004 ga_concat(&ga, fname);
6005 vim_free(fname);
6006 }
6007 if (pt != NULL && pt->pt_argc > 0)
6008 {
6009 ga_concat(&ga, (char_u *)", [");
6010 for (i = 0; i < pt->pt_argc; ++i)
6011 {
6012 if (i > 0)
6013 ga_concat(&ga, (char_u *)", ");
6014 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6015 vim_free(tf);
6016 }
6017 ga_concat(&ga, (char_u *)"]");
6018 }
6019 if (pt != NULL && pt->pt_dict != NULL)
6020 {
6021 typval_T dtv;
6022
6023 ga_concat(&ga, (char_u *)", ");
6024 dtv.v_type = VAR_DICT;
6025 dtv.vval.v_dict = pt->pt_dict;
6026 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6027 vim_free(tf);
6028 }
6029 // terminate with ')' and a NUL
6030 ga_concat_len(&ga, (char_u *)")", 2);
6031
6032 *tofree = ga.ga_data;
6033 r = *tofree;
6034
6035 return r;
6036}
6037
6038/*
6039 * Return a textual representation of a List in "tv".
6040 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6041 * When "copyID" is not zero replace recursive lists with "...". When
6042 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6043 * May return NULL.
6044 */
6045 static char_u *
6046list_tv2string(
6047 typval_T *tv,
6048 char_u **tofree,
6049 int copyID,
6050 int restore_copyID)
6051{
6052 char_u *r = NULL;
6053
6054 if (tv->vval.v_list == NULL)
6055 {
6056 // NULL list is equivalent to empty list.
6057 *tofree = NULL;
6058 r = (char_u *)"[]";
6059 }
6060 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6061 && tv->vval.v_list->lv_len > 0)
6062 {
6063 *tofree = NULL;
6064 r = (char_u *)"[...]";
6065 }
6066 else
6067 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006068 int old_copyID;
6069 if (restore_copyID)
6070 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006071
6072 tv->vval.v_list->lv_copyID = copyID;
6073 *tofree = list2string(tv, copyID, restore_copyID);
6074 if (restore_copyID)
6075 tv->vval.v_list->lv_copyID = old_copyID;
6076 r = *tofree;
6077 }
6078
6079 return r;
6080}
6081
6082/*
6083 * Return a textual representation of a Dict in "tv".
6084 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6085 * When "copyID" is not zero replace recursive dicts with "...".
6086 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6087 * replaced with "...". May return NULL.
6088 */
6089 static char_u *
6090dict_tv2string(
6091 typval_T *tv,
6092 char_u **tofree,
6093 int copyID,
6094 int restore_copyID)
6095{
6096 char_u *r = NULL;
6097
6098 if (tv->vval.v_dict == NULL)
6099 {
6100 // NULL dict is equivalent to empty dict.
6101 *tofree = NULL;
6102 r = (char_u *)"{}";
6103 }
6104 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6105 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6106 {
6107 *tofree = NULL;
6108 r = (char_u *)"{...}";
6109 }
6110 else
6111 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006112 int old_copyID;
6113 if (restore_copyID)
6114 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006115
6116 tv->vval.v_dict->dv_copyID = copyID;
6117 *tofree = dict2string(tv, copyID, restore_copyID);
6118 if (restore_copyID)
6119 tv->vval.v_dict->dv_copyID = old_copyID;
6120 r = *tofree;
6121 }
6122
6123 return r;
6124}
6125
6126/*
6127 * Return a textual representation of a job or a channel in "tv".
6128 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6129 * "numbuf" is used for a number.
6130 * When "composite_val" is FALSE, put quotes around strings as "string()",
6131 * otherwise does not put quotes around strings.
6132 * May return NULL.
6133 */
6134 static char_u *
6135jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006136 typval_T *tv UNUSED,
6137 char_u **tofree UNUSED,
6138 char_u *numbuf UNUSED,
6139 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006140{
6141 char_u *r = NULL;
6142
6143#ifdef FEAT_JOB_CHANNEL
6144 *tofree = NULL;
6145
6146 if (tv->v_type == VAR_JOB)
6147 r = job_to_string_buf(tv, numbuf);
6148 else
6149 r = channel_to_string_buf(tv, numbuf);
6150
6151 if (composite_val)
6152 {
6153 *tofree = string_quote(r, FALSE);
6154 r = *tofree;
6155 }
6156#endif
6157
6158 return r;
6159}
6160
6161/*
6162 * Return a textual representation of a class in "tv".
6163 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6164 * May return NULL.
6165 */
6166 static char_u *
6167class_tv2string(typval_T *tv, char_u **tofree)
6168{
6169 char_u *r = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01006170 size_t rsize;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006171 class_T *cl = tv->vval.v_class;
John Marriottbd4614f2024-11-18 20:25:21 +01006172 char_u *class_name = (char_u *)"[unknown]";
6173 size_t class_namelen = 9;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006174 char *s = "class";
John Marriottbd4614f2024-11-18 20:25:21 +01006175 size_t slen = 5;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006176
John Marriottbd4614f2024-11-18 20:25:21 +01006177 if (cl != NULL)
6178 {
6179 class_name = cl->class_name;
6180 class_namelen = STRLEN(cl->class_name);
6181 if (IS_INTERFACE(cl))
6182 {
6183 s = "interface";
6184 slen = 9;
6185 }
6186 else if (IS_ENUM(cl))
6187 {
6188 s = "enum";
6189 slen = 4;
6190 }
6191 }
6192
6193 rsize = slen + 1 + class_namelen + 1;
6194 r = *tofree = alloc(rsize);
6195 if (r != NULL)
6196 vim_snprintf((char *)r, rsize, "%s %s", s, (char *)class_name);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006197
6198 return r;
6199}
6200
6201/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006202 * Return a textual representation of an Object in "tv".
6203 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6204 * When "copyID" is not zero replace recursive object with "...".
6205 * When "restore_copyID" is FALSE, repeated items in the object are
6206 * replaced with "...". May return NULL.
6207 */
6208 static char_u *
6209object_tv2string(
6210 typval_T *tv,
6211 char_u **tofree,
6212 int copyID,
6213 int restore_copyID,
6214 char_u *numbuf,
6215 int echo_style,
6216 int composite_val)
6217{
6218 char_u *r = NULL;
6219
6220 object_T *obj = tv->vval.v_object;
6221 if (obj == NULL || obj->obj_class == NULL)
6222 {
6223 *tofree = NULL;
6224 r = (char_u *)"object of [unknown]";
6225 }
6226 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006227 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006228 {
John Marriottbd4614f2024-11-18 20:25:21 +01006229 size_t n = 25 + STRLEN((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006230 r = alloc(n);
6231 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006232 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006233 obj->obj_class->class_name);
6234 *tofree = r;
6235 }
6236 else
6237 {
6238 int old_copyID;
6239 if (restore_copyID)
6240 old_copyID = obj->obj_copyID;
6241
6242 obj->obj_copyID = copyID;
6243 *tofree = object2string(obj, numbuf, copyID, echo_style,
6244 restore_copyID, composite_val);
6245 if (restore_copyID)
6246 obj->obj_copyID = old_copyID;
6247 r = *tofree;
6248 }
6249
6250 return r;
6251}
6252
6253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * Return a string with the string representation of a variable.
6255 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006256 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006257 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006258 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006259 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006260 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006261 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6262 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006263 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006265 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006267 typval_T *tv,
6268 char_u **tofree,
6269 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006270 int copyID,
6271 int echo_style,
6272 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006273 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 static int recurse = 0;
6276 char_u *r = NULL;
6277
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006279 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006280 if (!did_echo_string_emsg)
6281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006282 // Only give this message once for a recursive call to avoid
6283 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006284 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006285 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006286 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006287 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006289 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006290 }
6291 ++recurse;
6292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 switch (tv->v_type)
6294 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006295 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006296 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006297 break;
6298
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006300 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006302
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006303 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006304 if (tv->vval.v_partial == NULL
6305 || tv->vval.v_partial->pt_obj == NULL)
6306 r = partial_tv2string(tv, tofree, numbuf, copyID);
6307 else
6308 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006309 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006311 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006312 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006313 break;
6314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006316 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006318
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006320 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006321 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006323 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006324 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006325 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006327 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006328 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006329 break;
6330
Bram Moolenaar835dc632016-02-07 14:27:38 +01006331 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006332 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006333 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006335
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006336 case VAR_INSTR:
6337 *tofree = NULL;
6338 r = (char_u *)"instructions";
6339 break;
6340
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006341 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006342 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006343 break;
6344
6345 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006346 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6347 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006348 break;
6349
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006350 case VAR_FLOAT:
6351 *tofree = NULL;
6352 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6353 r = numbuf;
6354 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006355
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006356 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006357 case VAR_SPECIAL:
6358 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006359 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006360 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006361
6362 case VAR_TYPEALIAS:
6363 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6364 r = *tofree;
6365 if (r == NULL)
6366 r = (char_u *)"";
6367 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006368 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006369
Bram Moolenaar8502c702014-06-17 12:51:16 +02006370 if (--recurse == 0)
6371 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006373}
6374
6375/*
6376 * Return a string with the string representation of a variable.
6377 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6378 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006379 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006380 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006381 * May return NULL.
6382 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006384echo_string(
6385 typval_T *tv,
6386 char_u **tofree,
6387 char_u *numbuf,
6388 int copyID)
6389{
6390 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6391}
6392
6393/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006394 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6395 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006396 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006397 */
6398 int
6399buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6400{
6401 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006402 char_u *t;
6403 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006404
6405 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6406 return -1;
6407
6408 if (lnum > buf->b_ml.ml_line_count)
6409 lnum = buf->b_ml.ml_line_count;
6410
6411 str = ml_get_buf(buf, lnum, FALSE);
6412 if (str == NULL)
6413 return -1;
6414
6415 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006416 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006417
Bram Moolenaar91458462021-01-13 20:08:38 +01006418 // count the number of characters
6419 t = str;
6420 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6421 t += mb_ptr2len(t);
6422
6423 // In insert mode, when the cursor is at the end of a non-empty line,
6424 // byteidx points to the NUL character immediately past the end of the
6425 // string. In this case, add one to the character count.
6426 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6427 count++;
6428
6429 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006430}
6431
6432/*
6433 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006434 * byte index. Works only for loaded buffers. Returns -1 on failure.
6435 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006436 */
6437 int
6438buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6439{
6440 char_u *str;
6441 char_u *t;
6442
6443 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6444 return -1;
6445
6446 if (lnum > buf->b_ml.ml_line_count)
6447 lnum = buf->b_ml.ml_line_count;
6448
6449 str = ml_get_buf(buf, lnum, FALSE);
6450 if (str == NULL)
6451 return -1;
6452
6453 // Convert the character offset to a byte offset
6454 t = str;
6455 while (*t != NUL && --charidx > 0)
6456 t += mb_ptr2len(t);
6457
Bram Moolenaar91458462021-01-13 20:08:38 +01006458 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006459}
6460
6461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006463 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466var2fpos(
6467 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006469 int *fnum, // set to fnum for '0, 'A, etc.
6470 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006472 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006474 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006477 if (varp->v_type == VAR_LIST)
6478 {
6479 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006480 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006481 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006482 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006483
6484 l = varp->vval.v_list;
6485 if (l == NULL)
6486 return NULL;
6487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006489 pos.lnum = list_find_nr(l, 0L, &error);
6490 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006491 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006492 if (charcol)
6493 len = (long)mb_charlen(ml_get(pos.lnum));
6494 else
John Marriottbfcc8952024-03-11 22:04:45 +01006495 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006496
Bram Moolenaarec65d772020-08-20 22:29:12 +02006497 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006499 li = list_find(l, 1L);
6500 if (li != NULL && li->li_tv.v_type == VAR_STRING
6501 && li->li_tv.vval.v_string != NULL
6502 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006503 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006504 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006505 }
6506 else
6507 {
6508 pos.col = list_find_nr(l, 1L, &error);
6509 if (error)
6510 return NULL;
6511 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006513 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006514 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006516 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006519 pos.coladd = list_find_nr(l, 2L, &error);
6520 if (error)
6521 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006522
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006523 return &pos;
6524 }
6525
Bram Moolenaarc5809432021-03-27 21:23:30 +01006526 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6527 return NULL;
6528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006529 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530 if (name == NULL)
6531 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006532
6533 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006534 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006535 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006536 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006537 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006538 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006539 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006540 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006541 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006542 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006543 pos = VIsual;
6544 else
6545 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006546 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006547 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006548 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006550 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006551 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6553 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006554 pos = *pp;
6555 }
6556 if (pos.lnum != 0)
6557 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006558 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006559 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6560 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006562
Bram Moolenaara5525202006-03-02 22:52:09 +00006563 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006564
Bram Moolenaar477933c2007-07-17 14:32:23 +00006565 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006566 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006567 // the "w_valid" flags are not reset when moving the cursor, but they
6568 // do matter for update_topline() and validate_botline().
6569 check_cursor_moved(curwin);
6570
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006571 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006573 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006575 // In silent Ex mode topline is zero, but that's not a valid line
6576 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006577 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006578 return &pos;
6579 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006580 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006581 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006582 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006584 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006585 return &pos;
6586 }
6587 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006590 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {
6592 pos.lnum = curbuf->b_ml.ml_line_count;
6593 pos.col = 0;
6594 }
6595 else
6596 {
6597 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006598 if (charcol)
6599 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6600 else
John Marriottbfcc8952024-03-11 22:04:45 +01006601 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603 return &pos;
6604 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006605 if (in_vim9script())
6606 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 return NULL;
6608}
6609
6610/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006611 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006612 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006613 * Note that the column is passed on as-is, the caller may want to decrement
6614 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006615 * If "charcol" is TRUE use the column as the character index instead of the
6616 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006617 * Return FAIL when conversion is not possible, doesn't check the position for
6618 * validity.
6619 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621list2fpos(
6622 typval_T *arg,
6623 pos_T *posp,
6624 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006625 colnr_T *curswantp,
6626 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006627{
6628 list_T *l = arg->vval.v_list;
6629 long i = 0;
6630 long n;
6631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006632 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6633 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006634 if (arg->v_type != VAR_LIST
6635 || l == NULL
6636 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006637 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006638 return FAIL;
6639
6640 if (fnump != NULL)
6641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006643 if (n < 0)
6644 return FAIL;
6645 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006647 *fnump = n;
6648 }
6649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006651 if (n < 0)
6652 return FAIL;
6653 posp->lnum = n;
6654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006656 if (n < 0)
6657 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006658 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006659 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006660 if (charcol)
6661 {
6662 buf_T *buf;
6663
6664 // Get the text for the specified line in a loaded buffer
6665 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6666 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6667 return FAIL;
6668
Bram Moolenaar79f23442022-10-10 12:42:57 +01006669 n = buf_charidx_to_byteidx(buf,
6670 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006671 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006672 posp->col = n;
6673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006675 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006676 posp->coladd = 0;
6677 else
6678 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006679
Bram Moolenaar493c1782014-05-28 14:34:46 +02006680 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006681 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006682
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006683 return OK;
6684}
6685
6686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 * Get the length of an environment variable name.
6688 * Advance "arg" to the first character after the name.
6689 * Return 0 for error.
6690 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 char_u *p;
6695 int len;
6696
6697 for (p = *arg; vim_isIDc(*p); ++p)
6698 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 return 0;
6701
6702 len = (int)(p - *arg);
6703 *arg = p;
6704 return len;
6705}
6706
6707/*
6708 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006709 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 * Return 0 if something is wrong.
6711 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006713get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 char_u *p;
6716 int len;
6717
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006718 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006720 {
6721 if (*p == ':')
6722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006723 // "s:" is start of "s:var", but "n:" is not and can be used in
6724 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006725 len = (int)(p - *arg);
6726 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6727 || len > 1)
6728 break;
6729 }
6730 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006731 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 return 0;
6733
6734 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006735 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
6737 return len;
6738}
6739
6740/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006741 * Get the length of the name of a variable or function.
6742 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006744 * Return -1 if curly braces expansion failed.
6745 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 * If the name contains 'magic' {}'s, expand them and return the
6747 * expanded name in an allocated string via 'alias' - caller must free.
6748 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006750get_name_len(
6751 char_u **arg,
6752 char_u **alias,
6753 int evaluate,
6754 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 char_u *p;
6758 char_u *expr_start;
6759 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006761 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6764 && (*arg)[2] == (int)KE_SNR)
6765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006766 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 *arg += 3;
6768 return get_id_len(arg) + 3;
6769 }
6770 len = eval_fname_script(*arg);
6771 if (len > 0)
6772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006773 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 *arg += len;
6775 }
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006780 p = find_name_end(*arg, &expr_start, &expr_end,
6781 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (expr_start != NULL)
6783 {
6784 char_u *temp_string;
6785
6786 if (!evaluate)
6787 {
6788 len += (int)(p - *arg);
6789 *arg = skipwhite(p);
6790 return len;
6791 }
6792
6793 /*
6794 * Include any <SID> etc in the expanded string:
6795 * Thus the -len here.
6796 */
6797 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6798 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006799 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 *alias = temp_string;
6801 *arg = skipwhite(p);
6802 return (int)STRLEN(temp_string);
6803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006806 // Only give an error when there is something, otherwise it will be
6807 // reported at a higher level.
6808 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006809 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
6811 return len;
6812}
6813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814/*
6815 * Find the end of a variable or function name, taking care of magic braces.
6816 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6817 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006818 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006819 * Return a pointer to just after the name. Equal to "arg" if there is no
6820 * valid name.
6821 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006823find_name_end(
6824 char_u *arg,
6825 char_u **expr_start,
6826 char_u **expr_end,
6827 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 int mb_nest = 0;
6830 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006832 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006833 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 *expr_start = NULL;
6838 *expr_end = NULL;
6839 }
6840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006841 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006842 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006843 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006844 return arg;
6845
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006846 for (p = arg; *p != NUL
6847 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006848 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006849 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006850 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006851 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006852 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006853 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006854 if (*p == '\'')
6855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006856 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006857 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006858 ;
6859 if (*p == NUL)
6860 break;
6861 }
6862 else if (*p == '"')
6863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006864 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006865 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006866 if (*p == '\\' && p[1] != NUL)
6867 ++p;
6868 if (*p == NUL)
6869 break;
6870 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006871 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006873 // "s:" is start of "s:var", but "n:" is not and can be used in
6874 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006875 len = (int)(p - arg);
6876 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006877 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006878 break;
6879 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 if (*p == '[')
6884 ++br_nest;
6885 else if (*p == ']')
6886 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006888
zeertzjqa93d9cd2023-05-02 16:25:47 +01006889 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 if (*p == '{')
6892 {
6893 mb_nest++;
6894 if (expr_start != NULL && *expr_start == NULL)
6895 *expr_start = p;
6896 }
6897 else if (*p == '}')
6898 {
6899 mb_nest--;
6900 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6901 *expr_end = p;
6902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
6906 return p;
6907}
6908
6909/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006910 * Expands out the 'magic' {}'s in a variable/function name.
6911 * Note that this can call itself recursively, to deal with
6912 * constructs like foo{bar}{baz}{bam}
6913 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6914 * "in_start" ^
6915 * "expr_start" ^
6916 * "expr_end" ^
6917 * "in_end" ^
6918 *
6919 * Returns a new allocated string, which the caller must free.
6920 * Returns NULL for failure.
6921 */
6922 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923make_expanded_name(
6924 char_u *in_start,
6925 char_u *expr_start,
6926 char_u *expr_end,
6927 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006928{
6929 char_u c1;
6930 char_u *retval = NULL;
6931 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006932
6933 if (expr_end == NULL || in_end == NULL)
6934 return NULL;
6935 *expr_start = NUL;
6936 *expr_end = NUL;
6937 c1 = *in_end;
6938 *in_end = NUL;
6939
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006940 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006941 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006942 {
John Marriottbd4614f2024-11-18 20:25:21 +01006943 size_t retvalsize = (size_t)(expr_start - in_start)
6944 + STRLEN(temp_result)
6945 + (size_t)(in_end - expr_end) + 1;
6946
6947 retval = alloc(retvalsize);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 if (retval != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01006949 vim_snprintf((char *)retval, retvalsize, "%s%s%s",
6950 in_start, temp_result, expr_end + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006951 }
6952 vim_free(temp_result);
6953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006955 *expr_start = '{';
6956 *expr_end = '}';
6957
6958 if (retval != NULL)
6959 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006960 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006961 if (expr_start != NULL)
6962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006963 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006964 temp_result = make_expanded_name(retval, expr_start,
6965 expr_end, temp_result);
6966 vim_free(retval);
6967 retval = temp_result;
6968 }
6969 }
6970
6971 return retval;
6972}
6973
6974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006979eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006981 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006982}
6983
6984/*
6985 * Return TRUE if character "c" can be used as the first character in a
6986 * variable or function name (excluding '{' and '}').
6987 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006990{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006991 return ASCII_ISALPHA(c) || c == '_';
6992}
6993
6994/*
6995 * Return TRUE if character "c" can be used as the first character of a
6996 * dictionary key.
6997 */
6998 int
6999eval_isdictc(int c)
7000{
7001 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002}
7003
7004/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007005 * Handle:
7006 * - expr[expr], expr[expr:expr] subscript
7007 * - ".name" lookup
7008 * - function call with Funcref variable: func(expr)
7009 * - method call: var->method()
7010 *
7011 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007012 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007013 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007015handle_subscript(
7016 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007017 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007019 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007020 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007021{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007022 int evaluate = evalarg != NULL
7023 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007024 int ret = OK;
7025 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007026 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007027 int getnext;
7028 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007029
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007030 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007031 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007032 // When at the end of the line and ".name" or "->{" or "->X" follows in
7033 // the next line then consume the line break.
7034 p = eval_next_non_blank(*arg, evalarg, &getnext);
7035 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007036 && ((*p == '.'
7037 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7038 || rettv->v_type == VAR_CLASS
7039 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007040 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7041 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7042 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007043 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007044 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007045 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007046 check_white = FALSE;
7047 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007048
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007049 if (rettv->v_type == VAR_ANY)
7050 {
7051 char_u *exp_name;
7052 int cc;
7053 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007054 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007055 type_T *type;
7056
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007057 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007058 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007059 if (**arg != '.')
7060 {
7061 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007062 semsg(_(e_expected_dot_after_name_str),
7063 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007064 ret = FAIL;
7065 break;
7066 }
7067 ++*arg;
7068 if (IS_WHITE_OR_NUL(**arg))
7069 {
7070 if (verbose)
7071 emsg(_(e_no_white_space_allowed_after_dot));
7072 ret = FAIL;
7073 break;
7074 }
7075
7076 // isolate the name
7077 exp_name = *arg;
7078 while (eval_isnamec(**arg))
7079 ++*arg;
7080 cc = **arg;
7081 **arg = NUL;
7082
7083 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007084 evalarg == NULL ? NULL : evalarg->eval_cctx,
7085 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007086 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007087
7088 if (idx < 0 && ufunc == NULL)
7089 {
7090 ret = FAIL;
7091 break;
7092 }
7093 if (idx >= 0)
7094 {
7095 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7096 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7097
7098 copy_tv(sv->sv_tv, rettv);
7099 }
7100 else
7101 {
7102 rettv->v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01007103 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007104 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007105 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007106 }
7107
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007108 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7109 || rettv->v_type == VAR_PARTIAL))
7110 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007111 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007112 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7113 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007114
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007115 // Stop the expression evaluation when immediately aborting on
7116 // error, or when an interrupt occurred or an exception was thrown
7117 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007118 if (aborting())
7119 {
7120 if (ret == OK)
7121 clear_tv(rettv);
7122 ret = FAIL;
7123 }
7124 dict_unref(selfdict);
7125 selfdict = NULL;
7126 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007127 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007128 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007129 if (in_vim9script())
7130 *arg = skipwhite(p + 2);
7131 else
7132 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007133 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007134 {
zeertzjqc481ad32023-03-11 16:18:51 +00007135 emsg(_(e_no_white_space_allowed_before_parenthesis));
7136 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007137 }
zeertzjqc481ad32023-03-11 16:18:51 +00007138 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7139 // expr->{lambda}() or expr->(lambda)()
7140 ret = eval_lambda(arg, rettv, evalarg, verbose);
7141 else
7142 // expr->name()
7143 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007144 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007145 // "." is ".name" lookup when we found a dict or when evaluating and
7146 // scriptversion is at least 2, where string concatenation is "..".
7147 else if (**arg == '['
7148 || (**arg == '.' && (rettv->v_type == VAR_DICT
7149 || (!evaluate
7150 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007151 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007152 {
7153 dict_unref(selfdict);
7154 if (rettv->v_type == VAR_DICT)
7155 {
7156 selfdict = rettv->vval.v_dict;
7157 if (selfdict != NULL)
7158 ++selfdict->dv_refcount;
7159 }
7160 else
7161 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007162 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007163 {
7164 clear_tv(rettv);
7165 ret = FAIL;
7166 }
7167 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007168 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7169 || rettv->v_type == VAR_OBJECT))
7170 {
7171 // class member: SomeClass.varname
7172 // class method: SomeClass.SomeMethod()
7173 // class constructor: SomeClass.new()
7174 // object member: someObject.varname
7175 // object method: someObject.SomeMethod()
7176 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7177 {
7178 clear_tv(rettv);
7179 ret = FAIL;
7180 }
7181 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007182 else
7183 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007184 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007186 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7187 // Don't do this when "Func" is already a partial that was bound
7188 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007189 if (selfdict != NULL
7190 && (rettv->v_type == VAR_FUNC
7191 || (rettv->v_type == VAR_PARTIAL
7192 && (rettv->vval.v_partial->pt_auto
7193 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007194 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007195
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007196 dict_unref(selfdict);
7197 return ret;
7198}
7199
7200/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 * Make a copy of an item.
7202 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007203 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7205 * reference to an already copied list/dict can be used.
7206 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209item_copy(
7210 typval_T *from,
7211 typval_T *to,
7212 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007213 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007215{
7216 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007218
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007221 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007222 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
7224 ++recurse;
7225
7226 switch (from->v_type)
7227 {
7228 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007229 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 case VAR_STRING:
7231 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007232 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007233 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007234 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007235 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007236 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007237 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007238 case VAR_CLASS:
7239 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007240 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007241 copy_tv(from, to);
7242 break;
7243 case VAR_LIST:
7244 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007245 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 if (from->vval.v_list == NULL)
7247 to->vval.v_list = NULL;
7248 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7249 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007250 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007251 to->vval.v_list = from->vval.v_list->lv_copylist;
7252 ++to->vval.v_list->lv_refcount;
7253 }
7254 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007255 to->vval.v_list = list_copy(from->vval.v_list,
7256 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007257 if (to->vval.v_list == NULL)
7258 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007260 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007262 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263 case VAR_DICT:
7264 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007265 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007266 if (from->vval.v_dict == NULL)
7267 to->vval.v_dict = NULL;
7268 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7269 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007270 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007271 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7272 ++to->vval.v_dict->dv_refcount;
7273 }
7274 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007275 to->vval.v_dict = dict_copy(from->vval.v_dict,
7276 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 if (to->vval.v_dict == NULL)
7278 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007280 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007281 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007283 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 }
7286 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007287 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288}
7289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 void
7291echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7292{
7293 char_u *tofree;
7294 char_u numbuf[NUMBUFLEN];
7295 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7296
7297 if (*atstart)
7298 {
7299 *atstart = FALSE;
7300 // Call msg_start() after eval1(), evaluating the expression
7301 // may cause a message to appear.
7302 if (with_space)
7303 {
7304 // Mark the saved text as finishing the line, so that what
7305 // follows is displayed on a new line when scrolling back
7306 // at the more prompt.
7307 msg_sb_eol();
7308 msg_start();
7309 }
7310 }
7311 else if (with_space)
7312 msg_puts_attr(" ", echo_attr);
7313
7314 if (p != NULL)
7315 for ( ; *p != NUL && !got_int; ++p)
7316 {
7317 if (*p == '\n' || *p == '\r' || *p == TAB)
7318 {
7319 if (*p != TAB && *needclr)
7320 {
7321 // remove any text still there from the command
7322 msg_clr_eos();
7323 *needclr = FALSE;
7324 }
7325 msg_putchar_attr(*p, echo_attr);
7326 }
7327 else
7328 {
7329 if (has_mbyte)
7330 {
7331 int i = (*mb_ptr2len)(p);
7332
7333 (void)msg_outtrans_len_attr(p, i, echo_attr);
7334 p += i - 1;
7335 }
7336 else
7337 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7338 }
7339 }
7340 vim_free(tofree);
7341}
7342
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 * ":echo expr1 ..." print each argument separated with a space, add a
7345 * newline at the end.
7346 * ":echon expr1 ..." print each argument plain.
7347 */
7348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007353 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 int needclr = TRUE;
7355 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007356 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007357 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007358 evalarg_T evalarg;
7359
Bram Moolenaare707c882020-07-01 13:07:37 +02007360 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
7362 if (eap->skip)
7363 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007364 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007366 // If eval1() causes an error message the text from the command may
7367 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007368 need_clr_eos = needclr;
7369
Bram Moolenaar68db9962021-05-09 23:19:22 +02007370 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007371 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {
7373 /*
7374 * Report the invalid expression unless the expression evaluation
7375 * has been cancelled due to an aborting error, an interrupt, or an
7376 * exception.
7377 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007378 if (!aborting() && did_emsg == did_emsg_before
7379 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007380 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007381 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 break;
7383 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007384 need_clr_eos = FALSE;
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007387 {
7388 if (rettv.v_type == VAR_VOID)
7389 {
7390 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7391 break;
7392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007394 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007396 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 arg = skipwhite(arg);
7398 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007399 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007400 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 if (eap->skip)
7403 --emsg_skip;
7404 else
7405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007406 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (needclr)
7408 msg_clr_eos();
7409 if (eap->cmdidx == CMD_echo)
7410 msg_end();
7411 }
7412}
7413
7414/*
7415 * ":echohl {name}".
7416 */
7417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007418ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007420 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
7423/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007424 * Returns the :echo attribute
7425 */
7426 int
7427get_echo_attr(void)
7428{
7429 return echo_attr;
7430}
7431
7432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 * ":execute expr1 ..." execute the result of an expression.
7434 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007435 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007437 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 * Each gets spaces around each argument and a newline at the end for
7439 * echo commands
7440 */
7441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007442ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 int ret = OK;
7447 char_u *p;
7448 garray_T ga;
7449 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007450 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 ga_init2(&ga, 1, 80);
7453
7454 if (eap->skip)
7455 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007456 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007458 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007459 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 if (!eap->skip)
7463 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007464 char_u buf[NUMBUFLEN];
7465
7466 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007467 {
7468 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7469 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007470 semsg(_(e_using_invalid_value_as_string_str),
7471 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007472 p = NULL;
7473 }
7474 else
7475 p = tv_get_string_buf(&rettv, buf);
7476 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007477 else
7478 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007479 if (p == NULL)
7480 {
7481 clear_tv(&rettv);
7482 ret = FAIL;
7483 break;
7484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 len = (int)STRLEN(p);
7486 if (ga_grow(&ga, len + 2) == FAIL)
7487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 ret = FAIL;
7490 break;
7491 }
7492 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 ga.ga_len += len;
7496 }
7497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007498 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 arg = skipwhite(arg);
7500 }
7501
7502 if (ret != FAIL && ga.ga_data != NULL)
7503 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007504 // use the first line of continuation lines for messages
7505 SOURCING_LNUM = start_lnum;
7506
Bram Moolenaar37fef162022-08-29 18:16:32 +01007507 if (eap->cmdidx == CMD_echomsg
7508 || eap->cmdidx == CMD_echowindow
7509 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007511 // Mark the already saved text as finishing the line, so that what
7512 // follows is displayed on a new line when scrolling back at the
7513 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007514 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007515 }
7516
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007517 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007518 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007519 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007520 out_flush();
7521 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007522 else if (eap->cmdidx == CMD_echowindow)
7523 {
7524#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007525 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007526#endif
7527 msg_attr(ga.ga_data, echo_attr);
7528#ifdef HAS_MESSAGE_WINDOW
7529 end_echowindow();
7530#endif
7531 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007532 else if (eap->cmdidx == CMD_echoconsole)
7533 {
7534 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7535 ui_write((char_u *)"\r\n", 2, TRUE);
7536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else if (eap->cmdidx == CMD_echoerr)
7538 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007539 int save_did_emsg = did_emsg;
7540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007541 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007542 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 if (!force_abort)
7544 did_emsg = save_did_emsg;
7545 }
7546 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007547 {
7548 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7549
7550 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7551 sticky_cmdmod_flags = cmdmod.cmod_flags
7552 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007554 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007555 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 }
7558
7559 ga_clear(&ga);
7560
7561 if (eap->skip)
7562 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007563 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564}
7565
7566/*
7567 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7568 * "arg" points to the "&" or '+' when called, to "option" when returning.
7569 * Returns NULL when no option name found. Otherwise pointer to the char
7570 * after the option name.
7571 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007572 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007573find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
7575 char_u *p = *arg;
7576
7577 ++p;
7578 if (*p == 'g' && p[1] == ':')
7579 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007580 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 p += 2;
7582 }
7583 else if (*p == 'l' && p[1] == ':')
7584 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007585 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 p += 2;
7587 }
7588 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007589 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
7591 if (!ASCII_ISALPHA(*p))
7592 return NULL;
7593 *arg = p;
7594
7595 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007596 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
7598 while (ASCII_ISALPHA(*p))
7599 ++p;
7600 return p;
7601}
7602
7603/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007604 * Display script name where an item was last set.
7605 * Should only be invoked when 'verbose' is non-zero.
7606 */
7607 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007608last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007609{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007610 char_u *p;
7611
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007612 if (script_ctx.sc_sid == 0)
7613 return;
7614
7615 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7616 if (p == NULL)
7617 return;
7618
7619 verbose_enter();
7620 msg_puts(_("\n\tLast set from "));
7621 msg_puts((char *)p);
7622 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007623 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007624 msg_puts(_(line_msg));
7625 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007626 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007627 verbose_leave();
7628 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007629}
7630
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007631#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633/*
7634 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007635 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 * "flags" can be "g" to do a global substitute.
7637 * Returns an allocated string, NULL for error.
7638 */
7639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007640do_string_sub(
7641 char_u *str,
John Marriottbd4614f2024-11-18 20:25:21 +01007642 size_t len,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643 char_u *pat,
7644 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007645 typval_T *expr,
John Marriottbd4614f2024-11-18 20:25:21 +01007646 char_u *flags,
7647 size_t *ret_len) // length of returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 garray_T ga;
7651 char_u *ret;
7652 char_u *save_cpo;
7653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007654 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007656 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 ga_init2(&ga, 1, 200);
7659
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 regmatch.rm_ic = p_ic;
7661 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7662 if (regmatch.regprog != NULL)
7663 {
John Marriottbd4614f2024-11-18 20:25:21 +01007664 char_u *tail = str;
7665 char_u *end = str + len;
7666 int do_all = (flags[0] == 'g');
7667 int sublen;
7668 int i;
7669 char_u *zero_width = NULL;
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007673 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007674 if (regmatch.startp[0] == regmatch.endp[0])
7675 {
7676 if (zero_width == regmatch.startp[0])
7677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007678 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007679 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007680 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7681 (size_t)i);
7682 ga.ga_len += i;
7683 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007684 continue;
7685 }
7686 zero_width = regmatch.startp[0];
7687 }
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 /*
7690 * Get some space for a temporary buffer to do the substitution
7691 * into. It will contain:
7692 * - The text up to where the match is.
7693 * - The substituted text.
7694 * - The text after the match.
7695 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007696 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007697 if (sublen <= 0)
7698 {
7699 ga_clear(&ga);
7700 break;
7701 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007702 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7704 {
7705 ga_clear(&ga);
7706 break;
7707 }
7708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007709 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 i = (int)(regmatch.startp[0] - tail);
7711 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007712 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007713 (void)vim_regsub(&regmatch, sub, expr,
7714 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7715 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007717 tail = regmatch.endp[0];
7718 if (*tail == NUL)
7719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (!do_all)
7721 break;
7722 }
7723
7724 if (ga.ga_data != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01007725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
John Marriottbd4614f2024-11-18 20:25:21 +01007727 ga.ga_len += (int)(end - tail);
7728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
Bram Moolenaar473de612013-06-08 18:19:48 +02007730 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732
John Marriottbd4614f2024-11-18 20:25:21 +01007733 if (ga.ga_data != NULL)
7734 {
7735 str = (char_u *)ga.ga_data;
7736 len = (size_t)ga.ga_len;
7737 }
7738 ret = vim_strnsave(str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007740 if (p_cpo == empty_option)
7741 p_cpo = save_cpo;
7742 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007744 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007745 // If it's still empty it was changed and restored, need to restore in
7746 // the complicated way.
7747 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007748 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007749 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
John Marriottbd4614f2024-11-18 20:25:21 +01007752 if (ret_len != NULL)
7753 *ret_len = len;
7754
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 return ret;
7756}