blob: a4525ca0c167cb5187ba4c57422b49d4b8dfabf9 [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;
995
996 if (check_internal_func(func_idx, argcount) == FAIL)
997 return FAIL;
998
999 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.bfunc.cbf_idx = func_idx;
1002 isn->isn_arg.bfunc.cbf_argcount = argcount;
1003
1004 stack->ga_len -= argcount; // drop the arguments
1005 if (ga_grow(stack, 1) == FAIL)
1006 return FAIL;
1007 ((type_T **)stack->ga_data)[stack->ga_len] =
1008 internal_func_ret_type(func_idx, argcount);
1009 ++stack->ga_len; // add return value
1010
1011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_DCALL or ISN_UCALL instruction.
1016 * Return FAIL if the number of arguments is wrong.
1017 */
1018 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001019generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020{
1021 isn_T *isn;
1022 garray_T *stack = &cctx->ctx_type_stack;
1023 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001024 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025
1026 if (argcount > regular_args && !has_varargs(ufunc))
1027 {
1028 semsg(_(e_toomanyarg), ufunc->uf_name);
1029 return FAIL;
1030 }
1031 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1032 {
1033 semsg(_(e_toofewarg), ufunc->uf_name);
1034 return FAIL;
1035 }
1036
1037 // Turn varargs into a list.
1038 if (ufunc->uf_va_name != NULL)
1039 {
1040 int count = argcount - regular_args;
1041
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001042 // If count is negative an empty list will be added after evaluating
1043 // default values for missing optional arguments.
1044 if (count >= 0)
1045 {
1046 generate_NEWLIST(cctx, count);
1047 argcount = regular_args + 1;
1048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 }
1050
1051 if ((isn = generate_instr(cctx,
1052 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1053 return FAIL;
1054 if (ufunc->uf_dfunc_idx >= 0)
1055 {
1056 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1057 isn->isn_arg.dfunc.cdf_argcount = argcount;
1058 }
1059 else
1060 {
1061 // A user function may be deleted and redefined later, can't use the
1062 // ufunc pointer, need to look it up again at runtime.
1063 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1064 isn->isn_arg.ufunc.cuf_argcount = argcount;
1065 }
1066
1067 stack->ga_len -= argcount; // drop the arguments
1068 if (ga_grow(stack, 1) == FAIL)
1069 return FAIL;
1070 // add return value
1071 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1072 ++stack->ga_len;
1073
1074 return OK;
1075}
1076
1077/*
1078 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1079 */
1080 static int
1081generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1082{
1083 isn_T *isn;
1084 garray_T *stack = &cctx->ctx_type_stack;
1085
1086 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1087 return FAIL;
1088 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1089 isn->isn_arg.ufunc.cuf_argcount = argcount;
1090
1091 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001092 if (ga_grow(stack, 1) == FAIL)
1093 return FAIL;
1094 // add return value
1095 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1096 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097
1098 return OK;
1099}
1100
1101/*
1102 * Generate an ISN_PCALL instruction.
1103 */
1104 static int
1105generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1106{
1107 isn_T *isn;
1108 garray_T *stack = &cctx->ctx_type_stack;
1109
1110 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1111 return FAIL;
1112 isn->isn_arg.pfunc.cpf_top = at_top;
1113 isn->isn_arg.pfunc.cpf_argcount = argcount;
1114
1115 stack->ga_len -= argcount; // drop the arguments
1116
1117 // drop the funcref/partial, get back the return value
1118 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_MEMBER instruction.
1125 */
1126 static int
1127generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1128{
1129 isn_T *isn;
1130 garray_T *stack = &cctx->ctx_type_stack;
1131 type_T *type;
1132
1133 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.string = vim_strnsave(name, (int)len);
1136
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001137 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001139 if (type->tt_type != VAR_DICT && type != &t_any)
1140 {
1141 emsg(_(e_dictreq));
1142 return FAIL;
1143 }
1144 // change dict type to dict member type
1145 if (type->tt_type == VAR_DICT)
1146 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147
1148 return OK;
1149}
1150
1151/*
1152 * Generate an ISN_ECHO instruction.
1153 */
1154 static int
1155generate_ECHO(cctx_T *cctx, int with_white, int count)
1156{
1157 isn_T *isn;
1158
1159 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1160 return FAIL;
1161 isn->isn_arg.echo.echo_with_white = with_white;
1162 isn->isn_arg.echo.echo_count = count;
1163
1164 return OK;
1165}
1166
Bram Moolenaarad39c092020-02-26 18:23:43 +01001167/*
1168 * Generate an ISN_EXECUTE instruction.
1169 */
1170 static int
1171generate_EXECUTE(cctx_T *cctx, int count)
1172{
1173 isn_T *isn;
1174
1175 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1176 return FAIL;
1177 isn->isn_arg.number = count;
1178
1179 return OK;
1180}
1181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 static int
1183generate_EXEC(cctx_T *cctx, char_u *line)
1184{
1185 isn_T *isn;
1186
1187 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.string = vim_strsave(line);
1190 return OK;
1191}
1192
1193static char e_white_both[] =
1194 N_("E1004: white space required before and after '%s'");
1195
1196/*
1197 * Reserve space for a local variable.
1198 * Return the index or -1 if it failed.
1199 */
1200 static int
1201reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1202{
1203 int idx;
1204 lvar_T *lvar;
1205
1206 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1207 {
1208 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1209 return -1;
1210 }
1211
1212 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1213 return -1;
1214 idx = cctx->ctx_locals.ga_len;
1215 if (cctx->ctx_max_local < idx + 1)
1216 cctx->ctx_max_local = idx + 1;
1217 ++cctx->ctx_locals.ga_len;
1218
1219 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1220 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1221 lvar->lv_const = isConst;
1222 lvar->lv_type = type;
1223
1224 return idx;
1225}
1226
1227/*
1228 * Skip over a type definition and return a pointer to just after it.
1229 */
1230 char_u *
1231skip_type(char_u *start)
1232{
1233 char_u *p = start;
1234
1235 while (ASCII_ISALNUM(*p) || *p == '_')
1236 ++p;
1237
1238 // Skip over "<type>"; this is permissive about white space.
1239 if (*skipwhite(p) == '<')
1240 {
1241 p = skipwhite(p);
1242 p = skip_type(skipwhite(p + 1));
1243 p = skipwhite(p);
1244 if (*p == '>')
1245 ++p;
1246 }
1247 return p;
1248}
1249
1250/*
1251 * Parse the member type: "<type>" and return "type" with the member set.
1252 * Use "type_list" if a new type needs to be added.
1253 * Returns NULL in case of failure.
1254 */
1255 static type_T *
1256parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1257{
1258 type_T *member_type;
1259
1260 if (**arg != '<')
1261 {
1262 if (*skipwhite(*arg) == '<')
1263 emsg(_("E1007: No white space allowed before <"));
1264 else
1265 emsg(_("E1008: Missing <type>"));
1266 return NULL;
1267 }
1268 *arg = skipwhite(*arg + 1);
1269
1270 member_type = parse_type(arg, type_list);
1271 if (member_type == NULL)
1272 return NULL;
1273
1274 *arg = skipwhite(*arg);
1275 if (**arg != '>')
1276 {
1277 emsg(_("E1009: Missing > after type"));
1278 return NULL;
1279 }
1280 ++*arg;
1281
1282 if (type->tt_type == VAR_LIST)
1283 return get_list_type(member_type, type_list);
1284 return get_dict_type(member_type, type_list);
1285}
1286
1287/*
1288 * Parse a type at "arg" and advance over it.
1289 * Return NULL for failure.
1290 */
1291 type_T *
1292parse_type(char_u **arg, garray_T *type_list)
1293{
1294 char_u *p = *arg;
1295 size_t len;
1296
1297 // skip over the first word
1298 while (ASCII_ISALNUM(*p) || *p == '_')
1299 ++p;
1300 len = p - *arg;
1301
1302 switch (**arg)
1303 {
1304 case 'a':
1305 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1306 {
1307 *arg += len;
1308 return &t_any;
1309 }
1310 break;
1311 case 'b':
1312 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1313 {
1314 *arg += len;
1315 return &t_bool;
1316 }
1317 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1318 {
1319 *arg += len;
1320 return &t_blob;
1321 }
1322 break;
1323 case 'c':
1324 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1325 {
1326 *arg += len;
1327 return &t_channel;
1328 }
1329 break;
1330 case 'd':
1331 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1332 {
1333 *arg += len;
1334 return parse_type_member(arg, &t_dict_any, type_list);
1335 }
1336 break;
1337 case 'f':
1338 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1339 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001340#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 *arg += len;
1342 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001343#else
1344 emsg(_("E1055: This Vim is not compiled with float support"));
1345 return &t_any;
1346#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 }
1348 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1349 {
1350 *arg += len;
1351 // TODO: arguments and return type
1352 return &t_func_any;
1353 }
1354 break;
1355 case 'j':
1356 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1357 {
1358 *arg += len;
1359 return &t_job;
1360 }
1361 break;
1362 case 'l':
1363 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1364 {
1365 *arg += len;
1366 return parse_type_member(arg, &t_list_any, type_list);
1367 }
1368 break;
1369 case 'n':
1370 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1371 {
1372 *arg += len;
1373 return &t_number;
1374 }
1375 break;
1376 case 'p':
1377 if (len == 4 && STRNCMP(*arg, "partial", len) == 0)
1378 {
1379 *arg += len;
1380 // TODO: arguments and return type
1381 return &t_partial_any;
1382 }
1383 break;
1384 case 's':
1385 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1386 {
1387 *arg += len;
1388 return &t_string;
1389 }
1390 break;
1391 case 'v':
1392 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1393 {
1394 *arg += len;
1395 return &t_void;
1396 }
1397 break;
1398 }
1399
1400 semsg(_("E1010: Type not recognized: %s"), *arg);
1401 return &t_any;
1402}
1403
1404/*
1405 * Check if "type1" and "type2" are exactly the same.
1406 */
1407 static int
1408equal_type(type_T *type1, type_T *type2)
1409{
1410 if (type1->tt_type != type2->tt_type)
1411 return FALSE;
1412 switch (type1->tt_type)
1413 {
1414 case VAR_VOID:
1415 case VAR_UNKNOWN:
1416 case VAR_SPECIAL:
1417 case VAR_BOOL:
1418 case VAR_NUMBER:
1419 case VAR_FLOAT:
1420 case VAR_STRING:
1421 case VAR_BLOB:
1422 case VAR_JOB:
1423 case VAR_CHANNEL:
1424 return TRUE; // not composite is always OK
1425 case VAR_LIST:
1426 case VAR_DICT:
1427 return equal_type(type1->tt_member, type2->tt_member);
1428 case VAR_FUNC:
1429 case VAR_PARTIAL:
1430 // TODO; check argument types.
1431 return equal_type(type1->tt_member, type2->tt_member)
1432 && type1->tt_argcount == type2->tt_argcount;
1433 }
1434 return TRUE;
1435}
1436
1437/*
1438 * Find the common type of "type1" and "type2" and put it in "dest".
1439 * "type2" and "dest" may be the same.
1440 */
1441 static void
1442common_type(type_T *type1, type_T *type2, type_T *dest)
1443{
1444 if (equal_type(type1, type2))
1445 {
1446 if (dest != type2)
1447 *dest = *type2;
1448 return;
1449 }
1450
1451 if (type1->tt_type == type2->tt_type)
1452 {
1453 dest->tt_type = type1->tt_type;
1454 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1455 {
1456 common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1457 return;
1458 }
1459 // TODO: VAR_FUNC and VAR_PARTIAL
1460 }
1461
1462 dest->tt_type = VAR_UNKNOWN; // "any"
1463}
1464
1465 char *
1466vartype_name(vartype_T type)
1467{
1468 switch (type)
1469 {
1470 case VAR_VOID: return "void";
1471 case VAR_UNKNOWN: return "any";
1472 case VAR_SPECIAL: return "special";
1473 case VAR_BOOL: return "bool";
1474 case VAR_NUMBER: return "number";
1475 case VAR_FLOAT: return "float";
1476 case VAR_STRING: return "string";
1477 case VAR_BLOB: return "blob";
1478 case VAR_JOB: return "job";
1479 case VAR_CHANNEL: return "channel";
1480 case VAR_LIST: return "list";
1481 case VAR_DICT: return "dict";
1482 case VAR_FUNC: return "function";
1483 case VAR_PARTIAL: return "partial";
1484 }
1485 return "???";
1486}
1487
1488/*
1489 * Return the name of a type.
1490 * The result may be in allocated memory, in which case "tofree" is set.
1491 */
1492 char *
1493type_name(type_T *type, char **tofree)
1494{
1495 char *name = vartype_name(type->tt_type);
1496
1497 *tofree = NULL;
1498 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1499 {
1500 char *member_free;
1501 char *member_name = type_name(type->tt_member, &member_free);
1502 size_t len;
1503
1504 len = STRLEN(name) + STRLEN(member_name) + 3;
1505 *tofree = alloc(len);
1506 if (*tofree != NULL)
1507 {
1508 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1509 vim_free(member_free);
1510 return *tofree;
1511 }
1512 }
1513 // TODO: function and partial argument types
1514
1515 return name;
1516}
1517
1518/*
1519 * Find "name" in script-local items of script "sid".
1520 * Returns the index in "sn_var_vals" if found.
1521 * If found but not in "sn_var_vals" returns -1.
1522 * If not found returns -2.
1523 */
1524 int
1525get_script_item_idx(int sid, char_u *name, int check_writable)
1526{
1527 hashtab_T *ht;
1528 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001529 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 int idx;
1531
1532 // First look the name up in the hashtable.
1533 if (sid <= 0 || sid > script_items.ga_len)
1534 return -1;
1535 ht = &SCRIPT_VARS(sid);
1536 di = find_var_in_ht(ht, 0, name, TRUE);
1537 if (di == NULL)
1538 return -2;
1539
1540 // Now find the svar_T index in sn_var_vals.
1541 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1542 {
1543 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1544
1545 if (sv->sv_tv == &di->di_tv)
1546 {
1547 if (check_writable && sv->sv_const)
1548 semsg(_(e_readonlyvar), name);
1549 return idx;
1550 }
1551 }
1552 return -1;
1553}
1554
1555/*
1556 * Find "name" in imported items of the current script/
1557 */
1558 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001559find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001561 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 int idx;
1563
1564 if (cctx != NULL)
1565 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1566 {
1567 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1568 + idx;
1569
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001570 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1571 : STRLEN(import->imp_name) == len
1572 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 return import;
1574 }
1575
1576 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1577 {
1578 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1579
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001580 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1581 : STRLEN(import->imp_name) == len
1582 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 return import;
1584 }
1585 return NULL;
1586}
1587
1588/*
1589 * Generate an instruction to load script-local variable "name".
1590 */
1591 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001592compile_load_scriptvar(
1593 cctx_T *cctx,
1594 char_u *name, // variable NUL terminated
1595 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001596 char_u **end, // end of variable
1597 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001599 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1601 imported_T *import;
1602
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001603 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001605 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001606 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1607 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 }
1609 if (idx >= 0)
1610 {
1611 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1612
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001613 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 current_sctx.sc_sid, idx, sv->sv_type);
1615 return OK;
1616 }
1617
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001618 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 if (import != NULL)
1620 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001621 if (import->imp_all)
1622 {
1623 char_u *p = skipwhite(*end);
1624 int name_len;
1625 ufunc_T *ufunc;
1626 type_T *type;
1627
1628 // Used "import * as Name", need to lookup the member.
1629 if (*p != '.')
1630 {
1631 semsg(_("E1060: expected dot after name: %s"), start);
1632 return FAIL;
1633 }
1634 ++p;
1635
1636 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1637 // TODO: what if it is a function?
1638 if (idx < 0)
1639 return FAIL;
1640 *end = p;
1641
1642 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1643 import->imp_sid,
1644 idx,
1645 type);
1646 }
1647 else
1648 {
1649 // TODO: check this is a variable, not a function
1650 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1651 import->imp_sid,
1652 import->imp_var_vals_idx,
1653 import->imp_type);
1654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 return OK;
1656 }
1657
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001658 if (error)
1659 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 return FAIL;
1661}
1662
1663/*
1664 * Compile a variable name into a load instruction.
1665 * "end" points to just after the name.
1666 * When "error" is FALSE do not give an error when not found.
1667 */
1668 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001669compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670{
1671 type_T *type;
1672 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001673 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 int res = FAIL;
1675
1676 if (*(*arg + 1) == ':')
1677 {
1678 // load namespaced variable
1679 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1680 if (name == NULL)
1681 return FAIL;
1682
1683 if (**arg == 'v')
1684 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001685 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687 else if (**arg == 'g')
1688 {
1689 // Global variables can be defined later, thus we don't check if it
1690 // exists, give error at runtime.
1691 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1692 }
1693 else if (**arg == 's')
1694 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001695 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 }
1697 else
1698 {
1699 semsg("Namespace not supported yet: %s", **arg);
1700 goto theend;
1701 }
1702 }
1703 else
1704 {
1705 size_t len = end - *arg;
1706 int idx;
1707 int gen_load = FALSE;
1708
1709 name = vim_strnsave(*arg, end - *arg);
1710 if (name == NULL)
1711 return FAIL;
1712
1713 idx = lookup_arg(*arg, len, cctx);
1714 if (idx >= 0)
1715 {
1716 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1717 type = cctx->ctx_ufunc->uf_arg_types[idx];
1718 else
1719 type = &t_any;
1720
1721 // Arguments are located above the frame pointer.
1722 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1723 if (cctx->ctx_ufunc->uf_va_name != NULL)
1724 --idx;
1725 gen_load = TRUE;
1726 }
1727 else if (lookup_vararg(*arg, len, cctx))
1728 {
1729 // varargs is always the last argument
1730 idx = -STACK_FRAME_SIZE - 1;
1731 type = cctx->ctx_ufunc->uf_va_type;
1732 gen_load = TRUE;
1733 }
1734 else
1735 {
1736 idx = lookup_local(*arg, len, cctx);
1737 if (idx >= 0)
1738 {
1739 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1740 gen_load = TRUE;
1741 }
1742 else
1743 {
1744 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1745 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1746 res = generate_PUSHBOOL(cctx, **arg == 't'
1747 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001748 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1749 == SCRIPT_VERSION_VIM9)
1750 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001751 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 }
1753 }
1754 if (gen_load)
1755 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1756 }
1757
1758 *arg = end;
1759
1760theend:
1761 if (res == FAIL && error)
1762 semsg(_(e_var_notfound), name);
1763 vim_free(name);
1764 return res;
1765}
1766
1767/*
1768 * Compile the argument expressions.
1769 * "arg" points to just after the "(" and is advanced to after the ")"
1770 */
1771 static int
1772compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1773{
1774 char_u *p = *arg;
1775
1776 while (*p != NUL && *p != ')')
1777 {
1778 if (compile_expr1(&p, cctx) == FAIL)
1779 return FAIL;
1780 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001781
1782 if (*p != ',' && *skipwhite(p) == ',')
1783 {
1784 emsg(_("E1068: No white space allowed before ,"));
1785 p = skipwhite(p);
1786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001788 {
1789 ++p;
1790 if (!VIM_ISWHITE(*p))
1791 emsg(_("E1069: white space required after ,"));
1792 }
1793 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001795 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 if (*p != ')')
1797 {
1798 emsg(_(e_missing_close));
1799 return FAIL;
1800 }
1801 *arg = p + 1;
1802 return OK;
1803}
1804
1805/*
1806 * Compile a function call: name(arg1, arg2)
1807 * "arg" points to "name", "arg + varlen" to the "(".
1808 * "argcount_init" is 1 for "value->method()"
1809 * Instructions:
1810 * EVAL arg1
1811 * EVAL arg2
1812 * BCALL / DCALL / UCALL
1813 */
1814 static int
1815compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1816{
1817 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001818 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 int argcount = argcount_init;
1820 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001821 char_u fname_buf[FLEN_FIXED + 1];
1822 char_u *tofree = NULL;
1823 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001825 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827 if (varlen >= sizeof(namebuf))
1828 {
1829 semsg(_("E1011: name too long: %s"), name);
1830 return FAIL;
1831 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001832 vim_strncpy(namebuf, *arg, varlen);
1833 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834
1835 *arg = skipwhite(*arg + varlen + 1);
1836 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001837 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001839 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 {
1841 int idx;
1842
1843 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001844 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001846 {
1847 res = generate_BCALL(cctx, idx, argcount);
1848 goto theend;
1849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 semsg(_(e_unknownfunc), namebuf);
1851 }
1852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001854 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001856 {
1857 res = generate_CALL(cctx, ufunc, argcount);
1858 goto theend;
1859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860
1861 // If the name is a variable, load it and use PCALL.
1862 p = namebuf;
1863 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001864 {
1865 res = generate_PCALL(cctx, argcount, FALSE);
1866 goto theend;
1867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868
1869 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001870 res = generate_UCALL(cctx, name, argcount);
1871
1872theend:
1873 vim_free(tofree);
1874 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875}
1876
1877// like NAMESPACE_CHAR but with 'a' and 'l'.
1878#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1879
1880/*
1881 * Find the end of a variable or function name. Unlike find_name_end() this
1882 * does not recognize magic braces.
1883 * Return a pointer to just after the name. Equal to "arg" if there is no
1884 * valid name.
1885 */
1886 char_u *
1887to_name_end(char_u *arg)
1888{
1889 char_u *p;
1890
1891 // Quick check for valid starting character.
1892 if (!eval_isnamec1(*arg))
1893 return arg;
1894
1895 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1896 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1897 // and can be used in slice "[n:]".
1898 if (*p == ':' && (p != arg + 1
1899 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1900 break;
1901 return p;
1902}
1903
1904/*
1905 * Like to_name_end() but also skip over a list or dict constant.
1906 */
1907 char_u *
1908to_name_const_end(char_u *arg)
1909{
1910 char_u *p = to_name_end(arg);
1911 typval_T rettv;
1912
1913 if (p == arg && *arg == '[')
1914 {
1915
1916 // Can be "[1, 2, 3]->Func()".
1917 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1918 p = arg;
1919 }
1920 else if (p == arg && *arg == '#' && arg[1] == '{')
1921 {
1922 ++p;
1923 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1924 p = arg;
1925 }
1926 else if (p == arg && *arg == '{')
1927 {
1928 int ret = get_lambda_tv(&p, &rettv, FALSE);
1929
1930 if (ret == NOTDONE)
1931 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1932 if (ret != OK)
1933 p = arg;
1934 }
1935
1936 return p;
1937}
1938
1939 static void
1940type_mismatch(type_T *expected, type_T *actual)
1941{
1942 char *tofree1, *tofree2;
1943
1944 semsg(_("E1013: type mismatch, expected %s but got %s"),
1945 type_name(expected, &tofree1), type_name(actual, &tofree2));
1946 vim_free(tofree1);
1947 vim_free(tofree2);
1948}
1949
1950/*
1951 * Check if the expected and actual types match.
1952 */
1953 static int
1954check_type(type_T *expected, type_T *actual, int give_msg)
1955{
1956 if (expected->tt_type != VAR_UNKNOWN)
1957 {
1958 if (expected->tt_type != actual->tt_type)
1959 {
1960 if (give_msg)
1961 type_mismatch(expected, actual);
1962 return FAIL;
1963 }
1964 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1965 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01001966 int ret;
1967
1968 // void is used for an empty list or dict
1969 if (actual->tt_member == &t_void)
1970 ret = OK;
1971 else
1972 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 if (ret == FAIL && give_msg)
1974 type_mismatch(expected, actual);
1975 return ret;
1976 }
1977 }
1978 return OK;
1979}
1980
1981/*
1982 * Check that
1983 * - "actual" is "expected" type or
1984 * - "actual" is a type that can be "expected" type: add a runtime check; or
1985 * - return FAIL.
1986 */
1987 static int
1988need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1989{
Bram Moolenaar436472f2020-02-20 22:54:43 +01001990 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 return OK;
1992 if (actual->tt_type != VAR_UNKNOWN)
1993 {
1994 type_mismatch(expected, actual);
1995 return FAIL;
1996 }
1997 generate_TYPECHECK(cctx, expected, offset);
1998 return OK;
1999}
2000
2001/*
2002 * parse a list: [expr, expr]
2003 * "*arg" points to the '['.
2004 */
2005 static int
2006compile_list(char_u **arg, cctx_T *cctx)
2007{
2008 char_u *p = skipwhite(*arg + 1);
2009 int count = 0;
2010
2011 while (*p != ']')
2012 {
2013 if (*p == NUL)
2014 return FAIL;
2015 if (compile_expr1(&p, cctx) == FAIL)
2016 break;
2017 ++count;
2018 if (*p == ',')
2019 ++p;
2020 p = skipwhite(p);
2021 }
2022 *arg = p + 1;
2023
2024 generate_NEWLIST(cctx, count);
2025 return OK;
2026}
2027
2028/*
2029 * parse a lambda: {arg, arg -> expr}
2030 * "*arg" points to the '{'.
2031 */
2032 static int
2033compile_lambda(char_u **arg, cctx_T *cctx)
2034{
2035 garray_T *instr = &cctx->ctx_instr;
2036 typval_T rettv;
2037 ufunc_T *ufunc;
2038
2039 // Get the funcref in "rettv".
2040 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2041 return FAIL;
2042 ufunc = rettv.vval.v_partial->pt_func;
2043
2044 // The function will have one line: "return {expr}".
2045 // Compile it into instructions.
2046 compile_def_function(ufunc, TRUE);
2047
2048 if (ufunc->uf_dfunc_idx >= 0)
2049 {
2050 if (ga_grow(instr, 1) == FAIL)
2051 return FAIL;
2052 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2053 return OK;
2054 }
2055 return FAIL;
2056}
2057
2058/*
2059 * Compile a lamda call: expr->{lambda}(args)
2060 * "arg" points to the "{".
2061 */
2062 static int
2063compile_lambda_call(char_u **arg, cctx_T *cctx)
2064{
2065 ufunc_T *ufunc;
2066 typval_T rettv;
2067 int argcount = 1;
2068 int ret = FAIL;
2069
2070 // Get the funcref in "rettv".
2071 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2072 return FAIL;
2073
2074 if (**arg != '(')
2075 {
2076 if (*skipwhite(*arg) == '(')
2077 semsg(_(e_nowhitespace));
2078 else
2079 semsg(_(e_missing_paren), "lambda");
2080 clear_tv(&rettv);
2081 return FAIL;
2082 }
2083
2084 // The function will have one line: "return {expr}".
2085 // Compile it into instructions.
2086 ufunc = rettv.vval.v_partial->pt_func;
2087 ++ufunc->uf_refcount;
2088 compile_def_function(ufunc, TRUE);
2089
2090 // compile the arguments
2091 *arg = skipwhite(*arg + 1);
2092 if (compile_arguments(arg, cctx, &argcount) == OK)
2093 // call the compiled function
2094 ret = generate_CALL(cctx, ufunc, argcount);
2095
2096 clear_tv(&rettv);
2097 return ret;
2098}
2099
2100/*
2101 * parse a dict: {'key': val} or #{key: val}
2102 * "*arg" points to the '{'.
2103 */
2104 static int
2105compile_dict(char_u **arg, cctx_T *cctx, int literal)
2106{
2107 garray_T *instr = &cctx->ctx_instr;
2108 int count = 0;
2109 dict_T *d = dict_alloc();
2110 dictitem_T *item;
2111
2112 if (d == NULL)
2113 return FAIL;
2114 *arg = skipwhite(*arg + 1);
2115 while (**arg != '}' && **arg != NUL)
2116 {
2117 char_u *key = NULL;
2118
2119 if (literal)
2120 {
2121 char_u *p = to_name_end(*arg);
2122
2123 if (p == *arg)
2124 {
2125 semsg(_("E1014: Invalid key: %s"), *arg);
2126 return FAIL;
2127 }
2128 key = vim_strnsave(*arg, p - *arg);
2129 if (generate_PUSHS(cctx, key) == FAIL)
2130 return FAIL;
2131 *arg = p;
2132 }
2133 else
2134 {
2135 isn_T *isn;
2136
2137 if (compile_expr1(arg, cctx) == FAIL)
2138 return FAIL;
2139 // TODO: check type is string
2140 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2141 if (isn->isn_type == ISN_PUSHS)
2142 key = isn->isn_arg.string;
2143 }
2144
2145 // Check for duplicate keys, if using string keys.
2146 if (key != NULL)
2147 {
2148 item = dict_find(d, key, -1);
2149 if (item != NULL)
2150 {
2151 semsg(_(e_duplicate_key), key);
2152 goto failret;
2153 }
2154 item = dictitem_alloc(key);
2155 if (item != NULL)
2156 {
2157 item->di_tv.v_type = VAR_UNKNOWN;
2158 item->di_tv.v_lock = 0;
2159 if (dict_add(d, item) == FAIL)
2160 dictitem_free(item);
2161 }
2162 }
2163
2164 *arg = skipwhite(*arg);
2165 if (**arg != ':')
2166 {
2167 semsg(_(e_missing_dict_colon), *arg);
2168 return FAIL;
2169 }
2170
2171 *arg = skipwhite(*arg + 1);
2172 if (compile_expr1(arg, cctx) == FAIL)
2173 return FAIL;
2174 ++count;
2175
2176 if (**arg == '}')
2177 break;
2178 if (**arg != ',')
2179 {
2180 semsg(_(e_missing_dict_comma), *arg);
2181 goto failret;
2182 }
2183 *arg = skipwhite(*arg + 1);
2184 }
2185
2186 if (**arg != '}')
2187 {
2188 semsg(_(e_missing_dict_end), *arg);
2189 goto failret;
2190 }
2191 *arg = *arg + 1;
2192
2193 dict_unref(d);
2194 return generate_NEWDICT(cctx, count);
2195
2196failret:
2197 dict_unref(d);
2198 return FAIL;
2199}
2200
2201/*
2202 * Compile "&option".
2203 */
2204 static int
2205compile_get_option(char_u **arg, cctx_T *cctx)
2206{
2207 typval_T rettv;
2208 char_u *start = *arg;
2209 int ret;
2210
2211 // parse the option and get the current value to get the type.
2212 rettv.v_type = VAR_UNKNOWN;
2213 ret = get_option_tv(arg, &rettv, TRUE);
2214 if (ret == OK)
2215 {
2216 // include the '&' in the name, get_option_tv() expects it.
2217 char_u *name = vim_strnsave(start, *arg - start);
2218 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2219
2220 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2221 vim_free(name);
2222 }
2223 clear_tv(&rettv);
2224
2225 return ret;
2226}
2227
2228/*
2229 * Compile "$VAR".
2230 */
2231 static int
2232compile_get_env(char_u **arg, cctx_T *cctx)
2233{
2234 char_u *start = *arg;
2235 int len;
2236 int ret;
2237 char_u *name;
2238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 ++*arg;
2240 len = get_env_len(arg);
2241 if (len == 0)
2242 {
2243 semsg(_(e_syntax_at), start - 1);
2244 return FAIL;
2245 }
2246
2247 // include the '$' in the name, get_env_tv() expects it.
2248 name = vim_strnsave(start, len + 1);
2249 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2250 vim_free(name);
2251 return ret;
2252}
2253
2254/*
2255 * Compile "@r".
2256 */
2257 static int
2258compile_get_register(char_u **arg, cctx_T *cctx)
2259{
2260 int ret;
2261
2262 ++*arg;
2263 if (**arg == NUL)
2264 {
2265 semsg(_(e_syntax_at), *arg - 1);
2266 return FAIL;
2267 }
2268 if (!valid_yank_reg(**arg, TRUE))
2269 {
2270 emsg_invreg(**arg);
2271 return FAIL;
2272 }
2273 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2274 ++*arg;
2275 return ret;
2276}
2277
2278/*
2279 * Apply leading '!', '-' and '+' to constant "rettv".
2280 */
2281 static int
2282apply_leader(typval_T *rettv, char_u *start, char_u *end)
2283{
2284 char_u *p = end;
2285
2286 // this works from end to start
2287 while (p > start)
2288 {
2289 --p;
2290 if (*p == '-' || *p == '+')
2291 {
2292 // only '-' has an effect, for '+' we only check the type
2293#ifdef FEAT_FLOAT
2294 if (rettv->v_type == VAR_FLOAT)
2295 {
2296 if (*p == '-')
2297 rettv->vval.v_float = -rettv->vval.v_float;
2298 }
2299 else
2300#endif
2301 {
2302 varnumber_T val;
2303 int error = FALSE;
2304
2305 // tv_get_number_chk() accepts a string, but we don't want that
2306 // here
2307 if (check_not_string(rettv) == FAIL)
2308 return FAIL;
2309 val = tv_get_number_chk(rettv, &error);
2310 clear_tv(rettv);
2311 if (error)
2312 return FAIL;
2313 if (*p == '-')
2314 val = -val;
2315 rettv->v_type = VAR_NUMBER;
2316 rettv->vval.v_number = val;
2317 }
2318 }
2319 else
2320 {
2321 int v = tv2bool(rettv);
2322
2323 // '!' is permissive in the type.
2324 clear_tv(rettv);
2325 rettv->v_type = VAR_BOOL;
2326 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2327 }
2328 }
2329 return OK;
2330}
2331
2332/*
2333 * Recognize v: variables that are constants and set "rettv".
2334 */
2335 static void
2336get_vim_constant(char_u **arg, typval_T *rettv)
2337{
2338 if (STRNCMP(*arg, "v:true", 6) == 0)
2339 {
2340 rettv->v_type = VAR_BOOL;
2341 rettv->vval.v_number = VVAL_TRUE;
2342 *arg += 6;
2343 }
2344 else if (STRNCMP(*arg, "v:false", 7) == 0)
2345 {
2346 rettv->v_type = VAR_BOOL;
2347 rettv->vval.v_number = VVAL_FALSE;
2348 *arg += 7;
2349 }
2350 else if (STRNCMP(*arg, "v:null", 6) == 0)
2351 {
2352 rettv->v_type = VAR_SPECIAL;
2353 rettv->vval.v_number = VVAL_NULL;
2354 *arg += 6;
2355 }
2356 else if (STRNCMP(*arg, "v:none", 6) == 0)
2357 {
2358 rettv->v_type = VAR_SPECIAL;
2359 rettv->vval.v_number = VVAL_NONE;
2360 *arg += 6;
2361 }
2362}
2363
2364/*
2365 * Compile code to apply '-', '+' and '!'.
2366 */
2367 static int
2368compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2369{
2370 char_u *p = end;
2371
2372 // this works from end to start
2373 while (p > start)
2374 {
2375 --p;
2376 if (*p == '-' || *p == '+')
2377 {
2378 int negate = *p == '-';
2379 isn_T *isn;
2380
2381 // TODO: check type
2382 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2383 {
2384 --p;
2385 if (*p == '-')
2386 negate = !negate;
2387 }
2388 // only '-' has an effect, for '+' we only check the type
2389 if (negate)
2390 isn = generate_instr(cctx, ISN_NEGATENR);
2391 else
2392 isn = generate_instr(cctx, ISN_CHECKNR);
2393 if (isn == NULL)
2394 return FAIL;
2395 }
2396 else
2397 {
2398 int invert = TRUE;
2399
2400 while (p > start && p[-1] == '!')
2401 {
2402 --p;
2403 invert = !invert;
2404 }
2405 if (generate_2BOOL(cctx, invert) == FAIL)
2406 return FAIL;
2407 }
2408 }
2409 return OK;
2410}
2411
2412/*
2413 * Compile whatever comes after "name" or "name()".
2414 */
2415 static int
2416compile_subscript(
2417 char_u **arg,
2418 cctx_T *cctx,
2419 char_u **start_leader,
2420 char_u *end_leader)
2421{
2422 for (;;)
2423 {
2424 if (**arg == '(')
2425 {
2426 int argcount = 0;
2427
2428 // funcref(arg)
2429 *arg = skipwhite(*arg + 1);
2430 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2431 return FAIL;
2432 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2433 return FAIL;
2434 }
2435 else if (**arg == '-' && (*arg)[1] == '>')
2436 {
2437 char_u *p;
2438
2439 // something->method()
2440 // Apply the '!', '-' and '+' first:
2441 // -1.0->func() works like (-1.0)->func()
2442 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2443 return FAIL;
2444 *start_leader = end_leader; // don't apply again later
2445
2446 *arg = skipwhite(*arg + 2);
2447 if (**arg == '{')
2448 {
2449 // lambda call: list->{lambda}
2450 if (compile_lambda_call(arg, cctx) == FAIL)
2451 return FAIL;
2452 }
2453 else
2454 {
2455 // method call: list->method()
2456 for (p = *arg; eval_isnamec1(*p); ++p)
2457 ;
2458 if (*p != '(')
2459 {
2460 semsg(_(e_missing_paren), arg);
2461 return FAIL;
2462 }
2463 // TODO: base value may not be the first argument
2464 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2465 return FAIL;
2466 }
2467 }
2468 else if (**arg == '[')
2469 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002470 garray_T *stack;
2471 type_T **typep;
2472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 // list index: list[123]
2474 // TODO: more arguments
2475 // TODO: dict member dict['name']
2476 *arg = skipwhite(*arg + 1);
2477 if (compile_expr1(arg, cctx) == FAIL)
2478 return FAIL;
2479
2480 if (**arg != ']')
2481 {
2482 emsg(_(e_missbrac));
2483 return FAIL;
2484 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002485 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486
2487 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2488 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002489 stack = &cctx->ctx_type_stack;
2490 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2491 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2492 {
2493 emsg(_(e_listreq));
2494 return FAIL;
2495 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002496 if ((*typep)->tt_type == VAR_LIST)
2497 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 }
2499 else if (**arg == '.' && (*arg)[1] != '.')
2500 {
2501 char_u *p;
2502
2503 ++*arg;
2504 p = *arg;
2505 // dictionary member: dict.name
2506 if (eval_isnamec1(*p))
2507 while (eval_isnamec(*p))
2508 MB_PTR_ADV(p);
2509 if (p == *arg)
2510 {
2511 semsg(_(e_syntax_at), *arg);
2512 return FAIL;
2513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2515 return FAIL;
2516 *arg = p;
2517 }
2518 else
2519 break;
2520 }
2521
2522 // TODO - see handle_subscript():
2523 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2524 // Don't do this when "Func" is already a partial that was bound
2525 // explicitly (pt_auto is FALSE).
2526
2527 return OK;
2528}
2529
2530/*
2531 * Compile an expression at "*p" and add instructions to "instr".
2532 * "p" is advanced until after the expression, skipping white space.
2533 *
2534 * This is the equivalent of eval1(), eval2(), etc.
2535 */
2536
2537/*
2538 * number number constant
2539 * 0zFFFFFFFF Blob constant
2540 * "string" string constant
2541 * 'string' literal string constant
2542 * &option-name option value
2543 * @r register contents
2544 * identifier variable value
2545 * function() function call
2546 * $VAR environment variable
2547 * (expression) nested expression
2548 * [expr, expr] List
2549 * {key: val, key: val} Dictionary
2550 * #{key: val, key: val} Dictionary with literal keys
2551 *
2552 * Also handle:
2553 * ! in front logical NOT
2554 * - in front unary minus
2555 * + in front unary plus (ignored)
2556 * trailing (arg) funcref/partial call
2557 * trailing [] subscript in String or List
2558 * trailing .name entry in Dictionary
2559 * trailing ->name() method call
2560 */
2561 static int
2562compile_expr7(char_u **arg, cctx_T *cctx)
2563{
2564 typval_T rettv;
2565 char_u *start_leader, *end_leader;
2566 int ret = OK;
2567
2568 /*
2569 * Skip '!', '-' and '+' characters. They are handled later.
2570 */
2571 start_leader = *arg;
2572 while (**arg == '!' || **arg == '-' || **arg == '+')
2573 *arg = skipwhite(*arg + 1);
2574 end_leader = *arg;
2575
2576 rettv.v_type = VAR_UNKNOWN;
2577 switch (**arg)
2578 {
2579 /*
2580 * Number constant.
2581 */
2582 case '0': // also for blob starting with 0z
2583 case '1':
2584 case '2':
2585 case '3':
2586 case '4':
2587 case '5':
2588 case '6':
2589 case '7':
2590 case '8':
2591 case '9':
2592 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2593 return FAIL;
2594 break;
2595
2596 /*
2597 * String constant: "string".
2598 */
2599 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2600 return FAIL;
2601 break;
2602
2603 /*
2604 * Literal string constant: 'str''ing'.
2605 */
2606 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2607 return FAIL;
2608 break;
2609
2610 /*
2611 * Constant Vim variable.
2612 */
2613 case 'v': get_vim_constant(arg, &rettv);
2614 ret = NOTDONE;
2615 break;
2616
2617 /*
2618 * List: [expr, expr]
2619 */
2620 case '[': ret = compile_list(arg, cctx);
2621 break;
2622
2623 /*
2624 * Dictionary: #{key: val, key: val}
2625 */
2626 case '#': if ((*arg)[1] == '{')
2627 {
2628 ++*arg;
2629 ret = compile_dict(arg, cctx, TRUE);
2630 }
2631 else
2632 ret = NOTDONE;
2633 break;
2634
2635 /*
2636 * Lambda: {arg, arg -> expr}
2637 * Dictionary: {'key': val, 'key': val}
2638 */
2639 case '{': {
2640 char_u *start = skipwhite(*arg + 1);
2641
2642 // Find out what comes after the arguments.
2643 ret = get_function_args(&start, '-', NULL,
2644 NULL, NULL, NULL, TRUE);
2645 if (ret != FAIL && *start == '>')
2646 ret = compile_lambda(arg, cctx);
2647 else
2648 ret = compile_dict(arg, cctx, FALSE);
2649 }
2650 break;
2651
2652 /*
2653 * Option value: &name
2654 */
2655 case '&': ret = compile_get_option(arg, cctx);
2656 break;
2657
2658 /*
2659 * Environment variable: $VAR.
2660 */
2661 case '$': ret = compile_get_env(arg, cctx);
2662 break;
2663
2664 /*
2665 * Register contents: @r.
2666 */
2667 case '@': ret = compile_get_register(arg, cctx);
2668 break;
2669 /*
2670 * nested expression: (expression).
2671 */
2672 case '(': *arg = skipwhite(*arg + 1);
2673 ret = compile_expr1(arg, cctx); // recursive!
2674 *arg = skipwhite(*arg);
2675 if (**arg == ')')
2676 ++*arg;
2677 else if (ret == OK)
2678 {
2679 emsg(_(e_missing_close));
2680 ret = FAIL;
2681 }
2682 break;
2683
2684 default: ret = NOTDONE;
2685 break;
2686 }
2687 if (ret == FAIL)
2688 return FAIL;
2689
2690 if (rettv.v_type != VAR_UNKNOWN)
2691 {
2692 // apply the '!', '-' and '+' before the constant
2693 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2694 {
2695 clear_tv(&rettv);
2696 return FAIL;
2697 }
2698 start_leader = end_leader; // don't apply again below
2699
2700 // push constant
2701 switch (rettv.v_type)
2702 {
2703 case VAR_BOOL:
2704 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2705 break;
2706 case VAR_SPECIAL:
2707 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2708 break;
2709 case VAR_NUMBER:
2710 generate_PUSHNR(cctx, rettv.vval.v_number);
2711 break;
2712#ifdef FEAT_FLOAT
2713 case VAR_FLOAT:
2714 generate_PUSHF(cctx, rettv.vval.v_float);
2715 break;
2716#endif
2717 case VAR_BLOB:
2718 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2719 rettv.vval.v_blob = NULL;
2720 break;
2721 case VAR_STRING:
2722 generate_PUSHS(cctx, rettv.vval.v_string);
2723 rettv.vval.v_string = NULL;
2724 break;
2725 default:
2726 iemsg("constant type missing");
2727 return FAIL;
2728 }
2729 }
2730 else if (ret == NOTDONE)
2731 {
2732 char_u *p;
2733 int r;
2734
2735 if (!eval_isnamec1(**arg))
2736 {
2737 semsg(_("E1015: Name expected: %s"), *arg);
2738 return FAIL;
2739 }
2740
2741 // "name" or "name()"
2742 p = to_name_end(*arg);
2743 if (*p == '(')
2744 r = compile_call(arg, p - *arg, cctx, 0);
2745 else
2746 r = compile_load(arg, p, cctx, TRUE);
2747 if (r == FAIL)
2748 return FAIL;
2749 }
2750
2751 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2752 return FAIL;
2753
2754 // Now deal with prefixed '-', '+' and '!', if not done already.
2755 return compile_leader(cctx, start_leader, end_leader);
2756}
2757
2758/*
2759 * * number multiplication
2760 * / number division
2761 * % number modulo
2762 */
2763 static int
2764compile_expr6(char_u **arg, cctx_T *cctx)
2765{
2766 char_u *op;
2767
2768 // get the first variable
2769 if (compile_expr7(arg, cctx) == FAIL)
2770 return FAIL;
2771
2772 /*
2773 * Repeat computing, until no "*", "/" or "%" is following.
2774 */
2775 for (;;)
2776 {
2777 op = skipwhite(*arg);
2778 if (*op != '*' && *op != '/' && *op != '%')
2779 break;
2780 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2781 {
2782 char_u buf[3];
2783
2784 vim_strncpy(buf, op, 1);
2785 semsg(_(e_white_both), buf);
2786 }
2787 *arg = skipwhite(op + 1);
2788
2789 // get the second variable
2790 if (compile_expr7(arg, cctx) == FAIL)
2791 return FAIL;
2792
2793 generate_two_op(cctx, op);
2794 }
2795
2796 return OK;
2797}
2798
2799/*
2800 * + number addition
2801 * - number subtraction
2802 * .. string concatenation
2803 */
2804 static int
2805compile_expr5(char_u **arg, cctx_T *cctx)
2806{
2807 char_u *op;
2808 int oplen;
2809
2810 // get the first variable
2811 if (compile_expr6(arg, cctx) == FAIL)
2812 return FAIL;
2813
2814 /*
2815 * Repeat computing, until no "+", "-" or ".." is following.
2816 */
2817 for (;;)
2818 {
2819 op = skipwhite(*arg);
2820 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2821 break;
2822 oplen = (*op == '.' ? 2 : 1);
2823
2824 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2825 {
2826 char_u buf[3];
2827
2828 vim_strncpy(buf, op, oplen);
2829 semsg(_(e_white_both), buf);
2830 }
2831
2832 *arg = skipwhite(op + oplen);
2833
2834 // get the second variable
2835 if (compile_expr6(arg, cctx) == FAIL)
2836 return FAIL;
2837
2838 if (*op == '.')
2839 {
2840 if (may_generate_2STRING(-2, cctx) == FAIL
2841 || may_generate_2STRING(-1, cctx) == FAIL)
2842 return FAIL;
2843 generate_instr_drop(cctx, ISN_CONCAT, 1);
2844 }
2845 else
2846 generate_two_op(cctx, op);
2847 }
2848
2849 return OK;
2850}
2851
2852/*
2853 * expr5a == expr5b
2854 * expr5a =~ expr5b
2855 * expr5a != expr5b
2856 * expr5a !~ expr5b
2857 * expr5a > expr5b
2858 * expr5a >= expr5b
2859 * expr5a < expr5b
2860 * expr5a <= expr5b
2861 * expr5a is expr5b
2862 * expr5a isnot expr5b
2863 *
2864 * Produces instructions:
2865 * EVAL expr5a Push result of "expr5a"
2866 * EVAL expr5b Push result of "expr5b"
2867 * COMPARE one of the compare instructions
2868 */
2869 static int
2870compile_expr4(char_u **arg, cctx_T *cctx)
2871{
2872 exptype_T type = EXPR_UNKNOWN;
2873 char_u *p;
2874 int len = 2;
2875 int i;
2876 int type_is = FALSE;
2877
2878 // get the first variable
2879 if (compile_expr5(arg, cctx) == FAIL)
2880 return FAIL;
2881
2882 p = skipwhite(*arg);
2883 switch (p[0])
2884 {
2885 case '=': if (p[1] == '=')
2886 type = EXPR_EQUAL;
2887 else if (p[1] == '~')
2888 type = EXPR_MATCH;
2889 break;
2890 case '!': if (p[1] == '=')
2891 type = EXPR_NEQUAL;
2892 else if (p[1] == '~')
2893 type = EXPR_NOMATCH;
2894 break;
2895 case '>': if (p[1] != '=')
2896 {
2897 type = EXPR_GREATER;
2898 len = 1;
2899 }
2900 else
2901 type = EXPR_GEQUAL;
2902 break;
2903 case '<': if (p[1] != '=')
2904 {
2905 type = EXPR_SMALLER;
2906 len = 1;
2907 }
2908 else
2909 type = EXPR_SEQUAL;
2910 break;
2911 case 'i': if (p[1] == 's')
2912 {
2913 // "is" and "isnot"; but not a prefix of a name
2914 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2915 len = 5;
2916 i = p[len];
2917 if (!isalnum(i) && i != '_')
2918 {
2919 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2920 type_is = TRUE;
2921 }
2922 }
2923 break;
2924 }
2925
2926 /*
2927 * If there is a comparative operator, use it.
2928 */
2929 if (type != EXPR_UNKNOWN)
2930 {
2931 int ic = FALSE; // Default: do not ignore case
2932
2933 if (type_is && (p[len] == '?' || p[len] == '#'))
2934 {
2935 semsg(_(e_invexpr2), *arg);
2936 return FAIL;
2937 }
2938 // extra question mark appended: ignore case
2939 if (p[len] == '?')
2940 {
2941 ic = TRUE;
2942 ++len;
2943 }
2944 // extra '#' appended: match case (ignored)
2945 else if (p[len] == '#')
2946 ++len;
2947 // nothing appended: match case
2948
2949 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2950 {
2951 char_u buf[7];
2952
2953 vim_strncpy(buf, p, len);
2954 semsg(_(e_white_both), buf);
2955 }
2956
2957 // get the second variable
2958 *arg = skipwhite(p + len);
2959 if (compile_expr5(arg, cctx) == FAIL)
2960 return FAIL;
2961
2962 generate_COMPARE(cctx, type, ic);
2963 }
2964
2965 return OK;
2966}
2967
2968/*
2969 * Compile || or &&.
2970 */
2971 static int
2972compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2973{
2974 char_u *p = skipwhite(*arg);
2975 int opchar = *op;
2976
2977 if (p[0] == opchar && p[1] == opchar)
2978 {
2979 garray_T *instr = &cctx->ctx_instr;
2980 garray_T end_ga;
2981
2982 /*
2983 * Repeat until there is no following "||" or "&&"
2984 */
2985 ga_init2(&end_ga, sizeof(int), 10);
2986 while (p[0] == opchar && p[1] == opchar)
2987 {
2988 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2989 semsg(_(e_white_both), op);
2990
2991 if (ga_grow(&end_ga, 1) == FAIL)
2992 {
2993 ga_clear(&end_ga);
2994 return FAIL;
2995 }
2996 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2997 ++end_ga.ga_len;
2998 generate_JUMP(cctx, opchar == '|'
2999 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3000
3001 // eval the next expression
3002 *arg = skipwhite(p + 2);
3003 if ((opchar == '|' ? compile_expr3(arg, cctx)
3004 : compile_expr4(arg, cctx)) == FAIL)
3005 {
3006 ga_clear(&end_ga);
3007 return FAIL;
3008 }
3009 p = skipwhite(*arg);
3010 }
3011
3012 // Fill in the end label in all jumps.
3013 while (end_ga.ga_len > 0)
3014 {
3015 isn_T *isn;
3016
3017 --end_ga.ga_len;
3018 isn = ((isn_T *)instr->ga_data)
3019 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3020 isn->isn_arg.jump.jump_where = instr->ga_len;
3021 }
3022 ga_clear(&end_ga);
3023 }
3024
3025 return OK;
3026}
3027
3028/*
3029 * expr4a && expr4a && expr4a logical AND
3030 *
3031 * Produces instructions:
3032 * EVAL expr4a Push result of "expr4a"
3033 * JUMP_AND_KEEP_IF_FALSE end
3034 * EVAL expr4b Push result of "expr4b"
3035 * JUMP_AND_KEEP_IF_FALSE end
3036 * EVAL expr4c Push result of "expr4c"
3037 * end:
3038 */
3039 static int
3040compile_expr3(char_u **arg, cctx_T *cctx)
3041{
3042 // get the first variable
3043 if (compile_expr4(arg, cctx) == FAIL)
3044 return FAIL;
3045
3046 // || and && work almost the same
3047 return compile_and_or(arg, cctx, "&&");
3048}
3049
3050/*
3051 * expr3a || expr3b || expr3c logical OR
3052 *
3053 * Produces instructions:
3054 * EVAL expr3a Push result of "expr3a"
3055 * JUMP_AND_KEEP_IF_TRUE end
3056 * EVAL expr3b Push result of "expr3b"
3057 * JUMP_AND_KEEP_IF_TRUE end
3058 * EVAL expr3c Push result of "expr3c"
3059 * end:
3060 */
3061 static int
3062compile_expr2(char_u **arg, cctx_T *cctx)
3063{
3064 // eval the first expression
3065 if (compile_expr3(arg, cctx) == FAIL)
3066 return FAIL;
3067
3068 // || and && work almost the same
3069 return compile_and_or(arg, cctx, "||");
3070}
3071
3072/*
3073 * Toplevel expression: expr2 ? expr1a : expr1b
3074 *
3075 * Produces instructions:
3076 * EVAL expr2 Push result of "expr"
3077 * JUMP_IF_FALSE alt jump if false
3078 * EVAL expr1a
3079 * JUMP_ALWAYS end
3080 * alt: EVAL expr1b
3081 * end:
3082 */
3083 static int
3084compile_expr1(char_u **arg, cctx_T *cctx)
3085{
3086 char_u *p;
3087
3088 // evaluate the first expression
3089 if (compile_expr2(arg, cctx) == FAIL)
3090 return FAIL;
3091
3092 p = skipwhite(*arg);
3093 if (*p == '?')
3094 {
3095 garray_T *instr = &cctx->ctx_instr;
3096 garray_T *stack = &cctx->ctx_type_stack;
3097 int alt_idx = instr->ga_len;
3098 int end_idx;
3099 isn_T *isn;
3100 type_T *type1;
3101 type_T *type2;
3102
3103 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3104 semsg(_(e_white_both), "?");
3105
3106 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3107
3108 // evaluate the second expression; any type is accepted
3109 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003110 if (compile_expr1(arg, cctx) == FAIL)
3111 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112
3113 // remember the type and drop it
3114 --stack->ga_len;
3115 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3116
3117 end_idx = instr->ga_len;
3118 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3119
3120 // jump here from JUMP_IF_FALSE
3121 isn = ((isn_T *)instr->ga_data) + alt_idx;
3122 isn->isn_arg.jump.jump_where = instr->ga_len;
3123
3124 // Check for the ":".
3125 p = skipwhite(*arg);
3126 if (*p != ':')
3127 {
3128 emsg(_(e_missing_colon));
3129 return FAIL;
3130 }
3131 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3132 semsg(_(e_white_both), ":");
3133
3134 // evaluate the third expression
3135 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003136 if (compile_expr1(arg, cctx) == FAIL)
3137 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
3139 // If the types differ, the result has a more generic type.
3140 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3141 common_type(type1, type2, type2);
3142
3143 // jump here from JUMP_ALWAYS
3144 isn = ((isn_T *)instr->ga_data) + end_idx;
3145 isn->isn_arg.jump.jump_where = instr->ga_len;
3146 }
3147 return OK;
3148}
3149
3150/*
3151 * compile "return [expr]"
3152 */
3153 static char_u *
3154compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3155{
3156 char_u *p = arg;
3157 garray_T *stack = &cctx->ctx_type_stack;
3158 type_T *stack_type;
3159
3160 if (*p != NUL && *p != '|' && *p != '\n')
3161 {
3162 // compile return argument into instructions
3163 if (compile_expr1(&p, cctx) == FAIL)
3164 return NULL;
3165
3166 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3167 if (set_return_type)
3168 cctx->ctx_ufunc->uf_ret_type = stack_type;
3169 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3170 == FAIL)
3171 return NULL;
3172 }
3173 else
3174 {
3175 if (set_return_type)
3176 cctx->ctx_ufunc->uf_ret_type = &t_void;
3177 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3178 {
3179 emsg(_("E1003: Missing return value"));
3180 return NULL;
3181 }
3182
3183 // No argument, return zero.
3184 generate_PUSHNR(cctx, 0);
3185 }
3186
3187 if (generate_instr(cctx, ISN_RETURN) == NULL)
3188 return NULL;
3189
3190 // "return val | endif" is possible
3191 return skipwhite(p);
3192}
3193
3194/*
3195 * Return the length of an assignment operator, or zero if there isn't one.
3196 */
3197 int
3198assignment_len(char_u *p, int *heredoc)
3199{
3200 if (*p == '=')
3201 {
3202 if (p[1] == '<' && p[2] == '<')
3203 {
3204 *heredoc = TRUE;
3205 return 3;
3206 }
3207 return 1;
3208 }
3209 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3210 return 2;
3211 if (STRNCMP(p, "..=", 3) == 0)
3212 return 3;
3213 return 0;
3214}
3215
3216// words that cannot be used as a variable
3217static char *reserved[] = {
3218 "true",
3219 "false",
3220 NULL
3221};
3222
3223/*
3224 * Get a line for "=<<".
3225 * Return a pointer to the line in allocated memory.
3226 * Return NULL for end-of-file or some error.
3227 */
3228 static char_u *
3229heredoc_getline(
3230 int c UNUSED,
3231 void *cookie,
3232 int indent UNUSED,
3233 int do_concat UNUSED)
3234{
3235 cctx_T *cctx = (cctx_T *)cookie;
3236
3237 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3238 NULL;
3239 ++cctx->ctx_lnum;
3240 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3241 [cctx->ctx_lnum]);
3242}
3243
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003244typedef enum {
3245 dest_local,
3246 dest_option,
3247 dest_env,
3248 dest_global,
3249 dest_vimvar,
3250 dest_script,
3251 dest_reg,
3252} assign_dest_T;
3253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254/*
3255 * compile "let var [= expr]", "const var = expr" and "var = expr"
3256 * "arg" points to "var".
3257 */
3258 static char_u *
3259compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3260{
3261 char_u *p;
3262 char_u *ret = NULL;
3263 int var_count = 0;
3264 int semicolon = 0;
3265 size_t varlen;
3266 garray_T *instr = &cctx->ctx_instr;
3267 int idx = -1;
3268 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003270 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003272 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 int oplen = 0;
3274 int heredoc = FALSE;
3275 type_T *type;
3276 lvar_T *lvar;
3277 char_u *name;
3278 char_u *sp;
3279 int has_type = FALSE;
3280 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3281 int instr_count = -1;
3282
3283 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3284 if (p == NULL)
3285 return NULL;
3286 if (var_count > 0)
3287 {
3288 // TODO: let [var, var] = list
3289 emsg("Cannot handle a list yet");
3290 return NULL;
3291 }
3292
3293 varlen = p - arg;
3294 name = vim_strnsave(arg, (int)varlen);
3295 if (name == NULL)
3296 return NULL;
3297
3298 if (*arg == '&')
3299 {
3300 int cc;
3301 long numval;
3302 char_u *stringval = NULL;
3303
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003304 dest = dest_option;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (cmdidx == CMD_const)
3306 {
3307 emsg(_(e_const_option));
3308 return NULL;
3309 }
3310 if (is_decl)
3311 {
3312 semsg(_("E1052: Cannot declare an option: %s"), arg);
3313 goto theend;
3314 }
3315 p = arg;
3316 p = find_option_end(&p, &opt_flags);
3317 if (p == NULL)
3318 {
3319 emsg(_(e_letunexp));
3320 return NULL;
3321 }
3322 cc = *p;
3323 *p = NUL;
3324 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3325 *p = cc;
3326 if (opt_type == -3)
3327 {
3328 semsg(_(e_unknown_option), *arg);
3329 return NULL;
3330 }
3331 if (opt_type == -2 || opt_type == 0)
3332 type = &t_string;
3333 else
3334 type = &t_number; // both number and boolean option
3335 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003336 else if (*arg == '$')
3337 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003338 dest = dest_env;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003339 if (is_decl)
3340 {
3341 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3342 goto theend;
3343 }
3344 }
3345 else if (*arg == '@')
3346 {
3347 if (!valid_yank_reg(arg[1], TRUE))
3348 {
3349 emsg_invreg(arg[1]);
3350 return FAIL;
3351 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003352 dest = dest_reg;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003353 if (is_decl)
3354 {
3355 semsg(_("E1066: Cannot declare a register: %s"), name);
3356 goto theend;
3357 }
3358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 else if (STRNCMP(arg, "g:", 2) == 0)
3360 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003361 dest = dest_global;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 if (is_decl)
3363 {
3364 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3365 goto theend;
3366 }
3367 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003368 else if (STRNCMP(arg, "v:", 2) == 0)
3369 {
3370 vimvaridx = find_vim_var(name + 2);
3371 if (vimvaridx < 0)
3372 {
3373 semsg(_(e_var_notfound), arg);
3374 goto theend;
3375 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003376 dest = dest_vimvar;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003377 if (is_decl)
3378 {
3379 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3380 goto theend;
3381 }
3382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 else
3384 {
3385 for (idx = 0; reserved[idx] != NULL; ++idx)
3386 if (STRCMP(reserved[idx], name) == 0)
3387 {
3388 semsg(_("E1034: Cannot use reserved name %s"), name);
3389 goto theend;
3390 }
3391
3392 idx = lookup_local(arg, varlen, cctx);
3393 if (idx >= 0)
3394 {
3395 if (is_decl)
3396 {
3397 semsg(_("E1017: Variable already declared: %s"), name);
3398 goto theend;
3399 }
3400 else
3401 {
3402 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3403 if (lvar->lv_const)
3404 {
3405 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3406 goto theend;
3407 }
3408 }
3409 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003410 else if (STRNCMP(arg, "s:", 2) == 0
3411 || lookup_script(arg, varlen) == OK
3412 || find_imported(arg, varlen, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003414 dest = dest_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 if (is_decl)
3416 {
3417 semsg(_("E1054: Variable already declared in the script: %s"),
3418 name);
3419 goto theend;
3420 }
3421 }
3422 }
3423
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003424 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 {
3426 if (is_decl && *p == ':')
3427 {
3428 // parse optional type: "let var: type = expr"
3429 p = skipwhite(p + 1);
3430 type = parse_type(&p, cctx->ctx_type_list);
3431 if (type == NULL)
3432 goto theend;
3433 has_type = TRUE;
3434 }
3435 else if (idx < 0)
3436 {
3437 // global and new local default to "any" type
3438 type = &t_any;
3439 }
3440 else
3441 {
3442 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3443 type = lvar->lv_type;
3444 }
3445 }
3446
3447 sp = p;
3448 p = skipwhite(p);
3449 op = p;
3450 oplen = assignment_len(p, &heredoc);
3451 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3452 {
3453 char_u buf[4];
3454
3455 vim_strncpy(buf, op, oplen);
3456 semsg(_(e_white_both), buf);
3457 }
3458
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003459 if (oplen == 3 && !heredoc && dest != dest_global
3460 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003462 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 goto theend;
3464 }
3465
3466 // +=, /=, etc. require an existing variable
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003467 if (idx < 0 && dest == dest_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 {
3469 if (oplen > 1 && !heredoc)
3470 {
3471 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3472 name);
3473 goto theend;
3474 }
3475
3476 // new local variable
3477 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3478 if (idx < 0)
3479 goto theend;
3480 }
3481
3482 if (heredoc)
3483 {
3484 list_T *l;
3485 listitem_T *li;
3486
3487 // [let] varname =<< [trim] {end}
3488 eap->getline = heredoc_getline;
3489 eap->cookie = cctx;
3490 l = heredoc_get(eap, op + 3);
3491
3492 // Push each line and the create the list.
3493 for (li = l->lv_first; li != NULL; li = li->li_next)
3494 {
3495 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3496 li->li_tv.vval.v_string = NULL;
3497 }
3498 generate_NEWLIST(cctx, l->lv_len);
3499 type = &t_list_string;
3500 list_free(l);
3501 p += STRLEN(p);
3502 }
3503 else if (oplen > 0)
3504 {
3505 // for "+=", "*=", "..=" etc. first load the current value
3506 if (*op != '=')
3507 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003508 switch (dest)
3509 {
3510 case dest_option:
3511 // TODO: check the option exists
3512 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3513 break;
3514 case dest_global:
3515 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3516 break;
3517 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003518 compile_load_scriptvar(cctx,
3519 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003520 break;
3521 case dest_env:
3522 // Include $ in the name here
3523 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3524 break;
3525 case dest_reg:
3526 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3527 break;
3528 case dest_vimvar:
3529 generate_LOADV(cctx, name + 2, TRUE);
3530 break;
3531 case dest_local:
3532 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3533 break;
3534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536
3537 // compile the expression
3538 instr_count = instr->ga_len;
3539 p = skipwhite(p + oplen);
3540 if (compile_expr1(&p, cctx) == FAIL)
3541 goto theend;
3542
3543 if (idx >= 0 && (is_decl || !has_type))
3544 {
3545 garray_T *stack = &cctx->ctx_type_stack;
3546 type_T *stacktype =
3547 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3548
3549 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3550 if (!has_type)
3551 {
3552 if (stacktype->tt_type == VAR_VOID)
3553 {
3554 emsg(_("E1031: Cannot use void value"));
3555 goto theend;
3556 }
3557 else
3558 lvar->lv_type = stacktype;
3559 }
3560 else
3561 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3562 goto theend;
3563 }
3564 }
3565 else if (cmdidx == CMD_const)
3566 {
3567 emsg(_("E1021: const requires a value"));
3568 goto theend;
3569 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003570 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 {
3572 emsg(_("E1022: type or initialization required"));
3573 goto theend;
3574 }
3575 else
3576 {
3577 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 if (ga_grow(instr, 1) == FAIL)
3579 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003580 switch (type->tt_type)
3581 {
3582 case VAR_BOOL:
3583 generate_PUSHBOOL(cctx, VVAL_FALSE);
3584 break;
3585 case VAR_SPECIAL:
3586 generate_PUSHSPEC(cctx, VVAL_NONE);
3587 break;
3588 case VAR_FLOAT:
3589#ifdef FEAT_FLOAT
3590 generate_PUSHF(cctx, 0.0);
3591#endif
3592 break;
3593 case VAR_STRING:
3594 generate_PUSHS(cctx, NULL);
3595 break;
3596 case VAR_BLOB:
3597 generate_PUSHBLOB(cctx, NULL);
3598 break;
3599 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003600 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003601 break;
3602 case VAR_PARTIAL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003603 // generate_PUSHPARTIAL(cctx, NULL);
3604 emsg("Partial type not supported yet");
Bram Moolenaar04d05222020-02-06 22:06:54 +01003605 break;
3606 case VAR_LIST:
3607 generate_NEWLIST(cctx, 0);
3608 break;
3609 case VAR_DICT:
3610 generate_NEWDICT(cctx, 0);
3611 break;
3612 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003613 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003614 break;
3615 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003616 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003617 break;
3618 case VAR_NUMBER:
3619 case VAR_UNKNOWN:
3620 case VAR_VOID:
3621 generate_PUSHNR(cctx, 0);
3622 break;
3623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
3625
3626 if (oplen > 0 && *op != '=')
3627 {
3628 type_T *expected = &t_number;
3629 garray_T *stack = &cctx->ctx_type_stack;
3630 type_T *stacktype;
3631
3632 // TODO: if type is known use float or any operation
3633
3634 if (*op == '.')
3635 expected = &t_string;
3636 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3637 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3638 goto theend;
3639
3640 if (*op == '.')
3641 generate_instr_drop(cctx, ISN_CONCAT, 1);
3642 else
3643 {
3644 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3645
3646 if (isn == NULL)
3647 goto theend;
3648 switch (*op)
3649 {
3650 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3651 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3652 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3653 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3654 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3655 }
3656 }
3657 }
3658
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003659 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003661 case dest_option:
3662 generate_STOREOPT(cctx, name + 1, opt_flags);
3663 break;
3664 case dest_global:
3665 // include g: with the name, easier to execute that way
3666 generate_STORE(cctx, ISN_STOREG, 0, name);
3667 break;
3668 case dest_env:
3669 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3670 break;
3671 case dest_reg:
3672 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3673 break;
3674 case dest_vimvar:
3675 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3676 break;
3677 case dest_script:
3678 {
3679 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3680 imported_T *import = NULL;
3681 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003682
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003683 if (name[1] != ':')
3684 {
3685 import = find_imported(name, 0, cctx);
3686 if (import != NULL)
3687 sid = import->imp_sid;
3688 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003690 idx = get_script_item_idx(sid, rawname, TRUE);
3691 // TODO: specific type
3692 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003693 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003694 else
3695 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3696 sid, idx, &t_any);
3697 }
3698 break;
3699 case dest_local:
3700 {
3701 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003703 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3704 // into ISN_STORENR
3705 if (instr->ga_len == instr_count + 1
3706 && isn->isn_type == ISN_PUSHNR)
3707 {
3708 varnumber_T val = isn->isn_arg.number;
3709 garray_T *stack = &cctx->ctx_type_stack;
3710
3711 isn->isn_type = ISN_STORENR;
3712 isn->isn_arg.storenr.str_idx = idx;
3713 isn->isn_arg.storenr.str_val = val;
3714 if (stack->ga_len > 0)
3715 --stack->ga_len;
3716 }
3717 else
3718 generate_STORE(cctx, ISN_STORE, idx, NULL);
3719 }
3720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
3722 ret = p;
3723
3724theend:
3725 vim_free(name);
3726 return ret;
3727}
3728
3729/*
3730 * Compile an :import command.
3731 */
3732 static char_u *
3733compile_import(char_u *arg, cctx_T *cctx)
3734{
3735 return handle_import(arg, &cctx->ctx_imports, 0);
3736}
3737
3738/*
3739 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3740 */
3741 static int
3742compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3743{
3744 garray_T *instr = &cctx->ctx_instr;
3745 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3746
3747 if (endlabel == NULL)
3748 return FAIL;
3749 endlabel->el_next = *el;
3750 *el = endlabel;
3751 endlabel->el_end_label = instr->ga_len;
3752
3753 generate_JUMP(cctx, when, 0);
3754 return OK;
3755}
3756
3757 static void
3758compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3759{
3760 garray_T *instr = &cctx->ctx_instr;
3761
3762 while (*el != NULL)
3763 {
3764 endlabel_T *cur = (*el);
3765 isn_T *isn;
3766
3767 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3768 isn->isn_arg.jump.jump_where = instr->ga_len;
3769 *el = cur->el_next;
3770 vim_free(cur);
3771 }
3772}
3773
3774/*
3775 * Create a new scope and set up the generic items.
3776 */
3777 static scope_T *
3778new_scope(cctx_T *cctx, scopetype_T type)
3779{
3780 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3781
3782 if (scope == NULL)
3783 return NULL;
3784 scope->se_outer = cctx->ctx_scope;
3785 cctx->ctx_scope = scope;
3786 scope->se_type = type;
3787 scope->se_local_count = cctx->ctx_locals.ga_len;
3788 return scope;
3789}
3790
3791/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003792 * Evaluate an expression that is a constant:
3793 * has(arg)
3794 *
3795 * Also handle:
3796 * ! in front logical NOT
3797 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003798 * Return FAIL if the expression is not a constant.
3799 */
3800 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003801evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003802{
3803 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003804 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003805
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003806 /*
3807 * Skip '!' characters. They are handled later.
3808 */
3809 start_leader = *arg;
3810 while (**arg == '!')
3811 *arg = skipwhite(*arg + 1);
3812 end_leader = *arg;
3813
3814 /*
3815 * Recognize only has() for now.
3816 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003817 if (STRNCMP("has(", *arg, 4) != 0)
3818 return FAIL;
3819 *arg = skipwhite(*arg + 4);
3820
3821 if (**arg == '"')
3822 {
3823 if (get_string_tv(arg, tv, TRUE) == FAIL)
3824 return FAIL;
3825 }
3826 else if (**arg == '\'')
3827 {
3828 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3829 return FAIL;
3830 }
3831 else
3832 return FAIL;
3833
3834 *arg = skipwhite(*arg);
3835 if (**arg != ')')
3836 return FAIL;
3837 *arg = skipwhite(*arg + 1);
3838
3839 argvars[0] = *tv;
3840 argvars[1].v_type = VAR_UNKNOWN;
3841 tv->v_type = VAR_NUMBER;
3842 tv->vval.v_number = 0;
3843 f_has(argvars, tv);
3844 clear_tv(&argvars[0]);
3845
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003846 while (start_leader < end_leader)
3847 {
3848 if (*start_leader == '!')
3849 tv->vval.v_number = !tv->vval.v_number;
3850 ++start_leader;
3851 }
3852
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003853 return OK;
3854}
3855
3856static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3857
3858/*
3859 * Compile constant || or &&.
3860 */
3861 static int
3862evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3863{
3864 char_u *p = skipwhite(*arg);
3865 int opchar = *op;
3866
3867 if (p[0] == opchar && p[1] == opchar)
3868 {
3869 int val = tv2bool(tv);
3870
3871 /*
3872 * Repeat until there is no following "||" or "&&"
3873 */
3874 while (p[0] == opchar && p[1] == opchar)
3875 {
3876 typval_T tv2;
3877
3878 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3879 return FAIL;
3880
3881 // eval the next expression
3882 *arg = skipwhite(p + 2);
3883 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01003884 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003885 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003886 : evaluate_const_expr7(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003887 {
3888 clear_tv(&tv2);
3889 return FAIL;
3890 }
3891 if ((opchar == '&') == val)
3892 {
3893 // false || tv2 or true && tv2: use tv2
3894 clear_tv(tv);
3895 *tv = tv2;
3896 val = tv2bool(tv);
3897 }
3898 else
3899 clear_tv(&tv2);
3900 p = skipwhite(*arg);
3901 }
3902 }
3903
3904 return OK;
3905}
3906
3907/*
3908 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3909 * Return FAIL if the expression is not a constant.
3910 */
3911 static int
3912evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3913{
3914 // evaluate the first expression
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003915 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003916 return FAIL;
3917
3918 // || and && work almost the same
3919 return evaluate_const_and_or(arg, cctx, "&&", tv);
3920}
3921
3922/*
3923 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3924 * Return FAIL if the expression is not a constant.
3925 */
3926 static int
3927evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3928{
3929 // evaluate the first expression
3930 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3931 return FAIL;
3932
3933 // || and && work almost the same
3934 return evaluate_const_and_or(arg, cctx, "||", tv);
3935}
3936
3937/*
3938 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3939 * E.g. for "has('feature')".
3940 * This does not produce error messages. "tv" should be cleared afterwards.
3941 * Return FAIL if the expression is not a constant.
3942 */
3943 static int
3944evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3945{
3946 char_u *p;
3947
3948 // evaluate the first expression
3949 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3950 return FAIL;
3951
3952 p = skipwhite(*arg);
3953 if (*p == '?')
3954 {
3955 int val = tv2bool(tv);
3956 typval_T tv2;
3957
3958 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3959 return FAIL;
3960
3961 // evaluate the second expression; any type is accepted
3962 clear_tv(tv);
3963 *arg = skipwhite(p + 1);
3964 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3965 return FAIL;
3966
3967 // Check for the ":".
3968 p = skipwhite(*arg);
3969 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3970 return FAIL;
3971
3972 // evaluate the third expression
3973 *arg = skipwhite(p + 1);
3974 tv2.v_type = VAR_UNKNOWN;
3975 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3976 {
3977 clear_tv(&tv2);
3978 return FAIL;
3979 }
3980 if (val)
3981 {
3982 // use the expr after "?"
3983 clear_tv(&tv2);
3984 }
3985 else
3986 {
3987 // use the expr after ":"
3988 clear_tv(tv);
3989 *tv = tv2;
3990 }
3991 }
3992 return OK;
3993}
3994
3995/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 * compile "if expr"
3997 *
3998 * "if expr" Produces instructions:
3999 * EVAL expr Push result of "expr"
4000 * JUMP_IF_FALSE end
4001 * ... body ...
4002 * end:
4003 *
4004 * "if expr | else" Produces instructions:
4005 * EVAL expr Push result of "expr"
4006 * JUMP_IF_FALSE else
4007 * ... body ...
4008 * JUMP_ALWAYS end
4009 * else:
4010 * ... body ...
4011 * end:
4012 *
4013 * "if expr1 | elseif expr2 | else" Produces instructions:
4014 * EVAL expr Push result of "expr"
4015 * JUMP_IF_FALSE elseif
4016 * ... body ...
4017 * JUMP_ALWAYS end
4018 * elseif:
4019 * EVAL expr Push result of "expr"
4020 * JUMP_IF_FALSE else
4021 * ... body ...
4022 * JUMP_ALWAYS end
4023 * else:
4024 * ... body ...
4025 * end:
4026 */
4027 static char_u *
4028compile_if(char_u *arg, cctx_T *cctx)
4029{
4030 char_u *p = arg;
4031 garray_T *instr = &cctx->ctx_instr;
4032 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004033 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004035 // compile "expr"; if we know it evaluates to FALSE skip the block
4036 tv.v_type = VAR_UNKNOWN;
4037 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4038 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4039 else
4040 cctx->ctx_skip = MAYBE;
4041 clear_tv(&tv);
4042 if (cctx->ctx_skip == MAYBE)
4043 {
4044 p = arg;
4045 if (compile_expr1(&p, cctx) == FAIL)
4046 return NULL;
4047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
4049 scope = new_scope(cctx, IF_SCOPE);
4050 if (scope == NULL)
4051 return NULL;
4052
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004053 if (cctx->ctx_skip == MAYBE)
4054 {
4055 // "where" is set when ":elseif", "else" or ":endif" is found
4056 scope->se_u.se_if.is_if_label = instr->ga_len;
4057 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4058 }
4059 else
4060 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061
4062 return p;
4063}
4064
4065 static char_u *
4066compile_elseif(char_u *arg, cctx_T *cctx)
4067{
4068 char_u *p = arg;
4069 garray_T *instr = &cctx->ctx_instr;
4070 isn_T *isn;
4071 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004072 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073
4074 if (scope == NULL || scope->se_type != IF_SCOPE)
4075 {
4076 emsg(_(e_elseif_without_if));
4077 return NULL;
4078 }
4079 cctx->ctx_locals.ga_len = scope->se_local_count;
4080
Bram Moolenaar158906c2020-02-06 20:39:45 +01004081 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004082 {
4083 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004085 return NULL;
4086 // previous "if" or "elseif" jumps here
4087 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4088 isn->isn_arg.jump.jump_where = instr->ga_len;
4089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004091 // compile "expr"; if we know it evaluates to FALSE skip the block
4092 tv.v_type = VAR_UNKNOWN;
4093 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4094 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4095 else
4096 cctx->ctx_skip = MAYBE;
4097 clear_tv(&tv);
4098 if (cctx->ctx_skip == MAYBE)
4099 {
4100 p = arg;
4101 if (compile_expr1(&p, cctx) == FAIL)
4102 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004104 // "where" is set when ":elseif", "else" or ":endif" is found
4105 scope->se_u.se_if.is_if_label = instr->ga_len;
4106 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4107 }
4108 else
4109 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110
4111 return p;
4112}
4113
4114 static char_u *
4115compile_else(char_u *arg, cctx_T *cctx)
4116{
4117 char_u *p = arg;
4118 garray_T *instr = &cctx->ctx_instr;
4119 isn_T *isn;
4120 scope_T *scope = cctx->ctx_scope;
4121
4122 if (scope == NULL || scope->se_type != IF_SCOPE)
4123 {
4124 emsg(_(e_else_without_if));
4125 return NULL;
4126 }
4127 cctx->ctx_locals.ga_len = scope->se_local_count;
4128
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004129 // jump from previous block to the end, unless the else block is empty
4130 if (cctx->ctx_skip == MAYBE)
4131 {
4132 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004134 return NULL;
4135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
Bram Moolenaar158906c2020-02-06 20:39:45 +01004137 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004138 {
4139 if (scope->se_u.se_if.is_if_label >= 0)
4140 {
4141 // previous "if" or "elseif" jumps here
4142 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4143 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004144 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004145 }
4146 }
4147
4148 if (cctx->ctx_skip != MAYBE)
4149 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150
4151 return p;
4152}
4153
4154 static char_u *
4155compile_endif(char_u *arg, cctx_T *cctx)
4156{
4157 scope_T *scope = cctx->ctx_scope;
4158 ifscope_T *ifscope;
4159 garray_T *instr = &cctx->ctx_instr;
4160 isn_T *isn;
4161
4162 if (scope == NULL || scope->se_type != IF_SCOPE)
4163 {
4164 emsg(_(e_endif_without_if));
4165 return NULL;
4166 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004167 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 cctx->ctx_scope = scope->se_outer;
4169 cctx->ctx_locals.ga_len = scope->se_local_count;
4170
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004171 if (scope->se_u.se_if.is_if_label >= 0)
4172 {
4173 // previous "if" or "elseif" jumps here
4174 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4175 isn->isn_arg.jump.jump_where = instr->ga_len;
4176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 // Fill in the "end" label in jumps at the end of the blocks.
4178 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004179 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180
4181 vim_free(scope);
4182 return arg;
4183}
4184
4185/*
4186 * compile "for var in expr"
4187 *
4188 * Produces instructions:
4189 * PUSHNR -1
4190 * STORE loop-idx Set index to -1
4191 * EVAL expr Push result of "expr"
4192 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4193 * - if beyond end, jump to "end"
4194 * - otherwise get item from list and push it
4195 * STORE var Store item in "var"
4196 * ... body ...
4197 * JUMP top Jump back to repeat
4198 * end: DROP Drop the result of "expr"
4199 *
4200 */
4201 static char_u *
4202compile_for(char_u *arg, cctx_T *cctx)
4203{
4204 char_u *p;
4205 size_t varlen;
4206 garray_T *instr = &cctx->ctx_instr;
4207 garray_T *stack = &cctx->ctx_type_stack;
4208 scope_T *scope;
4209 int loop_idx; // index of loop iteration variable
4210 int var_idx; // index of "var"
4211 type_T *vartype;
4212
4213 // TODO: list of variables: "for [key, value] in dict"
4214 // parse "var"
4215 for (p = arg; eval_isnamec1(*p); ++p)
4216 ;
4217 varlen = p - arg;
4218 var_idx = lookup_local(arg, varlen, cctx);
4219 if (var_idx >= 0)
4220 {
4221 semsg(_("E1023: variable already defined: %s"), arg);
4222 return NULL;
4223 }
4224
4225 // consume "in"
4226 p = skipwhite(p);
4227 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4228 {
4229 emsg(_(e_missing_in));
4230 return NULL;
4231 }
4232 p = skipwhite(p + 2);
4233
4234
4235 scope = new_scope(cctx, FOR_SCOPE);
4236 if (scope == NULL)
4237 return NULL;
4238
4239 // Reserve a variable to store the loop iteration counter.
4240 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4241 if (loop_idx < 0)
4242 return NULL;
4243
4244 // Reserve a variable to store "var"
4245 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4246 if (var_idx < 0)
4247 return NULL;
4248
4249 generate_STORENR(cctx, loop_idx, -1);
4250
4251 // compile "expr", it remains on the stack until "endfor"
4252 arg = p;
4253 if (compile_expr1(&arg, cctx) == FAIL)
4254 return NULL;
4255
4256 // now we know the type of "var"
4257 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4258 if (vartype->tt_type != VAR_LIST)
4259 {
4260 emsg(_("E1024: need a List to iterate over"));
4261 return NULL;
4262 }
4263 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4264 {
4265 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4266
4267 lvar->lv_type = vartype->tt_member;
4268 }
4269
4270 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004271 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
4273 generate_FOR(cctx, loop_idx);
4274 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4275
4276 return arg;
4277}
4278
4279/*
4280 * compile "endfor"
4281 */
4282 static char_u *
4283compile_endfor(char_u *arg, cctx_T *cctx)
4284{
4285 garray_T *instr = &cctx->ctx_instr;
4286 scope_T *scope = cctx->ctx_scope;
4287 forscope_T *forscope;
4288 isn_T *isn;
4289
4290 if (scope == NULL || scope->se_type != FOR_SCOPE)
4291 {
4292 emsg(_(e_for));
4293 return NULL;
4294 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004295 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 cctx->ctx_scope = scope->se_outer;
4297 cctx->ctx_locals.ga_len = scope->se_local_count;
4298
4299 // At end of ":for" scope jump back to the FOR instruction.
4300 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4301
4302 // Fill in the "end" label in the FOR statement so it can jump here
4303 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4304 isn->isn_arg.forloop.for_end = instr->ga_len;
4305
4306 // Fill in the "end" label any BREAK statements
4307 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4308
4309 // Below the ":for" scope drop the "expr" list from the stack.
4310 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4311 return NULL;
4312
4313 vim_free(scope);
4314
4315 return arg;
4316}
4317
4318/*
4319 * compile "while expr"
4320 *
4321 * Produces instructions:
4322 * top: EVAL expr Push result of "expr"
4323 * JUMP_IF_FALSE end jump if false
4324 * ... body ...
4325 * JUMP top Jump back to repeat
4326 * end:
4327 *
4328 */
4329 static char_u *
4330compile_while(char_u *arg, cctx_T *cctx)
4331{
4332 char_u *p = arg;
4333 garray_T *instr = &cctx->ctx_instr;
4334 scope_T *scope;
4335
4336 scope = new_scope(cctx, WHILE_SCOPE);
4337 if (scope == NULL)
4338 return NULL;
4339
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004340 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
4342 // compile "expr"
4343 if (compile_expr1(&p, cctx) == FAIL)
4344 return NULL;
4345
4346 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004347 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 JUMP_IF_FALSE, cctx) == FAIL)
4349 return FAIL;
4350
4351 return p;
4352}
4353
4354/*
4355 * compile "endwhile"
4356 */
4357 static char_u *
4358compile_endwhile(char_u *arg, cctx_T *cctx)
4359{
4360 scope_T *scope = cctx->ctx_scope;
4361
4362 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4363 {
4364 emsg(_(e_while));
4365 return NULL;
4366 }
4367 cctx->ctx_scope = scope->se_outer;
4368 cctx->ctx_locals.ga_len = scope->se_local_count;
4369
4370 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004371 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
4373 // Fill in the "end" label in the WHILE statement so it can jump here.
4374 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004375 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 vim_free(scope);
4378
4379 return arg;
4380}
4381
4382/*
4383 * compile "continue"
4384 */
4385 static char_u *
4386compile_continue(char_u *arg, cctx_T *cctx)
4387{
4388 scope_T *scope = cctx->ctx_scope;
4389
4390 for (;;)
4391 {
4392 if (scope == NULL)
4393 {
4394 emsg(_(e_continue));
4395 return NULL;
4396 }
4397 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4398 break;
4399 scope = scope->se_outer;
4400 }
4401
4402 // Jump back to the FOR or WHILE instruction.
4403 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004404 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4405 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return arg;
4407}
4408
4409/*
4410 * compile "break"
4411 */
4412 static char_u *
4413compile_break(char_u *arg, cctx_T *cctx)
4414{
4415 scope_T *scope = cctx->ctx_scope;
4416 endlabel_T **el;
4417
4418 for (;;)
4419 {
4420 if (scope == NULL)
4421 {
4422 emsg(_(e_break));
4423 return NULL;
4424 }
4425 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4426 break;
4427 scope = scope->se_outer;
4428 }
4429
4430 // Jump to the end of the FOR or WHILE loop.
4431 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004432 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004434 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4436 return FAIL;
4437
4438 return arg;
4439}
4440
4441/*
4442 * compile "{" start of block
4443 */
4444 static char_u *
4445compile_block(char_u *arg, cctx_T *cctx)
4446{
4447 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4448 return NULL;
4449 return skipwhite(arg + 1);
4450}
4451
4452/*
4453 * compile end of block: drop one scope
4454 */
4455 static void
4456compile_endblock(cctx_T *cctx)
4457{
4458 scope_T *scope = cctx->ctx_scope;
4459
4460 cctx->ctx_scope = scope->se_outer;
4461 cctx->ctx_locals.ga_len = scope->se_local_count;
4462 vim_free(scope);
4463}
4464
4465/*
4466 * compile "try"
4467 * Creates a new scope for the try-endtry, pointing to the first catch and
4468 * finally.
4469 * Creates another scope for the "try" block itself.
4470 * TRY instruction sets up exception handling at runtime.
4471 *
4472 * "try"
4473 * TRY -> catch1, -> finally push trystack entry
4474 * ... try block
4475 * "throw {exception}"
4476 * EVAL {exception}
4477 * THROW create exception
4478 * ... try block
4479 * " catch {expr}"
4480 * JUMP -> finally
4481 * catch1: PUSH exeception
4482 * EVAL {expr}
4483 * MATCH
4484 * JUMP nomatch -> catch2
4485 * CATCH remove exception
4486 * ... catch block
4487 * " catch"
4488 * JUMP -> finally
4489 * catch2: CATCH remove exception
4490 * ... catch block
4491 * " finally"
4492 * finally:
4493 * ... finally block
4494 * " endtry"
4495 * ENDTRY pop trystack entry, may rethrow
4496 */
4497 static char_u *
4498compile_try(char_u *arg, cctx_T *cctx)
4499{
4500 garray_T *instr = &cctx->ctx_instr;
4501 scope_T *try_scope;
4502 scope_T *scope;
4503
4504 // scope that holds the jumps that go to catch/finally/endtry
4505 try_scope = new_scope(cctx, TRY_SCOPE);
4506 if (try_scope == NULL)
4507 return NULL;
4508
4509 // "catch" is set when the first ":catch" is found.
4510 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004511 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 if (generate_instr(cctx, ISN_TRY) == NULL)
4513 return NULL;
4514
4515 // scope for the try block itself
4516 scope = new_scope(cctx, BLOCK_SCOPE);
4517 if (scope == NULL)
4518 return NULL;
4519
4520 return arg;
4521}
4522
4523/*
4524 * compile "catch {expr}"
4525 */
4526 static char_u *
4527compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4528{
4529 scope_T *scope = cctx->ctx_scope;
4530 garray_T *instr = &cctx->ctx_instr;
4531 char_u *p;
4532 isn_T *isn;
4533
4534 // end block scope from :try or :catch
4535 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4536 compile_endblock(cctx);
4537 scope = cctx->ctx_scope;
4538
4539 // Error if not in a :try scope
4540 if (scope == NULL || scope->se_type != TRY_SCOPE)
4541 {
4542 emsg(_(e_catch));
4543 return NULL;
4544 }
4545
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004546 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 {
4548 emsg(_("E1033: catch unreachable after catch-all"));
4549 return NULL;
4550 }
4551
4552 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004553 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 JUMP_ALWAYS, cctx) == FAIL)
4555 return NULL;
4556
4557 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004558 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 if (isn->isn_arg.try.try_catch == 0)
4560 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004561 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 {
4563 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004564 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 isn->isn_arg.jump.jump_where = instr->ga_len;
4566 }
4567
4568 p = skipwhite(arg);
4569 if (ends_excmd(*p))
4570 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004571 scope->se_u.se_try.ts_caught_all = TRUE;
4572 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 }
4574 else
4575 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004576 char_u *end;
4577 char_u *pat;
4578 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004579 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 // Push v:exception, push {expr} and MATCH
4582 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4583
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004584 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4585 if (*end != *p)
4586 {
4587 semsg(_("E1067: Separator mismatch: %s"), p);
4588 vim_free(tofree);
4589 return FAIL;
4590 }
4591 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004592 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004593 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004594 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004595 pat = vim_strnsave(p + 1, len);
4596 vim_free(tofree);
4597 p += len + 2;
4598 if (pat == NULL)
4599 return FAIL;
4600 if (generate_PUSHS(cctx, pat) == FAIL)
4601 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4604 return NULL;
4605
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004606 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4608 return NULL;
4609 }
4610
4611 if (generate_instr(cctx, ISN_CATCH) == NULL)
4612 return NULL;
4613
4614 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4615 return NULL;
4616 return p;
4617}
4618
4619 static char_u *
4620compile_finally(char_u *arg, cctx_T *cctx)
4621{
4622 scope_T *scope = cctx->ctx_scope;
4623 garray_T *instr = &cctx->ctx_instr;
4624 isn_T *isn;
4625
4626 // end block scope from :try or :catch
4627 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4628 compile_endblock(cctx);
4629 scope = cctx->ctx_scope;
4630
4631 // Error if not in a :try scope
4632 if (scope == NULL || scope->se_type != TRY_SCOPE)
4633 {
4634 emsg(_(e_finally));
4635 return NULL;
4636 }
4637
4638 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004639 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 if (isn->isn_arg.try.try_finally != 0)
4641 {
4642 emsg(_(e_finally_dup));
4643 return NULL;
4644 }
4645
4646 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004647 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004649 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 {
4651 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004652 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 isn->isn_arg.jump.jump_where = instr->ga_len;
4654 }
4655
4656 isn->isn_arg.try.try_finally = instr->ga_len;
4657 // TODO: set index in ts_finally_label jumps
4658
4659 return arg;
4660}
4661
4662 static char_u *
4663compile_endtry(char_u *arg, cctx_T *cctx)
4664{
4665 scope_T *scope = cctx->ctx_scope;
4666 garray_T *instr = &cctx->ctx_instr;
4667 isn_T *isn;
4668
4669 // end block scope from :catch or :finally
4670 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4671 compile_endblock(cctx);
4672 scope = cctx->ctx_scope;
4673
4674 // Error if not in a :try scope
4675 if (scope == NULL || scope->se_type != TRY_SCOPE)
4676 {
4677 if (scope == NULL)
4678 emsg(_(e_no_endtry));
4679 else if (scope->se_type == WHILE_SCOPE)
4680 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004681 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 emsg(_(e_endfor));
4683 else
4684 emsg(_(e_endif));
4685 return NULL;
4686 }
4687
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004688 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4690 {
4691 emsg(_("E1032: missing :catch or :finally"));
4692 return NULL;
4693 }
4694
4695 // Fill in the "end" label in jumps at the end of the blocks, if not done
4696 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004697 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
4699 // End :catch or :finally scope: set value in ISN_TRY instruction
4700 if (isn->isn_arg.try.try_finally == 0)
4701 isn->isn_arg.try.try_finally = instr->ga_len;
4702 compile_endblock(cctx);
4703
4704 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4705 return NULL;
4706 return arg;
4707}
4708
4709/*
4710 * compile "throw {expr}"
4711 */
4712 static char_u *
4713compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4714{
4715 char_u *p = skipwhite(arg);
4716
4717 if (ends_excmd(*p))
4718 {
4719 emsg(_(e_argreq));
4720 return NULL;
4721 }
4722 if (compile_expr1(&p, cctx) == FAIL)
4723 return NULL;
4724 if (may_generate_2STRING(-1, cctx) == FAIL)
4725 return NULL;
4726 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4727 return NULL;
4728
4729 return p;
4730}
4731
4732/*
4733 * compile "echo expr"
4734 */
4735 static char_u *
4736compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4737{
4738 char_u *p = arg;
4739 int count = 0;
4740
Bram Moolenaarad39c092020-02-26 18:23:43 +01004741 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 {
4743 if (compile_expr1(&p, cctx) == FAIL)
4744 return NULL;
4745 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004746 p = skipwhite(p);
4747 if (ends_excmd(*p))
4748 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 }
4750
4751 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004752 return p;
4753}
4754
4755/*
4756 * compile "execute expr"
4757 */
4758 static char_u *
4759compile_execute(char_u *arg, cctx_T *cctx)
4760{
4761 char_u *p = arg;
4762 int count = 0;
4763
4764 for (;;)
4765 {
4766 if (compile_expr1(&p, cctx) == FAIL)
4767 return NULL;
4768 ++count;
4769 p = skipwhite(p);
4770 if (ends_excmd(*p))
4771 break;
4772 }
4773
4774 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775
4776 return p;
4777}
4778
4779/*
4780 * After ex_function() has collected all the function lines: parse and compile
4781 * the lines into instructions.
4782 * Adds the function to "def_functions".
4783 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4784 * return statement (used for lambda).
4785 */
4786 void
4787compile_def_function(ufunc_T *ufunc, int set_return_type)
4788{
4789 dfunc_T *dfunc;
4790 char_u *line = NULL;
4791 char_u *p;
4792 exarg_T ea;
4793 char *errormsg = NULL; // error message
4794 int had_return = FALSE;
4795 cctx_T cctx;
4796 garray_T *instr;
4797 int called_emsg_before = called_emsg;
4798 int ret = FAIL;
4799 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004800 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801
4802 if (ufunc->uf_dfunc_idx >= 0)
4803 {
4804 // redefining a function that was compiled before
4805 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4806 dfunc->df_deleted = FALSE;
4807 }
4808 else
4809 {
4810 // Add the function to "def_functions".
4811 if (ga_grow(&def_functions, 1) == FAIL)
4812 return;
4813 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4814 vim_memset(dfunc, 0, sizeof(dfunc_T));
4815 dfunc->df_idx = def_functions.ga_len;
4816 ufunc->uf_dfunc_idx = dfunc->df_idx;
4817 dfunc->df_ufunc = ufunc;
4818 ++def_functions.ga_len;
4819 }
4820
4821 vim_memset(&cctx, 0, sizeof(cctx));
4822 cctx.ctx_ufunc = ufunc;
4823 cctx.ctx_lnum = -1;
4824 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4825 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4826 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4827 cctx.ctx_type_list = &ufunc->uf_type_list;
4828 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4829 instr = &cctx.ctx_instr;
4830
4831 // Most modern script version.
4832 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4833
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004834 if (ufunc->uf_def_args.ga_len > 0)
4835 {
4836 int count = ufunc->uf_def_args.ga_len;
4837 int i;
4838 char_u *arg;
4839 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4840
4841 // Produce instructions for the default values of optional arguments.
4842 // Store the instruction index in uf_def_arg_idx[] so that we know
4843 // where to start when the function is called, depending on the number
4844 // of arguments.
4845 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4846 if (ufunc->uf_def_arg_idx == NULL)
4847 goto erret;
4848 for (i = 0; i < count; ++i)
4849 {
4850 ufunc->uf_def_arg_idx[i] = instr->ga_len;
4851 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4852 if (compile_expr1(&arg, &cctx) == FAIL
4853 || generate_STORE(&cctx, ISN_STORE,
4854 i - count - off, NULL) == FAIL)
4855 goto erret;
4856 }
4857
4858 // If a varargs is following, push an empty list.
4859 if (ufunc->uf_va_name != NULL)
4860 {
4861 if (generate_NEWLIST(&cctx, 0) == FAIL
4862 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4863 goto erret;
4864 }
4865
4866 ufunc->uf_def_arg_idx[count] = instr->ga_len;
4867 }
4868
4869 /*
4870 * Loop over all the lines of the function and generate instructions.
4871 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 for (;;)
4873 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004874 int is_ex_command;
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 if (line != NULL && *line == '|')
4877 // the line continues after a '|'
4878 ++line;
4879 else if (line != NULL && *line != NUL)
4880 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004881 if (emsg_before == called_emsg)
4882 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 goto erret;
4884 }
4885 else
4886 {
4887 do
4888 {
4889 ++cctx.ctx_lnum;
4890 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4891 break;
4892 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4893 } while (line == NULL);
4894 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4895 break;
4896 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4897 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004898 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899
4900 had_return = FALSE;
4901 vim_memset(&ea, 0, sizeof(ea));
4902 ea.cmdlinep = &line;
4903 ea.cmd = skipwhite(line);
4904
4905 // "}" ends a block scope
4906 if (*ea.cmd == '}')
4907 {
4908 scopetype_T stype = cctx.ctx_scope == NULL
4909 ? NO_SCOPE : cctx.ctx_scope->se_type;
4910
4911 if (stype == BLOCK_SCOPE)
4912 {
4913 compile_endblock(&cctx);
4914 line = ea.cmd;
4915 }
4916 else
4917 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004918 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 goto erret;
4920 }
4921 if (line != NULL)
4922 line = skipwhite(ea.cmd + 1);
4923 continue;
4924 }
4925
4926 // "{" starts a block scope
4927 if (*ea.cmd == '{')
4928 {
4929 line = compile_block(ea.cmd, &cctx);
4930 continue;
4931 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004932 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
4934 /*
4935 * COMMAND MODIFIERS
4936 */
4937 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4938 {
4939 if (errormsg != NULL)
4940 goto erret;
4941 // empty line or comment
4942 line = (char_u *)"";
4943 continue;
4944 }
4945
4946 // Skip ":call" to get to the function name.
4947 if (checkforcmd(&ea.cmd, "call", 3))
4948 ea.cmd = skipwhite(ea.cmd);
4949
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004950 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004952 // Assuming the command starts with a variable or function name,
4953 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
4954 // val".
4955 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
4956 ? ea.cmd + 1 : ea.cmd;
4957 p = to_name_end(p);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01004958 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004960 int oplen;
4961 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004963 oplen = assignment_len(skipwhite(p), &heredoc);
4964 if (oplen > 0)
4965 {
4966 // Recognize an assignment if we recognize the variable
4967 // name:
4968 // "g:var = expr"
4969 // "var = expr" where "var" is a local var name.
4970 // "&opt = expr"
4971 // "$ENV = expr"
4972 // "@r = expr"
4973 if (*ea.cmd == '&'
4974 || *ea.cmd == '$'
4975 || *ea.cmd == '@'
4976 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4977 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4978 || lookup_script(ea.cmd, p - ea.cmd) == OK
4979 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
4980 {
4981 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4982 if (line == NULL)
4983 goto erret;
4984 continue;
4985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 }
4987 }
4988 }
4989
4990 /*
4991 * COMMAND after range
4992 */
4993 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004994 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
4995 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996
4997 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4998 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004999 if (cctx.ctx_skip == TRUE)
5000 {
5001 line += STRLEN(line);
5002 continue;
5003 }
5004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 // Expression or function call.
5006 if (ea.cmdidx == CMD_eval)
5007 {
5008 p = ea.cmd;
5009 if (compile_expr1(&p, &cctx) == FAIL)
5010 goto erret;
5011
5012 // drop the return value
5013 generate_instr_drop(&cctx, ISN_DROP, 1);
5014 line = p;
5015 continue;
5016 }
5017 if (ea.cmdidx == CMD_let)
5018 {
5019 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5020 if (line == NULL)
5021 goto erret;
5022 continue;
5023 }
5024 iemsg("Command from find_ex_command() not handled");
5025 goto erret;
5026 }
5027
5028 p = skipwhite(p);
5029
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005030 if (cctx.ctx_skip == TRUE
5031 && ea.cmdidx != CMD_elseif
5032 && ea.cmdidx != CMD_else
5033 && ea.cmdidx != CMD_endif)
5034 {
5035 line += STRLEN(line);
5036 continue;
5037 }
5038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 switch (ea.cmdidx)
5040 {
5041 case CMD_def:
5042 case CMD_function:
5043 // TODO: Nested function
5044 emsg("Nested function not implemented yet");
5045 goto erret;
5046
5047 case CMD_return:
5048 line = compile_return(p, set_return_type, &cctx);
5049 had_return = TRUE;
5050 break;
5051
5052 case CMD_let:
5053 case CMD_const:
5054 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5055 break;
5056
5057 case CMD_import:
5058 line = compile_import(p, &cctx);
5059 break;
5060
5061 case CMD_if:
5062 line = compile_if(p, &cctx);
5063 break;
5064 case CMD_elseif:
5065 line = compile_elseif(p, &cctx);
5066 break;
5067 case CMD_else:
5068 line = compile_else(p, &cctx);
5069 break;
5070 case CMD_endif:
5071 line = compile_endif(p, &cctx);
5072 break;
5073
5074 case CMD_while:
5075 line = compile_while(p, &cctx);
5076 break;
5077 case CMD_endwhile:
5078 line = compile_endwhile(p, &cctx);
5079 break;
5080
5081 case CMD_for:
5082 line = compile_for(p, &cctx);
5083 break;
5084 case CMD_endfor:
5085 line = compile_endfor(p, &cctx);
5086 break;
5087 case CMD_continue:
5088 line = compile_continue(p, &cctx);
5089 break;
5090 case CMD_break:
5091 line = compile_break(p, &cctx);
5092 break;
5093
5094 case CMD_try:
5095 line = compile_try(p, &cctx);
5096 break;
5097 case CMD_catch:
5098 line = compile_catch(p, &cctx);
5099 break;
5100 case CMD_finally:
5101 line = compile_finally(p, &cctx);
5102 break;
5103 case CMD_endtry:
5104 line = compile_endtry(p, &cctx);
5105 break;
5106 case CMD_throw:
5107 line = compile_throw(p, &cctx);
5108 break;
5109
5110 case CMD_echo:
5111 line = compile_echo(p, TRUE, &cctx);
5112 break;
5113 case CMD_echon:
5114 line = compile_echo(p, FALSE, &cctx);
5115 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005116 case CMD_execute:
5117 line = compile_execute(p, &cctx);
5118 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119
5120 default:
5121 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005122 // TODO:
5123 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005124 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 generate_EXEC(&cctx, line);
5126 line = (char_u *)"";
5127 break;
5128 }
5129 if (line == NULL)
5130 goto erret;
5131
5132 if (cctx.ctx_type_stack.ga_len < 0)
5133 {
5134 iemsg("Type stack underflow");
5135 goto erret;
5136 }
5137 }
5138
5139 if (cctx.ctx_scope != NULL)
5140 {
5141 if (cctx.ctx_scope->se_type == IF_SCOPE)
5142 emsg(_(e_endif));
5143 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5144 emsg(_(e_endwhile));
5145 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5146 emsg(_(e_endfor));
5147 else
5148 emsg(_("E1026: Missing }"));
5149 goto erret;
5150 }
5151
5152 if (!had_return)
5153 {
5154 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5155 {
5156 emsg(_("E1027: Missing return statement"));
5157 goto erret;
5158 }
5159
5160 // Return zero if there is no return at the end.
5161 generate_PUSHNR(&cctx, 0);
5162 generate_instr(&cctx, ISN_RETURN);
5163 }
5164
5165 dfunc->df_instr = instr->ga_data;
5166 dfunc->df_instr_count = instr->ga_len;
5167 dfunc->df_varcount = cctx.ctx_max_local;
5168
5169 ret = OK;
5170
5171erret:
5172 if (ret == FAIL)
5173 {
5174 ga_clear(instr);
5175 ufunc->uf_dfunc_idx = -1;
5176 --def_functions.ga_len;
5177 if (errormsg != NULL)
5178 emsg(errormsg);
5179 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005180 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
5182 // don't execute this function body
5183 ufunc->uf_lines.ga_len = 0;
5184 }
5185
5186 current_sctx = save_current_sctx;
5187 ga_clear(&cctx.ctx_type_stack);
5188 ga_clear(&cctx.ctx_locals);
5189}
5190
5191/*
5192 * Delete an instruction, free what it contains.
5193 */
5194 static void
5195delete_instr(isn_T *isn)
5196{
5197 switch (isn->isn_type)
5198 {
5199 case ISN_EXEC:
5200 case ISN_LOADENV:
5201 case ISN_LOADG:
5202 case ISN_LOADOPT:
5203 case ISN_MEMBER:
5204 case ISN_PUSHEXC:
5205 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005206 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005208 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 vim_free(isn->isn_arg.string);
5210 break;
5211
5212 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005213 case ISN_STORES:
5214 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 break;
5216
5217 case ISN_STOREOPT:
5218 vim_free(isn->isn_arg.storeopt.so_name);
5219 break;
5220
5221 case ISN_PUSHBLOB: // push blob isn_arg.blob
5222 blob_unref(isn->isn_arg.blob);
5223 break;
5224
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005225 case ISN_PUSHPARTIAL:
5226 // TODO
5227 break;
5228
5229 case ISN_PUSHJOB:
5230 job_unref(isn->isn_arg.job);
5231 break;
5232
5233 case ISN_PUSHCHANNEL:
5234 channel_unref(isn->isn_arg.channel);
5235 break;
5236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 case ISN_UCALL:
5238 vim_free(isn->isn_arg.ufunc.cuf_name);
5239 break;
5240
5241 case ISN_2BOOL:
5242 case ISN_2STRING:
5243 case ISN_ADDBLOB:
5244 case ISN_ADDLIST:
5245 case ISN_BCALL:
5246 case ISN_CATCH:
5247 case ISN_CHECKNR:
5248 case ISN_CHECKTYPE:
5249 case ISN_COMPAREANY:
5250 case ISN_COMPAREBLOB:
5251 case ISN_COMPAREBOOL:
5252 case ISN_COMPAREDICT:
5253 case ISN_COMPAREFLOAT:
5254 case ISN_COMPAREFUNC:
5255 case ISN_COMPARELIST:
5256 case ISN_COMPARENR:
5257 case ISN_COMPAREPARTIAL:
5258 case ISN_COMPARESPECIAL:
5259 case ISN_COMPARESTRING:
5260 case ISN_CONCAT:
5261 case ISN_DCALL:
5262 case ISN_DROP:
5263 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005264 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 case ISN_ENDTRY:
5266 case ISN_FOR:
5267 case ISN_FUNCREF:
5268 case ISN_INDEX:
5269 case ISN_JUMP:
5270 case ISN_LOAD:
5271 case ISN_LOADSCRIPT:
5272 case ISN_LOADREG:
5273 case ISN_LOADV:
5274 case ISN_NEGATENR:
5275 case ISN_NEWDICT:
5276 case ISN_NEWLIST:
5277 case ISN_OPNR:
5278 case ISN_OPFLOAT:
5279 case ISN_OPANY:
5280 case ISN_PCALL:
5281 case ISN_PUSHF:
5282 case ISN_PUSHNR:
5283 case ISN_PUSHBOOL:
5284 case ISN_PUSHSPEC:
5285 case ISN_RETURN:
5286 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005287 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005289 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 case ISN_STORESCRIPT:
5291 case ISN_THROW:
5292 case ISN_TRY:
5293 // nothing allocated
5294 break;
5295 }
5296}
5297
5298/*
5299 * When a user function is deleted, delete any associated def function.
5300 */
5301 void
5302delete_def_function(ufunc_T *ufunc)
5303{
5304 int idx;
5305
5306 if (ufunc->uf_dfunc_idx >= 0)
5307 {
5308 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5309 + ufunc->uf_dfunc_idx;
5310 ga_clear(&dfunc->df_def_args_isn);
5311
5312 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5313 delete_instr(dfunc->df_instr + idx);
5314 VIM_CLEAR(dfunc->df_instr);
5315
5316 dfunc->df_deleted = TRUE;
5317 }
5318}
5319
5320#if defined(EXITFREE) || defined(PROTO)
5321 void
5322free_def_functions(void)
5323{
5324 vim_free(def_functions.ga_data);
5325}
5326#endif
5327
5328
5329#endif // FEAT_EVAL