blob: 306cfe7f426491125766236630a3aa20153a2f57 [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 Lakshmanana061f342022-05-22 19:13:49 +01003997 * Handle the bitwise left/right shift operator expression:
3998 * 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/*
4515 * Handle a type cast before a base level expression.
4516 * "arg" must point to the first non-white of the expression.
4517 * "arg" is advanced to just after the recognized expression.
4518 * Return OK or FAIL.
4519 */
4520 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004521eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004522 char_u **arg,
4523 typval_T *rettv,
4524 evalarg_T *evalarg,
4525 int want_string) // after "." operator
4526{
4527 type_T *want_type = NULL;
4528 garray_T type_list; // list of pointers to allocated types
4529 int res;
4530 int evaluate = evalarg == NULL ? 0
4531 : (evalarg->eval_flags & EVAL_EVALUATE);
4532
4533 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004534 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4535 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004536 {
4537 ++*arg;
4538 ga_init2(&type_list, sizeof(type_T *), 10);
4539 want_type = parse_type(arg, &type_list, TRUE);
4540 if (want_type == NULL && (evaluate || **arg != '>'))
4541 {
4542 clear_type_list(&type_list);
4543 return FAIL;
4544 }
4545
4546 if (**arg != '>')
4547 {
4548 if (*skipwhite(*arg) == '>')
4549 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4550 else
4551 emsg(_(e_missing_gt));
4552 clear_type_list(&type_list);
4553 return FAIL;
4554 }
4555 ++*arg;
4556 *arg = skipwhite_and_linebreak(*arg, evalarg);
4557 }
4558
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004559 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004560
4561 if (want_type != NULL && evaluate)
4562 {
4563 if (res == OK)
4564 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004565 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4566 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004567
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004568 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004569 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004570 if (want_type->tt_type == VAR_BOOL
4571 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004572 && (actual->tt_flags & TTFLAG_BOOL_OK))
4573 {
4574 int n = tv2bool(rettv);
4575
4576 // can use "0" and "1" for boolean in some places
4577 clear_tv(rettv);
4578 rettv->v_type = VAR_BOOL;
4579 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4580 }
4581 else
4582 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004583 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004584
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004585 res = check_type(want_type, actual, TRUE, where);
4586 }
4587 }
4588 }
4589 clear_type_list(&type_list);
4590 }
4591
4592 return res;
4593}
4594
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004595 int
4596eval_leader(char_u **arg, int vim9)
4597{
4598 char_u *s = *arg;
4599 char_u *p = *arg;
4600
4601 while (*p == '!' || *p == '-' || *p == '+')
4602 {
4603 char_u *n = skipwhite(p + 1);
4604
4605 // ++, --, -+ and +- are not accepted in Vim9 script
4606 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4607 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004608 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004609 return FAIL;
4610 }
4611 p = n;
4612 }
4613 *arg = p;
4614 return OK;
4615}
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004618 * Check for a predefined value "true", "false" and "null.*".
4619 * Return OK when recognized.
4620 */
4621 int
4622handle_predefined(char_u *s, int len, typval_T *rettv)
4623{
4624 switch (len)
4625 {
4626 case 4: if (STRNCMP(s, "true", 4) == 0)
4627 {
4628 rettv->v_type = VAR_BOOL;
4629 rettv->vval.v_number = VVAL_TRUE;
4630 return OK;
4631 }
4632 if (STRNCMP(s, "null", 4) == 0)
4633 {
4634 rettv->v_type = VAR_SPECIAL;
4635 rettv->vval.v_number = VVAL_NULL;
4636 return OK;
4637 }
4638 break;
4639 case 5: if (STRNCMP(s, "false", 5) == 0)
4640 {
4641 rettv->v_type = VAR_BOOL;
4642 rettv->vval.v_number = VVAL_FALSE;
4643 return OK;
4644 }
4645 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004646 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4647 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004648#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004649 rettv->v_type = VAR_JOB;
4650 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004651#else
4652 rettv->v_type = VAR_SPECIAL;
4653 rettv->vval.v_number = VVAL_NULL;
4654#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004655 return OK;
4656 }
4657 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004658 case 9:
4659 if (STRNCMP(s, "null_", 5) != 0)
4660 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004661 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004662 if (STRNCMP(s + 5, "list", 4) == 0)
4663 {
4664 rettv->v_type = VAR_LIST;
4665 rettv->vval.v_list = NULL;
4666 return OK;
4667 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004668 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004669 if (STRNCMP(s + 5, "dict", 4) == 0)
4670 {
4671 rettv->v_type = VAR_DICT;
4672 rettv->vval.v_dict = NULL;
4673 return OK;
4674 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004675 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004676 if (STRNCMP(s + 5, "blob", 4) == 0)
4677 {
4678 rettv->v_type = VAR_BLOB;
4679 rettv->vval.v_blob = NULL;
4680 return OK;
4681 }
4682 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004683 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4684 {
4685 rettv->v_type = VAR_CLASS;
4686 rettv->vval.v_class = NULL;
4687 return OK;
4688 }
4689 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004690 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4691 {
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = NULL;
4694 return OK;
4695 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004696 if (STRNCMP(s, "null_object", 11) == 0)
4697 {
4698 rettv->v_type = VAR_OBJECT;
4699 rettv->vval.v_object = NULL;
4700 return OK;
4701 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004702 break;
4703 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004704 if (STRNCMP(s, "null_channel", 12) == 0)
4705 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004706#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004707 rettv->v_type = VAR_CHANNEL;
4708 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004709#else
4710 rettv->v_type = VAR_SPECIAL;
4711 rettv->vval.v_number = VVAL_NULL;
4712#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004713 return OK;
4714 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004715 if (STRNCMP(s, "null_partial", 12) == 0)
4716 {
4717 rettv->v_type = VAR_PARTIAL;
4718 rettv->vval.v_partial = NULL;
4719 return OK;
4720 }
4721 break;
4722 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4723 {
4724 rettv->v_type = VAR_FUNC;
4725 rettv->vval.v_string = NULL;
4726 return OK;
4727 }
4728 break;
4729 }
4730 return FAIL;
4731}
4732
4733/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004734 * Handle register contents: @r.
4735 */
4736 static void
4737eval9_reg_contents(
4738 char_u **arg,
4739 typval_T *rettv,
4740 int evaluate)
4741{
4742 int vim9script = in_vim9script();
4743
4744 ++*arg; // skip '@'
4745 if (evaluate)
4746 {
4747 if (vim9script && IS_WHITE_OR_NUL(**arg))
4748 semsg(_(e_syntax_error_at_str), *arg);
4749 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4750 emsg_invreg(**arg);
4751 else
4752 {
4753 rettv->v_type = VAR_STRING;
4754 rettv->vval.v_string = get_reg_contents(**arg,
4755 GREG_EXPR_SRC);
4756 }
4757 }
4758 if (**arg != NUL)
4759 ++*arg;
4760}
4761
4762/*
4763 * Handle a nested expression: (expression) or lambda: (arg) => expr
4764 */
4765 static int
4766eval9_nested_expr(
4767 char_u **arg,
4768 typval_T *rettv,
4769 evalarg_T *evalarg,
4770 int evaluate)
4771{
4772 int ret = NOTDONE;
4773 int vim9script = in_vim9script();
4774
4775 if (vim9script)
4776 {
4777 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4778 if (ret == OK && evaluate)
4779 {
4780 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4781
4782 // Compile it here to get the return type. The return
4783 // type is optional, when it's missing use t_unknown.
4784 // This is recognized in compile_return().
4785 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4786 ufunc->uf_ret_type = &t_unknown;
4787 if (compile_def_function(ufunc, FALSE,
4788 get_compile_type(ufunc), NULL) == FAIL)
4789 {
4790 clear_tv(rettv);
4791 ret = FAIL;
4792 }
4793 }
4794 }
4795 if (ret == NOTDONE)
4796 {
4797 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
4798 ret = eval1(arg, rettv, evalarg); // recursive!
4799
4800 *arg = skipwhite_and_linebreak(*arg, evalarg);
4801 if (**arg == ')')
4802 ++*arg;
4803 else if (ret == OK)
4804 {
4805 emsg(_(e_missing_closing_paren));
4806 clear_tv(rettv);
4807 ret = FAIL;
4808 }
4809 }
4810
4811 return ret;
4812}
4813
4814/*
4815* Handle be a variable or function name.
4816* Can also be a curly-braces kind of name: {expr}.
4817*/
4818 static int
4819eval9_var_func_name(
4820 char_u **arg,
4821 typval_T *rettv,
4822 evalarg_T *evalarg,
4823 int evaluate,
4824 char_u **name_start)
4825{
4826 char_u *s;
4827 int len;
4828 char_u *alias;
4829 int ret = OK;
4830 int vim9script = in_vim9script();
4831
4832 s = *arg;
4833 len = get_name_len(arg, &alias, evaluate, TRUE);
4834 if (alias != NULL)
4835 s = alias;
4836
4837 if (len <= 0)
4838 ret = FAIL;
4839 else
4840 {
4841 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4842
4843 if (evaluate && vim9script && len == 1 && *s == '_')
4844 {
4845 emsg(_(e_cannot_use_underscore_here));
4846 ret = FAIL;
4847 }
4848 else if (evaluate && vim9script && len > 2
4849 && s[0] == 's' && s[1] == ':')
4850 {
4851 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4852 ret = FAIL;
4853 }
4854 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
4855 {
4856 // "name(..." recursive!
4857 *arg = skipwhite(*arg);
4858 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
4859 }
4860 else if (evaluate)
4861 {
4862 // get the value of "true", "false", etc. or a variable
4863 ret = FAIL;
4864 if (vim9script)
4865 ret = handle_predefined(s, len, rettv);
4866 if (ret == FAIL)
4867 {
4868 *name_start = s;
4869 ret = eval_variable(s, len, 0, rettv, NULL,
4870 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
4871 }
4872 }
4873 else
4874 {
4875 // skip the name
4876 check_vars(s, len);
4877 ret = OK;
4878 }
4879 }
4880 vim_free(alias);
4881
4882 return ret;
4883}
4884
4885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 * Handle sixth level expression:
4887 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004888 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004889 * "string" string constant
4890 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * &option-name option value
4892 * @r register contents
4893 * identifier variable value
4894 * function() function call
4895 * $VAR environment variable
4896 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004897 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004899 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004900 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 *
4902 * Also handle:
4903 * ! in front logical NOT
4904 * - in front unary minus
4905 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 * trailing [] subscript in String or List
4907 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004908 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 *
4910 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004911 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 *
4913 * Return OK or FAIL.
4914 */
4915 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004916eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917 char_u **arg,
4918 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004919 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004922 int evaluate = evalarg != NULL
4923 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004924 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 char_u *start_leader, *end_leader;
4926 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004927 static int recurse = 0;
4928 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
4936 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004937 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
4939 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004940 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 end_leader = *arg;
4943
Keith Thompson184f71c2024-01-04 21:19:04 +01004944 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004945 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004946 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004947 ++*arg;
4948 return FAIL;
4949 }
4950
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004951 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004952 // and crash. With MSVC the stack is smaller.
4953 if (recurse ==
4954#ifdef _MSC_VER
4955 300
4956#else
4957 1000
4958#endif
4959 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004960 {
4961 semsg(_(e_expression_too_recursive_str), *arg);
4962 return FAIL;
4963 }
4964 ++recurse;
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 switch (**arg)
4967 {
4968 /*
4969 * Number constant.
4970 */
4971 case '0':
4972 case '1':
4973 case '2':
4974 case '3':
4975 case '4':
4976 case '5':
4977 case '6':
4978 case '7':
4979 case '8':
4980 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004981 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004982
4983 // Apply prefixed "-" and "+" now. Matters especially when
4984 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004985 if (ret == OK && evaluate && end_leader > start_leader
4986 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004987 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989
4990 /*
4991 * String constant: "string".
4992 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004993 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 break;
4995
4996 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004999 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005000 break;
5001
5002 /*
5003 * List: [expr, expr]
5004 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005005 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007
5008 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005009 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005010 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005011 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005012 break;
5013
5014 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005015 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005016 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005018 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005019 ret = NOTDONE;
5020 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005021 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005022 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005023 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 break;
5025
5026 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005027 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005029 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031
5032 /*
5033 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005034 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 */
LemonBoy2eaef102022-05-06 13:14:50 +01005036 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5037 ret = eval_interp_string(arg, rettv, evaluate);
5038 else
5039 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 break;
5041
5042 /*
5043 * Register contents: @r.
5044 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005045 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047
5048 /*
5049 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005050 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005052 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058
5059 if (ret == NOTDONE)
5060 {
5061 /*
5062 * Must be a variable or function name.
5063 * Can also be a curly-braces kind of name: {expr}.
5064 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005065 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 }
5067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005068 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5069 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005070 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005071 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5075 */
5076 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005077 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005078
5079 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005080 return ret;
5081}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005083/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005084 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005085 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005086 * Adjusts "end_leaderp" until it is at "start_leader".
5087 */
5088 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005089eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005090 typval_T *rettv,
5091 int numeric_only,
5092 char_u *start_leader,
5093 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005094{
5095 char_u *end_leader = *end_leaderp;
5096 int ret = OK;
5097 int error = FALSE;
5098 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005099 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005100 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005101 float_T f = 0.0;
5102
5103 if (rettv->v_type == VAR_FLOAT)
5104 f = rettv->vval.v_float;
5105 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005106 {
5107 while (VIM_ISWHITE(end_leader[-1]))
5108 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005109 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005110 val = tv2bool(rettv);
5111 else
5112 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005113 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005114 if (error)
5115 {
5116 clear_tv(rettv);
5117 ret = FAIL;
5118 }
5119 else
5120 {
5121 while (end_leader > start_leader)
5122 {
5123 --end_leader;
5124 if (*end_leader == '!')
5125 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005126 if (numeric_only)
5127 {
5128 ++end_leader;
5129 break;
5130 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005131 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005132 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005133 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005134 {
5135 rettv->v_type = VAR_BOOL;
5136 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5137 }
5138 else
5139 f = !f;
5140 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005141 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005142 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005143 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005144 type = VAR_BOOL;
5145 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005146 }
5147 else if (*end_leader == '-')
5148 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005149 if (rettv->v_type == VAR_FLOAT)
5150 f = -f;
5151 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005152 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005153 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005154 type = VAR_NUMBER;
5155 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005156 }
5157 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005158 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005160 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005161 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005163 else
5164 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005165 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005166 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005167 rettv->v_type = type;
5168 else
5169 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005170 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005173 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 return ret;
5175}
5176
5177/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005178 * Call the function referred to in "rettv".
5179 */
5180 static int
5181call_func_rettv(
5182 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005183 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005184 typval_T *rettv,
5185 int evaluate,
5186 dict_T *selfdict,
5187 typval_T *basetv)
5188{
5189 partial_T *pt = NULL;
5190 funcexe_T funcexe;
5191 typval_T functv;
5192 char_u *s;
5193 int ret;
5194
5195 // need to copy the funcref so that we can clear rettv
5196 if (evaluate)
5197 {
5198 functv = *rettv;
5199 rettv->v_type = VAR_UNKNOWN;
5200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005201 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005202 if (functv.v_type == VAR_PARTIAL)
5203 {
5204 pt = functv.vval.v_partial;
5205 s = partial_name(pt);
5206 }
5207 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005208 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005209 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005210 if (s == NULL || *s == NUL)
5211 {
5212 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005213 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005214 goto theend;
5215 }
5216 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005217 }
5218 else
5219 s = (char_u *)"";
5220
Bram Moolenaara80faa82020-04-12 19:37:17 +02005221 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005222 funcexe.fe_firstline = curwin->w_cursor.lnum;
5223 funcexe.fe_lastline = curwin->w_cursor.lnum;
5224 funcexe.fe_evaluate = evaluate;
5225 funcexe.fe_partial = pt;
5226 funcexe.fe_selfdict = selfdict;
5227 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005228 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005229
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005230theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // Clear the funcref afterwards, so that deleting it while
5232 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005233 if (evaluate)
5234 clear_tv(&functv);
5235
5236 return ret;
5237}
5238
5239/*
5240 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005241 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005242 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5243 */
5244 static int
5245eval_lambda(
5246 char_u **arg,
5247 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005248 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005249 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005250{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005251 int evaluate = evalarg != NULL
5252 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005253 typval_T base = *rettv;
5254 int ret;
5255
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005256 rettv->v_type = VAR_UNKNOWN;
5257
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005258 if (**arg == '{')
5259 {
5260 // ->{lambda}()
5261 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5262 }
5263 else
5264 {
5265 // ->(lambda)()
5266 ++*arg;
5267 ret = eval1(arg, rettv, evalarg);
5268 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005269 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005270 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005271 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005272 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005273 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005274 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5275 && rettv->v_type != VAR_PARTIAL)
5276 {
5277 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5278 return FAIL;
5279 }
5280 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005281 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005282 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005283 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005284
5285 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005286 {
5287 if (verbose)
5288 {
5289 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005290 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005291 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005292 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005293 }
5294 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005295 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005296 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005297 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005298 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005299
5300 // Clear the funcref afterwards, so that deleting it while
5301 // evaluating the arguments is possible (see test55).
5302 if (evaluate)
5303 clear_tv(&base);
5304
5305 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005306}
5307
5308/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005309 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005310 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005311 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5312 */
5313 static int
5314eval_method(
5315 char_u **arg,
5316 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005317 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005319{
5320 char_u *name;
5321 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005322 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005323 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005324 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005325 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005326 int evaluate = evalarg != NULL
5327 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005328
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005329 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005330
Bram Moolenaarac92e252019-08-03 21:58:38 +02005331 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005332 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005333 if (alias != NULL)
5334 name = alias;
5335
5336 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005337 {
5338 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005339 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005340 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005341 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005342 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005343 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005344 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005345
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005346 // If there is no "(" immediately following, but there is further on,
5347 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5348 // Does not handle anything where "(" is part of the expression.
5349 *arg = skipwhite(*arg);
5350
5351 if (**arg != '(' && alias == NULL
5352 && (paren = vim_strchr(*arg, '(')) != NULL)
5353 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005354 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005355
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005356 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005357 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005358 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005359 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5360 if (evalarg != NULL)
5361 {
5362 getline = evalarg->eval_getline;
5363 evalarg->eval_getline = NULL;
5364 }
5365
5366 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005367 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005368 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005369 *arg = name + len;
5370 ret = FAIL;
5371 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005372 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005373 {
5374 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005375 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005376 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005377
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005378 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005379 if (getline != NULL)
5380 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005381 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005382
5383 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005384 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005385 *arg = skipwhite(*arg);
5386
5387 if (**arg != '(')
5388 {
5389 if (verbose)
5390 semsg(_(e_missing_parenthesis_str), name);
5391 ret = FAIL;
5392 }
5393 else if (VIM_ISWHITE((*arg)[-1]))
5394 {
5395 if (verbose)
5396 emsg(_(e_no_white_space_allowed_before_parenthesis));
5397 ret = FAIL;
5398 }
5399 else
5400 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005401 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005402 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005403 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005404
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005405 // Clear the funcref afterwards, so that deleting it while
5406 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005407 if (evaluate)
5408 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005409 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005410
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005411 if (alias != NULL)
5412 vim_free(alias);
5413
Bram Moolenaarac92e252019-08-03 21:58:38 +02005414 return ret;
5415}
5416
5417/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005418 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5419 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5421 */
5422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423eval_index(
5424 char_u **arg,
5425 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005426 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005429 int evaluate = evalarg != NULL
5430 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005435 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005436 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005438 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5439 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005441 init_tv(&var1);
5442 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 /*
5446 * dict.name
5447 */
5448 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005449 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005451 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005453 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 /*
5458 * something[idx]
5459 *
5460 * Get the (first) variable from inside the [].
5461 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005462 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 if (**arg == ':')
5464 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005465 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005467 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005468 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005469 semsg(_(e_white_space_required_before_and_after_str_at_str),
5470 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005471 clear_tv(&var1);
5472 return FAIL;
5473 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005474 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005475 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005476 int error = FALSE;
5477
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005478 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005479 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005480 && var1.v_type == VAR_FLOAT)
5481 {
5482 var1.vval.v_string = typval_tostring(&var1, TRUE);
5483 var1.v_type = VAR_STRING;
5484 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005485
Bram Moolenaar4525a572022-02-13 11:57:33 +00005486 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005487 tv_get_number_chk(&var1, &error);
5488 else
5489 error = tv_get_string_chk(&var1) == NULL;
5490 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005491 {
5492 // not a number or string
5493 clear_tv(&var1);
5494 return FAIL;
5495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497
5498 /*
5499 * Get the second variable from inside the [:].
5500 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005501 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 if (**arg == ':')
5503 {
5504 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005505 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005506 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005507 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005508 semsg(_(e_white_space_required_before_and_after_str_at_str),
5509 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005510 if (!empty1)
5511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 if (**arg == ']')
5516 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005517 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005519 if (!empty1)
5520 clear_tv(&var1);
5521 return FAIL;
5522 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005523 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005526 if (!empty1)
5527 clear_tv(&var1);
5528 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 return FAIL;
5530 }
5531 }
5532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005534 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 if (**arg != ']')
5536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005538 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 clear_tv(&var1);
5540 if (range)
5541 clear_tv(&var2);
5542 return FAIL;
5543 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005544 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 }
5546
5547 if (evaluate)
5548 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005549 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005550 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005551 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005552
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005553 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005556 clear_tv(&var2);
5557 return res;
5558 }
5559 return OK;
5560}
5561
5562/*
5563 * Check if "rettv" can have an [index] or [sli:ce]
5564 */
5565 int
5566check_can_index(typval_T *rettv, int evaluate, int verbose)
5567{
5568 switch (rettv->v_type)
5569 {
5570 case VAR_FUNC:
5571 case VAR_PARTIAL:
5572 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005573 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005574 return FAIL;
5575 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005576 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005577 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005578 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005579 case VAR_BOOL:
5580 case VAR_SPECIAL:
5581 case VAR_JOB:
5582 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005583 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005584 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005585 if (verbose)
5586 emsg(_(e_cannot_index_special_variable));
5587 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005588 case VAR_CLASS:
5589 case VAR_TYPEALIAS:
5590 if (verbose)
5591 check_typval_is_value(rettv);
5592 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005593 case VAR_UNKNOWN:
5594 case VAR_ANY:
5595 case VAR_VOID:
5596 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005598 emsg(_(e_cannot_index_special_variable));
5599 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005601 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005603 case VAR_STRING:
5604 case VAR_LIST:
5605 case VAR_DICT:
5606 case VAR_BLOB:
5607 break;
5608 case VAR_NUMBER:
5609 if (in_vim9script())
5610 emsg(_(e_cannot_index_number));
5611 break;
5612 }
5613 return OK;
5614}
5615
5616/*
5617 * Apply index or range to "rettv".
5618 * "var1" is the first index, NULL for [:expr].
5619 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005620 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5621 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005622 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5623 */
5624 int
5625eval_index_inner(
5626 typval_T *rettv,
5627 int is_range,
5628 typval_T *var1,
5629 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005630 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005631 char_u *key,
5632 int keylen,
5633 int verbose)
5634{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005635 varnumber_T n1, n2 = 0;
5636 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005637
5638 n1 = 0;
5639 if (var1 != NULL && rettv->v_type != VAR_DICT)
5640 n1 = tv_get_number(var1);
5641
5642 if (is_range)
5643 {
5644 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005646 if (verbose)
5647 emsg(_(e_cannot_slice_dictionary));
5648 return FAIL;
5649 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005650 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005651 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005652 else
5653 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005654 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005655
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005656 switch (rettv->v_type)
5657 {
5658 case VAR_UNKNOWN:
5659 case VAR_ANY:
5660 case VAR_VOID:
5661 case VAR_FUNC:
5662 case VAR_PARTIAL:
5663 case VAR_FLOAT:
5664 case VAR_BOOL:
5665 case VAR_SPECIAL:
5666 case VAR_JOB:
5667 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005668 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005669 case VAR_CLASS:
5670 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005671 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005672 break; // not evaluating, skipping over subscript
5673
5674 case VAR_NUMBER:
5675 case VAR_STRING:
5676 {
5677 char_u *s = tv_get_string(rettv);
5678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005680 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005681 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005682 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005683 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005684 else
5685 s = char_from_string(s, n1);
5686 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005687 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005689 // The resulting variable is a substring. If the indexes
5690 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 if (n1 < 0)
5692 {
5693 n1 = len + n1;
5694 if (n1 < 0)
5695 n1 = 0;
5696 }
5697 if (n2 < 0)
5698 n2 = len + n2;
5699 else if (n2 >= len)
5700 n2 = len;
5701 if (n1 >= len || n2 < 0 || n1 > n2)
5702 s = NULL;
5703 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005704 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 }
5706 else
5707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005708 // The resulting variable is a string of a single
5709 // character. If the index is too big or negative the
5710 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005711 if (n1 >= len || n1 < 0)
5712 s = NULL;
5713 else
5714 s = vim_strnsave(s + n1, 1);
5715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005716 clear_tv(rettv);
5717 rettv->v_type = VAR_STRING;
5718 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005719 }
5720 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005722 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005723 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5724 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005725 break;
5726
5727 case VAR_LIST:
5728 if (var1 == NULL)
5729 n1 = 0;
5730 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005731 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005732 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005733 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005734 return FAIL;
5735 break;
5736
5737 case VAR_DICT:
5738 {
5739 dictitem_T *item;
5740 typval_T tmp;
5741
5742 if (key == NULL)
5743 {
5744 key = tv_get_string_chk(var1);
5745 if (key == NULL)
5746 return FAIL;
5747 }
5748
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005749 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005750
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005751 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005752 {
5753 if (verbose)
5754 {
5755 if (keylen > 0)
5756 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005757 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005758 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005759 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005760 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005761
5762 copy_tv(&item->di_tv, &tmp);
5763 clear_tv(rettv);
5764 *rettv = tmp;
5765 }
5766 break;
5767 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 return OK;
5769}
5770
5771/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005772 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005773 */
5774 char_u *
5775partial_name(partial_T *pt)
5776{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005777 if (pt != NULL)
5778 {
5779 if (pt->pt_name != NULL)
5780 return pt->pt_name;
5781 if (pt->pt_func != NULL)
5782 return pt->pt_func->uf_name;
5783 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005784 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005785}
5786
Bram Moolenaarddecc252016-04-06 22:59:37 +02005787 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005788partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005789{
5790 int i;
5791
5792 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005793 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005794 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005795 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005796 if (pt->pt_name != NULL)
5797 {
5798 func_unref(pt->pt_name);
5799 vim_free(pt->pt_name);
5800 }
5801 else
5802 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005803 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005804
Bram Moolenaar54656012021-06-09 20:50:46 +02005805 // "out_up" is no longer used, decrement refcount on partial that owns it.
5806 partial_unref(pt->pt_outer.out_up_partial);
5807
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005808 // Using pt_outer from another partial.
5809 partial_unref(pt->pt_outer_partial);
5810
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005811 // Decrease the reference count for the context of a closure. If down
5812 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005813 if (pt->pt_funcstack != NULL)
5814 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005815 --pt->pt_funcstack->fs_refcount;
5816 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005817 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005818 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005819 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5820 if (pt->pt_loopvars[i] != NULL)
5821 {
5822 --pt->pt_loopvars[i]->lvs_refcount;
5823 loopvars_check_refcount(pt->pt_loopvars[i]);
5824 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005825
Bram Moolenaarddecc252016-04-06 22:59:37 +02005826 vim_free(pt);
5827}
5828
5829/*
5830 * Unreference a closure: decrement the reference count and free it when it
5831 * becomes zero.
5832 */
5833 void
5834partial_unref(partial_T *pt)
5835{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005836 if (pt == NULL)
5837 return;
5838
5839 int done = FALSE;
5840
5841 if (--pt->pt_refcount <= 0)
5842 partial_free(pt);
5843
5844 // If the reference count goes down to one, the funcstack may be the
5845 // only reference and can be freed if no other partials reference it.
5846 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005847 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005848 // careful: if the funcstack is freed it may contain this partial
5849 // and it gets freed as well
5850 if (pt->pt_funcstack != NULL)
5851 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005852
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005853 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005854 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005855 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005856
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005857 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5858 if (pt->pt_loopvars[depth] != NULL
5859 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005860 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005861 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005862 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005863}
5864
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005866 * Return a textual representation of a string in "tv".
5867 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5868 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5869 * strings as "string()", otherwise does not put quotes around strings.
5870 * May return NULL.
5871 */
5872 static char_u *
5873string_tv2string(
5874 typval_T *tv,
5875 char_u **tofree,
5876 int echo_style,
5877 int composite_val)
5878{
5879 char_u *r = NULL;
5880
5881 if (echo_style && !composite_val)
5882 {
5883 *tofree = NULL;
5884 r = tv->vval.v_string;
5885 if (r == NULL)
5886 r = (char_u *)"";
5887 }
5888 else
5889 {
5890 *tofree = string_quote(tv->vval.v_string, FALSE);
5891 r = *tofree;
5892 }
5893
5894 return r;
5895}
5896
5897/*
5898 * Return a textual representation of a function in "tv".
5899 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5900 * When "echo_style" is FALSE, put quotes around the function name as
5901 * "function()", otherwise does not put quotes around function name.
5902 * May return NULL.
5903 */
5904 static char_u *
5905func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5906{
5907 char_u *r = NULL;
5908 char_u buf[MAX_FUNC_NAME_LEN];
5909
5910 if (echo_style)
5911 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005912 *tofree = NULL;
5913
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005914 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005915 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005916 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005917 {
5918 r = make_ufunc_name_readable(tv->vval.v_string, buf,
5919 MAX_FUNC_NAME_LEN);
5920 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005921 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005922 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005923 }
5924 else
5925 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005926 char_u *s = NULL;
5927
5928 if (tv->vval.v_string != NULL)
5929 s = make_ufunc_name_readable(tv->vval.v_string, buf,
5930 MAX_FUNC_NAME_LEN);
5931
5932 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005933 }
5934
5935 return r;
5936}
5937
5938/*
Ernie Rael9d779c52024-07-07 20:41:44 +02005939 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
5940 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5941 * When "echo_style" is FALSE, put quotes around the function name as
5942 * "function()", otherwise does not put quotes around function name.
5943 * May return NULL.
5944 */
5945 static char_u *
5946method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5947{
5948 char_u buf[MAX_FUNC_NAME_LEN];
5949 partial_T *pt = tv->vval.v_partial;
5950
5951 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
5952 pt->pt_func->uf_script_ctx.sc_sid,
5953 pt->pt_func->uf_class->class_name,
5954 pt->pt_func->uf_name);
5955 if (len >= sizeof(buf))
5956 {
5957 if (echo_style)
5958 {
5959 *tofree = NULL;
5960 return (char_u *)"function()";
5961 }
5962 else
5963 return *tofree = string_quote((char_u*)"", TRUE);
5964 }
5965
5966 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
5967}
5968
5969/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005970 * Return a textual representation of a partial in "tv".
5971 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5972 * "numbuf" is used for a number. May return NULL.
5973 */
5974 static char_u *
5975partial_tv2string(
5976 typval_T *tv,
5977 char_u **tofree,
5978 char_u *numbuf,
5979 int copyID)
5980{
5981 char_u *r = NULL;
5982 partial_T *pt;
5983 char_u *fname;
5984 garray_T ga;
5985 int i;
5986 char_u *tf;
5987
5988 pt = tv->vval.v_partial;
5989 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5990
5991 ga_init2(&ga, 1, 100);
5992 ga_concat(&ga, (char_u *)"function(");
5993 if (fname != NULL)
5994 {
5995 // When using uf_name prepend "g:" for a global function.
5996 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5997 && vim_isupper(fname[1]))
5998 {
5999 ga_concat(&ga, (char_u *)"'g:");
6000 ga_concat(&ga, fname + 1);
6001 }
6002 else
6003 ga_concat(&ga, fname);
6004 vim_free(fname);
6005 }
6006 if (pt != NULL && pt->pt_argc > 0)
6007 {
6008 ga_concat(&ga, (char_u *)", [");
6009 for (i = 0; i < pt->pt_argc; ++i)
6010 {
6011 if (i > 0)
6012 ga_concat(&ga, (char_u *)", ");
6013 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6014 vim_free(tf);
6015 }
6016 ga_concat(&ga, (char_u *)"]");
6017 }
6018 if (pt != NULL && pt->pt_dict != NULL)
6019 {
6020 typval_T dtv;
6021
6022 ga_concat(&ga, (char_u *)", ");
6023 dtv.v_type = VAR_DICT;
6024 dtv.vval.v_dict = pt->pt_dict;
6025 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6026 vim_free(tf);
6027 }
6028 // terminate with ')' and a NUL
6029 ga_concat_len(&ga, (char_u *)")", 2);
6030
6031 *tofree = ga.ga_data;
6032 r = *tofree;
6033
6034 return r;
6035}
6036
6037/*
6038 * Return a textual representation of a List in "tv".
6039 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6040 * When "copyID" is not zero replace recursive lists with "...". When
6041 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6042 * May return NULL.
6043 */
6044 static char_u *
6045list_tv2string(
6046 typval_T *tv,
6047 char_u **tofree,
6048 int copyID,
6049 int restore_copyID)
6050{
6051 char_u *r = NULL;
6052
6053 if (tv->vval.v_list == NULL)
6054 {
6055 // NULL list is equivalent to empty list.
6056 *tofree = NULL;
6057 r = (char_u *)"[]";
6058 }
6059 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6060 && tv->vval.v_list->lv_len > 0)
6061 {
6062 *tofree = NULL;
6063 r = (char_u *)"[...]";
6064 }
6065 else
6066 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006067 int old_copyID;
6068 if (restore_copyID)
6069 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006070
6071 tv->vval.v_list->lv_copyID = copyID;
6072 *tofree = list2string(tv, copyID, restore_copyID);
6073 if (restore_copyID)
6074 tv->vval.v_list->lv_copyID = old_copyID;
6075 r = *tofree;
6076 }
6077
6078 return r;
6079}
6080
6081/*
6082 * Return a textual representation of a Dict in "tv".
6083 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6084 * When "copyID" is not zero replace recursive dicts with "...".
6085 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6086 * replaced with "...". May return NULL.
6087 */
6088 static char_u *
6089dict_tv2string(
6090 typval_T *tv,
6091 char_u **tofree,
6092 int copyID,
6093 int restore_copyID)
6094{
6095 char_u *r = NULL;
6096
6097 if (tv->vval.v_dict == NULL)
6098 {
6099 // NULL dict is equivalent to empty dict.
6100 *tofree = NULL;
6101 r = (char_u *)"{}";
6102 }
6103 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6104 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6105 {
6106 *tofree = NULL;
6107 r = (char_u *)"{...}";
6108 }
6109 else
6110 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006111 int old_copyID;
6112 if (restore_copyID)
6113 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006114
6115 tv->vval.v_dict->dv_copyID = copyID;
6116 *tofree = dict2string(tv, copyID, restore_copyID);
6117 if (restore_copyID)
6118 tv->vval.v_dict->dv_copyID = old_copyID;
6119 r = *tofree;
6120 }
6121
6122 return r;
6123}
6124
6125/*
6126 * Return a textual representation of a job or a channel in "tv".
6127 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6128 * "numbuf" is used for a number.
6129 * When "composite_val" is FALSE, put quotes around strings as "string()",
6130 * otherwise does not put quotes around strings.
6131 * May return NULL.
6132 */
6133 static char_u *
6134jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006135 typval_T *tv UNUSED,
6136 char_u **tofree UNUSED,
6137 char_u *numbuf UNUSED,
6138 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006139{
6140 char_u *r = NULL;
6141
6142#ifdef FEAT_JOB_CHANNEL
6143 *tofree = NULL;
6144
6145 if (tv->v_type == VAR_JOB)
6146 r = job_to_string_buf(tv, numbuf);
6147 else
6148 r = channel_to_string_buf(tv, numbuf);
6149
6150 if (composite_val)
6151 {
6152 *tofree = string_quote(r, FALSE);
6153 r = *tofree;
6154 }
6155#endif
6156
6157 return r;
6158}
6159
6160/*
6161 * Return a textual representation of a class in "tv".
6162 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6163 * May return NULL.
6164 */
6165 static char_u *
6166class_tv2string(typval_T *tv, char_u **tofree)
6167{
6168 char_u *r = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01006169 size_t rsize;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006170 class_T *cl = tv->vval.v_class;
John Marriottbd4614f2024-11-18 20:25:21 +01006171 char_u *class_name = (char_u *)"[unknown]";
6172 size_t class_namelen = 9;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006173 char *s = "class";
John Marriottbd4614f2024-11-18 20:25:21 +01006174 size_t slen = 5;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006175
John Marriottbd4614f2024-11-18 20:25:21 +01006176 if (cl != NULL)
6177 {
6178 class_name = cl->class_name;
6179 class_namelen = STRLEN(cl->class_name);
6180 if (IS_INTERFACE(cl))
6181 {
6182 s = "interface";
6183 slen = 9;
6184 }
6185 else if (IS_ENUM(cl))
6186 {
6187 s = "enum";
6188 slen = 4;
6189 }
6190 }
6191
6192 rsize = slen + 1 + class_namelen + 1;
6193 r = *tofree = alloc(rsize);
6194 if (r != NULL)
6195 vim_snprintf((char *)r, rsize, "%s %s", s, (char *)class_name);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006196
6197 return r;
6198}
6199
6200/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006201 * Return a textual representation of an Object in "tv".
6202 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6203 * When "copyID" is not zero replace recursive object with "...".
6204 * When "restore_copyID" is FALSE, repeated items in the object are
6205 * replaced with "...". May return NULL.
6206 */
6207 static char_u *
6208object_tv2string(
6209 typval_T *tv,
6210 char_u **tofree,
6211 int copyID,
6212 int restore_copyID,
6213 char_u *numbuf,
6214 int echo_style,
6215 int composite_val)
6216{
6217 char_u *r = NULL;
6218
6219 object_T *obj = tv->vval.v_object;
6220 if (obj == NULL || obj->obj_class == NULL)
6221 {
6222 *tofree = NULL;
6223 r = (char_u *)"object of [unknown]";
6224 }
6225 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006226 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006227 {
John Marriottbd4614f2024-11-18 20:25:21 +01006228 size_t n = 25 + STRLEN((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006229 r = alloc(n);
6230 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006231 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006232 obj->obj_class->class_name);
6233 *tofree = r;
6234 }
6235 else
6236 {
6237 int old_copyID;
6238 if (restore_copyID)
6239 old_copyID = obj->obj_copyID;
6240
6241 obj->obj_copyID = copyID;
6242 *tofree = object2string(obj, numbuf, copyID, echo_style,
6243 restore_copyID, composite_val);
6244 if (restore_copyID)
6245 obj->obj_copyID = old_copyID;
6246 r = *tofree;
6247 }
6248
6249 return r;
6250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Return a string with the string representation of a variable.
6254 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006256 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006257 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006258 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006259 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006260 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6261 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006262 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006264 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006265echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266 typval_T *tv,
6267 char_u **tofree,
6268 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006269 int copyID,
6270 int echo_style,
6271 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006272 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 static int recurse = 0;
6275 char_u *r = NULL;
6276
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006278 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006279 if (!did_echo_string_emsg)
6280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006281 // Only give this message once for a recursive call to avoid
6282 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006283 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006284 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006285 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006288 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289 }
6290 ++recurse;
6291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 switch (tv->v_type)
6293 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006294 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006295 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006296 break;
6297
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006299 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006301
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006302 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006303 if (tv->vval.v_partial == NULL
6304 || tv->vval.v_partial->pt_obj == NULL)
6305 r = partial_tv2string(tv, tofree, numbuf, copyID);
6306 else
6307 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006308 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006310 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006311 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006312 break;
6313
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006315 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006317
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006319 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006320 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006322 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006323 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006324 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006326 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006327 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006328 break;
6329
Bram Moolenaar835dc632016-02-07 14:27:38 +01006330 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006331 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006332 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006334
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006335 case VAR_INSTR:
6336 *tofree = NULL;
6337 r = (char_u *)"instructions";
6338 break;
6339
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006340 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006341 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006342 break;
6343
6344 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006345 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6346 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006347 break;
6348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006349 case VAR_FLOAT:
6350 *tofree = NULL;
6351 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6352 r = numbuf;
6353 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006354
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006355 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006356 case VAR_SPECIAL:
6357 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006358 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006359 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006360
6361 case VAR_TYPEALIAS:
6362 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6363 r = *tofree;
6364 if (r == NULL)
6365 r = (char_u *)"";
6366 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368
Bram Moolenaar8502c702014-06-17 12:51:16 +02006369 if (--recurse == 0)
6370 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006372}
6373
6374/*
6375 * Return a string with the string representation of a variable.
6376 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6377 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006378 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006379 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006380 * May return NULL.
6381 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006382 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006383echo_string(
6384 typval_T *tv,
6385 char_u **tofree,
6386 char_u *numbuf,
6387 int copyID)
6388{
6389 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6390}
6391
6392/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006393 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6394 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006395 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006396 */
6397 int
6398buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6399{
6400 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006401 char_u *t;
6402 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006403
6404 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6405 return -1;
6406
6407 if (lnum > buf->b_ml.ml_line_count)
6408 lnum = buf->b_ml.ml_line_count;
6409
6410 str = ml_get_buf(buf, lnum, FALSE);
6411 if (str == NULL)
6412 return -1;
6413
6414 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006415 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006416
Bram Moolenaar91458462021-01-13 20:08:38 +01006417 // count the number of characters
6418 t = str;
6419 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6420 t += mb_ptr2len(t);
6421
6422 // In insert mode, when the cursor is at the end of a non-empty line,
6423 // byteidx points to the NUL character immediately past the end of the
6424 // string. In this case, add one to the character count.
6425 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6426 count++;
6427
6428 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006429}
6430
6431/*
6432 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006433 * byte index. Works only for loaded buffers. Returns -1 on failure.
6434 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006435 */
6436 int
6437buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6438{
6439 char_u *str;
6440 char_u *t;
6441
6442 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6443 return -1;
6444
6445 if (lnum > buf->b_ml.ml_line_count)
6446 lnum = buf->b_ml.ml_line_count;
6447
6448 str = ml_get_buf(buf, lnum, FALSE);
6449 if (str == NULL)
6450 return -1;
6451
6452 // Convert the character offset to a byte offset
6453 t = str;
6454 while (*t != NUL && --charidx > 0)
6455 t += mb_ptr2len(t);
6456
Bram Moolenaar91458462021-01-13 20:08:38 +01006457 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006458}
6459
6460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006462 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006465var2fpos(
6466 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006468 int *fnum, // set to fnum for '0, 'A, etc.
6469 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006471 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006473 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006476 if (varp->v_type == VAR_LIST)
6477 {
6478 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006479 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006480 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006481 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006482
6483 l = varp->vval.v_list;
6484 if (l == NULL)
6485 return NULL;
6486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006487 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006488 pos.lnum = list_find_nr(l, 0L, &error);
6489 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006490 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006491 if (charcol)
6492 len = (long)mb_charlen(ml_get(pos.lnum));
6493 else
John Marriottbfcc8952024-03-11 22:04:45 +01006494 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006495
Bram Moolenaarec65d772020-08-20 22:29:12 +02006496 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006498 li = list_find(l, 1L);
6499 if (li != NULL && li->li_tv.v_type == VAR_STRING
6500 && li->li_tv.vval.v_string != NULL
6501 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006502 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006503 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006504 }
6505 else
6506 {
6507 pos.col = list_find_nr(l, 1L, &error);
6508 if (error)
6509 return NULL;
6510 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006513 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006514 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006515 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006517 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006518 pos.coladd = list_find_nr(l, 2L, &error);
6519 if (error)
6520 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006521
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006522 return &pos;
6523 }
6524
Bram Moolenaarc5809432021-03-27 21:23:30 +01006525 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6526 return NULL;
6527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006528 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006529 if (name == NULL)
6530 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006531
6532 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006533 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006534 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006535 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006536 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006537 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006538 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006539 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006540 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006541 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006542 pos = VIsual;
6543 else
6544 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006545 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006546 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006547 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006549 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006550 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6552 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006553 pos = *pp;
6554 }
6555 if (pos.lnum != 0)
6556 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006557 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006558 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6559 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006561
Bram Moolenaara5525202006-03-02 22:52:09 +00006562 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006563
Bram Moolenaar477933c2007-07-17 14:32:23 +00006564 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006565 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006566 // the "w_valid" flags are not reset when moving the cursor, but they
6567 // do matter for update_topline() and validate_botline().
6568 check_cursor_moved(curwin);
6569
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006570 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006574 // In silent Ex mode topline is zero, but that's not a valid line
6575 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006576 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006577 return &pos;
6578 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006580 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006582 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006583 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006584 return &pos;
6585 }
6586 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006587 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006589 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
6591 pos.lnum = curbuf->b_ml.ml_line_count;
6592 pos.col = 0;
6593 }
6594 else
6595 {
6596 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006597 if (charcol)
6598 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6599 else
John Marriottbfcc8952024-03-11 22:04:45 +01006600 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 return &pos;
6603 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006604 if (in_vim9script())
6605 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 return NULL;
6607}
6608
6609/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006610 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006611 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006612 * Note that the column is passed on as-is, the caller may want to decrement
6613 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006614 * If "charcol" is TRUE use the column as the character index instead of the
6615 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006616 * Return FAIL when conversion is not possible, doesn't check the position for
6617 * validity.
6618 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620list2fpos(
6621 typval_T *arg,
6622 pos_T *posp,
6623 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006624 colnr_T *curswantp,
6625 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006626{
6627 list_T *l = arg->vval.v_list;
6628 long i = 0;
6629 long n;
6630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006631 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6632 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006633 if (arg->v_type != VAR_LIST
6634 || l == NULL
6635 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006636 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006637 return FAIL;
6638
6639 if (fnump != NULL)
6640 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006641 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006642 if (n < 0)
6643 return FAIL;
6644 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006646 *fnump = n;
6647 }
6648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006650 if (n < 0)
6651 return FAIL;
6652 posp->lnum = n;
6653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006655 if (n < 0)
6656 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006657 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006658 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006659 if (charcol)
6660 {
6661 buf_T *buf;
6662
6663 // Get the text for the specified line in a loaded buffer
6664 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6665 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6666 return FAIL;
6667
Bram Moolenaar79f23442022-10-10 12:42:57 +01006668 n = buf_charidx_to_byteidx(buf,
6669 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006670 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006671 posp->col = n;
6672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006673 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006674 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006675 posp->coladd = 0;
6676 else
6677 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006678
Bram Moolenaar493c1782014-05-28 14:34:46 +02006679 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006680 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006681
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006682 return OK;
6683}
6684
6685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 * Get the length of an environment variable name.
6687 * Advance "arg" to the first character after the name.
6688 * Return 0 for error.
6689 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 char_u *p;
6694 int len;
6695
6696 for (p = *arg; vim_isIDc(*p); ++p)
6697 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006698 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 return 0;
6700
6701 len = (int)(p - *arg);
6702 *arg = p;
6703 return len;
6704}
6705
6706/*
6707 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006708 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 * Return 0 if something is wrong.
6710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 char_u *p;
6715 int len;
6716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006717 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006719 {
6720 if (*p == ':')
6721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006722 // "s:" is start of "s:var", but "n:" is not and can be used in
6723 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006724 len = (int)(p - *arg);
6725 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6726 || len > 1)
6727 break;
6728 }
6729 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006730 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 return 0;
6732
6733 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006734 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 return len;
6737}
6738
6739/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006740 * Get the length of the name of a variable or function.
6741 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006743 * Return -1 if curly braces expansion failed.
6744 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * If the name contains 'magic' {}'s, expand them and return the
6746 * expanded name in an allocated string via 'alias' - caller must free.
6747 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749get_name_len(
6750 char_u **arg,
6751 char_u **alias,
6752 int evaluate,
6753 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 char_u *p;
6757 char_u *expr_start;
6758 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006760 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
6762 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6763 && (*arg)[2] == (int)KE_SNR)
6764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006765 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 *arg += 3;
6767 return get_id_len(arg) + 3;
6768 }
6769 len = eval_fname_script(*arg);
6770 if (len > 0)
6771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006772 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 *arg += len;
6774 }
6775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006779 p = find_name_end(*arg, &expr_start, &expr_end,
6780 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (expr_start != NULL)
6782 {
6783 char_u *temp_string;
6784
6785 if (!evaluate)
6786 {
6787 len += (int)(p - *arg);
6788 *arg = skipwhite(p);
6789 return len;
6790 }
6791
6792 /*
6793 * Include any <SID> etc in the expanded string:
6794 * Thus the -len here.
6795 */
6796 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6797 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006798 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 *alias = temp_string;
6800 *arg = skipwhite(p);
6801 return (int)STRLEN(temp_string);
6802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006805 // Only give an error when there is something, otherwise it will be
6806 // reported at a higher level.
6807 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006808 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 return len;
6811}
6812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813/*
6814 * Find the end of a variable or function name, taking care of magic braces.
6815 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6816 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006817 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 * Return a pointer to just after the name. Equal to "arg" if there is no
6819 * valid name.
6820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006821 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822find_name_end(
6823 char_u *arg,
6824 char_u **expr_start,
6825 char_u **expr_end,
6826 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 int mb_nest = 0;
6829 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006831 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006832 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006836 *expr_start = NULL;
6837 *expr_end = NULL;
6838 }
6839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006841 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006842 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006843 return arg;
6844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845 for (p = arg; *p != NUL
6846 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006847 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006848 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006849 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006850 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006851 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006853 if (*p == '\'')
6854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006855 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006856 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006857 ;
6858 if (*p == NUL)
6859 break;
6860 }
6861 else if (*p == '"')
6862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006863 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006864 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006865 if (*p == '\\' && p[1] != NUL)
6866 ++p;
6867 if (*p == NUL)
6868 break;
6869 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006870 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006872 // "s:" is start of "s:var", but "n:" is not and can be used in
6873 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006874 len = (int)(p - arg);
6875 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006876 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006877 break;
6878 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006880 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882 if (*p == '[')
6883 ++br_nest;
6884 else if (*p == ']')
6885 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006887
zeertzjqa93d9cd2023-05-02 16:25:47 +01006888 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 if (*p == '{')
6891 {
6892 mb_nest++;
6893 if (expr_start != NULL && *expr_start == NULL)
6894 *expr_start = p;
6895 }
6896 else if (*p == '}')
6897 {
6898 mb_nest--;
6899 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6900 *expr_end = p;
6901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
6905 return p;
6906}
6907
6908/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006909 * Expands out the 'magic' {}'s in a variable/function name.
6910 * Note that this can call itself recursively, to deal with
6911 * constructs like foo{bar}{baz}{bam}
6912 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6913 * "in_start" ^
6914 * "expr_start" ^
6915 * "expr_end" ^
6916 * "in_end" ^
6917 *
6918 * Returns a new allocated string, which the caller must free.
6919 * Returns NULL for failure.
6920 */
6921 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006922make_expanded_name(
6923 char_u *in_start,
6924 char_u *expr_start,
6925 char_u *expr_end,
6926 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006927{
6928 char_u c1;
6929 char_u *retval = NULL;
6930 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006931
6932 if (expr_end == NULL || in_end == NULL)
6933 return NULL;
6934 *expr_start = NUL;
6935 *expr_end = NUL;
6936 c1 = *in_end;
6937 *in_end = NUL;
6938
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006939 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006940 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941 {
John Marriottbd4614f2024-11-18 20:25:21 +01006942 size_t retvalsize = (size_t)(expr_start - in_start)
6943 + STRLEN(temp_result)
6944 + (size_t)(in_end - expr_end) + 1;
6945
6946 retval = alloc(retvalsize);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006947 if (retval != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01006948 vim_snprintf((char *)retval, retvalsize, "%s%s%s",
6949 in_start, temp_result, expr_end + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006950 }
6951 vim_free(temp_result);
6952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006953 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006954 *expr_start = '{';
6955 *expr_end = '}';
6956
6957 if (retval != NULL)
6958 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006959 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006960 if (expr_start != NULL)
6961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006962 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006963 temp_result = make_expanded_name(retval, expr_start,
6964 expr_end, temp_result);
6965 vim_free(retval);
6966 retval = temp_result;
6967 }
6968 }
6969
6970 return retval;
6971}
6972
6973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006978eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006980 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006981}
6982
6983/*
6984 * Return TRUE if character "c" can be used as the first character in a
6985 * variable or function name (excluding '{' and '}').
6986 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006988eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006989{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006990 return ASCII_ISALPHA(c) || c == '_';
6991}
6992
6993/*
6994 * Return TRUE if character "c" can be used as the first character of a
6995 * dictionary key.
6996 */
6997 int
6998eval_isdictc(int c)
6999{
7000 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001}
7002
7003/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007004 * Handle:
7005 * - expr[expr], expr[expr:expr] subscript
7006 * - ".name" lookup
7007 * - function call with Funcref variable: func(expr)
7008 * - method call: var->method()
7009 *
7010 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007011 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007012 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014handle_subscript(
7015 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007016 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007018 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007019 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007021 int evaluate = evalarg != NULL
7022 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007023 int ret = OK;
7024 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007025 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007026 int getnext;
7027 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007028
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007029 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007030 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007031 // When at the end of the line and ".name" or "->{" or "->X" follows in
7032 // the next line then consume the line break.
7033 p = eval_next_non_blank(*arg, evalarg, &getnext);
7034 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007035 && ((*p == '.'
7036 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7037 || rettv->v_type == VAR_CLASS
7038 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007039 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7040 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7041 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007042 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007043 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007044 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007045 check_white = FALSE;
7046 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007047
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007048 if (rettv->v_type == VAR_ANY)
7049 {
7050 char_u *exp_name;
7051 int cc;
7052 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007053 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007054 type_T *type;
7055
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007056 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007057 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007058 if (**arg != '.')
7059 {
7060 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007061 semsg(_(e_expected_dot_after_name_str),
7062 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007063 ret = FAIL;
7064 break;
7065 }
7066 ++*arg;
7067 if (IS_WHITE_OR_NUL(**arg))
7068 {
7069 if (verbose)
7070 emsg(_(e_no_white_space_allowed_after_dot));
7071 ret = FAIL;
7072 break;
7073 }
7074
7075 // isolate the name
7076 exp_name = *arg;
7077 while (eval_isnamec(**arg))
7078 ++*arg;
7079 cc = **arg;
7080 **arg = NUL;
7081
7082 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007083 evalarg == NULL ? NULL : evalarg->eval_cctx,
7084 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007085 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007086
7087 if (idx < 0 && ufunc == NULL)
7088 {
7089 ret = FAIL;
7090 break;
7091 }
7092 if (idx >= 0)
7093 {
7094 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7095 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7096
7097 copy_tv(sv->sv_tv, rettv);
7098 }
7099 else
7100 {
7101 rettv->v_type = VAR_FUNC;
7102 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7103 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007104 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007105 }
7106
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007107 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7108 || rettv->v_type == VAR_PARTIAL))
7109 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007110 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007111 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7112 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007113
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007114 // Stop the expression evaluation when immediately aborting on
7115 // error, or when an interrupt occurred or an exception was thrown
7116 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007117 if (aborting())
7118 {
7119 if (ret == OK)
7120 clear_tv(rettv);
7121 ret = FAIL;
7122 }
7123 dict_unref(selfdict);
7124 selfdict = NULL;
7125 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007126 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007127 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007128 if (in_vim9script())
7129 *arg = skipwhite(p + 2);
7130 else
7131 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007132 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007133 {
zeertzjqc481ad32023-03-11 16:18:51 +00007134 emsg(_(e_no_white_space_allowed_before_parenthesis));
7135 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007136 }
zeertzjqc481ad32023-03-11 16:18:51 +00007137 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7138 // expr->{lambda}() or expr->(lambda)()
7139 ret = eval_lambda(arg, rettv, evalarg, verbose);
7140 else
7141 // expr->name()
7142 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007143 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007144 // "." is ".name" lookup when we found a dict or when evaluating and
7145 // scriptversion is at least 2, where string concatenation is "..".
7146 else if (**arg == '['
7147 || (**arg == '.' && (rettv->v_type == VAR_DICT
7148 || (!evaluate
7149 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007150 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007151 {
7152 dict_unref(selfdict);
7153 if (rettv->v_type == VAR_DICT)
7154 {
7155 selfdict = rettv->vval.v_dict;
7156 if (selfdict != NULL)
7157 ++selfdict->dv_refcount;
7158 }
7159 else
7160 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007161 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007162 {
7163 clear_tv(rettv);
7164 ret = FAIL;
7165 }
7166 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007167 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7168 || rettv->v_type == VAR_OBJECT))
7169 {
7170 // class member: SomeClass.varname
7171 // class method: SomeClass.SomeMethod()
7172 // class constructor: SomeClass.new()
7173 // object member: someObject.varname
7174 // object method: someObject.SomeMethod()
7175 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7176 {
7177 clear_tv(rettv);
7178 ret = FAIL;
7179 }
7180 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007181 else
7182 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007183 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007185 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7186 // Don't do this when "Func" is already a partial that was bound
7187 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007188 if (selfdict != NULL
7189 && (rettv->v_type == VAR_FUNC
7190 || (rettv->v_type == VAR_PARTIAL
7191 && (rettv->vval.v_partial->pt_auto
7192 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007193 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007194
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007195 dict_unref(selfdict);
7196 return ret;
7197}
7198
7199/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 * Make a copy of an item.
7201 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007202 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7204 * reference to an already copied list/dict can be used.
7205 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208item_copy(
7209 typval_T *from,
7210 typval_T *to,
7211 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007212 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214{
7215 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007217
Bram Moolenaar33570922005-01-25 22:26:29 +00007218 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007219 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007220 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 }
7223 ++recurse;
7224
7225 switch (from->v_type)
7226 {
7227 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007228 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229 case VAR_STRING:
7230 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007231 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007232 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007233 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007234 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007235 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007236 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007237 case VAR_CLASS:
7238 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007239 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 copy_tv(from, to);
7241 break;
7242 case VAR_LIST:
7243 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007244 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007245 if (from->vval.v_list == NULL)
7246 to->vval.v_list = NULL;
7247 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007249 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 to->vval.v_list = from->vval.v_list->lv_copylist;
7251 ++to->vval.v_list->lv_refcount;
7252 }
7253 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007254 to->vval.v_list = list_copy(from->vval.v_list,
7255 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007256 if (to->vval.v_list == NULL)
7257 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007259 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007261 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007262 case VAR_DICT:
7263 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007264 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007265 if (from->vval.v_dict == NULL)
7266 to->vval.v_dict = NULL;
7267 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007269 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007270 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7271 ++to->vval.v_dict->dv_refcount;
7272 }
7273 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007274 to->vval.v_dict = dict_copy(from->vval.v_dict,
7275 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007276 if (to->vval.v_dict == NULL)
7277 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007279 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007280 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007282 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007283 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 }
7285 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007286 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287}
7288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 void
7290echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7291{
7292 char_u *tofree;
7293 char_u numbuf[NUMBUFLEN];
7294 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7295
7296 if (*atstart)
7297 {
7298 *atstart = FALSE;
7299 // Call msg_start() after eval1(), evaluating the expression
7300 // may cause a message to appear.
7301 if (with_space)
7302 {
7303 // Mark the saved text as finishing the line, so that what
7304 // follows is displayed on a new line when scrolling back
7305 // at the more prompt.
7306 msg_sb_eol();
7307 msg_start();
7308 }
7309 }
7310 else if (with_space)
7311 msg_puts_attr(" ", echo_attr);
7312
7313 if (p != NULL)
7314 for ( ; *p != NUL && !got_int; ++p)
7315 {
7316 if (*p == '\n' || *p == '\r' || *p == TAB)
7317 {
7318 if (*p != TAB && *needclr)
7319 {
7320 // remove any text still there from the command
7321 msg_clr_eos();
7322 *needclr = FALSE;
7323 }
7324 msg_putchar_attr(*p, echo_attr);
7325 }
7326 else
7327 {
7328 if (has_mbyte)
7329 {
7330 int i = (*mb_ptr2len)(p);
7331
7332 (void)msg_outtrans_len_attr(p, i, echo_attr);
7333 p += i - 1;
7334 }
7335 else
7336 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7337 }
7338 }
7339 vim_free(tofree);
7340}
7341
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 * ":echo expr1 ..." print each argument separated with a space, add a
7344 * newline at the end.
7345 * ":echon expr1 ..." print each argument plain.
7346 */
7347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
7350 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007352 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int needclr = TRUE;
7354 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007355 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007356 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007357 evalarg_T evalarg;
7358
Bram Moolenaare707c882020-07-01 13:07:37 +02007359 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 if (eap->skip)
7362 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007363 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007365 // If eval1() causes an error message the text from the command may
7366 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007367 need_clr_eos = needclr;
7368
Bram Moolenaar68db9962021-05-09 23:19:22 +02007369 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007370 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
7372 /*
7373 * Report the invalid expression unless the expression evaluation
7374 * has been cancelled due to an aborting error, an interrupt, or an
7375 * exception.
7376 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007377 if (!aborting() && did_emsg == did_emsg_before
7378 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007379 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007380 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 break;
7382 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007383 need_clr_eos = FALSE;
7384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007386 {
7387 if (rettv.v_type == VAR_VOID)
7388 {
7389 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7390 break;
7391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007393 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007395 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 arg = skipwhite(arg);
7397 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007398 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007399 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 if (eap->skip)
7402 --emsg_skip;
7403 else
7404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007405 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (needclr)
7407 msg_clr_eos();
7408 if (eap->cmdidx == CMD_echo)
7409 msg_end();
7410 }
7411}
7412
7413/*
7414 * ":echohl {name}".
7415 */
7416 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007419 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
7422/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007423 * Returns the :echo attribute
7424 */
7425 int
7426get_echo_attr(void)
7427{
7428 return echo_attr;
7429}
7430
7431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 * ":execute expr1 ..." execute the result of an expression.
7433 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007434 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007436 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 * Each gets spaces around each argument and a newline at the end for
7438 * echo commands
7439 */
7440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
7443 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 int ret = OK;
7446 char_u *p;
7447 garray_T ga;
7448 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007449 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 ga_init2(&ga, 1, 80);
7452
7453 if (eap->skip)
7454 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007455 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007457 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007458 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 if (!eap->skip)
7462 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007463 char_u buf[NUMBUFLEN];
7464
7465 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007466 {
7467 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7468 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007469 semsg(_(e_using_invalid_value_as_string_str),
7470 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007471 p = NULL;
7472 }
7473 else
7474 p = tv_get_string_buf(&rettv, buf);
7475 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007476 else
7477 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007478 if (p == NULL)
7479 {
7480 clear_tv(&rettv);
7481 ret = FAIL;
7482 break;
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 len = (int)STRLEN(p);
7485 if (ga_grow(&ga, len + 2) == FAIL)
7486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 ret = FAIL;
7489 break;
7490 }
7491 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 ga.ga_len += len;
7495 }
7496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 arg = skipwhite(arg);
7499 }
7500
7501 if (ret != FAIL && ga.ga_data != NULL)
7502 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007503 // use the first line of continuation lines for messages
7504 SOURCING_LNUM = start_lnum;
7505
Bram Moolenaar37fef162022-08-29 18:16:32 +01007506 if (eap->cmdidx == CMD_echomsg
7507 || eap->cmdidx == CMD_echowindow
7508 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007510 // Mark the already saved text as finishing the line, so that what
7511 // follows is displayed on a new line when scrolling back at the
7512 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007513 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007514 }
7515
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007516 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007517 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007518 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007519 out_flush();
7520 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007521 else if (eap->cmdidx == CMD_echowindow)
7522 {
7523#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007524 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007525#endif
7526 msg_attr(ga.ga_data, echo_attr);
7527#ifdef HAS_MESSAGE_WINDOW
7528 end_echowindow();
7529#endif
7530 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007531 else if (eap->cmdidx == CMD_echoconsole)
7532 {
7533 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7534 ui_write((char_u *)"\r\n", 2, TRUE);
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 else if (eap->cmdidx == CMD_echoerr)
7537 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007538 int save_did_emsg = did_emsg;
7539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007540 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (!force_abort)
7543 did_emsg = save_did_emsg;
7544 }
7545 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007546 {
7547 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7548
7549 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7550 sticky_cmdmod_flags = cmdmod.cmod_flags
7551 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007553 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007554 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557
7558 ga_clear(&ga);
7559
7560 if (eap->skip)
7561 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007562 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563}
7564
7565/*
7566 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7567 * "arg" points to the "&" or '+' when called, to "option" when returning.
7568 * Returns NULL when no option name found. Otherwise pointer to the char
7569 * after the option name.
7570 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007571 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007572find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 char_u *p = *arg;
7575
7576 ++p;
7577 if (*p == 'g' && p[1] == ':')
7578 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007579 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 p += 2;
7581 }
7582 else if (*p == 'l' && p[1] == ':')
7583 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007584 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 p += 2;
7586 }
7587 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007588 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 if (!ASCII_ISALPHA(*p))
7591 return NULL;
7592 *arg = p;
7593
7594 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007595 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 else
7597 while (ASCII_ISALPHA(*p))
7598 ++p;
7599 return p;
7600}
7601
7602/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007603 * Display script name where an item was last set.
7604 * Should only be invoked when 'verbose' is non-zero.
7605 */
7606 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007607last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007608{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007609 char_u *p;
7610
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007611 if (script_ctx.sc_sid == 0)
7612 return;
7613
7614 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7615 if (p == NULL)
7616 return;
7617
7618 verbose_enter();
7619 msg_puts(_("\n\tLast set from "));
7620 msg_puts((char *)p);
7621 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007622 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007623 msg_puts(_(line_msg));
7624 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007625 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007626 verbose_leave();
7627 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007628}
7629
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007630#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631
7632/*
7633 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007634 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 * "flags" can be "g" to do a global substitute.
7636 * Returns an allocated string, NULL for error.
7637 */
7638 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639do_string_sub(
7640 char_u *str,
John Marriottbd4614f2024-11-18 20:25:21 +01007641 size_t len,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642 char_u *pat,
7643 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007644 typval_T *expr,
John Marriottbd4614f2024-11-18 20:25:21 +01007645 char_u *flags,
7646 size_t *ret_len) // length of returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 garray_T ga;
7650 char_u *ret;
7651 char_u *save_cpo;
7652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007653 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007655 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
7657 ga_init2(&ga, 1, 200);
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 regmatch.rm_ic = p_ic;
7660 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7661 if (regmatch.regprog != NULL)
7662 {
John Marriottbd4614f2024-11-18 20:25:21 +01007663 char_u *tail = str;
7664 char_u *end = str + len;
7665 int do_all = (flags[0] == 'g');
7666 int sublen;
7667 int i;
7668 char_u *zero_width = NULL;
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7671 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007672 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007673 if (regmatch.startp[0] == regmatch.endp[0])
7674 {
7675 if (zero_width == regmatch.startp[0])
7676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007677 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007678 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007679 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7680 (size_t)i);
7681 ga.ga_len += i;
7682 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007683 continue;
7684 }
7685 zero_width = regmatch.startp[0];
7686 }
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 /*
7689 * Get some space for a temporary buffer to do the substitution
7690 * into. It will contain:
7691 * - The text up to where the match is.
7692 * - The substituted text.
7693 * - The text after the match.
7694 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007695 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007696 if (sublen <= 0)
7697 {
7698 ga_clear(&ga);
7699 break;
7700 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007701 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7703 {
7704 ga_clear(&ga);
7705 break;
7706 }
7707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007708 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 i = (int)(regmatch.startp[0] - tail);
7710 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007711 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007712 (void)vim_regsub(&regmatch, sub, expr,
7713 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7714 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007716 tail = regmatch.endp[0];
7717 if (*tail == NUL)
7718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (!do_all)
7720 break;
7721 }
7722
7723 if (ga.ga_data != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01007724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
John Marriottbd4614f2024-11-18 20:25:21 +01007726 ga.ga_len += (int)(end - tail);
7727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar473de612013-06-08 18:19:48 +02007729 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731
John Marriottbd4614f2024-11-18 20:25:21 +01007732 if (ga.ga_data != NULL)
7733 {
7734 str = (char_u *)ga.ga_data;
7735 len = (size_t)ga.ga_len;
7736 }
7737 ret = vim_strnsave(str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007739 if (p_cpo == empty_option)
7740 p_cpo = save_cpo;
7741 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007743 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007744 // If it's still empty it was changed and restored, need to restore in
7745 // the complicated way.
7746 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007747 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007748 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750
John Marriottbd4614f2024-11-18 20:25:21 +01007751 if (ret_len != NULL)
7752 *ret_len = len;
7753
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 return ret;
7755}