blob: 32d6349e517c9b780dff9192799bbce5ca462ded [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
1548 char_u **end) // end of variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001550 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1552 imported_T *import;
1553
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001554 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001556 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001557 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1558 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 }
1560 if (idx >= 0)
1561 {
1562 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1563
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001564 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 current_sctx.sc_sid, idx, sv->sv_type);
1566 return OK;
1567 }
1568
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001569 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if (import != NULL)
1571 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001572 if (import->imp_all)
1573 {
1574 char_u *p = skipwhite(*end);
1575 int name_len;
1576 ufunc_T *ufunc;
1577 type_T *type;
1578
1579 // Used "import * as Name", need to lookup the member.
1580 if (*p != '.')
1581 {
1582 semsg(_("E1060: expected dot after name: %s"), start);
1583 return FAIL;
1584 }
1585 ++p;
1586
1587 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1588 // TODO: what if it is a function?
1589 if (idx < 0)
1590 return FAIL;
1591 *end = p;
1592
1593 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1594 import->imp_sid,
1595 idx,
1596 type);
1597 }
1598 else
1599 {
1600 // TODO: check this is a variable, not a function
1601 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1602 import->imp_sid,
1603 import->imp_var_vals_idx,
1604 import->imp_type);
1605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 return OK;
1607 }
1608
1609 semsg(_("E1050: Item not found: %s"), name);
1610 return FAIL;
1611}
1612
1613/*
1614 * Compile a variable name into a load instruction.
1615 * "end" points to just after the name.
1616 * When "error" is FALSE do not give an error when not found.
1617 */
1618 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001619compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620{
1621 type_T *type;
1622 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001623 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 int res = FAIL;
1625
1626 if (*(*arg + 1) == ':')
1627 {
1628 // load namespaced variable
1629 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1630 if (name == NULL)
1631 return FAIL;
1632
1633 if (**arg == 'v')
1634 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001635 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 }
1637 else if (**arg == 'g')
1638 {
1639 // Global variables can be defined later, thus we don't check if it
1640 // exists, give error at runtime.
1641 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1642 }
1643 else if (**arg == 's')
1644 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001645 res = compile_load_scriptvar(cctx, name, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 }
1647 else
1648 {
1649 semsg("Namespace not supported yet: %s", **arg);
1650 goto theend;
1651 }
1652 }
1653 else
1654 {
1655 size_t len = end - *arg;
1656 int idx;
1657 int gen_load = FALSE;
1658
1659 name = vim_strnsave(*arg, end - *arg);
1660 if (name == NULL)
1661 return FAIL;
1662
1663 idx = lookup_arg(*arg, len, cctx);
1664 if (idx >= 0)
1665 {
1666 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1667 type = cctx->ctx_ufunc->uf_arg_types[idx];
1668 else
1669 type = &t_any;
1670
1671 // Arguments are located above the frame pointer.
1672 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1673 if (cctx->ctx_ufunc->uf_va_name != NULL)
1674 --idx;
1675 gen_load = TRUE;
1676 }
1677 else if (lookup_vararg(*arg, len, cctx))
1678 {
1679 // varargs is always the last argument
1680 idx = -STACK_FRAME_SIZE - 1;
1681 type = cctx->ctx_ufunc->uf_va_type;
1682 gen_load = TRUE;
1683 }
1684 else
1685 {
1686 idx = lookup_local(*arg, len, cctx);
1687 if (idx >= 0)
1688 {
1689 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1690 gen_load = TRUE;
1691 }
1692 else
1693 {
1694 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1695 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1696 res = generate_PUSHBOOL(cctx, **arg == 't'
1697 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001698 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1699 == SCRIPT_VERSION_VIM9)
1700 // in Vim9 script "var" can be script-local.
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001701 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 }
1703 }
1704 if (gen_load)
1705 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1706 }
1707
1708 *arg = end;
1709
1710theend:
1711 if (res == FAIL && error)
1712 semsg(_(e_var_notfound), name);
1713 vim_free(name);
1714 return res;
1715}
1716
1717/*
1718 * Compile the argument expressions.
1719 * "arg" points to just after the "(" and is advanced to after the ")"
1720 */
1721 static int
1722compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1723{
1724 char_u *p = *arg;
1725
1726 while (*p != NUL && *p != ')')
1727 {
1728 if (compile_expr1(&p, cctx) == FAIL)
1729 return FAIL;
1730 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001731
1732 if (*p != ',' && *skipwhite(p) == ',')
1733 {
1734 emsg(_("E1068: No white space allowed before ,"));
1735 p = skipwhite(p);
1736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001738 {
1739 ++p;
1740 if (!VIM_ISWHITE(*p))
1741 emsg(_("E1069: white space required after ,"));
1742 }
1743 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001745 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 if (*p != ')')
1747 {
1748 emsg(_(e_missing_close));
1749 return FAIL;
1750 }
1751 *arg = p + 1;
1752 return OK;
1753}
1754
1755/*
1756 * Compile a function call: name(arg1, arg2)
1757 * "arg" points to "name", "arg + varlen" to the "(".
1758 * "argcount_init" is 1 for "value->method()"
1759 * Instructions:
1760 * EVAL arg1
1761 * EVAL arg2
1762 * BCALL / DCALL / UCALL
1763 */
1764 static int
1765compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1766{
1767 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001768 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 int argcount = argcount_init;
1770 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001771 char_u fname_buf[FLEN_FIXED + 1];
1772 char_u *tofree = NULL;
1773 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001775 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776
1777 if (varlen >= sizeof(namebuf))
1778 {
1779 semsg(_("E1011: name too long: %s"), name);
1780 return FAIL;
1781 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001782 vim_strncpy(namebuf, *arg, varlen);
1783 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
1785 *arg = skipwhite(*arg + varlen + 1);
1786 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001787 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001789 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 {
1791 int idx;
1792
1793 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001794 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001796 {
1797 res = generate_BCALL(cctx, idx, argcount);
1798 goto theend;
1799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 semsg(_(e_unknownfunc), namebuf);
1801 }
1802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001804 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001806 {
1807 res = generate_CALL(cctx, ufunc, argcount);
1808 goto theend;
1809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810
1811 // If the name is a variable, load it and use PCALL.
1812 p = namebuf;
1813 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001814 {
1815 res = generate_PCALL(cctx, argcount, FALSE);
1816 goto theend;
1817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818
1819 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001820 res = generate_UCALL(cctx, name, argcount);
1821
1822theend:
1823 vim_free(tofree);
1824 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825}
1826
1827// like NAMESPACE_CHAR but with 'a' and 'l'.
1828#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1829
1830/*
1831 * Find the end of a variable or function name. Unlike find_name_end() this
1832 * does not recognize magic braces.
1833 * Return a pointer to just after the name. Equal to "arg" if there is no
1834 * valid name.
1835 */
1836 char_u *
1837to_name_end(char_u *arg)
1838{
1839 char_u *p;
1840
1841 // Quick check for valid starting character.
1842 if (!eval_isnamec1(*arg))
1843 return arg;
1844
1845 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1846 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1847 // and can be used in slice "[n:]".
1848 if (*p == ':' && (p != arg + 1
1849 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1850 break;
1851 return p;
1852}
1853
1854/*
1855 * Like to_name_end() but also skip over a list or dict constant.
1856 */
1857 char_u *
1858to_name_const_end(char_u *arg)
1859{
1860 char_u *p = to_name_end(arg);
1861 typval_T rettv;
1862
1863 if (p == arg && *arg == '[')
1864 {
1865
1866 // Can be "[1, 2, 3]->Func()".
1867 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1868 p = arg;
1869 }
1870 else if (p == arg && *arg == '#' && arg[1] == '{')
1871 {
1872 ++p;
1873 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1874 p = arg;
1875 }
1876 else if (p == arg && *arg == '{')
1877 {
1878 int ret = get_lambda_tv(&p, &rettv, FALSE);
1879
1880 if (ret == NOTDONE)
1881 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1882 if (ret != OK)
1883 p = arg;
1884 }
1885
1886 return p;
1887}
1888
1889 static void
1890type_mismatch(type_T *expected, type_T *actual)
1891{
1892 char *tofree1, *tofree2;
1893
1894 semsg(_("E1013: type mismatch, expected %s but got %s"),
1895 type_name(expected, &tofree1), type_name(actual, &tofree2));
1896 vim_free(tofree1);
1897 vim_free(tofree2);
1898}
1899
1900/*
1901 * Check if the expected and actual types match.
1902 */
1903 static int
1904check_type(type_T *expected, type_T *actual, int give_msg)
1905{
1906 if (expected->tt_type != VAR_UNKNOWN)
1907 {
1908 if (expected->tt_type != actual->tt_type)
1909 {
1910 if (give_msg)
1911 type_mismatch(expected, actual);
1912 return FAIL;
1913 }
1914 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1915 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01001916 int ret;
1917
1918 // void is used for an empty list or dict
1919 if (actual->tt_member == &t_void)
1920 ret = OK;
1921 else
1922 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 if (ret == FAIL && give_msg)
1924 type_mismatch(expected, actual);
1925 return ret;
1926 }
1927 }
1928 return OK;
1929}
1930
1931/*
1932 * Check that
1933 * - "actual" is "expected" type or
1934 * - "actual" is a type that can be "expected" type: add a runtime check; or
1935 * - return FAIL.
1936 */
1937 static int
1938need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1939{
Bram Moolenaar436472f2020-02-20 22:54:43 +01001940 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 return OK;
1942 if (actual->tt_type != VAR_UNKNOWN)
1943 {
1944 type_mismatch(expected, actual);
1945 return FAIL;
1946 }
1947 generate_TYPECHECK(cctx, expected, offset);
1948 return OK;
1949}
1950
1951/*
1952 * parse a list: [expr, expr]
1953 * "*arg" points to the '['.
1954 */
1955 static int
1956compile_list(char_u **arg, cctx_T *cctx)
1957{
1958 char_u *p = skipwhite(*arg + 1);
1959 int count = 0;
1960
1961 while (*p != ']')
1962 {
1963 if (*p == NUL)
1964 return FAIL;
1965 if (compile_expr1(&p, cctx) == FAIL)
1966 break;
1967 ++count;
1968 if (*p == ',')
1969 ++p;
1970 p = skipwhite(p);
1971 }
1972 *arg = p + 1;
1973
1974 generate_NEWLIST(cctx, count);
1975 return OK;
1976}
1977
1978/*
1979 * parse a lambda: {arg, arg -> expr}
1980 * "*arg" points to the '{'.
1981 */
1982 static int
1983compile_lambda(char_u **arg, cctx_T *cctx)
1984{
1985 garray_T *instr = &cctx->ctx_instr;
1986 typval_T rettv;
1987 ufunc_T *ufunc;
1988
1989 // Get the funcref in "rettv".
1990 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1991 return FAIL;
1992 ufunc = rettv.vval.v_partial->pt_func;
1993
1994 // The function will have one line: "return {expr}".
1995 // Compile it into instructions.
1996 compile_def_function(ufunc, TRUE);
1997
1998 if (ufunc->uf_dfunc_idx >= 0)
1999 {
2000 if (ga_grow(instr, 1) == FAIL)
2001 return FAIL;
2002 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2003 return OK;
2004 }
2005 return FAIL;
2006}
2007
2008/*
2009 * Compile a lamda call: expr->{lambda}(args)
2010 * "arg" points to the "{".
2011 */
2012 static int
2013compile_lambda_call(char_u **arg, cctx_T *cctx)
2014{
2015 ufunc_T *ufunc;
2016 typval_T rettv;
2017 int argcount = 1;
2018 int ret = FAIL;
2019
2020 // Get the funcref in "rettv".
2021 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2022 return FAIL;
2023
2024 if (**arg != '(')
2025 {
2026 if (*skipwhite(*arg) == '(')
2027 semsg(_(e_nowhitespace));
2028 else
2029 semsg(_(e_missing_paren), "lambda");
2030 clear_tv(&rettv);
2031 return FAIL;
2032 }
2033
2034 // The function will have one line: "return {expr}".
2035 // Compile it into instructions.
2036 ufunc = rettv.vval.v_partial->pt_func;
2037 ++ufunc->uf_refcount;
2038 compile_def_function(ufunc, TRUE);
2039
2040 // compile the arguments
2041 *arg = skipwhite(*arg + 1);
2042 if (compile_arguments(arg, cctx, &argcount) == OK)
2043 // call the compiled function
2044 ret = generate_CALL(cctx, ufunc, argcount);
2045
2046 clear_tv(&rettv);
2047 return ret;
2048}
2049
2050/*
2051 * parse a dict: {'key': val} or #{key: val}
2052 * "*arg" points to the '{'.
2053 */
2054 static int
2055compile_dict(char_u **arg, cctx_T *cctx, int literal)
2056{
2057 garray_T *instr = &cctx->ctx_instr;
2058 int count = 0;
2059 dict_T *d = dict_alloc();
2060 dictitem_T *item;
2061
2062 if (d == NULL)
2063 return FAIL;
2064 *arg = skipwhite(*arg + 1);
2065 while (**arg != '}' && **arg != NUL)
2066 {
2067 char_u *key = NULL;
2068
2069 if (literal)
2070 {
2071 char_u *p = to_name_end(*arg);
2072
2073 if (p == *arg)
2074 {
2075 semsg(_("E1014: Invalid key: %s"), *arg);
2076 return FAIL;
2077 }
2078 key = vim_strnsave(*arg, p - *arg);
2079 if (generate_PUSHS(cctx, key) == FAIL)
2080 return FAIL;
2081 *arg = p;
2082 }
2083 else
2084 {
2085 isn_T *isn;
2086
2087 if (compile_expr1(arg, cctx) == FAIL)
2088 return FAIL;
2089 // TODO: check type is string
2090 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2091 if (isn->isn_type == ISN_PUSHS)
2092 key = isn->isn_arg.string;
2093 }
2094
2095 // Check for duplicate keys, if using string keys.
2096 if (key != NULL)
2097 {
2098 item = dict_find(d, key, -1);
2099 if (item != NULL)
2100 {
2101 semsg(_(e_duplicate_key), key);
2102 goto failret;
2103 }
2104 item = dictitem_alloc(key);
2105 if (item != NULL)
2106 {
2107 item->di_tv.v_type = VAR_UNKNOWN;
2108 item->di_tv.v_lock = 0;
2109 if (dict_add(d, item) == FAIL)
2110 dictitem_free(item);
2111 }
2112 }
2113
2114 *arg = skipwhite(*arg);
2115 if (**arg != ':')
2116 {
2117 semsg(_(e_missing_dict_colon), *arg);
2118 return FAIL;
2119 }
2120
2121 *arg = skipwhite(*arg + 1);
2122 if (compile_expr1(arg, cctx) == FAIL)
2123 return FAIL;
2124 ++count;
2125
2126 if (**arg == '}')
2127 break;
2128 if (**arg != ',')
2129 {
2130 semsg(_(e_missing_dict_comma), *arg);
2131 goto failret;
2132 }
2133 *arg = skipwhite(*arg + 1);
2134 }
2135
2136 if (**arg != '}')
2137 {
2138 semsg(_(e_missing_dict_end), *arg);
2139 goto failret;
2140 }
2141 *arg = *arg + 1;
2142
2143 dict_unref(d);
2144 return generate_NEWDICT(cctx, count);
2145
2146failret:
2147 dict_unref(d);
2148 return FAIL;
2149}
2150
2151/*
2152 * Compile "&option".
2153 */
2154 static int
2155compile_get_option(char_u **arg, cctx_T *cctx)
2156{
2157 typval_T rettv;
2158 char_u *start = *arg;
2159 int ret;
2160
2161 // parse the option and get the current value to get the type.
2162 rettv.v_type = VAR_UNKNOWN;
2163 ret = get_option_tv(arg, &rettv, TRUE);
2164 if (ret == OK)
2165 {
2166 // include the '&' in the name, get_option_tv() expects it.
2167 char_u *name = vim_strnsave(start, *arg - start);
2168 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2169
2170 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2171 vim_free(name);
2172 }
2173 clear_tv(&rettv);
2174
2175 return ret;
2176}
2177
2178/*
2179 * Compile "$VAR".
2180 */
2181 static int
2182compile_get_env(char_u **arg, cctx_T *cctx)
2183{
2184 char_u *start = *arg;
2185 int len;
2186 int ret;
2187 char_u *name;
2188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 ++*arg;
2190 len = get_env_len(arg);
2191 if (len == 0)
2192 {
2193 semsg(_(e_syntax_at), start - 1);
2194 return FAIL;
2195 }
2196
2197 // include the '$' in the name, get_env_tv() expects it.
2198 name = vim_strnsave(start, len + 1);
2199 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2200 vim_free(name);
2201 return ret;
2202}
2203
2204/*
2205 * Compile "@r".
2206 */
2207 static int
2208compile_get_register(char_u **arg, cctx_T *cctx)
2209{
2210 int ret;
2211
2212 ++*arg;
2213 if (**arg == NUL)
2214 {
2215 semsg(_(e_syntax_at), *arg - 1);
2216 return FAIL;
2217 }
2218 if (!valid_yank_reg(**arg, TRUE))
2219 {
2220 emsg_invreg(**arg);
2221 return FAIL;
2222 }
2223 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2224 ++*arg;
2225 return ret;
2226}
2227
2228/*
2229 * Apply leading '!', '-' and '+' to constant "rettv".
2230 */
2231 static int
2232apply_leader(typval_T *rettv, char_u *start, char_u *end)
2233{
2234 char_u *p = end;
2235
2236 // this works from end to start
2237 while (p > start)
2238 {
2239 --p;
2240 if (*p == '-' || *p == '+')
2241 {
2242 // only '-' has an effect, for '+' we only check the type
2243#ifdef FEAT_FLOAT
2244 if (rettv->v_type == VAR_FLOAT)
2245 {
2246 if (*p == '-')
2247 rettv->vval.v_float = -rettv->vval.v_float;
2248 }
2249 else
2250#endif
2251 {
2252 varnumber_T val;
2253 int error = FALSE;
2254
2255 // tv_get_number_chk() accepts a string, but we don't want that
2256 // here
2257 if (check_not_string(rettv) == FAIL)
2258 return FAIL;
2259 val = tv_get_number_chk(rettv, &error);
2260 clear_tv(rettv);
2261 if (error)
2262 return FAIL;
2263 if (*p == '-')
2264 val = -val;
2265 rettv->v_type = VAR_NUMBER;
2266 rettv->vval.v_number = val;
2267 }
2268 }
2269 else
2270 {
2271 int v = tv2bool(rettv);
2272
2273 // '!' is permissive in the type.
2274 clear_tv(rettv);
2275 rettv->v_type = VAR_BOOL;
2276 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2277 }
2278 }
2279 return OK;
2280}
2281
2282/*
2283 * Recognize v: variables that are constants and set "rettv".
2284 */
2285 static void
2286get_vim_constant(char_u **arg, typval_T *rettv)
2287{
2288 if (STRNCMP(*arg, "v:true", 6) == 0)
2289 {
2290 rettv->v_type = VAR_BOOL;
2291 rettv->vval.v_number = VVAL_TRUE;
2292 *arg += 6;
2293 }
2294 else if (STRNCMP(*arg, "v:false", 7) == 0)
2295 {
2296 rettv->v_type = VAR_BOOL;
2297 rettv->vval.v_number = VVAL_FALSE;
2298 *arg += 7;
2299 }
2300 else if (STRNCMP(*arg, "v:null", 6) == 0)
2301 {
2302 rettv->v_type = VAR_SPECIAL;
2303 rettv->vval.v_number = VVAL_NULL;
2304 *arg += 6;
2305 }
2306 else if (STRNCMP(*arg, "v:none", 6) == 0)
2307 {
2308 rettv->v_type = VAR_SPECIAL;
2309 rettv->vval.v_number = VVAL_NONE;
2310 *arg += 6;
2311 }
2312}
2313
2314/*
2315 * Compile code to apply '-', '+' and '!'.
2316 */
2317 static int
2318compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2319{
2320 char_u *p = end;
2321
2322 // this works from end to start
2323 while (p > start)
2324 {
2325 --p;
2326 if (*p == '-' || *p == '+')
2327 {
2328 int negate = *p == '-';
2329 isn_T *isn;
2330
2331 // TODO: check type
2332 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2333 {
2334 --p;
2335 if (*p == '-')
2336 negate = !negate;
2337 }
2338 // only '-' has an effect, for '+' we only check the type
2339 if (negate)
2340 isn = generate_instr(cctx, ISN_NEGATENR);
2341 else
2342 isn = generate_instr(cctx, ISN_CHECKNR);
2343 if (isn == NULL)
2344 return FAIL;
2345 }
2346 else
2347 {
2348 int invert = TRUE;
2349
2350 while (p > start && p[-1] == '!')
2351 {
2352 --p;
2353 invert = !invert;
2354 }
2355 if (generate_2BOOL(cctx, invert) == FAIL)
2356 return FAIL;
2357 }
2358 }
2359 return OK;
2360}
2361
2362/*
2363 * Compile whatever comes after "name" or "name()".
2364 */
2365 static int
2366compile_subscript(
2367 char_u **arg,
2368 cctx_T *cctx,
2369 char_u **start_leader,
2370 char_u *end_leader)
2371{
2372 for (;;)
2373 {
2374 if (**arg == '(')
2375 {
2376 int argcount = 0;
2377
2378 // funcref(arg)
2379 *arg = skipwhite(*arg + 1);
2380 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2381 return FAIL;
2382 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2383 return FAIL;
2384 }
2385 else if (**arg == '-' && (*arg)[1] == '>')
2386 {
2387 char_u *p;
2388
2389 // something->method()
2390 // Apply the '!', '-' and '+' first:
2391 // -1.0->func() works like (-1.0)->func()
2392 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2393 return FAIL;
2394 *start_leader = end_leader; // don't apply again later
2395
2396 *arg = skipwhite(*arg + 2);
2397 if (**arg == '{')
2398 {
2399 // lambda call: list->{lambda}
2400 if (compile_lambda_call(arg, cctx) == FAIL)
2401 return FAIL;
2402 }
2403 else
2404 {
2405 // method call: list->method()
2406 for (p = *arg; eval_isnamec1(*p); ++p)
2407 ;
2408 if (*p != '(')
2409 {
2410 semsg(_(e_missing_paren), arg);
2411 return FAIL;
2412 }
2413 // TODO: base value may not be the first argument
2414 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2415 return FAIL;
2416 }
2417 }
2418 else if (**arg == '[')
2419 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002420 garray_T *stack;
2421 type_T **typep;
2422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 // list index: list[123]
2424 // TODO: more arguments
2425 // TODO: dict member dict['name']
2426 *arg = skipwhite(*arg + 1);
2427 if (compile_expr1(arg, cctx) == FAIL)
2428 return FAIL;
2429
2430 if (**arg != ']')
2431 {
2432 emsg(_(e_missbrac));
2433 return FAIL;
2434 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002435 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436
2437 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2438 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002439 stack = &cctx->ctx_type_stack;
2440 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2441 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2442 {
2443 emsg(_(e_listreq));
2444 return FAIL;
2445 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002446 if ((*typep)->tt_type == VAR_LIST)
2447 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 }
2449 else if (**arg == '.' && (*arg)[1] != '.')
2450 {
2451 char_u *p;
2452
2453 ++*arg;
2454 p = *arg;
2455 // dictionary member: dict.name
2456 if (eval_isnamec1(*p))
2457 while (eval_isnamec(*p))
2458 MB_PTR_ADV(p);
2459 if (p == *arg)
2460 {
2461 semsg(_(e_syntax_at), *arg);
2462 return FAIL;
2463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2465 return FAIL;
2466 *arg = p;
2467 }
2468 else
2469 break;
2470 }
2471
2472 // TODO - see handle_subscript():
2473 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2474 // Don't do this when "Func" is already a partial that was bound
2475 // explicitly (pt_auto is FALSE).
2476
2477 return OK;
2478}
2479
2480/*
2481 * Compile an expression at "*p" and add instructions to "instr".
2482 * "p" is advanced until after the expression, skipping white space.
2483 *
2484 * This is the equivalent of eval1(), eval2(), etc.
2485 */
2486
2487/*
2488 * number number constant
2489 * 0zFFFFFFFF Blob constant
2490 * "string" string constant
2491 * 'string' literal string constant
2492 * &option-name option value
2493 * @r register contents
2494 * identifier variable value
2495 * function() function call
2496 * $VAR environment variable
2497 * (expression) nested expression
2498 * [expr, expr] List
2499 * {key: val, key: val} Dictionary
2500 * #{key: val, key: val} Dictionary with literal keys
2501 *
2502 * Also handle:
2503 * ! in front logical NOT
2504 * - in front unary minus
2505 * + in front unary plus (ignored)
2506 * trailing (arg) funcref/partial call
2507 * trailing [] subscript in String or List
2508 * trailing .name entry in Dictionary
2509 * trailing ->name() method call
2510 */
2511 static int
2512compile_expr7(char_u **arg, cctx_T *cctx)
2513{
2514 typval_T rettv;
2515 char_u *start_leader, *end_leader;
2516 int ret = OK;
2517
2518 /*
2519 * Skip '!', '-' and '+' characters. They are handled later.
2520 */
2521 start_leader = *arg;
2522 while (**arg == '!' || **arg == '-' || **arg == '+')
2523 *arg = skipwhite(*arg + 1);
2524 end_leader = *arg;
2525
2526 rettv.v_type = VAR_UNKNOWN;
2527 switch (**arg)
2528 {
2529 /*
2530 * Number constant.
2531 */
2532 case '0': // also for blob starting with 0z
2533 case '1':
2534 case '2':
2535 case '3':
2536 case '4':
2537 case '5':
2538 case '6':
2539 case '7':
2540 case '8':
2541 case '9':
2542 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2543 return FAIL;
2544 break;
2545
2546 /*
2547 * String constant: "string".
2548 */
2549 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2550 return FAIL;
2551 break;
2552
2553 /*
2554 * Literal string constant: 'str''ing'.
2555 */
2556 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2557 return FAIL;
2558 break;
2559
2560 /*
2561 * Constant Vim variable.
2562 */
2563 case 'v': get_vim_constant(arg, &rettv);
2564 ret = NOTDONE;
2565 break;
2566
2567 /*
2568 * List: [expr, expr]
2569 */
2570 case '[': ret = compile_list(arg, cctx);
2571 break;
2572
2573 /*
2574 * Dictionary: #{key: val, key: val}
2575 */
2576 case '#': if ((*arg)[1] == '{')
2577 {
2578 ++*arg;
2579 ret = compile_dict(arg, cctx, TRUE);
2580 }
2581 else
2582 ret = NOTDONE;
2583 break;
2584
2585 /*
2586 * Lambda: {arg, arg -> expr}
2587 * Dictionary: {'key': val, 'key': val}
2588 */
2589 case '{': {
2590 char_u *start = skipwhite(*arg + 1);
2591
2592 // Find out what comes after the arguments.
2593 ret = get_function_args(&start, '-', NULL,
2594 NULL, NULL, NULL, TRUE);
2595 if (ret != FAIL && *start == '>')
2596 ret = compile_lambda(arg, cctx);
2597 else
2598 ret = compile_dict(arg, cctx, FALSE);
2599 }
2600 break;
2601
2602 /*
2603 * Option value: &name
2604 */
2605 case '&': ret = compile_get_option(arg, cctx);
2606 break;
2607
2608 /*
2609 * Environment variable: $VAR.
2610 */
2611 case '$': ret = compile_get_env(arg, cctx);
2612 break;
2613
2614 /*
2615 * Register contents: @r.
2616 */
2617 case '@': ret = compile_get_register(arg, cctx);
2618 break;
2619 /*
2620 * nested expression: (expression).
2621 */
2622 case '(': *arg = skipwhite(*arg + 1);
2623 ret = compile_expr1(arg, cctx); // recursive!
2624 *arg = skipwhite(*arg);
2625 if (**arg == ')')
2626 ++*arg;
2627 else if (ret == OK)
2628 {
2629 emsg(_(e_missing_close));
2630 ret = FAIL;
2631 }
2632 break;
2633
2634 default: ret = NOTDONE;
2635 break;
2636 }
2637 if (ret == FAIL)
2638 return FAIL;
2639
2640 if (rettv.v_type != VAR_UNKNOWN)
2641 {
2642 // apply the '!', '-' and '+' before the constant
2643 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2644 {
2645 clear_tv(&rettv);
2646 return FAIL;
2647 }
2648 start_leader = end_leader; // don't apply again below
2649
2650 // push constant
2651 switch (rettv.v_type)
2652 {
2653 case VAR_BOOL:
2654 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2655 break;
2656 case VAR_SPECIAL:
2657 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2658 break;
2659 case VAR_NUMBER:
2660 generate_PUSHNR(cctx, rettv.vval.v_number);
2661 break;
2662#ifdef FEAT_FLOAT
2663 case VAR_FLOAT:
2664 generate_PUSHF(cctx, rettv.vval.v_float);
2665 break;
2666#endif
2667 case VAR_BLOB:
2668 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2669 rettv.vval.v_blob = NULL;
2670 break;
2671 case VAR_STRING:
2672 generate_PUSHS(cctx, rettv.vval.v_string);
2673 rettv.vval.v_string = NULL;
2674 break;
2675 default:
2676 iemsg("constant type missing");
2677 return FAIL;
2678 }
2679 }
2680 else if (ret == NOTDONE)
2681 {
2682 char_u *p;
2683 int r;
2684
2685 if (!eval_isnamec1(**arg))
2686 {
2687 semsg(_("E1015: Name expected: %s"), *arg);
2688 return FAIL;
2689 }
2690
2691 // "name" or "name()"
2692 p = to_name_end(*arg);
2693 if (*p == '(')
2694 r = compile_call(arg, p - *arg, cctx, 0);
2695 else
2696 r = compile_load(arg, p, cctx, TRUE);
2697 if (r == FAIL)
2698 return FAIL;
2699 }
2700
2701 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2702 return FAIL;
2703
2704 // Now deal with prefixed '-', '+' and '!', if not done already.
2705 return compile_leader(cctx, start_leader, end_leader);
2706}
2707
2708/*
2709 * * number multiplication
2710 * / number division
2711 * % number modulo
2712 */
2713 static int
2714compile_expr6(char_u **arg, cctx_T *cctx)
2715{
2716 char_u *op;
2717
2718 // get the first variable
2719 if (compile_expr7(arg, cctx) == FAIL)
2720 return FAIL;
2721
2722 /*
2723 * Repeat computing, until no "*", "/" or "%" is following.
2724 */
2725 for (;;)
2726 {
2727 op = skipwhite(*arg);
2728 if (*op != '*' && *op != '/' && *op != '%')
2729 break;
2730 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2731 {
2732 char_u buf[3];
2733
2734 vim_strncpy(buf, op, 1);
2735 semsg(_(e_white_both), buf);
2736 }
2737 *arg = skipwhite(op + 1);
2738
2739 // get the second variable
2740 if (compile_expr7(arg, cctx) == FAIL)
2741 return FAIL;
2742
2743 generate_two_op(cctx, op);
2744 }
2745
2746 return OK;
2747}
2748
2749/*
2750 * + number addition
2751 * - number subtraction
2752 * .. string concatenation
2753 */
2754 static int
2755compile_expr5(char_u **arg, cctx_T *cctx)
2756{
2757 char_u *op;
2758 int oplen;
2759
2760 // get the first variable
2761 if (compile_expr6(arg, cctx) == FAIL)
2762 return FAIL;
2763
2764 /*
2765 * Repeat computing, until no "+", "-" or ".." is following.
2766 */
2767 for (;;)
2768 {
2769 op = skipwhite(*arg);
2770 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2771 break;
2772 oplen = (*op == '.' ? 2 : 1);
2773
2774 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2775 {
2776 char_u buf[3];
2777
2778 vim_strncpy(buf, op, oplen);
2779 semsg(_(e_white_both), buf);
2780 }
2781
2782 *arg = skipwhite(op + oplen);
2783
2784 // get the second variable
2785 if (compile_expr6(arg, cctx) == FAIL)
2786 return FAIL;
2787
2788 if (*op == '.')
2789 {
2790 if (may_generate_2STRING(-2, cctx) == FAIL
2791 || may_generate_2STRING(-1, cctx) == FAIL)
2792 return FAIL;
2793 generate_instr_drop(cctx, ISN_CONCAT, 1);
2794 }
2795 else
2796 generate_two_op(cctx, op);
2797 }
2798
2799 return OK;
2800}
2801
2802/*
2803 * expr5a == expr5b
2804 * expr5a =~ expr5b
2805 * expr5a != expr5b
2806 * expr5a !~ expr5b
2807 * expr5a > expr5b
2808 * expr5a >= expr5b
2809 * expr5a < expr5b
2810 * expr5a <= expr5b
2811 * expr5a is expr5b
2812 * expr5a isnot expr5b
2813 *
2814 * Produces instructions:
2815 * EVAL expr5a Push result of "expr5a"
2816 * EVAL expr5b Push result of "expr5b"
2817 * COMPARE one of the compare instructions
2818 */
2819 static int
2820compile_expr4(char_u **arg, cctx_T *cctx)
2821{
2822 exptype_T type = EXPR_UNKNOWN;
2823 char_u *p;
2824 int len = 2;
2825 int i;
2826 int type_is = FALSE;
2827
2828 // get the first variable
2829 if (compile_expr5(arg, cctx) == FAIL)
2830 return FAIL;
2831
2832 p = skipwhite(*arg);
2833 switch (p[0])
2834 {
2835 case '=': if (p[1] == '=')
2836 type = EXPR_EQUAL;
2837 else if (p[1] == '~')
2838 type = EXPR_MATCH;
2839 break;
2840 case '!': if (p[1] == '=')
2841 type = EXPR_NEQUAL;
2842 else if (p[1] == '~')
2843 type = EXPR_NOMATCH;
2844 break;
2845 case '>': if (p[1] != '=')
2846 {
2847 type = EXPR_GREATER;
2848 len = 1;
2849 }
2850 else
2851 type = EXPR_GEQUAL;
2852 break;
2853 case '<': if (p[1] != '=')
2854 {
2855 type = EXPR_SMALLER;
2856 len = 1;
2857 }
2858 else
2859 type = EXPR_SEQUAL;
2860 break;
2861 case 'i': if (p[1] == 's')
2862 {
2863 // "is" and "isnot"; but not a prefix of a name
2864 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2865 len = 5;
2866 i = p[len];
2867 if (!isalnum(i) && i != '_')
2868 {
2869 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2870 type_is = TRUE;
2871 }
2872 }
2873 break;
2874 }
2875
2876 /*
2877 * If there is a comparative operator, use it.
2878 */
2879 if (type != EXPR_UNKNOWN)
2880 {
2881 int ic = FALSE; // Default: do not ignore case
2882
2883 if (type_is && (p[len] == '?' || p[len] == '#'))
2884 {
2885 semsg(_(e_invexpr2), *arg);
2886 return FAIL;
2887 }
2888 // extra question mark appended: ignore case
2889 if (p[len] == '?')
2890 {
2891 ic = TRUE;
2892 ++len;
2893 }
2894 // extra '#' appended: match case (ignored)
2895 else if (p[len] == '#')
2896 ++len;
2897 // nothing appended: match case
2898
2899 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2900 {
2901 char_u buf[7];
2902
2903 vim_strncpy(buf, p, len);
2904 semsg(_(e_white_both), buf);
2905 }
2906
2907 // get the second variable
2908 *arg = skipwhite(p + len);
2909 if (compile_expr5(arg, cctx) == FAIL)
2910 return FAIL;
2911
2912 generate_COMPARE(cctx, type, ic);
2913 }
2914
2915 return OK;
2916}
2917
2918/*
2919 * Compile || or &&.
2920 */
2921 static int
2922compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2923{
2924 char_u *p = skipwhite(*arg);
2925 int opchar = *op;
2926
2927 if (p[0] == opchar && p[1] == opchar)
2928 {
2929 garray_T *instr = &cctx->ctx_instr;
2930 garray_T end_ga;
2931
2932 /*
2933 * Repeat until there is no following "||" or "&&"
2934 */
2935 ga_init2(&end_ga, sizeof(int), 10);
2936 while (p[0] == opchar && p[1] == opchar)
2937 {
2938 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2939 semsg(_(e_white_both), op);
2940
2941 if (ga_grow(&end_ga, 1) == FAIL)
2942 {
2943 ga_clear(&end_ga);
2944 return FAIL;
2945 }
2946 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2947 ++end_ga.ga_len;
2948 generate_JUMP(cctx, opchar == '|'
2949 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
2950
2951 // eval the next expression
2952 *arg = skipwhite(p + 2);
2953 if ((opchar == '|' ? compile_expr3(arg, cctx)
2954 : compile_expr4(arg, cctx)) == FAIL)
2955 {
2956 ga_clear(&end_ga);
2957 return FAIL;
2958 }
2959 p = skipwhite(*arg);
2960 }
2961
2962 // Fill in the end label in all jumps.
2963 while (end_ga.ga_len > 0)
2964 {
2965 isn_T *isn;
2966
2967 --end_ga.ga_len;
2968 isn = ((isn_T *)instr->ga_data)
2969 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2970 isn->isn_arg.jump.jump_where = instr->ga_len;
2971 }
2972 ga_clear(&end_ga);
2973 }
2974
2975 return OK;
2976}
2977
2978/*
2979 * expr4a && expr4a && expr4a logical AND
2980 *
2981 * Produces instructions:
2982 * EVAL expr4a Push result of "expr4a"
2983 * JUMP_AND_KEEP_IF_FALSE end
2984 * EVAL expr4b Push result of "expr4b"
2985 * JUMP_AND_KEEP_IF_FALSE end
2986 * EVAL expr4c Push result of "expr4c"
2987 * end:
2988 */
2989 static int
2990compile_expr3(char_u **arg, cctx_T *cctx)
2991{
2992 // get the first variable
2993 if (compile_expr4(arg, cctx) == FAIL)
2994 return FAIL;
2995
2996 // || and && work almost the same
2997 return compile_and_or(arg, cctx, "&&");
2998}
2999
3000/*
3001 * expr3a || expr3b || expr3c logical OR
3002 *
3003 * Produces instructions:
3004 * EVAL expr3a Push result of "expr3a"
3005 * JUMP_AND_KEEP_IF_TRUE end
3006 * EVAL expr3b Push result of "expr3b"
3007 * JUMP_AND_KEEP_IF_TRUE end
3008 * EVAL expr3c Push result of "expr3c"
3009 * end:
3010 */
3011 static int
3012compile_expr2(char_u **arg, cctx_T *cctx)
3013{
3014 // eval the first expression
3015 if (compile_expr3(arg, cctx) == FAIL)
3016 return FAIL;
3017
3018 // || and && work almost the same
3019 return compile_and_or(arg, cctx, "||");
3020}
3021
3022/*
3023 * Toplevel expression: expr2 ? expr1a : expr1b
3024 *
3025 * Produces instructions:
3026 * EVAL expr2 Push result of "expr"
3027 * JUMP_IF_FALSE alt jump if false
3028 * EVAL expr1a
3029 * JUMP_ALWAYS end
3030 * alt: EVAL expr1b
3031 * end:
3032 */
3033 static int
3034compile_expr1(char_u **arg, cctx_T *cctx)
3035{
3036 char_u *p;
3037
3038 // evaluate the first expression
3039 if (compile_expr2(arg, cctx) == FAIL)
3040 return FAIL;
3041
3042 p = skipwhite(*arg);
3043 if (*p == '?')
3044 {
3045 garray_T *instr = &cctx->ctx_instr;
3046 garray_T *stack = &cctx->ctx_type_stack;
3047 int alt_idx = instr->ga_len;
3048 int end_idx;
3049 isn_T *isn;
3050 type_T *type1;
3051 type_T *type2;
3052
3053 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3054 semsg(_(e_white_both), "?");
3055
3056 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3057
3058 // evaluate the second expression; any type is accepted
3059 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003060 if (compile_expr1(arg, cctx) == FAIL)
3061 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062
3063 // remember the type and drop it
3064 --stack->ga_len;
3065 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3066
3067 end_idx = instr->ga_len;
3068 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3069
3070 // jump here from JUMP_IF_FALSE
3071 isn = ((isn_T *)instr->ga_data) + alt_idx;
3072 isn->isn_arg.jump.jump_where = instr->ga_len;
3073
3074 // Check for the ":".
3075 p = skipwhite(*arg);
3076 if (*p != ':')
3077 {
3078 emsg(_(e_missing_colon));
3079 return FAIL;
3080 }
3081 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3082 semsg(_(e_white_both), ":");
3083
3084 // evaluate the third expression
3085 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003086 if (compile_expr1(arg, cctx) == FAIL)
3087 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088
3089 // If the types differ, the result has a more generic type.
3090 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3091 common_type(type1, type2, type2);
3092
3093 // jump here from JUMP_ALWAYS
3094 isn = ((isn_T *)instr->ga_data) + end_idx;
3095 isn->isn_arg.jump.jump_where = instr->ga_len;
3096 }
3097 return OK;
3098}
3099
3100/*
3101 * compile "return [expr]"
3102 */
3103 static char_u *
3104compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3105{
3106 char_u *p = arg;
3107 garray_T *stack = &cctx->ctx_type_stack;
3108 type_T *stack_type;
3109
3110 if (*p != NUL && *p != '|' && *p != '\n')
3111 {
3112 // compile return argument into instructions
3113 if (compile_expr1(&p, cctx) == FAIL)
3114 return NULL;
3115
3116 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3117 if (set_return_type)
3118 cctx->ctx_ufunc->uf_ret_type = stack_type;
3119 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3120 == FAIL)
3121 return NULL;
3122 }
3123 else
3124 {
3125 if (set_return_type)
3126 cctx->ctx_ufunc->uf_ret_type = &t_void;
3127 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3128 {
3129 emsg(_("E1003: Missing return value"));
3130 return NULL;
3131 }
3132
3133 // No argument, return zero.
3134 generate_PUSHNR(cctx, 0);
3135 }
3136
3137 if (generate_instr(cctx, ISN_RETURN) == NULL)
3138 return NULL;
3139
3140 // "return val | endif" is possible
3141 return skipwhite(p);
3142}
3143
3144/*
3145 * Return the length of an assignment operator, or zero if there isn't one.
3146 */
3147 int
3148assignment_len(char_u *p, int *heredoc)
3149{
3150 if (*p == '=')
3151 {
3152 if (p[1] == '<' && p[2] == '<')
3153 {
3154 *heredoc = TRUE;
3155 return 3;
3156 }
3157 return 1;
3158 }
3159 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3160 return 2;
3161 if (STRNCMP(p, "..=", 3) == 0)
3162 return 3;
3163 return 0;
3164}
3165
3166// words that cannot be used as a variable
3167static char *reserved[] = {
3168 "true",
3169 "false",
3170 NULL
3171};
3172
3173/*
3174 * Get a line for "=<<".
3175 * Return a pointer to the line in allocated memory.
3176 * Return NULL for end-of-file or some error.
3177 */
3178 static char_u *
3179heredoc_getline(
3180 int c UNUSED,
3181 void *cookie,
3182 int indent UNUSED,
3183 int do_concat UNUSED)
3184{
3185 cctx_T *cctx = (cctx_T *)cookie;
3186
3187 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3188 NULL;
3189 ++cctx->ctx_lnum;
3190 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3191 [cctx->ctx_lnum]);
3192}
3193
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003194typedef enum {
3195 dest_local,
3196 dest_option,
3197 dest_env,
3198 dest_global,
3199 dest_vimvar,
3200 dest_script,
3201 dest_reg,
3202} assign_dest_T;
3203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204/*
3205 * compile "let var [= expr]", "const var = expr" and "var = expr"
3206 * "arg" points to "var".
3207 */
3208 static char_u *
3209compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3210{
3211 char_u *p;
3212 char_u *ret = NULL;
3213 int var_count = 0;
3214 int semicolon = 0;
3215 size_t varlen;
3216 garray_T *instr = &cctx->ctx_instr;
3217 int idx = -1;
3218 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003220 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003222 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 int oplen = 0;
3224 int heredoc = FALSE;
3225 type_T *type;
3226 lvar_T *lvar;
3227 char_u *name;
3228 char_u *sp;
3229 int has_type = FALSE;
3230 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3231 int instr_count = -1;
3232
3233 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3234 if (p == NULL)
3235 return NULL;
3236 if (var_count > 0)
3237 {
3238 // TODO: let [var, var] = list
3239 emsg("Cannot handle a list yet");
3240 return NULL;
3241 }
3242
3243 varlen = p - arg;
3244 name = vim_strnsave(arg, (int)varlen);
3245 if (name == NULL)
3246 return NULL;
3247
3248 if (*arg == '&')
3249 {
3250 int cc;
3251 long numval;
3252 char_u *stringval = NULL;
3253
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003254 dest = dest_option;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 if (cmdidx == CMD_const)
3256 {
3257 emsg(_(e_const_option));
3258 return NULL;
3259 }
3260 if (is_decl)
3261 {
3262 semsg(_("E1052: Cannot declare an option: %s"), arg);
3263 goto theend;
3264 }
3265 p = arg;
3266 p = find_option_end(&p, &opt_flags);
3267 if (p == NULL)
3268 {
3269 emsg(_(e_letunexp));
3270 return NULL;
3271 }
3272 cc = *p;
3273 *p = NUL;
3274 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3275 *p = cc;
3276 if (opt_type == -3)
3277 {
3278 semsg(_(e_unknown_option), *arg);
3279 return NULL;
3280 }
3281 if (opt_type == -2 || opt_type == 0)
3282 type = &t_string;
3283 else
3284 type = &t_number; // both number and boolean option
3285 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003286 else if (*arg == '$')
3287 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003288 dest = dest_env;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003289 if (is_decl)
3290 {
3291 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3292 goto theend;
3293 }
3294 }
3295 else if (*arg == '@')
3296 {
3297 if (!valid_yank_reg(arg[1], TRUE))
3298 {
3299 emsg_invreg(arg[1]);
3300 return FAIL;
3301 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003302 dest = dest_reg;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003303 if (is_decl)
3304 {
3305 semsg(_("E1066: Cannot declare a register: %s"), name);
3306 goto theend;
3307 }
3308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 else if (STRNCMP(arg, "g:", 2) == 0)
3310 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003311 dest = dest_global;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 if (is_decl)
3313 {
3314 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3315 goto theend;
3316 }
3317 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003318 else if (STRNCMP(arg, "v:", 2) == 0)
3319 {
3320 vimvaridx = find_vim_var(name + 2);
3321 if (vimvaridx < 0)
3322 {
3323 semsg(_(e_var_notfound), arg);
3324 goto theend;
3325 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003326 dest = dest_vimvar;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003327 if (is_decl)
3328 {
3329 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3330 goto theend;
3331 }
3332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 else
3334 {
3335 for (idx = 0; reserved[idx] != NULL; ++idx)
3336 if (STRCMP(reserved[idx], name) == 0)
3337 {
3338 semsg(_("E1034: Cannot use reserved name %s"), name);
3339 goto theend;
3340 }
3341
3342 idx = lookup_local(arg, varlen, cctx);
3343 if (idx >= 0)
3344 {
3345 if (is_decl)
3346 {
3347 semsg(_("E1017: Variable already declared: %s"), name);
3348 goto theend;
3349 }
3350 else
3351 {
3352 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3353 if (lvar->lv_const)
3354 {
3355 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3356 goto theend;
3357 }
3358 }
3359 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003360 else if (STRNCMP(arg, "s:", 2) == 0
3361 || lookup_script(arg, varlen) == OK
3362 || find_imported(arg, varlen, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003364 dest = dest_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 if (is_decl)
3366 {
3367 semsg(_("E1054: Variable already declared in the script: %s"),
3368 name);
3369 goto theend;
3370 }
3371 }
3372 }
3373
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003374 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
3376 if (is_decl && *p == ':')
3377 {
3378 // parse optional type: "let var: type = expr"
3379 p = skipwhite(p + 1);
3380 type = parse_type(&p, cctx->ctx_type_list);
3381 if (type == NULL)
3382 goto theend;
3383 has_type = TRUE;
3384 }
3385 else if (idx < 0)
3386 {
3387 // global and new local default to "any" type
3388 type = &t_any;
3389 }
3390 else
3391 {
3392 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3393 type = lvar->lv_type;
3394 }
3395 }
3396
3397 sp = p;
3398 p = skipwhite(p);
3399 op = p;
3400 oplen = assignment_len(p, &heredoc);
3401 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3402 {
3403 char_u buf[4];
3404
3405 vim_strncpy(buf, op, oplen);
3406 semsg(_(e_white_both), buf);
3407 }
3408
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003409 if (oplen == 3 && !heredoc && dest != dest_global
3410 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003412 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 goto theend;
3414 }
3415
3416 // +=, /=, etc. require an existing variable
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003417 if (idx < 0 && dest == dest_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 {
3419 if (oplen > 1 && !heredoc)
3420 {
3421 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3422 name);
3423 goto theend;
3424 }
3425
3426 // new local variable
3427 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3428 if (idx < 0)
3429 goto theend;
3430 }
3431
3432 if (heredoc)
3433 {
3434 list_T *l;
3435 listitem_T *li;
3436
3437 // [let] varname =<< [trim] {end}
3438 eap->getline = heredoc_getline;
3439 eap->cookie = cctx;
3440 l = heredoc_get(eap, op + 3);
3441
3442 // Push each line and the create the list.
3443 for (li = l->lv_first; li != NULL; li = li->li_next)
3444 {
3445 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3446 li->li_tv.vval.v_string = NULL;
3447 }
3448 generate_NEWLIST(cctx, l->lv_len);
3449 type = &t_list_string;
3450 list_free(l);
3451 p += STRLEN(p);
3452 }
3453 else if (oplen > 0)
3454 {
3455 // for "+=", "*=", "..=" etc. first load the current value
3456 if (*op != '=')
3457 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003458 switch (dest)
3459 {
3460 case dest_option:
3461 // TODO: check the option exists
3462 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3463 break;
3464 case dest_global:
3465 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3466 break;
3467 case dest_script:
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003468 compile_load_scriptvar(cctx, name + (name[1] == ':' ? 2 : 0), NULL, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003469 break;
3470 case dest_env:
3471 // Include $ in the name here
3472 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3473 break;
3474 case dest_reg:
3475 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3476 break;
3477 case dest_vimvar:
3478 generate_LOADV(cctx, name + 2, TRUE);
3479 break;
3480 case dest_local:
3481 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3482 break;
3483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 }
3485
3486 // compile the expression
3487 instr_count = instr->ga_len;
3488 p = skipwhite(p + oplen);
3489 if (compile_expr1(&p, cctx) == FAIL)
3490 goto theend;
3491
3492 if (idx >= 0 && (is_decl || !has_type))
3493 {
3494 garray_T *stack = &cctx->ctx_type_stack;
3495 type_T *stacktype =
3496 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3497
3498 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3499 if (!has_type)
3500 {
3501 if (stacktype->tt_type == VAR_VOID)
3502 {
3503 emsg(_("E1031: Cannot use void value"));
3504 goto theend;
3505 }
3506 else
3507 lvar->lv_type = stacktype;
3508 }
3509 else
3510 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3511 goto theend;
3512 }
3513 }
3514 else if (cmdidx == CMD_const)
3515 {
3516 emsg(_("E1021: const requires a value"));
3517 goto theend;
3518 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003519 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 {
3521 emsg(_("E1022: type or initialization required"));
3522 goto theend;
3523 }
3524 else
3525 {
3526 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 if (ga_grow(instr, 1) == FAIL)
3528 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003529 switch (type->tt_type)
3530 {
3531 case VAR_BOOL:
3532 generate_PUSHBOOL(cctx, VVAL_FALSE);
3533 break;
3534 case VAR_SPECIAL:
3535 generate_PUSHSPEC(cctx, VVAL_NONE);
3536 break;
3537 case VAR_FLOAT:
3538#ifdef FEAT_FLOAT
3539 generate_PUSHF(cctx, 0.0);
3540#endif
3541 break;
3542 case VAR_STRING:
3543 generate_PUSHS(cctx, NULL);
3544 break;
3545 case VAR_BLOB:
3546 generate_PUSHBLOB(cctx, NULL);
3547 break;
3548 case VAR_FUNC:
3549 // generate_PUSHS(cctx, NULL); TODO
3550 break;
3551 case VAR_PARTIAL:
3552 // generate_PUSHS(cctx, NULL); TODO
3553 break;
3554 case VAR_LIST:
3555 generate_NEWLIST(cctx, 0);
3556 break;
3557 case VAR_DICT:
3558 generate_NEWDICT(cctx, 0);
3559 break;
3560 case VAR_JOB:
3561 // generate_PUSHS(cctx, NULL); TODO
3562 break;
3563 case VAR_CHANNEL:
3564 // generate_PUSHS(cctx, NULL); TODO
3565 break;
3566 case VAR_NUMBER:
3567 case VAR_UNKNOWN:
3568 case VAR_VOID:
3569 generate_PUSHNR(cctx, 0);
3570 break;
3571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 }
3573
3574 if (oplen > 0 && *op != '=')
3575 {
3576 type_T *expected = &t_number;
3577 garray_T *stack = &cctx->ctx_type_stack;
3578 type_T *stacktype;
3579
3580 // TODO: if type is known use float or any operation
3581
3582 if (*op == '.')
3583 expected = &t_string;
3584 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3585 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3586 goto theend;
3587
3588 if (*op == '.')
3589 generate_instr_drop(cctx, ISN_CONCAT, 1);
3590 else
3591 {
3592 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3593
3594 if (isn == NULL)
3595 goto theend;
3596 switch (*op)
3597 {
3598 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3599 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3600 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3601 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3602 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3603 }
3604 }
3605 }
3606
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003607 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003609 case dest_option:
3610 generate_STOREOPT(cctx, name + 1, opt_flags);
3611 break;
3612 case dest_global:
3613 // include g: with the name, easier to execute that way
3614 generate_STORE(cctx, ISN_STOREG, 0, name);
3615 break;
3616 case dest_env:
3617 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3618 break;
3619 case dest_reg:
3620 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3621 break;
3622 case dest_vimvar:
3623 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3624 break;
3625 case dest_script:
3626 {
3627 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3628 imported_T *import = NULL;
3629 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003630
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003631 if (name[1] != ':')
3632 {
3633 import = find_imported(name, 0, cctx);
3634 if (import != NULL)
3635 sid = import->imp_sid;
3636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003638 idx = get_script_item_idx(sid, rawname, TRUE);
3639 // TODO: specific type
3640 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003641 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003642 else
3643 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3644 sid, idx, &t_any);
3645 }
3646 break;
3647 case dest_local:
3648 {
3649 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003651 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3652 // into ISN_STORENR
3653 if (instr->ga_len == instr_count + 1
3654 && isn->isn_type == ISN_PUSHNR)
3655 {
3656 varnumber_T val = isn->isn_arg.number;
3657 garray_T *stack = &cctx->ctx_type_stack;
3658
3659 isn->isn_type = ISN_STORENR;
3660 isn->isn_arg.storenr.str_idx = idx;
3661 isn->isn_arg.storenr.str_val = val;
3662 if (stack->ga_len > 0)
3663 --stack->ga_len;
3664 }
3665 else
3666 generate_STORE(cctx, ISN_STORE, idx, NULL);
3667 }
3668 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670 ret = p;
3671
3672theend:
3673 vim_free(name);
3674 return ret;
3675}
3676
3677/*
3678 * Compile an :import command.
3679 */
3680 static char_u *
3681compile_import(char_u *arg, cctx_T *cctx)
3682{
3683 return handle_import(arg, &cctx->ctx_imports, 0);
3684}
3685
3686/*
3687 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3688 */
3689 static int
3690compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3691{
3692 garray_T *instr = &cctx->ctx_instr;
3693 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3694
3695 if (endlabel == NULL)
3696 return FAIL;
3697 endlabel->el_next = *el;
3698 *el = endlabel;
3699 endlabel->el_end_label = instr->ga_len;
3700
3701 generate_JUMP(cctx, when, 0);
3702 return OK;
3703}
3704
3705 static void
3706compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3707{
3708 garray_T *instr = &cctx->ctx_instr;
3709
3710 while (*el != NULL)
3711 {
3712 endlabel_T *cur = (*el);
3713 isn_T *isn;
3714
3715 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3716 isn->isn_arg.jump.jump_where = instr->ga_len;
3717 *el = cur->el_next;
3718 vim_free(cur);
3719 }
3720}
3721
3722/*
3723 * Create a new scope and set up the generic items.
3724 */
3725 static scope_T *
3726new_scope(cctx_T *cctx, scopetype_T type)
3727{
3728 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3729
3730 if (scope == NULL)
3731 return NULL;
3732 scope->se_outer = cctx->ctx_scope;
3733 cctx->ctx_scope = scope;
3734 scope->se_type = type;
3735 scope->se_local_count = cctx->ctx_locals.ga_len;
3736 return scope;
3737}
3738
3739/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003740 * Evaluate an expression that is a constant:
3741 * has(arg)
3742 *
3743 * Also handle:
3744 * ! in front logical NOT
3745 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003746 * Return FAIL if the expression is not a constant.
3747 */
3748 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003749evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003750{
3751 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003752 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003753
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003754 /*
3755 * Skip '!' characters. They are handled later.
3756 */
3757 start_leader = *arg;
3758 while (**arg == '!')
3759 *arg = skipwhite(*arg + 1);
3760 end_leader = *arg;
3761
3762 /*
3763 * Recognize only has() for now.
3764 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003765 if (STRNCMP("has(", *arg, 4) != 0)
3766 return FAIL;
3767 *arg = skipwhite(*arg + 4);
3768
3769 if (**arg == '"')
3770 {
3771 if (get_string_tv(arg, tv, TRUE) == FAIL)
3772 return FAIL;
3773 }
3774 else if (**arg == '\'')
3775 {
3776 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3777 return FAIL;
3778 }
3779 else
3780 return FAIL;
3781
3782 *arg = skipwhite(*arg);
3783 if (**arg != ')')
3784 return FAIL;
3785 *arg = skipwhite(*arg + 1);
3786
3787 argvars[0] = *tv;
3788 argvars[1].v_type = VAR_UNKNOWN;
3789 tv->v_type = VAR_NUMBER;
3790 tv->vval.v_number = 0;
3791 f_has(argvars, tv);
3792 clear_tv(&argvars[0]);
3793
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003794 while (start_leader < end_leader)
3795 {
3796 if (*start_leader == '!')
3797 tv->vval.v_number = !tv->vval.v_number;
3798 ++start_leader;
3799 }
3800
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003801 return OK;
3802}
3803
3804static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3805
3806/*
3807 * Compile constant || or &&.
3808 */
3809 static int
3810evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3811{
3812 char_u *p = skipwhite(*arg);
3813 int opchar = *op;
3814
3815 if (p[0] == opchar && p[1] == opchar)
3816 {
3817 int val = tv2bool(tv);
3818
3819 /*
3820 * Repeat until there is no following "||" or "&&"
3821 */
3822 while (p[0] == opchar && p[1] == opchar)
3823 {
3824 typval_T tv2;
3825
3826 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3827 return FAIL;
3828
3829 // eval the next expression
3830 *arg = skipwhite(p + 2);
3831 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01003832 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003833 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003834 : evaluate_const_expr7(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003835 {
3836 clear_tv(&tv2);
3837 return FAIL;
3838 }
3839 if ((opchar == '&') == val)
3840 {
3841 // false || tv2 or true && tv2: use tv2
3842 clear_tv(tv);
3843 *tv = tv2;
3844 val = tv2bool(tv);
3845 }
3846 else
3847 clear_tv(&tv2);
3848 p = skipwhite(*arg);
3849 }
3850 }
3851
3852 return OK;
3853}
3854
3855/*
3856 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3857 * Return FAIL if the expression is not a constant.
3858 */
3859 static int
3860evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3861{
3862 // evaluate the first expression
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003863 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003864 return FAIL;
3865
3866 // || and && work almost the same
3867 return evaluate_const_and_or(arg, cctx, "&&", tv);
3868}
3869
3870/*
3871 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3872 * Return FAIL if the expression is not a constant.
3873 */
3874 static int
3875evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3876{
3877 // evaluate the first expression
3878 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3879 return FAIL;
3880
3881 // || and && work almost the same
3882 return evaluate_const_and_or(arg, cctx, "||", tv);
3883}
3884
3885/*
3886 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3887 * E.g. for "has('feature')".
3888 * This does not produce error messages. "tv" should be cleared afterwards.
3889 * Return FAIL if the expression is not a constant.
3890 */
3891 static int
3892evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3893{
3894 char_u *p;
3895
3896 // evaluate the first expression
3897 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3898 return FAIL;
3899
3900 p = skipwhite(*arg);
3901 if (*p == '?')
3902 {
3903 int val = tv2bool(tv);
3904 typval_T tv2;
3905
3906 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3907 return FAIL;
3908
3909 // evaluate the second expression; any type is accepted
3910 clear_tv(tv);
3911 *arg = skipwhite(p + 1);
3912 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3913 return FAIL;
3914
3915 // Check for the ":".
3916 p = skipwhite(*arg);
3917 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3918 return FAIL;
3919
3920 // evaluate the third expression
3921 *arg = skipwhite(p + 1);
3922 tv2.v_type = VAR_UNKNOWN;
3923 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3924 {
3925 clear_tv(&tv2);
3926 return FAIL;
3927 }
3928 if (val)
3929 {
3930 // use the expr after "?"
3931 clear_tv(&tv2);
3932 }
3933 else
3934 {
3935 // use the expr after ":"
3936 clear_tv(tv);
3937 *tv = tv2;
3938 }
3939 }
3940 return OK;
3941}
3942
3943/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 * compile "if expr"
3945 *
3946 * "if expr" Produces instructions:
3947 * EVAL expr Push result of "expr"
3948 * JUMP_IF_FALSE end
3949 * ... body ...
3950 * end:
3951 *
3952 * "if expr | else" Produces instructions:
3953 * EVAL expr Push result of "expr"
3954 * JUMP_IF_FALSE else
3955 * ... body ...
3956 * JUMP_ALWAYS end
3957 * else:
3958 * ... body ...
3959 * end:
3960 *
3961 * "if expr1 | elseif expr2 | else" Produces instructions:
3962 * EVAL expr Push result of "expr"
3963 * JUMP_IF_FALSE elseif
3964 * ... body ...
3965 * JUMP_ALWAYS end
3966 * elseif:
3967 * EVAL expr Push result of "expr"
3968 * JUMP_IF_FALSE else
3969 * ... body ...
3970 * JUMP_ALWAYS end
3971 * else:
3972 * ... body ...
3973 * end:
3974 */
3975 static char_u *
3976compile_if(char_u *arg, cctx_T *cctx)
3977{
3978 char_u *p = arg;
3979 garray_T *instr = &cctx->ctx_instr;
3980 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003981 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003983 // compile "expr"; if we know it evaluates to FALSE skip the block
3984 tv.v_type = VAR_UNKNOWN;
3985 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3986 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3987 else
3988 cctx->ctx_skip = MAYBE;
3989 clear_tv(&tv);
3990 if (cctx->ctx_skip == MAYBE)
3991 {
3992 p = arg;
3993 if (compile_expr1(&p, cctx) == FAIL)
3994 return NULL;
3995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996
3997 scope = new_scope(cctx, IF_SCOPE);
3998 if (scope == NULL)
3999 return NULL;
4000
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004001 if (cctx->ctx_skip == MAYBE)
4002 {
4003 // "where" is set when ":elseif", "else" or ":endif" is found
4004 scope->se_u.se_if.is_if_label = instr->ga_len;
4005 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4006 }
4007 else
4008 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
4010 return p;
4011}
4012
4013 static char_u *
4014compile_elseif(char_u *arg, cctx_T *cctx)
4015{
4016 char_u *p = arg;
4017 garray_T *instr = &cctx->ctx_instr;
4018 isn_T *isn;
4019 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004020 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
4022 if (scope == NULL || scope->se_type != IF_SCOPE)
4023 {
4024 emsg(_(e_elseif_without_if));
4025 return NULL;
4026 }
4027 cctx->ctx_locals.ga_len = scope->se_local_count;
4028
Bram Moolenaar158906c2020-02-06 20:39:45 +01004029 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004030 {
4031 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004033 return NULL;
4034 // previous "if" or "elseif" jumps here
4035 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4036 isn->isn_arg.jump.jump_where = instr->ga_len;
4037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004039 // compile "expr"; if we know it evaluates to FALSE skip the block
4040 tv.v_type = VAR_UNKNOWN;
4041 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4042 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4043 else
4044 cctx->ctx_skip = MAYBE;
4045 clear_tv(&tv);
4046 if (cctx->ctx_skip == MAYBE)
4047 {
4048 p = arg;
4049 if (compile_expr1(&p, cctx) == FAIL)
4050 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004052 // "where" is set when ":elseif", "else" or ":endif" is found
4053 scope->se_u.se_if.is_if_label = instr->ga_len;
4054 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4055 }
4056 else
4057 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058
4059 return p;
4060}
4061
4062 static char_u *
4063compile_else(char_u *arg, cctx_T *cctx)
4064{
4065 char_u *p = arg;
4066 garray_T *instr = &cctx->ctx_instr;
4067 isn_T *isn;
4068 scope_T *scope = cctx->ctx_scope;
4069
4070 if (scope == NULL || scope->se_type != IF_SCOPE)
4071 {
4072 emsg(_(e_else_without_if));
4073 return NULL;
4074 }
4075 cctx->ctx_locals.ga_len = scope->se_local_count;
4076
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004077 // jump from previous block to the end, unless the else block is empty
4078 if (cctx->ctx_skip == MAYBE)
4079 {
4080 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004082 return NULL;
4083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084
Bram Moolenaar158906c2020-02-06 20:39:45 +01004085 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004086 {
4087 if (scope->se_u.se_if.is_if_label >= 0)
4088 {
4089 // previous "if" or "elseif" jumps here
4090 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4091 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004092 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004093 }
4094 }
4095
4096 if (cctx->ctx_skip != MAYBE)
4097 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
4099 return p;
4100}
4101
4102 static char_u *
4103compile_endif(char_u *arg, cctx_T *cctx)
4104{
4105 scope_T *scope = cctx->ctx_scope;
4106 ifscope_T *ifscope;
4107 garray_T *instr = &cctx->ctx_instr;
4108 isn_T *isn;
4109
4110 if (scope == NULL || scope->se_type != IF_SCOPE)
4111 {
4112 emsg(_(e_endif_without_if));
4113 return NULL;
4114 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004115 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 cctx->ctx_scope = scope->se_outer;
4117 cctx->ctx_locals.ga_len = scope->se_local_count;
4118
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004119 if (scope->se_u.se_if.is_if_label >= 0)
4120 {
4121 // previous "if" or "elseif" jumps here
4122 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4123 isn->isn_arg.jump.jump_where = instr->ga_len;
4124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 // Fill in the "end" label in jumps at the end of the blocks.
4126 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004127 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128
4129 vim_free(scope);
4130 return arg;
4131}
4132
4133/*
4134 * compile "for var in expr"
4135 *
4136 * Produces instructions:
4137 * PUSHNR -1
4138 * STORE loop-idx Set index to -1
4139 * EVAL expr Push result of "expr"
4140 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4141 * - if beyond end, jump to "end"
4142 * - otherwise get item from list and push it
4143 * STORE var Store item in "var"
4144 * ... body ...
4145 * JUMP top Jump back to repeat
4146 * end: DROP Drop the result of "expr"
4147 *
4148 */
4149 static char_u *
4150compile_for(char_u *arg, cctx_T *cctx)
4151{
4152 char_u *p;
4153 size_t varlen;
4154 garray_T *instr = &cctx->ctx_instr;
4155 garray_T *stack = &cctx->ctx_type_stack;
4156 scope_T *scope;
4157 int loop_idx; // index of loop iteration variable
4158 int var_idx; // index of "var"
4159 type_T *vartype;
4160
4161 // TODO: list of variables: "for [key, value] in dict"
4162 // parse "var"
4163 for (p = arg; eval_isnamec1(*p); ++p)
4164 ;
4165 varlen = p - arg;
4166 var_idx = lookup_local(arg, varlen, cctx);
4167 if (var_idx >= 0)
4168 {
4169 semsg(_("E1023: variable already defined: %s"), arg);
4170 return NULL;
4171 }
4172
4173 // consume "in"
4174 p = skipwhite(p);
4175 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4176 {
4177 emsg(_(e_missing_in));
4178 return NULL;
4179 }
4180 p = skipwhite(p + 2);
4181
4182
4183 scope = new_scope(cctx, FOR_SCOPE);
4184 if (scope == NULL)
4185 return NULL;
4186
4187 // Reserve a variable to store the loop iteration counter.
4188 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4189 if (loop_idx < 0)
4190 return NULL;
4191
4192 // Reserve a variable to store "var"
4193 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4194 if (var_idx < 0)
4195 return NULL;
4196
4197 generate_STORENR(cctx, loop_idx, -1);
4198
4199 // compile "expr", it remains on the stack until "endfor"
4200 arg = p;
4201 if (compile_expr1(&arg, cctx) == FAIL)
4202 return NULL;
4203
4204 // now we know the type of "var"
4205 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4206 if (vartype->tt_type != VAR_LIST)
4207 {
4208 emsg(_("E1024: need a List to iterate over"));
4209 return NULL;
4210 }
4211 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4212 {
4213 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4214
4215 lvar->lv_type = vartype->tt_member;
4216 }
4217
4218 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004219 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
4221 generate_FOR(cctx, loop_idx);
4222 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4223
4224 return arg;
4225}
4226
4227/*
4228 * compile "endfor"
4229 */
4230 static char_u *
4231compile_endfor(char_u *arg, cctx_T *cctx)
4232{
4233 garray_T *instr = &cctx->ctx_instr;
4234 scope_T *scope = cctx->ctx_scope;
4235 forscope_T *forscope;
4236 isn_T *isn;
4237
4238 if (scope == NULL || scope->se_type != FOR_SCOPE)
4239 {
4240 emsg(_(e_for));
4241 return NULL;
4242 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004243 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 cctx->ctx_scope = scope->se_outer;
4245 cctx->ctx_locals.ga_len = scope->se_local_count;
4246
4247 // At end of ":for" scope jump back to the FOR instruction.
4248 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4249
4250 // Fill in the "end" label in the FOR statement so it can jump here
4251 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4252 isn->isn_arg.forloop.for_end = instr->ga_len;
4253
4254 // Fill in the "end" label any BREAK statements
4255 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4256
4257 // Below the ":for" scope drop the "expr" list from the stack.
4258 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4259 return NULL;
4260
4261 vim_free(scope);
4262
4263 return arg;
4264}
4265
4266/*
4267 * compile "while expr"
4268 *
4269 * Produces instructions:
4270 * top: EVAL expr Push result of "expr"
4271 * JUMP_IF_FALSE end jump if false
4272 * ... body ...
4273 * JUMP top Jump back to repeat
4274 * end:
4275 *
4276 */
4277 static char_u *
4278compile_while(char_u *arg, cctx_T *cctx)
4279{
4280 char_u *p = arg;
4281 garray_T *instr = &cctx->ctx_instr;
4282 scope_T *scope;
4283
4284 scope = new_scope(cctx, WHILE_SCOPE);
4285 if (scope == NULL)
4286 return NULL;
4287
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004288 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289
4290 // compile "expr"
4291 if (compile_expr1(&p, cctx) == FAIL)
4292 return NULL;
4293
4294 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004295 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 JUMP_IF_FALSE, cctx) == FAIL)
4297 return FAIL;
4298
4299 return p;
4300}
4301
4302/*
4303 * compile "endwhile"
4304 */
4305 static char_u *
4306compile_endwhile(char_u *arg, cctx_T *cctx)
4307{
4308 scope_T *scope = cctx->ctx_scope;
4309
4310 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4311 {
4312 emsg(_(e_while));
4313 return NULL;
4314 }
4315 cctx->ctx_scope = scope->se_outer;
4316 cctx->ctx_locals.ga_len = scope->se_local_count;
4317
4318 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004319 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320
4321 // Fill in the "end" label in the WHILE statement so it can jump here.
4322 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004323 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
4325 vim_free(scope);
4326
4327 return arg;
4328}
4329
4330/*
4331 * compile "continue"
4332 */
4333 static char_u *
4334compile_continue(char_u *arg, cctx_T *cctx)
4335{
4336 scope_T *scope = cctx->ctx_scope;
4337
4338 for (;;)
4339 {
4340 if (scope == NULL)
4341 {
4342 emsg(_(e_continue));
4343 return NULL;
4344 }
4345 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4346 break;
4347 scope = scope->se_outer;
4348 }
4349
4350 // Jump back to the FOR or WHILE instruction.
4351 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004352 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4353 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 return arg;
4355}
4356
4357/*
4358 * compile "break"
4359 */
4360 static char_u *
4361compile_break(char_u *arg, cctx_T *cctx)
4362{
4363 scope_T *scope = cctx->ctx_scope;
4364 endlabel_T **el;
4365
4366 for (;;)
4367 {
4368 if (scope == NULL)
4369 {
4370 emsg(_(e_break));
4371 return NULL;
4372 }
4373 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4374 break;
4375 scope = scope->se_outer;
4376 }
4377
4378 // Jump to the end of the FOR or WHILE loop.
4379 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004380 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004382 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4384 return FAIL;
4385
4386 return arg;
4387}
4388
4389/*
4390 * compile "{" start of block
4391 */
4392 static char_u *
4393compile_block(char_u *arg, cctx_T *cctx)
4394{
4395 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4396 return NULL;
4397 return skipwhite(arg + 1);
4398}
4399
4400/*
4401 * compile end of block: drop one scope
4402 */
4403 static void
4404compile_endblock(cctx_T *cctx)
4405{
4406 scope_T *scope = cctx->ctx_scope;
4407
4408 cctx->ctx_scope = scope->se_outer;
4409 cctx->ctx_locals.ga_len = scope->se_local_count;
4410 vim_free(scope);
4411}
4412
4413/*
4414 * compile "try"
4415 * Creates a new scope for the try-endtry, pointing to the first catch and
4416 * finally.
4417 * Creates another scope for the "try" block itself.
4418 * TRY instruction sets up exception handling at runtime.
4419 *
4420 * "try"
4421 * TRY -> catch1, -> finally push trystack entry
4422 * ... try block
4423 * "throw {exception}"
4424 * EVAL {exception}
4425 * THROW create exception
4426 * ... try block
4427 * " catch {expr}"
4428 * JUMP -> finally
4429 * catch1: PUSH exeception
4430 * EVAL {expr}
4431 * MATCH
4432 * JUMP nomatch -> catch2
4433 * CATCH remove exception
4434 * ... catch block
4435 * " catch"
4436 * JUMP -> finally
4437 * catch2: CATCH remove exception
4438 * ... catch block
4439 * " finally"
4440 * finally:
4441 * ... finally block
4442 * " endtry"
4443 * ENDTRY pop trystack entry, may rethrow
4444 */
4445 static char_u *
4446compile_try(char_u *arg, cctx_T *cctx)
4447{
4448 garray_T *instr = &cctx->ctx_instr;
4449 scope_T *try_scope;
4450 scope_T *scope;
4451
4452 // scope that holds the jumps that go to catch/finally/endtry
4453 try_scope = new_scope(cctx, TRY_SCOPE);
4454 if (try_scope == NULL)
4455 return NULL;
4456
4457 // "catch" is set when the first ":catch" is found.
4458 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004459 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 if (generate_instr(cctx, ISN_TRY) == NULL)
4461 return NULL;
4462
4463 // scope for the try block itself
4464 scope = new_scope(cctx, BLOCK_SCOPE);
4465 if (scope == NULL)
4466 return NULL;
4467
4468 return arg;
4469}
4470
4471/*
4472 * compile "catch {expr}"
4473 */
4474 static char_u *
4475compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4476{
4477 scope_T *scope = cctx->ctx_scope;
4478 garray_T *instr = &cctx->ctx_instr;
4479 char_u *p;
4480 isn_T *isn;
4481
4482 // end block scope from :try or :catch
4483 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4484 compile_endblock(cctx);
4485 scope = cctx->ctx_scope;
4486
4487 // Error if not in a :try scope
4488 if (scope == NULL || scope->se_type != TRY_SCOPE)
4489 {
4490 emsg(_(e_catch));
4491 return NULL;
4492 }
4493
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004494 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 {
4496 emsg(_("E1033: catch unreachable after catch-all"));
4497 return NULL;
4498 }
4499
4500 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004501 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 JUMP_ALWAYS, cctx) == FAIL)
4503 return NULL;
4504
4505 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004506 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 if (isn->isn_arg.try.try_catch == 0)
4508 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004509 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 {
4511 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004512 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 isn->isn_arg.jump.jump_where = instr->ga_len;
4514 }
4515
4516 p = skipwhite(arg);
4517 if (ends_excmd(*p))
4518 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004519 scope->se_u.se_try.ts_caught_all = TRUE;
4520 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 }
4522 else
4523 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004524 char_u *end;
4525 char_u *pat;
4526 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004527 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 // Push v:exception, push {expr} and MATCH
4530 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4531
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004532 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4533 if (*end != *p)
4534 {
4535 semsg(_("E1067: Separator mismatch: %s"), p);
4536 vim_free(tofree);
4537 return FAIL;
4538 }
4539 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004540 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004541 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004542 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004543 pat = vim_strnsave(p + 1, len);
4544 vim_free(tofree);
4545 p += len + 2;
4546 if (pat == NULL)
4547 return FAIL;
4548 if (generate_PUSHS(cctx, pat) == FAIL)
4549 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4552 return NULL;
4553
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004554 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4556 return NULL;
4557 }
4558
4559 if (generate_instr(cctx, ISN_CATCH) == NULL)
4560 return NULL;
4561
4562 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4563 return NULL;
4564 return p;
4565}
4566
4567 static char_u *
4568compile_finally(char_u *arg, cctx_T *cctx)
4569{
4570 scope_T *scope = cctx->ctx_scope;
4571 garray_T *instr = &cctx->ctx_instr;
4572 isn_T *isn;
4573
4574 // end block scope from :try or :catch
4575 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4576 compile_endblock(cctx);
4577 scope = cctx->ctx_scope;
4578
4579 // Error if not in a :try scope
4580 if (scope == NULL || scope->se_type != TRY_SCOPE)
4581 {
4582 emsg(_(e_finally));
4583 return NULL;
4584 }
4585
4586 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004587 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 if (isn->isn_arg.try.try_finally != 0)
4589 {
4590 emsg(_(e_finally_dup));
4591 return NULL;
4592 }
4593
4594 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004595 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004597 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 {
4599 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004600 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 isn->isn_arg.jump.jump_where = instr->ga_len;
4602 }
4603
4604 isn->isn_arg.try.try_finally = instr->ga_len;
4605 // TODO: set index in ts_finally_label jumps
4606
4607 return arg;
4608}
4609
4610 static char_u *
4611compile_endtry(char_u *arg, cctx_T *cctx)
4612{
4613 scope_T *scope = cctx->ctx_scope;
4614 garray_T *instr = &cctx->ctx_instr;
4615 isn_T *isn;
4616
4617 // end block scope from :catch or :finally
4618 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4619 compile_endblock(cctx);
4620 scope = cctx->ctx_scope;
4621
4622 // Error if not in a :try scope
4623 if (scope == NULL || scope->se_type != TRY_SCOPE)
4624 {
4625 if (scope == NULL)
4626 emsg(_(e_no_endtry));
4627 else if (scope->se_type == WHILE_SCOPE)
4628 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004629 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 emsg(_(e_endfor));
4631 else
4632 emsg(_(e_endif));
4633 return NULL;
4634 }
4635
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004636 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4638 {
4639 emsg(_("E1032: missing :catch or :finally"));
4640 return NULL;
4641 }
4642
4643 // Fill in the "end" label in jumps at the end of the blocks, if not done
4644 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004645 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646
4647 // End :catch or :finally scope: set value in ISN_TRY instruction
4648 if (isn->isn_arg.try.try_finally == 0)
4649 isn->isn_arg.try.try_finally = instr->ga_len;
4650 compile_endblock(cctx);
4651
4652 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4653 return NULL;
4654 return arg;
4655}
4656
4657/*
4658 * compile "throw {expr}"
4659 */
4660 static char_u *
4661compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4662{
4663 char_u *p = skipwhite(arg);
4664
4665 if (ends_excmd(*p))
4666 {
4667 emsg(_(e_argreq));
4668 return NULL;
4669 }
4670 if (compile_expr1(&p, cctx) == FAIL)
4671 return NULL;
4672 if (may_generate_2STRING(-1, cctx) == FAIL)
4673 return NULL;
4674 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4675 return NULL;
4676
4677 return p;
4678}
4679
4680/*
4681 * compile "echo expr"
4682 */
4683 static char_u *
4684compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4685{
4686 char_u *p = arg;
4687 int count = 0;
4688
Bram Moolenaarad39c092020-02-26 18:23:43 +01004689 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 {
4691 if (compile_expr1(&p, cctx) == FAIL)
4692 return NULL;
4693 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004694 p = skipwhite(p);
4695 if (ends_excmd(*p))
4696 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 }
4698
4699 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004700 return p;
4701}
4702
4703/*
4704 * compile "execute expr"
4705 */
4706 static char_u *
4707compile_execute(char_u *arg, cctx_T *cctx)
4708{
4709 char_u *p = arg;
4710 int count = 0;
4711
4712 for (;;)
4713 {
4714 if (compile_expr1(&p, cctx) == FAIL)
4715 return NULL;
4716 ++count;
4717 p = skipwhite(p);
4718 if (ends_excmd(*p))
4719 break;
4720 }
4721
4722 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
4724 return p;
4725}
4726
4727/*
4728 * After ex_function() has collected all the function lines: parse and compile
4729 * the lines into instructions.
4730 * Adds the function to "def_functions".
4731 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4732 * return statement (used for lambda).
4733 */
4734 void
4735compile_def_function(ufunc_T *ufunc, int set_return_type)
4736{
4737 dfunc_T *dfunc;
4738 char_u *line = NULL;
4739 char_u *p;
4740 exarg_T ea;
4741 char *errormsg = NULL; // error message
4742 int had_return = FALSE;
4743 cctx_T cctx;
4744 garray_T *instr;
4745 int called_emsg_before = called_emsg;
4746 int ret = FAIL;
4747 sctx_T save_current_sctx = current_sctx;
4748
4749 if (ufunc->uf_dfunc_idx >= 0)
4750 {
4751 // redefining a function that was compiled before
4752 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4753 dfunc->df_deleted = FALSE;
4754 }
4755 else
4756 {
4757 // Add the function to "def_functions".
4758 if (ga_grow(&def_functions, 1) == FAIL)
4759 return;
4760 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4761 vim_memset(dfunc, 0, sizeof(dfunc_T));
4762 dfunc->df_idx = def_functions.ga_len;
4763 ufunc->uf_dfunc_idx = dfunc->df_idx;
4764 dfunc->df_ufunc = ufunc;
4765 ++def_functions.ga_len;
4766 }
4767
4768 vim_memset(&cctx, 0, sizeof(cctx));
4769 cctx.ctx_ufunc = ufunc;
4770 cctx.ctx_lnum = -1;
4771 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4772 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4773 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4774 cctx.ctx_type_list = &ufunc->uf_type_list;
4775 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4776 instr = &cctx.ctx_instr;
4777
4778 // Most modern script version.
4779 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4780
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004781 if (ufunc->uf_def_args.ga_len > 0)
4782 {
4783 int count = ufunc->uf_def_args.ga_len;
4784 int i;
4785 char_u *arg;
4786 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4787
4788 // Produce instructions for the default values of optional arguments.
4789 // Store the instruction index in uf_def_arg_idx[] so that we know
4790 // where to start when the function is called, depending on the number
4791 // of arguments.
4792 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4793 if (ufunc->uf_def_arg_idx == NULL)
4794 goto erret;
4795 for (i = 0; i < count; ++i)
4796 {
4797 ufunc->uf_def_arg_idx[i] = instr->ga_len;
4798 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4799 if (compile_expr1(&arg, &cctx) == FAIL
4800 || generate_STORE(&cctx, ISN_STORE,
4801 i - count - off, NULL) == FAIL)
4802 goto erret;
4803 }
4804
4805 // If a varargs is following, push an empty list.
4806 if (ufunc->uf_va_name != NULL)
4807 {
4808 if (generate_NEWLIST(&cctx, 0) == FAIL
4809 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4810 goto erret;
4811 }
4812
4813 ufunc->uf_def_arg_idx[count] = instr->ga_len;
4814 }
4815
4816 /*
4817 * Loop over all the lines of the function and generate instructions.
4818 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 for (;;)
4820 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004821 int is_ex_command;
4822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 if (line != NULL && *line == '|')
4824 // the line continues after a '|'
4825 ++line;
4826 else if (line != NULL && *line != NUL)
4827 {
4828 semsg(_("E488: Trailing characters: %s"), line);
4829 goto erret;
4830 }
4831 else
4832 {
4833 do
4834 {
4835 ++cctx.ctx_lnum;
4836 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4837 break;
4838 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4839 } while (line == NULL);
4840 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4841 break;
4842 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4843 }
4844
4845 had_return = FALSE;
4846 vim_memset(&ea, 0, sizeof(ea));
4847 ea.cmdlinep = &line;
4848 ea.cmd = skipwhite(line);
4849
4850 // "}" ends a block scope
4851 if (*ea.cmd == '}')
4852 {
4853 scopetype_T stype = cctx.ctx_scope == NULL
4854 ? NO_SCOPE : cctx.ctx_scope->se_type;
4855
4856 if (stype == BLOCK_SCOPE)
4857 {
4858 compile_endblock(&cctx);
4859 line = ea.cmd;
4860 }
4861 else
4862 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004863 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 goto erret;
4865 }
4866 if (line != NULL)
4867 line = skipwhite(ea.cmd + 1);
4868 continue;
4869 }
4870
4871 // "{" starts a block scope
4872 if (*ea.cmd == '{')
4873 {
4874 line = compile_block(ea.cmd, &cctx);
4875 continue;
4876 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004877 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
4879 /*
4880 * COMMAND MODIFIERS
4881 */
4882 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4883 {
4884 if (errormsg != NULL)
4885 goto erret;
4886 // empty line or comment
4887 line = (char_u *)"";
4888 continue;
4889 }
4890
4891 // Skip ":call" to get to the function name.
4892 if (checkforcmd(&ea.cmd, "call", 3))
4893 ea.cmd = skipwhite(ea.cmd);
4894
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004895 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004897 // Assuming the command starts with a variable or function name,
4898 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
4899 // val".
4900 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
4901 ? ea.cmd + 1 : ea.cmd;
4902 p = to_name_end(p);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01004903 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004905 int oplen;
4906 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004908 oplen = assignment_len(skipwhite(p), &heredoc);
4909 if (oplen > 0)
4910 {
4911 // Recognize an assignment if we recognize the variable
4912 // name:
4913 // "g:var = expr"
4914 // "var = expr" where "var" is a local var name.
4915 // "&opt = expr"
4916 // "$ENV = expr"
4917 // "@r = expr"
4918 if (*ea.cmd == '&'
4919 || *ea.cmd == '$'
4920 || *ea.cmd == '@'
4921 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4922 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4923 || lookup_script(ea.cmd, p - ea.cmd) == OK
4924 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
4925 {
4926 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4927 if (line == NULL)
4928 goto erret;
4929 continue;
4930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 }
4932 }
4933 }
4934
4935 /*
4936 * COMMAND after range
4937 */
4938 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004939 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
4940 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941
4942 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4943 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004944 if (cctx.ctx_skip == TRUE)
4945 {
4946 line += STRLEN(line);
4947 continue;
4948 }
4949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 // Expression or function call.
4951 if (ea.cmdidx == CMD_eval)
4952 {
4953 p = ea.cmd;
4954 if (compile_expr1(&p, &cctx) == FAIL)
4955 goto erret;
4956
4957 // drop the return value
4958 generate_instr_drop(&cctx, ISN_DROP, 1);
4959 line = p;
4960 continue;
4961 }
4962 if (ea.cmdidx == CMD_let)
4963 {
4964 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4965 if (line == NULL)
4966 goto erret;
4967 continue;
4968 }
4969 iemsg("Command from find_ex_command() not handled");
4970 goto erret;
4971 }
4972
4973 p = skipwhite(p);
4974
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004975 if (cctx.ctx_skip == TRUE
4976 && ea.cmdidx != CMD_elseif
4977 && ea.cmdidx != CMD_else
4978 && ea.cmdidx != CMD_endif)
4979 {
4980 line += STRLEN(line);
4981 continue;
4982 }
4983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 switch (ea.cmdidx)
4985 {
4986 case CMD_def:
4987 case CMD_function:
4988 // TODO: Nested function
4989 emsg("Nested function not implemented yet");
4990 goto erret;
4991
4992 case CMD_return:
4993 line = compile_return(p, set_return_type, &cctx);
4994 had_return = TRUE;
4995 break;
4996
4997 case CMD_let:
4998 case CMD_const:
4999 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5000 break;
5001
5002 case CMD_import:
5003 line = compile_import(p, &cctx);
5004 break;
5005
5006 case CMD_if:
5007 line = compile_if(p, &cctx);
5008 break;
5009 case CMD_elseif:
5010 line = compile_elseif(p, &cctx);
5011 break;
5012 case CMD_else:
5013 line = compile_else(p, &cctx);
5014 break;
5015 case CMD_endif:
5016 line = compile_endif(p, &cctx);
5017 break;
5018
5019 case CMD_while:
5020 line = compile_while(p, &cctx);
5021 break;
5022 case CMD_endwhile:
5023 line = compile_endwhile(p, &cctx);
5024 break;
5025
5026 case CMD_for:
5027 line = compile_for(p, &cctx);
5028 break;
5029 case CMD_endfor:
5030 line = compile_endfor(p, &cctx);
5031 break;
5032 case CMD_continue:
5033 line = compile_continue(p, &cctx);
5034 break;
5035 case CMD_break:
5036 line = compile_break(p, &cctx);
5037 break;
5038
5039 case CMD_try:
5040 line = compile_try(p, &cctx);
5041 break;
5042 case CMD_catch:
5043 line = compile_catch(p, &cctx);
5044 break;
5045 case CMD_finally:
5046 line = compile_finally(p, &cctx);
5047 break;
5048 case CMD_endtry:
5049 line = compile_endtry(p, &cctx);
5050 break;
5051 case CMD_throw:
5052 line = compile_throw(p, &cctx);
5053 break;
5054
5055 case CMD_echo:
5056 line = compile_echo(p, TRUE, &cctx);
5057 break;
5058 case CMD_echon:
5059 line = compile_echo(p, FALSE, &cctx);
5060 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005061 case CMD_execute:
5062 line = compile_execute(p, &cctx);
5063 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064
5065 default:
5066 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005067 // TODO:
5068 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005069 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 generate_EXEC(&cctx, line);
5071 line = (char_u *)"";
5072 break;
5073 }
5074 if (line == NULL)
5075 goto erret;
5076
5077 if (cctx.ctx_type_stack.ga_len < 0)
5078 {
5079 iemsg("Type stack underflow");
5080 goto erret;
5081 }
5082 }
5083
5084 if (cctx.ctx_scope != NULL)
5085 {
5086 if (cctx.ctx_scope->se_type == IF_SCOPE)
5087 emsg(_(e_endif));
5088 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5089 emsg(_(e_endwhile));
5090 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5091 emsg(_(e_endfor));
5092 else
5093 emsg(_("E1026: Missing }"));
5094 goto erret;
5095 }
5096
5097 if (!had_return)
5098 {
5099 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5100 {
5101 emsg(_("E1027: Missing return statement"));
5102 goto erret;
5103 }
5104
5105 // Return zero if there is no return at the end.
5106 generate_PUSHNR(&cctx, 0);
5107 generate_instr(&cctx, ISN_RETURN);
5108 }
5109
5110 dfunc->df_instr = instr->ga_data;
5111 dfunc->df_instr_count = instr->ga_len;
5112 dfunc->df_varcount = cctx.ctx_max_local;
5113
5114 ret = OK;
5115
5116erret:
5117 if (ret == FAIL)
5118 {
5119 ga_clear(instr);
5120 ufunc->uf_dfunc_idx = -1;
5121 --def_functions.ga_len;
5122 if (errormsg != NULL)
5123 emsg(errormsg);
5124 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005125 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
5127 // don't execute this function body
5128 ufunc->uf_lines.ga_len = 0;
5129 }
5130
5131 current_sctx = save_current_sctx;
5132 ga_clear(&cctx.ctx_type_stack);
5133 ga_clear(&cctx.ctx_locals);
5134}
5135
5136/*
5137 * Delete an instruction, free what it contains.
5138 */
5139 static void
5140delete_instr(isn_T *isn)
5141{
5142 switch (isn->isn_type)
5143 {
5144 case ISN_EXEC:
5145 case ISN_LOADENV:
5146 case ISN_LOADG:
5147 case ISN_LOADOPT:
5148 case ISN_MEMBER:
5149 case ISN_PUSHEXC:
5150 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005151 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 case ISN_STOREG:
5153 vim_free(isn->isn_arg.string);
5154 break;
5155
5156 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005157 case ISN_STORES:
5158 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 break;
5160
5161 case ISN_STOREOPT:
5162 vim_free(isn->isn_arg.storeopt.so_name);
5163 break;
5164
5165 case ISN_PUSHBLOB: // push blob isn_arg.blob
5166 blob_unref(isn->isn_arg.blob);
5167 break;
5168
5169 case ISN_UCALL:
5170 vim_free(isn->isn_arg.ufunc.cuf_name);
5171 break;
5172
5173 case ISN_2BOOL:
5174 case ISN_2STRING:
5175 case ISN_ADDBLOB:
5176 case ISN_ADDLIST:
5177 case ISN_BCALL:
5178 case ISN_CATCH:
5179 case ISN_CHECKNR:
5180 case ISN_CHECKTYPE:
5181 case ISN_COMPAREANY:
5182 case ISN_COMPAREBLOB:
5183 case ISN_COMPAREBOOL:
5184 case ISN_COMPAREDICT:
5185 case ISN_COMPAREFLOAT:
5186 case ISN_COMPAREFUNC:
5187 case ISN_COMPARELIST:
5188 case ISN_COMPARENR:
5189 case ISN_COMPAREPARTIAL:
5190 case ISN_COMPARESPECIAL:
5191 case ISN_COMPARESTRING:
5192 case ISN_CONCAT:
5193 case ISN_DCALL:
5194 case ISN_DROP:
5195 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005196 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 case ISN_ENDTRY:
5198 case ISN_FOR:
5199 case ISN_FUNCREF:
5200 case ISN_INDEX:
5201 case ISN_JUMP:
5202 case ISN_LOAD:
5203 case ISN_LOADSCRIPT:
5204 case ISN_LOADREG:
5205 case ISN_LOADV:
5206 case ISN_NEGATENR:
5207 case ISN_NEWDICT:
5208 case ISN_NEWLIST:
5209 case ISN_OPNR:
5210 case ISN_OPFLOAT:
5211 case ISN_OPANY:
5212 case ISN_PCALL:
5213 case ISN_PUSHF:
5214 case ISN_PUSHNR:
5215 case ISN_PUSHBOOL:
5216 case ISN_PUSHSPEC:
5217 case ISN_RETURN:
5218 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005219 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005221 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 case ISN_STORESCRIPT:
5223 case ISN_THROW:
5224 case ISN_TRY:
5225 // nothing allocated
5226 break;
5227 }
5228}
5229
5230/*
5231 * When a user function is deleted, delete any associated def function.
5232 */
5233 void
5234delete_def_function(ufunc_T *ufunc)
5235{
5236 int idx;
5237
5238 if (ufunc->uf_dfunc_idx >= 0)
5239 {
5240 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5241 + ufunc->uf_dfunc_idx;
5242 ga_clear(&dfunc->df_def_args_isn);
5243
5244 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5245 delete_instr(dfunc->df_instr + idx);
5246 VIM_CLEAR(dfunc->df_instr);
5247
5248 dfunc->df_deleted = TRUE;
5249 }
5250}
5251
5252#if defined(EXITFREE) || defined(PROTO)
5253 void
5254free_def_functions(void)
5255{
5256 vim_free(def_functions.ga_data);
5257}
5258#endif
5259
5260
5261#endif // FEAT_EVAL