xref: /vim-8.2.3635/src/eval.c (revision 2d5f385c)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
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  */
13 #define USING_FLOAT_STUFF
14 
15 #include "vim.h"
16 
17 #if defined(FEAT_EVAL) || defined(PROTO)
18 
19 #ifdef VMS
20 # include <float.h>
21 #endif
22 
23 #define NAMESPACE_CHAR	(char_u *)"abglstvw"
24 
25 /*
26  * When recursively copying lists and dicts we need to remember which ones we
27  * have done to avoid endless recursiveness.  This unique ID is used for that.
28  * The last bit is used for previous_funccal, ignored when comparing.
29  */
30 static int current_copyID = 0;
31 
32 /*
33  * Info used by a ":for" loop.
34  */
35 typedef struct
36 {
37     int		fi_semicolon;	// TRUE if ending in '; var]'
38     int		fi_varcount;	// nr of variables in the list
39     int		fi_break_count;	// nr of line breaks encountered
40     listwatch_T	fi_lw;		// keep an eye on the item used.
41     list_T	*fi_list;	// list being used
42     int		fi_bi;		// index of blob
43     blob_T	*fi_blob;	// blob being used
44     char_u	*fi_string;	// copy of string being used
45     int		fi_byte_idx;	// byte index in fi_string
46 } forinfo_T;
47 
48 static int tv_op(typval_T *tv1, typval_T *tv2, char_u  *op);
49 static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50 static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51 static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52 static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53 static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54 static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55 static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
56 
57 static int free_unref_items(int copyID);
58 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
59 
60 /*
61  * Return "n1" divided by "n2", taking care of dividing by zero.
62  * If "failed" is not NULL set it to TRUE when dividing by zero fails.
63  */
64 	varnumber_T
65 num_divide(varnumber_T n1, varnumber_T n2, int *failed)
66 {
67     varnumber_T	result;
68 
69     if (n2 == 0)
70     {
71 	if (in_vim9script())
72 	{
73 	    emsg(_(e_divide_by_zero));
74 	    if (failed != NULL)
75 		*failed = TRUE;
76 	}
77 	if (n1 == 0)
78 	    result = VARNUM_MIN; // similar to NaN
79 	else if (n1 < 0)
80 	    result = -VARNUM_MAX;
81 	else
82 	    result = VARNUM_MAX;
83     }
84     else
85 	result = n1 / n2;
86 
87     return result;
88 }
89 
90 /*
91  * Return "n1" modulus "n2", taking care of dividing by zero.
92  * If "failed" is not NULL set it to TRUE when dividing by zero fails.
93  */
94 	varnumber_T
95 num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
96 {
97     if (n2 == 0 && in_vim9script())
98     {
99 	emsg(_(e_divide_by_zero));
100 	if (failed != NULL)
101 	    *failed = TRUE;
102     }
103     return (n2 == 0) ? 0 : (n1 % n2);
104 }
105 
106 #if defined(EBCDIC) || defined(PROTO)
107 /*
108  * Compare struct fst by function name.
109  */
110     static int
111 compare_func_name(const void *s1, const void *s2)
112 {
113     struct fst *p1 = (struct fst *)s1;
114     struct fst *p2 = (struct fst *)s2;
115 
116     return STRCMP(p1->f_name, p2->f_name);
117 }
118 
119 /*
120  * Sort the function table by function name.
121  * The sorting of the table above is ASCII dependent.
122  * On machines using EBCDIC we have to sort it.
123  */
124     static void
125 sortFunctions(void)
126 {
127     int		funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
128 
129     qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
130 }
131 #endif
132 
133 /*
134  * Initialize the global and v: variables.
135  */
136     void
137 eval_init(void)
138 {
139     evalvars_init();
140     func_init();
141 
142 #ifdef EBCDIC
143     /*
144      * Sort the function table, to enable binary search.
145      */
146     sortFunctions();
147 #endif
148 }
149 
150 #if defined(EXITFREE) || defined(PROTO)
151     void
152 eval_clear(void)
153 {
154     evalvars_clear();
155     free_scriptnames();  // must come after evalvars_clear().
156     free_locales();
157 
158     // autoloaded script names
159     free_autoload_scriptnames();
160 
161     // unreferenced lists and dicts
162     (void)garbage_collect(FALSE);
163 
164     // functions not garbage collected
165     free_all_functions();
166 }
167 #endif
168 
169     void
170 fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
171 {
172     CLEAR_FIELD(*evalarg);
173     evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
174     if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
175     {
176 	evalarg->eval_getline = eap->getline;
177 	evalarg->eval_cookie = eap->cookie;
178     }
179 }
180 
181 /*
182  * Top level evaluation function, returning a boolean.
183  * Sets "error" to TRUE if there was an error.
184  * Return TRUE or FALSE.
185  */
186     int
187 eval_to_bool(
188     char_u	*arg,
189     int		*error,
190     exarg_T	*eap,
191     int		skip)	    // only parse, don't execute
192 {
193     typval_T	tv;
194     varnumber_T	retval = FALSE;
195     evalarg_T	evalarg;
196 
197     fill_evalarg_from_eap(&evalarg, eap, skip);
198 
199     if (skip)
200 	++emsg_skip;
201     if (eval0(arg, &tv, eap, &evalarg) == FAIL)
202 	*error = TRUE;
203     else
204     {
205 	*error = FALSE;
206 	if (!skip)
207 	{
208 	    if (in_vim9script())
209 		retval = tv_get_bool_chk(&tv, error);
210 	    else
211 		retval = (tv_get_number_chk(&tv, error) != 0);
212 	    clear_tv(&tv);
213 	}
214     }
215     if (skip)
216 	--emsg_skip;
217     clear_evalarg(&evalarg, eap);
218 
219     return (int)retval;
220 }
221 
222 /*
223  * Call eval1() and give an error message if not done at a lower level.
224  */
225     static int
226 eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
227 {
228     char_u	*start = *arg;
229     int		ret;
230     int		did_emsg_before = did_emsg;
231     int		called_emsg_before = called_emsg;
232     evalarg_T	evalarg;
233 
234     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
235 
236     ret = eval1(arg, rettv, &evalarg);
237     if (ret == FAIL)
238     {
239 	// Report the invalid expression unless the expression evaluation has
240 	// been cancelled due to an aborting error, an interrupt, or an
241 	// exception, or we already gave a more specific error.
242 	// Also check called_emsg for when using assert_fails().
243 	if (!aborting() && did_emsg == did_emsg_before
244 					  && called_emsg == called_emsg_before)
245 	    semsg(_(e_invexpr2), start);
246     }
247     clear_evalarg(&evalarg, eap);
248     return ret;
249 }
250 
251 /*
252  * Return whether a typval is a valid expression to pass to eval_expr_typval()
253  * or eval_expr_to_bool().  An empty string returns FALSE;
254  */
255     int
256 eval_expr_valid_arg(typval_T *tv)
257 {
258     return tv->v_type != VAR_UNKNOWN
259 	    && (tv->v_type != VAR_STRING
260 		  || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
261 }
262 
263 /*
264  * Evaluate an expression, which can be a function, partial or string.
265  * Pass arguments "argv[argc]".
266  * Return the result in "rettv" and OK or FAIL.
267  */
268     int
269 eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
270 {
271     char_u	*s;
272     char_u	buf[NUMBUFLEN];
273     funcexe_T	funcexe;
274 
275     if (expr->v_type == VAR_FUNC)
276     {
277 	s = expr->vval.v_string;
278 	if (s == NULL || *s == NUL)
279 	    return FAIL;
280 	CLEAR_FIELD(funcexe);
281 	funcexe.evaluate = TRUE;
282 	if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
283 	    return FAIL;
284     }
285     else if (expr->v_type == VAR_PARTIAL)
286     {
287 	partial_T   *partial = expr->vval.v_partial;
288 
289 	if (partial == NULL)
290 	    return FAIL;
291 
292 	if (partial->pt_func != NULL
293 			  && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
294 	{
295 	    if (call_def_function(partial->pt_func, argc, argv,
296 						       partial, rettv) == FAIL)
297 		return FAIL;
298 	}
299 	else
300 	{
301 	    s = partial_name(partial);
302 	    if (s == NULL || *s == NUL)
303 		return FAIL;
304 	    CLEAR_FIELD(funcexe);
305 	    funcexe.evaluate = TRUE;
306 	    funcexe.partial = partial;
307 	    if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
308 		return FAIL;
309 	}
310     }
311     else
312     {
313 	s = tv_get_string_buf_chk(expr, buf);
314 	if (s == NULL)
315 	    return FAIL;
316 	s = skipwhite(s);
317 	if (eval1_emsg(&s, rettv, NULL) == FAIL)
318 	    return FAIL;
319 	if (*skipwhite(s) != NUL)  // check for trailing chars after expr
320 	{
321 	    clear_tv(rettv);
322 	    semsg(_(e_invexpr2), s);
323 	    return FAIL;
324 	}
325     }
326     return OK;
327 }
328 
329 /*
330  * Like eval_to_bool() but using a typval_T instead of a string.
331  * Works for string, funcref and partial.
332  */
333     int
334 eval_expr_to_bool(typval_T *expr, int *error)
335 {
336     typval_T	rettv;
337     int		res;
338 
339     if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
340     {
341 	*error = TRUE;
342 	return FALSE;
343     }
344     res = (tv_get_bool_chk(&rettv, error) != 0);
345     clear_tv(&rettv);
346     return res;
347 }
348 
349 /*
350  * Top level evaluation function, returning a string.  If "skip" is TRUE,
351  * only parsing to "nextcmd" is done, without reporting errors.  Return
352  * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
353  */
354     char_u *
355 eval_to_string_skip(
356     char_u	*arg,
357     exarg_T	*eap,
358     int		skip)	    // only parse, don't execute
359 {
360     typval_T	tv;
361     char_u	*retval;
362     evalarg_T	evalarg;
363 
364     fill_evalarg_from_eap(&evalarg, eap, skip);
365     if (skip)
366 	++emsg_skip;
367     if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
368 	retval = NULL;
369     else
370     {
371 	retval = vim_strsave(tv_get_string(&tv));
372 	clear_tv(&tv);
373     }
374     if (skip)
375 	--emsg_skip;
376     clear_evalarg(&evalarg, eap);
377 
378     return retval;
379 }
380 
381 /*
382  * Skip over an expression at "*pp".
383  * Return FAIL for an error, OK otherwise.
384  */
385     int
386 skip_expr(char_u **pp, evalarg_T *evalarg)
387 {
388     typval_T	rettv;
389 
390     *pp = skipwhite(*pp);
391     return eval1(pp, &rettv, evalarg);
392 }
393 
394 /*
395  * Skip over an expression at "*pp".
396  * If in Vim9 script and line breaks are encountered, the lines are
397  * concatenated.  "evalarg->eval_tofree" will be set accordingly.
398  * "arg" is advanced to just after the expression.
399  * "start" is set to the start of the expression, "end" to just after the end.
400  * Also when the expression is copied to allocated memory.
401  * Return FAIL for an error, OK otherwise.
402  */
403     int
404 skip_expr_concatenate(
405 	char_u	    **arg,
406 	char_u	    **start,
407 	char_u	    **end,
408 	evalarg_T   *evalarg)
409 {
410     typval_T	rettv;
411     int		res;
412     int		vim9script = in_vim9script();
413     garray_T    *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
414     int		save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
415     int		evaluate = evalarg == NULL
416 			       ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
417 
418     if (vim9script && evaluate
419 	       && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
420     {
421 	ga_init2(gap, sizeof(char_u *), 10);
422 	// leave room for "start"
423 	if (ga_grow(gap, 1) == OK)
424 	    ++gap->ga_len;
425     }
426     *start = *arg;
427 
428     // Don't evaluate the expression.
429     if (evalarg != NULL)
430 	evalarg->eval_flags &= ~EVAL_EVALUATE;
431     *arg = skipwhite(*arg);
432     res = eval1(arg, &rettv, evalarg);
433     *end = *arg;
434     if (evalarg != NULL)
435 	evalarg->eval_flags = save_flags;
436 
437     if (vim9script && evaluate
438 	    && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
439     {
440 	if (evalarg->eval_ga.ga_len == 1)
441 	{
442 	    // just one line, no need to concatenate
443 	    ga_clear(gap);
444 	    gap->ga_itemsize = 0;
445 	}
446 	else
447 	{
448 	    char_u	    *p;
449 	    size_t	    endoff = STRLEN(*arg);
450 
451 	    // Line breaks encountered, concatenate all the lines.
452 	    *((char_u **)gap->ga_data) = *start;
453 	    p = ga_concat_strings(gap, "");
454 
455 	    // free the lines only when using getsourceline()
456 	    if (evalarg->eval_cookie != NULL)
457 	    {
458 		// Do not free the first line, the caller can still use it.
459 		*((char_u **)gap->ga_data) = NULL;
460 		// Do not free the last line, "arg" points into it, free it
461 		// later.
462 		vim_free(evalarg->eval_tofree);
463 		evalarg->eval_tofree =
464 				    ((char_u **)gap->ga_data)[gap->ga_len - 1];
465 		((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
466 		ga_clear_strings(gap);
467 	    }
468 	    else
469 		ga_clear(gap);
470 	    gap->ga_itemsize = 0;
471 	    if (p == NULL)
472 		return FAIL;
473 	    *start = p;
474 	    vim_free(evalarg->eval_tofree_lambda);
475 	    evalarg->eval_tofree_lambda = p;
476 	    // Compute "end" relative to the end.
477 	    *end = *start + STRLEN(*start) - endoff;
478 	}
479     }
480 
481     return res;
482 }
483 
484 /*
485  * Convert "tv" to a string.
486  * When "convert" is TRUE convert a List into a sequence of lines and convert
487  * a Float to a String.
488  * Returns an allocated string (NULL when out of memory).
489  */
490     char_u *
491 typval2string(typval_T *tv, int convert)
492 {
493     garray_T	ga;
494     char_u	*retval;
495 #ifdef FEAT_FLOAT
496     char_u	numbuf[NUMBUFLEN];
497 #endif
498 
499     if (convert && tv->v_type == VAR_LIST)
500     {
501 	ga_init2(&ga, (int)sizeof(char), 80);
502 	if (tv->vval.v_list != NULL)
503 	{
504 	    list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
505 	    if (tv->vval.v_list->lv_len > 0)
506 		ga_append(&ga, NL);
507 	}
508 	ga_append(&ga, NUL);
509 	retval = (char_u *)ga.ga_data;
510     }
511 #ifdef FEAT_FLOAT
512     else if (convert && tv->v_type == VAR_FLOAT)
513     {
514 	vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
515 	retval = vim_strsave(numbuf);
516     }
517 #endif
518     else
519 	retval = vim_strsave(tv_get_string(tv));
520     return retval;
521 }
522 
523 /*
524  * Top level evaluation function, returning a string.  Does not handle line
525  * breaks.
526  * When "convert" is TRUE convert a List into a sequence of lines and convert
527  * a Float to a String.
528  * Return pointer to allocated memory, or NULL for failure.
529  */
530     char_u *
531 eval_to_string_eap(
532     char_u	*arg,
533     int		convert,
534     exarg_T	*eap)
535 {
536     typval_T	tv;
537     char_u	*retval;
538     evalarg_T	evalarg;
539 
540     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
541     if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
542 	retval = NULL;
543     else
544     {
545 	retval = typval2string(&tv, convert);
546 	clear_tv(&tv);
547     }
548     clear_evalarg(&evalarg, NULL);
549 
550     return retval;
551 }
552 
553     char_u *
554 eval_to_string(
555     char_u	*arg,
556     int		convert)
557 {
558     return eval_to_string_eap(arg, convert, NULL);
559 }
560 
561 /*
562  * Call eval_to_string() without using current local variables and using
563  * textwinlock.  When "use_sandbox" is TRUE use the sandbox.
564  * Use legacy Vim script syntax.
565  */
566     char_u *
567 eval_to_string_safe(
568     char_u	*arg,
569     int		use_sandbox)
570 {
571     char_u	*retval;
572     funccal_entry_T funccal_entry;
573     int		save_sc_version = current_sctx.sc_version;
574 
575     current_sctx.sc_version = 1;
576     save_funccal(&funccal_entry);
577     if (use_sandbox)
578 	++sandbox;
579     ++textwinlock;
580     retval = eval_to_string(arg, FALSE);
581     if (use_sandbox)
582 	--sandbox;
583     --textwinlock;
584     restore_funccal();
585     current_sctx.sc_version = save_sc_version;
586     return retval;
587 }
588 
589 /*
590  * Top level evaluation function, returning a number.
591  * Evaluates "expr" silently.
592  * Returns -1 for an error.
593  */
594     varnumber_T
595 eval_to_number(char_u *expr)
596 {
597     typval_T	rettv;
598     varnumber_T	retval;
599     char_u	*p = skipwhite(expr);
600 
601     ++emsg_off;
602 
603     if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
604 	retval = -1;
605     else
606     {
607 	retval = tv_get_number_chk(&rettv, NULL);
608 	clear_tv(&rettv);
609     }
610     --emsg_off;
611 
612     return retval;
613 }
614 
615 /*
616  * Top level evaluation function.
617  * Returns an allocated typval_T with the result.
618  * Returns NULL when there is an error.
619  */
620     typval_T *
621 eval_expr(char_u *arg, exarg_T *eap)
622 {
623     typval_T	*tv;
624     evalarg_T	evalarg;
625 
626     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
627 
628     tv = ALLOC_ONE(typval_T);
629     if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
630 	VIM_CLEAR(tv);
631 
632     clear_evalarg(&evalarg, eap);
633     return tv;
634 }
635 
636 /*
637  * Call some Vim script function and return the result in "*rettv".
638  * Uses argv[0] to argv[argc - 1] for the function arguments.  argv[argc]
639  * should have type VAR_UNKNOWN.
640  * Returns OK or FAIL.
641  */
642     int
643 call_vim_function(
644     char_u      *func,
645     int		argc,
646     typval_T	*argv,
647     typval_T	*rettv)
648 {
649     int		ret;
650     funcexe_T	funcexe;
651 
652     rettv->v_type = VAR_UNKNOWN;		// clear_tv() uses this
653     CLEAR_FIELD(funcexe);
654     funcexe.firstline = curwin->w_cursor.lnum;
655     funcexe.lastline = curwin->w_cursor.lnum;
656     funcexe.evaluate = TRUE;
657     ret = call_func(func, -1, rettv, argc, argv, &funcexe);
658     if (ret == FAIL)
659 	clear_tv(rettv);
660 
661     return ret;
662 }
663 
664 /*
665  * Call Vim script function "func" and return the result as a number.
666  * Returns -1 when calling the function fails.
667  * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
668  * have type VAR_UNKNOWN.
669  */
670     varnumber_T
671 call_func_retnr(
672     char_u      *func,
673     int		argc,
674     typval_T	*argv)
675 {
676     typval_T	rettv;
677     varnumber_T	retval;
678 
679     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
680 	return -1;
681 
682     retval = tv_get_number_chk(&rettv, NULL);
683     clear_tv(&rettv);
684     return retval;
685 }
686 
687 /*
688  * Call Vim script function like call_func_retnr() and drop the result.
689  * Returns FAIL when calling the function fails.
690  */
691     int
692 call_func_noret(
693     char_u      *func,
694     int		argc,
695     typval_T	*argv)
696 {
697     typval_T	rettv;
698 
699     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
700 	return FAIL;
701     clear_tv(&rettv);
702     return OK;
703 }
704 
705 /*
706  * Call Vim script function "func" and return the result as a string.
707  * Uses "argv" and "argc" as call_func_retnr().
708  * Returns NULL when calling the function fails.
709  */
710     void *
711 call_func_retstr(
712     char_u      *func,
713     int		argc,
714     typval_T	*argv)
715 {
716     typval_T	rettv;
717     char_u	*retval;
718 
719     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
720 	return NULL;
721 
722     retval = vim_strsave(tv_get_string(&rettv));
723     clear_tv(&rettv);
724     return retval;
725 }
726 
727 /*
728  * Call Vim script function "func" and return the result as a List.
729  * Uses "argv" and "argc" as call_func_retnr().
730  * Returns NULL when there is something wrong.
731  */
732     void *
733 call_func_retlist(
734     char_u      *func,
735     int		argc,
736     typval_T	*argv)
737 {
738     typval_T	rettv;
739 
740     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
741 	return NULL;
742 
743     if (rettv.v_type != VAR_LIST)
744     {
745 	clear_tv(&rettv);
746 	return NULL;
747     }
748 
749     return rettv.vval.v_list;
750 }
751 
752 #ifdef FEAT_FOLDING
753 /*
754  * Evaluate "arg", which is 'foldexpr'.
755  * Note: caller must set "curwin" to match "arg".
756  * Returns the foldlevel, and any character preceding it in "*cp".  Doesn't
757  * give error messages.
758  */
759     int
760 eval_foldexpr(char_u *arg, int *cp)
761 {
762     typval_T	tv;
763     varnumber_T	retval;
764     char_u	*s;
765     int		use_sandbox = was_set_insecurely((char_u *)"foldexpr",
766 								   OPT_LOCAL);
767 
768     ++emsg_off;
769     if (use_sandbox)
770 	++sandbox;
771     ++textwinlock;
772     *cp = NUL;
773     if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
774 	retval = 0;
775     else
776     {
777 	// If the result is a number, just return the number.
778 	if (tv.v_type == VAR_NUMBER)
779 	    retval = tv.vval.v_number;
780 	else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
781 	    retval = 0;
782 	else
783 	{
784 	    // If the result is a string, check if there is a non-digit before
785 	    // the number.
786 	    s = tv.vval.v_string;
787 	    if (!VIM_ISDIGIT(*s) && *s != '-')
788 		*cp = *s++;
789 	    retval = atol((char *)s);
790 	}
791 	clear_tv(&tv);
792     }
793     --emsg_off;
794     if (use_sandbox)
795 	--sandbox;
796     --textwinlock;
797     clear_evalarg(&EVALARG_EVALUATE, NULL);
798 
799     return (int)retval;
800 }
801 #endif
802 
803 /*
804  * Get an lval: variable, Dict item or List item that can be assigned a value
805  * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
806  * "name.key", "name.key[expr]" etc.
807  * Indexing only works if "name" is an existing List or Dictionary.
808  * "name" points to the start of the name.
809  * If "rettv" is not NULL it points to the value to be assigned.
810  * "unlet" is TRUE for ":unlet": slightly different behavior when something is
811  * wrong; must end in space or cmd separator.
812  *
813  * flags:
814  *  GLV_QUIET:       do not give error messages
815  *  GLV_READ_ONLY:   will not change the variable
816  *  GLV_NO_AUTOLOAD: do not use script autoloading
817  *
818  * Returns a pointer to just after the name, including indexes.
819  * When an evaluation error occurs "lp->ll_name" is NULL;
820  * Returns NULL for a parsing error.  Still need to free items in "lp"!
821  */
822     char_u *
823 get_lval(
824     char_u	*name,
825     typval_T	*rettv,
826     lval_T	*lp,
827     int		unlet,
828     int		skip,
829     int		flags,	    // GLV_ values
830     int		fne_flags)  // flags for find_name_end()
831 {
832     char_u	*p;
833     char_u	*expr_start, *expr_end;
834     int		cc;
835     dictitem_T	*v;
836     typval_T	var1;
837     typval_T	var2;
838     int		empty1 = FALSE;
839     listitem_T	*ni;
840     char_u	*key = NULL;
841     int		len;
842     hashtab_T	*ht = NULL;
843     int		quiet = flags & GLV_QUIET;
844     int		writing;
845 
846     // Clear everything in "lp".
847     CLEAR_POINTER(lp);
848 
849     if (skip || (flags & GLV_COMPILING))
850     {
851 	// When skipping or compiling just find the end of the name.
852 	lp->ll_name = name;
853 	lp->ll_name_end = find_name_end(name, NULL, NULL,
854 						      FNE_INCL_BR | fne_flags);
855 	return lp->ll_name_end;
856     }
857 
858     // Find the end of the name.
859     p = find_name_end(name, &expr_start, &expr_end, fne_flags);
860     lp->ll_name_end = p;
861     if (expr_start != NULL)
862     {
863 	// Don't expand the name when we already know there is an error.
864 	if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
865 						    && *p != '[' && *p != '.')
866 	{
867 	    semsg(_(e_trailing_arg), p);
868 	    return NULL;
869 	}
870 
871 	lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
872 	if (lp->ll_exp_name == NULL)
873 	{
874 	    // Report an invalid expression in braces, unless the
875 	    // expression evaluation has been cancelled due to an
876 	    // aborting error, an interrupt, or an exception.
877 	    if (!aborting() && !quiet)
878 	    {
879 		emsg_severe = TRUE;
880 		semsg(_(e_invarg2), name);
881 		return NULL;
882 	    }
883 	}
884 	lp->ll_name = lp->ll_exp_name;
885     }
886     else
887     {
888 	lp->ll_name = name;
889 
890 	if (in_vim9script())
891 	{
892 	    // "a: type" is declaring variable "a" with a type, not "a:".
893 	    if (p == name + 2 && p[-1] == ':')
894 	    {
895 		--p;
896 		lp->ll_name_end = p;
897 	    }
898 	    if (*p == ':')
899 	    {
900 		scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
901 		char_u	 *tp = skipwhite(p + 1);
902 
903 		// parse the type after the name
904 		lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
905 		if (lp->ll_type == NULL && !quiet)
906 		    return NULL;
907 		lp->ll_name_end = tp;
908 	    }
909 	}
910     }
911 
912     // Without [idx] or .key we are done.
913     if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
914 	return p;
915 
916     cc = *p;
917     *p = NUL;
918     // When we would write to the variable pass &ht and prevent autoload.
919     writing = !(flags & GLV_READ_ONLY);
920     v = find_var(lp->ll_name, writing ? &ht : NULL,
921 					 (flags & GLV_NO_AUTOLOAD) || writing);
922     if (v == NULL && !quiet)
923 	semsg(_(e_undefined_variable_str), lp->ll_name);
924     *p = cc;
925     if (v == NULL)
926 	return NULL;
927 
928     if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
929     {
930 	if (!quiet)
931 	    semsg(_(e_variable_already_declared), lp->ll_name);
932 	return NULL;
933     }
934 
935     /*
936      * Loop until no more [idx] or .key is following.
937      */
938     lp->ll_tv = &v->di_tv;
939     var1.v_type = VAR_UNKNOWN;
940     var2.v_type = VAR_UNKNOWN;
941     while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
942     {
943 	if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
944 		&& !(lp->ll_tv->v_type == VAR_DICT)
945 		&& !(lp->ll_tv->v_type == VAR_BLOB
946 					   && lp->ll_tv->vval.v_blob != NULL))
947 	{
948 	    if (!quiet)
949 		emsg(_("E689: Can only index a List, Dictionary or Blob"));
950 	    return NULL;
951 	}
952 	if (lp->ll_range)
953 	{
954 	    if (!quiet)
955 		emsg(_("E708: [:] must come last"));
956 	    return NULL;
957 	}
958 
959 	if (in_vim9script() && lp->ll_valtype == NULL
960 		&& lp->ll_tv == &v->di_tv
961 		&& ht != NULL && ht == get_script_local_ht())
962 	{
963 	    svar_T  *sv = find_typval_in_script(lp->ll_tv);
964 
965 	    // Vim9 script local variable: get the type
966 	    if (sv != NULL)
967 		lp->ll_valtype = sv->sv_type;
968 	}
969 
970 	len = -1;
971 	if (*p == '.')
972 	{
973 	    key = p + 1;
974 	    for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 		;
976 	    if (len == 0)
977 	    {
978 		if (!quiet)
979 		    emsg(_(e_emptykey));
980 		return NULL;
981 	    }
982 	    p = key + len;
983 	}
984 	else
985 	{
986 	    // Get the index [expr] or the first index [expr: ].
987 	    p = skipwhite(p + 1);
988 	    if (*p == ':')
989 		empty1 = TRUE;
990 	    else
991 	    {
992 		empty1 = FALSE;
993 		if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL)  // recursive!
994 		    return NULL;
995 		if (tv_get_string_chk(&var1) == NULL)
996 		{
997 		    // not a number or string
998 		    clear_tv(&var1);
999 		    return NULL;
1000 		}
1001 		p = skipwhite(p);
1002 	    }
1003 
1004 	    // Optionally get the second index [ :expr].
1005 	    if (*p == ':')
1006 	    {
1007 		if (lp->ll_tv->v_type == VAR_DICT)
1008 		{
1009 		    if (!quiet)
1010 			emsg(_(e_cannot_slice_dictionary));
1011 		    clear_tv(&var1);
1012 		    return NULL;
1013 		}
1014 		if (rettv != NULL
1015 			&& !(rettv->v_type == VAR_LIST
1016 						 && rettv->vval.v_list != NULL)
1017 			&& !(rettv->v_type == VAR_BLOB
1018 						&& rettv->vval.v_blob != NULL))
1019 		{
1020 		    if (!quiet)
1021 			emsg(_("E709: [:] requires a List or Blob value"));
1022 		    clear_tv(&var1);
1023 		    return NULL;
1024 		}
1025 		p = skipwhite(p + 1);
1026 		if (*p == ']')
1027 		    lp->ll_empty2 = TRUE;
1028 		else
1029 		{
1030 		    lp->ll_empty2 = FALSE;
1031 		    // recursive!
1032 		    if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
1033 		    {
1034 			clear_tv(&var1);
1035 			return NULL;
1036 		    }
1037 		    if (tv_get_string_chk(&var2) == NULL)
1038 		    {
1039 			// not a number or string
1040 			clear_tv(&var1);
1041 			clear_tv(&var2);
1042 			return NULL;
1043 		    }
1044 		}
1045 		lp->ll_range = TRUE;
1046 	    }
1047 	    else
1048 		lp->ll_range = FALSE;
1049 
1050 	    if (*p != ']')
1051 	    {
1052 		if (!quiet)
1053 		    emsg(_(e_missbrac));
1054 		clear_tv(&var1);
1055 		clear_tv(&var2);
1056 		return NULL;
1057 	    }
1058 
1059 	    // Skip to past ']'.
1060 	    ++p;
1061 	}
1062 
1063 	if (lp->ll_tv->v_type == VAR_DICT)
1064 	{
1065 	    if (len == -1)
1066 	    {
1067 		// "[key]": get key from "var1"
1068 		key = tv_get_string_chk(&var1);	// is number or string
1069 		if (key == NULL)
1070 		{
1071 		    clear_tv(&var1);
1072 		    return NULL;
1073 		}
1074 	    }
1075 	    lp->ll_list = NULL;
1076 
1077 	    // a NULL dict is equivalent with an empty dict
1078 	    if (lp->ll_tv->vval.v_dict == NULL)
1079 	    {
1080 		lp->ll_tv->vval.v_dict = dict_alloc();
1081 		if (lp->ll_tv->vval.v_dict == NULL)
1082 		{
1083 		    clear_tv(&var1);
1084 		    return NULL;
1085 		}
1086 		++lp->ll_tv->vval.v_dict->dv_refcount;
1087 	    }
1088 	    lp->ll_dict = lp->ll_tv->vval.v_dict;
1089 
1090 	    lp->ll_di = dict_find(lp->ll_dict, key, len);
1091 
1092 	    // When assigning to a scope dictionary check that a function and
1093 	    // variable name is valid (only variable name unless it is l: or
1094 	    // g: dictionary). Disallow overwriting a builtin function.
1095 	    if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1096 	    {
1097 		int prevval;
1098 		int wrong;
1099 
1100 		if (len != -1)
1101 		{
1102 		    prevval = key[len];
1103 		    key[len] = NUL;
1104 		}
1105 		else
1106 		    prevval = 0; // avoid compiler warning
1107 		wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 			       && rettv->v_type == VAR_FUNC
1109 			       && var_wrong_func_name(key, lp->ll_di == NULL))
1110 			|| !valid_varname(key, TRUE);
1111 		if (len != -1)
1112 		    key[len] = prevval;
1113 		if (wrong)
1114 		{
1115 		    clear_tv(&var1);
1116 		    return NULL;
1117 		}
1118 	    }
1119 
1120 	    if (lp->ll_valtype != NULL)
1121 		// use the type of the member
1122 		lp->ll_valtype = lp->ll_valtype->tt_member;
1123 
1124 	    if (lp->ll_di == NULL)
1125 	    {
1126 		// Can't add "v:" or "a:" variable.
1127 		if (lp->ll_dict == get_vimvar_dict()
1128 			 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1129 		{
1130 		    semsg(_(e_illvar), name);
1131 		    clear_tv(&var1);
1132 		    return NULL;
1133 		}
1134 
1135 		// Key does not exist in dict: may need to add it.
1136 		if (*p == '[' || *p == '.' || unlet)
1137 		{
1138 		    if (!quiet)
1139 			semsg(_(e_dictkey), key);
1140 		    clear_tv(&var1);
1141 		    return NULL;
1142 		}
1143 		if (len == -1)
1144 		    lp->ll_newkey = vim_strsave(key);
1145 		else
1146 		    lp->ll_newkey = vim_strnsave(key, len);
1147 		clear_tv(&var1);
1148 		if (lp->ll_newkey == NULL)
1149 		    p = NULL;
1150 		break;
1151 	    }
1152 	    // existing variable, need to check if it can be changed
1153 	    else if ((flags & GLV_READ_ONLY) == 0
1154 			&& (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 			  || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1156 	    {
1157 		clear_tv(&var1);
1158 		return NULL;
1159 	    }
1160 
1161 	    clear_tv(&var1);
1162 	    lp->ll_tv = &lp->ll_di->di_tv;
1163 	}
1164 	else if (lp->ll_tv->v_type == VAR_BLOB)
1165 	{
1166 	    long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167 
1168 	    /*
1169 	     * Get the number and item for the only or first index of the List.
1170 	     */
1171 	    if (empty1)
1172 		lp->ll_n1 = 0;
1173 	    else
1174 		// is number or string
1175 		lp->ll_n1 = (long)tv_get_number(&var1);
1176 	    clear_tv(&var1);
1177 
1178 	    if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
1179 	    {
1180 		clear_tv(&var2);
1181 		return NULL;
1182 	    }
1183 	    if (lp->ll_range && !lp->ll_empty2)
1184 	    {
1185 		lp->ll_n2 = (long)tv_get_number(&var2);
1186 		clear_tv(&var2);
1187 		if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 								       == FAIL)
1189 		    return NULL;
1190 	    }
1191 	    lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 	    lp->ll_tv = NULL;
1193 	    break;
1194 	}
1195 	else
1196 	{
1197 	    /*
1198 	     * Get the number and item for the only or first index of the List.
1199 	     */
1200 	    if (empty1)
1201 		lp->ll_n1 = 0;
1202 	    else
1203 		// is number or string
1204 		lp->ll_n1 = (long)tv_get_number(&var1);
1205 	    clear_tv(&var1);
1206 
1207 	    lp->ll_dict = NULL;
1208 	    lp->ll_list = lp->ll_tv->vval.v_list;
1209 	    lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1210 	    if (lp->ll_li == NULL)
1211 	    {
1212 		clear_tv(&var2);
1213 		if (!quiet)
1214 		    semsg(_(e_listidx), lp->ll_n1);
1215 		return NULL;
1216 	    }
1217 
1218 	    if (lp->ll_valtype != NULL)
1219 		// use the type of the member
1220 		lp->ll_valtype = lp->ll_valtype->tt_member;
1221 
1222 	    /*
1223 	     * May need to find the item or absolute index for the second
1224 	     * index of a range.
1225 	     * When no index given: "lp->ll_empty2" is TRUE.
1226 	     * Otherwise "lp->ll_n2" is set to the second index.
1227 	     */
1228 	    if (lp->ll_range && !lp->ll_empty2)
1229 	    {
1230 		lp->ll_n2 = (long)tv_get_number(&var2);
1231 						    // is number or string
1232 		clear_tv(&var2);
1233 		if (lp->ll_n2 < 0)
1234 		{
1235 		    ni = list_find(lp->ll_list, lp->ll_n2);
1236 		    if (ni == NULL)
1237 		    {
1238 			if (!quiet)
1239 			    semsg(_(e_listidx), lp->ll_n2);
1240 			return NULL;
1241 		    }
1242 		    lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
1243 		}
1244 
1245 		// Check that lp->ll_n2 isn't before lp->ll_n1.
1246 		if (lp->ll_n1 < 0)
1247 		    lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1248 		if (lp->ll_n2 < lp->ll_n1)
1249 		{
1250 		    if (!quiet)
1251 			semsg(_(e_listidx), lp->ll_n2);
1252 		    return NULL;
1253 		}
1254 	    }
1255 
1256 	    lp->ll_tv = &lp->ll_li->li_tv;
1257 	}
1258     }
1259 
1260     clear_tv(&var1);
1261     lp->ll_name_end = p;
1262     return p;
1263 }
1264 
1265 /*
1266  * Clear lval "lp" that was filled by get_lval().
1267  */
1268     void
1269 clear_lval(lval_T *lp)
1270 {
1271     vim_free(lp->ll_exp_name);
1272     vim_free(lp->ll_newkey);
1273 }
1274 
1275 /*
1276  * Set a variable that was parsed by get_lval() to "rettv".
1277  * "endp" points to just after the parsed name.
1278  * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1279  * "%" for "%=", "." for ".=" or "=" for "=".
1280  */
1281     void
1282 set_var_lval(
1283     lval_T	*lp,
1284     char_u	*endp,
1285     typval_T	*rettv,
1286     int		copy,
1287     int		flags,	    // ASSIGN_CONST, ASSIGN_NO_DECL
1288     char_u	*op,
1289     int		var_idx)    // index for "let [a, b] = list"
1290 {
1291     int		cc;
1292     listitem_T	*ri;
1293     dictitem_T	*di;
1294 
1295     if (lp->ll_tv == NULL)
1296     {
1297 	cc = *endp;
1298 	*endp = NUL;
1299 	if (lp->ll_blob != NULL)
1300 	{
1301 	    int	    error = FALSE, val;
1302 
1303 	    if (op != NULL && *op != '=')
1304 	    {
1305 		semsg(_(e_letwrong), op);
1306 		return;
1307 	    }
1308 	    if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
1309 		return;
1310 
1311 	    if (lp->ll_range && rettv->v_type == VAR_BLOB)
1312 	    {
1313 		if (lp->ll_empty2)
1314 		    lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1315 
1316 		if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1317 								rettv) == FAIL)
1318 		    return;
1319 	    }
1320 	    else
1321 	    {
1322 		val = (int)tv_get_number_chk(rettv, &error);
1323 		if (!error)
1324 		    blob_set_append(lp->ll_blob, lp->ll_n1, val);
1325 	    }
1326 	}
1327 	else if (op != NULL && *op != '=')
1328 	{
1329 	    typval_T tv;
1330 
1331 	    if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1332 					     && (flags & ASSIGN_FOR_LOOP) == 0)
1333 	    {
1334 		emsg(_(e_cannot_mod));
1335 		*endp = cc;
1336 		return;
1337 	    }
1338 
1339 	    // handle +=, -=, *=, /=, %= and .=
1340 	    di = NULL;
1341 	    if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
1342 					     &tv, &di, EVAL_VAR_VERBOSE) == OK)
1343 	    {
1344 		if ((di == NULL
1345 			 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1346 			   && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
1347 			&& tv_op(&tv, rettv, op) == OK)
1348 		    set_var(lp->ll_name, &tv, FALSE);
1349 		clear_tv(&tv);
1350 	    }
1351 	}
1352 	else
1353 	{
1354 	    if (lp->ll_type != NULL
1355 		       && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
1356 		return;
1357 	    set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1358 							       flags, var_idx);
1359 	}
1360 	*endp = cc;
1361     }
1362     else if (value_check_lock(lp->ll_newkey == NULL
1363 		? lp->ll_tv->v_lock
1364 		: lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
1365 	;
1366     else if (lp->ll_range)
1367     {
1368 	listitem_T *ll_li = lp->ll_li;
1369 	int	    ll_n1 = lp->ll_n1;
1370 
1371 	if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1372 					     && (flags & ASSIGN_FOR_LOOP) == 0)
1373 	{
1374 	    emsg(_("E996: Cannot lock a range"));
1375 	    return;
1376 	}
1377 
1378 	/*
1379 	 * Check whether any of the list items is locked
1380 	 */
1381 	for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
1382 	{
1383 	    if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1384 		return;
1385 	    ri = ri->li_next;
1386 	    if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1387 		break;
1388 	    ll_li = ll_li->li_next;
1389 	    ++ll_n1;
1390 	}
1391 
1392 	/*
1393 	 * Assign the List values to the list items.
1394 	 */
1395 	for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1396 	{
1397 	    if (op != NULL && *op != '=')
1398 		tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1399 	    else
1400 	    {
1401 		clear_tv(&lp->ll_li->li_tv);
1402 		copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1403 	    }
1404 	    ri = ri->li_next;
1405 	    if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1406 		break;
1407 	    if (lp->ll_li->li_next == NULL)
1408 	    {
1409 		// Need to add an empty item.
1410 		if (list_append_number(lp->ll_list, 0) == FAIL)
1411 		{
1412 		    ri = NULL;
1413 		    break;
1414 		}
1415 	    }
1416 	    lp->ll_li = lp->ll_li->li_next;
1417 	    ++lp->ll_n1;
1418 	}
1419 	if (ri != NULL)
1420 	    emsg(_(e_list_value_has_more_items_than_targets));
1421 	else if (lp->ll_empty2
1422 		? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
1423 		: lp->ll_n1 != lp->ll_n2)
1424 	    emsg(_(e_list_value_does_not_have_enough_items));
1425     }
1426     else
1427     {
1428 	/*
1429 	 * Assign to a List or Dictionary item.
1430 	 */
1431 	if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1432 					     && (flags & ASSIGN_FOR_LOOP) == 0)
1433 	{
1434 	    emsg(_("E996: Cannot lock a list or dict"));
1435 	    return;
1436 	}
1437 
1438 	if (lp->ll_valtype != NULL
1439 		    && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
1440 	    return;
1441 
1442 	if (lp->ll_newkey != NULL)
1443 	{
1444 	    if (op != NULL && *op != '=')
1445 	    {
1446 		semsg(_(e_dictkey), lp->ll_newkey);
1447 		return;
1448 	    }
1449 
1450 	    // Need to add an item to the Dictionary.
1451 	    di = dictitem_alloc(lp->ll_newkey);
1452 	    if (di == NULL)
1453 		return;
1454 	    if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1455 	    {
1456 		vim_free(di);
1457 		return;
1458 	    }
1459 	    lp->ll_tv = &di->di_tv;
1460 	}
1461 	else if (op != NULL && *op != '=')
1462 	{
1463 	    tv_op(lp->ll_tv, rettv, op);
1464 	    return;
1465 	}
1466 	else
1467 	    clear_tv(lp->ll_tv);
1468 
1469 	/*
1470 	 * Assign the value to the variable or list item.
1471 	 */
1472 	if (copy)
1473 	    copy_tv(rettv, lp->ll_tv);
1474 	else
1475 	{
1476 	    *lp->ll_tv = *rettv;
1477 	    lp->ll_tv->v_lock = 0;
1478 	    init_tv(rettv);
1479 	}
1480     }
1481 }
1482 
1483 /*
1484  * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1485  * and "tv1 .= tv2"
1486  * Returns OK or FAIL.
1487  */
1488     static int
1489 tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
1490 {
1491     varnumber_T	n;
1492     char_u	numbuf[NUMBUFLEN];
1493     char_u	*s;
1494     int		failed = FALSE;
1495 
1496     // Can't do anything with a Funcref, Dict, v:true on the right.
1497     if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
1498 		      && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1499     {
1500 	switch (tv1->v_type)
1501 	{
1502 	    case VAR_UNKNOWN:
1503 	    case VAR_ANY:
1504 	    case VAR_VOID:
1505 	    case VAR_DICT:
1506 	    case VAR_FUNC:
1507 	    case VAR_PARTIAL:
1508 	    case VAR_BOOL:
1509 	    case VAR_SPECIAL:
1510 	    case VAR_JOB:
1511 	    case VAR_CHANNEL:
1512 		break;
1513 
1514 	    case VAR_BLOB:
1515 		if (*op != '+' || tv2->v_type != VAR_BLOB)
1516 		    break;
1517 		// BLOB += BLOB
1518 		if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1519 		{
1520 		    blob_T  *b1 = tv1->vval.v_blob;
1521 		    blob_T  *b2 = tv2->vval.v_blob;
1522 		    int	i, len = blob_len(b2);
1523 		    for (i = 0; i < len; i++)
1524 			ga_append(&b1->bv_ga, blob_get(b2, i));
1525 		}
1526 		return OK;
1527 
1528 	    case VAR_LIST:
1529 		if (*op != '+' || tv2->v_type != VAR_LIST)
1530 		    break;
1531 		// List += List
1532 		if (tv2->vval.v_list != NULL)
1533 		{
1534 		    if (tv1->vval.v_list == NULL)
1535 		    {
1536 			tv1->vval.v_list = tv2->vval.v_list;
1537 			++tv1->vval.v_list->lv_refcount;
1538 		    }
1539 		    else
1540 			list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1541 		}
1542 		return OK;
1543 
1544 	    case VAR_NUMBER:
1545 	    case VAR_STRING:
1546 		if (tv2->v_type == VAR_LIST)
1547 		    break;
1548 		if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
1549 		{
1550 		    // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
1551 		    n = tv_get_number(tv1);
1552 #ifdef FEAT_FLOAT
1553 		    if (tv2->v_type == VAR_FLOAT)
1554 		    {
1555 			float_T f = n;
1556 
1557 			if (*op == '%')
1558 			    break;
1559 			switch (*op)
1560 			{
1561 			    case '+': f += tv2->vval.v_float; break;
1562 			    case '-': f -= tv2->vval.v_float; break;
1563 			    case '*': f *= tv2->vval.v_float; break;
1564 			    case '/': f /= tv2->vval.v_float; break;
1565 			}
1566 			clear_tv(tv1);
1567 			tv1->v_type = VAR_FLOAT;
1568 			tv1->vval.v_float = f;
1569 		    }
1570 		    else
1571 #endif
1572 		    {
1573 			switch (*op)
1574 			{
1575 			    case '+': n += tv_get_number(tv2); break;
1576 			    case '-': n -= tv_get_number(tv2); break;
1577 			    case '*': n *= tv_get_number(tv2); break;
1578 			    case '/': n = num_divide(n, tv_get_number(tv2),
1579 							       &failed); break;
1580 			    case '%': n = num_modulus(n, tv_get_number(tv2),
1581 							       &failed); break;
1582 			}
1583 			clear_tv(tv1);
1584 			tv1->v_type = VAR_NUMBER;
1585 			tv1->vval.v_number = n;
1586 		    }
1587 		}
1588 		else
1589 		{
1590 		    if (tv2->v_type == VAR_FLOAT)
1591 			break;
1592 
1593 		    // str .= str
1594 		    s = tv_get_string(tv1);
1595 		    s = concat_str(s, tv_get_string_buf(tv2, numbuf));
1596 		    clear_tv(tv1);
1597 		    tv1->v_type = VAR_STRING;
1598 		    tv1->vval.v_string = s;
1599 		}
1600 		return failed ? FAIL : OK;
1601 
1602 	    case VAR_FLOAT:
1603 #ifdef FEAT_FLOAT
1604 		{
1605 		    float_T f;
1606 
1607 		    if (*op == '%' || *op == '.'
1608 				   || (tv2->v_type != VAR_FLOAT
1609 				    && tv2->v_type != VAR_NUMBER
1610 				    && tv2->v_type != VAR_STRING))
1611 			break;
1612 		    if (tv2->v_type == VAR_FLOAT)
1613 			f = tv2->vval.v_float;
1614 		    else
1615 			f = tv_get_number(tv2);
1616 		    switch (*op)
1617 		    {
1618 			case '+': tv1->vval.v_float += f; break;
1619 			case '-': tv1->vval.v_float -= f; break;
1620 			case '*': tv1->vval.v_float *= f; break;
1621 			case '/': tv1->vval.v_float /= f; break;
1622 		    }
1623 		}
1624 #endif
1625 		return OK;
1626 	}
1627     }
1628 
1629     semsg(_(e_letwrong), op);
1630     return FAIL;
1631 }
1632 
1633 /*
1634  * Evaluate the expression used in a ":for var in expr" command.
1635  * "arg" points to "var".
1636  * Set "*errp" to TRUE for an error, FALSE otherwise;
1637  * Return a pointer that holds the info.  Null when there is an error.
1638  */
1639     void *
1640 eval_for_line(
1641     char_u	*arg,
1642     int		*errp,
1643     exarg_T	*eap,
1644     evalarg_T	*evalarg)
1645 {
1646     forinfo_T	*fi;
1647     char_u	*expr;
1648     typval_T	tv;
1649     list_T	*l;
1650     int		skip = !(evalarg->eval_flags & EVAL_EVALUATE);
1651 
1652     *errp = TRUE;	// default: there is an error
1653 
1654     fi = ALLOC_CLEAR_ONE(forinfo_T);
1655     if (fi == NULL)
1656 	return NULL;
1657 
1658     expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
1659     if (expr == NULL)
1660 	return fi;
1661 
1662     expr = skipwhite_and_linebreak(expr, evalarg);
1663     if (expr[0] != 'i' || expr[1] != 'n'
1664 				  || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
1665     {
1666 	emsg(_(e_missing_in));
1667 	return fi;
1668     }
1669 
1670     if (skip)
1671 	++emsg_skip;
1672     expr = skipwhite_and_linebreak(expr + 2, evalarg);
1673     if (eval0(expr, &tv, eap, evalarg) == OK)
1674     {
1675 	*errp = FALSE;
1676 	if (!skip)
1677 	{
1678 	    if (tv.v_type == VAR_LIST)
1679 	    {
1680 		l = tv.vval.v_list;
1681 		if (l == NULL)
1682 		{
1683 		    // a null list is like an empty list: do nothing
1684 		    clear_tv(&tv);
1685 		}
1686 		else
1687 		{
1688 		    // Need a real list here.
1689 		    CHECK_LIST_MATERIALIZE(l);
1690 
1691 		    // No need to increment the refcount, it's already set for
1692 		    // the list being used in "tv".
1693 		    fi->fi_list = l;
1694 		    list_add_watch(l, &fi->fi_lw);
1695 		    fi->fi_lw.lw_item = l->lv_first;
1696 		}
1697 	    }
1698 	    else if (tv.v_type == VAR_BLOB)
1699 	    {
1700 		fi->fi_bi = 0;
1701 		if (tv.vval.v_blob != NULL)
1702 		{
1703 		    typval_T btv;
1704 
1705 		    // Make a copy, so that the iteration still works when the
1706 		    // blob is changed.
1707 		    blob_copy(tv.vval.v_blob, &btv);
1708 		    fi->fi_blob = btv.vval.v_blob;
1709 		}
1710 		clear_tv(&tv);
1711 	    }
1712 	    else if (tv.v_type == VAR_STRING)
1713 	    {
1714 		fi->fi_byte_idx = 0;
1715 		fi->fi_string = tv.vval.v_string;
1716 		tv.vval.v_string = NULL;
1717 		if (fi->fi_string == NULL)
1718 		    fi->fi_string = vim_strsave((char_u *)"");
1719 	    }
1720 	    else
1721 	    {
1722 		emsg(_(e_listreq));
1723 		clear_tv(&tv);
1724 	    }
1725 	}
1726     }
1727     if (skip)
1728 	--emsg_skip;
1729     fi->fi_break_count = evalarg->eval_break_count;
1730 
1731     return fi;
1732 }
1733 
1734 /*
1735  * Used when looping over a :for line, skip the "in expr" part.
1736  */
1737     void
1738 skip_for_lines(void *fi_void, evalarg_T *evalarg)
1739 {
1740     forinfo_T	*fi = (forinfo_T *)fi_void;
1741     int		i;
1742 
1743     for (i = 0; i < fi->fi_break_count; ++i)
1744 	eval_next_line(evalarg);
1745 }
1746 
1747 /*
1748  * Use the first item in a ":for" list.  Advance to the next.
1749  * Assign the values to the variable (list).  "arg" points to the first one.
1750  * Return TRUE when a valid item was found, FALSE when at end of list or
1751  * something wrong.
1752  */
1753     int
1754 next_for_item(void *fi_void, char_u *arg)
1755 {
1756     forinfo_T	*fi = (forinfo_T *)fi_void;
1757     int		result;
1758     int		flag = ASSIGN_FOR_LOOP | (in_vim9script()
1759 			 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1760 			 : 0);
1761     listitem_T	*item;
1762 
1763     if (fi->fi_blob != NULL)
1764     {
1765 	typval_T	tv;
1766 
1767 	if (fi->fi_bi >= blob_len(fi->fi_blob))
1768 	    return FALSE;
1769 	tv.v_type = VAR_NUMBER;
1770 	tv.v_lock = VAR_FIXED;
1771 	tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1772 	++fi->fi_bi;
1773 	return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1774 					    fi->fi_varcount, flag, NULL) == OK;
1775     }
1776 
1777     if (fi->fi_string != NULL)
1778     {
1779 	typval_T	tv;
1780 	int		len;
1781 
1782 	len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1783 	if (len == 0)
1784 	    return FALSE;
1785 	tv.v_type = VAR_STRING;
1786 	tv.v_lock = VAR_FIXED;
1787 	tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1788 	fi->fi_byte_idx += len;
1789 	result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1790 					    fi->fi_varcount, flag, NULL) == OK;
1791 	vim_free(tv.vval.v_string);
1792 	return result;
1793     }
1794 
1795     item = fi->fi_lw.lw_item;
1796     if (item == NULL)
1797 	result = FALSE;
1798     else
1799     {
1800 	fi->fi_lw.lw_item = item->li_next;
1801 	result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1802 					   fi->fi_varcount, flag, NULL) == OK);
1803     }
1804     return result;
1805 }
1806 
1807 /*
1808  * Free the structure used to store info used by ":for".
1809  */
1810     void
1811 free_for_info(void *fi_void)
1812 {
1813     forinfo_T    *fi = (forinfo_T *)fi_void;
1814 
1815     if (fi == NULL)
1816 	return;
1817     if (fi->fi_list != NULL)
1818     {
1819 	list_rem_watch(fi->fi_list, &fi->fi_lw);
1820 	list_unref(fi->fi_list);
1821     }
1822     else if (fi->fi_blob != NULL)
1823 	blob_unref(fi->fi_blob);
1824     else
1825 	vim_free(fi->fi_string);
1826     vim_free(fi);
1827 }
1828 
1829     void
1830 set_context_for_expression(
1831     expand_T	*xp,
1832     char_u	*arg,
1833     cmdidx_T	cmdidx)
1834 {
1835     int		has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
1836     int		c;
1837     char_u	*p;
1838 
1839     if (cmdidx == CMD_let || cmdidx == CMD_var
1840 				 || cmdidx == CMD_const || cmdidx == CMD_final)
1841     {
1842 	xp->xp_context = EXPAND_USER_VARS;
1843 	if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
1844 	{
1845 	    // ":let var1 var2 ...": find last space.
1846 	    for (p = arg + STRLEN(arg); p >= arg; )
1847 	    {
1848 		xp->xp_pattern = p;
1849 		MB_PTR_BACK(arg, p);
1850 		if (VIM_ISWHITE(*p))
1851 		    break;
1852 	    }
1853 	    return;
1854 	}
1855     }
1856     else
1857 	xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1858 							  : EXPAND_EXPRESSION;
1859     while ((xp->xp_pattern = vim_strpbrk(arg,
1860 				  (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1861     {
1862 	c = *xp->xp_pattern;
1863 	if (c == '&')
1864 	{
1865 	    c = xp->xp_pattern[1];
1866 	    if (c == '&')
1867 	    {
1868 		++xp->xp_pattern;
1869 		xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1870 	    }
1871 	    else if (c != ' ')
1872 	    {
1873 		xp->xp_context = EXPAND_SETTINGS;
1874 		if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1875 		    xp->xp_pattern += 2;
1876 
1877 	    }
1878 	}
1879 	else if (c == '$')
1880 	{
1881 	    // environment variable
1882 	    xp->xp_context = EXPAND_ENV_VARS;
1883 	}
1884 	else if (c == '=')
1885 	{
1886 	    has_expr = TRUE;
1887 	    xp->xp_context = EXPAND_EXPRESSION;
1888 	}
1889 	else if (c == '#'
1890 		&& xp->xp_context == EXPAND_EXPRESSION)
1891 	{
1892 	    // Autoload function/variable contains '#'.
1893 	    break;
1894 	}
1895 	else if ((c == '<' || c == '#')
1896 		&& xp->xp_context == EXPAND_FUNCTIONS
1897 		&& vim_strchr(xp->xp_pattern, '(') == NULL)
1898 	{
1899 	    // Function name can start with "<SNR>" and contain '#'.
1900 	    break;
1901 	}
1902 	else if (has_expr)
1903 	{
1904 	    if (c == '"')	    // string
1905 	    {
1906 		while ((c = *++xp->xp_pattern) != NUL && c != '"')
1907 		    if (c == '\\' && xp->xp_pattern[1] != NUL)
1908 			++xp->xp_pattern;
1909 		xp->xp_context = EXPAND_NOTHING;
1910 	    }
1911 	    else if (c == '\'')	    // literal string
1912 	    {
1913 		// Trick: '' is like stopping and starting a literal string.
1914 		while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1915 		    /* skip */ ;
1916 		xp->xp_context = EXPAND_NOTHING;
1917 	    }
1918 	    else if (c == '|')
1919 	    {
1920 		if (xp->xp_pattern[1] == '|')
1921 		{
1922 		    ++xp->xp_pattern;
1923 		    xp->xp_context = EXPAND_EXPRESSION;
1924 		}
1925 		else
1926 		    xp->xp_context = EXPAND_COMMANDS;
1927 	    }
1928 	    else
1929 		xp->xp_context = EXPAND_EXPRESSION;
1930 	}
1931 	else
1932 	    // Doesn't look like something valid, expand as an expression
1933 	    // anyway.
1934 	    xp->xp_context = EXPAND_EXPRESSION;
1935 	arg = xp->xp_pattern;
1936 	if (*arg != NUL)
1937 	    while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1938 		/* skip */ ;
1939     }
1940 
1941     // ":exe one two" completes "two"
1942     if ((cmdidx == CMD_execute
1943 		|| cmdidx == CMD_echo
1944 		|| cmdidx == CMD_echon
1945 		|| cmdidx == CMD_echomsg)
1946 	    && xp->xp_context == EXPAND_EXPRESSION)
1947     {
1948 	for (;;)
1949 	{
1950 	    char_u *n = skiptowhite(arg);
1951 
1952 	    if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1953 		break;
1954 	    arg = skipwhite(n);
1955 	}
1956     }
1957 
1958     xp->xp_pattern = arg;
1959 }
1960 
1961 /*
1962  * Return TRUE if "pat" matches "text".
1963  * Does not use 'cpo' and always uses 'magic'.
1964  */
1965     int
1966 pattern_match(char_u *pat, char_u *text, int ic)
1967 {
1968     int		matches = FALSE;
1969     char_u	*save_cpo;
1970     regmatch_T	regmatch;
1971 
1972     // avoid 'l' flag in 'cpoptions'
1973     save_cpo = p_cpo;
1974     p_cpo = empty_option;
1975     regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1976     if (regmatch.regprog != NULL)
1977     {
1978 	regmatch.rm_ic = ic;
1979 	matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1980 	vim_regfree(regmatch.regprog);
1981     }
1982     p_cpo = save_cpo;
1983     return matches;
1984 }
1985 
1986 /*
1987  * Handle a name followed by "(".  Both for just "name(arg)" and for
1988  * "expr->name(arg)".
1989  * Returns OK or FAIL.
1990  */
1991     static int
1992 eval_func(
1993 	char_u	    **arg,	// points to "(", will be advanced
1994 	evalarg_T   *evalarg,
1995 	char_u	    *name,
1996 	int	    name_len,
1997 	typval_T    *rettv,
1998 	int	    flags,
1999 	typval_T    *basetv)	// "expr" for "expr->name(arg)"
2000 {
2001     int		evaluate = flags & EVAL_EVALUATE;
2002     char_u	*s = name;
2003     int		len = name_len;
2004     partial_T	*partial;
2005     int		ret = OK;
2006     type_T	*type = NULL;
2007 
2008     if (!evaluate)
2009 	check_vars(s, len);
2010 
2011     // If "s" is the name of a variable of type VAR_FUNC
2012     // use its contents.
2013     s = deref_func_name(s, &len, &partial,
2014 				    in_vim9script() ? &type : NULL, !evaluate);
2015 
2016     // Need to make a copy, in case evaluating the arguments makes
2017     // the name invalid.
2018     s = vim_strsave(s);
2019     if (s == NULL || (flags & EVAL_CONSTANT))
2020 	ret = FAIL;
2021     else
2022     {
2023 	funcexe_T funcexe;
2024 
2025 	// Invoke the function.
2026 	CLEAR_FIELD(funcexe);
2027 	funcexe.firstline = curwin->w_cursor.lnum;
2028 	funcexe.lastline = curwin->w_cursor.lnum;
2029 	funcexe.evaluate = evaluate;
2030 	funcexe.partial = partial;
2031 	funcexe.basetv = basetv;
2032 	funcexe.check_type = type;
2033 	ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
2034     }
2035     vim_free(s);
2036 
2037     // If evaluate is FALSE rettv->v_type was not set in
2038     // get_func_tv, but it's needed in handle_subscript() to parse
2039     // what follows. So set it here.
2040     if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2041     {
2042 	rettv->vval.v_string = NULL;
2043 	rettv->v_type = VAR_FUNC;
2044     }
2045 
2046     // Stop the expression evaluation when immediately
2047     // aborting on error, or when an interrupt occurred or
2048     // an exception was thrown but not caught.
2049     if (evaluate && aborting())
2050     {
2051 	if (ret == OK)
2052 	    clear_tv(rettv);
2053 	ret = FAIL;
2054     }
2055     return ret;
2056 }
2057 
2058 /*
2059  * Get the next line source line without advancing.  But do skip over comment
2060  * lines.
2061  */
2062     static char_u *
2063 getline_peek_skip_comments(evalarg_T *evalarg)
2064 {
2065     for (;;)
2066     {
2067 	char_u *next = getline_peek(evalarg->eval_getline,
2068 							 evalarg->eval_cookie);
2069 	char_u *p;
2070 
2071 	if (next == NULL)
2072 	    break;
2073 	p = skipwhite(next);
2074 	if (*p != NUL && !vim9_comment_start(p))
2075 	    return next;
2076 	(void)eval_next_line(evalarg);
2077     }
2078     return NULL;
2079 }
2080 
2081 /*
2082  * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2083  * comment) and there is a next line, return the next line (skipping blanks)
2084  * and set "getnext".
2085  * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2086  * "arg" must point somewhere inside a line, not at the start.
2087  */
2088     char_u *
2089 eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2090 {
2091     char_u *p = skipwhite(arg);
2092 
2093     *getnext = FALSE;
2094     if (in_vim9script()
2095 	    && evalarg != NULL
2096 	    && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
2097 	    && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
2098     {
2099 	char_u *next;
2100 
2101 	if (evalarg->eval_cookie != NULL)
2102 	    next = getline_peek_skip_comments(evalarg);
2103 	else
2104 	    next = peek_next_line_from_context(evalarg->eval_cctx);
2105 
2106 	if (next != NULL)
2107 	{
2108 	    *getnext = TRUE;
2109 	    return skipwhite(next);
2110 	}
2111     }
2112     return p;
2113 }
2114 
2115 /*
2116  * To be called after eval_next_non_blank() sets "getnext" to TRUE.
2117  */
2118     char_u *
2119 eval_next_line(evalarg_T *evalarg)
2120 {
2121     garray_T	*gap = &evalarg->eval_ga;
2122     char_u	*line;
2123 
2124     if (evalarg->eval_cookie != NULL)
2125 	line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2126 							   GETLINE_CONCAT_ALL);
2127     else
2128 	line = next_line_from_context(evalarg->eval_cctx, TRUE);
2129     ++evalarg->eval_break_count;
2130     if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2131     {
2132 	// Going to concatenate the lines after parsing.
2133 	((char_u **)gap->ga_data)[gap->ga_len] = line;
2134 	++gap->ga_len;
2135     }
2136     else if (evalarg->eval_cookie != NULL)
2137     {
2138 	vim_free(evalarg->eval_tofree);
2139 	evalarg->eval_tofree = line;
2140     }
2141     return skipwhite(line);
2142 }
2143 
2144 /*
2145  * Call eval_next_non_blank() and get the next line if needed.
2146  */
2147     char_u *
2148 skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2149 {
2150     int	    getnext;
2151     char_u  *p = skipwhite(arg);
2152 
2153     if (evalarg == NULL)
2154 	return skipwhite(arg);
2155     eval_next_non_blank(p, evalarg, &getnext);
2156     if (getnext)
2157 	return eval_next_line(evalarg);
2158     return p;
2159 }
2160 
2161 /*
2162  * After using "evalarg" filled from "eap": free the memory.
2163  */
2164     void
2165 clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2166 {
2167     if (evalarg != NULL)
2168     {
2169 	if (evalarg->eval_tofree != NULL)
2170 	{
2171 	    if (eap != NULL)
2172 	    {
2173 		// We may need to keep the original command line, e.g. for
2174 		// ":let" it has the variable names.  But we may also need the
2175 		// new one, "nextcmd" points into it.  Keep both.
2176 		vim_free(eap->cmdline_tofree);
2177 		eap->cmdline_tofree = *eap->cmdlinep;
2178 		*eap->cmdlinep = evalarg->eval_tofree;
2179 	    }
2180 	    else
2181 		vim_free(evalarg->eval_tofree);
2182 	    evalarg->eval_tofree = NULL;
2183 	}
2184 
2185 	VIM_CLEAR(evalarg->eval_tofree_cmdline);
2186 	VIM_CLEAR(evalarg->eval_tofree_lambda);
2187     }
2188 }
2189 
2190 /*
2191  * The "evaluate" argument: When FALSE, the argument is only parsed but not
2192  * executed.  The function may return OK, but the rettv will be of type
2193  * VAR_UNKNOWN.  The function still returns FAIL for a syntax error.
2194  */
2195 
2196 /*
2197  * Handle zero level expression.
2198  * This calls eval1() and handles error message and nextcmd.
2199  * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
2200  * Note: "rettv.v_lock" is not set.
2201  * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
2202  * Return OK or FAIL.
2203  */
2204     int
2205 eval0(
2206     char_u	*arg,
2207     typval_T	*rettv,
2208     exarg_T	*eap,
2209     evalarg_T	*evalarg)
2210 {
2211     int		ret;
2212     char_u	*p;
2213     int		did_emsg_before = did_emsg;
2214     int		called_emsg_before = called_emsg;
2215     int		flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2216 
2217     p = skipwhite(arg);
2218     ret = eval1(&p, rettv, evalarg);
2219     p = skipwhite(p);
2220 
2221     if (ret == FAIL || !ends_excmd2(arg, p))
2222     {
2223 	if (ret != FAIL)
2224 	    clear_tv(rettv);
2225 	/*
2226 	 * Report the invalid expression unless the expression evaluation has
2227 	 * been cancelled due to an aborting error, an interrupt, or an
2228 	 * exception, or we already gave a more specific error.
2229 	 * Also check called_emsg for when using assert_fails().
2230 	 */
2231 	if (!aborting()
2232 		&& did_emsg == did_emsg_before
2233 		&& called_emsg == called_emsg_before
2234 		&& (flags & EVAL_CONSTANT) == 0
2235 		&& (!in_vim9script() || !vim9_bad_comment(p)))
2236 	    semsg(_(e_invexpr2), arg);
2237 
2238 	// Some of the expression may not have been consumed.  Do not check for
2239 	// a next command to avoid more errors, unless "|" is following, which
2240 	// could only be a command separator.
2241 	if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2242 	    eap->nextcmd = check_nextcmd(p);
2243 	return FAIL;
2244     }
2245 
2246     if (eap != NULL)
2247 	eap->nextcmd = check_nextcmd(p);
2248 
2249     return ret;
2250 }
2251 
2252 /*
2253  * Handle top level expression:
2254  *	expr2 ? expr1 : expr1
2255  *	expr2 ?? expr1
2256  *
2257  * "arg" must point to the first non-white of the expression.
2258  * "arg" is advanced to just after the recognized expression.
2259  *
2260  * Note: "rettv.v_lock" is not set.
2261  *
2262  * Return OK or FAIL.
2263  */
2264     int
2265 eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2266 {
2267     char_u  *p;
2268     int	    getnext;
2269 
2270     CLEAR_POINTER(rettv);
2271 
2272     /*
2273      * Get the first variable.
2274      */
2275     if (eval2(arg, rettv, evalarg) == FAIL)
2276 	return FAIL;
2277 
2278     p = eval_next_non_blank(*arg, evalarg, &getnext);
2279     if (*p == '?')
2280     {
2281 	int		op_falsy = p[1] == '?';
2282 	int		result;
2283 	typval_T	var2;
2284 	evalarg_T	*evalarg_used = evalarg;
2285 	evalarg_T	local_evalarg;
2286 	int		orig_flags;
2287 	int		evaluate;
2288 	int		vim9script = in_vim9script();
2289 
2290 	if (evalarg == NULL)
2291 	{
2292 	    CLEAR_FIELD(local_evalarg);
2293 	    evalarg_used = &local_evalarg;
2294 	}
2295 	orig_flags = evalarg_used->eval_flags;
2296 	evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
2297 
2298 	if (getnext)
2299 	    *arg = eval_next_line(evalarg_used);
2300 	else
2301 	{
2302 	    if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2303 	    {
2304 		error_white_both(p, op_falsy ? 2 : 1);
2305 		clear_tv(rettv);
2306 		return FAIL;
2307 	    }
2308 	    *arg = p;
2309 	}
2310 
2311 	result = FALSE;
2312 	if (evaluate)
2313 	{
2314 	    int		error = FALSE;
2315 
2316 	    if (op_falsy)
2317 		result = tv2bool(rettv);
2318 	    else if (vim9script)
2319 		result = tv_get_bool_chk(rettv, &error);
2320 	    else if (tv_get_number_chk(rettv, &error) != 0)
2321 		result = TRUE;
2322 	    if (error || !op_falsy || !result)
2323 		clear_tv(rettv);
2324 	    if (error)
2325 		return FAIL;
2326 	}
2327 
2328 	/*
2329 	 * Get the second variable.  Recursive!
2330 	 */
2331 	if (op_falsy)
2332 	    ++*arg;
2333 	if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
2334 	{
2335 	    error_white_both(p, op_falsy ? 2 : 1);
2336 	    clear_tv(rettv);
2337 	    return FAIL;
2338 	}
2339 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2340 	evalarg_used->eval_flags = (op_falsy ? !result : result)
2341 				    ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2342 	if (eval1(arg, &var2, evalarg_used) == FAIL)
2343 	{
2344 	    evalarg_used->eval_flags = orig_flags;
2345 	    return FAIL;
2346 	}
2347 	if (!op_falsy || !result)
2348 	    *rettv = var2;
2349 
2350 	if (!op_falsy)
2351 	{
2352 	    /*
2353 	     * Check for the ":".
2354 	     */
2355 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2356 	    if (*p != ':')
2357 	    {
2358 		emsg(_(e_missing_colon));
2359 		if (evaluate && result)
2360 		    clear_tv(rettv);
2361 		evalarg_used->eval_flags = orig_flags;
2362 		return FAIL;
2363 	    }
2364 	    if (getnext)
2365 		*arg = eval_next_line(evalarg_used);
2366 	    else
2367 	    {
2368 		if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2369 		{
2370 		    error_white_both(p, 1);
2371 		    clear_tv(rettv);
2372 		    evalarg_used->eval_flags = orig_flags;
2373 		    return FAIL;
2374 		}
2375 		*arg = p;
2376 	    }
2377 
2378 	    /*
2379 	     * Get the third variable.  Recursive!
2380 	     */
2381 	    if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
2382 	    {
2383 		error_white_both(p, 1);
2384 		clear_tv(rettv);
2385 		evalarg_used->eval_flags = orig_flags;
2386 		return FAIL;
2387 	    }
2388 	    *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2389 	    evalarg_used->eval_flags = !result ? orig_flags
2390 						 : orig_flags & ~EVAL_EVALUATE;
2391 	    if (eval1(arg, &var2, evalarg_used) == FAIL)
2392 	    {
2393 		if (evaluate && result)
2394 		    clear_tv(rettv);
2395 		evalarg_used->eval_flags = orig_flags;
2396 		return FAIL;
2397 	    }
2398 	    if (evaluate && !result)
2399 		*rettv = var2;
2400 	}
2401 
2402 	if (evalarg == NULL)
2403 	    clear_evalarg(&local_evalarg, NULL);
2404 	else
2405 	    evalarg->eval_flags = orig_flags;
2406     }
2407 
2408     return OK;
2409 }
2410 
2411 /*
2412  * Handle first level expression:
2413  *	expr2 || expr2 || expr2	    logical OR
2414  *
2415  * "arg" must point to the first non-white of the expression.
2416  * "arg" is advanced to just after the recognized expression.
2417  *
2418  * Return OK or FAIL.
2419  */
2420     static int
2421 eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2422 {
2423     char_u	*p;
2424     int		getnext;
2425 
2426     /*
2427      * Get the first variable.
2428      */
2429     if (eval3(arg, rettv, evalarg) == FAIL)
2430 	return FAIL;
2431 
2432     /*
2433      * Handle the  "||" operator.
2434      */
2435     p = eval_next_non_blank(*arg, evalarg, &getnext);
2436     if (p[0] == '|' && p[1] == '|')
2437     {
2438 	evalarg_T   *evalarg_used = evalarg;
2439 	evalarg_T   local_evalarg;
2440 	int	    evaluate;
2441 	int	    orig_flags;
2442 	long	    result = FALSE;
2443 	typval_T    var2;
2444 	int	    error = FALSE;
2445 	int	    vim9script = in_vim9script();
2446 
2447 	if (evalarg == NULL)
2448 	{
2449 	    CLEAR_FIELD(local_evalarg);
2450 	    evalarg_used = &local_evalarg;
2451 	}
2452 	orig_flags = evalarg_used->eval_flags;
2453 	evaluate = orig_flags & EVAL_EVALUATE;
2454 	if (evaluate)
2455 	{
2456 	    if (vim9script)
2457 		result = tv_get_bool_chk(rettv, &error);
2458 	    else if (tv_get_number_chk(rettv, &error) != 0)
2459 		result = TRUE;
2460 	    clear_tv(rettv);
2461 	    if (error)
2462 		return FAIL;
2463 	}
2464 
2465 	/*
2466 	 * Repeat until there is no following "||".
2467 	 */
2468 	while (p[0] == '|' && p[1] == '|')
2469 	{
2470 	    if (getnext)
2471 		*arg = eval_next_line(evalarg_used);
2472 	    else
2473 	    {
2474 		if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2475 		{
2476 		    error_white_both(p, 2);
2477 		    clear_tv(rettv);
2478 		    return FAIL;
2479 		}
2480 		*arg = p;
2481 	    }
2482 
2483 	    /*
2484 	     * Get the second variable.
2485 	     */
2486 	    if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2487 	    {
2488 		error_white_both(p, 2);
2489 		clear_tv(rettv);
2490 		return FAIL;
2491 	    }
2492 	    *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2493 	    evalarg_used->eval_flags = !result ? orig_flags
2494 						 : orig_flags & ~EVAL_EVALUATE;
2495 	    if (eval3(arg, &var2, evalarg_used) == FAIL)
2496 		return FAIL;
2497 
2498 	    /*
2499 	     * Compute the result.
2500 	     */
2501 	    if (evaluate && !result)
2502 	    {
2503 		if (vim9script)
2504 		    result = tv_get_bool_chk(&var2, &error);
2505 		else if (tv_get_number_chk(&var2, &error) != 0)
2506 		    result = TRUE;
2507 		clear_tv(&var2);
2508 		if (error)
2509 		    return FAIL;
2510 	    }
2511 	    if (evaluate)
2512 	    {
2513 		if (vim9script)
2514 		{
2515 		    rettv->v_type = VAR_BOOL;
2516 		    rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
2517 		}
2518 		else
2519 		{
2520 		    rettv->v_type = VAR_NUMBER;
2521 		    rettv->vval.v_number = result;
2522 		}
2523 	    }
2524 
2525 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2526 	}
2527 
2528 	if (evalarg == NULL)
2529 	    clear_evalarg(&local_evalarg, NULL);
2530 	else
2531 	    evalarg->eval_flags = orig_flags;
2532     }
2533 
2534     return OK;
2535 }
2536 
2537 /*
2538  * Handle second level expression:
2539  *	expr3 && expr3 && expr3	    logical AND
2540  *
2541  * "arg" must point to the first non-white of the expression.
2542  * "arg" is advanced to just after the recognized expression.
2543  *
2544  * Return OK or FAIL.
2545  */
2546     static int
2547 eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2548 {
2549     char_u	*p;
2550     int		getnext;
2551 
2552     /*
2553      * Get the first variable.
2554      */
2555     if (eval4(arg, rettv, evalarg) == FAIL)
2556 	return FAIL;
2557 
2558     /*
2559      * Handle the "&&" operator.
2560      */
2561     p = eval_next_non_blank(*arg, evalarg, &getnext);
2562     if (p[0] == '&' && p[1] == '&')
2563     {
2564 	evalarg_T   *evalarg_used = evalarg;
2565 	evalarg_T   local_evalarg;
2566 	int	    orig_flags;
2567 	int	    evaluate;
2568 	long	    result = TRUE;
2569 	typval_T    var2;
2570 	int	    error = FALSE;
2571 	int	    vim9script = in_vim9script();
2572 
2573 	if (evalarg == NULL)
2574 	{
2575 	    CLEAR_FIELD(local_evalarg);
2576 	    evalarg_used = &local_evalarg;
2577 	}
2578 	orig_flags = evalarg_used->eval_flags;
2579 	evaluate = orig_flags & EVAL_EVALUATE;
2580 	if (evaluate)
2581 	{
2582 	    if (vim9script)
2583 		result = tv_get_bool_chk(rettv, &error);
2584 	    else if (tv_get_number_chk(rettv, &error) == 0)
2585 		result = FALSE;
2586 	    clear_tv(rettv);
2587 	    if (error)
2588 		return FAIL;
2589 	}
2590 
2591 	/*
2592 	 * Repeat until there is no following "&&".
2593 	 */
2594 	while (p[0] == '&' && p[1] == '&')
2595 	{
2596 	    if (getnext)
2597 		*arg = eval_next_line(evalarg_used);
2598 	    else
2599 	    {
2600 		if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2601 		{
2602 		    error_white_both(p, 2);
2603 		    clear_tv(rettv);
2604 		    return FAIL;
2605 		}
2606 		*arg = p;
2607 	    }
2608 
2609 	    /*
2610 	     * Get the second variable.
2611 	     */
2612 	    if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2613 	    {
2614 		error_white_both(p, 2);
2615 		clear_tv(rettv);
2616 		return FAIL;
2617 	    }
2618 	    *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2619 	    evalarg_used->eval_flags = result ? orig_flags
2620 						 : orig_flags & ~EVAL_EVALUATE;
2621 	    CLEAR_FIELD(var2);
2622 	    if (eval4(arg, &var2, evalarg_used) == FAIL)
2623 		return FAIL;
2624 
2625 	    /*
2626 	     * Compute the result.
2627 	     */
2628 	    if (evaluate && result)
2629 	    {
2630 		if (vim9script)
2631 		    result = tv_get_bool_chk(&var2, &error);
2632 		else if (tv_get_number_chk(&var2, &error) == 0)
2633 		    result = FALSE;
2634 		clear_tv(&var2);
2635 		if (error)
2636 		    return FAIL;
2637 	    }
2638 	    if (evaluate)
2639 	    {
2640 		if (vim9script)
2641 		{
2642 		    rettv->v_type = VAR_BOOL;
2643 		    rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
2644 		}
2645 		else
2646 		{
2647 		    rettv->v_type = VAR_NUMBER;
2648 		    rettv->vval.v_number = result;
2649 		}
2650 	    }
2651 
2652 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2653 	}
2654 
2655 	if (evalarg == NULL)
2656 	    clear_evalarg(&local_evalarg, NULL);
2657 	else
2658 	    evalarg->eval_flags = orig_flags;
2659     }
2660 
2661     return OK;
2662 }
2663 
2664 /*
2665  * Handle third level expression:
2666  *	var1 == var2
2667  *	var1 =~ var2
2668  *	var1 != var2
2669  *	var1 !~ var2
2670  *	var1 > var2
2671  *	var1 >= var2
2672  *	var1 < var2
2673  *	var1 <= var2
2674  *	var1 is var2
2675  *	var1 isnot var2
2676  *
2677  * "arg" must point to the first non-white of the expression.
2678  * "arg" is advanced to just after the recognized expression.
2679  *
2680  * Return OK or FAIL.
2681  */
2682     static int
2683 eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2684 {
2685     char_u	*p;
2686     int		getnext;
2687     exprtype_T	type = EXPR_UNKNOWN;
2688     int		len = 2;
2689     int		type_is = FALSE;
2690 
2691     /*
2692      * Get the first variable.
2693      */
2694     if (eval5(arg, rettv, evalarg) == FAIL)
2695 	return FAIL;
2696 
2697     p = eval_next_non_blank(*arg, evalarg, &getnext);
2698     type = get_compare_type(p, &len, &type_is);
2699 
2700     /*
2701      * If there is a comparative operator, use it.
2702      */
2703     if (type != EXPR_UNKNOWN)
2704     {
2705 	typval_T    var2;
2706 	int	    ic;
2707 	int	    vim9script = in_vim9script();
2708 	int	    evaluate = evalarg == NULL
2709 				   ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
2710 
2711 	if (getnext)
2712 	    *arg = eval_next_line(evalarg);
2713 	else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2714 	{
2715 	    error_white_both(p, len);
2716 	    clear_tv(rettv);
2717 	    return FAIL;
2718 	}
2719 
2720 	if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2721 	{
2722 	    semsg(_(e_invexpr2), p);
2723 	    clear_tv(rettv);
2724 	    return FAIL;
2725 	}
2726 
2727 	// extra question mark appended: ignore case
2728 	if (p[len] == '?')
2729 	{
2730 	    ic = TRUE;
2731 	    ++len;
2732 	}
2733 	// extra '#' appended: match case
2734 	else if (p[len] == '#')
2735 	{
2736 	    ic = FALSE;
2737 	    ++len;
2738 	}
2739 	// nothing appended: use 'ignorecase' if not in Vim script
2740 	else
2741 	    ic = vim9script ? FALSE : p_ic;
2742 
2743 	/*
2744 	 * Get the second variable.
2745 	 */
2746 	if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2747 	{
2748 	    error_white_both(p, len);
2749 	    clear_tv(rettv);
2750 	    return FAIL;
2751 	}
2752 	*arg = skipwhite_and_linebreak(p + len, evalarg);
2753 	if (eval5(arg, &var2, evalarg) == FAIL)
2754 	{
2755 	    clear_tv(rettv);
2756 	    return FAIL;
2757 	}
2758 	if (evaluate)
2759 	{
2760 	    int ret;
2761 
2762 	    if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
2763 	    {
2764 		ret = FAIL;
2765 		clear_tv(rettv);
2766 	    }
2767 	    else
2768 		ret = typval_compare(rettv, &var2, type, ic);
2769 	    clear_tv(&var2);
2770 	    return ret;
2771 	}
2772     }
2773 
2774     return OK;
2775 }
2776 
2777 /*
2778  * Make a copy of blob "tv1" and append blob "tv2".
2779  */
2780     void
2781 eval_addblob(typval_T *tv1, typval_T *tv2)
2782 {
2783     blob_T  *b1 = tv1->vval.v_blob;
2784     blob_T  *b2 = tv2->vval.v_blob;
2785     blob_T  *b = blob_alloc();
2786     int	    i;
2787 
2788     if (b != NULL)
2789     {
2790 	for (i = 0; i < blob_len(b1); i++)
2791 	    ga_append(&b->bv_ga, blob_get(b1, i));
2792 	for (i = 0; i < blob_len(b2); i++)
2793 	    ga_append(&b->bv_ga, blob_get(b2, i));
2794 
2795 	clear_tv(tv1);
2796 	rettv_blob_set(tv1, b);
2797     }
2798 }
2799 
2800 /*
2801  * Make a copy of list "tv1" and append list "tv2".
2802  */
2803     int
2804 eval_addlist(typval_T *tv1, typval_T *tv2)
2805 {
2806     typval_T var3;
2807 
2808     // concatenate Lists
2809     if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2810     {
2811 	clear_tv(tv1);
2812 	clear_tv(tv2);
2813 	return FAIL;
2814     }
2815     clear_tv(tv1);
2816     *tv1 = var3;
2817     return OK;
2818 }
2819 
2820 /*
2821  * Handle fourth level expression:
2822  *	+	number addition
2823  *	-	number subtraction
2824  *	.	string concatenation (if script version is 1)
2825  *	..	string concatenation
2826  *
2827  * "arg" must point to the first non-white of the expression.
2828  * "arg" is advanced to just after the recognized expression.
2829  *
2830  * Return OK or FAIL.
2831  */
2832     static int
2833 eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2834 {
2835     /*
2836      * Get the first variable.
2837      */
2838     if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
2839 	return FAIL;
2840 
2841     /*
2842      * Repeat computing, until no '+', '-' or '.' is following.
2843      */
2844     for (;;)
2845     {
2846 	int	    evaluate;
2847 	int	    getnext;
2848 	char_u	    *p;
2849 	int	    op;
2850 	int	    oplen;
2851 	int	    concat;
2852 	typval_T    var2;
2853 	int	    vim9script = in_vim9script();
2854 
2855 	// "." is only string concatenation when scriptversion is 1
2856 	// "+=", "-=" and "..=" are assignments
2857 	p = eval_next_non_blank(*arg, evalarg, &getnext);
2858 	op = *p;
2859 	concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
2860 	if ((op != '+' && op != '-' && !concat) || p[1] == '='
2861 					       || (p[1] == '.' && p[2] == '='))
2862 	    break;
2863 
2864 	evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
2865 	oplen = (concat && p[1] == '.') ? 2 : 1;
2866 	if (getnext)
2867 	    *arg = eval_next_line(evalarg);
2868 	else
2869 	{
2870 	    if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2871 	    {
2872 		error_white_both(p, oplen);
2873 		clear_tv(rettv);
2874 		return FAIL;
2875 	    }
2876 	    *arg = p;
2877 	}
2878 	if ((op != '+' || (rettv->v_type != VAR_LIST
2879 						 && rettv->v_type != VAR_BLOB))
2880 #ifdef FEAT_FLOAT
2881 		&& (op == '.' || rettv->v_type != VAR_FLOAT)
2882 #endif
2883 		&& evaluate)
2884 	{
2885 	    int		error = FALSE;
2886 
2887 	    // For "list + ...", an illegal use of the first operand as
2888 	    // a number cannot be determined before evaluating the 2nd
2889 	    // operand: if this is also a list, all is ok.
2890 	    // For "something . ...", "something - ..." or "non-list + ...",
2891 	    // we know that the first operand needs to be a string or number
2892 	    // without evaluating the 2nd operand.  So check before to avoid
2893 	    // side effects after an error.
2894 	    if (op != '.')
2895 		tv_get_number_chk(rettv, &error);
2896 	    if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
2897 	    {
2898 		clear_tv(rettv);
2899 		return FAIL;
2900 	    }
2901 	}
2902 
2903 	/*
2904 	 * Get the second variable.
2905 	 */
2906 	if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
2907 	{
2908 	    error_white_both(p, oplen);
2909 	    clear_tv(rettv);
2910 	    return FAIL;
2911 	}
2912 	*arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
2913 	if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
2914 	{
2915 	    clear_tv(rettv);
2916 	    return FAIL;
2917 	}
2918 
2919 	if (evaluate)
2920 	{
2921 	    /*
2922 	     * Compute the result.
2923 	     */
2924 	    if (op == '.')
2925 	    {
2926 		char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2927 		char_u	*s1 = tv_get_string_buf(rettv, buf1);
2928 		char_u	*s2 = NULL;
2929 
2930 		if (vim9script && (var2.v_type == VAR_VOID
2931 			|| var2.v_type == VAR_CHANNEL
2932 			|| var2.v_type == VAR_JOB))
2933 		    emsg(_(e_inval_string));
2934 #ifdef FEAT_FLOAT
2935 		else if (vim9script && var2.v_type == VAR_FLOAT)
2936 		{
2937 		    vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2938 							    var2.vval.v_float);
2939 		    s2 = buf2;
2940 		}
2941 #endif
2942 		else
2943 		    s2 = tv_get_string_buf_chk(&var2, buf2);
2944 		if (s2 == NULL)		// type error ?
2945 		{
2946 		    clear_tv(rettv);
2947 		    clear_tv(&var2);
2948 		    return FAIL;
2949 		}
2950 		p = concat_str(s1, s2);
2951 		clear_tv(rettv);
2952 		rettv->v_type = VAR_STRING;
2953 		rettv->vval.v_string = p;
2954 	    }
2955 	    else if (op == '+' && rettv->v_type == VAR_BLOB
2956 						   && var2.v_type == VAR_BLOB)
2957 		eval_addblob(rettv, &var2);
2958 	    else if (op == '+' && rettv->v_type == VAR_LIST
2959 						   && var2.v_type == VAR_LIST)
2960 	    {
2961 		if (eval_addlist(rettv, &var2) == FAIL)
2962 		    return FAIL;
2963 	    }
2964 	    else
2965 	    {
2966 		int		error = FALSE;
2967 		varnumber_T	n1, n2;
2968 #ifdef FEAT_FLOAT
2969 		float_T	    f1 = 0, f2 = 0;
2970 
2971 		if (rettv->v_type == VAR_FLOAT)
2972 		{
2973 		    f1 = rettv->vval.v_float;
2974 		    n1 = 0;
2975 		}
2976 		else
2977 #endif
2978 		{
2979 		    n1 = tv_get_number_chk(rettv, &error);
2980 		    if (error)
2981 		    {
2982 			// This can only happen for "list + non-list" or
2983 			// "blob + non-blob".  For "non-list + ..." or
2984 			// "something - ...", we returned before evaluating the
2985 			// 2nd operand.
2986 			clear_tv(rettv);
2987 			clear_tv(&var2);
2988 			return FAIL;
2989 		    }
2990 #ifdef FEAT_FLOAT
2991 		    if (var2.v_type == VAR_FLOAT)
2992 			f1 = n1;
2993 #endif
2994 		}
2995 #ifdef FEAT_FLOAT
2996 		if (var2.v_type == VAR_FLOAT)
2997 		{
2998 		    f2 = var2.vval.v_float;
2999 		    n2 = 0;
3000 		}
3001 		else
3002 #endif
3003 		{
3004 		    n2 = tv_get_number_chk(&var2, &error);
3005 		    if (error)
3006 		    {
3007 			clear_tv(rettv);
3008 			clear_tv(&var2);
3009 			return FAIL;
3010 		    }
3011 #ifdef FEAT_FLOAT
3012 		    if (rettv->v_type == VAR_FLOAT)
3013 			f2 = n2;
3014 #endif
3015 		}
3016 		clear_tv(rettv);
3017 
3018 #ifdef FEAT_FLOAT
3019 		// If there is a float on either side the result is a float.
3020 		if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3021 		{
3022 		    if (op == '+')
3023 			f1 = f1 + f2;
3024 		    else
3025 			f1 = f1 - f2;
3026 		    rettv->v_type = VAR_FLOAT;
3027 		    rettv->vval.v_float = f1;
3028 		}
3029 		else
3030 #endif
3031 		{
3032 		    if (op == '+')
3033 			n1 = n1 + n2;
3034 		    else
3035 			n1 = n1 - n2;
3036 		    rettv->v_type = VAR_NUMBER;
3037 		    rettv->vval.v_number = n1;
3038 		}
3039 	    }
3040 	    clear_tv(&var2);
3041 	}
3042     }
3043     return OK;
3044 }
3045 
3046 /*
3047  * Handle fifth level expression:
3048  *	*	number multiplication
3049  *	/	number division
3050  *	%	number modulo
3051  *
3052  * "arg" must point to the first non-white of the expression.
3053  * "arg" is advanced to just after the recognized expression.
3054  *
3055  * Return OK or FAIL.
3056  */
3057     static int
3058 eval6(
3059     char_u	**arg,
3060     typval_T	*rettv,
3061     evalarg_T	*evalarg,
3062     int		want_string)  // after "." operator
3063 {
3064 #ifdef FEAT_FLOAT
3065     int	    use_float = FALSE;
3066 #endif
3067 
3068     /*
3069      * Get the first variable.
3070      */
3071     if (eval7(arg, rettv, evalarg, want_string) == FAIL)
3072 	return FAIL;
3073 
3074     /*
3075      * Repeat computing, until no '*', '/' or '%' is following.
3076      */
3077     for (;;)
3078     {
3079 	int	    evaluate;
3080 	int	    getnext;
3081 	typval_T    var2;
3082 	char_u	    *p;
3083 	int	    op;
3084 	varnumber_T n1, n2;
3085 #ifdef FEAT_FLOAT
3086 	float_T	    f1, f2;
3087 #endif
3088 	int	    error;
3089 
3090 	// "*=", "/=" and "%=" are assignments
3091 	p = eval_next_non_blank(*arg, evalarg, &getnext);
3092 	op = *p;
3093 	if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
3094 	    break;
3095 
3096 	evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3097 	if (getnext)
3098 	    *arg = eval_next_line(evalarg);
3099 	else
3100 	{
3101 	    if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3102 	    {
3103 		error_white_both(p, 1);
3104 		clear_tv(rettv);
3105 		return FAIL;
3106 	    }
3107 	    *arg = p;
3108 	}
3109 
3110 #ifdef FEAT_FLOAT
3111 	f1 = 0;
3112 	f2 = 0;
3113 #endif
3114 	error = FALSE;
3115 	if (evaluate)
3116 	{
3117 #ifdef FEAT_FLOAT
3118 	    if (rettv->v_type == VAR_FLOAT)
3119 	    {
3120 		f1 = rettv->vval.v_float;
3121 		use_float = TRUE;
3122 		n1 = 0;
3123 	    }
3124 	    else
3125 #endif
3126 		n1 = tv_get_number_chk(rettv, &error);
3127 	    clear_tv(rettv);
3128 	    if (error)
3129 		return FAIL;
3130 	}
3131 	else
3132 	    n1 = 0;
3133 
3134 	/*
3135 	 * Get the second variable.
3136 	 */
3137 	if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3138 	{
3139 	    error_white_both(*arg, 1);
3140 	    clear_tv(rettv);
3141 	    return FAIL;
3142 	}
3143 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3144 	if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
3145 	    return FAIL;
3146 
3147 	if (evaluate)
3148 	{
3149 #ifdef FEAT_FLOAT
3150 	    if (var2.v_type == VAR_FLOAT)
3151 	    {
3152 		if (!use_float)
3153 		{
3154 		    f1 = n1;
3155 		    use_float = TRUE;
3156 		}
3157 		f2 = var2.vval.v_float;
3158 		n2 = 0;
3159 	    }
3160 	    else
3161 #endif
3162 	    {
3163 		n2 = tv_get_number_chk(&var2, &error);
3164 		clear_tv(&var2);
3165 		if (error)
3166 		    return FAIL;
3167 #ifdef FEAT_FLOAT
3168 		if (use_float)
3169 		    f2 = n2;
3170 #endif
3171 	    }
3172 
3173 	    /*
3174 	     * Compute the result.
3175 	     * When either side is a float the result is a float.
3176 	     */
3177 #ifdef FEAT_FLOAT
3178 	    if (use_float)
3179 	    {
3180 		if (op == '*')
3181 		    f1 = f1 * f2;
3182 		else if (op == '/')
3183 		{
3184 # ifdef VMS
3185 		    // VMS crashes on divide by zero, work around it
3186 		    if (f2 == 0.0)
3187 		    {
3188 			if (f1 == 0)
3189 			    f1 = -1 * __F_FLT_MAX - 1L;   // similar to NaN
3190 			else if (f1 < 0)
3191 			    f1 = -1 * __F_FLT_MAX;
3192 			else
3193 			    f1 = __F_FLT_MAX;
3194 		    }
3195 		    else
3196 			f1 = f1 / f2;
3197 # else
3198 		    // We rely on the floating point library to handle divide
3199 		    // by zero to result in "inf" and not a crash.
3200 		    f1 = f1 / f2;
3201 # endif
3202 		}
3203 		else
3204 		{
3205 		    emsg(_(e_modulus));
3206 		    return FAIL;
3207 		}
3208 		rettv->v_type = VAR_FLOAT;
3209 		rettv->vval.v_float = f1;
3210 	    }
3211 	    else
3212 #endif
3213 	    {
3214 		int	    failed = FALSE;
3215 
3216 		if (op == '*')
3217 		    n1 = n1 * n2;
3218 		else if (op == '/')
3219 		    n1 = num_divide(n1, n2, &failed);
3220 		else
3221 		    n1 = num_modulus(n1, n2, &failed);
3222 		if (failed)
3223 		    return FAIL;
3224 
3225 		rettv->v_type = VAR_NUMBER;
3226 		rettv->vval.v_number = n1;
3227 	    }
3228 	}
3229     }
3230 
3231     return OK;
3232 }
3233 
3234     int
3235 eval_leader(char_u **arg, int vim9)
3236 {
3237     char_u	*s = *arg;
3238     char_u	*p = *arg;
3239 
3240     while (*p == '!' || *p == '-' || *p == '+')
3241     {
3242 	char_u *n = skipwhite(p + 1);
3243 
3244 	// ++, --, -+ and +- are not accepted in Vim9 script
3245 	if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3246 	{
3247 	    semsg(_(e_invexpr2), s);
3248 	    return FAIL;
3249 	}
3250 	p = n;
3251     }
3252     *arg = p;
3253     return OK;
3254 }
3255 
3256 /*
3257  * Handle sixth level expression:
3258  *  number		number constant
3259  *  0zFFFFFFFF		Blob constant
3260  *  "string"		string constant
3261  *  'string'		literal string constant
3262  *  &option-name	option value
3263  *  @r			register contents
3264  *  identifier		variable value
3265  *  function()		function call
3266  *  $VAR		environment variable
3267  *  (expression)	nested expression
3268  *  [expr, expr]	List
3269  *  {arg, arg -> expr}	Lambda
3270  *  {key: val, key: val}   Dictionary
3271  *  #{key: val, key: val}  Dictionary with literal keys
3272  *
3273  *  Also handle:
3274  *  ! in front		logical NOT
3275  *  - in front		unary minus
3276  *  + in front		unary plus (ignored)
3277  *  trailing []		subscript in String or List
3278  *  trailing .name	entry in Dictionary
3279  *  trailing ->name()	method call
3280  *
3281  * "arg" must point to the first non-white of the expression.
3282  * "arg" is advanced to just after the recognized expression.
3283  *
3284  * Return OK or FAIL.
3285  */
3286     static int
3287 eval7(
3288     char_u	**arg,
3289     typval_T	*rettv,
3290     evalarg_T	*evalarg,
3291     int		want_string)	// after "." operator
3292 {
3293     int		evaluate = evalarg != NULL
3294 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3295     int		len;
3296     char_u	*s;
3297     char_u	*start_leader, *end_leader;
3298     int		ret = OK;
3299     char_u	*alias;
3300 
3301     /*
3302      * Initialise variable so that clear_tv() can't mistake this for a
3303      * string and free a string that isn't there.
3304      */
3305     rettv->v_type = VAR_UNKNOWN;
3306 
3307     /*
3308      * Skip '!', '-' and '+' characters.  They are handled later.
3309      */
3310     start_leader = *arg;
3311     if (eval_leader(arg, in_vim9script()) == FAIL)
3312 	return FAIL;
3313     end_leader = *arg;
3314 
3315     if (**arg == '.' && (!isdigit(*(*arg + 1))
3316 #ifdef FEAT_FLOAT
3317 	    || current_sctx.sc_version < 2
3318 #endif
3319 	    ))
3320     {
3321 	semsg(_(e_invexpr2), *arg);
3322 	++*arg;
3323 	return FAIL;
3324     }
3325 
3326     switch (**arg)
3327     {
3328     /*
3329      * Number constant.
3330      */
3331     case '0':
3332     case '1':
3333     case '2':
3334     case '3':
3335     case '4':
3336     case '5':
3337     case '6':
3338     case '7':
3339     case '8':
3340     case '9':
3341     case '.':	ret = eval_number(arg, rettv, evaluate, want_string);
3342 
3343 		// Apply prefixed "-" and "+" now.  Matters especially when
3344 		// "->" follows.
3345 		if (ret == OK && evaluate && end_leader > start_leader
3346 						  && rettv->v_type != VAR_BLOB)
3347 		    ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
3348 		break;
3349 
3350     /*
3351      * String constant: "string".
3352      */
3353     case '"':	ret = eval_string(arg, rettv, evaluate);
3354 		break;
3355 
3356     /*
3357      * Literal string constant: 'str''ing'.
3358      */
3359     case '\'':	ret = eval_lit_string(arg, rettv, evaluate);
3360 		break;
3361 
3362     /*
3363      * List: [expr, expr]
3364      */
3365     case '[':	ret = eval_list(arg, rettv, evalarg, TRUE);
3366 		break;
3367 
3368     /*
3369      * Dictionary: #{key: val, key: val}
3370      */
3371     case '#':	if (in_vim9script())
3372 		{
3373 		    ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3374 		}
3375 		else if ((*arg)[1] == '{')
3376 		{
3377 		    ++*arg;
3378 		    ret = eval_dict(arg, rettv, evalarg, TRUE);
3379 		}
3380 		else
3381 		    ret = NOTDONE;
3382 		break;
3383 
3384     /*
3385      * Lambda: {arg, arg -> expr}
3386      * Dictionary: {'key': val, 'key': val}
3387      */
3388     case '{':	if (in_vim9script())
3389 		    ret = NOTDONE;
3390 		else
3391 		    ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
3392 		if (ret == NOTDONE)
3393 		    ret = eval_dict(arg, rettv, evalarg, FALSE);
3394 		break;
3395 
3396     /*
3397      * Option value: &name
3398      */
3399     case '&':	ret = eval_option(arg, rettv, evaluate);
3400 		break;
3401 
3402     /*
3403      * Environment variable: $VAR.
3404      */
3405     case '$':	ret = eval_env_var(arg, rettv, evaluate);
3406 		break;
3407 
3408     /*
3409      * Register contents: @r.
3410      */
3411     case '@':	++*arg;
3412 		if (evaluate)
3413 		{
3414 		    if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3415 			semsg(_(e_syntax_error_at_str), *arg);
3416 		    else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3417 			emsg_invreg(**arg);
3418 		    else
3419 		    {
3420 			rettv->v_type = VAR_STRING;
3421 			rettv->vval.v_string = get_reg_contents(**arg,
3422 								GREG_EXPR_SRC);
3423 		    }
3424 		}
3425 		if (**arg != NUL)
3426 		    ++*arg;
3427 		break;
3428 
3429     /*
3430      * nested expression: (expression).
3431      * lambda: (arg) => expr
3432      */
3433     case '(':	ret = NOTDONE;
3434 		if (in_vim9script())
3435 		{
3436 		    ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3437 		    if (ret == OK && evaluate)
3438 		    {
3439 			ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3440 
3441 			// compile it here to get the return type
3442 			if (compile_def_function(ufunc,
3443 					 TRUE, PROFILING(ufunc), NULL) == FAIL)
3444 			{
3445 			    clear_tv(rettv);
3446 			    ret = FAIL;
3447 			}
3448 		    }
3449 		}
3450 		if (ret == NOTDONE)
3451 		{
3452 		    *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3453 		    ret = eval1(arg, rettv, evalarg);	// recursive!
3454 
3455 		    *arg = skipwhite_and_linebreak(*arg, evalarg);
3456 		    if (**arg == ')')
3457 			++*arg;
3458 		    else if (ret == OK)
3459 		    {
3460 			emsg(_(e_missing_close));
3461 			clear_tv(rettv);
3462 			ret = FAIL;
3463 		    }
3464 		}
3465 		break;
3466 
3467     default:	ret = NOTDONE;
3468 		break;
3469     }
3470 
3471     if (ret == NOTDONE)
3472     {
3473 	/*
3474 	 * Must be a variable or function name.
3475 	 * Can also be a curly-braces kind of name: {expr}.
3476 	 */
3477 	s = *arg;
3478 	len = get_name_len(arg, &alias, evaluate, TRUE);
3479 	if (alias != NULL)
3480 	    s = alias;
3481 
3482 	if (len <= 0)
3483 	    ret = FAIL;
3484 	else
3485 	{
3486 	    int	    flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3487 
3488 	    if (evaluate && in_vim9script() && len == 1 && *s == '_')
3489 	    {
3490 		emsg(_(e_cannot_use_underscore_here));
3491 		ret = FAIL;
3492 	    }
3493 	    else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3494 	    {
3495 		// "name(..."  recursive!
3496 		*arg = skipwhite(*arg);
3497 		ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
3498 	    }
3499 	    else if (flags & EVAL_CONSTANT)
3500 		ret = FAIL;
3501 	    else if (evaluate)
3502 	    {
3503 		// get the value of "true", "false" or a variable
3504 		if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3505 		{
3506 		    rettv->v_type = VAR_BOOL;
3507 		    rettv->vval.v_number = VVAL_TRUE;
3508 		    ret = OK;
3509 		}
3510 		else if (len == 5 && in_vim9script()
3511 						&& STRNCMP(s, "false", 5) == 0)
3512 		{
3513 		    rettv->v_type = VAR_BOOL;
3514 		    rettv->vval.v_number = VVAL_FALSE;
3515 		    ret = OK;
3516 		}
3517 		else if (len == 4 && in_vim9script()
3518 						&& STRNCMP(s, "null", 4) == 0)
3519 		{
3520 		    rettv->v_type = VAR_SPECIAL;
3521 		    rettv->vval.v_number = VVAL_NULL;
3522 		    ret = OK;
3523 		}
3524 		else
3525 		    ret = eval_variable(s, len, rettv, NULL,
3526 					   EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
3527 	    }
3528 	    else
3529 	    {
3530 		// skip the name
3531 		check_vars(s, len);
3532 		ret = OK;
3533 	    }
3534 	}
3535 	vim_free(alias);
3536     }
3537 
3538     // Handle following '[', '(' and '.' for expr[expr], expr.name,
3539     // expr(expr), expr->name(expr)
3540     if (ret == OK)
3541 	ret = handle_subscript(arg, rettv, evalarg, TRUE);
3542 
3543     /*
3544      * Apply logical NOT and unary '-', from right to left, ignore '+'.
3545      */
3546     if (ret == OK && evaluate && end_leader > start_leader)
3547 	ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
3548     return ret;
3549 }
3550 
3551 /*
3552  * Apply the leading "!" and "-" before an eval7 expression to "rettv".
3553  * When "numeric_only" is TRUE only handle "+" and "-".
3554  * Adjusts "end_leaderp" until it is at "start_leader".
3555  */
3556     static int
3557 eval7_leader(
3558 	typval_T    *rettv,
3559 	int	    numeric_only,
3560 	char_u	    *start_leader,
3561 	char_u	    **end_leaderp)
3562 {
3563     char_u	*end_leader = *end_leaderp;
3564     int		ret = OK;
3565     int		error = FALSE;
3566     varnumber_T val = 0;
3567     vartype_T	type = rettv->v_type;
3568 #ifdef FEAT_FLOAT
3569     float_T	    f = 0.0;
3570 
3571     if (rettv->v_type == VAR_FLOAT)
3572 	f = rettv->vval.v_float;
3573     else
3574 #endif
3575     {
3576 	while (VIM_ISWHITE(end_leader[-1]))
3577 	    --end_leader;
3578 	if (in_vim9script() && end_leader[-1] == '!')
3579 	    val = tv2bool(rettv);
3580 	else
3581 	    val = tv_get_number_chk(rettv, &error);
3582     }
3583     if (error)
3584     {
3585 	clear_tv(rettv);
3586 	ret = FAIL;
3587     }
3588     else
3589     {
3590 	while (end_leader > start_leader)
3591 	{
3592 	    --end_leader;
3593 	    if (*end_leader == '!')
3594 	    {
3595 		if (numeric_only)
3596 		{
3597 		    ++end_leader;
3598 		    break;
3599 		}
3600 #ifdef FEAT_FLOAT
3601 		if (rettv->v_type == VAR_FLOAT)
3602 		{
3603 		    if (in_vim9script())
3604 		    {
3605 			rettv->v_type = VAR_BOOL;
3606 			val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3607 		    }
3608 		    else
3609 			f = !f;
3610 		}
3611 		else
3612 #endif
3613 		{
3614 		    val = !val;
3615 		    type = VAR_BOOL;
3616 		}
3617 	    }
3618 	    else if (*end_leader == '-')
3619 	    {
3620 #ifdef FEAT_FLOAT
3621 		if (rettv->v_type == VAR_FLOAT)
3622 		    f = -f;
3623 		else
3624 #endif
3625 		{
3626 		    val = -val;
3627 		    type = VAR_NUMBER;
3628 		}
3629 	    }
3630 	}
3631 #ifdef FEAT_FLOAT
3632 	if (rettv->v_type == VAR_FLOAT)
3633 	{
3634 	    clear_tv(rettv);
3635 	    rettv->vval.v_float = f;
3636 	}
3637 	else
3638 #endif
3639 	{
3640 	    clear_tv(rettv);
3641 	    if (in_vim9script())
3642 		rettv->v_type = type;
3643 	    else
3644 		rettv->v_type = VAR_NUMBER;
3645 	    rettv->vval.v_number = val;
3646 	}
3647     }
3648     *end_leaderp = end_leader;
3649     return ret;
3650 }
3651 
3652 /*
3653  * Call the function referred to in "rettv".
3654  */
3655     static int
3656 call_func_rettv(
3657 	char_u	    **arg,
3658 	evalarg_T   *evalarg,
3659 	typval_T    *rettv,
3660 	int	    evaluate,
3661 	dict_T	    *selfdict,
3662 	typval_T    *basetv)
3663 {
3664     partial_T	*pt = NULL;
3665     funcexe_T	funcexe;
3666     typval_T	functv;
3667     char_u	*s;
3668     int		ret;
3669 
3670     // need to copy the funcref so that we can clear rettv
3671     if (evaluate)
3672     {
3673 	functv = *rettv;
3674 	rettv->v_type = VAR_UNKNOWN;
3675 
3676 	// Invoke the function.  Recursive!
3677 	if (functv.v_type == VAR_PARTIAL)
3678 	{
3679 	    pt = functv.vval.v_partial;
3680 	    s = partial_name(pt);
3681 	}
3682 	else
3683 	    s = functv.vval.v_string;
3684     }
3685     else
3686 	s = (char_u *)"";
3687 
3688     CLEAR_FIELD(funcexe);
3689     funcexe.firstline = curwin->w_cursor.lnum;
3690     funcexe.lastline = curwin->w_cursor.lnum;
3691     funcexe.evaluate = evaluate;
3692     funcexe.partial = pt;
3693     funcexe.selfdict = selfdict;
3694     funcexe.basetv = basetv;
3695     ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
3696 
3697     // Clear the funcref afterwards, so that deleting it while
3698     // evaluating the arguments is possible (see test55).
3699     if (evaluate)
3700 	clear_tv(&functv);
3701 
3702     return ret;
3703 }
3704 
3705 /*
3706  * Evaluate "->method()".
3707  * "*arg" points to "method".
3708  * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3709  */
3710     static int
3711 eval_lambda(
3712     char_u	**arg,
3713     typval_T	*rettv,
3714     evalarg_T	*evalarg,
3715     int		verbose)	// give error messages
3716 {
3717     int		evaluate = evalarg != NULL
3718 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3719     typval_T	base = *rettv;
3720     int		ret;
3721 
3722     rettv->v_type = VAR_UNKNOWN;
3723 
3724     if (**arg == '{')
3725     {
3726 	// ->{lambda}()
3727 	ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3728     }
3729     else
3730     {
3731 	// ->(lambda)()
3732 	++*arg;
3733 	ret = eval1(arg, rettv, evalarg);
3734 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3735 	if (**arg != ')')
3736 	{
3737 	    emsg(_(e_missing_close));
3738 	    ret = FAIL;
3739 	}
3740 	++*arg;
3741     }
3742     if (ret != OK)
3743 	return FAIL;
3744     else if (**arg != '(')
3745     {
3746 	if (verbose)
3747 	{
3748 	    if (*skipwhite(*arg) == '(')
3749 		emsg(_(e_nowhitespace));
3750 	    else
3751 		semsg(_(e_missing_paren), "lambda");
3752 	}
3753 	clear_tv(rettv);
3754 	ret = FAIL;
3755     }
3756     else
3757 	ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
3758 
3759     // Clear the funcref afterwards, so that deleting it while
3760     // evaluating the arguments is possible (see test55).
3761     if (evaluate)
3762 	clear_tv(&base);
3763 
3764     return ret;
3765 }
3766 
3767 /*
3768  * Evaluate "->method()".
3769  * "*arg" points to "method".
3770  * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3771  */
3772     static int
3773 eval_method(
3774     char_u	**arg,
3775     typval_T	*rettv,
3776     evalarg_T	*evalarg,
3777     int		verbose)	// give error messages
3778 {
3779     char_u	*name;
3780     long	len;
3781     char_u	*alias;
3782     typval_T	base = *rettv;
3783     int		ret;
3784     int		evaluate = evalarg != NULL
3785 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3786 
3787     rettv->v_type = VAR_UNKNOWN;
3788 
3789     name = *arg;
3790     len = get_name_len(arg, &alias, evaluate, TRUE);
3791     if (alias != NULL)
3792 	name = alias;
3793 
3794     if (len <= 0)
3795     {
3796 	if (verbose)
3797 	    emsg(_("E260: Missing name after ->"));
3798 	ret = FAIL;
3799     }
3800     else
3801     {
3802 	*arg = skipwhite(*arg);
3803 	if (**arg != '(')
3804 	{
3805 	    if (verbose)
3806 		semsg(_(e_missing_paren), name);
3807 	    ret = FAIL;
3808 	}
3809 	else if (VIM_ISWHITE((*arg)[-1]))
3810 	{
3811 	    if (verbose)
3812 		emsg(_(e_nowhitespace));
3813 	    ret = FAIL;
3814 	}
3815 	else
3816 	    ret = eval_func(arg, evalarg, name, len, rettv,
3817 					  evaluate ? EVAL_EVALUATE : 0, &base);
3818     }
3819 
3820     // Clear the funcref afterwards, so that deleting it while
3821     // evaluating the arguments is possible (see test55).
3822     if (evaluate)
3823 	clear_tv(&base);
3824 
3825     return ret;
3826 }
3827 
3828 /*
3829  * Evaluate an "[expr]" or "[expr:expr]" index.  Also "dict.key".
3830  * "*arg" points to the '[' or '.'.
3831  * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3832  */
3833     static int
3834 eval_index(
3835     char_u	**arg,
3836     typval_T	*rettv,
3837     evalarg_T	*evalarg,
3838     int		verbose)	// give error messages
3839 {
3840     int		evaluate = evalarg != NULL
3841 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3842     int		empty1 = FALSE, empty2 = FALSE;
3843     typval_T	var1, var2;
3844     int		range = FALSE;
3845     char_u	*key = NULL;
3846     int		keylen = -1;
3847     int		vim9 = in_vim9script();
3848 
3849     if (check_can_index(rettv, evaluate, verbose) == FAIL)
3850 	return FAIL;
3851 
3852     init_tv(&var1);
3853     init_tv(&var2);
3854     if (**arg == '.')
3855     {
3856 	/*
3857 	 * dict.name
3858 	 */
3859 	key = *arg + 1;
3860 	for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
3861 	    ;
3862 	if (keylen == 0)
3863 	    return FAIL;
3864 	*arg = key + keylen;
3865     }
3866     else
3867     {
3868 	/*
3869 	 * something[idx]
3870 	 *
3871 	 * Get the (first) variable from inside the [].
3872 	 */
3873 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3874 	if (**arg == ':')
3875 	    empty1 = TRUE;
3876 	else if (eval1(arg, &var1, evalarg) == FAIL)	// recursive!
3877 	    return FAIL;
3878 	else if (vim9 && **arg == ':')
3879 	{
3880 	    semsg(_(e_white_space_required_before_and_after_str_at_str),
3881 								    ":", *arg);
3882 	    clear_tv(&var1);
3883 	    return FAIL;
3884 	}
3885 	else if (evaluate)
3886 	{
3887 #ifdef FEAT_FLOAT
3888 	    // allow for indexing with float
3889 	    if (vim9 && rettv->v_type == VAR_DICT
3890 						   && var1.v_type == VAR_FLOAT)
3891 	    {
3892 		var1.vval.v_string = typval_tostring(&var1, TRUE);
3893 		var1.v_type = VAR_STRING;
3894 	    }
3895 #endif
3896 	    if (tv_get_string_chk(&var1) == NULL)
3897 	    {
3898 		// not a number or string
3899 		clear_tv(&var1);
3900 		return FAIL;
3901 	    }
3902 	}
3903 
3904 	/*
3905 	 * Get the second variable from inside the [:].
3906 	 */
3907 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3908 	if (**arg == ':')
3909 	{
3910 	    range = TRUE;
3911 	    ++*arg;
3912 	    if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
3913 	    {
3914 		semsg(_(e_white_space_required_before_and_after_str_at_str),
3915 								":", *arg - 1);
3916 		if (!empty1)
3917 		    clear_tv(&var1);
3918 		return FAIL;
3919 	    }
3920 	    *arg = skipwhite_and_linebreak(*arg, evalarg);
3921 	    if (**arg == ']')
3922 		empty2 = TRUE;
3923 	    else if (eval1(arg, &var2, evalarg) == FAIL)	// recursive!
3924 	    {
3925 		if (!empty1)
3926 		    clear_tv(&var1);
3927 		return FAIL;
3928 	    }
3929 	    else if (evaluate && tv_get_string_chk(&var2) == NULL)
3930 	    {
3931 		// not a number or string
3932 		if (!empty1)
3933 		    clear_tv(&var1);
3934 		clear_tv(&var2);
3935 		return FAIL;
3936 	    }
3937 	}
3938 
3939 	// Check for the ']'.
3940 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3941 	if (**arg != ']')
3942 	{
3943 	    if (verbose)
3944 		emsg(_(e_missbrac));
3945 	    clear_tv(&var1);
3946 	    if (range)
3947 		clear_tv(&var2);
3948 	    return FAIL;
3949 	}
3950 	*arg = *arg + 1;	// skip over the ']'
3951     }
3952 
3953     if (evaluate)
3954     {
3955 	int res = eval_index_inner(rettv, range,
3956 		empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
3957 		key, keylen, verbose);
3958 
3959 	if (!empty1)
3960 	    clear_tv(&var1);
3961 	if (range)
3962 	    clear_tv(&var2);
3963 	return res;
3964     }
3965     return OK;
3966 }
3967 
3968 /*
3969  * Check if "rettv" can have an [index] or [sli:ce]
3970  */
3971     int
3972 check_can_index(typval_T *rettv, int evaluate, int verbose)
3973 {
3974     switch (rettv->v_type)
3975     {
3976 	case VAR_FUNC:
3977 	case VAR_PARTIAL:
3978 	    if (verbose)
3979 		emsg(_("E695: Cannot index a Funcref"));
3980 	    return FAIL;
3981 	case VAR_FLOAT:
3982 #ifdef FEAT_FLOAT
3983 	    if (verbose)
3984 		emsg(_(e_float_as_string));
3985 	    return FAIL;
3986 #endif
3987 	case VAR_BOOL:
3988 	case VAR_SPECIAL:
3989 	case VAR_JOB:
3990 	case VAR_CHANNEL:
3991 	    if (verbose)
3992 		emsg(_(e_cannot_index_special_variable));
3993 	    return FAIL;
3994 	case VAR_UNKNOWN:
3995 	case VAR_ANY:
3996 	case VAR_VOID:
3997 	    if (evaluate)
3998 	    {
3999 		emsg(_(e_cannot_index_special_variable));
4000 		return FAIL;
4001 	    }
4002 	    // FALLTHROUGH
4003 
4004 	case VAR_STRING:
4005 	case VAR_LIST:
4006 	case VAR_DICT:
4007 	case VAR_BLOB:
4008 	    break;
4009 	case VAR_NUMBER:
4010 	    if (in_vim9script())
4011 		emsg(_(e_cannot_index_number));
4012 	    break;
4013     }
4014     return OK;
4015 }
4016 
4017 /*
4018  * slice() function
4019  */
4020     void
4021 f_slice(typval_T *argvars, typval_T *rettv)
4022 {
4023     if (check_can_index(argvars, TRUE, FALSE) == OK)
4024     {
4025 	copy_tv(argvars, rettv);
4026 	eval_index_inner(rettv, TRUE, argvars + 1,
4027 		argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4028 		TRUE, NULL, 0, FALSE);
4029     }
4030 }
4031 
4032 /*
4033  * Apply index or range to "rettv".
4034  * "var1" is the first index, NULL for [:expr].
4035  * "var2" is the second index, NULL for [expr] and [expr: ]
4036  * "exclusive" is TRUE for slice(): second index is exclusive, use character
4037  * index for string.
4038  * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4039  */
4040     int
4041 eval_index_inner(
4042 	typval_T    *rettv,
4043 	int	    is_range,
4044 	typval_T    *var1,
4045 	typval_T    *var2,
4046 	int	    exclusive,
4047 	char_u	    *key,
4048 	int	    keylen,
4049 	int	    verbose)
4050 {
4051     varnumber_T	    n1, n2 = 0;
4052     long	    len;
4053 
4054     n1 = 0;
4055     if (var1 != NULL && rettv->v_type != VAR_DICT)
4056 	n1 = tv_get_number(var1);
4057 
4058     if (is_range)
4059     {
4060 	if (rettv->v_type == VAR_DICT)
4061 	{
4062 	    if (verbose)
4063 		emsg(_(e_cannot_slice_dictionary));
4064 	    return FAIL;
4065 	}
4066 	if (var2 != NULL)
4067 	    n2 = tv_get_number(var2);
4068 	else
4069 	    n2 = VARNUM_MAX;
4070     }
4071 
4072     switch (rettv->v_type)
4073     {
4074 	case VAR_UNKNOWN:
4075 	case VAR_ANY:
4076 	case VAR_VOID:
4077 	case VAR_FUNC:
4078 	case VAR_PARTIAL:
4079 	case VAR_FLOAT:
4080 	case VAR_BOOL:
4081 	case VAR_SPECIAL:
4082 	case VAR_JOB:
4083 	case VAR_CHANNEL:
4084 	    break; // not evaluating, skipping over subscript
4085 
4086 	case VAR_NUMBER:
4087 	case VAR_STRING:
4088 	    {
4089 		char_u	*s = tv_get_string(rettv);
4090 
4091 		len = (long)STRLEN(s);
4092 		if (in_vim9script() || exclusive)
4093 		{
4094 		    if (is_range)
4095 			s = string_slice(s, n1, n2, exclusive);
4096 		    else
4097 			s = char_from_string(s, n1);
4098 		}
4099 		else if (is_range)
4100 		{
4101 		    // The resulting variable is a substring.  If the indexes
4102 		    // are out of range the result is empty.
4103 		    if (n1 < 0)
4104 		    {
4105 			n1 = len + n1;
4106 			if (n1 < 0)
4107 			    n1 = 0;
4108 		    }
4109 		    if (n2 < 0)
4110 			n2 = len + n2;
4111 		    else if (n2 >= len)
4112 			n2 = len;
4113 		    if (n1 >= len || n2 < 0 || n1 > n2)
4114 			s = NULL;
4115 		    else
4116 			s = vim_strnsave(s + n1, n2 - n1 + 1);
4117 		}
4118 		else
4119 		{
4120 		    // The resulting variable is a string of a single
4121 		    // character.  If the index is too big or negative the
4122 		    // result is empty.
4123 		    if (n1 >= len || n1 < 0)
4124 			s = NULL;
4125 		    else
4126 			s = vim_strnsave(s + n1, 1);
4127 		}
4128 		clear_tv(rettv);
4129 		rettv->v_type = VAR_STRING;
4130 		rettv->vval.v_string = s;
4131 	    }
4132 	    break;
4133 
4134 	case VAR_BLOB:
4135 	    blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4136 							     exclusive, rettv);
4137 	    break;
4138 
4139 	case VAR_LIST:
4140 	    if (var1 == NULL)
4141 		n1 = 0;
4142 	    if (var2 == NULL)
4143 		n2 = VARNUM_MAX;
4144 	    if (list_slice_or_index(rettv->vval.v_list,
4145 			  is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
4146 		return FAIL;
4147 	    break;
4148 
4149 	case VAR_DICT:
4150 	    {
4151 		dictitem_T	*item;
4152 		typval_T	tmp;
4153 
4154 		if (key == NULL)
4155 		{
4156 		    key = tv_get_string_chk(var1);
4157 		    if (key == NULL)
4158 			return FAIL;
4159 		}
4160 
4161 		item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4162 
4163 		if (item == NULL && verbose)
4164 		    semsg(_(e_dictkey), key);
4165 		if (item == NULL)
4166 		    return FAIL;
4167 
4168 		copy_tv(&item->di_tv, &tmp);
4169 		clear_tv(rettv);
4170 		*rettv = tmp;
4171 	    }
4172 	    break;
4173     }
4174     return OK;
4175 }
4176 
4177 /*
4178  * Return the function name of partial "pt".
4179  */
4180     char_u *
4181 partial_name(partial_T *pt)
4182 {
4183     if (pt->pt_name != NULL)
4184 	return pt->pt_name;
4185     if (pt->pt_func != NULL)
4186 	return pt->pt_func->uf_name;
4187     return (char_u *)"";
4188 }
4189 
4190     static void
4191 partial_free(partial_T *pt)
4192 {
4193     int i;
4194 
4195     for (i = 0; i < pt->pt_argc; ++i)
4196 	clear_tv(&pt->pt_argv[i]);
4197     vim_free(pt->pt_argv);
4198     dict_unref(pt->pt_dict);
4199     if (pt->pt_name != NULL)
4200     {
4201 	func_unref(pt->pt_name);
4202 	vim_free(pt->pt_name);
4203     }
4204     else
4205 	func_ptr_unref(pt->pt_func);
4206 
4207     // Decrease the reference count for the context of a closure.  If down
4208     // to the minimum it may be time to free it.
4209     if (pt->pt_funcstack != NULL)
4210     {
4211 	--pt->pt_funcstack->fs_refcount;
4212 	funcstack_check_refcount(pt->pt_funcstack);
4213     }
4214 
4215     vim_free(pt);
4216 }
4217 
4218 /*
4219  * Unreference a closure: decrement the reference count and free it when it
4220  * becomes zero.
4221  */
4222     void
4223 partial_unref(partial_T *pt)
4224 {
4225     if (pt != NULL)
4226     {
4227 	if (--pt->pt_refcount <= 0)
4228 	    partial_free(pt);
4229 
4230 	// If the reference count goes down to one, the funcstack may be the
4231 	// only reference and can be freed if no other partials reference it.
4232 	else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4233 	    funcstack_check_refcount(pt->pt_funcstack);
4234     }
4235 }
4236 
4237 /*
4238  * Return the next (unique) copy ID.
4239  * Used for serializing nested structures.
4240  */
4241     int
4242 get_copyID(void)
4243 {
4244     current_copyID += COPYID_INC;
4245     return current_copyID;
4246 }
4247 
4248 /*
4249  * Garbage collection for lists and dictionaries.
4250  *
4251  * We use reference counts to be able to free most items right away when they
4252  * are no longer used.  But for composite items it's possible that it becomes
4253  * unused while the reference count is > 0: When there is a recursive
4254  * reference.  Example:
4255  *	:let l = [1, 2, 3]
4256  *	:let d = {9: l}
4257  *	:let l[1] = d
4258  *
4259  * Since this is quite unusual we handle this with garbage collection: every
4260  * once in a while find out which lists and dicts are not referenced from any
4261  * variable.
4262  *
4263  * Here is a good reference text about garbage collection (refers to Python
4264  * but it applies to all reference-counting mechanisms):
4265  *	http://python.ca/nas/python/gc/
4266  */
4267 
4268 /*
4269  * Do garbage collection for lists and dicts.
4270  * When "testing" is TRUE this is called from test_garbagecollect_now().
4271  * Return TRUE if some memory was freed.
4272  */
4273     int
4274 garbage_collect(int testing)
4275 {
4276     int		copyID;
4277     int		abort = FALSE;
4278     buf_T	*buf;
4279     win_T	*wp;
4280     int		did_free = FALSE;
4281     tabpage_T	*tp;
4282 
4283     if (!testing)
4284     {
4285 	// Only do this once.
4286 	want_garbage_collect = FALSE;
4287 	may_garbage_collect = FALSE;
4288 	garbage_collect_at_exit = FALSE;
4289     }
4290 
4291     // The execution stack can grow big, limit the size.
4292     if (exestack.ga_maxlen - exestack.ga_len > 500)
4293     {
4294 	size_t	new_len;
4295 	char_u	*pp;
4296 	int	n;
4297 
4298 	// Keep 150% of the current size, with a minimum of the growth size.
4299 	n = exestack.ga_len / 2;
4300 	if (n < exestack.ga_growsize)
4301 	    n = exestack.ga_growsize;
4302 
4303 	// Don't make it bigger though.
4304 	if (exestack.ga_len + n < exestack.ga_maxlen)
4305 	{
4306 	    new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4307 	    pp = vim_realloc(exestack.ga_data, new_len);
4308 	    if (pp == NULL)
4309 		return FAIL;
4310 	    exestack.ga_maxlen = exestack.ga_len + n;
4311 	    exestack.ga_data = pp;
4312 	}
4313     }
4314 
4315     // We advance by two because we add one for items referenced through
4316     // previous_funccal.
4317     copyID = get_copyID();
4318 
4319     /*
4320      * 1. Go through all accessible variables and mark all lists and dicts
4321      *    with copyID.
4322      */
4323 
4324     // Don't free variables in the previous_funccal list unless they are only
4325     // referenced through previous_funccal.  This must be first, because if
4326     // the item is referenced elsewhere the funccal must not be freed.
4327     abort = abort || set_ref_in_previous_funccal(copyID);
4328 
4329     // script-local variables
4330     abort = abort || garbage_collect_scriptvars(copyID);
4331 
4332     // buffer-local variables
4333     FOR_ALL_BUFFERS(buf)
4334 	abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4335 								  NULL, NULL);
4336 
4337     // window-local variables
4338     FOR_ALL_TAB_WINDOWS(tp, wp)
4339 	abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4340 								  NULL, NULL);
4341     if (aucmd_win != NULL)
4342 	abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4343 								  NULL, NULL);
4344 #ifdef FEAT_PROP_POPUP
4345     FOR_ALL_POPUPWINS(wp)
4346 	abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4347 								  NULL, NULL);
4348     FOR_ALL_TABPAGES(tp)
4349 	FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
4350 		abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4351 								  NULL, NULL);
4352 #endif
4353 
4354     // tabpage-local variables
4355     FOR_ALL_TABPAGES(tp)
4356 	abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4357 								  NULL, NULL);
4358     // global variables
4359     abort = abort || garbage_collect_globvars(copyID);
4360 
4361     // function-local variables
4362     abort = abort || set_ref_in_call_stack(copyID);
4363 
4364     // named functions (matters for closures)
4365     abort = abort || set_ref_in_functions(copyID);
4366 
4367     // function call arguments, if v:testing is set.
4368     abort = abort || set_ref_in_func_args(copyID);
4369 
4370     // v: vars
4371     abort = abort || garbage_collect_vimvars(copyID);
4372 
4373     // callbacks in buffers
4374     abort = abort || set_ref_in_buffers(copyID);
4375 
4376 #ifdef FEAT_LUA
4377     abort = abort || set_ref_in_lua(copyID);
4378 #endif
4379 
4380 #ifdef FEAT_PYTHON
4381     abort = abort || set_ref_in_python(copyID);
4382 #endif
4383 
4384 #ifdef FEAT_PYTHON3
4385     abort = abort || set_ref_in_python3(copyID);
4386 #endif
4387 
4388 #ifdef FEAT_JOB_CHANNEL
4389     abort = abort || set_ref_in_channel(copyID);
4390     abort = abort || set_ref_in_job(copyID);
4391 #endif
4392 #ifdef FEAT_NETBEANS_INTG
4393     abort = abort || set_ref_in_nb_channel(copyID);
4394 #endif
4395 
4396 #ifdef FEAT_TIMERS
4397     abort = abort || set_ref_in_timer(copyID);
4398 #endif
4399 
4400 #ifdef FEAT_QUICKFIX
4401     abort = abort || set_ref_in_quickfix(copyID);
4402 #endif
4403 
4404 #ifdef FEAT_TERMINAL
4405     abort = abort || set_ref_in_term(copyID);
4406 #endif
4407 
4408 #ifdef FEAT_PROP_POPUP
4409     abort = abort || set_ref_in_popups(copyID);
4410 #endif
4411 
4412     if (!abort)
4413     {
4414 	/*
4415 	 * 2. Free lists and dictionaries that are not referenced.
4416 	 */
4417 	did_free = free_unref_items(copyID);
4418 
4419 	/*
4420 	 * 3. Check if any funccal can be freed now.
4421 	 *    This may call us back recursively.
4422 	 */
4423 	free_unref_funccal(copyID, testing);
4424     }
4425     else if (p_verbose > 0)
4426     {
4427 	verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
4428     }
4429 
4430     return did_free;
4431 }
4432 
4433 /*
4434  * Free lists, dictionaries, channels and jobs that are no longer referenced.
4435  */
4436     static int
4437 free_unref_items(int copyID)
4438 {
4439     int		did_free = FALSE;
4440 
4441     // Let all "free" functions know that we are here.  This means no
4442     // dictionaries, lists, channels or jobs are to be freed, because we will
4443     // do that here.
4444     in_free_unref_items = TRUE;
4445 
4446     /*
4447      * PASS 1: free the contents of the items.  We don't free the items
4448      * themselves yet, so that it is possible to decrement refcount counters
4449      */
4450 
4451     // Go through the list of dicts and free items without the copyID.
4452     did_free |= dict_free_nonref(copyID);
4453 
4454     // Go through the list of lists and free items without the copyID.
4455     did_free |= list_free_nonref(copyID);
4456 
4457 #ifdef FEAT_JOB_CHANNEL
4458     // Go through the list of jobs and free items without the copyID. This
4459     // must happen before doing channels, because jobs refer to channels, but
4460     // the reference from the channel to the job isn't tracked.
4461     did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4462 
4463     // Go through the list of channels and free items without the copyID.
4464     did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4465 #endif
4466 
4467     /*
4468      * PASS 2: free the items themselves.
4469      */
4470     dict_free_items(copyID);
4471     list_free_items(copyID);
4472 
4473 #ifdef FEAT_JOB_CHANNEL
4474     // Go through the list of jobs and free items without the copyID. This
4475     // must happen before doing channels, because jobs refer to channels, but
4476     // the reference from the channel to the job isn't tracked.
4477     free_unused_jobs(copyID, COPYID_MASK);
4478 
4479     // Go through the list of channels and free items without the copyID.
4480     free_unused_channels(copyID, COPYID_MASK);
4481 #endif
4482 
4483     in_free_unref_items = FALSE;
4484 
4485     return did_free;
4486 }
4487 
4488 /*
4489  * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
4490  * "list_stack" is used to add lists to be marked.  Can be NULL.
4491  *
4492  * Returns TRUE if setting references failed somehow.
4493  */
4494     int
4495 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
4496 {
4497     int		todo;
4498     int		abort = FALSE;
4499     hashitem_T	*hi;
4500     hashtab_T	*cur_ht;
4501     ht_stack_T	*ht_stack = NULL;
4502     ht_stack_T	*tempitem;
4503 
4504     cur_ht = ht;
4505     for (;;)
4506     {
4507 	if (!abort)
4508 	{
4509 	    // Mark each item in the hashtab.  If the item contains a hashtab
4510 	    // it is added to ht_stack, if it contains a list it is added to
4511 	    // list_stack.
4512 	    todo = (int)cur_ht->ht_used;
4513 	    for (hi = cur_ht->ht_array; todo > 0; ++hi)
4514 		if (!HASHITEM_EMPTY(hi))
4515 		{
4516 		    --todo;
4517 		    abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4518 						       &ht_stack, list_stack);
4519 		}
4520 	}
4521 
4522 	if (ht_stack == NULL)
4523 	    break;
4524 
4525 	// take an item from the stack
4526 	cur_ht = ht_stack->ht;
4527 	tempitem = ht_stack;
4528 	ht_stack = ht_stack->prev;
4529 	free(tempitem);
4530     }
4531 
4532     return abort;
4533 }
4534 
4535 /*
4536  * Mark a dict and its items with "copyID".
4537  * Returns TRUE if setting references failed somehow.
4538  */
4539     int
4540 set_ref_in_dict(dict_T *d, int copyID)
4541 {
4542     if (d != NULL && d->dv_copyID != copyID)
4543     {
4544 	d->dv_copyID = copyID;
4545 	return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4546     }
4547     return FALSE;
4548 }
4549 
4550 /*
4551  * Mark a list and its items with "copyID".
4552  * Returns TRUE if setting references failed somehow.
4553  */
4554     int
4555 set_ref_in_list(list_T *ll, int copyID)
4556 {
4557     if (ll != NULL && ll->lv_copyID != copyID)
4558     {
4559 	ll->lv_copyID = copyID;
4560 	return set_ref_in_list_items(ll, copyID, NULL);
4561     }
4562     return FALSE;
4563 }
4564 
4565 /*
4566  * Mark all lists and dicts referenced through list "l" with "copyID".
4567  * "ht_stack" is used to add hashtabs to be marked.  Can be NULL.
4568  *
4569  * Returns TRUE if setting references failed somehow.
4570  */
4571     int
4572 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
4573 {
4574     listitem_T	 *li;
4575     int		 abort = FALSE;
4576     list_T	 *cur_l;
4577     list_stack_T *list_stack = NULL;
4578     list_stack_T *tempitem;
4579 
4580     cur_l = l;
4581     for (;;)
4582     {
4583 	if (!abort && cur_l->lv_first != &range_list_item)
4584 	    // Mark each item in the list.  If the item contains a hashtab
4585 	    // it is added to ht_stack, if it contains a list it is added to
4586 	    // list_stack.
4587 	    for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4588 		abort = abort || set_ref_in_item(&li->li_tv, copyID,
4589 						       ht_stack, &list_stack);
4590 	if (list_stack == NULL)
4591 	    break;
4592 
4593 	// take an item from the stack
4594 	cur_l = list_stack->list;
4595 	tempitem = list_stack;
4596 	list_stack = list_stack->prev;
4597 	free(tempitem);
4598     }
4599 
4600     return abort;
4601 }
4602 
4603 /*
4604  * Mark all lists and dicts referenced through typval "tv" with "copyID".
4605  * "list_stack" is used to add lists to be marked.  Can be NULL.
4606  * "ht_stack" is used to add hashtabs to be marked.  Can be NULL.
4607  *
4608  * Returns TRUE if setting references failed somehow.
4609  */
4610     int
4611 set_ref_in_item(
4612     typval_T	    *tv,
4613     int		    copyID,
4614     ht_stack_T	    **ht_stack,
4615     list_stack_T    **list_stack)
4616 {
4617     int		abort = FALSE;
4618 
4619     if (tv->v_type == VAR_DICT)
4620     {
4621 	dict_T	*dd = tv->vval.v_dict;
4622 
4623 	if (dd != NULL && dd->dv_copyID != copyID)
4624 	{
4625 	    // Didn't see this dict yet.
4626 	    dd->dv_copyID = copyID;
4627 	    if (ht_stack == NULL)
4628 	    {
4629 		abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4630 	    }
4631 	    else
4632 	    {
4633 		ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4634 
4635 		if (newitem == NULL)
4636 		    abort = TRUE;
4637 		else
4638 		{
4639 		    newitem->ht = &dd->dv_hashtab;
4640 		    newitem->prev = *ht_stack;
4641 		    *ht_stack = newitem;
4642 		}
4643 	    }
4644 	}
4645     }
4646     else if (tv->v_type == VAR_LIST)
4647     {
4648 	list_T	*ll = tv->vval.v_list;
4649 
4650 	if (ll != NULL && ll->lv_copyID != copyID)
4651 	{
4652 	    // Didn't see this list yet.
4653 	    ll->lv_copyID = copyID;
4654 	    if (list_stack == NULL)
4655 	    {
4656 		abort = set_ref_in_list_items(ll, copyID, ht_stack);
4657 	    }
4658 	    else
4659 	    {
4660 		list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4661 
4662 		if (newitem == NULL)
4663 		    abort = TRUE;
4664 		else
4665 		{
4666 		    newitem->list = ll;
4667 		    newitem->prev = *list_stack;
4668 		    *list_stack = newitem;
4669 		}
4670 	    }
4671 	}
4672     }
4673     else if (tv->v_type == VAR_FUNC)
4674     {
4675 	abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
4676     }
4677     else if (tv->v_type == VAR_PARTIAL)
4678     {
4679 	partial_T	*pt = tv->vval.v_partial;
4680 	int		i;
4681 
4682 	if (pt != NULL && pt->pt_copyID != copyID)
4683 	{
4684 	    // Didn't see this partial yet.
4685 	    pt->pt_copyID = copyID;
4686 
4687 	    abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
4688 
4689 	    if (pt->pt_dict != NULL)
4690 	    {
4691 		typval_T dtv;
4692 
4693 		dtv.v_type = VAR_DICT;
4694 		dtv.vval.v_dict = pt->pt_dict;
4695 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4696 	    }
4697 
4698 	    for (i = 0; i < pt->pt_argc; ++i)
4699 		abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4700 							ht_stack, list_stack);
4701 	    if (pt->pt_funcstack != NULL)
4702 	    {
4703 		typval_T    *stack = pt->pt_funcstack->fs_ga.ga_data;
4704 
4705 		for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4706 		    abort = abort || set_ref_in_item(stack + i, copyID,
4707 							 ht_stack, list_stack);
4708 	    }
4709 
4710 	}
4711     }
4712 #ifdef FEAT_JOB_CHANNEL
4713     else if (tv->v_type == VAR_JOB)
4714     {
4715 	job_T	    *job = tv->vval.v_job;
4716 	typval_T    dtv;
4717 
4718 	if (job != NULL && job->jv_copyID != copyID)
4719 	{
4720 	    job->jv_copyID = copyID;
4721 	    if (job->jv_channel != NULL)
4722 	    {
4723 		dtv.v_type = VAR_CHANNEL;
4724 		dtv.vval.v_channel = job->jv_channel;
4725 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4726 	    }
4727 	    if (job->jv_exit_cb.cb_partial != NULL)
4728 	    {
4729 		dtv.v_type = VAR_PARTIAL;
4730 		dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
4731 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4732 	    }
4733 	}
4734     }
4735     else if (tv->v_type == VAR_CHANNEL)
4736     {
4737 	channel_T   *ch =tv->vval.v_channel;
4738 	ch_part_T   part;
4739 	typval_T    dtv;
4740 	jsonq_T	    *jq;
4741 	cbq_T	    *cq;
4742 
4743 	if (ch != NULL && ch->ch_copyID != copyID)
4744 	{
4745 	    ch->ch_copyID = copyID;
4746 	    for (part = PART_SOCK; part < PART_COUNT; ++part)
4747 	    {
4748 		for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4749 							     jq = jq->jq_next)
4750 		    set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4751 		for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4752 							     cq = cq->cq_next)
4753 		    if (cq->cq_callback.cb_partial != NULL)
4754 		    {
4755 			dtv.v_type = VAR_PARTIAL;
4756 			dtv.vval.v_partial = cq->cq_callback.cb_partial;
4757 			set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4758 		    }
4759 		if (ch->ch_part[part].ch_callback.cb_partial != NULL)
4760 		{
4761 		    dtv.v_type = VAR_PARTIAL;
4762 		    dtv.vval.v_partial =
4763 				      ch->ch_part[part].ch_callback.cb_partial;
4764 		    set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4765 		}
4766 	    }
4767 	    if (ch->ch_callback.cb_partial != NULL)
4768 	    {
4769 		dtv.v_type = VAR_PARTIAL;
4770 		dtv.vval.v_partial = ch->ch_callback.cb_partial;
4771 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4772 	    }
4773 	    if (ch->ch_close_cb.cb_partial != NULL)
4774 	    {
4775 		dtv.v_type = VAR_PARTIAL;
4776 		dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
4777 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4778 	    }
4779 	}
4780     }
4781 #endif
4782     return abort;
4783 }
4784 
4785 /*
4786  * Return a string with the string representation of a variable.
4787  * If the memory is allocated "tofree" is set to it, otherwise NULL.
4788  * "numbuf" is used for a number.
4789  * When "copyID" is not NULL replace recursive lists and dicts with "...".
4790  * When both "echo_style" and "composite_val" are FALSE, put quotes around
4791  * strings as "string()", otherwise does not put quotes around strings, as
4792  * ":echo" displays values.
4793  * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4794  * are replaced with "...".
4795  * May return NULL.
4796  */
4797     char_u *
4798 echo_string_core(
4799     typval_T	*tv,
4800     char_u	**tofree,
4801     char_u	*numbuf,
4802     int		copyID,
4803     int		echo_style,
4804     int		restore_copyID,
4805     int		composite_val)
4806 {
4807     static int	recurse = 0;
4808     char_u	*r = NULL;
4809 
4810     if (recurse >= DICT_MAXNEST)
4811     {
4812 	if (!did_echo_string_emsg)
4813 	{
4814 	    // Only give this message once for a recursive call to avoid
4815 	    // flooding the user with errors.  And stop iterating over lists
4816 	    // and dicts.
4817 	    did_echo_string_emsg = TRUE;
4818 	    emsg(_("E724: variable nested too deep for displaying"));
4819 	}
4820 	*tofree = NULL;
4821 	return (char_u *)"{E724}";
4822     }
4823     ++recurse;
4824 
4825     switch (tv->v_type)
4826     {
4827 	case VAR_STRING:
4828 	    if (echo_style && !composite_val)
4829 	    {
4830 		*tofree = NULL;
4831 		r = tv->vval.v_string;
4832 		if (r == NULL)
4833 		    r = (char_u *)"";
4834 	    }
4835 	    else
4836 	    {
4837 		*tofree = string_quote(tv->vval.v_string, FALSE);
4838 		r = *tofree;
4839 	    }
4840 	    break;
4841 
4842 	case VAR_FUNC:
4843 	    if (echo_style)
4844 	    {
4845 		*tofree = NULL;
4846 		r = tv->vval.v_string;
4847 	    }
4848 	    else
4849 	    {
4850 		*tofree = string_quote(tv->vval.v_string, TRUE);
4851 		r = *tofree;
4852 	    }
4853 	    break;
4854 
4855 	case VAR_PARTIAL:
4856 	    {
4857 		partial_T   *pt = tv->vval.v_partial;
4858 		char_u	    *fname = string_quote(pt == NULL ? NULL
4859 						    : partial_name(pt), FALSE);
4860 		garray_T    ga;
4861 		int	    i;
4862 		char_u	    *tf;
4863 
4864 		ga_init2(&ga, 1, 100);
4865 		ga_concat(&ga, (char_u *)"function(");
4866 		if (fname != NULL)
4867 		{
4868 		    ga_concat(&ga, fname);
4869 		    vim_free(fname);
4870 		}
4871 		if (pt != NULL && pt->pt_argc > 0)
4872 		{
4873 		    ga_concat(&ga, (char_u *)", [");
4874 		    for (i = 0; i < pt->pt_argc; ++i)
4875 		    {
4876 			if (i > 0)
4877 			    ga_concat(&ga, (char_u *)", ");
4878 			ga_concat(&ga,
4879 			     tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4880 			vim_free(tf);
4881 		    }
4882 		    ga_concat(&ga, (char_u *)"]");
4883 		}
4884 		if (pt != NULL && pt->pt_dict != NULL)
4885 		{
4886 		    typval_T dtv;
4887 
4888 		    ga_concat(&ga, (char_u *)", ");
4889 		    dtv.v_type = VAR_DICT;
4890 		    dtv.vval.v_dict = pt->pt_dict;
4891 		    ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4892 		    vim_free(tf);
4893 		}
4894 		ga_concat(&ga, (char_u *)")");
4895 
4896 		*tofree = ga.ga_data;
4897 		r = *tofree;
4898 		break;
4899 	    }
4900 
4901 	case VAR_BLOB:
4902 	    r = blob2string(tv->vval.v_blob, tofree, numbuf);
4903 	    break;
4904 
4905 	case VAR_LIST:
4906 	    if (tv->vval.v_list == NULL)
4907 	    {
4908 		// NULL list is equivalent to empty list.
4909 		*tofree = NULL;
4910 		r = (char_u *)"[]";
4911 	    }
4912 	    else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4913 		    && tv->vval.v_list->lv_len > 0)
4914 	    {
4915 		*tofree = NULL;
4916 		r = (char_u *)"[...]";
4917 	    }
4918 	    else
4919 	    {
4920 		int old_copyID = tv->vval.v_list->lv_copyID;
4921 
4922 		tv->vval.v_list->lv_copyID = copyID;
4923 		*tofree = list2string(tv, copyID, restore_copyID);
4924 		if (restore_copyID)
4925 		    tv->vval.v_list->lv_copyID = old_copyID;
4926 		r = *tofree;
4927 	    }
4928 	    break;
4929 
4930 	case VAR_DICT:
4931 	    if (tv->vval.v_dict == NULL)
4932 	    {
4933 		// NULL dict is equivalent to empty dict.
4934 		*tofree = NULL;
4935 		r = (char_u *)"{}";
4936 	    }
4937 	    else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4938 		    && tv->vval.v_dict->dv_hashtab.ht_used != 0)
4939 	    {
4940 		*tofree = NULL;
4941 		r = (char_u *)"{...}";
4942 	    }
4943 	    else
4944 	    {
4945 		int old_copyID = tv->vval.v_dict->dv_copyID;
4946 
4947 		tv->vval.v_dict->dv_copyID = copyID;
4948 		*tofree = dict2string(tv, copyID, restore_copyID);
4949 		if (restore_copyID)
4950 		    tv->vval.v_dict->dv_copyID = old_copyID;
4951 		r = *tofree;
4952 	    }
4953 	    break;
4954 
4955 	case VAR_NUMBER:
4956 	case VAR_UNKNOWN:
4957 	case VAR_ANY:
4958 	case VAR_VOID:
4959 	    *tofree = NULL;
4960 	    r = tv_get_string_buf(tv, numbuf);
4961 	    break;
4962 
4963 	case VAR_JOB:
4964 	case VAR_CHANNEL:
4965 	    *tofree = NULL;
4966 	    r = tv_get_string_buf(tv, numbuf);
4967 	    if (composite_val)
4968 	    {
4969 		*tofree = string_quote(r, FALSE);
4970 		r = *tofree;
4971 	    }
4972 	    break;
4973 
4974 	case VAR_FLOAT:
4975 #ifdef FEAT_FLOAT
4976 	    *tofree = NULL;
4977 	    vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4978 	    r = numbuf;
4979 	    break;
4980 #endif
4981 
4982 	case VAR_BOOL:
4983 	case VAR_SPECIAL:
4984 	    *tofree = NULL;
4985 	    r = (char_u *)get_var_special_name(tv->vval.v_number);
4986 	    break;
4987     }
4988 
4989     if (--recurse == 0)
4990 	did_echo_string_emsg = FALSE;
4991     return r;
4992 }
4993 
4994 /*
4995  * Return a string with the string representation of a variable.
4996  * If the memory is allocated "tofree" is set to it, otherwise NULL.
4997  * "numbuf" is used for a number.
4998  * Does not put quotes around strings, as ":echo" displays values.
4999  * When "copyID" is not NULL replace recursive lists and dicts with "...".
5000  * May return NULL.
5001  */
5002     char_u *
5003 echo_string(
5004     typval_T	*tv,
5005     char_u	**tofree,
5006     char_u	*numbuf,
5007     int		copyID)
5008 {
5009     return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5010 }
5011 
5012 /*
5013  * Return string "str" in ' quotes, doubling ' characters.
5014  * If "str" is NULL an empty string is assumed.
5015  * If "function" is TRUE make it function('string').
5016  */
5017     char_u *
5018 string_quote(char_u *str, int function)
5019 {
5020     unsigned	len;
5021     char_u	*p, *r, *s;
5022 
5023     len = (function ? 13 : 3);
5024     if (str != NULL)
5025     {
5026 	len += (unsigned)STRLEN(str);
5027 	for (p = str; *p != NUL; MB_PTR_ADV(p))
5028 	    if (*p == '\'')
5029 		++len;
5030     }
5031     s = r = alloc(len);
5032     if (r != NULL)
5033     {
5034 	if (function)
5035 	{
5036 	    STRCPY(r, "function('");
5037 	    r += 10;
5038 	}
5039 	else
5040 	    *r++ = '\'';
5041 	if (str != NULL)
5042 	    for (p = str; *p != NUL; )
5043 	    {
5044 		if (*p == '\'')
5045 		    *r++ = '\'';
5046 		MB_COPY_CHAR(p, r);
5047 	    }
5048 	*r++ = '\'';
5049 	if (function)
5050 	    *r++ = ')';
5051 	*r++ = NUL;
5052     }
5053     return s;
5054 }
5055 
5056 #if defined(FEAT_FLOAT) || defined(PROTO)
5057 /*
5058  * Convert the string "text" to a floating point number.
5059  * This uses strtod().  setlocale(LC_NUMERIC, "C") has been used to make sure
5060  * this always uses a decimal point.
5061  * Returns the length of the text that was consumed.
5062  */
5063     int
5064 string2float(
5065     char_u	*text,
5066     float_T	*value)	    // result stored here
5067 {
5068     char	*s = (char *)text;
5069     float_T	f;
5070 
5071     // MS-Windows does not deal with "inf" and "nan" properly.
5072     if (STRNICMP(text, "inf", 3) == 0)
5073     {
5074 	*value = INFINITY;
5075 	return 3;
5076     }
5077     if (STRNICMP(text, "-inf", 3) == 0)
5078     {
5079 	*value = -INFINITY;
5080 	return 4;
5081     }
5082     if (STRNICMP(text, "nan", 3) == 0)
5083     {
5084 	*value = NAN;
5085 	return 3;
5086     }
5087     f = strtod(s, &s);
5088     *value = f;
5089     return (int)((char_u *)s - text);
5090 }
5091 #endif
5092 
5093 /*
5094  * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5095  * character index.  Works only for loaded buffers. Returns -1 on failure.
5096  * The index of the first byte and the first character is zero.
5097  */
5098     int
5099 buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5100 {
5101     char_u	*str;
5102     char_u	*t;
5103     int		count;
5104 
5105     if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5106 	return -1;
5107 
5108     if (lnum > buf->b_ml.ml_line_count)
5109 	lnum = buf->b_ml.ml_line_count;
5110 
5111     str = ml_get_buf(buf, lnum, FALSE);
5112     if (str == NULL)
5113 	return -1;
5114 
5115     if (*str == NUL)
5116 	return 0;
5117 
5118     // count the number of characters
5119     t = str;
5120     for (count = 0; *t != NUL && t <= str + byteidx; count++)
5121 	t += mb_ptr2len(t);
5122 
5123     // In insert mode, when the cursor is at the end of a non-empty line,
5124     // byteidx points to the NUL character immediately past the end of the
5125     // string. In this case, add one to the character count.
5126     if (*t == NUL && byteidx != 0 && t == str + byteidx)
5127 	count++;
5128 
5129     return count - 1;
5130 }
5131 
5132 /*
5133  * Convert the specified character index of line 'lnum' in buffer 'buf' to a
5134  * byte index.  Works only for loaded buffers. Returns -1 on failure.
5135  * The index of the first byte and the first character is zero.
5136  */
5137     int
5138 buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5139 {
5140     char_u	*str;
5141     char_u	*t;
5142 
5143     if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5144 	return -1;
5145 
5146     if (lnum > buf->b_ml.ml_line_count)
5147 	lnum = buf->b_ml.ml_line_count;
5148 
5149     str = ml_get_buf(buf, lnum, FALSE);
5150     if (str == NULL)
5151 	return -1;
5152 
5153     // Convert the character offset to a byte offset
5154     t = str;
5155     while (*t != NUL && --charidx > 0)
5156 	t += mb_ptr2len(t);
5157 
5158     return t - str;
5159 }
5160 
5161 /*
5162  * Translate a String variable into a position.
5163  * Returns NULL when there is an error.
5164  */
5165     pos_T *
5166 var2fpos(
5167     typval_T	*varp,
5168     int		dollar_lnum,	// TRUE when $ is last line
5169     int		*fnum,		// set to fnum for '0, 'A, etc.
5170     int		charcol)	// return character column
5171 {
5172     char_u		*name;
5173     static pos_T	pos;
5174     pos_T		*pp;
5175 
5176     // Argument can be [lnum, col, coladd].
5177     if (varp->v_type == VAR_LIST)
5178     {
5179 	list_T		*l;
5180 	int		len;
5181 	int		error = FALSE;
5182 	listitem_T	*li;
5183 
5184 	l = varp->vval.v_list;
5185 	if (l == NULL)
5186 	    return NULL;
5187 
5188 	// Get the line number
5189 	pos.lnum = list_find_nr(l, 0L, &error);
5190 	if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
5191 	    return NULL;	// invalid line number
5192 	if (charcol)
5193 	    len = (long)mb_charlen(ml_get(pos.lnum));
5194 	else
5195 	    len = (long)STRLEN(ml_get(pos.lnum));
5196 
5197 	// Get the column number
5198 	// We accept "$" for the column number: last column.
5199 	li = list_find(l, 1L);
5200 	if (li != NULL && li->li_tv.v_type == VAR_STRING
5201 		&& li->li_tv.vval.v_string != NULL
5202 		&& STRCMP(li->li_tv.vval.v_string, "$") == 0)
5203 	{
5204 	    pos.col = len + 1;
5205 	}
5206 	else
5207 	{
5208 	    pos.col = list_find_nr(l, 1L, &error);
5209 	    if (error)
5210 		return NULL;
5211 	}
5212 
5213 	// Accept a position up to the NUL after the line.
5214 	if (pos.col == 0 || (int)pos.col > len + 1)
5215 	    return NULL;	// invalid column number
5216 	--pos.col;
5217 
5218 	// Get the virtual offset.  Defaults to zero.
5219 	pos.coladd = list_find_nr(l, 2L, &error);
5220 	if (error)
5221 	    pos.coladd = 0;
5222 
5223 	return &pos;
5224     }
5225 
5226     if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5227 	return NULL;
5228 
5229     name = tv_get_string_chk(varp);
5230     if (name == NULL)
5231 	return NULL;
5232     if (name[0] == '.')				// cursor
5233     {
5234 	pos = curwin->w_cursor;
5235 	if (charcol)
5236 	    pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5237 	return &pos;
5238     }
5239     if (name[0] == 'v' && name[1] == NUL)	// Visual start
5240     {
5241 	if (VIsual_active)
5242 	    pos = VIsual;
5243 	else
5244 	    pos = curwin->w_cursor;
5245 	if (charcol)
5246 	    pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5247 	return &pos;
5248     }
5249     if (name[0] == '\'')			// mark
5250     {
5251 	pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
5252 	if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5253 	    return NULL;
5254 	if (charcol)
5255 	    pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
5256 	return pp;
5257     }
5258 
5259     pos.coladd = 0;
5260 
5261     if (name[0] == 'w' && dollar_lnum)
5262     {
5263 	pos.col = 0;
5264 	if (name[1] == '0')		// "w0": first visible line
5265 	{
5266 	    update_topline();
5267 	    // In silent Ex mode topline is zero, but that's not a valid line
5268 	    // number; use one instead.
5269 	    pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
5270 	    return &pos;
5271 	}
5272 	else if (name[1] == '$')	// "w$": last visible line
5273 	{
5274 	    validate_botline();
5275 	    // In silent Ex mode botline is zero, return zero then.
5276 	    pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
5277 	    return &pos;
5278 	}
5279     }
5280     else if (name[0] == '$')		// last column or line
5281     {
5282 	if (dollar_lnum)
5283 	{
5284 	    pos.lnum = curbuf->b_ml.ml_line_count;
5285 	    pos.col = 0;
5286 	}
5287 	else
5288 	{
5289 	    pos.lnum = curwin->w_cursor.lnum;
5290 	    if (charcol)
5291 		pos.col = (colnr_T)mb_charlen(ml_get_curline());
5292 	    else
5293 		pos.col = (colnr_T)STRLEN(ml_get_curline());
5294 	}
5295 	return &pos;
5296     }
5297     return NULL;
5298 }
5299 
5300 /*
5301  * Convert list in "arg" into a position and optional file number.
5302  * When "fnump" is NULL there is no file number, only 3 items.
5303  * Note that the column is passed on as-is, the caller may want to decrement
5304  * it to use 1 for the first column.
5305  * Return FAIL when conversion is not possible, doesn't check the position for
5306  * validity.
5307  */
5308     int
5309 list2fpos(
5310     typval_T	*arg,
5311     pos_T	*posp,
5312     int		*fnump,
5313     colnr_T	*curswantp,
5314     int		charcol)
5315 {
5316     list_T	*l = arg->vval.v_list;
5317     long	i = 0;
5318     long	n;
5319 
5320     // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5321     // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
5322     if (arg->v_type != VAR_LIST
5323 	    || l == NULL
5324 	    || l->lv_len < (fnump == NULL ? 2 : 3)
5325 	    || l->lv_len > (fnump == NULL ? 4 : 5))
5326 	return FAIL;
5327 
5328     if (fnump != NULL)
5329     {
5330 	n = list_find_nr(l, i++, NULL);	// fnum
5331 	if (n < 0)
5332 	    return FAIL;
5333 	if (n == 0)
5334 	    n = curbuf->b_fnum;		// current buffer
5335 	*fnump = n;
5336     }
5337 
5338     n = list_find_nr(l, i++, NULL);	// lnum
5339     if (n < 0)
5340 	return FAIL;
5341     posp->lnum = n;
5342 
5343     n = list_find_nr(l, i++, NULL);	// col
5344     if (n < 0)
5345 	return FAIL;
5346     // If character position is specified, then convert to byte position
5347     if (charcol)
5348     {
5349 	buf_T	*buf;
5350 
5351 	// Get the text for the specified line in a loaded buffer
5352 	buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5353 	if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5354 	    return FAIL;
5355 
5356 	n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
5357     }
5358     posp->col = n;
5359 
5360     n = list_find_nr(l, i, NULL);	// off
5361     if (n < 0)
5362 	posp->coladd = 0;
5363     else
5364 	posp->coladd = n;
5365 
5366     if (curswantp != NULL)
5367 	*curswantp = list_find_nr(l, i + 1, NULL);  // curswant
5368 
5369     return OK;
5370 }
5371 
5372 /*
5373  * Get the length of an environment variable name.
5374  * Advance "arg" to the first character after the name.
5375  * Return 0 for error.
5376  */
5377     int
5378 get_env_len(char_u **arg)
5379 {
5380     char_u	*p;
5381     int		len;
5382 
5383     for (p = *arg; vim_isIDc(*p); ++p)
5384 	;
5385     if (p == *arg)	    // no name found
5386 	return 0;
5387 
5388     len = (int)(p - *arg);
5389     *arg = p;
5390     return len;
5391 }
5392 
5393 /*
5394  * Get the length of the name of a function or internal variable.
5395  * "arg" is advanced to after the name.
5396  * Return 0 if something is wrong.
5397  */
5398     int
5399 get_id_len(char_u **arg)
5400 {
5401     char_u	*p;
5402     int		len;
5403 
5404     // Find the end of the name.
5405     for (p = *arg; eval_isnamec(*p); ++p)
5406     {
5407 	if (*p == ':')
5408 	{
5409 	    // "s:" is start of "s:var", but "n:" is not and can be used in
5410 	    // slice "[n:]".  Also "xx:" is not a namespace.
5411 	    len = (int)(p - *arg);
5412 	    if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5413 		    || len > 1)
5414 		break;
5415 	}
5416     }
5417     if (p == *arg)	    // no name found
5418 	return 0;
5419 
5420     len = (int)(p - *arg);
5421     *arg = p;
5422 
5423     return len;
5424 }
5425 
5426 /*
5427  * Get the length of the name of a variable or function.
5428  * Only the name is recognized, does not handle ".key" or "[idx]".
5429  * "arg" is advanced to the first non-white character after the name.
5430  * Return -1 if curly braces expansion failed.
5431  * Return 0 if something else is wrong.
5432  * If the name contains 'magic' {}'s, expand them and return the
5433  * expanded name in an allocated string via 'alias' - caller must free.
5434  */
5435     int
5436 get_name_len(
5437     char_u	**arg,
5438     char_u	**alias,
5439     int		evaluate,
5440     int		verbose)
5441 {
5442     int		len;
5443     char_u	*p;
5444     char_u	*expr_start;
5445     char_u	*expr_end;
5446 
5447     *alias = NULL;  // default to no alias
5448 
5449     if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5450 						  && (*arg)[2] == (int)KE_SNR)
5451     {
5452 	// hard coded <SNR>, already translated
5453 	*arg += 3;
5454 	return get_id_len(arg) + 3;
5455     }
5456     len = eval_fname_script(*arg);
5457     if (len > 0)
5458     {
5459 	// literal "<SID>", "s:" or "<SNR>"
5460 	*arg += len;
5461     }
5462 
5463     /*
5464      * Find the end of the name; check for {} construction.
5465      */
5466     p = find_name_end(*arg, &expr_start, &expr_end,
5467 					       len > 0 ? 0 : FNE_CHECK_START);
5468     if (expr_start != NULL)
5469     {
5470 	char_u	*temp_string;
5471 
5472 	if (!evaluate)
5473 	{
5474 	    len += (int)(p - *arg);
5475 	    *arg = skipwhite(p);
5476 	    return len;
5477 	}
5478 
5479 	/*
5480 	 * Include any <SID> etc in the expanded string:
5481 	 * Thus the -len here.
5482 	 */
5483 	temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5484 	if (temp_string == NULL)
5485 	    return -1;
5486 	*alias = temp_string;
5487 	*arg = skipwhite(p);
5488 	return (int)STRLEN(temp_string);
5489     }
5490 
5491     len += get_id_len(arg);
5492     // Only give an error when there is something, otherwise it will be
5493     // reported at a higher level.
5494     if (len == 0 && verbose && **arg != NUL)
5495 	semsg(_(e_invexpr2), *arg);
5496 
5497     return len;
5498 }
5499 
5500 /*
5501  * Find the end of a variable or function name, taking care of magic braces.
5502  * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5503  * start and end of the first magic braces item.
5504  * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
5505  * Return a pointer to just after the name.  Equal to "arg" if there is no
5506  * valid name.
5507  */
5508     char_u *
5509 find_name_end(
5510     char_u	*arg,
5511     char_u	**expr_start,
5512     char_u	**expr_end,
5513     int		flags)
5514 {
5515     int		mb_nest = 0;
5516     int		br_nest = 0;
5517     char_u	*p;
5518     int		len;
5519     int		vim9script = in_vim9script();
5520 
5521     if (expr_start != NULL)
5522     {
5523 	*expr_start = NULL;
5524 	*expr_end = NULL;
5525     }
5526 
5527     // Quick check for valid starting character.
5528     if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5529 						&& (*arg != '{' || vim9script))
5530 	return arg;
5531 
5532     for (p = arg; *p != NUL
5533 		    && (eval_isnamec(*p)
5534 			|| (*p == '{' && !vim9script)
5535 			|| ((flags & FNE_INCL_BR) && (*p == '['
5536 					 || (*p == '.' && eval_isdictc(p[1]))))
5537 			|| mb_nest != 0
5538 			|| br_nest != 0); MB_PTR_ADV(p))
5539     {
5540 	if (*p == '\'')
5541 	{
5542 	    // skip over 'string' to avoid counting [ and ] inside it.
5543 	    for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
5544 		;
5545 	    if (*p == NUL)
5546 		break;
5547 	}
5548 	else if (*p == '"')
5549 	{
5550 	    // skip over "str\"ing" to avoid counting [ and ] inside it.
5551 	    for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
5552 		if (*p == '\\' && p[1] != NUL)
5553 		    ++p;
5554 	    if (*p == NUL)
5555 		break;
5556 	}
5557 	else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5558 	{
5559 	    // "s:" is start of "s:var", but "n:" is not and can be used in
5560 	    // slice "[n:]".  Also "xx:" is not a namespace. But {ns}: is.
5561 	    len = (int)(p - arg);
5562 	    if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
5563 		    || (len > 1 && p[-1] != '}'))
5564 		break;
5565 	}
5566 
5567 	if (mb_nest == 0)
5568 	{
5569 	    if (*p == '[')
5570 		++br_nest;
5571 	    else if (*p == ']')
5572 		--br_nest;
5573 	}
5574 
5575 	if (br_nest == 0 && !vim9script)
5576 	{
5577 	    if (*p == '{')
5578 	    {
5579 		mb_nest++;
5580 		if (expr_start != NULL && *expr_start == NULL)
5581 		    *expr_start = p;
5582 	    }
5583 	    else if (*p == '}')
5584 	    {
5585 		mb_nest--;
5586 		if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5587 		    *expr_end = p;
5588 	    }
5589 	}
5590     }
5591 
5592     return p;
5593 }
5594 
5595 /*
5596  * Expands out the 'magic' {}'s in a variable/function name.
5597  * Note that this can call itself recursively, to deal with
5598  * constructs like foo{bar}{baz}{bam}
5599  * The four pointer arguments point to "foo{expre}ss{ion}bar"
5600  *			"in_start"      ^
5601  *			"expr_start"	   ^
5602  *			"expr_end"		 ^
5603  *			"in_end"			    ^
5604  *
5605  * Returns a new allocated string, which the caller must free.
5606  * Returns NULL for failure.
5607  */
5608     static char_u *
5609 make_expanded_name(
5610     char_u	*in_start,
5611     char_u	*expr_start,
5612     char_u	*expr_end,
5613     char_u	*in_end)
5614 {
5615     char_u	c1;
5616     char_u	*retval = NULL;
5617     char_u	*temp_result;
5618 
5619     if (expr_end == NULL || in_end == NULL)
5620 	return NULL;
5621     *expr_start	= NUL;
5622     *expr_end = NUL;
5623     c1 = *in_end;
5624     *in_end = NUL;
5625 
5626     temp_result = eval_to_string(expr_start + 1, FALSE);
5627     if (temp_result != NULL)
5628     {
5629 	retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5630 						   + (in_end - expr_end) + 1);
5631 	if (retval != NULL)
5632 	{
5633 	    STRCPY(retval, in_start);
5634 	    STRCAT(retval, temp_result);
5635 	    STRCAT(retval, expr_end + 1);
5636 	}
5637     }
5638     vim_free(temp_result);
5639 
5640     *in_end = c1;		// put char back for error messages
5641     *expr_start = '{';
5642     *expr_end = '}';
5643 
5644     if (retval != NULL)
5645     {
5646 	temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
5647 	if (expr_start != NULL)
5648 	{
5649 	    // Further expansion!
5650 	    temp_result = make_expanded_name(retval, expr_start,
5651 						       expr_end, temp_result);
5652 	    vim_free(retval);
5653 	    retval = temp_result;
5654 	}
5655     }
5656 
5657     return retval;
5658 }
5659 
5660 /*
5661  * Return TRUE if character "c" can be used in a variable or function name.
5662  * Does not include '{' or '}' for magic braces.
5663  */
5664     int
5665 eval_isnamec(int c)
5666 {
5667     return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
5668 }
5669 
5670 /*
5671  * Return TRUE if character "c" can be used as the first character in a
5672  * variable or function name (excluding '{' and '}').
5673  */
5674     int
5675 eval_isnamec1(int c)
5676 {
5677     return ASCII_ISALPHA(c) || c == '_';
5678 }
5679 
5680 /*
5681  * Return TRUE if character "c" can be used as the first character of a
5682  * dictionary key.
5683  */
5684     int
5685 eval_isdictc(int c)
5686 {
5687     return ASCII_ISALNUM(c) || c == '_';
5688 }
5689 
5690 /*
5691  * Handle:
5692  * - expr[expr], expr[expr:expr] subscript
5693  * - ".name" lookup
5694  * - function call with Funcref variable: func(expr)
5695  * - method call: var->method()
5696  *
5697  * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
5698  */
5699     int
5700 handle_subscript(
5701     char_u	**arg,
5702     typval_T	*rettv,
5703     evalarg_T	*evalarg,
5704     int		verbose)	// give error messages
5705 {
5706     int		evaluate = evalarg != NULL
5707 				      && (evalarg->eval_flags & EVAL_EVALUATE);
5708     int		ret = OK;
5709     dict_T	*selfdict = NULL;
5710     int		check_white = TRUE;
5711     int		getnext;
5712     char_u	*p;
5713 
5714     while (ret == OK)
5715     {
5716 	// When at the end of the line and ".name" or "->{" or "->X" follows in
5717 	// the next line then consume the line break.
5718 	p = eval_next_non_blank(*arg, evalarg, &getnext);
5719 	if (getnext
5720 	    && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
5721 		|| (p[0] == '-' && p[1] == '>'
5722 				     && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
5723 	{
5724 	    *arg = eval_next_line(evalarg);
5725 	    p = *arg;
5726 	    check_white = FALSE;
5727 	}
5728 
5729 	if (rettv->v_type == VAR_ANY)
5730 	{
5731 	    char_u	*exp_name;
5732 	    int		cc;
5733 	    int		idx;
5734 	    ufunc_T	*ufunc;
5735 	    type_T	*type;
5736 
5737 	    // Found script from "import * as {name}", script item name must
5738 	    // follow.
5739 	    if (**arg != '.')
5740 	    {
5741 		if (verbose)
5742 		    semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5743 		ret = FAIL;
5744 		break;
5745 	    }
5746 	    ++*arg;
5747 	    if (IS_WHITE_OR_NUL(**arg))
5748 	    {
5749 		if (verbose)
5750 		    emsg(_(e_no_white_space_allowed_after_dot));
5751 		ret = FAIL;
5752 		break;
5753 	    }
5754 
5755 	    // isolate the name
5756 	    exp_name = *arg;
5757 	    while (eval_isnamec(**arg))
5758 		++*arg;
5759 	    cc = **arg;
5760 	    **arg = NUL;
5761 
5762 	    idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5763 						  evalarg->eval_cctx, verbose);
5764 	    **arg = cc;
5765 	    *arg = skipwhite(*arg);
5766 
5767 	    if (idx < 0 && ufunc == NULL)
5768 	    {
5769 		ret = FAIL;
5770 		break;
5771 	    }
5772 	    if (idx >= 0)
5773 	    {
5774 		scriptitem_T    *si = SCRIPT_ITEM(rettv->vval.v_number);
5775 		svar_T		*sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5776 
5777 		copy_tv(sv->sv_tv, rettv);
5778 	    }
5779 	    else
5780 	    {
5781 		rettv->v_type = VAR_FUNC;
5782 		rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5783 	    }
5784 	}
5785 
5786 	if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5787 			    || rettv->v_type == VAR_PARTIAL))
5788 		    && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
5789 	{
5790 	    ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5791 							       selfdict, NULL);
5792 
5793 	    // Stop the expression evaluation when immediately aborting on
5794 	    // error, or when an interrupt occurred or an exception was thrown
5795 	    // but not caught.
5796 	    if (aborting())
5797 	    {
5798 		if (ret == OK)
5799 		    clear_tv(rettv);
5800 		ret = FAIL;
5801 	    }
5802 	    dict_unref(selfdict);
5803 	    selfdict = NULL;
5804 	}
5805 	else if (p[0] == '-' && p[1] == '>')
5806 	{
5807 	    *arg = skipwhite(p + 2);
5808 	    if (ret == OK)
5809 	    {
5810 		if ((**arg == '{' && !in_vim9script()) || **arg == '(')
5811 		    // expr->{lambda}() or expr->(lambda)()
5812 		    ret = eval_lambda(arg, rettv, evalarg, verbose);
5813 		else
5814 		    // expr->name()
5815 		    ret = eval_method(arg, rettv, evalarg, verbose);
5816 	    }
5817 	}
5818 	// "." is ".name" lookup when we found a dict or when evaluating and
5819 	// scriptversion is at least 2, where string concatenation is "..".
5820 	else if (**arg == '['
5821 		|| (**arg == '.' && (rettv->v_type == VAR_DICT
5822 			|| (!evaluate
5823 			    && (*arg)[1] != '.'
5824 			    && current_sctx.sc_version >= 2))))
5825 	{
5826 	    dict_unref(selfdict);
5827 	    if (rettv->v_type == VAR_DICT)
5828 	    {
5829 		selfdict = rettv->vval.v_dict;
5830 		if (selfdict != NULL)
5831 		    ++selfdict->dv_refcount;
5832 	    }
5833 	    else
5834 		selfdict = NULL;
5835 	    if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
5836 	    {
5837 		clear_tv(rettv);
5838 		ret = FAIL;
5839 	    }
5840 	}
5841 	else
5842 	    break;
5843     }
5844 
5845     // Turn "dict.Func" into a partial for "Func" bound to "dict".
5846     // Don't do this when "Func" is already a partial that was bound
5847     // explicitly (pt_auto is FALSE).
5848     if (selfdict != NULL
5849 	    && (rettv->v_type == VAR_FUNC
5850 		|| (rettv->v_type == VAR_PARTIAL
5851 		    && (rettv->vval.v_partial->pt_auto
5852 			|| rettv->vval.v_partial->pt_dict == NULL))))
5853 	selfdict = make_partial(selfdict, rettv);
5854 
5855     dict_unref(selfdict);
5856     return ret;
5857 }
5858 
5859 /*
5860  * Make a copy of an item.
5861  * Lists and Dictionaries are also copied.  A deep copy if "deep" is set.
5862  * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5863  * reference to an already copied list/dict can be used.
5864  * Returns FAIL or OK.
5865  */
5866     int
5867 item_copy(
5868     typval_T	*from,
5869     typval_T	*to,
5870     int		deep,
5871     int		copyID)
5872 {
5873     static int	recurse = 0;
5874     int		ret = OK;
5875 
5876     if (recurse >= DICT_MAXNEST)
5877     {
5878 	emsg(_("E698: variable nested too deep for making a copy"));
5879 	return FAIL;
5880     }
5881     ++recurse;
5882 
5883     switch (from->v_type)
5884     {
5885 	case VAR_NUMBER:
5886 	case VAR_FLOAT:
5887 	case VAR_STRING:
5888 	case VAR_FUNC:
5889 	case VAR_PARTIAL:
5890 	case VAR_BOOL:
5891 	case VAR_SPECIAL:
5892 	case VAR_JOB:
5893 	case VAR_CHANNEL:
5894 	    copy_tv(from, to);
5895 	    break;
5896 	case VAR_LIST:
5897 	    to->v_type = VAR_LIST;
5898 	    to->v_lock = 0;
5899 	    if (from->vval.v_list == NULL)
5900 		to->vval.v_list = NULL;
5901 	    else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5902 	    {
5903 		// use the copy made earlier
5904 		to->vval.v_list = from->vval.v_list->lv_copylist;
5905 		++to->vval.v_list->lv_refcount;
5906 	    }
5907 	    else
5908 		to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5909 	    if (to->vval.v_list == NULL)
5910 		ret = FAIL;
5911 	    break;
5912 	case VAR_BLOB:
5913 	    ret = blob_copy(from->vval.v_blob, to);
5914 	    break;
5915 	case VAR_DICT:
5916 	    to->v_type = VAR_DICT;
5917 	    to->v_lock = 0;
5918 	    if (from->vval.v_dict == NULL)
5919 		to->vval.v_dict = NULL;
5920 	    else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5921 	    {
5922 		// use the copy made earlier
5923 		to->vval.v_dict = from->vval.v_dict->dv_copydict;
5924 		++to->vval.v_dict->dv_refcount;
5925 	    }
5926 	    else
5927 		to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5928 	    if (to->vval.v_dict == NULL)
5929 		ret = FAIL;
5930 	    break;
5931 	case VAR_UNKNOWN:
5932 	case VAR_ANY:
5933 	case VAR_VOID:
5934 	    internal_error_no_abort("item_copy(UNKNOWN)");
5935 	    ret = FAIL;
5936     }
5937     --recurse;
5938     return ret;
5939 }
5940 
5941     void
5942 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5943 {
5944     char_u	*tofree;
5945     char_u	numbuf[NUMBUFLEN];
5946     char_u	*p = echo_string(rettv, &tofree, numbuf, get_copyID());
5947 
5948     if (*atstart)
5949     {
5950 	*atstart = FALSE;
5951 	// Call msg_start() after eval1(), evaluating the expression
5952 	// may cause a message to appear.
5953 	if (with_space)
5954 	{
5955 	    // Mark the saved text as finishing the line, so that what
5956 	    // follows is displayed on a new line when scrolling back
5957 	    // at the more prompt.
5958 	    msg_sb_eol();
5959 	    msg_start();
5960 	}
5961     }
5962     else if (with_space)
5963 	msg_puts_attr(" ", echo_attr);
5964 
5965     if (p != NULL)
5966 	for ( ; *p != NUL && !got_int; ++p)
5967 	{
5968 	    if (*p == '\n' || *p == '\r' || *p == TAB)
5969 	    {
5970 		if (*p != TAB && *needclr)
5971 		{
5972 		    // remove any text still there from the command
5973 		    msg_clr_eos();
5974 		    *needclr = FALSE;
5975 		}
5976 		msg_putchar_attr(*p, echo_attr);
5977 	    }
5978 	    else
5979 	    {
5980 		if (has_mbyte)
5981 		{
5982 		    int i = (*mb_ptr2len)(p);
5983 
5984 		    (void)msg_outtrans_len_attr(p, i, echo_attr);
5985 		    p += i - 1;
5986 		}
5987 		else
5988 		    (void)msg_outtrans_len_attr(p, 1, echo_attr);
5989 	    }
5990 	}
5991     vim_free(tofree);
5992 }
5993 
5994 /*
5995  * ":echo expr1 ..."	print each argument separated with a space, add a
5996  *			newline at the end.
5997  * ":echon expr1 ..."	print each argument plain.
5998  */
5999     void
6000 ex_echo(exarg_T *eap)
6001 {
6002     char_u	*arg = eap->arg;
6003     typval_T	rettv;
6004     char_u	*p;
6005     int		needclr = TRUE;
6006     int		atstart = TRUE;
6007     int		did_emsg_before = did_emsg;
6008     int		called_emsg_before = called_emsg;
6009     evalarg_T	evalarg;
6010 
6011     fill_evalarg_from_eap(&evalarg, eap, eap->skip);
6012 
6013     if (eap->skip)
6014 	++emsg_skip;
6015     while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
6016     {
6017 	// If eval1() causes an error message the text from the command may
6018 	// still need to be cleared. E.g., "echo 22,44".
6019 	need_clr_eos = needclr;
6020 
6021 	p = arg;
6022 	if (eval1(&arg, &rettv, &evalarg) == FAIL)
6023 	{
6024 	    /*
6025 	     * Report the invalid expression unless the expression evaluation
6026 	     * has been cancelled due to an aborting error, an interrupt, or an
6027 	     * exception.
6028 	     */
6029 	    if (!aborting() && did_emsg == did_emsg_before
6030 					  && called_emsg == called_emsg_before)
6031 		semsg(_(e_invexpr2), p);
6032 	    need_clr_eos = FALSE;
6033 	    break;
6034 	}
6035 	need_clr_eos = FALSE;
6036 
6037 	if (!eap->skip)
6038 	    echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
6039 
6040 	clear_tv(&rettv);
6041 	arg = skipwhite(arg);
6042     }
6043     eap->nextcmd = check_nextcmd(arg);
6044     clear_evalarg(&evalarg, eap);
6045 
6046     if (eap->skip)
6047 	--emsg_skip;
6048     else
6049     {
6050 	// remove text that may still be there from the command
6051 	if (needclr)
6052 	    msg_clr_eos();
6053 	if (eap->cmdidx == CMD_echo)
6054 	    msg_end();
6055     }
6056 }
6057 
6058 /*
6059  * ":echohl {name}".
6060  */
6061     void
6062 ex_echohl(exarg_T *eap)
6063 {
6064     echo_attr = syn_name2attr(eap->arg);
6065 }
6066 
6067 /*
6068  * Returns the :echo attribute
6069  */
6070     int
6071 get_echo_attr(void)
6072 {
6073     return echo_attr;
6074 }
6075 
6076 /*
6077  * ":execute expr1 ..."	execute the result of an expression.
6078  * ":echomsg expr1 ..."	Print a message
6079  * ":echoerr expr1 ..."	Print an error
6080  * ":echoconsole expr1 ..." Print a message on stdout
6081  * Each gets spaces around each argument and a newline at the end for
6082  * echo commands
6083  */
6084     void
6085 ex_execute(exarg_T *eap)
6086 {
6087     char_u	*arg = eap->arg;
6088     typval_T	rettv;
6089     int		ret = OK;
6090     char_u	*p;
6091     garray_T	ga;
6092     int		len;
6093 
6094     ga_init2(&ga, 1, 80);
6095 
6096     if (eap->skip)
6097 	++emsg_skip;
6098     while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
6099     {
6100 	ret = eval1_emsg(&arg, &rettv, eap);
6101 	if (ret == FAIL)
6102 	    break;
6103 
6104 	if (!eap->skip)
6105 	{
6106 	    char_u   buf[NUMBUFLEN];
6107 
6108 	    if (eap->cmdidx == CMD_execute)
6109 	    {
6110 		if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6111 		{
6112 		    emsg(_(e_inval_string));
6113 		    p = NULL;
6114 		}
6115 		else
6116 		    p = tv_get_string_buf(&rettv, buf);
6117 	    }
6118 	    else
6119 		p = tv_stringify(&rettv, buf);
6120 	    if (p == NULL)
6121 	    {
6122 		clear_tv(&rettv);
6123 		ret = FAIL;
6124 		break;
6125 	    }
6126 	    len = (int)STRLEN(p);
6127 	    if (ga_grow(&ga, len + 2) == FAIL)
6128 	    {
6129 		clear_tv(&rettv);
6130 		ret = FAIL;
6131 		break;
6132 	    }
6133 	    if (ga.ga_len)
6134 		((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
6135 	    STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
6136 	    ga.ga_len += len;
6137 	}
6138 
6139 	clear_tv(&rettv);
6140 	arg = skipwhite(arg);
6141     }
6142 
6143     if (ret != FAIL && ga.ga_data != NULL)
6144     {
6145 	if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6146 	{
6147 	    // Mark the already saved text as finishing the line, so that what
6148 	    // follows is displayed on a new line when scrolling back at the
6149 	    // more prompt.
6150 	    msg_sb_eol();
6151 	}
6152 
6153 	if (eap->cmdidx == CMD_echomsg)
6154 	{
6155 	    msg_attr(ga.ga_data, echo_attr);
6156 	    out_flush();
6157 	}
6158 	else if (eap->cmdidx == CMD_echoconsole)
6159 	{
6160 	    ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6161 	    ui_write((char_u *)"\r\n", 2, TRUE);
6162 	}
6163 	else if (eap->cmdidx == CMD_echoerr)
6164 	{
6165 	    int		save_did_emsg = did_emsg;
6166 
6167 	    // We don't want to abort following commands, restore did_emsg.
6168 	    emsg(ga.ga_data);
6169 	    if (!force_abort)
6170 		did_emsg = save_did_emsg;
6171 	}
6172 	else if (eap->cmdidx == CMD_execute)
6173 	    do_cmdline((char_u *)ga.ga_data,
6174 		       eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6175     }
6176 
6177     ga_clear(&ga);
6178 
6179     if (eap->skip)
6180 	--emsg_skip;
6181 
6182     eap->nextcmd = check_nextcmd(arg);
6183 }
6184 
6185 /*
6186  * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6187  * "arg" points to the "&" or '+' when called, to "option" when returning.
6188  * Returns NULL when no option name found.  Otherwise pointer to the char
6189  * after the option name.
6190  */
6191     char_u *
6192 find_option_end(char_u **arg, int *opt_flags)
6193 {
6194     char_u	*p = *arg;
6195 
6196     ++p;
6197     if (*p == 'g' && p[1] == ':')
6198     {
6199 	*opt_flags = OPT_GLOBAL;
6200 	p += 2;
6201     }
6202     else if (*p == 'l' && p[1] == ':')
6203     {
6204 	*opt_flags = OPT_LOCAL;
6205 	p += 2;
6206     }
6207     else
6208 	*opt_flags = 0;
6209 
6210     if (!ASCII_ISALPHA(*p))
6211 	return NULL;
6212     *arg = p;
6213 
6214     if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
6215 	p += 4;	    // termcap option
6216     else
6217 	while (ASCII_ISALPHA(*p))
6218 	    ++p;
6219     return p;
6220 }
6221 
6222 /*
6223  * Display script name where an item was last set.
6224  * Should only be invoked when 'verbose' is non-zero.
6225  */
6226     void
6227 last_set_msg(sctx_T script_ctx)
6228 {
6229     char_u *p;
6230 
6231     if (script_ctx.sc_sid != 0)
6232     {
6233 	p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
6234 	if (p != NULL)
6235 	{
6236 	    verbose_enter();
6237 	    msg_puts(_("\n\tLast set from "));
6238 	    msg_puts((char *)p);
6239 	    if (script_ctx.sc_lnum > 0)
6240 	    {
6241 		msg_puts(_(line_msg));
6242 		msg_outnum((long)script_ctx.sc_lnum);
6243 	    }
6244 	    verbose_leave();
6245 	    vim_free(p);
6246 	}
6247     }
6248 }
6249 
6250 #endif // FEAT_EVAL
6251 
6252 /*
6253  * Perform a substitution on "str" with pattern "pat" and substitute "sub".
6254  * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
6255  * "flags" can be "g" to do a global substitute.
6256  * Returns an allocated string, NULL for error.
6257  */
6258     char_u *
6259 do_string_sub(
6260     char_u	*str,
6261     char_u	*pat,
6262     char_u	*sub,
6263     typval_T	*expr,
6264     char_u	*flags)
6265 {
6266     int		sublen;
6267     regmatch_T	regmatch;
6268     int		i;
6269     int		do_all;
6270     char_u	*tail;
6271     char_u	*end;
6272     garray_T	ga;
6273     char_u	*ret;
6274     char_u	*save_cpo;
6275     char_u	*zero_width = NULL;
6276 
6277     // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
6278     save_cpo = p_cpo;
6279     p_cpo = empty_option;
6280 
6281     ga_init2(&ga, 1, 200);
6282 
6283     do_all = (flags[0] == 'g');
6284 
6285     regmatch.rm_ic = p_ic;
6286     regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6287     if (regmatch.regprog != NULL)
6288     {
6289 	tail = str;
6290 	end = str + STRLEN(str);
6291 	while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6292 	{
6293 	    // Skip empty match except for first match.
6294 	    if (regmatch.startp[0] == regmatch.endp[0])
6295 	    {
6296 		if (zero_width == regmatch.startp[0])
6297 		{
6298 		    // avoid getting stuck on a match with an empty string
6299 		    i = mb_ptr2len(tail);
6300 		    mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6301 								   (size_t)i);
6302 		    ga.ga_len += i;
6303 		    tail += i;
6304 		    continue;
6305 		}
6306 		zero_width = regmatch.startp[0];
6307 	    }
6308 
6309 	    /*
6310 	     * Get some space for a temporary buffer to do the substitution
6311 	     * into.  It will contain:
6312 	     * - The text up to where the match is.
6313 	     * - The substituted text.
6314 	     * - The text after the match.
6315 	     */
6316 	    sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
6317 	    if (ga_grow(&ga, (int)((end - tail) + sublen -
6318 			    (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6319 	    {
6320 		ga_clear(&ga);
6321 		break;
6322 	    }
6323 
6324 	    // copy the text up to where the match is
6325 	    i = (int)(regmatch.startp[0] - tail);
6326 	    mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
6327 	    // add the substituted text
6328 	    (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
6329 					  + ga.ga_len + i, TRUE, TRUE, FALSE);
6330 	    ga.ga_len += i + sublen - 1;
6331 	    tail = regmatch.endp[0];
6332 	    if (*tail == NUL)
6333 		break;
6334 	    if (!do_all)
6335 		break;
6336 	}
6337 
6338 	if (ga.ga_data != NULL)
6339 	    STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6340 
6341 	vim_regfree(regmatch.regprog);
6342     }
6343 
6344     ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6345     ga_clear(&ga);
6346     if (p_cpo == empty_option)
6347 	p_cpo = save_cpo;
6348     else
6349     {
6350 	// Darn, evaluating {sub} expression or {expr} changed the value.
6351 	// If it's still empty it was changed and restored, need to restore in
6352 	// the complicated way.
6353 	if (*p_cpo == NUL)
6354 	    set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
6355 	free_string_option(save_cpo);
6356     }
6357 
6358     return ret;
6359 }
6360