blob: 23237c2c702c94674617a5d8221796b00663668d [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.
274 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
275 partial, NULL, fc, rettv);
276 if (fc_arg == NULL)
277 remove_funccal();
278 if (r == FAIL)
279 return FAIL;
280 }
281 else
282 {
283 char_u *s = partial_name(partial);
284 funcexe_T funcexe;
285
286 if (s == NULL || *s == NUL)
287 return FAIL;
288
289 CLEAR_FIELD(funcexe);
290 funcexe.fe_evaluate = TRUE;
291 funcexe.fe_partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
295
296 return OK;
297}
298
299/*
300 * Evaluate an expression which is a function.
301 * Pass arguments "argv[argc]".
302 * Return the result in "rettv" and OK or FAIL.
303 */
304 static int
305eval_expr_func(
306 typval_T *expr,
307 typval_T *argv,
308 int argc,
309 typval_T *rettv)
310{
311 funcexe_T funcexe;
312 char_u buf[NUMBUFLEN];
313 char_u *s;
314
315 if (expr->v_type == VAR_FUNC)
316 s = expr->vval.v_string;
317 else
318 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
319 if (s == NULL || *s == NUL)
320 return FAIL;
321
322 CLEAR_FIELD(funcexe);
323 funcexe.fe_evaluate = TRUE;
324 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
325 return FAIL;
326
327 return OK;
328}
329
330/*
331 * Evaluate an expression, which is a string.
332 * Return the result in "rettv" and OK or FAIL.
333 */
334 static int
335eval_expr_string(
336 typval_T *expr,
337 typval_T *rettv)
338{
339 char_u *s;
340 char_u buf[NUMBUFLEN];
341
342 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
343 if (s == NULL)
344 return FAIL;
345
346 s = skipwhite(s);
347 if (eval1_emsg(&s, rettv, NULL) == FAIL)
348 return FAIL;
349
350 if (*skipwhite(s) != NUL) // check for trailing chars after expr
351 {
352 clear_tv(rettv);
353 semsg(_(e_invalid_expression_str), s);
354 return FAIL;
355 }
356
357 return OK;
358}
359
360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 * Evaluate an expression, which can be a function, partial or string.
362 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200363 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100364 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 * Return the result in "rettv" and OK or FAIL.
366 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200367 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100368eval_expr_typval(
369 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200370 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100371 typval_T *argv,
372 int argc,
373 funccall_T *fc_arg,
374 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100375{
zeertzjqad0c4422023-08-17 22:15:47 +0200376 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200377 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200378 else if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200379 return exe_typval_instr(expr, rettv);
zeertzjqad0c4422023-08-17 22:15:47 +0200380 else if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200381 return eval_expr_func(expr, argv, argc, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100382 else
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200383 return eval_expr_string(expr, rettv);
384
Bram Moolenaar48570482017-10-30 21:48:41 +0100385 return OK;
386}
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(
1302 char_u *name,
1303 lval_T *lp,
1304 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)
1320 {
1321 clear_tv(var1);
1322 return GLV_FAIL;
1323 }
1324 }
1325 lp->ll_list = NULL;
1326 lp->ll_object = NULL;
1327 lp->ll_class = NULL;
1328
1329 // a NULL dict is equivalent with an empty dict
1330 if (lp->ll_tv->vval.v_dict == NULL)
1331 {
1332 lp->ll_tv->vval.v_dict = dict_alloc();
1333 if (lp->ll_tv->vval.v_dict == NULL)
1334 {
1335 clear_tv(var1);
1336 return GLV_FAIL;
1337 }
1338 ++lp->ll_tv->vval.v_dict->dv_refcount;
1339 }
1340 lp->ll_dict = lp->ll_tv->vval.v_dict;
1341
1342 lp->ll_di = dict_find(lp->ll_dict, key, len);
1343
1344 // When assigning to a scope dictionary check that a function and
1345 // variable name is valid (only variable name unless it is l: or
1346 // g: dictionary). Disallow overwriting a builtin function.
1347 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1348 {
1349 int prevval;
1350
1351 if (len != -1)
1352 {
1353 prevval = key[len];
1354 key[len] = NUL;
1355 }
1356 else
1357 prevval = 0; // avoid compiler warning
1358 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1359 && (rettv->v_type == VAR_FUNC
1360 || rettv->v_type == VAR_PARTIAL)
1361 && var_wrong_func_name(key, lp->ll_di == NULL))
1362 || !valid_varname(key, -1, TRUE);
1363 if (len != -1)
1364 key[len] = prevval;
1365 if (wrong)
1366 {
1367 clear_tv(var1);
1368 return GLV_FAIL;
1369 }
1370 }
1371
1372 if (lp->ll_valtype != NULL)
1373 // use the type of the member
1374 lp->ll_valtype = lp->ll_valtype->tt_member;
1375
1376 if (lp->ll_di == NULL)
1377 {
1378 // Can't add "v:" or "a:" variable.
1379 if (lp->ll_dict == get_vimvar_dict()
1380 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1381 {
1382 semsg(_(e_illegal_variable_name_str), name);
1383 clear_tv(var1);
1384 return GLV_FAIL;
1385 }
1386
1387 // Key does not exist in dict: may need to add it.
1388 if (*p == '[' || *p == '.' || unlet)
1389 {
1390 if (!quiet)
1391 semsg(_(e_key_not_present_in_dictionary_str), key);
1392 clear_tv(var1);
1393 return GLV_FAIL;
1394 }
1395 if (len == -1)
1396 lp->ll_newkey = vim_strsave(key);
1397 else
1398 lp->ll_newkey = vim_strnsave(key, len);
1399 clear_tv(var1);
1400 if (lp->ll_newkey == NULL)
1401 p = NULL;
1402
1403 *key_end = p;
1404 return GLV_STOP;
1405 }
1406 // existing variable, need to check if it can be changed
1407 else if ((flags & GLV_READ_ONLY) == 0
1408 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1409 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1410 {
1411 clear_tv(var1);
1412 return GLV_FAIL;
1413 }
1414
1415 clear_tv(var1);
1416 lp->ll_tv = &lp->ll_di->di_tv;
1417
1418 return GLV_OK;
1419}
1420
1421/*
1422 * Get an blob lval variable that can be assigned a value to: "name",
1423 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1424 *
1425 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1426 * blob index. If the first index is not specified in a range, then 'empty1'
1427 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1428 * invalid indexes.
1429 *
1430 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1431 */
1432 static int
1433get_lval_blob(
1434 lval_T *lp,
1435 typval_T *var1,
1436 typval_T *var2,
1437 int empty1,
1438 int quiet)
1439{
1440 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1441
1442 // Get the number and item for the only or first index of the List.
1443 if (empty1)
1444 lp->ll_n1 = 0;
1445 else
1446 // is number or string
1447 lp->ll_n1 = (long)tv_get_number(var1);
1448 clear_tv(var1);
1449
1450 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
1451 {
1452 clear_tv(var2);
1453 return FAIL;
1454 }
1455 if (lp->ll_range && !lp->ll_empty2)
1456 {
1457 lp->ll_n2 = (long)tv_get_number(var2);
1458 clear_tv(var2);
1459 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1460 return FAIL;
1461 }
1462
1463 if (!lp->ll_range)
1464 // Indexing a single byte in a blob. So the rhs type is a
1465 // number.
1466 lp->ll_valtype = &t_number;
1467
1468 lp->ll_blob = lp->ll_tv->vval.v_blob;
1469 lp->ll_tv = NULL;
1470
1471 return OK;
1472}
1473
1474/*
1475 * Get a List lval variable that can be assigned a value to: "name",
1476 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1477 *
1478 * 'var1' specifies the starting List index and 'var2' specifies the ending
1479 * List index. If the first index is not specified in a range, then 'empty1'
1480 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1481 * invalid indexes.
1482 *
1483 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1484 */
1485 static int
1486get_lval_list(
1487 lval_T *lp,
1488 typval_T *var1,
1489 typval_T *var2,
1490 int empty1,
1491 int flags,
1492 int quiet)
1493{
1494 /*
1495 * Get the number and item for the only or first index of the List.
1496 */
1497 if (empty1)
1498 lp->ll_n1 = 0;
1499 else
1500 // is number or string
1501 lp->ll_n1 = (long)tv_get_number(var1);
1502 clear_tv(var1);
1503
1504 lp->ll_dict = NULL;
1505 lp->ll_object = NULL;
1506 lp->ll_class = NULL;
1507 lp->ll_list = lp->ll_tv->vval.v_list;
1508 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1509 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1510 if (lp->ll_li == NULL)
1511 {
1512 clear_tv(var2);
1513 return FAIL;
1514 }
1515
1516 if (lp->ll_valtype != NULL && !lp->ll_range)
1517 // use the type of the member
1518 lp->ll_valtype = lp->ll_valtype->tt_member;
1519
1520 /*
1521 * May need to find the item or absolute index for the second
1522 * index of a range.
1523 * When no index given: "lp->ll_empty2" is TRUE.
1524 * Otherwise "lp->ll_n2" is set to the second index.
1525 */
1526 if (lp->ll_range && !lp->ll_empty2)
1527 {
1528 lp->ll_n2 = (long)tv_get_number(var2);
1529 // is number or string
1530 clear_tv(var2);
1531 if (check_range_index_two(lp->ll_list,
1532 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1533 return FAIL;
1534 }
1535
1536 lp->ll_tv = &lp->ll_li->li_tv;
1537
1538 return OK;
1539}
1540
1541/*
1542 * Get a Class or Object lval variable that can be assigned a value to:
1543 * "name", "name.key", "name.key[expr]" etc.
1544 *
1545 * 'cl_exec' is the class that is executing, or NULL. 'v_type' is VAR_CLASS or
1546 * VAR_OBJECT. 'key' points to the member variable name and 'key_end' points
1547 * to the character after 'key'. If 'quiet' is TRUE, then error messages
1548 * are not displayed for invalid indexes.
1549 *
1550 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1551 * failure.
1552 */
1553 static int
1554get_lval_class_or_obj(
1555 class_T *cl_exec,
1556 vartype_T v_type,
1557 lval_T *lp,
1558 char_u *key,
1559 char_u *key_end,
1560 int flags,
1561 int quiet)
1562{
1563 lp->ll_dict = NULL;
1564 lp->ll_list = NULL;
1565
1566 class_T *cl;
1567 if (v_type == VAR_OBJECT)
1568 {
1569 if (lp->ll_tv->vval.v_object == NULL)
1570 {
1571 if (!quiet)
1572 emsg(_(e_using_null_object));
1573 return FAIL;
1574 }
1575 cl = lp->ll_tv->vval.v_object->obj_class;
1576 lp->ll_object = lp->ll_tv->vval.v_object;
1577 }
1578 else
1579 {
1580 cl = lp->ll_tv->vval.v_class;
1581 lp->ll_object = NULL;
1582 }
1583 lp->ll_class = cl;
1584
1585 // TODO: what if class is NULL?
1586 if (cl != NULL)
1587 {
1588 lp->ll_valtype = NULL;
1589
1590 if (flags & GLV_PREFER_FUNC)
1591 {
1592 // First look for a function with this name.
1593 // round 1: class functions (skipped for an object)
1594 // round 2: object methods
1595 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1596 round <= 2; ++round)
1597 {
1598 int m_idx;
1599 ufunc_T *fp;
1600
1601 fp = method_lookup(cl,
1602 round == 1 ? VAR_CLASS : VAR_OBJECT,
1603 key, key_end - key, &m_idx);
1604 lp->ll_oi = m_idx;
1605 if (fp != NULL)
1606 {
1607 lp->ll_ufunc = fp;
1608 lp->ll_valtype = fp->uf_func_type;
1609 break;
1610 }
1611 }
1612 }
1613
1614 if (lp->ll_valtype == NULL)
1615 {
1616 int m_idx;
1617 ocmember_T *om
1618 = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1619 lp->ll_oi = m_idx;
1620 if (om != NULL)
1621 {
1622 if (get_lval_check_access(cl_exec, cl, om,
1623 key_end, flags) == FAIL)
1624 return FAIL;
1625
1626 // When lhs is used to modify the variable, check it is
1627 // not a read-only variable.
1628 if ((flags & GLV_READ_ONLY) == 0
1629 && (*key_end != '.' && *key_end != '[')
1630 && oc_var_check_ro(cl, om))
1631 return FAIL;
1632
1633 lp->ll_valtype = om->ocm_type;
1634
1635 if (v_type == VAR_OBJECT)
1636 lp->ll_tv = ((typval_T *)(
1637 lp->ll_tv->vval.v_object + 1)) + m_idx;
1638 else
1639 lp->ll_tv = &cl->class_members_tv[m_idx];
1640 }
1641 }
1642
1643 if (lp->ll_valtype == NULL)
1644 {
1645 member_not_found_msg(cl, v_type, key, key_end - key);
1646 return FAIL;
1647 }
1648 }
1649
1650 return OK;
1651}
1652
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001653/*
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001654 * Get the lval of a list/dict/blob/object/class subitem starting at "p". Loop
1655 * until no more [idx] or .key is following.
1656 *
1657 * Returns a pointer to the character after the subscript on success or NULL on
1658 * failure.
1659 */
1660 static char_u *
1661get_lval_subscript(
1662 lval_T *lp,
1663 char_u *p,
1664 char_u *name,
1665 typval_T *rettv,
1666 hashtab_T *ht,
1667 dictitem_T *v,
1668 int unlet,
1669 int flags, // GLV_ values
1670 class_T *cl_exec)
1671{
1672 int vim9script = in_vim9script();
1673 int quiet = flags & GLV_QUIET;
1674 char_u *key = NULL;
1675 int len;
1676 typval_T var1;
1677 typval_T var2;
1678 int empty1 = FALSE;
1679
1680 /*
1681 * Loop until no more [idx] or .key is following.
1682 */
1683 var1.v_type = VAR_UNKNOWN;
1684 var2.v_type = VAR_UNKNOWN;
1685
1686 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1687 {
1688 vartype_T v_type = lp->ll_tv->v_type;
1689
1690 if (*p == '.' && v_type != VAR_DICT
1691 && v_type != VAR_OBJECT
1692 && v_type != VAR_CLASS)
1693 {
1694 if (!quiet)
1695 semsg(_(e_dot_not_allowed_after_str_str),
1696 vartype_name(v_type), name);
1697 return NULL;
1698 }
1699 if (v_type != VAR_LIST
1700 && v_type != VAR_DICT
1701 && v_type != VAR_BLOB
1702 && v_type != VAR_OBJECT
1703 && v_type != VAR_CLASS)
1704 {
1705 if (!quiet)
1706 semsg(_(e_index_not_allowed_after_str_str),
1707 vartype_name(v_type), name);
1708 return NULL;
1709 }
1710
1711 // A NULL list/blob works like an empty list/blob, allocate one now.
1712 int r = OK;
1713 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1714 r = rettv_list_alloc(lp->ll_tv);
1715 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1716 r = rettv_blob_alloc(lp->ll_tv);
1717 if (r == FAIL)
1718 return NULL;
1719
1720 if (lp->ll_range)
1721 {
1722 if (!quiet)
1723 emsg(_(e_slice_must_come_last));
1724 return NULL;
1725 }
1726#ifdef LOG_LOCKVAR
1727 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1728 vartype_name(v_type));
1729#endif
1730
1731 if (vim9script && lp->ll_valtype == NULL
1732 && v != NULL
1733 && lp->ll_tv == &v->di_tv
1734 && ht != NULL && ht == get_script_local_ht())
1735 {
1736 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1737
1738 // Vim9 script local variable: get the type
1739 if (sv != NULL)
1740 {
1741 lp->ll_valtype = sv->sv_type;
1742#ifdef LOG_LOCKVAR
1743 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1744 vartype_name(lp->ll_valtype->tt_type));
1745#endif
1746 }
1747 }
1748
1749 len = -1;
1750 if (*p == '.')
1751 {
1752 key = p + 1;
1753
1754 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1755 ;
1756 if (len == 0)
1757 {
1758 if (!quiet)
1759 emsg(_(e_cannot_use_empty_key_for_dictionary));
1760 return NULL;
1761 }
1762 p = key + len;
1763 }
1764 else
1765 {
1766 // Get the index [expr] or the first index [expr: ].
1767 p = skipwhite(p + 1);
1768 if (*p == ':')
1769 empty1 = TRUE;
1770 else
1771 {
1772 empty1 = FALSE;
1773 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
1774 return NULL;
1775 if (tv_get_string_chk(&var1) == NULL)
1776 {
1777 // not a number or string
1778 clear_tv(&var1);
1779 return NULL;
1780 }
1781 p = skipwhite(p);
1782 }
1783
1784 // Optionally get the second index [ :expr].
1785 if (*p == ':')
1786 {
1787 if (v_type == VAR_DICT)
1788 {
1789 if (!quiet)
1790 emsg(_(e_cannot_slice_dictionary));
1791 clear_tv(&var1);
1792 return NULL;
1793 }
1794 if (rettv != NULL
1795 && !(rettv->v_type == VAR_LIST
1796 && rettv->vval.v_list != NULL)
1797 && !(rettv->v_type == VAR_BLOB
1798 && rettv->vval.v_blob != NULL))
1799 {
1800 if (!quiet)
1801 emsg(_(e_slice_requires_list_or_blob_value));
1802 clear_tv(&var1);
1803 return NULL;
1804 }
1805 p = skipwhite(p + 1);
1806 if (*p == ']')
1807 lp->ll_empty2 = TRUE;
1808 else
1809 {
1810 lp->ll_empty2 = FALSE;
1811 // recursive!
1812 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
1813 {
1814 clear_tv(&var1);
1815 return NULL;
1816 }
1817 if (tv_get_string_chk(&var2) == NULL)
1818 {
1819 // not a number or string
1820 clear_tv(&var1);
1821 clear_tv(&var2);
1822 return NULL;
1823 }
1824 }
1825 lp->ll_range = TRUE;
1826 }
1827 else
1828 lp->ll_range = FALSE;
1829
1830 if (*p != ']')
1831 {
1832 if (!quiet)
1833 emsg(_(e_missing_closing_square_brace));
1834 clear_tv(&var1);
1835 clear_tv(&var2);
1836 return NULL;
1837 }
1838
1839 // Skip to past ']'.
1840 ++p;
1841 }
1842#ifdef LOG_LOCKVAR
1843 if (len == -1)
1844 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1845 empty1 ? ":" : (char*)tv_get_string(&var1));
1846 else
1847 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
1848#endif
1849
1850 if (v_type == VAR_DICT)
1851 {
1852 glv_status_T glv_status;
1853
1854 glv_status = get_lval_dict_item(name, lp, key, len, &p, &var1,
1855 flags, unlet, rettv);
1856 if (glv_status == GLV_FAIL)
1857 return NULL;
1858 if (glv_status == GLV_STOP)
1859 break;
1860 }
1861 else if (v_type == VAR_BLOB)
1862 {
1863 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
1864 return NULL;
1865
1866 break;
1867 }
1868 else if (v_type == VAR_LIST)
1869 {
1870 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
1871 return NULL;
1872 }
1873 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
1874 {
1875 if (get_lval_class_or_obj(cl_exec, v_type, lp, key, p, flags,
1876 quiet) == FAIL)
1877 return NULL;
1878 }
1879 }
1880
1881 clear_tv(&var1);
1882 return p;
1883}
1884
1885/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 * Get an lval: variable, Dict item or List item that can be assigned a value
1887 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1888 * "name.key", "name.key[expr]" etc.
1889 * Indexing only works if "name" is an existing List or Dictionary.
1890 * "name" points to the start of the name.
1891 * If "rettv" is not NULL it points to the value to be assigned.
1892 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1893 * wrong; must end in space or cmd separator.
1894 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001895 * flags:
1896 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001897 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001898 * GLV_NO_AUTOLOAD: do not use script autoloading
1899 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001901 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905get_lval(
1906 char_u *name,
1907 typval_T *rettv,
1908 lval_T *lp,
1909 int unlet,
1910 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 int flags, // GLV_ values
1912 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 char_u *expr_start, *expr_end;
1916 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001917 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001918 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001919 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001920 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001921 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001922 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923
Ernie Raelee865f32023-09-29 19:53:55 +02001924#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001925 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001926 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001927 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001928 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1929 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001930 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001931 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001932 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001933#endif
1934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001935 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001936 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001938 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001940 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001942 lp->ll_name_end = find_name_end(name, NULL, NULL,
1943 FNE_INCL_BR | fne_flags);
1944 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 }
1946
Bram Moolenaara749a422022-02-12 19:52:25 +00001947 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001948 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001949 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1950 {
1951 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1952 return NULL;
1953 }
1954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001955 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001956 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (expr_start != NULL)
1959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001961 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 && *p != '[' && *p != '.')
1963 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001964 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
1966 }
1967
1968 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1969 if (lp->ll_exp_name == NULL)
1970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 // Report an invalid expression in braces, unless the
1972 // expression evaluation has been cancelled due to an
1973 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!aborting() && !quiet)
1975 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001976 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001977 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 return NULL;
1979 }
1980 }
1981 lp->ll_name = lp->ll_exp_name;
1982 }
1983 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 lp->ll_name = name;
1986
Bram Moolenaar4525a572022-02-13 11:57:33 +00001987 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001989 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001990 // However, "g:[key]" is indexing a dictionary.
1991 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001992 {
1993 --p;
1994 lp->ll_name_end = p;
1995 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001996 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001997 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001998 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001999
Bram Moolenaar9510d222022-09-11 15:14:05 +01002000 if (is_scoped_variable(name))
2001 {
2002 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2003 return NULL;
2004 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002005 if (VIM_ISWHITE(*p))
2006 {
2007 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2008 return NULL;
2009 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002010 if (tp == p + 1 && !quiet)
2011 {
2012 semsg(_(e_white_space_required_after_str_str), ":", p);
2013 return NULL;
2014 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002015 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002016 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002017 semsg(_(e_using_type_not_in_script_context_str), p);
2018 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002019 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002020 if (vim9script && (flags & GLV_NO_DECL) &&
2021 !(flags & GLV_FOR_LOOP))
2022 {
2023 // Using a type and not in a "var" declaration.
2024 semsg(_(e_trailing_characters_str), p);
2025 return NULL;
2026 }
2027
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002028
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002029 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002030 lp->ll_type = parse_type(&tp,
2031 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2032 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002033 if (lp->ll_type == NULL && !quiet)
2034 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002035 lp->ll_name_end = tp;
2036 }
Ernie Raelee865f32023-09-29 19:53:55 +02002037 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 }
2039 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002040 if (lp->ll_name == NULL)
2041 return p;
2042
Bram Moolenaar62aec932022-01-29 21:45:34 +00002043 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002044 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002045 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002046 if (import != NULL)
2047 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002048 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002049 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002050 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002051 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002052 }
2053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
Ernie Rael0f058d12023-10-14 11:25:04 +02002055 // Without [idx] or .key we are done.
2056 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002057 {
2058 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002059 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002061 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062
Ernie Rael0f058d12023-10-14 11:25:04 +02002063 if (vim9script && lval_root != NULL)
2064 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002065 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002066 {
2067 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002068 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002069 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002070 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002071 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002072 {
2073 cc = *p;
2074 *p = NUL;
2075 // When we would write to the variable pass &ht and prevent autoload.
2076 writing = !(flags & GLV_READ_ONLY);
2077 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002078 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002079 if (v == NULL && !quiet)
2080 semsg(_(e_undefined_variable_str), lp->ll_name);
2081 *p = cc;
2082 if (v == NULL)
2083 return NULL;
2084 lp->ll_tv = &v->di_tv;
2085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086
Bram Moolenaar4525a572022-02-13 11:57:33 +00002087 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002088 {
2089 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002090 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002091 return NULL;
2092 }
2093
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002094 // If the next character is a "." or a "[", then process the subitem.
2095 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2096 if (p == NULL)
2097 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002099 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2100 {
2101 where_T where = WHERE_INIT;
2102
2103 // In a vim9 script, do type check and make sure the variable is
2104 // writable.
2105 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2106 return NULL;
2107 }
2108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 return p;
2111}
2112
2113/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002117clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118{
2119 vim_free(lp->ll_exp_name);
2120 vim_free(lp->ll_newkey);
2121}
2122
2123/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002124 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002126 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2127 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002130set_var_lval(
2131 lval_T *lp,
2132 char_u *endp,
2133 typval_T *rettv,
2134 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002135 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2136 char_u *op,
2137 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138{
2139 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141
2142 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002144 cc = *endp;
2145 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002146 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002147 return;
2148
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002149 if (lp->ll_blob != NULL)
2150 {
2151 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002152
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002153 if (op != NULL && *op != '=')
2154 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002155 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002156 return;
2157 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002158 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002159 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002160
2161 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2162 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002163 if (lp->ll_empty2)
2164 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2165
Bram Moolenaar68452172021-04-12 21:21:02 +02002166 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2167 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002168 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002169 }
2170 else
2171 {
2172 val = (int)tv_get_number_chk(rettv, &error);
2173 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002174 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002175 }
2176 }
2177 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002179 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002180
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002181 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2182 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002183 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002184 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002185 *endp = cc;
2186 return;
2187 }
2188
Bram Moolenaarff697e62019-02-12 22:28:33 +01002189 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002190 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002191 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002192 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002193 {
Ernie Raele75fde62023-12-21 17:18:54 +01002194 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002195 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002196 clear_tv(&tv);
2197 return;
2198 }
2199
Bram Moolenaar79518e22017-02-17 16:31:35 +01002200 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002201 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2202 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002203 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002204 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002205 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002206 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002209 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002210 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002211 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2212 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002213 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002214 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002215 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002216 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002217 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002219 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002220 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002221 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002222 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 else if (lp->ll_range)
2224 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002225 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2226 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002227 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002228 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002229 return;
2230 }
2231
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002232 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2233 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 }
2235 else
2236 {
2237 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002238 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002240 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2241 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002242 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002243 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002244 return;
2245 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002246
2247 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002248 && check_typval_arg_type(lp->ll_valtype, rettv,
2249 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002250 return;
2251
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 if (lp->ll_newkey != NULL)
2253 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 if (op != NULL && *op != '=')
2255 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002256 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 return;
2258 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002259 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2260 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002261 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002263 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002264 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (di == NULL)
2266 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002267 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2268 {
2269 vim_free(di);
2270 return;
2271 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002274 else if (op != NULL && *op != '=')
2275 {
2276 tv_op(lp->ll_tv, rettv, op);
2277 return;
2278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002281
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 /*
2283 * Assign the value to the variable or list item.
2284 */
2285 if (copy)
2286 copy_tv(rettv, lp->ll_tv);
2287 else
2288 {
2289 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002290 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 }
2293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294}
2295
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002296/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002297 * Handle "blob1 += blob2".
2298 * Returns OK or FAIL.
2299 */
2300 static int
2301tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2302{
2303 if (*op != '+' || tv2->v_type != VAR_BLOB)
2304 return FAIL;
2305
2306 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002307 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002308 return OK;
2309
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002310 if (tv1->vval.v_blob == NULL)
2311 {
2312 tv1->vval.v_blob = tv2->vval.v_blob;
2313 ++tv1->vval.v_blob->bv_refcount;
2314 return OK;
2315 }
2316
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002317 blob_T *b1 = tv1->vval.v_blob;
2318 blob_T *b2 = tv2->vval.v_blob;
2319 int len = blob_len(b2);
2320
2321 for (int i = 0; i < len; i++)
2322 ga_append(&b1->bv_ga, blob_get(b2, i));
2323
2324 return OK;
2325}
2326
2327/*
2328 * Handle "list1 += list2".
2329 * Returns OK or FAIL.
2330 */
2331 static int
2332tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2333{
2334 if (*op != '+' || tv2->v_type != VAR_LIST)
2335 return FAIL;
2336
2337 // List += List
2338 if (tv2->vval.v_list == NULL)
2339 return OK;
2340
2341 if (tv1->vval.v_list == NULL)
2342 {
2343 tv1->vval.v_list = tv2->vval.v_list;
2344 ++tv1->vval.v_list->lv_refcount;
2345 }
2346 else
2347 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2348
2349 return OK;
2350}
2351
2352/*
2353 * Handle number operations:
2354 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2355 *
2356 * Returns OK or FAIL.
2357 */
2358 static int
2359tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2360{
2361 varnumber_T n;
2362 int failed = FALSE;
2363
2364 n = tv_get_number(tv1);
2365 if (tv2->v_type == VAR_FLOAT)
2366 {
2367 float_T f = n;
2368
2369 if (*op == '%')
2370 return FAIL;
2371 switch (*op)
2372 {
2373 case '+': f += tv2->vval.v_float; break;
2374 case '-': f -= tv2->vval.v_float; break;
2375 case '*': f *= tv2->vval.v_float; break;
2376 case '/': f /= tv2->vval.v_float; break;
2377 }
2378 clear_tv(tv1);
2379 tv1->v_type = VAR_FLOAT;
2380 tv1->vval.v_float = f;
2381 }
2382 else
2383 {
2384 switch (*op)
2385 {
2386 case '+': n += tv_get_number(tv2); break;
2387 case '-': n -= tv_get_number(tv2); break;
2388 case '*': n *= tv_get_number(tv2); break;
2389 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2390 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2391 }
2392 clear_tv(tv1);
2393 tv1->v_type = VAR_NUMBER;
2394 tv1->vval.v_number = n;
2395 }
2396
2397 return failed ? FAIL : OK;
2398}
2399
2400/*
2401 * Handle "str1 .= str2"
2402 * Returns OK or FAIL.
2403 */
2404 static int
2405tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2406{
2407 char_u numbuf[NUMBUFLEN];
2408 char_u *s;
2409
2410 if (tv2->v_type == VAR_FLOAT)
2411 return FAIL;
2412
2413 // str .= str
2414 s = tv_get_string(tv1);
2415 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2416 clear_tv(tv1);
2417 tv1->v_type = VAR_STRING;
2418 tv1->vval.v_string = s;
2419
2420 return OK;
2421}
2422
2423/*
2424 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2425 * and "tv1 .= tv2"
2426 * Returns OK or FAIL.
2427 */
2428 static int
2429tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2430{
2431 if (tv2->v_type == VAR_LIST)
2432 return FAIL;
2433
2434 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2435 return tv_op_number(tv1, tv2, op);
2436
2437 return tv_op_string(tv1, tv2, op);
2438}
2439
2440/*
2441 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2442 * Returns OK or FAIL.
2443 */
2444 static int
2445tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2446{
2447 float_T f;
2448
2449 if (*op == '%' || *op == '.'
2450 || (tv2->v_type != VAR_FLOAT
2451 && tv2->v_type != VAR_NUMBER
2452 && tv2->v_type != VAR_STRING))
2453 return FAIL;
2454
2455 if (tv2->v_type == VAR_FLOAT)
2456 f = tv2->vval.v_float;
2457 else
2458 f = tv_get_number(tv2);
2459 switch (*op)
2460 {
2461 case '+': tv1->vval.v_float += f; break;
2462 case '-': tv1->vval.v_float -= f; break;
2463 case '*': tv1->vval.v_float *= f; break;
2464 case '/': tv1->vval.v_float /= f; break;
2465 }
2466
2467 return OK;
2468}
2469
2470/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002471 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2472 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 * Returns OK or FAIL.
2474 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002476tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002478 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002479 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002480 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2481 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2482 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002484 semsg(_(e_wrong_variable_type_for_str_equal), op);
2485 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 }
2487
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002488 int retval = FAIL;
2489 switch (tv1->v_type)
2490 {
2491 case VAR_UNKNOWN:
2492 case VAR_ANY:
2493 case VAR_VOID:
2494 case VAR_DICT:
2495 case VAR_FUNC:
2496 case VAR_PARTIAL:
2497 case VAR_BOOL:
2498 case VAR_SPECIAL:
2499 case VAR_JOB:
2500 case VAR_CHANNEL:
2501 case VAR_INSTR:
2502 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002503 case VAR_CLASS:
2504 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002505 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002506
2507 case VAR_BLOB:
2508 retval = tv_op_blob(tv1, tv2, op);
2509 break;
2510
2511 case VAR_LIST:
2512 retval = tv_op_list(tv1, tv2, op);
2513 break;
2514
2515 case VAR_NUMBER:
2516 case VAR_STRING:
2517 retval = tv_op_nr_or_string(tv1, tv2, op);
2518 break;
2519
2520 case VAR_FLOAT:
2521 retval = tv_op_float(tv1, tv2, op);
2522 break;
2523 }
2524
2525 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002526 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002527
2528 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529}
2530
2531/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002532 * Evaluate the expression used in a ":for var in expr" command.
2533 * "arg" points to "var".
2534 * Set "*errp" to TRUE for an error, FALSE otherwise;
2535 * Return a pointer that holds the info. Null when there is an error.
2536 */
2537 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002538eval_for_line(
2539 char_u *arg,
2540 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002541 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002542 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002543{
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002545 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002546 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 typval_T tv;
2548 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002549 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002551 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002553 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 if (fi == NULL)
2555 return NULL;
2556
Bram Moolenaar404557e2021-07-05 21:41:48 +02002557 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2558 &fi->fi_semicolon, FALSE);
2559 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002560 return fi;
2561
Bram Moolenaar404557e2021-07-05 21:41:48 +02002562 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002563 if (expr[0] != 'i' || expr[1] != 'n'
2564 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002566 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2567 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2568 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002569 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002570 return fi;
2571 }
2572
2573 if (skip)
2574 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002575 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2576 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002577 {
2578 *errp = FALSE;
2579 if (!skip)
2580 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002581 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002582 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002583 l = tv.vval.v_list;
2584 if (l == NULL)
2585 {
2586 // a null list is like an empty list: do nothing
2587 clear_tv(&tv);
2588 }
2589 else
2590 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002592 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002594 // No need to increment the refcount, it's already set for
2595 // the list being used in "tv".
2596 fi->fi_list = l;
2597 list_add_watch(l, &fi->fi_lw);
2598 fi->fi_lw.lw_item = l->lv_first;
2599 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002600 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002601 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002602 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002603 fi->fi_bi = 0;
2604 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002605 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002606 typval_T btv;
2607
2608 // Make a copy, so that the iteration still works when the
2609 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002611 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002612 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002613 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002614 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002615 else if (tv.v_type == VAR_STRING)
2616 {
2617 fi->fi_byte_idx = 0;
2618 fi->fi_string = tv.vval.v_string;
2619 tv.vval.v_string = NULL;
2620 if (fi->fi_string == NULL)
2621 fi->fi_string = vim_strsave((char_u *)"");
2622 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002623 else
2624 {
Sean Dewar80d73952021-08-04 19:25:54 +02002625 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002626 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002627 }
2628 }
2629 }
2630 if (skip)
2631 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002632 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002633
2634 return fi;
2635}
2636
2637/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002638 * Used when looping over a :for line, skip the "in expr" part.
2639 */
2640 void
2641skip_for_lines(void *fi_void, evalarg_T *evalarg)
2642{
2643 forinfo_T *fi = (forinfo_T *)fi_void;
2644 int i;
2645
2646 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002647 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002648}
2649
2650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002651 * Use the first item in a ":for" list. Advance to the next.
2652 * Assign the values to the variable (list). "arg" points to the first one.
2653 * Return TRUE when a valid item was found, FALSE when at end of list or
2654 * something wrong.
2655 */
2656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002657next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002658{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002659 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002660 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002661 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002662 ? (ASSIGN_FINAL
2663 // first round: error if variable exists
2664 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002665 | ASSIGN_NO_MEMBER_TYPE
2666 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002667 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002669 int skip_assign = in_vim9script() && arg[0] == '_'
2670 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002671
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002672 if (fi->fi_blob != NULL)
2673 {
2674 typval_T tv;
2675
2676 if (fi->fi_bi >= blob_len(fi->fi_blob))
2677 return FALSE;
2678 tv.v_type = VAR_NUMBER;
2679 tv.v_lock = VAR_FIXED;
2680 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2681 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002682 if (skip_assign)
2683 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002684 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002685 fi->fi_varcount, flag, NULL) == OK;
2686 }
2687
2688 if (fi->fi_string != NULL)
2689 {
2690 typval_T tv;
2691 int len;
2692
2693 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2694 if (len == 0)
2695 return FALSE;
2696 tv.v_type = VAR_STRING;
2697 tv.v_lock = VAR_FIXED;
2698 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2699 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002700 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002701 if (skip_assign)
2702 result = TRUE;
2703 else
2704 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002705 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002706 vim_free(tv.vval.v_string);
2707 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002708 }
2709
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 item = fi->fi_lw.lw_item;
2711 if (item == NULL)
2712 result = FALSE;
2713 else
2714 {
2715 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002716 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002717 if (skip_assign)
2718 result = TRUE;
2719 else
2720 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002721 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722 }
2723 return result;
2724}
2725
2726/*
2727 * Free the structure used to store info used by ":for".
2728 */
2729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002730free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731{
Bram Moolenaar33570922005-01-25 22:26:29 +00002732 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002734 if (fi == NULL)
2735 return;
2736 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002737 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002739 list_unref(fi->fi_list);
2740 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002741 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002742 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002743 else
2744 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002745 vim_free(fi);
2746}
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002749set_context_for_expression(
2750 expand_T *xp,
2751 char_u *arg,
2752 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002754 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002758 if (cmdidx == CMD_let || cmdidx == CMD_var
2759 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002760 {
2761 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002762 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002764 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002765 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766 {
2767 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002768 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002769 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770 break;
2771 }
2772 return;
2773 }
2774 }
2775 else
2776 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2777 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 while ((xp->xp_pattern = vim_strpbrk(arg,
2779 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2780 {
2781 c = *xp->xp_pattern;
2782 if (c == '&')
2783 {
2784 c = xp->xp_pattern[1];
2785 if (c == '&')
2786 {
2787 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002788 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002793 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2794 xp->xp_pattern += 2;
2795
2796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 else if (c == '$')
2799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 xp->xp_context = EXPAND_ENV_VARS;
2802 }
2803 else if (c == '=')
2804 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002805 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 xp->xp_context = EXPAND_EXPRESSION;
2807 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002808 else if (c == '#'
2809 && xp->xp_context == EXPAND_EXPRESSION)
2810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002812 break;
2813 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002814 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 && xp->xp_context == EXPAND_FUNCTIONS
2816 && vim_strchr(xp->xp_pattern, '(') == NULL)
2817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 break;
2820 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002821 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002823 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2826 if (c == '\\' && xp->xp_pattern[1] != NUL)
2827 ++xp->xp_pattern;
2828 xp->xp_context = EXPAND_NOTHING;
2829 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002832 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2834 /* skip */ ;
2835 xp->xp_context = EXPAND_NOTHING;
2836 }
2837 else if (c == '|')
2838 {
2839 if (xp->xp_pattern[1] == '|')
2840 {
2841 ++xp->xp_pattern;
2842 xp->xp_context = EXPAND_EXPRESSION;
2843 }
2844 else
2845 xp->xp_context = EXPAND_COMMANDS;
2846 }
2847 else
2848 xp->xp_context = EXPAND_EXPRESSION;
2849 }
2850 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // Doesn't look like something valid, expand as an expression
2852 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 arg = xp->xp_pattern;
2855 if (*arg != NUL)
2856 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2857 /* skip */ ;
2858 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002859
2860 // ":exe one two" completes "two"
2861 if ((cmdidx == CMD_execute
2862 || cmdidx == CMD_echo
2863 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002864 || cmdidx == CMD_echomsg
2865 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002866 && xp->xp_context == EXPAND_EXPRESSION)
2867 {
2868 for (;;)
2869 {
2870 char_u *n = skiptowhite(arg);
2871
2872 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2873 break;
2874 arg = skipwhite(n);
2875 }
2876 }
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 xp->xp_pattern = arg;
2879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002882 * Return TRUE if "pat" matches "text".
2883 * Does not use 'cpo' and always uses 'magic'.
2884 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002885 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002886pattern_match(char_u *pat, char_u *text, int ic)
2887{
2888 int matches = FALSE;
2889 char_u *save_cpo;
2890 regmatch_T regmatch;
2891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002892 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002893 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002894 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002895 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2896 if (regmatch.regprog != NULL)
2897 {
2898 regmatch.rm_ic = ic;
2899 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2900 vim_regfree(regmatch.regprog);
2901 }
2902 p_cpo = save_cpo;
2903 return matches;
2904}
2905
2906/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002907 * Handle a name followed by "(". Both for just "name(arg)" and for
2908 * "expr->name(arg)".
2909 * Returns OK or FAIL.
2910 */
2911 static int
2912eval_func(
2913 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002914 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002915 char_u *name,
2916 int name_len,
2917 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002918 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002919 typval_T *basetv) // "expr" for "expr->name(arg)"
2920{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002921 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002922 char_u *s = name;
2923 int len = name_len;
2924 partial_T *partial;
2925 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002926 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002927 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002928
2929 if (!evaluate)
2930 check_vars(s, len);
2931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002932 // If "s" is the name of a variable of type VAR_FUNC
2933 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002934 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002935 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002937 // Need to make a copy, in case evaluating the arguments makes
2938 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002939 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002940 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002941 ret = FAIL;
2942 else
2943 {
2944 funcexe_T funcexe;
2945
2946 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002947 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002948 funcexe.fe_firstline = curwin->w_cursor.lnum;
2949 funcexe.fe_lastline = curwin->w_cursor.lnum;
2950 funcexe.fe_evaluate = evaluate;
2951 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002952 if (partial != NULL)
2953 {
2954 funcexe.fe_object = partial->pt_obj;
2955 if (funcexe.fe_object != NULL)
2956 ++funcexe.fe_object->obj_refcount;
2957 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002958 funcexe.fe_basetv = basetv;
2959 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002960 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002961 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002962 }
2963 vim_free(s);
2964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002965 // If evaluate is FALSE rettv->v_type was not set in
2966 // get_func_tv, but it's needed in handle_subscript() to parse
2967 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002968 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2969 {
2970 rettv->vval.v_string = NULL;
2971 rettv->v_type = VAR_FUNC;
2972 }
2973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002974 // Stop the expression evaluation when immediately
2975 // aborting on error, or when an interrupt occurred or
2976 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002977 if (evaluate && aborting())
2978 {
2979 if (ret == OK)
2980 clear_tv(rettv);
2981 ret = FAIL;
2982 }
2983 return ret;
2984}
2985
2986/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002987 * After a NL, skip over empty lines and comment-only lines.
2988 */
2989 static char_u *
2990newline_skip_comments(char_u *arg)
2991{
2992 char_u *p = arg + 1;
2993
2994 for (;;)
2995 {
2996 p = skipwhite(p);
2997
2998 if (*p == NUL)
2999 break;
3000 if (vim9_comment_start(p))
3001 {
3002 char_u *nl = vim_strchr(p, NL);
3003
3004 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003005 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003006 p = nl;
3007 }
3008 if (*p != NL)
3009 break;
3010 ++p; // skip another NL
3011 }
3012 return p;
3013}
3014
3015/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003016 * Get the next line source line without advancing. But do skip over comment
3017 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003018 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003019 */
3020 static char_u *
3021getline_peek_skip_comments(evalarg_T *evalarg)
3022{
3023 for (;;)
3024 {
3025 char_u *next = getline_peek(evalarg->eval_getline,
3026 evalarg->eval_cookie);
3027 char_u *p;
3028
3029 if (next == NULL)
3030 break;
3031 p = skipwhite(next);
3032 if (*p != NUL && !vim9_comment_start(p))
3033 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003034 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003035 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003036 }
3037 return NULL;
3038}
3039
3040/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003041 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3042 * comment) and there is a next line, return the next line (skipping blanks)
3043 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003044 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3045 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046 * "arg" must point somewhere inside a line, not at the start.
3047 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003048 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3050{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003051 char_u *p = skipwhite(arg);
3052
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003054 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003056 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3057 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003058 && (*p == NUL || *p == NL
3059 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003060 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003061 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003062
Bram Moolenaare442d592022-05-05 12:20:28 +01003063 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003064 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003065 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003066 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003067 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003069
Bram Moolenaar9d489562020-07-30 20:08:50 +02003070 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003071 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003072 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003073 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003074 }
3075 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003076 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077}
3078
3079/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003080 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003081 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003082 *
3083 * If "arg" is not NULL, then the caller should assign the return value to
3084 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003085 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003086 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003087eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003088{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003089 garray_T *gap = &evalarg->eval_ga;
3090 char_u *line;
3091
Bram Moolenaar39be4982022-05-06 21:51:50 +01003092 if (arg != NULL)
3093 {
3094 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003095 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003096 // Truncate before a trailing comment, so that concatenating the lines
3097 // won't turn the rest into a comment.
3098 if (*skipwhite(arg) == '#')
3099 *arg = NUL;
3100 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003101
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003102 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003103 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3104 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003105 else
3106 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003107 if (line == NULL)
3108 return NULL;
3109
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003110 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003111 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3112 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003113 char_u *p = skipwhite(line);
3114
3115 // Going to concatenate the lines after parsing. For an empty or
3116 // comment line use an empty string.
3117 if (*p == NUL || vim9_comment_start(p))
3118 {
3119 vim_free(line);
3120 line = vim_strsave((char_u *)"");
3121 }
3122
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003123 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3124 ++gap->ga_len;
3125 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003126 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003127 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003128 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003129 evalarg->eval_tofree = line;
3130 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003131
3132 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003133 // line. The caller assigns the return value to "arg".
3134 // If "arg" is NULL, then the return value is discarded. In that case,
3135 // "arg" still points to the previous line. So don't reset
3136 // "eval_using_cmdline".
3137 if (arg != NULL)
3138 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003139 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003140}
3141
Bram Moolenaare6b53242020-07-01 17:28:33 +02003142/*
3143 * Call eval_next_non_blank() and get the next line if needed.
3144 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003145 char_u *
3146skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3147{
3148 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003149 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003150
Bram Moolenaar962d7212020-07-04 14:15:00 +02003151 if (evalarg == NULL)
3152 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003153 eval_next_non_blank(p, evalarg, &getnext);
3154 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003155 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003156 return p;
3157}
3158
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003159/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003160 * The "eval" functions have an "evalarg" argument: When NULL or
3161 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3162 * parsed but not executed. The functions may return OK, but the rettv will be
3163 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 */
3165
3166/*
3167 * Handle zero level expression.
3168 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003169 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003170 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003171 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * Return OK or FAIL.
3173 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003175eval0(
3176 char_u *arg,
3177 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003178 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003179 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003181 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3182}
3183
3184/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003185 * If "arg" is a simple function call without arguments then call it and return
3186 * the result. Otherwise return NOTDONE.
3187 */
3188 int
3189may_call_simple_func(
3190 char_u *arg,
3191 typval_T *rettv)
3192{
3193 char_u *parens = (char_u *)strstr((char *)arg, "()");
3194 int r = NOTDONE;
3195
3196 // If the expression is "FuncName()" then we can skip a lot of overhead.
3197 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3198 {
3199 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3200
3201 if (to_name_end(p, TRUE) == parens)
3202 r = call_simple_func(arg, (int)(parens - arg), rettv);
3203 }
3204 return r;
3205}
3206
3207/*
3208 * Handle zero level expression with optimization for a simple function call.
3209 * Same arguments and return value as eval0().
3210 */
3211 int
3212eval0_simple_funccal(
3213 char_u *arg,
3214 typval_T *rettv,
3215 exarg_T *eap,
3216 evalarg_T *evalarg)
3217{
3218 int r = may_call_simple_func(arg, rettv);
3219
3220 if (r == NOTDONE)
3221 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3222 return r;
3223}
3224
3225/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003226 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3227 * expression and don't check what comes after the expression.
3228 */
3229 int
3230eval0_retarg(
3231 char_u *arg,
3232 typval_T *rettv,
3233 exarg_T *eap,
3234 evalarg_T *evalarg,
3235 char_u **retarg)
3236{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 int ret;
3238 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003239 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003240 int did_emsg_before = did_emsg;
3241 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003242 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003243 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003246 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003247
Bram Moolenaar79481362022-06-27 20:15:10 +01003248 if (ret != FAIL)
3249 {
3250 expr_end = p;
3251 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003252
Bram Moolenaar79481362022-06-27 20:15:10 +01003253 // In Vim9 script a command block is not split at NL characters for
3254 // commands using an expression argument. Skip over a '#' comment to
3255 // check for a following NL. Require white space before the '#'.
3256 if (in_vim9script() && p > expr_end && retarg == NULL)
3257 while (*p == '#')
3258 {
3259 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003260
Bram Moolenaar79481362022-06-27 20:15:10 +01003261 if (nl == NULL)
3262 break;
3263 p = skipwhite(nl + 1);
3264 if (eap != NULL && *p != NUL)
3265 eap->nextcmd = p;
3266 check_for_end = FALSE;
3267 }
3268
3269 if (check_for_end)
3270 end_error = !ends_excmd2(arg, p);
3271 }
3272
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003273 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
3275 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 /*
3278 * Report the invalid expression unless the expression evaluation has
3279 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003280 * exception, or we already gave a more specific error.
3281 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003283 if (!aborting()
3284 && did_emsg == did_emsg_before
3285 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003286 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003287 {
3288 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003289 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003290 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003291 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003292 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003293
zeertzjqf2588b62023-05-05 17:22:35 +01003294 if (eap != NULL && p != NULL)
3295 {
3296 // Some of the expression may not have been consumed.
3297 // Only execute a next command if it cannot be a "||" operator.
3298 // The next command may be "catch".
3299 char_u *nextcmd = check_nextcmd(p);
3300 if (nextcmd != NULL && *nextcmd != '|')
3301 eap->nextcmd = nextcmd;
3302 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003305
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003306 if (retarg != NULL)
3307 *retarg = p;
3308 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003309 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return ret;
3312}
3313
3314/*
3315 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003316 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003317 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 *
3319 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003320 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003322 * Note: "rettv.v_lock" is not set.
3323 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * Return OK or FAIL.
3325 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003326 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003327eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003329 char_u *p;
3330 int getnext;
3331
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003332 CLEAR_POINTER(rettv);
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /*
3335 * Get the first variable.
3336 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003337 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 return FAIL;
3339
Bram Moolenaar793648f2020-06-26 21:28:25 +02003340 p = eval_next_non_blank(*arg, evalarg, &getnext);
3341 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003343 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003344 int result;
3345 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003346 evalarg_T *evalarg_used = evalarg;
3347 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003348 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003349 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003350 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003351
3352 if (evalarg == NULL)
3353 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003354 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003355 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003357 orig_flags = evalarg_used->eval_flags;
3358 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003359
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003360 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003361 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003362 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003363 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003364 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003365 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003366 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003367 clear_tv(rettv);
3368 return FAIL;
3369 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003370 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003371 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003374 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 int error = FALSE;
3377
Bram Moolenaar13106602020-10-04 16:06:05 +02003378 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003379 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003380 else if (vim9script)
3381 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003382 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003384 if (error || !op_falsy || !result)
3385 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 if (error)
3387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389
3390 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003391 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003393 if (op_falsy)
3394 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003395 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003396 {
mityu4ac198c2021-05-28 17:52:40 +02003397 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003398 clear_tv(rettv);
3399 return FAIL;
3400 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003401 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003402 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003403 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003404 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003405 {
3406 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003408 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003409 if (!op_falsy || !result)
3410 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003412 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003414 /*
3415 * Check for the ":".
3416 */
3417 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3418 if (*p != ':')
3419 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003420 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003421 if (evaluate && result)
3422 clear_tv(rettv);
3423 evalarg_used->eval_flags = orig_flags;
3424 return FAIL;
3425 }
3426 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003427 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003428 else
3429 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003430 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003431 {
3432 error_white_both(p, 1);
3433 clear_tv(rettv);
3434 evalarg_used->eval_flags = orig_flags;
3435 return FAIL;
3436 }
3437 *arg = p;
3438 }
3439
3440 /*
3441 * Get the third variable. Recursive!
3442 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003443 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003444 {
mityu4ac198c2021-05-28 17:52:40 +02003445 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003446 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003447 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003448 return FAIL;
3449 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003450 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3451 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003452 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003453 if (eval1(arg, &var2, evalarg_used) == FAIL)
3454 {
3455 if (evaluate && result)
3456 clear_tv(rettv);
3457 evalarg_used->eval_flags = orig_flags;
3458 return FAIL;
3459 }
3460 if (evaluate && !result)
3461 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003463
3464 if (evalarg == NULL)
3465 clear_evalarg(&local_evalarg, NULL);
3466 else
3467 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469
3470 return OK;
3471}
3472
3473/*
3474 * Handle first level expression:
3475 * expr2 || expr2 || expr2 logical OR
3476 *
3477 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003478 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 *
3480 * Return OK or FAIL.
3481 */
3482 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003483eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003485 char_u *p;
3486 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003489 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003491 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return FAIL;
3493
3494 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003495 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003497 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003498 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003500 evalarg_T *evalarg_used = evalarg;
3501 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003502 int evaluate;
3503 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003504 long result = FALSE;
3505 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003506 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003507 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003508
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003509 if (evalarg == NULL)
3510 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003511 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003512 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003514 orig_flags = evalarg_used->eval_flags;
3515 evaluate = orig_flags & EVAL_EVALUATE;
3516 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003517 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003518 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003519 result = tv_get_bool_chk(rettv, &error);
3520 else if (tv_get_number_chk(rettv, &error) != 0)
3521 result = TRUE;
3522 clear_tv(rettv);
3523 if (error)
3524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526
3527 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003528 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003530 while (p[0] == '|' && p[1] == '|')
3531 {
3532 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003533 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003534 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003535 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003536 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003537 {
3538 error_white_both(p, 2);
3539 clear_tv(rettv);
3540 return FAIL;
3541 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003542 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003543 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003544
3545 /*
3546 * Get the second variable.
3547 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003548 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003549 {
mityu4ac198c2021-05-28 17:52:40 +02003550 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003551 clear_tv(rettv);
3552 return FAIL;
3553 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003554 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3555 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003556 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003557 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003558 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003559
3560 /*
3561 * Compute the result.
3562 */
3563 if (evaluate && !result)
3564 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003565 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003566 result = tv_get_bool_chk(&var2, &error);
3567 else if (tv_get_number_chk(&var2, &error) != 0)
3568 result = TRUE;
3569 clear_tv(&var2);
3570 if (error)
3571 return FAIL;
3572 }
3573 if (evaluate)
3574 {
3575 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003576 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003577 rettv->v_type = VAR_BOOL;
3578 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003579 }
3580 else
3581 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003582 rettv->v_type = VAR_NUMBER;
3583 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003585 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003586
3587 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003589
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003590 if (evalarg == NULL)
3591 clear_evalarg(&local_evalarg, NULL);
3592 else
3593 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
3596 return OK;
3597}
3598
3599/*
3600 * Handle second level expression:
3601 * expr3 && expr3 && expr3 logical AND
3602 *
3603 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003604 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 *
3606 * Return OK or FAIL.
3607 */
3608 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003609eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003611 char_u *p;
3612 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003615 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003617 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 return FAIL;
3619
3620 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003621 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003623 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003624 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003626 evalarg_T *evalarg_used = evalarg;
3627 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003628 int orig_flags;
3629 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003630 long result = TRUE;
3631 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003632 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003633 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003634
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003635 if (evalarg == NULL)
3636 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003637 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003638 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003640 orig_flags = evalarg_used->eval_flags;
3641 evaluate = orig_flags & EVAL_EVALUATE;
3642 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003643 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003644 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003645 result = tv_get_bool_chk(rettv, &error);
3646 else if (tv_get_number_chk(rettv, &error) == 0)
3647 result = FALSE;
3648 clear_tv(rettv);
3649 if (error)
3650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652
3653 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003654 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003656 while (p[0] == '&' && p[1] == '&')
3657 {
3658 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003659 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003660 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003661 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003662 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003663 {
3664 error_white_both(p, 2);
3665 clear_tv(rettv);
3666 return FAIL;
3667 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003668 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003669 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003670
3671 /*
3672 * Get the second variable.
3673 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003674 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003675 {
mityu4ac198c2021-05-28 17:52:40 +02003676 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003677 clear_tv(rettv);
3678 return FAIL;
3679 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003680 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3681 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003682 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003683 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003684 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003685 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003686
3687 /*
3688 * Compute the result.
3689 */
3690 if (evaluate && result)
3691 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003692 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003693 result = tv_get_bool_chk(&var2, &error);
3694 else if (tv_get_number_chk(&var2, &error) == 0)
3695 result = FALSE;
3696 clear_tv(&var2);
3697 if (error)
3698 return FAIL;
3699 }
3700 if (evaluate)
3701 {
3702 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003703 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003704 rettv->v_type = VAR_BOOL;
3705 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003706 }
3707 else
3708 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003709 rettv->v_type = VAR_NUMBER;
3710 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003711 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003712 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003713
3714 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003716
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003717 if (evalarg == NULL)
3718 clear_evalarg(&local_evalarg, NULL);
3719 else
3720 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722
3723 return OK;
3724}
3725
3726/*
3727 * Handle third level expression:
3728 * var1 == var2
3729 * var1 =~ var2
3730 * var1 != var2
3731 * var1 !~ var2
3732 * var1 > var2
3733 * var1 >= var2
3734 * var1 < var2
3735 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003736 * var1 is var2
3737 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 *
3739 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003740 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 *
3742 * Return OK or FAIL.
3743 */
3744 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003745eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003748 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003749 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003751 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003754 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003756 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003759 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003760
Bram Moolenaar696ba232020-07-29 21:20:41 +02003761 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003764 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003766 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003768 typval_T var2;
3769 int ic;
3770 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003771 int evaluate = evalarg == NULL
3772 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003773 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003774
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003775 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003776 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003777 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003778 p = *arg;
3779 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003780 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3781 {
mityu4ac198c2021-05-28 17:52:40 +02003782 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003783 clear_tv(rettv);
3784 return FAIL;
3785 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003786
Bram Moolenaar696ba232020-07-29 21:20:41 +02003787 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3788 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003789 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003790 clear_tv(rettv);
3791 return FAIL;
3792 }
3793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (p[len] == '?')
3796 {
3797 ic = TRUE;
3798 ++len;
3799 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 else if (p[len] == '#')
3802 {
3803 ic = FALSE;
3804 ++len;
3805 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003806 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003808 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /*
3811 * Get the second variable.
3812 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003813 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3814 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003815 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003816 clear_tv(rettv);
3817 return FAIL;
3818 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003819 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003820 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return FAIL;
3824 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003825 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003826 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003827 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003828
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003829 // use the line of the comparison for messages
3830 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003831 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003832 {
3833 ret = FAIL;
3834 clear_tv(rettv);
3835 }
3836 else
3837 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003838 clear_tv(&var2);
3839 return ret;
3840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842
3843 return OK;
3844}
3845
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003846/*
3847 * Make a copy of blob "tv1" and append blob "tv2".
3848 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 void
3850eval_addblob(typval_T *tv1, typval_T *tv2)
3851{
3852 blob_T *b1 = tv1->vval.v_blob;
3853 blob_T *b2 = tv2->vval.v_blob;
3854 blob_T *b = blob_alloc();
3855 int i;
3856
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003857 if (b == NULL)
3858 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003860 for (i = 0; i < blob_len(b1); i++)
3861 ga_append(&b->bv_ga, blob_get(b1, i));
3862 for (i = 0; i < blob_len(b2); i++)
3863 ga_append(&b->bv_ga, blob_get(b2, i));
3864
3865 clear_tv(tv1);
3866 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867}
3868
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003869/*
3870 * Make a copy of list "tv1" and append list "tv2".
3871 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 int
3873eval_addlist(typval_T *tv1, typval_T *tv2)
3874{
3875 typval_T var3;
3876
3877 // concatenate Lists
3878 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3879 {
3880 clear_tv(tv1);
3881 clear_tv(tv2);
3882 return FAIL;
3883 }
3884 clear_tv(tv1);
3885 *tv1 = var3;
3886 return OK;
3887}
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003890 * Handle the bitwise left/right shift operator expression:
3891 * var1 << var2
3892 * var1 >> var2
3893 *
3894 * "arg" must point to the first non-white of the expression.
3895 * "arg" is advanced to just after the recognized expression.
3896 *
3897 * Return OK or FAIL.
3898 */
3899 static int
3900eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3901{
3902 /*
3903 * Get the first expression.
3904 */
3905 if (eval6(arg, rettv, evalarg) == FAIL)
3906 return FAIL;
3907
3908 /*
3909 * Repeat computing, until no '<<' or '>>' is following.
3910 */
3911 for (;;)
3912 {
3913 char_u *p;
3914 int getnext;
3915 exprtype_T type;
3916 int evaluate;
3917 typval_T var2;
3918 int vim9script;
3919
3920 p = eval_next_non_blank(*arg, evalarg, &getnext);
3921 if (p[0] == '<' && p[1] == '<')
3922 type = EXPR_LSHIFT;
3923 else if (p[0] == '>' && p[1] == '>')
3924 type = EXPR_RSHIFT;
3925 else
3926 return OK;
3927
3928 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003929 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3930 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003931 {
3932 // left operand should be a number
3933 emsg(_(e_bitshift_ops_must_be_number));
3934 clear_tv(rettv);
3935 return FAIL;
3936 }
3937
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003938 vim9script = in_vim9script();
3939 if (getnext)
3940 {
3941 *arg = eval_next_line(*arg, evalarg);
3942 p = *arg;
3943 }
3944 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3945 {
3946 error_white_both(*arg, 2);
3947 clear_tv(rettv);
3948 return FAIL;
3949 }
3950
3951 /*
3952 * Get the second variable.
3953 */
3954 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3955 {
3956 error_white_both(p, 2);
3957 clear_tv(rettv);
3958 return FAIL;
3959 }
3960 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3961 if (eval6(arg, &var2, evalarg) == FAIL)
3962 {
3963 clear_tv(rettv);
3964 return FAIL;
3965 }
3966
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003967 if (evaluate)
3968 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003969 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3970 {
3971 // right operand should be a positive number
3972 if (var2.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(rettv);
3977 clear_tv(&var2);
3978 return FAIL;
3979 }
3980
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003981 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3982 // shifting more bits than we have always results in zero
3983 rettv->vval.v_number = 0;
3984 else if (type == EXPR_LSHIFT)
3985 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003986 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003987 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003988 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003989 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003990 }
3991
3992 clear_tv(&var2);
3993 }
3994
3995 return OK;
3996}
3997
3998/*
3999 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004000 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004002 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004003 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 *
4005 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004006 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *
4008 * Return OK or FAIL.
4009 */
4010 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004011eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004014 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004016 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return FAIL;
4018
4019 /*
4020 * Repeat computing, until no '+', '-' or '.' is following.
4021 */
4022 for (;;)
4023 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004024 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004025 int getnext;
4026 char_u *p;
4027 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004028 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004029 int concat;
4030 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004031 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004032
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004033 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004034 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004035 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004036 p = eval_next_non_blank(*arg, evalarg, &getnext);
4037 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004038 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004039 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4040 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004042 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4043 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004044
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004045 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004046 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004047 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004048 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004049 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004050 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004051 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004052 {
mityu4ac198c2021-05-28 17:52:40 +02004053 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004054 clear_tv(rettv);
4055 return FAIL;
4056 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004057 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004058 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004059 if ((op != '+' || (rettv->v_type != VAR_LIST
4060 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004062 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004064 int error = FALSE;
4065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004066 // For "list + ...", an illegal use of the first operand as
4067 // a number cannot be determined before evaluating the 2nd
4068 // operand: if this is also a list, all is ok.
4069 // For "something . ...", "something - ..." or "non-list + ...",
4070 // we know that the first operand needs to be a string or number
4071 // without evaluating the 2nd operand. So check before to avoid
4072 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004073 if (op != '.')
4074 tv_get_number_chk(rettv, &error);
4075 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 {
4077 clear_tv(rettv);
4078 return FAIL;
4079 }
4080 }
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 /*
4083 * Get the second variable.
4084 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004085 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004086 {
mityu89dcb4d2021-05-28 13:50:17 +02004087 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004088 clear_tv(rettv);
4089 return FAIL;
4090 }
4091 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004092 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 return FAIL;
4096 }
4097
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004098 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 /*
4101 * Compute the result.
4102 */
4103 if (op == '.')
4104 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004105 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4106 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004107 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004108
Bram Moolenaar2e086612020-08-25 22:37:48 +02004109 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004110 || var2.v_type == VAR_CHANNEL
4111 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02004112 semsg(_(e_using_invalid_value_as_string_str),
4113 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02004114 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004115 {
4116 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4117 var2.vval.v_float);
4118 s2 = buf2;
4119 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004120 else
4121 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004122 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123 {
4124 clear_tv(rettv);
4125 clear_tv(&var2);
4126 return FAIL;
4127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004128 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
4130 rettv->v_type = VAR_STRING;
4131 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004133 else if (op == '+' && rettv->v_type == VAR_BLOB
4134 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004136 else if (op == '+' && rettv->v_type == VAR_LIST
4137 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004138 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004140 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else
4143 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004144 int error = FALSE;
4145 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004146 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004147
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004150 f1 = rettv->vval.v_float;
4151 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004153 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004155 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156 if (error)
4157 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004158 // This can only happen for "list + non-list" or
4159 // "blob + non-blob". For "non-list + ..." or
4160 // "something - ...", we returned before evaluating the
4161 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004162 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004163 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164 return FAIL;
4165 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (var2.v_type == VAR_FLOAT)
4167 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004169 if (var2.v_type == VAR_FLOAT)
4170 {
4171 f2 = var2.vval.v_float;
4172 n2 = 0;
4173 }
4174 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004176 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004177 if (error)
4178 {
4179 clear_tv(rettv);
4180 clear_tv(&var2);
4181 return FAIL;
4182 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004183 if (rettv->v_type == VAR_FLOAT)
4184 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004189 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4190 {
4191 if (op == '+')
4192 f1 = f1 + f2;
4193 else
4194 f1 = f1 - f2;
4195 rettv->v_type = VAR_FLOAT;
4196 rettv->vval.v_float = f1;
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004199 {
4200 if (op == '+')
4201 n1 = n1 + n2;
4202 else
4203 n1 = n1 - n2;
4204 rettv->v_type = VAR_NUMBER;
4205 rettv->vval.v_number = n1;
4206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211 return OK;
4212}
4213
4214/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004215 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * * number multiplication
4217 * / number division
4218 * % number modulo
4219 *
4220 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004221 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004226eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004227 char_u **arg,
4228 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004229 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004230 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004232 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004235 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004237 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat computing, until no '*', '/' or '%' is following.
4242 */
4243 for (;;)
4244 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004245 int evaluate;
4246 int getnext;
4247 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004248 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004249 int op;
4250 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004251 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004252 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004253
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004254 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004255 p = eval_next_non_blank(*arg, evalarg, &getnext);
4256 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004257 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004259
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004260 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004261 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004262 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004263 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004264 {
4265 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4266 {
mityu4ac198c2021-05-28 17:52:40 +02004267 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004268 clear_tv(rettv);
4269 return FAIL;
4270 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004271 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004274 f1 = 0;
4275 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004276 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004277 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004279 if (rettv->v_type == VAR_FLOAT)
4280 {
4281 f1 = rettv->vval.v_float;
4282 use_float = TRUE;
4283 n1 = 0;
4284 }
4285 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004286 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (error)
4289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 else
4292 n1 = 0;
4293
4294 /*
4295 * Get the second variable.
4296 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004297 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4298 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004299 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004300 clear_tv(rettv);
4301 return FAIL;
4302 }
4303 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004304 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004307 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004309 if (var2.v_type == VAR_FLOAT)
4310 {
4311 if (!use_float)
4312 {
4313 f1 = n1;
4314 use_float = TRUE;
4315 }
4316 f2 = var2.vval.v_float;
4317 n2 = 0;
4318 }
4319 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004321 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004322 clear_tv(&var2);
4323 if (error)
4324 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004325 if (use_float)
4326 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 /*
4330 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004333 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004335 if (op == '*')
4336 f1 = f1 * f2;
4337 else if (op == '/')
4338 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004339#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004341 if (f2 == 0.0)
4342 {
4343 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004345 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004346 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004347 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004348 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004349 }
4350 else
4351 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004352#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004353 // We rely on the floating point library to handle divide
4354 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004356#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004359 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004360 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 return FAIL;
4362 }
4363 rettv->v_type = VAR_FLOAT;
4364 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004368 int failed = FALSE;
4369
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004370 if (op == '*')
4371 n1 = n1 * n2;
4372 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004373 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004375 n1 = num_modulus(n1, n2, &failed);
4376 if (failed)
4377 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004379 rettv->v_type = VAR_NUMBER;
4380 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 }
4384
4385 return OK;
4386}
4387
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004388/*
4389 * Handle a type cast before a base level expression.
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to just after the recognized expression.
4392 * Return OK or FAIL.
4393 */
4394 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004395eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004396 char_u **arg,
4397 typval_T *rettv,
4398 evalarg_T *evalarg,
4399 int want_string) // after "." operator
4400{
4401 type_T *want_type = NULL;
4402 garray_T type_list; // list of pointers to allocated types
4403 int res;
4404 int evaluate = evalarg == NULL ? 0
4405 : (evalarg->eval_flags & EVAL_EVALUATE);
4406
4407 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004408 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4409 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004410 {
4411 ++*arg;
4412 ga_init2(&type_list, sizeof(type_T *), 10);
4413 want_type = parse_type(arg, &type_list, TRUE);
4414 if (want_type == NULL && (evaluate || **arg != '>'))
4415 {
4416 clear_type_list(&type_list);
4417 return FAIL;
4418 }
4419
4420 if (**arg != '>')
4421 {
4422 if (*skipwhite(*arg) == '>')
4423 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4424 else
4425 emsg(_(e_missing_gt));
4426 clear_type_list(&type_list);
4427 return FAIL;
4428 }
4429 ++*arg;
4430 *arg = skipwhite_and_linebreak(*arg, evalarg);
4431 }
4432
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004433 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004434
4435 if (want_type != NULL && evaluate)
4436 {
4437 if (res == OK)
4438 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004439 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4440 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004441
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004442 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004443 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004444 if (want_type->tt_type == VAR_BOOL
4445 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004446 && (actual->tt_flags & TTFLAG_BOOL_OK))
4447 {
4448 int n = tv2bool(rettv);
4449
4450 // can use "0" and "1" for boolean in some places
4451 clear_tv(rettv);
4452 rettv->v_type = VAR_BOOL;
4453 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4454 }
4455 else
4456 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004457 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004458
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004459 res = check_type(want_type, actual, TRUE, where);
4460 }
4461 }
4462 }
4463 clear_type_list(&type_list);
4464 }
4465
4466 return res;
4467}
4468
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004469 int
4470eval_leader(char_u **arg, int vim9)
4471{
4472 char_u *s = *arg;
4473 char_u *p = *arg;
4474
4475 while (*p == '!' || *p == '-' || *p == '+')
4476 {
4477 char_u *n = skipwhite(p + 1);
4478
4479 // ++, --, -+ and +- are not accepted in Vim9 script
4480 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4481 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004482 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004483 return FAIL;
4484 }
4485 p = n;
4486 }
4487 *arg = p;
4488 return OK;
4489}
4490
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004492 * Check for a predefined value "true", "false" and "null.*".
4493 * Return OK when recognized.
4494 */
4495 int
4496handle_predefined(char_u *s, int len, typval_T *rettv)
4497{
4498 switch (len)
4499 {
4500 case 4: if (STRNCMP(s, "true", 4) == 0)
4501 {
4502 rettv->v_type = VAR_BOOL;
4503 rettv->vval.v_number = VVAL_TRUE;
4504 return OK;
4505 }
4506 if (STRNCMP(s, "null", 4) == 0)
4507 {
4508 rettv->v_type = VAR_SPECIAL;
4509 rettv->vval.v_number = VVAL_NULL;
4510 return OK;
4511 }
4512 break;
4513 case 5: if (STRNCMP(s, "false", 5) == 0)
4514 {
4515 rettv->v_type = VAR_BOOL;
4516 rettv->vval.v_number = VVAL_FALSE;
4517 return OK;
4518 }
4519 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004520 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4521 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004523 rettv->v_type = VAR_JOB;
4524 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004525#else
4526 rettv->v_type = VAR_SPECIAL;
4527 rettv->vval.v_number = VVAL_NULL;
4528#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004529 return OK;
4530 }
4531 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004532 case 9:
4533 if (STRNCMP(s, "null_", 5) != 0)
4534 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004535 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004536 if (STRNCMP(s + 5, "list", 4) == 0)
4537 {
4538 rettv->v_type = VAR_LIST;
4539 rettv->vval.v_list = NULL;
4540 return OK;
4541 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004542 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004543 if (STRNCMP(s + 5, "dict", 4) == 0)
4544 {
4545 rettv->v_type = VAR_DICT;
4546 rettv->vval.v_dict = NULL;
4547 return OK;
4548 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004549 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004550 if (STRNCMP(s + 5, "blob", 4) == 0)
4551 {
4552 rettv->v_type = VAR_BLOB;
4553 rettv->vval.v_blob = NULL;
4554 return OK;
4555 }
4556 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004557 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4558 {
4559 rettv->v_type = VAR_CLASS;
4560 rettv->vval.v_class = NULL;
4561 return OK;
4562 }
4563 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004564 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4565 {
4566 rettv->v_type = VAR_STRING;
4567 rettv->vval.v_string = NULL;
4568 return OK;
4569 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004570 if (STRNCMP(s, "null_object", 11) == 0)
4571 {
4572 rettv->v_type = VAR_OBJECT;
4573 rettv->vval.v_object = NULL;
4574 return OK;
4575 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004576 break;
4577 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004578 if (STRNCMP(s, "null_channel", 12) == 0)
4579 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004581 rettv->v_type = VAR_CHANNEL;
4582 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004583#else
4584 rettv->v_type = VAR_SPECIAL;
4585 rettv->vval.v_number = VVAL_NULL;
4586#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004587 return OK;
4588 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004589 if (STRNCMP(s, "null_partial", 12) == 0)
4590 {
4591 rettv->v_type = VAR_PARTIAL;
4592 rettv->vval.v_partial = NULL;
4593 return OK;
4594 }
4595 break;
4596 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4597 {
4598 rettv->v_type = VAR_FUNC;
4599 rettv->vval.v_string = NULL;
4600 return OK;
4601 }
4602 break;
4603 }
4604 return FAIL;
4605}
4606
4607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 * Handle sixth level expression:
4609 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004610 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004611 * "string" string constant
4612 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 * &option-name option value
4614 * @r register contents
4615 * identifier variable value
4616 * function() function call
4617 * $VAR environment variable
4618 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004619 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004621 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004622 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 *
4624 * Also handle:
4625 * ! in front logical NOT
4626 * - in front unary minus
4627 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 * trailing [] subscript in String or List
4629 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004630 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 *
4632 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004633 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 *
4635 * Return OK or FAIL.
4636 */
4637 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004638eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004639 char_u **arg,
4640 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004641 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004644 int evaluate = evalarg != NULL
4645 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 int len;
4647 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004648 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 char_u *start_leader, *end_leader;
4650 int ret = OK;
4651 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004652 static int recurse = 0;
4653 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004662 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 */
4664 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004665 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004666 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 end_leader = *arg;
4668
Keith Thompson184f71c2024-01-04 21:19:04 +01004669 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004670 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004671 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004672 ++*arg;
4673 return FAIL;
4674 }
4675
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004676 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004677 // and crash. With MSVC the stack is smaller.
4678 if (recurse ==
4679#ifdef _MSC_VER
4680 300
4681#else
4682 1000
4683#endif
4684 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004685 {
4686 semsg(_(e_expression_too_recursive_str), *arg);
4687 return FAIL;
4688 }
4689 ++recurse;
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 switch (**arg)
4692 {
4693 /*
4694 * Number constant.
4695 */
4696 case '0':
4697 case '1':
4698 case '2':
4699 case '3':
4700 case '4':
4701 case '5':
4702 case '6':
4703 case '7':
4704 case '8':
4705 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004706 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004707
4708 // Apply prefixed "-" and "+" now. Matters especially when
4709 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004710 if (ret == OK && evaluate && end_leader > start_leader
4711 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004712 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 /*
4716 * String constant: "string".
4717 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004718 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 break;
4720
4721 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004722 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004724 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 break;
4726
4727 /*
4728 * List: [expr, expr]
4729 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004730 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 break;
4732
4733 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004734 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004735 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004736 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004737 {
4738 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4739 }
4740 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004741 {
4742 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004743 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004744 }
4745 else
4746 ret = NOTDONE;
4747 break;
4748
4749 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004750 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004751 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004753 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004754 ret = NOTDONE;
4755 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004756 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004757 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004758 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 break;
4760
4761 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004762 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004764 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 break;
4766
4767 /*
4768 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004769 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
LemonBoy2eaef102022-05-06 13:14:50 +01004771 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4772 ret = eval_interp_string(arg, rettv, evaluate);
4773 else
4774 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776
4777 /*
4778 * Register contents: @r.
4779 */
4780 case '@': ++*arg;
4781 if (evaluate)
4782 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004783 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004784 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004785 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004786 emsg_invreg(**arg);
4787 else
4788 {
4789 rettv->v_type = VAR_STRING;
4790 rettv->vval.v_string = get_reg_contents(**arg,
4791 GREG_EXPR_SRC);
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 if (**arg != NUL)
4795 ++*arg;
4796 break;
4797
4798 /*
4799 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004800 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004802 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004803 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004804 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004805 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004806 if (ret == OK && evaluate)
4807 {
4808 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4809
Bram Moolenaara9931532021-06-12 15:58:16 +02004810 // Compile it here to get the return type. The return
4811 // type is optional, when it's missing use t_unknown.
4812 // This is recognized in compile_return().
4813 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4814 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004815 if (compile_def_function(ufunc, FALSE,
4816 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004817 {
4818 clear_tv(rettv);
4819 ret = FAIL;
4820 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004821 }
4822 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004823 if (ret == NOTDONE)
4824 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004825 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004826 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004827
Bram Moolenaar9215f012020-06-27 21:18:00 +02004828 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004829 if (**arg == ')')
4830 ++*arg;
4831 else if (ret == OK)
4832 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004833 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004834 clear_tv(rettv);
4835 ret = FAIL;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 break;
4839
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 break;
4842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843
4844 if (ret == NOTDONE)
4845 {
4846 /*
4847 * Must be a variable or function name.
4848 * Can also be a curly-braces kind of name: {expr}.
4849 */
4850 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004851 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 if (alias != NULL)
4853 s = alias;
4854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004855 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856 ret = FAIL;
4857 else
4858 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004859 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4860
Bram Moolenaar4525a572022-02-13 11:57:33 +00004861 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004862 {
4863 emsg(_(e_cannot_use_underscore_here));
4864 ret = FAIL;
4865 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004866 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004867 && s[0] == 's' && s[1] == ':')
4868 {
4869 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4870 ret = FAIL;
4871 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004872 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004873 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004874 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004875 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004876 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004879 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004880 // get the value of "true", "false", etc. or a variable
4881 ret = FAIL;
4882 if (vim9script)
4883 ret = handle_predefined(s, len, rettv);
4884 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004885 {
4886 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004887 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004888 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004889 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004890 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004891 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004892 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004893 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004894 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004895 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004898 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 }
4900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4902 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004903 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004904 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 /*
4907 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4908 */
4909 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004910 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004911
4912 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004913 return ret;
4914}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004916/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004917 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004918 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004919 * Adjusts "end_leaderp" until it is at "start_leader".
4920 */
4921 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004922eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004923 typval_T *rettv,
4924 int numeric_only,
4925 char_u *start_leader,
4926 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004927{
4928 char_u *end_leader = *end_leaderp;
4929 int ret = OK;
4930 int error = FALSE;
4931 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004932 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004933 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004934 float_T f = 0.0;
4935
4936 if (rettv->v_type == VAR_FLOAT)
4937 f = rettv->vval.v_float;
4938 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004939 {
4940 while (VIM_ISWHITE(end_leader[-1]))
4941 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004942 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004943 val = tv2bool(rettv);
4944 else
4945 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004946 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004947 if (error)
4948 {
4949 clear_tv(rettv);
4950 ret = FAIL;
4951 }
4952 else
4953 {
4954 while (end_leader > start_leader)
4955 {
4956 --end_leader;
4957 if (*end_leader == '!')
4958 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004959 if (numeric_only)
4960 {
4961 ++end_leader;
4962 break;
4963 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004964 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004965 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004966 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004967 {
4968 rettv->v_type = VAR_BOOL;
4969 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4970 }
4971 else
4972 f = !f;
4973 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004974 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004975 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004976 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004977 type = VAR_BOOL;
4978 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004979 }
4980 else if (*end_leader == '-')
4981 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004982 if (rettv->v_type == VAR_FLOAT)
4983 f = -f;
4984 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004985 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004986 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004987 type = VAR_NUMBER;
4988 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004989 }
4990 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004991 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004993 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004994 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004996 else
4997 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004998 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004999 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005000 rettv->v_type = type;
5001 else
5002 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005003 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005006 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 return ret;
5008}
5009
5010/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005011 * Call the function referred to in "rettv".
5012 */
5013 static int
5014call_func_rettv(
5015 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005016 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005017 typval_T *rettv,
5018 int evaluate,
5019 dict_T *selfdict,
5020 typval_T *basetv)
5021{
5022 partial_T *pt = NULL;
5023 funcexe_T funcexe;
5024 typval_T functv;
5025 char_u *s;
5026 int ret;
5027
5028 // need to copy the funcref so that we can clear rettv
5029 if (evaluate)
5030 {
5031 functv = *rettv;
5032 rettv->v_type = VAR_UNKNOWN;
5033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005035 if (functv.v_type == VAR_PARTIAL)
5036 {
5037 pt = functv.vval.v_partial;
5038 s = partial_name(pt);
5039 }
5040 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005041 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005042 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005043 if (s == NULL || *s == NUL)
5044 {
5045 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005046 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005047 goto theend;
5048 }
5049 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005050 }
5051 else
5052 s = (char_u *)"";
5053
Bram Moolenaara80faa82020-04-12 19:37:17 +02005054 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005055 funcexe.fe_firstline = curwin->w_cursor.lnum;
5056 funcexe.fe_lastline = curwin->w_cursor.lnum;
5057 funcexe.fe_evaluate = evaluate;
5058 funcexe.fe_partial = pt;
5059 funcexe.fe_selfdict = selfdict;
5060 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005061 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005062
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005063theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // Clear the funcref afterwards, so that deleting it while
5065 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005066 if (evaluate)
5067 clear_tv(&functv);
5068
5069 return ret;
5070}
5071
5072/*
5073 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005074 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005075 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5076 */
5077 static int
5078eval_lambda(
5079 char_u **arg,
5080 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005081 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005083{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005084 int evaluate = evalarg != NULL
5085 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005086 typval_T base = *rettv;
5087 int ret;
5088
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005089 rettv->v_type = VAR_UNKNOWN;
5090
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005091 if (**arg == '{')
5092 {
5093 // ->{lambda}()
5094 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5095 }
5096 else
5097 {
5098 // ->(lambda)()
5099 ++*arg;
5100 ret = eval1(arg, rettv, evalarg);
5101 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005102 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005103 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005104 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005105 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005106 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005107 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5108 && rettv->v_type != VAR_PARTIAL)
5109 {
5110 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5111 return FAIL;
5112 }
5113 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005114 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005115 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005116 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005117
5118 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005119 {
5120 if (verbose)
5121 {
5122 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005123 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005124 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005125 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005126 }
5127 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005128 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005129 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005130 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005131 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005132
5133 // Clear the funcref afterwards, so that deleting it while
5134 // evaluating the arguments is possible (see test55).
5135 if (evaluate)
5136 clear_tv(&base);
5137
5138 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005139}
5140
5141/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005142 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005143 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005144 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5145 */
5146 static int
5147eval_method(
5148 char_u **arg,
5149 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005150 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005152{
5153 char_u *name;
5154 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005155 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005156 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005157 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005158 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005159 int evaluate = evalarg != NULL
5160 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005161
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005162 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005163
Bram Moolenaarac92e252019-08-03 21:58:38 +02005164 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005165 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005166 if (alias != NULL)
5167 name = alias;
5168
5169 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005170 {
5171 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005172 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005173 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005174 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005175 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005176 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005177 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005178
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005179 // If there is no "(" immediately following, but there is further on,
5180 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5181 // Does not handle anything where "(" is part of the expression.
5182 *arg = skipwhite(*arg);
5183
5184 if (**arg != '(' && alias == NULL
5185 && (paren = vim_strchr(*arg, '(')) != NULL)
5186 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005187 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005188
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005189 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005190 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005191 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005192 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5193 if (evalarg != NULL)
5194 {
5195 getline = evalarg->eval_getline;
5196 evalarg->eval_getline = NULL;
5197 }
5198
5199 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005200 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005201 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005202 *arg = name + len;
5203 ret = FAIL;
5204 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005205 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005206 {
5207 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005208 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005209 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005210
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005211 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005212 if (getline != NULL)
5213 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005214 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005215
5216 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005217 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005218 *arg = skipwhite(*arg);
5219
5220 if (**arg != '(')
5221 {
5222 if (verbose)
5223 semsg(_(e_missing_parenthesis_str), name);
5224 ret = FAIL;
5225 }
5226 else if (VIM_ISWHITE((*arg)[-1]))
5227 {
5228 if (verbose)
5229 emsg(_(e_no_white_space_allowed_before_parenthesis));
5230 ret = FAIL;
5231 }
5232 else
5233 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005234 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005235 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005236 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005237
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005238 // Clear the funcref afterwards, so that deleting it while
5239 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005240 if (evaluate)
5241 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005242 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005243
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005244 if (alias != NULL)
5245 vim_free(alias);
5246
Bram Moolenaarac92e252019-08-03 21:58:38 +02005247 return ret;
5248}
5249
5250/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005251 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5252 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5254 */
5255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005256eval_index(
5257 char_u **arg,
5258 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005259 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005262 int evaluate = evalarg != NULL
5263 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005268 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005269 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005271 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5272 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005274 init_tv(&var1);
5275 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 /*
5279 * dict.name
5280 */
5281 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005282 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005284 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005286 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 }
5288 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 /*
5291 * something[idx]
5292 *
5293 * Get the (first) variable from inside the [].
5294 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005295 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (**arg == ':')
5297 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005298 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005300 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005301 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005302 semsg(_(e_white_space_required_before_and_after_str_at_str),
5303 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005304 clear_tv(&var1);
5305 return FAIL;
5306 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005307 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005309 int error = FALSE;
5310
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005311 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005312 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005313 && var1.v_type == VAR_FLOAT)
5314 {
5315 var1.vval.v_string = typval_tostring(&var1, TRUE);
5316 var1.v_type = VAR_STRING;
5317 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005318
Bram Moolenaar4525a572022-02-13 11:57:33 +00005319 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005320 tv_get_number_chk(&var1, &error);
5321 else
5322 error = tv_get_string_chk(&var1) == NULL;
5323 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005324 {
5325 // not a number or string
5326 clear_tv(&var1);
5327 return FAIL;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005330
5331 /*
5332 * Get the second variable from inside the [:].
5333 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005334 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335 if (**arg == ':')
5336 {
5337 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005338 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005339 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005340 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005341 semsg(_(e_white_space_required_before_and_after_str_at_str),
5342 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 if (**arg == ']')
5349 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005350 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352 if (!empty1)
5353 clear_tv(&var1);
5354 return FAIL;
5355 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005356 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005357 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005358 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 if (!empty1)
5360 clear_tv(&var1);
5361 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 return FAIL;
5363 }
5364 }
5365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005367 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005371 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005377 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005382 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005383 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005384 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005385
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005386 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005389 clear_tv(&var2);
5390 return res;
5391 }
5392 return OK;
5393}
5394
5395/*
5396 * Check if "rettv" can have an [index] or [sli:ce]
5397 */
5398 int
5399check_can_index(typval_T *rettv, int evaluate, int verbose)
5400{
5401 switch (rettv->v_type)
5402 {
5403 case VAR_FUNC:
5404 case VAR_PARTIAL:
5405 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005406 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005407 return FAIL;
5408 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005409 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005410 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005411 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005412 case VAR_BOOL:
5413 case VAR_SPECIAL:
5414 case VAR_JOB:
5415 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005416 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005417 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005418 if (verbose)
5419 emsg(_(e_cannot_index_special_variable));
5420 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005421 case VAR_CLASS:
5422 case VAR_TYPEALIAS:
5423 if (verbose)
5424 check_typval_is_value(rettv);
5425 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005426 case VAR_UNKNOWN:
5427 case VAR_ANY:
5428 case VAR_VOID:
5429 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005431 emsg(_(e_cannot_index_special_variable));
5432 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005434 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005436 case VAR_STRING:
5437 case VAR_LIST:
5438 case VAR_DICT:
5439 case VAR_BLOB:
5440 break;
5441 case VAR_NUMBER:
5442 if (in_vim9script())
5443 emsg(_(e_cannot_index_number));
5444 break;
5445 }
5446 return OK;
5447}
5448
5449/*
5450 * Apply index or range to "rettv".
5451 * "var1" is the first index, NULL for [:expr].
5452 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005453 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5454 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005455 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5456 */
5457 int
5458eval_index_inner(
5459 typval_T *rettv,
5460 int is_range,
5461 typval_T *var1,
5462 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005463 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005464 char_u *key,
5465 int keylen,
5466 int verbose)
5467{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005468 varnumber_T n1, n2 = 0;
5469 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005470
5471 n1 = 0;
5472 if (var1 != NULL && rettv->v_type != VAR_DICT)
5473 n1 = tv_get_number(var1);
5474
5475 if (is_range)
5476 {
5477 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005479 if (verbose)
5480 emsg(_(e_cannot_slice_dictionary));
5481 return FAIL;
5482 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005483 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005484 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005485 else
5486 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005487 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005489 switch (rettv->v_type)
5490 {
5491 case VAR_UNKNOWN:
5492 case VAR_ANY:
5493 case VAR_VOID:
5494 case VAR_FUNC:
5495 case VAR_PARTIAL:
5496 case VAR_FLOAT:
5497 case VAR_BOOL:
5498 case VAR_SPECIAL:
5499 case VAR_JOB:
5500 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005501 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005502 case VAR_CLASS:
5503 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005504 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005505 break; // not evaluating, skipping over subscript
5506
5507 case VAR_NUMBER:
5508 case VAR_STRING:
5509 {
5510 char_u *s = tv_get_string(rettv);
5511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005513 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005514 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005515 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005516 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005517 else
5518 s = char_from_string(s, n1);
5519 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005520 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005522 // The resulting variable is a substring. If the indexes
5523 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 if (n1 < 0)
5525 {
5526 n1 = len + n1;
5527 if (n1 < 0)
5528 n1 = 0;
5529 }
5530 if (n2 < 0)
5531 n2 = len + n2;
5532 else if (n2 >= len)
5533 n2 = len;
5534 if (n1 >= len || n2 < 0 || n1 > n2)
5535 s = NULL;
5536 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005537 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 else
5540 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // The resulting variable is a string of a single
5542 // character. If the index is too big or negative the
5543 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 if (n1 >= len || n1 < 0)
5545 s = NULL;
5546 else
5547 s = vim_strnsave(s + n1, 1);
5548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 clear_tv(rettv);
5550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005552 }
5553 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005555 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005556 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5557 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005558 break;
5559
5560 case VAR_LIST:
5561 if (var1 == NULL)
5562 n1 = 0;
5563 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005564 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005565 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005566 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005567 return FAIL;
5568 break;
5569
5570 case VAR_DICT:
5571 {
5572 dictitem_T *item;
5573 typval_T tmp;
5574
5575 if (key == NULL)
5576 {
5577 key = tv_get_string_chk(var1);
5578 if (key == NULL)
5579 return FAIL;
5580 }
5581
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005582 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005583
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005584 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005585 {
5586 if (verbose)
5587 {
5588 if (keylen > 0)
5589 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005590 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005591 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005592 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005593 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005594
5595 copy_tv(&item->di_tv, &tmp);
5596 clear_tv(rettv);
5597 *rettv = tmp;
5598 }
5599 break;
5600 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 return OK;
5602}
5603
5604/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005605 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005606 */
5607 char_u *
5608partial_name(partial_T *pt)
5609{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005610 if (pt != NULL)
5611 {
5612 if (pt->pt_name != NULL)
5613 return pt->pt_name;
5614 if (pt->pt_func != NULL)
5615 return pt->pt_func->uf_name;
5616 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005617 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005618}
5619
Bram Moolenaarddecc252016-04-06 22:59:37 +02005620 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005621partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005622{
5623 int i;
5624
5625 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005627 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005629 if (pt->pt_name != NULL)
5630 {
5631 func_unref(pt->pt_name);
5632 vim_free(pt->pt_name);
5633 }
5634 else
5635 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005636 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005637
Bram Moolenaar54656012021-06-09 20:50:46 +02005638 // "out_up" is no longer used, decrement refcount on partial that owns it.
5639 partial_unref(pt->pt_outer.out_up_partial);
5640
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005641 // Using pt_outer from another partial.
5642 partial_unref(pt->pt_outer_partial);
5643
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005644 // Decrease the reference count for the context of a closure. If down
5645 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005646 if (pt->pt_funcstack != NULL)
5647 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005648 --pt->pt_funcstack->fs_refcount;
5649 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005650 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005651 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005652 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5653 if (pt->pt_loopvars[i] != NULL)
5654 {
5655 --pt->pt_loopvars[i]->lvs_refcount;
5656 loopvars_check_refcount(pt->pt_loopvars[i]);
5657 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005658
Bram Moolenaarddecc252016-04-06 22:59:37 +02005659 vim_free(pt);
5660}
5661
5662/*
5663 * Unreference a closure: decrement the reference count and free it when it
5664 * becomes zero.
5665 */
5666 void
5667partial_unref(partial_T *pt)
5668{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005669 if (pt == NULL)
5670 return;
5671
5672 int done = FALSE;
5673
5674 if (--pt->pt_refcount <= 0)
5675 partial_free(pt);
5676
5677 // If the reference count goes down to one, the funcstack may be the
5678 // only reference and can be freed if no other partials reference it.
5679 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005680 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005681 // careful: if the funcstack is freed it may contain this partial
5682 // and it gets freed as well
5683 if (pt->pt_funcstack != NULL)
5684 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005685
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005686 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005687 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005688 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005689
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005690 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5691 if (pt->pt_loopvars[depth] != NULL
5692 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005693 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005694 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005695 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005696}
5697
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005698/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005699 * Return a textual representation of a string in "tv".
5700 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5701 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5702 * strings as "string()", otherwise does not put quotes around strings.
5703 * May return NULL.
5704 */
5705 static char_u *
5706string_tv2string(
5707 typval_T *tv,
5708 char_u **tofree,
5709 int echo_style,
5710 int composite_val)
5711{
5712 char_u *r = NULL;
5713
5714 if (echo_style && !composite_val)
5715 {
5716 *tofree = NULL;
5717 r = tv->vval.v_string;
5718 if (r == NULL)
5719 r = (char_u *)"";
5720 }
5721 else
5722 {
5723 *tofree = string_quote(tv->vval.v_string, FALSE);
5724 r = *tofree;
5725 }
5726
5727 return r;
5728}
5729
5730/*
5731 * Return a textual representation of a function in "tv".
5732 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5733 * When "echo_style" is FALSE, put quotes around the function name as
5734 * "function()", otherwise does not put quotes around function name.
5735 * May return NULL.
5736 */
5737 static char_u *
5738func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5739{
5740 char_u *r = NULL;
5741 char_u buf[MAX_FUNC_NAME_LEN];
5742
5743 if (echo_style)
5744 {
5745 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5746 : make_ufunc_name_readable(tv->vval.v_string,
5747 buf, MAX_FUNC_NAME_LEN);
Christian Brabandtb335a932024-05-21 18:39:10 +02005748 if (r == buf && tv->vval.v_string != NULL)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005749 {
5750 r = vim_strsave(buf);
5751 *tofree = r;
5752 }
5753 else
5754 *tofree = NULL;
5755 }
5756 else
5757 {
5758 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5759 : make_ufunc_name_readable(tv->vval.v_string,
5760 buf, MAX_FUNC_NAME_LEN), TRUE);
5761 r = *tofree;
5762 }
5763
5764 return r;
5765}
5766
5767/*
5768 * Return a textual representation of a partial in "tv".
5769 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5770 * "numbuf" is used for a number. May return NULL.
5771 */
5772 static char_u *
5773partial_tv2string(
5774 typval_T *tv,
5775 char_u **tofree,
5776 char_u *numbuf,
5777 int copyID)
5778{
5779 char_u *r = NULL;
5780 partial_T *pt;
5781 char_u *fname;
5782 garray_T ga;
5783 int i;
5784 char_u *tf;
5785
5786 pt = tv->vval.v_partial;
5787 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5788
5789 ga_init2(&ga, 1, 100);
5790 ga_concat(&ga, (char_u *)"function(");
5791 if (fname != NULL)
5792 {
5793 // When using uf_name prepend "g:" for a global function.
5794 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5795 && vim_isupper(fname[1]))
5796 {
5797 ga_concat(&ga, (char_u *)"'g:");
5798 ga_concat(&ga, fname + 1);
5799 }
5800 else
5801 ga_concat(&ga, fname);
5802 vim_free(fname);
5803 }
5804 if (pt != NULL && pt->pt_argc > 0)
5805 {
5806 ga_concat(&ga, (char_u *)", [");
5807 for (i = 0; i < pt->pt_argc; ++i)
5808 {
5809 if (i > 0)
5810 ga_concat(&ga, (char_u *)", ");
5811 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5812 vim_free(tf);
5813 }
5814 ga_concat(&ga, (char_u *)"]");
5815 }
5816 if (pt != NULL && pt->pt_dict != NULL)
5817 {
5818 typval_T dtv;
5819
5820 ga_concat(&ga, (char_u *)", ");
5821 dtv.v_type = VAR_DICT;
5822 dtv.vval.v_dict = pt->pt_dict;
5823 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5824 vim_free(tf);
5825 }
5826 // terminate with ')' and a NUL
5827 ga_concat_len(&ga, (char_u *)")", 2);
5828
5829 *tofree = ga.ga_data;
5830 r = *tofree;
5831
5832 return r;
5833}
5834
5835/*
5836 * Return a textual representation of a List in "tv".
5837 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5838 * When "copyID" is not zero replace recursive lists with "...". When
5839 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
5840 * May return NULL.
5841 */
5842 static char_u *
5843list_tv2string(
5844 typval_T *tv,
5845 char_u **tofree,
5846 int copyID,
5847 int restore_copyID)
5848{
5849 char_u *r = NULL;
5850
5851 if (tv->vval.v_list == NULL)
5852 {
5853 // NULL list is equivalent to empty list.
5854 *tofree = NULL;
5855 r = (char_u *)"[]";
5856 }
5857 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5858 && tv->vval.v_list->lv_len > 0)
5859 {
5860 *tofree = NULL;
5861 r = (char_u *)"[...]";
5862 }
5863 else
5864 {
Christian Brabandtb335a932024-05-21 18:39:10 +02005865 int old_copyID;
5866 if (restore_copyID)
5867 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005868
5869 tv->vval.v_list->lv_copyID = copyID;
5870 *tofree = list2string(tv, copyID, restore_copyID);
5871 if (restore_copyID)
5872 tv->vval.v_list->lv_copyID = old_copyID;
5873 r = *tofree;
5874 }
5875
5876 return r;
5877}
5878
5879/*
5880 * Return a textual representation of a Dict in "tv".
5881 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5882 * When "copyID" is not zero replace recursive dicts with "...".
5883 * When "restore_copyID" is FALSE, repeated items in the dictionary are
5884 * replaced with "...". May return NULL.
5885 */
5886 static char_u *
5887dict_tv2string(
5888 typval_T *tv,
5889 char_u **tofree,
5890 int copyID,
5891 int restore_copyID)
5892{
5893 char_u *r = NULL;
5894
5895 if (tv->vval.v_dict == NULL)
5896 {
5897 // NULL dict is equivalent to empty dict.
5898 *tofree = NULL;
5899 r = (char_u *)"{}";
5900 }
5901 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5902 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
5903 {
5904 *tofree = NULL;
5905 r = (char_u *)"{...}";
5906 }
5907 else
5908 {
Christian Brabandtb335a932024-05-21 18:39:10 +02005909 int old_copyID;
5910 if (restore_copyID)
5911 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005912
5913 tv->vval.v_dict->dv_copyID = copyID;
5914 *tofree = dict2string(tv, copyID, restore_copyID);
5915 if (restore_copyID)
5916 tv->vval.v_dict->dv_copyID = old_copyID;
5917 r = *tofree;
5918 }
5919
5920 return r;
5921}
5922
5923/*
5924 * Return a textual representation of a job or a channel in "tv".
5925 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5926 * "numbuf" is used for a number.
5927 * When "composite_val" is FALSE, put quotes around strings as "string()",
5928 * otherwise does not put quotes around strings.
5929 * May return NULL.
5930 */
5931 static char_u *
5932jobchan_tv2string(
5933 typval_T *tv,
5934 char_u **tofree,
5935 char_u *numbuf,
5936 int composite_val)
5937{
5938 char_u *r = NULL;
5939
5940#ifdef FEAT_JOB_CHANNEL
5941 *tofree = NULL;
5942
5943 if (tv->v_type == VAR_JOB)
5944 r = job_to_string_buf(tv, numbuf);
5945 else
5946 r = channel_to_string_buf(tv, numbuf);
5947
5948 if (composite_val)
5949 {
5950 *tofree = string_quote(r, FALSE);
5951 r = *tofree;
5952 }
5953#endif
5954
5955 return r;
5956}
5957
5958/*
5959 * Return a textual representation of a class in "tv".
5960 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5961 * May return NULL.
5962 */
5963 static char_u *
5964class_tv2string(typval_T *tv, char_u **tofree)
5965{
5966 char_u *r = NULL;
5967 class_T *cl = tv->vval.v_class;
5968 char *s = "class";
5969
5970 if (cl != NULL && IS_INTERFACE(cl))
5971 s = "interface";
5972 else if (cl != NULL && IS_ENUM(cl))
5973 s = "enum";
5974 size_t len = STRLEN(s) + 1 +
5975 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5976 r = *tofree = alloc(len);
5977 vim_snprintf((char *)r, len, "%s %s", s,
5978 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5979
5980 return r;
5981}
5982
5983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Return a string with the string representation of a variable.
5985 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005987 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005988 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005989 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005990 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005991 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5992 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005993 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005995 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005996echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997 typval_T *tv,
5998 char_u **tofree,
5999 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006000 int copyID,
6001 int echo_style,
6002 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006003 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 static int recurse = 0;
6006 char_u *r = NULL;
6007
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006010 if (!did_echo_string_emsg)
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // Only give this message once for a recursive call to avoid
6013 // flooding the user with errors. And stop iterating over lists
6014 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006015 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006016 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006019 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 }
6021 ++recurse;
6022
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 switch (tv->v_type)
6024 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006026 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 break;
6028
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006030 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006033 case VAR_PARTIAL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006034 r = partial_tv2string(tv, tofree, numbuf, copyID);
6035 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006036
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006037 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006038 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006039 break;
6040
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006042 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044
Bram Moolenaar8c711452005-01-14 21:53:12 +00006045 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006046 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006049 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006050 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006051 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006053 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006054 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006055 break;
6056
Bram Moolenaar835dc632016-02-07 14:27:38 +01006057 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006058 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006059 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006062 case VAR_INSTR:
6063 *tofree = NULL;
6064 r = (char_u *)"instructions";
6065 break;
6066
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006067 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006068 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006069 break;
6070
6071 case VAR_OBJECT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006072 *tofree = r = object2string(tv->vval.v_object, numbuf, copyID,
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006073 echo_style, restore_copyID,
6074 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006075 break;
6076
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 case VAR_FLOAT:
6078 *tofree = NULL;
6079 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6080 r = numbuf;
6081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006082
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006083 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006084 case VAR_SPECIAL:
6085 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006086 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006087 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006088
6089 case VAR_TYPEALIAS:
6090 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6091 r = *tofree;
6092 if (r == NULL)
6093 r = (char_u *)"";
6094 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006096
Bram Moolenaar8502c702014-06-17 12:51:16 +02006097 if (--recurse == 0)
6098 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006100}
6101
6102/*
6103 * Return a string with the string representation of a variable.
6104 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6105 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006106 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006107 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006108 * May return NULL.
6109 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006110 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006111echo_string(
6112 typval_T *tv,
6113 char_u **tofree,
6114 char_u *numbuf,
6115 int copyID)
6116{
6117 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6118}
6119
6120/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006121 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6122 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006123 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006124 */
6125 int
6126buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6127{
6128 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006129 char_u *t;
6130 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006131
6132 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6133 return -1;
6134
6135 if (lnum > buf->b_ml.ml_line_count)
6136 lnum = buf->b_ml.ml_line_count;
6137
6138 str = ml_get_buf(buf, lnum, FALSE);
6139 if (str == NULL)
6140 return -1;
6141
6142 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006143 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006144
Bram Moolenaar91458462021-01-13 20:08:38 +01006145 // count the number of characters
6146 t = str;
6147 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6148 t += mb_ptr2len(t);
6149
6150 // In insert mode, when the cursor is at the end of a non-empty line,
6151 // byteidx points to the NUL character immediately past the end of the
6152 // string. In this case, add one to the character count.
6153 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6154 count++;
6155
6156 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006157}
6158
6159/*
6160 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006161 * byte index. Works only for loaded buffers. Returns -1 on failure.
6162 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006163 */
6164 int
6165buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6166{
6167 char_u *str;
6168 char_u *t;
6169
6170 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6171 return -1;
6172
6173 if (lnum > buf->b_ml.ml_line_count)
6174 lnum = buf->b_ml.ml_line_count;
6175
6176 str = ml_get_buf(buf, lnum, FALSE);
6177 if (str == NULL)
6178 return -1;
6179
6180 // Convert the character offset to a byte offset
6181 t = str;
6182 while (*t != NUL && --charidx > 0)
6183 t += mb_ptr2len(t);
6184
Bram Moolenaar91458462021-01-13 20:08:38 +01006185 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006186}
6187
6188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006190 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193var2fpos(
6194 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006196 int *fnum, // set to fnum for '0, 'A, etc.
6197 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006199 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006201 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006204 if (varp->v_type == VAR_LIST)
6205 {
6206 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006207 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006208 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006209 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006210
6211 l = varp->vval.v_list;
6212 if (l == NULL)
6213 return NULL;
6214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006216 pos.lnum = list_find_nr(l, 0L, &error);
6217 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006218 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006219 if (charcol)
6220 len = (long)mb_charlen(ml_get(pos.lnum));
6221 else
John Marriottbfcc8952024-03-11 22:04:45 +01006222 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006223
Bram Moolenaarec65d772020-08-20 22:29:12 +02006224 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006226 li = list_find(l, 1L);
6227 if (li != NULL && li->li_tv.v_type == VAR_STRING
6228 && li->li_tv.vval.v_string != NULL
6229 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006230 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006231 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006232 }
6233 else
6234 {
6235 pos.col = list_find_nr(l, 1L, &error);
6236 if (error)
6237 return NULL;
6238 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006240 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006241 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006242 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006243 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006245 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006246 pos.coladd = list_find_nr(l, 2L, &error);
6247 if (error)
6248 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006249
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006250 return &pos;
6251 }
6252
Bram Moolenaarc5809432021-03-27 21:23:30 +01006253 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6254 return NULL;
6255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006257 if (name == NULL)
6258 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006259
6260 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006261 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006262 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006263 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006264 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006265 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006266 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006267 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006268 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006269 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006270 pos = VIsual;
6271 else
6272 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006273 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006274 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006275 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006277 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006278 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6280 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006281 pos = *pp;
6282 }
6283 if (pos.lnum != 0)
6284 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006285 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006286 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6287 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006289
Bram Moolenaara5525202006-03-02 22:52:09 +00006290 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006291
Bram Moolenaar477933c2007-07-17 14:32:23 +00006292 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006293 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006294 // the "w_valid" flags are not reset when moving the cursor, but they
6295 // do matter for update_topline() and validate_botline().
6296 check_cursor_moved(curwin);
6297
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006298 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006299 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006302 // In silent Ex mode topline is zero, but that's not a valid line
6303 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006304 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006305 return &pos;
6306 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006307 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006308 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006309 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006311 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006312 return &pos;
6313 }
6314 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006315 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006317 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
6319 pos.lnum = curbuf->b_ml.ml_line_count;
6320 pos.col = 0;
6321 }
6322 else
6323 {
6324 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006325 if (charcol)
6326 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6327 else
John Marriottbfcc8952024-03-11 22:04:45 +01006328 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330 return &pos;
6331 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006332 if (in_vim9script())
6333 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 return NULL;
6335}
6336
6337/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006338 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006339 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006340 * Note that the column is passed on as-is, the caller may want to decrement
6341 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006342 * If "charcol" is TRUE use the column as the character index instead of the
6343 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006344 * Return FAIL when conversion is not possible, doesn't check the position for
6345 * validity.
6346 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348list2fpos(
6349 typval_T *arg,
6350 pos_T *posp,
6351 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006352 colnr_T *curswantp,
6353 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006354{
6355 list_T *l = arg->vval.v_list;
6356 long i = 0;
6357 long n;
6358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006359 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6360 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006361 if (arg->v_type != VAR_LIST
6362 || l == NULL
6363 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006364 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006365 return FAIL;
6366
6367 if (fnump != NULL)
6368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006369 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006370 if (n < 0)
6371 return FAIL;
6372 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006374 *fnump = n;
6375 }
6376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006377 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006378 if (n < 0)
6379 return FAIL;
6380 posp->lnum = n;
6381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006382 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006383 if (n < 0)
6384 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006385 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006386 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006387 if (charcol)
6388 {
6389 buf_T *buf;
6390
6391 // Get the text for the specified line in a loaded buffer
6392 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6393 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6394 return FAIL;
6395
Bram Moolenaar79f23442022-10-10 12:42:57 +01006396 n = buf_charidx_to_byteidx(buf,
6397 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006398 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006399 posp->col = n;
6400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006401 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006402 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006403 posp->coladd = 0;
6404 else
6405 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006406
Bram Moolenaar493c1782014-05-28 14:34:46 +02006407 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006409
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006410 return OK;
6411}
6412
6413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * Get the length of an environment variable name.
6415 * Advance "arg" to the first character after the name.
6416 * Return 0 for error.
6417 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006419get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 char_u *p;
6422 int len;
6423
6424 for (p = *arg; vim_isIDc(*p); ++p)
6425 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006426 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 return 0;
6428
6429 len = (int)(p - *arg);
6430 *arg = p;
6431 return len;
6432}
6433
6434/*
6435 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006436 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 * Return 0 if something is wrong.
6438 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 char_u *p;
6443 int len;
6444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006445 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006447 {
6448 if (*p == ':')
6449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006450 // "s:" is start of "s:var", but "n:" is not and can be used in
6451 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006452 len = (int)(p - *arg);
6453 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6454 || len > 1)
6455 break;
6456 }
6457 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006458 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 return 0;
6460
6461 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006462 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 return len;
6465}
6466
6467/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006468 * Get the length of the name of a variable or function.
6469 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006471 * Return -1 if curly braces expansion failed.
6472 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 * If the name contains 'magic' {}'s, expand them and return the
6474 * expanded name in an allocated string via 'alias' - caller must free.
6475 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477get_name_len(
6478 char_u **arg,
6479 char_u **alias,
6480 int evaluate,
6481 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 char_u *p;
6485 char_u *expr_start;
6486 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6491 && (*arg)[2] == (int)KE_SNR)
6492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 *arg += 3;
6495 return get_id_len(arg) + 3;
6496 }
6497 len = eval_fname_script(*arg);
6498 if (len > 0)
6499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006500 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 *arg += len;
6502 }
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006505 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006507 p = find_name_end(*arg, &expr_start, &expr_end,
6508 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (expr_start != NULL)
6510 {
6511 char_u *temp_string;
6512
6513 if (!evaluate)
6514 {
6515 len += (int)(p - *arg);
6516 *arg = skipwhite(p);
6517 return len;
6518 }
6519
6520 /*
6521 * Include any <SID> etc in the expanded string:
6522 * Thus the -len here.
6523 */
6524 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6525 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006526 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 *alias = temp_string;
6528 *arg = skipwhite(p);
6529 return (int)STRLEN(temp_string);
6530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006533 // Only give an error when there is something, otherwise it will be
6534 // reported at a higher level.
6535 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006536 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
6538 return len;
6539}
6540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006541/*
6542 * Find the end of a variable or function name, taking care of magic braces.
6543 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6544 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006545 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 * Return a pointer to just after the name. Equal to "arg" if there is no
6547 * valid name.
6548 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550find_name_end(
6551 char_u *arg,
6552 char_u **expr_start,
6553 char_u **expr_end,
6554 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556 int mb_nest = 0;
6557 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006559 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006560 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564 *expr_start = NULL;
6565 *expr_end = NULL;
6566 }
6567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006569 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006570 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006571 return arg;
6572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573 for (p = arg; *p != NUL
6574 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006575 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006576 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006577 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006579 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006580 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006581 if (*p == '\'')
6582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006584 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006585 ;
6586 if (*p == NUL)
6587 break;
6588 }
6589 else if (*p == '"')
6590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006591 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006592 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006593 if (*p == '\\' && p[1] != NUL)
6594 ++p;
6595 if (*p == NUL)
6596 break;
6597 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006598 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006600 // "s:" is start of "s:var", but "n:" is not and can be used in
6601 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006602 len = (int)(p - arg);
6603 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006604 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006605 break;
6606 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006608 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 if (*p == '[')
6611 ++br_nest;
6612 else if (*p == ']')
6613 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006615
zeertzjqa93d9cd2023-05-02 16:25:47 +01006616 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618 if (*p == '{')
6619 {
6620 mb_nest++;
6621 if (expr_start != NULL && *expr_start == NULL)
6622 *expr_start = p;
6623 }
6624 else if (*p == '}')
6625 {
6626 mb_nest--;
6627 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6628 *expr_end = p;
6629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 }
6632
6633 return p;
6634}
6635
6636/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006637 * Expands out the 'magic' {}'s in a variable/function name.
6638 * Note that this can call itself recursively, to deal with
6639 * constructs like foo{bar}{baz}{bam}
6640 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6641 * "in_start" ^
6642 * "expr_start" ^
6643 * "expr_end" ^
6644 * "in_end" ^
6645 *
6646 * Returns a new allocated string, which the caller must free.
6647 * Returns NULL for failure.
6648 */
6649 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650make_expanded_name(
6651 char_u *in_start,
6652 char_u *expr_start,
6653 char_u *expr_end,
6654 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006655{
6656 char_u c1;
6657 char_u *retval = NULL;
6658 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006659
6660 if (expr_end == NULL || in_end == NULL)
6661 return NULL;
6662 *expr_start = NUL;
6663 *expr_end = NUL;
6664 c1 = *in_end;
6665 *in_end = NUL;
6666
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006667 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006668 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006669 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006670 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6671 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006672 if (retval != NULL)
6673 {
6674 STRCPY(retval, in_start);
6675 STRCAT(retval, temp_result);
6676 STRCAT(retval, expr_end + 1);
6677 }
6678 }
6679 vim_free(temp_result);
6680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006681 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006682 *expr_start = '{';
6683 *expr_end = '}';
6684
6685 if (retval != NULL)
6686 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006687 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006688 if (expr_start != NULL)
6689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006691 temp_result = make_expanded_name(retval, expr_start,
6692 expr_end, temp_result);
6693 vim_free(retval);
6694 retval = temp_result;
6695 }
6696 }
6697
6698 return retval;
6699}
6700
6701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006703 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006708 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006709}
6710
6711/*
6712 * Return TRUE if character "c" can be used as the first character in a
6713 * variable or function name (excluding '{' and '}').
6714 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006717{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006718 return ASCII_ISALPHA(c) || c == '_';
6719}
6720
6721/*
6722 * Return TRUE if character "c" can be used as the first character of a
6723 * dictionary key.
6724 */
6725 int
6726eval_isdictc(int c)
6727{
6728 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729}
6730
6731/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006732 * Handle:
6733 * - expr[expr], expr[expr:expr] subscript
6734 * - ".name" lookup
6735 * - function call with Funcref variable: func(expr)
6736 * - method call: var->method()
6737 *
6738 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006739 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006742handle_subscript(
6743 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006744 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006745 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006746 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006747 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006748{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006749 int evaluate = evalarg != NULL
6750 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006751 int ret = OK;
6752 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006753 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006754 int getnext;
6755 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006756
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006757 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006758 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006759 // When at the end of the line and ".name" or "->{" or "->X" follows in
6760 // the next line then consume the line break.
6761 p = eval_next_non_blank(*arg, evalarg, &getnext);
6762 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006763 && ((*p == '.'
6764 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6765 || rettv->v_type == VAR_CLASS
6766 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006767 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6768 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6769 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006770 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006771 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006772 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006773 check_white = FALSE;
6774 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006775
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006776 if (rettv->v_type == VAR_ANY)
6777 {
6778 char_u *exp_name;
6779 int cc;
6780 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006781 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006782 type_T *type;
6783
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006784 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006785 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006786 if (**arg != '.')
6787 {
6788 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006789 semsg(_(e_expected_dot_after_name_str),
6790 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006791 ret = FAIL;
6792 break;
6793 }
6794 ++*arg;
6795 if (IS_WHITE_OR_NUL(**arg))
6796 {
6797 if (verbose)
6798 emsg(_(e_no_white_space_allowed_after_dot));
6799 ret = FAIL;
6800 break;
6801 }
6802
6803 // isolate the name
6804 exp_name = *arg;
6805 while (eval_isnamec(**arg))
6806 ++*arg;
6807 cc = **arg;
6808 **arg = NUL;
6809
6810 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006811 evalarg == NULL ? NULL : evalarg->eval_cctx,
6812 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006813 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006814
6815 if (idx < 0 && ufunc == NULL)
6816 {
6817 ret = FAIL;
6818 break;
6819 }
6820 if (idx >= 0)
6821 {
6822 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6823 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6824
6825 copy_tv(sv->sv_tv, rettv);
6826 }
6827 else
6828 {
6829 rettv->v_type = VAR_FUNC;
6830 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6831 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006832 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006833 }
6834
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006835 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6836 || rettv->v_type == VAR_PARTIAL))
6837 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006838 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006839 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6840 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006841
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006842 // Stop the expression evaluation when immediately aborting on
6843 // error, or when an interrupt occurred or an exception was thrown
6844 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006845 if (aborting())
6846 {
6847 if (ret == OK)
6848 clear_tv(rettv);
6849 ret = FAIL;
6850 }
6851 dict_unref(selfdict);
6852 selfdict = NULL;
6853 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006854 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006855 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006856 if (in_vim9script())
6857 *arg = skipwhite(p + 2);
6858 else
6859 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006860 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006861 {
zeertzjqc481ad32023-03-11 16:18:51 +00006862 emsg(_(e_no_white_space_allowed_before_parenthesis));
6863 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006864 }
zeertzjqc481ad32023-03-11 16:18:51 +00006865 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6866 // expr->{lambda}() or expr->(lambda)()
6867 ret = eval_lambda(arg, rettv, evalarg, verbose);
6868 else
6869 // expr->name()
6870 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006871 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006872 // "." is ".name" lookup when we found a dict or when evaluating and
6873 // scriptversion is at least 2, where string concatenation is "..".
6874 else if (**arg == '['
6875 || (**arg == '.' && (rettv->v_type == VAR_DICT
6876 || (!evaluate
6877 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006878 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006879 {
6880 dict_unref(selfdict);
6881 if (rettv->v_type == VAR_DICT)
6882 {
6883 selfdict = rettv->vval.v_dict;
6884 if (selfdict != NULL)
6885 ++selfdict->dv_refcount;
6886 }
6887 else
6888 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006889 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006890 {
6891 clear_tv(rettv);
6892 ret = FAIL;
6893 }
6894 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006895 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6896 || rettv->v_type == VAR_OBJECT))
6897 {
6898 // class member: SomeClass.varname
6899 // class method: SomeClass.SomeMethod()
6900 // class constructor: SomeClass.new()
6901 // object member: someObject.varname
6902 // object method: someObject.SomeMethod()
6903 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6904 {
6905 clear_tv(rettv);
6906 ret = FAIL;
6907 }
6908 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006909 else
6910 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006911 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006913 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6914 // Don't do this when "Func" is already a partial that was bound
6915 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006916 if (selfdict != NULL
6917 && (rettv->v_type == VAR_FUNC
6918 || (rettv->v_type == VAR_PARTIAL
6919 && (rettv->vval.v_partial->pt_auto
6920 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006921 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006922
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006923 dict_unref(selfdict);
6924 return ret;
6925}
6926
6927/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 * Make a copy of an item.
6929 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006930 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006931 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6932 * reference to an already copied list/dict can be used.
6933 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006935 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936item_copy(
6937 typval_T *from,
6938 typval_T *to,
6939 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006940 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942{
6943 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006948 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006949 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950 }
6951 ++recurse;
6952
6953 switch (from->v_type)
6954 {
6955 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006956 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006957 case VAR_STRING:
6958 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006959 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006960 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006961 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006962 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006963 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006964 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006965 case VAR_CLASS:
6966 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006967 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 copy_tv(from, to);
6969 break;
6970 case VAR_LIST:
6971 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006972 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006973 if (from->vval.v_list == NULL)
6974 to->vval.v_list = NULL;
6975 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006977 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006978 to->vval.v_list = from->vval.v_list->lv_copylist;
6979 ++to->vval.v_list->lv_refcount;
6980 }
6981 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006982 to->vval.v_list = list_copy(from->vval.v_list,
6983 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 if (to->vval.v_list == NULL)
6985 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006987 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006989 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006990 case VAR_DICT:
6991 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006993 if (from->vval.v_dict == NULL)
6994 to->vval.v_dict = NULL;
6995 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006997 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6999 ++to->vval.v_dict->dv_refcount;
7000 }
7001 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007002 to->vval.v_dict = dict_copy(from->vval.v_dict,
7003 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007004 if (to->vval.v_dict == NULL)
7005 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007006 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007007 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007008 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007010 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007011 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 }
7013 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007014 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015}
7016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 void
7018echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7019{
7020 char_u *tofree;
7021 char_u numbuf[NUMBUFLEN];
7022 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7023
7024 if (*atstart)
7025 {
7026 *atstart = FALSE;
7027 // Call msg_start() after eval1(), evaluating the expression
7028 // may cause a message to appear.
7029 if (with_space)
7030 {
7031 // Mark the saved text as finishing the line, so that what
7032 // follows is displayed on a new line when scrolling back
7033 // at the more prompt.
7034 msg_sb_eol();
7035 msg_start();
7036 }
7037 }
7038 else if (with_space)
7039 msg_puts_attr(" ", echo_attr);
7040
7041 if (p != NULL)
7042 for ( ; *p != NUL && !got_int; ++p)
7043 {
7044 if (*p == '\n' || *p == '\r' || *p == TAB)
7045 {
7046 if (*p != TAB && *needclr)
7047 {
7048 // remove any text still there from the command
7049 msg_clr_eos();
7050 *needclr = FALSE;
7051 }
7052 msg_putchar_attr(*p, echo_attr);
7053 }
7054 else
7055 {
7056 if (has_mbyte)
7057 {
7058 int i = (*mb_ptr2len)(p);
7059
7060 (void)msg_outtrans_len_attr(p, i, echo_attr);
7061 p += i - 1;
7062 }
7063 else
7064 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7065 }
7066 }
7067 vim_free(tofree);
7068}
7069
Bram Moolenaare9a41262005-01-15 22:18:47 +00007070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 * ":echo expr1 ..." print each argument separated with a space, add a
7072 * newline at the end.
7073 * ":echon expr1 ..." print each argument plain.
7074 */
7075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007076ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007080 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 int needclr = TRUE;
7082 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007083 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007084 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007085 evalarg_T evalarg;
7086
Bram Moolenaare707c882020-07-01 13:07:37 +02007087 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
7089 if (eap->skip)
7090 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007091 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007093 // If eval1() causes an error message the text from the command may
7094 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007095 need_clr_eos = needclr;
7096
Bram Moolenaar68db9962021-05-09 23:19:22 +02007097 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007098 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
7100 /*
7101 * Report the invalid expression unless the expression evaluation
7102 * has been cancelled due to an aborting error, an interrupt, or an
7103 * exception.
7104 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007105 if (!aborting() && did_emsg == did_emsg_before
7106 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007107 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007108 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 break;
7110 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007111 need_clr_eos = FALSE;
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007114 {
7115 if (rettv.v_type == VAR_VOID)
7116 {
7117 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7118 break;
7119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007121 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007123 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 arg = skipwhite(arg);
7125 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007126 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007127 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129 if (eap->skip)
7130 --emsg_skip;
7131 else
7132 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007133 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (needclr)
7135 msg_clr_eos();
7136 if (eap->cmdidx == CMD_echo)
7137 msg_end();
7138 }
7139}
7140
7141/*
7142 * ":echohl {name}".
7143 */
7144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007145ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007147 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
7150/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007151 * Returns the :echo attribute
7152 */
7153 int
7154get_echo_attr(void)
7155{
7156 return echo_attr;
7157}
7158
7159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 * ":execute expr1 ..." execute the result of an expression.
7161 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007162 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007164 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 * Each gets spaces around each argument and a newline at the end for
7166 * echo commands
7167 */
7168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 int ret = OK;
7174 char_u *p;
7175 garray_T ga;
7176 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007177 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179 ga_init2(&ga, 1, 80);
7180
7181 if (eap->skip)
7182 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007183 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007185 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007186 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189 if (!eap->skip)
7190 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007191 char_u buf[NUMBUFLEN];
7192
7193 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007194 {
7195 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7196 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007197 semsg(_(e_using_invalid_value_as_string_str),
7198 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007199 p = NULL;
7200 }
7201 else
7202 p = tv_get_string_buf(&rettv, buf);
7203 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007204 else
7205 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007206 if (p == NULL)
7207 {
7208 clear_tv(&rettv);
7209 ret = FAIL;
7210 break;
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 len = (int)STRLEN(p);
7213 if (ga_grow(&ga, len + 2) == FAIL)
7214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 ret = FAIL;
7217 break;
7218 }
7219 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 ga.ga_len += len;
7223 }
7224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 arg = skipwhite(arg);
7227 }
7228
7229 if (ret != FAIL && ga.ga_data != NULL)
7230 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007231 // use the first line of continuation lines for messages
7232 SOURCING_LNUM = start_lnum;
7233
Bram Moolenaar37fef162022-08-29 18:16:32 +01007234 if (eap->cmdidx == CMD_echomsg
7235 || eap->cmdidx == CMD_echowindow
7236 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007238 // Mark the already saved text as finishing the line, so that what
7239 // follows is displayed on a new line when scrolling back at the
7240 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007241 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007242 }
7243
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007244 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007245 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007246 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007247 out_flush();
7248 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007249 else if (eap->cmdidx == CMD_echowindow)
7250 {
7251#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007252 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007253#endif
7254 msg_attr(ga.ga_data, echo_attr);
7255#ifdef HAS_MESSAGE_WINDOW
7256 end_echowindow();
7257#endif
7258 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007259 else if (eap->cmdidx == CMD_echoconsole)
7260 {
7261 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7262 ui_write((char_u *)"\r\n", 2, TRUE);
7263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 else if (eap->cmdidx == CMD_echoerr)
7265 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007266 int save_did_emsg = did_emsg;
7267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007268 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007269 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (!force_abort)
7271 did_emsg = save_did_emsg;
7272 }
7273 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007274 {
7275 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7276
7277 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7278 sticky_cmdmod_flags = cmdmod.cmod_flags
7279 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007281 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007282 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285
7286 ga_clear(&ga);
7287
7288 if (eap->skip)
7289 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007290 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
7292
7293/*
7294 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7295 * "arg" points to the "&" or '+' when called, to "option" when returning.
7296 * Returns NULL when no option name found. Otherwise pointer to the char
7297 * after the option name.
7298 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007299 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007300find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 char_u *p = *arg;
7303
7304 ++p;
7305 if (*p == 'g' && p[1] == ':')
7306 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007307 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 p += 2;
7309 }
7310 else if (*p == 'l' && p[1] == ':')
7311 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007312 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 p += 2;
7314 }
7315 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007316 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318 if (!ASCII_ISALPHA(*p))
7319 return NULL;
7320 *arg = p;
7321
7322 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007323 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 else
7325 while (ASCII_ISALPHA(*p))
7326 ++p;
7327 return p;
7328}
7329
7330/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007331 * Display script name where an item was last set.
7332 * Should only be invoked when 'verbose' is non-zero.
7333 */
7334 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007335last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007336{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007337 char_u *p;
7338
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007339 if (script_ctx.sc_sid == 0)
7340 return;
7341
7342 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7343 if (p == NULL)
7344 return;
7345
7346 verbose_enter();
7347 msg_puts(_("\n\tLast set from "));
7348 msg_puts((char *)p);
7349 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007350 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007351 msg_puts(_(line_msg));
7352 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007353 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007354 verbose_leave();
7355 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007356}
7357
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007358#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
7360/*
7361 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007362 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 * "flags" can be "g" to do a global substitute.
7364 * Returns an allocated string, NULL for error.
7365 */
7366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007367do_string_sub(
7368 char_u *str,
7369 char_u *pat,
7370 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007371 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007372 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 int sublen;
7375 regmatch_T regmatch;
7376 int i;
7377 int do_all;
7378 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007379 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 garray_T ga;
7381 char_u *ret;
7382 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007383 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007385 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007387 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
7389 ga_init2(&ga, 1, 200);
7390
7391 do_all = (flags[0] == 'g');
7392
7393 regmatch.rm_ic = p_ic;
7394 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7395 if (regmatch.regprog != NULL)
7396 {
7397 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007398 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7400 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007401 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007402 if (regmatch.startp[0] == regmatch.endp[0])
7403 {
7404 if (zero_width == regmatch.startp[0])
7405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007406 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007407 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007408 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7409 (size_t)i);
7410 ga.ga_len += i;
7411 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007412 continue;
7413 }
7414 zero_width = regmatch.startp[0];
7415 }
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 /*
7418 * Get some space for a temporary buffer to do the substitution
7419 * into. It will contain:
7420 * - The text up to where the match is.
7421 * - The substituted text.
7422 * - The text after the match.
7423 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007424 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007425 if (sublen <= 0)
7426 {
7427 ga_clear(&ga);
7428 break;
7429 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007430 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7432 {
7433 ga_clear(&ga);
7434 break;
7435 }
7436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007437 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 i = (int)(regmatch.startp[0] - tail);
7439 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007440 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007441 (void)vim_regsub(&regmatch, sub, expr,
7442 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7443 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007445 tail = regmatch.endp[0];
7446 if (*tail == NUL)
7447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (!do_all)
7449 break;
7450 }
7451
7452 if (ga.ga_data != NULL)
7453 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7454
Bram Moolenaar473de612013-06-08 18:19:48 +02007455 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457
7458 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7459 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007460 if (p_cpo == empty_option)
7461 p_cpo = save_cpo;
7462 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007463 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007464 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007465 // If it's still empty it was changed and restored, need to restore in
7466 // the complicated way.
7467 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007468 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007469 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 return ret;
7473}