xref: /vim-8.2.3635/src/vim9compile.c (revision eab6dff1)
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  * vim9compile.c: :def and dealing with instructions
12  */
13 
14 #define USING_FLOAT_STUFF
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 DEFINE_VIM9_GLOBALS
24 #include "vim9.h"
25 
26 /*
27  * Chain of jump instructions where the end label needs to be set.
28  */
29 typedef struct endlabel_S endlabel_T;
30 struct endlabel_S {
31     endlabel_T	*el_next;	    // chain end_label locations
32     int		el_end_label;	    // instruction idx where to set end
33 };
34 
35 /*
36  * info specific for the scope of :if / elseif / else
37  */
38 typedef struct {
39     int		is_if_label;	    // instruction idx at IF or ELSEIF
40     endlabel_T	*is_end_label;	    // instructions to set end label
41 } ifscope_T;
42 
43 /*
44  * info specific for the scope of :while
45  */
46 typedef struct {
47     int		ws_top_label;	    // instruction idx at WHILE
48     endlabel_T	*ws_end_label;	    // instructions to set end
49 } whilescope_T;
50 
51 /*
52  * info specific for the scope of :for
53  */
54 typedef struct {
55     int		fs_top_label;	    // instruction idx at FOR
56     endlabel_T	*fs_end_label;	    // break instructions
57 } forscope_T;
58 
59 /*
60  * info specific for the scope of :try
61  */
62 typedef struct {
63     int		ts_try_label;	    // instruction idx at TRY
64     endlabel_T	*ts_end_label;	    // jump to :finally or :endtry
65     int		ts_catch_label;	    // instruction idx of last CATCH
66     int		ts_caught_all;	    // "catch" without argument encountered
67 } tryscope_T;
68 
69 typedef enum {
70     NO_SCOPE,
71     IF_SCOPE,
72     WHILE_SCOPE,
73     FOR_SCOPE,
74     TRY_SCOPE,
75     BLOCK_SCOPE
76 } scopetype_T;
77 
78 /*
79  * Info for one scope, pointed to by "ctx_scope".
80  */
81 typedef struct scope_S scope_T;
82 struct scope_S {
83     scope_T	*se_outer;	    // scope containing this one
84     scopetype_T se_type;
85     int		se_local_count;	    // ctx_locals.ga_len before scope
86     union {
87 	ifscope_T	se_if;
88 	whilescope_T	se_while;
89 	forscope_T	se_for;
90 	tryscope_T	se_try;
91     } se_u;
92 };
93 
94 /*
95  * Entry for "ctx_locals".  Used for arguments and local variables.
96  */
97 typedef struct {
98     char_u	*lv_name;
99     type_T	*lv_type;
100     int		lv_const;   // when TRUE cannot be assigned to
101     int		lv_arg;	    // when TRUE this is an argument
102 } lvar_T;
103 
104 /*
105  * Context for compiling lines of Vim script.
106  * Stores info about the local variables and condition stack.
107  */
108 struct cctx_S {
109     ufunc_T	*ctx_ufunc;	    // current function
110     int		ctx_lnum;	    // line number in current function
111     garray_T	ctx_instr;	    // generated instructions
112 
113     garray_T	ctx_locals;	    // currently visible local variables
114     int		ctx_max_local;	    // maximum number of locals at one time
115 
116     garray_T	ctx_imports;	    // imported items
117 
118     int		ctx_skip;	    // when TRUE skip commands, when FALSE skip
119 				    // commands after "else"
120     scope_T	*ctx_scope;	    // current scope, NULL at toplevel
121 
122     garray_T	ctx_type_stack;	    // type of each item on the stack
123     garray_T	*ctx_type_list;	    // space for adding types
124 };
125 
126 static char e_var_notfound[] = N_("E1001: variable not found: %s");
127 static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128 
129 static int compile_expr1(char_u **arg,  cctx_T *cctx);
130 static int compile_expr2(char_u **arg,  cctx_T *cctx);
131 static int compile_expr3(char_u **arg,  cctx_T *cctx);
132 
133 /*
134  * Lookup variable "name" in the local scope and return the index.
135  */
136     static int
137 lookup_local(char_u *name, size_t len, cctx_T *cctx)
138 {
139     int	    idx;
140 
141     if (len == 0)
142 	return -1;
143     for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
144     {
145 	lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
146 
147 	if (STRNCMP(name, lvar->lv_name, len) == 0
148 					       && STRLEN(lvar->lv_name) == len)
149 	    return idx;
150     }
151     return -1;
152 }
153 
154 /*
155  * Lookup an argument in the current function.
156  * Returns the argument index or -1 if not found.
157  */
158     static int
159 lookup_arg(char_u *name, size_t len, cctx_T *cctx)
160 {
161     int	    idx;
162 
163     if (len == 0)
164 	return -1;
165     for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
166     {
167 	char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
168 
169 	if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
170 	    return idx;
171     }
172     return -1;
173 }
174 
175 /*
176  * Lookup a vararg argument in the current function.
177  * Returns TRUE if there is a match.
178  */
179     static int
180 lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
181 {
182     char_u  *va_name = cctx->ctx_ufunc->uf_va_name;
183 
184     return len > 0 && va_name != NULL
185 		 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
186 }
187 
188 /*
189  * Lookup a variable in the current script.
190  * Returns OK or FAIL.
191  */
192     static int
193 lookup_script(char_u *name, size_t len)
194 {
195     int		    cc;
196     hashtab_T	    *ht = &SCRIPT_VARS(current_sctx.sc_sid);
197     dictitem_T	    *di;
198 
199     cc = name[len];
200     name[len] = NUL;
201     di = find_var_in_ht(ht, 0, name, TRUE);
202     name[len] = cc;
203     return di == NULL ? FAIL: OK;
204 }
205 
206     static type_T *
207 get_list_type(type_T *member_type, garray_T *type_list)
208 {
209     type_T *type;
210 
211     // recognize commonly used types
212     if (member_type->tt_type == VAR_UNKNOWN)
213 	return &t_list_any;
214     if (member_type->tt_type == VAR_VOID)
215 	return &t_list_empty;
216     if (member_type->tt_type == VAR_BOOL)
217 	return &t_list_bool;
218     if (member_type->tt_type == VAR_NUMBER)
219 	return &t_list_number;
220     if (member_type->tt_type == VAR_STRING)
221 	return &t_list_string;
222 
223     // Not a common type, create a new entry.
224     if (ga_grow(type_list, 1) == FAIL)
225 	return FAIL;
226     type = ((type_T *)type_list->ga_data) + type_list->ga_len;
227     ++type_list->ga_len;
228     type->tt_type = VAR_LIST;
229     type->tt_member = member_type;
230     return type;
231 }
232 
233     static type_T *
234 get_dict_type(type_T *member_type, garray_T *type_list)
235 {
236     type_T *type;
237 
238     // recognize commonly used types
239     if (member_type->tt_type == VAR_UNKNOWN)
240 	return &t_dict_any;
241     if (member_type->tt_type == VAR_VOID)
242 	return &t_dict_empty;
243     if (member_type->tt_type == VAR_BOOL)
244 	return &t_dict_bool;
245     if (member_type->tt_type == VAR_NUMBER)
246 	return &t_dict_number;
247     if (member_type->tt_type == VAR_STRING)
248 	return &t_dict_string;
249 
250     // Not a common type, create a new entry.
251     if (ga_grow(type_list, 1) == FAIL)
252 	return FAIL;
253     type = ((type_T *)type_list->ga_data) + type_list->ga_len;
254     ++type_list->ga_len;
255     type->tt_type = VAR_DICT;
256     type->tt_member = member_type;
257     return type;
258 }
259 
260 /////////////////////////////////////////////////////////////////////
261 // Following generate_ functions expect the caller to call ga_grow().
262 
263 /*
264  * Generate an instruction without arguments.
265  * Returns a pointer to the new instruction, NULL if failed.
266  */
267     static isn_T *
268 generate_instr(cctx_T *cctx, isntype_T isn_type)
269 {
270     garray_T	*instr = &cctx->ctx_instr;
271     isn_T	*isn;
272 
273     if (ga_grow(instr, 1) == FAIL)
274 	return NULL;
275     isn = ((isn_T *)instr->ga_data) + instr->ga_len;
276     isn->isn_type = isn_type;
277     isn->isn_lnum = cctx->ctx_lnum + 1;
278     ++instr->ga_len;
279 
280     return isn;
281 }
282 
283 /*
284  * Generate an instruction without arguments.
285  * "drop" will be removed from the stack.
286  * Returns a pointer to the new instruction, NULL if failed.
287  */
288     static isn_T *
289 generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
290 {
291     garray_T	*stack = &cctx->ctx_type_stack;
292 
293     stack->ga_len -= drop;
294     return generate_instr(cctx, isn_type);
295 }
296 
297 /*
298  * Generate instruction "isn_type" and put "type" on the type stack.
299  */
300     static isn_T *
301 generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
302 {
303     isn_T	*isn;
304     garray_T	*stack = &cctx->ctx_type_stack;
305 
306     if ((isn = generate_instr(cctx, isn_type)) == NULL)
307 	return NULL;
308 
309     if (ga_grow(stack, 1) == FAIL)
310 	return NULL;
311     ((type_T **)stack->ga_data)[stack->ga_len] = type;
312     ++stack->ga_len;
313 
314     return isn;
315 }
316 
317 /*
318  * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
319  */
320     static int
321 may_generate_2STRING(int offset, cctx_T *cctx)
322 {
323     isn_T	*isn;
324     garray_T	*stack = &cctx->ctx_type_stack;
325     type_T	**type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
326 
327     if ((*type)->tt_type == VAR_STRING)
328 	return OK;
329     *type = &t_string;
330 
331     if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
332 	return FAIL;
333     isn->isn_arg.number = offset;
334 
335     return OK;
336 }
337 
338     static int
339 check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
340 {
341     if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
342 	    && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
343 						     || type2 == VAR_UNKNOWN)))
344     {
345 	if (*op == '+')
346 	    emsg(_("E1035: wrong argument type for +"));
347 	else
348 	    semsg(_("E1036: %c requires number or float arguments"), *op);
349 	return FAIL;
350     }
351     return OK;
352 }
353 
354 /*
355  * Generate an instruction with two arguments.  The instruction depends on the
356  * type of the arguments.
357  */
358     static int
359 generate_two_op(cctx_T *cctx, char_u *op)
360 {
361     garray_T	*stack = &cctx->ctx_type_stack;
362     type_T	*type1;
363     type_T	*type2;
364     vartype_T	vartype;
365     isn_T	*isn;
366 
367     // Get the known type of the two items on the stack.  If they are matching
368     // use a type-specific instruction. Otherwise fall back to runtime type
369     // checking.
370     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
371     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
372     vartype = VAR_UNKNOWN;
373     if (type1->tt_type == type2->tt_type
374 	    && (type1->tt_type == VAR_NUMBER
375 		|| type1->tt_type == VAR_LIST
376 #ifdef FEAT_FLOAT
377 		|| type1->tt_type == VAR_FLOAT
378 #endif
379 		|| type1->tt_type == VAR_BLOB))
380 	vartype = type1->tt_type;
381 
382     switch (*op)
383     {
384 	case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
385 			  && type1->tt_type != VAR_UNKNOWN
386 			  && type2->tt_type != VAR_UNKNOWN
387 			  && check_number_or_float(
388 				   type1->tt_type, type2->tt_type, op) == FAIL)
389 		      return FAIL;
390 		  isn = generate_instr_drop(cctx,
391 			    vartype == VAR_NUMBER ? ISN_OPNR
392 			  : vartype == VAR_LIST ? ISN_ADDLIST
393 			  : vartype == VAR_BLOB ? ISN_ADDBLOB
394 #ifdef FEAT_FLOAT
395 			  : vartype == VAR_FLOAT ? ISN_OPFLOAT
396 #endif
397 			  : ISN_OPANY, 1);
398 		  if (isn != NULL)
399 		      isn->isn_arg.op.op_type = EXPR_ADD;
400 		  break;
401 
402 	case '-':
403 	case '*':
404 	case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
405 								   op) == FAIL)
406 		      return FAIL;
407 		  if (vartype == VAR_NUMBER)
408 		      isn = generate_instr_drop(cctx, ISN_OPNR, 1);
409 #ifdef FEAT_FLOAT
410 		  else if (vartype == VAR_FLOAT)
411 		      isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
412 #endif
413 		  else
414 		      isn = generate_instr_drop(cctx, ISN_OPANY, 1);
415 		  if (isn != NULL)
416 		      isn->isn_arg.op.op_type = *op == '*'
417 				 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
418 		  break;
419 
420 	case '%': if ((type1->tt_type != VAR_UNKNOWN
421 					       && type1->tt_type != VAR_NUMBER)
422 			  || (type2->tt_type != VAR_UNKNOWN
423 					      && type2->tt_type != VAR_NUMBER))
424 		  {
425 		      emsg(_("E1035: % requires number arguments"));
426 		      return FAIL;
427 		  }
428 		  isn = generate_instr_drop(cctx,
429 			      vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
430 		  if (isn != NULL)
431 		      isn->isn_arg.op.op_type = EXPR_REM;
432 		  break;
433     }
434 
435     // correct type of result
436     if (vartype == VAR_UNKNOWN)
437     {
438 	type_T *type = &t_any;
439 
440 #ifdef FEAT_FLOAT
441 	// float+number and number+float results in float
442 	if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
443 		&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
444 	    type = &t_float;
445 #endif
446 	((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
447     }
448 
449     return OK;
450 }
451 
452 /*
453  * Generate an ISN_COMPARE* instruction with a boolean result.
454  */
455     static int
456 generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
457 {
458     isntype_T	isntype = ISN_DROP;
459     isn_T	*isn;
460     garray_T	*stack = &cctx->ctx_type_stack;
461     vartype_T	type1;
462     vartype_T	type2;
463 
464     // Get the known type of the two items on the stack.  If they are matching
465     // use a type-specific instruction. Otherwise fall back to runtime type
466     // checking.
467     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
468     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
469     if (type1 == type2)
470     {
471 	switch (type1)
472 	{
473 	    case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
474 	    case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
475 	    case VAR_NUMBER: isntype = ISN_COMPARENR; break;
476 	    case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
477 	    case VAR_STRING: isntype = ISN_COMPARESTRING; break;
478 	    case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
479 	    case VAR_LIST: isntype = ISN_COMPARELIST; break;
480 	    case VAR_DICT: isntype = ISN_COMPAREDICT; break;
481 	    case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
482 	    case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
483 	    default: isntype = ISN_COMPAREANY; break;
484 	}
485     }
486     else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
487 	    || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
488 	      && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
489 	isntype = ISN_COMPAREANY;
490 
491     if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
492 	    && (isntype == ISN_COMPAREBOOL
493 	    || isntype == ISN_COMPARESPECIAL
494 	    || isntype == ISN_COMPARENR
495 	    || isntype == ISN_COMPAREFLOAT))
496     {
497 	semsg(_("E1037: Cannot use \"%s\" with %s"),
498 		exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
499 	return FAIL;
500     }
501     if (isntype == ISN_DROP
502 	    || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
503 		    && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
504 		       || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
505 	    || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
506 				 && exptype != EXPR_IS && exptype != EXPR_ISNOT
507 		    && (type1 == VAR_BLOB || type2 == VAR_BLOB
508 			|| type1 == VAR_LIST || type2 == VAR_LIST))))
509     {
510 	semsg(_("E1037: Cannot compare %s with %s"),
511 		vartype_name(type1), vartype_name(type2));
512 	return FAIL;
513     }
514 
515     if ((isn = generate_instr(cctx, isntype)) == NULL)
516 	return FAIL;
517     isn->isn_arg.op.op_type = exptype;
518     isn->isn_arg.op.op_ic = ic;
519 
520     // takes two arguments, puts one bool back
521     if (stack->ga_len >= 2)
522     {
523 	--stack->ga_len;
524 	((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
525     }
526 
527     return OK;
528 }
529 
530 /*
531  * Generate an ISN_2BOOL instruction.
532  */
533     static int
534 generate_2BOOL(cctx_T *cctx, int invert)
535 {
536     isn_T	*isn;
537     garray_T	*stack = &cctx->ctx_type_stack;
538 
539     if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
540 	return FAIL;
541     isn->isn_arg.number = invert;
542 
543     // type becomes bool
544     ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
545 
546     return OK;
547 }
548 
549     static int
550 generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
551 {
552     isn_T	*isn;
553     garray_T	*stack = &cctx->ctx_type_stack;
554 
555     if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
556 	return FAIL;
557     isn->isn_arg.type.ct_type = vartype->tt_type;  // TODO: whole type
558     isn->isn_arg.type.ct_off = offset;
559 
560     // type becomes vartype
561     ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
562 
563     return OK;
564 }
565 
566 /*
567  * Generate an ISN_PUSHNR instruction.
568  */
569     static int
570 generate_PUSHNR(cctx_T *cctx, varnumber_T number)
571 {
572     isn_T	*isn;
573 
574     if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
575 	return FAIL;
576     isn->isn_arg.number = number;
577 
578     return OK;
579 }
580 
581 /*
582  * Generate an ISN_PUSHBOOL instruction.
583  */
584     static int
585 generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
586 {
587     isn_T	*isn;
588 
589     if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
590 	return FAIL;
591     isn->isn_arg.number = number;
592 
593     return OK;
594 }
595 
596 /*
597  * Generate an ISN_PUSHSPEC instruction.
598  */
599     static int
600 generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
601 {
602     isn_T	*isn;
603 
604     if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
605 	return FAIL;
606     isn->isn_arg.number = number;
607 
608     return OK;
609 }
610 
611 #ifdef FEAT_FLOAT
612 /*
613  * Generate an ISN_PUSHF instruction.
614  */
615     static int
616 generate_PUSHF(cctx_T *cctx, float_T fnumber)
617 {
618     isn_T	*isn;
619 
620     if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
621 	return FAIL;
622     isn->isn_arg.fnumber = fnumber;
623 
624     return OK;
625 }
626 #endif
627 
628 /*
629  * Generate an ISN_PUSHS instruction.
630  * Consumes "str".
631  */
632     static int
633 generate_PUSHS(cctx_T *cctx, char_u *str)
634 {
635     isn_T	*isn;
636 
637     if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
638 	return FAIL;
639     isn->isn_arg.string = str;
640 
641     return OK;
642 }
643 
644 /*
645  * Generate an ISN_PUSHCHANNEL instruction.
646  * Consumes "channel".
647  */
648     static int
649 generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
650 {
651     isn_T	*isn;
652 
653     if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
654 	return FAIL;
655     isn->isn_arg.channel = channel;
656 
657     return OK;
658 }
659 
660 /*
661  * Generate an ISN_PUSHJOB instruction.
662  * Consumes "job".
663  */
664     static int
665 generate_PUSHJOB(cctx_T *cctx, job_T *job)
666 {
667     isn_T	*isn;
668 
669     if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
670 	return FAIL;
671     isn->isn_arg.job = job;
672 
673     return OK;
674 }
675 
676 /*
677  * Generate an ISN_PUSHBLOB instruction.
678  * Consumes "blob".
679  */
680     static int
681 generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
682 {
683     isn_T	*isn;
684 
685     if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
686 	return FAIL;
687     isn->isn_arg.blob = blob;
688 
689     return OK;
690 }
691 
692 /*
693  * Generate an ISN_PUSHFUNC instruction with name "name".
694  * Consumes "name".
695  */
696     static int
697 generate_PUSHFUNC(cctx_T *cctx, char_u *name)
698 {
699     isn_T	*isn;
700 
701     if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
702 	return FAIL;
703     isn->isn_arg.string = name;
704 
705     return OK;
706 }
707 
708 /*
709  * Generate an ISN_PUSHPARTIAL instruction with partial "part".
710  * Consumes "name".
711  */
712     static int
713 generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
714 {
715     isn_T	*isn;
716 
717     if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
718 						      &t_partial_any)) == NULL)
719 	return FAIL;
720     isn->isn_arg.partial = part;
721 
722     return OK;
723 }
724 
725 /*
726  * Generate an ISN_STORE instruction.
727  */
728     static int
729 generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
730 {
731     isn_T	*isn;
732 
733     if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
734 	return FAIL;
735     if (name != NULL)
736 	isn->isn_arg.string = vim_strsave(name);
737     else
738 	isn->isn_arg.number = idx;
739 
740     return OK;
741 }
742 
743 /*
744  * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
745  */
746     static int
747 generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
748 {
749     isn_T	*isn;
750 
751     if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
752 	return FAIL;
753     isn->isn_arg.storenr.str_idx = idx;
754     isn->isn_arg.storenr.str_val = value;
755 
756     return OK;
757 }
758 
759 /*
760  * Generate an ISN_STOREOPT instruction
761  */
762     static int
763 generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
764 {
765     isn_T	*isn;
766 
767     if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
768 	return FAIL;
769     isn->isn_arg.storeopt.so_name = vim_strsave(name);
770     isn->isn_arg.storeopt.so_flags = opt_flags;
771 
772     return OK;
773 }
774 
775 /*
776  * Generate an ISN_LOAD or similar instruction.
777  */
778     static int
779 generate_LOAD(
780 	cctx_T	    *cctx,
781 	isntype_T   isn_type,
782 	int	    idx,
783 	char_u	    *name,
784 	type_T	    *type)
785 {
786     isn_T	*isn;
787 
788     if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
789 	return FAIL;
790     if (name != NULL)
791 	isn->isn_arg.string = vim_strsave(name);
792     else
793 	isn->isn_arg.number = idx;
794 
795     return OK;
796 }
797 
798 /*
799  * Generate an ISN_LOADV instruction.
800  */
801     static int
802 generate_LOADV(
803 	cctx_T	    *cctx,
804 	char_u	    *name,
805 	int	    error)
806 {
807     // load v:var
808     int vidx = find_vim_var(name);
809 
810     if (vidx < 0)
811     {
812 	if (error)
813 	    semsg(_(e_var_notfound), name);
814 	return FAIL;
815     }
816 
817     // TODO: get actual type
818     return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
819 }
820 
821 /*
822  * Generate an ISN_LOADS instruction.
823  */
824     static int
825 generate_OLDSCRIPT(
826 	cctx_T	    *cctx,
827 	isntype_T   isn_type,
828 	char_u	    *name,
829 	int	    sid,
830 	type_T	    *type)
831 {
832     isn_T	*isn;
833 
834     if (isn_type == ISN_LOADS)
835 	isn = generate_instr_type(cctx, isn_type, type);
836     else
837 	isn = generate_instr_drop(cctx, isn_type, 1);
838     if (isn == NULL)
839 	return FAIL;
840     isn->isn_arg.loadstore.ls_name = vim_strsave(name);
841     isn->isn_arg.loadstore.ls_sid = sid;
842 
843     return OK;
844 }
845 
846 /*
847  * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
848  */
849     static int
850 generate_VIM9SCRIPT(
851 	cctx_T	    *cctx,
852 	isntype_T   isn_type,
853 	int	    sid,
854 	int	    idx,
855 	type_T	    *type)
856 {
857     isn_T	*isn;
858 
859     if (isn_type == ISN_LOADSCRIPT)
860 	isn = generate_instr_type(cctx, isn_type, type);
861     else
862 	isn = generate_instr_drop(cctx, isn_type, 1);
863     if (isn == NULL)
864 	return FAIL;
865     isn->isn_arg.script.script_sid = sid;
866     isn->isn_arg.script.script_idx = idx;
867     return OK;
868 }
869 
870 /*
871  * Generate an ISN_NEWLIST instruction.
872  */
873     static int
874 generate_NEWLIST(cctx_T *cctx, int count)
875 {
876     isn_T	*isn;
877     garray_T	*stack = &cctx->ctx_type_stack;
878     garray_T	*type_list = cctx->ctx_type_list;
879     type_T	*type;
880     type_T	*member;
881 
882     if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
883 	return FAIL;
884     isn->isn_arg.number = count;
885 
886     // drop the value types
887     stack->ga_len -= count;
888 
889     // Use the first value type for the list member type.  Use "void" for an
890     // empty list.
891     if (count > 0)
892 	member = ((type_T **)stack->ga_data)[stack->ga_len];
893     else
894 	member = &t_void;
895     type = get_list_type(member, type_list);
896 
897     // add the list type to the type stack
898     if (ga_grow(stack, 1) == FAIL)
899 	return FAIL;
900     ((type_T **)stack->ga_data)[stack->ga_len] = type;
901     ++stack->ga_len;
902 
903     return OK;
904 }
905 
906 /*
907  * Generate an ISN_NEWDICT instruction.
908  */
909     static int
910 generate_NEWDICT(cctx_T *cctx, int count)
911 {
912     isn_T	*isn;
913     garray_T	*stack = &cctx->ctx_type_stack;
914     garray_T	*type_list = cctx->ctx_type_list;
915     type_T	*type;
916     type_T	*member;
917 
918     if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
919 	return FAIL;
920     isn->isn_arg.number = count;
921 
922     // drop the key and value types
923     stack->ga_len -= 2 * count;
924 
925     // Use the first value type for the list member type.  Use "void" for an
926     // empty dict.
927     if (count > 0)
928 	member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
929     else
930 	member = &t_void;
931     type = get_dict_type(member, type_list);
932 
933     // add the dict type to the type stack
934     if (ga_grow(stack, 1) == FAIL)
935 	return FAIL;
936     ((type_T **)stack->ga_data)[stack->ga_len] = type;
937     ++stack->ga_len;
938 
939     return OK;
940 }
941 
942 /*
943  * Generate an ISN_FUNCREF instruction.
944  */
945     static int
946 generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
947 {
948     isn_T	*isn;
949     garray_T	*stack = &cctx->ctx_type_stack;
950 
951     if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
952 	return FAIL;
953     isn->isn_arg.number = dfunc_idx;
954 
955     if (ga_grow(stack, 1) == FAIL)
956 	return FAIL;
957     ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
958     // TODO: argument and return types
959     ++stack->ga_len;
960 
961     return OK;
962 }
963 
964 /*
965  * Generate an ISN_JUMP instruction.
966  */
967     static int
968 generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
969 {
970     isn_T	*isn;
971     garray_T	*stack = &cctx->ctx_type_stack;
972 
973     if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
974 	return FAIL;
975     isn->isn_arg.jump.jump_when = when;
976     isn->isn_arg.jump.jump_where = where;
977 
978     if (when != JUMP_ALWAYS && stack->ga_len > 0)
979 	--stack->ga_len;
980 
981     return OK;
982 }
983 
984     static int
985 generate_FOR(cctx_T *cctx, int loop_idx)
986 {
987     isn_T	*isn;
988     garray_T	*stack = &cctx->ctx_type_stack;
989 
990     if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
991 	return FAIL;
992     isn->isn_arg.forloop.for_idx = loop_idx;
993 
994     if (ga_grow(stack, 1) == FAIL)
995 	return FAIL;
996     // type doesn't matter, will be stored next
997     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
998     ++stack->ga_len;
999 
1000     return OK;
1001 }
1002 
1003 /*
1004  * Generate an ISN_BCALL instruction.
1005  * Return FAIL if the number of arguments is wrong.
1006  */
1007     static int
1008 generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1009 {
1010     isn_T	*isn;
1011     garray_T	*stack = &cctx->ctx_type_stack;
1012     type_T	*argtypes[MAX_FUNC_ARGS];
1013     int		i;
1014 
1015     if (check_internal_func(func_idx, argcount) == FAIL)
1016 	return FAIL;
1017 
1018     if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1019 	return FAIL;
1020     isn->isn_arg.bfunc.cbf_idx = func_idx;
1021     isn->isn_arg.bfunc.cbf_argcount = argcount;
1022 
1023     for (i = 0; i < argcount; ++i)
1024 	argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1025 
1026     stack->ga_len -= argcount; // drop the arguments
1027     if (ga_grow(stack, 1) == FAIL)
1028 	return FAIL;
1029     ((type_T **)stack->ga_data)[stack->ga_len] =
1030 			  internal_func_ret_type(func_idx, argcount, argtypes);
1031     ++stack->ga_len;	    // add return value
1032 
1033     return OK;
1034 }
1035 
1036 /*
1037  * Generate an ISN_DCALL or ISN_UCALL instruction.
1038  * Return FAIL if the number of arguments is wrong.
1039  */
1040     static int
1041 generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1042 {
1043     isn_T	*isn;
1044     garray_T	*stack = &cctx->ctx_type_stack;
1045     int		regular_args = ufunc->uf_args.ga_len;
1046     int		argcount = pushed_argcount;
1047 
1048     if (argcount > regular_args && !has_varargs(ufunc))
1049     {
1050 	semsg(_(e_toomanyarg), ufunc->uf_name);
1051 	return FAIL;
1052     }
1053     if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1054     {
1055 	semsg(_(e_toofewarg), ufunc->uf_name);
1056 	return FAIL;
1057     }
1058 
1059     // Turn varargs into a list.
1060     if (ufunc->uf_va_name != NULL)
1061     {
1062 	int count = argcount - regular_args;
1063 
1064 	// If count is negative an empty list will be added after evaluating
1065 	// default values for missing optional arguments.
1066 	if (count >= 0)
1067 	{
1068 	    generate_NEWLIST(cctx, count);
1069 	    argcount = regular_args + 1;
1070 	}
1071     }
1072 
1073     if ((isn = generate_instr(cctx,
1074 		    ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1075 	return FAIL;
1076     if (ufunc->uf_dfunc_idx >= 0)
1077     {
1078 	isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1079 	isn->isn_arg.dfunc.cdf_argcount = argcount;
1080     }
1081     else
1082     {
1083 	// A user function may be deleted and redefined later, can't use the
1084 	// ufunc pointer, need to look it up again at runtime.
1085 	isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1086 	isn->isn_arg.ufunc.cuf_argcount = argcount;
1087     }
1088 
1089     stack->ga_len -= argcount; // drop the arguments
1090     if (ga_grow(stack, 1) == FAIL)
1091 	return FAIL;
1092     // add return value
1093     ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1094     ++stack->ga_len;
1095 
1096     return OK;
1097 }
1098 
1099 /*
1100  * Generate an ISN_UCALL instruction when the function isn't defined yet.
1101  */
1102     static int
1103 generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1104 {
1105     isn_T	*isn;
1106     garray_T	*stack = &cctx->ctx_type_stack;
1107 
1108     if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1109 	return FAIL;
1110     isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1111     isn->isn_arg.ufunc.cuf_argcount = argcount;
1112 
1113     stack->ga_len -= argcount; // drop the arguments
1114     if (ga_grow(stack, 1) == FAIL)
1115 	return FAIL;
1116     // add return value
1117     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1118     ++stack->ga_len;
1119 
1120     return OK;
1121 }
1122 
1123 /*
1124  * Generate an ISN_PCALL instruction.
1125  */
1126     static int
1127 generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1128 {
1129     isn_T	*isn;
1130     garray_T	*stack = &cctx->ctx_type_stack;
1131 
1132     if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1133 	return FAIL;
1134     isn->isn_arg.pfunc.cpf_top = at_top;
1135     isn->isn_arg.pfunc.cpf_argcount = argcount;
1136 
1137     stack->ga_len -= argcount; // drop the arguments
1138 
1139     // drop the funcref/partial, get back the return value
1140     ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1141 
1142     return OK;
1143 }
1144 
1145 /*
1146  * Generate an ISN_MEMBER instruction.
1147  */
1148     static int
1149 generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1150 {
1151     isn_T	*isn;
1152     garray_T	*stack = &cctx->ctx_type_stack;
1153     type_T	*type;
1154 
1155     if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1156 	return FAIL;
1157     isn->isn_arg.string = vim_strnsave(name, (int)len);
1158 
1159     // check for dict type
1160     type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1161     if (type->tt_type != VAR_DICT && type != &t_any)
1162     {
1163 	emsg(_(e_dictreq));
1164 	return FAIL;
1165     }
1166     // change dict type to dict member type
1167     if (type->tt_type == VAR_DICT)
1168 	((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1169 
1170     return OK;
1171 }
1172 
1173 /*
1174  * Generate an ISN_ECHO instruction.
1175  */
1176     static int
1177 generate_ECHO(cctx_T *cctx, int with_white, int count)
1178 {
1179     isn_T	*isn;
1180 
1181     if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1182 	return FAIL;
1183     isn->isn_arg.echo.echo_with_white = with_white;
1184     isn->isn_arg.echo.echo_count = count;
1185 
1186     return OK;
1187 }
1188 
1189 /*
1190  * Generate an ISN_EXECUTE instruction.
1191  */
1192     static int
1193 generate_EXECUTE(cctx_T *cctx, int count)
1194 {
1195     isn_T	*isn;
1196 
1197     if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1198 	return FAIL;
1199     isn->isn_arg.number = count;
1200 
1201     return OK;
1202 }
1203 
1204     static int
1205 generate_EXEC(cctx_T *cctx, char_u *line)
1206 {
1207     isn_T	*isn;
1208 
1209     if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1210 	return FAIL;
1211     isn->isn_arg.string = vim_strsave(line);
1212     return OK;
1213 }
1214 
1215 static char e_white_both[] =
1216 			N_("E1004: white space required before and after '%s'");
1217 
1218 /*
1219  * Reserve space for a local variable.
1220  * Return the index or -1 if it failed.
1221  */
1222     static int
1223 reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1224 {
1225     int	    idx;
1226     lvar_T  *lvar;
1227 
1228     if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1229     {
1230 	emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1231 	return -1;
1232     }
1233 
1234     if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1235 	return -1;
1236     idx = cctx->ctx_locals.ga_len;
1237     if (cctx->ctx_max_local < idx + 1)
1238 	cctx->ctx_max_local = idx + 1;
1239     ++cctx->ctx_locals.ga_len;
1240 
1241     lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1242     lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1243     lvar->lv_const = isConst;
1244     lvar->lv_type = type;
1245 
1246     return idx;
1247 }
1248 
1249 /*
1250  * Skip over a type definition and return a pointer to just after it.
1251  */
1252     char_u *
1253 skip_type(char_u *start)
1254 {
1255     char_u *p = start;
1256 
1257     while (ASCII_ISALNUM(*p) || *p == '_')
1258 	++p;
1259 
1260     // Skip over "<type>"; this is permissive about white space.
1261     if (*skipwhite(p) == '<')
1262     {
1263 	p = skipwhite(p);
1264 	p = skip_type(skipwhite(p + 1));
1265 	p = skipwhite(p);
1266 	if (*p == '>')
1267 	    ++p;
1268     }
1269     return p;
1270 }
1271 
1272 /*
1273  * Parse the member type: "<type>" and return "type" with the member set.
1274  * Use "type_list" if a new type needs to be added.
1275  * Returns NULL in case of failure.
1276  */
1277     static type_T *
1278 parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1279 {
1280     type_T  *member_type;
1281 
1282     if (**arg != '<')
1283     {
1284 	if (*skipwhite(*arg) == '<')
1285 	    emsg(_("E1007: No white space allowed before <"));
1286 	else
1287 	    emsg(_("E1008: Missing <type>"));
1288 	return NULL;
1289     }
1290     *arg = skipwhite(*arg + 1);
1291 
1292     member_type = parse_type(arg, type_list);
1293     if (member_type == NULL)
1294 	return NULL;
1295 
1296     *arg = skipwhite(*arg);
1297     if (**arg != '>')
1298     {
1299 	emsg(_("E1009: Missing > after type"));
1300 	return NULL;
1301     }
1302     ++*arg;
1303 
1304     if (type->tt_type == VAR_LIST)
1305 	return get_list_type(member_type, type_list);
1306     return get_dict_type(member_type, type_list);
1307 }
1308 
1309 /*
1310  * Parse a type at "arg" and advance over it.
1311  * Return NULL for failure.
1312  */
1313     type_T *
1314 parse_type(char_u **arg, garray_T *type_list)
1315 {
1316     char_u  *p = *arg;
1317     size_t  len;
1318 
1319     // skip over the first word
1320     while (ASCII_ISALNUM(*p) || *p == '_')
1321 	++p;
1322     len = p - *arg;
1323 
1324     switch (**arg)
1325     {
1326 	case 'a':
1327 	    if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1328 	    {
1329 		*arg += len;
1330 		return &t_any;
1331 	    }
1332 	    break;
1333 	case 'b':
1334 	    if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1335 	    {
1336 		*arg += len;
1337 		return &t_bool;
1338 	    }
1339 	    if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1340 	    {
1341 		*arg += len;
1342 		return &t_blob;
1343 	    }
1344 	    break;
1345 	case 'c':
1346 	    if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1347 	    {
1348 		*arg += len;
1349 		return &t_channel;
1350 	    }
1351 	    break;
1352 	case 'd':
1353 	    if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1354 	    {
1355 		*arg += len;
1356 		return parse_type_member(arg, &t_dict_any, type_list);
1357 	    }
1358 	    break;
1359 	case 'f':
1360 	    if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1361 	    {
1362 #ifdef FEAT_FLOAT
1363 		*arg += len;
1364 		return &t_float;
1365 #else
1366 		emsg(_("E1055: This Vim is not compiled with float support"));
1367 		return &t_any;
1368 #endif
1369 	    }
1370 	    if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1371 	    {
1372 		*arg += len;
1373 		// TODO: arguments and return type
1374 		return &t_func_any;
1375 	    }
1376 	    break;
1377 	case 'j':
1378 	    if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1379 	    {
1380 		*arg += len;
1381 		return &t_job;
1382 	    }
1383 	    break;
1384 	case 'l':
1385 	    if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1386 	    {
1387 		*arg += len;
1388 		return parse_type_member(arg, &t_list_any, type_list);
1389 	    }
1390 	    break;
1391 	case 'n':
1392 	    if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1393 	    {
1394 		*arg += len;
1395 		return &t_number;
1396 	    }
1397 	    break;
1398 	case 'p':
1399 	    if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
1400 	    {
1401 		*arg += len;
1402 		// TODO: arguments and return type
1403 		return &t_partial_any;
1404 	    }
1405 	    break;
1406 	case 's':
1407 	    if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1408 	    {
1409 		*arg += len;
1410 		return &t_string;
1411 	    }
1412 	    break;
1413 	case 'v':
1414 	    if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1415 	    {
1416 		*arg += len;
1417 		return &t_void;
1418 	    }
1419 	    break;
1420     }
1421 
1422     semsg(_("E1010: Type not recognized: %s"), *arg);
1423     return &t_any;
1424 }
1425 
1426 /*
1427  * Check if "type1" and "type2" are exactly the same.
1428  */
1429     static int
1430 equal_type(type_T *type1, type_T *type2)
1431 {
1432     if (type1->tt_type != type2->tt_type)
1433 	return FALSE;
1434     switch (type1->tt_type)
1435     {
1436 	case VAR_VOID:
1437 	case VAR_UNKNOWN:
1438 	case VAR_SPECIAL:
1439 	case VAR_BOOL:
1440 	case VAR_NUMBER:
1441 	case VAR_FLOAT:
1442 	case VAR_STRING:
1443 	case VAR_BLOB:
1444 	case VAR_JOB:
1445 	case VAR_CHANNEL:
1446 	    return TRUE;  // not composite is always OK
1447 	case VAR_LIST:
1448 	case VAR_DICT:
1449 	    return equal_type(type1->tt_member, type2->tt_member);
1450 	case VAR_FUNC:
1451 	case VAR_PARTIAL:
1452 	    // TODO; check argument types.
1453 	    return equal_type(type1->tt_member, type2->tt_member)
1454 		&& type1->tt_argcount == type2->tt_argcount;
1455     }
1456     return TRUE;
1457 }
1458 
1459 /*
1460  * Find the common type of "type1" and "type2" and put it in "dest".
1461  * "type2" and "dest" may be the same.
1462  */
1463     static void
1464 common_type(type_T *type1, type_T *type2, type_T *dest)
1465 {
1466     if (equal_type(type1, type2))
1467     {
1468 	if (dest != type2)
1469 	    *dest = *type2;
1470 	return;
1471     }
1472 
1473     if (type1->tt_type == type2->tt_type)
1474     {
1475 	dest->tt_type = type1->tt_type;
1476 	if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1477 	{
1478 	    common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1479 	    return;
1480 	}
1481 	// TODO: VAR_FUNC and VAR_PARTIAL
1482     }
1483 
1484     dest->tt_type = VAR_UNKNOWN;  // "any"
1485 }
1486 
1487     char *
1488 vartype_name(vartype_T type)
1489 {
1490     switch (type)
1491     {
1492 	case VAR_VOID: return "void";
1493 	case VAR_UNKNOWN: return "any";
1494 	case VAR_SPECIAL: return "special";
1495 	case VAR_BOOL: return "bool";
1496 	case VAR_NUMBER: return "number";
1497 	case VAR_FLOAT: return "float";
1498 	case VAR_STRING: return "string";
1499 	case VAR_BLOB: return "blob";
1500 	case VAR_JOB: return "job";
1501 	case VAR_CHANNEL: return "channel";
1502 	case VAR_LIST: return "list";
1503 	case VAR_DICT: return "dict";
1504 	case VAR_FUNC: return "function";
1505 	case VAR_PARTIAL: return "partial";
1506     }
1507     return "???";
1508 }
1509 
1510 /*
1511  * Return the name of a type.
1512  * The result may be in allocated memory, in which case "tofree" is set.
1513  */
1514     char *
1515 type_name(type_T *type, char **tofree)
1516 {
1517     char *name = vartype_name(type->tt_type);
1518 
1519     *tofree = NULL;
1520     if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1521     {
1522 	char *member_free;
1523 	char *member_name = type_name(type->tt_member, &member_free);
1524 	size_t len;
1525 
1526 	len = STRLEN(name) + STRLEN(member_name) + 3;
1527 	*tofree = alloc(len);
1528 	if (*tofree != NULL)
1529 	{
1530 	    vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1531 	    vim_free(member_free);
1532 	    return *tofree;
1533 	}
1534     }
1535     // TODO: function and partial argument types
1536 
1537     return name;
1538 }
1539 
1540 /*
1541  * Find "name" in script-local items of script "sid".
1542  * Returns the index in "sn_var_vals" if found.
1543  * If found but not in "sn_var_vals" returns -1.
1544  * If not found returns -2.
1545  */
1546     int
1547 get_script_item_idx(int sid, char_u *name, int check_writable)
1548 {
1549     hashtab_T	    *ht;
1550     dictitem_T	    *di;
1551     scriptitem_T    *si = SCRIPT_ITEM(sid);
1552     int		    idx;
1553 
1554     // First look the name up in the hashtable.
1555     if (sid <= 0 || sid > script_items.ga_len)
1556 	return -1;
1557     ht = &SCRIPT_VARS(sid);
1558     di = find_var_in_ht(ht, 0, name, TRUE);
1559     if (di == NULL)
1560 	return -2;
1561 
1562     // Now find the svar_T index in sn_var_vals.
1563     for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1564     {
1565 	svar_T    *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1566 
1567 	if (sv->sv_tv == &di->di_tv)
1568 	{
1569 	    if (check_writable && sv->sv_const)
1570 		semsg(_(e_readonlyvar), name);
1571 	    return idx;
1572 	}
1573     }
1574     return -1;
1575 }
1576 
1577 /*
1578  * Find "name" in imported items of the current script/
1579  */
1580     imported_T *
1581 find_imported(char_u *name, size_t len, cctx_T *cctx)
1582 {
1583     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
1584     int		    idx;
1585 
1586     if (cctx != NULL)
1587 	for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1588 	{
1589 	    imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1590 									 + idx;
1591 
1592 	    if (len == 0 ? STRCMP(name, import->imp_name) == 0
1593 			 : STRLEN(import->imp_name) == len
1594 				  && STRNCMP(name, import->imp_name, len) == 0)
1595 		return import;
1596 	}
1597 
1598     for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1599     {
1600 	imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1601 
1602 	if (len == 0 ? STRCMP(name, import->imp_name) == 0
1603 		     : STRLEN(import->imp_name) == len
1604 				  && STRNCMP(name, import->imp_name, len) == 0)
1605 	    return import;
1606     }
1607     return NULL;
1608 }
1609 
1610 /*
1611  * Generate an instruction to load script-local variable "name".
1612  */
1613     static int
1614 compile_load_scriptvar(
1615 	cctx_T *cctx,
1616 	char_u *name,	    // variable NUL terminated
1617 	char_u *start,	    // start of variable
1618 	char_u **end,	    // end of variable
1619 	int    error)	    // when TRUE may give error
1620 {
1621     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
1622     int		    idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1623     imported_T	    *import;
1624 
1625     if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
1626     {
1627 	// variable is not in sn_var_vals: old style script.
1628 	return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1629 								       &t_any);
1630     }
1631     if (idx >= 0)
1632     {
1633 	svar_T		*sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1634 
1635 	generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1636 					current_sctx.sc_sid, idx, sv->sv_type);
1637 	return OK;
1638     }
1639 
1640     import = find_imported(name, 0, cctx);
1641     if (import != NULL)
1642     {
1643 	if (import->imp_all)
1644 	{
1645 	    char_u	*p = skipwhite(*end);
1646 	    int		name_len;
1647 	    ufunc_T	*ufunc;
1648 	    type_T	*type;
1649 
1650 	    // Used "import * as Name", need to lookup the member.
1651 	    if (*p != '.')
1652 	    {
1653 		semsg(_("E1060: expected dot after name: %s"), start);
1654 		return FAIL;
1655 	    }
1656 	    ++p;
1657 
1658 	    idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1659 	    // TODO: what if it is a function?
1660 	    if (idx < 0)
1661 		return FAIL;
1662 	    *end = p;
1663 
1664 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1665 		    import->imp_sid,
1666 		    idx,
1667 		    type);
1668 	}
1669 	else
1670 	{
1671 	    // TODO: check this is a variable, not a function
1672 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1673 		    import->imp_sid,
1674 		    import->imp_var_vals_idx,
1675 		    import->imp_type);
1676 	}
1677 	return OK;
1678     }
1679 
1680     if (error)
1681 	semsg(_("E1050: Item not found: %s"), name);
1682     return FAIL;
1683 }
1684 
1685 /*
1686  * Compile a variable name into a load instruction.
1687  * "end" points to just after the name.
1688  * When "error" is FALSE do not give an error when not found.
1689  */
1690     static int
1691 compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
1692 {
1693     type_T	*type;
1694     char_u	*name;
1695     char_u	*end = end_arg;
1696     int		res = FAIL;
1697 
1698     if (*(*arg + 1) == ':')
1699     {
1700 	// load namespaced variable
1701 	name = vim_strnsave(*arg + 2, end - (*arg + 2));
1702 	if (name == NULL)
1703 	    return FAIL;
1704 
1705 	if (**arg == 'v')
1706 	{
1707 	    res = generate_LOADV(cctx, name, error);
1708 	}
1709 	else if (**arg == 'g')
1710 	{
1711 	    // Global variables can be defined later, thus we don't check if it
1712 	    // exists, give error at runtime.
1713 	    res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1714 	}
1715 	else if (**arg == 's')
1716 	{
1717 	    res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
1718 	}
1719 	else
1720 	{
1721 	    semsg("Namespace not supported yet: %s", **arg);
1722 	    goto theend;
1723 	}
1724     }
1725     else
1726     {
1727 	size_t	    len = end - *arg;
1728 	int	    idx;
1729 	int	    gen_load = FALSE;
1730 
1731 	name = vim_strnsave(*arg, end - *arg);
1732 	if (name == NULL)
1733 	    return FAIL;
1734 
1735 	idx = lookup_arg(*arg, len, cctx);
1736 	if (idx >= 0)
1737 	{
1738 	    if (cctx->ctx_ufunc->uf_arg_types != NULL)
1739 		type = cctx->ctx_ufunc->uf_arg_types[idx];
1740 	    else
1741 		type = &t_any;
1742 
1743 	    // Arguments are located above the frame pointer.
1744 	    idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1745 	    if (cctx->ctx_ufunc->uf_va_name != NULL)
1746 		--idx;
1747 	    gen_load = TRUE;
1748 	}
1749 	else if (lookup_vararg(*arg, len, cctx))
1750 	{
1751 	    // varargs is always the last argument
1752 	    idx = -STACK_FRAME_SIZE - 1;
1753 	    type = cctx->ctx_ufunc->uf_va_type;
1754 	    gen_load = TRUE;
1755 	}
1756 	else
1757 	{
1758 	    idx = lookup_local(*arg, len, cctx);
1759 	    if (idx >= 0)
1760 	    {
1761 		type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1762 		gen_load = TRUE;
1763 	    }
1764 	    else
1765 	    {
1766 		if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1767 			|| (len == 5 && STRNCMP("false", *arg, 5) == 0))
1768 		    res = generate_PUSHBOOL(cctx, **arg == 't'
1769 						     ? VVAL_TRUE : VVAL_FALSE);
1770 		else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1771 							== SCRIPT_VERSION_VIM9)
1772 		    // in Vim9 script "var" can be script-local.
1773 		   res = compile_load_scriptvar(cctx, name, *arg, &end, error);
1774 	    }
1775 	}
1776 	if (gen_load)
1777 	    res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1778     }
1779 
1780     *arg = end;
1781 
1782 theend:
1783     if (res == FAIL && error)
1784 	semsg(_(e_var_notfound), name);
1785     vim_free(name);
1786     return res;
1787 }
1788 
1789 /*
1790  * Compile the argument expressions.
1791  * "arg" points to just after the "(" and is advanced to after the ")"
1792  */
1793     static int
1794 compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1795 {
1796     char_u *p = *arg;
1797 
1798     while (*p != NUL && *p != ')')
1799     {
1800 	if (compile_expr1(&p, cctx) == FAIL)
1801 	    return FAIL;
1802 	++*argcount;
1803 
1804 	if (*p != ',' && *skipwhite(p) == ',')
1805 	{
1806 	    emsg(_("E1068: No white space allowed before ,"));
1807 	    p = skipwhite(p);
1808 	}
1809 	if (*p == ',')
1810 	{
1811 	    ++p;
1812 	    if (!VIM_ISWHITE(*p))
1813 		emsg(_("E1069: white space required after ,"));
1814 	}
1815 	p = skipwhite(p);
1816     }
1817     p = skipwhite(p);
1818     if (*p != ')')
1819     {
1820 	emsg(_(e_missing_close));
1821 	return FAIL;
1822     }
1823     *arg = p + 1;
1824     return OK;
1825 }
1826 
1827 /*
1828  * Compile a function call:  name(arg1, arg2)
1829  * "arg" points to "name", "arg + varlen" to the "(".
1830  * "argcount_init" is 1 for "value->method()"
1831  * Instructions:
1832  *	EVAL arg1
1833  *	EVAL arg2
1834  *	BCALL / DCALL / UCALL
1835  */
1836     static int
1837 compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1838 {
1839     char_u	*name = *arg;
1840     char_u	*p;
1841     int		argcount = argcount_init;
1842     char_u	namebuf[100];
1843     char_u	fname_buf[FLEN_FIXED + 1];
1844     char_u	*tofree = NULL;
1845     int		error = FCERR_NONE;
1846     ufunc_T	*ufunc;
1847     int		res = FAIL;
1848 
1849     if (varlen >= sizeof(namebuf))
1850     {
1851 	semsg(_("E1011: name too long: %s"), name);
1852 	return FAIL;
1853     }
1854     vim_strncpy(namebuf, *arg, varlen);
1855     name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1856 
1857     *arg = skipwhite(*arg + varlen + 1);
1858     if (compile_arguments(arg, cctx, &argcount) == FAIL)
1859 	goto theend;
1860 
1861     if (ASCII_ISLOWER(*name) && name[1] != ':')
1862     {
1863 	int	    idx;
1864 
1865 	// builtin function
1866 	idx = find_internal_func(name);
1867 	if (idx >= 0)
1868 	{
1869 	    res = generate_BCALL(cctx, idx, argcount);
1870 	    goto theend;
1871 	}
1872 	semsg(_(e_unknownfunc), namebuf);
1873     }
1874 
1875     // If we can find the function by name generate the right call.
1876     ufunc = find_func(name, cctx);
1877     if (ufunc != NULL)
1878     {
1879 	res = generate_CALL(cctx, ufunc, argcount);
1880 	goto theend;
1881     }
1882 
1883     // If the name is a variable, load it and use PCALL.
1884     p = namebuf;
1885     if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
1886     {
1887 	res = generate_PCALL(cctx, argcount, FALSE);
1888 	goto theend;
1889     }
1890 
1891     // The function may be defined only later.  Need to figure out at runtime.
1892     res = generate_UCALL(cctx, name, argcount);
1893 
1894 theend:
1895     vim_free(tofree);
1896     return res;
1897 }
1898 
1899 // like NAMESPACE_CHAR but with 'a' and 'l'.
1900 #define VIM9_NAMESPACE_CHAR	(char_u *)"bgstvw"
1901 
1902 /*
1903  * Find the end of a variable or function name.  Unlike find_name_end() this
1904  * does not recognize magic braces.
1905  * Return a pointer to just after the name.  Equal to "arg" if there is no
1906  * valid name.
1907  */
1908     char_u *
1909 to_name_end(char_u *arg)
1910 {
1911     char_u	*p;
1912 
1913     // Quick check for valid starting character.
1914     if (!eval_isnamec1(*arg))
1915 	return arg;
1916 
1917     for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1918 	// Include a namespace such as "s:var" and "v:var".  But "n:" is not
1919 	// and can be used in slice "[n:]".
1920 	if (*p == ':' && (p != arg + 1
1921 			     || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1922 	    break;
1923     return p;
1924 }
1925 
1926 /*
1927  * Like to_name_end() but also skip over a list or dict constant.
1928  */
1929     char_u *
1930 to_name_const_end(char_u *arg)
1931 {
1932     char_u	*p = to_name_end(arg);
1933     typval_T	rettv;
1934 
1935     if (p == arg && *arg == '[')
1936     {
1937 
1938 	// Can be "[1, 2, 3]->Func()".
1939 	if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1940 	    p = arg;
1941     }
1942     else if (p == arg && *arg == '#' && arg[1] == '{')
1943     {
1944 	++p;
1945 	if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1946 	    p = arg;
1947     }
1948     else if (p == arg && *arg == '{')
1949     {
1950 	int	    ret = get_lambda_tv(&p, &rettv, FALSE);
1951 
1952 	if (ret == NOTDONE)
1953 	    ret = eval_dict(&p, &rettv, FALSE, FALSE);
1954 	if (ret != OK)
1955 	    p = arg;
1956     }
1957 
1958     return p;
1959 }
1960 
1961     static void
1962 type_mismatch(type_T *expected, type_T *actual)
1963 {
1964     char *tofree1, *tofree2;
1965 
1966     semsg(_("E1013: type mismatch, expected %s but got %s"),
1967 		   type_name(expected, &tofree1), type_name(actual, &tofree2));
1968     vim_free(tofree1);
1969     vim_free(tofree2);
1970 }
1971 
1972 /*
1973  * Check if the expected and actual types match.
1974  */
1975     static int
1976 check_type(type_T *expected, type_T *actual, int give_msg)
1977 {
1978     if (expected->tt_type != VAR_UNKNOWN)
1979     {
1980 	if (expected->tt_type != actual->tt_type)
1981 	{
1982 	    if (give_msg)
1983 		type_mismatch(expected, actual);
1984 	    return FAIL;
1985 	}
1986 	if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1987 	{
1988 	    int ret;
1989 
1990 	    // void is used for an empty list or dict
1991 	    if (actual->tt_member == &t_void)
1992 		ret = OK;
1993 	    else
1994 		ret = check_type(expected->tt_member, actual->tt_member, FALSE);
1995 	    if (ret == FAIL && give_msg)
1996 		type_mismatch(expected, actual);
1997 	    return ret;
1998 	}
1999     }
2000     return OK;
2001 }
2002 
2003 /*
2004  * Check that
2005  * - "actual" is "expected" type or
2006  * - "actual" is a type that can be "expected" type: add a runtime check; or
2007  * - return FAIL.
2008  */
2009     static int
2010 need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2011 {
2012     if (check_type(expected, actual, FALSE))
2013 	return OK;
2014     if (actual->tt_type != VAR_UNKNOWN)
2015     {
2016 	type_mismatch(expected, actual);
2017 	return FAIL;
2018     }
2019     generate_TYPECHECK(cctx, expected, offset);
2020     return OK;
2021 }
2022 
2023 /*
2024  * parse a list: [expr, expr]
2025  * "*arg" points to the '['.
2026  */
2027     static int
2028 compile_list(char_u **arg, cctx_T *cctx)
2029 {
2030     char_u	*p = skipwhite(*arg + 1);
2031     int		count = 0;
2032 
2033     while (*p != ']')
2034     {
2035 	if (*p == NUL)
2036 	    return FAIL;
2037 	if (compile_expr1(&p, cctx) == FAIL)
2038 	    break;
2039 	++count;
2040 	if (*p == ',')
2041 	    ++p;
2042 	p = skipwhite(p);
2043     }
2044     *arg = p + 1;
2045 
2046     generate_NEWLIST(cctx, count);
2047     return OK;
2048 }
2049 
2050 /*
2051  * parse a lambda: {arg, arg -> expr}
2052  * "*arg" points to the '{'.
2053  */
2054     static int
2055 compile_lambda(char_u **arg, cctx_T *cctx)
2056 {
2057     garray_T	*instr = &cctx->ctx_instr;
2058     typval_T	rettv;
2059     ufunc_T	*ufunc;
2060 
2061     // Get the funcref in "rettv".
2062     if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2063 	return FAIL;
2064     ufunc = rettv.vval.v_partial->pt_func;
2065 
2066     // The function will have one line: "return {expr}".
2067     // Compile it into instructions.
2068     compile_def_function(ufunc, TRUE);
2069 
2070     if (ufunc->uf_dfunc_idx >= 0)
2071     {
2072 	if (ga_grow(instr, 1) == FAIL)
2073 	    return FAIL;
2074 	generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2075 	return OK;
2076     }
2077     return FAIL;
2078 }
2079 
2080 /*
2081  * Compile a lamda call: expr->{lambda}(args)
2082  * "arg" points to the "{".
2083  */
2084     static int
2085 compile_lambda_call(char_u **arg, cctx_T *cctx)
2086 {
2087     ufunc_T	*ufunc;
2088     typval_T	rettv;
2089     int		argcount = 1;
2090     int		ret = FAIL;
2091 
2092     // Get the funcref in "rettv".
2093     if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2094 	return FAIL;
2095 
2096     if (**arg != '(')
2097     {
2098 	if (*skipwhite(*arg) == '(')
2099 	    semsg(_(e_nowhitespace));
2100 	else
2101 	    semsg(_(e_missing_paren), "lambda");
2102 	clear_tv(&rettv);
2103 	return FAIL;
2104     }
2105 
2106     // The function will have one line: "return {expr}".
2107     // Compile it into instructions.
2108     ufunc = rettv.vval.v_partial->pt_func;
2109     ++ufunc->uf_refcount;
2110     compile_def_function(ufunc, TRUE);
2111 
2112     // compile the arguments
2113     *arg = skipwhite(*arg + 1);
2114     if (compile_arguments(arg, cctx, &argcount) == OK)
2115 	// call the compiled function
2116 	ret = generate_CALL(cctx, ufunc, argcount);
2117 
2118     clear_tv(&rettv);
2119     return ret;
2120 }
2121 
2122 /*
2123  * parse a dict: {'key': val} or #{key: val}
2124  * "*arg" points to the '{'.
2125  */
2126     static int
2127 compile_dict(char_u **arg, cctx_T *cctx, int literal)
2128 {
2129     garray_T	*instr = &cctx->ctx_instr;
2130     int		count = 0;
2131     dict_T	*d = dict_alloc();
2132     dictitem_T	*item;
2133 
2134     if (d == NULL)
2135 	return FAIL;
2136     *arg = skipwhite(*arg + 1);
2137     while (**arg != '}' && **arg != NUL)
2138     {
2139 	char_u *key = NULL;
2140 
2141 	if (literal)
2142 	{
2143 	    char_u *p = to_name_end(*arg);
2144 
2145 	    if (p == *arg)
2146 	    {
2147 		semsg(_("E1014: Invalid key: %s"), *arg);
2148 		return FAIL;
2149 	    }
2150 	    key = vim_strnsave(*arg, p - *arg);
2151 	    if (generate_PUSHS(cctx, key) == FAIL)
2152 		return FAIL;
2153 	    *arg = p;
2154 	}
2155 	else
2156 	{
2157 	    isn_T		*isn;
2158 
2159 	    if (compile_expr1(arg, cctx) == FAIL)
2160 		return FAIL;
2161 	    // TODO: check type is string
2162 	    isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2163 	    if (isn->isn_type == ISN_PUSHS)
2164 		key = isn->isn_arg.string;
2165 	}
2166 
2167 	// Check for duplicate keys, if using string keys.
2168 	if (key != NULL)
2169 	{
2170 	    item = dict_find(d, key, -1);
2171 	    if (item != NULL)
2172 	    {
2173 		semsg(_(e_duplicate_key), key);
2174 		goto failret;
2175 	    }
2176 	    item = dictitem_alloc(key);
2177 	    if (item != NULL)
2178 	    {
2179 		item->di_tv.v_type = VAR_UNKNOWN;
2180 		item->di_tv.v_lock = 0;
2181 		if (dict_add(d, item) == FAIL)
2182 		    dictitem_free(item);
2183 	    }
2184 	}
2185 
2186 	*arg = skipwhite(*arg);
2187 	if (**arg != ':')
2188 	{
2189 	    semsg(_(e_missing_dict_colon), *arg);
2190 	    return FAIL;
2191 	}
2192 
2193 	*arg = skipwhite(*arg + 1);
2194 	if (compile_expr1(arg, cctx) == FAIL)
2195 	    return FAIL;
2196 	++count;
2197 
2198 	if (**arg == '}')
2199 	    break;
2200 	if (**arg != ',')
2201 	{
2202 	    semsg(_(e_missing_dict_comma), *arg);
2203 	    goto failret;
2204 	}
2205 	*arg = skipwhite(*arg + 1);
2206     }
2207 
2208     if (**arg != '}')
2209     {
2210 	semsg(_(e_missing_dict_end), *arg);
2211 	goto failret;
2212     }
2213     *arg = *arg + 1;
2214 
2215     dict_unref(d);
2216     return generate_NEWDICT(cctx, count);
2217 
2218 failret:
2219     dict_unref(d);
2220     return FAIL;
2221 }
2222 
2223 /*
2224  * Compile "&option".
2225  */
2226     static int
2227 compile_get_option(char_u **arg, cctx_T *cctx)
2228 {
2229     typval_T	rettv;
2230     char_u	*start = *arg;
2231     int		ret;
2232 
2233     // parse the option and get the current value to get the type.
2234     rettv.v_type = VAR_UNKNOWN;
2235     ret = get_option_tv(arg, &rettv, TRUE);
2236     if (ret == OK)
2237     {
2238 	// include the '&' in the name, get_option_tv() expects it.
2239 	char_u *name = vim_strnsave(start, *arg - start);
2240 	type_T	*type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2241 
2242 	ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2243 	vim_free(name);
2244     }
2245     clear_tv(&rettv);
2246 
2247     return ret;
2248 }
2249 
2250 /*
2251  * Compile "$VAR".
2252  */
2253     static int
2254 compile_get_env(char_u **arg, cctx_T *cctx)
2255 {
2256     char_u	*start = *arg;
2257     int		len;
2258     int		ret;
2259     char_u	*name;
2260 
2261     ++*arg;
2262     len = get_env_len(arg);
2263     if (len == 0)
2264     {
2265 	semsg(_(e_syntax_at), start - 1);
2266 	return FAIL;
2267     }
2268 
2269     // include the '$' in the name, get_env_tv() expects it.
2270     name = vim_strnsave(start, len + 1);
2271     ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2272     vim_free(name);
2273     return ret;
2274 }
2275 
2276 /*
2277  * Compile "@r".
2278  */
2279     static int
2280 compile_get_register(char_u **arg, cctx_T *cctx)
2281 {
2282     int		ret;
2283 
2284     ++*arg;
2285     if (**arg == NUL)
2286     {
2287 	semsg(_(e_syntax_at), *arg - 1);
2288 	return FAIL;
2289     }
2290     if (!valid_yank_reg(**arg, TRUE))
2291     {
2292 	emsg_invreg(**arg);
2293 	return FAIL;
2294     }
2295     ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2296     ++*arg;
2297     return ret;
2298 }
2299 
2300 /*
2301  * Apply leading '!', '-' and '+' to constant "rettv".
2302  */
2303     static int
2304 apply_leader(typval_T *rettv, char_u *start, char_u *end)
2305 {
2306     char_u *p = end;
2307 
2308     // this works from end to start
2309     while (p > start)
2310     {
2311 	--p;
2312 	if (*p == '-' || *p == '+')
2313 	{
2314 	    // only '-' has an effect, for '+' we only check the type
2315 #ifdef FEAT_FLOAT
2316 	    if (rettv->v_type == VAR_FLOAT)
2317 	    {
2318 		if (*p == '-')
2319 		    rettv->vval.v_float = -rettv->vval.v_float;
2320 	    }
2321 	    else
2322 #endif
2323 	    {
2324 		varnumber_T	val;
2325 		int		error = FALSE;
2326 
2327 		// tv_get_number_chk() accepts a string, but we don't want that
2328 		// here
2329 		if (check_not_string(rettv) == FAIL)
2330 		    return FAIL;
2331 		val = tv_get_number_chk(rettv, &error);
2332 		clear_tv(rettv);
2333 		if (error)
2334 		    return FAIL;
2335 		if (*p == '-')
2336 		    val = -val;
2337 		rettv->v_type = VAR_NUMBER;
2338 		rettv->vval.v_number = val;
2339 	    }
2340 	}
2341 	else
2342 	{
2343 	    int v = tv2bool(rettv);
2344 
2345 	    // '!' is permissive in the type.
2346 	    clear_tv(rettv);
2347 	    rettv->v_type = VAR_BOOL;
2348 	    rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2349 	}
2350     }
2351     return OK;
2352 }
2353 
2354 /*
2355  * Recognize v: variables that are constants and set "rettv".
2356  */
2357     static void
2358 get_vim_constant(char_u **arg, typval_T *rettv)
2359 {
2360     if (STRNCMP(*arg, "v:true", 6) == 0)
2361     {
2362 	rettv->v_type = VAR_BOOL;
2363 	rettv->vval.v_number = VVAL_TRUE;
2364 	*arg += 6;
2365     }
2366     else if (STRNCMP(*arg, "v:false", 7) == 0)
2367     {
2368 	rettv->v_type = VAR_BOOL;
2369 	rettv->vval.v_number = VVAL_FALSE;
2370 	*arg += 7;
2371     }
2372     else if (STRNCMP(*arg, "v:null", 6) == 0)
2373     {
2374 	rettv->v_type = VAR_SPECIAL;
2375 	rettv->vval.v_number = VVAL_NULL;
2376 	*arg += 6;
2377     }
2378     else if (STRNCMP(*arg, "v:none", 6) == 0)
2379     {
2380 	rettv->v_type = VAR_SPECIAL;
2381 	rettv->vval.v_number = VVAL_NONE;
2382 	*arg += 6;
2383     }
2384 }
2385 
2386 /*
2387  * Compile code to apply '-', '+' and '!'.
2388  */
2389     static int
2390 compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2391 {
2392     char_u	*p = end;
2393 
2394     // this works from end to start
2395     while (p > start)
2396     {
2397 	--p;
2398 	if (*p == '-' || *p == '+')
2399 	{
2400 	    int	    negate = *p == '-';
2401 	    isn_T   *isn;
2402 
2403 	    // TODO: check type
2404 	    while (p > start && (p[-1] == '-' || p[-1] == '+'))
2405 	    {
2406 		--p;
2407 		if (*p == '-')
2408 		    negate = !negate;
2409 	    }
2410 	    // only '-' has an effect, for '+' we only check the type
2411 	    if (negate)
2412 		isn = generate_instr(cctx, ISN_NEGATENR);
2413 	    else
2414 		isn = generate_instr(cctx, ISN_CHECKNR);
2415 	    if (isn == NULL)
2416 		return FAIL;
2417 	}
2418 	else
2419 	{
2420 	    int  invert = TRUE;
2421 
2422 	    while (p > start && p[-1] == '!')
2423 	    {
2424 		--p;
2425 		invert = !invert;
2426 	    }
2427 	    if (generate_2BOOL(cctx, invert) == FAIL)
2428 		return FAIL;
2429 	}
2430     }
2431     return OK;
2432 }
2433 
2434 /*
2435  * Compile whatever comes after "name" or "name()".
2436  */
2437     static int
2438 compile_subscript(
2439 	char_u **arg,
2440 	cctx_T *cctx,
2441 	char_u **start_leader,
2442 	char_u *end_leader)
2443 {
2444     for (;;)
2445     {
2446 	if (**arg == '(')
2447 	{
2448 	    int	    argcount = 0;
2449 
2450 	    // funcref(arg)
2451 	    *arg = skipwhite(*arg + 1);
2452 	    if (compile_arguments(arg, cctx, &argcount) == FAIL)
2453 		return FAIL;
2454 	    if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2455 		return FAIL;
2456 	}
2457 	else if (**arg == '-' && (*arg)[1] == '>')
2458 	{
2459 	    char_u *p;
2460 
2461 	    // something->method()
2462 	    // Apply the '!', '-' and '+' first:
2463 	    //   -1.0->func() works like (-1.0)->func()
2464 	    if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2465 		return FAIL;
2466 	    *start_leader = end_leader;   // don't apply again later
2467 
2468 	    *arg = skipwhite(*arg + 2);
2469 	    if (**arg == '{')
2470 	    {
2471 		// lambda call:  list->{lambda}
2472 		if (compile_lambda_call(arg, cctx) == FAIL)
2473 		    return FAIL;
2474 	    }
2475 	    else
2476 	    {
2477 		// method call:  list->method()
2478 		for (p = *arg; eval_isnamec1(*p); ++p)
2479 		    ;
2480 		if (*p != '(')
2481 		{
2482 		    semsg(_(e_missing_paren), arg);
2483 		    return FAIL;
2484 		}
2485 		// TODO: base value may not be the first argument
2486 		if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2487 		    return FAIL;
2488 	    }
2489 	}
2490 	else if (**arg == '[')
2491 	{
2492 	    garray_T	*stack;
2493 	    type_T	**typep;
2494 
2495 	    // list index: list[123]
2496 	    // TODO: more arguments
2497 	    // TODO: dict member  dict['name']
2498 	    *arg = skipwhite(*arg + 1);
2499 	    if (compile_expr1(arg, cctx) == FAIL)
2500 		return FAIL;
2501 
2502 	    if (**arg != ']')
2503 	    {
2504 		emsg(_(e_missbrac));
2505 		return FAIL;
2506 	    }
2507 	    *arg = *arg + 1;
2508 
2509 	    if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2510 		return FAIL;
2511 	    stack = &cctx->ctx_type_stack;
2512 	    typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2513 	    if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2514 	    {
2515 		emsg(_(e_listreq));
2516 		return FAIL;
2517 	    }
2518 	    if ((*typep)->tt_type == VAR_LIST)
2519 		*typep = (*typep)->tt_member;
2520 	}
2521 	else if (**arg == '.' && (*arg)[1] != '.')
2522 	{
2523 	    char_u *p;
2524 
2525 	    ++*arg;
2526 	    p = *arg;
2527 	    // dictionary member: dict.name
2528 	    if (eval_isnamec1(*p))
2529 		while (eval_isnamec(*p))
2530 		    MB_PTR_ADV(p);
2531 	    if (p == *arg)
2532 	    {
2533 		semsg(_(e_syntax_at), *arg);
2534 		return FAIL;
2535 	    }
2536 	    if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2537 		return FAIL;
2538 	    *arg = p;
2539 	}
2540 	else
2541 	    break;
2542     }
2543 
2544     // TODO - see handle_subscript():
2545     // Turn "dict.Func" into a partial for "Func" bound to "dict".
2546     // Don't do this when "Func" is already a partial that was bound
2547     // explicitly (pt_auto is FALSE).
2548 
2549     return OK;
2550 }
2551 
2552 /*
2553  * Compile an expression at "*p" and add instructions to "instr".
2554  * "p" is advanced until after the expression, skipping white space.
2555  *
2556  * This is the equivalent of eval1(), eval2(), etc.
2557  */
2558 
2559 /*
2560  *  number		number constant
2561  *  0zFFFFFFFF		Blob constant
2562  *  "string"		string constant
2563  *  'string'		literal string constant
2564  *  &option-name	option value
2565  *  @r			register contents
2566  *  identifier		variable value
2567  *  function()		function call
2568  *  $VAR		environment variable
2569  *  (expression)	nested expression
2570  *  [expr, expr]	List
2571  *  {key: val, key: val}   Dictionary
2572  *  #{key: val, key: val}  Dictionary with literal keys
2573  *
2574  *  Also handle:
2575  *  ! in front		logical NOT
2576  *  - in front		unary minus
2577  *  + in front		unary plus (ignored)
2578  *  trailing (arg)	funcref/partial call
2579  *  trailing []		subscript in String or List
2580  *  trailing .name	entry in Dictionary
2581  *  trailing ->name()	method call
2582  */
2583     static int
2584 compile_expr7(char_u **arg, cctx_T *cctx)
2585 {
2586     typval_T	rettv;
2587     char_u	*start_leader, *end_leader;
2588     int		ret = OK;
2589 
2590     /*
2591      * Skip '!', '-' and '+' characters.  They are handled later.
2592      */
2593     start_leader = *arg;
2594     while (**arg == '!' || **arg == '-' || **arg == '+')
2595 	*arg = skipwhite(*arg + 1);
2596     end_leader = *arg;
2597 
2598     rettv.v_type = VAR_UNKNOWN;
2599     switch (**arg)
2600     {
2601 	/*
2602 	 * Number constant.
2603 	 */
2604 	case '0':	// also for blob starting with 0z
2605 	case '1':
2606 	case '2':
2607 	case '3':
2608 	case '4':
2609 	case '5':
2610 	case '6':
2611 	case '7':
2612 	case '8':
2613 	case '9':
2614 	case '.':   if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2615 			return FAIL;
2616 		    break;
2617 
2618 	/*
2619 	 * String constant: "string".
2620 	 */
2621 	case '"':   if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2622 			return FAIL;
2623 		    break;
2624 
2625 	/*
2626 	 * Literal string constant: 'str''ing'.
2627 	 */
2628 	case '\'':  if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2629 			return FAIL;
2630 		    break;
2631 
2632 	/*
2633 	 * Constant Vim variable.
2634 	 */
2635 	case 'v':   get_vim_constant(arg, &rettv);
2636 		    ret = NOTDONE;
2637 		    break;
2638 
2639 	/*
2640 	 * List: [expr, expr]
2641 	 */
2642 	case '[':   ret = compile_list(arg, cctx);
2643 		    break;
2644 
2645 	/*
2646 	 * Dictionary: #{key: val, key: val}
2647 	 */
2648 	case '#':   if ((*arg)[1] == '{')
2649 		    {
2650 			++*arg;
2651 			ret = compile_dict(arg, cctx, TRUE);
2652 		    }
2653 		    else
2654 			ret = NOTDONE;
2655 		    break;
2656 
2657 	/*
2658 	 * Lambda: {arg, arg -> expr}
2659 	 * Dictionary: {'key': val, 'key': val}
2660 	 */
2661 	case '{':   {
2662 			char_u *start = skipwhite(*arg + 1);
2663 
2664 			// Find out what comes after the arguments.
2665 			ret = get_function_args(&start, '-', NULL,
2666 						       NULL, NULL, NULL, TRUE);
2667 			if (ret != FAIL && *start == '>')
2668 			    ret = compile_lambda(arg, cctx);
2669 			else
2670 			    ret = compile_dict(arg, cctx, FALSE);
2671 		    }
2672 		    break;
2673 
2674 	/*
2675 	 * Option value: &name
2676 	 */
2677 	case '&':	ret = compile_get_option(arg, cctx);
2678 			break;
2679 
2680 	/*
2681 	 * Environment variable: $VAR.
2682 	 */
2683 	case '$':	ret = compile_get_env(arg, cctx);
2684 			break;
2685 
2686 	/*
2687 	 * Register contents: @r.
2688 	 */
2689 	case '@':	ret = compile_get_register(arg, cctx);
2690 			break;
2691 	/*
2692 	 * nested expression: (expression).
2693 	 */
2694 	case '(':   *arg = skipwhite(*arg + 1);
2695 		    ret = compile_expr1(arg, cctx);	// recursive!
2696 		    *arg = skipwhite(*arg);
2697 		    if (**arg == ')')
2698 			++*arg;
2699 		    else if (ret == OK)
2700 		    {
2701 			emsg(_(e_missing_close));
2702 			ret = FAIL;
2703 		    }
2704 		    break;
2705 
2706 	default:    ret = NOTDONE;
2707 		    break;
2708     }
2709     if (ret == FAIL)
2710 	return FAIL;
2711 
2712     if (rettv.v_type != VAR_UNKNOWN)
2713     {
2714 	// apply the '!', '-' and '+' before the constant
2715 	if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2716 	{
2717 	    clear_tv(&rettv);
2718 	    return FAIL;
2719 	}
2720 	start_leader = end_leader;   // don't apply again below
2721 
2722 	// push constant
2723 	switch (rettv.v_type)
2724 	{
2725 	    case VAR_BOOL:
2726 		generate_PUSHBOOL(cctx, rettv.vval.v_number);
2727 		break;
2728 	    case VAR_SPECIAL:
2729 		generate_PUSHSPEC(cctx, rettv.vval.v_number);
2730 		break;
2731 	    case VAR_NUMBER:
2732 		generate_PUSHNR(cctx, rettv.vval.v_number);
2733 		break;
2734 #ifdef FEAT_FLOAT
2735 	    case VAR_FLOAT:
2736 		generate_PUSHF(cctx, rettv.vval.v_float);
2737 		break;
2738 #endif
2739 	    case VAR_BLOB:
2740 		generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2741 		rettv.vval.v_blob = NULL;
2742 		break;
2743 	    case VAR_STRING:
2744 		generate_PUSHS(cctx, rettv.vval.v_string);
2745 		rettv.vval.v_string = NULL;
2746 		break;
2747 	    default:
2748 		iemsg("constant type missing");
2749 		return FAIL;
2750 	}
2751     }
2752     else if (ret == NOTDONE)
2753     {
2754 	char_u	    *p;
2755 	int	    r;
2756 
2757 	if (!eval_isnamec1(**arg))
2758 	{
2759 	    semsg(_("E1015: Name expected: %s"), *arg);
2760 	    return FAIL;
2761 	}
2762 
2763 	// "name" or "name()"
2764 	p = to_name_end(*arg);
2765 	if (*p == '(')
2766 	    r = compile_call(arg, p - *arg, cctx, 0);
2767 	else
2768 	    r = compile_load(arg, p, cctx, TRUE);
2769 	if (r == FAIL)
2770 	    return FAIL;
2771     }
2772 
2773     if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2774 	return FAIL;
2775 
2776     // Now deal with prefixed '-', '+' and '!', if not done already.
2777     return compile_leader(cctx, start_leader, end_leader);
2778 }
2779 
2780 /*
2781  *	*	number multiplication
2782  *	/	number division
2783  *	%	number modulo
2784  */
2785     static int
2786 compile_expr6(char_u **arg, cctx_T *cctx)
2787 {
2788     char_u	*op;
2789 
2790     // get the first variable
2791     if (compile_expr7(arg, cctx) == FAIL)
2792 	return FAIL;
2793 
2794     /*
2795      * Repeat computing, until no "*", "/" or "%" is following.
2796      */
2797     for (;;)
2798     {
2799 	op = skipwhite(*arg);
2800 	if (*op != '*' && *op != '/' && *op != '%')
2801 	    break;
2802 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2803 	{
2804 	    char_u buf[3];
2805 
2806 	    vim_strncpy(buf, op, 1);
2807 	    semsg(_(e_white_both), buf);
2808 	}
2809 	*arg = skipwhite(op + 1);
2810 
2811 	// get the second variable
2812 	if (compile_expr7(arg, cctx) == FAIL)
2813 	    return FAIL;
2814 
2815 	generate_two_op(cctx, op);
2816     }
2817 
2818     return OK;
2819 }
2820 
2821 /*
2822  *      +	number addition
2823  *      -	number subtraction
2824  *      ..	string concatenation
2825  */
2826     static int
2827 compile_expr5(char_u **arg, cctx_T *cctx)
2828 {
2829     char_u	*op;
2830     int		oplen;
2831 
2832     // get the first variable
2833     if (compile_expr6(arg, cctx) == FAIL)
2834 	return FAIL;
2835 
2836     /*
2837      * Repeat computing, until no "+", "-" or ".." is following.
2838      */
2839     for (;;)
2840     {
2841 	op = skipwhite(*arg);
2842 	if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2843 	    break;
2844 	oplen = (*op == '.' ? 2 : 1);
2845 
2846 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2847 	{
2848 	    char_u buf[3];
2849 
2850 	    vim_strncpy(buf, op, oplen);
2851 	    semsg(_(e_white_both), buf);
2852 	}
2853 
2854 	*arg = skipwhite(op + oplen);
2855 
2856 	// get the second variable
2857 	if (compile_expr6(arg, cctx) == FAIL)
2858 	    return FAIL;
2859 
2860 	if (*op == '.')
2861 	{
2862 	    if (may_generate_2STRING(-2, cctx) == FAIL
2863 		    || may_generate_2STRING(-1, cctx) == FAIL)
2864 		return FAIL;
2865 	    generate_instr_drop(cctx, ISN_CONCAT, 1);
2866 	}
2867 	else
2868 	    generate_two_op(cctx, op);
2869     }
2870 
2871     return OK;
2872 }
2873 
2874 /*
2875  * expr5a == expr5b
2876  * expr5a =~ expr5b
2877  * expr5a != expr5b
2878  * expr5a !~ expr5b
2879  * expr5a > expr5b
2880  * expr5a >= expr5b
2881  * expr5a < expr5b
2882  * expr5a <= expr5b
2883  * expr5a is expr5b
2884  * expr5a isnot expr5b
2885  *
2886  * Produces instructions:
2887  *	EVAL expr5a		Push result of "expr5a"
2888  *	EVAL expr5b		Push result of "expr5b"
2889  *	COMPARE			one of the compare instructions
2890  */
2891     static int
2892 compile_expr4(char_u **arg, cctx_T *cctx)
2893 {
2894     exptype_T	type = EXPR_UNKNOWN;
2895     char_u	*p;
2896     int		len = 2;
2897     int		i;
2898     int		type_is = FALSE;
2899 
2900     // get the first variable
2901     if (compile_expr5(arg, cctx) == FAIL)
2902 	return FAIL;
2903 
2904     p = skipwhite(*arg);
2905     switch (p[0])
2906     {
2907 	case '=':   if (p[1] == '=')
2908 			type = EXPR_EQUAL;
2909 		    else if (p[1] == '~')
2910 			type = EXPR_MATCH;
2911 		    break;
2912 	case '!':   if (p[1] == '=')
2913 			type = EXPR_NEQUAL;
2914 		    else if (p[1] == '~')
2915 			type = EXPR_NOMATCH;
2916 		    break;
2917 	case '>':   if (p[1] != '=')
2918 		    {
2919 			type = EXPR_GREATER;
2920 			len = 1;
2921 		    }
2922 		    else
2923 			type = EXPR_GEQUAL;
2924 		    break;
2925 	case '<':   if (p[1] != '=')
2926 		    {
2927 			type = EXPR_SMALLER;
2928 			len = 1;
2929 		    }
2930 		    else
2931 			type = EXPR_SEQUAL;
2932 		    break;
2933 	case 'i':   if (p[1] == 's')
2934 		    {
2935 			// "is" and "isnot"; but not a prefix of a name
2936 			if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2937 			    len = 5;
2938 			i = p[len];
2939 			if (!isalnum(i) && i != '_')
2940 			{
2941 			    type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2942 			    type_is = TRUE;
2943 			}
2944 		    }
2945 		    break;
2946     }
2947 
2948     /*
2949      * If there is a comparative operator, use it.
2950      */
2951     if (type != EXPR_UNKNOWN)
2952     {
2953 	int ic = FALSE;  // Default: do not ignore case
2954 
2955 	if (type_is && (p[len] == '?' || p[len] == '#'))
2956 	{
2957 	    semsg(_(e_invexpr2), *arg);
2958 	    return FAIL;
2959 	}
2960 	// extra question mark appended: ignore case
2961 	if (p[len] == '?')
2962 	{
2963 	    ic = TRUE;
2964 	    ++len;
2965 	}
2966 	// extra '#' appended: match case (ignored)
2967 	else if (p[len] == '#')
2968 	    ++len;
2969 	// nothing appended: match case
2970 
2971 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2972 	{
2973 	    char_u buf[7];
2974 
2975 	    vim_strncpy(buf, p, len);
2976 	    semsg(_(e_white_both), buf);
2977 	}
2978 
2979 	// get the second variable
2980 	*arg = skipwhite(p + len);
2981 	if (compile_expr5(arg, cctx) == FAIL)
2982 	    return FAIL;
2983 
2984 	generate_COMPARE(cctx, type, ic);
2985     }
2986 
2987     return OK;
2988 }
2989 
2990 /*
2991  * Compile || or &&.
2992  */
2993     static int
2994 compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2995 {
2996     char_u	*p = skipwhite(*arg);
2997     int		opchar = *op;
2998 
2999     if (p[0] == opchar && p[1] == opchar)
3000     {
3001 	garray_T	*instr = &cctx->ctx_instr;
3002 	garray_T	end_ga;
3003 
3004 	/*
3005 	 * Repeat until there is no following "||" or "&&"
3006 	 */
3007 	ga_init2(&end_ga, sizeof(int), 10);
3008 	while (p[0] == opchar && p[1] == opchar)
3009 	{
3010 	    if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3011 		semsg(_(e_white_both), op);
3012 
3013 	    if (ga_grow(&end_ga, 1) == FAIL)
3014 	    {
3015 		ga_clear(&end_ga);
3016 		return FAIL;
3017 	    }
3018 	    *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3019 	    ++end_ga.ga_len;
3020 	    generate_JUMP(cctx, opchar == '|'
3021 			 ?  JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3022 
3023 	    // eval the next expression
3024 	    *arg = skipwhite(p + 2);
3025 	    if ((opchar == '|' ? compile_expr3(arg, cctx)
3026 					   : compile_expr4(arg, cctx)) == FAIL)
3027 	    {
3028 		ga_clear(&end_ga);
3029 		return FAIL;
3030 	    }
3031 	    p = skipwhite(*arg);
3032 	}
3033 
3034 	// Fill in the end label in all jumps.
3035 	while (end_ga.ga_len > 0)
3036 	{
3037 	    isn_T	*isn;
3038 
3039 	    --end_ga.ga_len;
3040 	    isn = ((isn_T *)instr->ga_data)
3041 				  + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3042 	    isn->isn_arg.jump.jump_where = instr->ga_len;
3043 	}
3044 	ga_clear(&end_ga);
3045     }
3046 
3047     return OK;
3048 }
3049 
3050 /*
3051  * expr4a && expr4a && expr4a	    logical AND
3052  *
3053  * Produces instructions:
3054  *	EVAL expr4a		Push result of "expr4a"
3055  *	JUMP_AND_KEEP_IF_FALSE end
3056  *	EVAL expr4b		Push result of "expr4b"
3057  *	JUMP_AND_KEEP_IF_FALSE end
3058  *	EVAL expr4c		Push result of "expr4c"
3059  * end:
3060  */
3061     static int
3062 compile_expr3(char_u **arg, cctx_T *cctx)
3063 {
3064     // get the first variable
3065     if (compile_expr4(arg, cctx) == FAIL)
3066 	return FAIL;
3067 
3068     // || and && work almost the same
3069     return compile_and_or(arg, cctx, "&&");
3070 }
3071 
3072 /*
3073  * expr3a || expr3b || expr3c	    logical OR
3074  *
3075  * Produces instructions:
3076  *	EVAL expr3a		Push result of "expr3a"
3077  *	JUMP_AND_KEEP_IF_TRUE end
3078  *	EVAL expr3b		Push result of "expr3b"
3079  *	JUMP_AND_KEEP_IF_TRUE end
3080  *	EVAL expr3c		Push result of "expr3c"
3081  * end:
3082  */
3083     static int
3084 compile_expr2(char_u **arg, cctx_T *cctx)
3085 {
3086     // eval the first expression
3087     if (compile_expr3(arg, cctx) == FAIL)
3088 	return FAIL;
3089 
3090     // || and && work almost the same
3091     return compile_and_or(arg, cctx, "||");
3092 }
3093 
3094 /*
3095  * Toplevel expression: expr2 ? expr1a : expr1b
3096  *
3097  * Produces instructions:
3098  *	EVAL expr2		Push result of "expr"
3099  *      JUMP_IF_FALSE alt	jump if false
3100  *      EVAL expr1a
3101  *      JUMP_ALWAYS end
3102  * alt:	EVAL expr1b
3103  * end:
3104  */
3105     static int
3106 compile_expr1(char_u **arg,  cctx_T *cctx)
3107 {
3108     char_u	*p;
3109 
3110     // evaluate the first expression
3111     if (compile_expr2(arg, cctx) == FAIL)
3112 	return FAIL;
3113 
3114     p = skipwhite(*arg);
3115     if (*p == '?')
3116     {
3117 	garray_T	*instr = &cctx->ctx_instr;
3118 	garray_T	*stack = &cctx->ctx_type_stack;
3119 	int		alt_idx = instr->ga_len;
3120 	int		end_idx;
3121 	isn_T		*isn;
3122 	type_T		*type1;
3123 	type_T		*type2;
3124 
3125 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3126 	    semsg(_(e_white_both), "?");
3127 
3128 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3129 
3130 	// evaluate the second expression; any type is accepted
3131 	*arg = skipwhite(p + 1);
3132 	if (compile_expr1(arg, cctx) == FAIL)
3133 	    return FAIL;
3134 
3135 	// remember the type and drop it
3136 	--stack->ga_len;
3137 	type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3138 
3139 	end_idx = instr->ga_len;
3140 	generate_JUMP(cctx, JUMP_ALWAYS, 0);
3141 
3142 	// jump here from JUMP_IF_FALSE
3143 	isn = ((isn_T *)instr->ga_data) + alt_idx;
3144 	isn->isn_arg.jump.jump_where = instr->ga_len;
3145 
3146 	// Check for the ":".
3147 	p = skipwhite(*arg);
3148 	if (*p != ':')
3149 	{
3150 	    emsg(_(e_missing_colon));
3151 	    return FAIL;
3152 	}
3153 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3154 	    semsg(_(e_white_both), ":");
3155 
3156 	// evaluate the third expression
3157 	*arg = skipwhite(p + 1);
3158 	if (compile_expr1(arg, cctx) == FAIL)
3159 	    return FAIL;
3160 
3161 	// If the types differ, the result has a more generic type.
3162 	type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3163 	common_type(type1, type2, type2);
3164 
3165 	// jump here from JUMP_ALWAYS
3166 	isn = ((isn_T *)instr->ga_data) + end_idx;
3167 	isn->isn_arg.jump.jump_where = instr->ga_len;
3168     }
3169     return OK;
3170 }
3171 
3172 /*
3173  * compile "return [expr]"
3174  */
3175     static char_u *
3176 compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3177 {
3178     char_u	*p = arg;
3179     garray_T	*stack = &cctx->ctx_type_stack;
3180     type_T	*stack_type;
3181 
3182     if (*p != NUL && *p != '|' && *p != '\n')
3183     {
3184 	// compile return argument into instructions
3185 	if (compile_expr1(&p, cctx) == FAIL)
3186 	    return NULL;
3187 
3188 	stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3189 	if (set_return_type)
3190 	    cctx->ctx_ufunc->uf_ret_type = stack_type;
3191 	else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3192 								       == FAIL)
3193 	    return NULL;
3194     }
3195     else
3196     {
3197 	if (set_return_type)
3198 	    cctx->ctx_ufunc->uf_ret_type = &t_void;
3199 	else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3200 	{
3201 	    emsg(_("E1003: Missing return value"));
3202 	    return NULL;
3203 	}
3204 
3205 	// No argument, return zero.
3206 	generate_PUSHNR(cctx, 0);
3207     }
3208 
3209     if (generate_instr(cctx, ISN_RETURN) == NULL)
3210 	return NULL;
3211 
3212     // "return val | endif" is possible
3213     return skipwhite(p);
3214 }
3215 
3216 /*
3217  * Return the length of an assignment operator, or zero if there isn't one.
3218  */
3219     int
3220 assignment_len(char_u *p, int *heredoc)
3221 {
3222     if (*p == '=')
3223     {
3224 	if (p[1] == '<' && p[2] == '<')
3225 	{
3226 	    *heredoc = TRUE;
3227 	    return 3;
3228 	}
3229 	return 1;
3230     }
3231     if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3232 	return 2;
3233     if (STRNCMP(p, "..=", 3) == 0)
3234 	return 3;
3235     return 0;
3236 }
3237 
3238 // words that cannot be used as a variable
3239 static char *reserved[] = {
3240     "true",
3241     "false",
3242     NULL
3243 };
3244 
3245 /*
3246  * Get a line for "=<<".
3247  * Return a pointer to the line in allocated memory.
3248  * Return NULL for end-of-file or some error.
3249  */
3250     static char_u *
3251 heredoc_getline(
3252 	int c UNUSED,
3253 	void *cookie,
3254 	int indent UNUSED,
3255 	int do_concat UNUSED)
3256 {
3257     cctx_T  *cctx = (cctx_T *)cookie;
3258 
3259     if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3260 	NULL;
3261     ++cctx->ctx_lnum;
3262     return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3263 							     [cctx->ctx_lnum]);
3264 }
3265 
3266 typedef enum {
3267     dest_local,
3268     dest_option,
3269     dest_env,
3270     dest_global,
3271     dest_vimvar,
3272     dest_script,
3273     dest_reg,
3274 } assign_dest_T;
3275 
3276 /*
3277  * compile "let var [= expr]", "const var = expr" and "var = expr"
3278  * "arg" points to "var".
3279  */
3280     static char_u *
3281 compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3282 {
3283     char_u	*p;
3284     char_u	*ret = NULL;
3285     int		var_count = 0;
3286     int		semicolon = 0;
3287     size_t	varlen;
3288     garray_T	*instr = &cctx->ctx_instr;
3289     int		idx = -1;
3290     char_u	*op;
3291     int		opt_type;
3292     assign_dest_T dest = dest_local;
3293     int		opt_flags = 0;
3294     int		vimvaridx = -1;
3295     int		oplen = 0;
3296     int		heredoc = FALSE;
3297     type_T	*type;
3298     lvar_T	*lvar;
3299     char_u	*name;
3300     char_u	*sp;
3301     int		has_type = FALSE;
3302     int		is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3303     int		instr_count = -1;
3304 
3305     p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3306     if (p == NULL)
3307 	return NULL;
3308     if (var_count > 0)
3309     {
3310 	// TODO: let [var, var] = list
3311 	emsg("Cannot handle a list yet");
3312 	return NULL;
3313     }
3314 
3315     varlen = p - arg;
3316     name = vim_strnsave(arg, (int)varlen);
3317     if (name == NULL)
3318 	return NULL;
3319 
3320     if (*arg == '&')
3321     {
3322 	int	    cc;
3323 	long	    numval;
3324 	char_u	    *stringval = NULL;
3325 
3326 	dest = dest_option;
3327 	if (cmdidx == CMD_const)
3328 	{
3329 	    emsg(_(e_const_option));
3330 	    return NULL;
3331 	}
3332 	if (is_decl)
3333 	{
3334 	    semsg(_("E1052: Cannot declare an option: %s"), arg);
3335 	    goto theend;
3336 	}
3337 	p = arg;
3338 	p = find_option_end(&p, &opt_flags);
3339 	if (p == NULL)
3340 	{
3341 	    emsg(_(e_letunexp));
3342 	    return NULL;
3343 	}
3344 	cc = *p;
3345 	*p = NUL;
3346 	opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3347 	*p = cc;
3348 	if (opt_type == -3)
3349 	{
3350 	    semsg(_(e_unknown_option), *arg);
3351 	    return NULL;
3352 	}
3353 	if (opt_type == -2 || opt_type == 0)
3354 	    type = &t_string;
3355 	else
3356 	    type = &t_number;	// both number and boolean option
3357     }
3358     else if (*arg == '$')
3359     {
3360 	dest = dest_env;
3361 	if (is_decl)
3362 	{
3363 	    semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3364 	    goto theend;
3365 	}
3366     }
3367     else if (*arg == '@')
3368     {
3369 	if (!valid_yank_reg(arg[1], TRUE))
3370 	{
3371 	    emsg_invreg(arg[1]);
3372 	    return FAIL;
3373 	}
3374 	dest = dest_reg;
3375 	if (is_decl)
3376 	{
3377 	    semsg(_("E1066: Cannot declare a register: %s"), name);
3378 	    goto theend;
3379 	}
3380     }
3381     else if (STRNCMP(arg, "g:", 2) == 0)
3382     {
3383 	dest = dest_global;
3384 	if (is_decl)
3385 	{
3386 	    semsg(_("E1016: Cannot declare a global variable: %s"), name);
3387 	    goto theend;
3388 	}
3389     }
3390     else if (STRNCMP(arg, "v:", 2) == 0)
3391     {
3392 	vimvaridx = find_vim_var(name + 2);
3393 	if (vimvaridx < 0)
3394 	{
3395 	    semsg(_(e_var_notfound), arg);
3396 	    goto theend;
3397 	}
3398 	dest = dest_vimvar;
3399 	if (is_decl)
3400 	{
3401 	    semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3402 	    goto theend;
3403 	}
3404     }
3405     else
3406     {
3407 	for (idx = 0; reserved[idx] != NULL; ++idx)
3408 	    if (STRCMP(reserved[idx], name) == 0)
3409 	    {
3410 		semsg(_("E1034: Cannot use reserved name %s"), name);
3411 		goto theend;
3412 	    }
3413 
3414 	idx = lookup_local(arg, varlen, cctx);
3415 	if (idx >= 0)
3416 	{
3417 	    if (is_decl)
3418 	    {
3419 		semsg(_("E1017: Variable already declared: %s"), name);
3420 		goto theend;
3421 	    }
3422 	    else
3423 	    {
3424 		lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3425 		if (lvar->lv_const)
3426 		{
3427 		    semsg(_("E1018: Cannot assign to a constant: %s"), name);
3428 		    goto theend;
3429 		}
3430 	    }
3431 	}
3432 	else if (STRNCMP(arg, "s:", 2) == 0
3433 		|| lookup_script(arg, varlen) == OK
3434 		|| find_imported(arg, varlen, cctx) != NULL)
3435 	{
3436 	    dest = dest_script;
3437 	    if (is_decl)
3438 	    {
3439 		semsg(_("E1054: Variable already declared in the script: %s"),
3440 									 name);
3441 		goto theend;
3442 	    }
3443 	}
3444     }
3445 
3446     if (dest != dest_option)
3447     {
3448 	if (is_decl && *p == ':')
3449 	{
3450 	    // parse optional type: "let var: type = expr"
3451 	    p = skipwhite(p + 1);
3452 	    type = parse_type(&p, cctx->ctx_type_list);
3453 	    if (type == NULL)
3454 		goto theend;
3455 	    has_type = TRUE;
3456 	}
3457 	else if (idx < 0)
3458 	{
3459 	    // global and new local default to "any" type
3460 	    type = &t_any;
3461 	}
3462 	else
3463 	{
3464 	    lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3465 	    type = lvar->lv_type;
3466 	}
3467     }
3468 
3469     sp = p;
3470     p = skipwhite(p);
3471     op = p;
3472     oplen = assignment_len(p, &heredoc);
3473     if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3474     {
3475 	char_u  buf[4];
3476 
3477 	vim_strncpy(buf, op, oplen);
3478 	semsg(_(e_white_both), buf);
3479     }
3480 
3481     if (oplen == 3 && !heredoc && dest != dest_global
3482 	    && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
3483     {
3484 	emsg(_("E1019: Can only concatenate to string"));
3485 	goto theend;
3486     }
3487 
3488     // +=, /=, etc. require an existing variable
3489     if (idx < 0 && dest == dest_local)
3490     {
3491 	if (oplen > 1 && !heredoc)
3492 	{
3493 	    semsg(_("E1020: cannot use an operator on a new variable: %s"),
3494 									 name);
3495 	    goto theend;
3496 	}
3497 
3498 	// new local variable
3499 	idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3500 	if (idx < 0)
3501 	    goto theend;
3502     }
3503 
3504     if (heredoc)
3505     {
3506 	list_T	   *l;
3507 	listitem_T *li;
3508 
3509 	// [let] varname =<< [trim] {end}
3510 	eap->getline = heredoc_getline;
3511 	eap->cookie = cctx;
3512 	l = heredoc_get(eap, op + 3);
3513 
3514 	// Push each line and the create the list.
3515 	for (li = l->lv_first; li != NULL; li = li->li_next)
3516 	{
3517 	    generate_PUSHS(cctx, li->li_tv.vval.v_string);
3518 	    li->li_tv.vval.v_string = NULL;
3519 	}
3520 	generate_NEWLIST(cctx, l->lv_len);
3521 	type = &t_list_string;
3522 	list_free(l);
3523 	p += STRLEN(p);
3524     }
3525     else if (oplen > 0)
3526     {
3527 	// for "+=", "*=", "..=" etc. first load the current value
3528 	if (*op != '=')
3529 	{
3530 	    switch (dest)
3531 	    {
3532 		case dest_option:
3533 		    // TODO: check the option exists
3534 		    generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3535 		    break;
3536 		case dest_global:
3537 		    generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3538 		    break;
3539 		case dest_script:
3540 		    compile_load_scriptvar(cctx,
3541 			    name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
3542 		    break;
3543 		case dest_env:
3544 		    // Include $ in the name here
3545 		    generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3546 		    break;
3547 		case dest_reg:
3548 		    generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3549 		    break;
3550 		case dest_vimvar:
3551 		    generate_LOADV(cctx, name + 2, TRUE);
3552 		    break;
3553 		case dest_local:
3554 		    generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3555 		    break;
3556 	    }
3557 	}
3558 
3559 	// compile the expression
3560 	instr_count = instr->ga_len;
3561 	p = skipwhite(p + oplen);
3562 	if (compile_expr1(&p, cctx) == FAIL)
3563 	    goto theend;
3564 
3565 	if (idx >= 0 && (is_decl || !has_type))
3566 	{
3567 	    garray_T	*stack = &cctx->ctx_type_stack;
3568 	    type_T	*stacktype =
3569 				((type_T **)stack->ga_data)[stack->ga_len - 1];
3570 
3571 	    lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3572 	    if (!has_type)
3573 	    {
3574 		if (stacktype->tt_type == VAR_VOID)
3575 		{
3576 		    emsg(_("E1031: Cannot use void value"));
3577 		    goto theend;
3578 		}
3579 		else
3580 		    lvar->lv_type = stacktype;
3581 	    }
3582 	    else
3583 		if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3584 		    goto theend;
3585 	}
3586     }
3587     else if (cmdidx == CMD_const)
3588     {
3589 	emsg(_("E1021: const requires a value"));
3590 	goto theend;
3591     }
3592     else if (!has_type || dest == dest_option)
3593     {
3594 	emsg(_("E1022: type or initialization required"));
3595 	goto theend;
3596     }
3597     else
3598     {
3599 	// variables are always initialized
3600 	if (ga_grow(instr, 1) == FAIL)
3601 	    goto theend;
3602 	switch (type->tt_type)
3603 	{
3604 	    case VAR_BOOL:
3605 		generate_PUSHBOOL(cctx, VVAL_FALSE);
3606 		break;
3607 	    case VAR_SPECIAL:
3608 		generate_PUSHSPEC(cctx, VVAL_NONE);
3609 		break;
3610 	    case VAR_FLOAT:
3611 #ifdef FEAT_FLOAT
3612 		generate_PUSHF(cctx, 0.0);
3613 #endif
3614 		break;
3615 	    case VAR_STRING:
3616 		generate_PUSHS(cctx, NULL);
3617 		break;
3618 	    case VAR_BLOB:
3619 		generate_PUSHBLOB(cctx, NULL);
3620 		break;
3621 	    case VAR_FUNC:
3622 		generate_PUSHFUNC(cctx, NULL);
3623 		break;
3624 	    case VAR_PARTIAL:
3625 		generate_PUSHPARTIAL(cctx, NULL);
3626 		break;
3627 	    case VAR_LIST:
3628 		generate_NEWLIST(cctx, 0);
3629 		break;
3630 	    case VAR_DICT:
3631 		generate_NEWDICT(cctx, 0);
3632 		break;
3633 	    case VAR_JOB:
3634 		generate_PUSHJOB(cctx, NULL);
3635 		break;
3636 	    case VAR_CHANNEL:
3637 		generate_PUSHCHANNEL(cctx, NULL);
3638 		break;
3639 	    case VAR_NUMBER:
3640 	    case VAR_UNKNOWN:
3641 	    case VAR_VOID:
3642 		generate_PUSHNR(cctx, 0);
3643 		break;
3644 	}
3645     }
3646 
3647     if (oplen > 0 && *op != '=')
3648     {
3649 	type_T	    *expected = &t_number;
3650 	garray_T    *stack = &cctx->ctx_type_stack;
3651 	type_T	    *stacktype;
3652 
3653 	// TODO: if type is known use float or any operation
3654 
3655 	if (*op == '.')
3656 	    expected = &t_string;
3657 	stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3658 	if (need_type(stacktype, expected, -1, cctx) == FAIL)
3659 	    goto theend;
3660 
3661 	if (*op == '.')
3662 	    generate_instr_drop(cctx, ISN_CONCAT, 1);
3663 	else
3664 	{
3665 	    isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3666 
3667 	    if (isn == NULL)
3668 		goto theend;
3669 	    switch (*op)
3670 	    {
3671 		case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3672 		case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3673 		case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3674 		case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3675 		case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3676 	    }
3677 	}
3678     }
3679 
3680     switch (dest)
3681     {
3682 	case dest_option:
3683 	    generate_STOREOPT(cctx, name + 1, opt_flags);
3684 	    break;
3685 	case dest_global:
3686 	    // include g: with the name, easier to execute that way
3687 	    generate_STORE(cctx, ISN_STOREG, 0, name);
3688 	    break;
3689 	case dest_env:
3690 	    generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3691 	    break;
3692 	case dest_reg:
3693 	    generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3694 	    break;
3695 	case dest_vimvar:
3696 	    generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3697 	    break;
3698 	case dest_script:
3699 	    {
3700 		char_u	    *rawname = name + (name[1] == ':' ? 2 : 0);
3701 		imported_T  *import = NULL;
3702 		int	    sid = current_sctx.sc_sid;
3703 
3704 		if (name[1] != ':')
3705 		{
3706 		    import = find_imported(name, 0, cctx);
3707 		    if (import != NULL)
3708 			sid = import->imp_sid;
3709 		}
3710 
3711 		idx = get_script_item_idx(sid, rawname, TRUE);
3712 		// TODO: specific type
3713 		if (idx < 0)
3714 		    generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
3715 		else
3716 		    generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3717 							     sid, idx, &t_any);
3718 	    }
3719 	    break;
3720 	case dest_local:
3721 	    {
3722 		isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3723 
3724 		// optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3725 		// into ISN_STORENR
3726 		if (instr->ga_len == instr_count + 1
3727 						&& isn->isn_type == ISN_PUSHNR)
3728 		{
3729 		    varnumber_T val = isn->isn_arg.number;
3730 		    garray_T	*stack = &cctx->ctx_type_stack;
3731 
3732 		    isn->isn_type = ISN_STORENR;
3733 		    isn->isn_arg.storenr.str_idx = idx;
3734 		    isn->isn_arg.storenr.str_val = val;
3735 		    if (stack->ga_len > 0)
3736 			--stack->ga_len;
3737 		}
3738 		else
3739 		    generate_STORE(cctx, ISN_STORE, idx, NULL);
3740 	    }
3741 	    break;
3742     }
3743     ret = p;
3744 
3745 theend:
3746     vim_free(name);
3747     return ret;
3748 }
3749 
3750 /*
3751  * Compile an :import command.
3752  */
3753     static char_u *
3754 compile_import(char_u *arg, cctx_T *cctx)
3755 {
3756     return handle_import(arg, &cctx->ctx_imports, 0);
3757 }
3758 
3759 /*
3760  * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3761  */
3762     static int
3763 compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3764 {
3765     garray_T	*instr = &cctx->ctx_instr;
3766     endlabel_T  *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3767 
3768     if (endlabel == NULL)
3769 	return FAIL;
3770     endlabel->el_next = *el;
3771     *el = endlabel;
3772     endlabel->el_end_label = instr->ga_len;
3773 
3774     generate_JUMP(cctx, when, 0);
3775     return OK;
3776 }
3777 
3778     static void
3779 compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3780 {
3781     garray_T	*instr = &cctx->ctx_instr;
3782 
3783     while (*el != NULL)
3784     {
3785 	endlabel_T  *cur = (*el);
3786 	isn_T	    *isn;
3787 
3788 	isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3789 	isn->isn_arg.jump.jump_where = instr->ga_len;
3790 	*el = cur->el_next;
3791 	vim_free(cur);
3792     }
3793 }
3794 
3795 /*
3796  * Create a new scope and set up the generic items.
3797  */
3798     static scope_T *
3799 new_scope(cctx_T *cctx, scopetype_T type)
3800 {
3801     scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3802 
3803     if (scope == NULL)
3804 	return NULL;
3805     scope->se_outer = cctx->ctx_scope;
3806     cctx->ctx_scope = scope;
3807     scope->se_type = type;
3808     scope->se_local_count = cctx->ctx_locals.ga_len;
3809     return scope;
3810 }
3811 
3812 /*
3813  * Evaluate an expression that is a constant:
3814  *  has(arg)
3815  *
3816  * Also handle:
3817  *  ! in front		logical NOT
3818  *
3819  * Return FAIL if the expression is not a constant.
3820  */
3821     static int
3822 evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3823 {
3824     typval_T	argvars[2];
3825     char_u	*start_leader, *end_leader;
3826 
3827     /*
3828      * Skip '!' characters.  They are handled later.
3829      */
3830     start_leader = *arg;
3831     while (**arg == '!')
3832 	*arg = skipwhite(*arg + 1);
3833     end_leader = *arg;
3834 
3835     /*
3836      * Recognize only has() for now.
3837      */
3838     if (STRNCMP("has(", *arg, 4) != 0)
3839 	return FAIL;
3840     *arg = skipwhite(*arg + 4);
3841 
3842     if (**arg == '"')
3843     {
3844 	if (get_string_tv(arg, tv, TRUE) == FAIL)
3845 	    return FAIL;
3846     }
3847     else if (**arg == '\'')
3848     {
3849 	if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3850 	    return FAIL;
3851     }
3852     else
3853 	return FAIL;
3854 
3855     *arg = skipwhite(*arg);
3856     if (**arg != ')')
3857 	return FAIL;
3858     *arg = skipwhite(*arg + 1);
3859 
3860     argvars[0] = *tv;
3861     argvars[1].v_type = VAR_UNKNOWN;
3862     tv->v_type = VAR_NUMBER;
3863     tv->vval.v_number = 0;
3864     f_has(argvars, tv);
3865     clear_tv(&argvars[0]);
3866 
3867     while (start_leader < end_leader)
3868     {
3869 	if (*start_leader == '!')
3870 	    tv->vval.v_number = !tv->vval.v_number;
3871 	++start_leader;
3872     }
3873 
3874     return OK;
3875 }
3876 
3877 static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3878 
3879 /*
3880  * Compile constant || or &&.
3881  */
3882     static int
3883 evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3884 {
3885     char_u	*p = skipwhite(*arg);
3886     int		opchar = *op;
3887 
3888     if (p[0] == opchar && p[1] == opchar)
3889     {
3890 	int	val = tv2bool(tv);
3891 
3892 	/*
3893 	 * Repeat until there is no following "||" or "&&"
3894 	 */
3895 	while (p[0] == opchar && p[1] == opchar)
3896 	{
3897 	    typval_T	tv2;
3898 
3899 	    if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3900 		return FAIL;
3901 
3902 	    // eval the next expression
3903 	    *arg = skipwhite(p + 2);
3904 	    tv2.v_type = VAR_UNKNOWN;
3905 	    tv2.v_lock = 0;
3906 	    if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3907 			       : evaluate_const_expr7(arg, cctx, &tv2)) == FAIL)
3908 	    {
3909 		clear_tv(&tv2);
3910 		return FAIL;
3911 	    }
3912 	    if ((opchar == '&') == val)
3913 	    {
3914 		// false || tv2  or true && tv2: use tv2
3915 		clear_tv(tv);
3916 		*tv = tv2;
3917 		val = tv2bool(tv);
3918 	    }
3919 	    else
3920 		clear_tv(&tv2);
3921 	    p = skipwhite(*arg);
3922 	}
3923     }
3924 
3925     return OK;
3926 }
3927 
3928 /*
3929  * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3930  * Return FAIL if the expression is not a constant.
3931  */
3932     static int
3933 evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3934 {
3935     // evaluate the first expression
3936     if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3937 	return FAIL;
3938 
3939     // || and && work almost the same
3940     return evaluate_const_and_or(arg, cctx, "&&", tv);
3941 }
3942 
3943 /*
3944  * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3945  * Return FAIL if the expression is not a constant.
3946  */
3947     static int
3948 evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3949 {
3950     // evaluate the first expression
3951     if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3952 	return FAIL;
3953 
3954     // || and && work almost the same
3955     return evaluate_const_and_or(arg, cctx, "||", tv);
3956 }
3957 
3958 /*
3959  * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3960  * E.g. for "has('feature')".
3961  * This does not produce error messages.  "tv" should be cleared afterwards.
3962  * Return FAIL if the expression is not a constant.
3963  */
3964     static int
3965 evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3966 {
3967     char_u	*p;
3968 
3969     // evaluate the first expression
3970     if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3971 	return FAIL;
3972 
3973     p = skipwhite(*arg);
3974     if (*p == '?')
3975     {
3976 	int		val = tv2bool(tv);
3977 	typval_T	tv2;
3978 
3979 	if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3980 	    return FAIL;
3981 
3982 	// evaluate the second expression; any type is accepted
3983 	clear_tv(tv);
3984 	*arg = skipwhite(p + 1);
3985 	if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3986 	    return FAIL;
3987 
3988 	// Check for the ":".
3989 	p = skipwhite(*arg);
3990 	if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3991 	    return FAIL;
3992 
3993 	// evaluate the third expression
3994 	*arg = skipwhite(p + 1);
3995 	tv2.v_type = VAR_UNKNOWN;
3996 	if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3997 	{
3998 	    clear_tv(&tv2);
3999 	    return FAIL;
4000 	}
4001 	if (val)
4002 	{
4003 	    // use the expr after "?"
4004 	    clear_tv(&tv2);
4005 	}
4006 	else
4007 	{
4008 	    // use the expr after ":"
4009 	    clear_tv(tv);
4010 	    *tv = tv2;
4011 	}
4012     }
4013     return OK;
4014 }
4015 
4016 /*
4017  * compile "if expr"
4018  *
4019  * "if expr" Produces instructions:
4020  *	EVAL expr		Push result of "expr"
4021  *	JUMP_IF_FALSE end
4022  *	... body ...
4023  * end:
4024  *
4025  * "if expr | else" Produces instructions:
4026  *	EVAL expr		Push result of "expr"
4027  *	JUMP_IF_FALSE else
4028  *	... body ...
4029  *	JUMP_ALWAYS end
4030  * else:
4031  *	... body ...
4032  * end:
4033  *
4034  * "if expr1 | elseif expr2 | else" Produces instructions:
4035  *	EVAL expr		Push result of "expr"
4036  *	JUMP_IF_FALSE elseif
4037  *	... body ...
4038  *	JUMP_ALWAYS end
4039  * elseif:
4040  *	EVAL expr		Push result of "expr"
4041  *	JUMP_IF_FALSE else
4042  *	... body ...
4043  *	JUMP_ALWAYS end
4044  * else:
4045  *	... body ...
4046  * end:
4047  */
4048     static char_u *
4049 compile_if(char_u *arg, cctx_T *cctx)
4050 {
4051     char_u	*p = arg;
4052     garray_T	*instr = &cctx->ctx_instr;
4053     scope_T	*scope;
4054     typval_T	tv;
4055 
4056     // compile "expr"; if we know it evaluates to FALSE skip the block
4057     tv.v_type = VAR_UNKNOWN;
4058     if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4059 	cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4060     else
4061 	cctx->ctx_skip = MAYBE;
4062     clear_tv(&tv);
4063     if (cctx->ctx_skip == MAYBE)
4064     {
4065 	p = arg;
4066 	if (compile_expr1(&p, cctx) == FAIL)
4067 	    return NULL;
4068     }
4069 
4070     scope = new_scope(cctx, IF_SCOPE);
4071     if (scope == NULL)
4072 	return NULL;
4073 
4074     if (cctx->ctx_skip == MAYBE)
4075     {
4076 	// "where" is set when ":elseif", "else" or ":endif" is found
4077 	scope->se_u.se_if.is_if_label = instr->ga_len;
4078 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4079     }
4080     else
4081 	scope->se_u.se_if.is_if_label = -1;
4082 
4083     return p;
4084 }
4085 
4086     static char_u *
4087 compile_elseif(char_u *arg, cctx_T *cctx)
4088 {
4089     char_u	*p = arg;
4090     garray_T	*instr = &cctx->ctx_instr;
4091     isn_T	*isn;
4092     scope_T	*scope = cctx->ctx_scope;
4093     typval_T	tv;
4094 
4095     if (scope == NULL || scope->se_type != IF_SCOPE)
4096     {
4097 	emsg(_(e_elseif_without_if));
4098 	return NULL;
4099     }
4100     cctx->ctx_locals.ga_len = scope->se_local_count;
4101 
4102     if (cctx->ctx_skip == MAYBE)
4103     {
4104 	if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
4105 						    JUMP_ALWAYS, cctx) == FAIL)
4106 	    return NULL;
4107 	// previous "if" or "elseif" jumps here
4108 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4109 	isn->isn_arg.jump.jump_where = instr->ga_len;
4110     }
4111 
4112     // compile "expr"; if we know it evaluates to FALSE skip the block
4113     tv.v_type = VAR_UNKNOWN;
4114     if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4115 	cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4116     else
4117 	cctx->ctx_skip = MAYBE;
4118     clear_tv(&tv);
4119     if (cctx->ctx_skip == MAYBE)
4120     {
4121 	p = arg;
4122 	if (compile_expr1(&p, cctx) == FAIL)
4123 	    return NULL;
4124 
4125 	// "where" is set when ":elseif", "else" or ":endif" is found
4126 	scope->se_u.se_if.is_if_label = instr->ga_len;
4127 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4128     }
4129     else
4130 	scope->se_u.se_if.is_if_label = -1;
4131 
4132     return p;
4133 }
4134 
4135     static char_u *
4136 compile_else(char_u *arg, cctx_T *cctx)
4137 {
4138     char_u	*p = arg;
4139     garray_T	*instr = &cctx->ctx_instr;
4140     isn_T	*isn;
4141     scope_T	*scope = cctx->ctx_scope;
4142 
4143     if (scope == NULL || scope->se_type != IF_SCOPE)
4144     {
4145 	emsg(_(e_else_without_if));
4146 	return NULL;
4147     }
4148     cctx->ctx_locals.ga_len = scope->se_local_count;
4149 
4150     // jump from previous block to the end, unless the else block is empty
4151     if (cctx->ctx_skip == MAYBE)
4152     {
4153 	if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
4154 						    JUMP_ALWAYS, cctx) == FAIL)
4155 	    return NULL;
4156     }
4157 
4158     if (cctx->ctx_skip == MAYBE)
4159     {
4160 	if (scope->se_u.se_if.is_if_label >= 0)
4161 	{
4162 	    // previous "if" or "elseif" jumps here
4163 	    isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4164 	    isn->isn_arg.jump.jump_where = instr->ga_len;
4165 	    scope->se_u.se_if.is_if_label = -1;
4166 	}
4167     }
4168 
4169     if (cctx->ctx_skip != MAYBE)
4170 	cctx->ctx_skip = !cctx->ctx_skip;
4171 
4172     return p;
4173 }
4174 
4175     static char_u *
4176 compile_endif(char_u *arg, cctx_T *cctx)
4177 {
4178     scope_T	*scope = cctx->ctx_scope;
4179     ifscope_T	*ifscope;
4180     garray_T	*instr = &cctx->ctx_instr;
4181     isn_T	*isn;
4182 
4183     if (scope == NULL || scope->se_type != IF_SCOPE)
4184     {
4185 	emsg(_(e_endif_without_if));
4186 	return NULL;
4187     }
4188     ifscope = &scope->se_u.se_if;
4189     cctx->ctx_scope = scope->se_outer;
4190     cctx->ctx_locals.ga_len = scope->se_local_count;
4191 
4192     if (scope->se_u.se_if.is_if_label >= 0)
4193     {
4194 	// previous "if" or "elseif" jumps here
4195 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4196 	isn->isn_arg.jump.jump_where = instr->ga_len;
4197     }
4198     // Fill in the "end" label in jumps at the end of the blocks.
4199     compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
4200     cctx->ctx_skip = FALSE;
4201 
4202     vim_free(scope);
4203     return arg;
4204 }
4205 
4206 /*
4207  * compile "for var in expr"
4208  *
4209  * Produces instructions:
4210  *       PUSHNR -1
4211  *       STORE loop-idx		Set index to -1
4212  *       EVAL expr		Push result of "expr"
4213  * top:  FOR loop-idx, end	Increment index, use list on bottom of stack
4214  *				- if beyond end, jump to "end"
4215  *				- otherwise get item from list and push it
4216  *       STORE var		Store item in "var"
4217  *       ... body ...
4218  *       JUMP top		Jump back to repeat
4219  * end:	 DROP			Drop the result of "expr"
4220  *
4221  */
4222     static char_u *
4223 compile_for(char_u *arg, cctx_T *cctx)
4224 {
4225     char_u	*p;
4226     size_t	varlen;
4227     garray_T	*instr = &cctx->ctx_instr;
4228     garray_T	*stack = &cctx->ctx_type_stack;
4229     scope_T	*scope;
4230     int		loop_idx;	// index of loop iteration variable
4231     int		var_idx;	// index of "var"
4232     type_T	*vartype;
4233 
4234     // TODO: list of variables: "for [key, value] in dict"
4235     // parse "var"
4236     for (p = arg; eval_isnamec1(*p); ++p)
4237 	;
4238     varlen = p - arg;
4239     var_idx = lookup_local(arg, varlen, cctx);
4240     if (var_idx >= 0)
4241     {
4242 	semsg(_("E1023: variable already defined: %s"), arg);
4243 	return NULL;
4244     }
4245 
4246     // consume "in"
4247     p = skipwhite(p);
4248     if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4249     {
4250 	emsg(_(e_missing_in));
4251 	return NULL;
4252     }
4253     p = skipwhite(p + 2);
4254 
4255 
4256     scope = new_scope(cctx, FOR_SCOPE);
4257     if (scope == NULL)
4258 	return NULL;
4259 
4260     // Reserve a variable to store the loop iteration counter.
4261     loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4262     if (loop_idx < 0)
4263 	return NULL;
4264 
4265     // Reserve a variable to store "var"
4266     var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4267     if (var_idx < 0)
4268 	return NULL;
4269 
4270     generate_STORENR(cctx, loop_idx, -1);
4271 
4272     // compile "expr", it remains on the stack until "endfor"
4273     arg = p;
4274     if (compile_expr1(&arg, cctx) == FAIL)
4275 	return NULL;
4276 
4277     // now we know the type of "var"
4278     vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4279     if (vartype->tt_type != VAR_LIST)
4280     {
4281 	emsg(_("E1024: need a List to iterate over"));
4282 	return NULL;
4283     }
4284     if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4285     {
4286 	lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4287 
4288 	lvar->lv_type = vartype->tt_member;
4289     }
4290 
4291     // "for_end" is set when ":endfor" is found
4292     scope->se_u.se_for.fs_top_label = instr->ga_len;
4293 
4294     generate_FOR(cctx, loop_idx);
4295     generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4296 
4297     return arg;
4298 }
4299 
4300 /*
4301  * compile "endfor"
4302  */
4303     static char_u *
4304 compile_endfor(char_u *arg, cctx_T *cctx)
4305 {
4306     garray_T	*instr = &cctx->ctx_instr;
4307     scope_T	*scope = cctx->ctx_scope;
4308     forscope_T	*forscope;
4309     isn_T	*isn;
4310 
4311     if (scope == NULL || scope->se_type != FOR_SCOPE)
4312     {
4313 	emsg(_(e_for));
4314 	return NULL;
4315     }
4316     forscope = &scope->se_u.se_for;
4317     cctx->ctx_scope = scope->se_outer;
4318     cctx->ctx_locals.ga_len = scope->se_local_count;
4319 
4320     // At end of ":for" scope jump back to the FOR instruction.
4321     generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4322 
4323     // Fill in the "end" label in the FOR statement so it can jump here
4324     isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4325     isn->isn_arg.forloop.for_end = instr->ga_len;
4326 
4327     // Fill in the "end" label any BREAK statements
4328     compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4329 
4330     // Below the ":for" scope drop the "expr" list from the stack.
4331     if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4332 	return NULL;
4333 
4334     vim_free(scope);
4335 
4336     return arg;
4337 }
4338 
4339 /*
4340  * compile "while expr"
4341  *
4342  * Produces instructions:
4343  * top:  EVAL expr		Push result of "expr"
4344  *       JUMP_IF_FALSE end	jump if false
4345  *       ... body ...
4346  *       JUMP top		Jump back to repeat
4347  * end:
4348  *
4349  */
4350     static char_u *
4351 compile_while(char_u *arg, cctx_T *cctx)
4352 {
4353     char_u	*p = arg;
4354     garray_T	*instr = &cctx->ctx_instr;
4355     scope_T	*scope;
4356 
4357     scope = new_scope(cctx, WHILE_SCOPE);
4358     if (scope == NULL)
4359 	return NULL;
4360 
4361     scope->se_u.se_while.ws_top_label = instr->ga_len;
4362 
4363     // compile "expr"
4364     if (compile_expr1(&p, cctx) == FAIL)
4365 	return NULL;
4366 
4367     // "while_end" is set when ":endwhile" is found
4368     if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
4369 						  JUMP_IF_FALSE, cctx) == FAIL)
4370 	return FAIL;
4371 
4372     return p;
4373 }
4374 
4375 /*
4376  * compile "endwhile"
4377  */
4378     static char_u *
4379 compile_endwhile(char_u *arg, cctx_T *cctx)
4380 {
4381     scope_T	*scope = cctx->ctx_scope;
4382 
4383     if (scope == NULL || scope->se_type != WHILE_SCOPE)
4384     {
4385 	emsg(_(e_while));
4386 	return NULL;
4387     }
4388     cctx->ctx_scope = scope->se_outer;
4389     cctx->ctx_locals.ga_len = scope->se_local_count;
4390 
4391     // At end of ":for" scope jump back to the FOR instruction.
4392     generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
4393 
4394     // Fill in the "end" label in the WHILE statement so it can jump here.
4395     // And in any jumps for ":break"
4396     compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
4397 
4398     vim_free(scope);
4399 
4400     return arg;
4401 }
4402 
4403 /*
4404  * compile "continue"
4405  */
4406     static char_u *
4407 compile_continue(char_u *arg, cctx_T *cctx)
4408 {
4409     scope_T	*scope = cctx->ctx_scope;
4410 
4411     for (;;)
4412     {
4413 	if (scope == NULL)
4414 	{
4415 	    emsg(_(e_continue));
4416 	    return NULL;
4417 	}
4418 	if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4419 	    break;
4420 	scope = scope->se_outer;
4421     }
4422 
4423     // Jump back to the FOR or WHILE instruction.
4424     generate_JUMP(cctx, JUMP_ALWAYS,
4425 	    scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4426 					  : scope->se_u.se_while.ws_top_label);
4427     return arg;
4428 }
4429 
4430 /*
4431  * compile "break"
4432  */
4433     static char_u *
4434 compile_break(char_u *arg, cctx_T *cctx)
4435 {
4436     scope_T	*scope = cctx->ctx_scope;
4437     endlabel_T	**el;
4438 
4439     for (;;)
4440     {
4441 	if (scope == NULL)
4442 	{
4443 	    emsg(_(e_break));
4444 	    return NULL;
4445 	}
4446 	if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4447 	    break;
4448 	scope = scope->se_outer;
4449     }
4450 
4451     // Jump to the end of the FOR or WHILE loop.
4452     if (scope->se_type == FOR_SCOPE)
4453 	el = &scope->se_u.se_for.fs_end_label;
4454     else
4455 	el = &scope->se_u.se_while.ws_end_label;
4456     if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4457 	return FAIL;
4458 
4459     return arg;
4460 }
4461 
4462 /*
4463  * compile "{" start of block
4464  */
4465     static char_u *
4466 compile_block(char_u *arg, cctx_T *cctx)
4467 {
4468     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4469 	return NULL;
4470     return skipwhite(arg + 1);
4471 }
4472 
4473 /*
4474  * compile end of block: drop one scope
4475  */
4476     static void
4477 compile_endblock(cctx_T *cctx)
4478 {
4479     scope_T	*scope = cctx->ctx_scope;
4480 
4481     cctx->ctx_scope = scope->se_outer;
4482     cctx->ctx_locals.ga_len = scope->se_local_count;
4483     vim_free(scope);
4484 }
4485 
4486 /*
4487  * compile "try"
4488  * Creates a new scope for the try-endtry, pointing to the first catch and
4489  * finally.
4490  * Creates another scope for the "try" block itself.
4491  * TRY instruction sets up exception handling at runtime.
4492  *
4493  *	"try"
4494  *	    TRY -> catch1, -> finally  push trystack entry
4495  *	    ... try block
4496  *	"throw {exception}"
4497  *	    EVAL {exception}
4498  *	    THROW		create exception
4499  *	    ... try block
4500  *	" catch {expr}"
4501  *	    JUMP -> finally
4502  * catch1:  PUSH exeception
4503  *	    EVAL {expr}
4504  *	    MATCH
4505  *	    JUMP nomatch -> catch2
4506  *	    CATCH   remove exception
4507  *	    ... catch block
4508  *	" catch"
4509  *	    JUMP -> finally
4510  * catch2:  CATCH   remove exception
4511  *	    ... catch block
4512  *	" finally"
4513  * finally:
4514  *	    ... finally block
4515  *	" endtry"
4516  *	    ENDTRY  pop trystack entry, may rethrow
4517  */
4518     static char_u *
4519 compile_try(char_u *arg, cctx_T *cctx)
4520 {
4521     garray_T	*instr = &cctx->ctx_instr;
4522     scope_T	*try_scope;
4523     scope_T	*scope;
4524 
4525     // scope that holds the jumps that go to catch/finally/endtry
4526     try_scope = new_scope(cctx, TRY_SCOPE);
4527     if (try_scope == NULL)
4528 	return NULL;
4529 
4530     // "catch" is set when the first ":catch" is found.
4531     // "finally" is set when ":finally" or ":endtry" is found
4532     try_scope->se_u.se_try.ts_try_label = instr->ga_len;
4533     if (generate_instr(cctx, ISN_TRY) == NULL)
4534 	return NULL;
4535 
4536     // scope for the try block itself
4537     scope = new_scope(cctx, BLOCK_SCOPE);
4538     if (scope == NULL)
4539 	return NULL;
4540 
4541     return arg;
4542 }
4543 
4544 /*
4545  * compile "catch {expr}"
4546  */
4547     static char_u *
4548 compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4549 {
4550     scope_T	*scope = cctx->ctx_scope;
4551     garray_T	*instr = &cctx->ctx_instr;
4552     char_u	*p;
4553     isn_T	*isn;
4554 
4555     // end block scope from :try or :catch
4556     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4557 	compile_endblock(cctx);
4558     scope = cctx->ctx_scope;
4559 
4560     // Error if not in a :try scope
4561     if (scope == NULL || scope->se_type != TRY_SCOPE)
4562     {
4563 	emsg(_(e_catch));
4564 	return NULL;
4565     }
4566 
4567     if (scope->se_u.se_try.ts_caught_all)
4568     {
4569 	emsg(_("E1033: catch unreachable after catch-all"));
4570 	return NULL;
4571     }
4572 
4573     // Jump from end of previous block to :finally or :endtry
4574     if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
4575 						    JUMP_ALWAYS, cctx) == FAIL)
4576 	return NULL;
4577 
4578     // End :try or :catch scope: set value in ISN_TRY instruction
4579     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
4580     if (isn->isn_arg.try.try_catch == 0)
4581 	isn->isn_arg.try.try_catch = instr->ga_len;
4582     if (scope->se_u.se_try.ts_catch_label != 0)
4583     {
4584 	// Previous catch without match jumps here
4585 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
4586 	isn->isn_arg.jump.jump_where = instr->ga_len;
4587     }
4588 
4589     p = skipwhite(arg);
4590     if (ends_excmd(*p))
4591     {
4592 	scope->se_u.se_try.ts_caught_all = TRUE;
4593 	scope->se_u.se_try.ts_catch_label = 0;
4594     }
4595     else
4596     {
4597 	char_u *end;
4598 	char_u *pat;
4599 	char_u *tofree = NULL;
4600 	int	len;
4601 
4602 	// Push v:exception, push {expr} and MATCH
4603 	generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4604 
4605 	end = skip_regexp(p + 1, *p, TRUE, &tofree);
4606 	if (*end != *p)
4607 	{
4608 	    semsg(_("E1067: Separator mismatch: %s"), p);
4609 	    vim_free(tofree);
4610 	    return FAIL;
4611 	}
4612 	if (tofree == NULL)
4613 	    len = (int)(end - (p + 1));
4614 	else
4615 	    len = (int)(end - (tofree + 1));
4616 	pat = vim_strnsave(p + 1, len);
4617 	vim_free(tofree);
4618 	p += len + 2;
4619 	if (pat == NULL)
4620 	    return FAIL;
4621 	if (generate_PUSHS(cctx, pat) == FAIL)
4622 	    return FAIL;
4623 
4624 	if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4625 	    return NULL;
4626 
4627 	scope->se_u.se_try.ts_catch_label = instr->ga_len;
4628 	if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4629 	    return NULL;
4630     }
4631 
4632     if (generate_instr(cctx, ISN_CATCH) == NULL)
4633 	return NULL;
4634 
4635     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4636 	return NULL;
4637     return p;
4638 }
4639 
4640     static char_u *
4641 compile_finally(char_u *arg, cctx_T *cctx)
4642 {
4643     scope_T	*scope = cctx->ctx_scope;
4644     garray_T	*instr = &cctx->ctx_instr;
4645     isn_T	*isn;
4646 
4647     // end block scope from :try or :catch
4648     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4649 	compile_endblock(cctx);
4650     scope = cctx->ctx_scope;
4651 
4652     // Error if not in a :try scope
4653     if (scope == NULL || scope->se_type != TRY_SCOPE)
4654     {
4655 	emsg(_(e_finally));
4656 	return NULL;
4657     }
4658 
4659     // End :catch or :finally scope: set value in ISN_TRY instruction
4660     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
4661     if (isn->isn_arg.try.try_finally != 0)
4662     {
4663 	emsg(_(e_finally_dup));
4664 	return NULL;
4665     }
4666 
4667     // Fill in the "end" label in jumps at the end of the blocks.
4668     compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
4669 
4670     if (scope->se_u.se_try.ts_catch_label != 0)
4671     {
4672 	// Previous catch without match jumps here
4673 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
4674 	isn->isn_arg.jump.jump_where = instr->ga_len;
4675     }
4676 
4677     isn->isn_arg.try.try_finally = instr->ga_len;
4678     // TODO: set index in ts_finally_label jumps
4679 
4680     return arg;
4681 }
4682 
4683     static char_u *
4684 compile_endtry(char_u *arg, cctx_T *cctx)
4685 {
4686     scope_T	*scope = cctx->ctx_scope;
4687     garray_T	*instr = &cctx->ctx_instr;
4688     isn_T	*isn;
4689 
4690     // end block scope from :catch or :finally
4691     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4692 	compile_endblock(cctx);
4693     scope = cctx->ctx_scope;
4694 
4695     // Error if not in a :try scope
4696     if (scope == NULL || scope->se_type != TRY_SCOPE)
4697     {
4698 	if (scope == NULL)
4699 	    emsg(_(e_no_endtry));
4700 	else if (scope->se_type == WHILE_SCOPE)
4701 	    emsg(_(e_endwhile));
4702 	else if (scope->se_type == FOR_SCOPE)
4703 	    emsg(_(e_endfor));
4704 	else
4705 	    emsg(_(e_endif));
4706 	return NULL;
4707     }
4708 
4709     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
4710     if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4711     {
4712 	emsg(_("E1032: missing :catch or :finally"));
4713 	return NULL;
4714     }
4715 
4716     // Fill in the "end" label in jumps at the end of the blocks, if not done
4717     // by ":finally".
4718     compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
4719 
4720     // End :catch or :finally scope: set value in ISN_TRY instruction
4721     if (isn->isn_arg.try.try_finally == 0)
4722 	isn->isn_arg.try.try_finally = instr->ga_len;
4723     compile_endblock(cctx);
4724 
4725     if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4726 	return NULL;
4727     return arg;
4728 }
4729 
4730 /*
4731  * compile "throw {expr}"
4732  */
4733     static char_u *
4734 compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4735 {
4736     char_u *p = skipwhite(arg);
4737 
4738     if (ends_excmd(*p))
4739     {
4740 	emsg(_(e_argreq));
4741 	return NULL;
4742     }
4743     if (compile_expr1(&p, cctx) == FAIL)
4744 	return NULL;
4745     if (may_generate_2STRING(-1, cctx) == FAIL)
4746 	return NULL;
4747     if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4748 	return NULL;
4749 
4750     return p;
4751 }
4752 
4753 /*
4754  * compile "echo expr"
4755  */
4756     static char_u *
4757 compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4758 {
4759     char_u	*p = arg;
4760     int		count = 0;
4761 
4762     for (;;)
4763     {
4764 	if (compile_expr1(&p, cctx) == FAIL)
4765 	    return NULL;
4766 	++count;
4767 	p = skipwhite(p);
4768 	if (ends_excmd(*p))
4769 	    break;
4770     }
4771 
4772     generate_ECHO(cctx, with_white, count);
4773     return p;
4774 }
4775 
4776 /*
4777  * compile "execute expr"
4778  */
4779     static char_u *
4780 compile_execute(char_u *arg, cctx_T *cctx)
4781 {
4782     char_u	*p = arg;
4783     int		count = 0;
4784 
4785     for (;;)
4786     {
4787 	if (compile_expr1(&p, cctx) == FAIL)
4788 	    return NULL;
4789 	++count;
4790 	p = skipwhite(p);
4791 	if (ends_excmd(*p))
4792 	    break;
4793     }
4794 
4795     generate_EXECUTE(cctx, count);
4796 
4797     return p;
4798 }
4799 
4800 /*
4801  * After ex_function() has collected all the function lines: parse and compile
4802  * the lines into instructions.
4803  * Adds the function to "def_functions".
4804  * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4805  * return statement (used for lambda).
4806  */
4807     void
4808 compile_def_function(ufunc_T *ufunc, int set_return_type)
4809 {
4810     dfunc_T	*dfunc;
4811     char_u	*line = NULL;
4812     char_u	*p;
4813     exarg_T	ea;
4814     char	*errormsg = NULL;	// error message
4815     int		had_return = FALSE;
4816     cctx_T	cctx;
4817     garray_T	*instr;
4818     int		called_emsg_before = called_emsg;
4819     int		ret = FAIL;
4820     sctx_T	save_current_sctx = current_sctx;
4821     int		emsg_before = called_emsg;
4822 
4823     if (ufunc->uf_dfunc_idx >= 0)
4824     {
4825 	// redefining a function that was compiled before
4826 	dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4827 	dfunc->df_deleted = FALSE;
4828     }
4829     else
4830     {
4831 	// Add the function to "def_functions".
4832 	if (ga_grow(&def_functions, 1) == FAIL)
4833 	    return;
4834 	dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4835 	vim_memset(dfunc, 0, sizeof(dfunc_T));
4836 	dfunc->df_idx = def_functions.ga_len;
4837 	ufunc->uf_dfunc_idx = dfunc->df_idx;
4838 	dfunc->df_ufunc = ufunc;
4839 	++def_functions.ga_len;
4840     }
4841 
4842     vim_memset(&cctx, 0, sizeof(cctx));
4843     cctx.ctx_ufunc = ufunc;
4844     cctx.ctx_lnum = -1;
4845     ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4846     ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4847     ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4848     cctx.ctx_type_list = &ufunc->uf_type_list;
4849     ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4850     instr = &cctx.ctx_instr;
4851 
4852     // Most modern script version.
4853     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4854 
4855     if (ufunc->uf_def_args.ga_len > 0)
4856     {
4857 	int	count = ufunc->uf_def_args.ga_len;
4858 	int	i;
4859 	char_u	*arg;
4860 	int	off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4861 
4862 	// Produce instructions for the default values of optional arguments.
4863 	// Store the instruction index in uf_def_arg_idx[] so that we know
4864 	// where to start when the function is called, depending on the number
4865 	// of arguments.
4866 	ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4867 	if (ufunc->uf_def_arg_idx == NULL)
4868 	    goto erret;
4869 	for (i = 0; i < count; ++i)
4870 	{
4871 	    ufunc->uf_def_arg_idx[i] = instr->ga_len;
4872 	    arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4873 	    if (compile_expr1(&arg, &cctx) == FAIL
4874 		    || generate_STORE(&cctx, ISN_STORE,
4875 						i - count - off, NULL) == FAIL)
4876 		goto erret;
4877 	}
4878 
4879 	// If a varargs is following, push an empty list.
4880 	if (ufunc->uf_va_name != NULL)
4881 	{
4882 	    if (generate_NEWLIST(&cctx, 0) == FAIL
4883 		    || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4884 		goto erret;
4885 	}
4886 
4887 	ufunc->uf_def_arg_idx[count] = instr->ga_len;
4888     }
4889 
4890     /*
4891      * Loop over all the lines of the function and generate instructions.
4892      */
4893     for (;;)
4894     {
4895 	int	is_ex_command;
4896 
4897 	if (line != NULL && *line == '|')
4898 	    // the line continues after a '|'
4899 	    ++line;
4900 	else if (line != NULL && *line != NUL)
4901 	{
4902 	    if (emsg_before == called_emsg)
4903 		semsg(_("E488: Trailing characters: %s"), line);
4904 	    goto erret;
4905 	}
4906 	else
4907 	{
4908 	    do
4909 	    {
4910 		++cctx.ctx_lnum;
4911 		if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4912 		    break;
4913 		line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4914 	    } while (line == NULL);
4915 	    if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4916 		break;
4917 	    SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4918 	}
4919 	emsg_before = called_emsg;
4920 
4921 	had_return = FALSE;
4922 	vim_memset(&ea, 0, sizeof(ea));
4923 	ea.cmdlinep = &line;
4924 	ea.cmd = skipwhite(line);
4925 
4926 	// "}" ends a block scope
4927 	if (*ea.cmd == '}')
4928 	{
4929 	    scopetype_T stype = cctx.ctx_scope == NULL
4930 					 ? NO_SCOPE : cctx.ctx_scope->se_type;
4931 
4932 	    if (stype == BLOCK_SCOPE)
4933 	    {
4934 		compile_endblock(&cctx);
4935 		line = ea.cmd;
4936 	    }
4937 	    else
4938 	    {
4939 		emsg(_("E1025: using } outside of a block scope"));
4940 		goto erret;
4941 	    }
4942 	    if (line != NULL)
4943 		line = skipwhite(ea.cmd + 1);
4944 	    continue;
4945 	}
4946 
4947 	// "{" starts a block scope
4948 	if (*ea.cmd == '{')
4949 	{
4950 	    line = compile_block(ea.cmd, &cctx);
4951 	    continue;
4952 	}
4953 	is_ex_command = *ea.cmd == ':';
4954 
4955 	/*
4956 	 * COMMAND MODIFIERS
4957 	 */
4958 	if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4959 	{
4960 	    if (errormsg != NULL)
4961 		goto erret;
4962 	    // empty line or comment
4963 	    line = (char_u *)"";
4964 	    continue;
4965 	}
4966 
4967 	// Skip ":call" to get to the function name.
4968 	if (checkforcmd(&ea.cmd, "call", 3))
4969 	    ea.cmd = skipwhite(ea.cmd);
4970 
4971 	if (!is_ex_command)
4972 	{
4973 	    // Assuming the command starts with a variable or function name,
4974 	    // find what follows.  Also "&opt = val", "$ENV = val" and "@r =
4975 	    // val".
4976 	    p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
4977 							 ? ea.cmd + 1 : ea.cmd;
4978 	    p = to_name_end(p);
4979 	    if ((p > ea.cmd && *p != NUL) || *p == '(')
4980 	    {
4981 		int oplen;
4982 		int heredoc;
4983 
4984 		oplen = assignment_len(skipwhite(p), &heredoc);
4985 		if (oplen > 0)
4986 		{
4987 		    // Recognize an assignment if we recognize the variable
4988 		    // name:
4989 		    // "g:var = expr"
4990 		    // "var = expr"  where "var" is a local var name.
4991 		    // "&opt = expr"
4992 		    // "$ENV = expr"
4993 		    // "@r = expr"
4994 		    if (*ea.cmd == '&'
4995 			    || *ea.cmd == '$'
4996 			    || *ea.cmd == '@'
4997 			    || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4998 			    || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4999 			    || lookup_script(ea.cmd, p - ea.cmd) == OK
5000 			    || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5001 		    {
5002 			line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5003 			if (line == NULL)
5004 			    goto erret;
5005 			continue;
5006 		    }
5007 		}
5008 	    }
5009 	}
5010 
5011 	/*
5012 	 * COMMAND after range
5013 	 */
5014 	ea.cmd = skip_range(ea.cmd, NULL);
5015 	p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5016 									&cctx);
5017 
5018 	if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5019 	{
5020 	    if (cctx.ctx_skip == TRUE)
5021 	    {
5022 		line += STRLEN(line);
5023 		continue;
5024 	    }
5025 
5026 	    // Expression or function call.
5027 	    if (ea.cmdidx == CMD_eval)
5028 	    {
5029 		p = ea.cmd;
5030 		if (compile_expr1(&p, &cctx) == FAIL)
5031 		    goto erret;
5032 
5033 		// drop the return value
5034 		generate_instr_drop(&cctx, ISN_DROP, 1);
5035 		line = p;
5036 		continue;
5037 	    }
5038 	    if (ea.cmdidx == CMD_let)
5039 	    {
5040 		line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5041 		if (line == NULL)
5042 		    goto erret;
5043 		continue;
5044 	    }
5045 	    iemsg("Command from find_ex_command() not handled");
5046 	    goto erret;
5047 	}
5048 
5049 	p = skipwhite(p);
5050 
5051 	if (cctx.ctx_skip == TRUE
5052 		&& ea.cmdidx != CMD_elseif
5053 		&& ea.cmdidx != CMD_else
5054 		&& ea.cmdidx != CMD_endif)
5055 	{
5056 	    line += STRLEN(line);
5057 	    continue;
5058 	}
5059 
5060 	switch (ea.cmdidx)
5061 	{
5062 	    case CMD_def:
5063 	    case CMD_function:
5064 		    // TODO: Nested function
5065 		    emsg("Nested function not implemented yet");
5066 		    goto erret;
5067 
5068 	    case CMD_return:
5069 		    line = compile_return(p, set_return_type, &cctx);
5070 		    had_return = TRUE;
5071 		    break;
5072 
5073 	    case CMD_let:
5074 	    case CMD_const:
5075 		    line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5076 		    break;
5077 
5078 	    case CMD_import:
5079 		    line = compile_import(p, &cctx);
5080 		    break;
5081 
5082 	    case CMD_if:
5083 		    line = compile_if(p, &cctx);
5084 		    break;
5085 	    case CMD_elseif:
5086 		    line = compile_elseif(p, &cctx);
5087 		    break;
5088 	    case CMD_else:
5089 		    line = compile_else(p, &cctx);
5090 		    break;
5091 	    case CMD_endif:
5092 		    line = compile_endif(p, &cctx);
5093 		    break;
5094 
5095 	    case CMD_while:
5096 		    line = compile_while(p, &cctx);
5097 		    break;
5098 	    case CMD_endwhile:
5099 		    line = compile_endwhile(p, &cctx);
5100 		    break;
5101 
5102 	    case CMD_for:
5103 		    line = compile_for(p, &cctx);
5104 		    break;
5105 	    case CMD_endfor:
5106 		    line = compile_endfor(p, &cctx);
5107 		    break;
5108 	    case CMD_continue:
5109 		    line = compile_continue(p, &cctx);
5110 		    break;
5111 	    case CMD_break:
5112 		    line = compile_break(p, &cctx);
5113 		    break;
5114 
5115 	    case CMD_try:
5116 		    line = compile_try(p, &cctx);
5117 		    break;
5118 	    case CMD_catch:
5119 		    line = compile_catch(p, &cctx);
5120 		    break;
5121 	    case CMD_finally:
5122 		    line = compile_finally(p, &cctx);
5123 		    break;
5124 	    case CMD_endtry:
5125 		    line = compile_endtry(p, &cctx);
5126 		    break;
5127 	    case CMD_throw:
5128 		    line = compile_throw(p, &cctx);
5129 		    break;
5130 
5131 	    case CMD_echo:
5132 		    line = compile_echo(p, TRUE, &cctx);
5133 		    break;
5134 	    case CMD_echon:
5135 		    line = compile_echo(p, FALSE, &cctx);
5136 		    break;
5137 	    case CMD_execute:
5138 		    line = compile_execute(p, &cctx);
5139 		    break;
5140 
5141 	    default:
5142 		    // Not recognized, execute with do_cmdline_cmd().
5143 		    // TODO:
5144 		    // CMD_echomsg
5145 		    // etc.
5146 		    generate_EXEC(&cctx, line);
5147 		    line = (char_u *)"";
5148 		    break;
5149 	}
5150 	if (line == NULL)
5151 	    goto erret;
5152 
5153 	if (cctx.ctx_type_stack.ga_len < 0)
5154 	{
5155 	    iemsg("Type stack underflow");
5156 	    goto erret;
5157 	}
5158     }
5159 
5160     if (cctx.ctx_scope != NULL)
5161     {
5162 	if (cctx.ctx_scope->se_type == IF_SCOPE)
5163 	    emsg(_(e_endif));
5164 	else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5165 	    emsg(_(e_endwhile));
5166 	else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5167 	    emsg(_(e_endfor));
5168 	else
5169 	    emsg(_("E1026: Missing }"));
5170 	goto erret;
5171     }
5172 
5173     if (!had_return)
5174     {
5175 	if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5176 	{
5177 	    emsg(_("E1027: Missing return statement"));
5178 	    goto erret;
5179 	}
5180 
5181 	// Return zero if there is no return at the end.
5182 	generate_PUSHNR(&cctx, 0);
5183 	generate_instr(&cctx, ISN_RETURN);
5184     }
5185 
5186     dfunc->df_instr = instr->ga_data;
5187     dfunc->df_instr_count = instr->ga_len;
5188     dfunc->df_varcount = cctx.ctx_max_local;
5189 
5190     ret = OK;
5191 
5192 erret:
5193     if (ret == FAIL)
5194     {
5195 	ga_clear(instr);
5196 	ufunc->uf_dfunc_idx = -1;
5197 	--def_functions.ga_len;
5198 	if (errormsg != NULL)
5199 	    emsg(errormsg);
5200 	else if (called_emsg == called_emsg_before)
5201 	    emsg(_("E1028: compile_def_function failed"));
5202 
5203 	// don't execute this function body
5204 	ufunc->uf_lines.ga_len = 0;
5205     }
5206 
5207     current_sctx = save_current_sctx;
5208     ga_clear(&cctx.ctx_type_stack);
5209     ga_clear(&cctx.ctx_locals);
5210 }
5211 
5212 /*
5213  * Delete an instruction, free what it contains.
5214  */
5215     static void
5216 delete_instr(isn_T *isn)
5217 {
5218     switch (isn->isn_type)
5219     {
5220 	case ISN_EXEC:
5221 	case ISN_LOADENV:
5222 	case ISN_LOADG:
5223 	case ISN_LOADOPT:
5224 	case ISN_MEMBER:
5225 	case ISN_PUSHEXC:
5226 	case ISN_PUSHS:
5227 	case ISN_STOREENV:
5228 	case ISN_STOREG:
5229 	case ISN_PUSHFUNC:
5230 	    vim_free(isn->isn_arg.string);
5231 	    break;
5232 
5233 	case ISN_LOADS:
5234 	case ISN_STORES:
5235 	    vim_free(isn->isn_arg.loadstore.ls_name);
5236 	    break;
5237 
5238 	case ISN_STOREOPT:
5239 	    vim_free(isn->isn_arg.storeopt.so_name);
5240 	    break;
5241 
5242 	case ISN_PUSHBLOB:   // push blob isn_arg.blob
5243 	    blob_unref(isn->isn_arg.blob);
5244 	    break;
5245 
5246 	case ISN_PUSHPARTIAL:
5247 	    partial_unref(isn->isn_arg.partial);
5248 	    break;
5249 
5250 	case ISN_PUSHJOB:
5251 #ifdef FEAT_JOB_CHANNEL
5252 	    job_unref(isn->isn_arg.job);
5253 #endif
5254 	    break;
5255 
5256 	case ISN_PUSHCHANNEL:
5257 #ifdef FEAT_JOB_CHANNEL
5258 	    channel_unref(isn->isn_arg.channel);
5259 #endif
5260 	    break;
5261 
5262 	case ISN_UCALL:
5263 	    vim_free(isn->isn_arg.ufunc.cuf_name);
5264 	    break;
5265 
5266 	case ISN_2BOOL:
5267 	case ISN_2STRING:
5268 	case ISN_ADDBLOB:
5269 	case ISN_ADDLIST:
5270 	case ISN_BCALL:
5271 	case ISN_CATCH:
5272 	case ISN_CHECKNR:
5273 	case ISN_CHECKTYPE:
5274 	case ISN_COMPAREANY:
5275 	case ISN_COMPAREBLOB:
5276 	case ISN_COMPAREBOOL:
5277 	case ISN_COMPAREDICT:
5278 	case ISN_COMPAREFLOAT:
5279 	case ISN_COMPAREFUNC:
5280 	case ISN_COMPARELIST:
5281 	case ISN_COMPARENR:
5282 	case ISN_COMPAREPARTIAL:
5283 	case ISN_COMPARESPECIAL:
5284 	case ISN_COMPARESTRING:
5285 	case ISN_CONCAT:
5286 	case ISN_DCALL:
5287 	case ISN_DROP:
5288 	case ISN_ECHO:
5289 	case ISN_EXECUTE:
5290 	case ISN_ENDTRY:
5291 	case ISN_FOR:
5292 	case ISN_FUNCREF:
5293 	case ISN_INDEX:
5294 	case ISN_JUMP:
5295 	case ISN_LOAD:
5296 	case ISN_LOADSCRIPT:
5297 	case ISN_LOADREG:
5298 	case ISN_LOADV:
5299 	case ISN_NEGATENR:
5300 	case ISN_NEWDICT:
5301 	case ISN_NEWLIST:
5302 	case ISN_OPNR:
5303 	case ISN_OPFLOAT:
5304 	case ISN_OPANY:
5305 	case ISN_PCALL:
5306 	case ISN_PUSHF:
5307 	case ISN_PUSHNR:
5308 	case ISN_PUSHBOOL:
5309 	case ISN_PUSHSPEC:
5310 	case ISN_RETURN:
5311 	case ISN_STORE:
5312 	case ISN_STOREV:
5313 	case ISN_STORENR:
5314 	case ISN_STOREREG:
5315 	case ISN_STORESCRIPT:
5316 	case ISN_THROW:
5317 	case ISN_TRY:
5318 	    // nothing allocated
5319 	    break;
5320     }
5321 }
5322 
5323 /*
5324  * When a user function is deleted, delete any associated def function.
5325  */
5326     void
5327 delete_def_function(ufunc_T *ufunc)
5328 {
5329     int idx;
5330 
5331     if (ufunc->uf_dfunc_idx >= 0)
5332     {
5333 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5334 							 + ufunc->uf_dfunc_idx;
5335 	ga_clear(&dfunc->df_def_args_isn);
5336 
5337 	for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5338 	    delete_instr(dfunc->df_instr + idx);
5339 	VIM_CLEAR(dfunc->df_instr);
5340 
5341 	dfunc->df_deleted = TRUE;
5342     }
5343 }
5344 
5345 #if defined(EXITFREE) || defined(PROTO)
5346     void
5347 free_def_functions(void)
5348 {
5349     vim_free(def_functions.ga_data);
5350 }
5351 #endif
5352 
5353 
5354 #endif // FEAT_EVAL
5355