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