blob: cc289b4535e3c0f52c698c0e10af613c235aef68 [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(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001302 lval_T *lp,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001303 char_u *name,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001304 char_u *key,
1305 int len,
1306 char_u **key_end,
1307 typval_T *var1,
1308 int flags,
1309 int unlet,
1310 typval_T *rettv)
1311{
1312 int quiet = flags & GLV_QUIET;
1313 char_u *p = *key_end;
1314
1315 if (len == -1)
1316 {
1317 // "[key]": get key from "var1"
1318 key = tv_get_string_chk(var1); // is number or string
1319 if (key == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001320 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001321 }
1322 lp->ll_list = NULL;
1323 lp->ll_object = NULL;
1324 lp->ll_class = NULL;
1325
1326 // a NULL dict is equivalent with an empty dict
1327 if (lp->ll_tv->vval.v_dict == NULL)
1328 {
1329 lp->ll_tv->vval.v_dict = dict_alloc();
1330 if (lp->ll_tv->vval.v_dict == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001331 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001332 ++lp->ll_tv->vval.v_dict->dv_refcount;
1333 }
1334 lp->ll_dict = lp->ll_tv->vval.v_dict;
1335
1336 lp->ll_di = dict_find(lp->ll_dict, key, len);
1337
1338 // When assigning to a scope dictionary check that a function and
1339 // variable name is valid (only variable name unless it is l: or
1340 // g: dictionary). Disallow overwriting a builtin function.
1341 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1342 {
1343 int prevval;
1344
1345 if (len != -1)
1346 {
1347 prevval = key[len];
1348 key[len] = NUL;
1349 }
1350 else
1351 prevval = 0; // avoid compiler warning
1352 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1353 && (rettv->v_type == VAR_FUNC
1354 || rettv->v_type == VAR_PARTIAL)
1355 && var_wrong_func_name(key, lp->ll_di == NULL))
1356 || !valid_varname(key, -1, TRUE);
1357 if (len != -1)
1358 key[len] = prevval;
1359 if (wrong)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001360 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001361 }
1362
1363 if (lp->ll_valtype != NULL)
1364 // use the type of the member
1365 lp->ll_valtype = lp->ll_valtype->tt_member;
1366
1367 if (lp->ll_di == NULL)
1368 {
1369 // Can't add "v:" or "a:" variable.
1370 if (lp->ll_dict == get_vimvar_dict()
1371 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1372 {
1373 semsg(_(e_illegal_variable_name_str), name);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001374 return GLV_FAIL;
1375 }
1376
1377 // Key does not exist in dict: may need to add it.
1378 if (*p == '[' || *p == '.' || unlet)
1379 {
1380 if (!quiet)
1381 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001382 return GLV_FAIL;
1383 }
1384 if (len == -1)
1385 lp->ll_newkey = vim_strsave(key);
1386 else
1387 lp->ll_newkey = vim_strnsave(key, len);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001388 if (lp->ll_newkey == NULL)
1389 p = NULL;
1390
1391 *key_end = p;
1392 return GLV_STOP;
1393 }
1394 // existing variable, need to check if it can be changed
1395 else if ((flags & GLV_READ_ONLY) == 0
1396 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1397 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001398 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001399
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001400 lp->ll_tv = &lp->ll_di->di_tv;
1401
1402 return GLV_OK;
1403}
1404
1405/*
1406 * Get an blob lval variable that can be assigned a value to: "name",
1407 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1408 *
1409 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1410 * blob index. If the first index is not specified in a range, then 'empty1'
1411 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1412 * invalid indexes.
1413 *
1414 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1415 */
1416 static int
1417get_lval_blob(
1418 lval_T *lp,
1419 typval_T *var1,
1420 typval_T *var2,
1421 int empty1,
1422 int quiet)
1423{
1424 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1425
1426 // Get the number and item for the only or first index of the List.
1427 if (empty1)
1428 lp->ll_n1 = 0;
1429 else
1430 // is number or string
1431 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001432
1433 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001434 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001435 if (lp->ll_range && !lp->ll_empty2)
1436 {
1437 lp->ll_n2 = (long)tv_get_number(var2);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001438 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1439 return FAIL;
1440 }
1441
1442 if (!lp->ll_range)
1443 // Indexing a single byte in a blob. So the rhs type is a
1444 // number.
1445 lp->ll_valtype = &t_number;
1446
1447 lp->ll_blob = lp->ll_tv->vval.v_blob;
1448 lp->ll_tv = NULL;
1449
1450 return OK;
1451}
1452
1453/*
1454 * Get a List lval variable that can be assigned a value to: "name",
1455 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1456 *
1457 * 'var1' specifies the starting List index and 'var2' specifies the ending
1458 * List index. If the first index is not specified in a range, then 'empty1'
1459 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1460 * invalid indexes.
1461 *
1462 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1463 */
1464 static int
1465get_lval_list(
1466 lval_T *lp,
1467 typval_T *var1,
1468 typval_T *var2,
1469 int empty1,
1470 int flags,
1471 int quiet)
1472{
1473 /*
1474 * Get the number and item for the only or first index of the List.
1475 */
1476 if (empty1)
1477 lp->ll_n1 = 0;
1478 else
1479 // is number or string
1480 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001481
1482 lp->ll_dict = NULL;
1483 lp->ll_object = NULL;
1484 lp->ll_class = NULL;
1485 lp->ll_list = lp->ll_tv->vval.v_list;
1486 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1487 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1488 if (lp->ll_li == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001489 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001490
1491 if (lp->ll_valtype != NULL && !lp->ll_range)
1492 // use the type of the member
1493 lp->ll_valtype = lp->ll_valtype->tt_member;
1494
1495 /*
1496 * May need to find the item or absolute index for the second
1497 * index of a range.
1498 * When no index given: "lp->ll_empty2" is TRUE.
1499 * Otherwise "lp->ll_n2" is set to the second index.
1500 */
1501 if (lp->ll_range && !lp->ll_empty2)
1502 {
1503 lp->ll_n2 = (long)tv_get_number(var2);
1504 // is number or string
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001505 if (check_range_index_two(lp->ll_list,
1506 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1507 return FAIL;
1508 }
1509
1510 lp->ll_tv = &lp->ll_li->li_tv;
1511
1512 return OK;
1513}
1514
1515/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001516 * Get a class or object lval method in class "cl". The 'key' argument points
1517 * to the method name and 'key_end' points to the character after 'key'.
1518 * 'v_type' is VAR_CLASS or VAR_OBJECT.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001519 *
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001520 * The method index, method function pointer and method type are returned in
1521 * "lp".
1522 */
1523 static void
1524get_lval_oc_method(
1525 lval_T *lp,
1526 class_T *cl,
1527 char_u *key,
1528 char_u *key_end,
1529 vartype_T v_type)
1530{
1531 // Look for a method with this name.
1532 // round 1: class functions (skipped for an object)
1533 // round 2: object methods
1534 for (int round = v_type == VAR_OBJECT ? 2 : 1; round <= 2; ++round)
1535 {
1536 int m_idx;
1537 ufunc_T *fp;
1538
1539 fp = method_lookup(cl, round == 1 ? VAR_CLASS : VAR_OBJECT,
1540 key, key_end - key, &m_idx);
1541 lp->ll_oi = m_idx;
1542 if (fp != NULL)
1543 {
1544 lp->ll_ufunc = fp;
1545 lp->ll_valtype = fp->uf_func_type;
1546 break;
1547 }
1548 }
1549}
1550
1551/*
1552 * Get a class or object lval variable in class "cl". The "key" argument
1553 * points to the variable name and "key_end" points to the character after
1554 * "key". "v_type" is VAR_CLASS or VAR_OBJECT. "cl_exec" is the class that is
1555 * executing, or NULL.
1556 *
1557 * The variable index, typval and type are returned in "lp". Returns FAIL if
1558 * the variable is not writable. Otherwise returns OK.
1559 */
1560 static int
1561get_lval_oc_variable(
1562 lval_T *lp,
1563 class_T *cl,
1564 char_u *key,
1565 char_u *key_end,
1566 vartype_T v_type,
1567 class_T *cl_exec,
1568 int flags)
1569{
1570 int m_idx;
1571 ocmember_T *om;
1572
1573 om = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1574 lp->ll_oi = m_idx;
1575 if (om == NULL)
1576 return OK;
1577
1578 // Check variable is accessible
1579 if (get_lval_check_access(cl_exec, cl, om, key_end, flags) == FAIL)
1580 return FAIL;
1581
1582 // When lhs is used to modify the variable, check it is not a read-only
1583 // variable.
1584 if ((flags & GLV_READ_ONLY) == 0 && (*key_end != '.' && *key_end != '[')
1585 && oc_var_check_ro(cl, om))
1586 return FAIL;
1587
1588 lp->ll_valtype = om->ocm_type;
1589
1590 if (v_type == VAR_OBJECT)
1591 lp->ll_tv = ((typval_T *)(lp->ll_tv->vval.v_object + 1)) + m_idx;
1592 else
1593 lp->ll_tv = &cl->class_members_tv[m_idx];
1594
1595 return OK;
1596}
1597
1598/*
1599 * Get a Class or Object lval variable or method that can be assigned a value
1600 * to: "name", "name.key", "name.key[expr]" etc.
1601 *
1602 * The 'key' argument points to the member name and 'key_end' points to the
1603 * character after 'key'. 'v_type' is VAR_CLASS or VAR_OBJECT. 'cl_exec' is
1604 * the class that is executing, or NULL. If 'quiet' is TRUE, then error
1605 * messages are not displayed for invalid indexes.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001606 *
1607 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1608 * failure.
1609 */
1610 static int
1611get_lval_class_or_obj(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001612 lval_T *lp,
1613 char_u *key,
1614 char_u *key_end,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001615 vartype_T v_type,
1616 class_T *cl_exec,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001617 int flags,
1618 int quiet)
1619{
1620 lp->ll_dict = NULL;
1621 lp->ll_list = NULL;
1622
1623 class_T *cl;
1624 if (v_type == VAR_OBJECT)
1625 {
1626 if (lp->ll_tv->vval.v_object == NULL)
1627 {
1628 if (!quiet)
1629 emsg(_(e_using_null_object));
1630 return FAIL;
1631 }
1632 cl = lp->ll_tv->vval.v_object->obj_class;
1633 lp->ll_object = lp->ll_tv->vval.v_object;
1634 }
1635 else
1636 {
1637 cl = lp->ll_tv->vval.v_class;
1638 lp->ll_object = NULL;
1639 }
1640 lp->ll_class = cl;
1641
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001642 if (cl == NULL)
1643 // TODO: what if class is NULL?
1644 return OK;
1645
1646 lp->ll_valtype = NULL;
1647
1648 if (flags & GLV_PREFER_FUNC)
1649 get_lval_oc_method(lp, cl, key, key_end, v_type);
1650
1651 // Look for object/class member variable
1652 if (lp->ll_valtype == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001653 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001654 if (get_lval_oc_variable(lp, cl, key, key_end, v_type, cl_exec, flags)
1655 == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001656 return FAIL;
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001657 }
1658
1659 if (lp->ll_valtype == NULL)
1660 {
1661 member_not_found_msg(cl, v_type, key, key_end - key);
1662 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001663 }
1664
1665 return OK;
1666}
1667
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001668/*
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001669 * Check whether dot (".") is allowed after the variable "name" with type
1670 * "v_type". Only Dict, Class and Object types support a dot after the name.
1671 * Returns TRUE if dot is allowed after the name.
1672 */
1673 static int
1674dot_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1675{
1676 if (v_type != VAR_DICT && v_type != VAR_OBJECT && v_type != VAR_CLASS)
1677 {
1678 if (!quiet)
1679 semsg(_(e_dot_not_allowed_after_str_str),
1680 vartype_name(v_type), name);
1681 return FALSE;
1682 }
1683
1684 return TRUE;
1685}
1686
1687/*
1688 * Check whether left bracket ("[") is allowed after the variable "name" with
1689 * type "v_type". Only Dict, List and Blob types support a bracket after the
1690 * variable name. Returns TRUE if bracket is allowed after the name.
1691 */
1692 static int
1693bracket_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1694{
1695 if (v_type == VAR_CLASS || v_type == VAR_OBJECT)
1696 {
1697 if (!quiet)
1698 semsg(_(e_index_not_allowed_after_str_str),
1699 vartype_name(v_type), name);
1700 return FALSE;
1701 }
1702
1703 return TRUE;
1704}
1705
1706/*
1707 * Check whether the variable "name" with type "v_type" can be followed by an
1708 * index. Only Dict, List, Blob, Object and Class types support indexing.
1709 * Returns TRUE if indexing is allowed after the name.
1710 */
1711 static int
1712index_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1713{
1714 if (v_type != VAR_LIST && v_type != VAR_DICT && v_type != VAR_BLOB &&
1715 v_type != VAR_OBJECT && v_type != VAR_CLASS)
1716 {
1717 if (!quiet)
1718 semsg(_(e_index_not_allowed_after_str_str),
1719 vartype_name(v_type), name);
1720 return FALSE;
1721 }
1722
1723 return TRUE;
1724}
1725
1726/*
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001727 * Get the lval of a list/dict/blob/object/class subitem starting at "p". Loop
1728 * until no more [idx] or .key is following.
1729 *
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001730 * If "rettv" is not NULL it points to the value to be assigned.
1731 * "unlet" is TRUE for ":unlet".
1732 *
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001733 * Returns a pointer to the character after the subscript on success or NULL on
1734 * failure.
1735 */
1736 static char_u *
1737get_lval_subscript(
1738 lval_T *lp,
1739 char_u *p,
1740 char_u *name,
1741 typval_T *rettv,
1742 hashtab_T *ht,
1743 dictitem_T *v,
1744 int unlet,
1745 int flags, // GLV_ values
1746 class_T *cl_exec)
1747{
1748 int vim9script = in_vim9script();
1749 int quiet = flags & GLV_QUIET;
1750 char_u *key = NULL;
1751 int len;
1752 typval_T var1;
1753 typval_T var2;
1754 int empty1 = FALSE;
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001755 int rc = FAIL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001756
1757 /*
1758 * Loop until no more [idx] or .key is following.
1759 */
1760 var1.v_type = VAR_UNKNOWN;
1761 var2.v_type = VAR_UNKNOWN;
1762
1763 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1764 {
1765 vartype_T v_type = lp->ll_tv->v_type;
1766
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001767 if (*p == '.' && !dot_allowed_after_type(name, v_type, quiet))
1768 goto done;
1769
1770 if (*p == '[' && !bracket_allowed_after_type(name, v_type, quiet))
1771 goto done;
1772
1773 if (!index_allowed_after_type(name, v_type, quiet))
1774 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001775
1776 // A NULL list/blob works like an empty list/blob, allocate one now.
1777 int r = OK;
1778 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1779 r = rettv_list_alloc(lp->ll_tv);
1780 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1781 r = rettv_blob_alloc(lp->ll_tv);
1782 if (r == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001783 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001784
1785 if (lp->ll_range)
1786 {
1787 if (!quiet)
1788 emsg(_(e_slice_must_come_last));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001789 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001790 }
1791#ifdef LOG_LOCKVAR
1792 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1793 vartype_name(v_type));
1794#endif
1795
1796 if (vim9script && lp->ll_valtype == NULL
1797 && v != NULL
1798 && lp->ll_tv == &v->di_tv
1799 && ht != NULL && ht == get_script_local_ht())
1800 {
1801 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1802
1803 // Vim9 script local variable: get the type
1804 if (sv != NULL)
1805 {
1806 lp->ll_valtype = sv->sv_type;
1807#ifdef LOG_LOCKVAR
1808 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1809 vartype_name(lp->ll_valtype->tt_type));
1810#endif
1811 }
1812 }
1813
1814 len = -1;
1815 if (*p == '.')
1816 {
1817 key = p + 1;
1818
1819 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1820 ;
1821 if (len == 0)
1822 {
1823 if (!quiet)
1824 emsg(_(e_cannot_use_empty_key_for_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001825 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001826 }
1827 p = key + len;
1828 }
1829 else
1830 {
1831 // Get the index [expr] or the first index [expr: ].
1832 p = skipwhite(p + 1);
1833 if (*p == ':')
1834 empty1 = TRUE;
1835 else
1836 {
1837 empty1 = FALSE;
1838 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001839 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001840 if (tv_get_string_chk(&var1) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001841 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001842 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001843 p = skipwhite(p);
1844 }
1845
1846 // Optionally get the second index [ :expr].
1847 if (*p == ':')
1848 {
1849 if (v_type == VAR_DICT)
1850 {
1851 if (!quiet)
1852 emsg(_(e_cannot_slice_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001853 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001854 }
1855 if (rettv != NULL
1856 && !(rettv->v_type == VAR_LIST
1857 && rettv->vval.v_list != NULL)
1858 && !(rettv->v_type == VAR_BLOB
1859 && rettv->vval.v_blob != NULL))
1860 {
1861 if (!quiet)
1862 emsg(_(e_slice_requires_list_or_blob_value));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001863 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001864 }
1865 p = skipwhite(p + 1);
1866 if (*p == ']')
1867 lp->ll_empty2 = TRUE;
1868 else
1869 {
1870 lp->ll_empty2 = FALSE;
1871 // recursive!
1872 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001873 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001874 if (tv_get_string_chk(&var2) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001875 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001876 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001877 }
1878 lp->ll_range = TRUE;
1879 }
1880 else
1881 lp->ll_range = FALSE;
1882
1883 if (*p != ']')
1884 {
1885 if (!quiet)
1886 emsg(_(e_missing_closing_square_brace));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001887 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001888 }
1889
1890 // Skip to past ']'.
1891 ++p;
1892 }
1893#ifdef LOG_LOCKVAR
1894 if (len == -1)
1895 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1896 empty1 ? ":" : (char*)tv_get_string(&var1));
1897 else
1898 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
1899#endif
1900
1901 if (v_type == VAR_DICT)
1902 {
1903 glv_status_T glv_status;
1904
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001905 glv_status = get_lval_dict_item(lp, name, key, len, &p, &var1,
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001906 flags, unlet, rettv);
1907 if (glv_status == GLV_FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001908 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001909 if (glv_status == GLV_STOP)
1910 break;
1911 }
1912 else if (v_type == VAR_BLOB)
1913 {
1914 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001915 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001916
1917 break;
1918 }
1919 else if (v_type == VAR_LIST)
1920 {
1921 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001922 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001923 }
1924 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
1925 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001926 if (get_lval_class_or_obj(lp, key, p, v_type, cl_exec, flags,
1927 quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001928 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001929 }
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001930
1931 clear_tv(&var1);
1932 clear_tv(&var2);
1933 var1.v_type = VAR_UNKNOWN;
1934 var2.v_type = VAR_UNKNOWN;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001935 }
1936
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001937 rc = OK;
1938
1939done:
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001940 clear_tv(&var1);
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001941 clear_tv(&var2);
1942 return rc == OK ? p : NULL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001943}
1944
1945/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 * Get an lval: variable, Dict item or List item that can be assigned a value
1947 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1948 * "name.key", "name.key[expr]" etc.
1949 * Indexing only works if "name" is an existing List or Dictionary.
1950 * "name" points to the start of the name.
1951 * If "rettv" is not NULL it points to the value to be assigned.
1952 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1953 * wrong; must end in space or cmd separator.
1954 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001955 * flags:
1956 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001957 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001958 * GLV_NO_AUTOLOAD: do not use script autoloading
1959 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965get_lval(
1966 char_u *name,
1967 typval_T *rettv,
1968 lval_T *lp,
1969 int unlet,
1970 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 int flags, // GLV_ values
1972 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 char_u *expr_start, *expr_end;
1976 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001977 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001978 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001979 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001980 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001981 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001982 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Ernie Raelee865f32023-09-29 19:53:55 +02001984#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001985 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001986 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001987 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001988 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1989 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001990 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001991 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001992 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001993#endif
1994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001995 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001996 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001998 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002000 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02002002 lp->ll_name_end = find_name_end(name, NULL, NULL,
2003 FNE_INCL_BR | fne_flags);
2004 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 }
2006
Bram Moolenaara749a422022-02-12 19:52:25 +00002007 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00002008 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00002009 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
2010 {
2011 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
2012 return NULL;
2013 }
2014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002015 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002016 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 if (expr_start != NULL)
2019 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002020 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01002021 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 && *p != '[' && *p != '.')
2023 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002024 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
2026 }
2027
2028 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2029 if (lp->ll_exp_name == NULL)
2030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002031 // Report an invalid expression in braces, unless the
2032 // expression evaluation has been cancelled due to an
2033 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 if (!aborting() && !quiet)
2035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002036 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002037 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 return NULL;
2039 }
2040 }
2041 lp->ll_name = lp->ll_exp_name;
2042 }
2043 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 lp->ll_name = name;
2046
Bram Moolenaar4525a572022-02-13 11:57:33 +00002047 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002049 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002050 // However, "g:[key]" is indexing a dictionary.
2051 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002052 {
2053 --p;
2054 lp->ll_name_end = p;
2055 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002056 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002057 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002058 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02002059
Bram Moolenaar9510d222022-09-11 15:14:05 +01002060 if (is_scoped_variable(name))
2061 {
2062 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2063 return NULL;
2064 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002065 if (VIM_ISWHITE(*p))
2066 {
2067 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2068 return NULL;
2069 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002070 if (tp == p + 1 && !quiet)
2071 {
2072 semsg(_(e_white_space_required_after_str_str), ":", p);
2073 return NULL;
2074 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002075 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002076 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002077 semsg(_(e_using_type_not_in_script_context_str), p);
2078 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002079 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002080 if (vim9script && (flags & GLV_NO_DECL) &&
2081 !(flags & GLV_FOR_LOOP))
2082 {
2083 // Using a type and not in a "var" declaration.
2084 semsg(_(e_trailing_characters_str), p);
2085 return NULL;
2086 }
2087
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002088
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002089 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002090 lp->ll_type = parse_type(&tp,
2091 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2092 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002093 if (lp->ll_type == NULL && !quiet)
2094 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002095 lp->ll_name_end = tp;
2096 }
Ernie Raelee865f32023-09-29 19:53:55 +02002097 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 }
2099 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002100 if (lp->ll_name == NULL)
2101 return p;
2102
Bram Moolenaar62aec932022-01-29 21:45:34 +00002103 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002104 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002105 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002106 if (import != NULL)
2107 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002108 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002109 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002110 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002111 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002112 }
2113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114
Ernie Rael0f058d12023-10-14 11:25:04 +02002115 // Without [idx] or .key we are done.
2116 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002117 {
2118 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002119 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002121 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122
Ernie Rael0f058d12023-10-14 11:25:04 +02002123 if (vim9script && lval_root != NULL)
2124 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002125 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002126 {
2127 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002128 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002129 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002130 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002131 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002132 {
2133 cc = *p;
2134 *p = NUL;
2135 // When we would write to the variable pass &ht and prevent autoload.
2136 writing = !(flags & GLV_READ_ONLY);
2137 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002138 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002139 if (v == NULL && !quiet)
2140 semsg(_(e_undefined_variable_str), lp->ll_name);
2141 *p = cc;
2142 if (v == NULL)
2143 return NULL;
2144 lp->ll_tv = &v->di_tv;
2145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146
Bram Moolenaar4525a572022-02-13 11:57:33 +00002147 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002148 {
2149 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002150 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002151 return NULL;
2152 }
2153
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002154 // If the next character is a "." or a "[", then process the subitem.
2155 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2156 if (p == NULL)
2157 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002159 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2160 {
2161 where_T where = WHERE_INIT;
2162
2163 // In a vim9 script, do type check and make sure the variable is
2164 // writable.
2165 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2166 return NULL;
2167 }
2168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 return p;
2171}
2172
2173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178{
2179 vim_free(lp->ll_exp_name);
2180 vim_free(lp->ll_newkey);
2181}
2182
2183/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002184 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002186 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2187 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190set_var_lval(
2191 lval_T *lp,
2192 char_u *endp,
2193 typval_T *rettv,
2194 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002195 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2196 char_u *op,
2197 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198{
2199 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002200 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201
2202 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002204 cc = *endp;
2205 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002206 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002207 return;
2208
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002209 if (lp->ll_blob != NULL)
2210 {
2211 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002212
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002213 if (op != NULL && *op != '=')
2214 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002215 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002216 return;
2217 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002218 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002219 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002220
2221 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2222 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002223 if (lp->ll_empty2)
2224 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2225
Bram Moolenaar68452172021-04-12 21:21:02 +02002226 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2227 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002228 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002229 }
2230 else
2231 {
2232 val = (int)tv_get_number_chk(rettv, &error);
2233 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002234 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002235 }
2236 }
2237 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002239 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002241 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2242 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002243 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002244 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002245 *endp = cc;
2246 return;
2247 }
2248
Bram Moolenaarff697e62019-02-12 22:28:33 +01002249 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002250 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002251 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002252 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002253 {
Ernie Raele75fde62023-12-21 17:18:54 +01002254 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002255 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002256 clear_tv(&tv);
2257 return;
2258 }
2259
Bram Moolenaar79518e22017-02-17 16:31:35 +01002260 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002261 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2262 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002263 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002264 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002265 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002266 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002269 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002270 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002271 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2272 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002273 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002274 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002275 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002276 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002277 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002279 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002280 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002281 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002282 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 else if (lp->ll_range)
2284 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002285 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2286 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002287 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002288 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002289 return;
2290 }
2291
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002292 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2293 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 }
2295 else
2296 {
2297 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002298 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002300 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2301 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002302 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002303 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002304 return;
2305 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002306
2307 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002308 && check_typval_arg_type(lp->ll_valtype, rettv,
2309 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002310 return;
2311
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 if (lp->ll_newkey != NULL)
2313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 if (op != NULL && *op != '=')
2315 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002316 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 return;
2318 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002319 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2320 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002321 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002323 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002324 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (di == NULL)
2326 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002327 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2328 {
2329 vim_free(di);
2330 return;
2331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 else if (op != NULL && *op != '=')
2335 {
2336 tv_op(lp->ll_tv, rettv, op);
2337 return;
2338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 /*
2343 * Assign the value to the variable or list item.
2344 */
2345 if (copy)
2346 copy_tv(rettv, lp->ll_tv);
2347 else
2348 {
2349 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002350 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 }
2353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354}
2355
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002357 * Handle "blob1 += blob2".
2358 * Returns OK or FAIL.
2359 */
2360 static int
2361tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2362{
2363 if (*op != '+' || tv2->v_type != VAR_BLOB)
2364 return FAIL;
2365
2366 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002367 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002368 return OK;
2369
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002370 if (tv1->vval.v_blob == NULL)
2371 {
2372 tv1->vval.v_blob = tv2->vval.v_blob;
2373 ++tv1->vval.v_blob->bv_refcount;
2374 return OK;
2375 }
2376
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002377 blob_T *b1 = tv1->vval.v_blob;
2378 blob_T *b2 = tv2->vval.v_blob;
2379 int len = blob_len(b2);
2380
2381 for (int i = 0; i < len; i++)
2382 ga_append(&b1->bv_ga, blob_get(b2, i));
2383
2384 return OK;
2385}
2386
2387/*
2388 * Handle "list1 += list2".
2389 * Returns OK or FAIL.
2390 */
2391 static int
2392tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2393{
2394 if (*op != '+' || tv2->v_type != VAR_LIST)
2395 return FAIL;
2396
2397 // List += List
2398 if (tv2->vval.v_list == NULL)
2399 return OK;
2400
2401 if (tv1->vval.v_list == NULL)
2402 {
2403 tv1->vval.v_list = tv2->vval.v_list;
2404 ++tv1->vval.v_list->lv_refcount;
2405 }
2406 else
2407 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2408
2409 return OK;
2410}
2411
2412/*
2413 * Handle number operations:
2414 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2415 *
2416 * Returns OK or FAIL.
2417 */
2418 static int
2419tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2420{
2421 varnumber_T n;
2422 int failed = FALSE;
2423
2424 n = tv_get_number(tv1);
2425 if (tv2->v_type == VAR_FLOAT)
2426 {
2427 float_T f = n;
2428
2429 if (*op == '%')
2430 return FAIL;
2431 switch (*op)
2432 {
2433 case '+': f += tv2->vval.v_float; break;
2434 case '-': f -= tv2->vval.v_float; break;
2435 case '*': f *= tv2->vval.v_float; break;
2436 case '/': f /= tv2->vval.v_float; break;
2437 }
2438 clear_tv(tv1);
2439 tv1->v_type = VAR_FLOAT;
2440 tv1->vval.v_float = f;
2441 }
2442 else
2443 {
2444 switch (*op)
2445 {
2446 case '+': n += tv_get_number(tv2); break;
2447 case '-': n -= tv_get_number(tv2); break;
2448 case '*': n *= tv_get_number(tv2); break;
2449 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2450 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2451 }
2452 clear_tv(tv1);
2453 tv1->v_type = VAR_NUMBER;
2454 tv1->vval.v_number = n;
2455 }
2456
2457 return failed ? FAIL : OK;
2458}
2459
2460/*
2461 * Handle "str1 .= str2"
2462 * Returns OK or FAIL.
2463 */
2464 static int
2465tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2466{
2467 char_u numbuf[NUMBUFLEN];
2468 char_u *s;
2469
2470 if (tv2->v_type == VAR_FLOAT)
2471 return FAIL;
2472
2473 // str .= str
2474 s = tv_get_string(tv1);
2475 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2476 clear_tv(tv1);
2477 tv1->v_type = VAR_STRING;
2478 tv1->vval.v_string = s;
2479
2480 return OK;
2481}
2482
2483/*
2484 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2485 * and "tv1 .= tv2"
2486 * Returns OK or FAIL.
2487 */
2488 static int
2489tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2490{
2491 if (tv2->v_type == VAR_LIST)
2492 return FAIL;
2493
2494 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2495 return tv_op_number(tv1, tv2, op);
2496
2497 return tv_op_string(tv1, tv2, op);
2498}
2499
2500/*
2501 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2502 * Returns OK or FAIL.
2503 */
2504 static int
2505tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2506{
2507 float_T f;
2508
2509 if (*op == '%' || *op == '.'
2510 || (tv2->v_type != VAR_FLOAT
2511 && tv2->v_type != VAR_NUMBER
2512 && tv2->v_type != VAR_STRING))
2513 return FAIL;
2514
2515 if (tv2->v_type == VAR_FLOAT)
2516 f = tv2->vval.v_float;
2517 else
2518 f = tv_get_number(tv2);
2519 switch (*op)
2520 {
2521 case '+': tv1->vval.v_float += f; break;
2522 case '-': tv1->vval.v_float -= f; break;
2523 case '*': tv1->vval.v_float *= f; break;
2524 case '/': tv1->vval.v_float /= f; break;
2525 }
2526
2527 return OK;
2528}
2529
2530/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002531 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2532 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 * Returns OK or FAIL.
2534 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002535 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002538 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002539 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002540 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2541 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2542 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002544 semsg(_(e_wrong_variable_type_for_str_equal), op);
2545 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 }
2547
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002548 int retval = FAIL;
2549 switch (tv1->v_type)
2550 {
2551 case VAR_UNKNOWN:
2552 case VAR_ANY:
2553 case VAR_VOID:
2554 case VAR_DICT:
2555 case VAR_FUNC:
2556 case VAR_PARTIAL:
2557 case VAR_BOOL:
2558 case VAR_SPECIAL:
2559 case VAR_JOB:
2560 case VAR_CHANNEL:
2561 case VAR_INSTR:
2562 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002563 case VAR_CLASS:
2564 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002565 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002566
2567 case VAR_BLOB:
2568 retval = tv_op_blob(tv1, tv2, op);
2569 break;
2570
2571 case VAR_LIST:
2572 retval = tv_op_list(tv1, tv2, op);
2573 break;
2574
2575 case VAR_NUMBER:
2576 case VAR_STRING:
2577 retval = tv_op_nr_or_string(tv1, tv2, op);
2578 break;
2579
2580 case VAR_FLOAT:
2581 retval = tv_op_float(tv1, tv2, op);
2582 break;
2583 }
2584
2585 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002586 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002587
2588 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589}
2590
2591/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002592 * Evaluate the expression used in a ":for var in expr" command.
2593 * "arg" points to "var".
2594 * Set "*errp" to TRUE for an error, FALSE otherwise;
2595 * Return a pointer that holds the info. Null when there is an error.
2596 */
2597 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598eval_for_line(
2599 char_u *arg,
2600 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002601 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002602 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603{
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002605 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 typval_T tv;
2608 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002609 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002611 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002613 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614 if (fi == NULL)
2615 return NULL;
2616
Bram Moolenaar404557e2021-07-05 21:41:48 +02002617 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2618 &fi->fi_semicolon, FALSE);
2619 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002620 return fi;
2621
Bram Moolenaar404557e2021-07-05 21:41:48 +02002622 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002623 if (expr[0] != 'i' || expr[1] != 'n'
2624 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002625 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002626 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2627 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2628 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002629 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002630 return fi;
2631 }
2632
2633 if (skip)
2634 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002635 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2636 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002637 {
2638 *errp = FALSE;
2639 if (!skip)
2640 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002641 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002642 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002643 l = tv.vval.v_list;
2644 if (l == NULL)
2645 {
2646 // a null list is like an empty list: do nothing
2647 clear_tv(&tv);
2648 }
2649 else
2650 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002652 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002654 // No need to increment the refcount, it's already set for
2655 // the list being used in "tv".
2656 fi->fi_list = l;
2657 list_add_watch(l, &fi->fi_lw);
2658 fi->fi_lw.lw_item = l->lv_first;
2659 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002660 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002661 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002662 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002663 fi->fi_bi = 0;
2664 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002665 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002666 typval_T btv;
2667
2668 // Make a copy, so that the iteration still works when the
2669 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002671 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002672 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002673 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002674 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002675 else if (tv.v_type == VAR_STRING)
2676 {
2677 fi->fi_byte_idx = 0;
2678 fi->fi_string = tv.vval.v_string;
2679 tv.vval.v_string = NULL;
2680 if (fi->fi_string == NULL)
2681 fi->fi_string = vim_strsave((char_u *)"");
2682 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683 else
2684 {
Sean Dewar80d73952021-08-04 19:25:54 +02002685 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002686 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687 }
2688 }
2689 }
2690 if (skip)
2691 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002692 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002693
2694 return fi;
2695}
2696
2697/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002698 * Used when looping over a :for line, skip the "in expr" part.
2699 */
2700 void
2701skip_for_lines(void *fi_void, evalarg_T *evalarg)
2702{
2703 forinfo_T *fi = (forinfo_T *)fi_void;
2704 int i;
2705
2706 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002707 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002708}
2709
2710/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711 * Use the first item in a ":for" list. Advance to the next.
2712 * Assign the values to the variable (list). "arg" points to the first one.
2713 * Return TRUE when a valid item was found, FALSE when at end of list or
2714 * something wrong.
2715 */
2716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002718{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002719 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002720 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002721 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002722 ? (ASSIGN_FINAL
2723 // first round: error if variable exists
2724 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002725 | ASSIGN_NO_MEMBER_TYPE
2726 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002727 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002728 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002729 int skip_assign = in_vim9script() && arg[0] == '_'
2730 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002732 if (fi->fi_blob != NULL)
2733 {
2734 typval_T tv;
2735
2736 if (fi->fi_bi >= blob_len(fi->fi_blob))
2737 return FALSE;
2738 tv.v_type = VAR_NUMBER;
2739 tv.v_lock = VAR_FIXED;
2740 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2741 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002742 if (skip_assign)
2743 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002744 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002745 fi->fi_varcount, flag, NULL) == OK;
2746 }
2747
2748 if (fi->fi_string != NULL)
2749 {
2750 typval_T tv;
2751 int len;
2752
2753 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2754 if (len == 0)
2755 return FALSE;
2756 tv.v_type = VAR_STRING;
2757 tv.v_lock = VAR_FIXED;
2758 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2759 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002760 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002761 if (skip_assign)
2762 result = TRUE;
2763 else
2764 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002765 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002766 vim_free(tv.vval.v_string);
2767 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002768 }
2769
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770 item = fi->fi_lw.lw_item;
2771 if (item == NULL)
2772 result = FALSE;
2773 else
2774 {
2775 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002776 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002777 if (skip_assign)
2778 result = TRUE;
2779 else
2780 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002781 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002782 }
2783 return result;
2784}
2785
2786/*
2787 * Free the structure used to store info used by ":for".
2788 */
2789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002790free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002791{
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002794 if (fi == NULL)
2795 return;
2796 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002797 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002799 list_unref(fi->fi_list);
2800 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002801 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002802 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002803 else
2804 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 vim_free(fi);
2806}
2807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002809set_context_for_expression(
2810 expand_T *xp,
2811 char_u *arg,
2812 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002814 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002818 if (cmdidx == CMD_let || cmdidx == CMD_var
2819 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 {
2821 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002822 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002824 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002825 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 {
2827 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002828 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002829 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 break;
2831 }
2832 return;
2833 }
2834 }
2835 else
2836 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2837 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 while ((xp->xp_pattern = vim_strpbrk(arg,
2839 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2840 {
2841 c = *xp->xp_pattern;
2842 if (c == '&')
2843 {
2844 c = xp->xp_pattern[1];
2845 if (c == '&')
2846 {
2847 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002848 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
2850 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002853 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2854 xp->xp_pattern += 2;
2855
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858 else if (c == '$')
2859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002860 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 xp->xp_context = EXPAND_ENV_VARS;
2862 }
2863 else if (c == '=')
2864 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002865 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 xp->xp_context = EXPAND_EXPRESSION;
2867 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002868 else if (c == '#'
2869 && xp->xp_context == EXPAND_EXPRESSION)
2870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002872 break;
2873 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002874 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 && xp->xp_context == EXPAND_FUNCTIONS
2876 && vim_strchr(xp->xp_pattern, '(') == NULL)
2877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002878 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 break;
2880 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002881 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002883 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2886 if (c == '\\' && xp->xp_pattern[1] != NUL)
2887 ++xp->xp_pattern;
2888 xp->xp_context = EXPAND_NOTHING;
2889 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002892 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2894 /* skip */ ;
2895 xp->xp_context = EXPAND_NOTHING;
2896 }
2897 else if (c == '|')
2898 {
2899 if (xp->xp_pattern[1] == '|')
2900 {
2901 ++xp->xp_pattern;
2902 xp->xp_context = EXPAND_EXPRESSION;
2903 }
2904 else
2905 xp->xp_context = EXPAND_COMMANDS;
2906 }
2907 else
2908 xp->xp_context = EXPAND_EXPRESSION;
2909 }
2910 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002911 // Doesn't look like something valid, expand as an expression
2912 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 arg = xp->xp_pattern;
2915 if (*arg != NUL)
2916 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2917 /* skip */ ;
2918 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002919
2920 // ":exe one two" completes "two"
2921 if ((cmdidx == CMD_execute
2922 || cmdidx == CMD_echo
2923 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002924 || cmdidx == CMD_echomsg
2925 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002926 && xp->xp_context == EXPAND_EXPRESSION)
2927 {
2928 for (;;)
2929 {
2930 char_u *n = skiptowhite(arg);
2931
2932 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2933 break;
2934 arg = skipwhite(n);
2935 }
2936 }
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 xp->xp_pattern = arg;
2939}
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002942 * Return TRUE if "pat" matches "text".
2943 * Does not use 'cpo' and always uses 'magic'.
2944 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002945 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002946pattern_match(char_u *pat, char_u *text, int ic)
2947{
2948 int matches = FALSE;
2949 char_u *save_cpo;
2950 regmatch_T regmatch;
2951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002952 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002953 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002954 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002955 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2956 if (regmatch.regprog != NULL)
2957 {
2958 regmatch.rm_ic = ic;
2959 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2960 vim_regfree(regmatch.regprog);
2961 }
2962 p_cpo = save_cpo;
2963 return matches;
2964}
2965
2966/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002967 * Handle a name followed by "(". Both for just "name(arg)" and for
2968 * "expr->name(arg)".
2969 * Returns OK or FAIL.
2970 */
2971 static int
2972eval_func(
2973 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002974 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002975 char_u *name,
2976 int name_len,
2977 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002978 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002979 typval_T *basetv) // "expr" for "expr->name(arg)"
2980{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002981 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002982 char_u *s = name;
2983 int len = name_len;
2984 partial_T *partial;
2985 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002986 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002987 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002988
2989 if (!evaluate)
2990 check_vars(s, len);
2991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 // If "s" is the name of a variable of type VAR_FUNC
2993 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002994 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002995 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 // Need to make a copy, in case evaluating the arguments makes
2998 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002999 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01003000 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003001 ret = FAIL;
3002 else
3003 {
3004 funcexe_T funcexe;
3005
3006 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02003007 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003008 funcexe.fe_firstline = curwin->w_cursor.lnum;
3009 funcexe.fe_lastline = curwin->w_cursor.lnum;
3010 funcexe.fe_evaluate = evaluate;
3011 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003012 if (partial != NULL)
3013 {
3014 funcexe.fe_object = partial->pt_obj;
3015 if (funcexe.fe_object != NULL)
3016 ++funcexe.fe_object->obj_refcount;
3017 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003018 funcexe.fe_basetv = basetv;
3019 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003020 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003021 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003022 }
3023 vim_free(s);
3024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003025 // If evaluate is FALSE rettv->v_type was not set in
3026 // get_func_tv, but it's needed in handle_subscript() to parse
3027 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003028 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3029 {
3030 rettv->vval.v_string = NULL;
3031 rettv->v_type = VAR_FUNC;
3032 }
3033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003034 // Stop the expression evaluation when immediately
3035 // aborting on error, or when an interrupt occurred or
3036 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003037 if (evaluate && aborting())
3038 {
3039 if (ret == OK)
3040 clear_tv(rettv);
3041 ret = FAIL;
3042 }
3043 return ret;
3044}
3045
3046/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003047 * After a NL, skip over empty lines and comment-only lines.
3048 */
3049 static char_u *
3050newline_skip_comments(char_u *arg)
3051{
3052 char_u *p = arg + 1;
3053
3054 for (;;)
3055 {
3056 p = skipwhite(p);
3057
3058 if (*p == NUL)
3059 break;
3060 if (vim9_comment_start(p))
3061 {
3062 char_u *nl = vim_strchr(p, NL);
3063
3064 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003065 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003066 p = nl;
3067 }
3068 if (*p != NL)
3069 break;
3070 ++p; // skip another NL
3071 }
3072 return p;
3073}
3074
3075/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003076 * Get the next line source line without advancing. But do skip over comment
3077 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003078 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003079 */
3080 static char_u *
3081getline_peek_skip_comments(evalarg_T *evalarg)
3082{
3083 for (;;)
3084 {
3085 char_u *next = getline_peek(evalarg->eval_getline,
3086 evalarg->eval_cookie);
3087 char_u *p;
3088
3089 if (next == NULL)
3090 break;
3091 p = skipwhite(next);
3092 if (*p != NUL && !vim9_comment_start(p))
3093 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003094 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003095 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003096 }
3097 return NULL;
3098}
3099
3100/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003101 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3102 * comment) and there is a next line, return the next line (skipping blanks)
3103 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003104 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3105 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003106 * "arg" must point somewhere inside a line, not at the start.
3107 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003108 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003109eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3110{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003111 char_u *p = skipwhite(arg);
3112
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003114 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003116 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3117 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003118 && (*p == NUL || *p == NL
3119 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003121 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003122
Bram Moolenaare442d592022-05-05 12:20:28 +01003123 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003124 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003125 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003126 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003127 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003128 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129
Bram Moolenaar9d489562020-07-30 20:08:50 +02003130 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003132 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003133 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003134 }
3135 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003136 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003137}
3138
3139/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003140 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003141 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003142 *
3143 * If "arg" is not NULL, then the caller should assign the return value to
3144 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003145 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003146 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003147eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003148{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003149 garray_T *gap = &evalarg->eval_ga;
3150 char_u *line;
3151
Bram Moolenaar39be4982022-05-06 21:51:50 +01003152 if (arg != NULL)
3153 {
3154 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003155 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003156 // Truncate before a trailing comment, so that concatenating the lines
3157 // won't turn the rest into a comment.
3158 if (*skipwhite(arg) == '#')
3159 *arg = NUL;
3160 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003161
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003162 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003163 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3164 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003165 else
3166 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003167 if (line == NULL)
3168 return NULL;
3169
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003170 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003171 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3172 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003173 char_u *p = skipwhite(line);
3174
3175 // Going to concatenate the lines after parsing. For an empty or
3176 // comment line use an empty string.
3177 if (*p == NUL || vim9_comment_start(p))
3178 {
3179 vim_free(line);
3180 line = vim_strsave((char_u *)"");
3181 }
3182
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003183 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3184 ++gap->ga_len;
3185 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003186 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003187 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003188 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003189 evalarg->eval_tofree = line;
3190 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003191
3192 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003193 // line. The caller assigns the return value to "arg".
3194 // If "arg" is NULL, then the return value is discarded. In that case,
3195 // "arg" still points to the previous line. So don't reset
3196 // "eval_using_cmdline".
3197 if (arg != NULL)
3198 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003199 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003200}
3201
Bram Moolenaare6b53242020-07-01 17:28:33 +02003202/*
3203 * Call eval_next_non_blank() and get the next line if needed.
3204 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003205 char_u *
3206skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3207{
3208 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003209 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003210
Bram Moolenaar962d7212020-07-04 14:15:00 +02003211 if (evalarg == NULL)
3212 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003213 eval_next_non_blank(p, evalarg, &getnext);
3214 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003215 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003216 return p;
3217}
3218
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003219/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003220 * The "eval" functions have an "evalarg" argument: When NULL or
3221 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3222 * parsed but not executed. The functions may return OK, but the rettv will be
3223 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 */
3225
3226/*
3227 * Handle zero level expression.
3228 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003230 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Return OK or FAIL.
3233 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval0(
3236 char_u *arg,
3237 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003238 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003239 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003241 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3242}
3243
3244/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003245 * If "arg" is a simple function call without arguments then call it and return
3246 * the result. Otherwise return NOTDONE.
3247 */
3248 int
3249may_call_simple_func(
3250 char_u *arg,
3251 typval_T *rettv)
3252{
3253 char_u *parens = (char_u *)strstr((char *)arg, "()");
3254 int r = NOTDONE;
3255
3256 // If the expression is "FuncName()" then we can skip a lot of overhead.
3257 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3258 {
3259 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3260
3261 if (to_name_end(p, TRUE) == parens)
zeertzjqc1ed7882024-08-01 22:46:54 +02003262 r = call_simple_func(arg, (size_t)(parens - arg), rettv);
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003263 }
3264 return r;
3265}
3266
3267/*
3268 * Handle zero level expression with optimization for a simple function call.
3269 * Same arguments and return value as eval0().
3270 */
3271 int
3272eval0_simple_funccal(
3273 char_u *arg,
3274 typval_T *rettv,
3275 exarg_T *eap,
3276 evalarg_T *evalarg)
3277{
3278 int r = may_call_simple_func(arg, rettv);
3279
3280 if (r == NOTDONE)
3281 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3282 return r;
3283}
3284
3285/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003286 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3287 * expression and don't check what comes after the expression.
3288 */
3289 int
3290eval0_retarg(
3291 char_u *arg,
3292 typval_T *rettv,
3293 exarg_T *eap,
3294 evalarg_T *evalarg,
3295 char_u **retarg)
3296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int ret;
3298 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003299 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003300 int did_emsg_before = did_emsg;
3301 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003302 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003303 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003306 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003307
Bram Moolenaar79481362022-06-27 20:15:10 +01003308 if (ret != FAIL)
3309 {
3310 expr_end = p;
3311 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003312
Bram Moolenaar79481362022-06-27 20:15:10 +01003313 // In Vim9 script a command block is not split at NL characters for
3314 // commands using an expression argument. Skip over a '#' comment to
3315 // check for a following NL. Require white space before the '#'.
3316 if (in_vim9script() && p > expr_end && retarg == NULL)
3317 while (*p == '#')
3318 {
3319 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003320
Bram Moolenaar79481362022-06-27 20:15:10 +01003321 if (nl == NULL)
3322 break;
3323 p = skipwhite(nl + 1);
3324 if (eap != NULL && *p != NUL)
3325 eap->nextcmd = p;
3326 check_for_end = FALSE;
3327 }
3328
3329 if (check_for_end)
3330 end_error = !ends_excmd2(arg, p);
3331 }
3332
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003333 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 /*
3338 * Report the invalid expression unless the expression evaluation has
3339 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003340 * exception, or we already gave a more specific error.
3341 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003343 if (!aborting()
3344 && did_emsg == did_emsg_before
3345 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003346 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003347 {
3348 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003349 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003350 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003351 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003352 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003353
zeertzjqf2588b62023-05-05 17:22:35 +01003354 if (eap != NULL && p != NULL)
3355 {
3356 // Some of the expression may not have been consumed.
3357 // Only execute a next command if it cannot be a "||" operator.
3358 // The next command may be "catch".
3359 char_u *nextcmd = check_nextcmd(p);
3360 if (nextcmd != NULL && *nextcmd != '|')
3361 eap->nextcmd = nextcmd;
3362 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003365
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003366 if (retarg != NULL)
3367 *retarg = p;
3368 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003369 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return ret;
3372}
3373
3374/*
3375 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003376 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003377 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 *
3379 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003380 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003382 * Note: "rettv.v_lock" is not set.
3383 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 * Return OK or FAIL.
3385 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003386 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003387eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003389 char_u *p;
3390 int getnext;
3391
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003392 CLEAR_POINTER(rettv);
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 /*
3395 * Get the first variable.
3396 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003397 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 return FAIL;
3399
Bram Moolenaar793648f2020-06-26 21:28:25 +02003400 p = eval_next_non_blank(*arg, evalarg, &getnext);
3401 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003403 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003404 int result;
3405 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003406 evalarg_T *evalarg_used = evalarg;
3407 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003408 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003409 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003410 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003411
3412 if (evalarg == NULL)
3413 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003414 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003415 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003416 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003417 orig_flags = evalarg_used->eval_flags;
3418 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003419
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003420 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003421 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003422 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003423 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003424 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003425 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003426 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003427 clear_tv(rettv);
3428 return FAIL;
3429 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003430 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003431 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003434 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 int error = FALSE;
3437
Bram Moolenaar13106602020-10-04 16:06:05 +02003438 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003439 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003440 else if (vim9script)
3441 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003442 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003444 if (error || !op_falsy || !result)
3445 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 if (error)
3447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449
3450 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003451 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003453 if (op_falsy)
3454 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003455 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003456 {
mityu4ac198c2021-05-28 17:52:40 +02003457 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003458 clear_tv(rettv);
3459 return FAIL;
3460 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003461 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003462 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003463 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003464 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003465 {
3466 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003468 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003469 if (!op_falsy || !result)
3470 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003472 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003474 /*
3475 * Check for the ":".
3476 */
3477 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3478 if (*p != ':')
3479 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003480 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003481 if (evaluate && result)
3482 clear_tv(rettv);
3483 evalarg_used->eval_flags = orig_flags;
3484 return FAIL;
3485 }
3486 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003487 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003488 else
3489 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003490 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003491 {
3492 error_white_both(p, 1);
3493 clear_tv(rettv);
3494 evalarg_used->eval_flags = orig_flags;
3495 return FAIL;
3496 }
3497 *arg = p;
3498 }
3499
3500 /*
3501 * Get the third variable. Recursive!
3502 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003503 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003504 {
mityu4ac198c2021-05-28 17:52:40 +02003505 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003506 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003507 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003508 return FAIL;
3509 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003510 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3511 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003512 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003513 if (eval1(arg, &var2, evalarg_used) == FAIL)
3514 {
3515 if (evaluate && result)
3516 clear_tv(rettv);
3517 evalarg_used->eval_flags = orig_flags;
3518 return FAIL;
3519 }
3520 if (evaluate && !result)
3521 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003523
3524 if (evalarg == NULL)
3525 clear_evalarg(&local_evalarg, NULL);
3526 else
3527 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
3530 return OK;
3531}
3532
3533/*
3534 * Handle first level expression:
3535 * expr2 || expr2 || expr2 logical OR
3536 *
3537 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003538 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 *
3540 * Return OK or FAIL.
3541 */
3542 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003543eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003545 char_u *p;
3546 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003549 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003551 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return FAIL;
3553
3554 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003555 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003557 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003558 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003560 evalarg_T *evalarg_used = evalarg;
3561 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003562 int evaluate;
3563 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003564 long result = FALSE;
3565 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003566 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003567 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003568
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003569 if (evalarg == NULL)
3570 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003571 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003572 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003573 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003574 orig_flags = evalarg_used->eval_flags;
3575 evaluate = orig_flags & EVAL_EVALUATE;
3576 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003577 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003578 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003579 result = tv_get_bool_chk(rettv, &error);
3580 else if (tv_get_number_chk(rettv, &error) != 0)
3581 result = TRUE;
3582 clear_tv(rettv);
3583 if (error)
3584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586
3587 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003588 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003590 while (p[0] == '|' && p[1] == '|')
3591 {
3592 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003593 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003594 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003595 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003596 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003597 {
3598 error_white_both(p, 2);
3599 clear_tv(rettv);
3600 return FAIL;
3601 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003602 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003604
3605 /*
3606 * Get the second variable.
3607 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003608 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003609 {
mityu4ac198c2021-05-28 17:52:40 +02003610 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003611 clear_tv(rettv);
3612 return FAIL;
3613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003614 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3615 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003616 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003617 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003618 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003619
3620 /*
3621 * Compute the result.
3622 */
3623 if (evaluate && !result)
3624 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003625 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003626 result = tv_get_bool_chk(&var2, &error);
3627 else if (tv_get_number_chk(&var2, &error) != 0)
3628 result = TRUE;
3629 clear_tv(&var2);
3630 if (error)
3631 return FAIL;
3632 }
3633 if (evaluate)
3634 {
3635 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003636 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003637 rettv->v_type = VAR_BOOL;
3638 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003639 }
3640 else
3641 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003642 rettv->v_type = VAR_NUMBER;
3643 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003644 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003645 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003646
3647 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003649
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003650 if (evalarg == NULL)
3651 clear_evalarg(&local_evalarg, NULL);
3652 else
3653 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655
3656 return OK;
3657}
3658
3659/*
3660 * Handle second level expression:
3661 * expr3 && expr3 && expr3 logical AND
3662 *
3663 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003664 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *
3666 * Return OK or FAIL.
3667 */
3668 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003669eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003671 char_u *p;
3672 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003675 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003677 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return FAIL;
3679
3680 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003681 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003683 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003684 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003686 evalarg_T *evalarg_used = evalarg;
3687 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003688 int orig_flags;
3689 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003690 long result = TRUE;
3691 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003692 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003693 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003694
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003695 if (evalarg == NULL)
3696 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003697 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003698 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003699 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003700 orig_flags = evalarg_used->eval_flags;
3701 evaluate = orig_flags & EVAL_EVALUATE;
3702 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003703 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003704 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003705 result = tv_get_bool_chk(rettv, &error);
3706 else if (tv_get_number_chk(rettv, &error) == 0)
3707 result = FALSE;
3708 clear_tv(rettv);
3709 if (error)
3710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712
3713 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003714 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003716 while (p[0] == '&' && p[1] == '&')
3717 {
3718 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003719 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003720 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003721 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003722 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003723 {
3724 error_white_both(p, 2);
3725 clear_tv(rettv);
3726 return FAIL;
3727 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003728 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003729 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003730
3731 /*
3732 * Get the second variable.
3733 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003734 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003735 {
mityu4ac198c2021-05-28 17:52:40 +02003736 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003737 clear_tv(rettv);
3738 return FAIL;
3739 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003740 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3741 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003742 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003743 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003744 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003745 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003746
3747 /*
3748 * Compute the result.
3749 */
3750 if (evaluate && result)
3751 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003752 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003753 result = tv_get_bool_chk(&var2, &error);
3754 else if (tv_get_number_chk(&var2, &error) == 0)
3755 result = FALSE;
3756 clear_tv(&var2);
3757 if (error)
3758 return FAIL;
3759 }
3760 if (evaluate)
3761 {
3762 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003763 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003764 rettv->v_type = VAR_BOOL;
3765 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003766 }
3767 else
3768 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003769 rettv->v_type = VAR_NUMBER;
3770 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003771 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003772 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003773
3774 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003776
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003777 if (evalarg == NULL)
3778 clear_evalarg(&local_evalarg, NULL);
3779 else
3780 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782
3783 return OK;
3784}
3785
3786/*
3787 * Handle third level expression:
3788 * var1 == var2
3789 * var1 =~ var2
3790 * var1 != var2
3791 * var1 !~ var2
3792 * var1 > var2
3793 * var1 >= var2
3794 * var1 < var2
3795 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003796 * var1 is var2
3797 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 *
3799 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003800 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *
3802 * Return OK or FAIL.
3803 */
3804 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003805eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003808 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003809 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003811 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003814 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003816 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FAIL;
3818
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003819 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003820
Bram Moolenaar696ba232020-07-29 21:20:41 +02003821 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003824 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003826 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003828 typval_T var2;
3829 int ic;
3830 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003831 int evaluate = evalarg == NULL
3832 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003833 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003834
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003835 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003836 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003837 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003838 p = *arg;
3839 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003840 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3841 {
mityu4ac198c2021-05-28 17:52:40 +02003842 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003843 clear_tv(rettv);
3844 return FAIL;
3845 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003846
Bram Moolenaar696ba232020-07-29 21:20:41 +02003847 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3848 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003849 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003850 clear_tv(rettv);
3851 return FAIL;
3852 }
3853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (p[len] == '?')
3856 {
3857 ic = TRUE;
3858 ++len;
3859 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003860 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 else if (p[len] == '#')
3862 {
3863 ic = FALSE;
3864 ++len;
3865 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003866 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003868 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870 /*
3871 * Get the second variable.
3872 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003873 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3874 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003875 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003876 clear_tv(rettv);
3877 return FAIL;
3878 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003879 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003880 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 return FAIL;
3884 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003885 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003886 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003887 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003888
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003889 // use the line of the comparison for messages
3890 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003891 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003892 {
3893 ret = FAIL;
3894 clear_tv(rettv);
3895 }
3896 else
3897 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003898 clear_tv(&var2);
3899 return ret;
3900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
3903 return OK;
3904}
3905
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003906/*
3907 * Make a copy of blob "tv1" and append blob "tv2".
3908 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 void
3910eval_addblob(typval_T *tv1, typval_T *tv2)
3911{
3912 blob_T *b1 = tv1->vval.v_blob;
3913 blob_T *b2 = tv2->vval.v_blob;
3914 blob_T *b = blob_alloc();
3915 int i;
3916
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003917 if (b == NULL)
3918 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003920 for (i = 0; i < blob_len(b1); i++)
3921 ga_append(&b->bv_ga, blob_get(b1, i));
3922 for (i = 0; i < blob_len(b2); i++)
3923 ga_append(&b->bv_ga, blob_get(b2, i));
3924
3925 clear_tv(tv1);
3926 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927}
3928
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003929/*
3930 * Make a copy of list "tv1" and append list "tv2".
3931 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 int
3933eval_addlist(typval_T *tv1, typval_T *tv2)
3934{
3935 typval_T var3;
3936
3937 // concatenate Lists
3938 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3939 {
3940 clear_tv(tv1);
3941 clear_tv(tv2);
3942 return FAIL;
3943 }
3944 clear_tv(tv1);
3945 *tv1 = var3;
3946 return OK;
3947}
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02003950 * Left or right shift the number "tv1" by the number "tv2" and store the
3951 * result in "tv1".
3952 *
3953 * Return OK or FAIL.
3954 */
3955 static int
3956eval_shift_number(typval_T *tv1, typval_T *tv2, int shift_type)
3957{
3958 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3959 {
3960 // right operand should be a positive number
3961 if (tv2->v_type != VAR_NUMBER)
3962 emsg(_(e_bitshift_ops_must_be_number));
3963 else
3964 emsg(_(e_bitshift_ops_must_be_positive));
3965 clear_tv(tv1);
3966 clear_tv(tv2);
3967 return FAIL;
3968 }
3969
3970 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3971 // shifting more bits than we have always results in zero
3972 tv1->vval.v_number = 0;
3973 else if (shift_type == EXPR_LSHIFT)
3974 tv1->vval.v_number =
3975 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
3976 else
3977 tv1->vval.v_number =
3978 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
3979
3980 return OK;
3981}
3982
3983/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003984 * Handle the bitwise left/right shift operator expression:
3985 * var1 << var2
3986 * var1 >> var2
3987 *
3988 * "arg" must point to the first non-white of the expression.
3989 * "arg" is advanced to just after the recognized expression.
3990 *
3991 * Return OK or FAIL.
3992 */
3993 static int
3994eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3995{
3996 /*
3997 * Get the first expression.
3998 */
3999 if (eval6(arg, rettv, evalarg) == FAIL)
4000 return FAIL;
4001
4002 /*
4003 * Repeat computing, until no '<<' or '>>' is following.
4004 */
4005 for (;;)
4006 {
4007 char_u *p;
4008 int getnext;
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004009 exprtype_T exprtype;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004010 int evaluate;
4011 typval_T var2;
4012 int vim9script;
4013
4014 p = eval_next_non_blank(*arg, evalarg, &getnext);
4015 if (p[0] == '<' && p[1] == '<')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004016 exprtype = EXPR_LSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004017 else if (p[0] == '>' && p[1] == '>')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004018 exprtype = EXPR_RSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004019 else
4020 return OK;
4021
4022 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004023 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
4024 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004025 {
4026 // left operand should be a number
4027 emsg(_(e_bitshift_ops_must_be_number));
4028 clear_tv(rettv);
4029 return FAIL;
4030 }
4031
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004032 vim9script = in_vim9script();
4033 if (getnext)
4034 {
4035 *arg = eval_next_line(*arg, evalarg);
4036 p = *arg;
4037 }
4038 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4039 {
4040 error_white_both(*arg, 2);
4041 clear_tv(rettv);
4042 return FAIL;
4043 }
4044
4045 /*
4046 * Get the second variable.
4047 */
4048 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
4049 {
4050 error_white_both(p, 2);
4051 clear_tv(rettv);
4052 return FAIL;
4053 }
4054 *arg = skipwhite_and_linebreak(p + 2, evalarg);
4055 if (eval6(arg, &var2, evalarg) == FAIL)
4056 {
4057 clear_tv(rettv);
4058 return FAIL;
4059 }
4060
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004061 if (evaluate)
4062 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004063 if (eval_shift_number(rettv, &var2, exprtype) == FAIL)
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004064 return FAIL;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004065 }
4066
4067 clear_tv(&var2);
4068 }
4069
4070 return OK;
4071}
4072
4073/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004074 * Concatenate strings "tv1" and "tv2" and store the result in "tv1".
4075 */
4076 static int
4077eval_concat_str(typval_T *tv1, typval_T *tv2)
4078{
4079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4080 char_u *s1 = tv_get_string_buf(tv1, buf1);
4081 char_u *s2 = NULL;
4082 char_u *p;
4083 int vim9script = in_vim9script();
4084
4085 if (vim9script && (tv2->v_type == VAR_VOID
4086 || tv2->v_type == VAR_CHANNEL
4087 || tv2->v_type == VAR_JOB))
4088 semsg(_(e_using_invalid_value_as_string_str),
4089 vartype_name(tv2->v_type));
4090 else if (vim9script && tv2->v_type == VAR_FLOAT)
4091 {
4092 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4093 tv2->vval.v_float);
4094 s2 = buf2;
4095 }
4096 else
4097 s2 = tv_get_string_buf_chk(tv2, buf2);
4098 if (s2 == NULL) // type error ?
4099 {
4100 clear_tv(tv1);
4101 clear_tv(tv2);
4102 return FAIL;
4103 }
4104
4105 p = concat_str(s1, s2);
4106 clear_tv(tv1);
4107 tv1->v_type = VAR_STRING;
4108 tv1->vval.v_string = p;
4109
4110 return OK;
4111}
4112
4113/*
4114 * Add or subtract numbers "tv1" and "tv2" and store the result in "tv1".
4115 * The numbers can be whole numbers or floats.
4116 */
4117 static int
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004118eval_addsub_number(typval_T *tv1, typval_T *tv2, int op)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004119{
4120 int error = FALSE;
4121 varnumber_T n1, n2;
4122 float_T f1 = 0, f2 = 0;
4123
4124 if (tv1->v_type == VAR_FLOAT)
4125 {
4126 f1 = tv1->vval.v_float;
4127 n1 = 0;
4128 }
4129 else
4130 {
4131 n1 = tv_get_number_chk(tv1, &error);
4132 if (error)
4133 {
4134 // This can only happen for "list + non-list" or
4135 // "blob + non-blob". For "non-list + ..." or
4136 // "something - ...", we returned before evaluating the
4137 // 2nd operand.
4138 clear_tv(tv1);
4139 clear_tv(tv2);
4140 return FAIL;
4141 }
4142 if (tv2->v_type == VAR_FLOAT)
4143 f1 = n1;
4144 }
4145 if (tv2->v_type == VAR_FLOAT)
4146 {
4147 f2 = tv2->vval.v_float;
4148 n2 = 0;
4149 }
4150 else
4151 {
4152 n2 = tv_get_number_chk(tv2, &error);
4153 if (error)
4154 {
4155 clear_tv(tv1);
4156 clear_tv(tv2);
4157 return FAIL;
4158 }
4159 if (tv1->v_type == VAR_FLOAT)
4160 f2 = n2;
4161 }
4162 clear_tv(tv1);
4163
4164 // If there is a float on either side the result is a float.
4165 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4166 {
4167 if (op == '+')
4168 f1 = f1 + f2;
4169 else
4170 f1 = f1 - f2;
4171 tv1->v_type = VAR_FLOAT;
4172 tv1->vval.v_float = f1;
4173 }
4174 else
4175 {
4176 if (op == '+')
4177 n1 = n1 + n2;
4178 else
4179 n1 = n1 - n2;
4180 tv1->v_type = VAR_NUMBER;
4181 tv1->vval.v_number = n1;
4182 }
4183
4184 return OK;
4185}
4186
4187/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004188 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004189 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004191 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004192 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 *
4194 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004195 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 *
4197 * Return OK or FAIL.
4198 */
4199 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004200eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004203 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004205 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207
4208 /*
4209 * Repeat computing, until no '+', '-' or '.' is following.
4210 */
4211 for (;;)
4212 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004213 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004214 int getnext;
4215 char_u *p;
4216 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004217 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004218 int concat;
4219 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004220 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004221
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004222 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004223 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004224 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004225 p = eval_next_non_blank(*arg, evalarg, &getnext);
4226 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004227 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004228 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4229 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004231 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4232 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004233
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004234 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004235 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004236 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004237 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004238 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004239 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004240 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004241 {
mityu4ac198c2021-05-28 17:52:40 +02004242 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004243 clear_tv(rettv);
4244 return FAIL;
4245 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004246 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004247 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004248 if ((op != '+' || (rettv->v_type != VAR_LIST
4249 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004250 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004251 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004253 int error = FALSE;
4254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004255 // For "list + ...", an illegal use of the first operand as
4256 // a number cannot be determined before evaluating the 2nd
4257 // operand: if this is also a list, all is ok.
4258 // For "something . ...", "something - ..." or "non-list + ...",
4259 // we know that the first operand needs to be a string or number
4260 // without evaluating the 2nd operand. So check before to avoid
4261 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004262 if (op != '.')
4263 tv_get_number_chk(rettv, &error);
4264 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 {
4266 clear_tv(rettv);
4267 return FAIL;
4268 }
4269 }
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 /*
4272 * Get the second variable.
4273 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004274 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004275 {
mityu89dcb4d2021-05-28 13:50:17 +02004276 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004277 clear_tv(rettv);
4278 return FAIL;
4279 }
4280 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004281 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285 }
4286
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004287 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 /*
4290 * Compute the result.
4291 */
4292 if (op == '.')
4293 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004294 if (eval_concat_str(rettv, &var2) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004297 else if (op == '+' && rettv->v_type == VAR_BLOB
4298 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004300 else if (op == '+' && rettv->v_type == VAR_LIST
4301 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004304 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 else
4307 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004308 if (eval_addsub_number(rettv, &var2, op) == FAIL)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004309 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 }
4314 return OK;
4315}
4316
4317/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004318 * Multiply or divide or compute the modulo of numbers "tv1" and "tv2" and
4319 * store the result in "tv1". The numbers can be whole numbers or floats.
4320 */
4321 static int
4322eval_multdiv_number(typval_T *tv1, typval_T *tv2, int op)
4323{
4324 varnumber_T n1, n2;
4325 float_T f1, f2;
4326 int error;
4327 int use_float = FALSE;
4328
4329 f1 = 0;
4330 f2 = 0;
4331 error = FALSE;
4332 if (tv1->v_type == VAR_FLOAT)
4333 {
4334 f1 = tv1->vval.v_float;
4335 use_float = TRUE;
4336 n1 = 0;
4337 }
4338 else
4339 n1 = tv_get_number_chk(tv1, &error);
4340 clear_tv(tv1);
4341 if (error)
4342 {
4343 clear_tv(tv2);
4344 return FAIL;
4345 }
4346
4347 if (tv2->v_type == VAR_FLOAT)
4348 {
4349 if (!use_float)
4350 {
4351 f1 = n1;
4352 use_float = TRUE;
4353 }
4354 f2 = tv2->vval.v_float;
4355 n2 = 0;
4356 }
4357 else
4358 {
4359 n2 = tv_get_number_chk(tv2, &error);
4360 clear_tv(tv2);
4361 if (error)
4362 return FAIL;
4363 if (use_float)
4364 f2 = n2;
4365 }
4366
4367 /*
4368 * Compute the result.
4369 * When either side is a float the result is a float.
4370 */
4371 if (use_float)
4372 {
4373 if (op == '*')
4374 f1 = f1 * f2;
4375 else if (op == '/')
4376 {
4377#ifdef VMS
4378 // VMS crashes on divide by zero, work around it
4379 if (f2 == 0.0)
4380 {
4381 if (f1 == 0)
4382 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
4383 else if (f1 < 0)
4384 f1 = -1 * __F_FLT_MAX;
4385 else
4386 f1 = __F_FLT_MAX;
4387 }
4388 else
4389 f1 = f1 / f2;
4390#else
4391 // We rely on the floating point library to handle divide
4392 // by zero to result in "inf" and not a crash.
4393 f1 = f1 / f2;
4394#endif
4395 }
4396 else
4397 {
4398 emsg(_(e_cannot_use_percent_with_float));
4399 return FAIL;
4400 }
4401 tv1->v_type = VAR_FLOAT;
4402 tv1->vval.v_float = f1;
4403 }
4404 else
4405 {
4406 int failed = FALSE;
4407
4408 if (op == '*')
4409 n1 = n1 * n2;
4410 else if (op == '/')
4411 n1 = num_divide(n1, n2, &failed);
4412 else
4413 n1 = num_modulus(n1, n2, &failed);
4414 if (failed)
4415 return FAIL;
4416
4417 tv1->v_type = VAR_NUMBER;
4418 tv1->vval.v_number = n1;
4419 }
4420
4421 return OK;
4422}
4423
4424/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004425 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 * * number multiplication
4427 * / number division
4428 * % number modulo
4429 *
4430 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004431 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 *
4433 * Return OK or FAIL.
4434 */
4435 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004436eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004437 char_u **arg,
4438 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004439 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004440 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004443 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004445 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return FAIL;
4447
4448 /*
4449 * Repeat computing, until no '*', '/' or '%' is following.
4450 */
4451 for (;;)
4452 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004453 int evaluate;
4454 int getnext;
4455 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004456 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004457 int op;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004458
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004459 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004460 p = eval_next_non_blank(*arg, evalarg, &getnext);
4461 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004462 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004464
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004465 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004466 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004467 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004468 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004469 {
4470 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4471 {
mityu4ac198c2021-05-28 17:52:40 +02004472 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004473 clear_tv(rettv);
4474 return FAIL;
4475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004476 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 /*
4480 * Get the second variable.
4481 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004482 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4483 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004484 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004485 clear_tv(rettv);
4486 return FAIL;
4487 }
4488 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004489 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004492 if (evaluate)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004493 // Compute the result.
4494 if (eval_multdiv_number(rettv, &var2, op) == FAIL)
4495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497
4498 return OK;
4499}
4500
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004501/*
4502 * Handle a type cast before a base level expression.
4503 * "arg" must point to the first non-white of the expression.
4504 * "arg" is advanced to just after the recognized expression.
4505 * Return OK or FAIL.
4506 */
4507 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004508eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004509 char_u **arg,
4510 typval_T *rettv,
4511 evalarg_T *evalarg,
4512 int want_string) // after "." operator
4513{
4514 type_T *want_type = NULL;
4515 garray_T type_list; // list of pointers to allocated types
4516 int res;
4517 int evaluate = evalarg == NULL ? 0
4518 : (evalarg->eval_flags & EVAL_EVALUATE);
4519
4520 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004521 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4522 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004523 {
4524 ++*arg;
4525 ga_init2(&type_list, sizeof(type_T *), 10);
4526 want_type = parse_type(arg, &type_list, TRUE);
4527 if (want_type == NULL && (evaluate || **arg != '>'))
4528 {
4529 clear_type_list(&type_list);
4530 return FAIL;
4531 }
4532
4533 if (**arg != '>')
4534 {
4535 if (*skipwhite(*arg) == '>')
4536 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4537 else
4538 emsg(_(e_missing_gt));
4539 clear_type_list(&type_list);
4540 return FAIL;
4541 }
4542 ++*arg;
4543 *arg = skipwhite_and_linebreak(*arg, evalarg);
4544 }
4545
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004546 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004547
4548 if (want_type != NULL && evaluate)
4549 {
4550 if (res == OK)
4551 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004552 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4553 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004554
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004555 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004556 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004557 if (want_type->tt_type == VAR_BOOL
4558 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004559 && (actual->tt_flags & TTFLAG_BOOL_OK))
4560 {
4561 int n = tv2bool(rettv);
4562
4563 // can use "0" and "1" for boolean in some places
4564 clear_tv(rettv);
4565 rettv->v_type = VAR_BOOL;
4566 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4567 }
4568 else
4569 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004570 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004571
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004572 res = check_type(want_type, actual, TRUE, where);
4573 }
4574 }
4575 }
4576 clear_type_list(&type_list);
4577 }
4578
4579 return res;
4580}
4581
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004582 int
4583eval_leader(char_u **arg, int vim9)
4584{
4585 char_u *s = *arg;
4586 char_u *p = *arg;
4587
4588 while (*p == '!' || *p == '-' || *p == '+')
4589 {
4590 char_u *n = skipwhite(p + 1);
4591
4592 // ++, --, -+ and +- are not accepted in Vim9 script
4593 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4594 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004595 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004596 return FAIL;
4597 }
4598 p = n;
4599 }
4600 *arg = p;
4601 return OK;
4602}
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004605 * Check for a predefined value "true", "false" and "null.*".
4606 * Return OK when recognized.
4607 */
4608 int
4609handle_predefined(char_u *s, int len, typval_T *rettv)
4610{
4611 switch (len)
4612 {
4613 case 4: if (STRNCMP(s, "true", 4) == 0)
4614 {
4615 rettv->v_type = VAR_BOOL;
4616 rettv->vval.v_number = VVAL_TRUE;
4617 return OK;
4618 }
4619 if (STRNCMP(s, "null", 4) == 0)
4620 {
4621 rettv->v_type = VAR_SPECIAL;
4622 rettv->vval.v_number = VVAL_NULL;
4623 return OK;
4624 }
4625 break;
4626 case 5: if (STRNCMP(s, "false", 5) == 0)
4627 {
4628 rettv->v_type = VAR_BOOL;
4629 rettv->vval.v_number = VVAL_FALSE;
4630 return OK;
4631 }
4632 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004633 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4634 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004635#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004636 rettv->v_type = VAR_JOB;
4637 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004638#else
4639 rettv->v_type = VAR_SPECIAL;
4640 rettv->vval.v_number = VVAL_NULL;
4641#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004642 return OK;
4643 }
4644 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004645 case 9:
4646 if (STRNCMP(s, "null_", 5) != 0)
4647 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004648 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004649 if (STRNCMP(s + 5, "list", 4) == 0)
4650 {
4651 rettv->v_type = VAR_LIST;
4652 rettv->vval.v_list = NULL;
4653 return OK;
4654 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004655 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004656 if (STRNCMP(s + 5, "dict", 4) == 0)
4657 {
4658 rettv->v_type = VAR_DICT;
4659 rettv->vval.v_dict = NULL;
4660 return OK;
4661 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004662 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004663 if (STRNCMP(s + 5, "blob", 4) == 0)
4664 {
4665 rettv->v_type = VAR_BLOB;
4666 rettv->vval.v_blob = NULL;
4667 return OK;
4668 }
4669 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004670 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4671 {
4672 rettv->v_type = VAR_CLASS;
4673 rettv->vval.v_class = NULL;
4674 return OK;
4675 }
4676 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004677 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4678 {
4679 rettv->v_type = VAR_STRING;
4680 rettv->vval.v_string = NULL;
4681 return OK;
4682 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004683 if (STRNCMP(s, "null_object", 11) == 0)
4684 {
4685 rettv->v_type = VAR_OBJECT;
4686 rettv->vval.v_object = NULL;
4687 return OK;
4688 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004689 break;
4690 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004691 if (STRNCMP(s, "null_channel", 12) == 0)
4692 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004693#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004694 rettv->v_type = VAR_CHANNEL;
4695 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004696#else
4697 rettv->v_type = VAR_SPECIAL;
4698 rettv->vval.v_number = VVAL_NULL;
4699#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004700 return OK;
4701 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004702 if (STRNCMP(s, "null_partial", 12) == 0)
4703 {
4704 rettv->v_type = VAR_PARTIAL;
4705 rettv->vval.v_partial = NULL;
4706 return OK;
4707 }
4708 break;
4709 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4710 {
4711 rettv->v_type = VAR_FUNC;
4712 rettv->vval.v_string = NULL;
4713 return OK;
4714 }
4715 break;
4716 }
4717 return FAIL;
4718}
4719
4720/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004721 * Handle register contents: @r.
4722 */
4723 static void
4724eval9_reg_contents(
4725 char_u **arg,
4726 typval_T *rettv,
4727 int evaluate)
4728{
4729 int vim9script = in_vim9script();
4730
4731 ++*arg; // skip '@'
4732 if (evaluate)
4733 {
4734 if (vim9script && IS_WHITE_OR_NUL(**arg))
4735 semsg(_(e_syntax_error_at_str), *arg);
4736 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4737 emsg_invreg(**arg);
4738 else
4739 {
4740 rettv->v_type = VAR_STRING;
4741 rettv->vval.v_string = get_reg_contents(**arg,
4742 GREG_EXPR_SRC);
4743 }
4744 }
4745 if (**arg != NUL)
4746 ++*arg;
4747}
4748
4749/*
4750 * Handle a nested expression: (expression) or lambda: (arg) => expr
4751 */
4752 static int
4753eval9_nested_expr(
4754 char_u **arg,
4755 typval_T *rettv,
4756 evalarg_T *evalarg,
4757 int evaluate)
4758{
4759 int ret = NOTDONE;
4760 int vim9script = in_vim9script();
4761
4762 if (vim9script)
4763 {
4764 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4765 if (ret == OK && evaluate)
4766 {
4767 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4768
4769 // Compile it here to get the return type. The return
4770 // type is optional, when it's missing use t_unknown.
4771 // This is recognized in compile_return().
4772 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4773 ufunc->uf_ret_type = &t_unknown;
4774 if (compile_def_function(ufunc, FALSE,
4775 get_compile_type(ufunc), NULL) == FAIL)
4776 {
4777 clear_tv(rettv);
4778 ret = FAIL;
4779 }
4780 }
4781 }
4782 if (ret == NOTDONE)
4783 {
4784 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
4785 ret = eval1(arg, rettv, evalarg); // recursive!
4786
4787 *arg = skipwhite_and_linebreak(*arg, evalarg);
4788 if (**arg == ')')
4789 ++*arg;
4790 else if (ret == OK)
4791 {
4792 emsg(_(e_missing_closing_paren));
4793 clear_tv(rettv);
4794 ret = FAIL;
4795 }
4796 }
4797
4798 return ret;
4799}
4800
4801/*
4802* Handle be a variable or function name.
4803* Can also be a curly-braces kind of name: {expr}.
4804*/
4805 static int
4806eval9_var_func_name(
4807 char_u **arg,
4808 typval_T *rettv,
4809 evalarg_T *evalarg,
4810 int evaluate,
4811 char_u **name_start)
4812{
4813 char_u *s;
4814 int len;
4815 char_u *alias;
4816 int ret = OK;
4817 int vim9script = in_vim9script();
4818
4819 s = *arg;
4820 len = get_name_len(arg, &alias, evaluate, TRUE);
4821 if (alias != NULL)
4822 s = alias;
4823
4824 if (len <= 0)
4825 ret = FAIL;
4826 else
4827 {
4828 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4829
4830 if (evaluate && vim9script && len == 1 && *s == '_')
4831 {
4832 emsg(_(e_cannot_use_underscore_here));
4833 ret = FAIL;
4834 }
4835 else if (evaluate && vim9script && len > 2
4836 && s[0] == 's' && s[1] == ':')
4837 {
4838 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4839 ret = FAIL;
4840 }
4841 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
4842 {
4843 // "name(..." recursive!
4844 *arg = skipwhite(*arg);
4845 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
4846 }
4847 else if (evaluate)
4848 {
4849 // get the value of "true", "false", etc. or a variable
4850 ret = FAIL;
4851 if (vim9script)
4852 ret = handle_predefined(s, len, rettv);
4853 if (ret == FAIL)
4854 {
4855 *name_start = s;
4856 ret = eval_variable(s, len, 0, rettv, NULL,
4857 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
4858 }
4859 }
4860 else
4861 {
4862 // skip the name
4863 check_vars(s, len);
4864 ret = OK;
4865 }
4866 }
4867 vim_free(alias);
4868
4869 return ret;
4870}
4871
4872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 * Handle sixth level expression:
4874 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004875 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004876 * "string" string constant
4877 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 * &option-name option value
4879 * @r register contents
4880 * identifier variable value
4881 * function() function call
4882 * $VAR environment variable
4883 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004884 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004886 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004887 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 *
4889 * Also handle:
4890 * ! in front logical NOT
4891 * - in front unary minus
4892 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004893 * trailing [] subscript in String or List
4894 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004895 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 *
4897 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004898 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 *
4900 * Return OK or FAIL.
4901 */
4902 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004903eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904 char_u **arg,
4905 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004906 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004909 int evaluate = evalarg != NULL
4910 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004911 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 char_u *start_leader, *end_leader;
4913 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004914 static int recurse = 0;
4915 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004924 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
4926 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004927 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 end_leader = *arg;
4930
Keith Thompson184f71c2024-01-04 21:19:04 +01004931 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004932 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004933 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004934 ++*arg;
4935 return FAIL;
4936 }
4937
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004938 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004939 // and crash. With MSVC the stack is smaller.
4940 if (recurse ==
4941#ifdef _MSC_VER
4942 300
4943#else
4944 1000
4945#endif
4946 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004947 {
4948 semsg(_(e_expression_too_recursive_str), *arg);
4949 return FAIL;
4950 }
4951 ++recurse;
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 switch (**arg)
4954 {
4955 /*
4956 * Number constant.
4957 */
4958 case '0':
4959 case '1':
4960 case '2':
4961 case '3':
4962 case '4':
4963 case '5':
4964 case '6':
4965 case '7':
4966 case '8':
4967 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004968 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004969
4970 // Apply prefixed "-" and "+" now. Matters especially when
4971 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004972 if (ret == OK && evaluate && end_leader > start_leader
4973 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004974 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976
4977 /*
4978 * String constant: "string".
4979 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004980 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 break;
4982
4983 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004986 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 break;
4988
4989 /*
4990 * List: [expr, expr]
4991 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004992 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 break;
4994
4995 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004996 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004997 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004998 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004999 break;
5000
5001 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005002 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005003 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005005 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005006 ret = NOTDONE;
5007 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005008 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005009 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005010 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 break;
5012
5013 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005014 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005016 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 break;
5018
5019 /*
5020 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005021 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 */
LemonBoy2eaef102022-05-06 13:14:50 +01005023 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5024 ret = eval_interp_string(arg, rettv, evaluate);
5025 else
5026 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 break;
5028
5029 /*
5030 * Register contents: @r.
5031 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005032 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034
5035 /*
5036 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005037 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005039 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 break;
5041
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045
5046 if (ret == NOTDONE)
5047 {
5048 /*
5049 * Must be a variable or function name.
5050 * Can also be a curly-braces kind of name: {expr}.
5051 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005052 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 }
5054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5056 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005057 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005058 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 /*
5061 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5062 */
5063 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005064 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005065
5066 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005067 return ret;
5068}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005069
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005070/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005071 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005072 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005073 * Adjusts "end_leaderp" until it is at "start_leader".
5074 */
5075 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005076eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005077 typval_T *rettv,
5078 int numeric_only,
5079 char_u *start_leader,
5080 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005081{
5082 char_u *end_leader = *end_leaderp;
5083 int ret = OK;
5084 int error = FALSE;
5085 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005086 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005087 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005088 float_T f = 0.0;
5089
5090 if (rettv->v_type == VAR_FLOAT)
5091 f = rettv->vval.v_float;
5092 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005093 {
5094 while (VIM_ISWHITE(end_leader[-1]))
5095 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005096 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005097 val = tv2bool(rettv);
5098 else
5099 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005100 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005101 if (error)
5102 {
5103 clear_tv(rettv);
5104 ret = FAIL;
5105 }
5106 else
5107 {
5108 while (end_leader > start_leader)
5109 {
5110 --end_leader;
5111 if (*end_leader == '!')
5112 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005113 if (numeric_only)
5114 {
5115 ++end_leader;
5116 break;
5117 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005118 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005119 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005120 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005121 {
5122 rettv->v_type = VAR_BOOL;
5123 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5124 }
5125 else
5126 f = !f;
5127 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005128 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005129 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005130 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005131 type = VAR_BOOL;
5132 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005133 }
5134 else if (*end_leader == '-')
5135 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005136 if (rettv->v_type == VAR_FLOAT)
5137 f = -f;
5138 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005139 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005140 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005141 type = VAR_NUMBER;
5142 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005143 }
5144 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005145 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005148 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005150 else
5151 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005152 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005153 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005154 rettv->v_type = type;
5155 else
5156 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005157 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005160 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 return ret;
5162}
5163
5164/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005165 * Call the function referred to in "rettv".
5166 */
5167 static int
5168call_func_rettv(
5169 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005170 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005171 typval_T *rettv,
5172 int evaluate,
5173 dict_T *selfdict,
5174 typval_T *basetv)
5175{
5176 partial_T *pt = NULL;
5177 funcexe_T funcexe;
5178 typval_T functv;
5179 char_u *s;
5180 int ret;
5181
5182 // need to copy the funcref so that we can clear rettv
5183 if (evaluate)
5184 {
5185 functv = *rettv;
5186 rettv->v_type = VAR_UNKNOWN;
5187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005189 if (functv.v_type == VAR_PARTIAL)
5190 {
5191 pt = functv.vval.v_partial;
5192 s = partial_name(pt);
5193 }
5194 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005195 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005196 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005197 if (s == NULL || *s == NUL)
5198 {
5199 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005200 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005201 goto theend;
5202 }
5203 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005204 }
5205 else
5206 s = (char_u *)"";
5207
Bram Moolenaara80faa82020-04-12 19:37:17 +02005208 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005209 funcexe.fe_firstline = curwin->w_cursor.lnum;
5210 funcexe.fe_lastline = curwin->w_cursor.lnum;
5211 funcexe.fe_evaluate = evaluate;
5212 funcexe.fe_partial = pt;
5213 funcexe.fe_selfdict = selfdict;
5214 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005215 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005216
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005217theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // Clear the funcref afterwards, so that deleting it while
5219 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005220 if (evaluate)
5221 clear_tv(&functv);
5222
5223 return ret;
5224}
5225
5226/*
5227 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005228 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005229 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5230 */
5231 static int
5232eval_lambda(
5233 char_u **arg,
5234 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005235 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005237{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005238 int evaluate = evalarg != NULL
5239 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005240 typval_T base = *rettv;
5241 int ret;
5242
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005243 rettv->v_type = VAR_UNKNOWN;
5244
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005245 if (**arg == '{')
5246 {
5247 // ->{lambda}()
5248 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5249 }
5250 else
5251 {
5252 // ->(lambda)()
5253 ++*arg;
5254 ret = eval1(arg, rettv, evalarg);
5255 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005256 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005257 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005258 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005259 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005260 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005261 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5262 && rettv->v_type != VAR_PARTIAL)
5263 {
5264 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5265 return FAIL;
5266 }
5267 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005268 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005269 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005270 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005271
5272 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005273 {
5274 if (verbose)
5275 {
5276 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005277 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005278 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005279 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005280 }
5281 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005282 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005283 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005284 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005285 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005286
5287 // Clear the funcref afterwards, so that deleting it while
5288 // evaluating the arguments is possible (see test55).
5289 if (evaluate)
5290 clear_tv(&base);
5291
5292 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005293}
5294
5295/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005296 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005297 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005298 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5299 */
5300 static int
5301eval_method(
5302 char_u **arg,
5303 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005304 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005305 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005306{
5307 char_u *name;
5308 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005309 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005310 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005311 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005312 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005313 int evaluate = evalarg != NULL
5314 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005315
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005316 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005317
Bram Moolenaarac92e252019-08-03 21:58:38 +02005318 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005319 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005320 if (alias != NULL)
5321 name = alias;
5322
5323 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005324 {
5325 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005326 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005327 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005328 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005329 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005330 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005331 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005332
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005333 // If there is no "(" immediately following, but there is further on,
5334 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5335 // Does not handle anything where "(" is part of the expression.
5336 *arg = skipwhite(*arg);
5337
5338 if (**arg != '(' && alias == NULL
5339 && (paren = vim_strchr(*arg, '(')) != NULL)
5340 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005341 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005342
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005343 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005344 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005345 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005346 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5347 if (evalarg != NULL)
5348 {
5349 getline = evalarg->eval_getline;
5350 evalarg->eval_getline = NULL;
5351 }
5352
5353 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005354 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005355 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005356 *arg = name + len;
5357 ret = FAIL;
5358 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005359 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005360 {
5361 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005362 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005363 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005364
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005365 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005366 if (getline != NULL)
5367 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005368 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005369
5370 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005371 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005372 *arg = skipwhite(*arg);
5373
5374 if (**arg != '(')
5375 {
5376 if (verbose)
5377 semsg(_(e_missing_parenthesis_str), name);
5378 ret = FAIL;
5379 }
5380 else if (VIM_ISWHITE((*arg)[-1]))
5381 {
5382 if (verbose)
5383 emsg(_(e_no_white_space_allowed_before_parenthesis));
5384 ret = FAIL;
5385 }
5386 else
5387 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005388 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005389 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005390 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005391
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005392 // Clear the funcref afterwards, so that deleting it while
5393 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005394 if (evaluate)
5395 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005396 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005397
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005398 if (alias != NULL)
5399 vim_free(alias);
5400
Bram Moolenaarac92e252019-08-03 21:58:38 +02005401 return ret;
5402}
5403
5404/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005405 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5406 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5408 */
5409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005410eval_index(
5411 char_u **arg,
5412 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005413 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005416 int evaluate = evalarg != NULL
5417 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005422 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005423 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005425 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5426 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005428 init_tv(&var1);
5429 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 /*
5433 * dict.name
5434 */
5435 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005436 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005438 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005440 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 }
5442 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 /*
5445 * something[idx]
5446 *
5447 * Get the (first) variable from inside the [].
5448 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005449 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 if (**arg == ':')
5451 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005452 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005454 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005455 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005456 semsg(_(e_white_space_required_before_and_after_str_at_str),
5457 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005458 clear_tv(&var1);
5459 return FAIL;
5460 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005461 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005463 int error = FALSE;
5464
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005465 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005466 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005467 && var1.v_type == VAR_FLOAT)
5468 {
5469 var1.vval.v_string = typval_tostring(&var1, TRUE);
5470 var1.v_type = VAR_STRING;
5471 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005472
Bram Moolenaar4525a572022-02-13 11:57:33 +00005473 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005474 tv_get_number_chk(&var1, &error);
5475 else
5476 error = tv_get_string_chk(&var1) == NULL;
5477 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005478 {
5479 // not a number or string
5480 clear_tv(&var1);
5481 return FAIL;
5482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484
5485 /*
5486 * Get the second variable from inside the [:].
5487 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005488 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 if (**arg == ':')
5490 {
5491 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005492 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005493 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005494 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005495 semsg(_(e_white_space_required_before_and_after_str_at_str),
5496 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005497 if (!empty1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 if (**arg == ']')
5503 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005504 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005506 if (!empty1)
5507 clear_tv(&var1);
5508 return FAIL;
5509 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005510 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005512 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005513 if (!empty1)
5514 clear_tv(&var1);
5515 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 return FAIL;
5517 }
5518 }
5519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005521 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 if (**arg != ']')
5523 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005524 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005525 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 clear_tv(&var1);
5527 if (range)
5528 clear_tv(&var2);
5529 return FAIL;
5530 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005531 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 }
5533
5534 if (evaluate)
5535 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005536 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005537 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005538 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005539
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005540 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005543 clear_tv(&var2);
5544 return res;
5545 }
5546 return OK;
5547}
5548
5549/*
5550 * Check if "rettv" can have an [index] or [sli:ce]
5551 */
5552 int
5553check_can_index(typval_T *rettv, int evaluate, int verbose)
5554{
5555 switch (rettv->v_type)
5556 {
5557 case VAR_FUNC:
5558 case VAR_PARTIAL:
5559 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005560 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005561 return FAIL;
5562 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005563 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005564 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005565 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005566 case VAR_BOOL:
5567 case VAR_SPECIAL:
5568 case VAR_JOB:
5569 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005570 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005571 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005572 if (verbose)
5573 emsg(_(e_cannot_index_special_variable));
5574 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005575 case VAR_CLASS:
5576 case VAR_TYPEALIAS:
5577 if (verbose)
5578 check_typval_is_value(rettv);
5579 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005580 case VAR_UNKNOWN:
5581 case VAR_ANY:
5582 case VAR_VOID:
5583 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005585 emsg(_(e_cannot_index_special_variable));
5586 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005588 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005590 case VAR_STRING:
5591 case VAR_LIST:
5592 case VAR_DICT:
5593 case VAR_BLOB:
5594 break;
5595 case VAR_NUMBER:
5596 if (in_vim9script())
5597 emsg(_(e_cannot_index_number));
5598 break;
5599 }
5600 return OK;
5601}
5602
5603/*
5604 * Apply index or range to "rettv".
5605 * "var1" is the first index, NULL for [:expr].
5606 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005607 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5608 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005609 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5610 */
5611 int
5612eval_index_inner(
5613 typval_T *rettv,
5614 int is_range,
5615 typval_T *var1,
5616 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005617 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005618 char_u *key,
5619 int keylen,
5620 int verbose)
5621{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005622 varnumber_T n1, n2 = 0;
5623 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005624
5625 n1 = 0;
5626 if (var1 != NULL && rettv->v_type != VAR_DICT)
5627 n1 = tv_get_number(var1);
5628
5629 if (is_range)
5630 {
5631 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005633 if (verbose)
5634 emsg(_(e_cannot_slice_dictionary));
5635 return FAIL;
5636 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005637 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005638 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005639 else
5640 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005641 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005642
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005643 switch (rettv->v_type)
5644 {
5645 case VAR_UNKNOWN:
5646 case VAR_ANY:
5647 case VAR_VOID:
5648 case VAR_FUNC:
5649 case VAR_PARTIAL:
5650 case VAR_FLOAT:
5651 case VAR_BOOL:
5652 case VAR_SPECIAL:
5653 case VAR_JOB:
5654 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005655 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005656 case VAR_CLASS:
5657 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005658 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005659 break; // not evaluating, skipping over subscript
5660
5661 case VAR_NUMBER:
5662 case VAR_STRING:
5663 {
5664 char_u *s = tv_get_string(rettv);
5665
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005667 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005668 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005669 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005670 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005671 else
5672 s = char_from_string(s, n1);
5673 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005674 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 // The resulting variable is a substring. If the indexes
5677 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 if (n1 < 0)
5679 {
5680 n1 = len + n1;
5681 if (n1 < 0)
5682 n1 = 0;
5683 }
5684 if (n2 < 0)
5685 n2 = len + n2;
5686 else if (n2 >= len)
5687 n2 = len;
5688 if (n1 >= len || n2 < 0 || n1 > n2)
5689 s = NULL;
5690 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005691 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 }
5693 else
5694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005695 // The resulting variable is a string of a single
5696 // character. If the index is too big or negative the
5697 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 if (n1 >= len || n1 < 0)
5699 s = NULL;
5700 else
5701 s = vim_strnsave(s + n1, 1);
5702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 clear_tv(rettv);
5704 rettv->v_type = VAR_STRING;
5705 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005706 }
5707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005709 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005710 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5711 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005712 break;
5713
5714 case VAR_LIST:
5715 if (var1 == NULL)
5716 n1 = 0;
5717 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005718 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005719 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005720 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005721 return FAIL;
5722 break;
5723
5724 case VAR_DICT:
5725 {
5726 dictitem_T *item;
5727 typval_T tmp;
5728
5729 if (key == NULL)
5730 {
5731 key = tv_get_string_chk(var1);
5732 if (key == NULL)
5733 return FAIL;
5734 }
5735
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005736 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005737
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005738 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005739 {
5740 if (verbose)
5741 {
5742 if (keylen > 0)
5743 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005744 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005745 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005746 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005747 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005748
5749 copy_tv(&item->di_tv, &tmp);
5750 clear_tv(rettv);
5751 *rettv = tmp;
5752 }
5753 break;
5754 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 return OK;
5756}
5757
5758/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005759 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005760 */
5761 char_u *
5762partial_name(partial_T *pt)
5763{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005764 if (pt != NULL)
5765 {
5766 if (pt->pt_name != NULL)
5767 return pt->pt_name;
5768 if (pt->pt_func != NULL)
5769 return pt->pt_func->uf_name;
5770 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005771 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005772}
5773
Bram Moolenaarddecc252016-04-06 22:59:37 +02005774 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005775partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005776{
5777 int i;
5778
5779 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005780 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005781 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005782 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005783 if (pt->pt_name != NULL)
5784 {
5785 func_unref(pt->pt_name);
5786 vim_free(pt->pt_name);
5787 }
5788 else
5789 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005790 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005791
Bram Moolenaar54656012021-06-09 20:50:46 +02005792 // "out_up" is no longer used, decrement refcount on partial that owns it.
5793 partial_unref(pt->pt_outer.out_up_partial);
5794
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005795 // Using pt_outer from another partial.
5796 partial_unref(pt->pt_outer_partial);
5797
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005798 // Decrease the reference count for the context of a closure. If down
5799 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005800 if (pt->pt_funcstack != NULL)
5801 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005802 --pt->pt_funcstack->fs_refcount;
5803 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005804 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005805 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005806 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5807 if (pt->pt_loopvars[i] != NULL)
5808 {
5809 --pt->pt_loopvars[i]->lvs_refcount;
5810 loopvars_check_refcount(pt->pt_loopvars[i]);
5811 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005812
Bram Moolenaarddecc252016-04-06 22:59:37 +02005813 vim_free(pt);
5814}
5815
5816/*
5817 * Unreference a closure: decrement the reference count and free it when it
5818 * becomes zero.
5819 */
5820 void
5821partial_unref(partial_T *pt)
5822{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005823 if (pt == NULL)
5824 return;
5825
5826 int done = FALSE;
5827
5828 if (--pt->pt_refcount <= 0)
5829 partial_free(pt);
5830
5831 // If the reference count goes down to one, the funcstack may be the
5832 // only reference and can be freed if no other partials reference it.
5833 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005834 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005835 // careful: if the funcstack is freed it may contain this partial
5836 // and it gets freed as well
5837 if (pt->pt_funcstack != NULL)
5838 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005839
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005840 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005841 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005842 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005843
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005844 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5845 if (pt->pt_loopvars[depth] != NULL
5846 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005847 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005848 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005849 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005850}
5851
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005852/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005853 * Return a textual representation of a string in "tv".
5854 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5855 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5856 * strings as "string()", otherwise does not put quotes around strings.
5857 * May return NULL.
5858 */
5859 static char_u *
5860string_tv2string(
5861 typval_T *tv,
5862 char_u **tofree,
5863 int echo_style,
5864 int composite_val)
5865{
5866 char_u *r = NULL;
5867
5868 if (echo_style && !composite_val)
5869 {
5870 *tofree = NULL;
5871 r = tv->vval.v_string;
5872 if (r == NULL)
5873 r = (char_u *)"";
5874 }
5875 else
5876 {
5877 *tofree = string_quote(tv->vval.v_string, FALSE);
5878 r = *tofree;
5879 }
5880
5881 return r;
5882}
5883
5884/*
5885 * Return a textual representation of a function in "tv".
5886 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5887 * When "echo_style" is FALSE, put quotes around the function name as
5888 * "function()", otherwise does not put quotes around function name.
5889 * May return NULL.
5890 */
5891 static char_u *
5892func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5893{
5894 char_u *r = NULL;
5895 char_u buf[MAX_FUNC_NAME_LEN];
5896
5897 if (echo_style)
5898 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005899 *tofree = NULL;
5900
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005901 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005902 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005903 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005904 {
5905 r = make_ufunc_name_readable(tv->vval.v_string, buf,
5906 MAX_FUNC_NAME_LEN);
5907 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005908 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02005909 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005910 }
5911 else
5912 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005913 char_u *s = NULL;
5914
5915 if (tv->vval.v_string != NULL)
5916 s = make_ufunc_name_readable(tv->vval.v_string, buf,
5917 MAX_FUNC_NAME_LEN);
5918
5919 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005920 }
5921
5922 return r;
5923}
5924
5925/*
Ernie Rael9d779c52024-07-07 20:41:44 +02005926 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
5927 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5928 * When "echo_style" is FALSE, put quotes around the function name as
5929 * "function()", otherwise does not put quotes around function name.
5930 * May return NULL.
5931 */
5932 static char_u *
5933method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5934{
5935 char_u buf[MAX_FUNC_NAME_LEN];
5936 partial_T *pt = tv->vval.v_partial;
5937
5938 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
5939 pt->pt_func->uf_script_ctx.sc_sid,
5940 pt->pt_func->uf_class->class_name,
5941 pt->pt_func->uf_name);
5942 if (len >= sizeof(buf))
5943 {
5944 if (echo_style)
5945 {
5946 *tofree = NULL;
5947 return (char_u *)"function()";
5948 }
5949 else
5950 return *tofree = string_quote((char_u*)"", TRUE);
5951 }
5952
5953 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
5954}
5955
5956/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005957 * Return a textual representation of a partial in "tv".
5958 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5959 * "numbuf" is used for a number. May return NULL.
5960 */
5961 static char_u *
5962partial_tv2string(
5963 typval_T *tv,
5964 char_u **tofree,
5965 char_u *numbuf,
5966 int copyID)
5967{
5968 char_u *r = NULL;
5969 partial_T *pt;
5970 char_u *fname;
5971 garray_T ga;
5972 int i;
5973 char_u *tf;
5974
5975 pt = tv->vval.v_partial;
5976 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5977
5978 ga_init2(&ga, 1, 100);
5979 ga_concat(&ga, (char_u *)"function(");
5980 if (fname != NULL)
5981 {
5982 // When using uf_name prepend "g:" for a global function.
5983 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5984 && vim_isupper(fname[1]))
5985 {
5986 ga_concat(&ga, (char_u *)"'g:");
5987 ga_concat(&ga, fname + 1);
5988 }
5989 else
5990 ga_concat(&ga, fname);
5991 vim_free(fname);
5992 }
5993 if (pt != NULL && pt->pt_argc > 0)
5994 {
5995 ga_concat(&ga, (char_u *)", [");
5996 for (i = 0; i < pt->pt_argc; ++i)
5997 {
5998 if (i > 0)
5999 ga_concat(&ga, (char_u *)", ");
6000 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6001 vim_free(tf);
6002 }
6003 ga_concat(&ga, (char_u *)"]");
6004 }
6005 if (pt != NULL && pt->pt_dict != NULL)
6006 {
6007 typval_T dtv;
6008
6009 ga_concat(&ga, (char_u *)", ");
6010 dtv.v_type = VAR_DICT;
6011 dtv.vval.v_dict = pt->pt_dict;
6012 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6013 vim_free(tf);
6014 }
6015 // terminate with ')' and a NUL
6016 ga_concat_len(&ga, (char_u *)")", 2);
6017
6018 *tofree = ga.ga_data;
6019 r = *tofree;
6020
6021 return r;
6022}
6023
6024/*
6025 * Return a textual representation of a List in "tv".
6026 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6027 * When "copyID" is not zero replace recursive lists with "...". When
6028 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6029 * May return NULL.
6030 */
6031 static char_u *
6032list_tv2string(
6033 typval_T *tv,
6034 char_u **tofree,
6035 int copyID,
6036 int restore_copyID)
6037{
6038 char_u *r = NULL;
6039
6040 if (tv->vval.v_list == NULL)
6041 {
6042 // NULL list is equivalent to empty list.
6043 *tofree = NULL;
6044 r = (char_u *)"[]";
6045 }
6046 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6047 && tv->vval.v_list->lv_len > 0)
6048 {
6049 *tofree = NULL;
6050 r = (char_u *)"[...]";
6051 }
6052 else
6053 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006054 int old_copyID;
6055 if (restore_copyID)
6056 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006057
6058 tv->vval.v_list->lv_copyID = copyID;
6059 *tofree = list2string(tv, copyID, restore_copyID);
6060 if (restore_copyID)
6061 tv->vval.v_list->lv_copyID = old_copyID;
6062 r = *tofree;
6063 }
6064
6065 return r;
6066}
6067
6068/*
6069 * Return a textual representation of a Dict in "tv".
6070 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6071 * When "copyID" is not zero replace recursive dicts with "...".
6072 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6073 * replaced with "...". May return NULL.
6074 */
6075 static char_u *
6076dict_tv2string(
6077 typval_T *tv,
6078 char_u **tofree,
6079 int copyID,
6080 int restore_copyID)
6081{
6082 char_u *r = NULL;
6083
6084 if (tv->vval.v_dict == NULL)
6085 {
6086 // NULL dict is equivalent to empty dict.
6087 *tofree = NULL;
6088 r = (char_u *)"{}";
6089 }
6090 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6091 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6092 {
6093 *tofree = NULL;
6094 r = (char_u *)"{...}";
6095 }
6096 else
6097 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006098 int old_copyID;
6099 if (restore_copyID)
6100 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006101
6102 tv->vval.v_dict->dv_copyID = copyID;
6103 *tofree = dict2string(tv, copyID, restore_copyID);
6104 if (restore_copyID)
6105 tv->vval.v_dict->dv_copyID = old_copyID;
6106 r = *tofree;
6107 }
6108
6109 return r;
6110}
6111
6112/*
6113 * Return a textual representation of a job or a channel in "tv".
6114 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6115 * "numbuf" is used for a number.
6116 * When "composite_val" is FALSE, put quotes around strings as "string()",
6117 * otherwise does not put quotes around strings.
6118 * May return NULL.
6119 */
6120 static char_u *
6121jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006122 typval_T *tv UNUSED,
6123 char_u **tofree UNUSED,
6124 char_u *numbuf UNUSED,
6125 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006126{
6127 char_u *r = NULL;
6128
6129#ifdef FEAT_JOB_CHANNEL
6130 *tofree = NULL;
6131
6132 if (tv->v_type == VAR_JOB)
6133 r = job_to_string_buf(tv, numbuf);
6134 else
6135 r = channel_to_string_buf(tv, numbuf);
6136
6137 if (composite_val)
6138 {
6139 *tofree = string_quote(r, FALSE);
6140 r = *tofree;
6141 }
6142#endif
6143
6144 return r;
6145}
6146
6147/*
6148 * Return a textual representation of a class in "tv".
6149 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6150 * May return NULL.
6151 */
6152 static char_u *
6153class_tv2string(typval_T *tv, char_u **tofree)
6154{
6155 char_u *r = NULL;
6156 class_T *cl = tv->vval.v_class;
6157 char *s = "class";
6158
6159 if (cl != NULL && IS_INTERFACE(cl))
6160 s = "interface";
6161 else if (cl != NULL && IS_ENUM(cl))
6162 s = "enum";
6163 size_t len = STRLEN(s) + 1 +
6164 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6165 r = *tofree = alloc(len);
6166 vim_snprintf((char *)r, len, "%s %s", s,
6167 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6168
6169 return r;
6170}
6171
6172/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006173 * Return a textual representation of an Object in "tv".
6174 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6175 * When "copyID" is not zero replace recursive object with "...".
6176 * When "restore_copyID" is FALSE, repeated items in the object are
6177 * replaced with "...". May return NULL.
6178 */
6179 static char_u *
6180object_tv2string(
6181 typval_T *tv,
6182 char_u **tofree,
6183 int copyID,
6184 int restore_copyID,
6185 char_u *numbuf,
6186 int echo_style,
6187 int composite_val)
6188{
6189 char_u *r = NULL;
6190
6191 object_T *obj = tv->vval.v_object;
6192 if (obj == NULL || obj->obj_class == NULL)
6193 {
6194 *tofree = NULL;
6195 r = (char_u *)"object of [unknown]";
6196 }
6197 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006198 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006199 {
Ernie Rael5285b3c2024-07-08 20:42:45 +02006200 size_t n = 25 + strlen((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006201 r = alloc(n);
6202 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006203 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006204 obj->obj_class->class_name);
6205 *tofree = r;
6206 }
6207 else
6208 {
6209 int old_copyID;
6210 if (restore_copyID)
6211 old_copyID = obj->obj_copyID;
6212
6213 obj->obj_copyID = copyID;
6214 *tofree = object2string(obj, numbuf, copyID, echo_style,
6215 restore_copyID, composite_val);
6216 if (restore_copyID)
6217 obj->obj_copyID = old_copyID;
6218 r = *tofree;
6219 }
6220
6221 return r;
6222}
6223
6224/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 * Return a string with the string representation of a variable.
6226 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006228 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006229 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006230 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006231 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006232 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6233 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006234 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006236 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006237echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238 typval_T *tv,
6239 char_u **tofree,
6240 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006241 int copyID,
6242 int echo_style,
6243 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006244 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006246 static int recurse = 0;
6247 char_u *r = NULL;
6248
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006250 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006251 if (!did_echo_string_emsg)
6252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006253 // Only give this message once for a recursive call to avoid
6254 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006255 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006256 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006257 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006259 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006260 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 }
6262 ++recurse;
6263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 switch (tv->v_type)
6265 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006267 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006268 break;
6269
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006271 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006272 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006273
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006274 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006275 if (tv->vval.v_partial == NULL
6276 || tv->vval.v_partial->pt_obj == NULL)
6277 r = partial_tv2string(tv, tofree, numbuf, copyID);
6278 else
6279 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006280 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006282 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006283 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006284 break;
6285
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006287 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006289
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006291 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006295 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006296 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006298 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006299 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006300 break;
6301
Bram Moolenaar835dc632016-02-07 14:27:38 +01006302 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006303 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006304 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006306
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006307 case VAR_INSTR:
6308 *tofree = NULL;
6309 r = (char_u *)"instructions";
6310 break;
6311
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006312 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006313 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006314 break;
6315
6316 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006317 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6318 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006319 break;
6320
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006321 case VAR_FLOAT:
6322 *tofree = NULL;
6323 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6324 r = numbuf;
6325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006326
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006327 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006328 case VAR_SPECIAL:
6329 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006330 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006331 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006332
6333 case VAR_TYPEALIAS:
6334 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6335 r = *tofree;
6336 if (r == NULL)
6337 r = (char_u *)"";
6338 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340
Bram Moolenaar8502c702014-06-17 12:51:16 +02006341 if (--recurse == 0)
6342 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006343 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006344}
6345
6346/*
6347 * Return a string with the string representation of a variable.
6348 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6349 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006350 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006351 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006352 * May return NULL.
6353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006355echo_string(
6356 typval_T *tv,
6357 char_u **tofree,
6358 char_u *numbuf,
6359 int copyID)
6360{
6361 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6362}
6363
6364/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006365 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6366 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006367 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006368 */
6369 int
6370buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6371{
6372 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006373 char_u *t;
6374 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006375
6376 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6377 return -1;
6378
6379 if (lnum > buf->b_ml.ml_line_count)
6380 lnum = buf->b_ml.ml_line_count;
6381
6382 str = ml_get_buf(buf, lnum, FALSE);
6383 if (str == NULL)
6384 return -1;
6385
6386 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006387 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006388
Bram Moolenaar91458462021-01-13 20:08:38 +01006389 // count the number of characters
6390 t = str;
6391 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6392 t += mb_ptr2len(t);
6393
6394 // In insert mode, when the cursor is at the end of a non-empty line,
6395 // byteidx points to the NUL character immediately past the end of the
6396 // string. In this case, add one to the character count.
6397 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6398 count++;
6399
6400 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006401}
6402
6403/*
6404 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006405 * byte index. Works only for loaded buffers. Returns -1 on failure.
6406 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006407 */
6408 int
6409buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6410{
6411 char_u *str;
6412 char_u *t;
6413
6414 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6415 return -1;
6416
6417 if (lnum > buf->b_ml.ml_line_count)
6418 lnum = buf->b_ml.ml_line_count;
6419
6420 str = ml_get_buf(buf, lnum, FALSE);
6421 if (str == NULL)
6422 return -1;
6423
6424 // Convert the character offset to a byte offset
6425 t = str;
6426 while (*t != NUL && --charidx > 0)
6427 t += mb_ptr2len(t);
6428
Bram Moolenaar91458462021-01-13 20:08:38 +01006429 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006430}
6431
6432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006434 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437var2fpos(
6438 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006439 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006440 int *fnum, // set to fnum for '0, 'A, etc.
6441 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006443 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006445 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006447 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006448 if (varp->v_type == VAR_LIST)
6449 {
6450 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006451 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006452 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006453 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006454
6455 l = varp->vval.v_list;
6456 if (l == NULL)
6457 return NULL;
6458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006459 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006460 pos.lnum = list_find_nr(l, 0L, &error);
6461 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006463 if (charcol)
6464 len = (long)mb_charlen(ml_get(pos.lnum));
6465 else
John Marriottbfcc8952024-03-11 22:04:45 +01006466 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006467
Bram Moolenaarec65d772020-08-20 22:29:12 +02006468 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006470 li = list_find(l, 1L);
6471 if (li != NULL && li->li_tv.v_type == VAR_STRING
6472 && li->li_tv.vval.v_string != NULL
6473 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006474 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006475 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006476 }
6477 else
6478 {
6479 pos.col = list_find_nr(l, 1L, &error);
6480 if (error)
6481 return NULL;
6482 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006485 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006486 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006487 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006489 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006490 pos.coladd = list_find_nr(l, 2L, &error);
6491 if (error)
6492 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006493
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006494 return &pos;
6495 }
6496
Bram Moolenaarc5809432021-03-27 21:23:30 +01006497 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6498 return NULL;
6499
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006500 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006501 if (name == NULL)
6502 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006503
6504 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006505 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006506 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006507 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006508 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006509 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006510 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006511 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006512 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006513 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006514 pos = VIsual;
6515 else
6516 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006517 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006518 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006519 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006521 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006522 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6524 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006525 pos = *pp;
6526 }
6527 if (pos.lnum != 0)
6528 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006529 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006530 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6531 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006533
Bram Moolenaara5525202006-03-02 22:52:09 +00006534 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006535
Bram Moolenaar477933c2007-07-17 14:32:23 +00006536 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006537 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006538 // the "w_valid" flags are not reset when moving the cursor, but they
6539 // do matter for update_topline() and validate_botline().
6540 check_cursor_moved(curwin);
6541
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006542 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006544 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006545 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006546 // In silent Ex mode topline is zero, but that's not a valid line
6547 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006548 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006549 return &pos;
6550 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006552 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006555 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006556 return &pos;
6557 }
6558 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006559 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006561 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
6563 pos.lnum = curbuf->b_ml.ml_line_count;
6564 pos.col = 0;
6565 }
6566 else
6567 {
6568 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006569 if (charcol)
6570 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6571 else
John Marriottbfcc8952024-03-11 22:04:45 +01006572 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574 return &pos;
6575 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006576 if (in_vim9script())
6577 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 return NULL;
6579}
6580
6581/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006582 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006583 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006584 * Note that the column is passed on as-is, the caller may want to decrement
6585 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006586 * If "charcol" is TRUE use the column as the character index instead of the
6587 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006588 * Return FAIL when conversion is not possible, doesn't check the position for
6589 * validity.
6590 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list2fpos(
6593 typval_T *arg,
6594 pos_T *posp,
6595 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006596 colnr_T *curswantp,
6597 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006598{
6599 list_T *l = arg->vval.v_list;
6600 long i = 0;
6601 long n;
6602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006603 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6604 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006605 if (arg->v_type != VAR_LIST
6606 || l == NULL
6607 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006608 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006609 return FAIL;
6610
6611 if (fnump != NULL)
6612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006613 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006614 if (n < 0)
6615 return FAIL;
6616 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006617 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006618 *fnump = n;
6619 }
6620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006621 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006622 if (n < 0)
6623 return FAIL;
6624 posp->lnum = n;
6625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006626 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006627 if (n < 0)
6628 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006629 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006630 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006631 if (charcol)
6632 {
6633 buf_T *buf;
6634
6635 // Get the text for the specified line in a loaded buffer
6636 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6637 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6638 return FAIL;
6639
Bram Moolenaar79f23442022-10-10 12:42:57 +01006640 n = buf_charidx_to_byteidx(buf,
6641 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006642 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006643 posp->col = n;
6644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006646 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006647 posp->coladd = 0;
6648 else
6649 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006650
Bram Moolenaar493c1782014-05-28 14:34:46 +02006651 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006652 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006653
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006654 return OK;
6655}
6656
6657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * Get the length of an environment variable name.
6659 * Advance "arg" to the first character after the name.
6660 * Return 0 for error.
6661 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 char_u *p;
6666 int len;
6667
6668 for (p = *arg; vim_isIDc(*p); ++p)
6669 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006670 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 return 0;
6672
6673 len = (int)(p - *arg);
6674 *arg = p;
6675 return len;
6676}
6677
6678/*
6679 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006680 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 * Return 0 if something is wrong.
6682 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006684get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 char_u *p;
6687 int len;
6688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006691 {
6692 if (*p == ':')
6693 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006694 // "s:" is start of "s:var", but "n:" is not and can be used in
6695 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006696 len = (int)(p - *arg);
6697 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6698 || len > 1)
6699 break;
6700 }
6701 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006702 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 return 0;
6704
6705 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006706 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 return len;
6709}
6710
6711/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006712 * Get the length of the name of a variable or function.
6713 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006715 * Return -1 if curly braces expansion failed.
6716 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 * If the name contains 'magic' {}'s, expand them and return the
6718 * expanded name in an allocated string via 'alias' - caller must free.
6719 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006721get_name_len(
6722 char_u **arg,
6723 char_u **alias,
6724 int evaluate,
6725 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 char_u *p;
6729 char_u *expr_start;
6730 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006732 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6735 && (*arg)[2] == (int)KE_SNR)
6736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006737 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 *arg += 3;
6739 return get_id_len(arg) + 3;
6740 }
6741 len = eval_fname_script(*arg);
6742 if (len > 0)
6743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006744 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 *arg += len;
6746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006749 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006751 p = find_name_end(*arg, &expr_start, &expr_end,
6752 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (expr_start != NULL)
6754 {
6755 char_u *temp_string;
6756
6757 if (!evaluate)
6758 {
6759 len += (int)(p - *arg);
6760 *arg = skipwhite(p);
6761 return len;
6762 }
6763
6764 /*
6765 * Include any <SID> etc in the expanded string:
6766 * Thus the -len here.
6767 */
6768 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6769 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006770 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 *alias = temp_string;
6772 *arg = skipwhite(p);
6773 return (int)STRLEN(temp_string);
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006777 // Only give an error when there is something, otherwise it will be
6778 // reported at a higher level.
6779 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006780 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
6782 return len;
6783}
6784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006785/*
6786 * Find the end of a variable or function name, taking care of magic braces.
6787 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6788 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006789 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790 * Return a pointer to just after the name. Equal to "arg" if there is no
6791 * valid name.
6792 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006794find_name_end(
6795 char_u *arg,
6796 char_u **expr_start,
6797 char_u **expr_end,
6798 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800 int mb_nest = 0;
6801 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006803 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006804 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006808 *expr_start = NULL;
6809 *expr_end = NULL;
6810 }
6811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006812 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006813 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006814 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006815 return arg;
6816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 for (p = arg; *p != NUL
6818 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006819 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006820 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006821 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006823 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006824 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006825 if (*p == '\'')
6826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006827 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006828 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006829 ;
6830 if (*p == NUL)
6831 break;
6832 }
6833 else if (*p == '"')
6834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006835 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006836 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006837 if (*p == '\\' && p[1] != NUL)
6838 ++p;
6839 if (*p == NUL)
6840 break;
6841 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006842 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006844 // "s:" is start of "s:var", but "n:" is not and can be used in
6845 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006846 len = (int)(p - arg);
6847 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006848 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006849 break;
6850 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006854 if (*p == '[')
6855 ++br_nest;
6856 else if (*p == ']')
6857 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006859
zeertzjqa93d9cd2023-05-02 16:25:47 +01006860 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 if (*p == '{')
6863 {
6864 mb_nest++;
6865 if (expr_start != NULL && *expr_start == NULL)
6866 *expr_start = p;
6867 }
6868 else if (*p == '}')
6869 {
6870 mb_nest--;
6871 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6872 *expr_end = p;
6873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
6877 return p;
6878}
6879
6880/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006881 * Expands out the 'magic' {}'s in a variable/function name.
6882 * Note that this can call itself recursively, to deal with
6883 * constructs like foo{bar}{baz}{bam}
6884 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6885 * "in_start" ^
6886 * "expr_start" ^
6887 * "expr_end" ^
6888 * "in_end" ^
6889 *
6890 * Returns a new allocated string, which the caller must free.
6891 * Returns NULL for failure.
6892 */
6893 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006894make_expanded_name(
6895 char_u *in_start,
6896 char_u *expr_start,
6897 char_u *expr_end,
6898 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006899{
6900 char_u c1;
6901 char_u *retval = NULL;
6902 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006903
6904 if (expr_end == NULL || in_end == NULL)
6905 return NULL;
6906 *expr_start = NUL;
6907 *expr_end = NUL;
6908 c1 = *in_end;
6909 *in_end = NUL;
6910
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006911 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006912 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006913 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006914 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6915 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006916 if (retval != NULL)
6917 {
6918 STRCPY(retval, in_start);
6919 STRCAT(retval, temp_result);
6920 STRCAT(retval, expr_end + 1);
6921 }
6922 }
6923 vim_free(temp_result);
6924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006925 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006926 *expr_start = '{';
6927 *expr_end = '}';
6928
6929 if (retval != NULL)
6930 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006931 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006932 if (expr_start != NULL)
6933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006934 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006935 temp_result = make_expanded_name(retval, expr_start,
6936 expr_end, temp_result);
6937 vim_free(retval);
6938 retval = temp_result;
6939 }
6940 }
6941
6942 return retval;
6943}
6944
6945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006952 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006953}
6954
6955/*
6956 * Return TRUE if character "c" can be used as the first character in a
6957 * variable or function name (excluding '{' and '}').
6958 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006961{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006962 return ASCII_ISALPHA(c) || c == '_';
6963}
6964
6965/*
6966 * Return TRUE if character "c" can be used as the first character of a
6967 * dictionary key.
6968 */
6969 int
6970eval_isdictc(int c)
6971{
6972 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973}
6974
6975/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006976 * Handle:
6977 * - expr[expr], expr[expr:expr] subscript
6978 * - ".name" lookup
6979 * - function call with Funcref variable: func(expr)
6980 * - method call: var->method()
6981 *
6982 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006983 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006984 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986handle_subscript(
6987 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006988 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006990 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006991 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006992{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006993 int evaluate = evalarg != NULL
6994 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006995 int ret = OK;
6996 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006997 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006998 int getnext;
6999 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007000
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007001 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007002 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007003 // When at the end of the line and ".name" or "->{" or "->X" follows in
7004 // the next line then consume the line break.
7005 p = eval_next_non_blank(*arg, evalarg, &getnext);
7006 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007007 && ((*p == '.'
7008 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7009 || rettv->v_type == VAR_CLASS
7010 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007011 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7012 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7013 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007014 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007015 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007016 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007017 check_white = FALSE;
7018 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007019
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007020 if (rettv->v_type == VAR_ANY)
7021 {
7022 char_u *exp_name;
7023 int cc;
7024 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007025 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007026 type_T *type;
7027
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007028 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007029 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007030 if (**arg != '.')
7031 {
7032 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007033 semsg(_(e_expected_dot_after_name_str),
7034 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007035 ret = FAIL;
7036 break;
7037 }
7038 ++*arg;
7039 if (IS_WHITE_OR_NUL(**arg))
7040 {
7041 if (verbose)
7042 emsg(_(e_no_white_space_allowed_after_dot));
7043 ret = FAIL;
7044 break;
7045 }
7046
7047 // isolate the name
7048 exp_name = *arg;
7049 while (eval_isnamec(**arg))
7050 ++*arg;
7051 cc = **arg;
7052 **arg = NUL;
7053
7054 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007055 evalarg == NULL ? NULL : evalarg->eval_cctx,
7056 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007057 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007058
7059 if (idx < 0 && ufunc == NULL)
7060 {
7061 ret = FAIL;
7062 break;
7063 }
7064 if (idx >= 0)
7065 {
7066 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7067 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7068
7069 copy_tv(sv->sv_tv, rettv);
7070 }
7071 else
7072 {
7073 rettv->v_type = VAR_FUNC;
7074 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7075 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007076 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007077 }
7078
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007079 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7080 || rettv->v_type == VAR_PARTIAL))
7081 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007082 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007083 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7084 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007085
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007086 // Stop the expression evaluation when immediately aborting on
7087 // error, or when an interrupt occurred or an exception was thrown
7088 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007089 if (aborting())
7090 {
7091 if (ret == OK)
7092 clear_tv(rettv);
7093 ret = FAIL;
7094 }
7095 dict_unref(selfdict);
7096 selfdict = NULL;
7097 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007098 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007099 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007100 if (in_vim9script())
7101 *arg = skipwhite(p + 2);
7102 else
7103 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007104 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007105 {
zeertzjqc481ad32023-03-11 16:18:51 +00007106 emsg(_(e_no_white_space_allowed_before_parenthesis));
7107 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007108 }
zeertzjqc481ad32023-03-11 16:18:51 +00007109 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7110 // expr->{lambda}() or expr->(lambda)()
7111 ret = eval_lambda(arg, rettv, evalarg, verbose);
7112 else
7113 // expr->name()
7114 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007115 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007116 // "." is ".name" lookup when we found a dict or when evaluating and
7117 // scriptversion is at least 2, where string concatenation is "..".
7118 else if (**arg == '['
7119 || (**arg == '.' && (rettv->v_type == VAR_DICT
7120 || (!evaluate
7121 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007122 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007123 {
7124 dict_unref(selfdict);
7125 if (rettv->v_type == VAR_DICT)
7126 {
7127 selfdict = rettv->vval.v_dict;
7128 if (selfdict != NULL)
7129 ++selfdict->dv_refcount;
7130 }
7131 else
7132 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007133 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007134 {
7135 clear_tv(rettv);
7136 ret = FAIL;
7137 }
7138 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007139 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7140 || rettv->v_type == VAR_OBJECT))
7141 {
7142 // class member: SomeClass.varname
7143 // class method: SomeClass.SomeMethod()
7144 // class constructor: SomeClass.new()
7145 // object member: someObject.varname
7146 // object method: someObject.SomeMethod()
7147 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7148 {
7149 clear_tv(rettv);
7150 ret = FAIL;
7151 }
7152 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007153 else
7154 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007155 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007157 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7158 // Don't do this when "Func" is already a partial that was bound
7159 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007160 if (selfdict != NULL
7161 && (rettv->v_type == VAR_FUNC
7162 || (rettv->v_type == VAR_PARTIAL
7163 && (rettv->vval.v_partial->pt_auto
7164 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007165 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007166
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007167 dict_unref(selfdict);
7168 return ret;
7169}
7170
7171/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172 * Make a copy of an item.
7173 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007174 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007175 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7176 * reference to an already copied list/dict can be used.
7177 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180item_copy(
7181 typval_T *from,
7182 typval_T *to,
7183 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007184 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186{
7187 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007192 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 }
7195 ++recurse;
7196
7197 switch (from->v_type)
7198 {
7199 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007200 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 case VAR_STRING:
7202 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007203 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007204 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007205 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007206 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007207 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007208 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007209 case VAR_CLASS:
7210 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007211 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 copy_tv(from, to);
7213 break;
7214 case VAR_LIST:
7215 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 if (from->vval.v_list == NULL)
7218 to->vval.v_list = NULL;
7219 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007221 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007222 to->vval.v_list = from->vval.v_list->lv_copylist;
7223 ++to->vval.v_list->lv_refcount;
7224 }
7225 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007226 to->vval.v_list = list_copy(from->vval.v_list,
7227 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007228 if (to->vval.v_list == NULL)
7229 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007231 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007233 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 case VAR_DICT:
7235 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007236 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 if (from->vval.v_dict == NULL)
7238 to->vval.v_dict = NULL;
7239 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007241 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7243 ++to->vval.v_dict->dv_refcount;
7244 }
7245 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007246 to->vval.v_dict = dict_copy(from->vval.v_dict,
7247 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 if (to->vval.v_dict == NULL)
7249 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007250 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007251 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007252 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007254 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007255 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 }
7257 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007258 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259}
7260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 void
7262echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7263{
7264 char_u *tofree;
7265 char_u numbuf[NUMBUFLEN];
7266 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7267
7268 if (*atstart)
7269 {
7270 *atstart = FALSE;
7271 // Call msg_start() after eval1(), evaluating the expression
7272 // may cause a message to appear.
7273 if (with_space)
7274 {
7275 // Mark the saved text as finishing the line, so that what
7276 // follows is displayed on a new line when scrolling back
7277 // at the more prompt.
7278 msg_sb_eol();
7279 msg_start();
7280 }
7281 }
7282 else if (with_space)
7283 msg_puts_attr(" ", echo_attr);
7284
7285 if (p != NULL)
7286 for ( ; *p != NUL && !got_int; ++p)
7287 {
7288 if (*p == '\n' || *p == '\r' || *p == TAB)
7289 {
7290 if (*p != TAB && *needclr)
7291 {
7292 // remove any text still there from the command
7293 msg_clr_eos();
7294 *needclr = FALSE;
7295 }
7296 msg_putchar_attr(*p, echo_attr);
7297 }
7298 else
7299 {
7300 if (has_mbyte)
7301 {
7302 int i = (*mb_ptr2len)(p);
7303
7304 (void)msg_outtrans_len_attr(p, i, echo_attr);
7305 p += i - 1;
7306 }
7307 else
7308 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7309 }
7310 }
7311 vim_free(tofree);
7312}
7313
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 * ":echo expr1 ..." print each argument separated with a space, add a
7316 * newline at the end.
7317 * ":echon expr1 ..." print each argument plain.
7318 */
7319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007324 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 int needclr = TRUE;
7326 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007327 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007328 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007329 evalarg_T evalarg;
7330
Bram Moolenaare707c882020-07-01 13:07:37 +02007331 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
7333 if (eap->skip)
7334 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007335 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007337 // If eval1() causes an error message the text from the command may
7338 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007339 need_clr_eos = needclr;
7340
Bram Moolenaar68db9962021-05-09 23:19:22 +02007341 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007342 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
7344 /*
7345 * Report the invalid expression unless the expression evaluation
7346 * has been cancelled due to an aborting error, an interrupt, or an
7347 * exception.
7348 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007349 if (!aborting() && did_emsg == did_emsg_before
7350 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007351 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007352 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 break;
7354 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007355 need_clr_eos = FALSE;
7356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007358 {
7359 if (rettv.v_type == VAR_VOID)
7360 {
7361 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7362 break;
7363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007365 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 arg = skipwhite(arg);
7369 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007370 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007371 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 if (eap->skip)
7374 --emsg_skip;
7375 else
7376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007377 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (needclr)
7379 msg_clr_eos();
7380 if (eap->cmdidx == CMD_echo)
7381 msg_end();
7382 }
7383}
7384
7385/*
7386 * ":echohl {name}".
7387 */
7388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007391 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393
7394/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007395 * Returns the :echo attribute
7396 */
7397 int
7398get_echo_attr(void)
7399{
7400 return echo_attr;
7401}
7402
7403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 * ":execute expr1 ..." execute the result of an expression.
7405 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007406 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007408 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 * Each gets spaces around each argument and a newline at the end for
7410 * echo commands
7411 */
7412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
7415 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 int ret = OK;
7418 char_u *p;
7419 garray_T ga;
7420 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007421 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 ga_init2(&ga, 1, 80);
7424
7425 if (eap->skip)
7426 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007427 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007429 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007430 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433 if (!eap->skip)
7434 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007435 char_u buf[NUMBUFLEN];
7436
7437 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007438 {
7439 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7440 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007441 semsg(_(e_using_invalid_value_as_string_str),
7442 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007443 p = NULL;
7444 }
7445 else
7446 p = tv_get_string_buf(&rettv, buf);
7447 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007448 else
7449 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007450 if (p == NULL)
7451 {
7452 clear_tv(&rettv);
7453 ret = FAIL;
7454 break;
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 len = (int)STRLEN(p);
7457 if (ga_grow(&ga, len + 2) == FAIL)
7458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007459 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 ret = FAIL;
7461 break;
7462 }
7463 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 ga.ga_len += len;
7467 }
7468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 arg = skipwhite(arg);
7471 }
7472
7473 if (ret != FAIL && ga.ga_data != NULL)
7474 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007475 // use the first line of continuation lines for messages
7476 SOURCING_LNUM = start_lnum;
7477
Bram Moolenaar37fef162022-08-29 18:16:32 +01007478 if (eap->cmdidx == CMD_echomsg
7479 || eap->cmdidx == CMD_echowindow
7480 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007482 // Mark the already saved text as finishing the line, so that what
7483 // follows is displayed on a new line when scrolling back at the
7484 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007485 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007486 }
7487
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007488 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007489 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007490 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007491 out_flush();
7492 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007493 else if (eap->cmdidx == CMD_echowindow)
7494 {
7495#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007496 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007497#endif
7498 msg_attr(ga.ga_data, echo_attr);
7499#ifdef HAS_MESSAGE_WINDOW
7500 end_echowindow();
7501#endif
7502 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007503 else if (eap->cmdidx == CMD_echoconsole)
7504 {
7505 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7506 ui_write((char_u *)"\r\n", 2, TRUE);
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 else if (eap->cmdidx == CMD_echoerr)
7509 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007510 int save_did_emsg = did_emsg;
7511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007512 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007513 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (!force_abort)
7515 did_emsg = save_did_emsg;
7516 }
7517 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007518 {
7519 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7520
7521 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7522 sticky_cmdmod_flags = cmdmod.cmod_flags
7523 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007525 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007526 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529
7530 ga_clear(&ga);
7531
7532 if (eap->skip)
7533 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007534 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
7537/*
7538 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7539 * "arg" points to the "&" or '+' when called, to "option" when returning.
7540 * Returns NULL when no option name found. Otherwise pointer to the char
7541 * after the option name.
7542 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007543 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007544find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 char_u *p = *arg;
7547
7548 ++p;
7549 if (*p == 'g' && p[1] == ':')
7550 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007551 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 p += 2;
7553 }
7554 else if (*p == 'l' && p[1] == ':')
7555 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007556 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 p += 2;
7558 }
7559 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007560 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
7562 if (!ASCII_ISALPHA(*p))
7563 return NULL;
7564 *arg = p;
7565
7566 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007567 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 else
7569 while (ASCII_ISALPHA(*p))
7570 ++p;
7571 return p;
7572}
7573
7574/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007575 * Display script name where an item was last set.
7576 * Should only be invoked when 'verbose' is non-zero.
7577 */
7578 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007579last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007580{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007581 char_u *p;
7582
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007583 if (script_ctx.sc_sid == 0)
7584 return;
7585
7586 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7587 if (p == NULL)
7588 return;
7589
7590 verbose_enter();
7591 msg_puts(_("\n\tLast set from "));
7592 msg_puts((char *)p);
7593 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007594 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007595 msg_puts(_(line_msg));
7596 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007597 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007598 verbose_leave();
7599 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007600}
7601
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007602#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
7604/*
7605 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007606 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 * "flags" can be "g" to do a global substitute.
7608 * Returns an allocated string, NULL for error.
7609 */
7610 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007611do_string_sub(
7612 char_u *str,
7613 char_u *pat,
7614 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007615 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007616 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 int sublen;
7619 regmatch_T regmatch;
7620 int i;
7621 int do_all;
7622 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007623 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 garray_T ga;
7625 char_u *ret;
7626 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007627 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007629 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007631 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633 ga_init2(&ga, 1, 200);
7634
7635 do_all = (flags[0] == 'g');
7636
7637 regmatch.rm_ic = p_ic;
7638 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7639 if (regmatch.regprog != NULL)
7640 {
7641 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007642 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7644 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007645 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007646 if (regmatch.startp[0] == regmatch.endp[0])
7647 {
7648 if (zero_width == regmatch.startp[0])
7649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007650 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007651 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007652 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7653 (size_t)i);
7654 ga.ga_len += i;
7655 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007656 continue;
7657 }
7658 zero_width = regmatch.startp[0];
7659 }
7660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 /*
7662 * Get some space for a temporary buffer to do the substitution
7663 * into. It will contain:
7664 * - The text up to where the match is.
7665 * - The substituted text.
7666 * - The text after the match.
7667 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007668 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007669 if (sublen <= 0)
7670 {
7671 ga_clear(&ga);
7672 break;
7673 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007674 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7676 {
7677 ga_clear(&ga);
7678 break;
7679 }
7680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007681 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 i = (int)(regmatch.startp[0] - tail);
7683 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007684 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007685 (void)vim_regsub(&regmatch, sub, expr,
7686 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7687 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007689 tail = regmatch.endp[0];
7690 if (*tail == NUL)
7691 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (!do_all)
7693 break;
7694 }
7695
7696 if (ga.ga_data != NULL)
7697 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7698
Bram Moolenaar473de612013-06-08 18:19:48 +02007699 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701
7702 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7703 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007704 if (p_cpo == empty_option)
7705 p_cpo = save_cpo;
7706 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007708 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007709 // If it's still empty it was changed and restored, need to restore in
7710 // the complicated way.
7711 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007712 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007713 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
7716 return ret;
7717}