blob: c9f72f52b320d7a2b254c69a1c82a07c9ee85df2 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100118 int ctx_skip; // when TRUE skip commands, when FALSE skip
119 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 scope_T *ctx_scope; // current scope, NULL at toplevel
121
122 garray_T ctx_type_stack; // type of each item on the stack
123 garray_T *ctx_type_list; // space for adding types
124};
125
126static char e_var_notfound[] = N_("E1001: variable not found: %s");
127static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128
129static int compile_expr1(char_u **arg, cctx_T *cctx);
130static int compile_expr2(char_u **arg, cctx_T *cctx);
131static int compile_expr3(char_u **arg, cctx_T *cctx);
132
133/*
134 * Lookup variable "name" in the local scope and return the index.
135 */
136 static int
137lookup_local(char_u *name, size_t len, cctx_T *cctx)
138{
139 int idx;
140
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100141 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 return -1;
143 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
144 {
145 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
146
147 if (STRNCMP(name, lvar->lv_name, len) == 0
148 && STRLEN(lvar->lv_name) == len)
149 return idx;
150 }
151 return -1;
152}
153
154/*
155 * Lookup an argument in the current function.
156 * Returns the argument index or -1 if not found.
157 */
158 static int
159lookup_arg(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
162
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100163 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 return -1;
165 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
166 {
167 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
168
169 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
170 return idx;
171 }
172 return -1;
173}
174
175/*
176 * Lookup a vararg argument in the current function.
177 * Returns TRUE if there is a match.
178 */
179 static int
180lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
181{
182 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
183
184 return len > 0 && va_name != NULL
185 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
186}
187
188/*
189 * Lookup a variable in the current script.
190 * Returns OK or FAIL.
191 */
192 static int
193lookup_script(char_u *name, size_t len)
194{
195 int cc;
196 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
197 dictitem_T *di;
198
199 cc = name[len];
200 name[len] = NUL;
201 di = find_var_in_ht(ht, 0, name, TRUE);
202 name[len] = cc;
203 return di == NULL ? FAIL: OK;
204}
205
206 static type_T *
207get_list_type(type_T *member_type, garray_T *type_list)
208{
209 type_T *type;
210
211 // recognize commonly used types
212 if (member_type->tt_type == VAR_UNKNOWN)
213 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100214 if (member_type->tt_type == VAR_VOID)
215 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100216 if (member_type->tt_type == VAR_BOOL)
217 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 if (member_type->tt_type == VAR_NUMBER)
219 return &t_list_number;
220 if (member_type->tt_type == VAR_STRING)
221 return &t_list_string;
222
223 // Not a common type, create a new entry.
224 if (ga_grow(type_list, 1) == FAIL)
225 return FAIL;
226 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
227 ++type_list->ga_len;
228 type->tt_type = VAR_LIST;
229 type->tt_member = member_type;
230 return type;
231}
232
233 static type_T *
234get_dict_type(type_T *member_type, garray_T *type_list)
235{
236 type_T *type;
237
238 // recognize commonly used types
239 if (member_type->tt_type == VAR_UNKNOWN)
240 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100241 if (member_type->tt_type == VAR_VOID)
242 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100243 if (member_type->tt_type == VAR_BOOL)
244 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 if (member_type->tt_type == VAR_NUMBER)
246 return &t_dict_number;
247 if (member_type->tt_type == VAR_STRING)
248 return &t_dict_string;
249
250 // Not a common type, create a new entry.
251 if (ga_grow(type_list, 1) == FAIL)
252 return FAIL;
253 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
254 ++type_list->ga_len;
255 type->tt_type = VAR_DICT;
256 type->tt_member = member_type;
257 return type;
258}
259
260/////////////////////////////////////////////////////////////////////
261// Following generate_ functions expect the caller to call ga_grow().
262
Bram Moolenaar080457c2020-03-03 21:53:32 +0100263#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
264#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266/*
267 * Generate an instruction without arguments.
268 * Returns a pointer to the new instruction, NULL if failed.
269 */
270 static isn_T *
271generate_instr(cctx_T *cctx, isntype_T isn_type)
272{
273 garray_T *instr = &cctx->ctx_instr;
274 isn_T *isn;
275
Bram Moolenaar080457c2020-03-03 21:53:32 +0100276 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (ga_grow(instr, 1) == FAIL)
278 return NULL;
279 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
280 isn->isn_type = isn_type;
281 isn->isn_lnum = cctx->ctx_lnum + 1;
282 ++instr->ga_len;
283
284 return isn;
285}
286
287/*
288 * Generate an instruction without arguments.
289 * "drop" will be removed from the stack.
290 * Returns a pointer to the new instruction, NULL if failed.
291 */
292 static isn_T *
293generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
294{
295 garray_T *stack = &cctx->ctx_type_stack;
296
Bram Moolenaar080457c2020-03-03 21:53:32 +0100297 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 stack->ga_len -= drop;
299 return generate_instr(cctx, isn_type);
300}
301
302/*
303 * Generate instruction "isn_type" and put "type" on the type stack.
304 */
305 static isn_T *
306generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
307{
308 isn_T *isn;
309 garray_T *stack = &cctx->ctx_type_stack;
310
311 if ((isn = generate_instr(cctx, isn_type)) == NULL)
312 return NULL;
313
314 if (ga_grow(stack, 1) == FAIL)
315 return NULL;
316 ((type_T **)stack->ga_data)[stack->ga_len] = type;
317 ++stack->ga_len;
318
319 return isn;
320}
321
322/*
323 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
324 */
325 static int
326may_generate_2STRING(int offset, cctx_T *cctx)
327{
328 isn_T *isn;
329 garray_T *stack = &cctx->ctx_type_stack;
330 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
331
332 if ((*type)->tt_type == VAR_STRING)
333 return OK;
334 *type = &t_string;
335
336 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
337 return FAIL;
338 isn->isn_arg.number = offset;
339
340 return OK;
341}
342
343 static int
344check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
345{
346 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
347 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
348 || type2 == VAR_UNKNOWN)))
349 {
350 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100351 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 else
353 semsg(_("E1036: %c requires number or float arguments"), *op);
354 return FAIL;
355 }
356 return OK;
357}
358
359/*
360 * Generate an instruction with two arguments. The instruction depends on the
361 * type of the arguments.
362 */
363 static int
364generate_two_op(cctx_T *cctx, char_u *op)
365{
366 garray_T *stack = &cctx->ctx_type_stack;
367 type_T *type1;
368 type_T *type2;
369 vartype_T vartype;
370 isn_T *isn;
371
Bram Moolenaar080457c2020-03-03 21:53:32 +0100372 RETURN_OK_IF_SKIP(cctx);
373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 // Get the known type of the two items on the stack. If they are matching
375 // use a type-specific instruction. Otherwise fall back to runtime type
376 // checking.
377 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
378 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
379 vartype = VAR_UNKNOWN;
380 if (type1->tt_type == type2->tt_type
381 && (type1->tt_type == VAR_NUMBER
382 || type1->tt_type == VAR_LIST
383#ifdef FEAT_FLOAT
384 || type1->tt_type == VAR_FLOAT
385#endif
386 || type1->tt_type == VAR_BLOB))
387 vartype = type1->tt_type;
388
389 switch (*op)
390 {
391 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100392 && type1->tt_type != VAR_UNKNOWN
393 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 && check_number_or_float(
395 type1->tt_type, type2->tt_type, op) == FAIL)
396 return FAIL;
397 isn = generate_instr_drop(cctx,
398 vartype == VAR_NUMBER ? ISN_OPNR
399 : vartype == VAR_LIST ? ISN_ADDLIST
400 : vartype == VAR_BLOB ? ISN_ADDBLOB
401#ifdef FEAT_FLOAT
402 : vartype == VAR_FLOAT ? ISN_OPFLOAT
403#endif
404 : ISN_OPANY, 1);
405 if (isn != NULL)
406 isn->isn_arg.op.op_type = EXPR_ADD;
407 break;
408
409 case '-':
410 case '*':
411 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
412 op) == FAIL)
413 return FAIL;
414 if (vartype == VAR_NUMBER)
415 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
416#ifdef FEAT_FLOAT
417 else if (vartype == VAR_FLOAT)
418 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
419#endif
420 else
421 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
422 if (isn != NULL)
423 isn->isn_arg.op.op_type = *op == '*'
424 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
425 break;
426
427 case '%': if ((type1->tt_type != VAR_UNKNOWN
428 && type1->tt_type != VAR_NUMBER)
429 || (type2->tt_type != VAR_UNKNOWN
430 && type2->tt_type != VAR_NUMBER))
431 {
432 emsg(_("E1035: % requires number arguments"));
433 return FAIL;
434 }
435 isn = generate_instr_drop(cctx,
436 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
437 if (isn != NULL)
438 isn->isn_arg.op.op_type = EXPR_REM;
439 break;
440 }
441
442 // correct type of result
443 if (vartype == VAR_UNKNOWN)
444 {
445 type_T *type = &t_any;
446
447#ifdef FEAT_FLOAT
448 // float+number and number+float results in float
449 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
450 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
451 type = &t_float;
452#endif
453 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
454 }
455
456 return OK;
457}
458
459/*
460 * Generate an ISN_COMPARE* instruction with a boolean result.
461 */
462 static int
463generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
464{
465 isntype_T isntype = ISN_DROP;
466 isn_T *isn;
467 garray_T *stack = &cctx->ctx_type_stack;
468 vartype_T type1;
469 vartype_T type2;
470
Bram Moolenaar080457c2020-03-03 21:53:32 +0100471 RETURN_OK_IF_SKIP(cctx);
472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 // Get the known type of the two items on the stack. If they are matching
474 // use a type-specific instruction. Otherwise fall back to runtime type
475 // checking.
476 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
477 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
478 if (type1 == type2)
479 {
480 switch (type1)
481 {
482 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
483 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
484 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
485 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
486 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
487 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
488 case VAR_LIST: isntype = ISN_COMPARELIST; break;
489 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
490 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
491 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
492 default: isntype = ISN_COMPAREANY; break;
493 }
494 }
495 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
496 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
497 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
498 isntype = ISN_COMPAREANY;
499
500 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
501 && (isntype == ISN_COMPAREBOOL
502 || isntype == ISN_COMPARESPECIAL
503 || isntype == ISN_COMPARENR
504 || isntype == ISN_COMPAREFLOAT))
505 {
506 semsg(_("E1037: Cannot use \"%s\" with %s"),
507 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
508 return FAIL;
509 }
510 if (isntype == ISN_DROP
511 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
512 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
513 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
514 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
515 && exptype != EXPR_IS && exptype != EXPR_ISNOT
516 && (type1 == VAR_BLOB || type2 == VAR_BLOB
517 || type1 == VAR_LIST || type2 == VAR_LIST))))
518 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100519 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 vartype_name(type1), vartype_name(type2));
521 return FAIL;
522 }
523
524 if ((isn = generate_instr(cctx, isntype)) == NULL)
525 return FAIL;
526 isn->isn_arg.op.op_type = exptype;
527 isn->isn_arg.op.op_ic = ic;
528
529 // takes two arguments, puts one bool back
530 if (stack->ga_len >= 2)
531 {
532 --stack->ga_len;
533 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
534 }
535
536 return OK;
537}
538
539/*
540 * Generate an ISN_2BOOL instruction.
541 */
542 static int
543generate_2BOOL(cctx_T *cctx, int invert)
544{
545 isn_T *isn;
546 garray_T *stack = &cctx->ctx_type_stack;
547
Bram Moolenaar080457c2020-03-03 21:53:32 +0100548 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
550 return FAIL;
551 isn->isn_arg.number = invert;
552
553 // type becomes bool
554 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
555
556 return OK;
557}
558
559 static int
560generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
561{
562 isn_T *isn;
563 garray_T *stack = &cctx->ctx_type_stack;
564
Bram Moolenaar080457c2020-03-03 21:53:32 +0100565 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
567 return FAIL;
568 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
569 isn->isn_arg.type.ct_off = offset;
570
571 // type becomes vartype
572 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
573
574 return OK;
575}
576
577/*
578 * Generate an ISN_PUSHNR instruction.
579 */
580 static int
581generate_PUSHNR(cctx_T *cctx, varnumber_T number)
582{
583 isn_T *isn;
584
Bram Moolenaar080457c2020-03-03 21:53:32 +0100585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
587 return FAIL;
588 isn->isn_arg.number = number;
589
590 return OK;
591}
592
593/*
594 * Generate an ISN_PUSHBOOL instruction.
595 */
596 static int
597generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
598{
599 isn_T *isn;
600
Bram Moolenaar080457c2020-03-03 21:53:32 +0100601 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
603 return FAIL;
604 isn->isn_arg.number = number;
605
606 return OK;
607}
608
609/*
610 * Generate an ISN_PUSHSPEC instruction.
611 */
612 static int
613generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
614{
615 isn_T *isn;
616
Bram Moolenaar080457c2020-03-03 21:53:32 +0100617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
619 return FAIL;
620 isn->isn_arg.number = number;
621
622 return OK;
623}
624
625#ifdef FEAT_FLOAT
626/*
627 * Generate an ISN_PUSHF instruction.
628 */
629 static int
630generate_PUSHF(cctx_T *cctx, float_T fnumber)
631{
632 isn_T *isn;
633
Bram Moolenaar080457c2020-03-03 21:53:32 +0100634 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
636 return FAIL;
637 isn->isn_arg.fnumber = fnumber;
638
639 return OK;
640}
641#endif
642
643/*
644 * Generate an ISN_PUSHS instruction.
645 * Consumes "str".
646 */
647 static int
648generate_PUSHS(cctx_T *cctx, char_u *str)
649{
650 isn_T *isn;
651
Bram Moolenaar080457c2020-03-03 21:53:32 +0100652 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
654 return FAIL;
655 isn->isn_arg.string = str;
656
657 return OK;
658}
659
660/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100661 * Generate an ISN_PUSHCHANNEL instruction.
662 * Consumes "channel".
663 */
664 static int
665generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
666{
667 isn_T *isn;
668
Bram Moolenaar080457c2020-03-03 21:53:32 +0100669 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100670 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
671 return FAIL;
672 isn->isn_arg.channel = channel;
673
674 return OK;
675}
676
677/*
678 * Generate an ISN_PUSHJOB instruction.
679 * Consumes "job".
680 */
681 static int
682generate_PUSHJOB(cctx_T *cctx, job_T *job)
683{
684 isn_T *isn;
685
Bram Moolenaar080457c2020-03-03 21:53:32 +0100686 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100687 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100688 return FAIL;
689 isn->isn_arg.job = job;
690
691 return OK;
692}
693
694/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 * Generate an ISN_PUSHBLOB instruction.
696 * Consumes "blob".
697 */
698 static int
699generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
700{
701 isn_T *isn;
702
Bram Moolenaar080457c2020-03-03 21:53:32 +0100703 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
705 return FAIL;
706 isn->isn_arg.blob = blob;
707
708 return OK;
709}
710
711/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100712 * Generate an ISN_PUSHFUNC instruction with name "name".
713 * Consumes "name".
714 */
715 static int
716generate_PUSHFUNC(cctx_T *cctx, char_u *name)
717{
718 isn_T *isn;
719
Bram Moolenaar080457c2020-03-03 21:53:32 +0100720 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100721 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
722 return FAIL;
723 isn->isn_arg.string = name;
724
725 return OK;
726}
727
728/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100729 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
730 * Consumes "name".
731 */
732 static int
733generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
734{
735 isn_T *isn;
736
Bram Moolenaar080457c2020-03-03 21:53:32 +0100737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100738 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
739 &t_partial_any)) == NULL)
740 return FAIL;
741 isn->isn_arg.partial = part;
742
743 return OK;
744}
745
746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 * Generate an ISN_STORE instruction.
748 */
749 static int
750generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
751{
752 isn_T *isn;
753
Bram Moolenaar080457c2020-03-03 21:53:32 +0100754 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
756 return FAIL;
757 if (name != NULL)
758 isn->isn_arg.string = vim_strsave(name);
759 else
760 isn->isn_arg.number = idx;
761
762 return OK;
763}
764
765/*
766 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
767 */
768 static int
769generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
770{
771 isn_T *isn;
772
Bram Moolenaar080457c2020-03-03 21:53:32 +0100773 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
775 return FAIL;
776 isn->isn_arg.storenr.str_idx = idx;
777 isn->isn_arg.storenr.str_val = value;
778
779 return OK;
780}
781
782/*
783 * Generate an ISN_STOREOPT instruction
784 */
785 static int
786generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
787{
788 isn_T *isn;
789
Bram Moolenaar080457c2020-03-03 21:53:32 +0100790 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
792 return FAIL;
793 isn->isn_arg.storeopt.so_name = vim_strsave(name);
794 isn->isn_arg.storeopt.so_flags = opt_flags;
795
796 return OK;
797}
798
799/*
800 * Generate an ISN_LOAD or similar instruction.
801 */
802 static int
803generate_LOAD(
804 cctx_T *cctx,
805 isntype_T isn_type,
806 int idx,
807 char_u *name,
808 type_T *type)
809{
810 isn_T *isn;
811
Bram Moolenaar080457c2020-03-03 21:53:32 +0100812 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
814 return FAIL;
815 if (name != NULL)
816 isn->isn_arg.string = vim_strsave(name);
817 else
818 isn->isn_arg.number = idx;
819
820 return OK;
821}
822
823/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100824 * Generate an ISN_LOADV instruction.
825 */
826 static int
827generate_LOADV(
828 cctx_T *cctx,
829 char_u *name,
830 int error)
831{
832 // load v:var
833 int vidx = find_vim_var(name);
834
Bram Moolenaar080457c2020-03-03 21:53:32 +0100835 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100836 if (vidx < 0)
837 {
838 if (error)
839 semsg(_(e_var_notfound), name);
840 return FAIL;
841 }
842
843 // TODO: get actual type
844 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
845}
846
847/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 * Generate an ISN_LOADS instruction.
849 */
850 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100851generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100853 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100855 int sid,
856 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857{
858 isn_T *isn;
859
Bram Moolenaar080457c2020-03-03 21:53:32 +0100860 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100861 if (isn_type == ISN_LOADS)
862 isn = generate_instr_type(cctx, isn_type, type);
863 else
864 isn = generate_instr_drop(cctx, isn_type, 1);
865 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100867 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
868 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
870 return OK;
871}
872
873/*
874 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
875 */
876 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100877generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 cctx_T *cctx,
879 isntype_T isn_type,
880 int sid,
881 int idx,
882 type_T *type)
883{
884 isn_T *isn;
885
Bram Moolenaar080457c2020-03-03 21:53:32 +0100886 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 if (isn_type == ISN_LOADSCRIPT)
888 isn = generate_instr_type(cctx, isn_type, type);
889 else
890 isn = generate_instr_drop(cctx, isn_type, 1);
891 if (isn == NULL)
892 return FAIL;
893 isn->isn_arg.script.script_sid = sid;
894 isn->isn_arg.script.script_idx = idx;
895 return OK;
896}
897
898/*
899 * Generate an ISN_NEWLIST instruction.
900 */
901 static int
902generate_NEWLIST(cctx_T *cctx, int count)
903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906 garray_T *type_list = cctx->ctx_type_list;
907 type_T *type;
908 type_T *member;
909
Bram Moolenaar080457c2020-03-03 21:53:32 +0100910 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
912 return FAIL;
913 isn->isn_arg.number = count;
914
915 // drop the value types
916 stack->ga_len -= count;
917
Bram Moolenaar436472f2020-02-20 22:54:43 +0100918 // Use the first value type for the list member type. Use "void" for an
919 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if (count > 0)
921 member = ((type_T **)stack->ga_data)[stack->ga_len];
922 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100923 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 type = get_list_type(member, type_list);
925
926 // add the list type to the type stack
927 if (ga_grow(stack, 1) == FAIL)
928 return FAIL;
929 ((type_T **)stack->ga_data)[stack->ga_len] = type;
930 ++stack->ga_len;
931
932 return OK;
933}
934
935/*
936 * Generate an ISN_NEWDICT instruction.
937 */
938 static int
939generate_NEWDICT(cctx_T *cctx, int count)
940{
941 isn_T *isn;
942 garray_T *stack = &cctx->ctx_type_stack;
943 garray_T *type_list = cctx->ctx_type_list;
944 type_T *type;
945 type_T *member;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = count;
951
952 // drop the key and value types
953 stack->ga_len -= 2 * count;
954
Bram Moolenaar436472f2020-02-20 22:54:43 +0100955 // Use the first value type for the list member type. Use "void" for an
956 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if (count > 0)
958 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
959 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100960 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 type = get_dict_type(member, type_list);
962
963 // add the dict type to the type stack
964 if (ga_grow(stack, 1) == FAIL)
965 return FAIL;
966 ((type_T **)stack->ga_data)[stack->ga_len] = type;
967 ++stack->ga_len;
968
969 return OK;
970}
971
972/*
973 * Generate an ISN_FUNCREF instruction.
974 */
975 static int
976generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
977{
978 isn_T *isn;
979 garray_T *stack = &cctx->ctx_type_stack;
980
Bram Moolenaar080457c2020-03-03 21:53:32 +0100981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
983 return FAIL;
984 isn->isn_arg.number = dfunc_idx;
985
986 if (ga_grow(stack, 1) == FAIL)
987 return FAIL;
988 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
989 // TODO: argument and return types
990 ++stack->ga_len;
991
992 return OK;
993}
994
995/*
996 * Generate an ISN_JUMP instruction.
997 */
998 static int
999generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1000{
1001 isn_T *isn;
1002 garray_T *stack = &cctx->ctx_type_stack;
1003
Bram Moolenaar080457c2020-03-03 21:53:32 +01001004 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.jump.jump_when = when;
1008 isn->isn_arg.jump.jump_where = where;
1009
1010 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1011 --stack->ga_len;
1012
1013 return OK;
1014}
1015
1016 static int
1017generate_FOR(cctx_T *cctx, int loop_idx)
1018{
1019 isn_T *isn;
1020 garray_T *stack = &cctx->ctx_type_stack;
1021
Bram Moolenaar080457c2020-03-03 21:53:32 +01001022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.forloop.for_idx = loop_idx;
1026
1027 if (ga_grow(stack, 1) == FAIL)
1028 return FAIL;
1029 // type doesn't matter, will be stored next
1030 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1031 ++stack->ga_len;
1032
1033 return OK;
1034}
1035
1036/*
1037 * Generate an ISN_BCALL instruction.
1038 * Return FAIL if the number of arguments is wrong.
1039 */
1040 static int
1041generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1042{
1043 isn_T *isn;
1044 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001045 type_T *argtypes[MAX_FUNC_ARGS];
1046 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 if (check_internal_func(func_idx, argcount) == FAIL)
1050 return FAIL;
1051
1052 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1053 return FAIL;
1054 isn->isn_arg.bfunc.cbf_idx = func_idx;
1055 isn->isn_arg.bfunc.cbf_argcount = argcount;
1056
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001057 for (i = 0; i < argcount; ++i)
1058 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 stack->ga_len -= argcount; // drop the arguments
1061 if (ga_grow(stack, 1) == FAIL)
1062 return FAIL;
1063 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001064 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 ++stack->ga_len; // add return value
1066
1067 return OK;
1068}
1069
1070/*
1071 * Generate an ISN_DCALL or ISN_UCALL instruction.
1072 * Return FAIL if the number of arguments is wrong.
1073 */
1074 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001075generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076{
1077 isn_T *isn;
1078 garray_T *stack = &cctx->ctx_type_stack;
1079 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001080 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 if (argcount > regular_args && !has_varargs(ufunc))
1084 {
1085 semsg(_(e_toomanyarg), ufunc->uf_name);
1086 return FAIL;
1087 }
1088 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1089 {
1090 semsg(_(e_toofewarg), ufunc->uf_name);
1091 return FAIL;
1092 }
1093
1094 // Turn varargs into a list.
1095 if (ufunc->uf_va_name != NULL)
1096 {
1097 int count = argcount - regular_args;
1098
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001099 // If count is negative an empty list will be added after evaluating
1100 // default values for missing optional arguments.
1101 if (count >= 0)
1102 {
1103 generate_NEWLIST(cctx, count);
1104 argcount = regular_args + 1;
1105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 }
1107
1108 if ((isn = generate_instr(cctx,
1109 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1110 return FAIL;
1111 if (ufunc->uf_dfunc_idx >= 0)
1112 {
1113 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1114 isn->isn_arg.dfunc.cdf_argcount = argcount;
1115 }
1116 else
1117 {
1118 // A user function may be deleted and redefined later, can't use the
1119 // ufunc pointer, need to look it up again at runtime.
1120 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1121 isn->isn_arg.ufunc.cuf_argcount = argcount;
1122 }
1123
1124 stack->ga_len -= argcount; // drop the arguments
1125 if (ga_grow(stack, 1) == FAIL)
1126 return FAIL;
1127 // add return value
1128 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1129 ++stack->ga_len;
1130
1131 return OK;
1132}
1133
1134/*
1135 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1136 */
1137 static int
1138generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1139{
1140 isn_T *isn;
1141 garray_T *stack = &cctx->ctx_type_stack;
1142
Bram Moolenaar080457c2020-03-03 21:53:32 +01001143 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1145 return FAIL;
1146 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1147 isn->isn_arg.ufunc.cuf_argcount = argcount;
1148
1149 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001150 if (ga_grow(stack, 1) == FAIL)
1151 return FAIL;
1152 // add return value
1153 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1154 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155
1156 return OK;
1157}
1158
1159/*
1160 * Generate an ISN_PCALL instruction.
1161 */
1162 static int
1163generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1164{
1165 isn_T *isn;
1166 garray_T *stack = &cctx->ctx_type_stack;
1167
Bram Moolenaar080457c2020-03-03 21:53:32 +01001168 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1170 return FAIL;
1171 isn->isn_arg.pfunc.cpf_top = at_top;
1172 isn->isn_arg.pfunc.cpf_argcount = argcount;
1173
1174 stack->ga_len -= argcount; // drop the arguments
1175
1176 // drop the funcref/partial, get back the return value
1177 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1178
1179 return OK;
1180}
1181
1182/*
1183 * Generate an ISN_MEMBER instruction.
1184 */
1185 static int
1186generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1187{
1188 isn_T *isn;
1189 garray_T *stack = &cctx->ctx_type_stack;
1190 type_T *type;
1191
Bram Moolenaar080457c2020-03-03 21:53:32 +01001192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1194 return FAIL;
1195 isn->isn_arg.string = vim_strnsave(name, (int)len);
1196
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001197 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001199 if (type->tt_type != VAR_DICT && type != &t_any)
1200 {
1201 emsg(_(e_dictreq));
1202 return FAIL;
1203 }
1204 // change dict type to dict member type
1205 if (type->tt_type == VAR_DICT)
1206 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207
1208 return OK;
1209}
1210
1211/*
1212 * Generate an ISN_ECHO instruction.
1213 */
1214 static int
1215generate_ECHO(cctx_T *cctx, int with_white, int count)
1216{
1217 isn_T *isn;
1218
Bram Moolenaar080457c2020-03-03 21:53:32 +01001219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1221 return FAIL;
1222 isn->isn_arg.echo.echo_with_white = with_white;
1223 isn->isn_arg.echo.echo_count = count;
1224
1225 return OK;
1226}
1227
Bram Moolenaarad39c092020-02-26 18:23:43 +01001228/*
1229 * Generate an ISN_EXECUTE instruction.
1230 */
1231 static int
1232generate_EXECUTE(cctx_T *cctx, int count)
1233{
1234 isn_T *isn;
1235
1236 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1237 return FAIL;
1238 isn->isn_arg.number = count;
1239
1240 return OK;
1241}
1242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 static int
1244generate_EXEC(cctx_T *cctx, char_u *line)
1245{
1246 isn_T *isn;
1247
Bram Moolenaar080457c2020-03-03 21:53:32 +01001248 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.string = vim_strsave(line);
1252 return OK;
1253}
1254
1255static char e_white_both[] =
1256 N_("E1004: white space required before and after '%s'");
1257
1258/*
1259 * Reserve space for a local variable.
1260 * Return the index or -1 if it failed.
1261 */
1262 static int
1263reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1264{
1265 int idx;
1266 lvar_T *lvar;
1267
1268 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1269 {
1270 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1271 return -1;
1272 }
1273
1274 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1275 return -1;
1276 idx = cctx->ctx_locals.ga_len;
1277 if (cctx->ctx_max_local < idx + 1)
1278 cctx->ctx_max_local = idx + 1;
1279 ++cctx->ctx_locals.ga_len;
1280
1281 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1282 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1283 lvar->lv_const = isConst;
1284 lvar->lv_type = type;
1285
1286 return idx;
1287}
1288
1289/*
1290 * Skip over a type definition and return a pointer to just after it.
1291 */
1292 char_u *
1293skip_type(char_u *start)
1294{
1295 char_u *p = start;
1296
1297 while (ASCII_ISALNUM(*p) || *p == '_')
1298 ++p;
1299
1300 // Skip over "<type>"; this is permissive about white space.
1301 if (*skipwhite(p) == '<')
1302 {
1303 p = skipwhite(p);
1304 p = skip_type(skipwhite(p + 1));
1305 p = skipwhite(p);
1306 if (*p == '>')
1307 ++p;
1308 }
1309 return p;
1310}
1311
1312/*
1313 * Parse the member type: "<type>" and return "type" with the member set.
1314 * Use "type_list" if a new type needs to be added.
1315 * Returns NULL in case of failure.
1316 */
1317 static type_T *
1318parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1319{
1320 type_T *member_type;
1321
1322 if (**arg != '<')
1323 {
1324 if (*skipwhite(*arg) == '<')
1325 emsg(_("E1007: No white space allowed before <"));
1326 else
1327 emsg(_("E1008: Missing <type>"));
1328 return NULL;
1329 }
1330 *arg = skipwhite(*arg + 1);
1331
1332 member_type = parse_type(arg, type_list);
1333 if (member_type == NULL)
1334 return NULL;
1335
1336 *arg = skipwhite(*arg);
1337 if (**arg != '>')
1338 {
1339 emsg(_("E1009: Missing > after type"));
1340 return NULL;
1341 }
1342 ++*arg;
1343
1344 if (type->tt_type == VAR_LIST)
1345 return get_list_type(member_type, type_list);
1346 return get_dict_type(member_type, type_list);
1347}
1348
1349/*
1350 * Parse a type at "arg" and advance over it.
1351 * Return NULL for failure.
1352 */
1353 type_T *
1354parse_type(char_u **arg, garray_T *type_list)
1355{
1356 char_u *p = *arg;
1357 size_t len;
1358
1359 // skip over the first word
1360 while (ASCII_ISALNUM(*p) || *p == '_')
1361 ++p;
1362 len = p - *arg;
1363
1364 switch (**arg)
1365 {
1366 case 'a':
1367 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1368 {
1369 *arg += len;
1370 return &t_any;
1371 }
1372 break;
1373 case 'b':
1374 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1375 {
1376 *arg += len;
1377 return &t_bool;
1378 }
1379 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1380 {
1381 *arg += len;
1382 return &t_blob;
1383 }
1384 break;
1385 case 'c':
1386 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1387 {
1388 *arg += len;
1389 return &t_channel;
1390 }
1391 break;
1392 case 'd':
1393 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1394 {
1395 *arg += len;
1396 return parse_type_member(arg, &t_dict_any, type_list);
1397 }
1398 break;
1399 case 'f':
1400 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1401 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001402#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 *arg += len;
1404 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001405#else
1406 emsg(_("E1055: This Vim is not compiled with float support"));
1407 return &t_any;
1408#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 }
1410 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1411 {
1412 *arg += len;
1413 // TODO: arguments and return type
1414 return &t_func_any;
1415 }
1416 break;
1417 case 'j':
1418 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1419 {
1420 *arg += len;
1421 return &t_job;
1422 }
1423 break;
1424 case 'l':
1425 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1426 {
1427 *arg += len;
1428 return parse_type_member(arg, &t_list_any, type_list);
1429 }
1430 break;
1431 case 'n':
1432 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1433 {
1434 *arg += len;
1435 return &t_number;
1436 }
1437 break;
1438 case 'p':
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001439 if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 {
1441 *arg += len;
1442 // TODO: arguments and return type
1443 return &t_partial_any;
1444 }
1445 break;
1446 case 's':
1447 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1448 {
1449 *arg += len;
1450 return &t_string;
1451 }
1452 break;
1453 case 'v':
1454 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1455 {
1456 *arg += len;
1457 return &t_void;
1458 }
1459 break;
1460 }
1461
1462 semsg(_("E1010: Type not recognized: %s"), *arg);
1463 return &t_any;
1464}
1465
1466/*
1467 * Check if "type1" and "type2" are exactly the same.
1468 */
1469 static int
1470equal_type(type_T *type1, type_T *type2)
1471{
1472 if (type1->tt_type != type2->tt_type)
1473 return FALSE;
1474 switch (type1->tt_type)
1475 {
1476 case VAR_VOID:
1477 case VAR_UNKNOWN:
1478 case VAR_SPECIAL:
1479 case VAR_BOOL:
1480 case VAR_NUMBER:
1481 case VAR_FLOAT:
1482 case VAR_STRING:
1483 case VAR_BLOB:
1484 case VAR_JOB:
1485 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001486 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 case VAR_LIST:
1488 case VAR_DICT:
1489 return equal_type(type1->tt_member, type2->tt_member);
1490 case VAR_FUNC:
1491 case VAR_PARTIAL:
1492 // TODO; check argument types.
1493 return equal_type(type1->tt_member, type2->tt_member)
1494 && type1->tt_argcount == type2->tt_argcount;
1495 }
1496 return TRUE;
1497}
1498
1499/*
1500 * Find the common type of "type1" and "type2" and put it in "dest".
1501 * "type2" and "dest" may be the same.
1502 */
1503 static void
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001504common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505{
1506 if (equal_type(type1, type2))
1507 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001508 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 return;
1510 }
1511
1512 if (type1->tt_type == type2->tt_type)
1513 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1515 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001516 type_T *common;
1517
1518 common_type(type1->tt_member, type2->tt_member, &common, type_list);
1519 if (type1->tt_type == VAR_LIST)
1520 *dest = get_list_type(common, type_list);
1521 else
1522 *dest = get_dict_type(common, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 return;
1524 }
1525 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001526 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 }
1528
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001529 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530}
1531
1532 char *
1533vartype_name(vartype_T type)
1534{
1535 switch (type)
1536 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001537 case VAR_UNKNOWN: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 case VAR_SPECIAL: return "special";
1540 case VAR_BOOL: return "bool";
1541 case VAR_NUMBER: return "number";
1542 case VAR_FLOAT: return "float";
1543 case VAR_STRING: return "string";
1544 case VAR_BLOB: return "blob";
1545 case VAR_JOB: return "job";
1546 case VAR_CHANNEL: return "channel";
1547 case VAR_LIST: return "list";
1548 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001549 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 case VAR_PARTIAL: return "partial";
1551 }
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001552 return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553}
1554
1555/*
1556 * Return the name of a type.
1557 * The result may be in allocated memory, in which case "tofree" is set.
1558 */
1559 char *
1560type_name(type_T *type, char **tofree)
1561{
1562 char *name = vartype_name(type->tt_type);
1563
1564 *tofree = NULL;
1565 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1566 {
1567 char *member_free;
1568 char *member_name = type_name(type->tt_member, &member_free);
1569 size_t len;
1570
1571 len = STRLEN(name) + STRLEN(member_name) + 3;
1572 *tofree = alloc(len);
1573 if (*tofree != NULL)
1574 {
1575 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1576 vim_free(member_free);
1577 return *tofree;
1578 }
1579 }
1580 // TODO: function and partial argument types
1581
1582 return name;
1583}
1584
1585/*
1586 * Find "name" in script-local items of script "sid".
1587 * Returns the index in "sn_var_vals" if found.
1588 * If found but not in "sn_var_vals" returns -1.
1589 * If not found returns -2.
1590 */
1591 int
1592get_script_item_idx(int sid, char_u *name, int check_writable)
1593{
1594 hashtab_T *ht;
1595 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001596 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 int idx;
1598
1599 // First look the name up in the hashtable.
1600 if (sid <= 0 || sid > script_items.ga_len)
1601 return -1;
1602 ht = &SCRIPT_VARS(sid);
1603 di = find_var_in_ht(ht, 0, name, TRUE);
1604 if (di == NULL)
1605 return -2;
1606
1607 // Now find the svar_T index in sn_var_vals.
1608 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1609 {
1610 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1611
1612 if (sv->sv_tv == &di->di_tv)
1613 {
1614 if (check_writable && sv->sv_const)
1615 semsg(_(e_readonlyvar), name);
1616 return idx;
1617 }
1618 }
1619 return -1;
1620}
1621
1622/*
1623 * Find "name" in imported items of the current script/
1624 */
1625 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001626find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001628 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 int idx;
1630
1631 if (cctx != NULL)
1632 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1633 {
1634 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1635 + idx;
1636
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001637 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1638 : STRLEN(import->imp_name) == len
1639 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 return import;
1641 }
1642
1643 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1644 {
1645 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1646
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001647 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1648 : STRLEN(import->imp_name) == len
1649 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 return import;
1651 }
1652 return NULL;
1653}
1654
1655/*
1656 * Generate an instruction to load script-local variable "name".
1657 */
1658 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001659compile_load_scriptvar(
1660 cctx_T *cctx,
1661 char_u *name, // variable NUL terminated
1662 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001663 char_u **end, // end of variable
1664 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001666 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1668 imported_T *import;
1669
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001670 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001672 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001673 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1674 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 }
1676 if (idx >= 0)
1677 {
1678 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1679
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001680 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 current_sctx.sc_sid, idx, sv->sv_type);
1682 return OK;
1683 }
1684
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001685 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 if (import != NULL)
1687 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001688 if (import->imp_all)
1689 {
1690 char_u *p = skipwhite(*end);
1691 int name_len;
1692 ufunc_T *ufunc;
1693 type_T *type;
1694
1695 // Used "import * as Name", need to lookup the member.
1696 if (*p != '.')
1697 {
1698 semsg(_("E1060: expected dot after name: %s"), start);
1699 return FAIL;
1700 }
1701 ++p;
1702
1703 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1704 // TODO: what if it is a function?
1705 if (idx < 0)
1706 return FAIL;
1707 *end = p;
1708
1709 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1710 import->imp_sid,
1711 idx,
1712 type);
1713 }
1714 else
1715 {
1716 // TODO: check this is a variable, not a function
1717 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1718 import->imp_sid,
1719 import->imp_var_vals_idx,
1720 import->imp_type);
1721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 return OK;
1723 }
1724
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001725 if (error)
1726 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 return FAIL;
1728}
1729
1730/*
1731 * Compile a variable name into a load instruction.
1732 * "end" points to just after the name.
1733 * When "error" is FALSE do not give an error when not found.
1734 */
1735 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001736compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737{
1738 type_T *type;
1739 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001740 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 int res = FAIL;
1742
1743 if (*(*arg + 1) == ':')
1744 {
1745 // load namespaced variable
1746 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1747 if (name == NULL)
1748 return FAIL;
1749
1750 if (**arg == 'v')
1751 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001752 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 }
1754 else if (**arg == 'g')
1755 {
1756 // Global variables can be defined later, thus we don't check if it
1757 // exists, give error at runtime.
1758 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1759 }
1760 else if (**arg == 's')
1761 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001762 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 }
1764 else
1765 {
1766 semsg("Namespace not supported yet: %s", **arg);
1767 goto theend;
1768 }
1769 }
1770 else
1771 {
1772 size_t len = end - *arg;
1773 int idx;
1774 int gen_load = FALSE;
1775
1776 name = vim_strnsave(*arg, end - *arg);
1777 if (name == NULL)
1778 return FAIL;
1779
1780 idx = lookup_arg(*arg, len, cctx);
1781 if (idx >= 0)
1782 {
1783 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1784 type = cctx->ctx_ufunc->uf_arg_types[idx];
1785 else
1786 type = &t_any;
1787
1788 // Arguments are located above the frame pointer.
1789 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1790 if (cctx->ctx_ufunc->uf_va_name != NULL)
1791 --idx;
1792 gen_load = TRUE;
1793 }
1794 else if (lookup_vararg(*arg, len, cctx))
1795 {
1796 // varargs is always the last argument
1797 idx = -STACK_FRAME_SIZE - 1;
1798 type = cctx->ctx_ufunc->uf_va_type;
1799 gen_load = TRUE;
1800 }
1801 else
1802 {
1803 idx = lookup_local(*arg, len, cctx);
1804 if (idx >= 0)
1805 {
1806 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1807 gen_load = TRUE;
1808 }
1809 else
1810 {
1811 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1812 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1813 res = generate_PUSHBOOL(cctx, **arg == 't'
1814 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001815 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1816 == SCRIPT_VERSION_VIM9)
1817 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001818 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 }
1821 if (gen_load)
1822 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1823 }
1824
1825 *arg = end;
1826
1827theend:
1828 if (res == FAIL && error)
1829 semsg(_(e_var_notfound), name);
1830 vim_free(name);
1831 return res;
1832}
1833
1834/*
1835 * Compile the argument expressions.
1836 * "arg" points to just after the "(" and is advanced to after the ")"
1837 */
1838 static int
1839compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1840{
1841 char_u *p = *arg;
1842
1843 while (*p != NUL && *p != ')')
1844 {
1845 if (compile_expr1(&p, cctx) == FAIL)
1846 return FAIL;
1847 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001848
1849 if (*p != ',' && *skipwhite(p) == ',')
1850 {
1851 emsg(_("E1068: No white space allowed before ,"));
1852 p = skipwhite(p);
1853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001855 {
1856 ++p;
1857 if (!VIM_ISWHITE(*p))
1858 emsg(_("E1069: white space required after ,"));
1859 }
1860 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001862 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 if (*p != ')')
1864 {
1865 emsg(_(e_missing_close));
1866 return FAIL;
1867 }
1868 *arg = p + 1;
1869 return OK;
1870}
1871
1872/*
1873 * Compile a function call: name(arg1, arg2)
1874 * "arg" points to "name", "arg + varlen" to the "(".
1875 * "argcount_init" is 1 for "value->method()"
1876 * Instructions:
1877 * EVAL arg1
1878 * EVAL arg2
1879 * BCALL / DCALL / UCALL
1880 */
1881 static int
1882compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1883{
1884 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001885 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 int argcount = argcount_init;
1887 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001888 char_u fname_buf[FLEN_FIXED + 1];
1889 char_u *tofree = NULL;
1890 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001892 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893
1894 if (varlen >= sizeof(namebuf))
1895 {
1896 semsg(_("E1011: name too long: %s"), name);
1897 return FAIL;
1898 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001899 vim_strncpy(namebuf, *arg, varlen);
1900 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901
1902 *arg = skipwhite(*arg + varlen + 1);
1903 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001904 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001906 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 {
1908 int idx;
1909
1910 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001911 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001913 {
1914 res = generate_BCALL(cctx, idx, argcount);
1915 goto theend;
1916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 semsg(_(e_unknownfunc), namebuf);
1918 }
1919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001921 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001923 {
1924 res = generate_CALL(cctx, ufunc, argcount);
1925 goto theend;
1926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927
1928 // If the name is a variable, load it and use PCALL.
1929 p = namebuf;
1930 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001931 {
1932 res = generate_PCALL(cctx, argcount, FALSE);
1933 goto theend;
1934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935
1936 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001937 res = generate_UCALL(cctx, name, argcount);
1938
1939theend:
1940 vim_free(tofree);
1941 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942}
1943
1944// like NAMESPACE_CHAR but with 'a' and 'l'.
1945#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1946
1947/*
1948 * Find the end of a variable or function name. Unlike find_name_end() this
1949 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001950 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 * Return a pointer to just after the name. Equal to "arg" if there is no
1952 * valid name.
1953 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001954 static char_u *
1955to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956{
1957 char_u *p;
1958
1959 // Quick check for valid starting character.
1960 if (!eval_isnamec1(*arg))
1961 return arg;
1962
1963 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1964 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1965 // and can be used in slice "[n:]".
1966 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001967 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1969 break;
1970 return p;
1971}
1972
1973/*
1974 * Like to_name_end() but also skip over a list or dict constant.
1975 */
1976 char_u *
1977to_name_const_end(char_u *arg)
1978{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001979 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 typval_T rettv;
1981
1982 if (p == arg && *arg == '[')
1983 {
1984
1985 // Can be "[1, 2, 3]->Func()".
1986 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1987 p = arg;
1988 }
1989 else if (p == arg && *arg == '#' && arg[1] == '{')
1990 {
1991 ++p;
1992 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1993 p = arg;
1994 }
1995 else if (p == arg && *arg == '{')
1996 {
1997 int ret = get_lambda_tv(&p, &rettv, FALSE);
1998
1999 if (ret == NOTDONE)
2000 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2001 if (ret != OK)
2002 p = arg;
2003 }
2004
2005 return p;
2006}
2007
2008 static void
2009type_mismatch(type_T *expected, type_T *actual)
2010{
2011 char *tofree1, *tofree2;
2012
2013 semsg(_("E1013: type mismatch, expected %s but got %s"),
2014 type_name(expected, &tofree1), type_name(actual, &tofree2));
2015 vim_free(tofree1);
2016 vim_free(tofree2);
2017}
2018
2019/*
2020 * Check if the expected and actual types match.
2021 */
2022 static int
2023check_type(type_T *expected, type_T *actual, int give_msg)
2024{
2025 if (expected->tt_type != VAR_UNKNOWN)
2026 {
2027 if (expected->tt_type != actual->tt_type)
2028 {
2029 if (give_msg)
2030 type_mismatch(expected, actual);
2031 return FAIL;
2032 }
2033 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2034 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01002035 int ret;
2036
2037 // void is used for an empty list or dict
2038 if (actual->tt_member == &t_void)
2039 ret = OK;
2040 else
2041 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 if (ret == FAIL && give_msg)
2043 type_mismatch(expected, actual);
2044 return ret;
2045 }
2046 }
2047 return OK;
2048}
2049
2050/*
2051 * Check that
2052 * - "actual" is "expected" type or
2053 * - "actual" is a type that can be "expected" type: add a runtime check; or
2054 * - return FAIL.
2055 */
2056 static int
2057need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2058{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002059 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 return OK;
2061 if (actual->tt_type != VAR_UNKNOWN)
2062 {
2063 type_mismatch(expected, actual);
2064 return FAIL;
2065 }
2066 generate_TYPECHECK(cctx, expected, offset);
2067 return OK;
2068}
2069
2070/*
2071 * parse a list: [expr, expr]
2072 * "*arg" points to the '['.
2073 */
2074 static int
2075compile_list(char_u **arg, cctx_T *cctx)
2076{
2077 char_u *p = skipwhite(*arg + 1);
2078 int count = 0;
2079
2080 while (*p != ']')
2081 {
2082 if (*p == NUL)
2083 return FAIL;
2084 if (compile_expr1(&p, cctx) == FAIL)
2085 break;
2086 ++count;
2087 if (*p == ',')
2088 ++p;
2089 p = skipwhite(p);
2090 }
2091 *arg = p + 1;
2092
2093 generate_NEWLIST(cctx, count);
2094 return OK;
2095}
2096
2097/*
2098 * parse a lambda: {arg, arg -> expr}
2099 * "*arg" points to the '{'.
2100 */
2101 static int
2102compile_lambda(char_u **arg, cctx_T *cctx)
2103{
2104 garray_T *instr = &cctx->ctx_instr;
2105 typval_T rettv;
2106 ufunc_T *ufunc;
2107
2108 // Get the funcref in "rettv".
2109 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2110 return FAIL;
2111 ufunc = rettv.vval.v_partial->pt_func;
2112
2113 // The function will have one line: "return {expr}".
2114 // Compile it into instructions.
2115 compile_def_function(ufunc, TRUE);
2116
2117 if (ufunc->uf_dfunc_idx >= 0)
2118 {
2119 if (ga_grow(instr, 1) == FAIL)
2120 return FAIL;
2121 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2122 return OK;
2123 }
2124 return FAIL;
2125}
2126
2127/*
2128 * Compile a lamda call: expr->{lambda}(args)
2129 * "arg" points to the "{".
2130 */
2131 static int
2132compile_lambda_call(char_u **arg, cctx_T *cctx)
2133{
2134 ufunc_T *ufunc;
2135 typval_T rettv;
2136 int argcount = 1;
2137 int ret = FAIL;
2138
2139 // Get the funcref in "rettv".
2140 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2141 return FAIL;
2142
2143 if (**arg != '(')
2144 {
2145 if (*skipwhite(*arg) == '(')
2146 semsg(_(e_nowhitespace));
2147 else
2148 semsg(_(e_missing_paren), "lambda");
2149 clear_tv(&rettv);
2150 return FAIL;
2151 }
2152
2153 // The function will have one line: "return {expr}".
2154 // Compile it into instructions.
2155 ufunc = rettv.vval.v_partial->pt_func;
2156 ++ufunc->uf_refcount;
2157 compile_def_function(ufunc, TRUE);
2158
2159 // compile the arguments
2160 *arg = skipwhite(*arg + 1);
2161 if (compile_arguments(arg, cctx, &argcount) == OK)
2162 // call the compiled function
2163 ret = generate_CALL(cctx, ufunc, argcount);
2164
2165 clear_tv(&rettv);
2166 return ret;
2167}
2168
2169/*
2170 * parse a dict: {'key': val} or #{key: val}
2171 * "*arg" points to the '{'.
2172 */
2173 static int
2174compile_dict(char_u **arg, cctx_T *cctx, int literal)
2175{
2176 garray_T *instr = &cctx->ctx_instr;
2177 int count = 0;
2178 dict_T *d = dict_alloc();
2179 dictitem_T *item;
2180
2181 if (d == NULL)
2182 return FAIL;
2183 *arg = skipwhite(*arg + 1);
2184 while (**arg != '}' && **arg != NUL)
2185 {
2186 char_u *key = NULL;
2187
2188 if (literal)
2189 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002190 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191
2192 if (p == *arg)
2193 {
2194 semsg(_("E1014: Invalid key: %s"), *arg);
2195 return FAIL;
2196 }
2197 key = vim_strnsave(*arg, p - *arg);
2198 if (generate_PUSHS(cctx, key) == FAIL)
2199 return FAIL;
2200 *arg = p;
2201 }
2202 else
2203 {
2204 isn_T *isn;
2205
2206 if (compile_expr1(arg, cctx) == FAIL)
2207 return FAIL;
2208 // TODO: check type is string
2209 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2210 if (isn->isn_type == ISN_PUSHS)
2211 key = isn->isn_arg.string;
2212 }
2213
2214 // Check for duplicate keys, if using string keys.
2215 if (key != NULL)
2216 {
2217 item = dict_find(d, key, -1);
2218 if (item != NULL)
2219 {
2220 semsg(_(e_duplicate_key), key);
2221 goto failret;
2222 }
2223 item = dictitem_alloc(key);
2224 if (item != NULL)
2225 {
2226 item->di_tv.v_type = VAR_UNKNOWN;
2227 item->di_tv.v_lock = 0;
2228 if (dict_add(d, item) == FAIL)
2229 dictitem_free(item);
2230 }
2231 }
2232
2233 *arg = skipwhite(*arg);
2234 if (**arg != ':')
2235 {
2236 semsg(_(e_missing_dict_colon), *arg);
2237 return FAIL;
2238 }
2239
2240 *arg = skipwhite(*arg + 1);
2241 if (compile_expr1(arg, cctx) == FAIL)
2242 return FAIL;
2243 ++count;
2244
2245 if (**arg == '}')
2246 break;
2247 if (**arg != ',')
2248 {
2249 semsg(_(e_missing_dict_comma), *arg);
2250 goto failret;
2251 }
2252 *arg = skipwhite(*arg + 1);
2253 }
2254
2255 if (**arg != '}')
2256 {
2257 semsg(_(e_missing_dict_end), *arg);
2258 goto failret;
2259 }
2260 *arg = *arg + 1;
2261
2262 dict_unref(d);
2263 return generate_NEWDICT(cctx, count);
2264
2265failret:
2266 dict_unref(d);
2267 return FAIL;
2268}
2269
2270/*
2271 * Compile "&option".
2272 */
2273 static int
2274compile_get_option(char_u **arg, cctx_T *cctx)
2275{
2276 typval_T rettv;
2277 char_u *start = *arg;
2278 int ret;
2279
2280 // parse the option and get the current value to get the type.
2281 rettv.v_type = VAR_UNKNOWN;
2282 ret = get_option_tv(arg, &rettv, TRUE);
2283 if (ret == OK)
2284 {
2285 // include the '&' in the name, get_option_tv() expects it.
2286 char_u *name = vim_strnsave(start, *arg - start);
2287 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2288
2289 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2290 vim_free(name);
2291 }
2292 clear_tv(&rettv);
2293
2294 return ret;
2295}
2296
2297/*
2298 * Compile "$VAR".
2299 */
2300 static int
2301compile_get_env(char_u **arg, cctx_T *cctx)
2302{
2303 char_u *start = *arg;
2304 int len;
2305 int ret;
2306 char_u *name;
2307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 ++*arg;
2309 len = get_env_len(arg);
2310 if (len == 0)
2311 {
2312 semsg(_(e_syntax_at), start - 1);
2313 return FAIL;
2314 }
2315
2316 // include the '$' in the name, get_env_tv() expects it.
2317 name = vim_strnsave(start, len + 1);
2318 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2319 vim_free(name);
2320 return ret;
2321}
2322
2323/*
2324 * Compile "@r".
2325 */
2326 static int
2327compile_get_register(char_u **arg, cctx_T *cctx)
2328{
2329 int ret;
2330
2331 ++*arg;
2332 if (**arg == NUL)
2333 {
2334 semsg(_(e_syntax_at), *arg - 1);
2335 return FAIL;
2336 }
2337 if (!valid_yank_reg(**arg, TRUE))
2338 {
2339 emsg_invreg(**arg);
2340 return FAIL;
2341 }
2342 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2343 ++*arg;
2344 return ret;
2345}
2346
2347/*
2348 * Apply leading '!', '-' and '+' to constant "rettv".
2349 */
2350 static int
2351apply_leader(typval_T *rettv, char_u *start, char_u *end)
2352{
2353 char_u *p = end;
2354
2355 // this works from end to start
2356 while (p > start)
2357 {
2358 --p;
2359 if (*p == '-' || *p == '+')
2360 {
2361 // only '-' has an effect, for '+' we only check the type
2362#ifdef FEAT_FLOAT
2363 if (rettv->v_type == VAR_FLOAT)
2364 {
2365 if (*p == '-')
2366 rettv->vval.v_float = -rettv->vval.v_float;
2367 }
2368 else
2369#endif
2370 {
2371 varnumber_T val;
2372 int error = FALSE;
2373
2374 // tv_get_number_chk() accepts a string, but we don't want that
2375 // here
2376 if (check_not_string(rettv) == FAIL)
2377 return FAIL;
2378 val = tv_get_number_chk(rettv, &error);
2379 clear_tv(rettv);
2380 if (error)
2381 return FAIL;
2382 if (*p == '-')
2383 val = -val;
2384 rettv->v_type = VAR_NUMBER;
2385 rettv->vval.v_number = val;
2386 }
2387 }
2388 else
2389 {
2390 int v = tv2bool(rettv);
2391
2392 // '!' is permissive in the type.
2393 clear_tv(rettv);
2394 rettv->v_type = VAR_BOOL;
2395 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2396 }
2397 }
2398 return OK;
2399}
2400
2401/*
2402 * Recognize v: variables that are constants and set "rettv".
2403 */
2404 static void
2405get_vim_constant(char_u **arg, typval_T *rettv)
2406{
2407 if (STRNCMP(*arg, "v:true", 6) == 0)
2408 {
2409 rettv->v_type = VAR_BOOL;
2410 rettv->vval.v_number = VVAL_TRUE;
2411 *arg += 6;
2412 }
2413 else if (STRNCMP(*arg, "v:false", 7) == 0)
2414 {
2415 rettv->v_type = VAR_BOOL;
2416 rettv->vval.v_number = VVAL_FALSE;
2417 *arg += 7;
2418 }
2419 else if (STRNCMP(*arg, "v:null", 6) == 0)
2420 {
2421 rettv->v_type = VAR_SPECIAL;
2422 rettv->vval.v_number = VVAL_NULL;
2423 *arg += 6;
2424 }
2425 else if (STRNCMP(*arg, "v:none", 6) == 0)
2426 {
2427 rettv->v_type = VAR_SPECIAL;
2428 rettv->vval.v_number = VVAL_NONE;
2429 *arg += 6;
2430 }
2431}
2432
2433/*
2434 * Compile code to apply '-', '+' and '!'.
2435 */
2436 static int
2437compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2438{
2439 char_u *p = end;
2440
2441 // this works from end to start
2442 while (p > start)
2443 {
2444 --p;
2445 if (*p == '-' || *p == '+')
2446 {
2447 int negate = *p == '-';
2448 isn_T *isn;
2449
2450 // TODO: check type
2451 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2452 {
2453 --p;
2454 if (*p == '-')
2455 negate = !negate;
2456 }
2457 // only '-' has an effect, for '+' we only check the type
2458 if (negate)
2459 isn = generate_instr(cctx, ISN_NEGATENR);
2460 else
2461 isn = generate_instr(cctx, ISN_CHECKNR);
2462 if (isn == NULL)
2463 return FAIL;
2464 }
2465 else
2466 {
2467 int invert = TRUE;
2468
2469 while (p > start && p[-1] == '!')
2470 {
2471 --p;
2472 invert = !invert;
2473 }
2474 if (generate_2BOOL(cctx, invert) == FAIL)
2475 return FAIL;
2476 }
2477 }
2478 return OK;
2479}
2480
2481/*
2482 * Compile whatever comes after "name" or "name()".
2483 */
2484 static int
2485compile_subscript(
2486 char_u **arg,
2487 cctx_T *cctx,
2488 char_u **start_leader,
2489 char_u *end_leader)
2490{
2491 for (;;)
2492 {
2493 if (**arg == '(')
2494 {
2495 int argcount = 0;
2496
2497 // funcref(arg)
2498 *arg = skipwhite(*arg + 1);
2499 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2500 return FAIL;
2501 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2502 return FAIL;
2503 }
2504 else if (**arg == '-' && (*arg)[1] == '>')
2505 {
2506 char_u *p;
2507
2508 // something->method()
2509 // Apply the '!', '-' and '+' first:
2510 // -1.0->func() works like (-1.0)->func()
2511 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2512 return FAIL;
2513 *start_leader = end_leader; // don't apply again later
2514
2515 *arg = skipwhite(*arg + 2);
2516 if (**arg == '{')
2517 {
2518 // lambda call: list->{lambda}
2519 if (compile_lambda_call(arg, cctx) == FAIL)
2520 return FAIL;
2521 }
2522 else
2523 {
2524 // method call: list->method()
2525 for (p = *arg; eval_isnamec1(*p); ++p)
2526 ;
2527 if (*p != '(')
2528 {
2529 semsg(_(e_missing_paren), arg);
2530 return FAIL;
2531 }
2532 // TODO: base value may not be the first argument
2533 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2534 return FAIL;
2535 }
2536 }
2537 else if (**arg == '[')
2538 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002539 garray_T *stack;
2540 type_T **typep;
2541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 // list index: list[123]
2543 // TODO: more arguments
2544 // TODO: dict member dict['name']
2545 *arg = skipwhite(*arg + 1);
2546 if (compile_expr1(arg, cctx) == FAIL)
2547 return FAIL;
2548
2549 if (**arg != ']')
2550 {
2551 emsg(_(e_missbrac));
2552 return FAIL;
2553 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002554 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555
2556 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2557 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002558 stack = &cctx->ctx_type_stack;
2559 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2560 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2561 {
2562 emsg(_(e_listreq));
2563 return FAIL;
2564 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002565 if ((*typep)->tt_type == VAR_LIST)
2566 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
2568 else if (**arg == '.' && (*arg)[1] != '.')
2569 {
2570 char_u *p;
2571
2572 ++*arg;
2573 p = *arg;
2574 // dictionary member: dict.name
2575 if (eval_isnamec1(*p))
2576 while (eval_isnamec(*p))
2577 MB_PTR_ADV(p);
2578 if (p == *arg)
2579 {
2580 semsg(_(e_syntax_at), *arg);
2581 return FAIL;
2582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2584 return FAIL;
2585 *arg = p;
2586 }
2587 else
2588 break;
2589 }
2590
2591 // TODO - see handle_subscript():
2592 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2593 // Don't do this when "Func" is already a partial that was bound
2594 // explicitly (pt_auto is FALSE).
2595
2596 return OK;
2597}
2598
2599/*
2600 * Compile an expression at "*p" and add instructions to "instr".
2601 * "p" is advanced until after the expression, skipping white space.
2602 *
2603 * This is the equivalent of eval1(), eval2(), etc.
2604 */
2605
2606/*
2607 * number number constant
2608 * 0zFFFFFFFF Blob constant
2609 * "string" string constant
2610 * 'string' literal string constant
2611 * &option-name option value
2612 * @r register contents
2613 * identifier variable value
2614 * function() function call
2615 * $VAR environment variable
2616 * (expression) nested expression
2617 * [expr, expr] List
2618 * {key: val, key: val} Dictionary
2619 * #{key: val, key: val} Dictionary with literal keys
2620 *
2621 * Also handle:
2622 * ! in front logical NOT
2623 * - in front unary minus
2624 * + in front unary plus (ignored)
2625 * trailing (arg) funcref/partial call
2626 * trailing [] subscript in String or List
2627 * trailing .name entry in Dictionary
2628 * trailing ->name() method call
2629 */
2630 static int
2631compile_expr7(char_u **arg, cctx_T *cctx)
2632{
2633 typval_T rettv;
2634 char_u *start_leader, *end_leader;
2635 int ret = OK;
2636
2637 /*
2638 * Skip '!', '-' and '+' characters. They are handled later.
2639 */
2640 start_leader = *arg;
2641 while (**arg == '!' || **arg == '-' || **arg == '+')
2642 *arg = skipwhite(*arg + 1);
2643 end_leader = *arg;
2644
2645 rettv.v_type = VAR_UNKNOWN;
2646 switch (**arg)
2647 {
2648 /*
2649 * Number constant.
2650 */
2651 case '0': // also for blob starting with 0z
2652 case '1':
2653 case '2':
2654 case '3':
2655 case '4':
2656 case '5':
2657 case '6':
2658 case '7':
2659 case '8':
2660 case '9':
2661 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2662 return FAIL;
2663 break;
2664
2665 /*
2666 * String constant: "string".
2667 */
2668 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2669 return FAIL;
2670 break;
2671
2672 /*
2673 * Literal string constant: 'str''ing'.
2674 */
2675 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2676 return FAIL;
2677 break;
2678
2679 /*
2680 * Constant Vim variable.
2681 */
2682 case 'v': get_vim_constant(arg, &rettv);
2683 ret = NOTDONE;
2684 break;
2685
2686 /*
2687 * List: [expr, expr]
2688 */
2689 case '[': ret = compile_list(arg, cctx);
2690 break;
2691
2692 /*
2693 * Dictionary: #{key: val, key: val}
2694 */
2695 case '#': if ((*arg)[1] == '{')
2696 {
2697 ++*arg;
2698 ret = compile_dict(arg, cctx, TRUE);
2699 }
2700 else
2701 ret = NOTDONE;
2702 break;
2703
2704 /*
2705 * Lambda: {arg, arg -> expr}
2706 * Dictionary: {'key': val, 'key': val}
2707 */
2708 case '{': {
2709 char_u *start = skipwhite(*arg + 1);
2710
2711 // Find out what comes after the arguments.
2712 ret = get_function_args(&start, '-', NULL,
2713 NULL, NULL, NULL, TRUE);
2714 if (ret != FAIL && *start == '>')
2715 ret = compile_lambda(arg, cctx);
2716 else
2717 ret = compile_dict(arg, cctx, FALSE);
2718 }
2719 break;
2720
2721 /*
2722 * Option value: &name
2723 */
2724 case '&': ret = compile_get_option(arg, cctx);
2725 break;
2726
2727 /*
2728 * Environment variable: $VAR.
2729 */
2730 case '$': ret = compile_get_env(arg, cctx);
2731 break;
2732
2733 /*
2734 * Register contents: @r.
2735 */
2736 case '@': ret = compile_get_register(arg, cctx);
2737 break;
2738 /*
2739 * nested expression: (expression).
2740 */
2741 case '(': *arg = skipwhite(*arg + 1);
2742 ret = compile_expr1(arg, cctx); // recursive!
2743 *arg = skipwhite(*arg);
2744 if (**arg == ')')
2745 ++*arg;
2746 else if (ret == OK)
2747 {
2748 emsg(_(e_missing_close));
2749 ret = FAIL;
2750 }
2751 break;
2752
2753 default: ret = NOTDONE;
2754 break;
2755 }
2756 if (ret == FAIL)
2757 return FAIL;
2758
2759 if (rettv.v_type != VAR_UNKNOWN)
2760 {
2761 // apply the '!', '-' and '+' before the constant
2762 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2763 {
2764 clear_tv(&rettv);
2765 return FAIL;
2766 }
2767 start_leader = end_leader; // don't apply again below
2768
2769 // push constant
2770 switch (rettv.v_type)
2771 {
2772 case VAR_BOOL:
2773 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2774 break;
2775 case VAR_SPECIAL:
2776 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2777 break;
2778 case VAR_NUMBER:
2779 generate_PUSHNR(cctx, rettv.vval.v_number);
2780 break;
2781#ifdef FEAT_FLOAT
2782 case VAR_FLOAT:
2783 generate_PUSHF(cctx, rettv.vval.v_float);
2784 break;
2785#endif
2786 case VAR_BLOB:
2787 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2788 rettv.vval.v_blob = NULL;
2789 break;
2790 case VAR_STRING:
2791 generate_PUSHS(cctx, rettv.vval.v_string);
2792 rettv.vval.v_string = NULL;
2793 break;
2794 default:
2795 iemsg("constant type missing");
2796 return FAIL;
2797 }
2798 }
2799 else if (ret == NOTDONE)
2800 {
2801 char_u *p;
2802 int r;
2803
2804 if (!eval_isnamec1(**arg))
2805 {
2806 semsg(_("E1015: Name expected: %s"), *arg);
2807 return FAIL;
2808 }
2809
2810 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002811 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 if (*p == '(')
2813 r = compile_call(arg, p - *arg, cctx, 0);
2814 else
2815 r = compile_load(arg, p, cctx, TRUE);
2816 if (r == FAIL)
2817 return FAIL;
2818 }
2819
2820 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2821 return FAIL;
2822
2823 // Now deal with prefixed '-', '+' and '!', if not done already.
2824 return compile_leader(cctx, start_leader, end_leader);
2825}
2826
2827/*
2828 * * number multiplication
2829 * / number division
2830 * % number modulo
2831 */
2832 static int
2833compile_expr6(char_u **arg, cctx_T *cctx)
2834{
2835 char_u *op;
2836
2837 // get the first variable
2838 if (compile_expr7(arg, cctx) == FAIL)
2839 return FAIL;
2840
2841 /*
2842 * Repeat computing, until no "*", "/" or "%" is following.
2843 */
2844 for (;;)
2845 {
2846 op = skipwhite(*arg);
2847 if (*op != '*' && *op != '/' && *op != '%')
2848 break;
2849 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2850 {
2851 char_u buf[3];
2852
2853 vim_strncpy(buf, op, 1);
2854 semsg(_(e_white_both), buf);
2855 }
2856 *arg = skipwhite(op + 1);
2857
2858 // get the second variable
2859 if (compile_expr7(arg, cctx) == FAIL)
2860 return FAIL;
2861
2862 generate_two_op(cctx, op);
2863 }
2864
2865 return OK;
2866}
2867
2868/*
2869 * + number addition
2870 * - number subtraction
2871 * .. string concatenation
2872 */
2873 static int
2874compile_expr5(char_u **arg, cctx_T *cctx)
2875{
2876 char_u *op;
2877 int oplen;
2878
2879 // get the first variable
2880 if (compile_expr6(arg, cctx) == FAIL)
2881 return FAIL;
2882
2883 /*
2884 * Repeat computing, until no "+", "-" or ".." is following.
2885 */
2886 for (;;)
2887 {
2888 op = skipwhite(*arg);
2889 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2890 break;
2891 oplen = (*op == '.' ? 2 : 1);
2892
2893 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2894 {
2895 char_u buf[3];
2896
2897 vim_strncpy(buf, op, oplen);
2898 semsg(_(e_white_both), buf);
2899 }
2900
2901 *arg = skipwhite(op + oplen);
2902
2903 // get the second variable
2904 if (compile_expr6(arg, cctx) == FAIL)
2905 return FAIL;
2906
2907 if (*op == '.')
2908 {
2909 if (may_generate_2STRING(-2, cctx) == FAIL
2910 || may_generate_2STRING(-1, cctx) == FAIL)
2911 return FAIL;
2912 generate_instr_drop(cctx, ISN_CONCAT, 1);
2913 }
2914 else
2915 generate_two_op(cctx, op);
2916 }
2917
2918 return OK;
2919}
2920
Bram Moolenaar080457c2020-03-03 21:53:32 +01002921 static exptype_T
2922get_compare_type(char_u *p, int *len, int *type_is)
2923{
2924 exptype_T type = EXPR_UNKNOWN;
2925 int i;
2926
2927 switch (p[0])
2928 {
2929 case '=': if (p[1] == '=')
2930 type = EXPR_EQUAL;
2931 else if (p[1] == '~')
2932 type = EXPR_MATCH;
2933 break;
2934 case '!': if (p[1] == '=')
2935 type = EXPR_NEQUAL;
2936 else if (p[1] == '~')
2937 type = EXPR_NOMATCH;
2938 break;
2939 case '>': if (p[1] != '=')
2940 {
2941 type = EXPR_GREATER;
2942 *len = 1;
2943 }
2944 else
2945 type = EXPR_GEQUAL;
2946 break;
2947 case '<': if (p[1] != '=')
2948 {
2949 type = EXPR_SMALLER;
2950 *len = 1;
2951 }
2952 else
2953 type = EXPR_SEQUAL;
2954 break;
2955 case 'i': if (p[1] == 's')
2956 {
2957 // "is" and "isnot"; but not a prefix of a name
2958 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2959 *len = 5;
2960 i = p[*len];
2961 if (!isalnum(i) && i != '_')
2962 {
2963 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2964 *type_is = TRUE;
2965 }
2966 }
2967 break;
2968 }
2969 return type;
2970}
2971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972/*
2973 * expr5a == expr5b
2974 * expr5a =~ expr5b
2975 * expr5a != expr5b
2976 * expr5a !~ expr5b
2977 * expr5a > expr5b
2978 * expr5a >= expr5b
2979 * expr5a < expr5b
2980 * expr5a <= expr5b
2981 * expr5a is expr5b
2982 * expr5a isnot expr5b
2983 *
2984 * Produces instructions:
2985 * EVAL expr5a Push result of "expr5a"
2986 * EVAL expr5b Push result of "expr5b"
2987 * COMPARE one of the compare instructions
2988 */
2989 static int
2990compile_expr4(char_u **arg, cctx_T *cctx)
2991{
2992 exptype_T type = EXPR_UNKNOWN;
2993 char_u *p;
2994 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 int type_is = FALSE;
2996
2997 // get the first variable
2998 if (compile_expr5(arg, cctx) == FAIL)
2999 return FAIL;
3000
3001 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003002 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
3004 /*
3005 * If there is a comparative operator, use it.
3006 */
3007 if (type != EXPR_UNKNOWN)
3008 {
3009 int ic = FALSE; // Default: do not ignore case
3010
3011 if (type_is && (p[len] == '?' || p[len] == '#'))
3012 {
3013 semsg(_(e_invexpr2), *arg);
3014 return FAIL;
3015 }
3016 // extra question mark appended: ignore case
3017 if (p[len] == '?')
3018 {
3019 ic = TRUE;
3020 ++len;
3021 }
3022 // extra '#' appended: match case (ignored)
3023 else if (p[len] == '#')
3024 ++len;
3025 // nothing appended: match case
3026
3027 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3028 {
3029 char_u buf[7];
3030
3031 vim_strncpy(buf, p, len);
3032 semsg(_(e_white_both), buf);
3033 }
3034
3035 // get the second variable
3036 *arg = skipwhite(p + len);
3037 if (compile_expr5(arg, cctx) == FAIL)
3038 return FAIL;
3039
3040 generate_COMPARE(cctx, type, ic);
3041 }
3042
3043 return OK;
3044}
3045
3046/*
3047 * Compile || or &&.
3048 */
3049 static int
3050compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3051{
3052 char_u *p = skipwhite(*arg);
3053 int opchar = *op;
3054
3055 if (p[0] == opchar && p[1] == opchar)
3056 {
3057 garray_T *instr = &cctx->ctx_instr;
3058 garray_T end_ga;
3059
3060 /*
3061 * Repeat until there is no following "||" or "&&"
3062 */
3063 ga_init2(&end_ga, sizeof(int), 10);
3064 while (p[0] == opchar && p[1] == opchar)
3065 {
3066 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3067 semsg(_(e_white_both), op);
3068
3069 if (ga_grow(&end_ga, 1) == FAIL)
3070 {
3071 ga_clear(&end_ga);
3072 return FAIL;
3073 }
3074 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3075 ++end_ga.ga_len;
3076 generate_JUMP(cctx, opchar == '|'
3077 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3078
3079 // eval the next expression
3080 *arg = skipwhite(p + 2);
3081 if ((opchar == '|' ? compile_expr3(arg, cctx)
3082 : compile_expr4(arg, cctx)) == FAIL)
3083 {
3084 ga_clear(&end_ga);
3085 return FAIL;
3086 }
3087 p = skipwhite(*arg);
3088 }
3089
3090 // Fill in the end label in all jumps.
3091 while (end_ga.ga_len > 0)
3092 {
3093 isn_T *isn;
3094
3095 --end_ga.ga_len;
3096 isn = ((isn_T *)instr->ga_data)
3097 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3098 isn->isn_arg.jump.jump_where = instr->ga_len;
3099 }
3100 ga_clear(&end_ga);
3101 }
3102
3103 return OK;
3104}
3105
3106/*
3107 * expr4a && expr4a && expr4a logical AND
3108 *
3109 * Produces instructions:
3110 * EVAL expr4a Push result of "expr4a"
3111 * JUMP_AND_KEEP_IF_FALSE end
3112 * EVAL expr4b Push result of "expr4b"
3113 * JUMP_AND_KEEP_IF_FALSE end
3114 * EVAL expr4c Push result of "expr4c"
3115 * end:
3116 */
3117 static int
3118compile_expr3(char_u **arg, cctx_T *cctx)
3119{
3120 // get the first variable
3121 if (compile_expr4(arg, cctx) == FAIL)
3122 return FAIL;
3123
3124 // || and && work almost the same
3125 return compile_and_or(arg, cctx, "&&");
3126}
3127
3128/*
3129 * expr3a || expr3b || expr3c logical OR
3130 *
3131 * Produces instructions:
3132 * EVAL expr3a Push result of "expr3a"
3133 * JUMP_AND_KEEP_IF_TRUE end
3134 * EVAL expr3b Push result of "expr3b"
3135 * JUMP_AND_KEEP_IF_TRUE end
3136 * EVAL expr3c Push result of "expr3c"
3137 * end:
3138 */
3139 static int
3140compile_expr2(char_u **arg, cctx_T *cctx)
3141{
3142 // eval the first expression
3143 if (compile_expr3(arg, cctx) == FAIL)
3144 return FAIL;
3145
3146 // || and && work almost the same
3147 return compile_and_or(arg, cctx, "||");
3148}
3149
3150/*
3151 * Toplevel expression: expr2 ? expr1a : expr1b
3152 *
3153 * Produces instructions:
3154 * EVAL expr2 Push result of "expr"
3155 * JUMP_IF_FALSE alt jump if false
3156 * EVAL expr1a
3157 * JUMP_ALWAYS end
3158 * alt: EVAL expr1b
3159 * end:
3160 */
3161 static int
3162compile_expr1(char_u **arg, cctx_T *cctx)
3163{
3164 char_u *p;
3165
3166 // evaluate the first expression
3167 if (compile_expr2(arg, cctx) == FAIL)
3168 return FAIL;
3169
3170 p = skipwhite(*arg);
3171 if (*p == '?')
3172 {
3173 garray_T *instr = &cctx->ctx_instr;
3174 garray_T *stack = &cctx->ctx_type_stack;
3175 int alt_idx = instr->ga_len;
3176 int end_idx;
3177 isn_T *isn;
3178 type_T *type1;
3179 type_T *type2;
3180
3181 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3182 semsg(_(e_white_both), "?");
3183
3184 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3185
3186 // evaluate the second expression; any type is accepted
3187 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003188 if (compile_expr1(arg, cctx) == FAIL)
3189 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
3191 // remember the type and drop it
3192 --stack->ga_len;
3193 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3194
3195 end_idx = instr->ga_len;
3196 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3197
3198 // jump here from JUMP_IF_FALSE
3199 isn = ((isn_T *)instr->ga_data) + alt_idx;
3200 isn->isn_arg.jump.jump_where = instr->ga_len;
3201
3202 // Check for the ":".
3203 p = skipwhite(*arg);
3204 if (*p != ':')
3205 {
3206 emsg(_(e_missing_colon));
3207 return FAIL;
3208 }
3209 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3210 semsg(_(e_white_both), ":");
3211
3212 // evaluate the third expression
3213 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003214 if (compile_expr1(arg, cctx) == FAIL)
3215 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 // If the types differ, the result has a more generic type.
3218 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003219 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
3221 // jump here from JUMP_ALWAYS
3222 isn = ((isn_T *)instr->ga_data) + end_idx;
3223 isn->isn_arg.jump.jump_where = instr->ga_len;
3224 }
3225 return OK;
3226}
3227
3228/*
3229 * compile "return [expr]"
3230 */
3231 static char_u *
3232compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3233{
3234 char_u *p = arg;
3235 garray_T *stack = &cctx->ctx_type_stack;
3236 type_T *stack_type;
3237
3238 if (*p != NUL && *p != '|' && *p != '\n')
3239 {
3240 // compile return argument into instructions
3241 if (compile_expr1(&p, cctx) == FAIL)
3242 return NULL;
3243
3244 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3245 if (set_return_type)
3246 cctx->ctx_ufunc->uf_ret_type = stack_type;
3247 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3248 == FAIL)
3249 return NULL;
3250 }
3251 else
3252 {
3253 if (set_return_type)
3254 cctx->ctx_ufunc->uf_ret_type = &t_void;
3255 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3256 {
3257 emsg(_("E1003: Missing return value"));
3258 return NULL;
3259 }
3260
3261 // No argument, return zero.
3262 generate_PUSHNR(cctx, 0);
3263 }
3264
3265 if (generate_instr(cctx, ISN_RETURN) == NULL)
3266 return NULL;
3267
3268 // "return val | endif" is possible
3269 return skipwhite(p);
3270}
3271
3272/*
3273 * Return the length of an assignment operator, or zero if there isn't one.
3274 */
3275 int
3276assignment_len(char_u *p, int *heredoc)
3277{
3278 if (*p == '=')
3279 {
3280 if (p[1] == '<' && p[2] == '<')
3281 {
3282 *heredoc = TRUE;
3283 return 3;
3284 }
3285 return 1;
3286 }
3287 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3288 return 2;
3289 if (STRNCMP(p, "..=", 3) == 0)
3290 return 3;
3291 return 0;
3292}
3293
3294// words that cannot be used as a variable
3295static char *reserved[] = {
3296 "true",
3297 "false",
3298 NULL
3299};
3300
3301/*
3302 * Get a line for "=<<".
3303 * Return a pointer to the line in allocated memory.
3304 * Return NULL for end-of-file or some error.
3305 */
3306 static char_u *
3307heredoc_getline(
3308 int c UNUSED,
3309 void *cookie,
3310 int indent UNUSED,
3311 int do_concat UNUSED)
3312{
3313 cctx_T *cctx = (cctx_T *)cookie;
3314
3315 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3316 NULL;
3317 ++cctx->ctx_lnum;
3318 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3319 [cctx->ctx_lnum]);
3320}
3321
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003322typedef enum {
3323 dest_local,
3324 dest_option,
3325 dest_env,
3326 dest_global,
3327 dest_vimvar,
3328 dest_script,
3329 dest_reg,
3330} assign_dest_T;
3331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332/*
3333 * compile "let var [= expr]", "const var = expr" and "var = expr"
3334 * "arg" points to "var".
3335 */
3336 static char_u *
3337compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3338{
3339 char_u *p;
3340 char_u *ret = NULL;
3341 int var_count = 0;
3342 int semicolon = 0;
3343 size_t varlen;
3344 garray_T *instr = &cctx->ctx_instr;
3345 int idx = -1;
3346 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003348 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003350 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 int oplen = 0;
3352 int heredoc = FALSE;
3353 type_T *type;
3354 lvar_T *lvar;
3355 char_u *name;
3356 char_u *sp;
3357 int has_type = FALSE;
3358 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3359 int instr_count = -1;
3360
3361 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3362 if (p == NULL)
3363 return NULL;
3364 if (var_count > 0)
3365 {
3366 // TODO: let [var, var] = list
3367 emsg("Cannot handle a list yet");
3368 return NULL;
3369 }
3370
3371 varlen = p - arg;
3372 name = vim_strnsave(arg, (int)varlen);
3373 if (name == NULL)
3374 return NULL;
3375
Bram Moolenaar080457c2020-03-03 21:53:32 +01003376 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003378 if (*arg == '&')
3379 {
3380 int cc;
3381 long numval;
3382 char_u *stringval = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaar080457c2020-03-03 21:53:32 +01003384 dest = dest_option;
3385 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003387 emsg(_(e_const_option));
3388 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 if (is_decl)
3391 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003392 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 goto theend;
3394 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003395 p = arg;
3396 p = find_option_end(&p, &opt_flags);
3397 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003399 emsg(_(e_letunexp));
3400 return NULL;
3401 }
3402 cc = *p;
3403 *p = NUL;
3404 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3405 *p = cc;
3406 if (opt_type == -3)
3407 {
3408 semsg(_(e_unknown_option), *arg);
3409 return NULL;
3410 }
3411 if (opt_type == -2 || opt_type == 0)
3412 type = &t_string;
3413 else
3414 type = &t_number; // both number and boolean option
3415 }
3416 else if (*arg == '$')
3417 {
3418 dest = dest_env;
3419 if (is_decl)
3420 {
3421 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3422 goto theend;
3423 }
3424 }
3425 else if (*arg == '@')
3426 {
3427 if (!valid_yank_reg(arg[1], TRUE))
3428 {
3429 emsg_invreg(arg[1]);
3430 return FAIL;
3431 }
3432 dest = dest_reg;
3433 if (is_decl)
3434 {
3435 semsg(_("E1066: Cannot declare a register: %s"), name);
3436 goto theend;
3437 }
3438 }
3439 else if (STRNCMP(arg, "g:", 2) == 0)
3440 {
3441 dest = dest_global;
3442 if (is_decl)
3443 {
3444 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3445 goto theend;
3446 }
3447 }
3448 else if (STRNCMP(arg, "v:", 2) == 0)
3449 {
3450 vimvaridx = find_vim_var(name + 2);
3451 if (vimvaridx < 0)
3452 {
3453 semsg(_(e_var_notfound), arg);
3454 goto theend;
3455 }
3456 dest = dest_vimvar;
3457 if (is_decl)
3458 {
3459 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3460 goto theend;
3461 }
3462 }
3463 else
3464 {
3465 for (idx = 0; reserved[idx] != NULL; ++idx)
3466 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003468 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 goto theend;
3470 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003471
3472 idx = lookup_local(arg, varlen, cctx);
3473 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003475 if (is_decl)
3476 {
3477 semsg(_("E1017: Variable already declared: %s"), name);
3478 goto theend;
3479 }
3480 else
3481 {
3482 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3483 if (lvar->lv_const)
3484 {
3485 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3486 goto theend;
3487 }
3488 }
3489 }
3490 else if (STRNCMP(arg, "s:", 2) == 0
3491 || lookup_script(arg, varlen) == OK
3492 || find_imported(arg, varlen, cctx) != NULL)
3493 {
3494 dest = dest_script;
3495 if (is_decl)
3496 {
3497 semsg(_("E1054: Variable already declared in the script: %s"),
3498 name);
3499 goto theend;
3500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 }
3503 }
3504
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003505 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 {
3507 if (is_decl && *p == ':')
3508 {
3509 // parse optional type: "let var: type = expr"
3510 p = skipwhite(p + 1);
3511 type = parse_type(&p, cctx->ctx_type_list);
3512 if (type == NULL)
3513 goto theend;
3514 has_type = TRUE;
3515 }
3516 else if (idx < 0)
3517 {
3518 // global and new local default to "any" type
3519 type = &t_any;
3520 }
3521 else
3522 {
3523 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3524 type = lvar->lv_type;
3525 }
3526 }
3527
3528 sp = p;
3529 p = skipwhite(p);
3530 op = p;
3531 oplen = assignment_len(p, &heredoc);
3532 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3533 {
3534 char_u buf[4];
3535
3536 vim_strncpy(buf, op, oplen);
3537 semsg(_(e_white_both), buf);
3538 }
3539
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003540 if (oplen == 3 && !heredoc && dest != dest_global
3541 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003543 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 goto theend;
3545 }
3546
3547 // +=, /=, etc. require an existing variable
Bram Moolenaar080457c2020-03-03 21:53:32 +01003548 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 {
3550 if (oplen > 1 && !heredoc)
3551 {
3552 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3553 name);
3554 goto theend;
3555 }
3556
3557 // new local variable
3558 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3559 if (idx < 0)
3560 goto theend;
3561 }
3562
3563 if (heredoc)
3564 {
3565 list_T *l;
3566 listitem_T *li;
3567
3568 // [let] varname =<< [trim] {end}
3569 eap->getline = heredoc_getline;
3570 eap->cookie = cctx;
3571 l = heredoc_get(eap, op + 3);
3572
3573 // Push each line and the create the list.
3574 for (li = l->lv_first; li != NULL; li = li->li_next)
3575 {
3576 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3577 li->li_tv.vval.v_string = NULL;
3578 }
3579 generate_NEWLIST(cctx, l->lv_len);
3580 type = &t_list_string;
3581 list_free(l);
3582 p += STRLEN(p);
3583 }
3584 else if (oplen > 0)
3585 {
3586 // for "+=", "*=", "..=" etc. first load the current value
3587 if (*op != '=')
3588 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003589 switch (dest)
3590 {
3591 case dest_option:
3592 // TODO: check the option exists
3593 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3594 break;
3595 case dest_global:
3596 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3597 break;
3598 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003599 compile_load_scriptvar(cctx,
3600 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003601 break;
3602 case dest_env:
3603 // Include $ in the name here
3604 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3605 break;
3606 case dest_reg:
3607 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3608 break;
3609 case dest_vimvar:
3610 generate_LOADV(cctx, name + 2, TRUE);
3611 break;
3612 case dest_local:
3613 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3614 break;
3615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 }
3617
3618 // compile the expression
3619 instr_count = instr->ga_len;
3620 p = skipwhite(p + oplen);
3621 if (compile_expr1(&p, cctx) == FAIL)
3622 goto theend;
3623
3624 if (idx >= 0 && (is_decl || !has_type))
3625 {
3626 garray_T *stack = &cctx->ctx_type_stack;
3627 type_T *stacktype =
3628 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3629
3630 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3631 if (!has_type)
3632 {
3633 if (stacktype->tt_type == VAR_VOID)
3634 {
3635 emsg(_("E1031: Cannot use void value"));
3636 goto theend;
3637 }
3638 else
3639 lvar->lv_type = stacktype;
3640 }
3641 else
3642 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3643 goto theend;
3644 }
3645 }
3646 else if (cmdidx == CMD_const)
3647 {
3648 emsg(_("E1021: const requires a value"));
3649 goto theend;
3650 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003651 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 {
3653 emsg(_("E1022: type or initialization required"));
3654 goto theend;
3655 }
3656 else
3657 {
3658 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 if (ga_grow(instr, 1) == FAIL)
3660 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003661 switch (type->tt_type)
3662 {
3663 case VAR_BOOL:
3664 generate_PUSHBOOL(cctx, VVAL_FALSE);
3665 break;
3666 case VAR_SPECIAL:
3667 generate_PUSHSPEC(cctx, VVAL_NONE);
3668 break;
3669 case VAR_FLOAT:
3670#ifdef FEAT_FLOAT
3671 generate_PUSHF(cctx, 0.0);
3672#endif
3673 break;
3674 case VAR_STRING:
3675 generate_PUSHS(cctx, NULL);
3676 break;
3677 case VAR_BLOB:
3678 generate_PUSHBLOB(cctx, NULL);
3679 break;
3680 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003681 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003682 break;
3683 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003684 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003685 break;
3686 case VAR_LIST:
3687 generate_NEWLIST(cctx, 0);
3688 break;
3689 case VAR_DICT:
3690 generate_NEWDICT(cctx, 0);
3691 break;
3692 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003693 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003694 break;
3695 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003696 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003697 break;
3698 case VAR_NUMBER:
3699 case VAR_UNKNOWN:
3700 case VAR_VOID:
3701 generate_PUSHNR(cctx, 0);
3702 break;
3703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 }
3705
3706 if (oplen > 0 && *op != '=')
3707 {
3708 type_T *expected = &t_number;
3709 garray_T *stack = &cctx->ctx_type_stack;
3710 type_T *stacktype;
3711
3712 // TODO: if type is known use float or any operation
3713
3714 if (*op == '.')
3715 expected = &t_string;
3716 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3717 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3718 goto theend;
3719
3720 if (*op == '.')
3721 generate_instr_drop(cctx, ISN_CONCAT, 1);
3722 else
3723 {
3724 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3725
3726 if (isn == NULL)
3727 goto theend;
3728 switch (*op)
3729 {
3730 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3731 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3732 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3733 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3734 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3735 }
3736 }
3737 }
3738
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003739 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003741 case dest_option:
3742 generate_STOREOPT(cctx, name + 1, opt_flags);
3743 break;
3744 case dest_global:
3745 // include g: with the name, easier to execute that way
3746 generate_STORE(cctx, ISN_STOREG, 0, name);
3747 break;
3748 case dest_env:
3749 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3750 break;
3751 case dest_reg:
3752 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3753 break;
3754 case dest_vimvar:
3755 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3756 break;
3757 case dest_script:
3758 {
3759 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3760 imported_T *import = NULL;
3761 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003762
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003763 if (name[1] != ':')
3764 {
3765 import = find_imported(name, 0, cctx);
3766 if (import != NULL)
3767 sid = import->imp_sid;
3768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003770 idx = get_script_item_idx(sid, rawname, TRUE);
3771 // TODO: specific type
3772 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003773 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003774 else
3775 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3776 sid, idx, &t_any);
3777 }
3778 break;
3779 case dest_local:
3780 {
3781 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003783 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3784 // into ISN_STORENR
3785 if (instr->ga_len == instr_count + 1
3786 && isn->isn_type == ISN_PUSHNR)
3787 {
3788 varnumber_T val = isn->isn_arg.number;
3789 garray_T *stack = &cctx->ctx_type_stack;
3790
3791 isn->isn_type = ISN_STORENR;
3792 isn->isn_arg.storenr.str_idx = idx;
3793 isn->isn_arg.storenr.str_val = val;
3794 if (stack->ga_len > 0)
3795 --stack->ga_len;
3796 }
3797 else
3798 generate_STORE(cctx, ISN_STORE, idx, NULL);
3799 }
3800 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 }
3802 ret = p;
3803
3804theend:
3805 vim_free(name);
3806 return ret;
3807}
3808
3809/*
3810 * Compile an :import command.
3811 */
3812 static char_u *
3813compile_import(char_u *arg, cctx_T *cctx)
3814{
3815 return handle_import(arg, &cctx->ctx_imports, 0);
3816}
3817
3818/*
3819 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3820 */
3821 static int
3822compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3823{
3824 garray_T *instr = &cctx->ctx_instr;
3825 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3826
3827 if (endlabel == NULL)
3828 return FAIL;
3829 endlabel->el_next = *el;
3830 *el = endlabel;
3831 endlabel->el_end_label = instr->ga_len;
3832
3833 generate_JUMP(cctx, when, 0);
3834 return OK;
3835}
3836
3837 static void
3838compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3839{
3840 garray_T *instr = &cctx->ctx_instr;
3841
3842 while (*el != NULL)
3843 {
3844 endlabel_T *cur = (*el);
3845 isn_T *isn;
3846
3847 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3848 isn->isn_arg.jump.jump_where = instr->ga_len;
3849 *el = cur->el_next;
3850 vim_free(cur);
3851 }
3852}
3853
3854/*
3855 * Create a new scope and set up the generic items.
3856 */
3857 static scope_T *
3858new_scope(cctx_T *cctx, scopetype_T type)
3859{
3860 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3861
3862 if (scope == NULL)
3863 return NULL;
3864 scope->se_outer = cctx->ctx_scope;
3865 cctx->ctx_scope = scope;
3866 scope->se_type = type;
3867 scope->se_local_count = cctx->ctx_locals.ga_len;
3868 return scope;
3869}
3870
3871/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003872 * Evaluate an expression that is a constant:
3873 * has(arg)
3874 *
3875 * Also handle:
3876 * ! in front logical NOT
3877 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003878 * Return FAIL if the expression is not a constant.
3879 */
3880 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003881evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003882{
3883 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003884 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003885
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003886 /*
3887 * Skip '!' characters. They are handled later.
3888 */
3889 start_leader = *arg;
3890 while (**arg == '!')
3891 *arg = skipwhite(*arg + 1);
3892 end_leader = *arg;
3893
3894 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01003895 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003896 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01003897 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3898 {
3899 tv->v_type = VAR_SPECIAL;
3900 tv->vval.v_number = VVAL_TRUE;
3901 *arg += 4;
3902 return OK;
3903 }
3904 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3905 {
3906 tv->v_type = VAR_SPECIAL;
3907 tv->vval.v_number = VVAL_FALSE;
3908 *arg += 5;
3909 return OK;
3910 }
3911
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003912 if (STRNCMP("has(", *arg, 4) != 0)
3913 return FAIL;
3914 *arg = skipwhite(*arg + 4);
3915
3916 if (**arg == '"')
3917 {
3918 if (get_string_tv(arg, tv, TRUE) == FAIL)
3919 return FAIL;
3920 }
3921 else if (**arg == '\'')
3922 {
3923 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3924 return FAIL;
3925 }
3926 else
3927 return FAIL;
3928
3929 *arg = skipwhite(*arg);
3930 if (**arg != ')')
3931 return FAIL;
3932 *arg = skipwhite(*arg + 1);
3933
3934 argvars[0] = *tv;
3935 argvars[1].v_type = VAR_UNKNOWN;
3936 tv->v_type = VAR_NUMBER;
3937 tv->vval.v_number = 0;
3938 f_has(argvars, tv);
3939 clear_tv(&argvars[0]);
3940
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003941 while (start_leader < end_leader)
3942 {
3943 if (*start_leader == '!')
3944 tv->vval.v_number = !tv->vval.v_number;
3945 ++start_leader;
3946 }
3947
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003948 return OK;
3949}
3950
Bram Moolenaar080457c2020-03-03 21:53:32 +01003951 static int
3952evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3953{
3954 exptype_T type = EXPR_UNKNOWN;
3955 char_u *p;
3956 int len = 2;
3957 int type_is = FALSE;
3958
3959 // get the first variable
3960 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3961 return FAIL;
3962
3963 p = skipwhite(*arg);
3964 type = get_compare_type(p, &len, &type_is);
3965
3966 /*
3967 * If there is a comparative operator, use it.
3968 */
3969 if (type != EXPR_UNKNOWN)
3970 {
3971 // TODO
3972 return FAIL;
3973 }
3974
3975 return OK;
3976}
3977
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003978static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3979
3980/*
3981 * Compile constant || or &&.
3982 */
3983 static int
3984evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3985{
3986 char_u *p = skipwhite(*arg);
3987 int opchar = *op;
3988
3989 if (p[0] == opchar && p[1] == opchar)
3990 {
3991 int val = tv2bool(tv);
3992
3993 /*
3994 * Repeat until there is no following "||" or "&&"
3995 */
3996 while (p[0] == opchar && p[1] == opchar)
3997 {
3998 typval_T tv2;
3999
4000 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4001 return FAIL;
4002
4003 // eval the next expression
4004 *arg = skipwhite(p + 2);
4005 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004006 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004007 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004008 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004009 {
4010 clear_tv(&tv2);
4011 return FAIL;
4012 }
4013 if ((opchar == '&') == val)
4014 {
4015 // false || tv2 or true && tv2: use tv2
4016 clear_tv(tv);
4017 *tv = tv2;
4018 val = tv2bool(tv);
4019 }
4020 else
4021 clear_tv(&tv2);
4022 p = skipwhite(*arg);
4023 }
4024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4031 * Return FAIL if the expression is not a constant.
4032 */
4033 static int
4034evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4035{
4036 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004037 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004038 return FAIL;
4039
4040 // || and && work almost the same
4041 return evaluate_const_and_or(arg, cctx, "&&", tv);
4042}
4043
4044/*
4045 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4046 * Return FAIL if the expression is not a constant.
4047 */
4048 static int
4049evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4050{
4051 // evaluate the first expression
4052 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4053 return FAIL;
4054
4055 // || and && work almost the same
4056 return evaluate_const_and_or(arg, cctx, "||", tv);
4057}
4058
4059/*
4060 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4061 * E.g. for "has('feature')".
4062 * This does not produce error messages. "tv" should be cleared afterwards.
4063 * Return FAIL if the expression is not a constant.
4064 */
4065 static int
4066evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4067{
4068 char_u *p;
4069
4070 // evaluate the first expression
4071 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4072 return FAIL;
4073
4074 p = skipwhite(*arg);
4075 if (*p == '?')
4076 {
4077 int val = tv2bool(tv);
4078 typval_T tv2;
4079
4080 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4081 return FAIL;
4082
4083 // evaluate the second expression; any type is accepted
4084 clear_tv(tv);
4085 *arg = skipwhite(p + 1);
4086 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4087 return FAIL;
4088
4089 // Check for the ":".
4090 p = skipwhite(*arg);
4091 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4092 return FAIL;
4093
4094 // evaluate the third expression
4095 *arg = skipwhite(p + 1);
4096 tv2.v_type = VAR_UNKNOWN;
4097 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4098 {
4099 clear_tv(&tv2);
4100 return FAIL;
4101 }
4102 if (val)
4103 {
4104 // use the expr after "?"
4105 clear_tv(&tv2);
4106 }
4107 else
4108 {
4109 // use the expr after ":"
4110 clear_tv(tv);
4111 *tv = tv2;
4112 }
4113 }
4114 return OK;
4115}
4116
4117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 * compile "if expr"
4119 *
4120 * "if expr" Produces instructions:
4121 * EVAL expr Push result of "expr"
4122 * JUMP_IF_FALSE end
4123 * ... body ...
4124 * end:
4125 *
4126 * "if expr | else" Produces instructions:
4127 * EVAL expr Push result of "expr"
4128 * JUMP_IF_FALSE else
4129 * ... body ...
4130 * JUMP_ALWAYS end
4131 * else:
4132 * ... body ...
4133 * end:
4134 *
4135 * "if expr1 | elseif expr2 | else" Produces instructions:
4136 * EVAL expr Push result of "expr"
4137 * JUMP_IF_FALSE elseif
4138 * ... body ...
4139 * JUMP_ALWAYS end
4140 * elseif:
4141 * EVAL expr Push result of "expr"
4142 * JUMP_IF_FALSE else
4143 * ... body ...
4144 * JUMP_ALWAYS end
4145 * else:
4146 * ... body ...
4147 * end:
4148 */
4149 static char_u *
4150compile_if(char_u *arg, cctx_T *cctx)
4151{
4152 char_u *p = arg;
4153 garray_T *instr = &cctx->ctx_instr;
4154 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004155 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004157 // compile "expr"; if we know it evaluates to FALSE skip the block
4158 tv.v_type = VAR_UNKNOWN;
4159 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4160 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4161 else
4162 cctx->ctx_skip = MAYBE;
4163 clear_tv(&tv);
4164 if (cctx->ctx_skip == MAYBE)
4165 {
4166 p = arg;
4167 if (compile_expr1(&p, cctx) == FAIL)
4168 return NULL;
4169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170
4171 scope = new_scope(cctx, IF_SCOPE);
4172 if (scope == NULL)
4173 return NULL;
4174
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004175 if (cctx->ctx_skip == MAYBE)
4176 {
4177 // "where" is set when ":elseif", "else" or ":endif" is found
4178 scope->se_u.se_if.is_if_label = instr->ga_len;
4179 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4180 }
4181 else
4182 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
4184 return p;
4185}
4186
4187 static char_u *
4188compile_elseif(char_u *arg, cctx_T *cctx)
4189{
4190 char_u *p = arg;
4191 garray_T *instr = &cctx->ctx_instr;
4192 isn_T *isn;
4193 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004194 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195
4196 if (scope == NULL || scope->se_type != IF_SCOPE)
4197 {
4198 emsg(_(e_elseif_without_if));
4199 return NULL;
4200 }
4201 cctx->ctx_locals.ga_len = scope->se_local_count;
4202
Bram Moolenaar158906c2020-02-06 20:39:45 +01004203 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004204 {
4205 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004207 return NULL;
4208 // previous "if" or "elseif" jumps here
4209 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4210 isn->isn_arg.jump.jump_where = instr->ga_len;
4211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004213 // compile "expr"; if we know it evaluates to FALSE skip the block
4214 tv.v_type = VAR_UNKNOWN;
4215 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4216 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4217 else
4218 cctx->ctx_skip = MAYBE;
4219 clear_tv(&tv);
4220 if (cctx->ctx_skip == MAYBE)
4221 {
4222 p = arg;
4223 if (compile_expr1(&p, cctx) == FAIL)
4224 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004226 // "where" is set when ":elseif", "else" or ":endif" is found
4227 scope->se_u.se_if.is_if_label = instr->ga_len;
4228 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4229 }
4230 else
4231 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
4233 return p;
4234}
4235
4236 static char_u *
4237compile_else(char_u *arg, cctx_T *cctx)
4238{
4239 char_u *p = arg;
4240 garray_T *instr = &cctx->ctx_instr;
4241 isn_T *isn;
4242 scope_T *scope = cctx->ctx_scope;
4243
4244 if (scope == NULL || scope->se_type != IF_SCOPE)
4245 {
4246 emsg(_(e_else_without_if));
4247 return NULL;
4248 }
4249 cctx->ctx_locals.ga_len = scope->se_local_count;
4250
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004251 // jump from previous block to the end, unless the else block is empty
4252 if (cctx->ctx_skip == MAYBE)
4253 {
4254 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004256 return NULL;
4257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
Bram Moolenaar158906c2020-02-06 20:39:45 +01004259 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004260 {
4261 if (scope->se_u.se_if.is_if_label >= 0)
4262 {
4263 // previous "if" or "elseif" jumps here
4264 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4265 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004266 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004267 }
4268 }
4269
4270 if (cctx->ctx_skip != MAYBE)
4271 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
4273 return p;
4274}
4275
4276 static char_u *
4277compile_endif(char_u *arg, cctx_T *cctx)
4278{
4279 scope_T *scope = cctx->ctx_scope;
4280 ifscope_T *ifscope;
4281 garray_T *instr = &cctx->ctx_instr;
4282 isn_T *isn;
4283
4284 if (scope == NULL || scope->se_type != IF_SCOPE)
4285 {
4286 emsg(_(e_endif_without_if));
4287 return NULL;
4288 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004289 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 cctx->ctx_scope = scope->se_outer;
4291 cctx->ctx_locals.ga_len = scope->se_local_count;
4292
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004293 if (scope->se_u.se_if.is_if_label >= 0)
4294 {
4295 // previous "if" or "elseif" jumps here
4296 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4297 isn->isn_arg.jump.jump_where = instr->ga_len;
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 // Fill in the "end" label in jumps at the end of the blocks.
4300 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004301 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
4303 vim_free(scope);
4304 return arg;
4305}
4306
4307/*
4308 * compile "for var in expr"
4309 *
4310 * Produces instructions:
4311 * PUSHNR -1
4312 * STORE loop-idx Set index to -1
4313 * EVAL expr Push result of "expr"
4314 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4315 * - if beyond end, jump to "end"
4316 * - otherwise get item from list and push it
4317 * STORE var Store item in "var"
4318 * ... body ...
4319 * JUMP top Jump back to repeat
4320 * end: DROP Drop the result of "expr"
4321 *
4322 */
4323 static char_u *
4324compile_for(char_u *arg, cctx_T *cctx)
4325{
4326 char_u *p;
4327 size_t varlen;
4328 garray_T *instr = &cctx->ctx_instr;
4329 garray_T *stack = &cctx->ctx_type_stack;
4330 scope_T *scope;
4331 int loop_idx; // index of loop iteration variable
4332 int var_idx; // index of "var"
4333 type_T *vartype;
4334
4335 // TODO: list of variables: "for [key, value] in dict"
4336 // parse "var"
4337 for (p = arg; eval_isnamec1(*p); ++p)
4338 ;
4339 varlen = p - arg;
4340 var_idx = lookup_local(arg, varlen, cctx);
4341 if (var_idx >= 0)
4342 {
4343 semsg(_("E1023: variable already defined: %s"), arg);
4344 return NULL;
4345 }
4346
4347 // consume "in"
4348 p = skipwhite(p);
4349 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4350 {
4351 emsg(_(e_missing_in));
4352 return NULL;
4353 }
4354 p = skipwhite(p + 2);
4355
4356
4357 scope = new_scope(cctx, FOR_SCOPE);
4358 if (scope == NULL)
4359 return NULL;
4360
4361 // Reserve a variable to store the loop iteration counter.
4362 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4363 if (loop_idx < 0)
4364 return NULL;
4365
4366 // Reserve a variable to store "var"
4367 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4368 if (var_idx < 0)
4369 return NULL;
4370
4371 generate_STORENR(cctx, loop_idx, -1);
4372
4373 // compile "expr", it remains on the stack until "endfor"
4374 arg = p;
4375 if (compile_expr1(&arg, cctx) == FAIL)
4376 return NULL;
4377
4378 // now we know the type of "var"
4379 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4380 if (vartype->tt_type != VAR_LIST)
4381 {
4382 emsg(_("E1024: need a List to iterate over"));
4383 return NULL;
4384 }
4385 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4386 {
4387 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4388
4389 lvar->lv_type = vartype->tt_member;
4390 }
4391
4392 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004393 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
4395 generate_FOR(cctx, loop_idx);
4396 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4397
4398 return arg;
4399}
4400
4401/*
4402 * compile "endfor"
4403 */
4404 static char_u *
4405compile_endfor(char_u *arg, cctx_T *cctx)
4406{
4407 garray_T *instr = &cctx->ctx_instr;
4408 scope_T *scope = cctx->ctx_scope;
4409 forscope_T *forscope;
4410 isn_T *isn;
4411
4412 if (scope == NULL || scope->se_type != FOR_SCOPE)
4413 {
4414 emsg(_(e_for));
4415 return NULL;
4416 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004417 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 cctx->ctx_scope = scope->se_outer;
4419 cctx->ctx_locals.ga_len = scope->se_local_count;
4420
4421 // At end of ":for" scope jump back to the FOR instruction.
4422 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4423
4424 // Fill in the "end" label in the FOR statement so it can jump here
4425 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4426 isn->isn_arg.forloop.for_end = instr->ga_len;
4427
4428 // Fill in the "end" label any BREAK statements
4429 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4430
4431 // Below the ":for" scope drop the "expr" list from the stack.
4432 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4433 return NULL;
4434
4435 vim_free(scope);
4436
4437 return arg;
4438}
4439
4440/*
4441 * compile "while expr"
4442 *
4443 * Produces instructions:
4444 * top: EVAL expr Push result of "expr"
4445 * JUMP_IF_FALSE end jump if false
4446 * ... body ...
4447 * JUMP top Jump back to repeat
4448 * end:
4449 *
4450 */
4451 static char_u *
4452compile_while(char_u *arg, cctx_T *cctx)
4453{
4454 char_u *p = arg;
4455 garray_T *instr = &cctx->ctx_instr;
4456 scope_T *scope;
4457
4458 scope = new_scope(cctx, WHILE_SCOPE);
4459 if (scope == NULL)
4460 return NULL;
4461
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004462 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463
4464 // compile "expr"
4465 if (compile_expr1(&p, cctx) == FAIL)
4466 return NULL;
4467
4468 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004469 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 JUMP_IF_FALSE, cctx) == FAIL)
4471 return FAIL;
4472
4473 return p;
4474}
4475
4476/*
4477 * compile "endwhile"
4478 */
4479 static char_u *
4480compile_endwhile(char_u *arg, cctx_T *cctx)
4481{
4482 scope_T *scope = cctx->ctx_scope;
4483
4484 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4485 {
4486 emsg(_(e_while));
4487 return NULL;
4488 }
4489 cctx->ctx_scope = scope->se_outer;
4490 cctx->ctx_locals.ga_len = scope->se_local_count;
4491
4492 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004493 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494
4495 // Fill in the "end" label in the WHILE statement so it can jump here.
4496 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004497 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
4499 vim_free(scope);
4500
4501 return arg;
4502}
4503
4504/*
4505 * compile "continue"
4506 */
4507 static char_u *
4508compile_continue(char_u *arg, cctx_T *cctx)
4509{
4510 scope_T *scope = cctx->ctx_scope;
4511
4512 for (;;)
4513 {
4514 if (scope == NULL)
4515 {
4516 emsg(_(e_continue));
4517 return NULL;
4518 }
4519 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4520 break;
4521 scope = scope->se_outer;
4522 }
4523
4524 // Jump back to the FOR or WHILE instruction.
4525 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004526 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4527 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 return arg;
4529}
4530
4531/*
4532 * compile "break"
4533 */
4534 static char_u *
4535compile_break(char_u *arg, cctx_T *cctx)
4536{
4537 scope_T *scope = cctx->ctx_scope;
4538 endlabel_T **el;
4539
4540 for (;;)
4541 {
4542 if (scope == NULL)
4543 {
4544 emsg(_(e_break));
4545 return NULL;
4546 }
4547 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4548 break;
4549 scope = scope->se_outer;
4550 }
4551
4552 // Jump to the end of the FOR or WHILE loop.
4553 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004554 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004556 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4558 return FAIL;
4559
4560 return arg;
4561}
4562
4563/*
4564 * compile "{" start of block
4565 */
4566 static char_u *
4567compile_block(char_u *arg, cctx_T *cctx)
4568{
4569 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4570 return NULL;
4571 return skipwhite(arg + 1);
4572}
4573
4574/*
4575 * compile end of block: drop one scope
4576 */
4577 static void
4578compile_endblock(cctx_T *cctx)
4579{
4580 scope_T *scope = cctx->ctx_scope;
4581
4582 cctx->ctx_scope = scope->se_outer;
4583 cctx->ctx_locals.ga_len = scope->se_local_count;
4584 vim_free(scope);
4585}
4586
4587/*
4588 * compile "try"
4589 * Creates a new scope for the try-endtry, pointing to the first catch and
4590 * finally.
4591 * Creates another scope for the "try" block itself.
4592 * TRY instruction sets up exception handling at runtime.
4593 *
4594 * "try"
4595 * TRY -> catch1, -> finally push trystack entry
4596 * ... try block
4597 * "throw {exception}"
4598 * EVAL {exception}
4599 * THROW create exception
4600 * ... try block
4601 * " catch {expr}"
4602 * JUMP -> finally
4603 * catch1: PUSH exeception
4604 * EVAL {expr}
4605 * MATCH
4606 * JUMP nomatch -> catch2
4607 * CATCH remove exception
4608 * ... catch block
4609 * " catch"
4610 * JUMP -> finally
4611 * catch2: CATCH remove exception
4612 * ... catch block
4613 * " finally"
4614 * finally:
4615 * ... finally block
4616 * " endtry"
4617 * ENDTRY pop trystack entry, may rethrow
4618 */
4619 static char_u *
4620compile_try(char_u *arg, cctx_T *cctx)
4621{
4622 garray_T *instr = &cctx->ctx_instr;
4623 scope_T *try_scope;
4624 scope_T *scope;
4625
4626 // scope that holds the jumps that go to catch/finally/endtry
4627 try_scope = new_scope(cctx, TRY_SCOPE);
4628 if (try_scope == NULL)
4629 return NULL;
4630
4631 // "catch" is set when the first ":catch" is found.
4632 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004633 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 if (generate_instr(cctx, ISN_TRY) == NULL)
4635 return NULL;
4636
4637 // scope for the try block itself
4638 scope = new_scope(cctx, BLOCK_SCOPE);
4639 if (scope == NULL)
4640 return NULL;
4641
4642 return arg;
4643}
4644
4645/*
4646 * compile "catch {expr}"
4647 */
4648 static char_u *
4649compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4650{
4651 scope_T *scope = cctx->ctx_scope;
4652 garray_T *instr = &cctx->ctx_instr;
4653 char_u *p;
4654 isn_T *isn;
4655
4656 // end block scope from :try or :catch
4657 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4658 compile_endblock(cctx);
4659 scope = cctx->ctx_scope;
4660
4661 // Error if not in a :try scope
4662 if (scope == NULL || scope->se_type != TRY_SCOPE)
4663 {
4664 emsg(_(e_catch));
4665 return NULL;
4666 }
4667
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004668 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 {
4670 emsg(_("E1033: catch unreachable after catch-all"));
4671 return NULL;
4672 }
4673
4674 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004675 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 JUMP_ALWAYS, cctx) == FAIL)
4677 return NULL;
4678
4679 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004680 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 if (isn->isn_arg.try.try_catch == 0)
4682 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004683 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 {
4685 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004686 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 isn->isn_arg.jump.jump_where = instr->ga_len;
4688 }
4689
4690 p = skipwhite(arg);
4691 if (ends_excmd(*p))
4692 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004693 scope->se_u.se_try.ts_caught_all = TRUE;
4694 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 }
4696 else
4697 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004698 char_u *end;
4699 char_u *pat;
4700 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004701 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 // Push v:exception, push {expr} and MATCH
4704 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4705
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004706 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4707 if (*end != *p)
4708 {
4709 semsg(_("E1067: Separator mismatch: %s"), p);
4710 vim_free(tofree);
4711 return FAIL;
4712 }
4713 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004714 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004715 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004716 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004717 pat = vim_strnsave(p + 1, len);
4718 vim_free(tofree);
4719 p += len + 2;
4720 if (pat == NULL)
4721 return FAIL;
4722 if (generate_PUSHS(cctx, pat) == FAIL)
4723 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4726 return NULL;
4727
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004728 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4730 return NULL;
4731 }
4732
4733 if (generate_instr(cctx, ISN_CATCH) == NULL)
4734 return NULL;
4735
4736 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4737 return NULL;
4738 return p;
4739}
4740
4741 static char_u *
4742compile_finally(char_u *arg, cctx_T *cctx)
4743{
4744 scope_T *scope = cctx->ctx_scope;
4745 garray_T *instr = &cctx->ctx_instr;
4746 isn_T *isn;
4747
4748 // end block scope from :try or :catch
4749 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4750 compile_endblock(cctx);
4751 scope = cctx->ctx_scope;
4752
4753 // Error if not in a :try scope
4754 if (scope == NULL || scope->se_type != TRY_SCOPE)
4755 {
4756 emsg(_(e_finally));
4757 return NULL;
4758 }
4759
4760 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004761 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 if (isn->isn_arg.try.try_finally != 0)
4763 {
4764 emsg(_(e_finally_dup));
4765 return NULL;
4766 }
4767
4768 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004769 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004771 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 {
4773 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004774 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 isn->isn_arg.jump.jump_where = instr->ga_len;
4776 }
4777
4778 isn->isn_arg.try.try_finally = instr->ga_len;
4779 // TODO: set index in ts_finally_label jumps
4780
4781 return arg;
4782}
4783
4784 static char_u *
4785compile_endtry(char_u *arg, cctx_T *cctx)
4786{
4787 scope_T *scope = cctx->ctx_scope;
4788 garray_T *instr = &cctx->ctx_instr;
4789 isn_T *isn;
4790
4791 // end block scope from :catch or :finally
4792 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4793 compile_endblock(cctx);
4794 scope = cctx->ctx_scope;
4795
4796 // Error if not in a :try scope
4797 if (scope == NULL || scope->se_type != TRY_SCOPE)
4798 {
4799 if (scope == NULL)
4800 emsg(_(e_no_endtry));
4801 else if (scope->se_type == WHILE_SCOPE)
4802 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004803 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 emsg(_(e_endfor));
4805 else
4806 emsg(_(e_endif));
4807 return NULL;
4808 }
4809
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004810 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4812 {
4813 emsg(_("E1032: missing :catch or :finally"));
4814 return NULL;
4815 }
4816
4817 // Fill in the "end" label in jumps at the end of the blocks, if not done
4818 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004819 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820
4821 // End :catch or :finally scope: set value in ISN_TRY instruction
4822 if (isn->isn_arg.try.try_finally == 0)
4823 isn->isn_arg.try.try_finally = instr->ga_len;
4824 compile_endblock(cctx);
4825
4826 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4827 return NULL;
4828 return arg;
4829}
4830
4831/*
4832 * compile "throw {expr}"
4833 */
4834 static char_u *
4835compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4836{
4837 char_u *p = skipwhite(arg);
4838
4839 if (ends_excmd(*p))
4840 {
4841 emsg(_(e_argreq));
4842 return NULL;
4843 }
4844 if (compile_expr1(&p, cctx) == FAIL)
4845 return NULL;
4846 if (may_generate_2STRING(-1, cctx) == FAIL)
4847 return NULL;
4848 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4849 return NULL;
4850
4851 return p;
4852}
4853
4854/*
4855 * compile "echo expr"
4856 */
4857 static char_u *
4858compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4859{
4860 char_u *p = arg;
4861 int count = 0;
4862
Bram Moolenaarad39c092020-02-26 18:23:43 +01004863 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 {
4865 if (compile_expr1(&p, cctx) == FAIL)
4866 return NULL;
4867 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004868 p = skipwhite(p);
4869 if (ends_excmd(*p))
4870 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 }
4872
4873 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004874 return p;
4875}
4876
4877/*
4878 * compile "execute expr"
4879 */
4880 static char_u *
4881compile_execute(char_u *arg, cctx_T *cctx)
4882{
4883 char_u *p = arg;
4884 int count = 0;
4885
4886 for (;;)
4887 {
4888 if (compile_expr1(&p, cctx) == FAIL)
4889 return NULL;
4890 ++count;
4891 p = skipwhite(p);
4892 if (ends_excmd(*p))
4893 break;
4894 }
4895
4896 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897
4898 return p;
4899}
4900
4901/*
4902 * After ex_function() has collected all the function lines: parse and compile
4903 * the lines into instructions.
4904 * Adds the function to "def_functions".
4905 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4906 * return statement (used for lambda).
4907 */
4908 void
4909compile_def_function(ufunc_T *ufunc, int set_return_type)
4910{
4911 dfunc_T *dfunc;
4912 char_u *line = NULL;
4913 char_u *p;
4914 exarg_T ea;
4915 char *errormsg = NULL; // error message
4916 int had_return = FALSE;
4917 cctx_T cctx;
4918 garray_T *instr;
4919 int called_emsg_before = called_emsg;
4920 int ret = FAIL;
4921 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004922 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923
4924 if (ufunc->uf_dfunc_idx >= 0)
4925 {
4926 // redefining a function that was compiled before
4927 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4928 dfunc->df_deleted = FALSE;
4929 }
4930 else
4931 {
4932 // Add the function to "def_functions".
4933 if (ga_grow(&def_functions, 1) == FAIL)
4934 return;
4935 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4936 vim_memset(dfunc, 0, sizeof(dfunc_T));
4937 dfunc->df_idx = def_functions.ga_len;
4938 ufunc->uf_dfunc_idx = dfunc->df_idx;
4939 dfunc->df_ufunc = ufunc;
4940 ++def_functions.ga_len;
4941 }
4942
4943 vim_memset(&cctx, 0, sizeof(cctx));
4944 cctx.ctx_ufunc = ufunc;
4945 cctx.ctx_lnum = -1;
4946 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4947 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4948 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4949 cctx.ctx_type_list = &ufunc->uf_type_list;
4950 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4951 instr = &cctx.ctx_instr;
4952
4953 // Most modern script version.
4954 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4955
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004956 if (ufunc->uf_def_args.ga_len > 0)
4957 {
4958 int count = ufunc->uf_def_args.ga_len;
4959 int i;
4960 char_u *arg;
4961 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4962
4963 // Produce instructions for the default values of optional arguments.
4964 // Store the instruction index in uf_def_arg_idx[] so that we know
4965 // where to start when the function is called, depending on the number
4966 // of arguments.
4967 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4968 if (ufunc->uf_def_arg_idx == NULL)
4969 goto erret;
4970 for (i = 0; i < count; ++i)
4971 {
4972 ufunc->uf_def_arg_idx[i] = instr->ga_len;
4973 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4974 if (compile_expr1(&arg, &cctx) == FAIL
4975 || generate_STORE(&cctx, ISN_STORE,
4976 i - count - off, NULL) == FAIL)
4977 goto erret;
4978 }
4979
4980 // If a varargs is following, push an empty list.
4981 if (ufunc->uf_va_name != NULL)
4982 {
4983 if (generate_NEWLIST(&cctx, 0) == FAIL
4984 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4985 goto erret;
4986 }
4987
4988 ufunc->uf_def_arg_idx[count] = instr->ga_len;
4989 }
4990
4991 /*
4992 * Loop over all the lines of the function and generate instructions.
4993 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 for (;;)
4995 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004996 int is_ex_command;
4997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 if (line != NULL && *line == '|')
4999 // the line continues after a '|'
5000 ++line;
5001 else if (line != NULL && *line != NUL)
5002 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005003 if (emsg_before == called_emsg)
5004 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 goto erret;
5006 }
5007 else
5008 {
5009 do
5010 {
5011 ++cctx.ctx_lnum;
5012 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5013 break;
5014 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5015 } while (line == NULL);
5016 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5017 break;
5018 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5019 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005020 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021
5022 had_return = FALSE;
5023 vim_memset(&ea, 0, sizeof(ea));
5024 ea.cmdlinep = &line;
5025 ea.cmd = skipwhite(line);
5026
5027 // "}" ends a block scope
5028 if (*ea.cmd == '}')
5029 {
5030 scopetype_T stype = cctx.ctx_scope == NULL
5031 ? NO_SCOPE : cctx.ctx_scope->se_type;
5032
5033 if (stype == BLOCK_SCOPE)
5034 {
5035 compile_endblock(&cctx);
5036 line = ea.cmd;
5037 }
5038 else
5039 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005040 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 goto erret;
5042 }
5043 if (line != NULL)
5044 line = skipwhite(ea.cmd + 1);
5045 continue;
5046 }
5047
5048 // "{" starts a block scope
5049 if (*ea.cmd == '{')
5050 {
5051 line = compile_block(ea.cmd, &cctx);
5052 continue;
5053 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005054 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
5056 /*
5057 * COMMAND MODIFIERS
5058 */
5059 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5060 {
5061 if (errormsg != NULL)
5062 goto erret;
5063 // empty line or comment
5064 line = (char_u *)"";
5065 continue;
5066 }
5067
5068 // Skip ":call" to get to the function name.
5069 if (checkforcmd(&ea.cmd, "call", 3))
5070 ea.cmd = skipwhite(ea.cmd);
5071
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005072 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005074 // Assuming the command starts with a variable or function name,
5075 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5076 // val".
5077 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5078 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005079 p = to_name_end(p, TRUE);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01005080 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005082 int oplen;
5083 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005085 oplen = assignment_len(skipwhite(p), &heredoc);
5086 if (oplen > 0)
5087 {
5088 // Recognize an assignment if we recognize the variable
5089 // name:
5090 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005091 // "local = expr" where "local" is a local var.
5092 // "script = expr" where "script" is a script-local var.
5093 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005094 // "&opt = expr"
5095 // "$ENV = expr"
5096 // "@r = expr"
5097 if (*ea.cmd == '&'
5098 || *ea.cmd == '$'
5099 || *ea.cmd == '@'
5100 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5101 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5102 || lookup_script(ea.cmd, p - ea.cmd) == OK
5103 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5104 {
5105 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5106 if (line == NULL)
5107 goto erret;
5108 continue;
5109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 }
5111 }
5112 }
5113
5114 /*
5115 * COMMAND after range
5116 */
5117 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005118 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5119 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120
5121 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5122 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005123 if (cctx.ctx_skip == TRUE)
5124 {
5125 line += STRLEN(line);
5126 continue;
5127 }
5128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 // Expression or function call.
5130 if (ea.cmdidx == CMD_eval)
5131 {
5132 p = ea.cmd;
5133 if (compile_expr1(&p, &cctx) == FAIL)
5134 goto erret;
5135
5136 // drop the return value
5137 generate_instr_drop(&cctx, ISN_DROP, 1);
5138 line = p;
5139 continue;
5140 }
5141 if (ea.cmdidx == CMD_let)
5142 {
5143 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5144 if (line == NULL)
5145 goto erret;
5146 continue;
5147 }
5148 iemsg("Command from find_ex_command() not handled");
5149 goto erret;
5150 }
5151
5152 p = skipwhite(p);
5153
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005154 if (cctx.ctx_skip == TRUE
5155 && ea.cmdidx != CMD_elseif
5156 && ea.cmdidx != CMD_else
5157 && ea.cmdidx != CMD_endif)
5158 {
5159 line += STRLEN(line);
5160 continue;
5161 }
5162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 switch (ea.cmdidx)
5164 {
5165 case CMD_def:
5166 case CMD_function:
5167 // TODO: Nested function
5168 emsg("Nested function not implemented yet");
5169 goto erret;
5170
5171 case CMD_return:
5172 line = compile_return(p, set_return_type, &cctx);
5173 had_return = TRUE;
5174 break;
5175
5176 case CMD_let:
5177 case CMD_const:
5178 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5179 break;
5180
5181 case CMD_import:
5182 line = compile_import(p, &cctx);
5183 break;
5184
5185 case CMD_if:
5186 line = compile_if(p, &cctx);
5187 break;
5188 case CMD_elseif:
5189 line = compile_elseif(p, &cctx);
5190 break;
5191 case CMD_else:
5192 line = compile_else(p, &cctx);
5193 break;
5194 case CMD_endif:
5195 line = compile_endif(p, &cctx);
5196 break;
5197
5198 case CMD_while:
5199 line = compile_while(p, &cctx);
5200 break;
5201 case CMD_endwhile:
5202 line = compile_endwhile(p, &cctx);
5203 break;
5204
5205 case CMD_for:
5206 line = compile_for(p, &cctx);
5207 break;
5208 case CMD_endfor:
5209 line = compile_endfor(p, &cctx);
5210 break;
5211 case CMD_continue:
5212 line = compile_continue(p, &cctx);
5213 break;
5214 case CMD_break:
5215 line = compile_break(p, &cctx);
5216 break;
5217
5218 case CMD_try:
5219 line = compile_try(p, &cctx);
5220 break;
5221 case CMD_catch:
5222 line = compile_catch(p, &cctx);
5223 break;
5224 case CMD_finally:
5225 line = compile_finally(p, &cctx);
5226 break;
5227 case CMD_endtry:
5228 line = compile_endtry(p, &cctx);
5229 break;
5230 case CMD_throw:
5231 line = compile_throw(p, &cctx);
5232 break;
5233
5234 case CMD_echo:
5235 line = compile_echo(p, TRUE, &cctx);
5236 break;
5237 case CMD_echon:
5238 line = compile_echo(p, FALSE, &cctx);
5239 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005240 case CMD_execute:
5241 line = compile_execute(p, &cctx);
5242 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
5244 default:
5245 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005246 // TODO:
5247 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005248 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 generate_EXEC(&cctx, line);
5250 line = (char_u *)"";
5251 break;
5252 }
5253 if (line == NULL)
5254 goto erret;
5255
5256 if (cctx.ctx_type_stack.ga_len < 0)
5257 {
5258 iemsg("Type stack underflow");
5259 goto erret;
5260 }
5261 }
5262
5263 if (cctx.ctx_scope != NULL)
5264 {
5265 if (cctx.ctx_scope->se_type == IF_SCOPE)
5266 emsg(_(e_endif));
5267 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5268 emsg(_(e_endwhile));
5269 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5270 emsg(_(e_endfor));
5271 else
5272 emsg(_("E1026: Missing }"));
5273 goto erret;
5274 }
5275
5276 if (!had_return)
5277 {
5278 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5279 {
5280 emsg(_("E1027: Missing return statement"));
5281 goto erret;
5282 }
5283
5284 // Return zero if there is no return at the end.
5285 generate_PUSHNR(&cctx, 0);
5286 generate_instr(&cctx, ISN_RETURN);
5287 }
5288
5289 dfunc->df_instr = instr->ga_data;
5290 dfunc->df_instr_count = instr->ga_len;
5291 dfunc->df_varcount = cctx.ctx_max_local;
5292
5293 ret = OK;
5294
5295erret:
5296 if (ret == FAIL)
5297 {
5298 ga_clear(instr);
5299 ufunc->uf_dfunc_idx = -1;
5300 --def_functions.ga_len;
5301 if (errormsg != NULL)
5302 emsg(errormsg);
5303 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005304 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305
5306 // don't execute this function body
5307 ufunc->uf_lines.ga_len = 0;
5308 }
5309
5310 current_sctx = save_current_sctx;
5311 ga_clear(&cctx.ctx_type_stack);
5312 ga_clear(&cctx.ctx_locals);
5313}
5314
5315/*
5316 * Delete an instruction, free what it contains.
5317 */
5318 static void
5319delete_instr(isn_T *isn)
5320{
5321 switch (isn->isn_type)
5322 {
5323 case ISN_EXEC:
5324 case ISN_LOADENV:
5325 case ISN_LOADG:
5326 case ISN_LOADOPT:
5327 case ISN_MEMBER:
5328 case ISN_PUSHEXC:
5329 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005330 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005332 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 vim_free(isn->isn_arg.string);
5334 break;
5335
5336 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005337 case ISN_STORES:
5338 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 break;
5340
5341 case ISN_STOREOPT:
5342 vim_free(isn->isn_arg.storeopt.so_name);
5343 break;
5344
5345 case ISN_PUSHBLOB: // push blob isn_arg.blob
5346 blob_unref(isn->isn_arg.blob);
5347 break;
5348
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005349 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005350 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005351 break;
5352
5353 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005354#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005355 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005356#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005357 break;
5358
5359 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005360#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005361 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005362#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005363 break;
5364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 case ISN_UCALL:
5366 vim_free(isn->isn_arg.ufunc.cuf_name);
5367 break;
5368
5369 case ISN_2BOOL:
5370 case ISN_2STRING:
5371 case ISN_ADDBLOB:
5372 case ISN_ADDLIST:
5373 case ISN_BCALL:
5374 case ISN_CATCH:
5375 case ISN_CHECKNR:
5376 case ISN_CHECKTYPE:
5377 case ISN_COMPAREANY:
5378 case ISN_COMPAREBLOB:
5379 case ISN_COMPAREBOOL:
5380 case ISN_COMPAREDICT:
5381 case ISN_COMPAREFLOAT:
5382 case ISN_COMPAREFUNC:
5383 case ISN_COMPARELIST:
5384 case ISN_COMPARENR:
5385 case ISN_COMPAREPARTIAL:
5386 case ISN_COMPARESPECIAL:
5387 case ISN_COMPARESTRING:
5388 case ISN_CONCAT:
5389 case ISN_DCALL:
5390 case ISN_DROP:
5391 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005392 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 case ISN_ENDTRY:
5394 case ISN_FOR:
5395 case ISN_FUNCREF:
5396 case ISN_INDEX:
5397 case ISN_JUMP:
5398 case ISN_LOAD:
5399 case ISN_LOADSCRIPT:
5400 case ISN_LOADREG:
5401 case ISN_LOADV:
5402 case ISN_NEGATENR:
5403 case ISN_NEWDICT:
5404 case ISN_NEWLIST:
5405 case ISN_OPNR:
5406 case ISN_OPFLOAT:
5407 case ISN_OPANY:
5408 case ISN_PCALL:
5409 case ISN_PUSHF:
5410 case ISN_PUSHNR:
5411 case ISN_PUSHBOOL:
5412 case ISN_PUSHSPEC:
5413 case ISN_RETURN:
5414 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005415 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005417 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 case ISN_STORESCRIPT:
5419 case ISN_THROW:
5420 case ISN_TRY:
5421 // nothing allocated
5422 break;
5423 }
5424}
5425
5426/*
5427 * When a user function is deleted, delete any associated def function.
5428 */
5429 void
5430delete_def_function(ufunc_T *ufunc)
5431{
5432 int idx;
5433
5434 if (ufunc->uf_dfunc_idx >= 0)
5435 {
5436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5437 + ufunc->uf_dfunc_idx;
5438 ga_clear(&dfunc->df_def_args_isn);
5439
5440 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5441 delete_instr(dfunc->df_instr + idx);
5442 VIM_CLEAR(dfunc->df_instr);
5443
5444 dfunc->df_deleted = TRUE;
5445 }
5446}
5447
5448#if defined(EXITFREE) || defined(PROTO)
5449 void
5450free_def_functions(void)
5451{
5452 vim_free(def_functions.ga_data);
5453}
5454#endif
5455
5456
5457#endif // FEAT_EVAL