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