blob: 87bb0e2c70619ca79db9b5f9738877c4676d1c02 [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 Moolenaarba60cc42020-08-12 19:15:33 +02001732 // ignore NULLs inserted for continuation lines
1733 if (line != NULL)
1734 {
1735 p = skipwhite(line);
1736 if (*p != NUL && !vim9_comment_start(p))
1737 return p;
1738 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001739 }
1740 return NULL;
1741}
1742
1743/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001744 * Called when checking for a following operator at "arg". When the rest of
1745 * the line is empty or only a comment, peek the next line. If there is a next
1746 * line return a pointer to it and set "nextp".
1747 * Otherwise skip over white space.
1748 */
1749 static char_u *
1750may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1751{
1752 char_u *p = skipwhite(arg);
1753
1754 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001755 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001756 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001757 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001758 if (*nextp != NULL)
1759 return *nextp;
1760 }
1761 return p;
1762}
1763
1764/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001765 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001766 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001767 * Returns NULL when at the end.
1768 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001769 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001770next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001771{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001772 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001773
1774 do
1775 {
1776 ++cctx->ctx_lnum;
1777 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001778 {
1779 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001780 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001781 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001782 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001783 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001784 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001785 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001786 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001787 return line;
1788}
1789
1790/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001791 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001792 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001793 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1794 */
1795 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001796may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001797{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001798 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001799 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001800 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001801
1802 if (next == NULL)
1803 return FAIL;
1804 *arg = skipwhite(next);
1805 }
1806 return OK;
1807}
1808
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001809/*
1810 * Idem, and give an error when failed.
1811 */
1812 static int
1813may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1814{
1815 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1816 {
1817 emsg(_("E1097: line incomplete"));
1818 return FAIL;
1819 }
1820 return OK;
1821}
1822
1823
Bram Moolenaara5565e42020-05-09 15:44:01 +02001824// Structure passed between the compile_expr* functions to keep track of
1825// constants that have been parsed but for which no code was produced yet. If
1826// possible expressions on these constants are applied at compile time. If
1827// that is not possible, the code to push the constants needs to be generated
1828// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001829// Using 50 should be more than enough of 5 levels of ().
1830#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001831typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001832 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001833 int pp_used; // active entries in pp_tv[]
1834} ppconst_T;
1835
Bram Moolenaar1c747212020-05-09 18:28:34 +02001836static int compile_expr0(char_u **arg, cctx_T *cctx);
1837static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1838
Bram Moolenaara5565e42020-05-09 15:44:01 +02001839/*
1840 * Generate a PUSH instruction for "tv".
1841 * "tv" will be consumed or cleared.
1842 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1843 */
1844 static int
1845generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1846{
1847 if (tv != NULL)
1848 {
1849 switch (tv->v_type)
1850 {
1851 case VAR_UNKNOWN:
1852 break;
1853 case VAR_BOOL:
1854 generate_PUSHBOOL(cctx, tv->vval.v_number);
1855 break;
1856 case VAR_SPECIAL:
1857 generate_PUSHSPEC(cctx, tv->vval.v_number);
1858 break;
1859 case VAR_NUMBER:
1860 generate_PUSHNR(cctx, tv->vval.v_number);
1861 break;
1862#ifdef FEAT_FLOAT
1863 case VAR_FLOAT:
1864 generate_PUSHF(cctx, tv->vval.v_float);
1865 break;
1866#endif
1867 case VAR_BLOB:
1868 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1869 tv->vval.v_blob = NULL;
1870 break;
1871 case VAR_STRING:
1872 generate_PUSHS(cctx, tv->vval.v_string);
1873 tv->vval.v_string = NULL;
1874 break;
1875 default:
1876 iemsg("constant type not supported");
1877 clear_tv(tv);
1878 return FAIL;
1879 }
1880 tv->v_type = VAR_UNKNOWN;
1881 }
1882 return OK;
1883}
1884
1885/*
1886 * Generate code for any ppconst entries.
1887 */
1888 static int
1889generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1890{
1891 int i;
1892 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001893 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001894
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001895 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001896 for (i = 0; i < ppconst->pp_used; ++i)
1897 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1898 ret = FAIL;
1899 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001900 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001901 return ret;
1902}
1903
1904/*
1905 * Clear ppconst constants. Used when failing.
1906 */
1907 static void
1908clear_ppconst(ppconst_T *ppconst)
1909{
1910 int i;
1911
1912 for (i = 0; i < ppconst->pp_used; ++i)
1913 clear_tv(&ppconst->pp_tv[i]);
1914 ppconst->pp_used = 0;
1915}
1916
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001917/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001918 * Generate an instruction to load script-local variable "name", without the
1919 * leading "s:".
1920 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 */
1922 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001923compile_load_scriptvar(
1924 cctx_T *cctx,
1925 char_u *name, // variable NUL terminated
1926 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001927 char_u **end, // end of variable
1928 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001930 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1932 imported_T *import;
1933
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001934 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001936 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001937 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1938 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 if (idx >= 0)
1941 {
1942 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1943
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001944 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 current_sctx.sc_sid, idx, sv->sv_type);
1946 return OK;
1947 }
1948
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001949 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 if (import != NULL)
1951 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001952 if (import->imp_all)
1953 {
1954 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001955 char_u *exp_name;
1956 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001957 ufunc_T *ufunc;
1958 type_T *type;
1959
1960 // Used "import * as Name", need to lookup the member.
1961 if (*p != '.')
1962 {
1963 semsg(_("E1060: expected dot after name: %s"), start);
1964 return FAIL;
1965 }
1966 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001967 if (VIM_ISWHITE(*p))
1968 {
1969 emsg(_("E1074: no white space allowed after dot"));
1970 return FAIL;
1971 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001972
Bram Moolenaar1c991142020-07-04 13:15:31 +02001973 // isolate one name
1974 exp_name = p;
1975 while (eval_isnamec(*p))
1976 ++p;
1977 cc = *p;
1978 *p = NUL;
1979
1980 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
1981 *p = cc;
1982 p = skipwhite(p);
1983
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001984 // TODO: what if it is a function?
1985 if (idx < 0)
1986 return FAIL;
1987 *end = p;
1988
1989 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1990 import->imp_sid,
1991 idx,
1992 type);
1993 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001994 else if (import->imp_funcname != NULL)
1995 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001996 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001997 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1998 import->imp_sid,
1999 import->imp_var_vals_idx,
2000 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 return OK;
2002 }
2003
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002004 if (error)
2005 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 return FAIL;
2007}
2008
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009 static int
2010generate_funcref(cctx_T *cctx, char_u *name)
2011{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002012 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002013
2014 if (ufunc == NULL)
2015 return FAIL;
2016
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002017 // Need to compile any default values to get the argument types.
2018 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2019 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2020 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002021 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002022}
2023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024/*
2025 * Compile a variable name into a load instruction.
2026 * "end" points to just after the name.
2027 * When "error" is FALSE do not give an error when not found.
2028 */
2029 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002033 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002034 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002036 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 if (*(*arg + 1) == ':')
2039 {
2040 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002041 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002042 {
2043 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002045 switch (**arg)
2046 {
2047 case 'g': isn_type = ISN_LOADGDICT; break;
2048 case 'w': isn_type = ISN_LOADWDICT; break;
2049 case 't': isn_type = ISN_LOADTDICT; break;
2050 case 'b': isn_type = ISN_LOADBDICT; break;
2051 default:
2052 semsg(_(e_namespace), *arg);
2053 goto theend;
2054 }
2055 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2056 goto theend;
2057 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 else
2060 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002061 isntype_T isn_type = ISN_DROP;
2062
2063 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2064 if (name == NULL)
2065 return FAIL;
2066
2067 switch (**arg)
2068 {
2069 case 'v': res = generate_LOADV(cctx, name, error);
2070 break;
2071 case 's': res = compile_load_scriptvar(cctx, name,
2072 NULL, NULL, error);
2073 break;
2074 case 'g': isn_type = ISN_LOADG; break;
2075 case 'w': isn_type = ISN_LOADW; break;
2076 case 't': isn_type = ISN_LOADT; break;
2077 case 'b': isn_type = ISN_LOADB; break;
2078 default: semsg(_(e_namespace), *arg);
2079 goto theend;
2080 }
2081 if (isn_type != ISN_DROP)
2082 {
2083 // Global, Buffer-local, Window-local and Tabpage-local
2084 // variables can be defined later, thus we don't check if it
2085 // exists, give error at runtime.
2086 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 }
2089 }
2090 else
2091 {
2092 size_t len = end - *arg;
2093 int idx;
2094 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002095 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
2097 name = vim_strnsave(*arg, end - *arg);
2098 if (name == NULL)
2099 return FAIL;
2100
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002101 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002103 if (!gen_load_outer)
2104 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 }
2106 else
2107 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002108 lvar_T *lvar = lookup_local(*arg, len, cctx);
2109
2110 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002112 type = lvar->lv_type;
2113 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002114 if (lvar->lv_from_outer)
2115 gen_load_outer = TRUE;
2116 else
2117 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 else
2120 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002121 // "var" can be script-local even without using "s:" if it
2122 // already exists.
2123 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2124 == SCRIPT_VERSION_VIM9
2125 || lookup_script(*arg, len) == OK)
2126 res = compile_load_scriptvar(cctx, name, *arg, &end,
2127 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002128
Bram Moolenaara5565e42020-05-09 15:44:01 +02002129 // When the name starts with an uppercase letter or "x:" it
2130 // can be a user defined function.
2131 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2132 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 }
2134 }
2135 if (gen_load)
2136 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002137 if (gen_load_outer)
2138 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 }
2140
2141 *arg = end;
2142
2143theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002144 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 semsg(_(e_var_notfound), name);
2146 vim_free(name);
2147 return res;
2148}
2149
2150/*
2151 * Compile the argument expressions.
2152 * "arg" points to just after the "(" and is advanced to after the ")"
2153 */
2154 static int
2155compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2156{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002157 char_u *p = *arg;
2158 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaare6085c52020-04-12 20:19:16 +02002160 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002162 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2163 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002164 if (*p == ')')
2165 {
2166 *arg = p + 1;
2167 return OK;
2168 }
2169
Bram Moolenaara5565e42020-05-09 15:44:01 +02002170 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 return FAIL;
2172 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002173
2174 if (*p != ',' && *skipwhite(p) == ',')
2175 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002176 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002177 p = skipwhite(p);
2178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002180 {
2181 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002182 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002183 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002184 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002185 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002186 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002188failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002189 emsg(_(e_missing_close));
2190 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191}
2192
2193/*
2194 * Compile a function call: name(arg1, arg2)
2195 * "arg" points to "name", "arg + varlen" to the "(".
2196 * "argcount_init" is 1 for "value->method()"
2197 * Instructions:
2198 * EVAL arg1
2199 * EVAL arg2
2200 * BCALL / DCALL / UCALL
2201 */
2202 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002203compile_call(
2204 char_u **arg,
2205 size_t varlen,
2206 cctx_T *cctx,
2207 ppconst_T *ppconst,
2208 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209{
2210 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002211 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 int argcount = argcount_init;
2213 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002214 char_u fname_buf[FLEN_FIXED + 1];
2215 char_u *tofree = NULL;
2216 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002218 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002219 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220
Bram Moolenaara5565e42020-05-09 15:44:01 +02002221 // we can evaluate "has('name')" at compile time
2222 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2223 {
2224 char_u *s = skipwhite(*arg + varlen + 1);
2225 typval_T argvars[2];
2226
2227 argvars[0].v_type = VAR_UNKNOWN;
2228 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002229 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002231 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002232 s = skipwhite(s);
2233 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2234 {
2235 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2236
2237 *arg = s + 1;
2238 argvars[1].v_type = VAR_UNKNOWN;
2239 tv->v_type = VAR_NUMBER;
2240 tv->vval.v_number = 0;
2241 f_has(argvars, tv);
2242 clear_tv(&argvars[0]);
2243 ++ppconst->pp_used;
2244 return OK;
2245 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002246 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002247 }
2248
2249 if (generate_ppconst(cctx, ppconst) == FAIL)
2250 return FAIL;
2251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 if (varlen >= sizeof(namebuf))
2253 {
2254 semsg(_("E1011: name too long: %s"), name);
2255 return FAIL;
2256 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002257 vim_strncpy(namebuf, *arg, varlen);
2258 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259
2260 *arg = skipwhite(*arg + varlen + 1);
2261 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002262 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263
Bram Moolenaara1773442020-08-12 15:21:22 +02002264 is_autoload = vim_strchr(name, '#') != NULL;
2265 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 {
2267 int idx;
2268
2269 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002270 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002272 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002273 else
2274 semsg(_(e_unknownfunc), namebuf);
2275 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 }
2277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002279 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002281 {
2282 res = generate_CALL(cctx, ufunc, argcount);
2283 goto theend;
2284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285
2286 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002287 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002288 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002290 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002291 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002292 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002293 garray_T *stack = &cctx->ctx_type_stack;
2294 type_T *type;
2295
2296 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2297 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 goto theend;
2299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002301 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002302 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002303 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002304 res = generate_UCALL(cctx, name, argcount);
2305 else
2306 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002307
2308theend:
2309 vim_free(tofree);
2310 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311}
2312
2313// like NAMESPACE_CHAR but with 'a' and 'l'.
2314#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2315
2316/*
2317 * Find the end of a variable or function name. Unlike find_name_end() this
2318 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002319 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 * Return a pointer to just after the name. Equal to "arg" if there is no
2321 * valid name.
2322 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002323 static char_u *
2324to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325{
2326 char_u *p;
2327
2328 // Quick check for valid starting character.
2329 if (!eval_isnamec1(*arg))
2330 return arg;
2331
2332 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2333 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2334 // and can be used in slice "[n:]".
2335 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002336 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2338 break;
2339 return p;
2340}
2341
2342/*
2343 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002344 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 */
2346 char_u *
2347to_name_const_end(char_u *arg)
2348{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002349 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 typval_T rettv;
2351
2352 if (p == arg && *arg == '[')
2353 {
2354
2355 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002356 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 p = arg;
2358 }
2359 else if (p == arg && *arg == '#' && arg[1] == '{')
2360 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002361 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002363 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 p = arg;
2365 }
2366 else if (p == arg && *arg == '{')
2367 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002368 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002370 // Can be "{x -> ret}()".
2371 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002373 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 if (ret != OK)
2375 p = arg;
2376 }
2377
2378 return p;
2379}
2380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381/*
2382 * parse a list: [expr, expr]
2383 * "*arg" points to the '['.
2384 */
2385 static int
2386compile_list(char_u **arg, cctx_T *cctx)
2387{
2388 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002389 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 int count = 0;
2391
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002392 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002394 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002396 semsg(_(e_list_end), *arg);
2397 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002398 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002399 if (*p == ',')
2400 {
2401 semsg(_(e_no_white_before), ",");
2402 return FAIL;
2403 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002404 if (*p == ']')
2405 {
2406 ++p;
2407 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002408 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002409 p += STRLEN(p);
2410 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002411 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002412 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 break;
2414 ++count;
2415 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002416 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002418 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2419 {
2420 semsg(_(e_white_after), ",");
2421 return FAIL;
2422 }
2423 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002424 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 p = skipwhite(p);
2426 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002427 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428
2429 generate_NEWLIST(cctx, count);
2430 return OK;
2431}
2432
2433/*
2434 * parse a lambda: {arg, arg -> expr}
2435 * "*arg" points to the '{'.
2436 */
2437 static int
2438compile_lambda(char_u **arg, cctx_T *cctx)
2439{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 typval_T rettv;
2441 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002442 evalarg_T evalarg;
2443
2444 CLEAR_FIELD(evalarg);
2445 evalarg.eval_flags = EVAL_EVALUATE;
2446 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447
2448 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002449 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002453 ++ufunc->uf_refcount;
2454 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002455 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
2457 // The function will have one line: "return {expr}".
2458 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002459 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002461 clear_evalarg(&evalarg, NULL);
2462
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002463 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002464 {
2465 // The return type will now be known.
2466 set_function_type(ufunc);
2467
2468 return generate_FUNCREF(cctx, ufunc);
2469 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002470
2471 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 return FAIL;
2473}
2474
2475/*
2476 * Compile a lamda call: expr->{lambda}(args)
2477 * "arg" points to the "{".
2478 */
2479 static int
2480compile_lambda_call(char_u **arg, cctx_T *cctx)
2481{
2482 ufunc_T *ufunc;
2483 typval_T rettv;
2484 int argcount = 1;
2485 int ret = FAIL;
2486
2487 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002488 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 return FAIL;
2490
2491 if (**arg != '(')
2492 {
2493 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002494 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 else
2496 semsg(_(e_missing_paren), "lambda");
2497 clear_tv(&rettv);
2498 return FAIL;
2499 }
2500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 ufunc = rettv.vval.v_partial->pt_func;
2502 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002503 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002504 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002505
2506 // The function will have one line: "return {expr}".
2507 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002508 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
2510 // compile the arguments
2511 *arg = skipwhite(*arg + 1);
2512 if (compile_arguments(arg, cctx, &argcount) == OK)
2513 // call the compiled function
2514 ret = generate_CALL(cctx, ufunc, argcount);
2515
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002516 if (ret == FAIL)
2517 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 return ret;
2519}
2520
2521/*
2522 * parse a dict: {'key': val} or #{key: val}
2523 * "*arg" points to the '{'.
2524 */
2525 static int
2526compile_dict(char_u **arg, cctx_T *cctx, int literal)
2527{
2528 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 int count = 0;
2531 dict_T *d = dict_alloc();
2532 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002533 char_u *whitep = *arg;
2534 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535
2536 if (d == NULL)
2537 return FAIL;
2538 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002539 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
2541 char_u *key = NULL;
2542
Bram Moolenaar23c55272020-06-21 16:58:13 +02002543 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002544 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002545 *arg = NULL;
2546 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002547 }
2548
2549 if (**arg == '}')
2550 break;
2551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 if (literal)
2553 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002554 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555
Bram Moolenaar2c330432020-04-13 14:41:35 +02002556 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 {
2558 semsg(_("E1014: Invalid key: %s"), *arg);
2559 return FAIL;
2560 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002561 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (generate_PUSHS(cctx, key) == FAIL)
2563 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002564 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 }
2566 else
2567 {
2568 isn_T *isn;
2569
Bram Moolenaara5565e42020-05-09 15:44:01 +02002570 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2573 if (isn->isn_type == ISN_PUSHS)
2574 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002575 else
2576 {
2577 type_T *keytype = ((type_T **)stack->ga_data)
2578 [stack->ga_len - 1];
2579 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2580 return FAIL;
2581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 }
2583
2584 // Check for duplicate keys, if using string keys.
2585 if (key != NULL)
2586 {
2587 item = dict_find(d, key, -1);
2588 if (item != NULL)
2589 {
2590 semsg(_(e_duplicate_key), key);
2591 goto failret;
2592 }
2593 item = dictitem_alloc(key);
2594 if (item != NULL)
2595 {
2596 item->di_tv.v_type = VAR_UNKNOWN;
2597 item->di_tv.v_lock = 0;
2598 if (dict_add(d, item) == FAIL)
2599 dictitem_free(item);
2600 }
2601 }
2602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 if (**arg != ':')
2604 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002605 if (*skipwhite(*arg) == ':')
2606 semsg(_(e_no_white_before), ":");
2607 else
2608 semsg(_(e_missing_dict_colon), *arg);
2609 return FAIL;
2610 }
2611 whitep = *arg + 1;
2612 if (!IS_WHITE_OR_NUL(*whitep))
2613 {
2614 semsg(_(e_white_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 return FAIL;
2616 }
2617
2618 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002619 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002620 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002621 *arg = NULL;
2622 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002623 }
2624
Bram Moolenaara5565e42020-05-09 15:44:01 +02002625 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 return FAIL;
2627 ++count;
2628
Bram Moolenaar2c330432020-04-13 14:41:35 +02002629 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002630 *arg = skipwhite(*arg);
2631 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002632 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002633 *arg = NULL;
2634 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 if (**arg == '}')
2637 break;
2638 if (**arg != ',')
2639 {
2640 semsg(_(e_missing_dict_comma), *arg);
2641 goto failret;
2642 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002643 if (IS_WHITE_OR_NUL(*whitep))
2644 {
2645 semsg(_(e_no_white_before), ",");
2646 return FAIL;
2647 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002648 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 *arg = skipwhite(*arg + 1);
2650 }
2651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 *arg = *arg + 1;
2653
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002654 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002655 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002656 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 *arg += STRLEN(*arg);
2658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 dict_unref(d);
2660 return generate_NEWDICT(cctx, count);
2661
2662failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002663 if (*arg == NULL)
2664 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 dict_unref(d);
2666 return FAIL;
2667}
2668
2669/*
2670 * Compile "&option".
2671 */
2672 static int
2673compile_get_option(char_u **arg, cctx_T *cctx)
2674{
2675 typval_T rettv;
2676 char_u *start = *arg;
2677 int ret;
2678
2679 // parse the option and get the current value to get the type.
2680 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002681 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 if (ret == OK)
2683 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002684 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 char_u *name = vim_strnsave(start, *arg - start);
2686 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2687
2688 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2689 vim_free(name);
2690 }
2691 clear_tv(&rettv);
2692
2693 return ret;
2694}
2695
2696/*
2697 * Compile "$VAR".
2698 */
2699 static int
2700compile_get_env(char_u **arg, cctx_T *cctx)
2701{
2702 char_u *start = *arg;
2703 int len;
2704 int ret;
2705 char_u *name;
2706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 ++*arg;
2708 len = get_env_len(arg);
2709 if (len == 0)
2710 {
2711 semsg(_(e_syntax_at), start - 1);
2712 return FAIL;
2713 }
2714
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002715 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 name = vim_strnsave(start, len + 1);
2717 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2718 vim_free(name);
2719 return ret;
2720}
2721
2722/*
2723 * Compile "@r".
2724 */
2725 static int
2726compile_get_register(char_u **arg, cctx_T *cctx)
2727{
2728 int ret;
2729
2730 ++*arg;
2731 if (**arg == NUL)
2732 {
2733 semsg(_(e_syntax_at), *arg - 1);
2734 return FAIL;
2735 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002736 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 {
2738 emsg_invreg(**arg);
2739 return FAIL;
2740 }
2741 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2742 ++*arg;
2743 return ret;
2744}
2745
2746/*
2747 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002748 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 */
2750 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002751apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002753 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754
2755 // this works from end to start
2756 while (p > start)
2757 {
2758 --p;
2759 if (*p == '-' || *p == '+')
2760 {
2761 // only '-' has an effect, for '+' we only check the type
2762#ifdef FEAT_FLOAT
2763 if (rettv->v_type == VAR_FLOAT)
2764 {
2765 if (*p == '-')
2766 rettv->vval.v_float = -rettv->vval.v_float;
2767 }
2768 else
2769#endif
2770 {
2771 varnumber_T val;
2772 int error = FALSE;
2773
2774 // tv_get_number_chk() accepts a string, but we don't want that
2775 // here
2776 if (check_not_string(rettv) == FAIL)
2777 return FAIL;
2778 val = tv_get_number_chk(rettv, &error);
2779 clear_tv(rettv);
2780 if (error)
2781 return FAIL;
2782 if (*p == '-')
2783 val = -val;
2784 rettv->v_type = VAR_NUMBER;
2785 rettv->vval.v_number = val;
2786 }
2787 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002788 else if (numeric_only)
2789 {
2790 ++p;
2791 break;
2792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 else
2794 {
2795 int v = tv2bool(rettv);
2796
2797 // '!' is permissive in the type.
2798 clear_tv(rettv);
2799 rettv->v_type = VAR_BOOL;
2800 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2801 }
2802 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002803 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 return OK;
2805}
2806
2807/*
2808 * Recognize v: variables that are constants and set "rettv".
2809 */
2810 static void
2811get_vim_constant(char_u **arg, typval_T *rettv)
2812{
2813 if (STRNCMP(*arg, "v:true", 6) == 0)
2814 {
2815 rettv->v_type = VAR_BOOL;
2816 rettv->vval.v_number = VVAL_TRUE;
2817 *arg += 6;
2818 }
2819 else if (STRNCMP(*arg, "v:false", 7) == 0)
2820 {
2821 rettv->v_type = VAR_BOOL;
2822 rettv->vval.v_number = VVAL_FALSE;
2823 *arg += 7;
2824 }
2825 else if (STRNCMP(*arg, "v:null", 6) == 0)
2826 {
2827 rettv->v_type = VAR_SPECIAL;
2828 rettv->vval.v_number = VVAL_NULL;
2829 *arg += 6;
2830 }
2831 else if (STRNCMP(*arg, "v:none", 6) == 0)
2832 {
2833 rettv->v_type = VAR_SPECIAL;
2834 rettv->vval.v_number = VVAL_NONE;
2835 *arg += 6;
2836 }
2837}
2838
Bram Moolenaar696ba232020-07-29 21:20:41 +02002839 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002840get_compare_type(char_u *p, int *len, int *type_is)
2841{
2842 exptype_T type = EXPR_UNKNOWN;
2843 int i;
2844
2845 switch (p[0])
2846 {
2847 case '=': if (p[1] == '=')
2848 type = EXPR_EQUAL;
2849 else if (p[1] == '~')
2850 type = EXPR_MATCH;
2851 break;
2852 case '!': if (p[1] == '=')
2853 type = EXPR_NEQUAL;
2854 else if (p[1] == '~')
2855 type = EXPR_NOMATCH;
2856 break;
2857 case '>': if (p[1] != '=')
2858 {
2859 type = EXPR_GREATER;
2860 *len = 1;
2861 }
2862 else
2863 type = EXPR_GEQUAL;
2864 break;
2865 case '<': if (p[1] != '=')
2866 {
2867 type = EXPR_SMALLER;
2868 *len = 1;
2869 }
2870 else
2871 type = EXPR_SEQUAL;
2872 break;
2873 case 'i': if (p[1] == 's')
2874 {
2875 // "is" and "isnot"; but not a prefix of a name
2876 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2877 *len = 5;
2878 i = p[*len];
2879 if (!isalnum(i) && i != '_')
2880 {
2881 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2882 *type_is = TRUE;
2883 }
2884 }
2885 break;
2886 }
2887 return type;
2888}
2889
2890/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002892 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 */
2894 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002895compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002897 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
2899 // this works from end to start
2900 while (p > start)
2901 {
2902 --p;
2903 if (*p == '-' || *p == '+')
2904 {
2905 int negate = *p == '-';
2906 isn_T *isn;
2907
2908 // TODO: check type
2909 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2910 {
2911 --p;
2912 if (*p == '-')
2913 negate = !negate;
2914 }
2915 // only '-' has an effect, for '+' we only check the type
2916 if (negate)
2917 isn = generate_instr(cctx, ISN_NEGATENR);
2918 else
2919 isn = generate_instr(cctx, ISN_CHECKNR);
2920 if (isn == NULL)
2921 return FAIL;
2922 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002923 else if (numeric_only)
2924 {
2925 ++p;
2926 break;
2927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 else
2929 {
2930 int invert = TRUE;
2931
2932 while (p > start && p[-1] == '!')
2933 {
2934 --p;
2935 invert = !invert;
2936 }
2937 if (generate_2BOOL(cctx, invert) == FAIL)
2938 return FAIL;
2939 }
2940 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002941 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 return OK;
2943}
2944
2945/*
2946 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002947 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 */
2949 static int
2950compile_subscript(
2951 char_u **arg,
2952 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002953 char_u *start_leader,
2954 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002955 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002957 char_u *name_start = *end_leader;
2958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 for (;;)
2960 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002961 char_u *p = skipwhite(*arg);
2962
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002963 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002965 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002966
2967 // If a following line starts with "->{" or "->X" advance to that
2968 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002969 // Also if a following line starts with ".x".
2970 if (next != NULL &&
2971 ((next[0] == '-' && next[1] == '>'
2972 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002973 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002974 {
2975 next = next_line_from_context(cctx, TRUE);
2976 if (next == NULL)
2977 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002978 *arg = next;
2979 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 }
2981 }
2982
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002983 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2984 // not a function call.
2985 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002987 garray_T *stack = &cctx->ctx_type_stack;
2988 type_T *type;
2989 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002991 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002992 return FAIL;
2993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002995 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2996
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002997 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2999 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003000 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 return FAIL;
3002 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003003 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003005 char_u *pstart = p;
3006
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003007 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003008 return FAIL;
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 // something->method()
3011 // Apply the '!', '-' and '+' first:
3012 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003013 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003016 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003017 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003018 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 if (**arg == '{')
3020 {
3021 // lambda call: list->{lambda}
3022 if (compile_lambda_call(arg, cctx) == FAIL)
3023 return FAIL;
3024 }
3025 else
3026 {
3027 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003028 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003029 if (!eval_isnamec1(*p))
3030 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003031 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003032 return FAIL;
3033 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003034 if (ASCII_ISALPHA(*p) && p[1] == ':')
3035 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003036 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 ;
3038 if (*p != '(')
3039 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003040 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 return FAIL;
3042 }
3043 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003044 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 return FAIL;
3046 }
3047 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003048 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003050 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003051 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003052 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003053
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003054 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003055 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003056 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003057 // TODO: blob index
3058 // TODO: more arguments
3059 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003060 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003061 return FAIL;
3062
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003063 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003064 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003065 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003066 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003067 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 return FAIL;
3069
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003070 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3071 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (**arg != ']')
3073 {
3074 emsg(_(e_missbrac));
3075 return FAIL;
3076 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003077 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003079 // We can index a list and a dict. If we don't know the type
3080 // we can use the index value type.
3081 // TODO: If we don't know use an instruction to figure it out at
3082 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003084 vtype = (*typep)->tt_type;
3085 if (*typep == &t_any)
3086 {
3087 type_T *valtype = ((type_T **)stack->ga_data)
3088 [stack->ga_len - 1];
3089 if (valtype == &t_string)
3090 vtype = VAR_DICT;
3091 }
3092 if (vtype == VAR_DICT)
3093 {
3094 if ((*typep)->tt_type == VAR_DICT)
3095 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003096 else
3097 {
3098 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3099 return FAIL;
3100 *typep = &t_any;
3101 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003102 if (may_generate_2STRING(-1, cctx) == FAIL)
3103 return FAIL;
3104 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3105 return FAIL;
3106 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003107 else if (vtype == VAR_STRING)
3108 {
3109 *typep = &t_number;
3110 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3111 return FAIL;
3112 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003113 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003114 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003115 if ((*typep)->tt_type == VAR_LIST)
3116 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003117 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003118 return FAIL;
3119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003120 else
3121 {
3122 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003123 return FAIL;
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003126 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003128 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003129 return FAIL;
3130
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003131 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003132 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3133 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003135 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003136 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 while (eval_isnamec(*p))
3138 MB_PTR_ADV(p);
3139 if (p == *arg)
3140 {
3141 semsg(_(e_syntax_at), *arg);
3142 return FAIL;
3143 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003144 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
3146 *arg = p;
3147 }
3148 else
3149 break;
3150 }
3151
3152 // TODO - see handle_subscript():
3153 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3154 // Don't do this when "Func" is already a partial that was bound
3155 // explicitly (pt_auto is FALSE).
3156
3157 return OK;
3158}
3159
3160/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003161 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3162 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003164 * If the value is a constant "ppconst->pp_ret" will be set.
3165 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003166 *
3167 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 */
3169
3170/*
3171 * number number constant
3172 * 0zFFFFFFFF Blob constant
3173 * "string" string constant
3174 * 'string' literal string constant
3175 * &option-name option value
3176 * @r register contents
3177 * identifier variable value
3178 * function() function call
3179 * $VAR environment variable
3180 * (expression) nested expression
3181 * [expr, expr] List
3182 * {key: val, key: val} Dictionary
3183 * #{key: val, key: val} Dictionary with literal keys
3184 *
3185 * Also handle:
3186 * ! in front logical NOT
3187 * - in front unary minus
3188 * + in front unary plus (ignored)
3189 * trailing (arg) funcref/partial call
3190 * trailing [] subscript in String or List
3191 * trailing .name entry in Dictionary
3192 * trailing ->name() method call
3193 */
3194 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003195compile_expr7(
3196 char_u **arg,
3197 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003198 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 char_u *start_leader, *end_leader;
3201 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003202 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003203 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
3205 /*
3206 * Skip '!', '-' and '+' characters. They are handled later.
3207 */
3208 start_leader = *arg;
3209 while (**arg == '!' || **arg == '-' || **arg == '+')
3210 *arg = skipwhite(*arg + 1);
3211 end_leader = *arg;
3212
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003213 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 switch (**arg)
3215 {
3216 /*
3217 * Number constant.
3218 */
3219 case '0': // also for blob starting with 0z
3220 case '1':
3221 case '2':
3222 case '3':
3223 case '4':
3224 case '5':
3225 case '6':
3226 case '7':
3227 case '8':
3228 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003229 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003231 // Apply "-" and "+" just before the number now, right to
3232 // left. Matters especially when "->" follows. Stops at
3233 // '!'.
3234 if (apply_leader(rettv, TRUE,
3235 start_leader, &end_leader) == FAIL)
3236 {
3237 clear_tv(rettv);
3238 return FAIL;
3239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 break;
3241
3242 /*
3243 * String constant: "string".
3244 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003245 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 break;
3248
3249 /*
3250 * Literal string constant: 'str''ing'.
3251 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003252 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 return FAIL;
3254 break;
3255
3256 /*
3257 * Constant Vim variable.
3258 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003259 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 ret = NOTDONE;
3261 break;
3262
3263 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003264 * "true" constant
3265 */
3266 case 't': if (STRNCMP(*arg, "true", 4) == 0
3267 && !eval_isnamec((*arg)[4]))
3268 {
3269 *arg += 4;
3270 rettv->v_type = VAR_BOOL;
3271 rettv->vval.v_number = VVAL_TRUE;
3272 }
3273 else
3274 ret = NOTDONE;
3275 break;
3276
3277 /*
3278 * "false" constant
3279 */
3280 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3281 && !eval_isnamec((*arg)[5]))
3282 {
3283 *arg += 5;
3284 rettv->v_type = VAR_BOOL;
3285 rettv->vval.v_number = VVAL_FALSE;
3286 }
3287 else
3288 ret = NOTDONE;
3289 break;
3290
3291 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 * List: [expr, expr]
3293 */
3294 case '[': ret = compile_list(arg, cctx);
3295 break;
3296
3297 /*
3298 * Dictionary: #{key: val, key: val}
3299 */
3300 case '#': if ((*arg)[1] == '{')
3301 {
3302 ++*arg;
3303 ret = compile_dict(arg, cctx, TRUE);
3304 }
3305 else
3306 ret = NOTDONE;
3307 break;
3308
3309 /*
3310 * Lambda: {arg, arg -> expr}
3311 * Dictionary: {'key': val, 'key': val}
3312 */
3313 case '{': {
3314 char_u *start = skipwhite(*arg + 1);
3315
3316 // Find out what comes after the arguments.
3317 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003318 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (ret != FAIL && *start == '>')
3320 ret = compile_lambda(arg, cctx);
3321 else
3322 ret = compile_dict(arg, cctx, FALSE);
3323 }
3324 break;
3325
3326 /*
3327 * Option value: &name
3328 */
3329 case '&': ret = compile_get_option(arg, cctx);
3330 break;
3331
3332 /*
3333 * Environment variable: $VAR.
3334 */
3335 case '$': ret = compile_get_env(arg, cctx);
3336 break;
3337
3338 /*
3339 * Register contents: @r.
3340 */
3341 case '@': ret = compile_get_register(arg, cctx);
3342 break;
3343 /*
3344 * nested expression: (expression).
3345 */
3346 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003347
3348 // recursive!
3349 if (ppconst->pp_used <= PPSIZE - 10)
3350 {
3351 ret = compile_expr1(arg, cctx, ppconst);
3352 }
3353 else
3354 {
3355 // Not enough space in ppconst, flush constants.
3356 if (generate_ppconst(cctx, ppconst) == FAIL)
3357 return FAIL;
3358 ret = compile_expr0(arg, cctx);
3359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 *arg = skipwhite(*arg);
3361 if (**arg == ')')
3362 ++*arg;
3363 else if (ret == OK)
3364 {
3365 emsg(_(e_missing_close));
3366 ret = FAIL;
3367 }
3368 break;
3369
3370 default: ret = NOTDONE;
3371 break;
3372 }
3373 if (ret == FAIL)
3374 return FAIL;
3375
Bram Moolenaar1c747212020-05-09 18:28:34 +02003376 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003378 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003379 clear_tv(rettv);
3380 else
3381 // A constant expression can possibly be handled compile time,
3382 // return the value instead of generating code.
3383 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 }
3385 else if (ret == NOTDONE)
3386 {
3387 char_u *p;
3388 int r;
3389
3390 if (!eval_isnamec1(**arg))
3391 {
3392 semsg(_("E1015: Name expected: %s"), *arg);
3393 return FAIL;
3394 }
3395
3396 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003397 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003399 {
3400 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003403 {
3404 if (generate_ppconst(cctx, ppconst) == FAIL)
3405 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 if (r == FAIL)
3409 return FAIL;
3410 }
3411
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003412 // Handle following "[]", ".member", etc.
3413 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003414 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003415 ppconst) == FAIL)
3416 return FAIL;
3417 if (ppconst->pp_used > 0)
3418 {
3419 // apply the '!', '-' and '+' before the constant
3420 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003421 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003422 return FAIL;
3423 return OK;
3424 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003425 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003427 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428}
3429
3430/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003431 * Give the "white on both sides" error, taking the operator from "p[len]".
3432 */
3433 void
3434error_white_both(char_u *op, int len)
3435{
3436 char_u buf[10];
3437
3438 vim_strncpy(buf, op, len);
3439 semsg(_(e_white_both), buf);
3440}
3441
3442/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003443 * <type>expr7: runtime type check / conversion
3444 */
3445 static int
3446compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3447{
3448 type_T *want_type = NULL;
3449
3450 // Recognize <type>
3451 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3452 {
3453 int called_emsg_before = called_emsg;
3454
3455 ++*arg;
3456 want_type = parse_type(arg, cctx->ctx_type_list);
3457 if (called_emsg != called_emsg_before)
3458 return FAIL;
3459
3460 if (**arg != '>')
3461 {
3462 if (*skipwhite(*arg) == '>')
3463 semsg(_(e_no_white_before), ">");
3464 else
3465 emsg(_("E1104: Missing >"));
3466 return FAIL;
3467 }
3468 ++*arg;
3469 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3470 return FAIL;
3471 }
3472
3473 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3474 return FAIL;
3475
3476 if (want_type != NULL)
3477 {
3478 garray_T *stack = &cctx->ctx_type_stack;
3479 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3480
3481 if (check_type(want_type, actual, FALSE) == FAIL)
3482 {
3483 generate_ppconst(cctx, ppconst);
3484 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3485 return FAIL;
3486 }
3487 }
3488
3489 return OK;
3490}
3491
3492/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 * * number multiplication
3494 * / number division
3495 * % number modulo
3496 */
3497 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003498compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499{
3500 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003501 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003502 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003504 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003505 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 return FAIL;
3507
3508 /*
3509 * Repeat computing, until no "*", "/" or "%" is following.
3510 */
3511 for (;;)
3512 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003513 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 if (*op != '*' && *op != '/' && *op != '%')
3515 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003516 if (next != NULL)
3517 {
3518 *arg = next_line_from_context(cctx, TRUE);
3519 op = skipwhite(*arg);
3520 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003521
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003522 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003524 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003525 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 }
3527 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003528 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003529 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003531 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003532 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003534
3535 if (ppconst->pp_used == ppconst_used + 2
3536 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3537 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003538 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003539 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3540 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003541 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003543 // both are numbers: compute the result
3544 switch (*op)
3545 {
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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003550 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003551 break;
3552 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003553 tv1->vval.v_number = res;
3554 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003555 }
3556 else
3557 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003558 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003559 generate_two_op(cctx, op);
3560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 }
3562
3563 return OK;
3564}
3565
3566/*
3567 * + number addition
3568 * - number subtraction
3569 * .. string concatenation
3570 */
3571 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003572compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573{
3574 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003575 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003577 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578
3579 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003580 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 return FAIL;
3582
3583 /*
3584 * Repeat computing, until no "+", "-" or ".." is following.
3585 */
3586 for (;;)
3587 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003588 op = may_peek_next_line(cctx, *arg, &next);
3589 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 break;
3591 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003592 if (next != NULL)
3593 {
3594 *arg = next_line_from_context(cctx, TRUE);
3595 op = skipwhite(*arg);
3596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003598 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003600 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003601 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 }
3603
3604 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003605 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003606 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003608 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003609 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 return FAIL;
3611
Bram Moolenaara5565e42020-05-09 15:44:01 +02003612 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003613 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003614 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3615 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3616 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3617 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003619 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3620 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003621
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003622 // concat/subtract/add constant numbers
3623 if (*op == '+')
3624 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3625 else if (*op == '-')
3626 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3627 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003628 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003629 // concatenate constant strings
3630 char_u *s1 = tv1->vval.v_string;
3631 char_u *s2 = tv2->vval.v_string;
3632 size_t len1 = STRLEN(s1);
3633
3634 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3635 if (tv1->vval.v_string == NULL)
3636 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003637 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003638 return FAIL;
3639 }
3640 mch_memmove(tv1->vval.v_string, s1, len1);
3641 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 vim_free(s1);
3643 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003644 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003645 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 }
3647 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003648 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003649 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003650 if (*op == '.')
3651 {
3652 if (may_generate_2STRING(-2, cctx) == FAIL
3653 || may_generate_2STRING(-1, cctx) == FAIL)
3654 return FAIL;
3655 generate_instr_drop(cctx, ISN_CONCAT, 1);
3656 }
3657 else
3658 generate_two_op(cctx, op);
3659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661
3662 return OK;
3663}
3664
3665/*
3666 * expr5a == expr5b
3667 * expr5a =~ expr5b
3668 * expr5a != expr5b
3669 * expr5a !~ expr5b
3670 * expr5a > expr5b
3671 * expr5a >= expr5b
3672 * expr5a < expr5b
3673 * expr5a <= expr5b
3674 * expr5a is expr5b
3675 * expr5a isnot expr5b
3676 *
3677 * Produces instructions:
3678 * EVAL expr5a Push result of "expr5a"
3679 * EVAL expr5b Push result of "expr5b"
3680 * COMPARE one of the compare instructions
3681 */
3682 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003683compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684{
3685 exptype_T type = EXPR_UNKNOWN;
3686 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003687 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003690 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691
3692 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003693 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 return FAIL;
3695
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003696 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003697 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698
3699 /*
3700 * If there is a comparative operator, use it.
3701 */
3702 if (type != EXPR_UNKNOWN)
3703 {
3704 int ic = FALSE; // Default: do not ignore case
3705
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003706 if (next != NULL)
3707 {
3708 *arg = next_line_from_context(cctx, TRUE);
3709 p = skipwhite(*arg);
3710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 if (type_is && (p[len] == '?' || p[len] == '#'))
3712 {
3713 semsg(_(e_invexpr2), *arg);
3714 return FAIL;
3715 }
3716 // extra question mark appended: ignore case
3717 if (p[len] == '?')
3718 {
3719 ic = TRUE;
3720 ++len;
3721 }
3722 // extra '#' appended: match case (ignored)
3723 else if (p[len] == '#')
3724 ++len;
3725 // nothing appended: match case
3726
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003727 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003729 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 }
3732
3733 // get the second variable
3734 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003735 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003736 return FAIL;
3737
Bram Moolenaara5565e42020-05-09 15:44:01 +02003738 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 return FAIL;
3740
Bram Moolenaara5565e42020-05-09 15:44:01 +02003741 if (ppconst->pp_used == ppconst_used + 2)
3742 {
3743 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3744 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3745 int ret;
3746
3747 // Both sides are a constant, compute the result now.
3748 // First check for a valid combination of types, this is more
3749 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003750 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003751 ret = FAIL;
3752 else
3753 {
3754 ret = typval_compare(tv1, tv2, type, ic);
3755 tv1->v_type = VAR_BOOL;
3756 tv1->vval.v_number = tv1->vval.v_number
3757 ? VVAL_TRUE : VVAL_FALSE;
3758 clear_tv(tv2);
3759 --ppconst->pp_used;
3760 }
3761 return ret;
3762 }
3763
3764 generate_ppconst(cctx, ppconst);
3765 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 }
3767
3768 return OK;
3769}
3770
Bram Moolenaar7f141552020-05-09 17:35:53 +02003771static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773/*
3774 * Compile || or &&.
3775 */
3776 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003777compile_and_or(
3778 char_u **arg,
3779 cctx_T *cctx,
3780 char *op,
3781 ppconst_T *ppconst,
3782 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003784 char_u *next;
3785 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 int opchar = *op;
3787
3788 if (p[0] == opchar && p[1] == opchar)
3789 {
3790 garray_T *instr = &cctx->ctx_instr;
3791 garray_T end_ga;
3792
3793 /*
3794 * Repeat until there is no following "||" or "&&"
3795 */
3796 ga_init2(&end_ga, sizeof(int), 10);
3797 while (p[0] == opchar && p[1] == opchar)
3798 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003799 if (next != NULL)
3800 {
3801 *arg = next_line_from_context(cctx, TRUE);
3802 p = skipwhite(*arg);
3803 }
3804
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3806 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 return FAIL;
3809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
Bram Moolenaara5565e42020-05-09 15:44:01 +02003811 // TODO: use ppconst if the value is a constant
3812 generate_ppconst(cctx, ppconst);
3813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (ga_grow(&end_ga, 1) == FAIL)
3815 {
3816 ga_clear(&end_ga);
3817 return FAIL;
3818 }
3819 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3820 ++end_ga.ga_len;
3821 generate_JUMP(cctx, opchar == '|'
3822 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3823
3824 // eval the next expression
3825 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003826 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003827 return FAIL;
3828
Bram Moolenaara5565e42020-05-09 15:44:01 +02003829 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3830 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 {
3832 ga_clear(&end_ga);
3833 return FAIL;
3834 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003835
3836 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003838 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
3840 // Fill in the end label in all jumps.
3841 while (end_ga.ga_len > 0)
3842 {
3843 isn_T *isn;
3844
3845 --end_ga.ga_len;
3846 isn = ((isn_T *)instr->ga_data)
3847 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3848 isn->isn_arg.jump.jump_where = instr->ga_len;
3849 }
3850 ga_clear(&end_ga);
3851 }
3852
3853 return OK;
3854}
3855
3856/*
3857 * expr4a && expr4a && expr4a logical AND
3858 *
3859 * Produces instructions:
3860 * EVAL expr4a Push result of "expr4a"
3861 * JUMP_AND_KEEP_IF_FALSE end
3862 * EVAL expr4b Push result of "expr4b"
3863 * JUMP_AND_KEEP_IF_FALSE end
3864 * EVAL expr4c Push result of "expr4c"
3865 * end:
3866 */
3867 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003868compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003870 int ppconst_used = ppconst->pp_used;
3871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003873 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 return FAIL;
3875
3876 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003877 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878}
3879
3880/*
3881 * expr3a || expr3b || expr3c logical OR
3882 *
3883 * Produces instructions:
3884 * EVAL expr3a Push result of "expr3a"
3885 * JUMP_AND_KEEP_IF_TRUE end
3886 * EVAL expr3b Push result of "expr3b"
3887 * JUMP_AND_KEEP_IF_TRUE end
3888 * EVAL expr3c Push result of "expr3c"
3889 * end:
3890 */
3891 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003892compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003894 int ppconst_used = ppconst->pp_used;
3895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 return FAIL;
3899
3900 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003901 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902}
3903
3904/*
3905 * Toplevel expression: expr2 ? expr1a : expr1b
3906 *
3907 * Produces instructions:
3908 * EVAL expr2 Push result of "expr"
3909 * JUMP_IF_FALSE alt jump if false
3910 * EVAL expr1a
3911 * JUMP_ALWAYS end
3912 * alt: EVAL expr1b
3913 * end:
3914 */
3915 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003916compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917{
3918 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003920 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921
Bram Moolenaar61a89812020-05-07 16:58:17 +02003922 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 return FAIL;
3925
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003926 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 if (*p == '?')
3928 {
3929 garray_T *instr = &cctx->ctx_instr;
3930 garray_T *stack = &cctx->ctx_type_stack;
3931 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003932 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003934 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003936 int has_const_expr = FALSE;
3937 int const_value = FALSE;
3938 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003940 if (next != NULL)
3941 {
3942 *arg = next_line_from_context(cctx, TRUE);
3943 p = skipwhite(*arg);
3944 }
3945
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003946 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3947 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003949 return FAIL;
3950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951
Bram Moolenaara5565e42020-05-09 15:44:01 +02003952 if (ppconst->pp_used == ppconst_used + 1)
3953 {
3954 // the condition is a constant, we know whether the ? or the :
3955 // expression is to be evaluated.
3956 has_const_expr = TRUE;
3957 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3958 clear_tv(&ppconst->pp_tv[ppconst_used]);
3959 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003960 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3961 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003962 }
3963 else
3964 {
3965 generate_ppconst(cctx, ppconst);
3966 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968
3969 // evaluate the second expression; any type is accepted
3970 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003971 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003972 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003974 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976 if (!has_const_expr)
3977 {
3978 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979
Bram Moolenaara5565e42020-05-09 15:44:01 +02003980 // remember the type and drop it
3981 --stack->ga_len;
3982 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 end_idx = instr->ga_len;
3985 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3986
3987 // jump here from JUMP_IF_FALSE
3988 isn = ((isn_T *)instr->ga_data) + alt_idx;
3989 isn->isn_arg.jump.jump_where = instr->ga_len;
3990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991
3992 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003993 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 if (*p != ':')
3995 {
3996 emsg(_(e_missing_colon));
3997 return FAIL;
3998 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003999 if (next != NULL)
4000 {
4001 *arg = next_line_from_context(cctx, TRUE);
4002 p = skipwhite(*arg);
4003 }
4004
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004005 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4006 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004008 return FAIL;
4009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010
4011 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004013 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4014 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004016 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004017 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004019 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020
Bram Moolenaara5565e42020-05-09 15:44:01 +02004021 if (!has_const_expr)
4022 {
4023 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
Bram Moolenaara5565e42020-05-09 15:44:01 +02004025 // If the types differ, the result has a more generic type.
4026 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4027 common_type(type1, type2, &type2, cctx->ctx_type_list);
4028
4029 // jump here from JUMP_ALWAYS
4030 isn = ((isn_T *)instr->ga_data) + end_idx;
4031 isn->isn_arg.jump.jump_where = instr->ga_len;
4032 }
4033
4034 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 }
4036 return OK;
4037}
4038
4039/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004040 * Toplevel expression.
4041 */
4042 static int
4043compile_expr0(char_u **arg, cctx_T *cctx)
4044{
4045 ppconst_T ppconst;
4046
4047 CLEAR_FIELD(ppconst);
4048 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4049 {
4050 clear_ppconst(&ppconst);
4051 return FAIL;
4052 }
4053 if (generate_ppconst(cctx, &ppconst) == FAIL)
4054 return FAIL;
4055 return OK;
4056}
4057
4058/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 * compile "return [expr]"
4060 */
4061 static char_u *
4062compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4063{
4064 char_u *p = arg;
4065 garray_T *stack = &cctx->ctx_type_stack;
4066 type_T *stack_type;
4067
4068 if (*p != NUL && *p != '|' && *p != '\n')
4069 {
4070 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 return NULL;
4073
4074 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4075 if (set_return_type)
4076 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004077 else
4078 {
4079 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4080 && stack_type->tt_type != VAR_VOID
4081 && stack_type->tt_type != VAR_UNKNOWN)
4082 {
4083 emsg(_("E1096: Returning a value in a function without a return type"));
4084 return NULL;
4085 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004086 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4087 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 }
4091 else
4092 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004093 // "set_return_type" cannot be TRUE, only used for a lambda which
4094 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004095 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4096 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
4098 emsg(_("E1003: Missing return value"));
4099 return NULL;
4100 }
4101
4102 // No argument, return zero.
4103 generate_PUSHNR(cctx, 0);
4104 }
4105
4106 if (generate_instr(cctx, ISN_RETURN) == NULL)
4107 return NULL;
4108
4109 // "return val | endif" is possible
4110 return skipwhite(p);
4111}
4112
4113/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004114 * Get a line from the compilation context, compatible with exarg_T getline().
4115 * Return a pointer to the line in allocated memory.
4116 * Return NULL for end-of-file or some error.
4117 */
4118 static char_u *
4119exarg_getline(
4120 int c UNUSED,
4121 void *cookie,
4122 int indent UNUSED,
4123 int do_concat UNUSED)
4124{
4125 cctx_T *cctx = (cctx_T *)cookie;
4126
4127 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4128 {
4129 iemsg("Heredoc got to end");
4130 return NULL;
4131 }
4132 ++cctx->ctx_lnum;
4133 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4134 [cctx->ctx_lnum]);
4135}
4136
4137/*
4138 * Compile a nested :def command.
4139 */
4140 static char_u *
4141compile_nested_function(exarg_T *eap, cctx_T *cctx)
4142{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004143 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004144 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004145 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004146 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004147 lvar_T *lvar;
4148 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004149 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004150
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004151 // Only g:Func() can use a namespace.
4152 if (name_start[1] == ':' && !is_global)
4153 {
4154 semsg(_(e_namespace), name_start);
4155 return NULL;
4156 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004157 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4158 return NULL;
4159
Bram Moolenaar04b12692020-05-04 23:24:44 +02004160 eap->arg = name_end;
4161 eap->getline = exarg_getline;
4162 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004163 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004164 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004165 lambda_name = get_lambda_name();
4166 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004167
Bram Moolenaar822ba242020-05-24 23:00:18 +02004168 if (ufunc == NULL)
4169 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004170 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004171 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004172 return NULL;
4173
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004174 if (is_global)
4175 {
4176 char_u *func_name = vim_strnsave(name_start + 2,
4177 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004178
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004179 if (func_name == NULL)
4180 r = FAIL;
4181 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004182 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004183 }
4184 else
4185 {
4186 // Define a local variable for the function reference.
4187 lvar = reserve_local(cctx, name_start, name_end - name_start,
4188 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004189 if (lvar == NULL)
4190 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004191 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004192 return NULL;
4193 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4194 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004195
Bram Moolenaar61a89812020-05-07 16:58:17 +02004196 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004197 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004198}
4199
4200/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 * Return the length of an assignment operator, or zero if there isn't one.
4202 */
4203 int
4204assignment_len(char_u *p, int *heredoc)
4205{
4206 if (*p == '=')
4207 {
4208 if (p[1] == '<' && p[2] == '<')
4209 {
4210 *heredoc = TRUE;
4211 return 3;
4212 }
4213 return 1;
4214 }
4215 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4216 return 2;
4217 if (STRNCMP(p, "..=", 3) == 0)
4218 return 3;
4219 return 0;
4220}
4221
4222// words that cannot be used as a variable
4223static char *reserved[] = {
4224 "true",
4225 "false",
4226 NULL
4227};
4228
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004229typedef enum {
4230 dest_local,
4231 dest_option,
4232 dest_env,
4233 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004234 dest_buffer,
4235 dest_window,
4236 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004237 dest_vimvar,
4238 dest_script,
4239 dest_reg,
4240} assign_dest_T;
4241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004243 * Generate the load instruction for "name".
4244 */
4245 static void
4246generate_loadvar(
4247 cctx_T *cctx,
4248 assign_dest_T dest,
4249 char_u *name,
4250 lvar_T *lvar,
4251 type_T *type)
4252{
4253 switch (dest)
4254 {
4255 case dest_option:
4256 // TODO: check the option exists
4257 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4258 break;
4259 case dest_global:
4260 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4261 break;
4262 case dest_buffer:
4263 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4264 break;
4265 case dest_window:
4266 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4267 break;
4268 case dest_tab:
4269 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4270 break;
4271 case dest_script:
4272 compile_load_scriptvar(cctx,
4273 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4274 break;
4275 case dest_env:
4276 // Include $ in the name here
4277 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4278 break;
4279 case dest_reg:
4280 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4281 break;
4282 case dest_vimvar:
4283 generate_LOADV(cctx, name + 2, TRUE);
4284 break;
4285 case dest_local:
4286 if (lvar->lv_from_outer)
4287 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4288 NULL, type);
4289 else
4290 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4291 break;
4292 }
4293}
4294
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004295 void
4296vim9_declare_error(char_u *name)
4297{
4298 char *scope = "";
4299
4300 switch (*name)
4301 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004302 case 'g': scope = _("global"); break;
4303 case 'b': scope = _("buffer"); break;
4304 case 'w': scope = _("window"); break;
4305 case 't': scope = _("tab"); break;
4306 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004307 case '$': semsg(_(e_declare_env_var), name);
4308 return;
4309 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4310 return;
4311 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4312 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004313 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004314 }
4315 semsg(_(e_declare_var), scope, name);
4316}
4317
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004318/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004319 * Compile declaration and assignment:
4320 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004322 * Return NULL for an error.
4323 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 */
4325 static char_u *
4326compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4327{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004328 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004330 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 char_u *ret = NULL;
4332 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004333 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004334 int scriptvar_sid = 0;
4335 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004338 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 int oplen = 0;
4341 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004342 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004343 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004344 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004348 // Skip over the "var" or "[var, var]" to get to any "=".
4349 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4350 if (p == NULL)
4351 return *arg == '[' ? arg : NULL;
4352
4353 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004355 // TODO: should we allow this, and figure out type inference from list
4356 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004357 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 return NULL;
4359 }
4360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 sp = p;
4362 p = skipwhite(p);
4363 op = p;
4364 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004365
4366 if (var_count > 0 && oplen == 0)
4367 // can be something like "[1, 2]->func()"
4368 return arg;
4369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4371 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004372 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004373 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004374 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 if (heredoc)
4377 {
4378 list_T *l;
4379 listitem_T *li;
4380
4381 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004382 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004384 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385
4386 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004387 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 {
4389 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4390 li->li_tv.vval.v_string = NULL;
4391 }
4392 generate_NEWLIST(cctx, l->lv_len);
4393 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004394 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 list_free(l);
4396 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004397 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004399 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004401 // for "[var, var] = expr" evaluate the expression here, loop over the
4402 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004403
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004404 p = skipwhite(op + oplen);
4405 if (compile_expr0(&p, cctx) == FAIL)
4406 return NULL;
4407 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004409 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004411 type_T *stacktype;
4412
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004413 stacktype = stack->ga_len == 0 ? &t_void
4414 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004415 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004417 emsg(_(e_cannot_use_void));
4418 goto theend;
4419 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004420 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004421 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004422 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004423 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4424 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004425 }
4426 }
4427
4428 /*
4429 * Loop over variables in "[var, var] = expr".
4430 * For "var = expr" and "let var: type" this is done only once.
4431 */
4432 if (var_count > 0)
4433 var_start = skipwhite(arg + 1); // skip over the "["
4434 else
4435 var_start = arg;
4436 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4437 {
4438 char_u *var_end = skip_var_one(var_start, FALSE);
4439 size_t varlen;
4440 int new_local = FALSE;
4441 int opt_type;
4442 int opt_flags = 0;
4443 assign_dest_T dest = dest_local;
4444 int vimvaridx = -1;
4445 lvar_T *lvar = NULL;
4446 lvar_T arg_lvar;
4447 int has_type = FALSE;
4448 int has_index = FALSE;
4449 int instr_count = -1;
4450
Bram Moolenaar65821722020-08-02 18:58:54 +02004451 if (*var_start == '@')
4452 p = var_start + 2;
4453 else
4454 {
4455 p = (*var_start == '&' || *var_start == '$')
4456 ? var_start + 1 : var_start;
4457 p = to_name_end(p, TRUE);
4458 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004459
4460 // "a: type" is declaring variable "a" with a type, not "a:".
4461 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4462 --var_end;
4463 if (is_decl && p == var_start + 2 && p[-1] == ':')
4464 --p;
4465
4466 varlen = p - var_start;
4467 vim_free(name);
4468 name = vim_strnsave(var_start, varlen);
4469 if (name == NULL)
4470 return NULL;
4471 if (!heredoc)
4472 type = &t_any;
4473
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004474 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004475 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004476 int declare_error = FALSE;
4477
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004478 if (*var_start == '&')
4479 {
4480 int cc;
4481 long numval;
4482
4483 dest = dest_option;
4484 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004486 emsg(_(e_const_option));
4487 goto theend;
4488 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004489 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004490 p = var_start;
4491 p = find_option_end(&p, &opt_flags);
4492 if (p == NULL)
4493 {
4494 // cannot happen?
4495 emsg(_(e_letunexp));
4496 goto theend;
4497 }
4498 cc = *p;
4499 *p = NUL;
4500 opt_type = get_option_value(var_start + 1, &numval,
4501 NULL, opt_flags);
4502 *p = cc;
4503 if (opt_type == -3)
4504 {
4505 semsg(_(e_unknown_option), var_start);
4506 goto theend;
4507 }
4508 if (opt_type == -2 || opt_type == 0)
4509 type = &t_string;
4510 else
4511 type = &t_number; // both number and boolean option
4512 }
4513 else if (*var_start == '$')
4514 {
4515 dest = dest_env;
4516 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004517 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004518 }
4519 else if (*var_start == '@')
4520 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004521 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004522 {
4523 emsg_invreg(var_start[1]);
4524 goto theend;
4525 }
4526 dest = dest_reg;
4527 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004528 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004529 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004530 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004531 {
4532 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004533 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004534 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004535 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004536 {
4537 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004538 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004540 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004541 {
4542 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004543 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004545 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004546 {
4547 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004548 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004549 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004550 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004551 {
4552 typval_T *vtv;
4553 int di_flags;
4554
4555 vimvaridx = find_vim_var(name + 2, &di_flags);
4556 if (vimvaridx < 0)
4557 {
4558 semsg(_(e_var_notfound), var_start);
4559 goto theend;
4560 }
4561 // We use the current value of "sandbox" here, is that OK?
4562 if (var_check_ro(di_flags, name, FALSE))
4563 goto theend;
4564 dest = dest_vimvar;
4565 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004566 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004567 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004568 }
4569 else
4570 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004571 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572
4573 for (idx = 0; reserved[idx] != NULL; ++idx)
4574 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004575 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004576 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004577 goto theend;
4578 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579
4580 lvar = lookup_local(var_start, varlen, cctx);
4581 if (lvar == NULL)
4582 {
4583 CLEAR_FIELD(arg_lvar);
4584 if (lookup_arg(var_start, varlen,
4585 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4586 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004587 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004588 if (is_decl)
4589 {
4590 semsg(_(e_used_as_arg), name);
4591 goto theend;
4592 }
4593 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004594 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004595 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004596 if (lvar != NULL)
4597 {
4598 if (is_decl)
4599 {
4600 semsg(_("E1017: Variable already declared: %s"), name);
4601 goto theend;
4602 }
4603 else if (lvar->lv_const)
4604 {
4605 semsg(_("E1018: Cannot assign to a constant: %s"),
4606 name);
4607 goto theend;
4608 }
4609 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004610 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004611 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004612 int script_namespace = varlen > 1
4613 && STRNCMP(var_start, "s:", 2) == 0;
4614 int script_var = (script_namespace
4615 ? lookup_script(var_start + 2, varlen - 2)
4616 : lookup_script(var_start, varlen)) == OK;
4617 imported_T *import =
4618 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004619
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004620 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004622 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4623
4624 if (is_decl)
4625 {
4626 if (script_namespace)
4627 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004628 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004629 else
4630 semsg(_("E1054: Variable already declared in the script: %s"),
4631 name);
4632 goto theend;
4633 }
4634 else if (cctx->ctx_ufunc->uf_script_ctx_version
4635 == SCRIPT_VERSION_VIM9
4636 && script_namespace
4637 && !script_var && import == NULL)
4638 {
4639 semsg(_(e_unknown_var), name);
4640 goto theend;
4641 }
4642
4643 dest = dest_script;
4644
4645 // existing script-local variables should have a type
4646 scriptvar_sid = current_sctx.sc_sid;
4647 if (import != NULL)
4648 scriptvar_sid = import->imp_sid;
4649 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4650 rawname, TRUE);
4651 if (scriptvar_idx >= 0)
4652 {
4653 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4654 svar_T *sv =
4655 ((svar_T *)si->sn_var_vals.ga_data)
4656 + scriptvar_idx;
4657 type = sv->sv_type;
4658 }
4659 }
4660 else if (name[1] == ':' && name[2] != NUL)
4661 {
4662 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004663 name);
4664 goto theend;
4665 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004666 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004667 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004668 semsg(_(e_unknown_var), name);
4669 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004670 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004671 else if (check_defined(var_start, varlen, cctx) == FAIL)
4672 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004674 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004675
4676 if (declare_error)
4677 {
4678 vim9_declare_error(name);
4679 goto theend;
4680 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004681 }
4682
4683 // handle "a:name" as a name, not index "name" on "a"
4684 if (varlen > 1 || var_start[varlen] != ':')
4685 p = var_end;
4686
4687 if (dest != dest_option)
4688 {
4689 if (is_decl && *p == ':')
4690 {
4691 // parse optional type: "let var: type = expr"
4692 if (!VIM_ISWHITE(p[1]))
4693 {
4694 semsg(_(e_white_after), ":");
4695 goto theend;
4696 }
4697 p = skipwhite(p + 1);
4698 type = parse_type(&p, cctx->ctx_type_list);
4699 has_type = TRUE;
4700 }
4701 else if (lvar != NULL)
4702 type = lvar->lv_type;
4703 }
4704
4705 if (oplen == 3 && !heredoc && dest != dest_global
4706 && type->tt_type != VAR_STRING
4707 && type->tt_type != VAR_ANY)
4708 {
4709 emsg(_("E1019: Can only concatenate to string"));
4710 goto theend;
4711 }
4712
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004713 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 {
4715 if (oplen > 1 && !heredoc)
4716 {
4717 // +=, /=, etc. require an existing variable
4718 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4719 name);
4720 goto theend;
4721 }
4722
4723 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004724 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4725 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004726 goto theend;
4727 lvar = reserve_local(cctx, var_start, varlen,
4728 cmdidx == CMD_const, type);
4729 if (lvar == NULL)
4730 goto theend;
4731 new_local = TRUE;
4732 }
4733
4734 member_type = type;
4735 if (var_end > var_start + varlen)
4736 {
4737 // Something follows after the variable: "var[idx]".
4738 if (is_decl)
4739 {
4740 emsg(_("E1087: cannot use an index when declaring a variable"));
4741 goto theend;
4742 }
4743
4744 if (var_start[varlen] == '[')
4745 {
4746 has_index = TRUE;
4747 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004748 member_type = &t_any;
4749 else
4750 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004751 }
4752 else
4753 {
4754 semsg("Not supported yet: %s", var_start);
4755 goto theend;
4756 }
4757 }
4758 else if (lvar == &arg_lvar)
4759 {
4760 semsg(_("E1090: Cannot assign to argument %s"), name);
4761 goto theend;
4762 }
4763
4764 if (!heredoc)
4765 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004766 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004768 if (oplen > 0 && var_count == 0)
4769 {
4770 // skip over the "=" and the expression
4771 p = skipwhite(op + oplen);
4772 compile_expr0(&p, cctx);
4773 }
4774 }
4775 else if (oplen > 0)
4776 {
4777 type_T *stacktype;
4778
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779 // For "var = expr" evaluate the expression.
4780 if (var_count == 0)
4781 {
4782 int r;
4783
4784 // for "+=", "*=", "..=" etc. first load the current value
4785 if (*op != '=')
4786 {
4787 generate_loadvar(cctx, dest, name, lvar, type);
4788
4789 if (has_index)
4790 {
4791 // TODO: get member from list or dict
4792 emsg("Index with operation not supported yet");
4793 goto theend;
4794 }
4795 }
4796
4797 // Compile the expression. Temporarily hide the new local
4798 // variable here, it is not available to this expression.
4799 if (new_local)
4800 --cctx->ctx_locals.ga_len;
4801 instr_count = instr->ga_len;
4802 p = skipwhite(op + oplen);
4803 r = compile_expr0(&p, cctx);
4804 if (new_local)
4805 ++cctx->ctx_locals.ga_len;
4806 if (r == FAIL)
4807 goto theend;
4808 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004809 else if (semicolon && var_idx == var_count - 1)
4810 {
4811 // For "[var; var] = expr" get the rest of the list
4812 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4813 goto theend;
4814 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004815 else
4816 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 // For "[var, var] = expr" get the "var_idx" item from the
4818 // list.
4819 if (generate_GETITEM(cctx, var_idx) == FAIL)
4820 return FAIL;
4821 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004822
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004823 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004824 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004825 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004826 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004827 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004828 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004829 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004830 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004831 emsg(_(e_cannot_use_void));
4832 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004834 else if ((stacktype->tt_type == VAR_FUNC
4835 || stacktype->tt_type == VAR_PARTIAL)
4836 && var_wrong_func_name(name, TRUE))
4837 {
4838 goto theend;
4839 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004840 else
4841 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004842 // An empty list or dict has a &t_void member,
4843 // for a variable that implies &t_any.
4844 if (stacktype == &t_list_empty)
4845 lvar->lv_type = &t_list_any;
4846 else if (stacktype == &t_dict_empty)
4847 lvar->lv_type = &t_dict_any;
4848 else
4849 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004850 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004851 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004852 else
4853 {
4854 type_T *use_type = lvar->lv_type;
4855
4856 if (has_index)
4857 {
4858 use_type = use_type->tt_member;
4859 if (use_type == NULL)
4860 use_type = &t_void;
4861 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004862 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004863 == FAIL)
4864 goto theend;
4865 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004866 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004867 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004868 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004869 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873 emsg(_(e_const_req_value));
4874 goto theend;
4875 }
4876 else if (!has_type || dest == dest_option)
4877 {
4878 emsg(_(e_type_req));
4879 goto theend;
4880 }
4881 else
4882 {
4883 // variables are always initialized
4884 if (ga_grow(instr, 1) == FAIL)
4885 goto theend;
4886 switch (member_type->tt_type)
4887 {
4888 case VAR_BOOL:
4889 generate_PUSHBOOL(cctx, VVAL_FALSE);
4890 break;
4891 case VAR_FLOAT:
4892#ifdef FEAT_FLOAT
4893 generate_PUSHF(cctx, 0.0);
4894#endif
4895 break;
4896 case VAR_STRING:
4897 generate_PUSHS(cctx, NULL);
4898 break;
4899 case VAR_BLOB:
4900 generate_PUSHBLOB(cctx, NULL);
4901 break;
4902 case VAR_FUNC:
4903 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4904 break;
4905 case VAR_LIST:
4906 generate_NEWLIST(cctx, 0);
4907 break;
4908 case VAR_DICT:
4909 generate_NEWDICT(cctx, 0);
4910 break;
4911 case VAR_JOB:
4912 generate_PUSHJOB(cctx, NULL);
4913 break;
4914 case VAR_CHANNEL:
4915 generate_PUSHCHANNEL(cctx, NULL);
4916 break;
4917 case VAR_NUMBER:
4918 case VAR_UNKNOWN:
4919 case VAR_ANY:
4920 case VAR_PARTIAL:
4921 case VAR_VOID:
4922 case VAR_SPECIAL: // cannot happen
4923 generate_PUSHNR(cctx, 0);
4924 break;
4925 }
4926 }
4927 if (var_count == 0)
4928 end = p;
4929 }
4930
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004931 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004932 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004933 break;
4934
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 if (oplen > 0 && *op != '=')
4936 {
4937 type_T *expected = &t_number;
4938 type_T *stacktype;
4939
4940 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004941 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942
4943 if (*op == '.')
4944 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004945 else if (*op == '+')
4946 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004948 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 goto theend;
4950
4951 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004952 {
4953 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4954 goto theend;
4955 }
4956 else if (*op == '+')
4957 {
4958 if (generate_add_instr(cctx,
4959 operator_type(member_type, stacktype),
4960 member_type, stacktype) == FAIL)
4961 goto theend;
4962 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 else
4964 {
4965 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4966
4967 if (isn == NULL)
4968 goto theend;
4969 switch (*op)
4970 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4972 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4973 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4974 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 }
4977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004980 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981 int r;
4982
4983 // Compile the "idx" in "var[idx]".
4984 if (new_local)
4985 --cctx->ctx_locals.ga_len;
4986 p = skipwhite(var_start + varlen + 1);
4987 r = compile_expr0(&p, cctx);
4988 if (new_local)
4989 ++cctx->ctx_locals.ga_len;
4990 if (r == FAIL)
4991 goto theend;
4992 if (*skipwhite(p) != ']')
4993 {
4994 emsg(_(e_missbrac));
4995 goto theend;
4996 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004997 if (type == &t_any)
4998 {
4999 type_T *idx_type = ((type_T **)stack->ga_data)[
5000 stack->ga_len - 1];
5001 // Index on variable of unknown type: guess the type from the
5002 // index type: number is dict, otherwise dict.
5003 // TODO: should do the assignment at runtime
5004 if (idx_type->tt_type == VAR_NUMBER)
5005 type = &t_list_any;
5006 else
5007 type = &t_dict_any;
5008 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005009 if (type->tt_type == VAR_DICT
5010 && may_generate_2STRING(-1, cctx) == FAIL)
5011 goto theend;
5012 if (type->tt_type == VAR_LIST
5013 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 {
5016 emsg(_(e_number_exp));
5017 goto theend;
5018 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005019
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 // Load the dict or list. On the stack we then have:
5021 // - value
5022 // - index
5023 // - variable
5024 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005025
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 if (type->tt_type == VAR_LIST)
5027 {
5028 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5029 return FAIL;
5030 }
5031 else if (type->tt_type == VAR_DICT)
5032 {
5033 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5034 return FAIL;
5035 }
5036 else
5037 {
5038 emsg(_(e_listreq));
5039 goto theend;
5040 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005041 }
5042 else
5043 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 switch (dest)
5045 {
5046 case dest_option:
5047 generate_STOREOPT(cctx, name + 1, opt_flags);
5048 break;
5049 case dest_global:
5050 // include g: with the name, easier to execute that way
5051 generate_STORE(cctx, ISN_STOREG, 0, name);
5052 break;
5053 case dest_buffer:
5054 // include b: with the name, easier to execute that way
5055 generate_STORE(cctx, ISN_STOREB, 0, name);
5056 break;
5057 case dest_window:
5058 // include w: with the name, easier to execute that way
5059 generate_STORE(cctx, ISN_STOREW, 0, name);
5060 break;
5061 case dest_tab:
5062 // include t: with the name, easier to execute that way
5063 generate_STORE(cctx, ISN_STORET, 0, name);
5064 break;
5065 case dest_env:
5066 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5067 break;
5068 case dest_reg:
5069 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5070 break;
5071 case dest_vimvar:
5072 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5073 break;
5074 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005075 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005076 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 {
5078 char_u *name_s = name;
5079
5080 // Include s: in the name for store_var()
5081 if (name[1] != ':')
5082 {
5083 int len = (int)STRLEN(name) + 3;
5084
5085 name_s = alloc(len);
5086 if (name_s == NULL)
5087 name_s = name;
5088 else
5089 vim_snprintf((char *)name_s, len,
5090 "s:%s", name);
5091 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005092 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5093 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 if (name_s != name)
5095 vim_free(name_s);
5096 }
5097 else
5098 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005099 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005100 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 break;
5102 case dest_local:
5103 if (lvar != NULL)
5104 {
5105 isn_T *isn = ((isn_T *)instr->ga_data)
5106 + instr->ga_len - 1;
5107
5108 // optimization: turn "var = 123" from ISN_PUSHNR +
5109 // ISN_STORE into ISN_STORENR
5110 if (!lvar->lv_from_outer
5111 && instr->ga_len == instr_count + 1
5112 && isn->isn_type == ISN_PUSHNR)
5113 {
5114 varnumber_T val = isn->isn_arg.number;
5115
5116 isn->isn_type = ISN_STORENR;
5117 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5118 isn->isn_arg.storenr.stnr_val = val;
5119 if (stack->ga_len > 0)
5120 --stack->ga_len;
5121 }
5122 else if (lvar->lv_from_outer)
5123 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005124 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 else
5126 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5127 }
5128 break;
5129 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005130 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131
5132 if (var_idx + 1 < var_count)
5133 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135
5136 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005137 if (var_count > 0 && !semicolon)
5138 {
5139 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5140 goto theend;
5141 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005142
Bram Moolenaarb2097502020-07-19 17:17:02 +02005143 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144
5145theend:
5146 vim_free(name);
5147 return ret;
5148}
5149
5150/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005151 * Check if "name" can be "unlet".
5152 */
5153 int
5154check_vim9_unlet(char_u *name)
5155{
5156 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5157 {
5158 semsg(_("E1081: Cannot unlet %s"), name);
5159 return FAIL;
5160 }
5161 return OK;
5162}
5163
5164/*
5165 * Callback passed to ex_unletlock().
5166 */
5167 static int
5168compile_unlet(
5169 lval_T *lvp,
5170 char_u *name_end,
5171 exarg_T *eap,
5172 int deep UNUSED,
5173 void *coookie)
5174{
5175 cctx_T *cctx = coookie;
5176
5177 if (lvp->ll_tv == NULL)
5178 {
5179 char_u *p = lvp->ll_name;
5180 int cc = *name_end;
5181 int ret = OK;
5182
5183 // Normal name. Only supports g:, w:, t: and b: namespaces.
5184 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005185 if (*p == '$')
5186 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5187 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005188 ret = FAIL;
5189 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005190 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005191
5192 *name_end = cc;
5193 return ret;
5194 }
5195
5196 // TODO: unlet {list}[idx]
5197 // TODO: unlet {dict}[key]
5198 emsg("Sorry, :unlet not fully implemented yet");
5199 return FAIL;
5200}
5201
5202/*
5203 * compile "unlet var", "lock var" and "unlock var"
5204 * "arg" points to "var".
5205 */
5206 static char_u *
5207compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5208{
5209 char_u *p = arg;
5210
5211 if (eap->cmdidx != CMD_unlet)
5212 {
5213 emsg("Sorry, :lock and unlock not implemented yet");
5214 return NULL;
5215 }
5216
5217 if (*p == '!')
5218 {
5219 p = skipwhite(p + 1);
5220 eap->forceit = TRUE;
5221 }
5222
5223 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5224 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5225}
5226
5227/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 * Compile an :import command.
5229 */
5230 static char_u *
5231compile_import(char_u *arg, cctx_T *cctx)
5232{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005233 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234}
5235
5236/*
5237 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5238 */
5239 static int
5240compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5241{
5242 garray_T *instr = &cctx->ctx_instr;
5243 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5244
5245 if (endlabel == NULL)
5246 return FAIL;
5247 endlabel->el_next = *el;
5248 *el = endlabel;
5249 endlabel->el_end_label = instr->ga_len;
5250
5251 generate_JUMP(cctx, when, 0);
5252 return OK;
5253}
5254
5255 static void
5256compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5257{
5258 garray_T *instr = &cctx->ctx_instr;
5259
5260 while (*el != NULL)
5261 {
5262 endlabel_T *cur = (*el);
5263 isn_T *isn;
5264
5265 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5266 isn->isn_arg.jump.jump_where = instr->ga_len;
5267 *el = cur->el_next;
5268 vim_free(cur);
5269 }
5270}
5271
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005272 static void
5273compile_free_jump_to_end(endlabel_T **el)
5274{
5275 while (*el != NULL)
5276 {
5277 endlabel_T *cur = (*el);
5278
5279 *el = cur->el_next;
5280 vim_free(cur);
5281 }
5282}
5283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284/*
5285 * Create a new scope and set up the generic items.
5286 */
5287 static scope_T *
5288new_scope(cctx_T *cctx, scopetype_T type)
5289{
5290 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5291
5292 if (scope == NULL)
5293 return NULL;
5294 scope->se_outer = cctx->ctx_scope;
5295 cctx->ctx_scope = scope;
5296 scope->se_type = type;
5297 scope->se_local_count = cctx->ctx_locals.ga_len;
5298 return scope;
5299}
5300
5301/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005302 * Free the current scope and go back to the outer scope.
5303 */
5304 static void
5305drop_scope(cctx_T *cctx)
5306{
5307 scope_T *scope = cctx->ctx_scope;
5308
5309 if (scope == NULL)
5310 {
5311 iemsg("calling drop_scope() without a scope");
5312 return;
5313 }
5314 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005315 switch (scope->se_type)
5316 {
5317 case IF_SCOPE:
5318 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5319 case FOR_SCOPE:
5320 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5321 case WHILE_SCOPE:
5322 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5323 case TRY_SCOPE:
5324 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5325 case NO_SCOPE:
5326 case BLOCK_SCOPE:
5327 break;
5328 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005329 vim_free(scope);
5330}
5331
5332/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 * compile "if expr"
5334 *
5335 * "if expr" Produces instructions:
5336 * EVAL expr Push result of "expr"
5337 * JUMP_IF_FALSE end
5338 * ... body ...
5339 * end:
5340 *
5341 * "if expr | else" Produces instructions:
5342 * EVAL expr Push result of "expr"
5343 * JUMP_IF_FALSE else
5344 * ... body ...
5345 * JUMP_ALWAYS end
5346 * else:
5347 * ... body ...
5348 * end:
5349 *
5350 * "if expr1 | elseif expr2 | else" Produces instructions:
5351 * EVAL expr Push result of "expr"
5352 * JUMP_IF_FALSE elseif
5353 * ... body ...
5354 * JUMP_ALWAYS end
5355 * elseif:
5356 * EVAL expr Push result of "expr"
5357 * JUMP_IF_FALSE else
5358 * ... body ...
5359 * JUMP_ALWAYS end
5360 * else:
5361 * ... body ...
5362 * end:
5363 */
5364 static char_u *
5365compile_if(char_u *arg, cctx_T *cctx)
5366{
5367 char_u *p = arg;
5368 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005369 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005371 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005372 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
Bram Moolenaara5565e42020-05-09 15:44:01 +02005374 CLEAR_FIELD(ppconst);
5375 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005376 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005377 clear_ppconst(&ppconst);
5378 return NULL;
5379 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005380 if (cctx->ctx_skip == SKIP_YES)
5381 clear_ppconst(&ppconst);
5382 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005383 {
5384 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005385 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005386 clear_ppconst(&ppconst);
5387 }
5388 else
5389 {
5390 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005391 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005392 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005393 return NULL;
5394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395
5396 scope = new_scope(cctx, IF_SCOPE);
5397 if (scope == NULL)
5398 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005399 scope->se_skip_save = skip_save;
5400 // "is_had_return" will be reset if any block does not end in :return
5401 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005403 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005404 {
5405 // "where" is set when ":elseif", "else" or ":endif" is found
5406 scope->se_u.se_if.is_if_label = instr->ga_len;
5407 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5408 }
5409 else
5410 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411
5412 return p;
5413}
5414
5415 static char_u *
5416compile_elseif(char_u *arg, cctx_T *cctx)
5417{
5418 char_u *p = arg;
5419 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005420 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 isn_T *isn;
5422 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005423 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424
5425 if (scope == NULL || scope->se_type != IF_SCOPE)
5426 {
5427 emsg(_(e_elseif_without_if));
5428 return NULL;
5429 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005430 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005431 if (!cctx->ctx_had_return)
5432 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005434 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005435 {
5436 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005438 return NULL;
5439 // previous "if" or "elseif" jumps here
5440 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5441 isn->isn_arg.jump.jump_where = instr->ga_len;
5442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005444 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005445 CLEAR_FIELD(ppconst);
5446 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005447 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005448 clear_ppconst(&ppconst);
5449 return NULL;
5450 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005451 if (scope->se_skip_save == SKIP_YES)
5452 clear_ppconst(&ppconst);
5453 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005454 {
5455 // The expression results in a constant.
5456 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005457 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005458 clear_ppconst(&ppconst);
5459 scope->se_u.se_if.is_if_label = -1;
5460 }
5461 else
5462 {
5463 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005464 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005465 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005466 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005468 // "where" is set when ":elseif", "else" or ":endif" is found
5469 scope->se_u.se_if.is_if_label = instr->ga_len;
5470 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472
5473 return p;
5474}
5475
5476 static char_u *
5477compile_else(char_u *arg, cctx_T *cctx)
5478{
5479 char_u *p = arg;
5480 garray_T *instr = &cctx->ctx_instr;
5481 isn_T *isn;
5482 scope_T *scope = cctx->ctx_scope;
5483
5484 if (scope == NULL || scope->se_type != IF_SCOPE)
5485 {
5486 emsg(_(e_else_without_if));
5487 return NULL;
5488 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005489 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005490 if (!cctx->ctx_had_return)
5491 scope->se_u.se_if.is_had_return = FALSE;
5492 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493
Bram Moolenaarefd88552020-06-18 20:50:10 +02005494 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005495 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005496 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005497 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005498 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005499 if (!cctx->ctx_had_return
5500 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5501 JUMP_ALWAYS, cctx) == FAIL)
5502 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005503 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005504
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005505 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005506 {
5507 if (scope->se_u.se_if.is_if_label >= 0)
5508 {
5509 // previous "if" or "elseif" jumps here
5510 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5511 isn->isn_arg.jump.jump_where = instr->ga_len;
5512 scope->se_u.se_if.is_if_label = -1;
5513 }
5514 }
5515
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005516 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005517 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
5520 return p;
5521}
5522
5523 static char_u *
5524compile_endif(char_u *arg, cctx_T *cctx)
5525{
5526 scope_T *scope = cctx->ctx_scope;
5527 ifscope_T *ifscope;
5528 garray_T *instr = &cctx->ctx_instr;
5529 isn_T *isn;
5530
5531 if (scope == NULL || scope->se_type != IF_SCOPE)
5532 {
5533 emsg(_(e_endif_without_if));
5534 return NULL;
5535 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005536 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005537 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005538 if (!cctx->ctx_had_return)
5539 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005541 if (scope->se_u.se_if.is_if_label >= 0)
5542 {
5543 // previous "if" or "elseif" jumps here
5544 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5545 isn->isn_arg.jump.jump_where = instr->ga_len;
5546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 // Fill in the "end" label in jumps at the end of the blocks.
5548 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005549 cctx->ctx_skip = scope->se_skip_save;
5550
5551 // If all the blocks end in :return and there is an :else then the
5552 // had_return flag is set.
5553 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005555 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 return arg;
5557}
5558
5559/*
5560 * compile "for var in expr"
5561 *
5562 * Produces instructions:
5563 * PUSHNR -1
5564 * STORE loop-idx Set index to -1
5565 * EVAL expr Push result of "expr"
5566 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5567 * - if beyond end, jump to "end"
5568 * - otherwise get item from list and push it
5569 * STORE var Store item in "var"
5570 * ... body ...
5571 * JUMP top Jump back to repeat
5572 * end: DROP Drop the result of "expr"
5573 *
5574 */
5575 static char_u *
5576compile_for(char_u *arg, cctx_T *cctx)
5577{
5578 char_u *p;
5579 size_t varlen;
5580 garray_T *instr = &cctx->ctx_instr;
5581 garray_T *stack = &cctx->ctx_type_stack;
5582 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005583 lvar_T *loop_lvar; // loop iteration variable
5584 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 type_T *vartype;
5586
5587 // TODO: list of variables: "for [key, value] in dict"
5588 // parse "var"
5589 for (p = arg; eval_isnamec1(*p); ++p)
5590 ;
5591 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005592 var_lvar = lookup_local(arg, varlen, cctx);
5593 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005594 {
5595 semsg(_("E1023: variable already defined: %s"), arg);
5596 return NULL;
5597 }
5598
5599 // consume "in"
5600 p = skipwhite(p);
5601 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5602 {
5603 emsg(_(e_missing_in));
5604 return NULL;
5605 }
5606 p = skipwhite(p + 2);
5607
5608
5609 scope = new_scope(cctx, FOR_SCOPE);
5610 if (scope == NULL)
5611 return NULL;
5612
5613 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005614 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5615 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005616 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005617 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005618 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621
5622 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005623 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5624 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005625 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005626 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005627 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005631 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632
5633 // compile "expr", it remains on the stack until "endfor"
5634 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005636 {
5637 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005641 // Now that we know the type of "var", check that it is a list, now or at
5642 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005644 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005646 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 return NULL;
5648 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005649 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005650 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651
5652 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005653 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005655 generate_FOR(cctx, loop_lvar->lv_idx);
5656 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657
5658 return arg;
5659}
5660
5661/*
5662 * compile "endfor"
5663 */
5664 static char_u *
5665compile_endfor(char_u *arg, cctx_T *cctx)
5666{
5667 garray_T *instr = &cctx->ctx_instr;
5668 scope_T *scope = cctx->ctx_scope;
5669 forscope_T *forscope;
5670 isn_T *isn;
5671
5672 if (scope == NULL || scope->se_type != FOR_SCOPE)
5673 {
5674 emsg(_(e_for));
5675 return NULL;
5676 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005677 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005679 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005680
5681 // At end of ":for" scope jump back to the FOR instruction.
5682 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5683
5684 // Fill in the "end" label in the FOR statement so it can jump here
5685 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5686 isn->isn_arg.forloop.for_end = instr->ga_len;
5687
5688 // Fill in the "end" label any BREAK statements
5689 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5690
5691 // Below the ":for" scope drop the "expr" list from the stack.
5692 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5693 return NULL;
5694
5695 vim_free(scope);
5696
5697 return arg;
5698}
5699
5700/*
5701 * compile "while expr"
5702 *
5703 * Produces instructions:
5704 * top: EVAL expr Push result of "expr"
5705 * JUMP_IF_FALSE end jump if false
5706 * ... body ...
5707 * JUMP top Jump back to repeat
5708 * end:
5709 *
5710 */
5711 static char_u *
5712compile_while(char_u *arg, cctx_T *cctx)
5713{
5714 char_u *p = arg;
5715 garray_T *instr = &cctx->ctx_instr;
5716 scope_T *scope;
5717
5718 scope = new_scope(cctx, WHILE_SCOPE);
5719 if (scope == NULL)
5720 return NULL;
5721
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005722 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723
5724 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005725 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 return NULL;
5727
5728 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005729 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730 JUMP_IF_FALSE, cctx) == FAIL)
5731 return FAIL;
5732
5733 return p;
5734}
5735
5736/*
5737 * compile "endwhile"
5738 */
5739 static char_u *
5740compile_endwhile(char_u *arg, cctx_T *cctx)
5741{
5742 scope_T *scope = cctx->ctx_scope;
5743
5744 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5745 {
5746 emsg(_(e_while));
5747 return NULL;
5748 }
5749 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005750 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005751
5752 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005753 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
5755 // Fill in the "end" label in the WHILE statement so it can jump here.
5756 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005757 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758
5759 vim_free(scope);
5760
5761 return arg;
5762}
5763
5764/*
5765 * compile "continue"
5766 */
5767 static char_u *
5768compile_continue(char_u *arg, cctx_T *cctx)
5769{
5770 scope_T *scope = cctx->ctx_scope;
5771
5772 for (;;)
5773 {
5774 if (scope == NULL)
5775 {
5776 emsg(_(e_continue));
5777 return NULL;
5778 }
5779 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5780 break;
5781 scope = scope->se_outer;
5782 }
5783
5784 // Jump back to the FOR or WHILE instruction.
5785 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005786 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5787 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 return arg;
5789}
5790
5791/*
5792 * compile "break"
5793 */
5794 static char_u *
5795compile_break(char_u *arg, cctx_T *cctx)
5796{
5797 scope_T *scope = cctx->ctx_scope;
5798 endlabel_T **el;
5799
5800 for (;;)
5801 {
5802 if (scope == NULL)
5803 {
5804 emsg(_(e_break));
5805 return NULL;
5806 }
5807 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5808 break;
5809 scope = scope->se_outer;
5810 }
5811
5812 // Jump to the end of the FOR or WHILE loop.
5813 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005814 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005816 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5818 return FAIL;
5819
5820 return arg;
5821}
5822
5823/*
5824 * compile "{" start of block
5825 */
5826 static char_u *
5827compile_block(char_u *arg, cctx_T *cctx)
5828{
5829 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5830 return NULL;
5831 return skipwhite(arg + 1);
5832}
5833
5834/*
5835 * compile end of block: drop one scope
5836 */
5837 static void
5838compile_endblock(cctx_T *cctx)
5839{
5840 scope_T *scope = cctx->ctx_scope;
5841
5842 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005843 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 vim_free(scope);
5845}
5846
5847/*
5848 * compile "try"
5849 * Creates a new scope for the try-endtry, pointing to the first catch and
5850 * finally.
5851 * Creates another scope for the "try" block itself.
5852 * TRY instruction sets up exception handling at runtime.
5853 *
5854 * "try"
5855 * TRY -> catch1, -> finally push trystack entry
5856 * ... try block
5857 * "throw {exception}"
5858 * EVAL {exception}
5859 * THROW create exception
5860 * ... try block
5861 * " catch {expr}"
5862 * JUMP -> finally
5863 * catch1: PUSH exeception
5864 * EVAL {expr}
5865 * MATCH
5866 * JUMP nomatch -> catch2
5867 * CATCH remove exception
5868 * ... catch block
5869 * " catch"
5870 * JUMP -> finally
5871 * catch2: CATCH remove exception
5872 * ... catch block
5873 * " finally"
5874 * finally:
5875 * ... finally block
5876 * " endtry"
5877 * ENDTRY pop trystack entry, may rethrow
5878 */
5879 static char_u *
5880compile_try(char_u *arg, cctx_T *cctx)
5881{
5882 garray_T *instr = &cctx->ctx_instr;
5883 scope_T *try_scope;
5884 scope_T *scope;
5885
5886 // scope that holds the jumps that go to catch/finally/endtry
5887 try_scope = new_scope(cctx, TRY_SCOPE);
5888 if (try_scope == NULL)
5889 return NULL;
5890
5891 // "catch" is set when the first ":catch" is found.
5892 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005893 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 if (generate_instr(cctx, ISN_TRY) == NULL)
5895 return NULL;
5896
5897 // scope for the try block itself
5898 scope = new_scope(cctx, BLOCK_SCOPE);
5899 if (scope == NULL)
5900 return NULL;
5901
5902 return arg;
5903}
5904
5905/*
5906 * compile "catch {expr}"
5907 */
5908 static char_u *
5909compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5910{
5911 scope_T *scope = cctx->ctx_scope;
5912 garray_T *instr = &cctx->ctx_instr;
5913 char_u *p;
5914 isn_T *isn;
5915
5916 // end block scope from :try or :catch
5917 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5918 compile_endblock(cctx);
5919 scope = cctx->ctx_scope;
5920
5921 // Error if not in a :try scope
5922 if (scope == NULL || scope->se_type != TRY_SCOPE)
5923 {
5924 emsg(_(e_catch));
5925 return NULL;
5926 }
5927
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005928 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929 {
5930 emsg(_("E1033: catch unreachable after catch-all"));
5931 return NULL;
5932 }
5933
5934 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005935 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 JUMP_ALWAYS, cctx) == FAIL)
5937 return NULL;
5938
5939 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005940 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 if (isn->isn_arg.try.try_catch == 0)
5942 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005943 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 {
5945 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005946 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 isn->isn_arg.jump.jump_where = instr->ga_len;
5948 }
5949
5950 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005951 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005953 scope->se_u.se_try.ts_caught_all = TRUE;
5954 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 }
5956 else
5957 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005958 char_u *end;
5959 char_u *pat;
5960 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005961 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005962 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964 // Push v:exception, push {expr} and MATCH
5965 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5966
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005967 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005968 if (*end != *p)
5969 {
5970 semsg(_("E1067: Separator mismatch: %s"), p);
5971 vim_free(tofree);
5972 return FAIL;
5973 }
5974 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005975 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005976 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005977 len = (int)(end - tofree);
5978 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005979 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005980 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005981 if (pat == NULL)
5982 return FAIL;
5983 if (generate_PUSHS(cctx, pat) == FAIL)
5984 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5987 return NULL;
5988
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5991 return NULL;
5992 }
5993
5994 if (generate_instr(cctx, ISN_CATCH) == NULL)
5995 return NULL;
5996
5997 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5998 return NULL;
5999 return p;
6000}
6001
6002 static char_u *
6003compile_finally(char_u *arg, cctx_T *cctx)
6004{
6005 scope_T *scope = cctx->ctx_scope;
6006 garray_T *instr = &cctx->ctx_instr;
6007 isn_T *isn;
6008
6009 // end block scope from :try or :catch
6010 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6011 compile_endblock(cctx);
6012 scope = cctx->ctx_scope;
6013
6014 // Error if not in a :try scope
6015 if (scope == NULL || scope->se_type != TRY_SCOPE)
6016 {
6017 emsg(_(e_finally));
6018 return NULL;
6019 }
6020
6021 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 if (isn->isn_arg.try.try_finally != 0)
6024 {
6025 emsg(_(e_finally_dup));
6026 return NULL;
6027 }
6028
6029 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006030 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031
Bram Moolenaar585fea72020-04-02 22:33:21 +02006032 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 {
6035 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006036 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006038 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 }
6040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 // TODO: set index in ts_finally_label jumps
6042
6043 return arg;
6044}
6045
6046 static char_u *
6047compile_endtry(char_u *arg, cctx_T *cctx)
6048{
6049 scope_T *scope = cctx->ctx_scope;
6050 garray_T *instr = &cctx->ctx_instr;
6051 isn_T *isn;
6052
6053 // end block scope from :catch or :finally
6054 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6055 compile_endblock(cctx);
6056 scope = cctx->ctx_scope;
6057
6058 // Error if not in a :try scope
6059 if (scope == NULL || scope->se_type != TRY_SCOPE)
6060 {
6061 if (scope == NULL)
6062 emsg(_(e_no_endtry));
6063 else if (scope->se_type == WHILE_SCOPE)
6064 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006065 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 emsg(_(e_endfor));
6067 else
6068 emsg(_(e_endif));
6069 return NULL;
6070 }
6071
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006072 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6074 {
6075 emsg(_("E1032: missing :catch or :finally"));
6076 return NULL;
6077 }
6078
6079 // Fill in the "end" label in jumps at the end of the blocks, if not done
6080 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006081 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
6083 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006084 if (isn->isn_arg.try.try_catch == 0)
6085 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 if (isn->isn_arg.try.try_finally == 0)
6087 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006088
6089 if (scope->se_u.se_try.ts_catch_label != 0)
6090 {
6091 // Last catch without match jumps here
6092 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6093 isn->isn_arg.jump.jump_where = instr->ga_len;
6094 }
6095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 compile_endblock(cctx);
6097
6098 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6099 return NULL;
6100 return arg;
6101}
6102
6103/*
6104 * compile "throw {expr}"
6105 */
6106 static char_u *
6107compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6108{
6109 char_u *p = skipwhite(arg);
6110
Bram Moolenaara5565e42020-05-09 15:44:01 +02006111 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 return NULL;
6113 if (may_generate_2STRING(-1, cctx) == FAIL)
6114 return NULL;
6115 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6116 return NULL;
6117
6118 return p;
6119}
6120
6121/*
6122 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006123 * compile "echomsg expr"
6124 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006125 * compile "execute expr"
6126 */
6127 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006128compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006129{
6130 char_u *p = arg;
6131 int count = 0;
6132
6133 for (;;)
6134 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006135 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006136 return NULL;
6137 ++count;
6138 p = skipwhite(p);
6139 if (ends_excmd(*p))
6140 break;
6141 }
6142
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006143 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6144 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6145 else if (cmdidx == CMD_execute)
6146 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6147 else if (cmdidx == CMD_echomsg)
6148 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6149 else
6150 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 return p;
6152}
6153
6154/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006155 * A command that is not compiled, execute with legacy code.
6156 */
6157 static char_u *
6158compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6159{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006160 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006161 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006162 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006163
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006164 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006165 goto theend;
6166
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006167 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006168 {
6169 long argt = excmd_get_argt(eap->cmdidx);
6170 int usefilter = FALSE;
6171
6172 has_expr = argt & (EX_XFILE | EX_EXPAND);
6173
6174 // If the command can be followed by a bar, find the bar and truncate
6175 // it, so that the following command can be compiled.
6176 // The '|' is overwritten with a NUL, it is put back below.
6177 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6178 && *eap->arg == '!')
6179 // :w !filter or :r !filter or :r! filter
6180 usefilter = TRUE;
6181 if ((argt & EX_TRLBAR) && !usefilter)
6182 {
6183 separate_nextcmd(eap);
6184 if (eap->nextcmd != NULL)
6185 nextcmd = eap->nextcmd;
6186 }
6187 }
6188
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006189 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6190 {
6191 // expand filename in "syntax include [@group] filename"
6192 has_expr = TRUE;
6193 eap->arg = skipwhite(eap->arg + 7);
6194 if (*eap->arg == '@')
6195 eap->arg = skiptowhite(eap->arg);
6196 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006197
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006198 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006199 {
6200 int count = 0;
6201 char_u *start = skipwhite(line);
6202
6203 // :cmd xxx`=expr1`yyy`=expr2`zzz
6204 // PUSHS ":cmd xxx"
6205 // eval expr1
6206 // PUSHS "yyy"
6207 // eval expr2
6208 // PUSHS "zzz"
6209 // EXECCONCAT 5
6210 for (;;)
6211 {
6212 if (p > start)
6213 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006214 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006215 ++count;
6216 }
6217 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006218 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006219 return NULL;
6220 may_generate_2STRING(-1, cctx);
6221 ++count;
6222 p = skipwhite(p);
6223 if (*p != '`')
6224 {
6225 emsg(_("E1083: missing backtick"));
6226 return NULL;
6227 }
6228 start = p + 1;
6229
6230 p = (char_u *)strstr((char *)start, "`=");
6231 if (p == NULL)
6232 {
6233 if (*skipwhite(start) != NUL)
6234 {
6235 generate_PUSHS(cctx, vim_strsave(start));
6236 ++count;
6237 }
6238 break;
6239 }
6240 }
6241 generate_EXECCONCAT(cctx, count);
6242 }
6243 else
6244 generate_EXEC(cctx, line);
6245
6246theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006247 if (*nextcmd != NUL)
6248 {
6249 // the parser expects a pointer to the bar, put it back
6250 --nextcmd;
6251 *nextcmd = '|';
6252 }
6253
6254 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006255}
6256
6257/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006258 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006259 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006260 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006261 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006262add_def_function(ufunc_T *ufunc)
6263{
6264 dfunc_T *dfunc;
6265
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006266 if (def_functions.ga_len == 0)
6267 {
6268 // The first position is not used, so that a zero uf_dfunc_idx means it
6269 // wasn't set.
6270 if (ga_grow(&def_functions, 1) == FAIL)
6271 return FAIL;
6272 ++def_functions.ga_len;
6273 }
6274
Bram Moolenaar09689a02020-05-09 22:50:08 +02006275 // Add the function to "def_functions".
6276 if (ga_grow(&def_functions, 1) == FAIL)
6277 return FAIL;
6278 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6279 CLEAR_POINTER(dfunc);
6280 dfunc->df_idx = def_functions.ga_len;
6281 ufunc->uf_dfunc_idx = dfunc->df_idx;
6282 dfunc->df_ufunc = ufunc;
6283 ++def_functions.ga_len;
6284 return OK;
6285}
6286
6287/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 * After ex_function() has collected all the function lines: parse and compile
6289 * the lines into instructions.
6290 * Adds the function to "def_functions".
6291 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6292 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006293 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006294 * This can be used recursively through compile_lambda(), which may reallocate
6295 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006296 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006298 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006299compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 char_u *line = NULL;
6302 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 cctx_T cctx;
6305 garray_T *instr;
6306 int called_emsg_before = called_emsg;
6307 int ret = FAIL;
6308 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006309 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006310 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006311 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006313 // When using a function that was compiled before: Free old instructions.
6314 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006315 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006317 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6318 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006319 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006321 else
6322 {
6323 if (add_def_function(ufunc) == FAIL)
6324 return FAIL;
6325 new_def_function = TRUE;
6326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327
Bram Moolenaar985116a2020-07-12 17:31:09 +02006328 ufunc->uf_def_status = UF_COMPILING;
6329
Bram Moolenaara80faa82020-04-12 19:37:17 +02006330 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 cctx.ctx_ufunc = ufunc;
6332 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006333 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6335 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6336 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6337 cctx.ctx_type_list = &ufunc->uf_type_list;
6338 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6339 instr = &cctx.ctx_instr;
6340
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006341 // Set the context to the function, it may be compiled when called from
6342 // another script. Set the script version to the most modern one.
6343 // The line number will be set in next_line_from_context().
6344 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6346
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006347 // Make sure error messages are OK.
6348 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6349 if (do_estack_push)
6350 estack_push_ufunc(ufunc, 1);
6351
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006352 if (ufunc->uf_def_args.ga_len > 0)
6353 {
6354 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006355 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006356 int i;
6357 char_u *arg;
6358 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006359 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006360
6361 // Produce instructions for the default values of optional arguments.
6362 // Store the instruction index in uf_def_arg_idx[] so that we know
6363 // where to start when the function is called, depending on the number
6364 // of arguments.
6365 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6366 if (ufunc->uf_def_arg_idx == NULL)
6367 goto erret;
6368 for (i = 0; i < count; ++i)
6369 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006370 garray_T *stack = &cctx.ctx_type_stack;
6371 type_T *val_type;
6372 int arg_idx = first_def_arg + i;
6373
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006374 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6375 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006376 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006377 goto erret;
6378
6379 // If no type specified use the type of the default value.
6380 // Otherwise check that the default value type matches the
6381 // specified type.
6382 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6383 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006384 {
6385 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006386 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006387 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006388 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006389 == FAIL)
6390 {
6391 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6392 arg_idx + 1);
6393 goto erret;
6394 }
6395
6396 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006397 goto erret;
6398 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006399 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006400
6401 if (did_set_arg_type)
6402 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006403 }
6404
6405 /*
6406 * Loop over all the lines of the function and generate instructions.
6407 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 for (;;)
6409 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006410 exarg_T ea;
6411 cmdmod_T save_cmdmod;
6412 int starts_with_colon = FALSE;
6413 char_u *cmd;
6414 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006415
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006416 // Bail out on the first error to avoid a flood of errors and report
6417 // the right line number when inside try/catch.
6418 if (emsg_before != called_emsg)
6419 goto erret;
6420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 if (line != NULL && *line == '|')
6422 // the line continues after a '|'
6423 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006424 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006425 && !(*line == '#' && (line == cctx.ctx_line_start
6426 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006428 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 goto erret;
6430 }
6431 else
6432 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006433 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006434 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006435 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006438 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439
Bram Moolenaara80faa82020-04-12 19:37:17 +02006440 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 ea.cmdlinep = &line;
6442 ea.cmd = skipwhite(line);
6443
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006444 // Some things can be recognized by the first character.
6445 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006447 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006448 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006449 if (ea.cmd[1] != '{')
6450 {
6451 line = (char_u *)"";
6452 continue;
6453 }
6454 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006456 case '}':
6457 {
6458 // "}" ends a block scope
6459 scopetype_T stype = cctx.ctx_scope == NULL
6460 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006462 if (stype == BLOCK_SCOPE)
6463 {
6464 compile_endblock(&cctx);
6465 line = ea.cmd;
6466 }
6467 else
6468 {
6469 emsg(_("E1025: using } outside of a block scope"));
6470 goto erret;
6471 }
6472 if (line != NULL)
6473 line = skipwhite(ea.cmd + 1);
6474 continue;
6475 }
6476
6477 case '{':
6478 // "{" starts a block scope
6479 // "{'a': 1}->func() is something else
6480 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6481 {
6482 line = compile_block(ea.cmd, &cctx);
6483 continue;
6484 }
6485 break;
6486
6487 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006488 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006489 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 }
6491
6492 /*
6493 * COMMAND MODIFIERS
6494 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006495 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6497 {
6498 if (errormsg != NULL)
6499 goto erret;
6500 // empty line or comment
6501 line = (char_u *)"";
6502 continue;
6503 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006504 // TODO: use modifiers in the command
6505 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006506 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507
6508 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006509 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006511 {
6512 if (*ea.cmd == '(')
6513 // not for "call()"
6514 ea.cmd = p;
6515 else
6516 ea.cmd = skipwhite(ea.cmd);
6517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006519 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006521 char_u *pskip;
6522
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006523 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006524 // find what follows.
6525 // Skip over "var.member", "var[idx]" and the like.
6526 // Also "&opt = val", "$ENV = val" and "@r = val".
6527 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006528 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006529 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006530 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006532 char_u *var_end;
6533 int oplen;
6534 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535
Bram Moolenaar65821722020-08-02 18:58:54 +02006536 if (ea.cmd[0] == '@')
6537 var_end = ea.cmd + 2;
6538 else
6539 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006540 FNE_CHECK_START | FNE_INCL_BR);
6541 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006542 if (oplen > 0)
6543 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006544 size_t len = p - ea.cmd;
6545
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006546 // Recognize an assignment if we recognize the variable
6547 // name:
6548 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006549 // "local = expr" where "local" is a local var.
6550 // "script = expr" where "script" is a script-local var.
6551 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006552 // "&opt = expr"
6553 // "$ENV = expr"
6554 // "@r = expr"
6555 if (*ea.cmd == '&'
6556 || *ea.cmd == '$'
6557 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006558 || ((len) > 2 && ea.cmd[1] == ':')
6559 || lookup_local(ea.cmd, len, &cctx) != NULL
6560 || lookup_arg(ea.cmd, len, NULL, NULL,
6561 NULL, &cctx) == OK
6562 || lookup_script(ea.cmd, len) == OK
6563 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006564 {
6565 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006566 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006567 goto erret;
6568 continue;
6569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 }
6571 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006572
6573 if (*ea.cmd == '[')
6574 {
6575 // [var, var] = expr
6576 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6577 if (line == NULL)
6578 goto erret;
6579 if (line != ea.cmd)
6580 continue;
6581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 }
6583
6584 /*
6585 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006586 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006588 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006589 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006590 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006591 ea.cmd = skip_range(ea.cmd, NULL);
6592 if (ea.cmd > cmd && !starts_with_colon)
6593 {
6594 emsg(_(e_colon_required));
6595 goto erret;
6596 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006597 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006598 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006599 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006600 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601
6602 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6603 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006604 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006605 {
6606 line += STRLEN(line);
6607 continue;
6608 }
6609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006611 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006613 // CMD_let cannot happen, compile_assignment() above is used
6614 iemsg("Command from find_ex_command() not handled");
6615 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 }
6618
6619 p = skipwhite(p);
6620
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006621 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006622 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006623 && ea.cmdidx != CMD_elseif
6624 && ea.cmdidx != CMD_else
6625 && ea.cmdidx != CMD_endif)
6626 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006627 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006628 continue;
6629 }
6630
Bram Moolenaarefd88552020-06-18 20:50:10 +02006631 if (ea.cmdidx != CMD_elseif
6632 && ea.cmdidx != CMD_else
6633 && ea.cmdidx != CMD_endif
6634 && ea.cmdidx != CMD_endfor
6635 && ea.cmdidx != CMD_endwhile
6636 && ea.cmdidx != CMD_catch
6637 && ea.cmdidx != CMD_finally
6638 && ea.cmdidx != CMD_endtry)
6639 {
6640 if (cctx.ctx_had_return)
6641 {
6642 emsg(_("E1095: Unreachable code after :return"));
6643 goto erret;
6644 }
6645 }
6646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 switch (ea.cmdidx)
6648 {
6649 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006650 ea.arg = p;
6651 line = compile_nested_function(&ea, &cctx);
6652 break;
6653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006655 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 goto erret;
6657
6658 case CMD_return:
6659 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006660 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 break;
6662
6663 case CMD_let:
6664 case CMD_const:
6665 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006666 if (line == p)
6667 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 break;
6669
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006670 case CMD_unlet:
6671 case CMD_unlockvar:
6672 case CMD_lockvar:
6673 line = compile_unletlock(p, &ea, &cctx);
6674 break;
6675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 case CMD_import:
6677 line = compile_import(p, &cctx);
6678 break;
6679
6680 case CMD_if:
6681 line = compile_if(p, &cctx);
6682 break;
6683 case CMD_elseif:
6684 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006685 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 break;
6687 case CMD_else:
6688 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006689 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 break;
6691 case CMD_endif:
6692 line = compile_endif(p, &cctx);
6693 break;
6694
6695 case CMD_while:
6696 line = compile_while(p, &cctx);
6697 break;
6698 case CMD_endwhile:
6699 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006700 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 break;
6702
6703 case CMD_for:
6704 line = compile_for(p, &cctx);
6705 break;
6706 case CMD_endfor:
6707 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006708 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 break;
6710 case CMD_continue:
6711 line = compile_continue(p, &cctx);
6712 break;
6713 case CMD_break:
6714 line = compile_break(p, &cctx);
6715 break;
6716
6717 case CMD_try:
6718 line = compile_try(p, &cctx);
6719 break;
6720 case CMD_catch:
6721 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006722 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 break;
6724 case CMD_finally:
6725 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006726 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 break;
6728 case CMD_endtry:
6729 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006730 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 break;
6732 case CMD_throw:
6733 line = compile_throw(p, &cctx);
6734 break;
6735
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006736 case CMD_eval:
6737 if (compile_expr0(&p, &cctx) == FAIL)
6738 goto erret;
6739
6740 // drop the return value
6741 generate_instr_drop(&cctx, ISN_DROP, 1);
6742
6743 line = skipwhite(p);
6744 break;
6745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006748 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006749 case CMD_echomsg:
6750 case CMD_echoerr:
6751 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006752 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006754 // TODO: other commands with an expression argument
6755
Bram Moolenaarae616492020-07-28 20:07:27 +02006756 case CMD_append:
6757 case CMD_change:
6758 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006759 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006760 case CMD_xit:
6761 not_in_vim9(&ea);
6762 goto erret;
6763
Bram Moolenaar002262f2020-07-08 17:47:57 +02006764 case CMD_SIZE:
6765 semsg(_("E476: Invalid command: %s"), ea.cmd);
6766 goto erret;
6767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006769 // Not recognized, execute with do_cmdline_cmd().
6770 ea.arg = p;
6771 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 break;
6773 }
6774 if (line == NULL)
6775 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006776 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777
6778 if (cctx.ctx_type_stack.ga_len < 0)
6779 {
6780 iemsg("Type stack underflow");
6781 goto erret;
6782 }
6783 }
6784
6785 if (cctx.ctx_scope != NULL)
6786 {
6787 if (cctx.ctx_scope->se_type == IF_SCOPE)
6788 emsg(_(e_endif));
6789 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6790 emsg(_(e_endwhile));
6791 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6792 emsg(_(e_endfor));
6793 else
6794 emsg(_("E1026: Missing }"));
6795 goto erret;
6796 }
6797
Bram Moolenaarefd88552020-06-18 20:50:10 +02006798 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 {
6800 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6801 {
6802 emsg(_("E1027: Missing return statement"));
6803 goto erret;
6804 }
6805
6806 // Return zero if there is no return at the end.
6807 generate_PUSHNR(&cctx, 0);
6808 generate_instr(&cctx, ISN_RETURN);
6809 }
6810
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006811 {
6812 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6813 + ufunc->uf_dfunc_idx;
6814 dfunc->df_deleted = FALSE;
6815 dfunc->df_instr = instr->ga_data;
6816 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006817 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006818 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006819 if (cctx.ctx_outer_used)
6820 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006821 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823
6824 ret = OK;
6825
6826erret:
6827 if (ret == FAIL)
6828 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006829 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006830 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6831 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006832
6833 for (idx = 0; idx < instr->ga_len; ++idx)
6834 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006836
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006837 // If using the last entry in the table and it was added above, we
6838 // might as well remove it.
6839 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006840 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006841 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006842 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006843 ufunc->uf_dfunc_idx = 0;
6844 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006845 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006846
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006847 while (cctx.ctx_scope != NULL)
6848 drop_scope(&cctx);
6849
Bram Moolenaar20431c92020-03-20 18:39:46 +01006850 // Don't execute this function body.
6851 ga_clear_strings(&ufunc->uf_lines);
6852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 if (errormsg != NULL)
6854 emsg(errormsg);
6855 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006856 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 }
6858
6859 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006860 if (do_estack_push)
6861 estack_pop();
6862
Bram Moolenaar20431c92020-03-20 18:39:46 +01006863 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006864 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006866 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867}
6868
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006869 void
6870set_function_type(ufunc_T *ufunc)
6871{
6872 int varargs = ufunc->uf_va_name != NULL;
6873 int argcount = ufunc->uf_args.ga_len;
6874
6875 // Create a type for the function, with the return type and any
6876 // argument types.
6877 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6878 // The type is included in "tt_args".
6879 if (argcount > 0 || varargs)
6880 {
6881 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6882 argcount, &ufunc->uf_type_list);
6883 // Add argument types to the function type.
6884 if (func_type_add_arg_types(ufunc->uf_func_type,
6885 argcount + varargs,
6886 &ufunc->uf_type_list) == FAIL)
6887 return;
6888 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6889 ufunc->uf_func_type->tt_min_argcount =
6890 argcount - ufunc->uf_def_args.ga_len;
6891 if (ufunc->uf_arg_types == NULL)
6892 {
6893 int i;
6894
6895 // lambda does not have argument types.
6896 for (i = 0; i < argcount; ++i)
6897 ufunc->uf_func_type->tt_args[i] = &t_any;
6898 }
6899 else
6900 mch_memmove(ufunc->uf_func_type->tt_args,
6901 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6902 if (varargs)
6903 {
6904 ufunc->uf_func_type->tt_args[argcount] =
6905 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6906 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6907 }
6908 }
6909 else
6910 // No arguments, can use a predefined type.
6911 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6912 argcount, &ufunc->uf_type_list);
6913}
6914
6915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916/*
6917 * Delete an instruction, free what it contains.
6918 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006919 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920delete_instr(isn_T *isn)
6921{
6922 switch (isn->isn_type)
6923 {
6924 case ISN_EXEC:
6925 case ISN_LOADENV:
6926 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006927 case ISN_LOADB:
6928 case ISN_LOADW:
6929 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006931 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 case ISN_PUSHEXC:
6933 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006934 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006936 case ISN_STOREB:
6937 case ISN_STOREW:
6938 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006939 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 vim_free(isn->isn_arg.string);
6941 break;
6942
6943 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006944 case ISN_STORES:
6945 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 break;
6947
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006948 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006949 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006950 vim_free(isn->isn_arg.unlet.ul_name);
6951 break;
6952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 case ISN_STOREOPT:
6954 vim_free(isn->isn_arg.storeopt.so_name);
6955 break;
6956
6957 case ISN_PUSHBLOB: // push blob isn_arg.blob
6958 blob_unref(isn->isn_arg.blob);
6959 break;
6960
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006961 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006962#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006963 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006964#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006965 break;
6966
6967 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006968#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006969 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006970#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006971 break;
6972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 case ISN_UCALL:
6974 vim_free(isn->isn_arg.ufunc.cuf_name);
6975 break;
6976
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006977 case ISN_FUNCREF:
6978 {
6979 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6980 + isn->isn_arg.funcref.fr_func;
6981 func_ptr_unref(dfunc->df_ufunc);
6982 }
6983 break;
6984
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006985 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02006986 {
6987 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
6988 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
6989
6990 if (ufunc != NULL)
6991 {
6992 // Clear uf_dfunc_idx so that the function is deleted.
6993 clear_def_function(ufunc);
6994 ufunc->uf_dfunc_idx = 0;
6995 func_ptr_unref(ufunc);
6996 }
6997
6998 vim_free(lambda);
6999 vim_free(isn->isn_arg.newfunc.nf_global);
7000 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007001 break;
7002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 case ISN_2BOOL:
7004 case ISN_2STRING:
7005 case ISN_ADDBLOB:
7006 case ISN_ADDLIST:
7007 case ISN_BCALL:
7008 case ISN_CATCH:
7009 case ISN_CHECKNR:
7010 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007011 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 case ISN_COMPAREANY:
7013 case ISN_COMPAREBLOB:
7014 case ISN_COMPAREBOOL:
7015 case ISN_COMPAREDICT:
7016 case ISN_COMPAREFLOAT:
7017 case ISN_COMPAREFUNC:
7018 case ISN_COMPARELIST:
7019 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 case ISN_COMPARESPECIAL:
7021 case ISN_COMPARESTRING:
7022 case ISN_CONCAT:
7023 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007024 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 case ISN_DROP:
7026 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007027 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007028 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007030 case ISN_EXECCONCAT:
7031 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007033 case ISN_LISTINDEX:
7034 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007036 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007037 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 case ISN_JUMP:
7039 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007040 case ISN_LOADBDICT:
7041 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007042 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007044 case ISN_LOADSCRIPT:
7045 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007047 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 case ISN_NEGATENR:
7049 case ISN_NEWDICT:
7050 case ISN_NEWLIST:
7051 case ISN_OPNR:
7052 case ISN_OPFLOAT:
7053 case ISN_OPANY:
7054 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007055 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 case ISN_PUSHF:
7057 case ISN_PUSHNR:
7058 case ISN_PUSHBOOL:
7059 case ISN_PUSHSPEC:
7060 case ISN_RETURN:
7061 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007062 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007063 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007065 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007067 case ISN_STOREDICT:
7068 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 case ISN_THROW:
7070 case ISN_TRY:
7071 // nothing allocated
7072 break;
7073 }
7074}
7075
7076/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007077 * Free all instructions for "dfunc".
7078 */
7079 static void
7080delete_def_function_contents(dfunc_T *dfunc)
7081{
7082 int idx;
7083
7084 ga_clear(&dfunc->df_def_args_isn);
7085
7086 if (dfunc->df_instr != NULL)
7087 {
7088 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7089 delete_instr(dfunc->df_instr + idx);
7090 VIM_CLEAR(dfunc->df_instr);
7091 }
7092
7093 dfunc->df_deleted = TRUE;
7094}
7095
7096/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007097 * When a user function is deleted, clear the contents of any associated def
7098 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 */
7100 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007101clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007103 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 {
7105 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7106 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
Bram Moolenaar20431c92020-03-20 18:39:46 +01007108 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007109 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 }
7111}
7112
7113#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007114/*
7115 * Free all functions defined with ":def".
7116 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 void
7118free_def_functions(void)
7119{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007120 int idx;
7121
7122 for (idx = 0; idx < def_functions.ga_len; ++idx)
7123 {
7124 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7125
7126 delete_def_function_contents(dfunc);
7127 }
7128
7129 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130}
7131#endif
7132
7133
7134#endif // FEAT_EVAL