xref: /vim-8.2.3635/src/typval.c (revision f3caeb63)
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  * typval.c: functions that deal with a typval
12  */
13 
14 #include "vim.h"
15 
16 #if defined(FEAT_EVAL) || defined(PROTO)
17 
18 /*
19  * Allocate memory for a variable type-value, and make it empty (0 or NULL
20  * value).
21  */
22     typval_T *
23 alloc_tv(void)
24 {
25     return ALLOC_CLEAR_ONE(typval_T);
26 }
27 
28 /*
29  * Allocate memory for a variable type-value, and assign a string to it.
30  * The string "s" must have been allocated, it is consumed.
31  * Return NULL for out of memory, the variable otherwise.
32  */
33     typval_T *
34 alloc_string_tv(char_u *s)
35 {
36     typval_T	*rettv;
37 
38     rettv = alloc_tv();
39     if (rettv != NULL)
40     {
41 	rettv->v_type = VAR_STRING;
42 	rettv->vval.v_string = s;
43     }
44     else
45 	vim_free(s);
46     return rettv;
47 }
48 
49 /*
50  * Free the memory for a variable type-value.
51  */
52     void
53 free_tv(typval_T *varp)
54 {
55     if (varp != NULL)
56     {
57 	switch (varp->v_type)
58 	{
59 	    case VAR_FUNC:
60 		func_unref(varp->vval.v_string);
61 		// FALLTHROUGH
62 	    case VAR_STRING:
63 		vim_free(varp->vval.v_string);
64 		break;
65 	    case VAR_PARTIAL:
66 		partial_unref(varp->vval.v_partial);
67 		break;
68 	    case VAR_BLOB:
69 		blob_unref(varp->vval.v_blob);
70 		break;
71 	    case VAR_LIST:
72 		list_unref(varp->vval.v_list);
73 		break;
74 	    case VAR_DICT:
75 		dict_unref(varp->vval.v_dict);
76 		break;
77 	    case VAR_JOB:
78 #ifdef FEAT_JOB_CHANNEL
79 		job_unref(varp->vval.v_job);
80 		break;
81 #endif
82 	    case VAR_CHANNEL:
83 #ifdef FEAT_JOB_CHANNEL
84 		channel_unref(varp->vval.v_channel);
85 		break;
86 #endif
87 	    case VAR_NUMBER:
88 	    case VAR_FLOAT:
89 	    case VAR_ANY:
90 	    case VAR_UNKNOWN:
91 	    case VAR_VOID:
92 	    case VAR_BOOL:
93 	    case VAR_SPECIAL:
94 	    case VAR_INSTR:
95 		break;
96 	}
97 	vim_free(varp);
98     }
99 }
100 
101 /*
102  * Free the memory for a variable value and set the value to NULL or 0.
103  */
104     void
105 clear_tv(typval_T *varp)
106 {
107     if (varp != NULL)
108     {
109 	switch (varp->v_type)
110 	{
111 	    case VAR_FUNC:
112 		func_unref(varp->vval.v_string);
113 		// FALLTHROUGH
114 	    case VAR_STRING:
115 		VIM_CLEAR(varp->vval.v_string);
116 		break;
117 	    case VAR_PARTIAL:
118 		partial_unref(varp->vval.v_partial);
119 		varp->vval.v_partial = NULL;
120 		break;
121 	    case VAR_BLOB:
122 		blob_unref(varp->vval.v_blob);
123 		varp->vval.v_blob = NULL;
124 		break;
125 	    case VAR_LIST:
126 		list_unref(varp->vval.v_list);
127 		varp->vval.v_list = NULL;
128 		break;
129 	    case VAR_DICT:
130 		dict_unref(varp->vval.v_dict);
131 		varp->vval.v_dict = NULL;
132 		break;
133 	    case VAR_NUMBER:
134 	    case VAR_BOOL:
135 	    case VAR_SPECIAL:
136 		varp->vval.v_number = 0;
137 		break;
138 	    case VAR_FLOAT:
139 #ifdef FEAT_FLOAT
140 		varp->vval.v_float = 0.0;
141 		break;
142 #endif
143 	    case VAR_JOB:
144 #ifdef FEAT_JOB_CHANNEL
145 		job_unref(varp->vval.v_job);
146 		varp->vval.v_job = NULL;
147 #endif
148 		break;
149 	    case VAR_CHANNEL:
150 #ifdef FEAT_JOB_CHANNEL
151 		channel_unref(varp->vval.v_channel);
152 		varp->vval.v_channel = NULL;
153 #endif
154 		break;
155 	    case VAR_INSTR:
156 		VIM_CLEAR(varp->vval.v_instr);
157 		break;
158 	    case VAR_UNKNOWN:
159 	    case VAR_ANY:
160 	    case VAR_VOID:
161 		break;
162 	}
163 	varp->v_lock = 0;
164     }
165 }
166 
167 /*
168  * Set the value of a variable to NULL without freeing items.
169  */
170     void
171 init_tv(typval_T *varp)
172 {
173     if (varp != NULL)
174 	CLEAR_POINTER(varp);
175 }
176 
177     static varnumber_T
178 tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
179 {
180     varnumber_T	n = 0L;
181 
182     switch (varp->v_type)
183     {
184 	case VAR_NUMBER:
185 	    if (in_vim9script() && want_bool && varp->vval.v_number != 0
186 						   && varp->vval.v_number != 1)
187 	    {
188 		semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 		break;
190 	    }
191 	    return varp->vval.v_number;
192 	case VAR_FLOAT:
193 #ifdef FEAT_FLOAT
194 	    emsg(_("E805: Using a Float as a Number"));
195 	    break;
196 #endif
197 	case VAR_FUNC:
198 	case VAR_PARTIAL:
199 	    emsg(_("E703: Using a Funcref as a Number"));
200 	    break;
201 	case VAR_STRING:
202 	    if (in_vim9script())
203 	    {
204 		emsg_using_string_as(varp, !want_bool);
205 		break;
206 	    }
207 	    if (varp->vval.v_string != NULL)
208 		vim_str2nr(varp->vval.v_string, NULL, NULL,
209 					    STR2NR_ALL, &n, NULL, 0, FALSE);
210 	    return n;
211 	case VAR_LIST:
212 	    emsg(_("E745: Using a List as a Number"));
213 	    break;
214 	case VAR_DICT:
215 	    emsg(_("E728: Using a Dictionary as a Number"));
216 	    break;
217 	case VAR_BOOL:
218 	case VAR_SPECIAL:
219 	    if (!want_bool && in_vim9script())
220 	    {
221 		if (varp->v_type == VAR_BOOL)
222 		    emsg(_(e_using_bool_as_number));
223 		else
224 		    emsg(_("E611: Using a Special as a Number"));
225 		break;
226 	    }
227 	    return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
228 	case VAR_JOB:
229 #ifdef FEAT_JOB_CHANNEL
230 	    emsg(_("E910: Using a Job as a Number"));
231 	    break;
232 #endif
233 	case VAR_CHANNEL:
234 #ifdef FEAT_JOB_CHANNEL
235 	    emsg(_("E913: Using a Channel as a Number"));
236 	    break;
237 #endif
238 	case VAR_BLOB:
239 	    emsg(_("E974: Using a Blob as a Number"));
240 	    break;
241 	case VAR_VOID:
242 	    emsg(_(e_cannot_use_void_value));
243 	    break;
244 	case VAR_UNKNOWN:
245 	case VAR_ANY:
246 	case VAR_INSTR:
247 	    internal_error_no_abort("tv_get_number(UNKNOWN)");
248 	    break;
249     }
250     if (denote == NULL)		// useful for values that must be unsigned
251 	n = -1;
252     else
253 	*denote = TRUE;
254     return n;
255 }
256 
257 /*
258  * Get the number value of a variable.
259  * If it is a String variable, uses vim_str2nr().
260  * For incompatible types, return 0.
261  * tv_get_number_chk() is similar to tv_get_number(), but informs the
262  * caller of incompatible types: it sets *denote to TRUE if "denote"
263  * is not NULL or returns -1 otherwise.
264  */
265     varnumber_T
266 tv_get_number(typval_T *varp)
267 {
268     int		error = FALSE;
269 
270     return tv_get_number_chk(varp, &error);	// return 0L on error
271 }
272 
273     varnumber_T
274 tv_get_number_chk(typval_T *varp, int *denote)
275 {
276     return tv_get_bool_or_number_chk(varp, denote, FALSE);
277 }
278 
279 /*
280  * Get the boolean value of "varp".  This is like tv_get_number_chk(),
281  * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
282  */
283     varnumber_T
284 tv_get_bool(typval_T *varp)
285 {
286     return tv_get_bool_or_number_chk(varp, NULL, TRUE);
287 }
288 
289 /*
290  * Get the boolean value of "varp".  This is like tv_get_number_chk(),
291  * but in Vim9 script accepts Number and Bool.
292  */
293     varnumber_T
294 tv_get_bool_chk(typval_T *varp, int *denote)
295 {
296     return tv_get_bool_or_number_chk(varp, denote, TRUE);
297 }
298 
299 #if defined(FEAT_FLOAT) || defined(PROTO)
300     float_T
301 tv_get_float(typval_T *varp)
302 {
303     switch (varp->v_type)
304     {
305 	case VAR_NUMBER:
306 	    return (float_T)(varp->vval.v_number);
307 	case VAR_FLOAT:
308 	    return varp->vval.v_float;
309 	case VAR_FUNC:
310 	case VAR_PARTIAL:
311 	    emsg(_("E891: Using a Funcref as a Float"));
312 	    break;
313 	case VAR_STRING:
314 	    emsg(_("E892: Using a String as a Float"));
315 	    break;
316 	case VAR_LIST:
317 	    emsg(_("E893: Using a List as a Float"));
318 	    break;
319 	case VAR_DICT:
320 	    emsg(_("E894: Using a Dictionary as a Float"));
321 	    break;
322 	case VAR_BOOL:
323 	    emsg(_("E362: Using a boolean value as a Float"));
324 	    break;
325 	case VAR_SPECIAL:
326 	    emsg(_("E907: Using a special value as a Float"));
327 	    break;
328 	case VAR_JOB:
329 # ifdef FEAT_JOB_CHANNEL
330 	    emsg(_("E911: Using a Job as a Float"));
331 	    break;
332 # endif
333 	case VAR_CHANNEL:
334 # ifdef FEAT_JOB_CHANNEL
335 	    emsg(_("E914: Using a Channel as a Float"));
336 	    break;
337 # endif
338 	case VAR_BLOB:
339 	    emsg(_("E975: Using a Blob as a Float"));
340 	    break;
341 	case VAR_VOID:
342 	    emsg(_(e_cannot_use_void_value));
343 	    break;
344 	case VAR_UNKNOWN:
345 	case VAR_ANY:
346 	case VAR_INSTR:
347 	    internal_error_no_abort("tv_get_float(UNKNOWN)");
348 	    break;
349     }
350     return 0;
351 }
352 #endif
353 
354 /*
355  * Give an error and return FAIL unless "tv" is a string.
356  */
357     int
358 check_for_string_arg(typval_T *args, int idx)
359 {
360     if (args[idx].v_type != VAR_STRING)
361     {
362 	if (idx >= 0)
363 	    semsg(_(e_string_required_for_argument_nr), idx + 1);
364 	else
365 	    emsg(_(e_stringreq));
366 	return FAIL;
367     }
368     return OK;
369 }
370 
371 /*
372  * Give an error and return FAIL unless "args[idx]" is a non-empty string.
373  */
374     int
375 check_for_nonempty_string_arg(typval_T *args, int idx)
376 {
377     if (check_for_string_arg(args, idx) == FAIL)
378 	return FAIL;
379     if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
380     {
381 	semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
382 	return FAIL;
383     }
384     return OK;
385 }
386 
387 /*
388  * Get the string value of a variable.
389  * If it is a Number variable, the number is converted into a string.
390  * tv_get_string() uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
391  * tv_get_string_buf() uses a given buffer.
392  * If the String variable has never been set, return an empty string.
393  * Never returns NULL;
394  * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
395  * NULL on error.
396  */
397     char_u *
398 tv_get_string(typval_T *varp)
399 {
400     static char_u   mybuf[NUMBUFLEN];
401 
402     return tv_get_string_buf(varp, mybuf);
403 }
404 
405 /*
406  * Like tv_get_string() but don't allow number to string conversion for Vim9.
407  */
408     char_u *
409 tv_get_string_strict(typval_T *varp)
410 {
411     static char_u   mybuf[NUMBUFLEN];
412     char_u	    *res =  tv_get_string_buf_chk_strict(
413 						 varp, mybuf, in_vim9script());
414 
415     return res != NULL ? res : (char_u *)"";
416 }
417 
418     char_u *
419 tv_get_string_buf(typval_T *varp, char_u *buf)
420 {
421     char_u	*res = tv_get_string_buf_chk(varp, buf);
422 
423     return res != NULL ? res : (char_u *)"";
424 }
425 
426 /*
427  * Careful: This uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
428  */
429     char_u *
430 tv_get_string_chk(typval_T *varp)
431 {
432     static char_u   mybuf[NUMBUFLEN];
433 
434     return tv_get_string_buf_chk(varp, mybuf);
435 }
436 
437     char_u *
438 tv_get_string_buf_chk(typval_T *varp, char_u *buf)
439 {
440     return tv_get_string_buf_chk_strict(varp, buf, FALSE);
441 }
442 
443     char_u *
444 tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
445 {
446     switch (varp->v_type)
447     {
448 	case VAR_NUMBER:
449 	    if (strict)
450 	    {
451 		emsg(_(e_using_number_as_string));
452 		break;
453 	    }
454 	    vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
455 					    (varnumber_T)varp->vval.v_number);
456 	    return buf;
457 	case VAR_FUNC:
458 	case VAR_PARTIAL:
459 	    emsg(_("E729: using Funcref as a String"));
460 	    break;
461 	case VAR_LIST:
462 	    emsg(_("E730: using List as a String"));
463 	    break;
464 	case VAR_DICT:
465 	    emsg(_("E731: using Dictionary as a String"));
466 	    break;
467 	case VAR_FLOAT:
468 #ifdef FEAT_FLOAT
469 	    if (strict)
470 	    {
471 		emsg(_(e_float_as_string));
472 		break;
473 	    }
474 	    vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
475 	    return buf;
476 #endif
477 	case VAR_STRING:
478 	    if (varp->vval.v_string != NULL)
479 		return varp->vval.v_string;
480 	    return (char_u *)"";
481 	case VAR_BOOL:
482 	case VAR_SPECIAL:
483 	    STRCPY(buf, get_var_special_name(varp->vval.v_number));
484 	    return buf;
485         case VAR_BLOB:
486 	    emsg(_("E976: using Blob as a String"));
487 	    break;
488 	case VAR_JOB:
489 #ifdef FEAT_JOB_CHANNEL
490 	    if (in_vim9script())
491 	    {
492 		semsg(_(e_using_invalid_value_as_string_str), "job");
493 		break;
494 	    }
495 	    return job_to_string_buf(varp, buf);
496 #endif
497 	    break;
498 	case VAR_CHANNEL:
499 #ifdef FEAT_JOB_CHANNEL
500 	    if (in_vim9script())
501 	    {
502 		semsg(_(e_using_invalid_value_as_string_str), "channel");
503 		break;
504 	    }
505 	    return channel_to_string_buf(varp, buf);
506 #endif
507 	    break;
508 	case VAR_VOID:
509 	    emsg(_(e_cannot_use_void_value));
510 	    break;
511 	case VAR_UNKNOWN:
512 	case VAR_ANY:
513 	case VAR_INSTR:
514 	    semsg(_(e_using_invalid_value_as_string_str),
515 						  vartype_name(varp->v_type));
516 	    break;
517     }
518     return NULL;
519 }
520 
521 /*
522  * Turn a typeval into a string.  Similar to tv_get_string_buf() but uses
523  * string() on Dict, List, etc.
524  */
525     char_u *
526 tv_stringify(typval_T *varp, char_u *buf)
527 {
528     if (varp->v_type == VAR_LIST
529 	    || varp->v_type == VAR_DICT
530 	    || varp->v_type == VAR_BLOB
531 	    || varp->v_type == VAR_FUNC
532 	    || varp->v_type == VAR_PARTIAL
533 	    || varp->v_type == VAR_FLOAT)
534     {
535 	typval_T tmp;
536 
537 	f_string(varp, &tmp);
538 	tv_get_string_buf(&tmp, buf);
539 	clear_tv(varp);
540 	*varp = tmp;
541 	return tmp.vval.v_string;
542     }
543     return tv_get_string_buf(varp, buf);
544 }
545 
546 /*
547  * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
548  * Also give an error message, using "name" or _("name") when use_gettext is
549  * TRUE.
550  */
551     int
552 tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
553 {
554     int	lock = 0;
555 
556     switch (tv->v_type)
557     {
558 	case VAR_BLOB:
559 	    if (tv->vval.v_blob != NULL)
560 		lock = tv->vval.v_blob->bv_lock;
561 	    break;
562 	case VAR_LIST:
563 	    if (tv->vval.v_list != NULL)
564 		lock = tv->vval.v_list->lv_lock;
565 	    break;
566 	case VAR_DICT:
567 	    if (tv->vval.v_dict != NULL)
568 		lock = tv->vval.v_dict->dv_lock;
569 	    break;
570 	default:
571 	    break;
572     }
573     return value_check_lock(tv->v_lock, name, use_gettext)
574 		   || (lock != 0 && value_check_lock(lock, name, use_gettext));
575 }
576 
577 /*
578  * Copy the values from typval_T "from" to typval_T "to".
579  * When needed allocates string or increases reference count.
580  * Does not make a copy of a list, blob or dict but copies the reference!
581  * It is OK for "from" and "to" to point to the same item.  This is used to
582  * make a copy later.
583  */
584     void
585 copy_tv(typval_T *from, typval_T *to)
586 {
587     to->v_type = from->v_type;
588     to->v_lock = 0;
589     switch (from->v_type)
590     {
591 	case VAR_NUMBER:
592 	case VAR_BOOL:
593 	case VAR_SPECIAL:
594 	    to->vval.v_number = from->vval.v_number;
595 	    break;
596 	case VAR_FLOAT:
597 #ifdef FEAT_FLOAT
598 	    to->vval.v_float = from->vval.v_float;
599 	    break;
600 #endif
601 	case VAR_JOB:
602 #ifdef FEAT_JOB_CHANNEL
603 	    to->vval.v_job = from->vval.v_job;
604 	    if (to->vval.v_job != NULL)
605 		++to->vval.v_job->jv_refcount;
606 	    break;
607 #endif
608 	case VAR_CHANNEL:
609 #ifdef FEAT_JOB_CHANNEL
610 	    to->vval.v_channel = from->vval.v_channel;
611 	    if (to->vval.v_channel != NULL)
612 		++to->vval.v_channel->ch_refcount;
613 	    break;
614 #endif
615 	case VAR_INSTR:
616 	    to->vval.v_instr = from->vval.v_instr;
617 	    break;
618 
619 	case VAR_STRING:
620 	case VAR_FUNC:
621 	    if (from->vval.v_string == NULL)
622 		to->vval.v_string = NULL;
623 	    else
624 	    {
625 		to->vval.v_string = vim_strsave(from->vval.v_string);
626 		if (from->v_type == VAR_FUNC)
627 		    func_ref(to->vval.v_string);
628 	    }
629 	    break;
630 	case VAR_PARTIAL:
631 	    if (from->vval.v_partial == NULL)
632 		to->vval.v_partial = NULL;
633 	    else
634 	    {
635 		to->vval.v_partial = from->vval.v_partial;
636 		++to->vval.v_partial->pt_refcount;
637 	    }
638 	    break;
639 	case VAR_BLOB:
640 	    if (from->vval.v_blob == NULL)
641 		to->vval.v_blob = NULL;
642 	    else
643 	    {
644 		to->vval.v_blob = from->vval.v_blob;
645 		++to->vval.v_blob->bv_refcount;
646 	    }
647 	    break;
648 	case VAR_LIST:
649 	    if (from->vval.v_list == NULL)
650 		to->vval.v_list = NULL;
651 	    else
652 	    {
653 		to->vval.v_list = from->vval.v_list;
654 		++to->vval.v_list->lv_refcount;
655 	    }
656 	    break;
657 	case VAR_DICT:
658 	    if (from->vval.v_dict == NULL)
659 		to->vval.v_dict = NULL;
660 	    else
661 	    {
662 		to->vval.v_dict = from->vval.v_dict;
663 		++to->vval.v_dict->dv_refcount;
664 	    }
665 	    break;
666 	case VAR_VOID:
667 	    emsg(_(e_cannot_use_void_value));
668 	    break;
669 	case VAR_UNKNOWN:
670 	case VAR_ANY:
671 	    internal_error_no_abort("copy_tv(UNKNOWN)");
672 	    break;
673     }
674 }
675 
676 /*
677  * Compare "typ1" and "typ2".  Put the result in "typ1".
678  */
679     int
680 typval_compare(
681     typval_T	*typ1,   // first operand
682     typval_T	*typ2,   // second operand
683     exprtype_T	type,    // operator
684     int		ic)      // ignore case
685 {
686     int		i;
687     varnumber_T	n1, n2;
688     char_u	*s1, *s2;
689     char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
690     int		type_is = type == EXPR_IS || type == EXPR_ISNOT;
691 
692     if (type_is && typ1->v_type != typ2->v_type)
693     {
694 	// For "is" a different type always means FALSE, for "notis"
695 	// it means TRUE.
696 	n1 = (type == EXPR_ISNOT);
697     }
698     else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
699     {
700 	if (type_is)
701 	{
702 	    n1 = (typ1->v_type == typ2->v_type
703 			    && typ1->vval.v_blob == typ2->vval.v_blob);
704 	    if (type == EXPR_ISNOT)
705 		n1 = !n1;
706 	}
707 	else if (typ1->v_type != typ2->v_type
708 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
709 	{
710 	    if (typ1->v_type != typ2->v_type)
711 		emsg(_("E977: Can only compare Blob with Blob"));
712 	    else
713 		emsg(_(e_invalblob));
714 	    clear_tv(typ1);
715 	    return FAIL;
716 	}
717 	else
718 	{
719 	    // Compare two Blobs for being equal or unequal.
720 	    n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
721 	    if (type == EXPR_NEQUAL)
722 		n1 = !n1;
723 	}
724     }
725     else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
726     {
727 	if (type_is)
728 	{
729 	    n1 = (typ1->v_type == typ2->v_type
730 			    && typ1->vval.v_list == typ2->vval.v_list);
731 	    if (type == EXPR_ISNOT)
732 		n1 = !n1;
733 	}
734 	else if (typ1->v_type != typ2->v_type
735 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
736 	{
737 	    if (typ1->v_type != typ2->v_type)
738 		emsg(_("E691: Can only compare List with List"));
739 	    else
740 		emsg(_("E692: Invalid operation for List"));
741 	    clear_tv(typ1);
742 	    return FAIL;
743 	}
744 	else
745 	{
746 	    // Compare two Lists for being equal or unequal.
747 	    n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
748 							    ic, FALSE);
749 	    if (type == EXPR_NEQUAL)
750 		n1 = !n1;
751 	}
752     }
753 
754     else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
755     {
756 	if (type_is)
757 	{
758 	    n1 = (typ1->v_type == typ2->v_type
759 			    && typ1->vval.v_dict == typ2->vval.v_dict);
760 	    if (type == EXPR_ISNOT)
761 		n1 = !n1;
762 	}
763 	else if (typ1->v_type != typ2->v_type
764 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
765 	{
766 	    if (typ1->v_type != typ2->v_type)
767 		emsg(_("E735: Can only compare Dictionary with Dictionary"));
768 	    else
769 		emsg(_("E736: Invalid operation for Dictionary"));
770 	    clear_tv(typ1);
771 	    return FAIL;
772 	}
773 	else
774 	{
775 	    // Compare two Dictionaries for being equal or unequal.
776 	    n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
777 							    ic, FALSE);
778 	    if (type == EXPR_NEQUAL)
779 		n1 = !n1;
780 	}
781     }
782 
783     else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
784 	|| typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
785     {
786 	if (type != EXPR_EQUAL && type != EXPR_NEQUAL
787 		&& type != EXPR_IS && type != EXPR_ISNOT)
788 	{
789 	    emsg(_("E694: Invalid operation for Funcrefs"));
790 	    clear_tv(typ1);
791 	    return FAIL;
792 	}
793 	if ((typ1->v_type == VAR_PARTIAL
794 					&& typ1->vval.v_partial == NULL)
795 		|| (typ2->v_type == VAR_PARTIAL
796 					&& typ2->vval.v_partial == NULL))
797 	    // When both partials are NULL, then they are equal.
798 	    // Otherwise they are not equal.
799 	    n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
800 	else if (type_is)
801 	{
802 	    if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
803 		// strings are considered the same if their value is
804 		// the same
805 		n1 = tv_equal(typ1, typ2, ic, FALSE);
806 	    else if (typ1->v_type == VAR_PARTIAL
807 					&& typ2->v_type == VAR_PARTIAL)
808 		n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
809 	    else
810 		n1 = FALSE;
811 	}
812 	else
813 	    n1 = tv_equal(typ1, typ2, ic, FALSE);
814 	if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
815 	    n1 = !n1;
816     }
817 
818 #ifdef FEAT_FLOAT
819     // If one of the two variables is a float, compare as a float.
820     // When using "=~" or "!~", always compare as string.
821     else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
822 	    && type != EXPR_MATCH && type != EXPR_NOMATCH)
823     {
824 	float_T f1, f2;
825 
826 	f1 = tv_get_float(typ1);
827 	f2 = tv_get_float(typ2);
828 	n1 = FALSE;
829 	switch (type)
830 	{
831 	    case EXPR_IS:
832 	    case EXPR_EQUAL:    n1 = (f1 == f2); break;
833 	    case EXPR_ISNOT:
834 	    case EXPR_NEQUAL:   n1 = (f1 != f2); break;
835 	    case EXPR_GREATER:  n1 = (f1 > f2); break;
836 	    case EXPR_GEQUAL:   n1 = (f1 >= f2); break;
837 	    case EXPR_SMALLER:  n1 = (f1 < f2); break;
838 	    case EXPR_SEQUAL:   n1 = (f1 <= f2); break;
839 	    case EXPR_UNKNOWN:
840 	    case EXPR_MATCH:
841 	    default:  break;  // avoid gcc warning
842 	}
843     }
844 #endif
845 
846     // If one of the two variables is a number, compare as a number.
847     // When using "=~" or "!~", always compare as string.
848     else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
849 	    && type != EXPR_MATCH && type != EXPR_NOMATCH)
850     {
851 	n1 = tv_get_number(typ1);
852 	n2 = tv_get_number(typ2);
853 	switch (type)
854 	{
855 	    case EXPR_IS:
856 	    case EXPR_EQUAL:    n1 = (n1 == n2); break;
857 	    case EXPR_ISNOT:
858 	    case EXPR_NEQUAL:   n1 = (n1 != n2); break;
859 	    case EXPR_GREATER:  n1 = (n1 > n2); break;
860 	    case EXPR_GEQUAL:   n1 = (n1 >= n2); break;
861 	    case EXPR_SMALLER:  n1 = (n1 < n2); break;
862 	    case EXPR_SEQUAL:   n1 = (n1 <= n2); break;
863 	    case EXPR_UNKNOWN:
864 	    case EXPR_MATCH:
865 	    default:  break;  // avoid gcc warning
866 	}
867     }
868     else if (in_vim9script() && (typ1->v_type == VAR_BOOL
869 						 || typ2->v_type == VAR_BOOL))
870     {
871 	if (typ1->v_type != typ2->v_type)
872 	{
873 	    semsg(_(e_cannot_compare_str_with_str),
874 		       vartype_name(typ1->v_type), vartype_name(typ2->v_type));
875 	    clear_tv(typ1);
876 	    return FAIL;
877 	}
878 	n1 = typ1->vval.v_number;
879 	n2 = typ2->vval.v_number;
880 	switch (type)
881 	{
882 	    case EXPR_IS:
883 	    case EXPR_EQUAL:    n1 = (n1 == n2); break;
884 	    case EXPR_ISNOT:
885 	    case EXPR_NEQUAL:   n1 = (n1 != n2); break;
886 	    default:
887 		emsg(_(e_invalid_operation_for_bool));
888 		clear_tv(typ1);
889 		return FAIL;
890 	}
891     }
892     else
893     {
894 	s1 = tv_get_string_buf(typ1, buf1);
895 	s2 = tv_get_string_buf(typ2, buf2);
896 	if (type != EXPR_MATCH && type != EXPR_NOMATCH)
897 	    i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
898 	else
899 	    i = 0;
900 	n1 = FALSE;
901 	switch (type)
902 	{
903 	    case EXPR_IS:
904 	    case EXPR_EQUAL:    n1 = (i == 0); break;
905 	    case EXPR_ISNOT:
906 	    case EXPR_NEQUAL:   n1 = (i != 0); break;
907 	    case EXPR_GREATER:  n1 = (i > 0); break;
908 	    case EXPR_GEQUAL:   n1 = (i >= 0); break;
909 	    case EXPR_SMALLER:  n1 = (i < 0); break;
910 	    case EXPR_SEQUAL:   n1 = (i <= 0); break;
911 
912 	    case EXPR_MATCH:
913 	    case EXPR_NOMATCH:
914 		    n1 = pattern_match(s2, s1, ic);
915 		    if (type == EXPR_NOMATCH)
916 			n1 = !n1;
917 		    break;
918 
919 	    default:  break;  // avoid gcc warning
920 	}
921     }
922     clear_tv(typ1);
923     if (in_vim9script())
924     {
925 	typ1->v_type = VAR_BOOL;
926 	typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
927     }
928     else
929     {
930 	typ1->v_type = VAR_NUMBER;
931 	typ1->vval.v_number = n1;
932     }
933 
934     return OK;
935 }
936 
937 /*
938  * Convert any type to a string, never give an error.
939  * When "quotes" is TRUE add quotes to a string.
940  * Returns an allocated string.
941  */
942     char_u *
943 typval_tostring(typval_T *arg, int quotes)
944 {
945     char_u	*tofree;
946     char_u	numbuf[NUMBUFLEN];
947     char_u	*ret = NULL;
948 
949     if (arg == NULL)
950 	return vim_strsave((char_u *)"(does not exist)");
951     if (!quotes && arg->v_type == VAR_STRING)
952     {
953 	ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
954 							 : arg->vval.v_string);
955     }
956     else
957     {
958 	ret = tv2string(arg, &tofree, numbuf, 0);
959 	// Make a copy if we have a value but it's not in allocated memory.
960 	if (ret != NULL && tofree == NULL)
961 	    ret = vim_strsave(ret);
962     }
963     return ret;
964 }
965 
966 /*
967  * Return TRUE if typeval "tv" is locked: Either that value is locked itself
968  * or it refers to a List or Dictionary that is locked.
969  */
970     int
971 tv_islocked(typval_T *tv)
972 {
973     return (tv->v_lock & VAR_LOCKED)
974 	|| (tv->v_type == VAR_LIST
975 		&& tv->vval.v_list != NULL
976 		&& (tv->vval.v_list->lv_lock & VAR_LOCKED))
977 	|| (tv->v_type == VAR_DICT
978 		&& tv->vval.v_dict != NULL
979 		&& (tv->vval.v_dict->dv_lock & VAR_LOCKED));
980 }
981 
982     static int
983 func_equal(
984     typval_T *tv1,
985     typval_T *tv2,
986     int	     ic)	    // ignore case
987 {
988     char_u	*s1, *s2;
989     dict_T	*d1, *d2;
990     int		a1, a2;
991     int		i;
992 
993     // empty and NULL function name considered the same
994     s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
995 					   : partial_name(tv1->vval.v_partial);
996     if (s1 != NULL && *s1 == NUL)
997 	s1 = NULL;
998     s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
999 					   : partial_name(tv2->vval.v_partial);
1000     if (s2 != NULL && *s2 == NUL)
1001 	s2 = NULL;
1002     if (s1 == NULL || s2 == NULL)
1003     {
1004 	if (s1 != s2)
1005 	    return FALSE;
1006     }
1007     else if (STRCMP(s1, s2) != 0)
1008 	return FALSE;
1009 
1010     // empty dict and NULL dict is different
1011     d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1012     d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1013     if (d1 == NULL || d2 == NULL)
1014     {
1015 	if (d1 != d2)
1016 	    return FALSE;
1017     }
1018     else if (!dict_equal(d1, d2, ic, TRUE))
1019 	return FALSE;
1020 
1021     // empty list and no list considered the same
1022     a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1023     a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1024     if (a1 != a2)
1025 	return FALSE;
1026     for (i = 0; i < a1; ++i)
1027 	if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1028 		      tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1029 	    return FALSE;
1030 
1031     return TRUE;
1032 }
1033 
1034 /*
1035  * Return TRUE if "tv1" and "tv2" have the same value.
1036  * Compares the items just like "==" would compare them, but strings and
1037  * numbers are different.  Floats and numbers are also different.
1038  */
1039     int
1040 tv_equal(
1041     typval_T *tv1,
1042     typval_T *tv2,
1043     int	     ic,	    // ignore case
1044     int	     recursive)	    // TRUE when used recursively
1045 {
1046     char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1047     char_u	*s1, *s2;
1048     static int  recursive_cnt = 0;	    // catch recursive loops
1049     int		r;
1050     static int	tv_equal_recurse_limit;
1051 
1052     // Catch lists and dicts that have an endless loop by limiting
1053     // recursiveness to a limit.  We guess they are equal then.
1054     // A fixed limit has the problem of still taking an awful long time.
1055     // Reduce the limit every time running into it. That should work fine for
1056     // deeply linked structures that are not recursively linked and catch
1057     // recursiveness quickly.
1058     if (!recursive)
1059 	tv_equal_recurse_limit = 1000;
1060     if (recursive_cnt >= tv_equal_recurse_limit)
1061     {
1062 	--tv_equal_recurse_limit;
1063 	return TRUE;
1064     }
1065 
1066     // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1067     // arguments.
1068     if ((tv1->v_type == VAR_FUNC
1069 		|| (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1070 	    && (tv2->v_type == VAR_FUNC
1071 		|| (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1072     {
1073 	++recursive_cnt;
1074 	r = func_equal(tv1, tv2, ic);
1075 	--recursive_cnt;
1076 	return r;
1077     }
1078 
1079     if (tv1->v_type != tv2->v_type
1080 	    && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1081 		|| (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
1082 	return FALSE;
1083 
1084     switch (tv1->v_type)
1085     {
1086 	case VAR_LIST:
1087 	    ++recursive_cnt;
1088 	    r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1089 	    --recursive_cnt;
1090 	    return r;
1091 
1092 	case VAR_DICT:
1093 	    ++recursive_cnt;
1094 	    r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1095 	    --recursive_cnt;
1096 	    return r;
1097 
1098 	case VAR_BLOB:
1099 	    return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1100 
1101 	case VAR_NUMBER:
1102 	case VAR_BOOL:
1103 	case VAR_SPECIAL:
1104 	    return tv1->vval.v_number == tv2->vval.v_number;
1105 
1106 	case VAR_STRING:
1107 	    s1 = tv_get_string_buf(tv1, buf1);
1108 	    s2 = tv_get_string_buf(tv2, buf2);
1109 	    return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1110 
1111 	case VAR_FLOAT:
1112 #ifdef FEAT_FLOAT
1113 	    return tv1->vval.v_float == tv2->vval.v_float;
1114 #endif
1115 	case VAR_JOB:
1116 #ifdef FEAT_JOB_CHANNEL
1117 	    return tv1->vval.v_job == tv2->vval.v_job;
1118 #endif
1119 	case VAR_CHANNEL:
1120 #ifdef FEAT_JOB_CHANNEL
1121 	    return tv1->vval.v_channel == tv2->vval.v_channel;
1122 #endif
1123 	case VAR_INSTR:
1124 	    return tv1->vval.v_instr == tv2->vval.v_instr;
1125 
1126 	case VAR_PARTIAL:
1127 	    return tv1->vval.v_partial == tv2->vval.v_partial;
1128 
1129 	case VAR_FUNC:
1130 	    return tv1->vval.v_string == tv2->vval.v_string;
1131 
1132 	case VAR_UNKNOWN:
1133 	case VAR_ANY:
1134 	case VAR_VOID:
1135 	    break;
1136     }
1137 
1138     // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1139     // does not equal anything, not even itself.
1140     return FALSE;
1141 }
1142 
1143 /*
1144  * Get an option value.
1145  * "arg" points to the '&' or '+' before the option name.
1146  * "arg" is advanced to character after the option name.
1147  * Return OK or FAIL.
1148  */
1149     int
1150 eval_option(
1151     char_u	**arg,
1152     typval_T	*rettv,	// when NULL, only check if option exists
1153     int		evaluate)
1154 {
1155     char_u	*option_end;
1156     long	numval;
1157     char_u	*stringval;
1158     getoption_T	opt_type;
1159     int		c;
1160     int		working = (**arg == '+');    // has("+option")
1161     int		ret = OK;
1162     int		opt_flags;
1163 
1164     // Isolate the option name and find its value.
1165     option_end = find_option_end(arg, &opt_flags);
1166     if (option_end == NULL)
1167     {
1168 	if (rettv != NULL)
1169 	    semsg(_("E112: Option name missing: %s"), *arg);
1170 	return FAIL;
1171     }
1172 
1173     if (!evaluate)
1174     {
1175 	*arg = option_end;
1176 	return OK;
1177     }
1178 
1179     c = *option_end;
1180     *option_end = NUL;
1181     opt_type = get_option_value(*arg, &numval,
1182 			       rettv == NULL ? NULL : &stringval, opt_flags);
1183 
1184     if (opt_type == gov_unknown)
1185     {
1186 	if (rettv != NULL)
1187 	    semsg(_(e_unknown_option), *arg);
1188 	ret = FAIL;
1189     }
1190     else if (rettv != NULL)
1191     {
1192 	rettv->v_lock = 0;
1193 	if (opt_type == gov_hidden_string)
1194 	{
1195 	    rettv->v_type = VAR_STRING;
1196 	    rettv->vval.v_string = NULL;
1197 	}
1198 	else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
1199 	{
1200 	    rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1201 						       ? VAR_BOOL : VAR_NUMBER;
1202 	    rettv->vval.v_number = 0;
1203 	}
1204 	else if (opt_type == gov_bool || opt_type == gov_number)
1205 	{
1206 	    if (in_vim9script() && opt_type == gov_bool)
1207 	    {
1208 		rettv->v_type = VAR_BOOL;
1209 		rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1210 	    }
1211 	    else
1212 	    {
1213 		rettv->v_type = VAR_NUMBER;
1214 		rettv->vval.v_number = numval;
1215 	    }
1216 	}
1217 	else				// string option
1218 	{
1219 	    rettv->v_type = VAR_STRING;
1220 	    rettv->vval.v_string = stringval;
1221 	}
1222     }
1223     else if (working && (opt_type == gov_hidden_bool
1224 			|| opt_type == gov_hidden_number
1225 			|| opt_type == gov_hidden_string))
1226 	ret = FAIL;
1227 
1228     *option_end = c;		    // put back for error messages
1229     *arg = option_end;
1230 
1231     return ret;
1232 }
1233 
1234 /*
1235  * Allocate a variable for a number constant.  Also deals with "0z" for blob.
1236  * Return OK or FAIL.
1237  */
1238     int
1239 eval_number(
1240 	char_u	    **arg,
1241 	typval_T    *rettv,
1242 	int	    evaluate,
1243 	int	    want_string UNUSED)
1244 {
1245     int		len;
1246 #ifdef FEAT_FLOAT
1247     char_u	*p;
1248     int		get_float = FALSE;
1249 
1250     // We accept a float when the format matches
1251     // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?".  This is very
1252     // strict to avoid backwards compatibility problems.
1253     // With script version 2 and later the leading digit can be
1254     // omitted.
1255     // Don't look for a float after the "." operator, so that
1256     // ":let vers = 1.2.3" doesn't fail.
1257     if (**arg == '.')
1258 	p = *arg;
1259     else
1260 	p = skipdigits(*arg + 1);
1261     if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1262     {
1263 	get_float = TRUE;
1264 	p = skipdigits(p + 2);
1265 	if (*p == 'e' || *p == 'E')
1266 	{
1267 	    ++p;
1268 	    if (*p == '-' || *p == '+')
1269 		++p;
1270 	    if (!vim_isdigit(*p))
1271 		get_float = FALSE;
1272 	    else
1273 		p = skipdigits(p + 1);
1274 	}
1275 	if (ASCII_ISALPHA(*p) || *p == '.')
1276 	    get_float = FALSE;
1277     }
1278     if (get_float)
1279     {
1280 	float_T	f;
1281 
1282 	*arg += string2float(*arg, &f);
1283 	if (evaluate)
1284 	{
1285 	    rettv->v_type = VAR_FLOAT;
1286 	    rettv->vval.v_float = f;
1287 	}
1288     }
1289     else
1290 #endif
1291     if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1292     {
1293 	char_u  *bp;
1294 	blob_T  *blob = NULL;  // init for gcc
1295 
1296 	// Blob constant: 0z0123456789abcdef
1297 	if (evaluate)
1298 	    blob = blob_alloc();
1299 	for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1300 	{
1301 	    if (!vim_isxdigit(bp[1]))
1302 	    {
1303 		if (blob != NULL)
1304 		{
1305 		    emsg(_("E973: Blob literal should have an even number of hex characters"));
1306 		    ga_clear(&blob->bv_ga);
1307 		    VIM_CLEAR(blob);
1308 		}
1309 		return FAIL;
1310 	    }
1311 	    if (blob != NULL)
1312 		ga_append(&blob->bv_ga,
1313 			     (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1314 	    if (bp[2] == '.' && vim_isxdigit(bp[3]))
1315 		++bp;
1316 	}
1317 	if (blob != NULL)
1318 	    rettv_blob_set(rettv, blob);
1319 	*arg = bp;
1320     }
1321     else
1322     {
1323 	varnumber_T	n;
1324 
1325 	// decimal, hex or octal number
1326 	vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1327 		      ? STR2NR_NO_OCT + STR2NR_QUOTE
1328 		      : STR2NR_ALL, &n, NULL, 0, TRUE);
1329 	if (len == 0)
1330 	{
1331 	    semsg(_(e_invexpr2), *arg);
1332 	    return FAIL;
1333 	}
1334 	*arg += len;
1335 	if (evaluate)
1336 	{
1337 	    rettv->v_type = VAR_NUMBER;
1338 	    rettv->vval.v_number = n;
1339 	}
1340     }
1341     return OK;
1342 }
1343 
1344 /*
1345  * Allocate a variable for a string constant.
1346  * Return OK or FAIL.
1347  */
1348     int
1349 eval_string(char_u **arg, typval_T *rettv, int evaluate)
1350 {
1351     char_u	*p;
1352     char_u	*end;
1353     int		extra = 0;
1354     int		len;
1355 
1356     // Find the end of the string, skipping backslashed characters.
1357     for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1358     {
1359 	if (*p == '\\' && p[1] != NUL)
1360 	{
1361 	    ++p;
1362 	    // A "\<x>" form occupies at least 4 characters, and produces up
1363 	    // to 21 characters (3 * 6 for the char and 3 for a modifier):
1364 	    // reserve space for 18 extra.
1365 	    // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1366 	    if (*p == '<')
1367 		extra += 18;
1368 	}
1369     }
1370 
1371     if (*p != '"')
1372     {
1373 	semsg(_("E114: Missing quote: %s"), *arg);
1374 	return FAIL;
1375     }
1376 
1377     // If only parsing, set *arg and return here
1378     if (!evaluate)
1379     {
1380 	*arg = p + 1;
1381 	return OK;
1382     }
1383 
1384     // Copy the string into allocated memory, handling backslashed
1385     // characters.
1386     rettv->v_type = VAR_STRING;
1387     len = (int)(p - *arg + extra);
1388     rettv->vval.v_string = alloc(len);
1389     if (rettv->vval.v_string == NULL)
1390 	return FAIL;
1391     end = rettv->vval.v_string;
1392 
1393     for (p = *arg + 1; *p != NUL && *p != '"'; )
1394     {
1395 	if (*p == '\\')
1396 	{
1397 	    switch (*++p)
1398 	    {
1399 		case 'b': *end++ = BS; ++p; break;
1400 		case 'e': *end++ = ESC; ++p; break;
1401 		case 'f': *end++ = FF; ++p; break;
1402 		case 'n': *end++ = NL; ++p; break;
1403 		case 'r': *end++ = CAR; ++p; break;
1404 		case 't': *end++ = TAB; ++p; break;
1405 
1406 		case 'X': // hex: "\x1", "\x12"
1407 		case 'x':
1408 		case 'u': // Unicode: "\u0023"
1409 		case 'U':
1410 			  if (vim_isxdigit(p[1]))
1411 			  {
1412 			      int	n, nr;
1413 			      int	c = toupper(*p);
1414 
1415 			      if (c == 'X')
1416 				  n = 2;
1417 			      else if (*p == 'u')
1418 				  n = 4;
1419 			      else
1420 				  n = 8;
1421 			      nr = 0;
1422 			      while (--n >= 0 && vim_isxdigit(p[1]))
1423 			      {
1424 				  ++p;
1425 				  nr = (nr << 4) + hex2nr(*p);
1426 			      }
1427 			      ++p;
1428 			      // For "\u" store the number according to
1429 			      // 'encoding'.
1430 			      if (c != 'X')
1431 				  end += (*mb_char2bytes)(nr, end);
1432 			      else
1433 				  *end++ = nr;
1434 			  }
1435 			  break;
1436 
1437 			  // octal: "\1", "\12", "\123"
1438 		case '0':
1439 		case '1':
1440 		case '2':
1441 		case '3':
1442 		case '4':
1443 		case '5':
1444 		case '6':
1445 		case '7': *end = *p++ - '0';
1446 			  if (*p >= '0' && *p <= '7')
1447 			  {
1448 			      *end = (*end << 3) + *p++ - '0';
1449 			      if (*p >= '0' && *p <= '7')
1450 				  *end = (*end << 3) + *p++ - '0';
1451 			  }
1452 			  ++end;
1453 			  break;
1454 
1455 			  // Special key, e.g.: "\<C-W>"
1456 		case '<':
1457 			  {
1458 			      int flags = FSK_KEYCODE | FSK_IN_STRING;
1459 
1460 			      if (p[1] != '*')
1461 				  flags |= FSK_SIMPLIFY;
1462 			      extra = trans_special(&p, end, flags, NULL);
1463 			      if (extra != 0)
1464 			      {
1465 				  end += extra;
1466 				  if (end >= rettv->vval.v_string + len)
1467 				      iemsg("eval_string() used more space than allocated");
1468 				  break;
1469 			      }
1470 			  }
1471 			  // FALLTHROUGH
1472 
1473 		default:  MB_COPY_CHAR(p, end);
1474 			  break;
1475 	    }
1476 	}
1477 	else
1478 	    MB_COPY_CHAR(p, end);
1479     }
1480     *end = NUL;
1481     if (*p != NUL) // just in case
1482 	++p;
1483     *arg = p;
1484 
1485     return OK;
1486 }
1487 
1488 /*
1489  * Allocate a variable for a 'str''ing' constant.
1490  * Return OK or FAIL.
1491  */
1492     int
1493 eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
1494 {
1495     char_u	*p;
1496     char_u	*str;
1497     int		reduce = 0;
1498 
1499     // Find the end of the string, skipping ''.
1500     for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1501     {
1502 	if (*p == '\'')
1503 	{
1504 	    if (p[1] != '\'')
1505 		break;
1506 	    ++reduce;
1507 	    ++p;
1508 	}
1509     }
1510 
1511     if (*p != '\'')
1512     {
1513 	semsg(_("E115: Missing quote: %s"), *arg);
1514 	return FAIL;
1515     }
1516 
1517     // If only parsing return after setting "*arg"
1518     if (!evaluate)
1519     {
1520 	*arg = p + 1;
1521 	return OK;
1522     }
1523 
1524     // Copy the string into allocated memory, handling '' to ' reduction.
1525     str = alloc((p - *arg) - reduce);
1526     if (str == NULL)
1527 	return FAIL;
1528     rettv->v_type = VAR_STRING;
1529     rettv->vval.v_string = str;
1530 
1531     for (p = *arg + 1; *p != NUL; )
1532     {
1533 	if (*p == '\'')
1534 	{
1535 	    if (p[1] != '\'')
1536 		break;
1537 	    ++p;
1538 	}
1539 	MB_COPY_CHAR(p, str);
1540     }
1541     *str = NUL;
1542     *arg = p + 1;
1543 
1544     return OK;
1545 }
1546 
1547 /*
1548  * Return a string with the string representation of a variable.
1549  * If the memory is allocated "tofree" is set to it, otherwise NULL.
1550  * "numbuf" is used for a number.
1551  * Puts quotes around strings, so that they can be parsed back by eval().
1552  * May return NULL.
1553  */
1554     char_u *
1555 tv2string(
1556     typval_T	*tv,
1557     char_u	**tofree,
1558     char_u	*numbuf,
1559     int		copyID)
1560 {
1561     return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1562 }
1563 
1564 /*
1565  * Get the value of an environment variable.
1566  * "arg" is pointing to the '$'.  It is advanced to after the name.
1567  * If the environment variable was not set, silently assume it is empty.
1568  * Return FAIL if the name is invalid.
1569  */
1570     int
1571 eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
1572 {
1573     char_u	*string = NULL;
1574     int		len;
1575     int		cc;
1576     char_u	*name;
1577     int		mustfree = FALSE;
1578 
1579     ++*arg;
1580     name = *arg;
1581     len = get_env_len(arg);
1582     if (evaluate)
1583     {
1584 	if (len == 0)
1585 	    return FAIL; // invalid empty name
1586 
1587 	cc = name[len];
1588 	name[len] = NUL;
1589 	// first try vim_getenv(), fast for normal environment vars
1590 	string = vim_getenv(name, &mustfree);
1591 	if (string != NULL && *string != NUL)
1592 	{
1593 	    if (!mustfree)
1594 		string = vim_strsave(string);
1595 	}
1596 	else
1597 	{
1598 	    if (mustfree)
1599 		vim_free(string);
1600 
1601 	    // next try expanding things like $VIM and ${HOME}
1602 	    string = expand_env_save(name - 1);
1603 	    if (string != NULL && *string == '$')
1604 		VIM_CLEAR(string);
1605 	}
1606 	name[len] = cc;
1607 
1608 	rettv->v_type = VAR_STRING;
1609 	rettv->vval.v_string = string;
1610     }
1611 
1612     return OK;
1613 }
1614 
1615 /*
1616  * Get the lnum from the first argument.
1617  * Also accepts ".", "$", etc., but that only works for the current buffer.
1618  * Returns -1 on error.
1619  */
1620     linenr_T
1621 tv_get_lnum(typval_T *argvars)
1622 {
1623     linenr_T	lnum = -1;
1624 
1625     if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1626 	lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1627     if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
1628     {
1629 	int	fnum;
1630 	pos_T	*fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
1631 
1632 	// no valid number, try using arg like line()
1633 	if (fp != NULL)
1634 	    lnum = fp->lnum;
1635     }
1636     return lnum;
1637 }
1638 
1639 /*
1640  * Get the lnum from the first argument.
1641  * Also accepts "$", then "buf" is used.
1642  * Returns 0 on error.
1643  */
1644     linenr_T
1645 tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1646 {
1647     if (argvars[0].v_type == VAR_STRING
1648 	    && argvars[0].vval.v_string != NULL
1649 	    && argvars[0].vval.v_string[0] == '$'
1650 	    && buf != NULL)
1651 	return buf->b_ml.ml_line_count;
1652     return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1653 }
1654 
1655 /*
1656  * Get buffer by number or pattern.
1657  */
1658     buf_T *
1659 tv_get_buf(typval_T *tv, int curtab_only)
1660 {
1661     char_u	*name = tv->vval.v_string;
1662     buf_T	*buf;
1663 
1664     if (tv->v_type == VAR_NUMBER)
1665 	return buflist_findnr((int)tv->vval.v_number);
1666     if (tv->v_type != VAR_STRING)
1667 	return NULL;
1668     if (name == NULL || *name == NUL)
1669 	return curbuf;
1670     if (name[0] == '$' && name[1] == NUL)
1671 	return lastbuf;
1672 
1673     buf = buflist_find_by_name(name, curtab_only);
1674 
1675     // If not found, try expanding the name, like done for bufexists().
1676     if (buf == NULL)
1677 	buf = find_buffer(tv);
1678 
1679     return buf;
1680 }
1681 
1682 /*
1683  * Like tv_get_buf() but give an error message is the type is wrong.
1684  */
1685     buf_T *
1686 tv_get_buf_from_arg(typval_T *tv)
1687 {
1688     buf_T *buf;
1689 
1690     ++emsg_off;
1691     buf = tv_get_buf(tv, FALSE);
1692     --emsg_off;
1693     if (buf == NULL
1694 	    && tv->v_type != VAR_NUMBER
1695 	    && tv->v_type != VAR_STRING)
1696 	// issue errmsg for type error
1697 	(void)tv_get_number(tv);
1698     return buf;
1699 }
1700 
1701 #endif // FEAT_EVAL
1702