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