blob: fe836487ba4deb8414b597137cc8aa0318f0a9ea [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 == '+')
338 semsg(_("E1035: wrong argument type for +"));
339 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/*
724 * Generate an ISN_LOADS instruction.
725 */
726 static int
727generate_LOADS(
728 cctx_T *cctx,
729 char_u *name,
730 int sid)
731{
732 isn_T *isn;
733
734 if ((isn = generate_instr_type(cctx, ISN_LOADS, &t_any)) == NULL)
735 return FAIL;
736 isn->isn_arg.loads.ls_name = vim_strsave(name);
737 isn->isn_arg.loads.ls_sid = sid;
738
739 return OK;
740}
741
742/*
743 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
744 */
745 static int
746generate_SCRIPT(
747 cctx_T *cctx,
748 isntype_T isn_type,
749 int sid,
750 int idx,
751 type_T *type)
752{
753 isn_T *isn;
754
755 if (isn_type == ISN_LOADSCRIPT)
756 isn = generate_instr_type(cctx, isn_type, type);
757 else
758 isn = generate_instr_drop(cctx, isn_type, 1);
759 if (isn == NULL)
760 return FAIL;
761 isn->isn_arg.script.script_sid = sid;
762 isn->isn_arg.script.script_idx = idx;
763 return OK;
764}
765
766/*
767 * Generate an ISN_NEWLIST instruction.
768 */
769 static int
770generate_NEWLIST(cctx_T *cctx, int count)
771{
772 isn_T *isn;
773 garray_T *stack = &cctx->ctx_type_stack;
774 garray_T *type_list = cctx->ctx_type_list;
775 type_T *type;
776 type_T *member;
777
778 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
779 return FAIL;
780 isn->isn_arg.number = count;
781
782 // drop the value types
783 stack->ga_len -= count;
784
785 // use the first value type for the list member type
786 if (count > 0)
787 member = ((type_T **)stack->ga_data)[stack->ga_len];
788 else
789 member = &t_any;
790 type = get_list_type(member, type_list);
791
792 // add the list type to the type stack
793 if (ga_grow(stack, 1) == FAIL)
794 return FAIL;
795 ((type_T **)stack->ga_data)[stack->ga_len] = type;
796 ++stack->ga_len;
797
798 return OK;
799}
800
801/*
802 * Generate an ISN_NEWDICT instruction.
803 */
804 static int
805generate_NEWDICT(cctx_T *cctx, int count)
806{
807 isn_T *isn;
808 garray_T *stack = &cctx->ctx_type_stack;
809 garray_T *type_list = cctx->ctx_type_list;
810 type_T *type;
811 type_T *member;
812
813 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
814 return FAIL;
815 isn->isn_arg.number = count;
816
817 // drop the key and value types
818 stack->ga_len -= 2 * count;
819
820 // use the first value type for the list member type
821 if (count > 0)
822 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
823 else
824 member = &t_any;
825 type = get_dict_type(member, type_list);
826
827 // add the dict type to the type stack
828 if (ga_grow(stack, 1) == FAIL)
829 return FAIL;
830 ((type_T **)stack->ga_data)[stack->ga_len] = type;
831 ++stack->ga_len;
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_FUNCREF instruction.
838 */
839 static int
840generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
841{
842 isn_T *isn;
843 garray_T *stack = &cctx->ctx_type_stack;
844
845 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
846 return FAIL;
847 isn->isn_arg.number = dfunc_idx;
848
849 if (ga_grow(stack, 1) == FAIL)
850 return FAIL;
851 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
852 // TODO: argument and return types
853 ++stack->ga_len;
854
855 return OK;
856}
857
858/*
859 * Generate an ISN_JUMP instruction.
860 */
861 static int
862generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
863{
864 isn_T *isn;
865 garray_T *stack = &cctx->ctx_type_stack;
866
867 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
868 return FAIL;
869 isn->isn_arg.jump.jump_when = when;
870 isn->isn_arg.jump.jump_where = where;
871
872 if (when != JUMP_ALWAYS && stack->ga_len > 0)
873 --stack->ga_len;
874
875 return OK;
876}
877
878 static int
879generate_FOR(cctx_T *cctx, int loop_idx)
880{
881 isn_T *isn;
882 garray_T *stack = &cctx->ctx_type_stack;
883
884 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
885 return FAIL;
886 isn->isn_arg.forloop.for_idx = loop_idx;
887
888 if (ga_grow(stack, 1) == FAIL)
889 return FAIL;
890 // type doesn't matter, will be stored next
891 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
892 ++stack->ga_len;
893
894 return OK;
895}
896
897/*
898 * Generate an ISN_BCALL instruction.
899 * Return FAIL if the number of arguments is wrong.
900 */
901 static int
902generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906
907 if (check_internal_func(func_idx, argcount) == FAIL)
908 return FAIL;
909
910 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
911 return FAIL;
912 isn->isn_arg.bfunc.cbf_idx = func_idx;
913 isn->isn_arg.bfunc.cbf_argcount = argcount;
914
915 stack->ga_len -= argcount; // drop the arguments
916 if (ga_grow(stack, 1) == FAIL)
917 return FAIL;
918 ((type_T **)stack->ga_data)[stack->ga_len] =
919 internal_func_ret_type(func_idx, argcount);
920 ++stack->ga_len; // add return value
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_DCALL or ISN_UCALL instruction.
927 * Return FAIL if the number of arguments is wrong.
928 */
929 static int
930generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int argcount)
931{
932 isn_T *isn;
933 garray_T *stack = &cctx->ctx_type_stack;
934 int regular_args = ufunc->uf_args.ga_len;
935
936 if (argcount > regular_args && !has_varargs(ufunc))
937 {
938 semsg(_(e_toomanyarg), ufunc->uf_name);
939 return FAIL;
940 }
941 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
942 {
943 semsg(_(e_toofewarg), ufunc->uf_name);
944 return FAIL;
945 }
946
947 // Turn varargs into a list.
948 if (ufunc->uf_va_name != NULL)
949 {
950 int count = argcount - regular_args;
951
952 // TODO: add default values for optional arguments?
953 generate_NEWLIST(cctx, count < 0 ? 0 : count);
954 argcount = regular_args + 1;
955 }
956
957 if ((isn = generate_instr(cctx,
958 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
959 return FAIL;
960 if (ufunc->uf_dfunc_idx >= 0)
961 {
962 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
963 isn->isn_arg.dfunc.cdf_argcount = argcount;
964 }
965 else
966 {
967 // A user function may be deleted and redefined later, can't use the
968 // ufunc pointer, need to look it up again at runtime.
969 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
970 isn->isn_arg.ufunc.cuf_argcount = argcount;
971 }
972
973 stack->ga_len -= argcount; // drop the arguments
974 if (ga_grow(stack, 1) == FAIL)
975 return FAIL;
976 // add return value
977 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
978 ++stack->ga_len;
979
980 return OK;
981}
982
983/*
984 * Generate an ISN_UCALL instruction when the function isn't defined yet.
985 */
986 static int
987generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
988{
989 isn_T *isn;
990 garray_T *stack = &cctx->ctx_type_stack;
991
992 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
993 return FAIL;
994 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
995 isn->isn_arg.ufunc.cuf_argcount = argcount;
996
997 stack->ga_len -= argcount; // drop the arguments
998
999 // drop the funcref/partial, get back the return value
1000 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1001
1002 return OK;
1003}
1004
1005/*
1006 * Generate an ISN_PCALL instruction.
1007 */
1008 static int
1009generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1010{
1011 isn_T *isn;
1012 garray_T *stack = &cctx->ctx_type_stack;
1013
1014 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1015 return FAIL;
1016 isn->isn_arg.pfunc.cpf_top = at_top;
1017 isn->isn_arg.pfunc.cpf_argcount = argcount;
1018
1019 stack->ga_len -= argcount; // drop the arguments
1020
1021 // drop the funcref/partial, get back the return value
1022 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1023
1024 return OK;
1025}
1026
1027/*
1028 * Generate an ISN_MEMBER instruction.
1029 */
1030 static int
1031generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1032{
1033 isn_T *isn;
1034 garray_T *stack = &cctx->ctx_type_stack;
1035 type_T *type;
1036
1037 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1038 return FAIL;
1039 isn->isn_arg.string = vim_strnsave(name, (int)len);
1040
1041 // change dict type to dict member type
1042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1043 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1044
1045 return OK;
1046}
1047
1048/*
1049 * Generate an ISN_ECHO instruction.
1050 */
1051 static int
1052generate_ECHO(cctx_T *cctx, int with_white, int count)
1053{
1054 isn_T *isn;
1055
1056 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1057 return FAIL;
1058 isn->isn_arg.echo.echo_with_white = with_white;
1059 isn->isn_arg.echo.echo_count = count;
1060
1061 return OK;
1062}
1063
1064 static int
1065generate_EXEC(cctx_T *cctx, char_u *line)
1066{
1067 isn_T *isn;
1068
1069 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1070 return FAIL;
1071 isn->isn_arg.string = vim_strsave(line);
1072 return OK;
1073}
1074
1075static char e_white_both[] =
1076 N_("E1004: white space required before and after '%s'");
1077
1078/*
1079 * Reserve space for a local variable.
1080 * Return the index or -1 if it failed.
1081 */
1082 static int
1083reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1084{
1085 int idx;
1086 lvar_T *lvar;
1087
1088 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1089 {
1090 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1091 return -1;
1092 }
1093
1094 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1095 return -1;
1096 idx = cctx->ctx_locals.ga_len;
1097 if (cctx->ctx_max_local < idx + 1)
1098 cctx->ctx_max_local = idx + 1;
1099 ++cctx->ctx_locals.ga_len;
1100
1101 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1102 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1103 lvar->lv_const = isConst;
1104 lvar->lv_type = type;
1105
1106 return idx;
1107}
1108
1109/*
1110 * Skip over a type definition and return a pointer to just after it.
1111 */
1112 char_u *
1113skip_type(char_u *start)
1114{
1115 char_u *p = start;
1116
1117 while (ASCII_ISALNUM(*p) || *p == '_')
1118 ++p;
1119
1120 // Skip over "<type>"; this is permissive about white space.
1121 if (*skipwhite(p) == '<')
1122 {
1123 p = skipwhite(p);
1124 p = skip_type(skipwhite(p + 1));
1125 p = skipwhite(p);
1126 if (*p == '>')
1127 ++p;
1128 }
1129 return p;
1130}
1131
1132/*
1133 * Parse the member type: "<type>" and return "type" with the member set.
1134 * Use "type_list" if a new type needs to be added.
1135 * Returns NULL in case of failure.
1136 */
1137 static type_T *
1138parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1139{
1140 type_T *member_type;
1141
1142 if (**arg != '<')
1143 {
1144 if (*skipwhite(*arg) == '<')
1145 emsg(_("E1007: No white space allowed before <"));
1146 else
1147 emsg(_("E1008: Missing <type>"));
1148 return NULL;
1149 }
1150 *arg = skipwhite(*arg + 1);
1151
1152 member_type = parse_type(arg, type_list);
1153 if (member_type == NULL)
1154 return NULL;
1155
1156 *arg = skipwhite(*arg);
1157 if (**arg != '>')
1158 {
1159 emsg(_("E1009: Missing > after type"));
1160 return NULL;
1161 }
1162 ++*arg;
1163
1164 if (type->tt_type == VAR_LIST)
1165 return get_list_type(member_type, type_list);
1166 return get_dict_type(member_type, type_list);
1167}
1168
1169/*
1170 * Parse a type at "arg" and advance over it.
1171 * Return NULL for failure.
1172 */
1173 type_T *
1174parse_type(char_u **arg, garray_T *type_list)
1175{
1176 char_u *p = *arg;
1177 size_t len;
1178
1179 // skip over the first word
1180 while (ASCII_ISALNUM(*p) || *p == '_')
1181 ++p;
1182 len = p - *arg;
1183
1184 switch (**arg)
1185 {
1186 case 'a':
1187 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1188 {
1189 *arg += len;
1190 return &t_any;
1191 }
1192 break;
1193 case 'b':
1194 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1195 {
1196 *arg += len;
1197 return &t_bool;
1198 }
1199 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1200 {
1201 *arg += len;
1202 return &t_blob;
1203 }
1204 break;
1205 case 'c':
1206 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1207 {
1208 *arg += len;
1209 return &t_channel;
1210 }
1211 break;
1212 case 'd':
1213 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1214 {
1215 *arg += len;
1216 return parse_type_member(arg, &t_dict_any, type_list);
1217 }
1218 break;
1219 case 'f':
1220 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1221 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001222#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 *arg += len;
1224 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001225#else
1226 emsg(_("E1055: This Vim is not compiled with float support"));
1227 return &t_any;
1228#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 }
1230 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1231 {
1232 *arg += len;
1233 // TODO: arguments and return type
1234 return &t_func_any;
1235 }
1236 break;
1237 case 'j':
1238 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1239 {
1240 *arg += len;
1241 return &t_job;
1242 }
1243 break;
1244 case 'l':
1245 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1246 {
1247 *arg += len;
1248 return parse_type_member(arg, &t_list_any, type_list);
1249 }
1250 break;
1251 case 'n':
1252 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1253 {
1254 *arg += len;
1255 return &t_number;
1256 }
1257 break;
1258 case 'p':
1259 if (len == 4 && STRNCMP(*arg, "partial", len) == 0)
1260 {
1261 *arg += len;
1262 // TODO: arguments and return type
1263 return &t_partial_any;
1264 }
1265 break;
1266 case 's':
1267 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1268 {
1269 *arg += len;
1270 return &t_string;
1271 }
1272 break;
1273 case 'v':
1274 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1275 {
1276 *arg += len;
1277 return &t_void;
1278 }
1279 break;
1280 }
1281
1282 semsg(_("E1010: Type not recognized: %s"), *arg);
1283 return &t_any;
1284}
1285
1286/*
1287 * Check if "type1" and "type2" are exactly the same.
1288 */
1289 static int
1290equal_type(type_T *type1, type_T *type2)
1291{
1292 if (type1->tt_type != type2->tt_type)
1293 return FALSE;
1294 switch (type1->tt_type)
1295 {
1296 case VAR_VOID:
1297 case VAR_UNKNOWN:
1298 case VAR_SPECIAL:
1299 case VAR_BOOL:
1300 case VAR_NUMBER:
1301 case VAR_FLOAT:
1302 case VAR_STRING:
1303 case VAR_BLOB:
1304 case VAR_JOB:
1305 case VAR_CHANNEL:
1306 return TRUE; // not composite is always OK
1307 case VAR_LIST:
1308 case VAR_DICT:
1309 return equal_type(type1->tt_member, type2->tt_member);
1310 case VAR_FUNC:
1311 case VAR_PARTIAL:
1312 // TODO; check argument types.
1313 return equal_type(type1->tt_member, type2->tt_member)
1314 && type1->tt_argcount == type2->tt_argcount;
1315 }
1316 return TRUE;
1317}
1318
1319/*
1320 * Find the common type of "type1" and "type2" and put it in "dest".
1321 * "type2" and "dest" may be the same.
1322 */
1323 static void
1324common_type(type_T *type1, type_T *type2, type_T *dest)
1325{
1326 if (equal_type(type1, type2))
1327 {
1328 if (dest != type2)
1329 *dest = *type2;
1330 return;
1331 }
1332
1333 if (type1->tt_type == type2->tt_type)
1334 {
1335 dest->tt_type = type1->tt_type;
1336 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1337 {
1338 common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1339 return;
1340 }
1341 // TODO: VAR_FUNC and VAR_PARTIAL
1342 }
1343
1344 dest->tt_type = VAR_UNKNOWN; // "any"
1345}
1346
1347 char *
1348vartype_name(vartype_T type)
1349{
1350 switch (type)
1351 {
1352 case VAR_VOID: return "void";
1353 case VAR_UNKNOWN: return "any";
1354 case VAR_SPECIAL: return "special";
1355 case VAR_BOOL: return "bool";
1356 case VAR_NUMBER: return "number";
1357 case VAR_FLOAT: return "float";
1358 case VAR_STRING: return "string";
1359 case VAR_BLOB: return "blob";
1360 case VAR_JOB: return "job";
1361 case VAR_CHANNEL: return "channel";
1362 case VAR_LIST: return "list";
1363 case VAR_DICT: return "dict";
1364 case VAR_FUNC: return "function";
1365 case VAR_PARTIAL: return "partial";
1366 }
1367 return "???";
1368}
1369
1370/*
1371 * Return the name of a type.
1372 * The result may be in allocated memory, in which case "tofree" is set.
1373 */
1374 char *
1375type_name(type_T *type, char **tofree)
1376{
1377 char *name = vartype_name(type->tt_type);
1378
1379 *tofree = NULL;
1380 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1381 {
1382 char *member_free;
1383 char *member_name = type_name(type->tt_member, &member_free);
1384 size_t len;
1385
1386 len = STRLEN(name) + STRLEN(member_name) + 3;
1387 *tofree = alloc(len);
1388 if (*tofree != NULL)
1389 {
1390 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1391 vim_free(member_free);
1392 return *tofree;
1393 }
1394 }
1395 // TODO: function and partial argument types
1396
1397 return name;
1398}
1399
1400/*
1401 * Find "name" in script-local items of script "sid".
1402 * Returns the index in "sn_var_vals" if found.
1403 * If found but not in "sn_var_vals" returns -1.
1404 * If not found returns -2.
1405 */
1406 int
1407get_script_item_idx(int sid, char_u *name, int check_writable)
1408{
1409 hashtab_T *ht;
1410 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001411 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 int idx;
1413
1414 // First look the name up in the hashtable.
1415 if (sid <= 0 || sid > script_items.ga_len)
1416 return -1;
1417 ht = &SCRIPT_VARS(sid);
1418 di = find_var_in_ht(ht, 0, name, TRUE);
1419 if (di == NULL)
1420 return -2;
1421
1422 // Now find the svar_T index in sn_var_vals.
1423 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1424 {
1425 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1426
1427 if (sv->sv_tv == &di->di_tv)
1428 {
1429 if (check_writable && sv->sv_const)
1430 semsg(_(e_readonlyvar), name);
1431 return idx;
1432 }
1433 }
1434 return -1;
1435}
1436
1437/*
1438 * Find "name" in imported items of the current script/
1439 */
1440 imported_T *
1441find_imported(char_u *name, cctx_T *cctx)
1442{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001443 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 int idx;
1445
1446 if (cctx != NULL)
1447 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1448 {
1449 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1450 + idx;
1451
1452 if (STRCMP(name, import->imp_name) == 0)
1453 return import;
1454 }
1455
1456 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1457 {
1458 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1459
1460 if (STRCMP(name, import->imp_name) == 0)
1461 return import;
1462 }
1463 return NULL;
1464}
1465
1466/*
1467 * Generate an instruction to load script-local variable "name".
1468 */
1469 static int
1470compile_load_scriptvar(cctx_T *cctx, char_u *name)
1471{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001472 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1474 imported_T *import;
1475
1476 if (idx == -1)
1477 {
1478 // variable exists but is not in sn_var_vals: old style script.
1479 return generate_LOADS(cctx, name, current_sctx.sc_sid);
1480 }
1481 if (idx >= 0)
1482 {
1483 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1484
1485 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1486 current_sctx.sc_sid, idx, sv->sv_type);
1487 return OK;
1488 }
1489
1490 import = find_imported(name, cctx);
1491 if (import != NULL)
1492 {
1493 // TODO: check this is a variable, not a function
1494 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1495 import->imp_sid,
1496 import->imp_var_vals_idx,
1497 import->imp_type);
1498 return OK;
1499 }
1500
1501 semsg(_("E1050: Item not found: %s"), name);
1502 return FAIL;
1503}
1504
1505/*
1506 * Compile a variable name into a load instruction.
1507 * "end" points to just after the name.
1508 * When "error" is FALSE do not give an error when not found.
1509 */
1510 static int
1511compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
1512{
1513 type_T *type;
1514 char_u *name;
1515 int res = FAIL;
1516
1517 if (*(*arg + 1) == ':')
1518 {
1519 // load namespaced variable
1520 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1521 if (name == NULL)
1522 return FAIL;
1523
1524 if (**arg == 'v')
1525 {
1526 // load v:var
1527 int vidx = find_vim_var(name);
1528
1529 if (vidx < 0)
1530 {
1531 if (error)
1532 semsg(_(e_var_notfound), name);
1533 goto theend;
1534 }
1535
1536 // TODO: get actual type
1537 res = generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
1538 }
1539 else if (**arg == 'g')
1540 {
1541 // Global variables can be defined later, thus we don't check if it
1542 // exists, give error at runtime.
1543 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1544 }
1545 else if (**arg == 's')
1546 {
1547 res = compile_load_scriptvar(cctx, name);
1548 }
1549 else
1550 {
1551 semsg("Namespace not supported yet: %s", **arg);
1552 goto theend;
1553 }
1554 }
1555 else
1556 {
1557 size_t len = end - *arg;
1558 int idx;
1559 int gen_load = FALSE;
1560
1561 name = vim_strnsave(*arg, end - *arg);
1562 if (name == NULL)
1563 return FAIL;
1564
1565 idx = lookup_arg(*arg, len, cctx);
1566 if (idx >= 0)
1567 {
1568 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1569 type = cctx->ctx_ufunc->uf_arg_types[idx];
1570 else
1571 type = &t_any;
1572
1573 // Arguments are located above the frame pointer.
1574 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1575 if (cctx->ctx_ufunc->uf_va_name != NULL)
1576 --idx;
1577 gen_load = TRUE;
1578 }
1579 else if (lookup_vararg(*arg, len, cctx))
1580 {
1581 // varargs is always the last argument
1582 idx = -STACK_FRAME_SIZE - 1;
1583 type = cctx->ctx_ufunc->uf_va_type;
1584 gen_load = TRUE;
1585 }
1586 else
1587 {
1588 idx = lookup_local(*arg, len, cctx);
1589 if (idx >= 0)
1590 {
1591 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1592 gen_load = TRUE;
1593 }
1594 else
1595 {
1596 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1597 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1598 res = generate_PUSHBOOL(cctx, **arg == 't'
1599 ? VVAL_TRUE : VVAL_FALSE);
1600 else
1601 res = compile_load_scriptvar(cctx, name);
1602 }
1603 }
1604 if (gen_load)
1605 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1606 }
1607
1608 *arg = end;
1609
1610theend:
1611 if (res == FAIL && error)
1612 semsg(_(e_var_notfound), name);
1613 vim_free(name);
1614 return res;
1615}
1616
1617/*
1618 * Compile the argument expressions.
1619 * "arg" points to just after the "(" and is advanced to after the ")"
1620 */
1621 static int
1622compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1623{
1624 char_u *p = *arg;
1625
1626 while (*p != NUL && *p != ')')
1627 {
1628 if (compile_expr1(&p, cctx) == FAIL)
1629 return FAIL;
1630 ++*argcount;
1631 if (*p == ',')
1632 p = skipwhite(p + 1);
1633 }
1634 if (*p != ')')
1635 {
1636 emsg(_(e_missing_close));
1637 return FAIL;
1638 }
1639 *arg = p + 1;
1640 return OK;
1641}
1642
1643/*
1644 * Compile a function call: name(arg1, arg2)
1645 * "arg" points to "name", "arg + varlen" to the "(".
1646 * "argcount_init" is 1 for "value->method()"
1647 * Instructions:
1648 * EVAL arg1
1649 * EVAL arg2
1650 * BCALL / DCALL / UCALL
1651 */
1652 static int
1653compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1654{
1655 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001656 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 int argcount = argcount_init;
1658 char_u namebuf[100];
1659 ufunc_T *ufunc;
1660
1661 if (varlen >= sizeof(namebuf))
1662 {
1663 semsg(_("E1011: name too long: %s"), name);
1664 return FAIL;
1665 }
1666 vim_strncpy(namebuf, name, varlen);
1667
1668 *arg = skipwhite(*arg + varlen + 1);
1669 if (compile_arguments(arg, cctx, &argcount) == FAIL)
1670 return FAIL;
1671
1672 if (ASCII_ISLOWER(*name))
1673 {
1674 int idx;
1675
1676 // builtin function
1677 idx = find_internal_func(namebuf);
1678 if (idx >= 0)
1679 return generate_BCALL(cctx, idx, argcount);
1680 semsg(_(e_unknownfunc), namebuf);
1681 }
1682
1683 // User defined function or variable must start with upper case.
1684 if (!ASCII_ISUPPER(*name))
1685 {
1686 semsg(_("E1012: Invalid function name: %s"), namebuf);
1687 return FAIL;
1688 }
1689
1690 // If we can find the function by name generate the right call.
1691 ufunc = find_func(namebuf, cctx);
1692 if (ufunc != NULL)
1693 return generate_CALL(cctx, ufunc, argcount);
1694
1695 // If the name is a variable, load it and use PCALL.
1696 p = namebuf;
1697 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
1698 return generate_PCALL(cctx, argcount, FALSE);
1699
1700 // The function may be defined only later. Need to figure out at runtime.
1701 return generate_UCALL(cctx, namebuf, argcount);
1702}
1703
1704// like NAMESPACE_CHAR but with 'a' and 'l'.
1705#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1706
1707/*
1708 * Find the end of a variable or function name. Unlike find_name_end() this
1709 * does not recognize magic braces.
1710 * Return a pointer to just after the name. Equal to "arg" if there is no
1711 * valid name.
1712 */
1713 char_u *
1714to_name_end(char_u *arg)
1715{
1716 char_u *p;
1717
1718 // Quick check for valid starting character.
1719 if (!eval_isnamec1(*arg))
1720 return arg;
1721
1722 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1723 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1724 // and can be used in slice "[n:]".
1725 if (*p == ':' && (p != arg + 1
1726 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1727 break;
1728 return p;
1729}
1730
1731/*
1732 * Like to_name_end() but also skip over a list or dict constant.
1733 */
1734 char_u *
1735to_name_const_end(char_u *arg)
1736{
1737 char_u *p = to_name_end(arg);
1738 typval_T rettv;
1739
1740 if (p == arg && *arg == '[')
1741 {
1742
1743 // Can be "[1, 2, 3]->Func()".
1744 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1745 p = arg;
1746 }
1747 else if (p == arg && *arg == '#' && arg[1] == '{')
1748 {
1749 ++p;
1750 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1751 p = arg;
1752 }
1753 else if (p == arg && *arg == '{')
1754 {
1755 int ret = get_lambda_tv(&p, &rettv, FALSE);
1756
1757 if (ret == NOTDONE)
1758 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1759 if (ret != OK)
1760 p = arg;
1761 }
1762
1763 return p;
1764}
1765
1766 static void
1767type_mismatch(type_T *expected, type_T *actual)
1768{
1769 char *tofree1, *tofree2;
1770
1771 semsg(_("E1013: type mismatch, expected %s but got %s"),
1772 type_name(expected, &tofree1), type_name(actual, &tofree2));
1773 vim_free(tofree1);
1774 vim_free(tofree2);
1775}
1776
1777/*
1778 * Check if the expected and actual types match.
1779 */
1780 static int
1781check_type(type_T *expected, type_T *actual, int give_msg)
1782{
1783 if (expected->tt_type != VAR_UNKNOWN)
1784 {
1785 if (expected->tt_type != actual->tt_type)
1786 {
1787 if (give_msg)
1788 type_mismatch(expected, actual);
1789 return FAIL;
1790 }
1791 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1792 {
1793 int ret = check_type(expected->tt_member, actual->tt_member,
1794 FALSE);
1795 if (ret == FAIL && give_msg)
1796 type_mismatch(expected, actual);
1797 return ret;
1798 }
1799 }
1800 return OK;
1801}
1802
1803/*
1804 * Check that
1805 * - "actual" is "expected" type or
1806 * - "actual" is a type that can be "expected" type: add a runtime check; or
1807 * - return FAIL.
1808 */
1809 static int
1810need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1811{
1812 if (equal_type(actual, expected) || expected->tt_type == VAR_UNKNOWN)
1813 return OK;
1814 if (actual->tt_type != VAR_UNKNOWN)
1815 {
1816 type_mismatch(expected, actual);
1817 return FAIL;
1818 }
1819 generate_TYPECHECK(cctx, expected, offset);
1820 return OK;
1821}
1822
1823/*
1824 * parse a list: [expr, expr]
1825 * "*arg" points to the '['.
1826 */
1827 static int
1828compile_list(char_u **arg, cctx_T *cctx)
1829{
1830 char_u *p = skipwhite(*arg + 1);
1831 int count = 0;
1832
1833 while (*p != ']')
1834 {
1835 if (*p == NUL)
1836 return FAIL;
1837 if (compile_expr1(&p, cctx) == FAIL)
1838 break;
1839 ++count;
1840 if (*p == ',')
1841 ++p;
1842 p = skipwhite(p);
1843 }
1844 *arg = p + 1;
1845
1846 generate_NEWLIST(cctx, count);
1847 return OK;
1848}
1849
1850/*
1851 * parse a lambda: {arg, arg -> expr}
1852 * "*arg" points to the '{'.
1853 */
1854 static int
1855compile_lambda(char_u **arg, cctx_T *cctx)
1856{
1857 garray_T *instr = &cctx->ctx_instr;
1858 typval_T rettv;
1859 ufunc_T *ufunc;
1860
1861 // Get the funcref in "rettv".
1862 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1863 return FAIL;
1864 ufunc = rettv.vval.v_partial->pt_func;
1865
1866 // The function will have one line: "return {expr}".
1867 // Compile it into instructions.
1868 compile_def_function(ufunc, TRUE);
1869
1870 if (ufunc->uf_dfunc_idx >= 0)
1871 {
1872 if (ga_grow(instr, 1) == FAIL)
1873 return FAIL;
1874 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
1875 return OK;
1876 }
1877 return FAIL;
1878}
1879
1880/*
1881 * Compile a lamda call: expr->{lambda}(args)
1882 * "arg" points to the "{".
1883 */
1884 static int
1885compile_lambda_call(char_u **arg, cctx_T *cctx)
1886{
1887 ufunc_T *ufunc;
1888 typval_T rettv;
1889 int argcount = 1;
1890 int ret = FAIL;
1891
1892 // Get the funcref in "rettv".
1893 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1894 return FAIL;
1895
1896 if (**arg != '(')
1897 {
1898 if (*skipwhite(*arg) == '(')
1899 semsg(_(e_nowhitespace));
1900 else
1901 semsg(_(e_missing_paren), "lambda");
1902 clear_tv(&rettv);
1903 return FAIL;
1904 }
1905
1906 // The function will have one line: "return {expr}".
1907 // Compile it into instructions.
1908 ufunc = rettv.vval.v_partial->pt_func;
1909 ++ufunc->uf_refcount;
1910 compile_def_function(ufunc, TRUE);
1911
1912 // compile the arguments
1913 *arg = skipwhite(*arg + 1);
1914 if (compile_arguments(arg, cctx, &argcount) == OK)
1915 // call the compiled function
1916 ret = generate_CALL(cctx, ufunc, argcount);
1917
1918 clear_tv(&rettv);
1919 return ret;
1920}
1921
1922/*
1923 * parse a dict: {'key': val} or #{key: val}
1924 * "*arg" points to the '{'.
1925 */
1926 static int
1927compile_dict(char_u **arg, cctx_T *cctx, int literal)
1928{
1929 garray_T *instr = &cctx->ctx_instr;
1930 int count = 0;
1931 dict_T *d = dict_alloc();
1932 dictitem_T *item;
1933
1934 if (d == NULL)
1935 return FAIL;
1936 *arg = skipwhite(*arg + 1);
1937 while (**arg != '}' && **arg != NUL)
1938 {
1939 char_u *key = NULL;
1940
1941 if (literal)
1942 {
1943 char_u *p = to_name_end(*arg);
1944
1945 if (p == *arg)
1946 {
1947 semsg(_("E1014: Invalid key: %s"), *arg);
1948 return FAIL;
1949 }
1950 key = vim_strnsave(*arg, p - *arg);
1951 if (generate_PUSHS(cctx, key) == FAIL)
1952 return FAIL;
1953 *arg = p;
1954 }
1955 else
1956 {
1957 isn_T *isn;
1958
1959 if (compile_expr1(arg, cctx) == FAIL)
1960 return FAIL;
1961 // TODO: check type is string
1962 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1963 if (isn->isn_type == ISN_PUSHS)
1964 key = isn->isn_arg.string;
1965 }
1966
1967 // Check for duplicate keys, if using string keys.
1968 if (key != NULL)
1969 {
1970 item = dict_find(d, key, -1);
1971 if (item != NULL)
1972 {
1973 semsg(_(e_duplicate_key), key);
1974 goto failret;
1975 }
1976 item = dictitem_alloc(key);
1977 if (item != NULL)
1978 {
1979 item->di_tv.v_type = VAR_UNKNOWN;
1980 item->di_tv.v_lock = 0;
1981 if (dict_add(d, item) == FAIL)
1982 dictitem_free(item);
1983 }
1984 }
1985
1986 *arg = skipwhite(*arg);
1987 if (**arg != ':')
1988 {
1989 semsg(_(e_missing_dict_colon), *arg);
1990 return FAIL;
1991 }
1992
1993 *arg = skipwhite(*arg + 1);
1994 if (compile_expr1(arg, cctx) == FAIL)
1995 return FAIL;
1996 ++count;
1997
1998 if (**arg == '}')
1999 break;
2000 if (**arg != ',')
2001 {
2002 semsg(_(e_missing_dict_comma), *arg);
2003 goto failret;
2004 }
2005 *arg = skipwhite(*arg + 1);
2006 }
2007
2008 if (**arg != '}')
2009 {
2010 semsg(_(e_missing_dict_end), *arg);
2011 goto failret;
2012 }
2013 *arg = *arg + 1;
2014
2015 dict_unref(d);
2016 return generate_NEWDICT(cctx, count);
2017
2018failret:
2019 dict_unref(d);
2020 return FAIL;
2021}
2022
2023/*
2024 * Compile "&option".
2025 */
2026 static int
2027compile_get_option(char_u **arg, cctx_T *cctx)
2028{
2029 typval_T rettv;
2030 char_u *start = *arg;
2031 int ret;
2032
2033 // parse the option and get the current value to get the type.
2034 rettv.v_type = VAR_UNKNOWN;
2035 ret = get_option_tv(arg, &rettv, TRUE);
2036 if (ret == OK)
2037 {
2038 // include the '&' in the name, get_option_tv() expects it.
2039 char_u *name = vim_strnsave(start, *arg - start);
2040 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2041
2042 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2043 vim_free(name);
2044 }
2045 clear_tv(&rettv);
2046
2047 return ret;
2048}
2049
2050/*
2051 * Compile "$VAR".
2052 */
2053 static int
2054compile_get_env(char_u **arg, cctx_T *cctx)
2055{
2056 char_u *start = *arg;
2057 int len;
2058 int ret;
2059 char_u *name;
2060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 ++*arg;
2062 len = get_env_len(arg);
2063 if (len == 0)
2064 {
2065 semsg(_(e_syntax_at), start - 1);
2066 return FAIL;
2067 }
2068
2069 // include the '$' in the name, get_env_tv() expects it.
2070 name = vim_strnsave(start, len + 1);
2071 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2072 vim_free(name);
2073 return ret;
2074}
2075
2076/*
2077 * Compile "@r".
2078 */
2079 static int
2080compile_get_register(char_u **arg, cctx_T *cctx)
2081{
2082 int ret;
2083
2084 ++*arg;
2085 if (**arg == NUL)
2086 {
2087 semsg(_(e_syntax_at), *arg - 1);
2088 return FAIL;
2089 }
2090 if (!valid_yank_reg(**arg, TRUE))
2091 {
2092 emsg_invreg(**arg);
2093 return FAIL;
2094 }
2095 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2096 ++*arg;
2097 return ret;
2098}
2099
2100/*
2101 * Apply leading '!', '-' and '+' to constant "rettv".
2102 */
2103 static int
2104apply_leader(typval_T *rettv, char_u *start, char_u *end)
2105{
2106 char_u *p = end;
2107
2108 // this works from end to start
2109 while (p > start)
2110 {
2111 --p;
2112 if (*p == '-' || *p == '+')
2113 {
2114 // only '-' has an effect, for '+' we only check the type
2115#ifdef FEAT_FLOAT
2116 if (rettv->v_type == VAR_FLOAT)
2117 {
2118 if (*p == '-')
2119 rettv->vval.v_float = -rettv->vval.v_float;
2120 }
2121 else
2122#endif
2123 {
2124 varnumber_T val;
2125 int error = FALSE;
2126
2127 // tv_get_number_chk() accepts a string, but we don't want that
2128 // here
2129 if (check_not_string(rettv) == FAIL)
2130 return FAIL;
2131 val = tv_get_number_chk(rettv, &error);
2132 clear_tv(rettv);
2133 if (error)
2134 return FAIL;
2135 if (*p == '-')
2136 val = -val;
2137 rettv->v_type = VAR_NUMBER;
2138 rettv->vval.v_number = val;
2139 }
2140 }
2141 else
2142 {
2143 int v = tv2bool(rettv);
2144
2145 // '!' is permissive in the type.
2146 clear_tv(rettv);
2147 rettv->v_type = VAR_BOOL;
2148 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2149 }
2150 }
2151 return OK;
2152}
2153
2154/*
2155 * Recognize v: variables that are constants and set "rettv".
2156 */
2157 static void
2158get_vim_constant(char_u **arg, typval_T *rettv)
2159{
2160 if (STRNCMP(*arg, "v:true", 6) == 0)
2161 {
2162 rettv->v_type = VAR_BOOL;
2163 rettv->vval.v_number = VVAL_TRUE;
2164 *arg += 6;
2165 }
2166 else if (STRNCMP(*arg, "v:false", 7) == 0)
2167 {
2168 rettv->v_type = VAR_BOOL;
2169 rettv->vval.v_number = VVAL_FALSE;
2170 *arg += 7;
2171 }
2172 else if (STRNCMP(*arg, "v:null", 6) == 0)
2173 {
2174 rettv->v_type = VAR_SPECIAL;
2175 rettv->vval.v_number = VVAL_NULL;
2176 *arg += 6;
2177 }
2178 else if (STRNCMP(*arg, "v:none", 6) == 0)
2179 {
2180 rettv->v_type = VAR_SPECIAL;
2181 rettv->vval.v_number = VVAL_NONE;
2182 *arg += 6;
2183 }
2184}
2185
2186/*
2187 * Compile code to apply '-', '+' and '!'.
2188 */
2189 static int
2190compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2191{
2192 char_u *p = end;
2193
2194 // this works from end to start
2195 while (p > start)
2196 {
2197 --p;
2198 if (*p == '-' || *p == '+')
2199 {
2200 int negate = *p == '-';
2201 isn_T *isn;
2202
2203 // TODO: check type
2204 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2205 {
2206 --p;
2207 if (*p == '-')
2208 negate = !negate;
2209 }
2210 // only '-' has an effect, for '+' we only check the type
2211 if (negate)
2212 isn = generate_instr(cctx, ISN_NEGATENR);
2213 else
2214 isn = generate_instr(cctx, ISN_CHECKNR);
2215 if (isn == NULL)
2216 return FAIL;
2217 }
2218 else
2219 {
2220 int invert = TRUE;
2221
2222 while (p > start && p[-1] == '!')
2223 {
2224 --p;
2225 invert = !invert;
2226 }
2227 if (generate_2BOOL(cctx, invert) == FAIL)
2228 return FAIL;
2229 }
2230 }
2231 return OK;
2232}
2233
2234/*
2235 * Compile whatever comes after "name" or "name()".
2236 */
2237 static int
2238compile_subscript(
2239 char_u **arg,
2240 cctx_T *cctx,
2241 char_u **start_leader,
2242 char_u *end_leader)
2243{
2244 for (;;)
2245 {
2246 if (**arg == '(')
2247 {
2248 int argcount = 0;
2249
2250 // funcref(arg)
2251 *arg = skipwhite(*arg + 1);
2252 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2253 return FAIL;
2254 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2255 return FAIL;
2256 }
2257 else if (**arg == '-' && (*arg)[1] == '>')
2258 {
2259 char_u *p;
2260
2261 // something->method()
2262 // Apply the '!', '-' and '+' first:
2263 // -1.0->func() works like (-1.0)->func()
2264 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2265 return FAIL;
2266 *start_leader = end_leader; // don't apply again later
2267
2268 *arg = skipwhite(*arg + 2);
2269 if (**arg == '{')
2270 {
2271 // lambda call: list->{lambda}
2272 if (compile_lambda_call(arg, cctx) == FAIL)
2273 return FAIL;
2274 }
2275 else
2276 {
2277 // method call: list->method()
2278 for (p = *arg; eval_isnamec1(*p); ++p)
2279 ;
2280 if (*p != '(')
2281 {
2282 semsg(_(e_missing_paren), arg);
2283 return FAIL;
2284 }
2285 // TODO: base value may not be the first argument
2286 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2287 return FAIL;
2288 }
2289 }
2290 else if (**arg == '[')
2291 {
2292 // list index: list[123]
2293 // TODO: more arguments
2294 // TODO: dict member dict['name']
2295 *arg = skipwhite(*arg + 1);
2296 if (compile_expr1(arg, cctx) == FAIL)
2297 return FAIL;
2298
2299 if (**arg != ']')
2300 {
2301 emsg(_(e_missbrac));
2302 return FAIL;
2303 }
2304 *arg = skipwhite(*arg + 1);
2305
2306 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2307 return FAIL;
2308 }
2309 else if (**arg == '.' && (*arg)[1] != '.')
2310 {
2311 char_u *p;
2312
2313 ++*arg;
2314 p = *arg;
2315 // dictionary member: dict.name
2316 if (eval_isnamec1(*p))
2317 while (eval_isnamec(*p))
2318 MB_PTR_ADV(p);
2319 if (p == *arg)
2320 {
2321 semsg(_(e_syntax_at), *arg);
2322 return FAIL;
2323 }
2324 // TODO: check type is dict
2325 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2326 return FAIL;
2327 *arg = p;
2328 }
2329 else
2330 break;
2331 }
2332
2333 // TODO - see handle_subscript():
2334 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2335 // Don't do this when "Func" is already a partial that was bound
2336 // explicitly (pt_auto is FALSE).
2337
2338 return OK;
2339}
2340
2341/*
2342 * Compile an expression at "*p" and add instructions to "instr".
2343 * "p" is advanced until after the expression, skipping white space.
2344 *
2345 * This is the equivalent of eval1(), eval2(), etc.
2346 */
2347
2348/*
2349 * number number constant
2350 * 0zFFFFFFFF Blob constant
2351 * "string" string constant
2352 * 'string' literal string constant
2353 * &option-name option value
2354 * @r register contents
2355 * identifier variable value
2356 * function() function call
2357 * $VAR environment variable
2358 * (expression) nested expression
2359 * [expr, expr] List
2360 * {key: val, key: val} Dictionary
2361 * #{key: val, key: val} Dictionary with literal keys
2362 *
2363 * Also handle:
2364 * ! in front logical NOT
2365 * - in front unary minus
2366 * + in front unary plus (ignored)
2367 * trailing (arg) funcref/partial call
2368 * trailing [] subscript in String or List
2369 * trailing .name entry in Dictionary
2370 * trailing ->name() method call
2371 */
2372 static int
2373compile_expr7(char_u **arg, cctx_T *cctx)
2374{
2375 typval_T rettv;
2376 char_u *start_leader, *end_leader;
2377 int ret = OK;
2378
2379 /*
2380 * Skip '!', '-' and '+' characters. They are handled later.
2381 */
2382 start_leader = *arg;
2383 while (**arg == '!' || **arg == '-' || **arg == '+')
2384 *arg = skipwhite(*arg + 1);
2385 end_leader = *arg;
2386
2387 rettv.v_type = VAR_UNKNOWN;
2388 switch (**arg)
2389 {
2390 /*
2391 * Number constant.
2392 */
2393 case '0': // also for blob starting with 0z
2394 case '1':
2395 case '2':
2396 case '3':
2397 case '4':
2398 case '5':
2399 case '6':
2400 case '7':
2401 case '8':
2402 case '9':
2403 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2404 return FAIL;
2405 break;
2406
2407 /*
2408 * String constant: "string".
2409 */
2410 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2411 return FAIL;
2412 break;
2413
2414 /*
2415 * Literal string constant: 'str''ing'.
2416 */
2417 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2418 return FAIL;
2419 break;
2420
2421 /*
2422 * Constant Vim variable.
2423 */
2424 case 'v': get_vim_constant(arg, &rettv);
2425 ret = NOTDONE;
2426 break;
2427
2428 /*
2429 * List: [expr, expr]
2430 */
2431 case '[': ret = compile_list(arg, cctx);
2432 break;
2433
2434 /*
2435 * Dictionary: #{key: val, key: val}
2436 */
2437 case '#': if ((*arg)[1] == '{')
2438 {
2439 ++*arg;
2440 ret = compile_dict(arg, cctx, TRUE);
2441 }
2442 else
2443 ret = NOTDONE;
2444 break;
2445
2446 /*
2447 * Lambda: {arg, arg -> expr}
2448 * Dictionary: {'key': val, 'key': val}
2449 */
2450 case '{': {
2451 char_u *start = skipwhite(*arg + 1);
2452
2453 // Find out what comes after the arguments.
2454 ret = get_function_args(&start, '-', NULL,
2455 NULL, NULL, NULL, TRUE);
2456 if (ret != FAIL && *start == '>')
2457 ret = compile_lambda(arg, cctx);
2458 else
2459 ret = compile_dict(arg, cctx, FALSE);
2460 }
2461 break;
2462
2463 /*
2464 * Option value: &name
2465 */
2466 case '&': ret = compile_get_option(arg, cctx);
2467 break;
2468
2469 /*
2470 * Environment variable: $VAR.
2471 */
2472 case '$': ret = compile_get_env(arg, cctx);
2473 break;
2474
2475 /*
2476 * Register contents: @r.
2477 */
2478 case '@': ret = compile_get_register(arg, cctx);
2479 break;
2480 /*
2481 * nested expression: (expression).
2482 */
2483 case '(': *arg = skipwhite(*arg + 1);
2484 ret = compile_expr1(arg, cctx); // recursive!
2485 *arg = skipwhite(*arg);
2486 if (**arg == ')')
2487 ++*arg;
2488 else if (ret == OK)
2489 {
2490 emsg(_(e_missing_close));
2491 ret = FAIL;
2492 }
2493 break;
2494
2495 default: ret = NOTDONE;
2496 break;
2497 }
2498 if (ret == FAIL)
2499 return FAIL;
2500
2501 if (rettv.v_type != VAR_UNKNOWN)
2502 {
2503 // apply the '!', '-' and '+' before the constant
2504 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2505 {
2506 clear_tv(&rettv);
2507 return FAIL;
2508 }
2509 start_leader = end_leader; // don't apply again below
2510
2511 // push constant
2512 switch (rettv.v_type)
2513 {
2514 case VAR_BOOL:
2515 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2516 break;
2517 case VAR_SPECIAL:
2518 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2519 break;
2520 case VAR_NUMBER:
2521 generate_PUSHNR(cctx, rettv.vval.v_number);
2522 break;
2523#ifdef FEAT_FLOAT
2524 case VAR_FLOAT:
2525 generate_PUSHF(cctx, rettv.vval.v_float);
2526 break;
2527#endif
2528 case VAR_BLOB:
2529 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2530 rettv.vval.v_blob = NULL;
2531 break;
2532 case VAR_STRING:
2533 generate_PUSHS(cctx, rettv.vval.v_string);
2534 rettv.vval.v_string = NULL;
2535 break;
2536 default:
2537 iemsg("constant type missing");
2538 return FAIL;
2539 }
2540 }
2541 else if (ret == NOTDONE)
2542 {
2543 char_u *p;
2544 int r;
2545
2546 if (!eval_isnamec1(**arg))
2547 {
2548 semsg(_("E1015: Name expected: %s"), *arg);
2549 return FAIL;
2550 }
2551
2552 // "name" or "name()"
2553 p = to_name_end(*arg);
2554 if (*p == '(')
2555 r = compile_call(arg, p - *arg, cctx, 0);
2556 else
2557 r = compile_load(arg, p, cctx, TRUE);
2558 if (r == FAIL)
2559 return FAIL;
2560 }
2561
2562 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2563 return FAIL;
2564
2565 // Now deal with prefixed '-', '+' and '!', if not done already.
2566 return compile_leader(cctx, start_leader, end_leader);
2567}
2568
2569/*
2570 * * number multiplication
2571 * / number division
2572 * % number modulo
2573 */
2574 static int
2575compile_expr6(char_u **arg, cctx_T *cctx)
2576{
2577 char_u *op;
2578
2579 // get the first variable
2580 if (compile_expr7(arg, cctx) == FAIL)
2581 return FAIL;
2582
2583 /*
2584 * Repeat computing, until no "*", "/" or "%" is following.
2585 */
2586 for (;;)
2587 {
2588 op = skipwhite(*arg);
2589 if (*op != '*' && *op != '/' && *op != '%')
2590 break;
2591 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2592 {
2593 char_u buf[3];
2594
2595 vim_strncpy(buf, op, 1);
2596 semsg(_(e_white_both), buf);
2597 }
2598 *arg = skipwhite(op + 1);
2599
2600 // get the second variable
2601 if (compile_expr7(arg, cctx) == FAIL)
2602 return FAIL;
2603
2604 generate_two_op(cctx, op);
2605 }
2606
2607 return OK;
2608}
2609
2610/*
2611 * + number addition
2612 * - number subtraction
2613 * .. string concatenation
2614 */
2615 static int
2616compile_expr5(char_u **arg, cctx_T *cctx)
2617{
2618 char_u *op;
2619 int oplen;
2620
2621 // get the first variable
2622 if (compile_expr6(arg, cctx) == FAIL)
2623 return FAIL;
2624
2625 /*
2626 * Repeat computing, until no "+", "-" or ".." is following.
2627 */
2628 for (;;)
2629 {
2630 op = skipwhite(*arg);
2631 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2632 break;
2633 oplen = (*op == '.' ? 2 : 1);
2634
2635 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2636 {
2637 char_u buf[3];
2638
2639 vim_strncpy(buf, op, oplen);
2640 semsg(_(e_white_both), buf);
2641 }
2642
2643 *arg = skipwhite(op + oplen);
2644
2645 // get the second variable
2646 if (compile_expr6(arg, cctx) == FAIL)
2647 return FAIL;
2648
2649 if (*op == '.')
2650 {
2651 if (may_generate_2STRING(-2, cctx) == FAIL
2652 || may_generate_2STRING(-1, cctx) == FAIL)
2653 return FAIL;
2654 generate_instr_drop(cctx, ISN_CONCAT, 1);
2655 }
2656 else
2657 generate_two_op(cctx, op);
2658 }
2659
2660 return OK;
2661}
2662
2663/*
2664 * expr5a == expr5b
2665 * expr5a =~ expr5b
2666 * expr5a != expr5b
2667 * expr5a !~ expr5b
2668 * expr5a > expr5b
2669 * expr5a >= expr5b
2670 * expr5a < expr5b
2671 * expr5a <= expr5b
2672 * expr5a is expr5b
2673 * expr5a isnot expr5b
2674 *
2675 * Produces instructions:
2676 * EVAL expr5a Push result of "expr5a"
2677 * EVAL expr5b Push result of "expr5b"
2678 * COMPARE one of the compare instructions
2679 */
2680 static int
2681compile_expr4(char_u **arg, cctx_T *cctx)
2682{
2683 exptype_T type = EXPR_UNKNOWN;
2684 char_u *p;
2685 int len = 2;
2686 int i;
2687 int type_is = FALSE;
2688
2689 // get the first variable
2690 if (compile_expr5(arg, cctx) == FAIL)
2691 return FAIL;
2692
2693 p = skipwhite(*arg);
2694 switch (p[0])
2695 {
2696 case '=': if (p[1] == '=')
2697 type = EXPR_EQUAL;
2698 else if (p[1] == '~')
2699 type = EXPR_MATCH;
2700 break;
2701 case '!': if (p[1] == '=')
2702 type = EXPR_NEQUAL;
2703 else if (p[1] == '~')
2704 type = EXPR_NOMATCH;
2705 break;
2706 case '>': if (p[1] != '=')
2707 {
2708 type = EXPR_GREATER;
2709 len = 1;
2710 }
2711 else
2712 type = EXPR_GEQUAL;
2713 break;
2714 case '<': if (p[1] != '=')
2715 {
2716 type = EXPR_SMALLER;
2717 len = 1;
2718 }
2719 else
2720 type = EXPR_SEQUAL;
2721 break;
2722 case 'i': if (p[1] == 's')
2723 {
2724 // "is" and "isnot"; but not a prefix of a name
2725 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2726 len = 5;
2727 i = p[len];
2728 if (!isalnum(i) && i != '_')
2729 {
2730 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2731 type_is = TRUE;
2732 }
2733 }
2734 break;
2735 }
2736
2737 /*
2738 * If there is a comparative operator, use it.
2739 */
2740 if (type != EXPR_UNKNOWN)
2741 {
2742 int ic = FALSE; // Default: do not ignore case
2743
2744 if (type_is && (p[len] == '?' || p[len] == '#'))
2745 {
2746 semsg(_(e_invexpr2), *arg);
2747 return FAIL;
2748 }
2749 // extra question mark appended: ignore case
2750 if (p[len] == '?')
2751 {
2752 ic = TRUE;
2753 ++len;
2754 }
2755 // extra '#' appended: match case (ignored)
2756 else if (p[len] == '#')
2757 ++len;
2758 // nothing appended: match case
2759
2760 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2761 {
2762 char_u buf[7];
2763
2764 vim_strncpy(buf, p, len);
2765 semsg(_(e_white_both), buf);
2766 }
2767
2768 // get the second variable
2769 *arg = skipwhite(p + len);
2770 if (compile_expr5(arg, cctx) == FAIL)
2771 return FAIL;
2772
2773 generate_COMPARE(cctx, type, ic);
2774 }
2775
2776 return OK;
2777}
2778
2779/*
2780 * Compile || or &&.
2781 */
2782 static int
2783compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2784{
2785 char_u *p = skipwhite(*arg);
2786 int opchar = *op;
2787
2788 if (p[0] == opchar && p[1] == opchar)
2789 {
2790 garray_T *instr = &cctx->ctx_instr;
2791 garray_T end_ga;
2792
2793 /*
2794 * Repeat until there is no following "||" or "&&"
2795 */
2796 ga_init2(&end_ga, sizeof(int), 10);
2797 while (p[0] == opchar && p[1] == opchar)
2798 {
2799 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2800 semsg(_(e_white_both), op);
2801
2802 if (ga_grow(&end_ga, 1) == FAIL)
2803 {
2804 ga_clear(&end_ga);
2805 return FAIL;
2806 }
2807 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2808 ++end_ga.ga_len;
2809 generate_JUMP(cctx, opchar == '|'
2810 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
2811
2812 // eval the next expression
2813 *arg = skipwhite(p + 2);
2814 if ((opchar == '|' ? compile_expr3(arg, cctx)
2815 : compile_expr4(arg, cctx)) == FAIL)
2816 {
2817 ga_clear(&end_ga);
2818 return FAIL;
2819 }
2820 p = skipwhite(*arg);
2821 }
2822
2823 // Fill in the end label in all jumps.
2824 while (end_ga.ga_len > 0)
2825 {
2826 isn_T *isn;
2827
2828 --end_ga.ga_len;
2829 isn = ((isn_T *)instr->ga_data)
2830 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2831 isn->isn_arg.jump.jump_where = instr->ga_len;
2832 }
2833 ga_clear(&end_ga);
2834 }
2835
2836 return OK;
2837}
2838
2839/*
2840 * expr4a && expr4a && expr4a logical AND
2841 *
2842 * Produces instructions:
2843 * EVAL expr4a Push result of "expr4a"
2844 * JUMP_AND_KEEP_IF_FALSE end
2845 * EVAL expr4b Push result of "expr4b"
2846 * JUMP_AND_KEEP_IF_FALSE end
2847 * EVAL expr4c Push result of "expr4c"
2848 * end:
2849 */
2850 static int
2851compile_expr3(char_u **arg, cctx_T *cctx)
2852{
2853 // get the first variable
2854 if (compile_expr4(arg, cctx) == FAIL)
2855 return FAIL;
2856
2857 // || and && work almost the same
2858 return compile_and_or(arg, cctx, "&&");
2859}
2860
2861/*
2862 * expr3a || expr3b || expr3c logical OR
2863 *
2864 * Produces instructions:
2865 * EVAL expr3a Push result of "expr3a"
2866 * JUMP_AND_KEEP_IF_TRUE end
2867 * EVAL expr3b Push result of "expr3b"
2868 * JUMP_AND_KEEP_IF_TRUE end
2869 * EVAL expr3c Push result of "expr3c"
2870 * end:
2871 */
2872 static int
2873compile_expr2(char_u **arg, cctx_T *cctx)
2874{
2875 // eval the first expression
2876 if (compile_expr3(arg, cctx) == FAIL)
2877 return FAIL;
2878
2879 // || and && work almost the same
2880 return compile_and_or(arg, cctx, "||");
2881}
2882
2883/*
2884 * Toplevel expression: expr2 ? expr1a : expr1b
2885 *
2886 * Produces instructions:
2887 * EVAL expr2 Push result of "expr"
2888 * JUMP_IF_FALSE alt jump if false
2889 * EVAL expr1a
2890 * JUMP_ALWAYS end
2891 * alt: EVAL expr1b
2892 * end:
2893 */
2894 static int
2895compile_expr1(char_u **arg, cctx_T *cctx)
2896{
2897 char_u *p;
2898
2899 // evaluate the first expression
2900 if (compile_expr2(arg, cctx) == FAIL)
2901 return FAIL;
2902
2903 p = skipwhite(*arg);
2904 if (*p == '?')
2905 {
2906 garray_T *instr = &cctx->ctx_instr;
2907 garray_T *stack = &cctx->ctx_type_stack;
2908 int alt_idx = instr->ga_len;
2909 int end_idx;
2910 isn_T *isn;
2911 type_T *type1;
2912 type_T *type2;
2913
2914 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2915 semsg(_(e_white_both), "?");
2916
2917 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
2918
2919 // evaluate the second expression; any type is accepted
2920 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01002921 if (compile_expr1(arg, cctx) == FAIL)
2922 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
2924 // remember the type and drop it
2925 --stack->ga_len;
2926 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
2927
2928 end_idx = instr->ga_len;
2929 generate_JUMP(cctx, JUMP_ALWAYS, 0);
2930
2931 // jump here from JUMP_IF_FALSE
2932 isn = ((isn_T *)instr->ga_data) + alt_idx;
2933 isn->isn_arg.jump.jump_where = instr->ga_len;
2934
2935 // Check for the ":".
2936 p = skipwhite(*arg);
2937 if (*p != ':')
2938 {
2939 emsg(_(e_missing_colon));
2940 return FAIL;
2941 }
2942 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2943 semsg(_(e_white_both), ":");
2944
2945 // evaluate the third expression
2946 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01002947 if (compile_expr1(arg, cctx) == FAIL)
2948 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949
2950 // If the types differ, the result has a more generic type.
2951 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2952 common_type(type1, type2, type2);
2953
2954 // jump here from JUMP_ALWAYS
2955 isn = ((isn_T *)instr->ga_data) + end_idx;
2956 isn->isn_arg.jump.jump_where = instr->ga_len;
2957 }
2958 return OK;
2959}
2960
2961/*
2962 * compile "return [expr]"
2963 */
2964 static char_u *
2965compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
2966{
2967 char_u *p = arg;
2968 garray_T *stack = &cctx->ctx_type_stack;
2969 type_T *stack_type;
2970
2971 if (*p != NUL && *p != '|' && *p != '\n')
2972 {
2973 // compile return argument into instructions
2974 if (compile_expr1(&p, cctx) == FAIL)
2975 return NULL;
2976
2977 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2978 if (set_return_type)
2979 cctx->ctx_ufunc->uf_ret_type = stack_type;
2980 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
2981 == FAIL)
2982 return NULL;
2983 }
2984 else
2985 {
2986 if (set_return_type)
2987 cctx->ctx_ufunc->uf_ret_type = &t_void;
2988 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
2989 {
2990 emsg(_("E1003: Missing return value"));
2991 return NULL;
2992 }
2993
2994 // No argument, return zero.
2995 generate_PUSHNR(cctx, 0);
2996 }
2997
2998 if (generate_instr(cctx, ISN_RETURN) == NULL)
2999 return NULL;
3000
3001 // "return val | endif" is possible
3002 return skipwhite(p);
3003}
3004
3005/*
3006 * Return the length of an assignment operator, or zero if there isn't one.
3007 */
3008 int
3009assignment_len(char_u *p, int *heredoc)
3010{
3011 if (*p == '=')
3012 {
3013 if (p[1] == '<' && p[2] == '<')
3014 {
3015 *heredoc = TRUE;
3016 return 3;
3017 }
3018 return 1;
3019 }
3020 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3021 return 2;
3022 if (STRNCMP(p, "..=", 3) == 0)
3023 return 3;
3024 return 0;
3025}
3026
3027// words that cannot be used as a variable
3028static char *reserved[] = {
3029 "true",
3030 "false",
3031 NULL
3032};
3033
3034/*
3035 * Get a line for "=<<".
3036 * Return a pointer to the line in allocated memory.
3037 * Return NULL for end-of-file or some error.
3038 */
3039 static char_u *
3040heredoc_getline(
3041 int c UNUSED,
3042 void *cookie,
3043 int indent UNUSED,
3044 int do_concat UNUSED)
3045{
3046 cctx_T *cctx = (cctx_T *)cookie;
3047
3048 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3049 NULL;
3050 ++cctx->ctx_lnum;
3051 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3052 [cctx->ctx_lnum]);
3053}
3054
3055/*
3056 * compile "let var [= expr]", "const var = expr" and "var = expr"
3057 * "arg" points to "var".
3058 */
3059 static char_u *
3060compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3061{
3062 char_u *p;
3063 char_u *ret = NULL;
3064 int var_count = 0;
3065 int semicolon = 0;
3066 size_t varlen;
3067 garray_T *instr = &cctx->ctx_instr;
3068 int idx = -1;
3069 char_u *op;
3070 int option = FALSE;
3071 int opt_type;
3072 int opt_flags = 0;
3073 int global = FALSE;
3074 int script = FALSE;
3075 int oplen = 0;
3076 int heredoc = FALSE;
3077 type_T *type;
3078 lvar_T *lvar;
3079 char_u *name;
3080 char_u *sp;
3081 int has_type = FALSE;
3082 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3083 int instr_count = -1;
3084
3085 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3086 if (p == NULL)
3087 return NULL;
3088 if (var_count > 0)
3089 {
3090 // TODO: let [var, var] = list
3091 emsg("Cannot handle a list yet");
3092 return NULL;
3093 }
3094
3095 varlen = p - arg;
3096 name = vim_strnsave(arg, (int)varlen);
3097 if (name == NULL)
3098 return NULL;
3099
3100 if (*arg == '&')
3101 {
3102 int cc;
3103 long numval;
3104 char_u *stringval = NULL;
3105
3106 option = TRUE;
3107 if (cmdidx == CMD_const)
3108 {
3109 emsg(_(e_const_option));
3110 return NULL;
3111 }
3112 if (is_decl)
3113 {
3114 semsg(_("E1052: Cannot declare an option: %s"), arg);
3115 goto theend;
3116 }
3117 p = arg;
3118 p = find_option_end(&p, &opt_flags);
3119 if (p == NULL)
3120 {
3121 emsg(_(e_letunexp));
3122 return NULL;
3123 }
3124 cc = *p;
3125 *p = NUL;
3126 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3127 *p = cc;
3128 if (opt_type == -3)
3129 {
3130 semsg(_(e_unknown_option), *arg);
3131 return NULL;
3132 }
3133 if (opt_type == -2 || opt_type == 0)
3134 type = &t_string;
3135 else
3136 type = &t_number; // both number and boolean option
3137 }
3138 else if (STRNCMP(arg, "g:", 2) == 0)
3139 {
3140 global = TRUE;
3141 if (is_decl)
3142 {
3143 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3144 goto theend;
3145 }
3146 }
3147 else
3148 {
3149 for (idx = 0; reserved[idx] != NULL; ++idx)
3150 if (STRCMP(reserved[idx], name) == 0)
3151 {
3152 semsg(_("E1034: Cannot use reserved name %s"), name);
3153 goto theend;
3154 }
3155
3156 idx = lookup_local(arg, varlen, cctx);
3157 if (idx >= 0)
3158 {
3159 if (is_decl)
3160 {
3161 semsg(_("E1017: Variable already declared: %s"), name);
3162 goto theend;
3163 }
3164 else
3165 {
3166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3167 if (lvar->lv_const)
3168 {
3169 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3170 goto theend;
3171 }
3172 }
3173 }
3174 else if (lookup_script(arg, varlen) == OK)
3175 {
3176 script = TRUE;
3177 if (is_decl)
3178 {
3179 semsg(_("E1054: Variable already declared in the script: %s"),
3180 name);
3181 goto theend;
3182 }
3183 }
3184 }
3185
3186 if (!option)
3187 {
3188 if (is_decl && *p == ':')
3189 {
3190 // parse optional type: "let var: type = expr"
3191 p = skipwhite(p + 1);
3192 type = parse_type(&p, cctx->ctx_type_list);
3193 if (type == NULL)
3194 goto theend;
3195 has_type = TRUE;
3196 }
3197 else if (idx < 0)
3198 {
3199 // global and new local default to "any" type
3200 type = &t_any;
3201 }
3202 else
3203 {
3204 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3205 type = lvar->lv_type;
3206 }
3207 }
3208
3209 sp = p;
3210 p = skipwhite(p);
3211 op = p;
3212 oplen = assignment_len(p, &heredoc);
3213 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3214 {
3215 char_u buf[4];
3216
3217 vim_strncpy(buf, op, oplen);
3218 semsg(_(e_white_both), buf);
3219 }
3220
3221 if (oplen == 3 && !heredoc && !global && type->tt_type != VAR_STRING
3222 && type->tt_type != VAR_UNKNOWN)
3223 {
3224 emsg("E1019: Can only concatenate to string");
3225 goto theend;
3226 }
3227
3228 // +=, /=, etc. require an existing variable
3229 if (idx < 0 && !global && !option)
3230 {
3231 if (oplen > 1 && !heredoc)
3232 {
3233 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3234 name);
3235 goto theend;
3236 }
3237
3238 // new local variable
3239 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3240 if (idx < 0)
3241 goto theend;
3242 }
3243
3244 if (heredoc)
3245 {
3246 list_T *l;
3247 listitem_T *li;
3248
3249 // [let] varname =<< [trim] {end}
3250 eap->getline = heredoc_getline;
3251 eap->cookie = cctx;
3252 l = heredoc_get(eap, op + 3);
3253
3254 // Push each line and the create the list.
3255 for (li = l->lv_first; li != NULL; li = li->li_next)
3256 {
3257 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3258 li->li_tv.vval.v_string = NULL;
3259 }
3260 generate_NEWLIST(cctx, l->lv_len);
3261 type = &t_list_string;
3262 list_free(l);
3263 p += STRLEN(p);
3264 }
3265 else if (oplen > 0)
3266 {
3267 // for "+=", "*=", "..=" etc. first load the current value
3268 if (*op != '=')
3269 {
3270 if (option)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003271 // TODO: check the option exists
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3273 else if (global)
3274 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3275 else
3276 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3277 }
3278
3279 // compile the expression
3280 instr_count = instr->ga_len;
3281 p = skipwhite(p + oplen);
3282 if (compile_expr1(&p, cctx) == FAIL)
3283 goto theend;
3284
3285 if (idx >= 0 && (is_decl || !has_type))
3286 {
3287 garray_T *stack = &cctx->ctx_type_stack;
3288 type_T *stacktype =
3289 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3290
3291 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3292 if (!has_type)
3293 {
3294 if (stacktype->tt_type == VAR_VOID)
3295 {
3296 emsg(_("E1031: Cannot use void value"));
3297 goto theend;
3298 }
3299 else
3300 lvar->lv_type = stacktype;
3301 }
3302 else
3303 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3304 goto theend;
3305 }
3306 }
3307 else if (cmdidx == CMD_const)
3308 {
3309 emsg(_("E1021: const requires a value"));
3310 goto theend;
3311 }
3312 else if (!has_type || option)
3313 {
3314 emsg(_("E1022: type or initialization required"));
3315 goto theend;
3316 }
3317 else
3318 {
3319 // variables are always initialized
3320 // TODO: support more types
3321 if (ga_grow(instr, 1) == FAIL)
3322 goto theend;
3323 if (type->tt_type == VAR_STRING)
3324 generate_PUSHS(cctx, vim_strsave((char_u *)""));
3325 else
3326 generate_PUSHNR(cctx, 0);
3327 }
3328
3329 if (oplen > 0 && *op != '=')
3330 {
3331 type_T *expected = &t_number;
3332 garray_T *stack = &cctx->ctx_type_stack;
3333 type_T *stacktype;
3334
3335 // TODO: if type is known use float or any operation
3336
3337 if (*op == '.')
3338 expected = &t_string;
3339 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3340 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3341 goto theend;
3342
3343 if (*op == '.')
3344 generate_instr_drop(cctx, ISN_CONCAT, 1);
3345 else
3346 {
3347 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3348
3349 if (isn == NULL)
3350 goto theend;
3351 switch (*op)
3352 {
3353 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3354 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3355 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3356 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3357 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3358 }
3359 }
3360 }
3361
3362 if (option)
3363 generate_STOREOPT(cctx, name + 1, opt_flags);
3364 else if (global)
3365 generate_STORE(cctx, ISN_STOREG, 0, name + 2);
3366 else if (script)
3367 {
3368 idx = get_script_item_idx(current_sctx.sc_sid, name, TRUE);
3369 // TODO: specific type
3370 generate_SCRIPT(cctx, ISN_STORESCRIPT,
3371 current_sctx.sc_sid, idx, &t_any);
3372 }
3373 else
3374 {
3375 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3376
3377 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
3378 // ISN_STORENR
3379 if (instr->ga_len == instr_count + 1 && isn->isn_type == ISN_PUSHNR)
3380 {
3381 varnumber_T val = isn->isn_arg.number;
3382 garray_T *stack = &cctx->ctx_type_stack;
3383
3384 isn->isn_type = ISN_STORENR;
3385 isn->isn_arg.storenr.str_idx = idx;
3386 isn->isn_arg.storenr.str_val = val;
3387 if (stack->ga_len > 0)
3388 --stack->ga_len;
3389 }
3390 else
3391 generate_STORE(cctx, ISN_STORE, idx, NULL);
3392 }
3393 ret = p;
3394
3395theend:
3396 vim_free(name);
3397 return ret;
3398}
3399
3400/*
3401 * Compile an :import command.
3402 */
3403 static char_u *
3404compile_import(char_u *arg, cctx_T *cctx)
3405{
3406 return handle_import(arg, &cctx->ctx_imports, 0);
3407}
3408
3409/*
3410 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3411 */
3412 static int
3413compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3414{
3415 garray_T *instr = &cctx->ctx_instr;
3416 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3417
3418 if (endlabel == NULL)
3419 return FAIL;
3420 endlabel->el_next = *el;
3421 *el = endlabel;
3422 endlabel->el_end_label = instr->ga_len;
3423
3424 generate_JUMP(cctx, when, 0);
3425 return OK;
3426}
3427
3428 static void
3429compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3430{
3431 garray_T *instr = &cctx->ctx_instr;
3432
3433 while (*el != NULL)
3434 {
3435 endlabel_T *cur = (*el);
3436 isn_T *isn;
3437
3438 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3439 isn->isn_arg.jump.jump_where = instr->ga_len;
3440 *el = cur->el_next;
3441 vim_free(cur);
3442 }
3443}
3444
3445/*
3446 * Create a new scope and set up the generic items.
3447 */
3448 static scope_T *
3449new_scope(cctx_T *cctx, scopetype_T type)
3450{
3451 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3452
3453 if (scope == NULL)
3454 return NULL;
3455 scope->se_outer = cctx->ctx_scope;
3456 cctx->ctx_scope = scope;
3457 scope->se_type = type;
3458 scope->se_local_count = cctx->ctx_locals.ga_len;
3459 return scope;
3460}
3461
3462/*
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003463 * Evaluate an expression that is a constant: has(arg)
3464 * Return FAIL if the expression is not a constant.
3465 */
3466 static int
3467evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3468{
3469 typval_T argvars[2];
3470
3471 if (STRNCMP("has(", *arg, 4) != 0)
3472 return FAIL;
3473 *arg = skipwhite(*arg + 4);
3474
3475 if (**arg == '"')
3476 {
3477 if (get_string_tv(arg, tv, TRUE) == FAIL)
3478 return FAIL;
3479 }
3480 else if (**arg == '\'')
3481 {
3482 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3483 return FAIL;
3484 }
3485 else
3486 return FAIL;
3487
3488 *arg = skipwhite(*arg);
3489 if (**arg != ')')
3490 return FAIL;
3491 *arg = skipwhite(*arg + 1);
3492
3493 argvars[0] = *tv;
3494 argvars[1].v_type = VAR_UNKNOWN;
3495 tv->v_type = VAR_NUMBER;
3496 tv->vval.v_number = 0;
3497 f_has(argvars, tv);
3498 clear_tv(&argvars[0]);
3499
3500 return OK;
3501}
3502
3503static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3504
3505/*
3506 * Compile constant || or &&.
3507 */
3508 static int
3509evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3510{
3511 char_u *p = skipwhite(*arg);
3512 int opchar = *op;
3513
3514 if (p[0] == opchar && p[1] == opchar)
3515 {
3516 int val = tv2bool(tv);
3517
3518 /*
3519 * Repeat until there is no following "||" or "&&"
3520 */
3521 while (p[0] == opchar && p[1] == opchar)
3522 {
3523 typval_T tv2;
3524
3525 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3526 return FAIL;
3527
3528 // eval the next expression
3529 *arg = skipwhite(p + 2);
3530 tv2.v_type = VAR_UNKNOWN;
3531 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3532 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3533 {
3534 clear_tv(&tv2);
3535 return FAIL;
3536 }
3537 if ((opchar == '&') == val)
3538 {
3539 // false || tv2 or true && tv2: use tv2
3540 clear_tv(tv);
3541 *tv = tv2;
3542 val = tv2bool(tv);
3543 }
3544 else
3545 clear_tv(&tv2);
3546 p = skipwhite(*arg);
3547 }
3548 }
3549
3550 return OK;
3551}
3552
3553/*
3554 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3555 * Return FAIL if the expression is not a constant.
3556 */
3557 static int
3558evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3559{
3560 // evaluate the first expression
3561 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3562 return FAIL;
3563
3564 // || and && work almost the same
3565 return evaluate_const_and_or(arg, cctx, "&&", tv);
3566}
3567
3568/*
3569 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3570 * Return FAIL if the expression is not a constant.
3571 */
3572 static int
3573evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3574{
3575 // evaluate the first expression
3576 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3577 return FAIL;
3578
3579 // || and && work almost the same
3580 return evaluate_const_and_or(arg, cctx, "||", tv);
3581}
3582
3583/*
3584 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3585 * E.g. for "has('feature')".
3586 * This does not produce error messages. "tv" should be cleared afterwards.
3587 * Return FAIL if the expression is not a constant.
3588 */
3589 static int
3590evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3591{
3592 char_u *p;
3593
3594 // evaluate the first expression
3595 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3596 return FAIL;
3597
3598 p = skipwhite(*arg);
3599 if (*p == '?')
3600 {
3601 int val = tv2bool(tv);
3602 typval_T tv2;
3603
3604 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3605 return FAIL;
3606
3607 // evaluate the second expression; any type is accepted
3608 clear_tv(tv);
3609 *arg = skipwhite(p + 1);
3610 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3611 return FAIL;
3612
3613 // Check for the ":".
3614 p = skipwhite(*arg);
3615 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3616 return FAIL;
3617
3618 // evaluate the third expression
3619 *arg = skipwhite(p + 1);
3620 tv2.v_type = VAR_UNKNOWN;
3621 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3622 {
3623 clear_tv(&tv2);
3624 return FAIL;
3625 }
3626 if (val)
3627 {
3628 // use the expr after "?"
3629 clear_tv(&tv2);
3630 }
3631 else
3632 {
3633 // use the expr after ":"
3634 clear_tv(tv);
3635 *tv = tv2;
3636 }
3637 }
3638 return OK;
3639}
3640
3641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 * compile "if expr"
3643 *
3644 * "if expr" Produces instructions:
3645 * EVAL expr Push result of "expr"
3646 * JUMP_IF_FALSE end
3647 * ... body ...
3648 * end:
3649 *
3650 * "if expr | else" Produces instructions:
3651 * EVAL expr Push result of "expr"
3652 * JUMP_IF_FALSE else
3653 * ... body ...
3654 * JUMP_ALWAYS end
3655 * else:
3656 * ... body ...
3657 * end:
3658 *
3659 * "if expr1 | elseif expr2 | else" Produces instructions:
3660 * EVAL expr Push result of "expr"
3661 * JUMP_IF_FALSE elseif
3662 * ... body ...
3663 * JUMP_ALWAYS end
3664 * elseif:
3665 * EVAL expr Push result of "expr"
3666 * JUMP_IF_FALSE else
3667 * ... body ...
3668 * JUMP_ALWAYS end
3669 * else:
3670 * ... body ...
3671 * end:
3672 */
3673 static char_u *
3674compile_if(char_u *arg, cctx_T *cctx)
3675{
3676 char_u *p = arg;
3677 garray_T *instr = &cctx->ctx_instr;
3678 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003679 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003681 // compile "expr"; if we know it evaluates to FALSE skip the block
3682 tv.v_type = VAR_UNKNOWN;
3683 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3684 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3685 else
3686 cctx->ctx_skip = MAYBE;
3687 clear_tv(&tv);
3688 if (cctx->ctx_skip == MAYBE)
3689 {
3690 p = arg;
3691 if (compile_expr1(&p, cctx) == FAIL)
3692 return NULL;
3693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694
3695 scope = new_scope(cctx, IF_SCOPE);
3696 if (scope == NULL)
3697 return NULL;
3698
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003699 if (cctx->ctx_skip == MAYBE)
3700 {
3701 // "where" is set when ":elseif", "else" or ":endif" is found
3702 scope->se_u.se_if.is_if_label = instr->ga_len;
3703 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3704 }
3705 else
3706 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707
3708 return p;
3709}
3710
3711 static char_u *
3712compile_elseif(char_u *arg, cctx_T *cctx)
3713{
3714 char_u *p = arg;
3715 garray_T *instr = &cctx->ctx_instr;
3716 isn_T *isn;
3717 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003718 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719
3720 if (scope == NULL || scope->se_type != IF_SCOPE)
3721 {
3722 emsg(_(e_elseif_without_if));
3723 return NULL;
3724 }
3725 cctx->ctx_locals.ga_len = scope->se_local_count;
3726
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003727 if (cctx->ctx_skip != TRUE)
3728 {
3729 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003731 return NULL;
3732 // previous "if" or "elseif" jumps here
3733 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3734 isn->isn_arg.jump.jump_where = instr->ga_len;
3735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003737 // compile "expr"; if we know it evaluates to FALSE skip the block
3738 tv.v_type = VAR_UNKNOWN;
3739 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3740 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3741 else
3742 cctx->ctx_skip = MAYBE;
3743 clear_tv(&tv);
3744 if (cctx->ctx_skip == MAYBE)
3745 {
3746 p = arg;
3747 if (compile_expr1(&p, cctx) == FAIL)
3748 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003750 // "where" is set when ":elseif", "else" or ":endif" is found
3751 scope->se_u.se_if.is_if_label = instr->ga_len;
3752 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3753 }
3754 else
3755 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756
3757 return p;
3758}
3759
3760 static char_u *
3761compile_else(char_u *arg, cctx_T *cctx)
3762{
3763 char_u *p = arg;
3764 garray_T *instr = &cctx->ctx_instr;
3765 isn_T *isn;
3766 scope_T *scope = cctx->ctx_scope;
3767
3768 if (scope == NULL || scope->se_type != IF_SCOPE)
3769 {
3770 emsg(_(e_else_without_if));
3771 return NULL;
3772 }
3773 cctx->ctx_locals.ga_len = scope->se_local_count;
3774
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003775 // jump from previous block to the end, unless the else block is empty
3776 if (cctx->ctx_skip == MAYBE)
3777 {
3778 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003780 return NULL;
3781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003783 if (cctx->ctx_skip != TRUE)
3784 {
3785 if (scope->se_u.se_if.is_if_label >= 0)
3786 {
3787 // previous "if" or "elseif" jumps here
3788 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3789 isn->isn_arg.jump.jump_where = instr->ga_len;
3790 }
3791 }
3792
3793 if (cctx->ctx_skip != MAYBE)
3794 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795
3796 return p;
3797}
3798
3799 static char_u *
3800compile_endif(char_u *arg, cctx_T *cctx)
3801{
3802 scope_T *scope = cctx->ctx_scope;
3803 ifscope_T *ifscope;
3804 garray_T *instr = &cctx->ctx_instr;
3805 isn_T *isn;
3806
3807 if (scope == NULL || scope->se_type != IF_SCOPE)
3808 {
3809 emsg(_(e_endif_without_if));
3810 return NULL;
3811 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003812 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 cctx->ctx_scope = scope->se_outer;
3814 cctx->ctx_locals.ga_len = scope->se_local_count;
3815
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003816 if (scope->se_u.se_if.is_if_label >= 0)
3817 {
3818 // previous "if" or "elseif" jumps here
3819 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3820 isn->isn_arg.jump.jump_where = instr->ga_len;
3821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 // Fill in the "end" label in jumps at the end of the blocks.
3823 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003824 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825
3826 vim_free(scope);
3827 return arg;
3828}
3829
3830/*
3831 * compile "for var in expr"
3832 *
3833 * Produces instructions:
3834 * PUSHNR -1
3835 * STORE loop-idx Set index to -1
3836 * EVAL expr Push result of "expr"
3837 * top: FOR loop-idx, end Increment index, use list on bottom of stack
3838 * - if beyond end, jump to "end"
3839 * - otherwise get item from list and push it
3840 * STORE var Store item in "var"
3841 * ... body ...
3842 * JUMP top Jump back to repeat
3843 * end: DROP Drop the result of "expr"
3844 *
3845 */
3846 static char_u *
3847compile_for(char_u *arg, cctx_T *cctx)
3848{
3849 char_u *p;
3850 size_t varlen;
3851 garray_T *instr = &cctx->ctx_instr;
3852 garray_T *stack = &cctx->ctx_type_stack;
3853 scope_T *scope;
3854 int loop_idx; // index of loop iteration variable
3855 int var_idx; // index of "var"
3856 type_T *vartype;
3857
3858 // TODO: list of variables: "for [key, value] in dict"
3859 // parse "var"
3860 for (p = arg; eval_isnamec1(*p); ++p)
3861 ;
3862 varlen = p - arg;
3863 var_idx = lookup_local(arg, varlen, cctx);
3864 if (var_idx >= 0)
3865 {
3866 semsg(_("E1023: variable already defined: %s"), arg);
3867 return NULL;
3868 }
3869
3870 // consume "in"
3871 p = skipwhite(p);
3872 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
3873 {
3874 emsg(_(e_missing_in));
3875 return NULL;
3876 }
3877 p = skipwhite(p + 2);
3878
3879
3880 scope = new_scope(cctx, FOR_SCOPE);
3881 if (scope == NULL)
3882 return NULL;
3883
3884 // Reserve a variable to store the loop iteration counter.
3885 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
3886 if (loop_idx < 0)
3887 return NULL;
3888
3889 // Reserve a variable to store "var"
3890 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
3891 if (var_idx < 0)
3892 return NULL;
3893
3894 generate_STORENR(cctx, loop_idx, -1);
3895
3896 // compile "expr", it remains on the stack until "endfor"
3897 arg = p;
3898 if (compile_expr1(&arg, cctx) == FAIL)
3899 return NULL;
3900
3901 // now we know the type of "var"
3902 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3903 if (vartype->tt_type != VAR_LIST)
3904 {
3905 emsg(_("E1024: need a List to iterate over"));
3906 return NULL;
3907 }
3908 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
3909 {
3910 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
3911
3912 lvar->lv_type = vartype->tt_member;
3913 }
3914
3915 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003916 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917
3918 generate_FOR(cctx, loop_idx);
3919 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
3920
3921 return arg;
3922}
3923
3924/*
3925 * compile "endfor"
3926 */
3927 static char_u *
3928compile_endfor(char_u *arg, cctx_T *cctx)
3929{
3930 garray_T *instr = &cctx->ctx_instr;
3931 scope_T *scope = cctx->ctx_scope;
3932 forscope_T *forscope;
3933 isn_T *isn;
3934
3935 if (scope == NULL || scope->se_type != FOR_SCOPE)
3936 {
3937 emsg(_(e_for));
3938 return NULL;
3939 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003940 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 cctx->ctx_scope = scope->se_outer;
3942 cctx->ctx_locals.ga_len = scope->se_local_count;
3943
3944 // At end of ":for" scope jump back to the FOR instruction.
3945 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
3946
3947 // Fill in the "end" label in the FOR statement so it can jump here
3948 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
3949 isn->isn_arg.forloop.for_end = instr->ga_len;
3950
3951 // Fill in the "end" label any BREAK statements
3952 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
3953
3954 // Below the ":for" scope drop the "expr" list from the stack.
3955 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
3956 return NULL;
3957
3958 vim_free(scope);
3959
3960 return arg;
3961}
3962
3963/*
3964 * compile "while expr"
3965 *
3966 * Produces instructions:
3967 * top: EVAL expr Push result of "expr"
3968 * JUMP_IF_FALSE end jump if false
3969 * ... body ...
3970 * JUMP top Jump back to repeat
3971 * end:
3972 *
3973 */
3974 static char_u *
3975compile_while(char_u *arg, cctx_T *cctx)
3976{
3977 char_u *p = arg;
3978 garray_T *instr = &cctx->ctx_instr;
3979 scope_T *scope;
3980
3981 scope = new_scope(cctx, WHILE_SCOPE);
3982 if (scope == NULL)
3983 return NULL;
3984
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003985 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
3987 // compile "expr"
3988 if (compile_expr1(&p, cctx) == FAIL)
3989 return NULL;
3990
3991 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003992 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 JUMP_IF_FALSE, cctx) == FAIL)
3994 return FAIL;
3995
3996 return p;
3997}
3998
3999/*
4000 * compile "endwhile"
4001 */
4002 static char_u *
4003compile_endwhile(char_u *arg, cctx_T *cctx)
4004{
4005 scope_T *scope = cctx->ctx_scope;
4006
4007 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4008 {
4009 emsg(_(e_while));
4010 return NULL;
4011 }
4012 cctx->ctx_scope = scope->se_outer;
4013 cctx->ctx_locals.ga_len = scope->se_local_count;
4014
4015 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004016 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
4018 // Fill in the "end" label in the WHILE statement so it can jump here.
4019 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004020 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
4022 vim_free(scope);
4023
4024 return arg;
4025}
4026
4027/*
4028 * compile "continue"
4029 */
4030 static char_u *
4031compile_continue(char_u *arg, cctx_T *cctx)
4032{
4033 scope_T *scope = cctx->ctx_scope;
4034
4035 for (;;)
4036 {
4037 if (scope == NULL)
4038 {
4039 emsg(_(e_continue));
4040 return NULL;
4041 }
4042 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4043 break;
4044 scope = scope->se_outer;
4045 }
4046
4047 // Jump back to the FOR or WHILE instruction.
4048 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004049 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4050 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return arg;
4052}
4053
4054/*
4055 * compile "break"
4056 */
4057 static char_u *
4058compile_break(char_u *arg, cctx_T *cctx)
4059{
4060 scope_T *scope = cctx->ctx_scope;
4061 endlabel_T **el;
4062
4063 for (;;)
4064 {
4065 if (scope == NULL)
4066 {
4067 emsg(_(e_break));
4068 return NULL;
4069 }
4070 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4071 break;
4072 scope = scope->se_outer;
4073 }
4074
4075 // Jump to the end of the FOR or WHILE loop.
4076 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004077 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004079 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4081 return FAIL;
4082
4083 return arg;
4084}
4085
4086/*
4087 * compile "{" start of block
4088 */
4089 static char_u *
4090compile_block(char_u *arg, cctx_T *cctx)
4091{
4092 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4093 return NULL;
4094 return skipwhite(arg + 1);
4095}
4096
4097/*
4098 * compile end of block: drop one scope
4099 */
4100 static void
4101compile_endblock(cctx_T *cctx)
4102{
4103 scope_T *scope = cctx->ctx_scope;
4104
4105 cctx->ctx_scope = scope->se_outer;
4106 cctx->ctx_locals.ga_len = scope->se_local_count;
4107 vim_free(scope);
4108}
4109
4110/*
4111 * compile "try"
4112 * Creates a new scope for the try-endtry, pointing to the first catch and
4113 * finally.
4114 * Creates another scope for the "try" block itself.
4115 * TRY instruction sets up exception handling at runtime.
4116 *
4117 * "try"
4118 * TRY -> catch1, -> finally push trystack entry
4119 * ... try block
4120 * "throw {exception}"
4121 * EVAL {exception}
4122 * THROW create exception
4123 * ... try block
4124 * " catch {expr}"
4125 * JUMP -> finally
4126 * catch1: PUSH exeception
4127 * EVAL {expr}
4128 * MATCH
4129 * JUMP nomatch -> catch2
4130 * CATCH remove exception
4131 * ... catch block
4132 * " catch"
4133 * JUMP -> finally
4134 * catch2: CATCH remove exception
4135 * ... catch block
4136 * " finally"
4137 * finally:
4138 * ... finally block
4139 * " endtry"
4140 * ENDTRY pop trystack entry, may rethrow
4141 */
4142 static char_u *
4143compile_try(char_u *arg, cctx_T *cctx)
4144{
4145 garray_T *instr = &cctx->ctx_instr;
4146 scope_T *try_scope;
4147 scope_T *scope;
4148
4149 // scope that holds the jumps that go to catch/finally/endtry
4150 try_scope = new_scope(cctx, TRY_SCOPE);
4151 if (try_scope == NULL)
4152 return NULL;
4153
4154 // "catch" is set when the first ":catch" is found.
4155 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004156 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 if (generate_instr(cctx, ISN_TRY) == NULL)
4158 return NULL;
4159
4160 // scope for the try block itself
4161 scope = new_scope(cctx, BLOCK_SCOPE);
4162 if (scope == NULL)
4163 return NULL;
4164
4165 return arg;
4166}
4167
4168/*
4169 * compile "catch {expr}"
4170 */
4171 static char_u *
4172compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4173{
4174 scope_T *scope = cctx->ctx_scope;
4175 garray_T *instr = &cctx->ctx_instr;
4176 char_u *p;
4177 isn_T *isn;
4178
4179 // end block scope from :try or :catch
4180 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4181 compile_endblock(cctx);
4182 scope = cctx->ctx_scope;
4183
4184 // Error if not in a :try scope
4185 if (scope == NULL || scope->se_type != TRY_SCOPE)
4186 {
4187 emsg(_(e_catch));
4188 return NULL;
4189 }
4190
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004191 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 {
4193 emsg(_("E1033: catch unreachable after catch-all"));
4194 return NULL;
4195 }
4196
4197 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004198 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 JUMP_ALWAYS, cctx) == FAIL)
4200 return NULL;
4201
4202 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004203 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 if (isn->isn_arg.try.try_catch == 0)
4205 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004206 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 {
4208 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004209 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 isn->isn_arg.jump.jump_where = instr->ga_len;
4211 }
4212
4213 p = skipwhite(arg);
4214 if (ends_excmd(*p))
4215 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004216 scope->se_u.se_try.ts_caught_all = TRUE;
4217 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 }
4219 else
4220 {
4221 // Push v:exception, push {expr} and MATCH
4222 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4223
4224 if (compile_expr1(&p, cctx) == FAIL)
4225 return NULL;
4226
4227 // TODO: check for strings?
4228 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4229 return NULL;
4230
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004231 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4233 return NULL;
4234 }
4235
4236 if (generate_instr(cctx, ISN_CATCH) == NULL)
4237 return NULL;
4238
4239 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4240 return NULL;
4241 return p;
4242}
4243
4244 static char_u *
4245compile_finally(char_u *arg, cctx_T *cctx)
4246{
4247 scope_T *scope = cctx->ctx_scope;
4248 garray_T *instr = &cctx->ctx_instr;
4249 isn_T *isn;
4250
4251 // end block scope from :try or :catch
4252 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4253 compile_endblock(cctx);
4254 scope = cctx->ctx_scope;
4255
4256 // Error if not in a :try scope
4257 if (scope == NULL || scope->se_type != TRY_SCOPE)
4258 {
4259 emsg(_(e_finally));
4260 return NULL;
4261 }
4262
4263 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004264 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 if (isn->isn_arg.try.try_finally != 0)
4266 {
4267 emsg(_(e_finally_dup));
4268 return NULL;
4269 }
4270
4271 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004272 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004274 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 {
4276 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004277 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 isn->isn_arg.jump.jump_where = instr->ga_len;
4279 }
4280
4281 isn->isn_arg.try.try_finally = instr->ga_len;
4282 // TODO: set index in ts_finally_label jumps
4283
4284 return arg;
4285}
4286
4287 static char_u *
4288compile_endtry(char_u *arg, cctx_T *cctx)
4289{
4290 scope_T *scope = cctx->ctx_scope;
4291 garray_T *instr = &cctx->ctx_instr;
4292 isn_T *isn;
4293
4294 // end block scope from :catch or :finally
4295 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4296 compile_endblock(cctx);
4297 scope = cctx->ctx_scope;
4298
4299 // Error if not in a :try scope
4300 if (scope == NULL || scope->se_type != TRY_SCOPE)
4301 {
4302 if (scope == NULL)
4303 emsg(_(e_no_endtry));
4304 else if (scope->se_type == WHILE_SCOPE)
4305 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004306 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 emsg(_(e_endfor));
4308 else
4309 emsg(_(e_endif));
4310 return NULL;
4311 }
4312
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004313 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4315 {
4316 emsg(_("E1032: missing :catch or :finally"));
4317 return NULL;
4318 }
4319
4320 // Fill in the "end" label in jumps at the end of the blocks, if not done
4321 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004322 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323
4324 // End :catch or :finally scope: set value in ISN_TRY instruction
4325 if (isn->isn_arg.try.try_finally == 0)
4326 isn->isn_arg.try.try_finally = instr->ga_len;
4327 compile_endblock(cctx);
4328
4329 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4330 return NULL;
4331 return arg;
4332}
4333
4334/*
4335 * compile "throw {expr}"
4336 */
4337 static char_u *
4338compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4339{
4340 char_u *p = skipwhite(arg);
4341
4342 if (ends_excmd(*p))
4343 {
4344 emsg(_(e_argreq));
4345 return NULL;
4346 }
4347 if (compile_expr1(&p, cctx) == FAIL)
4348 return NULL;
4349 if (may_generate_2STRING(-1, cctx) == FAIL)
4350 return NULL;
4351 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4352 return NULL;
4353
4354 return p;
4355}
4356
4357/*
4358 * compile "echo expr"
4359 */
4360 static char_u *
4361compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4362{
4363 char_u *p = arg;
4364 int count = 0;
4365
4366 // for ()
4367 {
4368 if (compile_expr1(&p, cctx) == FAIL)
4369 return NULL;
4370 ++count;
4371 }
4372
4373 generate_ECHO(cctx, with_white, count);
4374
4375 return p;
4376}
4377
4378/*
4379 * After ex_function() has collected all the function lines: parse and compile
4380 * the lines into instructions.
4381 * Adds the function to "def_functions".
4382 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4383 * return statement (used for lambda).
4384 */
4385 void
4386compile_def_function(ufunc_T *ufunc, int set_return_type)
4387{
4388 dfunc_T *dfunc;
4389 char_u *line = NULL;
4390 char_u *p;
4391 exarg_T ea;
4392 char *errormsg = NULL; // error message
4393 int had_return = FALSE;
4394 cctx_T cctx;
4395 garray_T *instr;
4396 int called_emsg_before = called_emsg;
4397 int ret = FAIL;
4398 sctx_T save_current_sctx = current_sctx;
4399
4400 if (ufunc->uf_dfunc_idx >= 0)
4401 {
4402 // redefining a function that was compiled before
4403 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4404 dfunc->df_deleted = FALSE;
4405 }
4406 else
4407 {
4408 // Add the function to "def_functions".
4409 if (ga_grow(&def_functions, 1) == FAIL)
4410 return;
4411 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4412 vim_memset(dfunc, 0, sizeof(dfunc_T));
4413 dfunc->df_idx = def_functions.ga_len;
4414 ufunc->uf_dfunc_idx = dfunc->df_idx;
4415 dfunc->df_ufunc = ufunc;
4416 ++def_functions.ga_len;
4417 }
4418
4419 vim_memset(&cctx, 0, sizeof(cctx));
4420 cctx.ctx_ufunc = ufunc;
4421 cctx.ctx_lnum = -1;
4422 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4423 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4424 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4425 cctx.ctx_type_list = &ufunc->uf_type_list;
4426 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4427 instr = &cctx.ctx_instr;
4428
4429 // Most modern script version.
4430 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4431
4432 for (;;)
4433 {
4434 if (line != NULL && *line == '|')
4435 // the line continues after a '|'
4436 ++line;
4437 else if (line != NULL && *line != NUL)
4438 {
4439 semsg(_("E488: Trailing characters: %s"), line);
4440 goto erret;
4441 }
4442 else
4443 {
4444 do
4445 {
4446 ++cctx.ctx_lnum;
4447 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4448 break;
4449 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4450 } while (line == NULL);
4451 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4452 break;
4453 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4454 }
4455
4456 had_return = FALSE;
4457 vim_memset(&ea, 0, sizeof(ea));
4458 ea.cmdlinep = &line;
4459 ea.cmd = skipwhite(line);
4460
4461 // "}" ends a block scope
4462 if (*ea.cmd == '}')
4463 {
4464 scopetype_T stype = cctx.ctx_scope == NULL
4465 ? NO_SCOPE : cctx.ctx_scope->se_type;
4466
4467 if (stype == BLOCK_SCOPE)
4468 {
4469 compile_endblock(&cctx);
4470 line = ea.cmd;
4471 }
4472 else
4473 {
4474 emsg("E1025: using } outside of a block scope");
4475 goto erret;
4476 }
4477 if (line != NULL)
4478 line = skipwhite(ea.cmd + 1);
4479 continue;
4480 }
4481
4482 // "{" starts a block scope
4483 if (*ea.cmd == '{')
4484 {
4485 line = compile_block(ea.cmd, &cctx);
4486 continue;
4487 }
4488
4489 /*
4490 * COMMAND MODIFIERS
4491 */
4492 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4493 {
4494 if (errormsg != NULL)
4495 goto erret;
4496 // empty line or comment
4497 line = (char_u *)"";
4498 continue;
4499 }
4500
4501 // Skip ":call" to get to the function name.
4502 if (checkforcmd(&ea.cmd, "call", 3))
4503 ea.cmd = skipwhite(ea.cmd);
4504
4505 // Assuming the command starts with a variable or function name, find
4506 // what follows. Also "&opt = value".
4507 p = (*ea.cmd == '&') ? ea.cmd + 1 : ea.cmd;
4508 p = to_name_end(p);
4509 if (p > ea.cmd && *p != NUL)
4510 {
4511 int oplen;
4512 int heredoc;
4513
4514 // "funcname(" is always a function call.
4515 // "varname[]" is an expression.
4516 // "g:varname" is an expression.
4517 // "varname->expr" is an expression.
4518 if (*p == '('
4519 || *p == '['
4520 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4521 || (*p == '-' && p[1] == '>'))
4522 {
4523 // TODO
4524 }
4525
4526 oplen = assignment_len(skipwhite(p), &heredoc);
4527 if (oplen > 0)
4528 {
4529 // Recognize an assignment if we recognize the variable name:
4530 // "g:var = expr"
4531 // "var = expr" where "var" is a local var name.
4532 // "&opt = expr"
4533 if (*ea.cmd == '&'
4534 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4535 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4536 || lookup_script(ea.cmd, p - ea.cmd) == OK)
4537 {
4538 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4539 if (line == NULL)
4540 goto erret;
4541 continue;
4542 }
4543 }
4544 }
4545
4546 /*
4547 * COMMAND after range
4548 */
4549 ea.cmd = skip_range(ea.cmd, NULL);
4550 p = find_ex_command(&ea, NULL, lookup_local, &cctx);
4551
4552 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4553 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004554 if (cctx.ctx_skip == TRUE)
4555 {
4556 line += STRLEN(line);
4557 continue;
4558 }
4559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 // Expression or function call.
4561 if (ea.cmdidx == CMD_eval)
4562 {
4563 p = ea.cmd;
4564 if (compile_expr1(&p, &cctx) == FAIL)
4565 goto erret;
4566
4567 // drop the return value
4568 generate_instr_drop(&cctx, ISN_DROP, 1);
4569 line = p;
4570 continue;
4571 }
4572 if (ea.cmdidx == CMD_let)
4573 {
4574 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4575 if (line == NULL)
4576 goto erret;
4577 continue;
4578 }
4579 iemsg("Command from find_ex_command() not handled");
4580 goto erret;
4581 }
4582
4583 p = skipwhite(p);
4584
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004585 if (cctx.ctx_skip == TRUE
4586 && ea.cmdidx != CMD_elseif
4587 && ea.cmdidx != CMD_else
4588 && ea.cmdidx != CMD_endif)
4589 {
4590 line += STRLEN(line);
4591 continue;
4592 }
4593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 switch (ea.cmdidx)
4595 {
4596 case CMD_def:
4597 case CMD_function:
4598 // TODO: Nested function
4599 emsg("Nested function not implemented yet");
4600 goto erret;
4601
4602 case CMD_return:
4603 line = compile_return(p, set_return_type, &cctx);
4604 had_return = TRUE;
4605 break;
4606
4607 case CMD_let:
4608 case CMD_const:
4609 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
4610 break;
4611
4612 case CMD_import:
4613 line = compile_import(p, &cctx);
4614 break;
4615
4616 case CMD_if:
4617 line = compile_if(p, &cctx);
4618 break;
4619 case CMD_elseif:
4620 line = compile_elseif(p, &cctx);
4621 break;
4622 case CMD_else:
4623 line = compile_else(p, &cctx);
4624 break;
4625 case CMD_endif:
4626 line = compile_endif(p, &cctx);
4627 break;
4628
4629 case CMD_while:
4630 line = compile_while(p, &cctx);
4631 break;
4632 case CMD_endwhile:
4633 line = compile_endwhile(p, &cctx);
4634 break;
4635
4636 case CMD_for:
4637 line = compile_for(p, &cctx);
4638 break;
4639 case CMD_endfor:
4640 line = compile_endfor(p, &cctx);
4641 break;
4642 case CMD_continue:
4643 line = compile_continue(p, &cctx);
4644 break;
4645 case CMD_break:
4646 line = compile_break(p, &cctx);
4647 break;
4648
4649 case CMD_try:
4650 line = compile_try(p, &cctx);
4651 break;
4652 case CMD_catch:
4653 line = compile_catch(p, &cctx);
4654 break;
4655 case CMD_finally:
4656 line = compile_finally(p, &cctx);
4657 break;
4658 case CMD_endtry:
4659 line = compile_endtry(p, &cctx);
4660 break;
4661 case CMD_throw:
4662 line = compile_throw(p, &cctx);
4663 break;
4664
4665 case CMD_echo:
4666 line = compile_echo(p, TRUE, &cctx);
4667 break;
4668 case CMD_echon:
4669 line = compile_echo(p, FALSE, &cctx);
4670 break;
4671
4672 default:
4673 // Not recognized, execute with do_cmdline_cmd().
4674 generate_EXEC(&cctx, line);
4675 line = (char_u *)"";
4676 break;
4677 }
4678 if (line == NULL)
4679 goto erret;
4680
4681 if (cctx.ctx_type_stack.ga_len < 0)
4682 {
4683 iemsg("Type stack underflow");
4684 goto erret;
4685 }
4686 }
4687
4688 if (cctx.ctx_scope != NULL)
4689 {
4690 if (cctx.ctx_scope->se_type == IF_SCOPE)
4691 emsg(_(e_endif));
4692 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
4693 emsg(_(e_endwhile));
4694 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
4695 emsg(_(e_endfor));
4696 else
4697 emsg(_("E1026: Missing }"));
4698 goto erret;
4699 }
4700
4701 if (!had_return)
4702 {
4703 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
4704 {
4705 emsg(_("E1027: Missing return statement"));
4706 goto erret;
4707 }
4708
4709 // Return zero if there is no return at the end.
4710 generate_PUSHNR(&cctx, 0);
4711 generate_instr(&cctx, ISN_RETURN);
4712 }
4713
4714 dfunc->df_instr = instr->ga_data;
4715 dfunc->df_instr_count = instr->ga_len;
4716 dfunc->df_varcount = cctx.ctx_max_local;
4717
4718 ret = OK;
4719
4720erret:
4721 if (ret == FAIL)
4722 {
4723 ga_clear(instr);
4724 ufunc->uf_dfunc_idx = -1;
4725 --def_functions.ga_len;
4726 if (errormsg != NULL)
4727 emsg(errormsg);
4728 else if (called_emsg == called_emsg_before)
4729 emsg("E1028: compile_def_function failed");
4730
4731 // don't execute this function body
4732 ufunc->uf_lines.ga_len = 0;
4733 }
4734
4735 current_sctx = save_current_sctx;
4736 ga_clear(&cctx.ctx_type_stack);
4737 ga_clear(&cctx.ctx_locals);
4738}
4739
4740/*
4741 * Delete an instruction, free what it contains.
4742 */
4743 static void
4744delete_instr(isn_T *isn)
4745{
4746 switch (isn->isn_type)
4747 {
4748 case ISN_EXEC:
4749 case ISN_LOADENV:
4750 case ISN_LOADG:
4751 case ISN_LOADOPT:
4752 case ISN_MEMBER:
4753 case ISN_PUSHEXC:
4754 case ISN_PUSHS:
4755 case ISN_STOREG:
4756 vim_free(isn->isn_arg.string);
4757 break;
4758
4759 case ISN_LOADS:
4760 vim_free(isn->isn_arg.loads.ls_name);
4761 break;
4762
4763 case ISN_STOREOPT:
4764 vim_free(isn->isn_arg.storeopt.so_name);
4765 break;
4766
4767 case ISN_PUSHBLOB: // push blob isn_arg.blob
4768 blob_unref(isn->isn_arg.blob);
4769 break;
4770
4771 case ISN_UCALL:
4772 vim_free(isn->isn_arg.ufunc.cuf_name);
4773 break;
4774
4775 case ISN_2BOOL:
4776 case ISN_2STRING:
4777 case ISN_ADDBLOB:
4778 case ISN_ADDLIST:
4779 case ISN_BCALL:
4780 case ISN_CATCH:
4781 case ISN_CHECKNR:
4782 case ISN_CHECKTYPE:
4783 case ISN_COMPAREANY:
4784 case ISN_COMPAREBLOB:
4785 case ISN_COMPAREBOOL:
4786 case ISN_COMPAREDICT:
4787 case ISN_COMPAREFLOAT:
4788 case ISN_COMPAREFUNC:
4789 case ISN_COMPARELIST:
4790 case ISN_COMPARENR:
4791 case ISN_COMPAREPARTIAL:
4792 case ISN_COMPARESPECIAL:
4793 case ISN_COMPARESTRING:
4794 case ISN_CONCAT:
4795 case ISN_DCALL:
4796 case ISN_DROP:
4797 case ISN_ECHO:
4798 case ISN_ENDTRY:
4799 case ISN_FOR:
4800 case ISN_FUNCREF:
4801 case ISN_INDEX:
4802 case ISN_JUMP:
4803 case ISN_LOAD:
4804 case ISN_LOADSCRIPT:
4805 case ISN_LOADREG:
4806 case ISN_LOADV:
4807 case ISN_NEGATENR:
4808 case ISN_NEWDICT:
4809 case ISN_NEWLIST:
4810 case ISN_OPNR:
4811 case ISN_OPFLOAT:
4812 case ISN_OPANY:
4813 case ISN_PCALL:
4814 case ISN_PUSHF:
4815 case ISN_PUSHNR:
4816 case ISN_PUSHBOOL:
4817 case ISN_PUSHSPEC:
4818 case ISN_RETURN:
4819 case ISN_STORE:
4820 case ISN_STORENR:
4821 case ISN_STORESCRIPT:
4822 case ISN_THROW:
4823 case ISN_TRY:
4824 // nothing allocated
4825 break;
4826 }
4827}
4828
4829/*
4830 * When a user function is deleted, delete any associated def function.
4831 */
4832 void
4833delete_def_function(ufunc_T *ufunc)
4834{
4835 int idx;
4836
4837 if (ufunc->uf_dfunc_idx >= 0)
4838 {
4839 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4840 + ufunc->uf_dfunc_idx;
4841 ga_clear(&dfunc->df_def_args_isn);
4842
4843 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
4844 delete_instr(dfunc->df_instr + idx);
4845 VIM_CLEAR(dfunc->df_instr);
4846
4847 dfunc->df_deleted = TRUE;
4848 }
4849}
4850
4851#if defined(EXITFREE) || defined(PROTO)
4852 void
4853free_def_functions(void)
4854{
4855 vim_free(def_functions.ga_data);
4856}
4857#endif
4858
4859
4860#endif // FEAT_EVAL