blob: f4f8c058db3aefea2770029c31a0287702214b07 [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;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822
823 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824 if (evalarg != NULL)
825 {
826 // need to evaluate this to get an import, like in "a.Func"
827 save_flags = evalarg->eval_flags;
828 evalarg->eval_flags |= EVAL_EVALUATE;
829 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100830 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100831 {
832 dictitem_T *v;
833
834 // If <SID>VarName was used it would not be found, try another way.
835 v = find_var_also_in_script(name, NULL, FALSE);
836 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100837 {
838 name = NULL;
839 goto theend;
840 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100841 copy_tv(&v->di_tv, &ref);
842 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 if (*skipwhite(*arg) != NUL)
844 {
845 if (verbose)
846 semsg(_(e_trailing_characters_str), *arg);
847 name = NULL;
848 }
849 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
850 {
851 name = ref.vval.v_string;
852 ref.vval.v_string = NULL;
853 *tofree = name;
854 }
855 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
856 {
857 if (ref.vval.v_partial->pt_argc > 0
858 || ref.vval.v_partial->pt_dict != NULL)
859 {
860 if (verbose)
861 emsg(_(e_cannot_use_partial_here));
862 name = NULL;
863 }
864 else
865 {
866 name = vim_strsave(partial_name(ref.vval.v_partial));
867 *tofree = name;
868 }
869 }
870 else
871 {
872 if (verbose)
873 semsg(_(e_not_callable_type_str), name);
874 name = NULL;
875 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100876
877theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000878 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100879 if (evalarg != NULL)
880 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000881 return name;
882}
883
884/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100885 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200886 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
887 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100891call_vim_function(
892 char_u *func,
893 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200894 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200895 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200898 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000899 char_u *arg;
900 char_u *name;
901 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000902 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100904 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200905 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000906 funcexe.fe_firstline = curwin->w_cursor.lnum;
907 funcexe.fe_lastline = curwin->w_cursor.lnum;
908 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000909
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000910 // The name might be "import.Func" or "Funcref". We don't know, we need to
911 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000912 // autoload script has errors. Guess that when there is a dot in the name
913 // showing errors is the right choice.
914 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000915 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000916 if (ignore_errors)
917 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000918 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000919 if (ignore_errors)
920 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000921 if (name == NULL)
922 name = func;
923
924 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
925
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000926 if (ret == FAIL)
927 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000928 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000929
930 return ret;
931}
932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100933/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100934 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000935 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
936 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000937 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000938 */
939 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100940call_func_retstr(
941 char_u *func,
942 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200943 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000944{
945 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000946 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000947
Bram Moolenaarded27a12018-08-01 19:06:03 +0200948 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949 return NULL;
950
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100951 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return retval;
954}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000956/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100957 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000958 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000959 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100960 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000961 */
962 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963call_func_retlist(
964 char_u *func,
965 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200966 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000967{
968 typval_T rettv;
969
Bram Moolenaarded27a12018-08-01 19:06:03 +0200970 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000971 return NULL;
972
973 if (rettv.v_type != VAR_LIST)
974 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100975 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
976 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 clear_tv(&rettv);
978 return NULL;
979 }
980
981 return rettv.vval.v_list;
982}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaare70dd112022-01-21 16:31:11 +0000984#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100986 * Evaluate "arg", which is 'foldexpr'.
987 * Note: caller must set "curwin" to match "arg".
988 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
989 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 */
991 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000992eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000994 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200996 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000998 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000999 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001000 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001002 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001003 current_sctx = wp->w_p_script_ctx[WV_FDE];
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001006 if (use_sandbox)
1007 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001008 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001010
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001011 // Evaluate the expression. If the expression is "FuncName()" call the
1012 // function directly.
1013 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 retval = 0;
1015 else
1016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 if (tv.v_type == VAR_NUMBER)
1019 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001020 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 retval = 0;
1022 else
1023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // If the result is a string, check if there is a non-digit before
1025 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001027 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 *cp = *s++;
1029 retval = atol((char *)s);
1030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001034 if (use_sandbox)
1035 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001036 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001037 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001038 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001040 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042#endif
1043
Ernie Rael64885642023-10-04 20:16:22 +02001044#ifdef LOG_LOCKVAR
1045typedef struct flag_string_S
1046{
1047 int flag;
1048 char *str;
1049} flag_string_T;
1050
1051 static char *
1052flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1053{
1054 char *p = buf;
1055 *p = NUL;
1056 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1057 {
1058 if ((fstring->flag & flags) != 0)
1059 {
1060 size_t len = STRLEN(fstring->str);
1061 if (n > p - buf + len + 7)
1062 {
1063 STRCAT(p, fstring->str);
1064 p += len;
1065 STRCAT(p, " ");
1066 ++p;
1067 }
1068 else
1069 {
1070 STRCAT(buf, "...");
1071 break;
1072 }
1073 }
1074 }
1075 return buf;
1076}
1077
1078flag_string_T glv_flag_strings[] = {
1079 { GLV_QUIET, "QUIET" },
1080 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1081 { GLV_READ_ONLY, "READ_ONLY" },
1082 { GLV_NO_DECL, "NO_DECL" },
1083 { GLV_COMPILING, "COMPILING" },
1084 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1085 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1086 { 0, NULL }
1087};
1088#endif
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
Ernie Raelee865f32023-09-29 19:53:55 +02001091 * Fill in "lp" using "root". This is used in a special case when
1092 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1093 *
1094 * This is typically called with "lval_root" as "root". For a class, find
1095 * the name from lp in the class from root, fill in lval_T if found. For a
1096 * complex type, list/dict use it as the result; just put the root into
1097 * ll_tv.
1098 *
1099 * "lval_root" is a hack used during run-time/instr-execution to provide the
1100 * starting point for "get_lval()" to traverse a chain of indexes. In some
1101 * cases get_lval sees a bare name and uses this function to populate the
1102 * lval_T.
1103 *
1104 * For setting up "lval_root" (currently only used with lockvar)
1105 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1106 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1107 */
1108 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001109fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001110{
1111#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001112 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1113 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001114#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001115 if (lr->lr_tv == NULL)
1116 return;
Ernie Rael64885642023-10-04 20:16:22 +02001117 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001118 {
Ernie Rael64885642023-10-04 20:16:22 +02001119 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001120 {
1121 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001122 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001123 int m_idx;
1124 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1125 lp->ll_name_end - lp->ll_name, &m_idx);
1126 if (m != NULL)
1127 {
1128 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001129 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001130 lp->ll_oi = m_idx;
1131 lp->ll_valtype = m->ocm_type;
1132 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1133#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001134 ch_log(NULL, "LKVAR: ... class member %s.%s",
1135 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001136#endif
1137 return;
1138 }
1139 }
1140 }
1141
1142#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001143 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001144#endif
Ernie Rael64885642023-10-04 20:16:22 +02001145 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001146 lp->ll_is_root = TRUE;
1147}
1148
1149/*
Ernie Rael64885642023-10-04 20:16:22 +02001150 * Check if the class has permission to access the member.
1151 * Returns OK or FAIL.
1152 */
1153 static int
1154get_lval_check_access(
1155 class_T *cl_exec, // executing class, NULL if :def or script level
1156 class_T *cl, // class which contains the member
1157 ocmember_T *om, // member being accessed
1158 char_u *p, // char after member name
1159 int flags) // GLV flags to check if writing to lval
1160{
1161#ifdef LOG_LOCKVAR
1162 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1163 (void*)cl_exec, (void*)cl, *p);
1164#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001165 if (cl_exec != NULL && cl_exec == cl)
1166 return OK;
1167
1168 char *msg = NULL;
1169 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001170 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001171 case VIM_ACCESS_PRIVATE:
1172 msg = e_cannot_access_protected_variable_str;
1173 break;
1174 case VIM_ACCESS_READ:
1175 // If [idx] or .key following, read only OK.
1176 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001177 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001178 if ((flags & GLV_READ_ONLY) == 0)
1179 {
1180 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001181 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001182 if (om->ocm_type->tt_type == VAR_OBJECT)
1183 semsg(_(e_enumvalue_str_cannot_be_modified),
1184 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001185 else
1186 msg = e_variable_is_not_writable_str;
1187 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001188 else
1189 msg = e_variable_is_not_writable_str;
1190 }
1191 break;
1192 case VIM_ACCESS_ALL:
1193 break;
1194 }
1195 if (msg != NULL)
1196 {
1197 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1198 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001199 }
1200 return OK;
1201}
1202
1203/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001204 * Get lval information for a variable imported from script "imp_sid". On
1205 * success, updates "lp" with the variable name, type, script ID and typval.
1206 * The variable name starts at or after "p".
1207 * If "rettv" is not NULL it points to the value to be assigned. This used to
1208 * match the rhs and lhs types.
1209 * Returns a pointer to the character after the variable name if the imported
1210 * variable is valid and writable.
1211 * Returns NULL if the variable is not exported or typval is not found or the
1212 * rhs type doesn't match the lhs type or the variable is not writable.
1213 */
1214 static char_u *
1215get_lval_imported(
1216 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001217 scid_T imp_sid,
1218 char_u *p,
1219 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001220 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001221{
1222 ufunc_T *ufunc;
1223 type_T *type = NULL;
1224 int cc;
1225 int rc = FAIL;
1226
1227 p = skipwhite(p);
1228
1229 import_check_sourced_sid(&imp_sid);
1230 lp->ll_sid = imp_sid;
1231 lp->ll_name = p;
1232 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1233 lp->ll_name_end = p;
1234
1235 // check the item is exported
1236 cc = *p;
1237 *p = NUL;
1238 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1239 TRUE) == -1)
1240 goto failed;
1241
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001242 // Get the typval for the exported item
1243 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1244 if (ht == NULL)
1245 goto failed;
1246
1247 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1248 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001249 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001250 goto success;
1251
1252 *dip = di;
1253
1254 // Check whether the variable is writable.
1255 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1256 if (sv != NULL && sv->sv_const != 0)
1257 {
1258 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1259 goto failed;
1260 }
1261
1262 // check whether variable is locked
1263 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1264 goto failed;
1265
1266 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001267 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001268
1269success:
1270 rc = OK;
1271
1272failed:
1273 *p = cc;
1274 return rc == OK ? p : NULL;
1275}
1276
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001277typedef enum {
1278 GLV_FAIL,
1279 GLV_OK,
1280 GLV_STOP
1281} glv_status_T;
1282
1283/*
1284 * Get an Dict lval variable that can be assigned a value to: "name",
1285 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1286 * "name" points to the start of the name.
1287 * If "rettv" is not NULL it points to the value to be assigned.
1288 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1289 * wrong; must end in space or cmd separator.
1290 *
1291 * flags:
1292 * GLV_QUIET: do not give error messages
1293 * GLV_READ_ONLY: will not change the variable
1294 * GLV_NO_AUTOLOAD: do not use script autoloading
1295 *
1296 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1297 * failure. Returns GLV_STOP to stop processing the characters following
1298 * 'key_end'.
1299 */
1300 static int
1301get_lval_dict_item(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001302 lval_T *lp,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001303 char_u *name,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001304 char_u *key,
1305 int len,
1306 char_u **key_end,
1307 typval_T *var1,
1308 int flags,
1309 int unlet,
1310 typval_T *rettv)
1311{
1312 int quiet = flags & GLV_QUIET;
1313 char_u *p = *key_end;
1314
1315 if (len == -1)
1316 {
1317 // "[key]": get key from "var1"
1318 key = tv_get_string_chk(var1); // is number or string
1319 if (key == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001320 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001321 }
1322 lp->ll_list = NULL;
1323 lp->ll_object = NULL;
1324 lp->ll_class = NULL;
1325
1326 // a NULL dict is equivalent with an empty dict
1327 if (lp->ll_tv->vval.v_dict == NULL)
1328 {
1329 lp->ll_tv->vval.v_dict = dict_alloc();
1330 if (lp->ll_tv->vval.v_dict == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001331 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001332 ++lp->ll_tv->vval.v_dict->dv_refcount;
1333 }
1334 lp->ll_dict = lp->ll_tv->vval.v_dict;
1335
1336 lp->ll_di = dict_find(lp->ll_dict, key, len);
1337
1338 // When assigning to a scope dictionary check that a function and
1339 // variable name is valid (only variable name unless it is l: or
1340 // g: dictionary). Disallow overwriting a builtin function.
1341 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1342 {
1343 int prevval;
1344
1345 if (len != -1)
1346 {
1347 prevval = key[len];
1348 key[len] = NUL;
1349 }
1350 else
1351 prevval = 0; // avoid compiler warning
1352 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1353 && (rettv->v_type == VAR_FUNC
1354 || rettv->v_type == VAR_PARTIAL)
1355 && var_wrong_func_name(key, lp->ll_di == NULL))
1356 || !valid_varname(key, -1, TRUE);
1357 if (len != -1)
1358 key[len] = prevval;
1359 if (wrong)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001360 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001361 }
1362
1363 if (lp->ll_valtype != NULL)
1364 // use the type of the member
1365 lp->ll_valtype = lp->ll_valtype->tt_member;
1366
1367 if (lp->ll_di == NULL)
1368 {
1369 // Can't add "v:" or "a:" variable.
1370 if (lp->ll_dict == get_vimvar_dict()
1371 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1372 {
1373 semsg(_(e_illegal_variable_name_str), name);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001374 return GLV_FAIL;
1375 }
1376
1377 // Key does not exist in dict: may need to add it.
1378 if (*p == '[' || *p == '.' || unlet)
1379 {
1380 if (!quiet)
1381 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001382 return GLV_FAIL;
1383 }
1384 if (len == -1)
1385 lp->ll_newkey = vim_strsave(key);
1386 else
1387 lp->ll_newkey = vim_strnsave(key, len);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001388 if (lp->ll_newkey == NULL)
1389 p = NULL;
1390
1391 *key_end = p;
1392 return GLV_STOP;
1393 }
1394 // existing variable, need to check if it can be changed
1395 else if ((flags & GLV_READ_ONLY) == 0
1396 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1397 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001398 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001399
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001400 lp->ll_tv = &lp->ll_di->di_tv;
1401
1402 return GLV_OK;
1403}
1404
1405/*
1406 * Get an blob lval variable that can be assigned a value to: "name",
1407 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1408 *
1409 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1410 * blob index. If the first index is not specified in a range, then 'empty1'
1411 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1412 * invalid indexes.
1413 *
1414 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1415 */
1416 static int
1417get_lval_blob(
1418 lval_T *lp,
1419 typval_T *var1,
1420 typval_T *var2,
1421 int empty1,
1422 int quiet)
1423{
1424 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1425
1426 // Get the number and item for the only or first index of the List.
1427 if (empty1)
1428 lp->ll_n1 = 0;
1429 else
1430 // is number or string
1431 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001432
1433 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001434 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001435 if (lp->ll_range && !lp->ll_empty2)
1436 {
1437 lp->ll_n2 = (long)tv_get_number(var2);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001438 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1439 return FAIL;
1440 }
1441
1442 if (!lp->ll_range)
1443 // Indexing a single byte in a blob. So the rhs type is a
1444 // number.
1445 lp->ll_valtype = &t_number;
1446
1447 lp->ll_blob = lp->ll_tv->vval.v_blob;
1448 lp->ll_tv = NULL;
1449
1450 return OK;
1451}
1452
1453/*
1454 * Get a List lval variable that can be assigned a value to: "name",
1455 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1456 *
1457 * 'var1' specifies the starting List index and 'var2' specifies the ending
1458 * List index. If the first index is not specified in a range, then 'empty1'
1459 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1460 * invalid indexes.
1461 *
1462 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1463 */
1464 static int
1465get_lval_list(
1466 lval_T *lp,
1467 typval_T *var1,
1468 typval_T *var2,
1469 int empty1,
1470 int flags,
1471 int quiet)
1472{
1473 /*
1474 * Get the number and item for the only or first index of the List.
1475 */
1476 if (empty1)
1477 lp->ll_n1 = 0;
1478 else
1479 // is number or string
1480 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001481
1482 lp->ll_dict = NULL;
1483 lp->ll_object = NULL;
1484 lp->ll_class = NULL;
1485 lp->ll_list = lp->ll_tv->vval.v_list;
1486 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1487 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1488 if (lp->ll_li == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001489 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001490
1491 if (lp->ll_valtype != NULL && !lp->ll_range)
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001492 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001493 // use the type of the member
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001494 if (lp->ll_valtype->tt_member != NULL)
1495 lp->ll_valtype = lp->ll_valtype->tt_member;
1496 else
1497 // If the LHS member type is not known (VAR_ANY), then get it from
1498 // the list item (after indexing)
1499 lp->ll_valtype = typval2type(&lp->ll_li->li_tv, get_copyID(),
1500 &lp->ll_type_list, TVTT_DO_MEMBER);
1501
1502 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001503
1504 /*
1505 * May need to find the item or absolute index for the second
1506 * index of a range.
1507 * When no index given: "lp->ll_empty2" is TRUE.
1508 * Otherwise "lp->ll_n2" is set to the second index.
1509 */
1510 if (lp->ll_range && !lp->ll_empty2)
1511 {
1512 lp->ll_n2 = (long)tv_get_number(var2);
1513 // is number or string
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001514 if (check_range_index_two(lp->ll_list,
1515 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1516 return FAIL;
1517 }
1518
1519 lp->ll_tv = &lp->ll_li->li_tv;
1520
1521 return OK;
1522}
1523
1524/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001525 * Get a class or object lval method in class "cl". The 'key' argument points
1526 * to the method name and 'key_end' points to the character after 'key'.
1527 * 'v_type' is VAR_CLASS or VAR_OBJECT.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001528 *
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001529 * The method index, method function pointer and method type are returned in
1530 * "lp".
1531 */
1532 static void
1533get_lval_oc_method(
1534 lval_T *lp,
1535 class_T *cl,
1536 char_u *key,
1537 char_u *key_end,
1538 vartype_T v_type)
1539{
1540 // Look for a method with this name.
1541 // round 1: class functions (skipped for an object)
1542 // round 2: object methods
1543 for (int round = v_type == VAR_OBJECT ? 2 : 1; round <= 2; ++round)
1544 {
1545 int m_idx;
1546 ufunc_T *fp;
1547
1548 fp = method_lookup(cl, round == 1 ? VAR_CLASS : VAR_OBJECT,
1549 key, key_end - key, &m_idx);
1550 lp->ll_oi = m_idx;
1551 if (fp != NULL)
1552 {
1553 lp->ll_ufunc = fp;
1554 lp->ll_valtype = fp->uf_func_type;
1555 break;
1556 }
1557 }
1558}
1559
1560/*
1561 * Get a class or object lval variable in class "cl". The "key" argument
1562 * points to the variable name and "key_end" points to the character after
1563 * "key". "v_type" is VAR_CLASS or VAR_OBJECT. "cl_exec" is the class that is
1564 * executing, or NULL.
1565 *
1566 * The variable index, typval and type are returned in "lp". Returns FAIL if
1567 * the variable is not writable. Otherwise returns OK.
1568 */
1569 static int
1570get_lval_oc_variable(
1571 lval_T *lp,
1572 class_T *cl,
1573 char_u *key,
1574 char_u *key_end,
1575 vartype_T v_type,
1576 class_T *cl_exec,
1577 int flags)
1578{
1579 int m_idx;
1580 ocmember_T *om;
1581
1582 om = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1583 lp->ll_oi = m_idx;
1584 if (om == NULL)
1585 return OK;
1586
1587 // Check variable is accessible
1588 if (get_lval_check_access(cl_exec, cl, om, key_end, flags) == FAIL)
1589 return FAIL;
1590
1591 // When lhs is used to modify the variable, check it is not a read-only
1592 // variable.
1593 if ((flags & GLV_READ_ONLY) == 0 && (*key_end != '.' && *key_end != '[')
1594 && oc_var_check_ro(cl, om))
1595 return FAIL;
1596
1597 lp->ll_valtype = om->ocm_type;
1598
1599 if (v_type == VAR_OBJECT)
1600 lp->ll_tv = ((typval_T *)(lp->ll_tv->vval.v_object + 1)) + m_idx;
1601 else
1602 lp->ll_tv = &cl->class_members_tv[m_idx];
1603
1604 return OK;
1605}
1606
1607/*
1608 * Get a Class or Object lval variable or method that can be assigned a value
1609 * to: "name", "name.key", "name.key[expr]" etc.
1610 *
1611 * The 'key' argument points to the member name and 'key_end' points to the
1612 * character after 'key'. 'v_type' is VAR_CLASS or VAR_OBJECT. 'cl_exec' is
1613 * the class that is executing, or NULL. If 'quiet' is TRUE, then error
1614 * messages are not displayed for invalid indexes.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001615 *
1616 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1617 * failure.
1618 */
1619 static int
1620get_lval_class_or_obj(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001621 lval_T *lp,
1622 char_u *key,
1623 char_u *key_end,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001624 vartype_T v_type,
1625 class_T *cl_exec,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001626 int flags,
1627 int quiet)
1628{
1629 lp->ll_dict = NULL;
1630 lp->ll_list = NULL;
1631
1632 class_T *cl;
1633 if (v_type == VAR_OBJECT)
1634 {
1635 if (lp->ll_tv->vval.v_object == NULL)
1636 {
1637 if (!quiet)
1638 emsg(_(e_using_null_object));
1639 return FAIL;
1640 }
1641 cl = lp->ll_tv->vval.v_object->obj_class;
1642 lp->ll_object = lp->ll_tv->vval.v_object;
1643 }
1644 else
1645 {
1646 cl = lp->ll_tv->vval.v_class;
1647 lp->ll_object = NULL;
1648 }
1649 lp->ll_class = cl;
1650
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001651 if (cl == NULL)
1652 // TODO: what if class is NULL?
1653 return OK;
1654
1655 lp->ll_valtype = NULL;
1656
1657 if (flags & GLV_PREFER_FUNC)
1658 get_lval_oc_method(lp, cl, key, key_end, v_type);
1659
1660 // Look for object/class member variable
1661 if (lp->ll_valtype == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001662 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001663 if (get_lval_oc_variable(lp, cl, key, key_end, v_type, cl_exec, flags)
1664 == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001665 return FAIL;
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001666 }
1667
1668 if (lp->ll_valtype == NULL)
1669 {
1670 member_not_found_msg(cl, v_type, key, key_end - key);
1671 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001672 }
1673
1674 return OK;
1675}
1676
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001677/*
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001678 * Check whether dot (".") is allowed after the variable "name" with type
1679 * "v_type". Only Dict, Class and Object types support a dot after the name.
1680 * Returns TRUE if dot is allowed after the name.
1681 */
1682 static int
1683dot_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1684{
1685 if (v_type != VAR_DICT && v_type != VAR_OBJECT && v_type != VAR_CLASS)
1686 {
1687 if (!quiet)
1688 semsg(_(e_dot_not_allowed_after_str_str),
1689 vartype_name(v_type), name);
1690 return FALSE;
1691 }
1692
1693 return TRUE;
1694}
1695
1696/*
1697 * Check whether left bracket ("[") is allowed after the variable "name" with
1698 * type "v_type". Only Dict, List and Blob types support a bracket after the
1699 * variable name. Returns TRUE if bracket is allowed after the name.
1700 */
1701 static int
1702bracket_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1703{
1704 if (v_type == VAR_CLASS || v_type == VAR_OBJECT)
1705 {
1706 if (!quiet)
1707 semsg(_(e_index_not_allowed_after_str_str),
1708 vartype_name(v_type), name);
1709 return FALSE;
1710 }
1711
1712 return TRUE;
1713}
1714
1715/*
1716 * Check whether the variable "name" with type "v_type" can be followed by an
1717 * index. Only Dict, List, Blob, Object and Class types support indexing.
1718 * Returns TRUE if indexing is allowed after the name.
1719 */
1720 static int
1721index_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1722{
1723 if (v_type != VAR_LIST && v_type != VAR_DICT && v_type != VAR_BLOB &&
1724 v_type != VAR_OBJECT && v_type != VAR_CLASS)
1725 {
1726 if (!quiet)
1727 semsg(_(e_index_not_allowed_after_str_str),
1728 vartype_name(v_type), name);
1729 return FALSE;
1730 }
1731
1732 return TRUE;
1733}
1734
1735/*
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001736 * Get the lval of a list/dict/blob/object/class subitem starting at "p". Loop
1737 * until no more [idx] or .key is following.
1738 *
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001739 * If "rettv" is not NULL it points to the value to be assigned.
1740 * "unlet" is TRUE for ":unlet".
1741 *
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001742 * Returns a pointer to the character after the subscript on success or NULL on
1743 * failure.
1744 */
1745 static char_u *
1746get_lval_subscript(
1747 lval_T *lp,
1748 char_u *p,
1749 char_u *name,
1750 typval_T *rettv,
1751 hashtab_T *ht,
1752 dictitem_T *v,
1753 int unlet,
1754 int flags, // GLV_ values
1755 class_T *cl_exec)
1756{
1757 int vim9script = in_vim9script();
1758 int quiet = flags & GLV_QUIET;
1759 char_u *key = NULL;
1760 int len;
1761 typval_T var1;
1762 typval_T var2;
1763 int empty1 = FALSE;
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001764 int rc = FAIL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001765
1766 /*
1767 * Loop until no more [idx] or .key is following.
1768 */
1769 var1.v_type = VAR_UNKNOWN;
1770 var2.v_type = VAR_UNKNOWN;
1771
1772 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1773 {
1774 vartype_T v_type = lp->ll_tv->v_type;
1775
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001776 if (*p == '.' && !dot_allowed_after_type(name, v_type, quiet))
1777 goto done;
1778
1779 if (*p == '[' && !bracket_allowed_after_type(name, v_type, quiet))
1780 goto done;
1781
1782 if (!index_allowed_after_type(name, v_type, quiet))
1783 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001784
1785 // A NULL list/blob works like an empty list/blob, allocate one now.
1786 int r = OK;
1787 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1788 r = rettv_list_alloc(lp->ll_tv);
1789 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1790 r = rettv_blob_alloc(lp->ll_tv);
1791 if (r == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001792 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001793
1794 if (lp->ll_range)
1795 {
1796 if (!quiet)
1797 emsg(_(e_slice_must_come_last));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001798 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001799 }
1800#ifdef LOG_LOCKVAR
1801 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1802 vartype_name(v_type));
1803#endif
1804
1805 if (vim9script && lp->ll_valtype == NULL
1806 && v != NULL
1807 && lp->ll_tv == &v->di_tv
1808 && ht != NULL && ht == get_script_local_ht())
1809 {
1810 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1811
1812 // Vim9 script local variable: get the type
1813 if (sv != NULL)
1814 {
1815 lp->ll_valtype = sv->sv_type;
1816#ifdef LOG_LOCKVAR
1817 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1818 vartype_name(lp->ll_valtype->tt_type));
1819#endif
1820 }
1821 }
1822
1823 len = -1;
1824 if (*p == '.')
1825 {
1826 key = p + 1;
1827
1828 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1829 ;
1830 if (len == 0)
1831 {
1832 if (!quiet)
1833 emsg(_(e_cannot_use_empty_key_for_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001834 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001835 }
1836 p = key + len;
1837 }
1838 else
1839 {
1840 // Get the index [expr] or the first index [expr: ].
1841 p = skipwhite(p + 1);
1842 if (*p == ':')
1843 empty1 = TRUE;
1844 else
1845 {
1846 empty1 = FALSE;
1847 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001848 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001849 if (tv_get_string_chk(&var1) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001850 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001851 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001852 p = skipwhite(p);
1853 }
1854
1855 // Optionally get the second index [ :expr].
1856 if (*p == ':')
1857 {
1858 if (v_type == VAR_DICT)
1859 {
1860 if (!quiet)
1861 emsg(_(e_cannot_slice_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001862 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001863 }
1864 if (rettv != NULL
1865 && !(rettv->v_type == VAR_LIST
1866 && rettv->vval.v_list != NULL)
1867 && !(rettv->v_type == VAR_BLOB
1868 && rettv->vval.v_blob != NULL))
1869 {
1870 if (!quiet)
1871 emsg(_(e_slice_requires_list_or_blob_value));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001872 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001873 }
1874 p = skipwhite(p + 1);
1875 if (*p == ']')
1876 lp->ll_empty2 = TRUE;
1877 else
1878 {
1879 lp->ll_empty2 = FALSE;
1880 // recursive!
1881 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001882 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001883 if (tv_get_string_chk(&var2) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001884 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001885 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001886 }
1887 lp->ll_range = TRUE;
1888 }
1889 else
1890 lp->ll_range = FALSE;
1891
1892 if (*p != ']')
1893 {
1894 if (!quiet)
1895 emsg(_(e_missing_closing_square_brace));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001896 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001897 }
1898
1899 // Skip to past ']'.
1900 ++p;
1901 }
1902#ifdef LOG_LOCKVAR
1903 if (len == -1)
1904 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1905 empty1 ? ":" : (char*)tv_get_string(&var1));
1906 else
1907 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
1908#endif
1909
1910 if (v_type == VAR_DICT)
1911 {
1912 glv_status_T glv_status;
1913
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001914 glv_status = get_lval_dict_item(lp, name, key, len, &p, &var1,
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001915 flags, unlet, rettv);
1916 if (glv_status == GLV_FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001917 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001918 if (glv_status == GLV_STOP)
1919 break;
1920 }
1921 else if (v_type == VAR_BLOB)
1922 {
1923 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001924 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001925
1926 break;
1927 }
1928 else if (v_type == VAR_LIST)
1929 {
1930 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001931 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001932 }
1933 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
1934 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001935 if (get_lval_class_or_obj(lp, key, p, v_type, cl_exec, flags,
1936 quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001937 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001938 }
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001939
1940 clear_tv(&var1);
1941 clear_tv(&var2);
1942 var1.v_type = VAR_UNKNOWN;
1943 var2.v_type = VAR_UNKNOWN;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001944 }
1945
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001946 rc = OK;
1947
1948done:
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001949 clear_tv(&var1);
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001950 clear_tv(&var2);
1951 return rc == OK ? p : NULL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001952}
1953
1954/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 * Get an lval: variable, Dict item or List item that can be assigned a value
1956 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1957 * "name.key", "name.key[expr]" etc.
1958 * Indexing only works if "name" is an existing List or Dictionary.
1959 * "name" points to the start of the name.
1960 * If "rettv" is not NULL it points to the value to be assigned.
1961 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1962 * wrong; must end in space or cmd separator.
1963 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001964 * flags:
1965 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001966 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001967 * GLV_NO_AUTOLOAD: do not use script autoloading
1968 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001974get_lval(
1975 char_u *name,
1976 typval_T *rettv,
1977 lval_T *lp,
1978 int unlet,
1979 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001980 int flags, // GLV_ values
1981 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 char_u *expr_start, *expr_end;
1985 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001986 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001987 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001988 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001989 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001990 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001991 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992
Ernie Raelee865f32023-09-29 19:53:55 +02001993#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001994 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001995 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001996 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001997 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1998 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001999 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02002000 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02002001 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02002002#endif
2003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02002005 CLEAR_POINTER(lp);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002006 ga_init2(&lp->ll_type_list, sizeof(type_T *), 10);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002008 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002010 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02002012 lp->ll_name_end = find_name_end(name, NULL, NULL,
2013 FNE_INCL_BR | fne_flags);
2014 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 }
2016
Bram Moolenaara749a422022-02-12 19:52:25 +00002017 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00002018 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00002019 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
2020 {
2021 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
2022 return NULL;
2023 }
2024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002025 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002026 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (expr_start != NULL)
2029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002030 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01002031 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 && *p != '[' && *p != '.')
2033 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002034 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 return NULL;
2036 }
2037
2038 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2039 if (lp->ll_exp_name == NULL)
2040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002041 // Report an invalid expression in braces, unless the
2042 // expression evaluation has been cancelled due to an
2043 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 if (!aborting() && !quiet)
2045 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002046 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002047 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 return NULL;
2049 }
2050 }
2051 lp->ll_name = lp->ll_exp_name;
2052 }
2053 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 lp->ll_name = name;
2056
Bram Moolenaar4525a572022-02-13 11:57:33 +00002057 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002059 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002060 // However, "g:[key]" is indexing a dictionary.
2061 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002062 {
2063 --p;
2064 lp->ll_name_end = p;
2065 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002066 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002067 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002068 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02002069
Bram Moolenaar9510d222022-09-11 15:14:05 +01002070 if (is_scoped_variable(name))
2071 {
2072 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2073 return NULL;
2074 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002075 if (VIM_ISWHITE(*p))
2076 {
2077 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2078 return NULL;
2079 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002080 if (tp == p + 1 && !quiet)
2081 {
2082 semsg(_(e_white_space_required_after_str_str), ":", p);
2083 return NULL;
2084 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002085 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002086 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002087 semsg(_(e_using_type_not_in_script_context_str), p);
2088 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002089 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002090 if (vim9script && (flags & GLV_NO_DECL) &&
2091 !(flags & GLV_FOR_LOOP))
2092 {
2093 // Using a type and not in a "var" declaration.
2094 semsg(_(e_trailing_characters_str), p);
2095 return NULL;
2096 }
2097
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002098
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002099 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002100 lp->ll_type = parse_type(&tp,
2101 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2102 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002103 if (lp->ll_type == NULL && !quiet)
2104 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002105 lp->ll_name_end = tp;
2106 }
Ernie Raelee865f32023-09-29 19:53:55 +02002107 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 }
2109 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002110 if (lp->ll_name == NULL)
2111 return p;
2112
Bram Moolenaar62aec932022-01-29 21:45:34 +00002113 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002114 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002115 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002116 if (import != NULL)
2117 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002118 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002119 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002120 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002121 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002122 }
2123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
Ernie Rael0f058d12023-10-14 11:25:04 +02002125 // Without [idx] or .key we are done.
2126 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002127 {
2128 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002129 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002131 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132
Ernie Rael0f058d12023-10-14 11:25:04 +02002133 if (vim9script && lval_root != NULL)
2134 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002135 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002136 {
2137 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002138 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002139 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002140 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002141 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002142 {
2143 cc = *p;
2144 *p = NUL;
2145 // When we would write to the variable pass &ht and prevent autoload.
2146 writing = !(flags & GLV_READ_ONLY);
2147 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002148 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002149 if (v == NULL && !quiet)
2150 semsg(_(e_undefined_variable_str), lp->ll_name);
2151 *p = cc;
2152 if (v == NULL)
2153 return NULL;
2154 lp->ll_tv = &v->di_tv;
2155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156
Bram Moolenaar4525a572022-02-13 11:57:33 +00002157 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002158 {
2159 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002160 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002161 return NULL;
2162 }
2163
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002164 // If the next character is a "." or a "[", then process the subitem.
2165 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2166 if (p == NULL)
2167 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002169 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2170 {
2171 where_T where = WHERE_INIT;
2172
2173 // In a vim9 script, do type check and make sure the variable is
2174 // writable.
2175 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2176 return NULL;
2177 }
2178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 return p;
2181}
2182
2183/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002187clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188{
2189 vim_free(lp->ll_exp_name);
2190 vim_free(lp->ll_newkey);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002191 clear_type_list(&lp->ll_type_list);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192}
2193
2194/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002195 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002197 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2198 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201set_var_lval(
2202 lval_T *lp,
2203 char_u *endp,
2204 typval_T *rettv,
2205 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002206 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2207 char_u *op,
2208 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209{
2210 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002211 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212
2213 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002215 cc = *endp;
2216 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002217 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002218 return;
2219
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002220 if (lp->ll_blob != NULL)
2221 {
2222 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002223
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002224 if (op != NULL && *op != '=')
2225 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002226 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002227 return;
2228 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002229 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002230 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002231
2232 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2233 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002234 if (lp->ll_empty2)
2235 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2236
Bram Moolenaar68452172021-04-12 21:21:02 +02002237 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2238 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002239 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002240 }
2241 else
2242 {
2243 val = (int)tv_get_number_chk(rettv, &error);
2244 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002245 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002246 }
2247 }
2248 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002250 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002252 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2253 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002254 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002255 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002256 *endp = cc;
2257 return;
2258 }
2259
Bram Moolenaarff697e62019-02-12 22:28:33 +01002260 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002261 di = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01002262 if (eval_variable(lp->ll_name, (int)(lp->ll_name_end - lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002263 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002264 {
Ernie Raele75fde62023-12-21 17:18:54 +01002265 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002266 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002267 clear_tv(&tv);
2268 return;
2269 }
2270
Bram Moolenaar79518e22017-02-17 16:31:35 +01002271 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002272 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2273 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002274 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002275 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002276 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002277 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002280 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002281 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002282 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2283 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002284 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002285 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002286 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002287 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002288 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002290 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002291 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002292 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002293 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 else if (lp->ll_range)
2295 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002296 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2297 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002298 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002299 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002300 return;
2301 }
2302
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002303 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2304 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 }
2306 else
2307 {
2308 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002309 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002311 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2312 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002313 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002314 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002315 return;
2316 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002317
2318 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002319 && check_typval_arg_type(lp->ll_valtype, rettv,
2320 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002321 return;
2322
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (lp->ll_newkey != NULL)
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && *op != '=')
2326 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002327 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 return;
2329 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002330 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2331 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002332 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002334 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002335 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (di == NULL)
2337 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002338 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2339 {
2340 vim_free(di);
2341 return;
2342 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002344 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 else if (op != NULL && *op != '=')
2346 {
2347 tv_op(lp->ll_tv, rettv, op);
2348 return;
2349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 /*
2354 * Assign the value to the variable or list item.
2355 */
2356 if (copy)
2357 copy_tv(rettv, lp->ll_tv);
2358 else
2359 {
2360 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002361 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 }
2364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365}
2366
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002367/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002368 * Handle "blob1 += blob2".
2369 * Returns OK or FAIL.
2370 */
2371 static int
2372tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2373{
2374 if (*op != '+' || tv2->v_type != VAR_BLOB)
2375 return FAIL;
2376
2377 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002378 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002379 return OK;
2380
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002381 if (tv1->vval.v_blob == NULL)
2382 {
2383 tv1->vval.v_blob = tv2->vval.v_blob;
2384 ++tv1->vval.v_blob->bv_refcount;
2385 return OK;
2386 }
2387
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002388 blob_T *b1 = tv1->vval.v_blob;
2389 blob_T *b2 = tv2->vval.v_blob;
2390 int len = blob_len(b2);
2391
2392 for (int i = 0; i < len; i++)
2393 ga_append(&b1->bv_ga, blob_get(b2, i));
2394
2395 return OK;
2396}
2397
2398/*
2399 * Handle "list1 += list2".
2400 * Returns OK or FAIL.
2401 */
2402 static int
2403tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2404{
2405 if (*op != '+' || tv2->v_type != VAR_LIST)
2406 return FAIL;
2407
2408 // List += List
2409 if (tv2->vval.v_list == NULL)
2410 return OK;
2411
2412 if (tv1->vval.v_list == NULL)
2413 {
2414 tv1->vval.v_list = tv2->vval.v_list;
2415 ++tv1->vval.v_list->lv_refcount;
2416 }
2417 else
2418 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2419
2420 return OK;
2421}
2422
2423/*
2424 * Handle number operations:
2425 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2426 *
2427 * Returns OK or FAIL.
2428 */
2429 static int
2430tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2431{
2432 varnumber_T n;
2433 int failed = FALSE;
2434
2435 n = tv_get_number(tv1);
2436 if (tv2->v_type == VAR_FLOAT)
2437 {
2438 float_T f = n;
2439
2440 if (*op == '%')
2441 return FAIL;
2442 switch (*op)
2443 {
2444 case '+': f += tv2->vval.v_float; break;
2445 case '-': f -= tv2->vval.v_float; break;
2446 case '*': f *= tv2->vval.v_float; break;
2447 case '/': f /= tv2->vval.v_float; break;
2448 }
2449 clear_tv(tv1);
2450 tv1->v_type = VAR_FLOAT;
2451 tv1->vval.v_float = f;
2452 }
2453 else
2454 {
2455 switch (*op)
2456 {
2457 case '+': n += tv_get_number(tv2); break;
2458 case '-': n -= tv_get_number(tv2); break;
2459 case '*': n *= tv_get_number(tv2); break;
2460 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2461 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2462 }
2463 clear_tv(tv1);
2464 tv1->v_type = VAR_NUMBER;
2465 tv1->vval.v_number = n;
2466 }
2467
2468 return failed ? FAIL : OK;
2469}
2470
2471/*
2472 * Handle "str1 .= str2"
2473 * Returns OK or FAIL.
2474 */
2475 static int
2476tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2477{
2478 char_u numbuf[NUMBUFLEN];
2479 char_u *s;
2480
2481 if (tv2->v_type == VAR_FLOAT)
2482 return FAIL;
2483
2484 // str .= str
2485 s = tv_get_string(tv1);
2486 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2487 clear_tv(tv1);
2488 tv1->v_type = VAR_STRING;
2489 tv1->vval.v_string = s;
2490
2491 return OK;
2492}
2493
2494/*
2495 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2496 * and "tv1 .= tv2"
2497 * Returns OK or FAIL.
2498 */
2499 static int
2500tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2501{
2502 if (tv2->v_type == VAR_LIST)
2503 return FAIL;
2504
2505 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2506 return tv_op_number(tv1, tv2, op);
2507
2508 return tv_op_string(tv1, tv2, op);
2509}
2510
2511/*
2512 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2513 * Returns OK or FAIL.
2514 */
2515 static int
2516tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2517{
2518 float_T f;
2519
2520 if (*op == '%' || *op == '.'
2521 || (tv2->v_type != VAR_FLOAT
2522 && tv2->v_type != VAR_NUMBER
2523 && tv2->v_type != VAR_STRING))
2524 return FAIL;
2525
2526 if (tv2->v_type == VAR_FLOAT)
2527 f = tv2->vval.v_float;
2528 else
2529 f = tv_get_number(tv2);
2530 switch (*op)
2531 {
2532 case '+': tv1->vval.v_float += f; break;
2533 case '-': tv1->vval.v_float -= f; break;
2534 case '*': tv1->vval.v_float *= f; break;
2535 case '/': tv1->vval.v_float /= f; break;
2536 }
2537
2538 return OK;
2539}
2540
2541/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002542 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2543 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 * Returns OK or FAIL.
2545 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002549 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002550 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002551 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2552 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2553 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002555 semsg(_(e_wrong_variable_type_for_str_equal), op);
2556 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 }
2558
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002559 int retval = FAIL;
2560 switch (tv1->v_type)
2561 {
2562 case VAR_UNKNOWN:
2563 case VAR_ANY:
2564 case VAR_VOID:
2565 case VAR_DICT:
2566 case VAR_FUNC:
2567 case VAR_PARTIAL:
2568 case VAR_BOOL:
2569 case VAR_SPECIAL:
2570 case VAR_JOB:
2571 case VAR_CHANNEL:
2572 case VAR_INSTR:
2573 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002574 case VAR_CLASS:
2575 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002576 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002577
2578 case VAR_BLOB:
2579 retval = tv_op_blob(tv1, tv2, op);
2580 break;
2581
2582 case VAR_LIST:
2583 retval = tv_op_list(tv1, tv2, op);
2584 break;
2585
2586 case VAR_NUMBER:
2587 case VAR_STRING:
2588 retval = tv_op_nr_or_string(tv1, tv2, op);
2589 break;
2590
2591 case VAR_FLOAT:
2592 retval = tv_op_float(tv1, tv2, op);
2593 break;
2594 }
2595
2596 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002597 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002598
2599 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002600}
2601
2602/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603 * Evaluate the expression used in a ":for var in expr" command.
2604 * "arg" points to "var".
2605 * Set "*errp" to TRUE for an error, FALSE otherwise;
2606 * Return a pointer that holds the info. Null when there is an error.
2607 */
2608 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002609eval_for_line(
2610 char_u *arg,
2611 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002612 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002613 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614{
Bram Moolenaar33570922005-01-25 22:26:29 +00002615 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002616 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002617 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002618 typval_T tv;
2619 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002620 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002622 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002623
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002624 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002625 if (fi == NULL)
2626 return NULL;
2627
Bram Moolenaar404557e2021-07-05 21:41:48 +02002628 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2629 &fi->fi_semicolon, FALSE);
2630 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002631 return fi;
2632
Bram Moolenaar404557e2021-07-05 21:41:48 +02002633 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002634 if (expr[0] != 'i' || expr[1] != 'n'
2635 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002636 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002637 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2638 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2639 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002640 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641 return fi;
2642 }
2643
2644 if (skip)
2645 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002646 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2647 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648 {
2649 *errp = FALSE;
2650 if (!skip)
2651 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002652 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002654 l = tv.vval.v_list;
2655 if (l == NULL)
2656 {
2657 // a null list is like an empty list: do nothing
2658 clear_tv(&tv);
2659 }
2660 else
2661 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002663 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002665 // No need to increment the refcount, it's already set for
2666 // the list being used in "tv".
2667 fi->fi_list = l;
2668 list_add_watch(l, &fi->fi_lw);
2669 fi->fi_lw.lw_item = l->lv_first;
2670 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002671 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002672 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002673 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002674 fi->fi_bi = 0;
2675 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002676 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002677 typval_T btv;
2678
2679 // Make a copy, so that the iteration still works when the
2680 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002682 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002683 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002684 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002685 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002686 else if (tv.v_type == VAR_STRING)
2687 {
2688 fi->fi_byte_idx = 0;
2689 fi->fi_string = tv.vval.v_string;
2690 tv.vval.v_string = NULL;
2691 if (fi->fi_string == NULL)
2692 fi->fi_string = vim_strsave((char_u *)"");
2693 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002694 else
2695 {
Sean Dewar80d73952021-08-04 19:25:54 +02002696 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002697 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002698 }
2699 }
2700 }
2701 if (skip)
2702 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002703 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704
2705 return fi;
2706}
2707
2708/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002709 * Used when looping over a :for line, skip the "in expr" part.
2710 */
2711 void
2712skip_for_lines(void *fi_void, evalarg_T *evalarg)
2713{
2714 forinfo_T *fi = (forinfo_T *)fi_void;
2715 int i;
2716
2717 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002718 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002719}
2720
2721/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722 * Use the first item in a ":for" list. Advance to the next.
2723 * Assign the values to the variable (list). "arg" points to the first one.
2724 * Return TRUE when a valid item was found, FALSE when at end of list or
2725 * something wrong.
2726 */
2727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002728next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002730 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002732 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002733 ? (ASSIGN_FINAL
2734 // first round: error if variable exists
2735 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002736 | ASSIGN_NO_MEMBER_TYPE
2737 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002738 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002739 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002740 int skip_assign = in_vim9script() && arg[0] == '_'
2741 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002743 if (fi->fi_blob != NULL)
2744 {
2745 typval_T tv;
2746
2747 if (fi->fi_bi >= blob_len(fi->fi_blob))
2748 return FALSE;
2749 tv.v_type = VAR_NUMBER;
2750 tv.v_lock = VAR_FIXED;
2751 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2752 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002753 if (skip_assign)
2754 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002755 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002756 fi->fi_varcount, flag, NULL) == OK;
2757 }
2758
2759 if (fi->fi_string != NULL)
2760 {
2761 typval_T tv;
2762 int len;
2763
2764 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2765 if (len == 0)
2766 return FALSE;
2767 tv.v_type = VAR_STRING;
2768 tv.v_lock = VAR_FIXED;
2769 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2770 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002771 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002772 if (skip_assign)
2773 result = TRUE;
2774 else
2775 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002776 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002777 vim_free(tv.vval.v_string);
2778 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002779 }
2780
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781 item = fi->fi_lw.lw_item;
2782 if (item == NULL)
2783 result = FALSE;
2784 else
2785 {
2786 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002787 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002788 if (skip_assign)
2789 result = TRUE;
2790 else
2791 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002792 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793 }
2794 return result;
2795}
2796
2797/*
2798 * Free the structure used to store info used by ":for".
2799 */
2800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002801free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802{
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002805 if (fi == NULL)
2806 return;
2807 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002808 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002810 list_unref(fi->fi_list);
2811 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002812 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002813 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002814 else
2815 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 vim_free(fi);
2817}
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820set_context_for_expression(
2821 expand_T *xp,
2822 char_u *arg,
2823 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002825 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002829 if (cmdidx == CMD_let || cmdidx == CMD_var
2830 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 {
2832 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002833 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002836 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002837 {
2838 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002839 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002840 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 break;
2842 }
2843 return;
2844 }
2845 }
2846 else
2847 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2848 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 while ((xp->xp_pattern = vim_strpbrk(arg,
2850 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2851 {
2852 c = *xp->xp_pattern;
2853 if (c == '&')
2854 {
2855 c = xp->xp_pattern[1];
2856 if (c == '&')
2857 {
2858 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002859 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002864 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2865 xp->xp_pattern += 2;
2866
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 else if (c == '$')
2870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 xp->xp_context = EXPAND_ENV_VARS;
2873 }
2874 else if (c == '=')
2875 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002876 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 xp->xp_context = EXPAND_EXPRESSION;
2878 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002879 else if (c == '#'
2880 && xp->xp_context == EXPAND_EXPRESSION)
2881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002882 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002883 break;
2884 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002885 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 && xp->xp_context == EXPAND_FUNCTIONS
2887 && vim_strchr(xp->xp_pattern, '(') == NULL)
2888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 break;
2891 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002892 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002894 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2897 if (c == '\\' && xp->xp_pattern[1] != NUL)
2898 ++xp->xp_pattern;
2899 xp->xp_context = EXPAND_NOTHING;
2900 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002901 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002903 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2905 /* skip */ ;
2906 xp->xp_context = EXPAND_NOTHING;
2907 }
2908 else if (c == '|')
2909 {
2910 if (xp->xp_pattern[1] == '|')
2911 {
2912 ++xp->xp_pattern;
2913 xp->xp_context = EXPAND_EXPRESSION;
2914 }
2915 else
2916 xp->xp_context = EXPAND_COMMANDS;
2917 }
2918 else
2919 xp->xp_context = EXPAND_EXPRESSION;
2920 }
2921 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 // Doesn't look like something valid, expand as an expression
2923 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 arg = xp->xp_pattern;
2926 if (*arg != NUL)
2927 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2928 /* skip */ ;
2929 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002930
2931 // ":exe one two" completes "two"
2932 if ((cmdidx == CMD_execute
2933 || cmdidx == CMD_echo
2934 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002935 || cmdidx == CMD_echomsg
2936 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002937 && xp->xp_context == EXPAND_EXPRESSION)
2938 {
2939 for (;;)
2940 {
2941 char_u *n = skiptowhite(arg);
2942
2943 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2944 break;
2945 arg = skipwhite(n);
2946 }
2947 }
2948
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 xp->xp_pattern = arg;
2950}
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002953 * Return TRUE if "pat" matches "text".
2954 * Does not use 'cpo' and always uses 'magic'.
2955 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002956 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002957pattern_match(char_u *pat, char_u *text, int ic)
2958{
2959 int matches = FALSE;
2960 char_u *save_cpo;
2961 regmatch_T regmatch;
2962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002963 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002964 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002965 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002966 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2967 if (regmatch.regprog != NULL)
2968 {
2969 regmatch.rm_ic = ic;
2970 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2971 vim_regfree(regmatch.regprog);
2972 }
2973 p_cpo = save_cpo;
2974 return matches;
2975}
2976
2977/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002978 * Handle a name followed by "(". Both for just "name(arg)" and for
2979 * "expr->name(arg)".
2980 * Returns OK or FAIL.
2981 */
2982 static int
2983eval_func(
2984 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002985 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002986 char_u *name,
2987 int name_len,
2988 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002989 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002990 typval_T *basetv) // "expr" for "expr->name(arg)"
2991{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002992 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002993 char_u *s = name;
2994 int len = name_len;
2995 partial_T *partial;
2996 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002997 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002998 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002999
3000 if (!evaluate)
3001 check_vars(s, len);
3002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003003 // If "s" is the name of a variable of type VAR_FUNC
3004 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003005 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003006 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003008 // Need to make a copy, in case evaluating the arguments makes
3009 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003010 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01003011 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003012 ret = FAIL;
3013 else
3014 {
3015 funcexe_T funcexe;
3016
3017 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02003018 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003019 funcexe.fe_firstline = curwin->w_cursor.lnum;
3020 funcexe.fe_lastline = curwin->w_cursor.lnum;
3021 funcexe.fe_evaluate = evaluate;
3022 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003023 if (partial != NULL)
3024 {
3025 funcexe.fe_object = partial->pt_obj;
3026 if (funcexe.fe_object != NULL)
3027 ++funcexe.fe_object->obj_refcount;
3028 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003029 funcexe.fe_basetv = basetv;
3030 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003031 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003032 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003033 }
3034 vim_free(s);
3035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003036 // If evaluate is FALSE rettv->v_type was not set in
3037 // get_func_tv, but it's needed in handle_subscript() to parse
3038 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003039 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3040 {
3041 rettv->vval.v_string = NULL;
3042 rettv->v_type = VAR_FUNC;
3043 }
3044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003045 // Stop the expression evaluation when immediately
3046 // aborting on error, or when an interrupt occurred or
3047 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003048 if (evaluate && aborting())
3049 {
3050 if (ret == OK)
3051 clear_tv(rettv);
3052 ret = FAIL;
3053 }
3054 return ret;
3055}
3056
3057/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003058 * After a NL, skip over empty lines and comment-only lines.
3059 */
3060 static char_u *
3061newline_skip_comments(char_u *arg)
3062{
3063 char_u *p = arg + 1;
3064
3065 for (;;)
3066 {
3067 p = skipwhite(p);
3068
3069 if (*p == NUL)
3070 break;
3071 if (vim9_comment_start(p))
3072 {
3073 char_u *nl = vim_strchr(p, NL);
3074
3075 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003076 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003077 p = nl;
3078 }
3079 if (*p != NL)
3080 break;
3081 ++p; // skip another NL
3082 }
3083 return p;
3084}
3085
3086/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003087 * Get the next line source line without advancing. But do skip over comment
3088 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003089 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003090 */
3091 static char_u *
3092getline_peek_skip_comments(evalarg_T *evalarg)
3093{
3094 for (;;)
3095 {
3096 char_u *next = getline_peek(evalarg->eval_getline,
3097 evalarg->eval_cookie);
3098 char_u *p;
3099
3100 if (next == NULL)
3101 break;
3102 p = skipwhite(next);
3103 if (*p != NUL && !vim9_comment_start(p))
3104 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003105 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003106 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003107 }
3108 return NULL;
3109}
3110
3111/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003112 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3113 * comment) and there is a next line, return the next line (skipping blanks)
3114 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003115 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3116 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003117 * "arg" must point somewhere inside a line, not at the start.
3118 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003119 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3121{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003122 char_u *p = skipwhite(arg);
3123
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003124 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003125 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003127 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3128 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003129 && (*p == NUL || *p == NL
3130 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003132 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003133
Bram Moolenaare442d592022-05-05 12:20:28 +01003134 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003135 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003136 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003137 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003138 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003139 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140
Bram Moolenaar9d489562020-07-30 20:08:50 +02003141 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003143 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003144 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 }
3146 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003147 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148}
3149
3150/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003151 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003152 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003153 *
3154 * If "arg" is not NULL, then the caller should assign the return value to
3155 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003156 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003157 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003158eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003159{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003160 garray_T *gap = &evalarg->eval_ga;
3161 char_u *line;
3162
Bram Moolenaar39be4982022-05-06 21:51:50 +01003163 if (arg != NULL)
3164 {
3165 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003166 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003167 // Truncate before a trailing comment, so that concatenating the lines
3168 // won't turn the rest into a comment.
3169 if (*skipwhite(arg) == '#')
3170 *arg = NUL;
3171 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003172
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003173 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003174 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3175 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003176 else
3177 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003178 if (line == NULL)
3179 return NULL;
3180
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003181 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003182 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3183 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003184 char_u *p = skipwhite(line);
3185
3186 // Going to concatenate the lines after parsing. For an empty or
3187 // comment line use an empty string.
3188 if (*p == NUL || vim9_comment_start(p))
3189 {
3190 vim_free(line);
3191 line = vim_strsave((char_u *)"");
3192 }
3193
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003194 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3195 ++gap->ga_len;
3196 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003197 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003198 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003199 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003200 evalarg->eval_tofree = line;
3201 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003202
3203 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003204 // line. The caller assigns the return value to "arg".
3205 // If "arg" is NULL, then the return value is discarded. In that case,
3206 // "arg" still points to the previous line. So don't reset
3207 // "eval_using_cmdline".
3208 if (arg != NULL)
3209 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003210 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003211}
3212
Bram Moolenaare6b53242020-07-01 17:28:33 +02003213/*
3214 * Call eval_next_non_blank() and get the next line if needed.
3215 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003216 char_u *
3217skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3218{
3219 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003220 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003221
Bram Moolenaar962d7212020-07-04 14:15:00 +02003222 if (evalarg == NULL)
3223 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003224 eval_next_non_blank(p, evalarg, &getnext);
3225 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003226 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003227 return p;
3228}
3229
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003230/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003231 * The "eval" functions have an "evalarg" argument: When NULL or
3232 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3233 * parsed but not executed. The functions may return OK, but the rettv will be
3234 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
3236
3237/*
3238 * Handle zero level expression.
3239 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003241 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003242 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * Return OK or FAIL.
3244 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246eval0(
3247 char_u *arg,
3248 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003249 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003250 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003252 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3253}
3254
3255/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003256 * If "arg" is a simple function call without arguments then call it and return
3257 * the result. Otherwise return NOTDONE.
3258 */
3259 int
3260may_call_simple_func(
3261 char_u *arg,
3262 typval_T *rettv)
3263{
3264 char_u *parens = (char_u *)strstr((char *)arg, "()");
3265 int r = NOTDONE;
3266
3267 // If the expression is "FuncName()" then we can skip a lot of overhead.
3268 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3269 {
3270 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3271
3272 if (to_name_end(p, TRUE) == parens)
zeertzjqc1ed7882024-08-01 22:46:54 +02003273 r = call_simple_func(arg, (size_t)(parens - arg), rettv);
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003274 }
3275 return r;
3276}
3277
3278/*
3279 * Handle zero level expression with optimization for a simple function call.
3280 * Same arguments and return value as eval0().
3281 */
3282 int
3283eval0_simple_funccal(
3284 char_u *arg,
3285 typval_T *rettv,
3286 exarg_T *eap,
3287 evalarg_T *evalarg)
3288{
3289 int r = may_call_simple_func(arg, rettv);
3290
3291 if (r == NOTDONE)
3292 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3293 return r;
3294}
3295
3296/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003297 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3298 * expression and don't check what comes after the expression.
3299 */
3300 int
3301eval0_retarg(
3302 char_u *arg,
3303 typval_T *rettv,
3304 exarg_T *eap,
3305 evalarg_T *evalarg,
3306 char_u **retarg)
3307{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 int ret;
3309 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003310 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003311 int did_emsg_before = did_emsg;
3312 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003313 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003314 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003317 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003318
Bram Moolenaar79481362022-06-27 20:15:10 +01003319 if (ret != FAIL)
3320 {
3321 expr_end = p;
3322 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003323
Bram Moolenaar79481362022-06-27 20:15:10 +01003324 // In Vim9 script a command block is not split at NL characters for
3325 // commands using an expression argument. Skip over a '#' comment to
3326 // check for a following NL. Require white space before the '#'.
3327 if (in_vim9script() && p > expr_end && retarg == NULL)
3328 while (*p == '#')
3329 {
3330 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003331
Bram Moolenaar79481362022-06-27 20:15:10 +01003332 if (nl == NULL)
3333 break;
3334 p = skipwhite(nl + 1);
3335 if (eap != NULL && *p != NUL)
3336 eap->nextcmd = p;
3337 check_for_end = FALSE;
3338 }
3339
3340 if (check_for_end)
3341 end_error = !ends_excmd2(arg, p);
3342 }
3343
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003344 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 /*
3349 * Report the invalid expression unless the expression evaluation has
3350 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003351 * exception, or we already gave a more specific error.
3352 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003354 if (!aborting()
3355 && did_emsg == did_emsg_before
3356 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003357 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003358 {
3359 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003360 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003361 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003362 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003363 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003364
zeertzjqf2588b62023-05-05 17:22:35 +01003365 if (eap != NULL && p != NULL)
3366 {
3367 // Some of the expression may not have been consumed.
3368 // Only execute a next command if it cannot be a "||" operator.
3369 // The next command may be "catch".
3370 char_u *nextcmd = check_nextcmd(p);
3371 if (nextcmd != NULL && *nextcmd != '|')
3372 eap->nextcmd = nextcmd;
3373 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003376
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003377 if (retarg != NULL)
3378 *retarg = p;
3379 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003380 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return ret;
3383}
3384
3385/*
3386 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003387 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003388 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 *
3390 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003391 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003393 * Note: "rettv.v_lock" is not set.
3394 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 * Return OK or FAIL.
3396 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003397 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003398eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003400 char_u *p;
3401 int getnext;
3402
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003403 CLEAR_POINTER(rettv);
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 /*
3406 * Get the first variable.
3407 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003408 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 return FAIL;
3410
Bram Moolenaar793648f2020-06-26 21:28:25 +02003411 p = eval_next_non_blank(*arg, evalarg, &getnext);
3412 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003414 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003415 int result;
3416 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003417 evalarg_T *evalarg_used = evalarg;
3418 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003419 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003420 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003421 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003422
3423 if (evalarg == NULL)
3424 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003425 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003426 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003427 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003428 orig_flags = evalarg_used->eval_flags;
3429 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003430
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003431 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003432 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003433 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003434 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003435 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003436 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003437 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003438 clear_tv(rettv);
3439 return FAIL;
3440 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003441 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003442 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003445 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 int error = FALSE;
3448
Bram Moolenaar13106602020-10-04 16:06:05 +02003449 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003450 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003451 else if (vim9script)
3452 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003453 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003455 if (error || !op_falsy || !result)
3456 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003457 if (error)
3458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460
3461 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003462 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003464 if (op_falsy)
3465 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003466 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003467 {
mityu4ac198c2021-05-28 17:52:40 +02003468 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003469 clear_tv(rettv);
3470 return FAIL;
3471 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003472 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003473 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003474 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003475 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003476 {
3477 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003479 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003480 if (!op_falsy || !result)
3481 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003483 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003485 /*
3486 * Check for the ":".
3487 */
3488 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3489 if (*p != ':')
3490 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003491 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003492 if (evaluate && result)
3493 clear_tv(rettv);
3494 evalarg_used->eval_flags = orig_flags;
3495 return FAIL;
3496 }
3497 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003498 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003499 else
3500 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003501 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003502 {
3503 error_white_both(p, 1);
3504 clear_tv(rettv);
3505 evalarg_used->eval_flags = orig_flags;
3506 return FAIL;
3507 }
3508 *arg = p;
3509 }
3510
3511 /*
3512 * Get the third variable. Recursive!
3513 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003514 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003515 {
mityu4ac198c2021-05-28 17:52:40 +02003516 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003517 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003518 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003519 return FAIL;
3520 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003521 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3522 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003523 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003524 if (eval1(arg, &var2, evalarg_used) == FAIL)
3525 {
3526 if (evaluate && result)
3527 clear_tv(rettv);
3528 evalarg_used->eval_flags = orig_flags;
3529 return FAIL;
3530 }
3531 if (evaluate && !result)
3532 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003534
3535 if (evalarg == NULL)
3536 clear_evalarg(&local_evalarg, NULL);
3537 else
3538 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 return OK;
3542}
3543
3544/*
3545 * Handle first level expression:
3546 * expr2 || expr2 || expr2 logical OR
3547 *
3548 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003549 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 *
3551 * Return OK or FAIL.
3552 */
3553 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003554eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003556 char_u *p;
3557 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003560 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003562 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return FAIL;
3564
3565 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003566 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003568 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003569 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003571 evalarg_T *evalarg_used = evalarg;
3572 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003573 int evaluate;
3574 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003575 long result = FALSE;
3576 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003577 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003578 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003579
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003580 if (evalarg == NULL)
3581 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003582 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003583 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003585 orig_flags = evalarg_used->eval_flags;
3586 evaluate = orig_flags & EVAL_EVALUATE;
3587 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003588 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003589 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003590 result = tv_get_bool_chk(rettv, &error);
3591 else if (tv_get_number_chk(rettv, &error) != 0)
3592 result = TRUE;
3593 clear_tv(rettv);
3594 if (error)
3595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597
3598 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003599 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003601 while (p[0] == '|' && p[1] == '|')
3602 {
3603 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003604 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003605 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003606 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003607 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003608 {
3609 error_white_both(p, 2);
3610 clear_tv(rettv);
3611 return FAIL;
3612 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003613 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003614 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003615
3616 /*
3617 * Get the second variable.
3618 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003619 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003620 {
mityu4ac198c2021-05-28 17:52:40 +02003621 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003622 clear_tv(rettv);
3623 return FAIL;
3624 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003625 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3626 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003627 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003628 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003630
3631 /*
3632 * Compute the result.
3633 */
3634 if (evaluate && !result)
3635 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003636 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003637 result = tv_get_bool_chk(&var2, &error);
3638 else if (tv_get_number_chk(&var2, &error) != 0)
3639 result = TRUE;
3640 clear_tv(&var2);
3641 if (error)
3642 return FAIL;
3643 }
3644 if (evaluate)
3645 {
3646 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003647 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003648 rettv->v_type = VAR_BOOL;
3649 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003650 }
3651 else
3652 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003653 rettv->v_type = VAR_NUMBER;
3654 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003657
3658 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003660
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003661 if (evalarg == NULL)
3662 clear_evalarg(&local_evalarg, NULL);
3663 else
3664 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666
3667 return OK;
3668}
3669
3670/*
3671 * Handle second level expression:
3672 * expr3 && expr3 && expr3 logical AND
3673 *
3674 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003675 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 *
3677 * Return OK or FAIL.
3678 */
3679 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003680eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003682 char_u *p;
3683 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003686 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003688 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return FAIL;
3690
3691 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003692 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003694 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003695 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003697 evalarg_T *evalarg_used = evalarg;
3698 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003699 int orig_flags;
3700 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003701 long result = TRUE;
3702 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003703 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003704 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003705
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003706 if (evalarg == NULL)
3707 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003708 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003709 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003710 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003711 orig_flags = evalarg_used->eval_flags;
3712 evaluate = orig_flags & EVAL_EVALUATE;
3713 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003714 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003715 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003716 result = tv_get_bool_chk(rettv, &error);
3717 else if (tv_get_number_chk(rettv, &error) == 0)
3718 result = FALSE;
3719 clear_tv(rettv);
3720 if (error)
3721 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723
3724 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003725 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003727 while (p[0] == '&' && p[1] == '&')
3728 {
3729 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003730 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003731 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003732 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003733 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003734 {
3735 error_white_both(p, 2);
3736 clear_tv(rettv);
3737 return FAIL;
3738 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003739 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003740 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003741
3742 /*
3743 * Get the second variable.
3744 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003745 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003746 {
mityu4ac198c2021-05-28 17:52:40 +02003747 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003748 clear_tv(rettv);
3749 return FAIL;
3750 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003751 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3752 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003753 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003754 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003755 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003757
3758 /*
3759 * Compute the result.
3760 */
3761 if (evaluate && result)
3762 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003763 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003764 result = tv_get_bool_chk(&var2, &error);
3765 else if (tv_get_number_chk(&var2, &error) == 0)
3766 result = FALSE;
3767 clear_tv(&var2);
3768 if (error)
3769 return FAIL;
3770 }
3771 if (evaluate)
3772 {
3773 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003774 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003775 rettv->v_type = VAR_BOOL;
3776 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003777 }
3778 else
3779 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003780 rettv->v_type = VAR_NUMBER;
3781 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003782 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003783 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003784
3785 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003787
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003788 if (evalarg == NULL)
3789 clear_evalarg(&local_evalarg, NULL);
3790 else
3791 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794 return OK;
3795}
3796
3797/*
3798 * Handle third level expression:
3799 * var1 == var2
3800 * var1 =~ var2
3801 * var1 != var2
3802 * var1 !~ var2
3803 * var1 > var2
3804 * var1 >= var2
3805 * var1 < var2
3806 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003807 * var1 is var2
3808 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 *
3810 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003811 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 *
3813 * Return OK or FAIL.
3814 */
3815 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003816eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003819 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003820 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003822 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
3824 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003825 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003827 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003830 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003831
Bram Moolenaar696ba232020-07-29 21:20:41 +02003832 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003835 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003837 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003839 typval_T var2;
3840 int ic;
3841 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003842 int evaluate = evalarg == NULL
3843 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003844 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003845
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003846 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003847 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003848 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003849 p = *arg;
3850 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003851 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3852 {
mityu4ac198c2021-05-28 17:52:40 +02003853 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003854 clear_tv(rettv);
3855 return FAIL;
3856 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003857
Bram Moolenaar696ba232020-07-29 21:20:41 +02003858 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3859 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003860 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003861 clear_tv(rettv);
3862 return FAIL;
3863 }
3864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003865 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (p[len] == '?')
3867 {
3868 ic = TRUE;
3869 ++len;
3870 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 else if (p[len] == '#')
3873 {
3874 ic = FALSE;
3875 ++len;
3876 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003877 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003879 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 /*
3882 * Get the second variable.
3883 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003884 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3885 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003886 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003887 clear_tv(rettv);
3888 return FAIL;
3889 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003890 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003891 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return FAIL;
3895 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003896 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003897 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003898 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003899
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003900 // use the line of the comparison for messages
3901 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003902 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003903 {
3904 ret = FAIL;
3905 clear_tv(rettv);
3906 }
3907 else
3908 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003909 clear_tv(&var2);
3910 return ret;
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913
3914 return OK;
3915}
3916
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003917/*
3918 * Make a copy of blob "tv1" and append blob "tv2".
3919 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 void
3921eval_addblob(typval_T *tv1, typval_T *tv2)
3922{
3923 blob_T *b1 = tv1->vval.v_blob;
3924 blob_T *b2 = tv2->vval.v_blob;
3925 blob_T *b = blob_alloc();
3926 int i;
3927
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003928 if (b == NULL)
3929 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003931 for (i = 0; i < blob_len(b1); i++)
3932 ga_append(&b->bv_ga, blob_get(b1, i));
3933 for (i = 0; i < blob_len(b2); i++)
3934 ga_append(&b->bv_ga, blob_get(b2, i));
3935
3936 clear_tv(tv1);
3937 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938}
3939
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003940/*
3941 * Make a copy of list "tv1" and append list "tv2".
3942 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 int
3944eval_addlist(typval_T *tv1, typval_T *tv2)
3945{
3946 typval_T var3;
3947
3948 // concatenate Lists
3949 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3950 {
3951 clear_tv(tv1);
3952 clear_tv(tv2);
3953 return FAIL;
3954 }
3955 clear_tv(tv1);
3956 *tv1 = var3;
3957 return OK;
3958}
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02003961 * Left or right shift the number "tv1" by the number "tv2" and store the
3962 * result in "tv1".
3963 *
3964 * Return OK or FAIL.
3965 */
3966 static int
3967eval_shift_number(typval_T *tv1, typval_T *tv2, int shift_type)
3968{
3969 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3970 {
3971 // right operand should be a positive number
3972 if (tv2->v_type != VAR_NUMBER)
3973 emsg(_(e_bitshift_ops_must_be_number));
3974 else
3975 emsg(_(e_bitshift_ops_must_be_positive));
3976 clear_tv(tv1);
3977 clear_tv(tv2);
3978 return FAIL;
3979 }
3980
3981 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3982 // shifting more bits than we have always results in zero
3983 tv1->vval.v_number = 0;
3984 else if (shift_type == EXPR_LSHIFT)
3985 tv1->vval.v_number =
3986 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
3987 else
3988 tv1->vval.v_number =
3989 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
3990
3991 return OK;
3992}
3993
3994/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003995 * Handle the bitwise left/right shift operator expression:
3996 * var1 << var2
3997 * var1 >> var2
3998 *
3999 * "arg" must point to the first non-white of the expression.
4000 * "arg" is advanced to just after the recognized expression.
4001 *
4002 * Return OK or FAIL.
4003 */
4004 static int
4005eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
4006{
4007 /*
4008 * Get the first expression.
4009 */
4010 if (eval6(arg, rettv, evalarg) == FAIL)
4011 return FAIL;
4012
4013 /*
4014 * Repeat computing, until no '<<' or '>>' is following.
4015 */
4016 for (;;)
4017 {
4018 char_u *p;
4019 int getnext;
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004020 exprtype_T exprtype;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004021 int evaluate;
4022 typval_T var2;
4023 int vim9script;
4024
4025 p = eval_next_non_blank(*arg, evalarg, &getnext);
4026 if (p[0] == '<' && p[1] == '<')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004027 exprtype = EXPR_LSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004028 else if (p[0] == '>' && p[1] == '>')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004029 exprtype = EXPR_RSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004030 else
4031 return OK;
4032
4033 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004034 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
4035 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004036 {
4037 // left operand should be a number
4038 emsg(_(e_bitshift_ops_must_be_number));
4039 clear_tv(rettv);
4040 return FAIL;
4041 }
4042
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004043 vim9script = in_vim9script();
4044 if (getnext)
4045 {
4046 *arg = eval_next_line(*arg, evalarg);
4047 p = *arg;
4048 }
4049 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4050 {
4051 error_white_both(*arg, 2);
4052 clear_tv(rettv);
4053 return FAIL;
4054 }
4055
4056 /*
4057 * Get the second variable.
4058 */
4059 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
4060 {
4061 error_white_both(p, 2);
4062 clear_tv(rettv);
4063 return FAIL;
4064 }
4065 *arg = skipwhite_and_linebreak(p + 2, evalarg);
4066 if (eval6(arg, &var2, evalarg) == FAIL)
4067 {
4068 clear_tv(rettv);
4069 return FAIL;
4070 }
4071
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004072 if (evaluate)
4073 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004074 if (eval_shift_number(rettv, &var2, exprtype) == FAIL)
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004075 return FAIL;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004076 }
4077
4078 clear_tv(&var2);
4079 }
4080
4081 return OK;
4082}
4083
4084/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004085 * Concatenate strings "tv1" and "tv2" and store the result in "tv1".
4086 */
4087 static int
4088eval_concat_str(typval_T *tv1, typval_T *tv2)
4089{
4090 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4091 char_u *s1 = tv_get_string_buf(tv1, buf1);
4092 char_u *s2 = NULL;
4093 char_u *p;
4094 int vim9script = in_vim9script();
4095
4096 if (vim9script && (tv2->v_type == VAR_VOID
4097 || tv2->v_type == VAR_CHANNEL
4098 || tv2->v_type == VAR_JOB))
4099 semsg(_(e_using_invalid_value_as_string_str),
4100 vartype_name(tv2->v_type));
4101 else if (vim9script && tv2->v_type == VAR_FLOAT)
4102 {
4103 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4104 tv2->vval.v_float);
4105 s2 = buf2;
4106 }
4107 else
4108 s2 = tv_get_string_buf_chk(tv2, buf2);
4109 if (s2 == NULL) // type error ?
4110 {
4111 clear_tv(tv1);
4112 clear_tv(tv2);
4113 return FAIL;
4114 }
4115
4116 p = concat_str(s1, s2);
4117 clear_tv(tv1);
4118 tv1->v_type = VAR_STRING;
4119 tv1->vval.v_string = p;
4120
4121 return OK;
4122}
4123
4124/*
4125 * Add or subtract numbers "tv1" and "tv2" and store the result in "tv1".
4126 * The numbers can be whole numbers or floats.
4127 */
4128 static int
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004129eval_addsub_number(typval_T *tv1, typval_T *tv2, int op)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004130{
4131 int error = FALSE;
4132 varnumber_T n1, n2;
4133 float_T f1 = 0, f2 = 0;
4134
4135 if (tv1->v_type == VAR_FLOAT)
4136 {
4137 f1 = tv1->vval.v_float;
4138 n1 = 0;
4139 }
4140 else
4141 {
4142 n1 = tv_get_number_chk(tv1, &error);
4143 if (error)
4144 {
4145 // This can only happen for "list + non-list" or
4146 // "blob + non-blob". For "non-list + ..." or
4147 // "something - ...", we returned before evaluating the
4148 // 2nd operand.
4149 clear_tv(tv1);
4150 clear_tv(tv2);
4151 return FAIL;
4152 }
4153 if (tv2->v_type == VAR_FLOAT)
4154 f1 = n1;
4155 }
4156 if (tv2->v_type == VAR_FLOAT)
4157 {
4158 f2 = tv2->vval.v_float;
4159 n2 = 0;
4160 }
4161 else
4162 {
4163 n2 = tv_get_number_chk(tv2, &error);
4164 if (error)
4165 {
4166 clear_tv(tv1);
4167 clear_tv(tv2);
4168 return FAIL;
4169 }
4170 if (tv1->v_type == VAR_FLOAT)
4171 f2 = n2;
4172 }
4173 clear_tv(tv1);
4174
4175 // If there is a float on either side the result is a float.
4176 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4177 {
4178 if (op == '+')
4179 f1 = f1 + f2;
4180 else
4181 f1 = f1 - f2;
4182 tv1->v_type = VAR_FLOAT;
4183 tv1->vval.v_float = f1;
4184 }
4185 else
4186 {
4187 if (op == '+')
4188 n1 = n1 + n2;
4189 else
4190 n1 = n1 - n2;
4191 tv1->v_type = VAR_NUMBER;
4192 tv1->vval.v_number = n1;
4193 }
4194
4195 return OK;
4196}
4197
4198/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004199 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004200 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004202 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004203 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 *
4205 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004206 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 *
4208 * Return OK or FAIL.
4209 */
4210 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004211eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004214 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004216 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218
4219 /*
4220 * Repeat computing, until no '+', '-' or '.' is following.
4221 */
4222 for (;;)
4223 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004224 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004225 int getnext;
4226 char_u *p;
4227 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004228 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004229 int concat;
4230 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004231 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004232
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004233 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004234 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004235 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004236 p = eval_next_non_blank(*arg, evalarg, &getnext);
4237 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004238 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004239 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4240 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004242 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4243 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004244
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004245 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004246 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004247 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004248 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004249 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004250 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004251 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004252 {
mityu4ac198c2021-05-28 17:52:40 +02004253 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004254 clear_tv(rettv);
4255 return FAIL;
4256 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004257 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004258 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004259 if ((op != '+' || (rettv->v_type != VAR_LIST
4260 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004261 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004262 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004264 int error = FALSE;
4265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 // For "list + ...", an illegal use of the first operand as
4267 // a number cannot be determined before evaluating the 2nd
4268 // operand: if this is also a list, all is ok.
4269 // For "something . ...", "something - ..." or "non-list + ...",
4270 // we know that the first operand needs to be a string or number
4271 // without evaluating the 2nd operand. So check before to avoid
4272 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004273 if (op != '.')
4274 tv_get_number_chk(rettv, &error);
4275 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 {
4277 clear_tv(rettv);
4278 return FAIL;
4279 }
4280 }
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 /*
4283 * Get the second variable.
4284 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004285 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004286 {
mityu89dcb4d2021-05-28 13:50:17 +02004287 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004288 clear_tv(rettv);
4289 return FAIL;
4290 }
4291 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004292 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return FAIL;
4296 }
4297
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004298 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
4300 /*
4301 * Compute the result.
4302 */
4303 if (op == '.')
4304 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004305 if (eval_concat_str(rettv, &var2) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004308 else if (op == '+' && rettv->v_type == VAR_BLOB
4309 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004311 else if (op == '+' && rettv->v_type == VAR_LIST
4312 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004315 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 else
4318 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004319 if (eval_addsub_number(rettv, &var2, op) == FAIL)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 }
4325 return OK;
4326}
4327
4328/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004329 * Multiply or divide or compute the modulo of numbers "tv1" and "tv2" and
4330 * store the result in "tv1". The numbers can be whole numbers or floats.
4331 */
4332 static int
4333eval_multdiv_number(typval_T *tv1, typval_T *tv2, int op)
4334{
4335 varnumber_T n1, n2;
4336 float_T f1, f2;
4337 int error;
4338 int use_float = FALSE;
4339
4340 f1 = 0;
4341 f2 = 0;
4342 error = FALSE;
4343 if (tv1->v_type == VAR_FLOAT)
4344 {
4345 f1 = tv1->vval.v_float;
4346 use_float = TRUE;
4347 n1 = 0;
4348 }
4349 else
4350 n1 = tv_get_number_chk(tv1, &error);
4351 clear_tv(tv1);
4352 if (error)
4353 {
4354 clear_tv(tv2);
4355 return FAIL;
4356 }
4357
4358 if (tv2->v_type == VAR_FLOAT)
4359 {
4360 if (!use_float)
4361 {
4362 f1 = n1;
4363 use_float = TRUE;
4364 }
4365 f2 = tv2->vval.v_float;
4366 n2 = 0;
4367 }
4368 else
4369 {
4370 n2 = tv_get_number_chk(tv2, &error);
4371 clear_tv(tv2);
4372 if (error)
4373 return FAIL;
4374 if (use_float)
4375 f2 = n2;
4376 }
4377
4378 /*
4379 * Compute the result.
4380 * When either side is a float the result is a float.
4381 */
4382 if (use_float)
4383 {
4384 if (op == '*')
4385 f1 = f1 * f2;
4386 else if (op == '/')
4387 {
4388#ifdef VMS
4389 // VMS crashes on divide by zero, work around it
4390 if (f2 == 0.0)
4391 {
4392 if (f1 == 0)
4393 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
4394 else if (f1 < 0)
4395 f1 = -1 * __F_FLT_MAX;
4396 else
4397 f1 = __F_FLT_MAX;
4398 }
4399 else
4400 f1 = f1 / f2;
4401#else
4402 // We rely on the floating point library to handle divide
4403 // by zero to result in "inf" and not a crash.
4404 f1 = f1 / f2;
4405#endif
4406 }
4407 else
4408 {
4409 emsg(_(e_cannot_use_percent_with_float));
4410 return FAIL;
4411 }
4412 tv1->v_type = VAR_FLOAT;
4413 tv1->vval.v_float = f1;
4414 }
4415 else
4416 {
4417 int failed = FALSE;
4418
4419 if (op == '*')
4420 n1 = n1 * n2;
4421 else if (op == '/')
4422 n1 = num_divide(n1, n2, &failed);
4423 else
4424 n1 = num_modulus(n1, n2, &failed);
4425 if (failed)
4426 return FAIL;
4427
4428 tv1->v_type = VAR_NUMBER;
4429 tv1->vval.v_number = n1;
4430 }
4431
4432 return OK;
4433}
4434
4435/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004436 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * * number multiplication
4438 * / number division
4439 * % number modulo
4440 *
4441 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004442 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 *
4444 * Return OK or FAIL.
4445 */
4446 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004447eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004448 char_u **arg,
4449 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004450 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004451 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004454 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004456 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 return FAIL;
4458
4459 /*
4460 * Repeat computing, until no '*', '/' or '%' is following.
4461 */
4462 for (;;)
4463 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004464 int evaluate;
4465 int getnext;
4466 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004467 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004468 int op;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004469
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004470 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004471 p = eval_next_non_blank(*arg, evalarg, &getnext);
4472 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004473 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004475
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004476 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004477 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004478 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004479 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004480 {
4481 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4482 {
mityu4ac198c2021-05-28 17:52:40 +02004483 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004484 clear_tv(rettv);
4485 return FAIL;
4486 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004487 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 /*
4491 * Get the second variable.
4492 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004493 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4494 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004495 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004496 clear_tv(rettv);
4497 return FAIL;
4498 }
4499 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004500 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 return FAIL;
4502
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004503 if (evaluate)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004504 // Compute the result.
4505 if (eval_multdiv_number(rettv, &var2, op) == FAIL)
4506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
4509 return OK;
4510}
4511
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004512/*
4513 * Handle a type cast before a base level expression.
4514 * "arg" must point to the first non-white of the expression.
4515 * "arg" is advanced to just after the recognized expression.
4516 * Return OK or FAIL.
4517 */
4518 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004519eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004520 char_u **arg,
4521 typval_T *rettv,
4522 evalarg_T *evalarg,
4523 int want_string) // after "." operator
4524{
4525 type_T *want_type = NULL;
4526 garray_T type_list; // list of pointers to allocated types
4527 int res;
4528 int evaluate = evalarg == NULL ? 0
4529 : (evalarg->eval_flags & EVAL_EVALUATE);
4530
4531 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004532 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4533 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004534 {
4535 ++*arg;
4536 ga_init2(&type_list, sizeof(type_T *), 10);
4537 want_type = parse_type(arg, &type_list, TRUE);
4538 if (want_type == NULL && (evaluate || **arg != '>'))
4539 {
4540 clear_type_list(&type_list);
4541 return FAIL;
4542 }
4543
4544 if (**arg != '>')
4545 {
4546 if (*skipwhite(*arg) == '>')
4547 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4548 else
4549 emsg(_(e_missing_gt));
4550 clear_type_list(&type_list);
4551 return FAIL;
4552 }
4553 ++*arg;
4554 *arg = skipwhite_and_linebreak(*arg, evalarg);
4555 }
4556
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004557 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004558
4559 if (want_type != NULL && evaluate)
4560 {
4561 if (res == OK)
4562 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004563 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4564 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004565
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004566 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004567 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004568 if (want_type->tt_type == VAR_BOOL
4569 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004570 && (actual->tt_flags & TTFLAG_BOOL_OK))
4571 {
4572 int n = tv2bool(rettv);
4573
4574 // can use "0" and "1" for boolean in some places
4575 clear_tv(rettv);
4576 rettv->v_type = VAR_BOOL;
4577 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4578 }
4579 else
4580 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004581 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004582
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004583 res = check_type(want_type, actual, TRUE, where);
4584 }
4585 }
4586 }
4587 clear_type_list(&type_list);
4588 }
4589
4590 return res;
4591}
4592
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004593 int
4594eval_leader(char_u **arg, int vim9)
4595{
4596 char_u *s = *arg;
4597 char_u *p = *arg;
4598
4599 while (*p == '!' || *p == '-' || *p == '+')
4600 {
4601 char_u *n = skipwhite(p + 1);
4602
4603 // ++, --, -+ and +- are not accepted in Vim9 script
4604 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4605 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004606 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004607 return FAIL;
4608 }
4609 p = n;
4610 }
4611 *arg = p;
4612 return OK;
4613}
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004616 * Check for a predefined value "true", "false" and "null.*".
4617 * Return OK when recognized.
4618 */
4619 int
4620handle_predefined(char_u *s, int len, typval_T *rettv)
4621{
4622 switch (len)
4623 {
4624 case 4: if (STRNCMP(s, "true", 4) == 0)
4625 {
4626 rettv->v_type = VAR_BOOL;
4627 rettv->vval.v_number = VVAL_TRUE;
4628 return OK;
4629 }
4630 if (STRNCMP(s, "null", 4) == 0)
4631 {
4632 rettv->v_type = VAR_SPECIAL;
4633 rettv->vval.v_number = VVAL_NULL;
4634 return OK;
4635 }
4636 break;
4637 case 5: if (STRNCMP(s, "false", 5) == 0)
4638 {
4639 rettv->v_type = VAR_BOOL;
4640 rettv->vval.v_number = VVAL_FALSE;
4641 return OK;
4642 }
4643 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004644 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4645 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004647 rettv->v_type = VAR_JOB;
4648 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004649#else
4650 rettv->v_type = VAR_SPECIAL;
4651 rettv->vval.v_number = VVAL_NULL;
4652#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004653 return OK;
4654 }
4655 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004656 case 9:
4657 if (STRNCMP(s, "null_", 5) != 0)
4658 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004659 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004660 if (STRNCMP(s + 5, "list", 4) == 0)
4661 {
4662 rettv->v_type = VAR_LIST;
4663 rettv->vval.v_list = NULL;
4664 return OK;
4665 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004666 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004667 if (STRNCMP(s + 5, "dict", 4) == 0)
4668 {
4669 rettv->v_type = VAR_DICT;
4670 rettv->vval.v_dict = NULL;
4671 return OK;
4672 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004673 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004674 if (STRNCMP(s + 5, "blob", 4) == 0)
4675 {
4676 rettv->v_type = VAR_BLOB;
4677 rettv->vval.v_blob = NULL;
4678 return OK;
4679 }
4680 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004681 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4682 {
4683 rettv->v_type = VAR_CLASS;
4684 rettv->vval.v_class = NULL;
4685 return OK;
4686 }
4687 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004688 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4689 {
4690 rettv->v_type = VAR_STRING;
4691 rettv->vval.v_string = NULL;
4692 return OK;
4693 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004694 if (STRNCMP(s, "null_object", 11) == 0)
4695 {
4696 rettv->v_type = VAR_OBJECT;
4697 rettv->vval.v_object = NULL;
4698 return OK;
4699 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004700 break;
4701 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004702 if (STRNCMP(s, "null_channel", 12) == 0)
4703 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004704#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004705 rettv->v_type = VAR_CHANNEL;
4706 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004707#else
4708 rettv->v_type = VAR_SPECIAL;
4709 rettv->vval.v_number = VVAL_NULL;
4710#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004711 return OK;
4712 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004713 if (STRNCMP(s, "null_partial", 12) == 0)
4714 {
4715 rettv->v_type = VAR_PARTIAL;
4716 rettv->vval.v_partial = NULL;
4717 return OK;
4718 }
4719 break;
4720 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4721 {
4722 rettv->v_type = VAR_FUNC;
4723 rettv->vval.v_string = NULL;
4724 return OK;
4725 }
4726 break;
4727 }
4728 return FAIL;
4729}
4730
4731/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004732 * Handle register contents: @r.
4733 */
4734 static void
4735eval9_reg_contents(
4736 char_u **arg,
4737 typval_T *rettv,
4738 int evaluate)
4739{
4740 int vim9script = in_vim9script();
4741
4742 ++*arg; // skip '@'
4743 if (evaluate)
4744 {
4745 if (vim9script && IS_WHITE_OR_NUL(**arg))
4746 semsg(_(e_syntax_error_at_str), *arg);
4747 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4748 emsg_invreg(**arg);
4749 else
4750 {
4751 rettv->v_type = VAR_STRING;
4752 rettv->vval.v_string = get_reg_contents(**arg,
4753 GREG_EXPR_SRC);
4754 }
4755 }
4756 if (**arg != NUL)
4757 ++*arg;
4758}
4759
4760/*
4761 * Handle a nested expression: (expression) or lambda: (arg) => expr
4762 */
4763 static int
4764eval9_nested_expr(
4765 char_u **arg,
4766 typval_T *rettv,
4767 evalarg_T *evalarg,
4768 int evaluate)
4769{
4770 int ret = NOTDONE;
4771 int vim9script = in_vim9script();
4772
4773 if (vim9script)
4774 {
4775 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4776 if (ret == OK && evaluate)
4777 {
4778 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4779
4780 // Compile it here to get the return type. The return
4781 // type is optional, when it's missing use t_unknown.
4782 // This is recognized in compile_return().
4783 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4784 ufunc->uf_ret_type = &t_unknown;
4785 if (compile_def_function(ufunc, FALSE,
4786 get_compile_type(ufunc), NULL) == FAIL)
4787 {
4788 clear_tv(rettv);
4789 ret = FAIL;
4790 }
4791 }
4792 }
4793 if (ret == NOTDONE)
4794 {
4795 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
4796 ret = eval1(arg, rettv, evalarg); // recursive!
4797
4798 *arg = skipwhite_and_linebreak(*arg, evalarg);
4799 if (**arg == ')')
4800 ++*arg;
4801 else if (ret == OK)
4802 {
4803 emsg(_(e_missing_closing_paren));
4804 clear_tv(rettv);
4805 ret = FAIL;
4806 }
4807 }
4808
4809 return ret;
4810}
4811
4812/*
4813* Handle be a variable or function name.
4814* Can also be a curly-braces kind of name: {expr}.
4815*/
4816 static int
4817eval9_var_func_name(
4818 char_u **arg,
4819 typval_T *rettv,
4820 evalarg_T *evalarg,
4821 int evaluate,
4822 char_u **name_start)
4823{
4824 char_u *s;
4825 int len;
4826 char_u *alias;
4827 int ret = OK;
4828 int vim9script = in_vim9script();
4829
4830 s = *arg;
4831 len = get_name_len(arg, &alias, evaluate, TRUE);
4832 if (alias != NULL)
4833 s = alias;
4834
4835 if (len <= 0)
4836 ret = FAIL;
4837 else
4838 {
4839 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4840
4841 if (evaluate && vim9script && len == 1 && *s == '_')
4842 {
4843 emsg(_(e_cannot_use_underscore_here));
4844 ret = FAIL;
4845 }
4846 else if (evaluate && vim9script && len > 2
4847 && s[0] == 's' && s[1] == ':')
4848 {
4849 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4850 ret = FAIL;
4851 }
4852 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
4853 {
4854 // "name(..." recursive!
4855 *arg = skipwhite(*arg);
4856 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
4857 }
4858 else if (evaluate)
4859 {
4860 // get the value of "true", "false", etc. or a variable
4861 ret = FAIL;
4862 if (vim9script)
4863 ret = handle_predefined(s, len, rettv);
4864 if (ret == FAIL)
4865 {
4866 *name_start = s;
4867 ret = eval_variable(s, len, 0, rettv, NULL,
4868 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
4869 }
4870 }
4871 else
4872 {
4873 // skip the name
4874 check_vars(s, len);
4875 ret = OK;
4876 }
4877 }
4878 vim_free(alias);
4879
4880 return ret;
4881}
4882
4883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 * Handle sixth level expression:
4885 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004886 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004887 * "string" string constant
4888 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 * &option-name option value
4890 * @r register contents
4891 * identifier variable value
4892 * function() function call
4893 * $VAR environment variable
4894 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004895 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004897 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004898 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 *
4900 * Also handle:
4901 * ! in front logical NOT
4902 * - in front unary minus
4903 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 * trailing [] subscript in String or List
4905 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004906 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 *
4908 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004909 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 *
4911 * Return OK or FAIL.
4912 */
4913 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004914eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915 char_u **arg,
4916 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004917 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004920 int evaluate = evalarg != NULL
4921 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004922 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 char_u *start_leader, *end_leader;
4924 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004925 static int recurse = 0;
4926 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004932 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004935 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
4937 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004938 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004939 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 end_leader = *arg;
4941
Keith Thompson184f71c2024-01-04 21:19:04 +01004942 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004943 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004944 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004945 ++*arg;
4946 return FAIL;
4947 }
4948
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004949 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004950 // and crash. With MSVC the stack is smaller.
4951 if (recurse ==
4952#ifdef _MSC_VER
4953 300
4954#else
4955 1000
4956#endif
4957 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004958 {
4959 semsg(_(e_expression_too_recursive_str), *arg);
4960 return FAIL;
4961 }
4962 ++recurse;
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 switch (**arg)
4965 {
4966 /*
4967 * Number constant.
4968 */
4969 case '0':
4970 case '1':
4971 case '2':
4972 case '3':
4973 case '4':
4974 case '5':
4975 case '6':
4976 case '7':
4977 case '8':
4978 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004979 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004980
4981 // Apply prefixed "-" and "+" now. Matters especially when
4982 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004983 if (ret == OK && evaluate && end_leader > start_leader
4984 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004985 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 break;
4987
4988 /*
4989 * String constant: "string".
4990 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004991 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993
4994 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004997 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 break;
4999
5000 /*
5001 * List: [expr, expr]
5002 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005003 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005
5006 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005007 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005008 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005009 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005010 break;
5011
5012 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005013 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005014 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005016 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005017 ret = NOTDONE;
5018 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005019 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005020 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005021 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 break;
5023
5024 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005025 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005027 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029
5030 /*
5031 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005032 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
LemonBoy2eaef102022-05-06 13:14:50 +01005034 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5035 ret = eval_interp_string(arg, rettv, evaluate);
5036 else
5037 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 break;
5039
5040 /*
5041 * Register contents: @r.
5042 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005043 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 break;
5045
5046 /*
5047 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005048 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005050 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 break;
5052
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056
5057 if (ret == NOTDONE)
5058 {
5059 /*
5060 * Must be a variable or function name.
5061 * Can also be a curly-braces kind of name: {expr}.
5062 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005063 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 }
5065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5067 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005069 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 /*
5072 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5073 */
5074 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005075 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005076
5077 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005078 return ret;
5079}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005080
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005081/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005082 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005083 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005084 * Adjusts "end_leaderp" until it is at "start_leader".
5085 */
5086 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005087eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005088 typval_T *rettv,
5089 int numeric_only,
5090 char_u *start_leader,
5091 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005092{
5093 char_u *end_leader = *end_leaderp;
5094 int ret = OK;
5095 int error = FALSE;
5096 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005097 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005098 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005099 float_T f = 0.0;
5100
5101 if (rettv->v_type == VAR_FLOAT)
5102 f = rettv->vval.v_float;
5103 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005104 {
5105 while (VIM_ISWHITE(end_leader[-1]))
5106 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005107 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005108 val = tv2bool(rettv);
5109 else
5110 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005111 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005112 if (error)
5113 {
5114 clear_tv(rettv);
5115 ret = FAIL;
5116 }
5117 else
5118 {
5119 while (end_leader > start_leader)
5120 {
5121 --end_leader;
5122 if (*end_leader == '!')
5123 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005124 if (numeric_only)
5125 {
5126 ++end_leader;
5127 break;
5128 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005129 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005130 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005131 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005132 {
5133 rettv->v_type = VAR_BOOL;
5134 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5135 }
5136 else
5137 f = !f;
5138 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005139 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005140 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005141 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005142 type = VAR_BOOL;
5143 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005144 }
5145 else if (*end_leader == '-')
5146 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005147 if (rettv->v_type == VAR_FLOAT)
5148 f = -f;
5149 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005150 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005151 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005152 type = VAR_NUMBER;
5153 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005154 }
5155 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005156 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005158 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005159 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161 else
5162 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005163 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005164 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005165 rettv->v_type = type;
5166 else
5167 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005168 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005171 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 return ret;
5173}
5174
5175/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005176 * Call the function referred to in "rettv".
5177 */
5178 static int
5179call_func_rettv(
5180 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005181 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005182 typval_T *rettv,
5183 int evaluate,
5184 dict_T *selfdict,
5185 typval_T *basetv)
5186{
5187 partial_T *pt = NULL;
5188 funcexe_T funcexe;
5189 typval_T functv;
5190 char_u *s;
5191 int ret;
5192
5193 // need to copy the funcref so that we can clear rettv
5194 if (evaluate)
5195 {
5196 functv = *rettv;
5197 rettv->v_type = VAR_UNKNOWN;
5198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005200 if (functv.v_type == VAR_PARTIAL)
5201 {
5202 pt = functv.vval.v_partial;
5203 s = partial_name(pt);
5204 }
5205 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005206 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005207 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005208 if (s == NULL || *s == NUL)
5209 {
5210 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005211 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005212 goto theend;
5213 }
5214 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005215 }
5216 else
5217 s = (char_u *)"";
5218
Bram Moolenaara80faa82020-04-12 19:37:17 +02005219 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005220 funcexe.fe_firstline = curwin->w_cursor.lnum;
5221 funcexe.fe_lastline = curwin->w_cursor.lnum;
5222 funcexe.fe_evaluate = evaluate;
5223 funcexe.fe_partial = pt;
5224 funcexe.fe_selfdict = selfdict;
5225 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005226 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005227
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005228theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // Clear the funcref afterwards, so that deleting it while
5230 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005231 if (evaluate)
5232 clear_tv(&functv);
5233
5234 return ret;
5235}
5236
5237/*
5238 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005239 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005240 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5241 */
5242 static int
5243eval_lambda(
5244 char_u **arg,
5245 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005246 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005248{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005249 int evaluate = evalarg != NULL
5250 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005251 typval_T base = *rettv;
5252 int ret;
5253
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005254 rettv->v_type = VAR_UNKNOWN;
5255
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005256 if (**arg == '{')
5257 {
5258 // ->{lambda}()
5259 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5260 }
5261 else
5262 {
5263 // ->(lambda)()
5264 ++*arg;
5265 ret = eval1(arg, rettv, evalarg);
5266 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005267 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005268 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005269 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005270 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005271 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005272 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5273 && rettv->v_type != VAR_PARTIAL)
5274 {
5275 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5276 return FAIL;
5277 }
5278 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005279 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005280 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005281 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005282
5283 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005284 {
5285 if (verbose)
5286 {
5287 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005288 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005289 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005290 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005291 }
5292 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005293 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005294 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005295 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005296 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005297
5298 // Clear the funcref afterwards, so that deleting it while
5299 // evaluating the arguments is possible (see test55).
5300 if (evaluate)
5301 clear_tv(&base);
5302
5303 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005304}
5305
5306/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005307 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005308 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005309 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5310 */
5311 static int
5312eval_method(
5313 char_u **arg,
5314 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005315 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005317{
5318 char_u *name;
5319 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005320 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005321 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005322 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005323 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005324 int evaluate = evalarg != NULL
5325 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005326
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005327 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005328
Bram Moolenaarac92e252019-08-03 21:58:38 +02005329 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005330 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005331 if (alias != NULL)
5332 name = alias;
5333
5334 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005335 {
5336 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005337 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005338 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005339 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005340 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005341 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005342 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005343
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005344 // If there is no "(" immediately following, but there is further on,
5345 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5346 // Does not handle anything where "(" is part of the expression.
5347 *arg = skipwhite(*arg);
5348
5349 if (**arg != '(' && alias == NULL
5350 && (paren = vim_strchr(*arg, '(')) != NULL)
5351 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005352 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005353
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005354 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005355 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005356 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005357 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5358 if (evalarg != NULL)
5359 {
5360 getline = evalarg->eval_getline;
5361 evalarg->eval_getline = NULL;
5362 }
5363
5364 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005365 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005366 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005367 *arg = name + len;
5368 ret = FAIL;
5369 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005370 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005371 {
5372 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005373 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005374 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005375
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005376 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005377 if (getline != NULL)
5378 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005379 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005380
5381 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005382 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005383 *arg = skipwhite(*arg);
5384
5385 if (**arg != '(')
5386 {
5387 if (verbose)
5388 semsg(_(e_missing_parenthesis_str), name);
5389 ret = FAIL;
5390 }
5391 else if (VIM_ISWHITE((*arg)[-1]))
5392 {
5393 if (verbose)
5394 emsg(_(e_no_white_space_allowed_before_parenthesis));
5395 ret = FAIL;
5396 }
5397 else
5398 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005399 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005400 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005401 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005402
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005403 // Clear the funcref afterwards, so that deleting it while
5404 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005405 if (evaluate)
5406 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005407 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005408
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005409 if (alias != NULL)
5410 vim_free(alias);
5411
Bram Moolenaarac92e252019-08-03 21:58:38 +02005412 return ret;
5413}
5414
5415/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005416 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5417 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5419 */
5420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005421eval_index(
5422 char_u **arg,
5423 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005424 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005427 int evaluate = evalarg != NULL
5428 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005433 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005434 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005436 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5437 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005439 init_tv(&var1);
5440 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 /*
5444 * dict.name
5445 */
5446 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005447 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005449 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005451 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 /*
5456 * something[idx]
5457 *
5458 * Get the (first) variable from inside the [].
5459 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005460 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 if (**arg == ':')
5462 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005463 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005465 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005466 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005467 semsg(_(e_white_space_required_before_and_after_str_at_str),
5468 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005469 clear_tv(&var1);
5470 return FAIL;
5471 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005472 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005473 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005474 int error = FALSE;
5475
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005476 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005477 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005478 && var1.v_type == VAR_FLOAT)
5479 {
5480 var1.vval.v_string = typval_tostring(&var1, TRUE);
5481 var1.v_type = VAR_STRING;
5482 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005483
Bram Moolenaar4525a572022-02-13 11:57:33 +00005484 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005485 tv_get_number_chk(&var1, &error);
5486 else
5487 error = tv_get_string_chk(&var1) == NULL;
5488 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005489 {
5490 // not a number or string
5491 clear_tv(&var1);
5492 return FAIL;
5493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
5496 /*
5497 * Get the second variable from inside the [:].
5498 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005499 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 if (**arg == ':')
5501 {
5502 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005503 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005504 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005505 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005506 semsg(_(e_white_space_required_before_and_after_str_at_str),
5507 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005508 if (!empty1)
5509 clear_tv(&var1);
5510 return FAIL;
5511 }
5512 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513 if (**arg == ']')
5514 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005515 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005517 if (!empty1)
5518 clear_tv(&var1);
5519 return FAIL;
5520 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005521 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005523 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 if (!empty1)
5525 clear_tv(&var1);
5526 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 return FAIL;
5528 }
5529 }
5530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005532 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533 if (**arg != ']')
5534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005535 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005536 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 clear_tv(&var1);
5538 if (range)
5539 clear_tv(&var2);
5540 return FAIL;
5541 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005542 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544
5545 if (evaluate)
5546 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005547 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005548 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005549 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005550
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005551 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005554 clear_tv(&var2);
5555 return res;
5556 }
5557 return OK;
5558}
5559
5560/*
5561 * Check if "rettv" can have an [index] or [sli:ce]
5562 */
5563 int
5564check_can_index(typval_T *rettv, int evaluate, int verbose)
5565{
5566 switch (rettv->v_type)
5567 {
5568 case VAR_FUNC:
5569 case VAR_PARTIAL:
5570 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005571 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005572 return FAIL;
5573 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005574 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005575 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005576 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005577 case VAR_BOOL:
5578 case VAR_SPECIAL:
5579 case VAR_JOB:
5580 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005581 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005582 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005583 if (verbose)
5584 emsg(_(e_cannot_index_special_variable));
5585 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005586 case VAR_CLASS:
5587 case VAR_TYPEALIAS:
5588 if (verbose)
5589 check_typval_is_value(rettv);
5590 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005591 case VAR_UNKNOWN:
5592 case VAR_ANY:
5593 case VAR_VOID:
5594 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005596 emsg(_(e_cannot_index_special_variable));
5597 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005599 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005601 case VAR_STRING:
5602 case VAR_LIST:
5603 case VAR_DICT:
5604 case VAR_BLOB:
5605 break;
5606 case VAR_NUMBER:
5607 if (in_vim9script())
5608 emsg(_(e_cannot_index_number));
5609 break;
5610 }
5611 return OK;
5612}
5613
5614/*
5615 * Apply index or range to "rettv".
5616 * "var1" is the first index, NULL for [:expr].
5617 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005618 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5619 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005620 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5621 */
5622 int
5623eval_index_inner(
5624 typval_T *rettv,
5625 int is_range,
5626 typval_T *var1,
5627 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005628 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005629 char_u *key,
5630 int keylen,
5631 int verbose)
5632{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005633 varnumber_T n1, n2 = 0;
5634 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005635
5636 n1 = 0;
5637 if (var1 != NULL && rettv->v_type != VAR_DICT)
5638 n1 = tv_get_number(var1);
5639
5640 if (is_range)
5641 {
5642 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005644 if (verbose)
5645 emsg(_(e_cannot_slice_dictionary));
5646 return FAIL;
5647 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005648 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005649 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005650 else
5651 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005652 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005653
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005654 switch (rettv->v_type)
5655 {
5656 case VAR_UNKNOWN:
5657 case VAR_ANY:
5658 case VAR_VOID:
5659 case VAR_FUNC:
5660 case VAR_PARTIAL:
5661 case VAR_FLOAT:
5662 case VAR_BOOL:
5663 case VAR_SPECIAL:
5664 case VAR_JOB:
5665 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005666 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005667 case VAR_CLASS:
5668 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005669 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005670 break; // not evaluating, skipping over subscript
5671
5672 case VAR_NUMBER:
5673 case VAR_STRING:
5674 {
5675 char_u *s = tv_get_string(rettv);
5676
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005678 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005679 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005680 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005681 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005682 else
5683 s = char_from_string(s, n1);
5684 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005685 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 // The resulting variable is a substring. If the indexes
5688 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 if (n1 < 0)
5690 {
5691 n1 = len + n1;
5692 if (n1 < 0)
5693 n1 = 0;
5694 }
5695 if (n2 < 0)
5696 n2 = len + n2;
5697 else if (n2 >= len)
5698 n2 = len;
5699 if (n1 >= len || n2 < 0 || n1 > n2)
5700 s = NULL;
5701 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005702 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 }
5704 else
5705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005706 // The resulting variable is a string of a single
5707 // character. If the index is too big or negative the
5708 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 if (n1 >= len || n1 < 0)
5710 s = NULL;
5711 else
5712 s = vim_strnsave(s + n1, 1);
5713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005714 clear_tv(rettv);
5715 rettv->v_type = VAR_STRING;
5716 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005717 }
5718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005720 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005721 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5722 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005723 break;
5724
5725 case VAR_LIST:
5726 if (var1 == NULL)
5727 n1 = 0;
5728 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005729 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005730 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005731 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005732 return FAIL;
5733 break;
5734
5735 case VAR_DICT:
5736 {
5737 dictitem_T *item;
5738 typval_T tmp;
5739
5740 if (key == NULL)
5741 {
5742 key = tv_get_string_chk(var1);
5743 if (key == NULL)
5744 return FAIL;
5745 }
5746
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005747 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005748
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005749 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005750 {
5751 if (verbose)
5752 {
5753 if (keylen > 0)
5754 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005755 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005756 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005757 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005758 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005759
5760 copy_tv(&item->di_tv, &tmp);
5761 clear_tv(rettv);
5762 *rettv = tmp;
5763 }
5764 break;
5765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 return OK;
5767}
5768
5769/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005770 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005771 */
5772 char_u *
5773partial_name(partial_T *pt)
5774{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005775 if (pt != NULL)
5776 {
5777 if (pt->pt_name != NULL)
5778 return pt->pt_name;
5779 if (pt->pt_func != NULL)
5780 return pt->pt_func->uf_name;
5781 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005782 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005783}
5784
Bram Moolenaarddecc252016-04-06 22:59:37 +02005785 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005786partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005787{
5788 int i;
5789
5790 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005791 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005792 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005793 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005794 if (pt->pt_name != NULL)
5795 {
5796 func_unref(pt->pt_name);
5797 vim_free(pt->pt_name);
5798 }
5799 else
5800 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005801 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005802
Bram Moolenaar54656012021-06-09 20:50:46 +02005803 // "out_up" is no longer used, decrement refcount on partial that owns it.
5804 partial_unref(pt->pt_outer.out_up_partial);
5805
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005806 // Using pt_outer from another partial.
5807 partial_unref(pt->pt_outer_partial);
5808
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005809 // Decrease the reference count for the context of a closure. If down
5810 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005811 if (pt->pt_funcstack != NULL)
5812 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005813 --pt->pt_funcstack->fs_refcount;
5814 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005815 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005816 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005817 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5818 if (pt->pt_loopvars[i] != NULL)
5819 {
5820 --pt->pt_loopvars[i]->lvs_refcount;
5821 loopvars_check_refcount(pt->pt_loopvars[i]);
5822 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005823
Bram Moolenaarddecc252016-04-06 22:59:37 +02005824 vim_free(pt);
5825}
5826
5827/*
5828 * Unreference a closure: decrement the reference count and free it when it
5829 * becomes zero.
5830 */
5831 void
5832partial_unref(partial_T *pt)
5833{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005834 if (pt == NULL)
5835 return;
5836
5837 int done = FALSE;
5838
5839 if (--pt->pt_refcount <= 0)
5840 partial_free(pt);
5841
5842 // If the reference count goes down to one, the funcstack may be the
5843 // only reference and can be freed if no other partials reference it.
5844 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005845 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005846 // careful: if the funcstack is freed it may contain this partial
5847 // and it gets freed as well
5848 if (pt->pt_funcstack != NULL)
5849 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005850
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005851 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005852 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005853 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005854
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005855 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5856 if (pt->pt_loopvars[depth] != NULL
5857 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005858 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005859 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005860 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005861}
5862
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005864 * Return a textual representation of a string in "tv".
5865 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5866 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5867 * strings as "string()", otherwise does not put quotes around strings.
5868 * May return NULL.
5869 */
5870 static char_u *
5871string_tv2string(
5872 typval_T *tv,
5873 char_u **tofree,
5874 int echo_style,
5875 int composite_val)
5876{
5877 char_u *r = NULL;
5878
5879 if (echo_style && !composite_val)
5880 {
5881 *tofree = NULL;
5882 r = tv->vval.v_string;
5883 if (r == NULL)
5884 r = (char_u *)"";
5885 }
5886 else
5887 {
5888 *tofree = string_quote(tv->vval.v_string, FALSE);
5889 r = *tofree;
5890 }
5891
5892 return r;
5893}
5894
5895/*
5896 * Return a textual representation of a function in "tv".
5897 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5898 * When "echo_style" is FALSE, put quotes around the function name as
5899 * "function()", otherwise does not put quotes around function name.
5900 * May return NULL.
5901 */
5902 static char_u *
5903func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5904{
5905 char_u *r = NULL;
5906 char_u buf[MAX_FUNC_NAME_LEN];
5907
5908 if (echo_style)
5909 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005910 *tofree = NULL;
5911
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005912 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005913 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005914 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005915 {
5916 r = make_ufunc_name_readable(tv->vval.v_string, buf,
5917 MAX_FUNC_NAME_LEN);
5918 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005919 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005920 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005921 }
5922 else
5923 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005924 char_u *s = NULL;
5925
5926 if (tv->vval.v_string != NULL)
5927 s = make_ufunc_name_readable(tv->vval.v_string, buf,
5928 MAX_FUNC_NAME_LEN);
5929
5930 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005931 }
5932
5933 return r;
5934}
5935
5936/*
Ernie Rael9d779c52024-07-07 20:41:44 +02005937 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
5938 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5939 * When "echo_style" is FALSE, put quotes around the function name as
5940 * "function()", otherwise does not put quotes around function name.
5941 * May return NULL.
5942 */
5943 static char_u *
5944method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5945{
5946 char_u buf[MAX_FUNC_NAME_LEN];
5947 partial_T *pt = tv->vval.v_partial;
5948
5949 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
5950 pt->pt_func->uf_script_ctx.sc_sid,
5951 pt->pt_func->uf_class->class_name,
5952 pt->pt_func->uf_name);
5953 if (len >= sizeof(buf))
5954 {
5955 if (echo_style)
5956 {
5957 *tofree = NULL;
5958 return (char_u *)"function()";
5959 }
5960 else
5961 return *tofree = string_quote((char_u*)"", TRUE);
5962 }
5963
5964 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
5965}
5966
5967/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005968 * Return a textual representation of a partial in "tv".
5969 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5970 * "numbuf" is used for a number. May return NULL.
5971 */
5972 static char_u *
5973partial_tv2string(
5974 typval_T *tv,
5975 char_u **tofree,
5976 char_u *numbuf,
5977 int copyID)
5978{
5979 char_u *r = NULL;
5980 partial_T *pt;
5981 char_u *fname;
5982 garray_T ga;
5983 int i;
5984 char_u *tf;
5985
5986 pt = tv->vval.v_partial;
5987 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5988
5989 ga_init2(&ga, 1, 100);
5990 ga_concat(&ga, (char_u *)"function(");
5991 if (fname != NULL)
5992 {
5993 // When using uf_name prepend "g:" for a global function.
5994 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5995 && vim_isupper(fname[1]))
5996 {
5997 ga_concat(&ga, (char_u *)"'g:");
5998 ga_concat(&ga, fname + 1);
5999 }
6000 else
6001 ga_concat(&ga, fname);
6002 vim_free(fname);
6003 }
6004 if (pt != NULL && pt->pt_argc > 0)
6005 {
6006 ga_concat(&ga, (char_u *)", [");
6007 for (i = 0; i < pt->pt_argc; ++i)
6008 {
6009 if (i > 0)
6010 ga_concat(&ga, (char_u *)", ");
6011 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6012 vim_free(tf);
6013 }
6014 ga_concat(&ga, (char_u *)"]");
6015 }
6016 if (pt != NULL && pt->pt_dict != NULL)
6017 {
6018 typval_T dtv;
6019
6020 ga_concat(&ga, (char_u *)", ");
6021 dtv.v_type = VAR_DICT;
6022 dtv.vval.v_dict = pt->pt_dict;
6023 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6024 vim_free(tf);
6025 }
6026 // terminate with ')' and a NUL
6027 ga_concat_len(&ga, (char_u *)")", 2);
6028
6029 *tofree = ga.ga_data;
6030 r = *tofree;
6031
6032 return r;
6033}
6034
6035/*
6036 * Return a textual representation of a List in "tv".
6037 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6038 * When "copyID" is not zero replace recursive lists with "...". When
6039 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6040 * May return NULL.
6041 */
6042 static char_u *
6043list_tv2string(
6044 typval_T *tv,
6045 char_u **tofree,
6046 int copyID,
6047 int restore_copyID)
6048{
6049 char_u *r = NULL;
6050
6051 if (tv->vval.v_list == NULL)
6052 {
6053 // NULL list is equivalent to empty list.
6054 *tofree = NULL;
6055 r = (char_u *)"[]";
6056 }
6057 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6058 && tv->vval.v_list->lv_len > 0)
6059 {
6060 *tofree = NULL;
6061 r = (char_u *)"[...]";
6062 }
6063 else
6064 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006065 int old_copyID;
6066 if (restore_copyID)
6067 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006068
6069 tv->vval.v_list->lv_copyID = copyID;
6070 *tofree = list2string(tv, copyID, restore_copyID);
6071 if (restore_copyID)
6072 tv->vval.v_list->lv_copyID = old_copyID;
6073 r = *tofree;
6074 }
6075
6076 return r;
6077}
6078
6079/*
6080 * Return a textual representation of a Dict in "tv".
6081 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6082 * When "copyID" is not zero replace recursive dicts with "...".
6083 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6084 * replaced with "...". May return NULL.
6085 */
6086 static char_u *
6087dict_tv2string(
6088 typval_T *tv,
6089 char_u **tofree,
6090 int copyID,
6091 int restore_copyID)
6092{
6093 char_u *r = NULL;
6094
6095 if (tv->vval.v_dict == NULL)
6096 {
6097 // NULL dict is equivalent to empty dict.
6098 *tofree = NULL;
6099 r = (char_u *)"{}";
6100 }
6101 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6102 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6103 {
6104 *tofree = NULL;
6105 r = (char_u *)"{...}";
6106 }
6107 else
6108 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006109 int old_copyID;
6110 if (restore_copyID)
6111 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006112
6113 tv->vval.v_dict->dv_copyID = copyID;
6114 *tofree = dict2string(tv, copyID, restore_copyID);
6115 if (restore_copyID)
6116 tv->vval.v_dict->dv_copyID = old_copyID;
6117 r = *tofree;
6118 }
6119
6120 return r;
6121}
6122
6123/*
6124 * Return a textual representation of a job or a channel in "tv".
6125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6126 * "numbuf" is used for a number.
6127 * When "composite_val" is FALSE, put quotes around strings as "string()",
6128 * otherwise does not put quotes around strings.
6129 * May return NULL.
6130 */
6131 static char_u *
6132jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006133 typval_T *tv UNUSED,
6134 char_u **tofree UNUSED,
6135 char_u *numbuf UNUSED,
6136 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006137{
6138 char_u *r = NULL;
6139
6140#ifdef FEAT_JOB_CHANNEL
6141 *tofree = NULL;
6142
6143 if (tv->v_type == VAR_JOB)
6144 r = job_to_string_buf(tv, numbuf);
6145 else
6146 r = channel_to_string_buf(tv, numbuf);
6147
6148 if (composite_val)
6149 {
6150 *tofree = string_quote(r, FALSE);
6151 r = *tofree;
6152 }
6153#endif
6154
6155 return r;
6156}
6157
6158/*
6159 * Return a textual representation of a class in "tv".
6160 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6161 * May return NULL.
6162 */
6163 static char_u *
6164class_tv2string(typval_T *tv, char_u **tofree)
6165{
6166 char_u *r = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01006167 size_t rsize;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006168 class_T *cl = tv->vval.v_class;
John Marriottbd4614f2024-11-18 20:25:21 +01006169 char_u *class_name = (char_u *)"[unknown]";
6170 size_t class_namelen = 9;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006171 char *s = "class";
John Marriottbd4614f2024-11-18 20:25:21 +01006172 size_t slen = 5;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006173
John Marriottbd4614f2024-11-18 20:25:21 +01006174 if (cl != NULL)
6175 {
6176 class_name = cl->class_name;
6177 class_namelen = STRLEN(cl->class_name);
6178 if (IS_INTERFACE(cl))
6179 {
6180 s = "interface";
6181 slen = 9;
6182 }
6183 else if (IS_ENUM(cl))
6184 {
6185 s = "enum";
6186 slen = 4;
6187 }
6188 }
6189
6190 rsize = slen + 1 + class_namelen + 1;
6191 r = *tofree = alloc(rsize);
6192 if (r != NULL)
6193 vim_snprintf((char *)r, rsize, "%s %s", s, (char *)class_name);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006194
6195 return r;
6196}
6197
6198/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006199 * Return a textual representation of an Object in "tv".
6200 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6201 * When "copyID" is not zero replace recursive object with "...".
6202 * When "restore_copyID" is FALSE, repeated items in the object are
6203 * replaced with "...". May return NULL.
6204 */
6205 static char_u *
6206object_tv2string(
6207 typval_T *tv,
6208 char_u **tofree,
6209 int copyID,
6210 int restore_copyID,
6211 char_u *numbuf,
6212 int echo_style,
6213 int composite_val)
6214{
6215 char_u *r = NULL;
6216
6217 object_T *obj = tv->vval.v_object;
6218 if (obj == NULL || obj->obj_class == NULL)
6219 {
6220 *tofree = NULL;
6221 r = (char_u *)"object of [unknown]";
6222 }
6223 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006224 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006225 {
John Marriottbd4614f2024-11-18 20:25:21 +01006226 size_t n = 25 + STRLEN((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006227 r = alloc(n);
6228 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006229 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006230 obj->obj_class->class_name);
6231 *tofree = r;
6232 }
6233 else
6234 {
6235 int old_copyID;
6236 if (restore_copyID)
6237 old_copyID = obj->obj_copyID;
6238
6239 obj->obj_copyID = copyID;
6240 *tofree = object2string(obj, numbuf, copyID, echo_style,
6241 restore_copyID, composite_val);
6242 if (restore_copyID)
6243 obj->obj_copyID = old_copyID;
6244 r = *tofree;
6245 }
6246
6247 return r;
6248}
6249
6250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 * Return a string with the string representation of a variable.
6252 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006254 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006255 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006256 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006257 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006258 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6259 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006260 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006262 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006263echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006264 typval_T *tv,
6265 char_u **tofree,
6266 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006267 int copyID,
6268 int echo_style,
6269 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006270 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006272 static int recurse = 0;
6273 char_u *r = NULL;
6274
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006277 if (!did_echo_string_emsg)
6278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006279 // Only give this message once for a recursive call to avoid
6280 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006281 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006282 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006283 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006285 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006286 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 }
6288 ++recurse;
6289
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 switch (tv->v_type)
6291 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006292 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006293 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006294 break;
6295
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006297 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006299
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006300 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006301 if (tv->vval.v_partial == NULL
6302 || tv->vval.v_partial->pt_obj == NULL)
6303 r = partial_tv2string(tv, tofree, numbuf, copyID);
6304 else
6305 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006306 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006307
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006308 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006309 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006310 break;
6311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006313 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006315
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006317 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006318 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006321 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006322 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006324 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006325 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006326 break;
6327
Bram Moolenaar835dc632016-02-07 14:27:38 +01006328 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006329 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006330 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006332
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006333 case VAR_INSTR:
6334 *tofree = NULL;
6335 r = (char_u *)"instructions";
6336 break;
6337
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006338 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006339 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006340 break;
6341
6342 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006343 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6344 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006345 break;
6346
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006347 case VAR_FLOAT:
6348 *tofree = NULL;
6349 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6350 r = numbuf;
6351 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006352
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006353 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006354 case VAR_SPECIAL:
6355 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006356 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006357 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006358
6359 case VAR_TYPEALIAS:
6360 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6361 r = *tofree;
6362 if (r == NULL)
6363 r = (char_u *)"";
6364 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366
Bram Moolenaar8502c702014-06-17 12:51:16 +02006367 if (--recurse == 0)
6368 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006369 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006370}
6371
6372/*
6373 * Return a string with the string representation of a variable.
6374 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6375 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006376 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006377 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006378 * May return NULL.
6379 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006380 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006381echo_string(
6382 typval_T *tv,
6383 char_u **tofree,
6384 char_u *numbuf,
6385 int copyID)
6386{
6387 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6388}
6389
6390/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006391 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6392 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006393 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006394 */
6395 int
6396buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6397{
6398 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006399 char_u *t;
6400 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006401
6402 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6403 return -1;
6404
6405 if (lnum > buf->b_ml.ml_line_count)
6406 lnum = buf->b_ml.ml_line_count;
6407
6408 str = ml_get_buf(buf, lnum, FALSE);
6409 if (str == NULL)
6410 return -1;
6411
6412 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006413 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006414
Bram Moolenaar91458462021-01-13 20:08:38 +01006415 // count the number of characters
6416 t = str;
6417 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6418 t += mb_ptr2len(t);
6419
6420 // In insert mode, when the cursor is at the end of a non-empty line,
6421 // byteidx points to the NUL character immediately past the end of the
6422 // string. In this case, add one to the character count.
6423 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6424 count++;
6425
6426 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006427}
6428
6429/*
6430 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006431 * byte index. Works only for loaded buffers. Returns -1 on failure.
6432 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006433 */
6434 int
6435buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6436{
6437 char_u *str;
6438 char_u *t;
6439
6440 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6441 return -1;
6442
6443 if (lnum > buf->b_ml.ml_line_count)
6444 lnum = buf->b_ml.ml_line_count;
6445
6446 str = ml_get_buf(buf, lnum, FALSE);
6447 if (str == NULL)
6448 return -1;
6449
6450 // Convert the character offset to a byte offset
6451 t = str;
6452 while (*t != NUL && --charidx > 0)
6453 t += mb_ptr2len(t);
6454
Bram Moolenaar91458462021-01-13 20:08:38 +01006455 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006456}
6457
6458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006460 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463var2fpos(
6464 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006466 int *fnum, // set to fnum for '0, 'A, etc.
6467 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006469 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006471 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006474 if (varp->v_type == VAR_LIST)
6475 {
6476 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006477 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006478 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006479 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006480
6481 l = varp->vval.v_list;
6482 if (l == NULL)
6483 return NULL;
6484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006485 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006486 pos.lnum = list_find_nr(l, 0L, &error);
6487 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006489 if (charcol)
6490 len = (long)mb_charlen(ml_get(pos.lnum));
6491 else
John Marriottbfcc8952024-03-11 22:04:45 +01006492 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006493
Bram Moolenaarec65d772020-08-20 22:29:12 +02006494 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006495 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006496 li = list_find(l, 1L);
6497 if (li != NULL && li->li_tv.v_type == VAR_STRING
6498 && li->li_tv.vval.v_string != NULL
6499 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006500 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006501 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006502 }
6503 else
6504 {
6505 pos.col = list_find_nr(l, 1L, &error);
6506 if (error)
6507 return NULL;
6508 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006510 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006511 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006513 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006516 pos.coladd = list_find_nr(l, 2L, &error);
6517 if (error)
6518 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006519
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006520 return &pos;
6521 }
6522
Bram Moolenaarc5809432021-03-27 21:23:30 +01006523 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6524 return NULL;
6525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006526 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006527 if (name == NULL)
6528 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006529
6530 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006531 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006532 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006533 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006534 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006535 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006536 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006537 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006538 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006539 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006540 pos = VIsual;
6541 else
6542 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006543 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006544 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006545 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006547 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006548 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6550 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006551 pos = *pp;
6552 }
6553 if (pos.lnum != 0)
6554 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006555 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006556 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6557 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006559
Bram Moolenaara5525202006-03-02 22:52:09 +00006560 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006561
Bram Moolenaar477933c2007-07-17 14:32:23 +00006562 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006563 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006564 // the "w_valid" flags are not reset when moving the cursor, but they
6565 // do matter for update_topline() and validate_botline().
6566 check_cursor_moved(curwin);
6567
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006568 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006569 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006570 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006571 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 // In silent Ex mode topline is zero, but that's not a valid line
6573 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006574 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006575 return &pos;
6576 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006578 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006579 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006580 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006581 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006582 return &pos;
6583 }
6584 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006585 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006587 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
6589 pos.lnum = curbuf->b_ml.ml_line_count;
6590 pos.col = 0;
6591 }
6592 else
6593 {
6594 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006595 if (charcol)
6596 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6597 else
John Marriottbfcc8952024-03-11 22:04:45 +01006598 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 return &pos;
6601 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006602 if (in_vim9script())
6603 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 return NULL;
6605}
6606
6607/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006608 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006609 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006610 * Note that the column is passed on as-is, the caller may want to decrement
6611 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006612 * If "charcol" is TRUE use the column as the character index instead of the
6613 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006614 * Return FAIL when conversion is not possible, doesn't check the position for
6615 * validity.
6616 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006618list2fpos(
6619 typval_T *arg,
6620 pos_T *posp,
6621 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006622 colnr_T *curswantp,
6623 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006624{
6625 list_T *l = arg->vval.v_list;
6626 long i = 0;
6627 long n;
6628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006629 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6630 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006631 if (arg->v_type != VAR_LIST
6632 || l == NULL
6633 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006634 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006635 return FAIL;
6636
6637 if (fnump != NULL)
6638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006640 if (n < 0)
6641 return FAIL;
6642 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006644 *fnump = n;
6645 }
6646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006648 if (n < 0)
6649 return FAIL;
6650 posp->lnum = n;
6651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006652 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006653 if (n < 0)
6654 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006655 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006656 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006657 if (charcol)
6658 {
6659 buf_T *buf;
6660
6661 // Get the text for the specified line in a loaded buffer
6662 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6663 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6664 return FAIL;
6665
Bram Moolenaar79f23442022-10-10 12:42:57 +01006666 n = buf_charidx_to_byteidx(buf,
6667 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006668 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006669 posp->col = n;
6670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006671 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006672 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006673 posp->coladd = 0;
6674 else
6675 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006676
Bram Moolenaar493c1782014-05-28 14:34:46 +02006677 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006679
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006680 return OK;
6681}
6682
6683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 * Get the length of an environment variable name.
6685 * Advance "arg" to the first character after the name.
6686 * Return 0 for error.
6687 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 char_u *p;
6692 int len;
6693
6694 for (p = *arg; vim_isIDc(*p); ++p)
6695 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006696 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 return 0;
6698
6699 len = (int)(p - *arg);
6700 *arg = p;
6701 return len;
6702}
6703
6704/*
6705 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006706 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 * Return 0 if something is wrong.
6708 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 char_u *p;
6713 int len;
6714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006715 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006717 {
6718 if (*p == ':')
6719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006720 // "s:" is start of "s:var", but "n:" is not and can be used in
6721 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006722 len = (int)(p - *arg);
6723 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6724 || len > 1)
6725 break;
6726 }
6727 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006728 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 return 0;
6730
6731 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006732 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 return len;
6735}
6736
6737/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006738 * Get the length of the name of a variable or function.
6739 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006741 * Return -1 if curly braces expansion failed.
6742 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 * If the name contains 'magic' {}'s, expand them and return the
6744 * expanded name in an allocated string via 'alias' - caller must free.
6745 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747get_name_len(
6748 char_u **arg,
6749 char_u **alias,
6750 int evaluate,
6751 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 char_u *p;
6755 char_u *expr_start;
6756 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006758 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6761 && (*arg)[2] == (int)KE_SNR)
6762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006763 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 *arg += 3;
6765 return get_id_len(arg) + 3;
6766 }
6767 len = eval_fname_script(*arg);
6768 if (len > 0)
6769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006770 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 *arg += len;
6772 }
6773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006777 p = find_name_end(*arg, &expr_start, &expr_end,
6778 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (expr_start != NULL)
6780 {
6781 char_u *temp_string;
6782
6783 if (!evaluate)
6784 {
6785 len += (int)(p - *arg);
6786 *arg = skipwhite(p);
6787 return len;
6788 }
6789
6790 /*
6791 * Include any <SID> etc in the expanded string:
6792 * Thus the -len here.
6793 */
6794 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6795 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006796 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 *alias = temp_string;
6798 *arg = skipwhite(p);
6799 return (int)STRLEN(temp_string);
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006803 // Only give an error when there is something, otherwise it will be
6804 // reported at a higher level.
6805 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006806 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808 return len;
6809}
6810
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006811/*
6812 * Find the end of a variable or function name, taking care of magic braces.
6813 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6814 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006815 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006816 * Return a pointer to just after the name. Equal to "arg" if there is no
6817 * valid name.
6818 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006820find_name_end(
6821 char_u *arg,
6822 char_u **expr_start,
6823 char_u **expr_end,
6824 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 int mb_nest = 0;
6827 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006829 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006830 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 *expr_start = NULL;
6835 *expr_end = NULL;
6836 }
6837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006838 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006839 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006840 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006841 return arg;
6842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 for (p = arg; *p != NUL
6844 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006845 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006846 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006847 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006848 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006849 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006850 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006851 if (*p == '\'')
6852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006854 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006855 ;
6856 if (*p == NUL)
6857 break;
6858 }
6859 else if (*p == '"')
6860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006861 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006862 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006863 if (*p == '\\' && p[1] != NUL)
6864 ++p;
6865 if (*p == NUL)
6866 break;
6867 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006868 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6869 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006870 // "s:" is start of "s:var", but "n:" is not and can be used in
6871 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006872 len = (int)(p - arg);
6873 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006874 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006875 break;
6876 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006880 if (*p == '[')
6881 ++br_nest;
6882 else if (*p == ']')
6883 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006885
zeertzjqa93d9cd2023-05-02 16:25:47 +01006886 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006888 if (*p == '{')
6889 {
6890 mb_nest++;
6891 if (expr_start != NULL && *expr_start == NULL)
6892 *expr_start = p;
6893 }
6894 else if (*p == '}')
6895 {
6896 mb_nest--;
6897 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6898 *expr_end = p;
6899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902
6903 return p;
6904}
6905
6906/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006907 * Expands out the 'magic' {}'s in a variable/function name.
6908 * Note that this can call itself recursively, to deal with
6909 * constructs like foo{bar}{baz}{bam}
6910 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6911 * "in_start" ^
6912 * "expr_start" ^
6913 * "expr_end" ^
6914 * "in_end" ^
6915 *
6916 * Returns a new allocated string, which the caller must free.
6917 * Returns NULL for failure.
6918 */
6919 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006920make_expanded_name(
6921 char_u *in_start,
6922 char_u *expr_start,
6923 char_u *expr_end,
6924 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925{
6926 char_u c1;
6927 char_u *retval = NULL;
6928 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929
6930 if (expr_end == NULL || in_end == NULL)
6931 return NULL;
6932 *expr_start = NUL;
6933 *expr_end = NUL;
6934 c1 = *in_end;
6935 *in_end = NUL;
6936
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006937 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006938 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006939 {
John Marriottbd4614f2024-11-18 20:25:21 +01006940 size_t retvalsize = (size_t)(expr_start - in_start)
6941 + STRLEN(temp_result)
6942 + (size_t)(in_end - expr_end) + 1;
6943
6944 retval = alloc(retvalsize);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006945 if (retval != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01006946 vim_snprintf((char *)retval, retvalsize, "%s%s%s",
6947 in_start, temp_result, expr_end + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 }
6949 vim_free(temp_result);
6950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006951 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006952 *expr_start = '{';
6953 *expr_end = '}';
6954
6955 if (retval != NULL)
6956 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006957 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006958 if (expr_start != NULL)
6959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006960 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006961 temp_result = make_expanded_name(retval, expr_start,
6962 expr_end, temp_result);
6963 vim_free(retval);
6964 retval = temp_result;
6965 }
6966 }
6967
6968 return retval;
6969}
6970
6971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006978 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006979}
6980
6981/*
6982 * Return TRUE if character "c" can be used as the first character in a
6983 * variable or function name (excluding '{' and '}').
6984 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006987{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006988 return ASCII_ISALPHA(c) || c == '_';
6989}
6990
6991/*
6992 * Return TRUE if character "c" can be used as the first character of a
6993 * dictionary key.
6994 */
6995 int
6996eval_isdictc(int c)
6997{
6998 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999}
7000
7001/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007002 * Handle:
7003 * - expr[expr], expr[expr:expr] subscript
7004 * - ".name" lookup
7005 * - function call with Funcref variable: func(expr)
7006 * - method call: var->method()
7007 *
7008 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007009 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012handle_subscript(
7013 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007014 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007015 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007016 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007017 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007018{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007019 int evaluate = evalarg != NULL
7020 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007021 int ret = OK;
7022 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007023 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007024 int getnext;
7025 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007026
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007027 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007028 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007029 // When at the end of the line and ".name" or "->{" or "->X" follows in
7030 // the next line then consume the line break.
7031 p = eval_next_non_blank(*arg, evalarg, &getnext);
7032 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007033 && ((*p == '.'
7034 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7035 || rettv->v_type == VAR_CLASS
7036 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007037 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7038 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7039 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007040 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007041 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007042 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007043 check_white = FALSE;
7044 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007045
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007046 if (rettv->v_type == VAR_ANY)
7047 {
7048 char_u *exp_name;
7049 int cc;
7050 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007051 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007052 type_T *type;
7053
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007054 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007055 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007056 if (**arg != '.')
7057 {
7058 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007059 semsg(_(e_expected_dot_after_name_str),
7060 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007061 ret = FAIL;
7062 break;
7063 }
7064 ++*arg;
7065 if (IS_WHITE_OR_NUL(**arg))
7066 {
7067 if (verbose)
7068 emsg(_(e_no_white_space_allowed_after_dot));
7069 ret = FAIL;
7070 break;
7071 }
7072
7073 // isolate the name
7074 exp_name = *arg;
7075 while (eval_isnamec(**arg))
7076 ++*arg;
7077 cc = **arg;
7078 **arg = NUL;
7079
7080 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007081 evalarg == NULL ? NULL : evalarg->eval_cctx,
7082 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007083 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007084
7085 if (idx < 0 && ufunc == NULL)
7086 {
7087 ret = FAIL;
7088 break;
7089 }
7090 if (idx >= 0)
7091 {
7092 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7093 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7094
7095 copy_tv(sv->sv_tv, rettv);
7096 }
7097 else
7098 {
7099 rettv->v_type = VAR_FUNC;
7100 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7101 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007102 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007103 }
7104
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007105 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7106 || rettv->v_type == VAR_PARTIAL))
7107 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007108 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007109 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7110 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007111
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007112 // Stop the expression evaluation when immediately aborting on
7113 // error, or when an interrupt occurred or an exception was thrown
7114 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007115 if (aborting())
7116 {
7117 if (ret == OK)
7118 clear_tv(rettv);
7119 ret = FAIL;
7120 }
7121 dict_unref(selfdict);
7122 selfdict = NULL;
7123 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007124 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007125 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007126 if (in_vim9script())
7127 *arg = skipwhite(p + 2);
7128 else
7129 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007130 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007131 {
zeertzjqc481ad32023-03-11 16:18:51 +00007132 emsg(_(e_no_white_space_allowed_before_parenthesis));
7133 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007134 }
zeertzjqc481ad32023-03-11 16:18:51 +00007135 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7136 // expr->{lambda}() or expr->(lambda)()
7137 ret = eval_lambda(arg, rettv, evalarg, verbose);
7138 else
7139 // expr->name()
7140 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007141 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007142 // "." is ".name" lookup when we found a dict or when evaluating and
7143 // scriptversion is at least 2, where string concatenation is "..".
7144 else if (**arg == '['
7145 || (**arg == '.' && (rettv->v_type == VAR_DICT
7146 || (!evaluate
7147 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007148 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007149 {
7150 dict_unref(selfdict);
7151 if (rettv->v_type == VAR_DICT)
7152 {
7153 selfdict = rettv->vval.v_dict;
7154 if (selfdict != NULL)
7155 ++selfdict->dv_refcount;
7156 }
7157 else
7158 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007159 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007160 {
7161 clear_tv(rettv);
7162 ret = FAIL;
7163 }
7164 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007165 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7166 || rettv->v_type == VAR_OBJECT))
7167 {
7168 // class member: SomeClass.varname
7169 // class method: SomeClass.SomeMethod()
7170 // class constructor: SomeClass.new()
7171 // object member: someObject.varname
7172 // object method: someObject.SomeMethod()
7173 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7174 {
7175 clear_tv(rettv);
7176 ret = FAIL;
7177 }
7178 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007179 else
7180 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007181 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007183 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7184 // Don't do this when "Func" is already a partial that was bound
7185 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007186 if (selfdict != NULL
7187 && (rettv->v_type == VAR_FUNC
7188 || (rettv->v_type == VAR_PARTIAL
7189 && (rettv->vval.v_partial->pt_auto
7190 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007191 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007192
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007193 dict_unref(selfdict);
7194 return ret;
7195}
7196
7197/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 * Make a copy of an item.
7199 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007200 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007201 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7202 * reference to an already copied list/dict can be used.
7203 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007206item_copy(
7207 typval_T *from,
7208 typval_T *to,
7209 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007210 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007211 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212{
7213 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007215
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007217 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007218 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 }
7221 ++recurse;
7222
7223 switch (from->v_type)
7224 {
7225 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007226 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 case VAR_STRING:
7228 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007229 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007230 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007231 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007232 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007233 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007234 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007235 case VAR_CLASS:
7236 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007237 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 copy_tv(from, to);
7239 break;
7240 case VAR_LIST:
7241 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007242 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007243 if (from->vval.v_list == NULL)
7244 to->vval.v_list = NULL;
7245 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007247 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 to->vval.v_list = from->vval.v_list->lv_copylist;
7249 ++to->vval.v_list->lv_refcount;
7250 }
7251 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007252 to->vval.v_list = list_copy(from->vval.v_list,
7253 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007254 if (to->vval.v_list == NULL)
7255 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007257 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007259 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 case VAR_DICT:
7261 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007262 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007263 if (from->vval.v_dict == NULL)
7264 to->vval.v_dict = NULL;
7265 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007267 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007268 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7269 ++to->vval.v_dict->dv_refcount;
7270 }
7271 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007272 to->vval.v_dict = dict_copy(from->vval.v_dict,
7273 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007274 if (to->vval.v_dict == NULL)
7275 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007277 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007278 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007280 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 }
7283 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285}
7286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 void
7288echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7289{
7290 char_u *tofree;
7291 char_u numbuf[NUMBUFLEN];
7292 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7293
7294 if (*atstart)
7295 {
7296 *atstart = FALSE;
7297 // Call msg_start() after eval1(), evaluating the expression
7298 // may cause a message to appear.
7299 if (with_space)
7300 {
7301 // Mark the saved text as finishing the line, so that what
7302 // follows is displayed on a new line when scrolling back
7303 // at the more prompt.
7304 msg_sb_eol();
7305 msg_start();
7306 }
7307 }
7308 else if (with_space)
7309 msg_puts_attr(" ", echo_attr);
7310
7311 if (p != NULL)
7312 for ( ; *p != NUL && !got_int; ++p)
7313 {
7314 if (*p == '\n' || *p == '\r' || *p == TAB)
7315 {
7316 if (*p != TAB && *needclr)
7317 {
7318 // remove any text still there from the command
7319 msg_clr_eos();
7320 *needclr = FALSE;
7321 }
7322 msg_putchar_attr(*p, echo_attr);
7323 }
7324 else
7325 {
7326 if (has_mbyte)
7327 {
7328 int i = (*mb_ptr2len)(p);
7329
7330 (void)msg_outtrans_len_attr(p, i, echo_attr);
7331 p += i - 1;
7332 }
7333 else
7334 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7335 }
7336 }
7337 vim_free(tofree);
7338}
7339
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 * ":echo expr1 ..." print each argument separated with a space, add a
7342 * newline at the end.
7343 * ":echon expr1 ..." print each argument plain.
7344 */
7345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007350 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 int needclr = TRUE;
7352 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007353 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007354 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007355 evalarg_T evalarg;
7356
Bram Moolenaare707c882020-07-01 13:07:37 +02007357 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359 if (eap->skip)
7360 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007361 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007363 // If eval1() causes an error message the text from the command may
7364 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007365 need_clr_eos = needclr;
7366
Bram Moolenaar68db9962021-05-09 23:19:22 +02007367 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007368 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 /*
7371 * Report the invalid expression unless the expression evaluation
7372 * has been cancelled due to an aborting error, an interrupt, or an
7373 * exception.
7374 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007375 if (!aborting() && did_emsg == did_emsg_before
7376 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007377 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007378 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 break;
7380 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007381 need_clr_eos = FALSE;
7382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007384 {
7385 if (rettv.v_type == VAR_VOID)
7386 {
7387 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7388 break;
7389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007391 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 arg = skipwhite(arg);
7395 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007396 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007397 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399 if (eap->skip)
7400 --emsg_skip;
7401 else
7402 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007403 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (needclr)
7405 msg_clr_eos();
7406 if (eap->cmdidx == CMD_echo)
7407 msg_end();
7408 }
7409}
7410
7411/*
7412 * ":echohl {name}".
7413 */
7414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007417 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
7420/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007421 * Returns the :echo attribute
7422 */
7423 int
7424get_echo_attr(void)
7425{
7426 return echo_attr;
7427}
7428
7429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 * ":execute expr1 ..." execute the result of an expression.
7431 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007432 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007434 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 * Each gets spaces around each argument and a newline at the end for
7436 * echo commands
7437 */
7438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007439ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 int ret = OK;
7444 char_u *p;
7445 garray_T ga;
7446 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007447 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 ga_init2(&ga, 1, 80);
7450
7451 if (eap->skip)
7452 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007453 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007455 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007456 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 if (!eap->skip)
7460 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007461 char_u buf[NUMBUFLEN];
7462
7463 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007464 {
7465 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7466 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007467 semsg(_(e_using_invalid_value_as_string_str),
7468 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007469 p = NULL;
7470 }
7471 else
7472 p = tv_get_string_buf(&rettv, buf);
7473 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007474 else
7475 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007476 if (p == NULL)
7477 {
7478 clear_tv(&rettv);
7479 ret = FAIL;
7480 break;
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 len = (int)STRLEN(p);
7483 if (ga_grow(&ga, len + 2) == FAIL)
7484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 ret = FAIL;
7487 break;
7488 }
7489 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 ga.ga_len += len;
7493 }
7494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 arg = skipwhite(arg);
7497 }
7498
7499 if (ret != FAIL && ga.ga_data != NULL)
7500 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007501 // use the first line of continuation lines for messages
7502 SOURCING_LNUM = start_lnum;
7503
Bram Moolenaar37fef162022-08-29 18:16:32 +01007504 if (eap->cmdidx == CMD_echomsg
7505 || eap->cmdidx == CMD_echowindow
7506 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007507 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007508 // Mark the already saved text as finishing the line, so that what
7509 // follows is displayed on a new line when scrolling back at the
7510 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007511 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007512 }
7513
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007514 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007515 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007516 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007517 out_flush();
7518 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007519 else if (eap->cmdidx == CMD_echowindow)
7520 {
7521#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007522 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007523#endif
7524 msg_attr(ga.ga_data, echo_attr);
7525#ifdef HAS_MESSAGE_WINDOW
7526 end_echowindow();
7527#endif
7528 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007529 else if (eap->cmdidx == CMD_echoconsole)
7530 {
7531 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7532 ui_write((char_u *)"\r\n", 2, TRUE);
7533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 else if (eap->cmdidx == CMD_echoerr)
7535 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007536 int save_did_emsg = did_emsg;
7537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007538 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007539 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 if (!force_abort)
7541 did_emsg = save_did_emsg;
7542 }
7543 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007544 {
7545 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7546
7547 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7548 sticky_cmdmod_flags = cmdmod.cmod_flags
7549 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007551 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007552 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555
7556 ga_clear(&ga);
7557
7558 if (eap->skip)
7559 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007560 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561}
7562
7563/*
7564 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7565 * "arg" points to the "&" or '+' when called, to "option" when returning.
7566 * Returns NULL when no option name found. Otherwise pointer to the char
7567 * after the option name.
7568 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007569 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007570find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
7572 char_u *p = *arg;
7573
7574 ++p;
7575 if (*p == 'g' && p[1] == ':')
7576 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007577 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 p += 2;
7579 }
7580 else if (*p == 'l' && p[1] == ':')
7581 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007582 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 p += 2;
7584 }
7585 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007586 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
7588 if (!ASCII_ISALPHA(*p))
7589 return NULL;
7590 *arg = p;
7591
7592 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007593 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 else
7595 while (ASCII_ISALPHA(*p))
7596 ++p;
7597 return p;
7598}
7599
7600/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007601 * Display script name where an item was last set.
7602 * Should only be invoked when 'verbose' is non-zero.
7603 */
7604 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007605last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007606{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007607 char_u *p;
7608
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007609 if (script_ctx.sc_sid == 0)
7610 return;
7611
7612 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7613 if (p == NULL)
7614 return;
7615
7616 verbose_enter();
7617 msg_puts(_("\n\tLast set from "));
7618 msg_puts((char *)p);
7619 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007620 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007621 msg_puts(_(line_msg));
7622 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007623 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007624 verbose_leave();
7625 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007626}
7627
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007628#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
7630/*
7631 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007632 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 * "flags" can be "g" to do a global substitute.
7634 * Returns an allocated string, NULL for error.
7635 */
7636 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007637do_string_sub(
7638 char_u *str,
John Marriottbd4614f2024-11-18 20:25:21 +01007639 size_t len,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007640 char_u *pat,
7641 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007642 typval_T *expr,
John Marriottbd4614f2024-11-18 20:25:21 +01007643 char_u *flags,
7644 size_t *ret_len) // length of returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 garray_T ga;
7648 char_u *ret;
7649 char_u *save_cpo;
7650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007651 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007653 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 ga_init2(&ga, 1, 200);
7656
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 regmatch.rm_ic = p_ic;
7658 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7659 if (regmatch.regprog != NULL)
7660 {
John Marriottbd4614f2024-11-18 20:25:21 +01007661 char_u *tail = str;
7662 char_u *end = str + len;
7663 int do_all = (flags[0] == 'g');
7664 int sublen;
7665 int i;
7666 char_u *zero_width = NULL;
7667
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007671 if (regmatch.startp[0] == regmatch.endp[0])
7672 {
7673 if (zero_width == regmatch.startp[0])
7674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007675 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007676 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007677 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7678 (size_t)i);
7679 ga.ga_len += i;
7680 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007681 continue;
7682 }
7683 zero_width = regmatch.startp[0];
7684 }
7685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 /*
7687 * Get some space for a temporary buffer to do the substitution
7688 * into. It will contain:
7689 * - The text up to where the match is.
7690 * - The substituted text.
7691 * - The text after the match.
7692 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007693 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007694 if (sublen <= 0)
7695 {
7696 ga_clear(&ga);
7697 break;
7698 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007699 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7701 {
7702 ga_clear(&ga);
7703 break;
7704 }
7705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007706 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 i = (int)(regmatch.startp[0] - tail);
7708 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007709 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007710 (void)vim_regsub(&regmatch, sub, expr,
7711 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7712 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007714 tail = regmatch.endp[0];
7715 if (*tail == NUL)
7716 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (!do_all)
7718 break;
7719 }
7720
7721 if (ga.ga_data != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01007722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
John Marriottbd4614f2024-11-18 20:25:21 +01007724 ga.ga_len += (int)(end - tail);
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
Bram Moolenaar473de612013-06-08 18:19:48 +02007727 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
7729
John Marriottbd4614f2024-11-18 20:25:21 +01007730 if (ga.ga_data != NULL)
7731 {
7732 str = (char_u *)ga.ga_data;
7733 len = (size_t)ga.ga_len;
7734 }
7735 ret = vim_strnsave(str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007737 if (p_cpo == empty_option)
7738 p_cpo = save_cpo;
7739 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007741 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007742 // If it's still empty it was changed and restored, need to restore in
7743 // the complicated way.
7744 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007745 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007746 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
John Marriottbd4614f2024-11-18 20:25:21 +01007749 if (ret_len != NULL)
7750 *ret_len = len;
7751
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 return ret;
7753}