blob: 8b58d968898a26d1bfed7967b1c669f193f65bdd [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
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaarf9b2b492020-08-05 14:34:14 +0200151static char e_unknown_var[] = N_("E1089: unknown variable: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
Bram Moolenaar20431c92020-03-20 18:39:46 +0100153static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200294 int c = p[len];
295
296 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 if (lookup_script(p, len) == OK
298 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200299 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200300 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200301 || find_imported(p, len, cctx) != NULL
302 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200304 p[len] = c;
Bram Moolenaareef21022020-08-01 22:16:43 +0200305 semsg(_(e_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 return FAIL;
307 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200308 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 return OK;
310}
311
Bram Moolenaar65b95452020-07-19 14:03:09 +0200312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313/////////////////////////////////////////////////////////////////////
314// Following generate_ functions expect the caller to call ga_grow().
315
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200316#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
317#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/*
320 * Generate an instruction without arguments.
321 * Returns a pointer to the new instruction, NULL if failed.
322 */
323 static isn_T *
324generate_instr(cctx_T *cctx, isntype_T isn_type)
325{
326 garray_T *instr = &cctx->ctx_instr;
327 isn_T *isn;
328
Bram Moolenaar080457c2020-03-03 21:53:32 +0100329 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 if (ga_grow(instr, 1) == FAIL)
331 return NULL;
332 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
333 isn->isn_type = isn_type;
334 isn->isn_lnum = cctx->ctx_lnum + 1;
335 ++instr->ga_len;
336
337 return isn;
338}
339
340/*
341 * Generate an instruction without arguments.
342 * "drop" will be removed from the stack.
343 * Returns a pointer to the new instruction, NULL if failed.
344 */
345 static isn_T *
346generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
347{
348 garray_T *stack = &cctx->ctx_type_stack;
349
Bram Moolenaar080457c2020-03-03 21:53:32 +0100350 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351 stack->ga_len -= drop;
352 return generate_instr(cctx, isn_type);
353}
354
355/*
356 * Generate instruction "isn_type" and put "type" on the type stack.
357 */
358 static isn_T *
359generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
360{
361 isn_T *isn;
362 garray_T *stack = &cctx->ctx_type_stack;
363
364 if ((isn = generate_instr(cctx, isn_type)) == NULL)
365 return NULL;
366
367 if (ga_grow(stack, 1) == FAIL)
368 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200369 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 ++stack->ga_len;
371
372 return isn;
373}
374
375/*
376 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
377 */
378 static int
379may_generate_2STRING(int offset, cctx_T *cctx)
380{
381 isn_T *isn;
382 garray_T *stack = &cctx->ctx_type_stack;
383 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
384
385 if ((*type)->tt_type == VAR_STRING)
386 return OK;
387 *type = &t_string;
388
389 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
390 return FAIL;
391 isn->isn_arg.number = offset;
392
393 return OK;
394}
395
396 static int
397check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
398{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200399 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200401 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 {
403 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200404 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405 else
406 semsg(_("E1036: %c requires number or float arguments"), *op);
407 return FAIL;
408 }
409 return OK;
410}
411
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200412 static int
413generate_add_instr(
414 cctx_T *cctx,
415 vartype_T vartype,
416 type_T *type1,
417 type_T *type2)
418{
419 isn_T *isn = generate_instr_drop(cctx,
420 vartype == VAR_NUMBER ? ISN_OPNR
421 : vartype == VAR_LIST ? ISN_ADDLIST
422 : vartype == VAR_BLOB ? ISN_ADDBLOB
423#ifdef FEAT_FLOAT
424 : vartype == VAR_FLOAT ? ISN_OPFLOAT
425#endif
426 : ISN_OPANY, 1);
427
428 if (vartype != VAR_LIST && vartype != VAR_BLOB
429 && type1->tt_type != VAR_ANY
430 && type2->tt_type != VAR_ANY
431 && check_number_or_float(
432 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
433 return FAIL;
434
435 if (isn != NULL)
436 isn->isn_arg.op.op_type = EXPR_ADD;
437 return isn == NULL ? FAIL : OK;
438}
439
440/*
441 * Get the type to use for an instruction for an operation on "type1" and
442 * "type2". If they are matching use a type-specific instruction. Otherwise
443 * fall back to runtime type checking.
444 */
445 static vartype_T
446operator_type(type_T *type1, type_T *type2)
447{
448 if (type1->tt_type == type2->tt_type
449 && (type1->tt_type == VAR_NUMBER
450 || type1->tt_type == VAR_LIST
451#ifdef FEAT_FLOAT
452 || type1->tt_type == VAR_FLOAT
453#endif
454 || type1->tt_type == VAR_BLOB))
455 return type1->tt_type;
456 return VAR_ANY;
457}
458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459/*
460 * Generate an instruction with two arguments. The instruction depends on the
461 * type of the arguments.
462 */
463 static int
464generate_two_op(cctx_T *cctx, char_u *op)
465{
466 garray_T *stack = &cctx->ctx_type_stack;
467 type_T *type1;
468 type_T *type2;
469 vartype_T vartype;
470 isn_T *isn;
471
Bram Moolenaar080457c2020-03-03 21:53:32 +0100472 RETURN_OK_IF_SKIP(cctx);
473
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200474 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
476 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200477 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 switch (*op)
480 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200481 case '+':
482 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 break;
485
486 case '-':
487 case '*':
488 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
489 op) == FAIL)
490 return FAIL;
491 if (vartype == VAR_NUMBER)
492 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
493#ifdef FEAT_FLOAT
494 else if (vartype == VAR_FLOAT)
495 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
496#endif
497 else
498 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
499 if (isn != NULL)
500 isn->isn_arg.op.op_type = *op == '*'
501 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
502 break;
503
Bram Moolenaar4c683752020-04-05 21:38:23 +0200504 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200506 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 && type2->tt_type != VAR_NUMBER))
508 {
509 emsg(_("E1035: % requires number arguments"));
510 return FAIL;
511 }
512 isn = generate_instr_drop(cctx,
513 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
514 if (isn != NULL)
515 isn->isn_arg.op.op_type = EXPR_REM;
516 break;
517 }
518
519 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200520 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 {
522 type_T *type = &t_any;
523
524#ifdef FEAT_FLOAT
525 // float+number and number+float results in float
526 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
527 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
528 type = &t_float;
529#endif
530 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
531 }
532
533 return OK;
534}
535
536/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200537 * Get the instruction to use for comparing "type1" with "type2"
538 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200540 static isntype_T
541get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542{
543 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar4c683752020-04-05 21:38:23 +0200545 if (type1 == VAR_UNKNOWN)
546 type1 = VAR_ANY;
547 if (type2 == VAR_UNKNOWN)
548 type2 = VAR_ANY;
549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 if (type1 == type2)
551 {
552 switch (type1)
553 {
554 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
555 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
556 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
557 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
558 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
559 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
560 case VAR_LIST: isntype = ISN_COMPARELIST; break;
561 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
562 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 default: isntype = ISN_COMPAREANY; break;
564 }
565 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200566 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
568 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
569 isntype = ISN_COMPAREANY;
570
571 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
572 && (isntype == ISN_COMPAREBOOL
573 || isntype == ISN_COMPARESPECIAL
574 || isntype == ISN_COMPARENR
575 || isntype == ISN_COMPAREFLOAT))
576 {
577 semsg(_("E1037: Cannot use \"%s\" with %s"),
578 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200579 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 }
581 if (isntype == ISN_DROP
582 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
583 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
584 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
585 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
586 && exptype != EXPR_IS && exptype != EXPR_ISNOT
587 && (type1 == VAR_BLOB || type2 == VAR_BLOB
588 || type1 == VAR_LIST || type2 == VAR_LIST))))
589 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100590 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200592 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200594 return isntype;
595}
596
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200597 int
598check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
599{
600 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
601 return FAIL;
602 return OK;
603}
604
Bram Moolenaara5565e42020-05-09 15:44:01 +0200605/*
606 * Generate an ISN_COMPARE* instruction with a boolean result.
607 */
608 static int
609generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
610{
611 isntype_T isntype;
612 isn_T *isn;
613 garray_T *stack = &cctx->ctx_type_stack;
614 vartype_T type1;
615 vartype_T type2;
616
617 RETURN_OK_IF_SKIP(cctx);
618
619 // Get the known type of the two items on the stack. If they are matching
620 // use a type-specific instruction. Otherwise fall back to runtime type
621 // checking.
622 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
623 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
624 isntype = get_compare_isn(exptype, type1, type2);
625 if (isntype == ISN_DROP)
626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
628 if ((isn = generate_instr(cctx, isntype)) == NULL)
629 return FAIL;
630 isn->isn_arg.op.op_type = exptype;
631 isn->isn_arg.op.op_ic = ic;
632
633 // takes two arguments, puts one bool back
634 if (stack->ga_len >= 2)
635 {
636 --stack->ga_len;
637 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
638 }
639
640 return OK;
641}
642
643/*
644 * Generate an ISN_2BOOL instruction.
645 */
646 static int
647generate_2BOOL(cctx_T *cctx, int invert)
648{
649 isn_T *isn;
650 garray_T *stack = &cctx->ctx_type_stack;
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(cctx, ISN_2BOOL)) == NULL)
654 return FAIL;
655 isn->isn_arg.number = invert;
656
657 // type becomes bool
658 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
659
660 return OK;
661}
662
663 static int
664generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
665{
666 isn_T *isn;
667 garray_T *stack = &cctx->ctx_type_stack;
668
Bram Moolenaar080457c2020-03-03 21:53:32 +0100669 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
671 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200672 // TODO: whole type, e.g. for a function also arg and return types
673 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 isn->isn_arg.type.ct_off = offset;
675
676 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200677 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678
679 return OK;
680}
681
682/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200683 * Check that
684 * - "actual" is "expected" type or
685 * - "actual" is a type that can be "expected" type: add a runtime check; or
686 * - return FAIL.
687 */
688 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200689need_type(
690 type_T *actual,
691 type_T *expected,
692 int offset,
693 cctx_T *cctx,
694 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200695{
696 if (check_type(expected, actual, FALSE) == OK)
697 return OK;
698 if (actual->tt_type != VAR_ANY
699 && actual->tt_type != VAR_UNKNOWN
700 && !(actual->tt_type == VAR_FUNC
701 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
702 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200703 if (!silent)
704 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200705 return FAIL;
706 }
707 generate_TYPECHECK(cctx, expected, offset);
708 return OK;
709}
710
711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 * Generate an ISN_PUSHNR instruction.
713 */
714 static int
715generate_PUSHNR(cctx_T *cctx, varnumber_T number)
716{
717 isn_T *isn;
718
Bram Moolenaar080457c2020-03-03 21:53:32 +0100719 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
721 return FAIL;
722 isn->isn_arg.number = number;
723
724 return OK;
725}
726
727/*
728 * Generate an ISN_PUSHBOOL instruction.
729 */
730 static int
731generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
732{
733 isn_T *isn;
734
Bram Moolenaar080457c2020-03-03 21:53:32 +0100735 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
737 return FAIL;
738 isn->isn_arg.number = number;
739
740 return OK;
741}
742
743/*
744 * Generate an ISN_PUSHSPEC instruction.
745 */
746 static int
747generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
748{
749 isn_T *isn;
750
Bram Moolenaar080457c2020-03-03 21:53:32 +0100751 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
753 return FAIL;
754 isn->isn_arg.number = number;
755
756 return OK;
757}
758
759#ifdef FEAT_FLOAT
760/*
761 * Generate an ISN_PUSHF instruction.
762 */
763 static int
764generate_PUSHF(cctx_T *cctx, float_T fnumber)
765{
766 isn_T *isn;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
770 return FAIL;
771 isn->isn_arg.fnumber = fnumber;
772
773 return OK;
774}
775#endif
776
777/*
778 * Generate an ISN_PUSHS instruction.
779 * Consumes "str".
780 */
781 static int
782generate_PUSHS(cctx_T *cctx, char_u *str)
783{
784 isn_T *isn;
785
Bram Moolenaar080457c2020-03-03 21:53:32 +0100786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
788 return FAIL;
789 isn->isn_arg.string = str;
790
791 return OK;
792}
793
794/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100795 * Generate an ISN_PUSHCHANNEL instruction.
796 * Consumes "channel".
797 */
798 static int
799generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
800{
801 isn_T *isn;
802
Bram Moolenaar080457c2020-03-03 21:53:32 +0100803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100804 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
805 return FAIL;
806 isn->isn_arg.channel = channel;
807
808 return OK;
809}
810
811/*
812 * Generate an ISN_PUSHJOB instruction.
813 * Consumes "job".
814 */
815 static int
816generate_PUSHJOB(cctx_T *cctx, job_T *job)
817{
818 isn_T *isn;
819
Bram Moolenaar080457c2020-03-03 21:53:32 +0100820 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100821 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100822 return FAIL;
823 isn->isn_arg.job = job;
824
825 return OK;
826}
827
828/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 * Generate an ISN_PUSHBLOB instruction.
830 * Consumes "blob".
831 */
832 static int
833generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
834{
835 isn_T *isn;
836
Bram Moolenaar080457c2020-03-03 21:53:32 +0100837 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
839 return FAIL;
840 isn->isn_arg.blob = blob;
841
842 return OK;
843}
844
845/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100846 * Generate an ISN_PUSHFUNC instruction with name "name".
847 * Consumes "name".
848 */
849 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200850generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100851{
852 isn_T *isn;
853
Bram Moolenaar080457c2020-03-03 21:53:32 +0100854 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200855 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100856 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200857 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100858
859 return OK;
860}
861
862/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200863 * Generate an ISN_GETITEM instruction with "index".
864 */
865 static int
866generate_GETITEM(cctx_T *cctx, int index)
867{
868 isn_T *isn;
869 garray_T *stack = &cctx->ctx_type_stack;
870 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
871 type_T *item_type = &t_any;
872
873 RETURN_OK_IF_SKIP(cctx);
874
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200875 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200876 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200877 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200878 emsg(_(e_listreq));
879 return FAIL;
880 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200881 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200882 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
883 return FAIL;
884 isn->isn_arg.number = index;
885
886 // add the item type to the type stack
887 if (ga_grow(stack, 1) == FAIL)
888 return FAIL;
889 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
890 ++stack->ga_len;
891 return OK;
892}
893
894/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200895 * Generate an ISN_SLICE instruction with "count".
896 */
897 static int
898generate_SLICE(cctx_T *cctx, int count)
899{
900 isn_T *isn;
901
902 RETURN_OK_IF_SKIP(cctx);
903 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
904 return FAIL;
905 isn->isn_arg.number = count;
906 return OK;
907}
908
909/*
910 * Generate an ISN_CHECKLEN instruction with "min_len".
911 */
912 static int
913generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
914{
915 isn_T *isn;
916
917 RETURN_OK_IF_SKIP(cctx);
918
919 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
920 return FAIL;
921 isn->isn_arg.checklen.cl_min_len = min_len;
922 isn->isn_arg.checklen.cl_more_OK = more_OK;
923
924 return OK;
925}
926
927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 * Generate an ISN_STORE instruction.
929 */
930 static int
931generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
937 return FAIL;
938 if (name != NULL)
939 isn->isn_arg.string = vim_strsave(name);
940 else
941 isn->isn_arg.number = idx;
942
943 return OK;
944}
945
946/*
947 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
948 */
949 static int
950generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
951{
952 isn_T *isn;
953
Bram Moolenaar080457c2020-03-03 21:53:32 +0100954 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
956 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100957 isn->isn_arg.storenr.stnr_idx = idx;
958 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 return OK;
961}
962
963/*
964 * Generate an ISN_STOREOPT instruction
965 */
966 static int
967generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +0200972 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 return FAIL;
974 isn->isn_arg.storeopt.so_name = vim_strsave(name);
975 isn->isn_arg.storeopt.so_flags = opt_flags;
976
977 return OK;
978}
979
980/*
981 * Generate an ISN_LOAD or similar instruction.
982 */
983 static int
984generate_LOAD(
985 cctx_T *cctx,
986 isntype_T isn_type,
987 int idx,
988 char_u *name,
989 type_T *type)
990{
991 isn_T *isn;
992
Bram Moolenaar080457c2020-03-03 21:53:32 +0100993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001005 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 */
1007 static int
1008generate_LOADV(
1009 cctx_T *cctx,
1010 char_u *name,
1011 int error)
1012{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001013 int di_flags;
1014 int vidx = find_vim_var(name, &di_flags);
1015 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001016
Bram Moolenaar080457c2020-03-03 21:53:32 +01001017 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001018 if (vidx < 0)
1019 {
1020 if (error)
1021 semsg(_(e_var_notfound), name);
1022 return FAIL;
1023 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001024 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001025
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001026 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001027}
1028
1029/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001030 * Generate an ISN_UNLET instruction.
1031 */
1032 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001033generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001034{
1035 isn_T *isn;
1036
1037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001038 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001039 return FAIL;
1040 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1041 isn->isn_arg.unlet.ul_forceit = forceit;
1042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 * Generate an ISN_LOADS instruction.
1048 */
1049 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001050generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001052 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001054 int sid,
1055 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056{
1057 isn_T *isn;
1058
Bram Moolenaar080457c2020-03-03 21:53:32 +01001059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001060 if (isn_type == ISN_LOADS)
1061 isn = generate_instr_type(cctx, isn_type, type);
1062 else
1063 isn = generate_instr_drop(cctx, isn_type, 1);
1064 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001066 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1067 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068
1069 return OK;
1070}
1071
1072/*
1073 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1074 */
1075 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001076generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 cctx_T *cctx,
1078 isntype_T isn_type,
1079 int sid,
1080 int idx,
1081 type_T *type)
1082{
1083 isn_T *isn;
1084
Bram Moolenaar080457c2020-03-03 21:53:32 +01001085 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 if (isn_type == ISN_LOADSCRIPT)
1087 isn = generate_instr_type(cctx, isn_type, type);
1088 else
1089 isn = generate_instr_drop(cctx, isn_type, 1);
1090 if (isn == NULL)
1091 return FAIL;
1092 isn->isn_arg.script.script_sid = sid;
1093 isn->isn_arg.script.script_idx = idx;
1094 return OK;
1095}
1096
1097/*
1098 * Generate an ISN_NEWLIST instruction.
1099 */
1100 static int
1101generate_NEWLIST(cctx_T *cctx, int count)
1102{
1103 isn_T *isn;
1104 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 type_T *type;
1106 type_T *member;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112
Bram Moolenaar127542b2020-08-09 17:22:04 +02001113 // get the member type from all the items on the stack.
1114 member = get_member_type_from_stack(
1115 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1116 cctx->ctx_type_list);
1117 type = get_list_type(member, cctx->ctx_type_list);
1118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 // drop the value types
1120 stack->ga_len -= count;
1121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 // add the list type to the type stack
1123 if (ga_grow(stack, 1) == FAIL)
1124 return FAIL;
1125 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1126 ++stack->ga_len;
1127
1128 return OK;
1129}
1130
1131/*
1132 * Generate an ISN_NEWDICT instruction.
1133 */
1134 static int
1135generate_NEWDICT(cctx_T *cctx, int count)
1136{
1137 isn_T *isn;
1138 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 type_T *type;
1140 type_T *member;
1141
Bram Moolenaar080457c2020-03-03 21:53:32 +01001142 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1144 return FAIL;
1145 isn->isn_arg.number = count;
1146
Bram Moolenaar127542b2020-08-09 17:22:04 +02001147 member = get_member_type_from_stack(
1148 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1149 cctx->ctx_type_list);
1150 type = get_dict_type(member, cctx->ctx_type_list);
1151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 // drop the key and value types
1153 stack->ga_len -= 2 * count;
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // add the dict type to the type stack
1156 if (ga_grow(stack, 1) == FAIL)
1157 return FAIL;
1158 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1159 ++stack->ga_len;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_FUNCREF instruction.
1166 */
1167 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001168generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169{
1170 isn_T *isn;
1171 garray_T *stack = &cctx->ctx_type_stack;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1175 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001176 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001177 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
1179 if (ga_grow(stack, 1) == FAIL)
1180 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001181 ((type_T **)stack->ga_data)[stack->ga_len] =
1182 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 ++stack->ga_len;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001189 * Generate an ISN_NEWFUNC instruction.
1190 */
1191 static int
1192generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1193{
1194 isn_T *isn;
1195 char_u *name;
1196
1197 RETURN_OK_IF_SKIP(cctx);
1198 name = vim_strsave(lambda_name);
1199 if (name == NULL)
1200 return FAIL;
1201 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.newfunc.nf_lambda = name;
1204 isn->isn_arg.newfunc.nf_global = func_name;
1205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 * Generate an ISN_JUMP instruction.
1211 */
1212 static int
1213generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1214{
1215 isn_T *isn;
1216 garray_T *stack = &cctx->ctx_type_stack;
1217
Bram Moolenaar080457c2020-03-03 21:53:32 +01001218 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1220 return FAIL;
1221 isn->isn_arg.jump.jump_when = when;
1222 isn->isn_arg.jump.jump_where = where;
1223
1224 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1225 --stack->ga_len;
1226
1227 return OK;
1228}
1229
1230 static int
1231generate_FOR(cctx_T *cctx, int loop_idx)
1232{
1233 isn_T *isn;
1234 garray_T *stack = &cctx->ctx_type_stack;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.forloop.for_idx = loop_idx;
1240
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 // type doesn't matter, will be stored next
1244 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1245 ++stack->ga_len;
1246
1247 return OK;
1248}
1249
1250/*
1251 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001252 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 * Return FAIL if the number of arguments is wrong.
1254 */
1255 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001256generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257{
1258 isn_T *isn;
1259 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001260 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001261 type_T *argtypes[MAX_FUNC_ARGS];
1262 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
Bram Moolenaar080457c2020-03-03 21:53:32 +01001264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001265 argoff = check_internal_func(func_idx, argcount);
1266 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 return FAIL;
1268
Bram Moolenaar389df252020-07-09 21:20:47 +02001269 if (method_call && argoff > 1)
1270 {
1271 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.shuffle.shfl_item = argcount;
1274 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1275 }
1276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1278 return FAIL;
1279 isn->isn_arg.bfunc.cbf_idx = func_idx;
1280 isn->isn_arg.bfunc.cbf_argcount = argcount;
1281
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001282 for (i = 0; i < argcount; ++i)
1283 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 stack->ga_len -= argcount; // drop the arguments
1286 if (ga_grow(stack, 1) == FAIL)
1287 return FAIL;
1288 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001289 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 ++stack->ga_len; // add return value
1291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_DCALL or ISN_UCALL instruction.
1297 * Return FAIL if the number of arguments is wrong.
1298 */
1299 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001300generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301{
1302 isn_T *isn;
1303 garray_T *stack = &cctx->ctx_type_stack;
1304 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001305 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
Bram Moolenaar080457c2020-03-03 21:53:32 +01001307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if (argcount > regular_args && !has_varargs(ufunc))
1309 {
1310 semsg(_(e_toomanyarg), ufunc->uf_name);
1311 return FAIL;
1312 }
1313 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1314 {
1315 semsg(_(e_toofewarg), ufunc->uf_name);
1316 return FAIL;
1317 }
1318
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001319 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001320 {
1321 int i;
1322
1323 for (i = 0; i < argcount; ++i)
1324 {
1325 type_T *expected;
1326 type_T *actual;
1327
1328 if (i < regular_args)
1329 {
1330 if (ufunc->uf_arg_types == NULL)
1331 continue;
1332 expected = ufunc->uf_arg_types[i];
1333 }
1334 else
1335 expected = ufunc->uf_va_type->tt_member;
1336 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001337 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001338 {
1339 arg_type_mismatch(expected, actual, i + 1);
1340 return FAIL;
1341 }
1342 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001343 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001344 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1345 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001346 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001347 }
1348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001350 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001351 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001353 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 {
1355 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1356 isn->isn_arg.dfunc.cdf_argcount = argcount;
1357 }
1358 else
1359 {
1360 // A user function may be deleted and redefined later, can't use the
1361 // ufunc pointer, need to look it up again at runtime.
1362 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1363 isn->isn_arg.ufunc.cuf_argcount = argcount;
1364 }
1365
1366 stack->ga_len -= argcount; // drop the arguments
1367 if (ga_grow(stack, 1) == FAIL)
1368 return FAIL;
1369 // add return value
1370 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1371 ++stack->ga_len;
1372
1373 return OK;
1374}
1375
1376/*
1377 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1378 */
1379 static int
1380generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1381{
1382 isn_T *isn;
1383 garray_T *stack = &cctx->ctx_type_stack;
1384
Bram Moolenaar080457c2020-03-03 21:53:32 +01001385 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1387 return FAIL;
1388 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1389 isn->isn_arg.ufunc.cuf_argcount = argcount;
1390
1391 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001392 if (ga_grow(stack, 1) == FAIL)
1393 return FAIL;
1394 // add return value
1395 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1396 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397
1398 return OK;
1399}
1400
1401/*
1402 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001403 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 */
1405 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001406generate_PCALL(
1407 cctx_T *cctx,
1408 int argcount,
1409 char_u *name,
1410 type_T *type,
1411 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412{
1413 isn_T *isn;
1414 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001415 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
Bram Moolenaar080457c2020-03-03 21:53:32 +01001417 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001418
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001419 if (type->tt_type == VAR_ANY)
1420 ret_type = &t_any;
1421 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001422 {
1423 if (type->tt_argcount != -1)
1424 {
1425 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1426
1427 if (argcount < type->tt_min_argcount - varargs)
1428 {
1429 semsg(_(e_toofewarg), "[reference]");
1430 return FAIL;
1431 }
1432 if (!varargs && argcount > type->tt_argcount)
1433 {
1434 semsg(_(e_toomanyarg), "[reference]");
1435 return FAIL;
1436 }
1437 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001438 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001439 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001440 else
1441 {
1442 semsg(_("E1085: Not a callable type: %s"), name);
1443 return FAIL;
1444 }
1445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1447 return FAIL;
1448 isn->isn_arg.pfunc.cpf_top = at_top;
1449 isn->isn_arg.pfunc.cpf_argcount = argcount;
1450
1451 stack->ga_len -= argcount; // drop the arguments
1452
1453 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001454 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001456 // If partial is above the arguments it must be cleared and replaced with
1457 // the return value.
1458 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1459 return FAIL;
1460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 return OK;
1462}
1463
1464/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 */
1467 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001468generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
1472 type_T *type;
1473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001475 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001477 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001479 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001481 if (type->tt_type != VAR_DICT && type != &t_any)
1482 {
1483 emsg(_(e_dictreq));
1484 return FAIL;
1485 }
1486 // change dict type to dict member type
1487 if (type->tt_type == VAR_DICT)
1488 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
1490 return OK;
1491}
1492
1493/*
1494 * Generate an ISN_ECHO instruction.
1495 */
1496 static int
1497generate_ECHO(cctx_T *cctx, int with_white, int count)
1498{
1499 isn_T *isn;
1500
Bram Moolenaar080457c2020-03-03 21:53:32 +01001501 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1503 return FAIL;
1504 isn->isn_arg.echo.echo_with_white = with_white;
1505 isn->isn_arg.echo.echo_count = count;
1506
1507 return OK;
1508}
1509
Bram Moolenaarad39c092020-02-26 18:23:43 +01001510/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001511 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001512 */
1513 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001514generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001515{
1516 isn_T *isn;
1517
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001518 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001519 return FAIL;
1520 isn->isn_arg.number = count;
1521
1522 return OK;
1523}
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 static int
1526generate_EXEC(cctx_T *cctx, char_u *line)
1527{
1528 isn_T *isn;
1529
Bram Moolenaar080457c2020-03-03 21:53:32 +01001530 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1532 return FAIL;
1533 isn->isn_arg.string = vim_strsave(line);
1534 return OK;
1535}
1536
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001537 static int
1538generate_EXECCONCAT(cctx_T *cctx, int count)
1539{
1540 isn_T *isn;
1541
1542 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1543 return FAIL;
1544 isn->isn_arg.number = count;
1545 return OK;
1546}
1547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548/*
1549 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001550 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001552 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1554{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 lvar_T *lvar;
1556
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001557 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001559 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001560 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 }
1562
1563 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001564 return NULL;
1565 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001567 // Every local variable uses the next entry on the stack. We could re-use
1568 // the last ones when leaving a scope, but then variables used in a closure
1569 // might get overwritten. To keep things simple do not re-use stack
1570 // entries. This is less efficient, but memory is cheap these days.
1571 lvar->lv_idx = cctx->ctx_locals_count++;
1572
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001573 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 lvar->lv_const = isConst;
1575 lvar->lv_type = type;
1576
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001577 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578}
1579
1580/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001581 * Remove local variables above "new_top".
1582 */
1583 static void
1584unwind_locals(cctx_T *cctx, int new_top)
1585{
1586 if (cctx->ctx_locals.ga_len > new_top)
1587 {
1588 int idx;
1589 lvar_T *lvar;
1590
1591 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1592 {
1593 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1594 vim_free(lvar->lv_name);
1595 }
1596 }
1597 cctx->ctx_locals.ga_len = new_top;
1598}
1599
1600/*
1601 * Free all local variables.
1602 */
1603 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001604free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001605{
1606 unwind_locals(cctx, 0);
1607 ga_clear(&cctx->ctx_locals);
1608}
1609
1610/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 * Find "name" in script-local items of script "sid".
1612 * Returns the index in "sn_var_vals" if found.
1613 * If found but not in "sn_var_vals" returns -1.
1614 * If not found returns -2.
1615 */
1616 int
1617get_script_item_idx(int sid, char_u *name, int check_writable)
1618{
1619 hashtab_T *ht;
1620 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001621 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 int idx;
1623
1624 // First look the name up in the hashtable.
1625 if (sid <= 0 || sid > script_items.ga_len)
1626 return -1;
1627 ht = &SCRIPT_VARS(sid);
1628 di = find_var_in_ht(ht, 0, name, TRUE);
1629 if (di == NULL)
1630 return -2;
1631
1632 // Now find the svar_T index in sn_var_vals.
1633 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1634 {
1635 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1636
1637 if (sv->sv_tv == &di->di_tv)
1638 {
1639 if (check_writable && sv->sv_const)
1640 semsg(_(e_readonlyvar), name);
1641 return idx;
1642 }
1643 }
1644 return -1;
1645}
1646
1647/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001648 * Find "name" in imported items of the current script or in "cctx" if not
1649 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 */
1651 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001652find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 int idx;
1655
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001656 if (current_sctx.sc_sid <= 0)
1657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 if (cctx != NULL)
1659 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1660 {
1661 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1662 + idx;
1663
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001664 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1665 : STRLEN(import->imp_name) == len
1666 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 return import;
1668 }
1669
Bram Moolenaarefa94442020-08-08 22:16:00 +02001670 return find_imported_in_script(name, len, current_sctx.sc_sid);
1671}
1672
1673 imported_T *
1674find_imported_in_script(char_u *name, size_t len, int sid)
1675{
1676 scriptitem_T *si = SCRIPT_ITEM(sid);
1677 int idx;
1678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1680 {
1681 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1682
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001683 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1684 : STRLEN(import->imp_name) == len
1685 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 return import;
1687 }
1688 return NULL;
1689}
1690
1691/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001692 * Free all imported variables.
1693 */
1694 static void
1695free_imported(cctx_T *cctx)
1696{
1697 int idx;
1698
1699 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1700 {
1701 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1702
1703 vim_free(import->imp_name);
1704 }
1705 ga_clear(&cctx->ctx_imports);
1706}
1707
1708/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001709 * Return TRUE if "p" points at a "#" but not at "#{".
1710 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001711 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001712vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001713{
1714 return p[0] == '#' && p[1] != '{';
1715}
1716
1717/*
1718 * Return a pointer to the next line that isn't empty or only contains a
1719 * comment. Skips over white space.
1720 * Returns NULL if there is none.
1721 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001722 char_u *
1723peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001724{
1725 int lnum = cctx->ctx_lnum;
1726
1727 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1728 {
1729 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001730 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001731
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001732 if (line == NULL)
1733 break;
1734 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001735 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02001736 return p;
1737 }
1738 return NULL;
1739}
1740
1741/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001742 * Called when checking for a following operator at "arg". When the rest of
1743 * the line is empty or only a comment, peek the next line. If there is a next
1744 * line return a pointer to it and set "nextp".
1745 * Otherwise skip over white space.
1746 */
1747 static char_u *
1748may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1749{
1750 char_u *p = skipwhite(arg);
1751
1752 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001753 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001754 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001755 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001756 if (*nextp != NULL)
1757 return *nextp;
1758 }
1759 return p;
1760}
1761
1762/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001763 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001764 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001765 * Returns NULL when at the end.
1766 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001767 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001768next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001769{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001770 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001771
1772 do
1773 {
1774 ++cctx->ctx_lnum;
1775 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001776 {
1777 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001778 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001779 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001780 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001781 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001782 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001783 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001784 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001785 return line;
1786}
1787
1788/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001789 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001790 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001791 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1792 */
1793 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001794may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001795{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001796 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001797 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001798 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001799
1800 if (next == NULL)
1801 return FAIL;
1802 *arg = skipwhite(next);
1803 }
1804 return OK;
1805}
1806
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001807/*
1808 * Idem, and give an error when failed.
1809 */
1810 static int
1811may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1812{
1813 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1814 {
1815 emsg(_("E1097: line incomplete"));
1816 return FAIL;
1817 }
1818 return OK;
1819}
1820
1821
Bram Moolenaara5565e42020-05-09 15:44:01 +02001822// Structure passed between the compile_expr* functions to keep track of
1823// constants that have been parsed but for which no code was produced yet. If
1824// possible expressions on these constants are applied at compile time. If
1825// that is not possible, the code to push the constants needs to be generated
1826// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001827// Using 50 should be more than enough of 5 levels of ().
1828#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001829typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001830 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001831 int pp_used; // active entries in pp_tv[]
1832} ppconst_T;
1833
Bram Moolenaar1c747212020-05-09 18:28:34 +02001834static int compile_expr0(char_u **arg, cctx_T *cctx);
1835static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1836
Bram Moolenaara5565e42020-05-09 15:44:01 +02001837/*
1838 * Generate a PUSH instruction for "tv".
1839 * "tv" will be consumed or cleared.
1840 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1841 */
1842 static int
1843generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1844{
1845 if (tv != NULL)
1846 {
1847 switch (tv->v_type)
1848 {
1849 case VAR_UNKNOWN:
1850 break;
1851 case VAR_BOOL:
1852 generate_PUSHBOOL(cctx, tv->vval.v_number);
1853 break;
1854 case VAR_SPECIAL:
1855 generate_PUSHSPEC(cctx, tv->vval.v_number);
1856 break;
1857 case VAR_NUMBER:
1858 generate_PUSHNR(cctx, tv->vval.v_number);
1859 break;
1860#ifdef FEAT_FLOAT
1861 case VAR_FLOAT:
1862 generate_PUSHF(cctx, tv->vval.v_float);
1863 break;
1864#endif
1865 case VAR_BLOB:
1866 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1867 tv->vval.v_blob = NULL;
1868 break;
1869 case VAR_STRING:
1870 generate_PUSHS(cctx, tv->vval.v_string);
1871 tv->vval.v_string = NULL;
1872 break;
1873 default:
1874 iemsg("constant type not supported");
1875 clear_tv(tv);
1876 return FAIL;
1877 }
1878 tv->v_type = VAR_UNKNOWN;
1879 }
1880 return OK;
1881}
1882
1883/*
1884 * Generate code for any ppconst entries.
1885 */
1886 static int
1887generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1888{
1889 int i;
1890 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001891 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001892
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001893 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001894 for (i = 0; i < ppconst->pp_used; ++i)
1895 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1896 ret = FAIL;
1897 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001898 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001899 return ret;
1900}
1901
1902/*
1903 * Clear ppconst constants. Used when failing.
1904 */
1905 static void
1906clear_ppconst(ppconst_T *ppconst)
1907{
1908 int i;
1909
1910 for (i = 0; i < ppconst->pp_used; ++i)
1911 clear_tv(&ppconst->pp_tv[i]);
1912 ppconst->pp_used = 0;
1913}
1914
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001915/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 * Generate an instruction to load script-local variable "name", without the
1917 * leading "s:".
1918 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 */
1920 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001921compile_load_scriptvar(
1922 cctx_T *cctx,
1923 char_u *name, // variable NUL terminated
1924 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001925 char_u **end, // end of variable
1926 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001928 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1930 imported_T *import;
1931
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001932 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001934 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001935 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1936 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 }
1938 if (idx >= 0)
1939 {
1940 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1941
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001942 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 current_sctx.sc_sid, idx, sv->sv_type);
1944 return OK;
1945 }
1946
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001947 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 if (import != NULL)
1949 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001950 if (import->imp_all)
1951 {
1952 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001953 char_u *exp_name;
1954 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001955 ufunc_T *ufunc;
1956 type_T *type;
1957
1958 // Used "import * as Name", need to lookup the member.
1959 if (*p != '.')
1960 {
1961 semsg(_("E1060: expected dot after name: %s"), start);
1962 return FAIL;
1963 }
1964 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001965 if (VIM_ISWHITE(*p))
1966 {
1967 emsg(_("E1074: no white space allowed after dot"));
1968 return FAIL;
1969 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001970
Bram Moolenaar1c991142020-07-04 13:15:31 +02001971 // isolate one name
1972 exp_name = p;
1973 while (eval_isnamec(*p))
1974 ++p;
1975 cc = *p;
1976 *p = NUL;
1977
1978 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
1979 *p = cc;
1980 p = skipwhite(p);
1981
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001982 // TODO: what if it is a function?
1983 if (idx < 0)
1984 return FAIL;
1985 *end = p;
1986
1987 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1988 import->imp_sid,
1989 idx,
1990 type);
1991 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001992 else if (import->imp_funcname != NULL)
1993 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001994 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001995 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1996 import->imp_sid,
1997 import->imp_var_vals_idx,
1998 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 return OK;
2000 }
2001
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002002 if (error)
2003 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 return FAIL;
2005}
2006
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002007 static int
2008generate_funcref(cctx_T *cctx, char_u *name)
2009{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002010 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002011
2012 if (ufunc == NULL)
2013 return FAIL;
2014
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002015 // Need to compile any default values to get the argument types.
2016 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2017 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2018 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002019 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020}
2021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022/*
2023 * Compile a variable name into a load instruction.
2024 * "end" points to just after the name.
2025 * When "error" is FALSE do not give an error when not found.
2026 */
2027 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002028compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029{
2030 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002031 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002032 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002034 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035
2036 if (*(*arg + 1) == ':')
2037 {
2038 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002039 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002040 {
2041 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002043 switch (**arg)
2044 {
2045 case 'g': isn_type = ISN_LOADGDICT; break;
2046 case 'w': isn_type = ISN_LOADWDICT; break;
2047 case 't': isn_type = ISN_LOADTDICT; break;
2048 case 'b': isn_type = ISN_LOADBDICT; break;
2049 default:
2050 semsg(_(e_namespace), *arg);
2051 goto theend;
2052 }
2053 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2054 goto theend;
2055 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 else
2058 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002059 isntype_T isn_type = ISN_DROP;
2060
2061 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2062 if (name == NULL)
2063 return FAIL;
2064
2065 switch (**arg)
2066 {
2067 case 'v': res = generate_LOADV(cctx, name, error);
2068 break;
2069 case 's': res = compile_load_scriptvar(cctx, name,
2070 NULL, NULL, error);
2071 break;
2072 case 'g': isn_type = ISN_LOADG; break;
2073 case 'w': isn_type = ISN_LOADW; break;
2074 case 't': isn_type = ISN_LOADT; break;
2075 case 'b': isn_type = ISN_LOADB; break;
2076 default: semsg(_(e_namespace), *arg);
2077 goto theend;
2078 }
2079 if (isn_type != ISN_DROP)
2080 {
2081 // Global, Buffer-local, Window-local and Tabpage-local
2082 // variables can be defined later, thus we don't check if it
2083 // exists, give error at runtime.
2084 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 }
2088 else
2089 {
2090 size_t len = end - *arg;
2091 int idx;
2092 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002093 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094
2095 name = vim_strnsave(*arg, end - *arg);
2096 if (name == NULL)
2097 return FAIL;
2098
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002099 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002101 if (!gen_load_outer)
2102 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 }
2104 else
2105 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002106 lvar_T *lvar = lookup_local(*arg, len, cctx);
2107
2108 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002110 type = lvar->lv_type;
2111 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002112 if (lvar->lv_from_outer)
2113 gen_load_outer = TRUE;
2114 else
2115 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 }
2117 else
2118 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002119 // "var" can be script-local even without using "s:" if it
2120 // already exists.
2121 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2122 == SCRIPT_VERSION_VIM9
2123 || lookup_script(*arg, len) == OK)
2124 res = compile_load_scriptvar(cctx, name, *arg, &end,
2125 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002126
Bram Moolenaara5565e42020-05-09 15:44:01 +02002127 // When the name starts with an uppercase letter or "x:" it
2128 // can be a user defined function.
2129 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2130 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 }
2132 }
2133 if (gen_load)
2134 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002135 if (gen_load_outer)
2136 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 }
2138
2139 *arg = end;
2140
2141theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002142 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 semsg(_(e_var_notfound), name);
2144 vim_free(name);
2145 return res;
2146}
2147
2148/*
2149 * Compile the argument expressions.
2150 * "arg" points to just after the "(" and is advanced to after the ")"
2151 */
2152 static int
2153compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2154{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002155 char_u *p = *arg;
2156 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
Bram Moolenaare6085c52020-04-12 20:19:16 +02002158 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002160 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2161 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002162 if (*p == ')')
2163 {
2164 *arg = p + 1;
2165 return OK;
2166 }
2167
Bram Moolenaara5565e42020-05-09 15:44:01 +02002168 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 return FAIL;
2170 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002171
2172 if (*p != ',' && *skipwhite(p) == ',')
2173 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002174 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002175 p = skipwhite(p);
2176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002178 {
2179 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002180 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002181 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002182 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002183 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002184 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002186failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002187 emsg(_(e_missing_close));
2188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189}
2190
2191/*
2192 * Compile a function call: name(arg1, arg2)
2193 * "arg" points to "name", "arg + varlen" to the "(".
2194 * "argcount_init" is 1 for "value->method()"
2195 * Instructions:
2196 * EVAL arg1
2197 * EVAL arg2
2198 * BCALL / DCALL / UCALL
2199 */
2200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002201compile_call(
2202 char_u **arg,
2203 size_t varlen,
2204 cctx_T *cctx,
2205 ppconst_T *ppconst,
2206 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207{
2208 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002209 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 int argcount = argcount_init;
2211 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002212 char_u fname_buf[FLEN_FIXED + 1];
2213 char_u *tofree = NULL;
2214 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002216 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002217 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218
Bram Moolenaara5565e42020-05-09 15:44:01 +02002219 // we can evaluate "has('name')" at compile time
2220 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2221 {
2222 char_u *s = skipwhite(*arg + varlen + 1);
2223 typval_T argvars[2];
2224
2225 argvars[0].v_type = VAR_UNKNOWN;
2226 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002227 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002228 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002229 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230 s = skipwhite(s);
2231 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2232 {
2233 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2234
2235 *arg = s + 1;
2236 argvars[1].v_type = VAR_UNKNOWN;
2237 tv->v_type = VAR_NUMBER;
2238 tv->vval.v_number = 0;
2239 f_has(argvars, tv);
2240 clear_tv(&argvars[0]);
2241 ++ppconst->pp_used;
2242 return OK;
2243 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002244 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002245 }
2246
2247 if (generate_ppconst(cctx, ppconst) == FAIL)
2248 return FAIL;
2249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 if (varlen >= sizeof(namebuf))
2251 {
2252 semsg(_("E1011: name too long: %s"), name);
2253 return FAIL;
2254 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002255 vim_strncpy(namebuf, *arg, varlen);
2256 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257
2258 *arg = skipwhite(*arg + varlen + 1);
2259 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002260 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261
Bram Moolenaara1773442020-08-12 15:21:22 +02002262 is_autoload = vim_strchr(name, '#') != NULL;
2263 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 {
2265 int idx;
2266
2267 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002268 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002270 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002271 else
2272 semsg(_(e_unknownfunc), namebuf);
2273 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 }
2275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002277 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002279 {
2280 res = generate_CALL(cctx, ufunc, argcount);
2281 goto theend;
2282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283
2284 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002285 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002286 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002288 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002289 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002291 garray_T *stack = &cctx->ctx_type_stack;
2292 type_T *type;
2293
2294 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2295 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002296 goto theend;
2297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002299 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002300 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002301 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002302 res = generate_UCALL(cctx, name, argcount);
2303 else
2304 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002305
2306theend:
2307 vim_free(tofree);
2308 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309}
2310
2311// like NAMESPACE_CHAR but with 'a' and 'l'.
2312#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2313
2314/*
2315 * Find the end of a variable or function name. Unlike find_name_end() this
2316 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002317 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 * Return a pointer to just after the name. Equal to "arg" if there is no
2319 * valid name.
2320 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002321 static char_u *
2322to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323{
2324 char_u *p;
2325
2326 // Quick check for valid starting character.
2327 if (!eval_isnamec1(*arg))
2328 return arg;
2329
2330 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2331 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2332 // and can be used in slice "[n:]".
2333 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002334 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2336 break;
2337 return p;
2338}
2339
2340/*
2341 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002342 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 */
2344 char_u *
2345to_name_const_end(char_u *arg)
2346{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002347 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 typval_T rettv;
2349
2350 if (p == arg && *arg == '[')
2351 {
2352
2353 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002354 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 p = arg;
2356 }
2357 else if (p == arg && *arg == '#' && arg[1] == '{')
2358 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002359 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002361 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 p = arg;
2363 }
2364 else if (p == arg && *arg == '{')
2365 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002366 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002368 // Can be "{x -> ret}()".
2369 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002371 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (ret != OK)
2373 p = arg;
2374 }
2375
2376 return p;
2377}
2378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379/*
2380 * parse a list: [expr, expr]
2381 * "*arg" points to the '['.
2382 */
2383 static int
2384compile_list(char_u **arg, cctx_T *cctx)
2385{
2386 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002387 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 int count = 0;
2389
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002390 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002392 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002393 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002394 semsg(_(e_list_end), *arg);
2395 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002396 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002397 if (*p == ',')
2398 {
2399 semsg(_(e_no_white_before), ",");
2400 return FAIL;
2401 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002402 if (*p == ']')
2403 {
2404 ++p;
2405 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002406 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002407 p += STRLEN(p);
2408 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002409 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002410 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 break;
2412 ++count;
2413 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002414 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002416 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2417 {
2418 semsg(_(e_white_after), ",");
2419 return FAIL;
2420 }
2421 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002422 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 p = skipwhite(p);
2424 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002425 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426
2427 generate_NEWLIST(cctx, count);
2428 return OK;
2429}
2430
2431/*
2432 * parse a lambda: {arg, arg -> expr}
2433 * "*arg" points to the '{'.
2434 */
2435 static int
2436compile_lambda(char_u **arg, cctx_T *cctx)
2437{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 typval_T rettv;
2439 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002440 evalarg_T evalarg;
2441
2442 CLEAR_FIELD(evalarg);
2443 evalarg.eval_flags = EVAL_EVALUATE;
2444 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445
2446 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002447 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002451 ++ufunc->uf_refcount;
2452 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002453 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454
2455 // The function will have one line: "return {expr}".
2456 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002457 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002459 clear_evalarg(&evalarg, NULL);
2460
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002461 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002462 {
2463 // The return type will now be known.
2464 set_function_type(ufunc);
2465
2466 return generate_FUNCREF(cctx, ufunc);
2467 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002468
2469 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 return FAIL;
2471}
2472
2473/*
2474 * Compile a lamda call: expr->{lambda}(args)
2475 * "arg" points to the "{".
2476 */
2477 static int
2478compile_lambda_call(char_u **arg, cctx_T *cctx)
2479{
2480 ufunc_T *ufunc;
2481 typval_T rettv;
2482 int argcount = 1;
2483 int ret = FAIL;
2484
2485 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002486 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 return FAIL;
2488
2489 if (**arg != '(')
2490 {
2491 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002492 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 else
2494 semsg(_(e_missing_paren), "lambda");
2495 clear_tv(&rettv);
2496 return FAIL;
2497 }
2498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 ufunc = rettv.vval.v_partial->pt_func;
2500 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002501 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002502 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002503
2504 // The function will have one line: "return {expr}".
2505 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002506 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
2508 // compile the arguments
2509 *arg = skipwhite(*arg + 1);
2510 if (compile_arguments(arg, cctx, &argcount) == OK)
2511 // call the compiled function
2512 ret = generate_CALL(cctx, ufunc, argcount);
2513
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002514 if (ret == FAIL)
2515 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 return ret;
2517}
2518
2519/*
2520 * parse a dict: {'key': val} or #{key: val}
2521 * "*arg" points to the '{'.
2522 */
2523 static int
2524compile_dict(char_u **arg, cctx_T *cctx, int literal)
2525{
2526 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002527 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 int count = 0;
2529 dict_T *d = dict_alloc();
2530 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002531 char_u *whitep = *arg;
2532 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533
2534 if (d == NULL)
2535 return FAIL;
2536 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002537 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 {
2539 char_u *key = NULL;
2540
Bram Moolenaar23c55272020-06-21 16:58:13 +02002541 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002542 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002543 *arg = NULL;
2544 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002545 }
2546
2547 if (**arg == '}')
2548 break;
2549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 if (literal)
2551 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002552 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553
Bram Moolenaar2c330432020-04-13 14:41:35 +02002554 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
2556 semsg(_("E1014: Invalid key: %s"), *arg);
2557 return FAIL;
2558 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002559 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 if (generate_PUSHS(cctx, key) == FAIL)
2561 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002562 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564 else
2565 {
2566 isn_T *isn;
2567
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2571 if (isn->isn_type == ISN_PUSHS)
2572 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002573 else
2574 {
2575 type_T *keytype = ((type_T **)stack->ga_data)
2576 [stack->ga_len - 1];
2577 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2578 return FAIL;
2579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 }
2581
2582 // Check for duplicate keys, if using string keys.
2583 if (key != NULL)
2584 {
2585 item = dict_find(d, key, -1);
2586 if (item != NULL)
2587 {
2588 semsg(_(e_duplicate_key), key);
2589 goto failret;
2590 }
2591 item = dictitem_alloc(key);
2592 if (item != NULL)
2593 {
2594 item->di_tv.v_type = VAR_UNKNOWN;
2595 item->di_tv.v_lock = 0;
2596 if (dict_add(d, item) == FAIL)
2597 dictitem_free(item);
2598 }
2599 }
2600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 if (**arg != ':')
2602 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002603 if (*skipwhite(*arg) == ':')
2604 semsg(_(e_no_white_before), ":");
2605 else
2606 semsg(_(e_missing_dict_colon), *arg);
2607 return FAIL;
2608 }
2609 whitep = *arg + 1;
2610 if (!IS_WHITE_OR_NUL(*whitep))
2611 {
2612 semsg(_(e_white_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 return FAIL;
2614 }
2615
2616 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002617 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002618 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002619 *arg = NULL;
2620 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002621 }
2622
Bram Moolenaara5565e42020-05-09 15:44:01 +02002623 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 return FAIL;
2625 ++count;
2626
Bram Moolenaar2c330432020-04-13 14:41:35 +02002627 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002628 *arg = skipwhite(*arg);
2629 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002630 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002631 *arg = NULL;
2632 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 if (**arg == '}')
2635 break;
2636 if (**arg != ',')
2637 {
2638 semsg(_(e_missing_dict_comma), *arg);
2639 goto failret;
2640 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002641 if (IS_WHITE_OR_NUL(*whitep))
2642 {
2643 semsg(_(e_no_white_before), ",");
2644 return FAIL;
2645 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002646 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 *arg = skipwhite(*arg + 1);
2648 }
2649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 *arg = *arg + 1;
2651
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002652 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002653 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002654 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002655 *arg += STRLEN(*arg);
2656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 dict_unref(d);
2658 return generate_NEWDICT(cctx, count);
2659
2660failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002661 if (*arg == NULL)
2662 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 dict_unref(d);
2664 return FAIL;
2665}
2666
2667/*
2668 * Compile "&option".
2669 */
2670 static int
2671compile_get_option(char_u **arg, cctx_T *cctx)
2672{
2673 typval_T rettv;
2674 char_u *start = *arg;
2675 int ret;
2676
2677 // parse the option and get the current value to get the type.
2678 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002679 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (ret == OK)
2681 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002682 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 char_u *name = vim_strnsave(start, *arg - start);
2684 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2685
2686 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2687 vim_free(name);
2688 }
2689 clear_tv(&rettv);
2690
2691 return ret;
2692}
2693
2694/*
2695 * Compile "$VAR".
2696 */
2697 static int
2698compile_get_env(char_u **arg, cctx_T *cctx)
2699{
2700 char_u *start = *arg;
2701 int len;
2702 int ret;
2703 char_u *name;
2704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 ++*arg;
2706 len = get_env_len(arg);
2707 if (len == 0)
2708 {
2709 semsg(_(e_syntax_at), start - 1);
2710 return FAIL;
2711 }
2712
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002713 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 name = vim_strnsave(start, len + 1);
2715 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2716 vim_free(name);
2717 return ret;
2718}
2719
2720/*
2721 * Compile "@r".
2722 */
2723 static int
2724compile_get_register(char_u **arg, cctx_T *cctx)
2725{
2726 int ret;
2727
2728 ++*arg;
2729 if (**arg == NUL)
2730 {
2731 semsg(_(e_syntax_at), *arg - 1);
2732 return FAIL;
2733 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002734 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 {
2736 emsg_invreg(**arg);
2737 return FAIL;
2738 }
2739 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2740 ++*arg;
2741 return ret;
2742}
2743
2744/*
2745 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002746 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 */
2748 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002749apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002751 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
2753 // this works from end to start
2754 while (p > start)
2755 {
2756 --p;
2757 if (*p == '-' || *p == '+')
2758 {
2759 // only '-' has an effect, for '+' we only check the type
2760#ifdef FEAT_FLOAT
2761 if (rettv->v_type == VAR_FLOAT)
2762 {
2763 if (*p == '-')
2764 rettv->vval.v_float = -rettv->vval.v_float;
2765 }
2766 else
2767#endif
2768 {
2769 varnumber_T val;
2770 int error = FALSE;
2771
2772 // tv_get_number_chk() accepts a string, but we don't want that
2773 // here
2774 if (check_not_string(rettv) == FAIL)
2775 return FAIL;
2776 val = tv_get_number_chk(rettv, &error);
2777 clear_tv(rettv);
2778 if (error)
2779 return FAIL;
2780 if (*p == '-')
2781 val = -val;
2782 rettv->v_type = VAR_NUMBER;
2783 rettv->vval.v_number = val;
2784 }
2785 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002786 else if (numeric_only)
2787 {
2788 ++p;
2789 break;
2790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 else
2792 {
2793 int v = tv2bool(rettv);
2794
2795 // '!' is permissive in the type.
2796 clear_tv(rettv);
2797 rettv->v_type = VAR_BOOL;
2798 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2799 }
2800 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002801 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 return OK;
2803}
2804
2805/*
2806 * Recognize v: variables that are constants and set "rettv".
2807 */
2808 static void
2809get_vim_constant(char_u **arg, typval_T *rettv)
2810{
2811 if (STRNCMP(*arg, "v:true", 6) == 0)
2812 {
2813 rettv->v_type = VAR_BOOL;
2814 rettv->vval.v_number = VVAL_TRUE;
2815 *arg += 6;
2816 }
2817 else if (STRNCMP(*arg, "v:false", 7) == 0)
2818 {
2819 rettv->v_type = VAR_BOOL;
2820 rettv->vval.v_number = VVAL_FALSE;
2821 *arg += 7;
2822 }
2823 else if (STRNCMP(*arg, "v:null", 6) == 0)
2824 {
2825 rettv->v_type = VAR_SPECIAL;
2826 rettv->vval.v_number = VVAL_NULL;
2827 *arg += 6;
2828 }
2829 else if (STRNCMP(*arg, "v:none", 6) == 0)
2830 {
2831 rettv->v_type = VAR_SPECIAL;
2832 rettv->vval.v_number = VVAL_NONE;
2833 *arg += 6;
2834 }
2835}
2836
Bram Moolenaar696ba232020-07-29 21:20:41 +02002837 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002838get_compare_type(char_u *p, int *len, int *type_is)
2839{
2840 exptype_T type = EXPR_UNKNOWN;
2841 int i;
2842
2843 switch (p[0])
2844 {
2845 case '=': if (p[1] == '=')
2846 type = EXPR_EQUAL;
2847 else if (p[1] == '~')
2848 type = EXPR_MATCH;
2849 break;
2850 case '!': if (p[1] == '=')
2851 type = EXPR_NEQUAL;
2852 else if (p[1] == '~')
2853 type = EXPR_NOMATCH;
2854 break;
2855 case '>': if (p[1] != '=')
2856 {
2857 type = EXPR_GREATER;
2858 *len = 1;
2859 }
2860 else
2861 type = EXPR_GEQUAL;
2862 break;
2863 case '<': if (p[1] != '=')
2864 {
2865 type = EXPR_SMALLER;
2866 *len = 1;
2867 }
2868 else
2869 type = EXPR_SEQUAL;
2870 break;
2871 case 'i': if (p[1] == 's')
2872 {
2873 // "is" and "isnot"; but not a prefix of a name
2874 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2875 *len = 5;
2876 i = p[*len];
2877 if (!isalnum(i) && i != '_')
2878 {
2879 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2880 *type_is = TRUE;
2881 }
2882 }
2883 break;
2884 }
2885 return type;
2886}
2887
2888/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002890 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 */
2892 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002893compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002895 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
2897 // this works from end to start
2898 while (p > start)
2899 {
2900 --p;
2901 if (*p == '-' || *p == '+')
2902 {
2903 int negate = *p == '-';
2904 isn_T *isn;
2905
2906 // TODO: check type
2907 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2908 {
2909 --p;
2910 if (*p == '-')
2911 negate = !negate;
2912 }
2913 // only '-' has an effect, for '+' we only check the type
2914 if (negate)
2915 isn = generate_instr(cctx, ISN_NEGATENR);
2916 else
2917 isn = generate_instr(cctx, ISN_CHECKNR);
2918 if (isn == NULL)
2919 return FAIL;
2920 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002921 else if (numeric_only)
2922 {
2923 ++p;
2924 break;
2925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 else
2927 {
2928 int invert = TRUE;
2929
2930 while (p > start && p[-1] == '!')
2931 {
2932 --p;
2933 invert = !invert;
2934 }
2935 if (generate_2BOOL(cctx, invert) == FAIL)
2936 return FAIL;
2937 }
2938 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002939 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 return OK;
2941}
2942
2943/*
2944 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002945 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 */
2947 static int
2948compile_subscript(
2949 char_u **arg,
2950 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002951 char_u *start_leader,
2952 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002953 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002955 char_u *name_start = *end_leader;
2956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 for (;;)
2958 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002959 char_u *p = skipwhite(*arg);
2960
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002961 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002962 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002963 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964
2965 // If a following line starts with "->{" or "->X" advance to that
2966 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002967 // Also if a following line starts with ".x".
2968 if (next != NULL &&
2969 ((next[0] == '-' && next[1] == '>'
2970 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002971 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002972 {
2973 next = next_line_from_context(cctx, TRUE);
2974 if (next == NULL)
2975 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002976 *arg = next;
2977 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002978 }
2979 }
2980
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002981 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2982 // not a function call.
2983 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002985 garray_T *stack = &cctx->ctx_type_stack;
2986 type_T *type;
2987 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002989 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002990 return FAIL;
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002993 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2994
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002995 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2997 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002998 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 return FAIL;
3000 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003001 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003003 char_u *pstart = p;
3004
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003005 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003006 return FAIL;
3007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 // something->method()
3009 // Apply the '!', '-' and '+' first:
3010 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003011 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003014 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003015 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003016 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 if (**arg == '{')
3018 {
3019 // lambda call: list->{lambda}
3020 if (compile_lambda_call(arg, cctx) == FAIL)
3021 return FAIL;
3022 }
3023 else
3024 {
3025 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003026 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003027 if (!eval_isnamec1(*p))
3028 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003029 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003030 return FAIL;
3031 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003032 if (ASCII_ISALPHA(*p) && p[1] == ':')
3033 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003034 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 ;
3036 if (*p != '(')
3037 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003038 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return FAIL;
3040 }
3041 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003042 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 return FAIL;
3044 }
3045 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003046 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003048 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003049 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003050 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003051
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003052 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003053 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003054 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003055 // TODO: blob index
3056 // TODO: more arguments
3057 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003058 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003059 return FAIL;
3060
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003061 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003062 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003063 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003064 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003065 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003068 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3069 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 if (**arg != ']')
3071 {
3072 emsg(_(e_missbrac));
3073 return FAIL;
3074 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003075 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003077 // We can index a list and a dict. If we don't know the type
3078 // we can use the index value type.
3079 // TODO: If we don't know use an instruction to figure it out at
3080 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003081 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003082 vtype = (*typep)->tt_type;
3083 if (*typep == &t_any)
3084 {
3085 type_T *valtype = ((type_T **)stack->ga_data)
3086 [stack->ga_len - 1];
3087 if (valtype == &t_string)
3088 vtype = VAR_DICT;
3089 }
3090 if (vtype == VAR_DICT)
3091 {
3092 if ((*typep)->tt_type == VAR_DICT)
3093 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003094 else
3095 {
3096 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3097 return FAIL;
3098 *typep = &t_any;
3099 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003100 if (may_generate_2STRING(-1, cctx) == FAIL)
3101 return FAIL;
3102 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3103 return FAIL;
3104 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003105 else if (vtype == VAR_STRING)
3106 {
3107 *typep = &t_number;
3108 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3109 return FAIL;
3110 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003111 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003112 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003113 if ((*typep)->tt_type == VAR_LIST)
3114 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003115 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003116 return FAIL;
3117 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003118 else
3119 {
3120 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003121 return FAIL;
3122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003124 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003126 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003127 return FAIL;
3128
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003129 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003130 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3131 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003133 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003134 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 while (eval_isnamec(*p))
3136 MB_PTR_ADV(p);
3137 if (p == *arg)
3138 {
3139 semsg(_(e_syntax_at), *arg);
3140 return FAIL;
3141 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003142 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 return FAIL;
3144 *arg = p;
3145 }
3146 else
3147 break;
3148 }
3149
3150 // TODO - see handle_subscript():
3151 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3152 // Don't do this when "Func" is already a partial that was bound
3153 // explicitly (pt_auto is FALSE).
3154
3155 return OK;
3156}
3157
3158/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003159 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3160 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003162 * If the value is a constant "ppconst->pp_ret" will be set.
3163 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003164 *
3165 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 */
3167
3168/*
3169 * number number constant
3170 * 0zFFFFFFFF Blob constant
3171 * "string" string constant
3172 * 'string' literal string constant
3173 * &option-name option value
3174 * @r register contents
3175 * identifier variable value
3176 * function() function call
3177 * $VAR environment variable
3178 * (expression) nested expression
3179 * [expr, expr] List
3180 * {key: val, key: val} Dictionary
3181 * #{key: val, key: val} Dictionary with literal keys
3182 *
3183 * Also handle:
3184 * ! in front logical NOT
3185 * - in front unary minus
3186 * + in front unary plus (ignored)
3187 * trailing (arg) funcref/partial call
3188 * trailing [] subscript in String or List
3189 * trailing .name entry in Dictionary
3190 * trailing ->name() method call
3191 */
3192 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003193compile_expr7(
3194 char_u **arg,
3195 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003196 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 char_u *start_leader, *end_leader;
3199 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003200 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003201 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202
3203 /*
3204 * Skip '!', '-' and '+' characters. They are handled later.
3205 */
3206 start_leader = *arg;
3207 while (**arg == '!' || **arg == '-' || **arg == '+')
3208 *arg = skipwhite(*arg + 1);
3209 end_leader = *arg;
3210
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003211 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 switch (**arg)
3213 {
3214 /*
3215 * Number constant.
3216 */
3217 case '0': // also for blob starting with 0z
3218 case '1':
3219 case '2':
3220 case '3':
3221 case '4':
3222 case '5':
3223 case '6':
3224 case '7':
3225 case '8':
3226 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003227 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003229 // Apply "-" and "+" just before the number now, right to
3230 // left. Matters especially when "->" follows. Stops at
3231 // '!'.
3232 if (apply_leader(rettv, TRUE,
3233 start_leader, &end_leader) == FAIL)
3234 {
3235 clear_tv(rettv);
3236 return FAIL;
3237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 break;
3239
3240 /*
3241 * String constant: "string".
3242 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003243 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 return FAIL;
3245 break;
3246
3247 /*
3248 * Literal string constant: 'str''ing'.
3249 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003250 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 return FAIL;
3252 break;
3253
3254 /*
3255 * Constant Vim variable.
3256 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003257 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 ret = NOTDONE;
3259 break;
3260
3261 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003262 * "true" constant
3263 */
3264 case 't': if (STRNCMP(*arg, "true", 4) == 0
3265 && !eval_isnamec((*arg)[4]))
3266 {
3267 *arg += 4;
3268 rettv->v_type = VAR_BOOL;
3269 rettv->vval.v_number = VVAL_TRUE;
3270 }
3271 else
3272 ret = NOTDONE;
3273 break;
3274
3275 /*
3276 * "false" constant
3277 */
3278 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3279 && !eval_isnamec((*arg)[5]))
3280 {
3281 *arg += 5;
3282 rettv->v_type = VAR_BOOL;
3283 rettv->vval.v_number = VVAL_FALSE;
3284 }
3285 else
3286 ret = NOTDONE;
3287 break;
3288
3289 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 * List: [expr, expr]
3291 */
3292 case '[': ret = compile_list(arg, cctx);
3293 break;
3294
3295 /*
3296 * Dictionary: #{key: val, key: val}
3297 */
3298 case '#': if ((*arg)[1] == '{')
3299 {
3300 ++*arg;
3301 ret = compile_dict(arg, cctx, TRUE);
3302 }
3303 else
3304 ret = NOTDONE;
3305 break;
3306
3307 /*
3308 * Lambda: {arg, arg -> expr}
3309 * Dictionary: {'key': val, 'key': val}
3310 */
3311 case '{': {
3312 char_u *start = skipwhite(*arg + 1);
3313
3314 // Find out what comes after the arguments.
3315 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003316 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 if (ret != FAIL && *start == '>')
3318 ret = compile_lambda(arg, cctx);
3319 else
3320 ret = compile_dict(arg, cctx, FALSE);
3321 }
3322 break;
3323
3324 /*
3325 * Option value: &name
3326 */
3327 case '&': ret = compile_get_option(arg, cctx);
3328 break;
3329
3330 /*
3331 * Environment variable: $VAR.
3332 */
3333 case '$': ret = compile_get_env(arg, cctx);
3334 break;
3335
3336 /*
3337 * Register contents: @r.
3338 */
3339 case '@': ret = compile_get_register(arg, cctx);
3340 break;
3341 /*
3342 * nested expression: (expression).
3343 */
3344 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003345
3346 // recursive!
3347 if (ppconst->pp_used <= PPSIZE - 10)
3348 {
3349 ret = compile_expr1(arg, cctx, ppconst);
3350 }
3351 else
3352 {
3353 // Not enough space in ppconst, flush constants.
3354 if (generate_ppconst(cctx, ppconst) == FAIL)
3355 return FAIL;
3356 ret = compile_expr0(arg, cctx);
3357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 *arg = skipwhite(*arg);
3359 if (**arg == ')')
3360 ++*arg;
3361 else if (ret == OK)
3362 {
3363 emsg(_(e_missing_close));
3364 ret = FAIL;
3365 }
3366 break;
3367
3368 default: ret = NOTDONE;
3369 break;
3370 }
3371 if (ret == FAIL)
3372 return FAIL;
3373
Bram Moolenaar1c747212020-05-09 18:28:34 +02003374 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003376 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003377 clear_tv(rettv);
3378 else
3379 // A constant expression can possibly be handled compile time,
3380 // return the value instead of generating code.
3381 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 }
3383 else if (ret == NOTDONE)
3384 {
3385 char_u *p;
3386 int r;
3387
3388 if (!eval_isnamec1(**arg))
3389 {
3390 semsg(_("E1015: Name expected: %s"), *arg);
3391 return FAIL;
3392 }
3393
3394 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003395 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003397 {
3398 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003401 {
3402 if (generate_ppconst(cctx, ppconst) == FAIL)
3403 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 if (r == FAIL)
3407 return FAIL;
3408 }
3409
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003410 // Handle following "[]", ".member", etc.
3411 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003412 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003413 ppconst) == FAIL)
3414 return FAIL;
3415 if (ppconst->pp_used > 0)
3416 {
3417 // apply the '!', '-' and '+' before the constant
3418 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003419 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003420 return FAIL;
3421 return OK;
3422 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003423 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003425 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426}
3427
3428/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003429 * Give the "white on both sides" error, taking the operator from "p[len]".
3430 */
3431 void
3432error_white_both(char_u *op, int len)
3433{
3434 char_u buf[10];
3435
3436 vim_strncpy(buf, op, len);
3437 semsg(_(e_white_both), buf);
3438}
3439
3440/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003441 * <type>expr7: runtime type check / conversion
3442 */
3443 static int
3444compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3445{
3446 type_T *want_type = NULL;
3447
3448 // Recognize <type>
3449 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3450 {
3451 int called_emsg_before = called_emsg;
3452
3453 ++*arg;
3454 want_type = parse_type(arg, cctx->ctx_type_list);
3455 if (called_emsg != called_emsg_before)
3456 return FAIL;
3457
3458 if (**arg != '>')
3459 {
3460 if (*skipwhite(*arg) == '>')
3461 semsg(_(e_no_white_before), ">");
3462 else
3463 emsg(_("E1104: Missing >"));
3464 return FAIL;
3465 }
3466 ++*arg;
3467 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3468 return FAIL;
3469 }
3470
3471 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3472 return FAIL;
3473
3474 if (want_type != NULL)
3475 {
3476 garray_T *stack = &cctx->ctx_type_stack;
3477 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3478
3479 if (check_type(want_type, actual, FALSE) == FAIL)
3480 {
3481 generate_ppconst(cctx, ppconst);
3482 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3483 return FAIL;
3484 }
3485 }
3486
3487 return OK;
3488}
3489
3490/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 * * number multiplication
3492 * / number division
3493 * % number modulo
3494 */
3495 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003496compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497{
3498 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003499 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003500 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003502 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003503 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 return FAIL;
3505
3506 /*
3507 * Repeat computing, until no "*", "/" or "%" is following.
3508 */
3509 for (;;)
3510 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003511 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 if (*op != '*' && *op != '/' && *op != '%')
3513 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003514 if (next != NULL)
3515 {
3516 *arg = next_line_from_context(cctx, TRUE);
3517 op = skipwhite(*arg);
3518 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003519
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003520 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003522 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003526 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003527 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003529 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003530 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003532
3533 if (ppconst->pp_used == ppconst_used + 2
3534 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3535 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003536 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003537 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3538 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003539 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003541 // both are numbers: compute the result
3542 switch (*op)
3543 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003544 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003545 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003546 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003547 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003548 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003549 break;
3550 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003551 tv1->vval.v_number = res;
3552 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003553 }
3554 else
3555 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003556 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003557 generate_two_op(cctx, op);
3558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560
3561 return OK;
3562}
3563
3564/*
3565 * + number addition
3566 * - number subtraction
3567 * .. string concatenation
3568 */
3569 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003570compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571{
3572 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003573 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003575 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576
3577 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003578 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 return FAIL;
3580
3581 /*
3582 * Repeat computing, until no "+", "-" or ".." is following.
3583 */
3584 for (;;)
3585 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003586 op = may_peek_next_line(cctx, *arg, &next);
3587 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 break;
3589 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003590 if (next != NULL)
3591 {
3592 *arg = next_line_from_context(cctx, TRUE);
3593 op = skipwhite(*arg);
3594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003596 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003598 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 }
3601
3602 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003603 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003604 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003606 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003607 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 return FAIL;
3609
Bram Moolenaara5565e42020-05-09 15:44:01 +02003610 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003611 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003612 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3613 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3614 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3615 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003617 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3618 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003619
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003620 // concat/subtract/add constant numbers
3621 if (*op == '+')
3622 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3623 else if (*op == '-')
3624 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3625 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003627 // concatenate constant strings
3628 char_u *s1 = tv1->vval.v_string;
3629 char_u *s2 = tv2->vval.v_string;
3630 size_t len1 = STRLEN(s1);
3631
3632 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3633 if (tv1->vval.v_string == NULL)
3634 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003635 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003636 return FAIL;
3637 }
3638 mch_memmove(tv1->vval.v_string, s1, len1);
3639 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003640 vim_free(s1);
3641 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003643 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 }
3645 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003646 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003647 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003648 if (*op == '.')
3649 {
3650 if (may_generate_2STRING(-2, cctx) == FAIL
3651 || may_generate_2STRING(-1, cctx) == FAIL)
3652 return FAIL;
3653 generate_instr_drop(cctx, ISN_CONCAT, 1);
3654 }
3655 else
3656 generate_two_op(cctx, op);
3657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 }
3659
3660 return OK;
3661}
3662
3663/*
3664 * expr5a == expr5b
3665 * expr5a =~ expr5b
3666 * expr5a != expr5b
3667 * expr5a !~ expr5b
3668 * expr5a > expr5b
3669 * expr5a >= expr5b
3670 * expr5a < expr5b
3671 * expr5a <= expr5b
3672 * expr5a is expr5b
3673 * expr5a isnot expr5b
3674 *
3675 * Produces instructions:
3676 * EVAL expr5a Push result of "expr5a"
3677 * EVAL expr5b Push result of "expr5b"
3678 * COMPARE one of the compare instructions
3679 */
3680 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003681compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682{
3683 exptype_T type = EXPR_UNKNOWN;
3684 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003685 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003688 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689
3690 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003691 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003694 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003695 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696
3697 /*
3698 * If there is a comparative operator, use it.
3699 */
3700 if (type != EXPR_UNKNOWN)
3701 {
3702 int ic = FALSE; // Default: do not ignore case
3703
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003704 if (next != NULL)
3705 {
3706 *arg = next_line_from_context(cctx, TRUE);
3707 p = skipwhite(*arg);
3708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 if (type_is && (p[len] == '?' || p[len] == '#'))
3710 {
3711 semsg(_(e_invexpr2), *arg);
3712 return FAIL;
3713 }
3714 // extra question mark appended: ignore case
3715 if (p[len] == '?')
3716 {
3717 ic = TRUE;
3718 ++len;
3719 }
3720 // extra '#' appended: match case (ignored)
3721 else if (p[len] == '#')
3722 ++len;
3723 // nothing appended: match case
3724
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003725 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003727 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003728 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730
3731 // get the second variable
3732 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003733 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003734 return FAIL;
3735
Bram Moolenaara5565e42020-05-09 15:44:01 +02003736 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 return FAIL;
3738
Bram Moolenaara5565e42020-05-09 15:44:01 +02003739 if (ppconst->pp_used == ppconst_used + 2)
3740 {
3741 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3742 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3743 int ret;
3744
3745 // Both sides are a constant, compute the result now.
3746 // First check for a valid combination of types, this is more
3747 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003748 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003749 ret = FAIL;
3750 else
3751 {
3752 ret = typval_compare(tv1, tv2, type, ic);
3753 tv1->v_type = VAR_BOOL;
3754 tv1->vval.v_number = tv1->vval.v_number
3755 ? VVAL_TRUE : VVAL_FALSE;
3756 clear_tv(tv2);
3757 --ppconst->pp_used;
3758 }
3759 return ret;
3760 }
3761
3762 generate_ppconst(cctx, ppconst);
3763 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 }
3765
3766 return OK;
3767}
3768
Bram Moolenaar7f141552020-05-09 17:35:53 +02003769static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771/*
3772 * Compile || or &&.
3773 */
3774 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003775compile_and_or(
3776 char_u **arg,
3777 cctx_T *cctx,
3778 char *op,
3779 ppconst_T *ppconst,
3780 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003782 char_u *next;
3783 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 int opchar = *op;
3785
3786 if (p[0] == opchar && p[1] == opchar)
3787 {
3788 garray_T *instr = &cctx->ctx_instr;
3789 garray_T end_ga;
3790
3791 /*
3792 * Repeat until there is no following "||" or "&&"
3793 */
3794 ga_init2(&end_ga, sizeof(int), 10);
3795 while (p[0] == opchar && p[1] == opchar)
3796 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003797 if (next != NULL)
3798 {
3799 *arg = next_line_from_context(cctx, TRUE);
3800 p = skipwhite(*arg);
3801 }
3802
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003803 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3804 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003806 return FAIL;
3807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808
Bram Moolenaara5565e42020-05-09 15:44:01 +02003809 // TODO: use ppconst if the value is a constant
3810 generate_ppconst(cctx, ppconst);
3811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 if (ga_grow(&end_ga, 1) == FAIL)
3813 {
3814 ga_clear(&end_ga);
3815 return FAIL;
3816 }
3817 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3818 ++end_ga.ga_len;
3819 generate_JUMP(cctx, opchar == '|'
3820 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3821
3822 // eval the next expression
3823 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003824 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003825 return FAIL;
3826
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3828 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 {
3830 ga_clear(&end_ga);
3831 return FAIL;
3832 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003833
3834 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837
3838 // Fill in the end label in all jumps.
3839 while (end_ga.ga_len > 0)
3840 {
3841 isn_T *isn;
3842
3843 --end_ga.ga_len;
3844 isn = ((isn_T *)instr->ga_data)
3845 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3846 isn->isn_arg.jump.jump_where = instr->ga_len;
3847 }
3848 ga_clear(&end_ga);
3849 }
3850
3851 return OK;
3852}
3853
3854/*
3855 * expr4a && expr4a && expr4a logical AND
3856 *
3857 * Produces instructions:
3858 * EVAL expr4a Push result of "expr4a"
3859 * JUMP_AND_KEEP_IF_FALSE end
3860 * EVAL expr4b Push result of "expr4b"
3861 * JUMP_AND_KEEP_IF_FALSE end
3862 * EVAL expr4c Push result of "expr4c"
3863 * end:
3864 */
3865 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003866compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003868 int ppconst_used = ppconst->pp_used;
3869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003871 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return FAIL;
3873
3874 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003875 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876}
3877
3878/*
3879 * expr3a || expr3b || expr3c logical OR
3880 *
3881 * Produces instructions:
3882 * EVAL expr3a Push result of "expr3a"
3883 * JUMP_AND_KEEP_IF_TRUE end
3884 * EVAL expr3b Push result of "expr3b"
3885 * JUMP_AND_KEEP_IF_TRUE end
3886 * EVAL expr3c Push result of "expr3c"
3887 * end:
3888 */
3889 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003892 int ppconst_used = ppconst->pp_used;
3893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 return FAIL;
3897
3898 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900}
3901
3902/*
3903 * Toplevel expression: expr2 ? expr1a : expr1b
3904 *
3905 * Produces instructions:
3906 * EVAL expr2 Push result of "expr"
3907 * JUMP_IF_FALSE alt jump if false
3908 * EVAL expr1a
3909 * JUMP_ALWAYS end
3910 * alt: EVAL expr1b
3911 * end:
3912 */
3913 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915{
3916 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003917 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003918 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaar61a89812020-05-07 16:58:17 +02003920 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003921 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 return FAIL;
3923
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003924 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 if (*p == '?')
3926 {
3927 garray_T *instr = &cctx->ctx_instr;
3928 garray_T *stack = &cctx->ctx_type_stack;
3929 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003930 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003932 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003934 int has_const_expr = FALSE;
3935 int const_value = FALSE;
3936 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003938 if (next != NULL)
3939 {
3940 *arg = next_line_from_context(cctx, TRUE);
3941 p = skipwhite(*arg);
3942 }
3943
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003944 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3945 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003947 return FAIL;
3948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 if (ppconst->pp_used == ppconst_used + 1)
3951 {
3952 // the condition is a constant, we know whether the ? or the :
3953 // expression is to be evaluated.
3954 has_const_expr = TRUE;
3955 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3956 clear_tv(&ppconst->pp_tv[ppconst_used]);
3957 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003958 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3959 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003960 }
3961 else
3962 {
3963 generate_ppconst(cctx, ppconst);
3964 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966
3967 // evaluate the second expression; any type is accepted
3968 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003969 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003970 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003972 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973
Bram Moolenaara5565e42020-05-09 15:44:01 +02003974 if (!has_const_expr)
3975 {
3976 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 // remember the type and drop it
3979 --stack->ga_len;
3980 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981
Bram Moolenaara5565e42020-05-09 15:44:01 +02003982 end_idx = instr->ga_len;
3983 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3984
3985 // jump here from JUMP_IF_FALSE
3986 isn = ((isn_T *)instr->ga_data) + alt_idx;
3987 isn->isn_arg.jump.jump_where = instr->ga_len;
3988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989
3990 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003991 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 if (*p != ':')
3993 {
3994 emsg(_(e_missing_colon));
3995 return FAIL;
3996 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 if (next != NULL)
3998 {
3999 *arg = next_line_from_context(cctx, TRUE);
4000 p = skipwhite(*arg);
4001 }
4002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004003 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4004 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 return FAIL;
4007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
4009 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004010 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004011 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4012 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004014 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004015 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004016 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaara5565e42020-05-09 15:44:01 +02004019 if (!has_const_expr)
4020 {
4021 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 // If the types differ, the result has a more generic type.
4024 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4025 common_type(type1, type2, &type2, cctx->ctx_type_list);
4026
4027 // jump here from JUMP_ALWAYS
4028 isn = ((isn_T *)instr->ga_data) + end_idx;
4029 isn->isn_arg.jump.jump_where = instr->ga_len;
4030 }
4031
4032 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 }
4034 return OK;
4035}
4036
4037/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 * Toplevel expression.
4039 */
4040 static int
4041compile_expr0(char_u **arg, cctx_T *cctx)
4042{
4043 ppconst_T ppconst;
4044
4045 CLEAR_FIELD(ppconst);
4046 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4047 {
4048 clear_ppconst(&ppconst);
4049 return FAIL;
4050 }
4051 if (generate_ppconst(cctx, &ppconst) == FAIL)
4052 return FAIL;
4053 return OK;
4054}
4055
4056/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 * compile "return [expr]"
4058 */
4059 static char_u *
4060compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4061{
4062 char_u *p = arg;
4063 garray_T *stack = &cctx->ctx_type_stack;
4064 type_T *stack_type;
4065
4066 if (*p != NUL && *p != '|' && *p != '\n')
4067 {
4068 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004069 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 return NULL;
4071
4072 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4073 if (set_return_type)
4074 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004075 else
4076 {
4077 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4078 && stack_type->tt_type != VAR_VOID
4079 && stack_type->tt_type != VAR_UNKNOWN)
4080 {
4081 emsg(_("E1096: Returning a value in a function without a return type"));
4082 return NULL;
4083 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004084 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4085 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 }
4089 else
4090 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004091 // "set_return_type" cannot be TRUE, only used for a lambda which
4092 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004093 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4094 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 {
4096 emsg(_("E1003: Missing return value"));
4097 return NULL;
4098 }
4099
4100 // No argument, return zero.
4101 generate_PUSHNR(cctx, 0);
4102 }
4103
4104 if (generate_instr(cctx, ISN_RETURN) == NULL)
4105 return NULL;
4106
4107 // "return val | endif" is possible
4108 return skipwhite(p);
4109}
4110
4111/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004112 * Get a line from the compilation context, compatible with exarg_T getline().
4113 * Return a pointer to the line in allocated memory.
4114 * Return NULL for end-of-file or some error.
4115 */
4116 static char_u *
4117exarg_getline(
4118 int c UNUSED,
4119 void *cookie,
4120 int indent UNUSED,
4121 int do_concat UNUSED)
4122{
4123 cctx_T *cctx = (cctx_T *)cookie;
4124
4125 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4126 {
4127 iemsg("Heredoc got to end");
4128 return NULL;
4129 }
4130 ++cctx->ctx_lnum;
4131 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4132 [cctx->ctx_lnum]);
4133}
4134
4135/*
4136 * Compile a nested :def command.
4137 */
4138 static char_u *
4139compile_nested_function(exarg_T *eap, cctx_T *cctx)
4140{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004141 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004142 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004143 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004144 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004145 lvar_T *lvar;
4146 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004147 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004148
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004149 // Only g:Func() can use a namespace.
4150 if (name_start[1] == ':' && !is_global)
4151 {
4152 semsg(_(e_namespace), name_start);
4153 return NULL;
4154 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004155 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4156 return NULL;
4157
Bram Moolenaar04b12692020-05-04 23:24:44 +02004158 eap->arg = name_end;
4159 eap->getline = exarg_getline;
4160 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004161 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004162 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004163 lambda_name = get_lambda_name();
4164 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004165
Bram Moolenaar822ba242020-05-24 23:00:18 +02004166 if (ufunc == NULL)
4167 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004168 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004169 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004170 return NULL;
4171
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004172 if (is_global)
4173 {
4174 char_u *func_name = vim_strnsave(name_start + 2,
4175 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004176
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004177 if (func_name == NULL)
4178 r = FAIL;
4179 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004180 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004181 }
4182 else
4183 {
4184 // Define a local variable for the function reference.
4185 lvar = reserve_local(cctx, name_start, name_end - name_start,
4186 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004187 if (lvar == NULL)
4188 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004189 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004190 return NULL;
4191 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4192 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004193
Bram Moolenaar61a89812020-05-07 16:58:17 +02004194 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004195 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004196}
4197
4198/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 * Return the length of an assignment operator, or zero if there isn't one.
4200 */
4201 int
4202assignment_len(char_u *p, int *heredoc)
4203{
4204 if (*p == '=')
4205 {
4206 if (p[1] == '<' && p[2] == '<')
4207 {
4208 *heredoc = TRUE;
4209 return 3;
4210 }
4211 return 1;
4212 }
4213 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4214 return 2;
4215 if (STRNCMP(p, "..=", 3) == 0)
4216 return 3;
4217 return 0;
4218}
4219
4220// words that cannot be used as a variable
4221static char *reserved[] = {
4222 "true",
4223 "false",
4224 NULL
4225};
4226
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004227typedef enum {
4228 dest_local,
4229 dest_option,
4230 dest_env,
4231 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004232 dest_buffer,
4233 dest_window,
4234 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004235 dest_vimvar,
4236 dest_script,
4237 dest_reg,
4238} assign_dest_T;
4239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004241 * Generate the load instruction for "name".
4242 */
4243 static void
4244generate_loadvar(
4245 cctx_T *cctx,
4246 assign_dest_T dest,
4247 char_u *name,
4248 lvar_T *lvar,
4249 type_T *type)
4250{
4251 switch (dest)
4252 {
4253 case dest_option:
4254 // TODO: check the option exists
4255 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4256 break;
4257 case dest_global:
4258 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4259 break;
4260 case dest_buffer:
4261 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4262 break;
4263 case dest_window:
4264 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4265 break;
4266 case dest_tab:
4267 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4268 break;
4269 case dest_script:
4270 compile_load_scriptvar(cctx,
4271 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4272 break;
4273 case dest_env:
4274 // Include $ in the name here
4275 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4276 break;
4277 case dest_reg:
4278 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4279 break;
4280 case dest_vimvar:
4281 generate_LOADV(cctx, name + 2, TRUE);
4282 break;
4283 case dest_local:
4284 if (lvar->lv_from_outer)
4285 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4286 NULL, type);
4287 else
4288 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4289 break;
4290 }
4291}
4292
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004293 void
4294vim9_declare_error(char_u *name)
4295{
4296 char *scope = "";
4297
4298 switch (*name)
4299 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004300 case 'g': scope = _("global"); break;
4301 case 'b': scope = _("buffer"); break;
4302 case 'w': scope = _("window"); break;
4303 case 't': scope = _("tab"); break;
4304 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004305 case '$': semsg(_(e_declare_env_var), name);
4306 return;
4307 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4308 return;
4309 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4310 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004311 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004312 }
4313 semsg(_(e_declare_var), scope, name);
4314}
4315
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004316/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004317 * Compile declaration and assignment:
4318 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004320 * Return NULL for an error.
4321 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 */
4323 static char_u *
4324compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4325{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004326 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004328 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 char_u *ret = NULL;
4330 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004331 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004332 int scriptvar_sid = 0;
4333 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004336 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 int oplen = 0;
4339 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004340 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004341 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004342 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004346 // Skip over the "var" or "[var, var]" to get to any "=".
4347 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4348 if (p == NULL)
4349 return *arg == '[' ? arg : NULL;
4350
4351 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004353 // TODO: should we allow this, and figure out type inference from list
4354 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004355 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 return NULL;
4357 }
4358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 sp = p;
4360 p = skipwhite(p);
4361 op = p;
4362 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004363
4364 if (var_count > 0 && oplen == 0)
4365 // can be something like "[1, 2]->func()"
4366 return arg;
4367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4369 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004370 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004371 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004372 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 if (heredoc)
4375 {
4376 list_T *l;
4377 listitem_T *li;
4378
4379 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004380 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004382 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
4384 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004385 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 {
4387 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4388 li->li_tv.vval.v_string = NULL;
4389 }
4390 generate_NEWLIST(cctx, l->lv_len);
4391 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004392 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 list_free(l);
4394 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004395 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004397 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004399 // for "[var, var] = expr" evaluate the expression here, loop over the
4400 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004401
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004402 p = skipwhite(op + oplen);
4403 if (compile_expr0(&p, cctx) == FAIL)
4404 return NULL;
4405 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004407 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004409 type_T *stacktype;
4410
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004411 stacktype = stack->ga_len == 0 ? &t_void
4412 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004413 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004415 emsg(_(e_cannot_use_void));
4416 goto theend;
4417 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004418 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004419 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004420 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004421 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4422 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004423 }
4424 }
4425
4426 /*
4427 * Loop over variables in "[var, var] = expr".
4428 * For "var = expr" and "let var: type" this is done only once.
4429 */
4430 if (var_count > 0)
4431 var_start = skipwhite(arg + 1); // skip over the "["
4432 else
4433 var_start = arg;
4434 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4435 {
4436 char_u *var_end = skip_var_one(var_start, FALSE);
4437 size_t varlen;
4438 int new_local = FALSE;
4439 int opt_type;
4440 int opt_flags = 0;
4441 assign_dest_T dest = dest_local;
4442 int vimvaridx = -1;
4443 lvar_T *lvar = NULL;
4444 lvar_T arg_lvar;
4445 int has_type = FALSE;
4446 int has_index = FALSE;
4447 int instr_count = -1;
4448
Bram Moolenaar65821722020-08-02 18:58:54 +02004449 if (*var_start == '@')
4450 p = var_start + 2;
4451 else
4452 {
4453 p = (*var_start == '&' || *var_start == '$')
4454 ? var_start + 1 : var_start;
4455 p = to_name_end(p, TRUE);
4456 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004457
4458 // "a: type" is declaring variable "a" with a type, not "a:".
4459 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4460 --var_end;
4461 if (is_decl && p == var_start + 2 && p[-1] == ':')
4462 --p;
4463
4464 varlen = p - var_start;
4465 vim_free(name);
4466 name = vim_strnsave(var_start, varlen);
4467 if (name == NULL)
4468 return NULL;
4469 if (!heredoc)
4470 type = &t_any;
4471
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004472 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004473 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004474 int declare_error = FALSE;
4475
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004476 if (*var_start == '&')
4477 {
4478 int cc;
4479 long numval;
4480
4481 dest = dest_option;
4482 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004484 emsg(_(e_const_option));
4485 goto theend;
4486 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004487 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004488 p = var_start;
4489 p = find_option_end(&p, &opt_flags);
4490 if (p == NULL)
4491 {
4492 // cannot happen?
4493 emsg(_(e_letunexp));
4494 goto theend;
4495 }
4496 cc = *p;
4497 *p = NUL;
4498 opt_type = get_option_value(var_start + 1, &numval,
4499 NULL, opt_flags);
4500 *p = cc;
4501 if (opt_type == -3)
4502 {
4503 semsg(_(e_unknown_option), var_start);
4504 goto theend;
4505 }
4506 if (opt_type == -2 || opt_type == 0)
4507 type = &t_string;
4508 else
4509 type = &t_number; // both number and boolean option
4510 }
4511 else if (*var_start == '$')
4512 {
4513 dest = dest_env;
4514 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004515 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004516 }
4517 else if (*var_start == '@')
4518 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004519 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004520 {
4521 emsg_invreg(var_start[1]);
4522 goto theend;
4523 }
4524 dest = dest_reg;
4525 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004526 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004527 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004528 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004529 {
4530 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004531 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004532 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004533 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004534 {
4535 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004536 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004537 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004538 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 {
4540 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004541 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004542 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004543 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 {
4545 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004546 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004547 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004548 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004549 {
4550 typval_T *vtv;
4551 int di_flags;
4552
4553 vimvaridx = find_vim_var(name + 2, &di_flags);
4554 if (vimvaridx < 0)
4555 {
4556 semsg(_(e_var_notfound), var_start);
4557 goto theend;
4558 }
4559 // We use the current value of "sandbox" here, is that OK?
4560 if (var_check_ro(di_flags, name, FALSE))
4561 goto theend;
4562 dest = dest_vimvar;
4563 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004564 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004565 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004566 }
4567 else
4568 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004569 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004570
4571 for (idx = 0; reserved[idx] != NULL; ++idx)
4572 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004573 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004574 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004575 goto theend;
4576 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577
4578 lvar = lookup_local(var_start, varlen, cctx);
4579 if (lvar == NULL)
4580 {
4581 CLEAR_FIELD(arg_lvar);
4582 if (lookup_arg(var_start, varlen,
4583 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4584 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004585 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004586 if (is_decl)
4587 {
4588 semsg(_(e_used_as_arg), name);
4589 goto theend;
4590 }
4591 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004592 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004593 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004594 if (lvar != NULL)
4595 {
4596 if (is_decl)
4597 {
4598 semsg(_("E1017: Variable already declared: %s"), name);
4599 goto theend;
4600 }
4601 else if (lvar->lv_const)
4602 {
4603 semsg(_("E1018: Cannot assign to a constant: %s"),
4604 name);
4605 goto theend;
4606 }
4607 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004608 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004609 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004610 int script_namespace = varlen > 1
4611 && STRNCMP(var_start, "s:", 2) == 0;
4612 int script_var = (script_namespace
4613 ? lookup_script(var_start + 2, varlen - 2)
4614 : lookup_script(var_start, varlen)) == OK;
4615 imported_T *import =
4616 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004617
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004618 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004619 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004620 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4621
4622 if (is_decl)
4623 {
4624 if (script_namespace)
4625 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004626 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004627 else
4628 semsg(_("E1054: Variable already declared in the script: %s"),
4629 name);
4630 goto theend;
4631 }
4632 else if (cctx->ctx_ufunc->uf_script_ctx_version
4633 == SCRIPT_VERSION_VIM9
4634 && script_namespace
4635 && !script_var && import == NULL)
4636 {
4637 semsg(_(e_unknown_var), name);
4638 goto theend;
4639 }
4640
4641 dest = dest_script;
4642
4643 // existing script-local variables should have a type
4644 scriptvar_sid = current_sctx.sc_sid;
4645 if (import != NULL)
4646 scriptvar_sid = import->imp_sid;
4647 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4648 rawname, TRUE);
4649 if (scriptvar_idx >= 0)
4650 {
4651 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4652 svar_T *sv =
4653 ((svar_T *)si->sn_var_vals.ga_data)
4654 + scriptvar_idx;
4655 type = sv->sv_type;
4656 }
4657 }
4658 else if (name[1] == ':' && name[2] != NUL)
4659 {
4660 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004661 name);
4662 goto theend;
4663 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004664 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004665 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004666 semsg(_(e_unknown_var), name);
4667 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004668 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004669 else if (check_defined(var_start, varlen, cctx) == FAIL)
4670 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004671 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004672 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004673
4674 if (declare_error)
4675 {
4676 vim9_declare_error(name);
4677 goto theend;
4678 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004679 }
4680
4681 // handle "a:name" as a name, not index "name" on "a"
4682 if (varlen > 1 || var_start[varlen] != ':')
4683 p = var_end;
4684
4685 if (dest != dest_option)
4686 {
4687 if (is_decl && *p == ':')
4688 {
4689 // parse optional type: "let var: type = expr"
4690 if (!VIM_ISWHITE(p[1]))
4691 {
4692 semsg(_(e_white_after), ":");
4693 goto theend;
4694 }
4695 p = skipwhite(p + 1);
4696 type = parse_type(&p, cctx->ctx_type_list);
4697 has_type = TRUE;
4698 }
4699 else if (lvar != NULL)
4700 type = lvar->lv_type;
4701 }
4702
4703 if (oplen == 3 && !heredoc && dest != dest_global
4704 && type->tt_type != VAR_STRING
4705 && type->tt_type != VAR_ANY)
4706 {
4707 emsg(_("E1019: Can only concatenate to string"));
4708 goto theend;
4709 }
4710
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004711 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004712 {
4713 if (oplen > 1 && !heredoc)
4714 {
4715 // +=, /=, etc. require an existing variable
4716 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4717 name);
4718 goto theend;
4719 }
4720
4721 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004722 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4723 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004724 goto theend;
4725 lvar = reserve_local(cctx, var_start, varlen,
4726 cmdidx == CMD_const, type);
4727 if (lvar == NULL)
4728 goto theend;
4729 new_local = TRUE;
4730 }
4731
4732 member_type = type;
4733 if (var_end > var_start + varlen)
4734 {
4735 // Something follows after the variable: "var[idx]".
4736 if (is_decl)
4737 {
4738 emsg(_("E1087: cannot use an index when declaring a variable"));
4739 goto theend;
4740 }
4741
4742 if (var_start[varlen] == '[')
4743 {
4744 has_index = TRUE;
4745 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004746 member_type = &t_any;
4747 else
4748 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004749 }
4750 else
4751 {
4752 semsg("Not supported yet: %s", var_start);
4753 goto theend;
4754 }
4755 }
4756 else if (lvar == &arg_lvar)
4757 {
4758 semsg(_("E1090: Cannot assign to argument %s"), name);
4759 goto theend;
4760 }
4761
4762 if (!heredoc)
4763 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004764 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004765 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004766 if (oplen > 0 && var_count == 0)
4767 {
4768 // skip over the "=" and the expression
4769 p = skipwhite(op + oplen);
4770 compile_expr0(&p, cctx);
4771 }
4772 }
4773 else if (oplen > 0)
4774 {
4775 type_T *stacktype;
4776
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 // For "var = expr" evaluate the expression.
4778 if (var_count == 0)
4779 {
4780 int r;
4781
4782 // for "+=", "*=", "..=" etc. first load the current value
4783 if (*op != '=')
4784 {
4785 generate_loadvar(cctx, dest, name, lvar, type);
4786
4787 if (has_index)
4788 {
4789 // TODO: get member from list or dict
4790 emsg("Index with operation not supported yet");
4791 goto theend;
4792 }
4793 }
4794
4795 // Compile the expression. Temporarily hide the new local
4796 // variable here, it is not available to this expression.
4797 if (new_local)
4798 --cctx->ctx_locals.ga_len;
4799 instr_count = instr->ga_len;
4800 p = skipwhite(op + oplen);
4801 r = compile_expr0(&p, cctx);
4802 if (new_local)
4803 ++cctx->ctx_locals.ga_len;
4804 if (r == FAIL)
4805 goto theend;
4806 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004807 else if (semicolon && var_idx == var_count - 1)
4808 {
4809 // For "[var; var] = expr" get the rest of the list
4810 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4811 goto theend;
4812 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004813 else
4814 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 // For "[var, var] = expr" get the "var_idx" item from the
4816 // list.
4817 if (generate_GETITEM(cctx, var_idx) == FAIL)
4818 return FAIL;
4819 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004820
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004821 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004822 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004823 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004824 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004825 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004826 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004827 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004829 emsg(_(e_cannot_use_void));
4830 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004832 else if ((stacktype->tt_type == VAR_FUNC
4833 || stacktype->tt_type == VAR_PARTIAL)
4834 && var_wrong_func_name(name, TRUE))
4835 {
4836 goto theend;
4837 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 else
4839 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004840 // An empty list or dict has a &t_void member,
4841 // for a variable that implies &t_any.
4842 if (stacktype == &t_list_empty)
4843 lvar->lv_type = &t_list_any;
4844 else if (stacktype == &t_dict_empty)
4845 lvar->lv_type = &t_dict_any;
4846 else
4847 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004849 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004850 else
4851 {
4852 type_T *use_type = lvar->lv_type;
4853
4854 if (has_index)
4855 {
4856 use_type = use_type->tt_member;
4857 if (use_type == NULL)
4858 use_type = &t_void;
4859 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004860 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004861 == FAIL)
4862 goto theend;
4863 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004864 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004865 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004866 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004867 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 emsg(_(e_const_req_value));
4872 goto theend;
4873 }
4874 else if (!has_type || dest == dest_option)
4875 {
4876 emsg(_(e_type_req));
4877 goto theend;
4878 }
4879 else
4880 {
4881 // variables are always initialized
4882 if (ga_grow(instr, 1) == FAIL)
4883 goto theend;
4884 switch (member_type->tt_type)
4885 {
4886 case VAR_BOOL:
4887 generate_PUSHBOOL(cctx, VVAL_FALSE);
4888 break;
4889 case VAR_FLOAT:
4890#ifdef FEAT_FLOAT
4891 generate_PUSHF(cctx, 0.0);
4892#endif
4893 break;
4894 case VAR_STRING:
4895 generate_PUSHS(cctx, NULL);
4896 break;
4897 case VAR_BLOB:
4898 generate_PUSHBLOB(cctx, NULL);
4899 break;
4900 case VAR_FUNC:
4901 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4902 break;
4903 case VAR_LIST:
4904 generate_NEWLIST(cctx, 0);
4905 break;
4906 case VAR_DICT:
4907 generate_NEWDICT(cctx, 0);
4908 break;
4909 case VAR_JOB:
4910 generate_PUSHJOB(cctx, NULL);
4911 break;
4912 case VAR_CHANNEL:
4913 generate_PUSHCHANNEL(cctx, NULL);
4914 break;
4915 case VAR_NUMBER:
4916 case VAR_UNKNOWN:
4917 case VAR_ANY:
4918 case VAR_PARTIAL:
4919 case VAR_VOID:
4920 case VAR_SPECIAL: // cannot happen
4921 generate_PUSHNR(cctx, 0);
4922 break;
4923 }
4924 }
4925 if (var_count == 0)
4926 end = p;
4927 }
4928
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004929 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004930 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004931 break;
4932
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004933 if (oplen > 0 && *op != '=')
4934 {
4935 type_T *expected = &t_number;
4936 type_T *stacktype;
4937
4938 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004939 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004940
4941 if (*op == '.')
4942 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004943 else if (*op == '+')
4944 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004946 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 goto theend;
4948
4949 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004950 {
4951 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4952 goto theend;
4953 }
4954 else if (*op == '+')
4955 {
4956 if (generate_add_instr(cctx,
4957 operator_type(member_type, stacktype),
4958 member_type, stacktype) == FAIL)
4959 goto theend;
4960 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 else
4962 {
4963 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4964
4965 if (isn == NULL)
4966 goto theend;
4967 switch (*op)
4968 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4970 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4971 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4972 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 }
4975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004978 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 int r;
4980
4981 // Compile the "idx" in "var[idx]".
4982 if (new_local)
4983 --cctx->ctx_locals.ga_len;
4984 p = skipwhite(var_start + varlen + 1);
4985 r = compile_expr0(&p, cctx);
4986 if (new_local)
4987 ++cctx->ctx_locals.ga_len;
4988 if (r == FAIL)
4989 goto theend;
4990 if (*skipwhite(p) != ']')
4991 {
4992 emsg(_(e_missbrac));
4993 goto theend;
4994 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004995 if (type == &t_any)
4996 {
4997 type_T *idx_type = ((type_T **)stack->ga_data)[
4998 stack->ga_len - 1];
4999 // Index on variable of unknown type: guess the type from the
5000 // index type: number is dict, otherwise dict.
5001 // TODO: should do the assignment at runtime
5002 if (idx_type->tt_type == VAR_NUMBER)
5003 type = &t_list_any;
5004 else
5005 type = &t_dict_any;
5006 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 if (type->tt_type == VAR_DICT
5008 && may_generate_2STRING(-1, cctx) == FAIL)
5009 goto theend;
5010 if (type->tt_type == VAR_LIST
5011 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005012 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 {
5014 emsg(_(e_number_exp));
5015 goto theend;
5016 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 // Load the dict or list. On the stack we then have:
5019 // - value
5020 // - index
5021 // - variable
5022 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 if (type->tt_type == VAR_LIST)
5025 {
5026 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5027 return FAIL;
5028 }
5029 else if (type->tt_type == VAR_DICT)
5030 {
5031 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5032 return FAIL;
5033 }
5034 else
5035 {
5036 emsg(_(e_listreq));
5037 goto theend;
5038 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005039 }
5040 else
5041 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 switch (dest)
5043 {
5044 case dest_option:
5045 generate_STOREOPT(cctx, name + 1, opt_flags);
5046 break;
5047 case dest_global:
5048 // include g: with the name, easier to execute that way
5049 generate_STORE(cctx, ISN_STOREG, 0, name);
5050 break;
5051 case dest_buffer:
5052 // include b: with the name, easier to execute that way
5053 generate_STORE(cctx, ISN_STOREB, 0, name);
5054 break;
5055 case dest_window:
5056 // include w: with the name, easier to execute that way
5057 generate_STORE(cctx, ISN_STOREW, 0, name);
5058 break;
5059 case dest_tab:
5060 // include t: with the name, easier to execute that way
5061 generate_STORE(cctx, ISN_STORET, 0, name);
5062 break;
5063 case dest_env:
5064 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5065 break;
5066 case dest_reg:
5067 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5068 break;
5069 case dest_vimvar:
5070 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5071 break;
5072 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005073 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005074 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 {
5076 char_u *name_s = name;
5077
5078 // Include s: in the name for store_var()
5079 if (name[1] != ':')
5080 {
5081 int len = (int)STRLEN(name) + 3;
5082
5083 name_s = alloc(len);
5084 if (name_s == NULL)
5085 name_s = name;
5086 else
5087 vim_snprintf((char *)name_s, len,
5088 "s:%s", name);
5089 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005090 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5091 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 if (name_s != name)
5093 vim_free(name_s);
5094 }
5095 else
5096 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005097 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005098 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 break;
5100 case dest_local:
5101 if (lvar != NULL)
5102 {
5103 isn_T *isn = ((isn_T *)instr->ga_data)
5104 + instr->ga_len - 1;
5105
5106 // optimization: turn "var = 123" from ISN_PUSHNR +
5107 // ISN_STORE into ISN_STORENR
5108 if (!lvar->lv_from_outer
5109 && instr->ga_len == instr_count + 1
5110 && isn->isn_type == ISN_PUSHNR)
5111 {
5112 varnumber_T val = isn->isn_arg.number;
5113
5114 isn->isn_type = ISN_STORENR;
5115 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5116 isn->isn_arg.storenr.stnr_val = val;
5117 if (stack->ga_len > 0)
5118 --stack->ga_len;
5119 }
5120 else if (lvar->lv_from_outer)
5121 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005122 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005123 else
5124 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5125 }
5126 break;
5127 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005128 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129
5130 if (var_idx + 1 < var_count)
5131 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133
5134 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005135 if (var_count > 0 && !semicolon)
5136 {
5137 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5138 goto theend;
5139 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140
Bram Moolenaarb2097502020-07-19 17:17:02 +02005141 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142
5143theend:
5144 vim_free(name);
5145 return ret;
5146}
5147
5148/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005149 * Check if "name" can be "unlet".
5150 */
5151 int
5152check_vim9_unlet(char_u *name)
5153{
5154 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5155 {
5156 semsg(_("E1081: Cannot unlet %s"), name);
5157 return FAIL;
5158 }
5159 return OK;
5160}
5161
5162/*
5163 * Callback passed to ex_unletlock().
5164 */
5165 static int
5166compile_unlet(
5167 lval_T *lvp,
5168 char_u *name_end,
5169 exarg_T *eap,
5170 int deep UNUSED,
5171 void *coookie)
5172{
5173 cctx_T *cctx = coookie;
5174
5175 if (lvp->ll_tv == NULL)
5176 {
5177 char_u *p = lvp->ll_name;
5178 int cc = *name_end;
5179 int ret = OK;
5180
5181 // Normal name. Only supports g:, w:, t: and b: namespaces.
5182 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005183 if (*p == '$')
5184 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5185 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005186 ret = FAIL;
5187 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005188 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005189
5190 *name_end = cc;
5191 return ret;
5192 }
5193
5194 // TODO: unlet {list}[idx]
5195 // TODO: unlet {dict}[key]
5196 emsg("Sorry, :unlet not fully implemented yet");
5197 return FAIL;
5198}
5199
5200/*
5201 * compile "unlet var", "lock var" and "unlock var"
5202 * "arg" points to "var".
5203 */
5204 static char_u *
5205compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5206{
5207 char_u *p = arg;
5208
5209 if (eap->cmdidx != CMD_unlet)
5210 {
5211 emsg("Sorry, :lock and unlock not implemented yet");
5212 return NULL;
5213 }
5214
5215 if (*p == '!')
5216 {
5217 p = skipwhite(p + 1);
5218 eap->forceit = TRUE;
5219 }
5220
5221 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5222 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5223}
5224
5225/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 * Compile an :import command.
5227 */
5228 static char_u *
5229compile_import(char_u *arg, cctx_T *cctx)
5230{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005231 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232}
5233
5234/*
5235 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5236 */
5237 static int
5238compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5239{
5240 garray_T *instr = &cctx->ctx_instr;
5241 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5242
5243 if (endlabel == NULL)
5244 return FAIL;
5245 endlabel->el_next = *el;
5246 *el = endlabel;
5247 endlabel->el_end_label = instr->ga_len;
5248
5249 generate_JUMP(cctx, when, 0);
5250 return OK;
5251}
5252
5253 static void
5254compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5255{
5256 garray_T *instr = &cctx->ctx_instr;
5257
5258 while (*el != NULL)
5259 {
5260 endlabel_T *cur = (*el);
5261 isn_T *isn;
5262
5263 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5264 isn->isn_arg.jump.jump_where = instr->ga_len;
5265 *el = cur->el_next;
5266 vim_free(cur);
5267 }
5268}
5269
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005270 static void
5271compile_free_jump_to_end(endlabel_T **el)
5272{
5273 while (*el != NULL)
5274 {
5275 endlabel_T *cur = (*el);
5276
5277 *el = cur->el_next;
5278 vim_free(cur);
5279 }
5280}
5281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282/*
5283 * Create a new scope and set up the generic items.
5284 */
5285 static scope_T *
5286new_scope(cctx_T *cctx, scopetype_T type)
5287{
5288 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5289
5290 if (scope == NULL)
5291 return NULL;
5292 scope->se_outer = cctx->ctx_scope;
5293 cctx->ctx_scope = scope;
5294 scope->se_type = type;
5295 scope->se_local_count = cctx->ctx_locals.ga_len;
5296 return scope;
5297}
5298
5299/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005300 * Free the current scope and go back to the outer scope.
5301 */
5302 static void
5303drop_scope(cctx_T *cctx)
5304{
5305 scope_T *scope = cctx->ctx_scope;
5306
5307 if (scope == NULL)
5308 {
5309 iemsg("calling drop_scope() without a scope");
5310 return;
5311 }
5312 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005313 switch (scope->se_type)
5314 {
5315 case IF_SCOPE:
5316 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5317 case FOR_SCOPE:
5318 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5319 case WHILE_SCOPE:
5320 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5321 case TRY_SCOPE:
5322 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5323 case NO_SCOPE:
5324 case BLOCK_SCOPE:
5325 break;
5326 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005327 vim_free(scope);
5328}
5329
5330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 * compile "if expr"
5332 *
5333 * "if expr" Produces instructions:
5334 * EVAL expr Push result of "expr"
5335 * JUMP_IF_FALSE end
5336 * ... body ...
5337 * end:
5338 *
5339 * "if expr | else" Produces instructions:
5340 * EVAL expr Push result of "expr"
5341 * JUMP_IF_FALSE else
5342 * ... body ...
5343 * JUMP_ALWAYS end
5344 * else:
5345 * ... body ...
5346 * end:
5347 *
5348 * "if expr1 | elseif expr2 | else" Produces instructions:
5349 * EVAL expr Push result of "expr"
5350 * JUMP_IF_FALSE elseif
5351 * ... body ...
5352 * JUMP_ALWAYS end
5353 * elseif:
5354 * EVAL expr Push result of "expr"
5355 * JUMP_IF_FALSE else
5356 * ... body ...
5357 * JUMP_ALWAYS end
5358 * else:
5359 * ... body ...
5360 * end:
5361 */
5362 static char_u *
5363compile_if(char_u *arg, cctx_T *cctx)
5364{
5365 char_u *p = arg;
5366 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005367 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005369 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005370 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371
Bram Moolenaara5565e42020-05-09 15:44:01 +02005372 CLEAR_FIELD(ppconst);
5373 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005374 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375 clear_ppconst(&ppconst);
5376 return NULL;
5377 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005378 if (cctx->ctx_skip == SKIP_YES)
5379 clear_ppconst(&ppconst);
5380 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005381 {
5382 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005383 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005384 clear_ppconst(&ppconst);
5385 }
5386 else
5387 {
5388 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005389 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005390 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005391 return NULL;
5392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393
5394 scope = new_scope(cctx, IF_SCOPE);
5395 if (scope == NULL)
5396 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005397 scope->se_skip_save = skip_save;
5398 // "is_had_return" will be reset if any block does not end in :return
5399 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005401 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005402 {
5403 // "where" is set when ":elseif", "else" or ":endif" is found
5404 scope->se_u.se_if.is_if_label = instr->ga_len;
5405 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5406 }
5407 else
5408 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409
5410 return p;
5411}
5412
5413 static char_u *
5414compile_elseif(char_u *arg, cctx_T *cctx)
5415{
5416 char_u *p = arg;
5417 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005418 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419 isn_T *isn;
5420 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005421 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
5423 if (scope == NULL || scope->se_type != IF_SCOPE)
5424 {
5425 emsg(_(e_elseif_without_if));
5426 return NULL;
5427 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005428 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005429 if (!cctx->ctx_had_return)
5430 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005432 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005433 {
5434 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005436 return NULL;
5437 // previous "if" or "elseif" jumps here
5438 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5439 isn->isn_arg.jump.jump_where = instr->ga_len;
5440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005442 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005443 CLEAR_FIELD(ppconst);
5444 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005445 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005446 clear_ppconst(&ppconst);
5447 return NULL;
5448 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005449 if (scope->se_skip_save == SKIP_YES)
5450 clear_ppconst(&ppconst);
5451 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005452 {
5453 // The expression results in a constant.
5454 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005455 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005456 clear_ppconst(&ppconst);
5457 scope->se_u.se_if.is_if_label = -1;
5458 }
5459 else
5460 {
5461 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005462 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005463 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005464 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005466 // "where" is set when ":elseif", "else" or ":endif" is found
5467 scope->se_u.se_if.is_if_label = instr->ga_len;
5468 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470
5471 return p;
5472}
5473
5474 static char_u *
5475compile_else(char_u *arg, cctx_T *cctx)
5476{
5477 char_u *p = arg;
5478 garray_T *instr = &cctx->ctx_instr;
5479 isn_T *isn;
5480 scope_T *scope = cctx->ctx_scope;
5481
5482 if (scope == NULL || scope->se_type != IF_SCOPE)
5483 {
5484 emsg(_(e_else_without_if));
5485 return NULL;
5486 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005487 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005488 if (!cctx->ctx_had_return)
5489 scope->se_u.se_if.is_had_return = FALSE;
5490 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491
Bram Moolenaarefd88552020-06-18 20:50:10 +02005492 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005493 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005494 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005495 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005496 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005497 if (!cctx->ctx_had_return
5498 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5499 JUMP_ALWAYS, cctx) == FAIL)
5500 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005501 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005502
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005503 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005504 {
5505 if (scope->se_u.se_if.is_if_label >= 0)
5506 {
5507 // previous "if" or "elseif" jumps here
5508 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5509 isn->isn_arg.jump.jump_where = instr->ga_len;
5510 scope->se_u.se_if.is_if_label = -1;
5511 }
5512 }
5513
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005514 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005515 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517
5518 return p;
5519}
5520
5521 static char_u *
5522compile_endif(char_u *arg, cctx_T *cctx)
5523{
5524 scope_T *scope = cctx->ctx_scope;
5525 ifscope_T *ifscope;
5526 garray_T *instr = &cctx->ctx_instr;
5527 isn_T *isn;
5528
5529 if (scope == NULL || scope->se_type != IF_SCOPE)
5530 {
5531 emsg(_(e_endif_without_if));
5532 return NULL;
5533 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005534 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005535 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005536 if (!cctx->ctx_had_return)
5537 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005539 if (scope->se_u.se_if.is_if_label >= 0)
5540 {
5541 // previous "if" or "elseif" jumps here
5542 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5543 isn->isn_arg.jump.jump_where = instr->ga_len;
5544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 // Fill in the "end" label in jumps at the end of the blocks.
5546 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005547 cctx->ctx_skip = scope->se_skip_save;
5548
5549 // If all the blocks end in :return and there is an :else then the
5550 // had_return flag is set.
5551 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005553 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554 return arg;
5555}
5556
5557/*
5558 * compile "for var in expr"
5559 *
5560 * Produces instructions:
5561 * PUSHNR -1
5562 * STORE loop-idx Set index to -1
5563 * EVAL expr Push result of "expr"
5564 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5565 * - if beyond end, jump to "end"
5566 * - otherwise get item from list and push it
5567 * STORE var Store item in "var"
5568 * ... body ...
5569 * JUMP top Jump back to repeat
5570 * end: DROP Drop the result of "expr"
5571 *
5572 */
5573 static char_u *
5574compile_for(char_u *arg, cctx_T *cctx)
5575{
5576 char_u *p;
5577 size_t varlen;
5578 garray_T *instr = &cctx->ctx_instr;
5579 garray_T *stack = &cctx->ctx_type_stack;
5580 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005581 lvar_T *loop_lvar; // loop iteration variable
5582 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583 type_T *vartype;
5584
5585 // TODO: list of variables: "for [key, value] in dict"
5586 // parse "var"
5587 for (p = arg; eval_isnamec1(*p); ++p)
5588 ;
5589 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005590 var_lvar = lookup_local(arg, varlen, cctx);
5591 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 {
5593 semsg(_("E1023: variable already defined: %s"), arg);
5594 return NULL;
5595 }
5596
5597 // consume "in"
5598 p = skipwhite(p);
5599 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5600 {
5601 emsg(_(e_missing_in));
5602 return NULL;
5603 }
5604 p = skipwhite(p + 2);
5605
5606
5607 scope = new_scope(cctx, FOR_SCOPE);
5608 if (scope == NULL)
5609 return NULL;
5610
5611 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005612 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5613 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005614 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005615 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005616 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619
5620 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005621 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5622 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005623 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005624 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005625 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005626 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005629 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
5631 // compile "expr", it remains on the stack until "endfor"
5632 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005633 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005634 {
5635 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005639 // Now that we know the type of "var", check that it is a list, now or at
5640 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005642 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005644 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 return NULL;
5646 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005647 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005648 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649
5650 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005651 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005653 generate_FOR(cctx, loop_lvar->lv_idx);
5654 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655
5656 return arg;
5657}
5658
5659/*
5660 * compile "endfor"
5661 */
5662 static char_u *
5663compile_endfor(char_u *arg, cctx_T *cctx)
5664{
5665 garray_T *instr = &cctx->ctx_instr;
5666 scope_T *scope = cctx->ctx_scope;
5667 forscope_T *forscope;
5668 isn_T *isn;
5669
5670 if (scope == NULL || scope->se_type != FOR_SCOPE)
5671 {
5672 emsg(_(e_for));
5673 return NULL;
5674 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005675 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005677 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678
5679 // At end of ":for" scope jump back to the FOR instruction.
5680 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5681
5682 // Fill in the "end" label in the FOR statement so it can jump here
5683 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5684 isn->isn_arg.forloop.for_end = instr->ga_len;
5685
5686 // Fill in the "end" label any BREAK statements
5687 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5688
5689 // Below the ":for" scope drop the "expr" list from the stack.
5690 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5691 return NULL;
5692
5693 vim_free(scope);
5694
5695 return arg;
5696}
5697
5698/*
5699 * compile "while expr"
5700 *
5701 * Produces instructions:
5702 * top: EVAL expr Push result of "expr"
5703 * JUMP_IF_FALSE end jump if false
5704 * ... body ...
5705 * JUMP top Jump back to repeat
5706 * end:
5707 *
5708 */
5709 static char_u *
5710compile_while(char_u *arg, cctx_T *cctx)
5711{
5712 char_u *p = arg;
5713 garray_T *instr = &cctx->ctx_instr;
5714 scope_T *scope;
5715
5716 scope = new_scope(cctx, WHILE_SCOPE);
5717 if (scope == NULL)
5718 return NULL;
5719
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005720 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721
5722 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005723 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724 return NULL;
5725
5726 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005727 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 JUMP_IF_FALSE, cctx) == FAIL)
5729 return FAIL;
5730
5731 return p;
5732}
5733
5734/*
5735 * compile "endwhile"
5736 */
5737 static char_u *
5738compile_endwhile(char_u *arg, cctx_T *cctx)
5739{
5740 scope_T *scope = cctx->ctx_scope;
5741
5742 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5743 {
5744 emsg(_(e_while));
5745 return NULL;
5746 }
5747 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005748 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749
5750 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005751 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
5753 // Fill in the "end" label in the WHILE statement so it can jump here.
5754 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005755 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756
5757 vim_free(scope);
5758
5759 return arg;
5760}
5761
5762/*
5763 * compile "continue"
5764 */
5765 static char_u *
5766compile_continue(char_u *arg, cctx_T *cctx)
5767{
5768 scope_T *scope = cctx->ctx_scope;
5769
5770 for (;;)
5771 {
5772 if (scope == NULL)
5773 {
5774 emsg(_(e_continue));
5775 return NULL;
5776 }
5777 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5778 break;
5779 scope = scope->se_outer;
5780 }
5781
5782 // Jump back to the FOR or WHILE instruction.
5783 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005784 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5785 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786 return arg;
5787}
5788
5789/*
5790 * compile "break"
5791 */
5792 static char_u *
5793compile_break(char_u *arg, cctx_T *cctx)
5794{
5795 scope_T *scope = cctx->ctx_scope;
5796 endlabel_T **el;
5797
5798 for (;;)
5799 {
5800 if (scope == NULL)
5801 {
5802 emsg(_(e_break));
5803 return NULL;
5804 }
5805 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5806 break;
5807 scope = scope->se_outer;
5808 }
5809
5810 // Jump to the end of the FOR or WHILE loop.
5811 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005812 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005814 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5816 return FAIL;
5817
5818 return arg;
5819}
5820
5821/*
5822 * compile "{" start of block
5823 */
5824 static char_u *
5825compile_block(char_u *arg, cctx_T *cctx)
5826{
5827 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5828 return NULL;
5829 return skipwhite(arg + 1);
5830}
5831
5832/*
5833 * compile end of block: drop one scope
5834 */
5835 static void
5836compile_endblock(cctx_T *cctx)
5837{
5838 scope_T *scope = cctx->ctx_scope;
5839
5840 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005841 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 vim_free(scope);
5843}
5844
5845/*
5846 * compile "try"
5847 * Creates a new scope for the try-endtry, pointing to the first catch and
5848 * finally.
5849 * Creates another scope for the "try" block itself.
5850 * TRY instruction sets up exception handling at runtime.
5851 *
5852 * "try"
5853 * TRY -> catch1, -> finally push trystack entry
5854 * ... try block
5855 * "throw {exception}"
5856 * EVAL {exception}
5857 * THROW create exception
5858 * ... try block
5859 * " catch {expr}"
5860 * JUMP -> finally
5861 * catch1: PUSH exeception
5862 * EVAL {expr}
5863 * MATCH
5864 * JUMP nomatch -> catch2
5865 * CATCH remove exception
5866 * ... catch block
5867 * " catch"
5868 * JUMP -> finally
5869 * catch2: CATCH remove exception
5870 * ... catch block
5871 * " finally"
5872 * finally:
5873 * ... finally block
5874 * " endtry"
5875 * ENDTRY pop trystack entry, may rethrow
5876 */
5877 static char_u *
5878compile_try(char_u *arg, cctx_T *cctx)
5879{
5880 garray_T *instr = &cctx->ctx_instr;
5881 scope_T *try_scope;
5882 scope_T *scope;
5883
5884 // scope that holds the jumps that go to catch/finally/endtry
5885 try_scope = new_scope(cctx, TRY_SCOPE);
5886 if (try_scope == NULL)
5887 return NULL;
5888
5889 // "catch" is set when the first ":catch" is found.
5890 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005891 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 if (generate_instr(cctx, ISN_TRY) == NULL)
5893 return NULL;
5894
5895 // scope for the try block itself
5896 scope = new_scope(cctx, BLOCK_SCOPE);
5897 if (scope == NULL)
5898 return NULL;
5899
5900 return arg;
5901}
5902
5903/*
5904 * compile "catch {expr}"
5905 */
5906 static char_u *
5907compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5908{
5909 scope_T *scope = cctx->ctx_scope;
5910 garray_T *instr = &cctx->ctx_instr;
5911 char_u *p;
5912 isn_T *isn;
5913
5914 // end block scope from :try or :catch
5915 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5916 compile_endblock(cctx);
5917 scope = cctx->ctx_scope;
5918
5919 // Error if not in a :try scope
5920 if (scope == NULL || scope->se_type != TRY_SCOPE)
5921 {
5922 emsg(_(e_catch));
5923 return NULL;
5924 }
5925
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005926 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 {
5928 emsg(_("E1033: catch unreachable after catch-all"));
5929 return NULL;
5930 }
5931
5932 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005933 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005934 JUMP_ALWAYS, cctx) == FAIL)
5935 return NULL;
5936
5937 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005938 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939 if (isn->isn_arg.try.try_catch == 0)
5940 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005941 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 {
5943 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005944 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005945 isn->isn_arg.jump.jump_where = instr->ga_len;
5946 }
5947
5948 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005949 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005951 scope->se_u.se_try.ts_caught_all = TRUE;
5952 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 }
5954 else
5955 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005956 char_u *end;
5957 char_u *pat;
5958 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005959 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005960 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 // Push v:exception, push {expr} and MATCH
5963 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5964
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005965 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005966 if (*end != *p)
5967 {
5968 semsg(_("E1067: Separator mismatch: %s"), p);
5969 vim_free(tofree);
5970 return FAIL;
5971 }
5972 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005973 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005974 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005975 len = (int)(end - tofree);
5976 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005977 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005978 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005979 if (pat == NULL)
5980 return FAIL;
5981 if (generate_PUSHS(cctx, pat) == FAIL)
5982 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5985 return NULL;
5986
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005987 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5989 return NULL;
5990 }
5991
5992 if (generate_instr(cctx, ISN_CATCH) == NULL)
5993 return NULL;
5994
5995 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5996 return NULL;
5997 return p;
5998}
5999
6000 static char_u *
6001compile_finally(char_u *arg, cctx_T *cctx)
6002{
6003 scope_T *scope = cctx->ctx_scope;
6004 garray_T *instr = &cctx->ctx_instr;
6005 isn_T *isn;
6006
6007 // end block scope from :try or :catch
6008 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6009 compile_endblock(cctx);
6010 scope = cctx->ctx_scope;
6011
6012 // Error if not in a :try scope
6013 if (scope == NULL || scope->se_type != TRY_SCOPE)
6014 {
6015 emsg(_(e_finally));
6016 return NULL;
6017 }
6018
6019 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006020 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 if (isn->isn_arg.try.try_finally != 0)
6022 {
6023 emsg(_(e_finally_dup));
6024 return NULL;
6025 }
6026
6027 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006028 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
Bram Moolenaar585fea72020-04-02 22:33:21 +02006030 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006031 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 {
6033 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006034 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006036 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 }
6038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 // TODO: set index in ts_finally_label jumps
6040
6041 return arg;
6042}
6043
6044 static char_u *
6045compile_endtry(char_u *arg, cctx_T *cctx)
6046{
6047 scope_T *scope = cctx->ctx_scope;
6048 garray_T *instr = &cctx->ctx_instr;
6049 isn_T *isn;
6050
6051 // end block scope from :catch or :finally
6052 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6053 compile_endblock(cctx);
6054 scope = cctx->ctx_scope;
6055
6056 // Error if not in a :try scope
6057 if (scope == NULL || scope->se_type != TRY_SCOPE)
6058 {
6059 if (scope == NULL)
6060 emsg(_(e_no_endtry));
6061 else if (scope->se_type == WHILE_SCOPE)
6062 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006063 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 emsg(_(e_endfor));
6065 else
6066 emsg(_(e_endif));
6067 return NULL;
6068 }
6069
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006070 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6072 {
6073 emsg(_("E1032: missing :catch or :finally"));
6074 return NULL;
6075 }
6076
6077 // Fill in the "end" label in jumps at the end of the blocks, if not done
6078 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006079 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006082 if (isn->isn_arg.try.try_catch == 0)
6083 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 if (isn->isn_arg.try.try_finally == 0)
6085 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006086
6087 if (scope->se_u.se_try.ts_catch_label != 0)
6088 {
6089 // Last catch without match jumps here
6090 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6091 isn->isn_arg.jump.jump_where = instr->ga_len;
6092 }
6093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094 compile_endblock(cctx);
6095
6096 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6097 return NULL;
6098 return arg;
6099}
6100
6101/*
6102 * compile "throw {expr}"
6103 */
6104 static char_u *
6105compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6106{
6107 char_u *p = skipwhite(arg);
6108
Bram Moolenaara5565e42020-05-09 15:44:01 +02006109 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 return NULL;
6111 if (may_generate_2STRING(-1, cctx) == FAIL)
6112 return NULL;
6113 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6114 return NULL;
6115
6116 return p;
6117}
6118
6119/*
6120 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006121 * compile "echomsg expr"
6122 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006123 * compile "execute expr"
6124 */
6125 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006126compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006127{
6128 char_u *p = arg;
6129 int count = 0;
6130
6131 for (;;)
6132 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006133 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006134 return NULL;
6135 ++count;
6136 p = skipwhite(p);
6137 if (ends_excmd(*p))
6138 break;
6139 }
6140
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006141 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6142 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6143 else if (cmdidx == CMD_execute)
6144 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6145 else if (cmdidx == CMD_echomsg)
6146 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6147 else
6148 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 return p;
6150}
6151
6152/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006153 * A command that is not compiled, execute with legacy code.
6154 */
6155 static char_u *
6156compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6157{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006158 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006159 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006160 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006161
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006162 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006163 goto theend;
6164
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006165 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006166 {
6167 long argt = excmd_get_argt(eap->cmdidx);
6168 int usefilter = FALSE;
6169
6170 has_expr = argt & (EX_XFILE | EX_EXPAND);
6171
6172 // If the command can be followed by a bar, find the bar and truncate
6173 // it, so that the following command can be compiled.
6174 // The '|' is overwritten with a NUL, it is put back below.
6175 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6176 && *eap->arg == '!')
6177 // :w !filter or :r !filter or :r! filter
6178 usefilter = TRUE;
6179 if ((argt & EX_TRLBAR) && !usefilter)
6180 {
6181 separate_nextcmd(eap);
6182 if (eap->nextcmd != NULL)
6183 nextcmd = eap->nextcmd;
6184 }
6185 }
6186
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006187 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6188 {
6189 // expand filename in "syntax include [@group] filename"
6190 has_expr = TRUE;
6191 eap->arg = skipwhite(eap->arg + 7);
6192 if (*eap->arg == '@')
6193 eap->arg = skiptowhite(eap->arg);
6194 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006195
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006196 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006197 {
6198 int count = 0;
6199 char_u *start = skipwhite(line);
6200
6201 // :cmd xxx`=expr1`yyy`=expr2`zzz
6202 // PUSHS ":cmd xxx"
6203 // eval expr1
6204 // PUSHS "yyy"
6205 // eval expr2
6206 // PUSHS "zzz"
6207 // EXECCONCAT 5
6208 for (;;)
6209 {
6210 if (p > start)
6211 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006212 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006213 ++count;
6214 }
6215 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006216 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006217 return NULL;
6218 may_generate_2STRING(-1, cctx);
6219 ++count;
6220 p = skipwhite(p);
6221 if (*p != '`')
6222 {
6223 emsg(_("E1083: missing backtick"));
6224 return NULL;
6225 }
6226 start = p + 1;
6227
6228 p = (char_u *)strstr((char *)start, "`=");
6229 if (p == NULL)
6230 {
6231 if (*skipwhite(start) != NUL)
6232 {
6233 generate_PUSHS(cctx, vim_strsave(start));
6234 ++count;
6235 }
6236 break;
6237 }
6238 }
6239 generate_EXECCONCAT(cctx, count);
6240 }
6241 else
6242 generate_EXEC(cctx, line);
6243
6244theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006245 if (*nextcmd != NUL)
6246 {
6247 // the parser expects a pointer to the bar, put it back
6248 --nextcmd;
6249 *nextcmd = '|';
6250 }
6251
6252 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006253}
6254
6255/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006256 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006257 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006258 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006259 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006260add_def_function(ufunc_T *ufunc)
6261{
6262 dfunc_T *dfunc;
6263
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006264 if (def_functions.ga_len == 0)
6265 {
6266 // The first position is not used, so that a zero uf_dfunc_idx means it
6267 // wasn't set.
6268 if (ga_grow(&def_functions, 1) == FAIL)
6269 return FAIL;
6270 ++def_functions.ga_len;
6271 }
6272
Bram Moolenaar09689a02020-05-09 22:50:08 +02006273 // Add the function to "def_functions".
6274 if (ga_grow(&def_functions, 1) == FAIL)
6275 return FAIL;
6276 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6277 CLEAR_POINTER(dfunc);
6278 dfunc->df_idx = def_functions.ga_len;
6279 ufunc->uf_dfunc_idx = dfunc->df_idx;
6280 dfunc->df_ufunc = ufunc;
6281 ++def_functions.ga_len;
6282 return OK;
6283}
6284
6285/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 * After ex_function() has collected all the function lines: parse and compile
6287 * the lines into instructions.
6288 * Adds the function to "def_functions".
6289 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6290 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006291 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006292 * This can be used recursively through compile_lambda(), which may reallocate
6293 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006294 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006296 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006297compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 char_u *line = NULL;
6300 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 cctx_T cctx;
6303 garray_T *instr;
6304 int called_emsg_before = called_emsg;
6305 int ret = FAIL;
6306 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006307 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006308 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006309 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006311 // When using a function that was compiled before: Free old instructions.
6312 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006313 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006315 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6316 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006317 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006319 else
6320 {
6321 if (add_def_function(ufunc) == FAIL)
6322 return FAIL;
6323 new_def_function = TRUE;
6324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325
Bram Moolenaar985116a2020-07-12 17:31:09 +02006326 ufunc->uf_def_status = UF_COMPILING;
6327
Bram Moolenaara80faa82020-04-12 19:37:17 +02006328 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 cctx.ctx_ufunc = ufunc;
6330 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006331 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6333 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6334 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6335 cctx.ctx_type_list = &ufunc->uf_type_list;
6336 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6337 instr = &cctx.ctx_instr;
6338
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006339 // Set the context to the function, it may be compiled when called from
6340 // another script. Set the script version to the most modern one.
6341 // The line number will be set in next_line_from_context().
6342 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6344
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006345 // Make sure error messages are OK.
6346 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6347 if (do_estack_push)
6348 estack_push_ufunc(ufunc, 1);
6349
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006350 if (ufunc->uf_def_args.ga_len > 0)
6351 {
6352 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006353 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006354 int i;
6355 char_u *arg;
6356 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006357 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006358
6359 // Produce instructions for the default values of optional arguments.
6360 // Store the instruction index in uf_def_arg_idx[] so that we know
6361 // where to start when the function is called, depending on the number
6362 // of arguments.
6363 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6364 if (ufunc->uf_def_arg_idx == NULL)
6365 goto erret;
6366 for (i = 0; i < count; ++i)
6367 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006368 garray_T *stack = &cctx.ctx_type_stack;
6369 type_T *val_type;
6370 int arg_idx = first_def_arg + i;
6371
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006372 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6373 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006374 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006375 goto erret;
6376
6377 // If no type specified use the type of the default value.
6378 // Otherwise check that the default value type matches the
6379 // specified type.
6380 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6381 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006382 {
6383 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006384 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006385 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006386 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006387 == FAIL)
6388 {
6389 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6390 arg_idx + 1);
6391 goto erret;
6392 }
6393
6394 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006395 goto erret;
6396 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006397 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006398
6399 if (did_set_arg_type)
6400 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006401 }
6402
6403 /*
6404 * Loop over all the lines of the function and generate instructions.
6405 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 for (;;)
6407 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006408 exarg_T ea;
6409 cmdmod_T save_cmdmod;
6410 int starts_with_colon = FALSE;
6411 char_u *cmd;
6412 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006413
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006414 // Bail out on the first error to avoid a flood of errors and report
6415 // the right line number when inside try/catch.
6416 if (emsg_before != called_emsg)
6417 goto erret;
6418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 if (line != NULL && *line == '|')
6420 // the line continues after a '|'
6421 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006422 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006423 && !(*line == '#' && (line == cctx.ctx_line_start
6424 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006426 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 goto erret;
6428 }
6429 else
6430 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006431 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006432 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006433 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006436 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437
Bram Moolenaara80faa82020-04-12 19:37:17 +02006438 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 ea.cmdlinep = &line;
6440 ea.cmd = skipwhite(line);
6441
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006442 // Some things can be recognized by the first character.
6443 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006445 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006446 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006447 if (ea.cmd[1] != '{')
6448 {
6449 line = (char_u *)"";
6450 continue;
6451 }
6452 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006454 case '}':
6455 {
6456 // "}" ends a block scope
6457 scopetype_T stype = cctx.ctx_scope == NULL
6458 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006460 if (stype == BLOCK_SCOPE)
6461 {
6462 compile_endblock(&cctx);
6463 line = ea.cmd;
6464 }
6465 else
6466 {
6467 emsg(_("E1025: using } outside of a block scope"));
6468 goto erret;
6469 }
6470 if (line != NULL)
6471 line = skipwhite(ea.cmd + 1);
6472 continue;
6473 }
6474
6475 case '{':
6476 // "{" starts a block scope
6477 // "{'a': 1}->func() is something else
6478 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6479 {
6480 line = compile_block(ea.cmd, &cctx);
6481 continue;
6482 }
6483 break;
6484
6485 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006486 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 }
6489
6490 /*
6491 * COMMAND MODIFIERS
6492 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006493 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6495 {
6496 if (errormsg != NULL)
6497 goto erret;
6498 // empty line or comment
6499 line = (char_u *)"";
6500 continue;
6501 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006502 // TODO: use modifiers in the command
6503 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006504 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505
6506 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006507 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006509 {
6510 if (*ea.cmd == '(')
6511 // not for "call()"
6512 ea.cmd = p;
6513 else
6514 ea.cmd = skipwhite(ea.cmd);
6515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006517 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006519 char_u *pskip;
6520
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006521 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006522 // find what follows.
6523 // Skip over "var.member", "var[idx]" and the like.
6524 // Also "&opt = val", "$ENV = val" and "@r = val".
6525 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006526 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006527 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006528 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006530 char_u *var_end;
6531 int oplen;
6532 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533
Bram Moolenaar65821722020-08-02 18:58:54 +02006534 if (ea.cmd[0] == '@')
6535 var_end = ea.cmd + 2;
6536 else
6537 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006538 FNE_CHECK_START | FNE_INCL_BR);
6539 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006540 if (oplen > 0)
6541 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006542 size_t len = p - ea.cmd;
6543
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006544 // Recognize an assignment if we recognize the variable
6545 // name:
6546 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006547 // "local = expr" where "local" is a local var.
6548 // "script = expr" where "script" is a script-local var.
6549 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 // "&opt = expr"
6551 // "$ENV = expr"
6552 // "@r = expr"
6553 if (*ea.cmd == '&'
6554 || *ea.cmd == '$'
6555 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006556 || ((len) > 2 && ea.cmd[1] == ':')
6557 || lookup_local(ea.cmd, len, &cctx) != NULL
6558 || lookup_arg(ea.cmd, len, NULL, NULL,
6559 NULL, &cctx) == OK
6560 || lookup_script(ea.cmd, len) == OK
6561 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006562 {
6563 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006564 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006565 goto erret;
6566 continue;
6567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 }
6569 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006570
6571 if (*ea.cmd == '[')
6572 {
6573 // [var, var] = expr
6574 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6575 if (line == NULL)
6576 goto erret;
6577 if (line != ea.cmd)
6578 continue;
6579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 }
6581
6582 /*
6583 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006584 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006586 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006587 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006588 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006589 ea.cmd = skip_range(ea.cmd, NULL);
6590 if (ea.cmd > cmd && !starts_with_colon)
6591 {
6592 emsg(_(e_colon_required));
6593 goto erret;
6594 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006595 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006596 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006597 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006598 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599
6600 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6601 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006602 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006603 {
6604 line += STRLEN(line);
6605 continue;
6606 }
6607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006609 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006611 // CMD_let cannot happen, compile_assignment() above is used
6612 iemsg("Command from find_ex_command() not handled");
6613 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 }
6616
6617 p = skipwhite(p);
6618
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006619 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006620 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006621 && ea.cmdidx != CMD_elseif
6622 && ea.cmdidx != CMD_else
6623 && ea.cmdidx != CMD_endif)
6624 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006625 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006626 continue;
6627 }
6628
Bram Moolenaarefd88552020-06-18 20:50:10 +02006629 if (ea.cmdidx != CMD_elseif
6630 && ea.cmdidx != CMD_else
6631 && ea.cmdidx != CMD_endif
6632 && ea.cmdidx != CMD_endfor
6633 && ea.cmdidx != CMD_endwhile
6634 && ea.cmdidx != CMD_catch
6635 && ea.cmdidx != CMD_finally
6636 && ea.cmdidx != CMD_endtry)
6637 {
6638 if (cctx.ctx_had_return)
6639 {
6640 emsg(_("E1095: Unreachable code after :return"));
6641 goto erret;
6642 }
6643 }
6644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 switch (ea.cmdidx)
6646 {
6647 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006648 ea.arg = p;
6649 line = compile_nested_function(&ea, &cctx);
6650 break;
6651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006653 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 goto erret;
6655
6656 case CMD_return:
6657 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006658 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 break;
6660
6661 case CMD_let:
6662 case CMD_const:
6663 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006664 if (line == p)
6665 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 break;
6667
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006668 case CMD_unlet:
6669 case CMD_unlockvar:
6670 case CMD_lockvar:
6671 line = compile_unletlock(p, &ea, &cctx);
6672 break;
6673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 case CMD_import:
6675 line = compile_import(p, &cctx);
6676 break;
6677
6678 case CMD_if:
6679 line = compile_if(p, &cctx);
6680 break;
6681 case CMD_elseif:
6682 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006683 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684 break;
6685 case CMD_else:
6686 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006687 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 break;
6689 case CMD_endif:
6690 line = compile_endif(p, &cctx);
6691 break;
6692
6693 case CMD_while:
6694 line = compile_while(p, &cctx);
6695 break;
6696 case CMD_endwhile:
6697 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006698 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 break;
6700
6701 case CMD_for:
6702 line = compile_for(p, &cctx);
6703 break;
6704 case CMD_endfor:
6705 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006706 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 break;
6708 case CMD_continue:
6709 line = compile_continue(p, &cctx);
6710 break;
6711 case CMD_break:
6712 line = compile_break(p, &cctx);
6713 break;
6714
6715 case CMD_try:
6716 line = compile_try(p, &cctx);
6717 break;
6718 case CMD_catch:
6719 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006720 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 break;
6722 case CMD_finally:
6723 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006724 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 break;
6726 case CMD_endtry:
6727 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006728 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 break;
6730 case CMD_throw:
6731 line = compile_throw(p, &cctx);
6732 break;
6733
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006734 case CMD_eval:
6735 if (compile_expr0(&p, &cctx) == FAIL)
6736 goto erret;
6737
6738 // drop the return value
6739 generate_instr_drop(&cctx, ISN_DROP, 1);
6740
6741 line = skipwhite(p);
6742 break;
6743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006746 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006747 case CMD_echomsg:
6748 case CMD_echoerr:
6749 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006750 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006752 // TODO: other commands with an expression argument
6753
Bram Moolenaarae616492020-07-28 20:07:27 +02006754 case CMD_append:
6755 case CMD_change:
6756 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006757 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006758 case CMD_xit:
6759 not_in_vim9(&ea);
6760 goto erret;
6761
Bram Moolenaar002262f2020-07-08 17:47:57 +02006762 case CMD_SIZE:
6763 semsg(_("E476: Invalid command: %s"), ea.cmd);
6764 goto erret;
6765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006767 // Not recognized, execute with do_cmdline_cmd().
6768 ea.arg = p;
6769 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 break;
6771 }
6772 if (line == NULL)
6773 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006774 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775
6776 if (cctx.ctx_type_stack.ga_len < 0)
6777 {
6778 iemsg("Type stack underflow");
6779 goto erret;
6780 }
6781 }
6782
6783 if (cctx.ctx_scope != NULL)
6784 {
6785 if (cctx.ctx_scope->se_type == IF_SCOPE)
6786 emsg(_(e_endif));
6787 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6788 emsg(_(e_endwhile));
6789 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6790 emsg(_(e_endfor));
6791 else
6792 emsg(_("E1026: Missing }"));
6793 goto erret;
6794 }
6795
Bram Moolenaarefd88552020-06-18 20:50:10 +02006796 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 {
6798 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6799 {
6800 emsg(_("E1027: Missing return statement"));
6801 goto erret;
6802 }
6803
6804 // Return zero if there is no return at the end.
6805 generate_PUSHNR(&cctx, 0);
6806 generate_instr(&cctx, ISN_RETURN);
6807 }
6808
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006809 {
6810 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6811 + ufunc->uf_dfunc_idx;
6812 dfunc->df_deleted = FALSE;
6813 dfunc->df_instr = instr->ga_data;
6814 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006815 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006816 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006817 if (cctx.ctx_outer_used)
6818 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006819 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821
6822 ret = OK;
6823
6824erret:
6825 if (ret == FAIL)
6826 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006827 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006828 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6829 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006830
6831 for (idx = 0; idx < instr->ga_len; ++idx)
6832 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006834
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006835 // If using the last entry in the table and it was added above, we
6836 // might as well remove it.
6837 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006838 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006839 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006840 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006841 ufunc->uf_dfunc_idx = 0;
6842 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006843 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006844
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006845 while (cctx.ctx_scope != NULL)
6846 drop_scope(&cctx);
6847
Bram Moolenaar20431c92020-03-20 18:39:46 +01006848 // Don't execute this function body.
6849 ga_clear_strings(&ufunc->uf_lines);
6850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851 if (errormsg != NULL)
6852 emsg(errormsg);
6853 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006854 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 }
6856
6857 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006858 if (do_estack_push)
6859 estack_pop();
6860
Bram Moolenaar20431c92020-03-20 18:39:46 +01006861 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006862 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006864 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865}
6866
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006867 void
6868set_function_type(ufunc_T *ufunc)
6869{
6870 int varargs = ufunc->uf_va_name != NULL;
6871 int argcount = ufunc->uf_args.ga_len;
6872
6873 // Create a type for the function, with the return type and any
6874 // argument types.
6875 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6876 // The type is included in "tt_args".
6877 if (argcount > 0 || varargs)
6878 {
6879 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6880 argcount, &ufunc->uf_type_list);
6881 // Add argument types to the function type.
6882 if (func_type_add_arg_types(ufunc->uf_func_type,
6883 argcount + varargs,
6884 &ufunc->uf_type_list) == FAIL)
6885 return;
6886 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6887 ufunc->uf_func_type->tt_min_argcount =
6888 argcount - ufunc->uf_def_args.ga_len;
6889 if (ufunc->uf_arg_types == NULL)
6890 {
6891 int i;
6892
6893 // lambda does not have argument types.
6894 for (i = 0; i < argcount; ++i)
6895 ufunc->uf_func_type->tt_args[i] = &t_any;
6896 }
6897 else
6898 mch_memmove(ufunc->uf_func_type->tt_args,
6899 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6900 if (varargs)
6901 {
6902 ufunc->uf_func_type->tt_args[argcount] =
6903 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6904 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6905 }
6906 }
6907 else
6908 // No arguments, can use a predefined type.
6909 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6910 argcount, &ufunc->uf_type_list);
6911}
6912
6913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914/*
6915 * Delete an instruction, free what it contains.
6916 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006917 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918delete_instr(isn_T *isn)
6919{
6920 switch (isn->isn_type)
6921 {
6922 case ISN_EXEC:
6923 case ISN_LOADENV:
6924 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006925 case ISN_LOADB:
6926 case ISN_LOADW:
6927 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006929 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 case ISN_PUSHEXC:
6931 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006932 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006934 case ISN_STOREB:
6935 case ISN_STOREW:
6936 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006937 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 vim_free(isn->isn_arg.string);
6939 break;
6940
6941 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006942 case ISN_STORES:
6943 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 break;
6945
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006946 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006947 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006948 vim_free(isn->isn_arg.unlet.ul_name);
6949 break;
6950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 case ISN_STOREOPT:
6952 vim_free(isn->isn_arg.storeopt.so_name);
6953 break;
6954
6955 case ISN_PUSHBLOB: // push blob isn_arg.blob
6956 blob_unref(isn->isn_arg.blob);
6957 break;
6958
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006959 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006961 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006962#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006963 break;
6964
6965 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006966#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006967 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006968#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006969 break;
6970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 case ISN_UCALL:
6972 vim_free(isn->isn_arg.ufunc.cuf_name);
6973 break;
6974
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006975 case ISN_FUNCREF:
6976 {
6977 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6978 + isn->isn_arg.funcref.fr_func;
6979 func_ptr_unref(dfunc->df_ufunc);
6980 }
6981 break;
6982
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006983 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02006984 {
6985 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
6986 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
6987
6988 if (ufunc != NULL)
6989 {
6990 // Clear uf_dfunc_idx so that the function is deleted.
6991 clear_def_function(ufunc);
6992 ufunc->uf_dfunc_idx = 0;
6993 func_ptr_unref(ufunc);
6994 }
6995
6996 vim_free(lambda);
6997 vim_free(isn->isn_arg.newfunc.nf_global);
6998 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006999 break;
7000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 case ISN_2BOOL:
7002 case ISN_2STRING:
7003 case ISN_ADDBLOB:
7004 case ISN_ADDLIST:
7005 case ISN_BCALL:
7006 case ISN_CATCH:
7007 case ISN_CHECKNR:
7008 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007009 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 case ISN_COMPAREANY:
7011 case ISN_COMPAREBLOB:
7012 case ISN_COMPAREBOOL:
7013 case ISN_COMPAREDICT:
7014 case ISN_COMPAREFLOAT:
7015 case ISN_COMPAREFUNC:
7016 case ISN_COMPARELIST:
7017 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 case ISN_COMPARESPECIAL:
7019 case ISN_COMPARESTRING:
7020 case ISN_CONCAT:
7021 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007022 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 case ISN_DROP:
7024 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007025 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007026 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007028 case ISN_EXECCONCAT:
7029 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007031 case ISN_LISTINDEX:
7032 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007033 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007034 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007035 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 case ISN_JUMP:
7037 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007038 case ISN_LOADBDICT:
7039 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007040 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007042 case ISN_LOADSCRIPT:
7043 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007045 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 case ISN_NEGATENR:
7047 case ISN_NEWDICT:
7048 case ISN_NEWLIST:
7049 case ISN_OPNR:
7050 case ISN_OPFLOAT:
7051 case ISN_OPANY:
7052 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007053 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 case ISN_PUSHF:
7055 case ISN_PUSHNR:
7056 case ISN_PUSHBOOL:
7057 case ISN_PUSHSPEC:
7058 case ISN_RETURN:
7059 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007060 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007061 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007063 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007065 case ISN_STOREDICT:
7066 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 case ISN_THROW:
7068 case ISN_TRY:
7069 // nothing allocated
7070 break;
7071 }
7072}
7073
7074/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007075 * Free all instructions for "dfunc".
7076 */
7077 static void
7078delete_def_function_contents(dfunc_T *dfunc)
7079{
7080 int idx;
7081
7082 ga_clear(&dfunc->df_def_args_isn);
7083
7084 if (dfunc->df_instr != NULL)
7085 {
7086 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7087 delete_instr(dfunc->df_instr + idx);
7088 VIM_CLEAR(dfunc->df_instr);
7089 }
7090
7091 dfunc->df_deleted = TRUE;
7092}
7093
7094/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007095 * When a user function is deleted, clear the contents of any associated def
7096 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 */
7098 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007099clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007101 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 {
7103 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7104 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105
Bram Moolenaar20431c92020-03-20 18:39:46 +01007106 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007107 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 }
7109}
7110
7111#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007112/*
7113 * Free all functions defined with ":def".
7114 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 void
7116free_def_functions(void)
7117{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007118 int idx;
7119
7120 for (idx = 0; idx < def_functions.ga_len; ++idx)
7121 {
7122 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7123
7124 delete_def_function_contents(dfunc);
7125 }
7126
7127 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128}
7129#endif
7130
7131
7132#endif // FEAT_EVAL