blob: 35f20c4c76295056d7bbd68f69b31230dd9156b5 [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/*
645 * Generate an ISN_PUSHBLOB instruction.
646 * Consumes "blob".
647 */
648 static int
649generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
650{
651 isn_T *isn;
652
653 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
654 return FAIL;
655 isn->isn_arg.blob = blob;
656
657 return OK;
658}
659
660/*
661 * Generate an ISN_STORE instruction.
662 */
663 static int
664generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
665{
666 isn_T *isn;
667
668 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
669 return FAIL;
670 if (name != NULL)
671 isn->isn_arg.string = vim_strsave(name);
672 else
673 isn->isn_arg.number = idx;
674
675 return OK;
676}
677
678/*
679 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
680 */
681 static int
682generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
683{
684 isn_T *isn;
685
686 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
687 return FAIL;
688 isn->isn_arg.storenr.str_idx = idx;
689 isn->isn_arg.storenr.str_val = value;
690
691 return OK;
692}
693
694/*
695 * Generate an ISN_STOREOPT instruction
696 */
697 static int
698generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
699{
700 isn_T *isn;
701
702 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
703 return FAIL;
704 isn->isn_arg.storeopt.so_name = vim_strsave(name);
705 isn->isn_arg.storeopt.so_flags = opt_flags;
706
707 return OK;
708}
709
710/*
711 * Generate an ISN_LOAD or similar instruction.
712 */
713 static int
714generate_LOAD(
715 cctx_T *cctx,
716 isntype_T isn_type,
717 int idx,
718 char_u *name,
719 type_T *type)
720{
721 isn_T *isn;
722
723 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
724 return FAIL;
725 if (name != NULL)
726 isn->isn_arg.string = vim_strsave(name);
727 else
728 isn->isn_arg.number = idx;
729
730 return OK;
731}
732
733/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100734 * Generate an ISN_LOADV instruction.
735 */
736 static int
737generate_LOADV(
738 cctx_T *cctx,
739 char_u *name,
740 int error)
741{
742 // load v:var
743 int vidx = find_vim_var(name);
744
745 if (vidx < 0)
746 {
747 if (error)
748 semsg(_(e_var_notfound), name);
749 return FAIL;
750 }
751
752 // TODO: get actual type
753 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
754}
755
756/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 * Generate an ISN_LOADS instruction.
758 */
759 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100760generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100762 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100764 int sid,
765 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766{
767 isn_T *isn;
768
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100769 if (isn_type == ISN_LOADS)
770 isn = generate_instr_type(cctx, isn_type, type);
771 else
772 isn = generate_instr_drop(cctx, isn_type, 1);
773 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100775 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
776 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
778 return OK;
779}
780
781/*
782 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
783 */
784 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100785generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 cctx_T *cctx,
787 isntype_T isn_type,
788 int sid,
789 int idx,
790 type_T *type)
791{
792 isn_T *isn;
793
794 if (isn_type == ISN_LOADSCRIPT)
795 isn = generate_instr_type(cctx, isn_type, type);
796 else
797 isn = generate_instr_drop(cctx, isn_type, 1);
798 if (isn == NULL)
799 return FAIL;
800 isn->isn_arg.script.script_sid = sid;
801 isn->isn_arg.script.script_idx = idx;
802 return OK;
803}
804
805/*
806 * Generate an ISN_NEWLIST instruction.
807 */
808 static int
809generate_NEWLIST(cctx_T *cctx, int count)
810{
811 isn_T *isn;
812 garray_T *stack = &cctx->ctx_type_stack;
813 garray_T *type_list = cctx->ctx_type_list;
814 type_T *type;
815 type_T *member;
816
817 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
818 return FAIL;
819 isn->isn_arg.number = count;
820
821 // drop the value types
822 stack->ga_len -= count;
823
Bram Moolenaar436472f2020-02-20 22:54:43 +0100824 // Use the first value type for the list member type. Use "void" for an
825 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 if (count > 0)
827 member = ((type_T **)stack->ga_data)[stack->ga_len];
828 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100829 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 type = get_list_type(member, type_list);
831
832 // add the list type to the type stack
833 if (ga_grow(stack, 1) == FAIL)
834 return FAIL;
835 ((type_T **)stack->ga_data)[stack->ga_len] = type;
836 ++stack->ga_len;
837
838 return OK;
839}
840
841/*
842 * Generate an ISN_NEWDICT instruction.
843 */
844 static int
845generate_NEWDICT(cctx_T *cctx, int count)
846{
847 isn_T *isn;
848 garray_T *stack = &cctx->ctx_type_stack;
849 garray_T *type_list = cctx->ctx_type_list;
850 type_T *type;
851 type_T *member;
852
853 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = count;
856
857 // drop the key and value types
858 stack->ga_len -= 2 * count;
859
Bram Moolenaar436472f2020-02-20 22:54:43 +0100860 // Use the first value type for the list member type. Use "void" for an
861 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if (count > 0)
863 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
864 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100865 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 type = get_dict_type(member, type_list);
867
868 // add the dict type to the type stack
869 if (ga_grow(stack, 1) == FAIL)
870 return FAIL;
871 ((type_T **)stack->ga_data)[stack->ga_len] = type;
872 ++stack->ga_len;
873
874 return OK;
875}
876
877/*
878 * Generate an ISN_FUNCREF instruction.
879 */
880 static int
881generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
882{
883 isn_T *isn;
884 garray_T *stack = &cctx->ctx_type_stack;
885
886 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
887 return FAIL;
888 isn->isn_arg.number = dfunc_idx;
889
890 if (ga_grow(stack, 1) == FAIL)
891 return FAIL;
892 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
893 // TODO: argument and return types
894 ++stack->ga_len;
895
896 return OK;
897}
898
899/*
900 * Generate an ISN_JUMP instruction.
901 */
902 static int
903generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
904{
905 isn_T *isn;
906 garray_T *stack = &cctx->ctx_type_stack;
907
908 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
909 return FAIL;
910 isn->isn_arg.jump.jump_when = when;
911 isn->isn_arg.jump.jump_where = where;
912
913 if (when != JUMP_ALWAYS && stack->ga_len > 0)
914 --stack->ga_len;
915
916 return OK;
917}
918
919 static int
920generate_FOR(cctx_T *cctx, int loop_idx)
921{
922 isn_T *isn;
923 garray_T *stack = &cctx->ctx_type_stack;
924
925 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
926 return FAIL;
927 isn->isn_arg.forloop.for_idx = loop_idx;
928
929 if (ga_grow(stack, 1) == FAIL)
930 return FAIL;
931 // type doesn't matter, will be stored next
932 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
933 ++stack->ga_len;
934
935 return OK;
936}
937
938/*
939 * Generate an ISN_BCALL instruction.
940 * Return FAIL if the number of arguments is wrong.
941 */
942 static int
943generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
948 if (check_internal_func(func_idx, argcount) == FAIL)
949 return FAIL;
950
951 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
952 return FAIL;
953 isn->isn_arg.bfunc.cbf_idx = func_idx;
954 isn->isn_arg.bfunc.cbf_argcount = argcount;
955
956 stack->ga_len -= argcount; // drop the arguments
957 if (ga_grow(stack, 1) == FAIL)
958 return FAIL;
959 ((type_T **)stack->ga_data)[stack->ga_len] =
960 internal_func_ret_type(func_idx, argcount);
961 ++stack->ga_len; // add return value
962
963 return OK;
964}
965
966/*
967 * Generate an ISN_DCALL or ISN_UCALL instruction.
968 * Return FAIL if the number of arguments is wrong.
969 */
970 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100971generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972{
973 isn_T *isn;
974 garray_T *stack = &cctx->ctx_type_stack;
975 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100976 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977
978 if (argcount > regular_args && !has_varargs(ufunc))
979 {
980 semsg(_(e_toomanyarg), ufunc->uf_name);
981 return FAIL;
982 }
983 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
984 {
985 semsg(_(e_toofewarg), ufunc->uf_name);
986 return FAIL;
987 }
988
989 // Turn varargs into a list.
990 if (ufunc->uf_va_name != NULL)
991 {
992 int count = argcount - regular_args;
993
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100994 // If count is negative an empty list will be added after evaluating
995 // default values for missing optional arguments.
996 if (count >= 0)
997 {
998 generate_NEWLIST(cctx, count);
999 argcount = regular_args + 1;
1000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 }
1002
1003 if ((isn = generate_instr(cctx,
1004 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1005 return FAIL;
1006 if (ufunc->uf_dfunc_idx >= 0)
1007 {
1008 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1009 isn->isn_arg.dfunc.cdf_argcount = argcount;
1010 }
1011 else
1012 {
1013 // A user function may be deleted and redefined later, can't use the
1014 // ufunc pointer, need to look it up again at runtime.
1015 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1016 isn->isn_arg.ufunc.cuf_argcount = argcount;
1017 }
1018
1019 stack->ga_len -= argcount; // drop the arguments
1020 if (ga_grow(stack, 1) == FAIL)
1021 return FAIL;
1022 // add return value
1023 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1024 ++stack->ga_len;
1025
1026 return OK;
1027}
1028
1029/*
1030 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1031 */
1032 static int
1033generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1034{
1035 isn_T *isn;
1036 garray_T *stack = &cctx->ctx_type_stack;
1037
1038 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1039 return FAIL;
1040 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1041 isn->isn_arg.ufunc.cuf_argcount = argcount;
1042
1043 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001044 if (ga_grow(stack, 1) == FAIL)
1045 return FAIL;
1046 // add return value
1047 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1048 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049
1050 return OK;
1051}
1052
1053/*
1054 * Generate an ISN_PCALL instruction.
1055 */
1056 static int
1057generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1058{
1059 isn_T *isn;
1060 garray_T *stack = &cctx->ctx_type_stack;
1061
1062 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1063 return FAIL;
1064 isn->isn_arg.pfunc.cpf_top = at_top;
1065 isn->isn_arg.pfunc.cpf_argcount = argcount;
1066
1067 stack->ga_len -= argcount; // drop the arguments
1068
1069 // drop the funcref/partial, get back the return value
1070 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_MEMBER instruction.
1077 */
1078 static int
1079generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1080{
1081 isn_T *isn;
1082 garray_T *stack = &cctx->ctx_type_stack;
1083 type_T *type;
1084
1085 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.string = vim_strnsave(name, (int)len);
1088
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001089 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001091 if (type->tt_type != VAR_DICT && type != &t_any)
1092 {
1093 emsg(_(e_dictreq));
1094 return FAIL;
1095 }
1096 // change dict type to dict member type
1097 if (type->tt_type == VAR_DICT)
1098 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099
1100 return OK;
1101}
1102
1103/*
1104 * Generate an ISN_ECHO instruction.
1105 */
1106 static int
1107generate_ECHO(cctx_T *cctx, int with_white, int count)
1108{
1109 isn_T *isn;
1110
1111 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1112 return FAIL;
1113 isn->isn_arg.echo.echo_with_white = with_white;
1114 isn->isn_arg.echo.echo_count = count;
1115
1116 return OK;
1117}
1118
Bram Moolenaarad39c092020-02-26 18:23:43 +01001119/*
1120 * Generate an ISN_EXECUTE instruction.
1121 */
1122 static int
1123generate_EXECUTE(cctx_T *cctx, int count)
1124{
1125 isn_T *isn;
1126
1127 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1128 return FAIL;
1129 isn->isn_arg.number = count;
1130
1131 return OK;
1132}
1133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 static int
1135generate_EXEC(cctx_T *cctx, char_u *line)
1136{
1137 isn_T *isn;
1138
1139 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.string = vim_strsave(line);
1142 return OK;
1143}
1144
1145static char e_white_both[] =
1146 N_("E1004: white space required before and after '%s'");
1147
1148/*
1149 * Reserve space for a local variable.
1150 * Return the index or -1 if it failed.
1151 */
1152 static int
1153reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1154{
1155 int idx;
1156 lvar_T *lvar;
1157
1158 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1159 {
1160 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1161 return -1;
1162 }
1163
1164 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1165 return -1;
1166 idx = cctx->ctx_locals.ga_len;
1167 if (cctx->ctx_max_local < idx + 1)
1168 cctx->ctx_max_local = idx + 1;
1169 ++cctx->ctx_locals.ga_len;
1170
1171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1172 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1173 lvar->lv_const = isConst;
1174 lvar->lv_type = type;
1175
1176 return idx;
1177}
1178
1179/*
1180 * Skip over a type definition and return a pointer to just after it.
1181 */
1182 char_u *
1183skip_type(char_u *start)
1184{
1185 char_u *p = start;
1186
1187 while (ASCII_ISALNUM(*p) || *p == '_')
1188 ++p;
1189
1190 // Skip over "<type>"; this is permissive about white space.
1191 if (*skipwhite(p) == '<')
1192 {
1193 p = skipwhite(p);
1194 p = skip_type(skipwhite(p + 1));
1195 p = skipwhite(p);
1196 if (*p == '>')
1197 ++p;
1198 }
1199 return p;
1200}
1201
1202/*
1203 * Parse the member type: "<type>" and return "type" with the member set.
1204 * Use "type_list" if a new type needs to be added.
1205 * Returns NULL in case of failure.
1206 */
1207 static type_T *
1208parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1209{
1210 type_T *member_type;
1211
1212 if (**arg != '<')
1213 {
1214 if (*skipwhite(*arg) == '<')
1215 emsg(_("E1007: No white space allowed before <"));
1216 else
1217 emsg(_("E1008: Missing <type>"));
1218 return NULL;
1219 }
1220 *arg = skipwhite(*arg + 1);
1221
1222 member_type = parse_type(arg, type_list);
1223 if (member_type == NULL)
1224 return NULL;
1225
1226 *arg = skipwhite(*arg);
1227 if (**arg != '>')
1228 {
1229 emsg(_("E1009: Missing > after type"));
1230 return NULL;
1231 }
1232 ++*arg;
1233
1234 if (type->tt_type == VAR_LIST)
1235 return get_list_type(member_type, type_list);
1236 return get_dict_type(member_type, type_list);
1237}
1238
1239/*
1240 * Parse a type at "arg" and advance over it.
1241 * Return NULL for failure.
1242 */
1243 type_T *
1244parse_type(char_u **arg, garray_T *type_list)
1245{
1246 char_u *p = *arg;
1247 size_t len;
1248
1249 // skip over the first word
1250 while (ASCII_ISALNUM(*p) || *p == '_')
1251 ++p;
1252 len = p - *arg;
1253
1254 switch (**arg)
1255 {
1256 case 'a':
1257 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1258 {
1259 *arg += len;
1260 return &t_any;
1261 }
1262 break;
1263 case 'b':
1264 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1265 {
1266 *arg += len;
1267 return &t_bool;
1268 }
1269 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1270 {
1271 *arg += len;
1272 return &t_blob;
1273 }
1274 break;
1275 case 'c':
1276 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1277 {
1278 *arg += len;
1279 return &t_channel;
1280 }
1281 break;
1282 case 'd':
1283 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1284 {
1285 *arg += len;
1286 return parse_type_member(arg, &t_dict_any, type_list);
1287 }
1288 break;
1289 case 'f':
1290 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1291 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001292#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 *arg += len;
1294 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001295#else
1296 emsg(_("E1055: This Vim is not compiled with float support"));
1297 return &t_any;
1298#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 }
1300 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1301 {
1302 *arg += len;
1303 // TODO: arguments and return type
1304 return &t_func_any;
1305 }
1306 break;
1307 case 'j':
1308 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1309 {
1310 *arg += len;
1311 return &t_job;
1312 }
1313 break;
1314 case 'l':
1315 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1316 {
1317 *arg += len;
1318 return parse_type_member(arg, &t_list_any, type_list);
1319 }
1320 break;
1321 case 'n':
1322 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1323 {
1324 *arg += len;
1325 return &t_number;
1326 }
1327 break;
1328 case 'p':
1329 if (len == 4 && STRNCMP(*arg, "partial", len) == 0)
1330 {
1331 *arg += len;
1332 // TODO: arguments and return type
1333 return &t_partial_any;
1334 }
1335 break;
1336 case 's':
1337 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1338 {
1339 *arg += len;
1340 return &t_string;
1341 }
1342 break;
1343 case 'v':
1344 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1345 {
1346 *arg += len;
1347 return &t_void;
1348 }
1349 break;
1350 }
1351
1352 semsg(_("E1010: Type not recognized: %s"), *arg);
1353 return &t_any;
1354}
1355
1356/*
1357 * Check if "type1" and "type2" are exactly the same.
1358 */
1359 static int
1360equal_type(type_T *type1, type_T *type2)
1361{
1362 if (type1->tt_type != type2->tt_type)
1363 return FALSE;
1364 switch (type1->tt_type)
1365 {
1366 case VAR_VOID:
1367 case VAR_UNKNOWN:
1368 case VAR_SPECIAL:
1369 case VAR_BOOL:
1370 case VAR_NUMBER:
1371 case VAR_FLOAT:
1372 case VAR_STRING:
1373 case VAR_BLOB:
1374 case VAR_JOB:
1375 case VAR_CHANNEL:
1376 return TRUE; // not composite is always OK
1377 case VAR_LIST:
1378 case VAR_DICT:
1379 return equal_type(type1->tt_member, type2->tt_member);
1380 case VAR_FUNC:
1381 case VAR_PARTIAL:
1382 // TODO; check argument types.
1383 return equal_type(type1->tt_member, type2->tt_member)
1384 && type1->tt_argcount == type2->tt_argcount;
1385 }
1386 return TRUE;
1387}
1388
1389/*
1390 * Find the common type of "type1" and "type2" and put it in "dest".
1391 * "type2" and "dest" may be the same.
1392 */
1393 static void
1394common_type(type_T *type1, type_T *type2, type_T *dest)
1395{
1396 if (equal_type(type1, type2))
1397 {
1398 if (dest != type2)
1399 *dest = *type2;
1400 return;
1401 }
1402
1403 if (type1->tt_type == type2->tt_type)
1404 {
1405 dest->tt_type = type1->tt_type;
1406 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1407 {
1408 common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1409 return;
1410 }
1411 // TODO: VAR_FUNC and VAR_PARTIAL
1412 }
1413
1414 dest->tt_type = VAR_UNKNOWN; // "any"
1415}
1416
1417 char *
1418vartype_name(vartype_T type)
1419{
1420 switch (type)
1421 {
1422 case VAR_VOID: return "void";
1423 case VAR_UNKNOWN: return "any";
1424 case VAR_SPECIAL: return "special";
1425 case VAR_BOOL: return "bool";
1426 case VAR_NUMBER: return "number";
1427 case VAR_FLOAT: return "float";
1428 case VAR_STRING: return "string";
1429 case VAR_BLOB: return "blob";
1430 case VAR_JOB: return "job";
1431 case VAR_CHANNEL: return "channel";
1432 case VAR_LIST: return "list";
1433 case VAR_DICT: return "dict";
1434 case VAR_FUNC: return "function";
1435 case VAR_PARTIAL: return "partial";
1436 }
1437 return "???";
1438}
1439
1440/*
1441 * Return the name of a type.
1442 * The result may be in allocated memory, in which case "tofree" is set.
1443 */
1444 char *
1445type_name(type_T *type, char **tofree)
1446{
1447 char *name = vartype_name(type->tt_type);
1448
1449 *tofree = NULL;
1450 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1451 {
1452 char *member_free;
1453 char *member_name = type_name(type->tt_member, &member_free);
1454 size_t len;
1455
1456 len = STRLEN(name) + STRLEN(member_name) + 3;
1457 *tofree = alloc(len);
1458 if (*tofree != NULL)
1459 {
1460 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1461 vim_free(member_free);
1462 return *tofree;
1463 }
1464 }
1465 // TODO: function and partial argument types
1466
1467 return name;
1468}
1469
1470/*
1471 * Find "name" in script-local items of script "sid".
1472 * Returns the index in "sn_var_vals" if found.
1473 * If found but not in "sn_var_vals" returns -1.
1474 * If not found returns -2.
1475 */
1476 int
1477get_script_item_idx(int sid, char_u *name, int check_writable)
1478{
1479 hashtab_T *ht;
1480 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001481 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 int idx;
1483
1484 // First look the name up in the hashtable.
1485 if (sid <= 0 || sid > script_items.ga_len)
1486 return -1;
1487 ht = &SCRIPT_VARS(sid);
1488 di = find_var_in_ht(ht, 0, name, TRUE);
1489 if (di == NULL)
1490 return -2;
1491
1492 // Now find the svar_T index in sn_var_vals.
1493 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1494 {
1495 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1496
1497 if (sv->sv_tv == &di->di_tv)
1498 {
1499 if (check_writable && sv->sv_const)
1500 semsg(_(e_readonlyvar), name);
1501 return idx;
1502 }
1503 }
1504 return -1;
1505}
1506
1507/*
1508 * Find "name" in imported items of the current script/
1509 */
1510 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001511find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001513 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 int idx;
1515
1516 if (cctx != NULL)
1517 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1518 {
1519 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1520 + idx;
1521
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001522 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1523 : STRLEN(import->imp_name) == len
1524 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 return import;
1526 }
1527
1528 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1529 {
1530 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1531
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001532 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1533 : STRLEN(import->imp_name) == len
1534 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 return import;
1536 }
1537 return NULL;
1538}
1539
1540/*
1541 * Generate an instruction to load script-local variable "name".
1542 */
1543 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001544compile_load_scriptvar(
1545 cctx_T *cctx,
1546 char_u *name, // variable NUL terminated
1547 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001548 char_u **end, // end of variable
1549 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001551 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1553 imported_T *import;
1554
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001555 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001557 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001558 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1559 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 }
1561 if (idx >= 0)
1562 {
1563 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1564
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001565 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 current_sctx.sc_sid, idx, sv->sv_type);
1567 return OK;
1568 }
1569
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001570 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 if (import != NULL)
1572 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001573 if (import->imp_all)
1574 {
1575 char_u *p = skipwhite(*end);
1576 int name_len;
1577 ufunc_T *ufunc;
1578 type_T *type;
1579
1580 // Used "import * as Name", need to lookup the member.
1581 if (*p != '.')
1582 {
1583 semsg(_("E1060: expected dot after name: %s"), start);
1584 return FAIL;
1585 }
1586 ++p;
1587
1588 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1589 // TODO: what if it is a function?
1590 if (idx < 0)
1591 return FAIL;
1592 *end = p;
1593
1594 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1595 import->imp_sid,
1596 idx,
1597 type);
1598 }
1599 else
1600 {
1601 // TODO: check this is a variable, not a function
1602 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1603 import->imp_sid,
1604 import->imp_var_vals_idx,
1605 import->imp_type);
1606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 return OK;
1608 }
1609
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001610 if (error)
1611 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 return FAIL;
1613}
1614
1615/*
1616 * Compile a variable name into a load instruction.
1617 * "end" points to just after the name.
1618 * When "error" is FALSE do not give an error when not found.
1619 */
1620 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001621compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622{
1623 type_T *type;
1624 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001625 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 int res = FAIL;
1627
1628 if (*(*arg + 1) == ':')
1629 {
1630 // load namespaced variable
1631 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1632 if (name == NULL)
1633 return FAIL;
1634
1635 if (**arg == 'v')
1636 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001637 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 }
1639 else if (**arg == 'g')
1640 {
1641 // Global variables can be defined later, thus we don't check if it
1642 // exists, give error at runtime.
1643 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1644 }
1645 else if (**arg == 's')
1646 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001647 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 }
1649 else
1650 {
1651 semsg("Namespace not supported yet: %s", **arg);
1652 goto theend;
1653 }
1654 }
1655 else
1656 {
1657 size_t len = end - *arg;
1658 int idx;
1659 int gen_load = FALSE;
1660
1661 name = vim_strnsave(*arg, end - *arg);
1662 if (name == NULL)
1663 return FAIL;
1664
1665 idx = lookup_arg(*arg, len, cctx);
1666 if (idx >= 0)
1667 {
1668 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1669 type = cctx->ctx_ufunc->uf_arg_types[idx];
1670 else
1671 type = &t_any;
1672
1673 // Arguments are located above the frame pointer.
1674 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1675 if (cctx->ctx_ufunc->uf_va_name != NULL)
1676 --idx;
1677 gen_load = TRUE;
1678 }
1679 else if (lookup_vararg(*arg, len, cctx))
1680 {
1681 // varargs is always the last argument
1682 idx = -STACK_FRAME_SIZE - 1;
1683 type = cctx->ctx_ufunc->uf_va_type;
1684 gen_load = TRUE;
1685 }
1686 else
1687 {
1688 idx = lookup_local(*arg, len, cctx);
1689 if (idx >= 0)
1690 {
1691 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1692 gen_load = TRUE;
1693 }
1694 else
1695 {
1696 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1697 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1698 res = generate_PUSHBOOL(cctx, **arg == 't'
1699 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001700 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1701 == SCRIPT_VERSION_VIM9)
1702 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001703 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 }
1705 }
1706 if (gen_load)
1707 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1708 }
1709
1710 *arg = end;
1711
1712theend:
1713 if (res == FAIL && error)
1714 semsg(_(e_var_notfound), name);
1715 vim_free(name);
1716 return res;
1717}
1718
1719/*
1720 * Compile the argument expressions.
1721 * "arg" points to just after the "(" and is advanced to after the ")"
1722 */
1723 static int
1724compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1725{
1726 char_u *p = *arg;
1727
1728 while (*p != NUL && *p != ')')
1729 {
1730 if (compile_expr1(&p, cctx) == FAIL)
1731 return FAIL;
1732 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001733
1734 if (*p != ',' && *skipwhite(p) == ',')
1735 {
1736 emsg(_("E1068: No white space allowed before ,"));
1737 p = skipwhite(p);
1738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001740 {
1741 ++p;
1742 if (!VIM_ISWHITE(*p))
1743 emsg(_("E1069: white space required after ,"));
1744 }
1745 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001747 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 if (*p != ')')
1749 {
1750 emsg(_(e_missing_close));
1751 return FAIL;
1752 }
1753 *arg = p + 1;
1754 return OK;
1755}
1756
1757/*
1758 * Compile a function call: name(arg1, arg2)
1759 * "arg" points to "name", "arg + varlen" to the "(".
1760 * "argcount_init" is 1 for "value->method()"
1761 * Instructions:
1762 * EVAL arg1
1763 * EVAL arg2
1764 * BCALL / DCALL / UCALL
1765 */
1766 static int
1767compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1768{
1769 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001770 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 int argcount = argcount_init;
1772 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001773 char_u fname_buf[FLEN_FIXED + 1];
1774 char_u *tofree = NULL;
1775 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001777 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778
1779 if (varlen >= sizeof(namebuf))
1780 {
1781 semsg(_("E1011: name too long: %s"), name);
1782 return FAIL;
1783 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001784 vim_strncpy(namebuf, *arg, varlen);
1785 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786
1787 *arg = skipwhite(*arg + varlen + 1);
1788 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001789 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001791 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 {
1793 int idx;
1794
1795 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001796 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001798 {
1799 res = generate_BCALL(cctx, idx, argcount);
1800 goto theend;
1801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 semsg(_(e_unknownfunc), namebuf);
1803 }
1804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001806 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001808 {
1809 res = generate_CALL(cctx, ufunc, argcount);
1810 goto theend;
1811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812
1813 // If the name is a variable, load it and use PCALL.
1814 p = namebuf;
1815 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001816 {
1817 res = generate_PCALL(cctx, argcount, FALSE);
1818 goto theend;
1819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
1821 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001822 res = generate_UCALL(cctx, name, argcount);
1823
1824theend:
1825 vim_free(tofree);
1826 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827}
1828
1829// like NAMESPACE_CHAR but with 'a' and 'l'.
1830#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1831
1832/*
1833 * Find the end of a variable or function name. Unlike find_name_end() this
1834 * does not recognize magic braces.
1835 * Return a pointer to just after the name. Equal to "arg" if there is no
1836 * valid name.
1837 */
1838 char_u *
1839to_name_end(char_u *arg)
1840{
1841 char_u *p;
1842
1843 // Quick check for valid starting character.
1844 if (!eval_isnamec1(*arg))
1845 return arg;
1846
1847 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1848 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1849 // and can be used in slice "[n:]".
1850 if (*p == ':' && (p != arg + 1
1851 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1852 break;
1853 return p;
1854}
1855
1856/*
1857 * Like to_name_end() but also skip over a list or dict constant.
1858 */
1859 char_u *
1860to_name_const_end(char_u *arg)
1861{
1862 char_u *p = to_name_end(arg);
1863 typval_T rettv;
1864
1865 if (p == arg && *arg == '[')
1866 {
1867
1868 // Can be "[1, 2, 3]->Func()".
1869 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1870 p = arg;
1871 }
1872 else if (p == arg && *arg == '#' && arg[1] == '{')
1873 {
1874 ++p;
1875 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1876 p = arg;
1877 }
1878 else if (p == arg && *arg == '{')
1879 {
1880 int ret = get_lambda_tv(&p, &rettv, FALSE);
1881
1882 if (ret == NOTDONE)
1883 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1884 if (ret != OK)
1885 p = arg;
1886 }
1887
1888 return p;
1889}
1890
1891 static void
1892type_mismatch(type_T *expected, type_T *actual)
1893{
1894 char *tofree1, *tofree2;
1895
1896 semsg(_("E1013: type mismatch, expected %s but got %s"),
1897 type_name(expected, &tofree1), type_name(actual, &tofree2));
1898 vim_free(tofree1);
1899 vim_free(tofree2);
1900}
1901
1902/*
1903 * Check if the expected and actual types match.
1904 */
1905 static int
1906check_type(type_T *expected, type_T *actual, int give_msg)
1907{
1908 if (expected->tt_type != VAR_UNKNOWN)
1909 {
1910 if (expected->tt_type != actual->tt_type)
1911 {
1912 if (give_msg)
1913 type_mismatch(expected, actual);
1914 return FAIL;
1915 }
1916 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1917 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01001918 int ret;
1919
1920 // void is used for an empty list or dict
1921 if (actual->tt_member == &t_void)
1922 ret = OK;
1923 else
1924 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if (ret == FAIL && give_msg)
1926 type_mismatch(expected, actual);
1927 return ret;
1928 }
1929 }
1930 return OK;
1931}
1932
1933/*
1934 * Check that
1935 * - "actual" is "expected" type or
1936 * - "actual" is a type that can be "expected" type: add a runtime check; or
1937 * - return FAIL.
1938 */
1939 static int
1940need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1941{
Bram Moolenaar436472f2020-02-20 22:54:43 +01001942 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 return OK;
1944 if (actual->tt_type != VAR_UNKNOWN)
1945 {
1946 type_mismatch(expected, actual);
1947 return FAIL;
1948 }
1949 generate_TYPECHECK(cctx, expected, offset);
1950 return OK;
1951}
1952
1953/*
1954 * parse a list: [expr, expr]
1955 * "*arg" points to the '['.
1956 */
1957 static int
1958compile_list(char_u **arg, cctx_T *cctx)
1959{
1960 char_u *p = skipwhite(*arg + 1);
1961 int count = 0;
1962
1963 while (*p != ']')
1964 {
1965 if (*p == NUL)
1966 return FAIL;
1967 if (compile_expr1(&p, cctx) == FAIL)
1968 break;
1969 ++count;
1970 if (*p == ',')
1971 ++p;
1972 p = skipwhite(p);
1973 }
1974 *arg = p + 1;
1975
1976 generate_NEWLIST(cctx, count);
1977 return OK;
1978}
1979
1980/*
1981 * parse a lambda: {arg, arg -> expr}
1982 * "*arg" points to the '{'.
1983 */
1984 static int
1985compile_lambda(char_u **arg, cctx_T *cctx)
1986{
1987 garray_T *instr = &cctx->ctx_instr;
1988 typval_T rettv;
1989 ufunc_T *ufunc;
1990
1991 // Get the funcref in "rettv".
1992 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1993 return FAIL;
1994 ufunc = rettv.vval.v_partial->pt_func;
1995
1996 // The function will have one line: "return {expr}".
1997 // Compile it into instructions.
1998 compile_def_function(ufunc, TRUE);
1999
2000 if (ufunc->uf_dfunc_idx >= 0)
2001 {
2002 if (ga_grow(instr, 1) == FAIL)
2003 return FAIL;
2004 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2005 return OK;
2006 }
2007 return FAIL;
2008}
2009
2010/*
2011 * Compile a lamda call: expr->{lambda}(args)
2012 * "arg" points to the "{".
2013 */
2014 static int
2015compile_lambda_call(char_u **arg, cctx_T *cctx)
2016{
2017 ufunc_T *ufunc;
2018 typval_T rettv;
2019 int argcount = 1;
2020 int ret = FAIL;
2021
2022 // Get the funcref in "rettv".
2023 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2024 return FAIL;
2025
2026 if (**arg != '(')
2027 {
2028 if (*skipwhite(*arg) == '(')
2029 semsg(_(e_nowhitespace));
2030 else
2031 semsg(_(e_missing_paren), "lambda");
2032 clear_tv(&rettv);
2033 return FAIL;
2034 }
2035
2036 // The function will have one line: "return {expr}".
2037 // Compile it into instructions.
2038 ufunc = rettv.vval.v_partial->pt_func;
2039 ++ufunc->uf_refcount;
2040 compile_def_function(ufunc, TRUE);
2041
2042 // compile the arguments
2043 *arg = skipwhite(*arg + 1);
2044 if (compile_arguments(arg, cctx, &argcount) == OK)
2045 // call the compiled function
2046 ret = generate_CALL(cctx, ufunc, argcount);
2047
2048 clear_tv(&rettv);
2049 return ret;
2050}
2051
2052/*
2053 * parse a dict: {'key': val} or #{key: val}
2054 * "*arg" points to the '{'.
2055 */
2056 static int
2057compile_dict(char_u **arg, cctx_T *cctx, int literal)
2058{
2059 garray_T *instr = &cctx->ctx_instr;
2060 int count = 0;
2061 dict_T *d = dict_alloc();
2062 dictitem_T *item;
2063
2064 if (d == NULL)
2065 return FAIL;
2066 *arg = skipwhite(*arg + 1);
2067 while (**arg != '}' && **arg != NUL)
2068 {
2069 char_u *key = NULL;
2070
2071 if (literal)
2072 {
2073 char_u *p = to_name_end(*arg);
2074
2075 if (p == *arg)
2076 {
2077 semsg(_("E1014: Invalid key: %s"), *arg);
2078 return FAIL;
2079 }
2080 key = vim_strnsave(*arg, p - *arg);
2081 if (generate_PUSHS(cctx, key) == FAIL)
2082 return FAIL;
2083 *arg = p;
2084 }
2085 else
2086 {
2087 isn_T *isn;
2088
2089 if (compile_expr1(arg, cctx) == FAIL)
2090 return FAIL;
2091 // TODO: check type is string
2092 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2093 if (isn->isn_type == ISN_PUSHS)
2094 key = isn->isn_arg.string;
2095 }
2096
2097 // Check for duplicate keys, if using string keys.
2098 if (key != NULL)
2099 {
2100 item = dict_find(d, key, -1);
2101 if (item != NULL)
2102 {
2103 semsg(_(e_duplicate_key), key);
2104 goto failret;
2105 }
2106 item = dictitem_alloc(key);
2107 if (item != NULL)
2108 {
2109 item->di_tv.v_type = VAR_UNKNOWN;
2110 item->di_tv.v_lock = 0;
2111 if (dict_add(d, item) == FAIL)
2112 dictitem_free(item);
2113 }
2114 }
2115
2116 *arg = skipwhite(*arg);
2117 if (**arg != ':')
2118 {
2119 semsg(_(e_missing_dict_colon), *arg);
2120 return FAIL;
2121 }
2122
2123 *arg = skipwhite(*arg + 1);
2124 if (compile_expr1(arg, cctx) == FAIL)
2125 return FAIL;
2126 ++count;
2127
2128 if (**arg == '}')
2129 break;
2130 if (**arg != ',')
2131 {
2132 semsg(_(e_missing_dict_comma), *arg);
2133 goto failret;
2134 }
2135 *arg = skipwhite(*arg + 1);
2136 }
2137
2138 if (**arg != '}')
2139 {
2140 semsg(_(e_missing_dict_end), *arg);
2141 goto failret;
2142 }
2143 *arg = *arg + 1;
2144
2145 dict_unref(d);
2146 return generate_NEWDICT(cctx, count);
2147
2148failret:
2149 dict_unref(d);
2150 return FAIL;
2151}
2152
2153/*
2154 * Compile "&option".
2155 */
2156 static int
2157compile_get_option(char_u **arg, cctx_T *cctx)
2158{
2159 typval_T rettv;
2160 char_u *start = *arg;
2161 int ret;
2162
2163 // parse the option and get the current value to get the type.
2164 rettv.v_type = VAR_UNKNOWN;
2165 ret = get_option_tv(arg, &rettv, TRUE);
2166 if (ret == OK)
2167 {
2168 // include the '&' in the name, get_option_tv() expects it.
2169 char_u *name = vim_strnsave(start, *arg - start);
2170 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2171
2172 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2173 vim_free(name);
2174 }
2175 clear_tv(&rettv);
2176
2177 return ret;
2178}
2179
2180/*
2181 * Compile "$VAR".
2182 */
2183 static int
2184compile_get_env(char_u **arg, cctx_T *cctx)
2185{
2186 char_u *start = *arg;
2187 int len;
2188 int ret;
2189 char_u *name;
2190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 ++*arg;
2192 len = get_env_len(arg);
2193 if (len == 0)
2194 {
2195 semsg(_(e_syntax_at), start - 1);
2196 return FAIL;
2197 }
2198
2199 // include the '$' in the name, get_env_tv() expects it.
2200 name = vim_strnsave(start, len + 1);
2201 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2202 vim_free(name);
2203 return ret;
2204}
2205
2206/*
2207 * Compile "@r".
2208 */
2209 static int
2210compile_get_register(char_u **arg, cctx_T *cctx)
2211{
2212 int ret;
2213
2214 ++*arg;
2215 if (**arg == NUL)
2216 {
2217 semsg(_(e_syntax_at), *arg - 1);
2218 return FAIL;
2219 }
2220 if (!valid_yank_reg(**arg, TRUE))
2221 {
2222 emsg_invreg(**arg);
2223 return FAIL;
2224 }
2225 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2226 ++*arg;
2227 return ret;
2228}
2229
2230/*
2231 * Apply leading '!', '-' and '+' to constant "rettv".
2232 */
2233 static int
2234apply_leader(typval_T *rettv, char_u *start, char_u *end)
2235{
2236 char_u *p = end;
2237
2238 // this works from end to start
2239 while (p > start)
2240 {
2241 --p;
2242 if (*p == '-' || *p == '+')
2243 {
2244 // only '-' has an effect, for '+' we only check the type
2245#ifdef FEAT_FLOAT
2246 if (rettv->v_type == VAR_FLOAT)
2247 {
2248 if (*p == '-')
2249 rettv->vval.v_float = -rettv->vval.v_float;
2250 }
2251 else
2252#endif
2253 {
2254 varnumber_T val;
2255 int error = FALSE;
2256
2257 // tv_get_number_chk() accepts a string, but we don't want that
2258 // here
2259 if (check_not_string(rettv) == FAIL)
2260 return FAIL;
2261 val = tv_get_number_chk(rettv, &error);
2262 clear_tv(rettv);
2263 if (error)
2264 return FAIL;
2265 if (*p == '-')
2266 val = -val;
2267 rettv->v_type = VAR_NUMBER;
2268 rettv->vval.v_number = val;
2269 }
2270 }
2271 else
2272 {
2273 int v = tv2bool(rettv);
2274
2275 // '!' is permissive in the type.
2276 clear_tv(rettv);
2277 rettv->v_type = VAR_BOOL;
2278 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2279 }
2280 }
2281 return OK;
2282}
2283
2284/*
2285 * Recognize v: variables that are constants and set "rettv".
2286 */
2287 static void
2288get_vim_constant(char_u **arg, typval_T *rettv)
2289{
2290 if (STRNCMP(*arg, "v:true", 6) == 0)
2291 {
2292 rettv->v_type = VAR_BOOL;
2293 rettv->vval.v_number = VVAL_TRUE;
2294 *arg += 6;
2295 }
2296 else if (STRNCMP(*arg, "v:false", 7) == 0)
2297 {
2298 rettv->v_type = VAR_BOOL;
2299 rettv->vval.v_number = VVAL_FALSE;
2300 *arg += 7;
2301 }
2302 else if (STRNCMP(*arg, "v:null", 6) == 0)
2303 {
2304 rettv->v_type = VAR_SPECIAL;
2305 rettv->vval.v_number = VVAL_NULL;
2306 *arg += 6;
2307 }
2308 else if (STRNCMP(*arg, "v:none", 6) == 0)
2309 {
2310 rettv->v_type = VAR_SPECIAL;
2311 rettv->vval.v_number = VVAL_NONE;
2312 *arg += 6;
2313 }
2314}
2315
2316/*
2317 * Compile code to apply '-', '+' and '!'.
2318 */
2319 static int
2320compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2321{
2322 char_u *p = end;
2323
2324 // this works from end to start
2325 while (p > start)
2326 {
2327 --p;
2328 if (*p == '-' || *p == '+')
2329 {
2330 int negate = *p == '-';
2331 isn_T *isn;
2332
2333 // TODO: check type
2334 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2335 {
2336 --p;
2337 if (*p == '-')
2338 negate = !negate;
2339 }
2340 // only '-' has an effect, for '+' we only check the type
2341 if (negate)
2342 isn = generate_instr(cctx, ISN_NEGATENR);
2343 else
2344 isn = generate_instr(cctx, ISN_CHECKNR);
2345 if (isn == NULL)
2346 return FAIL;
2347 }
2348 else
2349 {
2350 int invert = TRUE;
2351
2352 while (p > start && p[-1] == '!')
2353 {
2354 --p;
2355 invert = !invert;
2356 }
2357 if (generate_2BOOL(cctx, invert) == FAIL)
2358 return FAIL;
2359 }
2360 }
2361 return OK;
2362}
2363
2364/*
2365 * Compile whatever comes after "name" or "name()".
2366 */
2367 static int
2368compile_subscript(
2369 char_u **arg,
2370 cctx_T *cctx,
2371 char_u **start_leader,
2372 char_u *end_leader)
2373{
2374 for (;;)
2375 {
2376 if (**arg == '(')
2377 {
2378 int argcount = 0;
2379
2380 // funcref(arg)
2381 *arg = skipwhite(*arg + 1);
2382 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2383 return FAIL;
2384 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2385 return FAIL;
2386 }
2387 else if (**arg == '-' && (*arg)[1] == '>')
2388 {
2389 char_u *p;
2390
2391 // something->method()
2392 // Apply the '!', '-' and '+' first:
2393 // -1.0->func() works like (-1.0)->func()
2394 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2395 return FAIL;
2396 *start_leader = end_leader; // don't apply again later
2397
2398 *arg = skipwhite(*arg + 2);
2399 if (**arg == '{')
2400 {
2401 // lambda call: list->{lambda}
2402 if (compile_lambda_call(arg, cctx) == FAIL)
2403 return FAIL;
2404 }
2405 else
2406 {
2407 // method call: list->method()
2408 for (p = *arg; eval_isnamec1(*p); ++p)
2409 ;
2410 if (*p != '(')
2411 {
2412 semsg(_(e_missing_paren), arg);
2413 return FAIL;
2414 }
2415 // TODO: base value may not be the first argument
2416 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2417 return FAIL;
2418 }
2419 }
2420 else if (**arg == '[')
2421 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002422 garray_T *stack;
2423 type_T **typep;
2424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 // list index: list[123]
2426 // TODO: more arguments
2427 // TODO: dict member dict['name']
2428 *arg = skipwhite(*arg + 1);
2429 if (compile_expr1(arg, cctx) == FAIL)
2430 return FAIL;
2431
2432 if (**arg != ']')
2433 {
2434 emsg(_(e_missbrac));
2435 return FAIL;
2436 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002437 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2440 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002441 stack = &cctx->ctx_type_stack;
2442 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2443 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2444 {
2445 emsg(_(e_listreq));
2446 return FAIL;
2447 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002448 if ((*typep)->tt_type == VAR_LIST)
2449 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 }
2451 else if (**arg == '.' && (*arg)[1] != '.')
2452 {
2453 char_u *p;
2454
2455 ++*arg;
2456 p = *arg;
2457 // dictionary member: dict.name
2458 if (eval_isnamec1(*p))
2459 while (eval_isnamec(*p))
2460 MB_PTR_ADV(p);
2461 if (p == *arg)
2462 {
2463 semsg(_(e_syntax_at), *arg);
2464 return FAIL;
2465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2467 return FAIL;
2468 *arg = p;
2469 }
2470 else
2471 break;
2472 }
2473
2474 // TODO - see handle_subscript():
2475 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2476 // Don't do this when "Func" is already a partial that was bound
2477 // explicitly (pt_auto is FALSE).
2478
2479 return OK;
2480}
2481
2482/*
2483 * Compile an expression at "*p" and add instructions to "instr".
2484 * "p" is advanced until after the expression, skipping white space.
2485 *
2486 * This is the equivalent of eval1(), eval2(), etc.
2487 */
2488
2489/*
2490 * number number constant
2491 * 0zFFFFFFFF Blob constant
2492 * "string" string constant
2493 * 'string' literal string constant
2494 * &option-name option value
2495 * @r register contents
2496 * identifier variable value
2497 * function() function call
2498 * $VAR environment variable
2499 * (expression) nested expression
2500 * [expr, expr] List
2501 * {key: val, key: val} Dictionary
2502 * #{key: val, key: val} Dictionary with literal keys
2503 *
2504 * Also handle:
2505 * ! in front logical NOT
2506 * - in front unary minus
2507 * + in front unary plus (ignored)
2508 * trailing (arg) funcref/partial call
2509 * trailing [] subscript in String or List
2510 * trailing .name entry in Dictionary
2511 * trailing ->name() method call
2512 */
2513 static int
2514compile_expr7(char_u **arg, cctx_T *cctx)
2515{
2516 typval_T rettv;
2517 char_u *start_leader, *end_leader;
2518 int ret = OK;
2519
2520 /*
2521 * Skip '!', '-' and '+' characters. They are handled later.
2522 */
2523 start_leader = *arg;
2524 while (**arg == '!' || **arg == '-' || **arg == '+')
2525 *arg = skipwhite(*arg + 1);
2526 end_leader = *arg;
2527
2528 rettv.v_type = VAR_UNKNOWN;
2529 switch (**arg)
2530 {
2531 /*
2532 * Number constant.
2533 */
2534 case '0': // also for blob starting with 0z
2535 case '1':
2536 case '2':
2537 case '3':
2538 case '4':
2539 case '5':
2540 case '6':
2541 case '7':
2542 case '8':
2543 case '9':
2544 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2545 return FAIL;
2546 break;
2547
2548 /*
2549 * String constant: "string".
2550 */
2551 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2552 return FAIL;
2553 break;
2554
2555 /*
2556 * Literal string constant: 'str''ing'.
2557 */
2558 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2559 return FAIL;
2560 break;
2561
2562 /*
2563 * Constant Vim variable.
2564 */
2565 case 'v': get_vim_constant(arg, &rettv);
2566 ret = NOTDONE;
2567 break;
2568
2569 /*
2570 * List: [expr, expr]
2571 */
2572 case '[': ret = compile_list(arg, cctx);
2573 break;
2574
2575 /*
2576 * Dictionary: #{key: val, key: val}
2577 */
2578 case '#': if ((*arg)[1] == '{')
2579 {
2580 ++*arg;
2581 ret = compile_dict(arg, cctx, TRUE);
2582 }
2583 else
2584 ret = NOTDONE;
2585 break;
2586
2587 /*
2588 * Lambda: {arg, arg -> expr}
2589 * Dictionary: {'key': val, 'key': val}
2590 */
2591 case '{': {
2592 char_u *start = skipwhite(*arg + 1);
2593
2594 // Find out what comes after the arguments.
2595 ret = get_function_args(&start, '-', NULL,
2596 NULL, NULL, NULL, TRUE);
2597 if (ret != FAIL && *start == '>')
2598 ret = compile_lambda(arg, cctx);
2599 else
2600 ret = compile_dict(arg, cctx, FALSE);
2601 }
2602 break;
2603
2604 /*
2605 * Option value: &name
2606 */
2607 case '&': ret = compile_get_option(arg, cctx);
2608 break;
2609
2610 /*
2611 * Environment variable: $VAR.
2612 */
2613 case '$': ret = compile_get_env(arg, cctx);
2614 break;
2615
2616 /*
2617 * Register contents: @r.
2618 */
2619 case '@': ret = compile_get_register(arg, cctx);
2620 break;
2621 /*
2622 * nested expression: (expression).
2623 */
2624 case '(': *arg = skipwhite(*arg + 1);
2625 ret = compile_expr1(arg, cctx); // recursive!
2626 *arg = skipwhite(*arg);
2627 if (**arg == ')')
2628 ++*arg;
2629 else if (ret == OK)
2630 {
2631 emsg(_(e_missing_close));
2632 ret = FAIL;
2633 }
2634 break;
2635
2636 default: ret = NOTDONE;
2637 break;
2638 }
2639 if (ret == FAIL)
2640 return FAIL;
2641
2642 if (rettv.v_type != VAR_UNKNOWN)
2643 {
2644 // apply the '!', '-' and '+' before the constant
2645 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2646 {
2647 clear_tv(&rettv);
2648 return FAIL;
2649 }
2650 start_leader = end_leader; // don't apply again below
2651
2652 // push constant
2653 switch (rettv.v_type)
2654 {
2655 case VAR_BOOL:
2656 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2657 break;
2658 case VAR_SPECIAL:
2659 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2660 break;
2661 case VAR_NUMBER:
2662 generate_PUSHNR(cctx, rettv.vval.v_number);
2663 break;
2664#ifdef FEAT_FLOAT
2665 case VAR_FLOAT:
2666 generate_PUSHF(cctx, rettv.vval.v_float);
2667 break;
2668#endif
2669 case VAR_BLOB:
2670 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2671 rettv.vval.v_blob = NULL;
2672 break;
2673 case VAR_STRING:
2674 generate_PUSHS(cctx, rettv.vval.v_string);
2675 rettv.vval.v_string = NULL;
2676 break;
2677 default:
2678 iemsg("constant type missing");
2679 return FAIL;
2680 }
2681 }
2682 else if (ret == NOTDONE)
2683 {
2684 char_u *p;
2685 int r;
2686
2687 if (!eval_isnamec1(**arg))
2688 {
2689 semsg(_("E1015: Name expected: %s"), *arg);
2690 return FAIL;
2691 }
2692
2693 // "name" or "name()"
2694 p = to_name_end(*arg);
2695 if (*p == '(')
2696 r = compile_call(arg, p - *arg, cctx, 0);
2697 else
2698 r = compile_load(arg, p, cctx, TRUE);
2699 if (r == FAIL)
2700 return FAIL;
2701 }
2702
2703 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2704 return FAIL;
2705
2706 // Now deal with prefixed '-', '+' and '!', if not done already.
2707 return compile_leader(cctx, start_leader, end_leader);
2708}
2709
2710/*
2711 * * number multiplication
2712 * / number division
2713 * % number modulo
2714 */
2715 static int
2716compile_expr6(char_u **arg, cctx_T *cctx)
2717{
2718 char_u *op;
2719
2720 // get the first variable
2721 if (compile_expr7(arg, cctx) == FAIL)
2722 return FAIL;
2723
2724 /*
2725 * Repeat computing, until no "*", "/" or "%" is following.
2726 */
2727 for (;;)
2728 {
2729 op = skipwhite(*arg);
2730 if (*op != '*' && *op != '/' && *op != '%')
2731 break;
2732 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2733 {
2734 char_u buf[3];
2735
2736 vim_strncpy(buf, op, 1);
2737 semsg(_(e_white_both), buf);
2738 }
2739 *arg = skipwhite(op + 1);
2740
2741 // get the second variable
2742 if (compile_expr7(arg, cctx) == FAIL)
2743 return FAIL;
2744
2745 generate_two_op(cctx, op);
2746 }
2747
2748 return OK;
2749}
2750
2751/*
2752 * + number addition
2753 * - number subtraction
2754 * .. string concatenation
2755 */
2756 static int
2757compile_expr5(char_u **arg, cctx_T *cctx)
2758{
2759 char_u *op;
2760 int oplen;
2761
2762 // get the first variable
2763 if (compile_expr6(arg, cctx) == FAIL)
2764 return FAIL;
2765
2766 /*
2767 * Repeat computing, until no "+", "-" or ".." is following.
2768 */
2769 for (;;)
2770 {
2771 op = skipwhite(*arg);
2772 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2773 break;
2774 oplen = (*op == '.' ? 2 : 1);
2775
2776 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2777 {
2778 char_u buf[3];
2779
2780 vim_strncpy(buf, op, oplen);
2781 semsg(_(e_white_both), buf);
2782 }
2783
2784 *arg = skipwhite(op + oplen);
2785
2786 // get the second variable
2787 if (compile_expr6(arg, cctx) == FAIL)
2788 return FAIL;
2789
2790 if (*op == '.')
2791 {
2792 if (may_generate_2STRING(-2, cctx) == FAIL
2793 || may_generate_2STRING(-1, cctx) == FAIL)
2794 return FAIL;
2795 generate_instr_drop(cctx, ISN_CONCAT, 1);
2796 }
2797 else
2798 generate_two_op(cctx, op);
2799 }
2800
2801 return OK;
2802}
2803
2804/*
2805 * expr5a == expr5b
2806 * expr5a =~ expr5b
2807 * expr5a != expr5b
2808 * expr5a !~ expr5b
2809 * expr5a > expr5b
2810 * expr5a >= expr5b
2811 * expr5a < expr5b
2812 * expr5a <= expr5b
2813 * expr5a is expr5b
2814 * expr5a isnot expr5b
2815 *
2816 * Produces instructions:
2817 * EVAL expr5a Push result of "expr5a"
2818 * EVAL expr5b Push result of "expr5b"
2819 * COMPARE one of the compare instructions
2820 */
2821 static int
2822compile_expr4(char_u **arg, cctx_T *cctx)
2823{
2824 exptype_T type = EXPR_UNKNOWN;
2825 char_u *p;
2826 int len = 2;
2827 int i;
2828 int type_is = FALSE;
2829
2830 // get the first variable
2831 if (compile_expr5(arg, cctx) == FAIL)
2832 return FAIL;
2833
2834 p = skipwhite(*arg);
2835 switch (p[0])
2836 {
2837 case '=': if (p[1] == '=')
2838 type = EXPR_EQUAL;
2839 else if (p[1] == '~')
2840 type = EXPR_MATCH;
2841 break;
2842 case '!': if (p[1] == '=')
2843 type = EXPR_NEQUAL;
2844 else if (p[1] == '~')
2845 type = EXPR_NOMATCH;
2846 break;
2847 case '>': if (p[1] != '=')
2848 {
2849 type = EXPR_GREATER;
2850 len = 1;
2851 }
2852 else
2853 type = EXPR_GEQUAL;
2854 break;
2855 case '<': if (p[1] != '=')
2856 {
2857 type = EXPR_SMALLER;
2858 len = 1;
2859 }
2860 else
2861 type = EXPR_SEQUAL;
2862 break;
2863 case 'i': if (p[1] == 's')
2864 {
2865 // "is" and "isnot"; but not a prefix of a name
2866 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2867 len = 5;
2868 i = p[len];
2869 if (!isalnum(i) && i != '_')
2870 {
2871 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2872 type_is = TRUE;
2873 }
2874 }
2875 break;
2876 }
2877
2878 /*
2879 * If there is a comparative operator, use it.
2880 */
2881 if (type != EXPR_UNKNOWN)
2882 {
2883 int ic = FALSE; // Default: do not ignore case
2884
2885 if (type_is && (p[len] == '?' || p[len] == '#'))
2886 {
2887 semsg(_(e_invexpr2), *arg);
2888 return FAIL;
2889 }
2890 // extra question mark appended: ignore case
2891 if (p[len] == '?')
2892 {
2893 ic = TRUE;
2894 ++len;
2895 }
2896 // extra '#' appended: match case (ignored)
2897 else if (p[len] == '#')
2898 ++len;
2899 // nothing appended: match case
2900
2901 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2902 {
2903 char_u buf[7];
2904
2905 vim_strncpy(buf, p, len);
2906 semsg(_(e_white_both), buf);
2907 }
2908
2909 // get the second variable
2910 *arg = skipwhite(p + len);
2911 if (compile_expr5(arg, cctx) == FAIL)
2912 return FAIL;
2913
2914 generate_COMPARE(cctx, type, ic);
2915 }
2916
2917 return OK;
2918}
2919
2920/*
2921 * Compile || or &&.
2922 */
2923 static int
2924compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2925{
2926 char_u *p = skipwhite(*arg);
2927 int opchar = *op;
2928
2929 if (p[0] == opchar && p[1] == opchar)
2930 {
2931 garray_T *instr = &cctx->ctx_instr;
2932 garray_T end_ga;
2933
2934 /*
2935 * Repeat until there is no following "||" or "&&"
2936 */
2937 ga_init2(&end_ga, sizeof(int), 10);
2938 while (p[0] == opchar && p[1] == opchar)
2939 {
2940 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2941 semsg(_(e_white_both), op);
2942
2943 if (ga_grow(&end_ga, 1) == FAIL)
2944 {
2945 ga_clear(&end_ga);
2946 return FAIL;
2947 }
2948 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2949 ++end_ga.ga_len;
2950 generate_JUMP(cctx, opchar == '|'
2951 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
2952
2953 // eval the next expression
2954 *arg = skipwhite(p + 2);
2955 if ((opchar == '|' ? compile_expr3(arg, cctx)
2956 : compile_expr4(arg, cctx)) == FAIL)
2957 {
2958 ga_clear(&end_ga);
2959 return FAIL;
2960 }
2961 p = skipwhite(*arg);
2962 }
2963
2964 // Fill in the end label in all jumps.
2965 while (end_ga.ga_len > 0)
2966 {
2967 isn_T *isn;
2968
2969 --end_ga.ga_len;
2970 isn = ((isn_T *)instr->ga_data)
2971 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2972 isn->isn_arg.jump.jump_where = instr->ga_len;
2973 }
2974 ga_clear(&end_ga);
2975 }
2976
2977 return OK;
2978}
2979
2980/*
2981 * expr4a && expr4a && expr4a logical AND
2982 *
2983 * Produces instructions:
2984 * EVAL expr4a Push result of "expr4a"
2985 * JUMP_AND_KEEP_IF_FALSE end
2986 * EVAL expr4b Push result of "expr4b"
2987 * JUMP_AND_KEEP_IF_FALSE end
2988 * EVAL expr4c Push result of "expr4c"
2989 * end:
2990 */
2991 static int
2992compile_expr3(char_u **arg, cctx_T *cctx)
2993{
2994 // get the first variable
2995 if (compile_expr4(arg, cctx) == FAIL)
2996 return FAIL;
2997
2998 // || and && work almost the same
2999 return compile_and_or(arg, cctx, "&&");
3000}
3001
3002/*
3003 * expr3a || expr3b || expr3c logical OR
3004 *
3005 * Produces instructions:
3006 * EVAL expr3a Push result of "expr3a"
3007 * JUMP_AND_KEEP_IF_TRUE end
3008 * EVAL expr3b Push result of "expr3b"
3009 * JUMP_AND_KEEP_IF_TRUE end
3010 * EVAL expr3c Push result of "expr3c"
3011 * end:
3012 */
3013 static int
3014compile_expr2(char_u **arg, cctx_T *cctx)
3015{
3016 // eval the first expression
3017 if (compile_expr3(arg, cctx) == FAIL)
3018 return FAIL;
3019
3020 // || and && work almost the same
3021 return compile_and_or(arg, cctx, "||");
3022}
3023
3024/*
3025 * Toplevel expression: expr2 ? expr1a : expr1b
3026 *
3027 * Produces instructions:
3028 * EVAL expr2 Push result of "expr"
3029 * JUMP_IF_FALSE alt jump if false
3030 * EVAL expr1a
3031 * JUMP_ALWAYS end
3032 * alt: EVAL expr1b
3033 * end:
3034 */
3035 static int
3036compile_expr1(char_u **arg, cctx_T *cctx)
3037{
3038 char_u *p;
3039
3040 // evaluate the first expression
3041 if (compile_expr2(arg, cctx) == FAIL)
3042 return FAIL;
3043
3044 p = skipwhite(*arg);
3045 if (*p == '?')
3046 {
3047 garray_T *instr = &cctx->ctx_instr;
3048 garray_T *stack = &cctx->ctx_type_stack;
3049 int alt_idx = instr->ga_len;
3050 int end_idx;
3051 isn_T *isn;
3052 type_T *type1;
3053 type_T *type2;
3054
3055 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3056 semsg(_(e_white_both), "?");
3057
3058 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3059
3060 // evaluate the second expression; any type is accepted
3061 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003062 if (compile_expr1(arg, cctx) == FAIL)
3063 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
3065 // remember the type and drop it
3066 --stack->ga_len;
3067 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3068
3069 end_idx = instr->ga_len;
3070 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3071
3072 // jump here from JUMP_IF_FALSE
3073 isn = ((isn_T *)instr->ga_data) + alt_idx;
3074 isn->isn_arg.jump.jump_where = instr->ga_len;
3075
3076 // Check for the ":".
3077 p = skipwhite(*arg);
3078 if (*p != ':')
3079 {
3080 emsg(_(e_missing_colon));
3081 return FAIL;
3082 }
3083 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3084 semsg(_(e_white_both), ":");
3085
3086 // evaluate the third expression
3087 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003088 if (compile_expr1(arg, cctx) == FAIL)
3089 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090
3091 // If the types differ, the result has a more generic type.
3092 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3093 common_type(type1, type2, type2);
3094
3095 // jump here from JUMP_ALWAYS
3096 isn = ((isn_T *)instr->ga_data) + end_idx;
3097 isn->isn_arg.jump.jump_where = instr->ga_len;
3098 }
3099 return OK;
3100}
3101
3102/*
3103 * compile "return [expr]"
3104 */
3105 static char_u *
3106compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3107{
3108 char_u *p = arg;
3109 garray_T *stack = &cctx->ctx_type_stack;
3110 type_T *stack_type;
3111
3112 if (*p != NUL && *p != '|' && *p != '\n')
3113 {
3114 // compile return argument into instructions
3115 if (compile_expr1(&p, cctx) == FAIL)
3116 return NULL;
3117
3118 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3119 if (set_return_type)
3120 cctx->ctx_ufunc->uf_ret_type = stack_type;
3121 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3122 == FAIL)
3123 return NULL;
3124 }
3125 else
3126 {
3127 if (set_return_type)
3128 cctx->ctx_ufunc->uf_ret_type = &t_void;
3129 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3130 {
3131 emsg(_("E1003: Missing return value"));
3132 return NULL;
3133 }
3134
3135 // No argument, return zero.
3136 generate_PUSHNR(cctx, 0);
3137 }
3138
3139 if (generate_instr(cctx, ISN_RETURN) == NULL)
3140 return NULL;
3141
3142 // "return val | endif" is possible
3143 return skipwhite(p);
3144}
3145
3146/*
3147 * Return the length of an assignment operator, or zero if there isn't one.
3148 */
3149 int
3150assignment_len(char_u *p, int *heredoc)
3151{
3152 if (*p == '=')
3153 {
3154 if (p[1] == '<' && p[2] == '<')
3155 {
3156 *heredoc = TRUE;
3157 return 3;
3158 }
3159 return 1;
3160 }
3161 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3162 return 2;
3163 if (STRNCMP(p, "..=", 3) == 0)
3164 return 3;
3165 return 0;
3166}
3167
3168// words that cannot be used as a variable
3169static char *reserved[] = {
3170 "true",
3171 "false",
3172 NULL
3173};
3174
3175/*
3176 * Get a line for "=<<".
3177 * Return a pointer to the line in allocated memory.
3178 * Return NULL for end-of-file or some error.
3179 */
3180 static char_u *
3181heredoc_getline(
3182 int c UNUSED,
3183 void *cookie,
3184 int indent UNUSED,
3185 int do_concat UNUSED)
3186{
3187 cctx_T *cctx = (cctx_T *)cookie;
3188
3189 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3190 NULL;
3191 ++cctx->ctx_lnum;
3192 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3193 [cctx->ctx_lnum]);
3194}
3195
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003196typedef enum {
3197 dest_local,
3198 dest_option,
3199 dest_env,
3200 dest_global,
3201 dest_vimvar,
3202 dest_script,
3203 dest_reg,
3204} assign_dest_T;
3205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206/*
3207 * compile "let var [= expr]", "const var = expr" and "var = expr"
3208 * "arg" points to "var".
3209 */
3210 static char_u *
3211compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3212{
3213 char_u *p;
3214 char_u *ret = NULL;
3215 int var_count = 0;
3216 int semicolon = 0;
3217 size_t varlen;
3218 garray_T *instr = &cctx->ctx_instr;
3219 int idx = -1;
3220 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003222 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003224 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 int oplen = 0;
3226 int heredoc = FALSE;
3227 type_T *type;
3228 lvar_T *lvar;
3229 char_u *name;
3230 char_u *sp;
3231 int has_type = FALSE;
3232 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3233 int instr_count = -1;
3234
3235 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3236 if (p == NULL)
3237 return NULL;
3238 if (var_count > 0)
3239 {
3240 // TODO: let [var, var] = list
3241 emsg("Cannot handle a list yet");
3242 return NULL;
3243 }
3244
3245 varlen = p - arg;
3246 name = vim_strnsave(arg, (int)varlen);
3247 if (name == NULL)
3248 return NULL;
3249
3250 if (*arg == '&')
3251 {
3252 int cc;
3253 long numval;
3254 char_u *stringval = NULL;
3255
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003256 dest = dest_option;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 if (cmdidx == CMD_const)
3258 {
3259 emsg(_(e_const_option));
3260 return NULL;
3261 }
3262 if (is_decl)
3263 {
3264 semsg(_("E1052: Cannot declare an option: %s"), arg);
3265 goto theend;
3266 }
3267 p = arg;
3268 p = find_option_end(&p, &opt_flags);
3269 if (p == NULL)
3270 {
3271 emsg(_(e_letunexp));
3272 return NULL;
3273 }
3274 cc = *p;
3275 *p = NUL;
3276 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3277 *p = cc;
3278 if (opt_type == -3)
3279 {
3280 semsg(_(e_unknown_option), *arg);
3281 return NULL;
3282 }
3283 if (opt_type == -2 || opt_type == 0)
3284 type = &t_string;
3285 else
3286 type = &t_number; // both number and boolean option
3287 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003288 else if (*arg == '$')
3289 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003290 dest = dest_env;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003291 if (is_decl)
3292 {
3293 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3294 goto theend;
3295 }
3296 }
3297 else if (*arg == '@')
3298 {
3299 if (!valid_yank_reg(arg[1], TRUE))
3300 {
3301 emsg_invreg(arg[1]);
3302 return FAIL;
3303 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003304 dest = dest_reg;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003305 if (is_decl)
3306 {
3307 semsg(_("E1066: Cannot declare a register: %s"), name);
3308 goto theend;
3309 }
3310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 else if (STRNCMP(arg, "g:", 2) == 0)
3312 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003313 dest = dest_global;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 if (is_decl)
3315 {
3316 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3317 goto theend;
3318 }
3319 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003320 else if (STRNCMP(arg, "v:", 2) == 0)
3321 {
3322 vimvaridx = find_vim_var(name + 2);
3323 if (vimvaridx < 0)
3324 {
3325 semsg(_(e_var_notfound), arg);
3326 goto theend;
3327 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003328 dest = dest_vimvar;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003329 if (is_decl)
3330 {
3331 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3332 goto theend;
3333 }
3334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 else
3336 {
3337 for (idx = 0; reserved[idx] != NULL; ++idx)
3338 if (STRCMP(reserved[idx], name) == 0)
3339 {
3340 semsg(_("E1034: Cannot use reserved name %s"), name);
3341 goto theend;
3342 }
3343
3344 idx = lookup_local(arg, varlen, cctx);
3345 if (idx >= 0)
3346 {
3347 if (is_decl)
3348 {
3349 semsg(_("E1017: Variable already declared: %s"), name);
3350 goto theend;
3351 }
3352 else
3353 {
3354 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3355 if (lvar->lv_const)
3356 {
3357 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3358 goto theend;
3359 }
3360 }
3361 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003362 else if (STRNCMP(arg, "s:", 2) == 0
3363 || lookup_script(arg, varlen) == OK
3364 || find_imported(arg, varlen, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003366 dest = dest_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 if (is_decl)
3368 {
3369 semsg(_("E1054: Variable already declared in the script: %s"),
3370 name);
3371 goto theend;
3372 }
3373 }
3374 }
3375
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003376 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 {
3378 if (is_decl && *p == ':')
3379 {
3380 // parse optional type: "let var: type = expr"
3381 p = skipwhite(p + 1);
3382 type = parse_type(&p, cctx->ctx_type_list);
3383 if (type == NULL)
3384 goto theend;
3385 has_type = TRUE;
3386 }
3387 else if (idx < 0)
3388 {
3389 // global and new local default to "any" type
3390 type = &t_any;
3391 }
3392 else
3393 {
3394 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3395 type = lvar->lv_type;
3396 }
3397 }
3398
3399 sp = p;
3400 p = skipwhite(p);
3401 op = p;
3402 oplen = assignment_len(p, &heredoc);
3403 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3404 {
3405 char_u buf[4];
3406
3407 vim_strncpy(buf, op, oplen);
3408 semsg(_(e_white_both), buf);
3409 }
3410
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003411 if (oplen == 3 && !heredoc && dest != dest_global
3412 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003414 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 goto theend;
3416 }
3417
3418 // +=, /=, etc. require an existing variable
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003419 if (idx < 0 && dest == dest_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 {
3421 if (oplen > 1 && !heredoc)
3422 {
3423 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3424 name);
3425 goto theend;
3426 }
3427
3428 // new local variable
3429 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3430 if (idx < 0)
3431 goto theend;
3432 }
3433
3434 if (heredoc)
3435 {
3436 list_T *l;
3437 listitem_T *li;
3438
3439 // [let] varname =<< [trim] {end}
3440 eap->getline = heredoc_getline;
3441 eap->cookie = cctx;
3442 l = heredoc_get(eap, op + 3);
3443
3444 // Push each line and the create the list.
3445 for (li = l->lv_first; li != NULL; li = li->li_next)
3446 {
3447 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3448 li->li_tv.vval.v_string = NULL;
3449 }
3450 generate_NEWLIST(cctx, l->lv_len);
3451 type = &t_list_string;
3452 list_free(l);
3453 p += STRLEN(p);
3454 }
3455 else if (oplen > 0)
3456 {
3457 // for "+=", "*=", "..=" etc. first load the current value
3458 if (*op != '=')
3459 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003460 switch (dest)
3461 {
3462 case dest_option:
3463 // TODO: check the option exists
3464 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3465 break;
3466 case dest_global:
3467 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3468 break;
3469 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003470 compile_load_scriptvar(cctx,
3471 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003472 break;
3473 case dest_env:
3474 // Include $ in the name here
3475 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3476 break;
3477 case dest_reg:
3478 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3479 break;
3480 case dest_vimvar:
3481 generate_LOADV(cctx, name + 2, TRUE);
3482 break;
3483 case dest_local:
3484 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3485 break;
3486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 }
3488
3489 // compile the expression
3490 instr_count = instr->ga_len;
3491 p = skipwhite(p + oplen);
3492 if (compile_expr1(&p, cctx) == FAIL)
3493 goto theend;
3494
3495 if (idx >= 0 && (is_decl || !has_type))
3496 {
3497 garray_T *stack = &cctx->ctx_type_stack;
3498 type_T *stacktype =
3499 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3500
3501 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3502 if (!has_type)
3503 {
3504 if (stacktype->tt_type == VAR_VOID)
3505 {
3506 emsg(_("E1031: Cannot use void value"));
3507 goto theend;
3508 }
3509 else
3510 lvar->lv_type = stacktype;
3511 }
3512 else
3513 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3514 goto theend;
3515 }
3516 }
3517 else if (cmdidx == CMD_const)
3518 {
3519 emsg(_("E1021: const requires a value"));
3520 goto theend;
3521 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003522 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 {
3524 emsg(_("E1022: type or initialization required"));
3525 goto theend;
3526 }
3527 else
3528 {
3529 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (ga_grow(instr, 1) == FAIL)
3531 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003532 switch (type->tt_type)
3533 {
3534 case VAR_BOOL:
3535 generate_PUSHBOOL(cctx, VVAL_FALSE);
3536 break;
3537 case VAR_SPECIAL:
3538 generate_PUSHSPEC(cctx, VVAL_NONE);
3539 break;
3540 case VAR_FLOAT:
3541#ifdef FEAT_FLOAT
3542 generate_PUSHF(cctx, 0.0);
3543#endif
3544 break;
3545 case VAR_STRING:
3546 generate_PUSHS(cctx, NULL);
3547 break;
3548 case VAR_BLOB:
3549 generate_PUSHBLOB(cctx, NULL);
3550 break;
3551 case VAR_FUNC:
3552 // generate_PUSHS(cctx, NULL); TODO
3553 break;
3554 case VAR_PARTIAL:
3555 // generate_PUSHS(cctx, NULL); TODO
3556 break;
3557 case VAR_LIST:
3558 generate_NEWLIST(cctx, 0);
3559 break;
3560 case VAR_DICT:
3561 generate_NEWDICT(cctx, 0);
3562 break;
3563 case VAR_JOB:
3564 // generate_PUSHS(cctx, NULL); TODO
3565 break;
3566 case VAR_CHANNEL:
3567 // generate_PUSHS(cctx, NULL); TODO
3568 break;
3569 case VAR_NUMBER:
3570 case VAR_UNKNOWN:
3571 case VAR_VOID:
3572 generate_PUSHNR(cctx, 0);
3573 break;
3574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576
3577 if (oplen > 0 && *op != '=')
3578 {
3579 type_T *expected = &t_number;
3580 garray_T *stack = &cctx->ctx_type_stack;
3581 type_T *stacktype;
3582
3583 // TODO: if type is known use float or any operation
3584
3585 if (*op == '.')
3586 expected = &t_string;
3587 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3588 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3589 goto theend;
3590
3591 if (*op == '.')
3592 generate_instr_drop(cctx, ISN_CONCAT, 1);
3593 else
3594 {
3595 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3596
3597 if (isn == NULL)
3598 goto theend;
3599 switch (*op)
3600 {
3601 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3602 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3603 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3604 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3605 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3606 }
3607 }
3608 }
3609
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003610 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003612 case dest_option:
3613 generate_STOREOPT(cctx, name + 1, opt_flags);
3614 break;
3615 case dest_global:
3616 // include g: with the name, easier to execute that way
3617 generate_STORE(cctx, ISN_STOREG, 0, name);
3618 break;
3619 case dest_env:
3620 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3621 break;
3622 case dest_reg:
3623 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3624 break;
3625 case dest_vimvar:
3626 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3627 break;
3628 case dest_script:
3629 {
3630 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3631 imported_T *import = NULL;
3632 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003633
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003634 if (name[1] != ':')
3635 {
3636 import = find_imported(name, 0, cctx);
3637 if (import != NULL)
3638 sid = import->imp_sid;
3639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003641 idx = get_script_item_idx(sid, rawname, TRUE);
3642 // TODO: specific type
3643 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003644 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003645 else
3646 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3647 sid, idx, &t_any);
3648 }
3649 break;
3650 case dest_local:
3651 {
3652 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003654 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3655 // into ISN_STORENR
3656 if (instr->ga_len == instr_count + 1
3657 && isn->isn_type == ISN_PUSHNR)
3658 {
3659 varnumber_T val = isn->isn_arg.number;
3660 garray_T *stack = &cctx->ctx_type_stack;
3661
3662 isn->isn_type = ISN_STORENR;
3663 isn->isn_arg.storenr.str_idx = idx;
3664 isn->isn_arg.storenr.str_val = val;
3665 if (stack->ga_len > 0)
3666 --stack->ga_len;
3667 }
3668 else
3669 generate_STORE(cctx, ISN_STORE, idx, NULL);
3670 }
3671 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 ret = p;
3674
3675theend:
3676 vim_free(name);
3677 return ret;
3678}
3679
3680/*
3681 * Compile an :import command.
3682 */
3683 static char_u *
3684compile_import(char_u *arg, cctx_T *cctx)
3685{
3686 return handle_import(arg, &cctx->ctx_imports, 0);
3687}
3688
3689/*
3690 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3691 */
3692 static int
3693compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3694{
3695 garray_T *instr = &cctx->ctx_instr;
3696 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3697
3698 if (endlabel == NULL)
3699 return FAIL;
3700 endlabel->el_next = *el;
3701 *el = endlabel;
3702 endlabel->el_end_label = instr->ga_len;
3703
3704 generate_JUMP(cctx, when, 0);
3705 return OK;
3706}
3707
3708 static void
3709compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3710{
3711 garray_T *instr = &cctx->ctx_instr;
3712
3713 while (*el != NULL)
3714 {
3715 endlabel_T *cur = (*el);
3716 isn_T *isn;
3717
3718 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3719 isn->isn_arg.jump.jump_where = instr->ga_len;
3720 *el = cur->el_next;
3721 vim_free(cur);
3722 }
3723}
3724
3725/*
3726 * Create a new scope and set up the generic items.
3727 */
3728 static scope_T *
3729new_scope(cctx_T *cctx, scopetype_T type)
3730{
3731 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3732
3733 if (scope == NULL)
3734 return NULL;
3735 scope->se_outer = cctx->ctx_scope;
3736 cctx->ctx_scope = scope;
3737 scope->se_type = type;
3738 scope->se_local_count = cctx->ctx_locals.ga_len;
3739 return scope;
3740}
3741
3742/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003743 * Evaluate an expression that is a constant:
3744 * has(arg)
3745 *
3746 * Also handle:
3747 * ! in front logical NOT
3748 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003749 * Return FAIL if the expression is not a constant.
3750 */
3751 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003752evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003753{
3754 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003755 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003756
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003757 /*
3758 * Skip '!' characters. They are handled later.
3759 */
3760 start_leader = *arg;
3761 while (**arg == '!')
3762 *arg = skipwhite(*arg + 1);
3763 end_leader = *arg;
3764
3765 /*
3766 * Recognize only has() for now.
3767 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003768 if (STRNCMP("has(", *arg, 4) != 0)
3769 return FAIL;
3770 *arg = skipwhite(*arg + 4);
3771
3772 if (**arg == '"')
3773 {
3774 if (get_string_tv(arg, tv, TRUE) == FAIL)
3775 return FAIL;
3776 }
3777 else if (**arg == '\'')
3778 {
3779 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3780 return FAIL;
3781 }
3782 else
3783 return FAIL;
3784
3785 *arg = skipwhite(*arg);
3786 if (**arg != ')')
3787 return FAIL;
3788 *arg = skipwhite(*arg + 1);
3789
3790 argvars[0] = *tv;
3791 argvars[1].v_type = VAR_UNKNOWN;
3792 tv->v_type = VAR_NUMBER;
3793 tv->vval.v_number = 0;
3794 f_has(argvars, tv);
3795 clear_tv(&argvars[0]);
3796
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003797 while (start_leader < end_leader)
3798 {
3799 if (*start_leader == '!')
3800 tv->vval.v_number = !tv->vval.v_number;
3801 ++start_leader;
3802 }
3803
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003804 return OK;
3805}
3806
3807static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3808
3809/*
3810 * Compile constant || or &&.
3811 */
3812 static int
3813evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3814{
3815 char_u *p = skipwhite(*arg);
3816 int opchar = *op;
3817
3818 if (p[0] == opchar && p[1] == opchar)
3819 {
3820 int val = tv2bool(tv);
3821
3822 /*
3823 * Repeat until there is no following "||" or "&&"
3824 */
3825 while (p[0] == opchar && p[1] == opchar)
3826 {
3827 typval_T tv2;
3828
3829 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3830 return FAIL;
3831
3832 // eval the next expression
3833 *arg = skipwhite(p + 2);
3834 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01003835 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003836 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003837 : evaluate_const_expr7(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003838 {
3839 clear_tv(&tv2);
3840 return FAIL;
3841 }
3842 if ((opchar == '&') == val)
3843 {
3844 // false || tv2 or true && tv2: use tv2
3845 clear_tv(tv);
3846 *tv = tv2;
3847 val = tv2bool(tv);
3848 }
3849 else
3850 clear_tv(&tv2);
3851 p = skipwhite(*arg);
3852 }
3853 }
3854
3855 return OK;
3856}
3857
3858/*
3859 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3860 * Return FAIL if the expression is not a constant.
3861 */
3862 static int
3863evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3864{
3865 // evaluate the first expression
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003866 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003867 return FAIL;
3868
3869 // || and && work almost the same
3870 return evaluate_const_and_or(arg, cctx, "&&", tv);
3871}
3872
3873/*
3874 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3875 * Return FAIL if the expression is not a constant.
3876 */
3877 static int
3878evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3879{
3880 // evaluate the first expression
3881 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3882 return FAIL;
3883
3884 // || and && work almost the same
3885 return evaluate_const_and_or(arg, cctx, "||", tv);
3886}
3887
3888/*
3889 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3890 * E.g. for "has('feature')".
3891 * This does not produce error messages. "tv" should be cleared afterwards.
3892 * Return FAIL if the expression is not a constant.
3893 */
3894 static int
3895evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3896{
3897 char_u *p;
3898
3899 // evaluate the first expression
3900 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3901 return FAIL;
3902
3903 p = skipwhite(*arg);
3904 if (*p == '?')
3905 {
3906 int val = tv2bool(tv);
3907 typval_T tv2;
3908
3909 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3910 return FAIL;
3911
3912 // evaluate the second expression; any type is accepted
3913 clear_tv(tv);
3914 *arg = skipwhite(p + 1);
3915 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3916 return FAIL;
3917
3918 // Check for the ":".
3919 p = skipwhite(*arg);
3920 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3921 return FAIL;
3922
3923 // evaluate the third expression
3924 *arg = skipwhite(p + 1);
3925 tv2.v_type = VAR_UNKNOWN;
3926 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3927 {
3928 clear_tv(&tv2);
3929 return FAIL;
3930 }
3931 if (val)
3932 {
3933 // use the expr after "?"
3934 clear_tv(&tv2);
3935 }
3936 else
3937 {
3938 // use the expr after ":"
3939 clear_tv(tv);
3940 *tv = tv2;
3941 }
3942 }
3943 return OK;
3944}
3945
3946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 * compile "if expr"
3948 *
3949 * "if expr" Produces instructions:
3950 * EVAL expr Push result of "expr"
3951 * JUMP_IF_FALSE end
3952 * ... body ...
3953 * end:
3954 *
3955 * "if expr | else" Produces instructions:
3956 * EVAL expr Push result of "expr"
3957 * JUMP_IF_FALSE else
3958 * ... body ...
3959 * JUMP_ALWAYS end
3960 * else:
3961 * ... body ...
3962 * end:
3963 *
3964 * "if expr1 | elseif expr2 | else" Produces instructions:
3965 * EVAL expr Push result of "expr"
3966 * JUMP_IF_FALSE elseif
3967 * ... body ...
3968 * JUMP_ALWAYS end
3969 * elseif:
3970 * EVAL expr Push result of "expr"
3971 * JUMP_IF_FALSE else
3972 * ... body ...
3973 * JUMP_ALWAYS end
3974 * else:
3975 * ... body ...
3976 * end:
3977 */
3978 static char_u *
3979compile_if(char_u *arg, cctx_T *cctx)
3980{
3981 char_u *p = arg;
3982 garray_T *instr = &cctx->ctx_instr;
3983 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003984 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003986 // compile "expr"; if we know it evaluates to FALSE skip the block
3987 tv.v_type = VAR_UNKNOWN;
3988 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3989 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3990 else
3991 cctx->ctx_skip = MAYBE;
3992 clear_tv(&tv);
3993 if (cctx->ctx_skip == MAYBE)
3994 {
3995 p = arg;
3996 if (compile_expr1(&p, cctx) == FAIL)
3997 return NULL;
3998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000 scope = new_scope(cctx, IF_SCOPE);
4001 if (scope == NULL)
4002 return NULL;
4003
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004004 if (cctx->ctx_skip == MAYBE)
4005 {
4006 // "where" is set when ":elseif", "else" or ":endif" is found
4007 scope->se_u.se_if.is_if_label = instr->ga_len;
4008 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4009 }
4010 else
4011 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012
4013 return p;
4014}
4015
4016 static char_u *
4017compile_elseif(char_u *arg, cctx_T *cctx)
4018{
4019 char_u *p = arg;
4020 garray_T *instr = &cctx->ctx_instr;
4021 isn_T *isn;
4022 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004023 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
4025 if (scope == NULL || scope->se_type != IF_SCOPE)
4026 {
4027 emsg(_(e_elseif_without_if));
4028 return NULL;
4029 }
4030 cctx->ctx_locals.ga_len = scope->se_local_count;
4031
Bram Moolenaar158906c2020-02-06 20:39:45 +01004032 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004033 {
4034 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004036 return NULL;
4037 // previous "if" or "elseif" jumps here
4038 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4039 isn->isn_arg.jump.jump_where = instr->ga_len;
4040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004042 // compile "expr"; if we know it evaluates to FALSE skip the block
4043 tv.v_type = VAR_UNKNOWN;
4044 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4045 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4046 else
4047 cctx->ctx_skip = MAYBE;
4048 clear_tv(&tv);
4049 if (cctx->ctx_skip == MAYBE)
4050 {
4051 p = arg;
4052 if (compile_expr1(&p, cctx) == FAIL)
4053 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004055 // "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_else(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;
4072
4073 if (scope == NULL || scope->se_type != IF_SCOPE)
4074 {
4075 emsg(_(e_else_without_if));
4076 return NULL;
4077 }
4078 cctx->ctx_locals.ga_len = scope->se_local_count;
4079
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004080 // jump from previous block to the end, unless the else block is empty
4081 if (cctx->ctx_skip == MAYBE)
4082 {
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 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaar158906c2020-02-06 20:39:45 +01004088 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004089 {
4090 if (scope->se_u.se_if.is_if_label >= 0)
4091 {
4092 // previous "if" or "elseif" jumps here
4093 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4094 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004095 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004096 }
4097 }
4098
4099 if (cctx->ctx_skip != MAYBE)
4100 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
4102 return p;
4103}
4104
4105 static char_u *
4106compile_endif(char_u *arg, cctx_T *cctx)
4107{
4108 scope_T *scope = cctx->ctx_scope;
4109 ifscope_T *ifscope;
4110 garray_T *instr = &cctx->ctx_instr;
4111 isn_T *isn;
4112
4113 if (scope == NULL || scope->se_type != IF_SCOPE)
4114 {
4115 emsg(_(e_endif_without_if));
4116 return NULL;
4117 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004118 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 cctx->ctx_scope = scope->se_outer;
4120 cctx->ctx_locals.ga_len = scope->se_local_count;
4121
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004122 if (scope->se_u.se_if.is_if_label >= 0)
4123 {
4124 // previous "if" or "elseif" jumps here
4125 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4126 isn->isn_arg.jump.jump_where = instr->ga_len;
4127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 // Fill in the "end" label in jumps at the end of the blocks.
4129 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004130 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131
4132 vim_free(scope);
4133 return arg;
4134}
4135
4136/*
4137 * compile "for var in expr"
4138 *
4139 * Produces instructions:
4140 * PUSHNR -1
4141 * STORE loop-idx Set index to -1
4142 * EVAL expr Push result of "expr"
4143 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4144 * - if beyond end, jump to "end"
4145 * - otherwise get item from list and push it
4146 * STORE var Store item in "var"
4147 * ... body ...
4148 * JUMP top Jump back to repeat
4149 * end: DROP Drop the result of "expr"
4150 *
4151 */
4152 static char_u *
4153compile_for(char_u *arg, cctx_T *cctx)
4154{
4155 char_u *p;
4156 size_t varlen;
4157 garray_T *instr = &cctx->ctx_instr;
4158 garray_T *stack = &cctx->ctx_type_stack;
4159 scope_T *scope;
4160 int loop_idx; // index of loop iteration variable
4161 int var_idx; // index of "var"
4162 type_T *vartype;
4163
4164 // TODO: list of variables: "for [key, value] in dict"
4165 // parse "var"
4166 for (p = arg; eval_isnamec1(*p); ++p)
4167 ;
4168 varlen = p - arg;
4169 var_idx = lookup_local(arg, varlen, cctx);
4170 if (var_idx >= 0)
4171 {
4172 semsg(_("E1023: variable already defined: %s"), arg);
4173 return NULL;
4174 }
4175
4176 // consume "in"
4177 p = skipwhite(p);
4178 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4179 {
4180 emsg(_(e_missing_in));
4181 return NULL;
4182 }
4183 p = skipwhite(p + 2);
4184
4185
4186 scope = new_scope(cctx, FOR_SCOPE);
4187 if (scope == NULL)
4188 return NULL;
4189
4190 // Reserve a variable to store the loop iteration counter.
4191 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4192 if (loop_idx < 0)
4193 return NULL;
4194
4195 // Reserve a variable to store "var"
4196 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4197 if (var_idx < 0)
4198 return NULL;
4199
4200 generate_STORENR(cctx, loop_idx, -1);
4201
4202 // compile "expr", it remains on the stack until "endfor"
4203 arg = p;
4204 if (compile_expr1(&arg, cctx) == FAIL)
4205 return NULL;
4206
4207 // now we know the type of "var"
4208 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4209 if (vartype->tt_type != VAR_LIST)
4210 {
4211 emsg(_("E1024: need a List to iterate over"));
4212 return NULL;
4213 }
4214 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4215 {
4216 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4217
4218 lvar->lv_type = vartype->tt_member;
4219 }
4220
4221 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004222 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223
4224 generate_FOR(cctx, loop_idx);
4225 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4226
4227 return arg;
4228}
4229
4230/*
4231 * compile "endfor"
4232 */
4233 static char_u *
4234compile_endfor(char_u *arg, cctx_T *cctx)
4235{
4236 garray_T *instr = &cctx->ctx_instr;
4237 scope_T *scope = cctx->ctx_scope;
4238 forscope_T *forscope;
4239 isn_T *isn;
4240
4241 if (scope == NULL || scope->se_type != FOR_SCOPE)
4242 {
4243 emsg(_(e_for));
4244 return NULL;
4245 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004246 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 cctx->ctx_scope = scope->se_outer;
4248 cctx->ctx_locals.ga_len = scope->se_local_count;
4249
4250 // At end of ":for" scope jump back to the FOR instruction.
4251 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4252
4253 // Fill in the "end" label in the FOR statement so it can jump here
4254 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4255 isn->isn_arg.forloop.for_end = instr->ga_len;
4256
4257 // Fill in the "end" label any BREAK statements
4258 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4259
4260 // Below the ":for" scope drop the "expr" list from the stack.
4261 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4262 return NULL;
4263
4264 vim_free(scope);
4265
4266 return arg;
4267}
4268
4269/*
4270 * compile "while expr"
4271 *
4272 * Produces instructions:
4273 * top: EVAL expr Push result of "expr"
4274 * JUMP_IF_FALSE end jump if false
4275 * ... body ...
4276 * JUMP top Jump back to repeat
4277 * end:
4278 *
4279 */
4280 static char_u *
4281compile_while(char_u *arg, cctx_T *cctx)
4282{
4283 char_u *p = arg;
4284 garray_T *instr = &cctx->ctx_instr;
4285 scope_T *scope;
4286
4287 scope = new_scope(cctx, WHILE_SCOPE);
4288 if (scope == NULL)
4289 return NULL;
4290
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004291 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
4293 // compile "expr"
4294 if (compile_expr1(&p, cctx) == FAIL)
4295 return NULL;
4296
4297 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004298 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 JUMP_IF_FALSE, cctx) == FAIL)
4300 return FAIL;
4301
4302 return p;
4303}
4304
4305/*
4306 * compile "endwhile"
4307 */
4308 static char_u *
4309compile_endwhile(char_u *arg, cctx_T *cctx)
4310{
4311 scope_T *scope = cctx->ctx_scope;
4312
4313 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4314 {
4315 emsg(_(e_while));
4316 return NULL;
4317 }
4318 cctx->ctx_scope = scope->se_outer;
4319 cctx->ctx_locals.ga_len = scope->se_local_count;
4320
4321 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004322 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323
4324 // Fill in the "end" label in the WHILE statement so it can jump here.
4325 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004326 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327
4328 vim_free(scope);
4329
4330 return arg;
4331}
4332
4333/*
4334 * compile "continue"
4335 */
4336 static char_u *
4337compile_continue(char_u *arg, cctx_T *cctx)
4338{
4339 scope_T *scope = cctx->ctx_scope;
4340
4341 for (;;)
4342 {
4343 if (scope == NULL)
4344 {
4345 emsg(_(e_continue));
4346 return NULL;
4347 }
4348 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4349 break;
4350 scope = scope->se_outer;
4351 }
4352
4353 // Jump back to the FOR or WHILE instruction.
4354 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004355 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4356 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 return arg;
4358}
4359
4360/*
4361 * compile "break"
4362 */
4363 static char_u *
4364compile_break(char_u *arg, cctx_T *cctx)
4365{
4366 scope_T *scope = cctx->ctx_scope;
4367 endlabel_T **el;
4368
4369 for (;;)
4370 {
4371 if (scope == NULL)
4372 {
4373 emsg(_(e_break));
4374 return NULL;
4375 }
4376 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4377 break;
4378 scope = scope->se_outer;
4379 }
4380
4381 // Jump to the end of the FOR or WHILE loop.
4382 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004383 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004385 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4387 return FAIL;
4388
4389 return arg;
4390}
4391
4392/*
4393 * compile "{" start of block
4394 */
4395 static char_u *
4396compile_block(char_u *arg, cctx_T *cctx)
4397{
4398 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4399 return NULL;
4400 return skipwhite(arg + 1);
4401}
4402
4403/*
4404 * compile end of block: drop one scope
4405 */
4406 static void
4407compile_endblock(cctx_T *cctx)
4408{
4409 scope_T *scope = cctx->ctx_scope;
4410
4411 cctx->ctx_scope = scope->se_outer;
4412 cctx->ctx_locals.ga_len = scope->se_local_count;
4413 vim_free(scope);
4414}
4415
4416/*
4417 * compile "try"
4418 * Creates a new scope for the try-endtry, pointing to the first catch and
4419 * finally.
4420 * Creates another scope for the "try" block itself.
4421 * TRY instruction sets up exception handling at runtime.
4422 *
4423 * "try"
4424 * TRY -> catch1, -> finally push trystack entry
4425 * ... try block
4426 * "throw {exception}"
4427 * EVAL {exception}
4428 * THROW create exception
4429 * ... try block
4430 * " catch {expr}"
4431 * JUMP -> finally
4432 * catch1: PUSH exeception
4433 * EVAL {expr}
4434 * MATCH
4435 * JUMP nomatch -> catch2
4436 * CATCH remove exception
4437 * ... catch block
4438 * " catch"
4439 * JUMP -> finally
4440 * catch2: CATCH remove exception
4441 * ... catch block
4442 * " finally"
4443 * finally:
4444 * ... finally block
4445 * " endtry"
4446 * ENDTRY pop trystack entry, may rethrow
4447 */
4448 static char_u *
4449compile_try(char_u *arg, cctx_T *cctx)
4450{
4451 garray_T *instr = &cctx->ctx_instr;
4452 scope_T *try_scope;
4453 scope_T *scope;
4454
4455 // scope that holds the jumps that go to catch/finally/endtry
4456 try_scope = new_scope(cctx, TRY_SCOPE);
4457 if (try_scope == NULL)
4458 return NULL;
4459
4460 // "catch" is set when the first ":catch" is found.
4461 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004462 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 if (generate_instr(cctx, ISN_TRY) == NULL)
4464 return NULL;
4465
4466 // scope for the try block itself
4467 scope = new_scope(cctx, BLOCK_SCOPE);
4468 if (scope == NULL)
4469 return NULL;
4470
4471 return arg;
4472}
4473
4474/*
4475 * compile "catch {expr}"
4476 */
4477 static char_u *
4478compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4479{
4480 scope_T *scope = cctx->ctx_scope;
4481 garray_T *instr = &cctx->ctx_instr;
4482 char_u *p;
4483 isn_T *isn;
4484
4485 // end block scope from :try or :catch
4486 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4487 compile_endblock(cctx);
4488 scope = cctx->ctx_scope;
4489
4490 // Error if not in a :try scope
4491 if (scope == NULL || scope->se_type != TRY_SCOPE)
4492 {
4493 emsg(_(e_catch));
4494 return NULL;
4495 }
4496
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004497 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 {
4499 emsg(_("E1033: catch unreachable after catch-all"));
4500 return NULL;
4501 }
4502
4503 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004504 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 JUMP_ALWAYS, cctx) == FAIL)
4506 return NULL;
4507
4508 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004509 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 if (isn->isn_arg.try.try_catch == 0)
4511 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004512 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 {
4514 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004515 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 isn->isn_arg.jump.jump_where = instr->ga_len;
4517 }
4518
4519 p = skipwhite(arg);
4520 if (ends_excmd(*p))
4521 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004522 scope->se_u.se_try.ts_caught_all = TRUE;
4523 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 }
4525 else
4526 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004527 char_u *end;
4528 char_u *pat;
4529 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004530 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 // Push v:exception, push {expr} and MATCH
4533 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4534
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004535 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4536 if (*end != *p)
4537 {
4538 semsg(_("E1067: Separator mismatch: %s"), p);
4539 vim_free(tofree);
4540 return FAIL;
4541 }
4542 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004543 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004544 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004545 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004546 pat = vim_strnsave(p + 1, len);
4547 vim_free(tofree);
4548 p += len + 2;
4549 if (pat == NULL)
4550 return FAIL;
4551 if (generate_PUSHS(cctx, pat) == FAIL)
4552 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4555 return NULL;
4556
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004557 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4559 return NULL;
4560 }
4561
4562 if (generate_instr(cctx, ISN_CATCH) == NULL)
4563 return NULL;
4564
4565 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4566 return NULL;
4567 return p;
4568}
4569
4570 static char_u *
4571compile_finally(char_u *arg, cctx_T *cctx)
4572{
4573 scope_T *scope = cctx->ctx_scope;
4574 garray_T *instr = &cctx->ctx_instr;
4575 isn_T *isn;
4576
4577 // end block scope from :try or :catch
4578 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4579 compile_endblock(cctx);
4580 scope = cctx->ctx_scope;
4581
4582 // Error if not in a :try scope
4583 if (scope == NULL || scope->se_type != TRY_SCOPE)
4584 {
4585 emsg(_(e_finally));
4586 return NULL;
4587 }
4588
4589 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 if (isn->isn_arg.try.try_finally != 0)
4592 {
4593 emsg(_(e_finally_dup));
4594 return NULL;
4595 }
4596
4597 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004598 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004600 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 {
4602 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004603 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 isn->isn_arg.jump.jump_where = instr->ga_len;
4605 }
4606
4607 isn->isn_arg.try.try_finally = instr->ga_len;
4608 // TODO: set index in ts_finally_label jumps
4609
4610 return arg;
4611}
4612
4613 static char_u *
4614compile_endtry(char_u *arg, cctx_T *cctx)
4615{
4616 scope_T *scope = cctx->ctx_scope;
4617 garray_T *instr = &cctx->ctx_instr;
4618 isn_T *isn;
4619
4620 // end block scope from :catch or :finally
4621 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4622 compile_endblock(cctx);
4623 scope = cctx->ctx_scope;
4624
4625 // Error if not in a :try scope
4626 if (scope == NULL || scope->se_type != TRY_SCOPE)
4627 {
4628 if (scope == NULL)
4629 emsg(_(e_no_endtry));
4630 else if (scope->se_type == WHILE_SCOPE)
4631 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004632 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 emsg(_(e_endfor));
4634 else
4635 emsg(_(e_endif));
4636 return NULL;
4637 }
4638
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_catch == 0 && isn->isn_arg.try.try_finally == 0)
4641 {
4642 emsg(_("E1032: missing :catch or :finally"));
4643 return NULL;
4644 }
4645
4646 // Fill in the "end" label in jumps at the end of the blocks, if not done
4647 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004648 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
4650 // End :catch or :finally scope: set value in ISN_TRY instruction
4651 if (isn->isn_arg.try.try_finally == 0)
4652 isn->isn_arg.try.try_finally = instr->ga_len;
4653 compile_endblock(cctx);
4654
4655 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4656 return NULL;
4657 return arg;
4658}
4659
4660/*
4661 * compile "throw {expr}"
4662 */
4663 static char_u *
4664compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4665{
4666 char_u *p = skipwhite(arg);
4667
4668 if (ends_excmd(*p))
4669 {
4670 emsg(_(e_argreq));
4671 return NULL;
4672 }
4673 if (compile_expr1(&p, cctx) == FAIL)
4674 return NULL;
4675 if (may_generate_2STRING(-1, cctx) == FAIL)
4676 return NULL;
4677 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4678 return NULL;
4679
4680 return p;
4681}
4682
4683/*
4684 * compile "echo expr"
4685 */
4686 static char_u *
4687compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4688{
4689 char_u *p = arg;
4690 int count = 0;
4691
Bram Moolenaarad39c092020-02-26 18:23:43 +01004692 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 {
4694 if (compile_expr1(&p, cctx) == FAIL)
4695 return NULL;
4696 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004697 p = skipwhite(p);
4698 if (ends_excmd(*p))
4699 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 }
4701
4702 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004703 return p;
4704}
4705
4706/*
4707 * compile "execute expr"
4708 */
4709 static char_u *
4710compile_execute(char_u *arg, cctx_T *cctx)
4711{
4712 char_u *p = arg;
4713 int count = 0;
4714
4715 for (;;)
4716 {
4717 if (compile_expr1(&p, cctx) == FAIL)
4718 return NULL;
4719 ++count;
4720 p = skipwhite(p);
4721 if (ends_excmd(*p))
4722 break;
4723 }
4724
4725 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726
4727 return p;
4728}
4729
4730/*
4731 * After ex_function() has collected all the function lines: parse and compile
4732 * the lines into instructions.
4733 * Adds the function to "def_functions".
4734 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4735 * return statement (used for lambda).
4736 */
4737 void
4738compile_def_function(ufunc_T *ufunc, int set_return_type)
4739{
4740 dfunc_T *dfunc;
4741 char_u *line = NULL;
4742 char_u *p;
4743 exarg_T ea;
4744 char *errormsg = NULL; // error message
4745 int had_return = FALSE;
4746 cctx_T cctx;
4747 garray_T *instr;
4748 int called_emsg_before = called_emsg;
4749 int ret = FAIL;
4750 sctx_T save_current_sctx = current_sctx;
4751
4752 if (ufunc->uf_dfunc_idx >= 0)
4753 {
4754 // redefining a function that was compiled before
4755 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4756 dfunc->df_deleted = FALSE;
4757 }
4758 else
4759 {
4760 // Add the function to "def_functions".
4761 if (ga_grow(&def_functions, 1) == FAIL)
4762 return;
4763 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4764 vim_memset(dfunc, 0, sizeof(dfunc_T));
4765 dfunc->df_idx = def_functions.ga_len;
4766 ufunc->uf_dfunc_idx = dfunc->df_idx;
4767 dfunc->df_ufunc = ufunc;
4768 ++def_functions.ga_len;
4769 }
4770
4771 vim_memset(&cctx, 0, sizeof(cctx));
4772 cctx.ctx_ufunc = ufunc;
4773 cctx.ctx_lnum = -1;
4774 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4775 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4776 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4777 cctx.ctx_type_list = &ufunc->uf_type_list;
4778 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4779 instr = &cctx.ctx_instr;
4780
4781 // Most modern script version.
4782 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4783
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004784 if (ufunc->uf_def_args.ga_len > 0)
4785 {
4786 int count = ufunc->uf_def_args.ga_len;
4787 int i;
4788 char_u *arg;
4789 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4790
4791 // Produce instructions for the default values of optional arguments.
4792 // Store the instruction index in uf_def_arg_idx[] so that we know
4793 // where to start when the function is called, depending on the number
4794 // of arguments.
4795 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4796 if (ufunc->uf_def_arg_idx == NULL)
4797 goto erret;
4798 for (i = 0; i < count; ++i)
4799 {
4800 ufunc->uf_def_arg_idx[i] = instr->ga_len;
4801 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4802 if (compile_expr1(&arg, &cctx) == FAIL
4803 || generate_STORE(&cctx, ISN_STORE,
4804 i - count - off, NULL) == FAIL)
4805 goto erret;
4806 }
4807
4808 // If a varargs is following, push an empty list.
4809 if (ufunc->uf_va_name != NULL)
4810 {
4811 if (generate_NEWLIST(&cctx, 0) == FAIL
4812 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4813 goto erret;
4814 }
4815
4816 ufunc->uf_def_arg_idx[count] = instr->ga_len;
4817 }
4818
4819 /*
4820 * Loop over all the lines of the function and generate instructions.
4821 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 for (;;)
4823 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004824 int is_ex_command;
4825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 if (line != NULL && *line == '|')
4827 // the line continues after a '|'
4828 ++line;
4829 else if (line != NULL && *line != NUL)
4830 {
4831 semsg(_("E488: Trailing characters: %s"), line);
4832 goto erret;
4833 }
4834 else
4835 {
4836 do
4837 {
4838 ++cctx.ctx_lnum;
4839 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4840 break;
4841 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4842 } while (line == NULL);
4843 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4844 break;
4845 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4846 }
4847
4848 had_return = FALSE;
4849 vim_memset(&ea, 0, sizeof(ea));
4850 ea.cmdlinep = &line;
4851 ea.cmd = skipwhite(line);
4852
4853 // "}" ends a block scope
4854 if (*ea.cmd == '}')
4855 {
4856 scopetype_T stype = cctx.ctx_scope == NULL
4857 ? NO_SCOPE : cctx.ctx_scope->se_type;
4858
4859 if (stype == BLOCK_SCOPE)
4860 {
4861 compile_endblock(&cctx);
4862 line = ea.cmd;
4863 }
4864 else
4865 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004866 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 goto erret;
4868 }
4869 if (line != NULL)
4870 line = skipwhite(ea.cmd + 1);
4871 continue;
4872 }
4873
4874 // "{" starts a block scope
4875 if (*ea.cmd == '{')
4876 {
4877 line = compile_block(ea.cmd, &cctx);
4878 continue;
4879 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004880 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881
4882 /*
4883 * COMMAND MODIFIERS
4884 */
4885 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4886 {
4887 if (errormsg != NULL)
4888 goto erret;
4889 // empty line or comment
4890 line = (char_u *)"";
4891 continue;
4892 }
4893
4894 // Skip ":call" to get to the function name.
4895 if (checkforcmd(&ea.cmd, "call", 3))
4896 ea.cmd = skipwhite(ea.cmd);
4897
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004898 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004900 // Assuming the command starts with a variable or function name,
4901 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
4902 // val".
4903 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
4904 ? ea.cmd + 1 : ea.cmd;
4905 p = to_name_end(p);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01004906 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004908 int oplen;
4909 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004911 oplen = assignment_len(skipwhite(p), &heredoc);
4912 if (oplen > 0)
4913 {
4914 // Recognize an assignment if we recognize the variable
4915 // name:
4916 // "g:var = expr"
4917 // "var = expr" where "var" is a local var name.
4918 // "&opt = expr"
4919 // "$ENV = expr"
4920 // "@r = expr"
4921 if (*ea.cmd == '&'
4922 || *ea.cmd == '$'
4923 || *ea.cmd == '@'
4924 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4925 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4926 || lookup_script(ea.cmd, p - ea.cmd) == OK
4927 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
4928 {
4929 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4930 if (line == NULL)
4931 goto erret;
4932 continue;
4933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 }
4935 }
4936 }
4937
4938 /*
4939 * COMMAND after range
4940 */
4941 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004942 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
4943 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944
4945 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4946 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004947 if (cctx.ctx_skip == TRUE)
4948 {
4949 line += STRLEN(line);
4950 continue;
4951 }
4952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 // Expression or function call.
4954 if (ea.cmdidx == CMD_eval)
4955 {
4956 p = ea.cmd;
4957 if (compile_expr1(&p, &cctx) == FAIL)
4958 goto erret;
4959
4960 // drop the return value
4961 generate_instr_drop(&cctx, ISN_DROP, 1);
4962 line = p;
4963 continue;
4964 }
4965 if (ea.cmdidx == CMD_let)
4966 {
4967 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4968 if (line == NULL)
4969 goto erret;
4970 continue;
4971 }
4972 iemsg("Command from find_ex_command() not handled");
4973 goto erret;
4974 }
4975
4976 p = skipwhite(p);
4977
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004978 if (cctx.ctx_skip == TRUE
4979 && ea.cmdidx != CMD_elseif
4980 && ea.cmdidx != CMD_else
4981 && ea.cmdidx != CMD_endif)
4982 {
4983 line += STRLEN(line);
4984 continue;
4985 }
4986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 switch (ea.cmdidx)
4988 {
4989 case CMD_def:
4990 case CMD_function:
4991 // TODO: Nested function
4992 emsg("Nested function not implemented yet");
4993 goto erret;
4994
4995 case CMD_return:
4996 line = compile_return(p, set_return_type, &cctx);
4997 had_return = TRUE;
4998 break;
4999
5000 case CMD_let:
5001 case CMD_const:
5002 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5003 break;
5004
5005 case CMD_import:
5006 line = compile_import(p, &cctx);
5007 break;
5008
5009 case CMD_if:
5010 line = compile_if(p, &cctx);
5011 break;
5012 case CMD_elseif:
5013 line = compile_elseif(p, &cctx);
5014 break;
5015 case CMD_else:
5016 line = compile_else(p, &cctx);
5017 break;
5018 case CMD_endif:
5019 line = compile_endif(p, &cctx);
5020 break;
5021
5022 case CMD_while:
5023 line = compile_while(p, &cctx);
5024 break;
5025 case CMD_endwhile:
5026 line = compile_endwhile(p, &cctx);
5027 break;
5028
5029 case CMD_for:
5030 line = compile_for(p, &cctx);
5031 break;
5032 case CMD_endfor:
5033 line = compile_endfor(p, &cctx);
5034 break;
5035 case CMD_continue:
5036 line = compile_continue(p, &cctx);
5037 break;
5038 case CMD_break:
5039 line = compile_break(p, &cctx);
5040 break;
5041
5042 case CMD_try:
5043 line = compile_try(p, &cctx);
5044 break;
5045 case CMD_catch:
5046 line = compile_catch(p, &cctx);
5047 break;
5048 case CMD_finally:
5049 line = compile_finally(p, &cctx);
5050 break;
5051 case CMD_endtry:
5052 line = compile_endtry(p, &cctx);
5053 break;
5054 case CMD_throw:
5055 line = compile_throw(p, &cctx);
5056 break;
5057
5058 case CMD_echo:
5059 line = compile_echo(p, TRUE, &cctx);
5060 break;
5061 case CMD_echon:
5062 line = compile_echo(p, FALSE, &cctx);
5063 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005064 case CMD_execute:
5065 line = compile_execute(p, &cctx);
5066 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067
5068 default:
5069 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005070 // TODO:
5071 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005072 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 generate_EXEC(&cctx, line);
5074 line = (char_u *)"";
5075 break;
5076 }
5077 if (line == NULL)
5078 goto erret;
5079
5080 if (cctx.ctx_type_stack.ga_len < 0)
5081 {
5082 iemsg("Type stack underflow");
5083 goto erret;
5084 }
5085 }
5086
5087 if (cctx.ctx_scope != NULL)
5088 {
5089 if (cctx.ctx_scope->se_type == IF_SCOPE)
5090 emsg(_(e_endif));
5091 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5092 emsg(_(e_endwhile));
5093 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5094 emsg(_(e_endfor));
5095 else
5096 emsg(_("E1026: Missing }"));
5097 goto erret;
5098 }
5099
5100 if (!had_return)
5101 {
5102 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5103 {
5104 emsg(_("E1027: Missing return statement"));
5105 goto erret;
5106 }
5107
5108 // Return zero if there is no return at the end.
5109 generate_PUSHNR(&cctx, 0);
5110 generate_instr(&cctx, ISN_RETURN);
5111 }
5112
5113 dfunc->df_instr = instr->ga_data;
5114 dfunc->df_instr_count = instr->ga_len;
5115 dfunc->df_varcount = cctx.ctx_max_local;
5116
5117 ret = OK;
5118
5119erret:
5120 if (ret == FAIL)
5121 {
5122 ga_clear(instr);
5123 ufunc->uf_dfunc_idx = -1;
5124 --def_functions.ga_len;
5125 if (errormsg != NULL)
5126 emsg(errormsg);
5127 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005128 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129
5130 // don't execute this function body
5131 ufunc->uf_lines.ga_len = 0;
5132 }
5133
5134 current_sctx = save_current_sctx;
5135 ga_clear(&cctx.ctx_type_stack);
5136 ga_clear(&cctx.ctx_locals);
5137}
5138
5139/*
5140 * Delete an instruction, free what it contains.
5141 */
5142 static void
5143delete_instr(isn_T *isn)
5144{
5145 switch (isn->isn_type)
5146 {
5147 case ISN_EXEC:
5148 case ISN_LOADENV:
5149 case ISN_LOADG:
5150 case ISN_LOADOPT:
5151 case ISN_MEMBER:
5152 case ISN_PUSHEXC:
5153 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005154 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 case ISN_STOREG:
5156 vim_free(isn->isn_arg.string);
5157 break;
5158
5159 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005160 case ISN_STORES:
5161 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 break;
5163
5164 case ISN_STOREOPT:
5165 vim_free(isn->isn_arg.storeopt.so_name);
5166 break;
5167
5168 case ISN_PUSHBLOB: // push blob isn_arg.blob
5169 blob_unref(isn->isn_arg.blob);
5170 break;
5171
5172 case ISN_UCALL:
5173 vim_free(isn->isn_arg.ufunc.cuf_name);
5174 break;
5175
5176 case ISN_2BOOL:
5177 case ISN_2STRING:
5178 case ISN_ADDBLOB:
5179 case ISN_ADDLIST:
5180 case ISN_BCALL:
5181 case ISN_CATCH:
5182 case ISN_CHECKNR:
5183 case ISN_CHECKTYPE:
5184 case ISN_COMPAREANY:
5185 case ISN_COMPAREBLOB:
5186 case ISN_COMPAREBOOL:
5187 case ISN_COMPAREDICT:
5188 case ISN_COMPAREFLOAT:
5189 case ISN_COMPAREFUNC:
5190 case ISN_COMPARELIST:
5191 case ISN_COMPARENR:
5192 case ISN_COMPAREPARTIAL:
5193 case ISN_COMPARESPECIAL:
5194 case ISN_COMPARESTRING:
5195 case ISN_CONCAT:
5196 case ISN_DCALL:
5197 case ISN_DROP:
5198 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005199 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 case ISN_ENDTRY:
5201 case ISN_FOR:
5202 case ISN_FUNCREF:
5203 case ISN_INDEX:
5204 case ISN_JUMP:
5205 case ISN_LOAD:
5206 case ISN_LOADSCRIPT:
5207 case ISN_LOADREG:
5208 case ISN_LOADV:
5209 case ISN_NEGATENR:
5210 case ISN_NEWDICT:
5211 case ISN_NEWLIST:
5212 case ISN_OPNR:
5213 case ISN_OPFLOAT:
5214 case ISN_OPANY:
5215 case ISN_PCALL:
5216 case ISN_PUSHF:
5217 case ISN_PUSHNR:
5218 case ISN_PUSHBOOL:
5219 case ISN_PUSHSPEC:
5220 case ISN_RETURN:
5221 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005222 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005224 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 case ISN_STORESCRIPT:
5226 case ISN_THROW:
5227 case ISN_TRY:
5228 // nothing allocated
5229 break;
5230 }
5231}
5232
5233/*
5234 * When a user function is deleted, delete any associated def function.
5235 */
5236 void
5237delete_def_function(ufunc_T *ufunc)
5238{
5239 int idx;
5240
5241 if (ufunc->uf_dfunc_idx >= 0)
5242 {
5243 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5244 + ufunc->uf_dfunc_idx;
5245 ga_clear(&dfunc->df_def_args_isn);
5246
5247 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5248 delete_instr(dfunc->df_instr + idx);
5249 VIM_CLEAR(dfunc->df_instr);
5250
5251 dfunc->df_deleted = TRUE;
5252 }
5253}
5254
5255#if defined(EXITFREE) || defined(PROTO)
5256 void
5257free_def_functions(void)
5258{
5259 vim_free(def_functions.ga_data);
5260}
5261#endif
5262
5263
5264#endif // FEAT_EVAL