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