blob: 99f8e22cfe9d5555f5c50a107b9d8c6b6884d21f [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.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200377 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 */
379 static int
380may_generate_2STRING(int offset, cctx_T *cctx)
381{
382 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 garray_T *stack = &cctx->ctx_type_stack;
385 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
386
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200387 switch ((*type)->tt_type)
388 {
389 // nothing to be done
390 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200392 // conversion possible
393 case VAR_SPECIAL:
394 case VAR_BOOL:
395 case VAR_NUMBER:
396 case VAR_FLOAT:
397 break;
398
399 // conversion possible (with runtime check)
400 case VAR_ANY:
401 case VAR_UNKNOWN:
402 isntype = ISN_2STRING_ANY;
403 break;
404
405 // conversion not possible
406 case VAR_VOID:
407 case VAR_BLOB:
408 case VAR_FUNC:
409 case VAR_PARTIAL:
410 case VAR_LIST:
411 case VAR_DICT:
412 case VAR_JOB:
413 case VAR_CHANNEL:
414 to_string_error((*type)->tt_type);
415 return FAIL;
416 }
417
418 *type = &t_string;
419 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 return FAIL;
421 isn->isn_arg.number = offset;
422
423 return OK;
424}
425
426 static int
427check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
428{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200429 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200431 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 {
433 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200434 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 else
436 semsg(_("E1036: %c requires number or float arguments"), *op);
437 return FAIL;
438 }
439 return OK;
440}
441
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200442 static int
443generate_add_instr(
444 cctx_T *cctx,
445 vartype_T vartype,
446 type_T *type1,
447 type_T *type2)
448{
449 isn_T *isn = generate_instr_drop(cctx,
450 vartype == VAR_NUMBER ? ISN_OPNR
451 : vartype == VAR_LIST ? ISN_ADDLIST
452 : vartype == VAR_BLOB ? ISN_ADDBLOB
453#ifdef FEAT_FLOAT
454 : vartype == VAR_FLOAT ? ISN_OPFLOAT
455#endif
456 : ISN_OPANY, 1);
457
458 if (vartype != VAR_LIST && vartype != VAR_BLOB
459 && type1->tt_type != VAR_ANY
460 && type2->tt_type != VAR_ANY
461 && check_number_or_float(
462 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
463 return FAIL;
464
465 if (isn != NULL)
466 isn->isn_arg.op.op_type = EXPR_ADD;
467 return isn == NULL ? FAIL : OK;
468}
469
470/*
471 * Get the type to use for an instruction for an operation on "type1" and
472 * "type2". If they are matching use a type-specific instruction. Otherwise
473 * fall back to runtime type checking.
474 */
475 static vartype_T
476operator_type(type_T *type1, type_T *type2)
477{
478 if (type1->tt_type == type2->tt_type
479 && (type1->tt_type == VAR_NUMBER
480 || type1->tt_type == VAR_LIST
481#ifdef FEAT_FLOAT
482 || type1->tt_type == VAR_FLOAT
483#endif
484 || type1->tt_type == VAR_BLOB))
485 return type1->tt_type;
486 return VAR_ANY;
487}
488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489/*
490 * Generate an instruction with two arguments. The instruction depends on the
491 * type of the arguments.
492 */
493 static int
494generate_two_op(cctx_T *cctx, char_u *op)
495{
496 garray_T *stack = &cctx->ctx_type_stack;
497 type_T *type1;
498 type_T *type2;
499 vartype_T vartype;
500 isn_T *isn;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_OK_IF_SKIP(cctx);
503
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200504 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
506 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200507 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508
509 switch (*op)
510 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200511 case '+':
512 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 break;
515
516 case '-':
517 case '*':
518 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
519 op) == FAIL)
520 return FAIL;
521 if (vartype == VAR_NUMBER)
522 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
523#ifdef FEAT_FLOAT
524 else if (vartype == VAR_FLOAT)
525 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
526#endif
527 else
528 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
529 if (isn != NULL)
530 isn->isn_arg.op.op_type = *op == '*'
531 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
532 break;
533
Bram Moolenaar4c683752020-04-05 21:38:23 +0200534 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200536 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 && type2->tt_type != VAR_NUMBER))
538 {
539 emsg(_("E1035: % requires number arguments"));
540 return FAIL;
541 }
542 isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
544 if (isn != NULL)
545 isn->isn_arg.op.op_type = EXPR_REM;
546 break;
547 }
548
549 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 {
552 type_T *type = &t_any;
553
554#ifdef FEAT_FLOAT
555 // float+number and number+float results in float
556 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
557 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
558 type = &t_float;
559#endif
560 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
561 }
562
563 return OK;
564}
565
566/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200567 * Get the instruction to use for comparing "type1" with "type2"
568 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200570 static isntype_T
571get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572{
573 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574
Bram Moolenaar4c683752020-04-05 21:38:23 +0200575 if (type1 == VAR_UNKNOWN)
576 type1 = VAR_ANY;
577 if (type2 == VAR_UNKNOWN)
578 type2 = VAR_ANY;
579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (type1 == type2)
581 {
582 switch (type1)
583 {
584 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
585 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
586 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
587 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
588 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
589 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
590 case VAR_LIST: isntype = ISN_COMPARELIST; break;
591 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
592 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 default: isntype = ISN_COMPAREANY; break;
594 }
595 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200596 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
598 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
599 isntype = ISN_COMPAREANY;
600
601 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
602 && (isntype == ISN_COMPAREBOOL
603 || isntype == ISN_COMPARESPECIAL
604 || isntype == ISN_COMPARENR
605 || isntype == ISN_COMPAREFLOAT))
606 {
607 semsg(_("E1037: Cannot use \"%s\" with %s"),
608 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200609 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 }
611 if (isntype == ISN_DROP
612 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
613 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
614 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
615 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
616 && exptype != EXPR_IS && exptype != EXPR_ISNOT
617 && (type1 == VAR_BLOB || type2 == VAR_BLOB
618 || type1 == VAR_LIST || type2 == VAR_LIST))))
619 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100620 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200624 return isntype;
625}
626
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200627 int
628check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
629{
630 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
631 return FAIL;
632 return OK;
633}
634
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635/*
636 * Generate an ISN_COMPARE* instruction with a boolean result.
637 */
638 static int
639generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
640{
641 isntype_T isntype;
642 isn_T *isn;
643 garray_T *stack = &cctx->ctx_type_stack;
644 vartype_T type1;
645 vartype_T type2;
646
647 RETURN_OK_IF_SKIP(cctx);
648
649 // Get the known type of the two items on the stack. If they are matching
650 // use a type-specific instruction. Otherwise fall back to runtime type
651 // checking.
652 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
653 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
654 isntype = get_compare_isn(exptype, type1, type2);
655 if (isntype == ISN_DROP)
656 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 if ((isn = generate_instr(cctx, isntype)) == NULL)
659 return FAIL;
660 isn->isn_arg.op.op_type = exptype;
661 isn->isn_arg.op.op_ic = ic;
662
663 // takes two arguments, puts one bool back
664 if (stack->ga_len >= 2)
665 {
666 --stack->ga_len;
667 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
668 }
669
670 return OK;
671}
672
673/*
674 * Generate an ISN_2BOOL instruction.
675 */
676 static int
677generate_2BOOL(cctx_T *cctx, int invert)
678{
679 isn_T *isn;
680 garray_T *stack = &cctx->ctx_type_stack;
681
Bram Moolenaar080457c2020-03-03 21:53:32 +0100682 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
684 return FAIL;
685 isn->isn_arg.number = invert;
686
687 // type becomes bool
688 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
689
690 return OK;
691}
692
693 static int
694generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
695{
696 isn_T *isn;
697 garray_T *stack = &cctx->ctx_type_stack;
698
Bram Moolenaar080457c2020-03-03 21:53:32 +0100699 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
701 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200702 // TODO: whole type, e.g. for a function also arg and return types
703 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 isn->isn_arg.type.ct_off = offset;
705
706 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200707 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708
709 return OK;
710}
711
712/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200713 * Check that
714 * - "actual" is "expected" type or
715 * - "actual" is a type that can be "expected" type: add a runtime check; or
716 * - return FAIL.
717 */
718 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200719need_type(
720 type_T *actual,
721 type_T *expected,
722 int offset,
723 cctx_T *cctx,
724 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200725{
726 if (check_type(expected, actual, FALSE) == OK)
727 return OK;
728 if (actual->tt_type != VAR_ANY
729 && actual->tt_type != VAR_UNKNOWN
730 && !(actual->tt_type == VAR_FUNC
731 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
732 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200733 if (!silent)
734 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200735 return FAIL;
736 }
737 generate_TYPECHECK(cctx, expected, offset);
738 return OK;
739}
740
741/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 * Generate an ISN_PUSHNR instruction.
743 */
744 static int
745generate_PUSHNR(cctx_T *cctx, varnumber_T number)
746{
747 isn_T *isn;
748
Bram Moolenaar080457c2020-03-03 21:53:32 +0100749 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
751 return FAIL;
752 isn->isn_arg.number = number;
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_PUSHBOOL instruction.
759 */
760 static int
761generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
762{
763 isn_T *isn;
764
Bram Moolenaar080457c2020-03-03 21:53:32 +0100765 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
767 return FAIL;
768 isn->isn_arg.number = number;
769
770 return OK;
771}
772
773/*
774 * Generate an ISN_PUSHSPEC instruction.
775 */
776 static int
777generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
778{
779 isn_T *isn;
780
Bram Moolenaar080457c2020-03-03 21:53:32 +0100781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
783 return FAIL;
784 isn->isn_arg.number = number;
785
786 return OK;
787}
788
789#ifdef FEAT_FLOAT
790/*
791 * Generate an ISN_PUSHF instruction.
792 */
793 static int
794generate_PUSHF(cctx_T *cctx, float_T fnumber)
795{
796 isn_T *isn;
797
Bram Moolenaar080457c2020-03-03 21:53:32 +0100798 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
800 return FAIL;
801 isn->isn_arg.fnumber = fnumber;
802
803 return OK;
804}
805#endif
806
807/*
808 * Generate an ISN_PUSHS instruction.
809 * Consumes "str".
810 */
811 static int
812generate_PUSHS(cctx_T *cctx, char_u *str)
813{
814 isn_T *isn;
815
Bram Moolenaar080457c2020-03-03 21:53:32 +0100816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
818 return FAIL;
819 isn->isn_arg.string = str;
820
821 return OK;
822}
823
824/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100825 * Generate an ISN_PUSHCHANNEL instruction.
826 * Consumes "channel".
827 */
828 static int
829generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
830{
831 isn_T *isn;
832
Bram Moolenaar080457c2020-03-03 21:53:32 +0100833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100834 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
835 return FAIL;
836 isn->isn_arg.channel = channel;
837
838 return OK;
839}
840
841/*
842 * Generate an ISN_PUSHJOB instruction.
843 * Consumes "job".
844 */
845 static int
846generate_PUSHJOB(cctx_T *cctx, job_T *job)
847{
848 isn_T *isn;
849
Bram Moolenaar080457c2020-03-03 21:53:32 +0100850 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100851 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100852 return FAIL;
853 isn->isn_arg.job = job;
854
855 return OK;
856}
857
858/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 * Generate an ISN_PUSHBLOB instruction.
860 * Consumes "blob".
861 */
862 static int
863generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
864{
865 isn_T *isn;
866
Bram Moolenaar080457c2020-03-03 21:53:32 +0100867 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
869 return FAIL;
870 isn->isn_arg.blob = blob;
871
872 return OK;
873}
874
875/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100876 * Generate an ISN_PUSHFUNC instruction with name "name".
877 * Consumes "name".
878 */
879 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200880generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100881{
882 isn_T *isn;
883
Bram Moolenaar080457c2020-03-03 21:53:32 +0100884 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200885 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100886 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200887 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100888
889 return OK;
890}
891
892/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200893 * Generate an ISN_GETITEM instruction with "index".
894 */
895 static int
896generate_GETITEM(cctx_T *cctx, int index)
897{
898 isn_T *isn;
899 garray_T *stack = &cctx->ctx_type_stack;
900 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
901 type_T *item_type = &t_any;
902
903 RETURN_OK_IF_SKIP(cctx);
904
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200905 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200906 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200907 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200908 emsg(_(e_listreq));
909 return FAIL;
910 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200911 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
913 return FAIL;
914 isn->isn_arg.number = index;
915
916 // add the item type to the type stack
917 if (ga_grow(stack, 1) == FAIL)
918 return FAIL;
919 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
920 ++stack->ga_len;
921 return OK;
922}
923
924/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200925 * Generate an ISN_SLICE instruction with "count".
926 */
927 static int
928generate_SLICE(cctx_T *cctx, int count)
929{
930 isn_T *isn;
931
932 RETURN_OK_IF_SKIP(cctx);
933 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
934 return FAIL;
935 isn->isn_arg.number = count;
936 return OK;
937}
938
939/*
940 * Generate an ISN_CHECKLEN instruction with "min_len".
941 */
942 static int
943generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
944{
945 isn_T *isn;
946
947 RETURN_OK_IF_SKIP(cctx);
948
949 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
950 return FAIL;
951 isn->isn_arg.checklen.cl_min_len = min_len;
952 isn->isn_arg.checklen.cl_more_OK = more_OK;
953
954 return OK;
955}
956
957/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 * Generate an ISN_STORE instruction.
959 */
960 static int
961generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
962{
963 isn_T *isn;
964
Bram Moolenaar080457c2020-03-03 21:53:32 +0100965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
967 return FAIL;
968 if (name != NULL)
969 isn->isn_arg.string = vim_strsave(name);
970 else
971 isn->isn_arg.number = idx;
972
973 return OK;
974}
975
976/*
977 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
978 */
979 static int
980generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
986 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100987 isn->isn_arg.storenr.stnr_idx = idx;
988 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989
990 return OK;
991}
992
993/*
994 * Generate an ISN_STOREOPT instruction
995 */
996 static int
997generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001002 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 return FAIL;
1004 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1005 isn->isn_arg.storeopt.so_flags = opt_flags;
1006
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_LOAD or similar instruction.
1012 */
1013 static int
1014generate_LOAD(
1015 cctx_T *cctx,
1016 isntype_T isn_type,
1017 int idx,
1018 char_u *name,
1019 type_T *type)
1020{
1021 isn_T *isn;
1022
Bram Moolenaar080457c2020-03-03 21:53:32 +01001023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1025 return FAIL;
1026 if (name != NULL)
1027 isn->isn_arg.string = vim_strsave(name);
1028 else
1029 isn->isn_arg.number = idx;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001035 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001036 */
1037 static int
1038generate_LOADV(
1039 cctx_T *cctx,
1040 char_u *name,
1041 int error)
1042{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001043 int di_flags;
1044 int vidx = find_vim_var(name, &di_flags);
1045 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001048 if (vidx < 0)
1049 {
1050 if (error)
1051 semsg(_(e_var_notfound), name);
1052 return FAIL;
1053 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001054 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001056 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001057}
1058
1059/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001060 * Generate an ISN_UNLET instruction.
1061 */
1062 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001063generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001064{
1065 isn_T *isn;
1066
1067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001068 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001069 return FAIL;
1070 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1071 isn->isn_arg.unlet.ul_forceit = forceit;
1072
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 * Generate an ISN_LOADS instruction.
1078 */
1079 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001080generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001084 int sid,
1085 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086{
1087 isn_T *isn;
1088
Bram Moolenaar080457c2020-03-03 21:53:32 +01001089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 if (isn_type == ISN_LOADS)
1091 isn = generate_instr_type(cctx, isn_type, type);
1092 else
1093 isn = generate_instr_drop(cctx, isn_type, 1);
1094 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001096 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1097 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
1099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1104 */
1105 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 cctx_T *cctx,
1108 isntype_T isn_type,
1109 int sid,
1110 int idx,
1111 type_T *type)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if (isn_type == ISN_LOADSCRIPT)
1117 isn = generate_instr_type(cctx, isn_type, type);
1118 else
1119 isn = generate_instr_drop(cctx, isn_type, 1);
1120 if (isn == NULL)
1121 return FAIL;
1122 isn->isn_arg.script.script_sid = sid;
1123 isn->isn_arg.script.script_idx = idx;
1124 return OK;
1125}
1126
1127/*
1128 * Generate an ISN_NEWLIST instruction.
1129 */
1130 static int
1131generate_NEWLIST(cctx_T *cctx, int count)
1132{
1133 isn_T *isn;
1134 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 type_T *type;
1136 type_T *member;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.number = count;
1142
Bram Moolenaar127542b2020-08-09 17:22:04 +02001143 // get the member type from all the items on the stack.
1144 member = get_member_type_from_stack(
1145 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1146 cctx->ctx_type_list);
1147 type = get_list_type(member, cctx->ctx_type_list);
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 // drop the value types
1150 stack->ga_len -= count;
1151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 // add the list type to the type stack
1153 if (ga_grow(stack, 1) == FAIL)
1154 return FAIL;
1155 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1156 ++stack->ga_len;
1157
1158 return OK;
1159}
1160
1161/*
1162 * Generate an ISN_NEWDICT instruction.
1163 */
1164 static int
1165generate_NEWDICT(cctx_T *cctx, int count)
1166{
1167 isn_T *isn;
1168 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 type_T *type;
1170 type_T *member;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = count;
1176
Bram Moolenaar127542b2020-08-09 17:22:04 +02001177 member = get_member_type_from_stack(
1178 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1179 cctx->ctx_type_list);
1180 type = get_dict_type(member, cctx->ctx_type_list);
1181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 // drop the key and value types
1183 stack->ga_len -= 2 * count;
1184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 // add the dict type to the type stack
1186 if (ga_grow(stack, 1) == FAIL)
1187 return FAIL;
1188 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1189 ++stack->ga_len;
1190
1191 return OK;
1192}
1193
1194/*
1195 * Generate an ISN_FUNCREF instruction.
1196 */
1197 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001198generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199{
1200 isn_T *isn;
1201 garray_T *stack = &cctx->ctx_type_stack;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1205 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001206 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001207 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208
1209 if (ga_grow(stack, 1) == FAIL)
1210 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001211 ((type_T **)stack->ga_data)[stack->ga_len] =
1212 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 ++stack->ga_len;
1214
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001219 * Generate an ISN_NEWFUNC instruction.
1220 */
1221 static int
1222generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1223{
1224 isn_T *isn;
1225 char_u *name;
1226
1227 RETURN_OK_IF_SKIP(cctx);
1228 name = vim_strsave(lambda_name);
1229 if (name == NULL)
1230 return FAIL;
1231 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1232 return FAIL;
1233 isn->isn_arg.newfunc.nf_lambda = name;
1234 isn->isn_arg.newfunc.nf_global = func_name;
1235
1236 return OK;
1237}
1238
1239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 * Generate an ISN_JUMP instruction.
1241 */
1242 static int
1243generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1244{
1245 isn_T *isn;
1246 garray_T *stack = &cctx->ctx_type_stack;
1247
Bram Moolenaar080457c2020-03-03 21:53:32 +01001248 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.jump.jump_when = when;
1252 isn->isn_arg.jump.jump_where = where;
1253
1254 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1255 --stack->ga_len;
1256
1257 return OK;
1258}
1259
1260 static int
1261generate_FOR(cctx_T *cctx, int loop_idx)
1262{
1263 isn_T *isn;
1264 garray_T *stack = &cctx->ctx_type_stack;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.forloop.for_idx = loop_idx;
1270
1271 if (ga_grow(stack, 1) == FAIL)
1272 return FAIL;
1273 // type doesn't matter, will be stored next
1274 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1275 ++stack->ga_len;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001282 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 * Return FAIL if the number of arguments is wrong.
1284 */
1285 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001286generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287{
1288 isn_T *isn;
1289 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001290 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001291 type_T *argtypes[MAX_FUNC_ARGS];
1292 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001295 argoff = check_internal_func(func_idx, argcount);
1296 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 return FAIL;
1298
Bram Moolenaar389df252020-07-09 21:20:47 +02001299 if (method_call && argoff > 1)
1300 {
1301 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1302 return FAIL;
1303 isn->isn_arg.shuffle.shfl_item = argcount;
1304 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1305 }
1306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.bfunc.cbf_idx = func_idx;
1310 isn->isn_arg.bfunc.cbf_argcount = argcount;
1311
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001312 for (i = 0; i < argcount; ++i)
1313 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 stack->ga_len -= argcount; // drop the arguments
1316 if (ga_grow(stack, 1) == FAIL)
1317 return FAIL;
1318 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001319 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 ++stack->ga_len; // add return value
1321
1322 return OK;
1323}
1324
1325/*
1326 * Generate an ISN_DCALL or ISN_UCALL instruction.
1327 * Return FAIL if the number of arguments is wrong.
1328 */
1329 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001330generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001335 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
Bram Moolenaar080457c2020-03-03 21:53:32 +01001337 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 if (argcount > regular_args && !has_varargs(ufunc))
1339 {
1340 semsg(_(e_toomanyarg), ufunc->uf_name);
1341 return FAIL;
1342 }
1343 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1344 {
1345 semsg(_(e_toofewarg), ufunc->uf_name);
1346 return FAIL;
1347 }
1348
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001349 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001350 {
1351 int i;
1352
1353 for (i = 0; i < argcount; ++i)
1354 {
1355 type_T *expected;
1356 type_T *actual;
1357
1358 if (i < regular_args)
1359 {
1360 if (ufunc->uf_arg_types == NULL)
1361 continue;
1362 expected = ufunc->uf_arg_types[i];
1363 }
1364 else
1365 expected = ufunc->uf_va_type->tt_member;
1366 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001367 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001368 {
1369 arg_type_mismatch(expected, actual, i + 1);
1370 return FAIL;
1371 }
1372 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001373 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001374 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1375 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001376 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001377 }
1378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001380 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001381 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001383 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 {
1385 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1386 isn->isn_arg.dfunc.cdf_argcount = argcount;
1387 }
1388 else
1389 {
1390 // A user function may be deleted and redefined later, can't use the
1391 // ufunc pointer, need to look it up again at runtime.
1392 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1393 isn->isn_arg.ufunc.cuf_argcount = argcount;
1394 }
1395
1396 stack->ga_len -= argcount; // drop the arguments
1397 if (ga_grow(stack, 1) == FAIL)
1398 return FAIL;
1399 // add return value
1400 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1401 ++stack->ga_len;
1402
1403 return OK;
1404}
1405
1406/*
1407 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1408 */
1409 static int
1410generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1411{
1412 isn_T *isn;
1413 garray_T *stack = &cctx->ctx_type_stack;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1417 return FAIL;
1418 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1419 isn->isn_arg.ufunc.cuf_argcount = argcount;
1420
1421 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001422 if (ga_grow(stack, 1) == FAIL)
1423 return FAIL;
1424 // add return value
1425 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1426 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
1428 return OK;
1429}
1430
1431/*
1432 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001433 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 */
1435 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001436generate_PCALL(
1437 cctx_T *cctx,
1438 int argcount,
1439 char_u *name,
1440 type_T *type,
1441 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446
Bram Moolenaar080457c2020-03-03 21:53:32 +01001447 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001448
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001449 if (type->tt_type == VAR_ANY)
1450 ret_type = &t_any;
1451 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001452 {
1453 if (type->tt_argcount != -1)
1454 {
1455 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1456
1457 if (argcount < type->tt_min_argcount - varargs)
1458 {
1459 semsg(_(e_toofewarg), "[reference]");
1460 return FAIL;
1461 }
1462 if (!varargs && argcount > type->tt_argcount)
1463 {
1464 semsg(_(e_toomanyarg), "[reference]");
1465 return FAIL;
1466 }
1467 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001468 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001469 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001470 else
1471 {
1472 semsg(_("E1085: Not a callable type: %s"), name);
1473 return FAIL;
1474 }
1475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1477 return FAIL;
1478 isn->isn_arg.pfunc.cpf_top = at_top;
1479 isn->isn_arg.pfunc.cpf_argcount = argcount;
1480
1481 stack->ga_len -= argcount; // drop the arguments
1482
1483 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001484 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001486 // If partial is above the arguments it must be cleared and replaced with
1487 // the return value.
1488 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1489 return FAIL;
1490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 return OK;
1492}
1493
1494/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001495 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 */
1497 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001498generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499{
1500 isn_T *isn;
1501 garray_T *stack = &cctx->ctx_type_stack;
1502 type_T *type;
1503
Bram Moolenaar080457c2020-03-03 21:53:32 +01001504 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001505 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001507 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001509 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001511 if (type->tt_type != VAR_DICT && type != &t_any)
1512 {
1513 emsg(_(e_dictreq));
1514 return FAIL;
1515 }
1516 // change dict type to dict member type
1517 if (type->tt_type == VAR_DICT)
1518 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519
1520 return OK;
1521}
1522
1523/*
1524 * Generate an ISN_ECHO instruction.
1525 */
1526 static int
1527generate_ECHO(cctx_T *cctx, int with_white, int count)
1528{
1529 isn_T *isn;
1530
Bram Moolenaar080457c2020-03-03 21:53:32 +01001531 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1533 return FAIL;
1534 isn->isn_arg.echo.echo_with_white = with_white;
1535 isn->isn_arg.echo.echo_count = count;
1536
1537 return OK;
1538}
1539
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001541 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001542 */
1543 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545{
1546 isn_T *isn;
1547
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549 return FAIL;
1550 isn->isn_arg.number = count;
1551
1552 return OK;
1553}
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 static int
1556generate_EXEC(cctx_T *cctx, char_u *line)
1557{
1558 isn_T *isn;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.string = vim_strsave(line);
1564 return OK;
1565}
1566
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001567 static int
1568generate_EXECCONCAT(cctx_T *cctx, int count)
1569{
1570 isn_T *isn;
1571
1572 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.number = count;
1575 return OK;
1576}
1577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578/*
1579 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001580 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001582 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1584{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 lvar_T *lvar;
1586
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001587 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001589 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001590 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 }
1592
1593 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001594 return NULL;
1595 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 // Every local variable uses the next entry on the stack. We could re-use
1598 // the last ones when leaving a scope, but then variables used in a closure
1599 // might get overwritten. To keep things simple do not re-use stack
1600 // entries. This is less efficient, but memory is cheap these days.
1601 lvar->lv_idx = cctx->ctx_locals_count++;
1602
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001603 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 lvar->lv_const = isConst;
1605 lvar->lv_type = type;
1606
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001607 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608}
1609
1610/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001611 * Remove local variables above "new_top".
1612 */
1613 static void
1614unwind_locals(cctx_T *cctx, int new_top)
1615{
1616 if (cctx->ctx_locals.ga_len > new_top)
1617 {
1618 int idx;
1619 lvar_T *lvar;
1620
1621 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1622 {
1623 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1624 vim_free(lvar->lv_name);
1625 }
1626 }
1627 cctx->ctx_locals.ga_len = new_top;
1628}
1629
1630/*
1631 * Free all local variables.
1632 */
1633 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001635{
1636 unwind_locals(cctx, 0);
1637 ga_clear(&cctx->ctx_locals);
1638}
1639
1640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 * Find "name" in script-local items of script "sid".
1642 * Returns the index in "sn_var_vals" if found.
1643 * If found but not in "sn_var_vals" returns -1.
1644 * If not found returns -2.
1645 */
1646 int
1647get_script_item_idx(int sid, char_u *name, int check_writable)
1648{
1649 hashtab_T *ht;
1650 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001651 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 int idx;
1653
1654 // First look the name up in the hashtable.
1655 if (sid <= 0 || sid > script_items.ga_len)
1656 return -1;
1657 ht = &SCRIPT_VARS(sid);
1658 di = find_var_in_ht(ht, 0, name, TRUE);
1659 if (di == NULL)
1660 return -2;
1661
1662 // Now find the svar_T index in sn_var_vals.
1663 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1664 {
1665 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1666
1667 if (sv->sv_tv == &di->di_tv)
1668 {
1669 if (check_writable && sv->sv_const)
1670 semsg(_(e_readonlyvar), name);
1671 return idx;
1672 }
1673 }
1674 return -1;
1675}
1676
1677/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001678 * Find "name" in imported items of the current script or in "cctx" if not
1679 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 */
1681 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001682find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 int idx;
1685
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001686 if (current_sctx.sc_sid <= 0)
1687 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 if (cctx != NULL)
1689 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1690 {
1691 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1692 + idx;
1693
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001694 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1695 : STRLEN(import->imp_name) == len
1696 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 return import;
1698 }
1699
Bram Moolenaarefa94442020-08-08 22:16:00 +02001700 return find_imported_in_script(name, len, current_sctx.sc_sid);
1701}
1702
1703 imported_T *
1704find_imported_in_script(char_u *name, size_t len, int sid)
1705{
1706 scriptitem_T *si = SCRIPT_ITEM(sid);
1707 int idx;
1708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1710 {
1711 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1712
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001713 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1714 : STRLEN(import->imp_name) == len
1715 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 return import;
1717 }
1718 return NULL;
1719}
1720
1721/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001722 * Free all imported variables.
1723 */
1724 static void
1725free_imported(cctx_T *cctx)
1726{
1727 int idx;
1728
1729 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1730 {
1731 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1732
1733 vim_free(import->imp_name);
1734 }
1735 ga_clear(&cctx->ctx_imports);
1736}
1737
1738/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001739 * Return TRUE if "p" points at a "#" but not at "#{".
1740 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001741 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001742vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001743{
1744 return p[0] == '#' && p[1] != '{';
1745}
1746
1747/*
1748 * Return a pointer to the next line that isn't empty or only contains a
1749 * comment. Skips over white space.
1750 * Returns NULL if there is none.
1751 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001752 char_u *
1753peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001754{
1755 int lnum = cctx->ctx_lnum;
1756
1757 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1758 {
1759 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001760 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001761
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001762 // ignore NULLs inserted for continuation lines
1763 if (line != NULL)
1764 {
1765 p = skipwhite(line);
1766 if (*p != NUL && !vim9_comment_start(p))
1767 return p;
1768 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001769 }
1770 return NULL;
1771}
1772
1773/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001774 * Called when checking for a following operator at "arg". When the rest of
1775 * the line is empty or only a comment, peek the next line. If there is a next
1776 * line return a pointer to it and set "nextp".
1777 * Otherwise skip over white space.
1778 */
1779 static char_u *
1780may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1781{
1782 char_u *p = skipwhite(arg);
1783
1784 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001785 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001786 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001787 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001788 if (*nextp != NULL)
1789 return *nextp;
1790 }
1791 return p;
1792}
1793
1794/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001795 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001796 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001797 * Returns NULL when at the end.
1798 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001799 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001800next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001801{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001802 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001803
1804 do
1805 {
1806 ++cctx->ctx_lnum;
1807 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001808 {
1809 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001810 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001811 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001812 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001813 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001814 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001815 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001816 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001817 return line;
1818}
1819
1820/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001821 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001822 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001823 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1824 */
1825 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001826may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001827{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001828 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001829 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001830 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001831
1832 if (next == NULL)
1833 return FAIL;
1834 *arg = skipwhite(next);
1835 }
1836 return OK;
1837}
1838
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001839/*
1840 * Idem, and give an error when failed.
1841 */
1842 static int
1843may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1844{
1845 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1846 {
1847 emsg(_("E1097: line incomplete"));
1848 return FAIL;
1849 }
1850 return OK;
1851}
1852
1853
Bram Moolenaara5565e42020-05-09 15:44:01 +02001854// Structure passed between the compile_expr* functions to keep track of
1855// constants that have been parsed but for which no code was produced yet. If
1856// possible expressions on these constants are applied at compile time. If
1857// that is not possible, the code to push the constants needs to be generated
1858// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001859// Using 50 should be more than enough of 5 levels of ().
1860#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001861typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001862 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001863 int pp_used; // active entries in pp_tv[]
1864} ppconst_T;
1865
Bram Moolenaar1c747212020-05-09 18:28:34 +02001866static int compile_expr0(char_u **arg, cctx_T *cctx);
1867static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1868
Bram Moolenaara5565e42020-05-09 15:44:01 +02001869/*
1870 * Generate a PUSH instruction for "tv".
1871 * "tv" will be consumed or cleared.
1872 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1873 */
1874 static int
1875generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1876{
1877 if (tv != NULL)
1878 {
1879 switch (tv->v_type)
1880 {
1881 case VAR_UNKNOWN:
1882 break;
1883 case VAR_BOOL:
1884 generate_PUSHBOOL(cctx, tv->vval.v_number);
1885 break;
1886 case VAR_SPECIAL:
1887 generate_PUSHSPEC(cctx, tv->vval.v_number);
1888 break;
1889 case VAR_NUMBER:
1890 generate_PUSHNR(cctx, tv->vval.v_number);
1891 break;
1892#ifdef FEAT_FLOAT
1893 case VAR_FLOAT:
1894 generate_PUSHF(cctx, tv->vval.v_float);
1895 break;
1896#endif
1897 case VAR_BLOB:
1898 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1899 tv->vval.v_blob = NULL;
1900 break;
1901 case VAR_STRING:
1902 generate_PUSHS(cctx, tv->vval.v_string);
1903 tv->vval.v_string = NULL;
1904 break;
1905 default:
1906 iemsg("constant type not supported");
1907 clear_tv(tv);
1908 return FAIL;
1909 }
1910 tv->v_type = VAR_UNKNOWN;
1911 }
1912 return OK;
1913}
1914
1915/*
1916 * Generate code for any ppconst entries.
1917 */
1918 static int
1919generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1920{
1921 int i;
1922 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001923 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001924
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001925 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001926 for (i = 0; i < ppconst->pp_used; ++i)
1927 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1928 ret = FAIL;
1929 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001930 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001931 return ret;
1932}
1933
1934/*
1935 * Clear ppconst constants. Used when failing.
1936 */
1937 static void
1938clear_ppconst(ppconst_T *ppconst)
1939{
1940 int i;
1941
1942 for (i = 0; i < ppconst->pp_used; ++i)
1943 clear_tv(&ppconst->pp_tv[i]);
1944 ppconst->pp_used = 0;
1945}
1946
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001947/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001948 * Generate an instruction to load script-local variable "name", without the
1949 * leading "s:".
1950 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 */
1952 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001953compile_load_scriptvar(
1954 cctx_T *cctx,
1955 char_u *name, // variable NUL terminated
1956 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001957 char_u **end, // end of variable
1958 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001960 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1962 imported_T *import;
1963
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001964 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001966 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001967 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1968 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 }
1970 if (idx >= 0)
1971 {
1972 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1973
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001974 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 current_sctx.sc_sid, idx, sv->sv_type);
1976 return OK;
1977 }
1978
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001979 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if (import != NULL)
1981 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001982 if (import->imp_all)
1983 {
1984 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001985 char_u *exp_name;
1986 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001987 ufunc_T *ufunc;
1988 type_T *type;
1989
1990 // Used "import * as Name", need to lookup the member.
1991 if (*p != '.')
1992 {
1993 semsg(_("E1060: expected dot after name: %s"), start);
1994 return FAIL;
1995 }
1996 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001997 if (VIM_ISWHITE(*p))
1998 {
1999 emsg(_("E1074: no white space allowed after dot"));
2000 return FAIL;
2001 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002002
Bram Moolenaar1c991142020-07-04 13:15:31 +02002003 // isolate one name
2004 exp_name = p;
2005 while (eval_isnamec(*p))
2006 ++p;
2007 cc = *p;
2008 *p = NUL;
2009
2010 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2011 *p = cc;
2012 p = skipwhite(p);
2013
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002014 // TODO: what if it is a function?
2015 if (idx < 0)
2016 return FAIL;
2017 *end = p;
2018
2019 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2020 import->imp_sid,
2021 idx,
2022 type);
2023 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002024 else if (import->imp_funcname != NULL)
2025 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002026 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002027 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2028 import->imp_sid,
2029 import->imp_var_vals_idx,
2030 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 return OK;
2032 }
2033
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002034 if (error)
2035 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 return FAIL;
2037}
2038
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 static int
2040generate_funcref(cctx_T *cctx, char_u *name)
2041{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002042 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002043
2044 if (ufunc == NULL)
2045 return FAIL;
2046
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002047 // Need to compile any default values to get the argument types.
2048 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2049 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2050 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002051 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002052}
2053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054/*
2055 * Compile a variable name into a load instruction.
2056 * "end" points to just after the name.
2057 * When "error" is FALSE do not give an error when not found.
2058 */
2059 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002060compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061{
2062 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002063 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002064 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002066 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067
2068 if (*(*arg + 1) == ':')
2069 {
2070 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002071 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002072 {
2073 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002075 switch (**arg)
2076 {
2077 case 'g': isn_type = ISN_LOADGDICT; break;
2078 case 'w': isn_type = ISN_LOADWDICT; break;
2079 case 't': isn_type = ISN_LOADTDICT; break;
2080 case 'b': isn_type = ISN_LOADBDICT; break;
2081 default:
2082 semsg(_(e_namespace), *arg);
2083 goto theend;
2084 }
2085 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2086 goto theend;
2087 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 else
2090 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002091 isntype_T isn_type = ISN_DROP;
2092
2093 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2094 if (name == NULL)
2095 return FAIL;
2096
2097 switch (**arg)
2098 {
2099 case 'v': res = generate_LOADV(cctx, name, error);
2100 break;
2101 case 's': res = compile_load_scriptvar(cctx, name,
2102 NULL, NULL, error);
2103 break;
2104 case 'g': isn_type = ISN_LOADG; break;
2105 case 'w': isn_type = ISN_LOADW; break;
2106 case 't': isn_type = ISN_LOADT; break;
2107 case 'b': isn_type = ISN_LOADB; break;
2108 default: semsg(_(e_namespace), *arg);
2109 goto theend;
2110 }
2111 if (isn_type != ISN_DROP)
2112 {
2113 // Global, Buffer-local, Window-local and Tabpage-local
2114 // variables can be defined later, thus we don't check if it
2115 // exists, give error at runtime.
2116 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 }
2120 else
2121 {
2122 size_t len = end - *arg;
2123 int idx;
2124 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002125 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126
2127 name = vim_strnsave(*arg, end - *arg);
2128 if (name == NULL)
2129 return FAIL;
2130
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002131 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002133 if (!gen_load_outer)
2134 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 }
2136 else
2137 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002138 lvar_T *lvar = lookup_local(*arg, len, cctx);
2139
2140 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002142 type = lvar->lv_type;
2143 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002144 if (lvar->lv_from_outer)
2145 gen_load_outer = TRUE;
2146 else
2147 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 }
2149 else
2150 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002151 // "var" can be script-local even without using "s:" if it
2152 // already exists.
2153 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2154 == SCRIPT_VERSION_VIM9
2155 || lookup_script(*arg, len) == OK)
2156 res = compile_load_scriptvar(cctx, name, *arg, &end,
2157 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002158
Bram Moolenaara5565e42020-05-09 15:44:01 +02002159 // When the name starts with an uppercase letter or "x:" it
2160 // can be a user defined function.
2161 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2162 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 }
2164 }
2165 if (gen_load)
2166 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002167 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002168 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002169 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002170 cctx->ctx_outer_used = TRUE;
2171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 }
2173
2174 *arg = end;
2175
2176theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002177 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 semsg(_(e_var_notfound), name);
2179 vim_free(name);
2180 return res;
2181}
2182
2183/*
2184 * Compile the argument expressions.
2185 * "arg" points to just after the "(" and is advanced to after the ")"
2186 */
2187 static int
2188compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2189{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002190 char_u *p = *arg;
2191 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192
Bram Moolenaare6085c52020-04-12 20:19:16 +02002193 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002195 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2196 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002197 if (*p == ')')
2198 {
2199 *arg = p + 1;
2200 return OK;
2201 }
2202
Bram Moolenaara5565e42020-05-09 15:44:01 +02002203 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 return FAIL;
2205 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002206
2207 if (*p != ',' && *skipwhite(p) == ',')
2208 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002209 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002210 p = skipwhite(p);
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 {
2214 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002215 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002216 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002217 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002218 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002219 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002221failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002222 emsg(_(e_missing_close));
2223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224}
2225
2226/*
2227 * Compile a function call: name(arg1, arg2)
2228 * "arg" points to "name", "arg + varlen" to the "(".
2229 * "argcount_init" is 1 for "value->method()"
2230 * Instructions:
2231 * EVAL arg1
2232 * EVAL arg2
2233 * BCALL / DCALL / UCALL
2234 */
2235 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002236compile_call(
2237 char_u **arg,
2238 size_t varlen,
2239 cctx_T *cctx,
2240 ppconst_T *ppconst,
2241 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242{
2243 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002244 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 int argcount = argcount_init;
2246 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002247 char_u fname_buf[FLEN_FIXED + 1];
2248 char_u *tofree = NULL;
2249 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002251 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002252 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253
Bram Moolenaara5565e42020-05-09 15:44:01 +02002254 // we can evaluate "has('name')" at compile time
2255 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2256 {
2257 char_u *s = skipwhite(*arg + varlen + 1);
2258 typval_T argvars[2];
2259
2260 argvars[0].v_type = VAR_UNKNOWN;
2261 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002262 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002264 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002265 s = skipwhite(s);
2266 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2267 {
2268 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2269
2270 *arg = s + 1;
2271 argvars[1].v_type = VAR_UNKNOWN;
2272 tv->v_type = VAR_NUMBER;
2273 tv->vval.v_number = 0;
2274 f_has(argvars, tv);
2275 clear_tv(&argvars[0]);
2276 ++ppconst->pp_used;
2277 return OK;
2278 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002279 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002280 }
2281
2282 if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 if (varlen >= sizeof(namebuf))
2286 {
2287 semsg(_("E1011: name too long: %s"), name);
2288 return FAIL;
2289 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 vim_strncpy(namebuf, *arg, varlen);
2291 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292
2293 *arg = skipwhite(*arg + varlen + 1);
2294 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
Bram Moolenaara1773442020-08-12 15:21:22 +02002297 is_autoload = vim_strchr(name, '#') != NULL;
2298 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 {
2300 int idx;
2301
2302 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002303 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002305 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306 else
2307 semsg(_(e_unknownfunc), namebuf);
2308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 }
2310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002312 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002314 {
2315 res = generate_CALL(cctx, ufunc, argcount);
2316 goto theend;
2317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318
2319 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002320 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002321 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002323 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002324 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002325 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002326 garray_T *stack = &cctx->ctx_type_stack;
2327 type_T *type;
2328
2329 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2330 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002331 goto theend;
2332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002334 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002335 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002336 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002337 res = generate_UCALL(cctx, name, argcount);
2338 else
2339 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340
2341theend:
2342 vim_free(tofree);
2343 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344}
2345
2346// like NAMESPACE_CHAR but with 'a' and 'l'.
2347#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2348
2349/*
2350 * Find the end of a variable or function name. Unlike find_name_end() this
2351 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 * Return a pointer to just after the name. Equal to "arg" if there is no
2354 * valid name.
2355 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002356 static char_u *
2357to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358{
2359 char_u *p;
2360
2361 // Quick check for valid starting character.
2362 if (!eval_isnamec1(*arg))
2363 return arg;
2364
2365 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2366 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2367 // and can be used in slice "[n:]".
2368 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002369 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2371 break;
2372 return p;
2373}
2374
2375/*
2376 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002377 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 */
2379 char_u *
2380to_name_const_end(char_u *arg)
2381{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002382 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 typval_T rettv;
2384
2385 if (p == arg && *arg == '[')
2386 {
2387
2388 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002389 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 p = arg;
2391 }
2392 else if (p == arg && *arg == '#' && arg[1] == '{')
2393 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002394 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002396 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 p = arg;
2398 }
2399 else if (p == arg && *arg == '{')
2400 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002401 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002403 // Can be "{x -> ret}()".
2404 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002406 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 if (ret != OK)
2408 p = arg;
2409 }
2410
2411 return p;
2412}
2413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414/*
2415 * parse a list: [expr, expr]
2416 * "*arg" points to the '['.
2417 */
2418 static int
2419compile_list(char_u **arg, cctx_T *cctx)
2420{
2421 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002422 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 int count = 0;
2424
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002425 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002427 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002428 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002429 semsg(_(e_list_end), *arg);
2430 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002431 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002432 if (*p == ',')
2433 {
2434 semsg(_(e_no_white_before), ",");
2435 return FAIL;
2436 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002437 if (*p == ']')
2438 {
2439 ++p;
2440 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002441 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002442 p += STRLEN(p);
2443 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002444 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002445 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 break;
2447 ++count;
2448 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002449 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002451 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2452 {
2453 semsg(_(e_white_after), ",");
2454 return FAIL;
2455 }
2456 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002457 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 p = skipwhite(p);
2459 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002460 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461
2462 generate_NEWLIST(cctx, count);
2463 return OK;
2464}
2465
2466/*
2467 * parse a lambda: {arg, arg -> expr}
2468 * "*arg" points to the '{'.
2469 */
2470 static int
2471compile_lambda(char_u **arg, cctx_T *cctx)
2472{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 typval_T rettv;
2474 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002475 evalarg_T evalarg;
2476
2477 CLEAR_FIELD(evalarg);
2478 evalarg.eval_flags = EVAL_EVALUATE;
2479 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480
2481 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002482 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002486 ++ufunc->uf_refcount;
2487 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002488 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489
2490 // The function will have one line: "return {expr}".
2491 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002492 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002494 clear_evalarg(&evalarg, NULL);
2495
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002496 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002497 {
2498 // The return type will now be known.
2499 set_function_type(ufunc);
2500
2501 return generate_FUNCREF(cctx, ufunc);
2502 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002503
2504 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 return FAIL;
2506}
2507
2508/*
2509 * Compile a lamda call: expr->{lambda}(args)
2510 * "arg" points to the "{".
2511 */
2512 static int
2513compile_lambda_call(char_u **arg, cctx_T *cctx)
2514{
2515 ufunc_T *ufunc;
2516 typval_T rettv;
2517 int argcount = 1;
2518 int ret = FAIL;
2519
2520 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002521 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 return FAIL;
2523
2524 if (**arg != '(')
2525 {
2526 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002527 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 else
2529 semsg(_(e_missing_paren), "lambda");
2530 clear_tv(&rettv);
2531 return FAIL;
2532 }
2533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 ufunc = rettv.vval.v_partial->pt_func;
2535 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002536 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002537 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002538
2539 // The function will have one line: "return {expr}".
2540 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002541 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542
2543 // compile the arguments
2544 *arg = skipwhite(*arg + 1);
2545 if (compile_arguments(arg, cctx, &argcount) == OK)
2546 // call the compiled function
2547 ret = generate_CALL(cctx, ufunc, argcount);
2548
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002549 if (ret == FAIL)
2550 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 return ret;
2552}
2553
2554/*
2555 * parse a dict: {'key': val} or #{key: val}
2556 * "*arg" points to the '{'.
2557 */
2558 static int
2559compile_dict(char_u **arg, cctx_T *cctx, int literal)
2560{
2561 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002562 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 int count = 0;
2564 dict_T *d = dict_alloc();
2565 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002566 char_u *whitep = *arg;
2567 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568
2569 if (d == NULL)
2570 return FAIL;
2571 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002572 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 {
2574 char_u *key = NULL;
2575
Bram Moolenaar23c55272020-06-21 16:58:13 +02002576 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002577 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002578 *arg = NULL;
2579 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002580 }
2581
2582 if (**arg == '}')
2583 break;
2584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 if (literal)
2586 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002587 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
Bram Moolenaar2c330432020-04-13 14:41:35 +02002589 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 {
2591 semsg(_("E1014: Invalid key: %s"), *arg);
2592 return FAIL;
2593 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002594 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 if (generate_PUSHS(cctx, key) == FAIL)
2596 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002597 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 }
2599 else
2600 {
2601 isn_T *isn;
2602
Bram Moolenaara5565e42020-05-09 15:44:01 +02002603 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2606 if (isn->isn_type == ISN_PUSHS)
2607 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002608 else
2609 {
2610 type_T *keytype = ((type_T **)stack->ga_data)
2611 [stack->ga_len - 1];
2612 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2613 return FAIL;
2614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 }
2616
2617 // Check for duplicate keys, if using string keys.
2618 if (key != NULL)
2619 {
2620 item = dict_find(d, key, -1);
2621 if (item != NULL)
2622 {
2623 semsg(_(e_duplicate_key), key);
2624 goto failret;
2625 }
2626 item = dictitem_alloc(key);
2627 if (item != NULL)
2628 {
2629 item->di_tv.v_type = VAR_UNKNOWN;
2630 item->di_tv.v_lock = 0;
2631 if (dict_add(d, item) == FAIL)
2632 dictitem_free(item);
2633 }
2634 }
2635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 if (**arg != ':')
2637 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002638 if (*skipwhite(*arg) == ':')
2639 semsg(_(e_no_white_before), ":");
2640 else
2641 semsg(_(e_missing_dict_colon), *arg);
2642 return FAIL;
2643 }
2644 whitep = *arg + 1;
2645 if (!IS_WHITE_OR_NUL(*whitep))
2646 {
2647 semsg(_(e_white_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 return FAIL;
2649 }
2650
2651 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002652 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002653 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002654 *arg = NULL;
2655 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002656 }
2657
Bram Moolenaara5565e42020-05-09 15:44:01 +02002658 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 return FAIL;
2660 ++count;
2661
Bram Moolenaar2c330432020-04-13 14:41:35 +02002662 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002663 *arg = skipwhite(*arg);
2664 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002665 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002666 *arg = NULL;
2667 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 if (**arg == '}')
2670 break;
2671 if (**arg != ',')
2672 {
2673 semsg(_(e_missing_dict_comma), *arg);
2674 goto failret;
2675 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002676 if (IS_WHITE_OR_NUL(*whitep))
2677 {
2678 semsg(_(e_no_white_before), ",");
2679 return FAIL;
2680 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002681 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 *arg = skipwhite(*arg + 1);
2683 }
2684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 *arg = *arg + 1;
2686
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002687 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002688 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002689 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002690 *arg += STRLEN(*arg);
2691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 dict_unref(d);
2693 return generate_NEWDICT(cctx, count);
2694
2695failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002696 if (*arg == NULL)
2697 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 dict_unref(d);
2699 return FAIL;
2700}
2701
2702/*
2703 * Compile "&option".
2704 */
2705 static int
2706compile_get_option(char_u **arg, cctx_T *cctx)
2707{
2708 typval_T rettv;
2709 char_u *start = *arg;
2710 int ret;
2711
2712 // parse the option and get the current value to get the type.
2713 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002714 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 if (ret == OK)
2716 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002717 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 char_u *name = vim_strnsave(start, *arg - start);
2719 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2720
2721 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2722 vim_free(name);
2723 }
2724 clear_tv(&rettv);
2725
2726 return ret;
2727}
2728
2729/*
2730 * Compile "$VAR".
2731 */
2732 static int
2733compile_get_env(char_u **arg, cctx_T *cctx)
2734{
2735 char_u *start = *arg;
2736 int len;
2737 int ret;
2738 char_u *name;
2739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 ++*arg;
2741 len = get_env_len(arg);
2742 if (len == 0)
2743 {
2744 semsg(_(e_syntax_at), start - 1);
2745 return FAIL;
2746 }
2747
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002748 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 name = vim_strnsave(start, len + 1);
2750 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2751 vim_free(name);
2752 return ret;
2753}
2754
2755/*
2756 * Compile "@r".
2757 */
2758 static int
2759compile_get_register(char_u **arg, cctx_T *cctx)
2760{
2761 int ret;
2762
2763 ++*arg;
2764 if (**arg == NUL)
2765 {
2766 semsg(_(e_syntax_at), *arg - 1);
2767 return FAIL;
2768 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002769 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 {
2771 emsg_invreg(**arg);
2772 return FAIL;
2773 }
2774 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2775 ++*arg;
2776 return ret;
2777}
2778
2779/*
2780 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002781 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 */
2783 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002784apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002786 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787
2788 // this works from end to start
2789 while (p > start)
2790 {
2791 --p;
2792 if (*p == '-' || *p == '+')
2793 {
2794 // only '-' has an effect, for '+' we only check the type
2795#ifdef FEAT_FLOAT
2796 if (rettv->v_type == VAR_FLOAT)
2797 {
2798 if (*p == '-')
2799 rettv->vval.v_float = -rettv->vval.v_float;
2800 }
2801 else
2802#endif
2803 {
2804 varnumber_T val;
2805 int error = FALSE;
2806
2807 // tv_get_number_chk() accepts a string, but we don't want that
2808 // here
2809 if (check_not_string(rettv) == FAIL)
2810 return FAIL;
2811 val = tv_get_number_chk(rettv, &error);
2812 clear_tv(rettv);
2813 if (error)
2814 return FAIL;
2815 if (*p == '-')
2816 val = -val;
2817 rettv->v_type = VAR_NUMBER;
2818 rettv->vval.v_number = val;
2819 }
2820 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002821 else if (numeric_only)
2822 {
2823 ++p;
2824 break;
2825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 else
2827 {
2828 int v = tv2bool(rettv);
2829
2830 // '!' is permissive in the type.
2831 clear_tv(rettv);
2832 rettv->v_type = VAR_BOOL;
2833 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2834 }
2835 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002836 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 return OK;
2838}
2839
2840/*
2841 * Recognize v: variables that are constants and set "rettv".
2842 */
2843 static void
2844get_vim_constant(char_u **arg, typval_T *rettv)
2845{
2846 if (STRNCMP(*arg, "v:true", 6) == 0)
2847 {
2848 rettv->v_type = VAR_BOOL;
2849 rettv->vval.v_number = VVAL_TRUE;
2850 *arg += 6;
2851 }
2852 else if (STRNCMP(*arg, "v:false", 7) == 0)
2853 {
2854 rettv->v_type = VAR_BOOL;
2855 rettv->vval.v_number = VVAL_FALSE;
2856 *arg += 7;
2857 }
2858 else if (STRNCMP(*arg, "v:null", 6) == 0)
2859 {
2860 rettv->v_type = VAR_SPECIAL;
2861 rettv->vval.v_number = VVAL_NULL;
2862 *arg += 6;
2863 }
2864 else if (STRNCMP(*arg, "v:none", 6) == 0)
2865 {
2866 rettv->v_type = VAR_SPECIAL;
2867 rettv->vval.v_number = VVAL_NONE;
2868 *arg += 6;
2869 }
2870}
2871
Bram Moolenaar696ba232020-07-29 21:20:41 +02002872 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002873get_compare_type(char_u *p, int *len, int *type_is)
2874{
2875 exptype_T type = EXPR_UNKNOWN;
2876 int i;
2877
2878 switch (p[0])
2879 {
2880 case '=': if (p[1] == '=')
2881 type = EXPR_EQUAL;
2882 else if (p[1] == '~')
2883 type = EXPR_MATCH;
2884 break;
2885 case '!': if (p[1] == '=')
2886 type = EXPR_NEQUAL;
2887 else if (p[1] == '~')
2888 type = EXPR_NOMATCH;
2889 break;
2890 case '>': if (p[1] != '=')
2891 {
2892 type = EXPR_GREATER;
2893 *len = 1;
2894 }
2895 else
2896 type = EXPR_GEQUAL;
2897 break;
2898 case '<': if (p[1] != '=')
2899 {
2900 type = EXPR_SMALLER;
2901 *len = 1;
2902 }
2903 else
2904 type = EXPR_SEQUAL;
2905 break;
2906 case 'i': if (p[1] == 's')
2907 {
2908 // "is" and "isnot"; but not a prefix of a name
2909 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2910 *len = 5;
2911 i = p[*len];
2912 if (!isalnum(i) && i != '_')
2913 {
2914 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2915 *type_is = TRUE;
2916 }
2917 }
2918 break;
2919 }
2920 return type;
2921}
2922
2923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002925 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 */
2927 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002928compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002930 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931
2932 // this works from end to start
2933 while (p > start)
2934 {
2935 --p;
2936 if (*p == '-' || *p == '+')
2937 {
2938 int negate = *p == '-';
2939 isn_T *isn;
2940
2941 // TODO: check type
2942 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2943 {
2944 --p;
2945 if (*p == '-')
2946 negate = !negate;
2947 }
2948 // only '-' has an effect, for '+' we only check the type
2949 if (negate)
2950 isn = generate_instr(cctx, ISN_NEGATENR);
2951 else
2952 isn = generate_instr(cctx, ISN_CHECKNR);
2953 if (isn == NULL)
2954 return FAIL;
2955 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002956 else if (numeric_only)
2957 {
2958 ++p;
2959 break;
2960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 else
2962 {
2963 int invert = TRUE;
2964
2965 while (p > start && p[-1] == '!')
2966 {
2967 --p;
2968 invert = !invert;
2969 }
2970 if (generate_2BOOL(cctx, invert) == FAIL)
2971 return FAIL;
2972 }
2973 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002974 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 return OK;
2976}
2977
2978/*
2979 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002980 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 */
2982 static int
2983compile_subscript(
2984 char_u **arg,
2985 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002986 char_u *start_leader,
2987 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002988 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002990 char_u *name_start = *end_leader;
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 for (;;)
2993 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002994 char_u *p = skipwhite(*arg);
2995
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002996 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002997 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002998 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002999
3000 // If a following line starts with "->{" or "->X" advance to that
3001 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003002 // Also if a following line starts with ".x".
3003 if (next != NULL &&
3004 ((next[0] == '-' && next[1] == '>'
3005 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003006 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003007 {
3008 next = next_line_from_context(cctx, TRUE);
3009 if (next == NULL)
3010 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003011 *arg = next;
3012 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003013 }
3014 }
3015
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003016 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3017 // not a function call.
3018 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003020 garray_T *stack = &cctx->ctx_type_stack;
3021 type_T *type;
3022 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003024 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003025 return FAIL;
3026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003028 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3029
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003030 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3032 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003033 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 return FAIL;
3035 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003036 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003038 char_u *pstart = p;
3039
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003040 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003041 return FAIL;
3042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 // something->method()
3044 // Apply the '!', '-' and '+' first:
3045 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003046 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003049 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003050 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003051 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 if (**arg == '{')
3053 {
3054 // lambda call: list->{lambda}
3055 if (compile_lambda_call(arg, cctx) == FAIL)
3056 return FAIL;
3057 }
3058 else
3059 {
3060 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003061 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003062 if (!eval_isnamec1(*p))
3063 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003064 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003065 return FAIL;
3066 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003067 if (ASCII_ISALPHA(*p) && p[1] == ':')
3068 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003069 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 ;
3071 if (*p != '(')
3072 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003073 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 return FAIL;
3075 }
3076 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003077 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 return FAIL;
3079 }
3080 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003081 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003084 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003085 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003086
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003087 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003088 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003089 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003090 // TODO: blob index
3091 // TODO: more arguments
3092 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003093 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003094 return FAIL;
3095
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003096 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003097 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003098 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003099 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003100 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 return FAIL;
3102
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003103 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3104 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 if (**arg != ']')
3106 {
3107 emsg(_(e_missbrac));
3108 return FAIL;
3109 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003110 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003112 // We can index a list and a dict. If we don't know the type
3113 // we can use the index value type.
3114 // TODO: If we don't know use an instruction to figure it out at
3115 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003116 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003117 vtype = (*typep)->tt_type;
3118 if (*typep == &t_any)
3119 {
3120 type_T *valtype = ((type_T **)stack->ga_data)
3121 [stack->ga_len - 1];
3122 if (valtype == &t_string)
3123 vtype = VAR_DICT;
3124 }
3125 if (vtype == VAR_DICT)
3126 {
3127 if ((*typep)->tt_type == VAR_DICT)
3128 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003129 else
3130 {
3131 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3132 return FAIL;
3133 *typep = &t_any;
3134 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003135 if (may_generate_2STRING(-1, cctx) == FAIL)
3136 return FAIL;
3137 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3138 return FAIL;
3139 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003140 else if (vtype == VAR_STRING)
3141 {
3142 *typep = &t_number;
3143 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3144 return FAIL;
3145 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003146 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003147 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003148 if ((*typep)->tt_type == VAR_LIST)
3149 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003150 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003151 return FAIL;
3152 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003153 else
3154 {
3155 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003156 return FAIL;
3157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003159 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003161 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003162 return FAIL;
3163
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003164 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003165 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003168 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003169 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 while (eval_isnamec(*p))
3171 MB_PTR_ADV(p);
3172 if (p == *arg)
3173 {
3174 semsg(_(e_syntax_at), *arg);
3175 return FAIL;
3176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003177 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 return FAIL;
3179 *arg = p;
3180 }
3181 else
3182 break;
3183 }
3184
3185 // TODO - see handle_subscript():
3186 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3187 // Don't do this when "Func" is already a partial that was bound
3188 // explicitly (pt_auto is FALSE).
3189
3190 return OK;
3191}
3192
3193/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003194 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3195 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003197 * If the value is a constant "ppconst->pp_ret" will be set.
3198 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003199 *
3200 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 */
3202
3203/*
3204 * number number constant
3205 * 0zFFFFFFFF Blob constant
3206 * "string" string constant
3207 * 'string' literal string constant
3208 * &option-name option value
3209 * @r register contents
3210 * identifier variable value
3211 * function() function call
3212 * $VAR environment variable
3213 * (expression) nested expression
3214 * [expr, expr] List
3215 * {key: val, key: val} Dictionary
3216 * #{key: val, key: val} Dictionary with literal keys
3217 *
3218 * Also handle:
3219 * ! in front logical NOT
3220 * - in front unary minus
3221 * + in front unary plus (ignored)
3222 * trailing (arg) funcref/partial call
3223 * trailing [] subscript in String or List
3224 * trailing .name entry in Dictionary
3225 * trailing ->name() method call
3226 */
3227 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003228compile_expr7(
3229 char_u **arg,
3230 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003231 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 char_u *start_leader, *end_leader;
3234 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003235 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003236 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
3238 /*
3239 * Skip '!', '-' and '+' characters. They are handled later.
3240 */
3241 start_leader = *arg;
3242 while (**arg == '!' || **arg == '-' || **arg == '+')
3243 *arg = skipwhite(*arg + 1);
3244 end_leader = *arg;
3245
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003246 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 switch (**arg)
3248 {
3249 /*
3250 * Number constant.
3251 */
3252 case '0': // also for blob starting with 0z
3253 case '1':
3254 case '2':
3255 case '3':
3256 case '4':
3257 case '5':
3258 case '6':
3259 case '7':
3260 case '8':
3261 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003262 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003264 // Apply "-" and "+" just before the number now, right to
3265 // left. Matters especially when "->" follows. Stops at
3266 // '!'.
3267 if (apply_leader(rettv, TRUE,
3268 start_leader, &end_leader) == FAIL)
3269 {
3270 clear_tv(rettv);
3271 return FAIL;
3272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 break;
3274
3275 /*
3276 * String constant: "string".
3277 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003278 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 return FAIL;
3280 break;
3281
3282 /*
3283 * Literal string constant: 'str''ing'.
3284 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003285 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 return FAIL;
3287 break;
3288
3289 /*
3290 * Constant Vim variable.
3291 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003292 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 ret = NOTDONE;
3294 break;
3295
3296 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003297 * "true" constant
3298 */
3299 case 't': if (STRNCMP(*arg, "true", 4) == 0
3300 && !eval_isnamec((*arg)[4]))
3301 {
3302 *arg += 4;
3303 rettv->v_type = VAR_BOOL;
3304 rettv->vval.v_number = VVAL_TRUE;
3305 }
3306 else
3307 ret = NOTDONE;
3308 break;
3309
3310 /*
3311 * "false" constant
3312 */
3313 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3314 && !eval_isnamec((*arg)[5]))
3315 {
3316 *arg += 5;
3317 rettv->v_type = VAR_BOOL;
3318 rettv->vval.v_number = VVAL_FALSE;
3319 }
3320 else
3321 ret = NOTDONE;
3322 break;
3323
3324 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 * List: [expr, expr]
3326 */
3327 case '[': ret = compile_list(arg, cctx);
3328 break;
3329
3330 /*
3331 * Dictionary: #{key: val, key: val}
3332 */
3333 case '#': if ((*arg)[1] == '{')
3334 {
3335 ++*arg;
3336 ret = compile_dict(arg, cctx, TRUE);
3337 }
3338 else
3339 ret = NOTDONE;
3340 break;
3341
3342 /*
3343 * Lambda: {arg, arg -> expr}
3344 * Dictionary: {'key': val, 'key': val}
3345 */
3346 case '{': {
3347 char_u *start = skipwhite(*arg + 1);
3348
3349 // Find out what comes after the arguments.
3350 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003351 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 if (ret != FAIL && *start == '>')
3353 ret = compile_lambda(arg, cctx);
3354 else
3355 ret = compile_dict(arg, cctx, FALSE);
3356 }
3357 break;
3358
3359 /*
3360 * Option value: &name
3361 */
3362 case '&': ret = compile_get_option(arg, cctx);
3363 break;
3364
3365 /*
3366 * Environment variable: $VAR.
3367 */
3368 case '$': ret = compile_get_env(arg, cctx);
3369 break;
3370
3371 /*
3372 * Register contents: @r.
3373 */
3374 case '@': ret = compile_get_register(arg, cctx);
3375 break;
3376 /*
3377 * nested expression: (expression).
3378 */
3379 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003380
3381 // recursive!
3382 if (ppconst->pp_used <= PPSIZE - 10)
3383 {
3384 ret = compile_expr1(arg, cctx, ppconst);
3385 }
3386 else
3387 {
3388 // Not enough space in ppconst, flush constants.
3389 if (generate_ppconst(cctx, ppconst) == FAIL)
3390 return FAIL;
3391 ret = compile_expr0(arg, cctx);
3392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 *arg = skipwhite(*arg);
3394 if (**arg == ')')
3395 ++*arg;
3396 else if (ret == OK)
3397 {
3398 emsg(_(e_missing_close));
3399 ret = FAIL;
3400 }
3401 break;
3402
3403 default: ret = NOTDONE;
3404 break;
3405 }
3406 if (ret == FAIL)
3407 return FAIL;
3408
Bram Moolenaar1c747212020-05-09 18:28:34 +02003409 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003411 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003412 clear_tv(rettv);
3413 else
3414 // A constant expression can possibly be handled compile time,
3415 // return the value instead of generating code.
3416 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 }
3418 else if (ret == NOTDONE)
3419 {
3420 char_u *p;
3421 int r;
3422
3423 if (!eval_isnamec1(**arg))
3424 {
3425 semsg(_("E1015: Name expected: %s"), *arg);
3426 return FAIL;
3427 }
3428
3429 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003430 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003432 {
3433 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003436 {
3437 if (generate_ppconst(cctx, ppconst) == FAIL)
3438 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 if (r == FAIL)
3442 return FAIL;
3443 }
3444
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003445 // Handle following "[]", ".member", etc.
3446 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003447 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003448 ppconst) == FAIL)
3449 return FAIL;
3450 if (ppconst->pp_used > 0)
3451 {
3452 // apply the '!', '-' and '+' before the constant
3453 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003454 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003455 return FAIL;
3456 return OK;
3457 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003458 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003460 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461}
3462
3463/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003464 * Give the "white on both sides" error, taking the operator from "p[len]".
3465 */
3466 void
3467error_white_both(char_u *op, int len)
3468{
3469 char_u buf[10];
3470
3471 vim_strncpy(buf, op, len);
3472 semsg(_(e_white_both), buf);
3473}
3474
3475/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003476 * <type>expr7: runtime type check / conversion
3477 */
3478 static int
3479compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3480{
3481 type_T *want_type = NULL;
3482
3483 // Recognize <type>
3484 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3485 {
3486 int called_emsg_before = called_emsg;
3487
3488 ++*arg;
3489 want_type = parse_type(arg, cctx->ctx_type_list);
3490 if (called_emsg != called_emsg_before)
3491 return FAIL;
3492
3493 if (**arg != '>')
3494 {
3495 if (*skipwhite(*arg) == '>')
3496 semsg(_(e_no_white_before), ">");
3497 else
3498 emsg(_("E1104: Missing >"));
3499 return FAIL;
3500 }
3501 ++*arg;
3502 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3503 return FAIL;
3504 }
3505
3506 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3507 return FAIL;
3508
3509 if (want_type != NULL)
3510 {
3511 garray_T *stack = &cctx->ctx_type_stack;
3512 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3513
3514 if (check_type(want_type, actual, FALSE) == FAIL)
3515 {
3516 generate_ppconst(cctx, ppconst);
3517 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3518 return FAIL;
3519 }
3520 }
3521
3522 return OK;
3523}
3524
3525/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 * * number multiplication
3527 * / number division
3528 * % number modulo
3529 */
3530 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003531compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532{
3533 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003534 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003535 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003537 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003538 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 return FAIL;
3540
3541 /*
3542 * Repeat computing, until no "*", "/" or "%" is following.
3543 */
3544 for (;;)
3545 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003546 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (*op != '*' && *op != '/' && *op != '%')
3548 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003549 if (next != NULL)
3550 {
3551 *arg = next_line_from_context(cctx, TRUE);
3552 op = skipwhite(*arg);
3553 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003554
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003555 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003557 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003558 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003561 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003562 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003564 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003565 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003567
3568 if (ppconst->pp_used == ppconst_used + 2
3569 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3570 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003571 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003572 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3573 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003574 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003576 // both are numbers: compute the result
3577 switch (*op)
3578 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003579 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003580 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003581 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003582 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003583 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003584 break;
3585 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003586 tv1->vval.v_number = res;
3587 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003588 }
3589 else
3590 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003591 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003592 generate_two_op(cctx, op);
3593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 }
3595
3596 return OK;
3597}
3598
3599/*
3600 * + number addition
3601 * - number subtraction
3602 * .. string concatenation
3603 */
3604 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606{
3607 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003608 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003610 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611
3612 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003613 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 return FAIL;
3615
3616 /*
3617 * Repeat computing, until no "+", "-" or ".." is following.
3618 */
3619 for (;;)
3620 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003621 op = may_peek_next_line(cctx, *arg, &next);
3622 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 break;
3624 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003625 if (next != NULL)
3626 {
3627 *arg = next_line_from_context(cctx, TRUE);
3628 op = skipwhite(*arg);
3629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003631 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003633 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003634 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636
3637 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003638 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003639 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003641 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003642 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 return FAIL;
3644
Bram Moolenaara5565e42020-05-09 15:44:01 +02003645 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003646 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003647 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3648 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3649 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3650 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003652 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3653 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003655 // concat/subtract/add constant numbers
3656 if (*op == '+')
3657 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3658 else if (*op == '-')
3659 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3660 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003661 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003662 // concatenate constant strings
3663 char_u *s1 = tv1->vval.v_string;
3664 char_u *s2 = tv2->vval.v_string;
3665 size_t len1 = STRLEN(s1);
3666
3667 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3668 if (tv1->vval.v_string == NULL)
3669 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003670 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003671 return FAIL;
3672 }
3673 mch_memmove(tv1->vval.v_string, s1, len1);
3674 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003675 vim_free(s1);
3676 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003677 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003678 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 }
3680 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003681 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003682 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003683 if (*op == '.')
3684 {
3685 if (may_generate_2STRING(-2, cctx) == FAIL
3686 || may_generate_2STRING(-1, cctx) == FAIL)
3687 return FAIL;
3688 generate_instr_drop(cctx, ISN_CONCAT, 1);
3689 }
3690 else
3691 generate_two_op(cctx, op);
3692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
3694
3695 return OK;
3696}
3697
3698/*
3699 * expr5a == expr5b
3700 * expr5a =~ expr5b
3701 * expr5a != expr5b
3702 * expr5a !~ expr5b
3703 * expr5a > expr5b
3704 * expr5a >= expr5b
3705 * expr5a < expr5b
3706 * expr5a <= expr5b
3707 * expr5a is expr5b
3708 * expr5a isnot expr5b
3709 *
3710 * Produces instructions:
3711 * EVAL expr5a Push result of "expr5a"
3712 * EVAL expr5b Push result of "expr5b"
3713 * COMPARE one of the compare instructions
3714 */
3715 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003716compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717{
3718 exptype_T type = EXPR_UNKNOWN;
3719 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003720 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003723 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
3725 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003726 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 return FAIL;
3728
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003729 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003730 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
3732 /*
3733 * If there is a comparative operator, use it.
3734 */
3735 if (type != EXPR_UNKNOWN)
3736 {
3737 int ic = FALSE; // Default: do not ignore case
3738
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003739 if (next != NULL)
3740 {
3741 *arg = next_line_from_context(cctx, TRUE);
3742 p = skipwhite(*arg);
3743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 if (type_is && (p[len] == '?' || p[len] == '#'))
3745 {
3746 semsg(_(e_invexpr2), *arg);
3747 return FAIL;
3748 }
3749 // extra question mark appended: ignore case
3750 if (p[len] == '?')
3751 {
3752 ic = TRUE;
3753 ++len;
3754 }
3755 // extra '#' appended: match case (ignored)
3756 else if (p[len] == '#')
3757 ++len;
3758 // nothing appended: match case
3759
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003760 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003762 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003763 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 }
3765
3766 // get the second variable
3767 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003768 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003769 return FAIL;
3770
Bram Moolenaara5565e42020-05-09 15:44:01 +02003771 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 return FAIL;
3773
Bram Moolenaara5565e42020-05-09 15:44:01 +02003774 if (ppconst->pp_used == ppconst_used + 2)
3775 {
3776 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3777 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3778 int ret;
3779
3780 // Both sides are a constant, compute the result now.
3781 // First check for a valid combination of types, this is more
3782 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003783 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003784 ret = FAIL;
3785 else
3786 {
3787 ret = typval_compare(tv1, tv2, type, ic);
3788 tv1->v_type = VAR_BOOL;
3789 tv1->vval.v_number = tv1->vval.v_number
3790 ? VVAL_TRUE : VVAL_FALSE;
3791 clear_tv(tv2);
3792 --ppconst->pp_used;
3793 }
3794 return ret;
3795 }
3796
3797 generate_ppconst(cctx, ppconst);
3798 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
3800
3801 return OK;
3802}
3803
Bram Moolenaar7f141552020-05-09 17:35:53 +02003804static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806/*
3807 * Compile || or &&.
3808 */
3809 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003810compile_and_or(
3811 char_u **arg,
3812 cctx_T *cctx,
3813 char *op,
3814 ppconst_T *ppconst,
3815 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003817 char_u *next;
3818 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 int opchar = *op;
3820
3821 if (p[0] == opchar && p[1] == opchar)
3822 {
3823 garray_T *instr = &cctx->ctx_instr;
3824 garray_T end_ga;
3825
3826 /*
3827 * Repeat until there is no following "||" or "&&"
3828 */
3829 ga_init2(&end_ga, sizeof(int), 10);
3830 while (p[0] == opchar && p[1] == opchar)
3831 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003832 if (next != NULL)
3833 {
3834 *arg = next_line_from_context(cctx, TRUE);
3835 p = skipwhite(*arg);
3836 }
3837
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003838 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3839 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003841 return FAIL;
3842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 // TODO: use ppconst if the value is a constant
3845 generate_ppconst(cctx, ppconst);
3846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 if (ga_grow(&end_ga, 1) == FAIL)
3848 {
3849 ga_clear(&end_ga);
3850 return FAIL;
3851 }
3852 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3853 ++end_ga.ga_len;
3854 generate_JUMP(cctx, opchar == '|'
3855 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3856
3857 // eval the next expression
3858 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003859 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003860 return FAIL;
3861
Bram Moolenaara5565e42020-05-09 15:44:01 +02003862 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3863 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 {
3865 ga_clear(&end_ga);
3866 return FAIL;
3867 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003868
3869 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003871 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
3873 // Fill in the end label in all jumps.
3874 while (end_ga.ga_len > 0)
3875 {
3876 isn_T *isn;
3877
3878 --end_ga.ga_len;
3879 isn = ((isn_T *)instr->ga_data)
3880 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3881 isn->isn_arg.jump.jump_where = instr->ga_len;
3882 }
3883 ga_clear(&end_ga);
3884 }
3885
3886 return OK;
3887}
3888
3889/*
3890 * expr4a && expr4a && expr4a logical AND
3891 *
3892 * Produces instructions:
3893 * EVAL expr4a Push result of "expr4a"
3894 * JUMP_AND_KEEP_IF_FALSE end
3895 * EVAL expr4b Push result of "expr4b"
3896 * JUMP_AND_KEEP_IF_FALSE end
3897 * EVAL expr4c Push result of "expr4c"
3898 * end:
3899 */
3900 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003901compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 int ppconst_used = ppconst->pp_used;
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return FAIL;
3908
3909 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003910 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911}
3912
3913/*
3914 * expr3a || expr3b || expr3c logical OR
3915 *
3916 * Produces instructions:
3917 * EVAL expr3a Push result of "expr3a"
3918 * JUMP_AND_KEEP_IF_TRUE end
3919 * EVAL expr3b Push result of "expr3b"
3920 * JUMP_AND_KEEP_IF_TRUE end
3921 * EVAL expr3c Push result of "expr3c"
3922 * end:
3923 */
3924 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003927 int ppconst_used = ppconst->pp_used;
3928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003930 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 return FAIL;
3932
3933 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003934 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935}
3936
3937/*
3938 * Toplevel expression: expr2 ? expr1a : expr1b
3939 *
3940 * Produces instructions:
3941 * EVAL expr2 Push result of "expr"
3942 * JUMP_IF_FALSE alt jump if false
3943 * EVAL expr1a
3944 * JUMP_ALWAYS end
3945 * alt: EVAL expr1b
3946 * end:
3947 */
3948 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003949compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950{
3951 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003952 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003953 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954
Bram Moolenaar61a89812020-05-07 16:58:17 +02003955 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003956 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 return FAIL;
3958
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003959 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 if (*p == '?')
3961 {
3962 garray_T *instr = &cctx->ctx_instr;
3963 garray_T *stack = &cctx->ctx_type_stack;
3964 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003965 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003967 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003969 int has_const_expr = FALSE;
3970 int const_value = FALSE;
3971 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003973 if (next != NULL)
3974 {
3975 *arg = next_line_from_context(cctx, TRUE);
3976 p = skipwhite(*arg);
3977 }
3978
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003979 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3980 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003982 return FAIL;
3983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984
Bram Moolenaara5565e42020-05-09 15:44:01 +02003985 if (ppconst->pp_used == ppconst_used + 1)
3986 {
3987 // the condition is a constant, we know whether the ? or the :
3988 // expression is to be evaluated.
3989 has_const_expr = TRUE;
3990 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3991 clear_tv(&ppconst->pp_tv[ppconst_used]);
3992 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003993 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3994 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003995 }
3996 else
3997 {
3998 generate_ppconst(cctx, ppconst);
3999 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
4002 // evaluate the second expression; any type is accepted
4003 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004004 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004005 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004007 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 if (!has_const_expr)
4010 {
4011 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 // remember the type and drop it
4014 --stack->ga_len;
4015 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 end_idx = instr->ga_len;
4018 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4019
4020 // jump here from JUMP_IF_FALSE
4021 isn = ((isn_T *)instr->ga_data) + alt_idx;
4022 isn->isn_arg.jump.jump_where = instr->ga_len;
4023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
4025 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004026 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 if (*p != ':')
4028 {
4029 emsg(_(e_missing_colon));
4030 return FAIL;
4031 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004032 if (next != NULL)
4033 {
4034 *arg = next_line_from_context(cctx, TRUE);
4035 p = skipwhite(*arg);
4036 }
4037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004038 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4039 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 return FAIL;
4042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043
4044 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004045 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004046 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4047 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004049 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004050 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004051 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004052 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 if (!has_const_expr)
4055 {
4056 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
Bram Moolenaara5565e42020-05-09 15:44:01 +02004058 // If the types differ, the result has a more generic type.
4059 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4060 common_type(type1, type2, &type2, cctx->ctx_type_list);
4061
4062 // jump here from JUMP_ALWAYS
4063 isn = ((isn_T *)instr->ga_data) + end_idx;
4064 isn->isn_arg.jump.jump_where = instr->ga_len;
4065 }
4066
4067 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 }
4069 return OK;
4070}
4071
4072/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004073 * Toplevel expression.
4074 */
4075 static int
4076compile_expr0(char_u **arg, cctx_T *cctx)
4077{
4078 ppconst_T ppconst;
4079
4080 CLEAR_FIELD(ppconst);
4081 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4082 {
4083 clear_ppconst(&ppconst);
4084 return FAIL;
4085 }
4086 if (generate_ppconst(cctx, &ppconst) == FAIL)
4087 return FAIL;
4088 return OK;
4089}
4090
4091/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 * compile "return [expr]"
4093 */
4094 static char_u *
4095compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4096{
4097 char_u *p = arg;
4098 garray_T *stack = &cctx->ctx_type_stack;
4099 type_T *stack_type;
4100
4101 if (*p != NUL && *p != '|' && *p != '\n')
4102 {
4103 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004104 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 return NULL;
4106
4107 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4108 if (set_return_type)
4109 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004110 else
4111 {
4112 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4113 && stack_type->tt_type != VAR_VOID
4114 && stack_type->tt_type != VAR_UNKNOWN)
4115 {
4116 emsg(_("E1096: Returning a value in a function without a return type"));
4117 return NULL;
4118 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004119 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4120 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 }
4124 else
4125 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004126 // "set_return_type" cannot be TRUE, only used for a lambda which
4127 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004128 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4129 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
4131 emsg(_("E1003: Missing return value"));
4132 return NULL;
4133 }
4134
4135 // No argument, return zero.
4136 generate_PUSHNR(cctx, 0);
4137 }
4138
4139 if (generate_instr(cctx, ISN_RETURN) == NULL)
4140 return NULL;
4141
4142 // "return val | endif" is possible
4143 return skipwhite(p);
4144}
4145
4146/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004147 * Get a line from the compilation context, compatible with exarg_T getline().
4148 * Return a pointer to the line in allocated memory.
4149 * Return NULL for end-of-file or some error.
4150 */
4151 static char_u *
4152exarg_getline(
4153 int c UNUSED,
4154 void *cookie,
4155 int indent UNUSED,
4156 int do_concat UNUSED)
4157{
4158 cctx_T *cctx = (cctx_T *)cookie;
4159
4160 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4161 {
4162 iemsg("Heredoc got to end");
4163 return NULL;
4164 }
4165 ++cctx->ctx_lnum;
4166 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4167 [cctx->ctx_lnum]);
4168}
4169
4170/*
4171 * Compile a nested :def command.
4172 */
4173 static char_u *
4174compile_nested_function(exarg_T *eap, cctx_T *cctx)
4175{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004176 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004177 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004178 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004179 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004180 lvar_T *lvar;
4181 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004182 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004183
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004184 // Only g:Func() can use a namespace.
4185 if (name_start[1] == ':' && !is_global)
4186 {
4187 semsg(_(e_namespace), name_start);
4188 return NULL;
4189 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004190 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4191 return NULL;
4192
Bram Moolenaar04b12692020-05-04 23:24:44 +02004193 eap->arg = name_end;
4194 eap->getline = exarg_getline;
4195 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004196 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004197 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004198 lambda_name = get_lambda_name();
4199 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004200
Bram Moolenaar822ba242020-05-24 23:00:18 +02004201 if (ufunc == NULL)
4202 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004203 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004204 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004205 return NULL;
4206
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004207 if (is_global)
4208 {
4209 char_u *func_name = vim_strnsave(name_start + 2,
4210 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004211
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004212 if (func_name == NULL)
4213 r = FAIL;
4214 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004215 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004216 }
4217 else
4218 {
4219 // Define a local variable for the function reference.
4220 lvar = reserve_local(cctx, name_start, name_end - name_start,
4221 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004222 if (lvar == NULL)
4223 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004224 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004225 return NULL;
4226 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4227 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004228
Bram Moolenaar61a89812020-05-07 16:58:17 +02004229 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004230 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004231}
4232
4233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 * Return the length of an assignment operator, or zero if there isn't one.
4235 */
4236 int
4237assignment_len(char_u *p, int *heredoc)
4238{
4239 if (*p == '=')
4240 {
4241 if (p[1] == '<' && p[2] == '<')
4242 {
4243 *heredoc = TRUE;
4244 return 3;
4245 }
4246 return 1;
4247 }
4248 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4249 return 2;
4250 if (STRNCMP(p, "..=", 3) == 0)
4251 return 3;
4252 return 0;
4253}
4254
4255// words that cannot be used as a variable
4256static char *reserved[] = {
4257 "true",
4258 "false",
4259 NULL
4260};
4261
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004262typedef enum {
4263 dest_local,
4264 dest_option,
4265 dest_env,
4266 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004267 dest_buffer,
4268 dest_window,
4269 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004270 dest_vimvar,
4271 dest_script,
4272 dest_reg,
4273} assign_dest_T;
4274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004276 * Generate the load instruction for "name".
4277 */
4278 static void
4279generate_loadvar(
4280 cctx_T *cctx,
4281 assign_dest_T dest,
4282 char_u *name,
4283 lvar_T *lvar,
4284 type_T *type)
4285{
4286 switch (dest)
4287 {
4288 case dest_option:
4289 // TODO: check the option exists
4290 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4291 break;
4292 case dest_global:
4293 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4294 break;
4295 case dest_buffer:
4296 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4297 break;
4298 case dest_window:
4299 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4300 break;
4301 case dest_tab:
4302 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4303 break;
4304 case dest_script:
4305 compile_load_scriptvar(cctx,
4306 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4307 break;
4308 case dest_env:
4309 // Include $ in the name here
4310 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4311 break;
4312 case dest_reg:
4313 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4314 break;
4315 case dest_vimvar:
4316 generate_LOADV(cctx, name + 2, TRUE);
4317 break;
4318 case dest_local:
4319 if (lvar->lv_from_outer)
4320 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4321 NULL, type);
4322 else
4323 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4324 break;
4325 }
4326}
4327
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004328 void
4329vim9_declare_error(char_u *name)
4330{
4331 char *scope = "";
4332
4333 switch (*name)
4334 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004335 case 'g': scope = _("global"); break;
4336 case 'b': scope = _("buffer"); break;
4337 case 'w': scope = _("window"); break;
4338 case 't': scope = _("tab"); break;
4339 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004340 case '$': semsg(_(e_declare_env_var), name);
4341 return;
4342 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4343 return;
4344 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4345 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004346 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004347 }
4348 semsg(_(e_declare_var), scope, name);
4349}
4350
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004351/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004352 * Compile declaration and assignment:
4353 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004355 * Return NULL for an error.
4356 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 */
4358 static char_u *
4359compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4360{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004361 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004363 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 char_u *ret = NULL;
4365 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004366 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004367 int scriptvar_sid = 0;
4368 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004371 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 int oplen = 0;
4374 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004375 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004376 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004377 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004381 // Skip over the "var" or "[var, var]" to get to any "=".
4382 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4383 if (p == NULL)
4384 return *arg == '[' ? arg : NULL;
4385
4386 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004388 // TODO: should we allow this, and figure out type inference from list
4389 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004390 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 return NULL;
4392 }
4393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 sp = p;
4395 p = skipwhite(p);
4396 op = p;
4397 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004398
4399 if (var_count > 0 && oplen == 0)
4400 // can be something like "[1, 2]->func()"
4401 return arg;
4402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4404 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004405 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004406 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004407 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 if (heredoc)
4410 {
4411 list_T *l;
4412 listitem_T *li;
4413
4414 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004415 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004417 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
4419 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004420 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 {
4422 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4423 li->li_tv.vval.v_string = NULL;
4424 }
4425 generate_NEWLIST(cctx, l->lv_len);
4426 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004427 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 list_free(l);
4429 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004430 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004432 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004434 // for "[var, var] = expr" evaluate the expression here, loop over the
4435 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004436
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004437 p = skipwhite(op + oplen);
4438 if (compile_expr0(&p, cctx) == FAIL)
4439 return NULL;
4440 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004442 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004444 type_T *stacktype;
4445
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004446 stacktype = stack->ga_len == 0 ? &t_void
4447 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004448 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004450 emsg(_(e_cannot_use_void));
4451 goto theend;
4452 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004453 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004454 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004455 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004456 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4457 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004458 }
4459 }
4460
4461 /*
4462 * Loop over variables in "[var, var] = expr".
4463 * For "var = expr" and "let var: type" this is done only once.
4464 */
4465 if (var_count > 0)
4466 var_start = skipwhite(arg + 1); // skip over the "["
4467 else
4468 var_start = arg;
4469 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4470 {
4471 char_u *var_end = skip_var_one(var_start, FALSE);
4472 size_t varlen;
4473 int new_local = FALSE;
4474 int opt_type;
4475 int opt_flags = 0;
4476 assign_dest_T dest = dest_local;
4477 int vimvaridx = -1;
4478 lvar_T *lvar = NULL;
4479 lvar_T arg_lvar;
4480 int has_type = FALSE;
4481 int has_index = FALSE;
4482 int instr_count = -1;
4483
Bram Moolenaar65821722020-08-02 18:58:54 +02004484 if (*var_start == '@')
4485 p = var_start + 2;
4486 else
4487 {
4488 p = (*var_start == '&' || *var_start == '$')
4489 ? var_start + 1 : var_start;
4490 p = to_name_end(p, TRUE);
4491 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004492
4493 // "a: type" is declaring variable "a" with a type, not "a:".
4494 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4495 --var_end;
4496 if (is_decl && p == var_start + 2 && p[-1] == ':')
4497 --p;
4498
4499 varlen = p - var_start;
4500 vim_free(name);
4501 name = vim_strnsave(var_start, varlen);
4502 if (name == NULL)
4503 return NULL;
4504 if (!heredoc)
4505 type = &t_any;
4506
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004507 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004508 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004509 int declare_error = FALSE;
4510
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 if (*var_start == '&')
4512 {
4513 int cc;
4514 long numval;
4515
4516 dest = dest_option;
4517 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004519 emsg(_(e_const_option));
4520 goto theend;
4521 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004522 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004523 p = var_start;
4524 p = find_option_end(&p, &opt_flags);
4525 if (p == NULL)
4526 {
4527 // cannot happen?
4528 emsg(_(e_letunexp));
4529 goto theend;
4530 }
4531 cc = *p;
4532 *p = NUL;
4533 opt_type = get_option_value(var_start + 1, &numval,
4534 NULL, opt_flags);
4535 *p = cc;
4536 if (opt_type == -3)
4537 {
4538 semsg(_(e_unknown_option), var_start);
4539 goto theend;
4540 }
4541 if (opt_type == -2 || opt_type == 0)
4542 type = &t_string;
4543 else
4544 type = &t_number; // both number and boolean option
4545 }
4546 else if (*var_start == '$')
4547 {
4548 dest = dest_env;
4549 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004550 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004551 }
4552 else if (*var_start == '@')
4553 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004554 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004555 {
4556 emsg_invreg(var_start[1]);
4557 goto theend;
4558 }
4559 dest = dest_reg;
4560 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004561 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004562 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004563 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564 {
4565 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004566 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004567 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004568 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004569 {
4570 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004571 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004573 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004574 {
4575 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004576 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004578 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579 {
4580 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004581 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004582 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004583 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004584 {
4585 typval_T *vtv;
4586 int di_flags;
4587
4588 vimvaridx = find_vim_var(name + 2, &di_flags);
4589 if (vimvaridx < 0)
4590 {
4591 semsg(_(e_var_notfound), var_start);
4592 goto theend;
4593 }
4594 // We use the current value of "sandbox" here, is that OK?
4595 if (var_check_ro(di_flags, name, FALSE))
4596 goto theend;
4597 dest = dest_vimvar;
4598 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004599 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004600 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601 }
4602 else
4603 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004604 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004605
4606 for (idx = 0; reserved[idx] != NULL; ++idx)
4607 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004608 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004609 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004610 goto theend;
4611 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004612
4613 lvar = lookup_local(var_start, varlen, cctx);
4614 if (lvar == NULL)
4615 {
4616 CLEAR_FIELD(arg_lvar);
4617 if (lookup_arg(var_start, varlen,
4618 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4619 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004620 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 if (is_decl)
4622 {
4623 semsg(_(e_used_as_arg), name);
4624 goto theend;
4625 }
4626 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004627 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004628 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 if (lvar != NULL)
4630 {
4631 if (is_decl)
4632 {
4633 semsg(_("E1017: Variable already declared: %s"), name);
4634 goto theend;
4635 }
4636 else if (lvar->lv_const)
4637 {
4638 semsg(_("E1018: Cannot assign to a constant: %s"),
4639 name);
4640 goto theend;
4641 }
4642 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004643 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004644 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004645 int script_namespace = varlen > 1
4646 && STRNCMP(var_start, "s:", 2) == 0;
4647 int script_var = (script_namespace
4648 ? lookup_script(var_start + 2, varlen - 2)
4649 : lookup_script(var_start, varlen)) == OK;
4650 imported_T *import =
4651 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004652
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004653 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004654 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004655 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4656
4657 if (is_decl)
4658 {
4659 if (script_namespace)
4660 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004661 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004662 else
4663 semsg(_("E1054: Variable already declared in the script: %s"),
4664 name);
4665 goto theend;
4666 }
4667 else if (cctx->ctx_ufunc->uf_script_ctx_version
4668 == SCRIPT_VERSION_VIM9
4669 && script_namespace
4670 && !script_var && import == NULL)
4671 {
4672 semsg(_(e_unknown_var), name);
4673 goto theend;
4674 }
4675
4676 dest = dest_script;
4677
4678 // existing script-local variables should have a type
4679 scriptvar_sid = current_sctx.sc_sid;
4680 if (import != NULL)
4681 scriptvar_sid = import->imp_sid;
4682 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4683 rawname, TRUE);
4684 if (scriptvar_idx >= 0)
4685 {
4686 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4687 svar_T *sv =
4688 ((svar_T *)si->sn_var_vals.ga_data)
4689 + scriptvar_idx;
4690 type = sv->sv_type;
4691 }
4692 }
4693 else if (name[1] == ':' && name[2] != NUL)
4694 {
4695 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 name);
4697 goto theend;
4698 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004699 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004700 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004701 semsg(_(e_unknown_var), name);
4702 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004703 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004704 else if (check_defined(var_start, varlen, cctx) == FAIL)
4705 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004707 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004708
4709 if (declare_error)
4710 {
4711 vim9_declare_error(name);
4712 goto theend;
4713 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 }
4715
4716 // handle "a:name" as a name, not index "name" on "a"
4717 if (varlen > 1 || var_start[varlen] != ':')
4718 p = var_end;
4719
4720 if (dest != dest_option)
4721 {
4722 if (is_decl && *p == ':')
4723 {
4724 // parse optional type: "let var: type = expr"
4725 if (!VIM_ISWHITE(p[1]))
4726 {
4727 semsg(_(e_white_after), ":");
4728 goto theend;
4729 }
4730 p = skipwhite(p + 1);
4731 type = parse_type(&p, cctx->ctx_type_list);
4732 has_type = TRUE;
4733 }
4734 else if (lvar != NULL)
4735 type = lvar->lv_type;
4736 }
4737
4738 if (oplen == 3 && !heredoc && dest != dest_global
4739 && type->tt_type != VAR_STRING
4740 && type->tt_type != VAR_ANY)
4741 {
4742 emsg(_("E1019: Can only concatenate to string"));
4743 goto theend;
4744 }
4745
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004746 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004747 {
4748 if (oplen > 1 && !heredoc)
4749 {
4750 // +=, /=, etc. require an existing variable
4751 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4752 name);
4753 goto theend;
4754 }
4755
4756 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004757 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4758 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759 goto theend;
4760 lvar = reserve_local(cctx, var_start, varlen,
4761 cmdidx == CMD_const, type);
4762 if (lvar == NULL)
4763 goto theend;
4764 new_local = TRUE;
4765 }
4766
4767 member_type = type;
4768 if (var_end > var_start + varlen)
4769 {
4770 // Something follows after the variable: "var[idx]".
4771 if (is_decl)
4772 {
4773 emsg(_("E1087: cannot use an index when declaring a variable"));
4774 goto theend;
4775 }
4776
4777 if (var_start[varlen] == '[')
4778 {
4779 has_index = TRUE;
4780 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004781 member_type = &t_any;
4782 else
4783 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 }
4785 else
4786 {
4787 semsg("Not supported yet: %s", var_start);
4788 goto theend;
4789 }
4790 }
4791 else if (lvar == &arg_lvar)
4792 {
4793 semsg(_("E1090: Cannot assign to argument %s"), name);
4794 goto theend;
4795 }
4796
4797 if (!heredoc)
4798 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004799 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004800 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004801 if (oplen > 0 && var_count == 0)
4802 {
4803 // skip over the "=" and the expression
4804 p = skipwhite(op + oplen);
4805 compile_expr0(&p, cctx);
4806 }
4807 }
4808 else if (oplen > 0)
4809 {
4810 type_T *stacktype;
4811
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004812 // For "var = expr" evaluate the expression.
4813 if (var_count == 0)
4814 {
4815 int r;
4816
4817 // for "+=", "*=", "..=" etc. first load the current value
4818 if (*op != '=')
4819 {
4820 generate_loadvar(cctx, dest, name, lvar, type);
4821
4822 if (has_index)
4823 {
4824 // TODO: get member from list or dict
4825 emsg("Index with operation not supported yet");
4826 goto theend;
4827 }
4828 }
4829
4830 // Compile the expression. Temporarily hide the new local
4831 // variable here, it is not available to this expression.
4832 if (new_local)
4833 --cctx->ctx_locals.ga_len;
4834 instr_count = instr->ga_len;
4835 p = skipwhite(op + oplen);
4836 r = compile_expr0(&p, cctx);
4837 if (new_local)
4838 ++cctx->ctx_locals.ga_len;
4839 if (r == FAIL)
4840 goto theend;
4841 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004842 else if (semicolon && var_idx == var_count - 1)
4843 {
4844 // For "[var; var] = expr" get the rest of the list
4845 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4846 goto theend;
4847 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004848 else
4849 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004850 // For "[var, var] = expr" get the "var_idx" item from the
4851 // list.
4852 if (generate_GETITEM(cctx, var_idx) == FAIL)
4853 return FAIL;
4854 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004855
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004856 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004857 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004858 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004860 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004861 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004862 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004864 emsg(_(e_cannot_use_void));
4865 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004867 else if ((stacktype->tt_type == VAR_FUNC
4868 || stacktype->tt_type == VAR_PARTIAL)
4869 && var_wrong_func_name(name, TRUE))
4870 {
4871 goto theend;
4872 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873 else
4874 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004875 // An empty list or dict has a &t_void member,
4876 // for a variable that implies &t_any.
4877 if (stacktype == &t_list_empty)
4878 lvar->lv_type = &t_list_any;
4879 else if (stacktype == &t_dict_empty)
4880 lvar->lv_type = &t_dict_any;
4881 else
4882 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004883 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004884 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004885 else
4886 {
4887 type_T *use_type = lvar->lv_type;
4888
4889 if (has_index)
4890 {
4891 use_type = use_type->tt_member;
4892 if (use_type == NULL)
4893 use_type = &t_void;
4894 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004895 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004896 == FAIL)
4897 goto theend;
4898 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004899 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004900 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004901 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004902 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004906 emsg(_(e_const_req_value));
4907 goto theend;
4908 }
4909 else if (!has_type || dest == dest_option)
4910 {
4911 emsg(_(e_type_req));
4912 goto theend;
4913 }
4914 else
4915 {
4916 // variables are always initialized
4917 if (ga_grow(instr, 1) == FAIL)
4918 goto theend;
4919 switch (member_type->tt_type)
4920 {
4921 case VAR_BOOL:
4922 generate_PUSHBOOL(cctx, VVAL_FALSE);
4923 break;
4924 case VAR_FLOAT:
4925#ifdef FEAT_FLOAT
4926 generate_PUSHF(cctx, 0.0);
4927#endif
4928 break;
4929 case VAR_STRING:
4930 generate_PUSHS(cctx, NULL);
4931 break;
4932 case VAR_BLOB:
4933 generate_PUSHBLOB(cctx, NULL);
4934 break;
4935 case VAR_FUNC:
4936 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4937 break;
4938 case VAR_LIST:
4939 generate_NEWLIST(cctx, 0);
4940 break;
4941 case VAR_DICT:
4942 generate_NEWDICT(cctx, 0);
4943 break;
4944 case VAR_JOB:
4945 generate_PUSHJOB(cctx, NULL);
4946 break;
4947 case VAR_CHANNEL:
4948 generate_PUSHCHANNEL(cctx, NULL);
4949 break;
4950 case VAR_NUMBER:
4951 case VAR_UNKNOWN:
4952 case VAR_ANY:
4953 case VAR_PARTIAL:
4954 case VAR_VOID:
4955 case VAR_SPECIAL: // cannot happen
4956 generate_PUSHNR(cctx, 0);
4957 break;
4958 }
4959 }
4960 if (var_count == 0)
4961 end = p;
4962 }
4963
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004964 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004965 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004966 break;
4967
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 if (oplen > 0 && *op != '=')
4969 {
4970 type_T *expected = &t_number;
4971 type_T *stacktype;
4972
4973 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004974 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975
4976 if (*op == '.')
4977 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004978 else if (*op == '+')
4979 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004981 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 goto theend;
4983
4984 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004985 {
4986 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4987 goto theend;
4988 }
4989 else if (*op == '+')
4990 {
4991 if (generate_add_instr(cctx,
4992 operator_type(member_type, stacktype),
4993 member_type, stacktype) == FAIL)
4994 goto theend;
4995 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 else
4997 {
4998 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4999
5000 if (isn == NULL)
5001 goto theend;
5002 switch (*op)
5003 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5005 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5006 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5007 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 }
5010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005013 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005014 int r;
5015
5016 // Compile the "idx" in "var[idx]".
5017 if (new_local)
5018 --cctx->ctx_locals.ga_len;
5019 p = skipwhite(var_start + varlen + 1);
5020 r = compile_expr0(&p, cctx);
5021 if (new_local)
5022 ++cctx->ctx_locals.ga_len;
5023 if (r == FAIL)
5024 goto theend;
5025 if (*skipwhite(p) != ']')
5026 {
5027 emsg(_(e_missbrac));
5028 goto theend;
5029 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005030 if (type == &t_any)
5031 {
5032 type_T *idx_type = ((type_T **)stack->ga_data)[
5033 stack->ga_len - 1];
5034 // Index on variable of unknown type: guess the type from the
5035 // index type: number is dict, otherwise dict.
5036 // TODO: should do the assignment at runtime
5037 if (idx_type->tt_type == VAR_NUMBER)
5038 type = &t_list_any;
5039 else
5040 type = &t_dict_any;
5041 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 if (type->tt_type == VAR_DICT
5043 && may_generate_2STRING(-1, cctx) == FAIL)
5044 goto theend;
5045 if (type->tt_type == VAR_LIST
5046 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005047 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 {
5049 emsg(_(e_number_exp));
5050 goto theend;
5051 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005052
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 // Load the dict or list. On the stack we then have:
5054 // - value
5055 // - index
5056 // - variable
5057 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005058
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 if (type->tt_type == VAR_LIST)
5060 {
5061 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5062 return FAIL;
5063 }
5064 else if (type->tt_type == VAR_DICT)
5065 {
5066 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5067 return FAIL;
5068 }
5069 else
5070 {
5071 emsg(_(e_listreq));
5072 goto theend;
5073 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005074 }
5075 else
5076 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 switch (dest)
5078 {
5079 case dest_option:
5080 generate_STOREOPT(cctx, name + 1, opt_flags);
5081 break;
5082 case dest_global:
5083 // include g: with the name, easier to execute that way
5084 generate_STORE(cctx, ISN_STOREG, 0, name);
5085 break;
5086 case dest_buffer:
5087 // include b: with the name, easier to execute that way
5088 generate_STORE(cctx, ISN_STOREB, 0, name);
5089 break;
5090 case dest_window:
5091 // include w: with the name, easier to execute that way
5092 generate_STORE(cctx, ISN_STOREW, 0, name);
5093 break;
5094 case dest_tab:
5095 // include t: with the name, easier to execute that way
5096 generate_STORE(cctx, ISN_STORET, 0, name);
5097 break;
5098 case dest_env:
5099 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5100 break;
5101 case dest_reg:
5102 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5103 break;
5104 case dest_vimvar:
5105 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5106 break;
5107 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005108 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005109 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005110 {
5111 char_u *name_s = name;
5112
5113 // Include s: in the name for store_var()
5114 if (name[1] != ':')
5115 {
5116 int len = (int)STRLEN(name) + 3;
5117
5118 name_s = alloc(len);
5119 if (name_s == NULL)
5120 name_s = name;
5121 else
5122 vim_snprintf((char *)name_s, len,
5123 "s:%s", name);
5124 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005125 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5126 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 if (name_s != name)
5128 vim_free(name_s);
5129 }
5130 else
5131 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005132 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005133 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 break;
5135 case dest_local:
5136 if (lvar != NULL)
5137 {
5138 isn_T *isn = ((isn_T *)instr->ga_data)
5139 + instr->ga_len - 1;
5140
5141 // optimization: turn "var = 123" from ISN_PUSHNR +
5142 // ISN_STORE into ISN_STORENR
5143 if (!lvar->lv_from_outer
5144 && instr->ga_len == instr_count + 1
5145 && isn->isn_type == ISN_PUSHNR)
5146 {
5147 varnumber_T val = isn->isn_arg.number;
5148
5149 isn->isn_type = ISN_STORENR;
5150 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5151 isn->isn_arg.storenr.stnr_val = val;
5152 if (stack->ga_len > 0)
5153 --stack->ga_len;
5154 }
5155 else if (lvar->lv_from_outer)
5156 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005157 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158 else
5159 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5160 }
5161 break;
5162 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005163 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164
5165 if (var_idx + 1 < var_count)
5166 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005168
5169 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005170 if (var_count > 0 && !semicolon)
5171 {
5172 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5173 goto theend;
5174 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175
Bram Moolenaarb2097502020-07-19 17:17:02 +02005176 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177
5178theend:
5179 vim_free(name);
5180 return ret;
5181}
5182
5183/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005184 * Check if "name" can be "unlet".
5185 */
5186 int
5187check_vim9_unlet(char_u *name)
5188{
5189 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5190 {
5191 semsg(_("E1081: Cannot unlet %s"), name);
5192 return FAIL;
5193 }
5194 return OK;
5195}
5196
5197/*
5198 * Callback passed to ex_unletlock().
5199 */
5200 static int
5201compile_unlet(
5202 lval_T *lvp,
5203 char_u *name_end,
5204 exarg_T *eap,
5205 int deep UNUSED,
5206 void *coookie)
5207{
5208 cctx_T *cctx = coookie;
5209
5210 if (lvp->ll_tv == NULL)
5211 {
5212 char_u *p = lvp->ll_name;
5213 int cc = *name_end;
5214 int ret = OK;
5215
5216 // Normal name. Only supports g:, w:, t: and b: namespaces.
5217 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005218 if (*p == '$')
5219 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5220 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005221 ret = FAIL;
5222 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005223 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005224
5225 *name_end = cc;
5226 return ret;
5227 }
5228
5229 // TODO: unlet {list}[idx]
5230 // TODO: unlet {dict}[key]
5231 emsg("Sorry, :unlet not fully implemented yet");
5232 return FAIL;
5233}
5234
5235/*
5236 * compile "unlet var", "lock var" and "unlock var"
5237 * "arg" points to "var".
5238 */
5239 static char_u *
5240compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5241{
5242 char_u *p = arg;
5243
5244 if (eap->cmdidx != CMD_unlet)
5245 {
5246 emsg("Sorry, :lock and unlock not implemented yet");
5247 return NULL;
5248 }
5249
5250 if (*p == '!')
5251 {
5252 p = skipwhite(p + 1);
5253 eap->forceit = TRUE;
5254 }
5255
5256 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5257 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5258}
5259
5260/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 * Compile an :import command.
5262 */
5263 static char_u *
5264compile_import(char_u *arg, cctx_T *cctx)
5265{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005266 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267}
5268
5269/*
5270 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5271 */
5272 static int
5273compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5274{
5275 garray_T *instr = &cctx->ctx_instr;
5276 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5277
5278 if (endlabel == NULL)
5279 return FAIL;
5280 endlabel->el_next = *el;
5281 *el = endlabel;
5282 endlabel->el_end_label = instr->ga_len;
5283
5284 generate_JUMP(cctx, when, 0);
5285 return OK;
5286}
5287
5288 static void
5289compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5290{
5291 garray_T *instr = &cctx->ctx_instr;
5292
5293 while (*el != NULL)
5294 {
5295 endlabel_T *cur = (*el);
5296 isn_T *isn;
5297
5298 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5299 isn->isn_arg.jump.jump_where = instr->ga_len;
5300 *el = cur->el_next;
5301 vim_free(cur);
5302 }
5303}
5304
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005305 static void
5306compile_free_jump_to_end(endlabel_T **el)
5307{
5308 while (*el != NULL)
5309 {
5310 endlabel_T *cur = (*el);
5311
5312 *el = cur->el_next;
5313 vim_free(cur);
5314 }
5315}
5316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317/*
5318 * Create a new scope and set up the generic items.
5319 */
5320 static scope_T *
5321new_scope(cctx_T *cctx, scopetype_T type)
5322{
5323 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5324
5325 if (scope == NULL)
5326 return NULL;
5327 scope->se_outer = cctx->ctx_scope;
5328 cctx->ctx_scope = scope;
5329 scope->se_type = type;
5330 scope->se_local_count = cctx->ctx_locals.ga_len;
5331 return scope;
5332}
5333
5334/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005335 * Free the current scope and go back to the outer scope.
5336 */
5337 static void
5338drop_scope(cctx_T *cctx)
5339{
5340 scope_T *scope = cctx->ctx_scope;
5341
5342 if (scope == NULL)
5343 {
5344 iemsg("calling drop_scope() without a scope");
5345 return;
5346 }
5347 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005348 switch (scope->se_type)
5349 {
5350 case IF_SCOPE:
5351 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5352 case FOR_SCOPE:
5353 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5354 case WHILE_SCOPE:
5355 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5356 case TRY_SCOPE:
5357 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5358 case NO_SCOPE:
5359 case BLOCK_SCOPE:
5360 break;
5361 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005362 vim_free(scope);
5363}
5364
5365/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005366 * compile "if expr"
5367 *
5368 * "if expr" Produces instructions:
5369 * EVAL expr Push result of "expr"
5370 * JUMP_IF_FALSE end
5371 * ... body ...
5372 * end:
5373 *
5374 * "if expr | else" Produces instructions:
5375 * EVAL expr Push result of "expr"
5376 * JUMP_IF_FALSE else
5377 * ... body ...
5378 * JUMP_ALWAYS end
5379 * else:
5380 * ... body ...
5381 * end:
5382 *
5383 * "if expr1 | elseif expr2 | else" Produces instructions:
5384 * EVAL expr Push result of "expr"
5385 * JUMP_IF_FALSE elseif
5386 * ... body ...
5387 * JUMP_ALWAYS end
5388 * elseif:
5389 * EVAL expr Push result of "expr"
5390 * JUMP_IF_FALSE else
5391 * ... body ...
5392 * JUMP_ALWAYS end
5393 * else:
5394 * ... body ...
5395 * end:
5396 */
5397 static char_u *
5398compile_if(char_u *arg, cctx_T *cctx)
5399{
5400 char_u *p = arg;
5401 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005404 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005405 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406
Bram Moolenaara5565e42020-05-09 15:44:01 +02005407 CLEAR_FIELD(ppconst);
5408 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005409 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005410 clear_ppconst(&ppconst);
5411 return NULL;
5412 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005413 if (cctx->ctx_skip == SKIP_YES)
5414 clear_ppconst(&ppconst);
5415 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005416 {
5417 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005418 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005419 clear_ppconst(&ppconst);
5420 }
5421 else
5422 {
5423 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005424 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005425 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005426 return NULL;
5427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428
5429 scope = new_scope(cctx, IF_SCOPE);
5430 if (scope == NULL)
5431 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005432 scope->se_skip_save = skip_save;
5433 // "is_had_return" will be reset if any block does not end in :return
5434 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005436 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005437 {
5438 // "where" is set when ":elseif", "else" or ":endif" is found
5439 scope->se_u.se_if.is_if_label = instr->ga_len;
5440 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5441 }
5442 else
5443 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444
5445 return p;
5446}
5447
5448 static char_u *
5449compile_elseif(char_u *arg, cctx_T *cctx)
5450{
5451 char_u *p = arg;
5452 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005453 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 isn_T *isn;
5455 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005456 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457
5458 if (scope == NULL || scope->se_type != IF_SCOPE)
5459 {
5460 emsg(_(e_elseif_without_if));
5461 return NULL;
5462 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005463 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005464 if (!cctx->ctx_had_return)
5465 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005467 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005468 {
5469 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005471 return NULL;
5472 // previous "if" or "elseif" jumps here
5473 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5474 isn->isn_arg.jump.jump_where = instr->ga_len;
5475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005477 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005478 CLEAR_FIELD(ppconst);
5479 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005480 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005481 clear_ppconst(&ppconst);
5482 return NULL;
5483 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005484 if (scope->se_skip_save == SKIP_YES)
5485 clear_ppconst(&ppconst);
5486 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005487 {
5488 // The expression results in a constant.
5489 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005490 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005491 clear_ppconst(&ppconst);
5492 scope->se_u.se_if.is_if_label = -1;
5493 }
5494 else
5495 {
5496 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005497 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005498 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005499 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005501 // "where" is set when ":elseif", "else" or ":endif" is found
5502 scope->se_u.se_if.is_if_label = instr->ga_len;
5503 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505
5506 return p;
5507}
5508
5509 static char_u *
5510compile_else(char_u *arg, cctx_T *cctx)
5511{
5512 char_u *p = arg;
5513 garray_T *instr = &cctx->ctx_instr;
5514 isn_T *isn;
5515 scope_T *scope = cctx->ctx_scope;
5516
5517 if (scope == NULL || scope->se_type != IF_SCOPE)
5518 {
5519 emsg(_(e_else_without_if));
5520 return NULL;
5521 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005522 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005523 if (!cctx->ctx_had_return)
5524 scope->se_u.se_if.is_had_return = FALSE;
5525 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526
Bram Moolenaarefd88552020-06-18 20:50:10 +02005527 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005528 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005529 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005530 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005531 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005532 if (!cctx->ctx_had_return
5533 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5534 JUMP_ALWAYS, cctx) == FAIL)
5535 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005536 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005537
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005538 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005539 {
5540 if (scope->se_u.se_if.is_if_label >= 0)
5541 {
5542 // previous "if" or "elseif" jumps here
5543 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5544 isn->isn_arg.jump.jump_where = instr->ga_len;
5545 scope->se_u.se_if.is_if_label = -1;
5546 }
5547 }
5548
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005549 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005550 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552
5553 return p;
5554}
5555
5556 static char_u *
5557compile_endif(char_u *arg, cctx_T *cctx)
5558{
5559 scope_T *scope = cctx->ctx_scope;
5560 ifscope_T *ifscope;
5561 garray_T *instr = &cctx->ctx_instr;
5562 isn_T *isn;
5563
5564 if (scope == NULL || scope->se_type != IF_SCOPE)
5565 {
5566 emsg(_(e_endif_without_if));
5567 return NULL;
5568 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005569 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005570 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005571 if (!cctx->ctx_had_return)
5572 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005574 if (scope->se_u.se_if.is_if_label >= 0)
5575 {
5576 // previous "if" or "elseif" jumps here
5577 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5578 isn->isn_arg.jump.jump_where = instr->ga_len;
5579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580 // Fill in the "end" label in jumps at the end of the blocks.
5581 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005582 cctx->ctx_skip = scope->se_skip_save;
5583
5584 // If all the blocks end in :return and there is an :else then the
5585 // had_return flag is set.
5586 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005588 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 return arg;
5590}
5591
5592/*
5593 * compile "for var in expr"
5594 *
5595 * Produces instructions:
5596 * PUSHNR -1
5597 * STORE loop-idx Set index to -1
5598 * EVAL expr Push result of "expr"
5599 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5600 * - if beyond end, jump to "end"
5601 * - otherwise get item from list and push it
5602 * STORE var Store item in "var"
5603 * ... body ...
5604 * JUMP top Jump back to repeat
5605 * end: DROP Drop the result of "expr"
5606 *
5607 */
5608 static char_u *
5609compile_for(char_u *arg, cctx_T *cctx)
5610{
5611 char_u *p;
5612 size_t varlen;
5613 garray_T *instr = &cctx->ctx_instr;
5614 garray_T *stack = &cctx->ctx_type_stack;
5615 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005616 lvar_T *loop_lvar; // loop iteration variable
5617 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 type_T *vartype;
5619
5620 // TODO: list of variables: "for [key, value] in dict"
5621 // parse "var"
5622 for (p = arg; eval_isnamec1(*p); ++p)
5623 ;
5624 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005625 var_lvar = lookup_local(arg, varlen, cctx);
5626 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 {
5628 semsg(_("E1023: variable already defined: %s"), arg);
5629 return NULL;
5630 }
5631
5632 // consume "in"
5633 p = skipwhite(p);
5634 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5635 {
5636 emsg(_(e_missing_in));
5637 return NULL;
5638 }
5639 p = skipwhite(p + 2);
5640
5641
5642 scope = new_scope(cctx, FOR_SCOPE);
5643 if (scope == NULL)
5644 return NULL;
5645
5646 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005647 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5648 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005649 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005650 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005651 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005656 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5657 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005658 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005659 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005660 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005664 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665
5666 // compile "expr", it remains on the stack until "endfor"
5667 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005668 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005669 {
5670 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005674 // Now that we know the type of "var", check that it is a list, now or at
5675 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005677 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005679 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005680 return NULL;
5681 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005682 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005683 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684
5685 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005686 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005688 generate_FOR(cctx, loop_lvar->lv_idx);
5689 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690
5691 return arg;
5692}
5693
5694/*
5695 * compile "endfor"
5696 */
5697 static char_u *
5698compile_endfor(char_u *arg, cctx_T *cctx)
5699{
5700 garray_T *instr = &cctx->ctx_instr;
5701 scope_T *scope = cctx->ctx_scope;
5702 forscope_T *forscope;
5703 isn_T *isn;
5704
5705 if (scope == NULL || scope->se_type != FOR_SCOPE)
5706 {
5707 emsg(_(e_for));
5708 return NULL;
5709 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005710 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005712 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713
5714 // At end of ":for" scope jump back to the FOR instruction.
5715 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5716
5717 // Fill in the "end" label in the FOR statement so it can jump here
5718 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5719 isn->isn_arg.forloop.for_end = instr->ga_len;
5720
5721 // Fill in the "end" label any BREAK statements
5722 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5723
5724 // Below the ":for" scope drop the "expr" list from the stack.
5725 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5726 return NULL;
5727
5728 vim_free(scope);
5729
5730 return arg;
5731}
5732
5733/*
5734 * compile "while expr"
5735 *
5736 * Produces instructions:
5737 * top: EVAL expr Push result of "expr"
5738 * JUMP_IF_FALSE end jump if false
5739 * ... body ...
5740 * JUMP top Jump back to repeat
5741 * end:
5742 *
5743 */
5744 static char_u *
5745compile_while(char_u *arg, cctx_T *cctx)
5746{
5747 char_u *p = arg;
5748 garray_T *instr = &cctx->ctx_instr;
5749 scope_T *scope;
5750
5751 scope = new_scope(cctx, WHILE_SCOPE);
5752 if (scope == NULL)
5753 return NULL;
5754
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005755 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756
5757 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005758 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 return NULL;
5760
5761 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005762 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 JUMP_IF_FALSE, cctx) == FAIL)
5764 return FAIL;
5765
5766 return p;
5767}
5768
5769/*
5770 * compile "endwhile"
5771 */
5772 static char_u *
5773compile_endwhile(char_u *arg, cctx_T *cctx)
5774{
5775 scope_T *scope = cctx->ctx_scope;
5776
5777 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5778 {
5779 emsg(_(e_while));
5780 return NULL;
5781 }
5782 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005783 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784
5785 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005786 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
5788 // Fill in the "end" label in the WHILE statement so it can jump here.
5789 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005790 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791
5792 vim_free(scope);
5793
5794 return arg;
5795}
5796
5797/*
5798 * compile "continue"
5799 */
5800 static char_u *
5801compile_continue(char_u *arg, cctx_T *cctx)
5802{
5803 scope_T *scope = cctx->ctx_scope;
5804
5805 for (;;)
5806 {
5807 if (scope == NULL)
5808 {
5809 emsg(_(e_continue));
5810 return NULL;
5811 }
5812 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5813 break;
5814 scope = scope->se_outer;
5815 }
5816
5817 // Jump back to the FOR or WHILE instruction.
5818 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5820 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 return arg;
5822}
5823
5824/*
5825 * compile "break"
5826 */
5827 static char_u *
5828compile_break(char_u *arg, cctx_T *cctx)
5829{
5830 scope_T *scope = cctx->ctx_scope;
5831 endlabel_T **el;
5832
5833 for (;;)
5834 {
5835 if (scope == NULL)
5836 {
5837 emsg(_(e_break));
5838 return NULL;
5839 }
5840 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5841 break;
5842 scope = scope->se_outer;
5843 }
5844
5845 // Jump to the end of the FOR or WHILE loop.
5846 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005847 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005849 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5851 return FAIL;
5852
5853 return arg;
5854}
5855
5856/*
5857 * compile "{" start of block
5858 */
5859 static char_u *
5860compile_block(char_u *arg, cctx_T *cctx)
5861{
5862 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5863 return NULL;
5864 return skipwhite(arg + 1);
5865}
5866
5867/*
5868 * compile end of block: drop one scope
5869 */
5870 static void
5871compile_endblock(cctx_T *cctx)
5872{
5873 scope_T *scope = cctx->ctx_scope;
5874
5875 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005876 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 vim_free(scope);
5878}
5879
5880/*
5881 * compile "try"
5882 * Creates a new scope for the try-endtry, pointing to the first catch and
5883 * finally.
5884 * Creates another scope for the "try" block itself.
5885 * TRY instruction sets up exception handling at runtime.
5886 *
5887 * "try"
5888 * TRY -> catch1, -> finally push trystack entry
5889 * ... try block
5890 * "throw {exception}"
5891 * EVAL {exception}
5892 * THROW create exception
5893 * ... try block
5894 * " catch {expr}"
5895 * JUMP -> finally
5896 * catch1: PUSH exeception
5897 * EVAL {expr}
5898 * MATCH
5899 * JUMP nomatch -> catch2
5900 * CATCH remove exception
5901 * ... catch block
5902 * " catch"
5903 * JUMP -> finally
5904 * catch2: CATCH remove exception
5905 * ... catch block
5906 * " finally"
5907 * finally:
5908 * ... finally block
5909 * " endtry"
5910 * ENDTRY pop trystack entry, may rethrow
5911 */
5912 static char_u *
5913compile_try(char_u *arg, cctx_T *cctx)
5914{
5915 garray_T *instr = &cctx->ctx_instr;
5916 scope_T *try_scope;
5917 scope_T *scope;
5918
5919 // scope that holds the jumps that go to catch/finally/endtry
5920 try_scope = new_scope(cctx, TRY_SCOPE);
5921 if (try_scope == NULL)
5922 return NULL;
5923
5924 // "catch" is set when the first ":catch" is found.
5925 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005926 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 if (generate_instr(cctx, ISN_TRY) == NULL)
5928 return NULL;
5929
5930 // scope for the try block itself
5931 scope = new_scope(cctx, BLOCK_SCOPE);
5932 if (scope == NULL)
5933 return NULL;
5934
5935 return arg;
5936}
5937
5938/*
5939 * compile "catch {expr}"
5940 */
5941 static char_u *
5942compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5943{
5944 scope_T *scope = cctx->ctx_scope;
5945 garray_T *instr = &cctx->ctx_instr;
5946 char_u *p;
5947 isn_T *isn;
5948
5949 // end block scope from :try or :catch
5950 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5951 compile_endblock(cctx);
5952 scope = cctx->ctx_scope;
5953
5954 // Error if not in a :try scope
5955 if (scope == NULL || scope->se_type != TRY_SCOPE)
5956 {
5957 emsg(_(e_catch));
5958 return NULL;
5959 }
5960
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005961 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 {
5963 emsg(_("E1033: catch unreachable after catch-all"));
5964 return NULL;
5965 }
5966
5967 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005968 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 JUMP_ALWAYS, cctx) == FAIL)
5970 return NULL;
5971
5972 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005973 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 if (isn->isn_arg.try.try_catch == 0)
5975 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005976 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 {
5978 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005979 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 isn->isn_arg.jump.jump_where = instr->ga_len;
5981 }
5982
5983 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005984 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005986 scope->se_u.se_try.ts_caught_all = TRUE;
5987 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 }
5989 else
5990 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005991 char_u *end;
5992 char_u *pat;
5993 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005994 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005995 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 // Push v:exception, push {expr} and MATCH
5998 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5999
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006000 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006001 if (*end != *p)
6002 {
6003 semsg(_("E1067: Separator mismatch: %s"), p);
6004 vim_free(tofree);
6005 return FAIL;
6006 }
6007 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006008 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006009 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006010 len = (int)(end - tofree);
6011 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006012 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006013 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006014 if (pat == NULL)
6015 return FAIL;
6016 if (generate_PUSHS(cctx, pat) == FAIL)
6017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6020 return NULL;
6021
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6024 return NULL;
6025 }
6026
6027 if (generate_instr(cctx, ISN_CATCH) == NULL)
6028 return NULL;
6029
6030 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6031 return NULL;
6032 return p;
6033}
6034
6035 static char_u *
6036compile_finally(char_u *arg, cctx_T *cctx)
6037{
6038 scope_T *scope = cctx->ctx_scope;
6039 garray_T *instr = &cctx->ctx_instr;
6040 isn_T *isn;
6041
6042 // end block scope from :try or :catch
6043 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6044 compile_endblock(cctx);
6045 scope = cctx->ctx_scope;
6046
6047 // Error if not in a :try scope
6048 if (scope == NULL || scope->se_type != TRY_SCOPE)
6049 {
6050 emsg(_(e_finally));
6051 return NULL;
6052 }
6053
6054 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006055 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 if (isn->isn_arg.try.try_finally != 0)
6057 {
6058 emsg(_(e_finally_dup));
6059 return NULL;
6060 }
6061
6062 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006063 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar585fea72020-04-02 22:33:21 +02006065 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006066 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 {
6068 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006069 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006071 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 }
6073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 // TODO: set index in ts_finally_label jumps
6075
6076 return arg;
6077}
6078
6079 static char_u *
6080compile_endtry(char_u *arg, cctx_T *cctx)
6081{
6082 scope_T *scope = cctx->ctx_scope;
6083 garray_T *instr = &cctx->ctx_instr;
6084 isn_T *isn;
6085
6086 // end block scope from :catch or :finally
6087 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6088 compile_endblock(cctx);
6089 scope = cctx->ctx_scope;
6090
6091 // Error if not in a :try scope
6092 if (scope == NULL || scope->se_type != TRY_SCOPE)
6093 {
6094 if (scope == NULL)
6095 emsg(_(e_no_endtry));
6096 else if (scope->se_type == WHILE_SCOPE)
6097 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006098 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 emsg(_(e_endfor));
6100 else
6101 emsg(_(e_endif));
6102 return NULL;
6103 }
6104
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006105 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6107 {
6108 emsg(_("E1032: missing :catch or :finally"));
6109 return NULL;
6110 }
6111
6112 // Fill in the "end" label in jumps at the end of the blocks, if not done
6113 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006114 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
6116 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006117 if (isn->isn_arg.try.try_catch == 0)
6118 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 if (isn->isn_arg.try.try_finally == 0)
6120 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006121
6122 if (scope->se_u.se_try.ts_catch_label != 0)
6123 {
6124 // Last catch without match jumps here
6125 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6126 isn->isn_arg.jump.jump_where = instr->ga_len;
6127 }
6128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 compile_endblock(cctx);
6130
6131 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6132 return NULL;
6133 return arg;
6134}
6135
6136/*
6137 * compile "throw {expr}"
6138 */
6139 static char_u *
6140compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6141{
6142 char_u *p = skipwhite(arg);
6143
Bram Moolenaara5565e42020-05-09 15:44:01 +02006144 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 return NULL;
6146 if (may_generate_2STRING(-1, cctx) == FAIL)
6147 return NULL;
6148 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6149 return NULL;
6150
6151 return p;
6152}
6153
6154/*
6155 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006156 * compile "echomsg expr"
6157 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006158 * compile "execute expr"
6159 */
6160 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006161compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006162{
6163 char_u *p = arg;
6164 int count = 0;
6165
6166 for (;;)
6167 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006168 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006169 return NULL;
6170 ++count;
6171 p = skipwhite(p);
6172 if (ends_excmd(*p))
6173 break;
6174 }
6175
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006176 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6177 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6178 else if (cmdidx == CMD_execute)
6179 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6180 else if (cmdidx == CMD_echomsg)
6181 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6182 else
6183 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 return p;
6185}
6186
6187/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006188 * A command that is not compiled, execute with legacy code.
6189 */
6190 static char_u *
6191compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6192{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006193 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006194 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006195 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006196
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006197 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006198 goto theend;
6199
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006200 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006201 {
6202 long argt = excmd_get_argt(eap->cmdidx);
6203 int usefilter = FALSE;
6204
6205 has_expr = argt & (EX_XFILE | EX_EXPAND);
6206
6207 // If the command can be followed by a bar, find the bar and truncate
6208 // it, so that the following command can be compiled.
6209 // The '|' is overwritten with a NUL, it is put back below.
6210 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6211 && *eap->arg == '!')
6212 // :w !filter or :r !filter or :r! filter
6213 usefilter = TRUE;
6214 if ((argt & EX_TRLBAR) && !usefilter)
6215 {
6216 separate_nextcmd(eap);
6217 if (eap->nextcmd != NULL)
6218 nextcmd = eap->nextcmd;
6219 }
6220 }
6221
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006222 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6223 {
6224 // expand filename in "syntax include [@group] filename"
6225 has_expr = TRUE;
6226 eap->arg = skipwhite(eap->arg + 7);
6227 if (*eap->arg == '@')
6228 eap->arg = skiptowhite(eap->arg);
6229 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006230
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006231 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006232 {
6233 int count = 0;
6234 char_u *start = skipwhite(line);
6235
6236 // :cmd xxx`=expr1`yyy`=expr2`zzz
6237 // PUSHS ":cmd xxx"
6238 // eval expr1
6239 // PUSHS "yyy"
6240 // eval expr2
6241 // PUSHS "zzz"
6242 // EXECCONCAT 5
6243 for (;;)
6244 {
6245 if (p > start)
6246 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006247 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006248 ++count;
6249 }
6250 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006251 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006252 return NULL;
6253 may_generate_2STRING(-1, cctx);
6254 ++count;
6255 p = skipwhite(p);
6256 if (*p != '`')
6257 {
6258 emsg(_("E1083: missing backtick"));
6259 return NULL;
6260 }
6261 start = p + 1;
6262
6263 p = (char_u *)strstr((char *)start, "`=");
6264 if (p == NULL)
6265 {
6266 if (*skipwhite(start) != NUL)
6267 {
6268 generate_PUSHS(cctx, vim_strsave(start));
6269 ++count;
6270 }
6271 break;
6272 }
6273 }
6274 generate_EXECCONCAT(cctx, count);
6275 }
6276 else
6277 generate_EXEC(cctx, line);
6278
6279theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006280 if (*nextcmd != NUL)
6281 {
6282 // the parser expects a pointer to the bar, put it back
6283 --nextcmd;
6284 *nextcmd = '|';
6285 }
6286
6287 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006288}
6289
6290/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006291 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006292 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006293 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006294 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006295add_def_function(ufunc_T *ufunc)
6296{
6297 dfunc_T *dfunc;
6298
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006299 if (def_functions.ga_len == 0)
6300 {
6301 // The first position is not used, so that a zero uf_dfunc_idx means it
6302 // wasn't set.
6303 if (ga_grow(&def_functions, 1) == FAIL)
6304 return FAIL;
6305 ++def_functions.ga_len;
6306 }
6307
Bram Moolenaar09689a02020-05-09 22:50:08 +02006308 // Add the function to "def_functions".
6309 if (ga_grow(&def_functions, 1) == FAIL)
6310 return FAIL;
6311 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6312 CLEAR_POINTER(dfunc);
6313 dfunc->df_idx = def_functions.ga_len;
6314 ufunc->uf_dfunc_idx = dfunc->df_idx;
6315 dfunc->df_ufunc = ufunc;
6316 ++def_functions.ga_len;
6317 return OK;
6318}
6319
6320/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 * After ex_function() has collected all the function lines: parse and compile
6322 * the lines into instructions.
6323 * Adds the function to "def_functions".
6324 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6325 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006326 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006327 * This can be used recursively through compile_lambda(), which may reallocate
6328 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006329 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006331 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006332compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 char_u *line = NULL;
6335 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337 cctx_T cctx;
6338 garray_T *instr;
6339 int called_emsg_before = called_emsg;
6340 int ret = FAIL;
6341 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006342 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006343 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006344 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006346 // When using a function that was compiled before: Free old instructions.
6347 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006348 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006350 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6351 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006352 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006354 else
6355 {
6356 if (add_def_function(ufunc) == FAIL)
6357 return FAIL;
6358 new_def_function = TRUE;
6359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
Bram Moolenaar985116a2020-07-12 17:31:09 +02006361 ufunc->uf_def_status = UF_COMPILING;
6362
Bram Moolenaara80faa82020-04-12 19:37:17 +02006363 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 cctx.ctx_ufunc = ufunc;
6365 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006366 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6368 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6369 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6370 cctx.ctx_type_list = &ufunc->uf_type_list;
6371 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6372 instr = &cctx.ctx_instr;
6373
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006374 // Set the context to the function, it may be compiled when called from
6375 // another script. Set the script version to the most modern one.
6376 // The line number will be set in next_line_from_context().
6377 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6379
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006380 // Make sure error messages are OK.
6381 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6382 if (do_estack_push)
6383 estack_push_ufunc(ufunc, 1);
6384
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006385 if (ufunc->uf_def_args.ga_len > 0)
6386 {
6387 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006388 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006389 int i;
6390 char_u *arg;
6391 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006392 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006393
6394 // Produce instructions for the default values of optional arguments.
6395 // Store the instruction index in uf_def_arg_idx[] so that we know
6396 // where to start when the function is called, depending on the number
6397 // of arguments.
6398 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6399 if (ufunc->uf_def_arg_idx == NULL)
6400 goto erret;
6401 for (i = 0; i < count; ++i)
6402 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006403 garray_T *stack = &cctx.ctx_type_stack;
6404 type_T *val_type;
6405 int arg_idx = first_def_arg + i;
6406
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006407 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6408 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006409 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006410 goto erret;
6411
6412 // If no type specified use the type of the default value.
6413 // Otherwise check that the default value type matches the
6414 // specified type.
6415 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6416 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006417 {
6418 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006419 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006420 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006421 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006422 == FAIL)
6423 {
6424 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6425 arg_idx + 1);
6426 goto erret;
6427 }
6428
6429 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006430 goto erret;
6431 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006432 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006433
6434 if (did_set_arg_type)
6435 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006436 }
6437
6438 /*
6439 * Loop over all the lines of the function and generate instructions.
6440 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 for (;;)
6442 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006443 exarg_T ea;
6444 cmdmod_T save_cmdmod;
6445 int starts_with_colon = FALSE;
6446 char_u *cmd;
6447 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006448
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006449 // Bail out on the first error to avoid a flood of errors and report
6450 // the right line number when inside try/catch.
6451 if (emsg_before != called_emsg)
6452 goto erret;
6453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 if (line != NULL && *line == '|')
6455 // the line continues after a '|'
6456 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006457 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006458 && !(*line == '#' && (line == cctx.ctx_line_start
6459 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006461 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 goto erret;
6463 }
6464 else
6465 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006466 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006467 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006468 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006471 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472
Bram Moolenaara80faa82020-04-12 19:37:17 +02006473 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474 ea.cmdlinep = &line;
6475 ea.cmd = skipwhite(line);
6476
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006477 // Some things can be recognized by the first character.
6478 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006480 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006481 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006482 if (ea.cmd[1] != '{')
6483 {
6484 line = (char_u *)"";
6485 continue;
6486 }
6487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006489 case '}':
6490 {
6491 // "}" ends a block scope
6492 scopetype_T stype = cctx.ctx_scope == NULL
6493 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006495 if (stype == BLOCK_SCOPE)
6496 {
6497 compile_endblock(&cctx);
6498 line = ea.cmd;
6499 }
6500 else
6501 {
6502 emsg(_("E1025: using } outside of a block scope"));
6503 goto erret;
6504 }
6505 if (line != NULL)
6506 line = skipwhite(ea.cmd + 1);
6507 continue;
6508 }
6509
6510 case '{':
6511 // "{" starts a block scope
6512 // "{'a': 1}->func() is something else
6513 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6514 {
6515 line = compile_block(ea.cmd, &cctx);
6516 continue;
6517 }
6518 break;
6519
6520 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006521 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006522 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 }
6524
6525 /*
6526 * COMMAND MODIFIERS
6527 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006528 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6530 {
6531 if (errormsg != NULL)
6532 goto erret;
6533 // empty line or comment
6534 line = (char_u *)"";
6535 continue;
6536 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006537 // TODO: use modifiers in the command
6538 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006539 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540
6541 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006542 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006544 {
6545 if (*ea.cmd == '(')
6546 // not for "call()"
6547 ea.cmd = p;
6548 else
6549 ea.cmd = skipwhite(ea.cmd);
6550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006552 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006554 char_u *pskip;
6555
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006556 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006557 // find what follows.
6558 // Skip over "var.member", "var[idx]" and the like.
6559 // Also "&opt = val", "$ENV = val" and "@r = val".
6560 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006561 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006562 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006563 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006565 char_u *var_end;
6566 int oplen;
6567 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568
Bram Moolenaar65821722020-08-02 18:58:54 +02006569 if (ea.cmd[0] == '@')
6570 var_end = ea.cmd + 2;
6571 else
6572 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006573 FNE_CHECK_START | FNE_INCL_BR);
6574 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006575 if (oplen > 0)
6576 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006577 size_t len = p - ea.cmd;
6578
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006579 // Recognize an assignment if we recognize the variable
6580 // name:
6581 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006582 // "local = expr" where "local" is a local var.
6583 // "script = expr" where "script" is a script-local var.
6584 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006585 // "&opt = expr"
6586 // "$ENV = expr"
6587 // "@r = expr"
6588 if (*ea.cmd == '&'
6589 || *ea.cmd == '$'
6590 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006591 || ((len) > 2 && ea.cmd[1] == ':')
6592 || lookup_local(ea.cmd, len, &cctx) != NULL
6593 || lookup_arg(ea.cmd, len, NULL, NULL,
6594 NULL, &cctx) == OK
6595 || lookup_script(ea.cmd, len) == OK
6596 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006597 {
6598 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006599 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006600 goto erret;
6601 continue;
6602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 }
6604 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006605
6606 if (*ea.cmd == '[')
6607 {
6608 // [var, var] = expr
6609 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6610 if (line == NULL)
6611 goto erret;
6612 if (line != ea.cmd)
6613 continue;
6614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 }
6616
6617 /*
6618 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006619 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006621 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006622 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006623 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006624 ea.cmd = skip_range(ea.cmd, NULL);
6625 if (ea.cmd > cmd && !starts_with_colon)
6626 {
6627 emsg(_(e_colon_required));
6628 goto erret;
6629 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006630 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006631 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006632 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006633 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634
6635 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6636 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006637 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006638 {
6639 line += STRLEN(line);
6640 continue;
6641 }
6642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006644 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006646 // CMD_let cannot happen, compile_assignment() above is used
6647 iemsg("Command from find_ex_command() not handled");
6648 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 }
6651
6652 p = skipwhite(p);
6653
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006654 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006655 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006656 && ea.cmdidx != CMD_elseif
6657 && ea.cmdidx != CMD_else
6658 && ea.cmdidx != CMD_endif)
6659 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006660 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006661 continue;
6662 }
6663
Bram Moolenaarefd88552020-06-18 20:50:10 +02006664 if (ea.cmdidx != CMD_elseif
6665 && ea.cmdidx != CMD_else
6666 && ea.cmdidx != CMD_endif
6667 && ea.cmdidx != CMD_endfor
6668 && ea.cmdidx != CMD_endwhile
6669 && ea.cmdidx != CMD_catch
6670 && ea.cmdidx != CMD_finally
6671 && ea.cmdidx != CMD_endtry)
6672 {
6673 if (cctx.ctx_had_return)
6674 {
6675 emsg(_("E1095: Unreachable code after :return"));
6676 goto erret;
6677 }
6678 }
6679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 switch (ea.cmdidx)
6681 {
6682 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006683 ea.arg = p;
6684 line = compile_nested_function(&ea, &cctx);
6685 break;
6686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006688 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689 goto erret;
6690
6691 case CMD_return:
6692 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006693 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 break;
6695
6696 case CMD_let:
6697 case CMD_const:
6698 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006699 if (line == p)
6700 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 break;
6702
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006703 case CMD_unlet:
6704 case CMD_unlockvar:
6705 case CMD_lockvar:
6706 line = compile_unletlock(p, &ea, &cctx);
6707 break;
6708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 case CMD_import:
6710 line = compile_import(p, &cctx);
6711 break;
6712
6713 case CMD_if:
6714 line = compile_if(p, &cctx);
6715 break;
6716 case CMD_elseif:
6717 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006718 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 break;
6720 case CMD_else:
6721 line = compile_else(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_endif:
6725 line = compile_endif(p, &cctx);
6726 break;
6727
6728 case CMD_while:
6729 line = compile_while(p, &cctx);
6730 break;
6731 case CMD_endwhile:
6732 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006733 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 break;
6735
6736 case CMD_for:
6737 line = compile_for(p, &cctx);
6738 break;
6739 case CMD_endfor:
6740 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006741 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 break;
6743 case CMD_continue:
6744 line = compile_continue(p, &cctx);
6745 break;
6746 case CMD_break:
6747 line = compile_break(p, &cctx);
6748 break;
6749
6750 case CMD_try:
6751 line = compile_try(p, &cctx);
6752 break;
6753 case CMD_catch:
6754 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006755 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 break;
6757 case CMD_finally:
6758 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006759 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 break;
6761 case CMD_endtry:
6762 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006763 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 break;
6765 case CMD_throw:
6766 line = compile_throw(p, &cctx);
6767 break;
6768
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006769 case CMD_eval:
6770 if (compile_expr0(&p, &cctx) == FAIL)
6771 goto erret;
6772
6773 // drop the return value
6774 generate_instr_drop(&cctx, ISN_DROP, 1);
6775
6776 line = skipwhite(p);
6777 break;
6778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006781 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006782 case CMD_echomsg:
6783 case CMD_echoerr:
6784 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006785 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006787 // TODO: other commands with an expression argument
6788
Bram Moolenaarae616492020-07-28 20:07:27 +02006789 case CMD_append:
6790 case CMD_change:
6791 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006792 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006793 case CMD_xit:
6794 not_in_vim9(&ea);
6795 goto erret;
6796
Bram Moolenaar002262f2020-07-08 17:47:57 +02006797 case CMD_SIZE:
6798 semsg(_("E476: Invalid command: %s"), ea.cmd);
6799 goto erret;
6800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006802 // Not recognized, execute with do_cmdline_cmd().
6803 ea.arg = p;
6804 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 break;
6806 }
6807 if (line == NULL)
6808 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006809 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 if (cctx.ctx_type_stack.ga_len < 0)
6812 {
6813 iemsg("Type stack underflow");
6814 goto erret;
6815 }
6816 }
6817
6818 if (cctx.ctx_scope != NULL)
6819 {
6820 if (cctx.ctx_scope->se_type == IF_SCOPE)
6821 emsg(_(e_endif));
6822 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6823 emsg(_(e_endwhile));
6824 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6825 emsg(_(e_endfor));
6826 else
6827 emsg(_("E1026: Missing }"));
6828 goto erret;
6829 }
6830
Bram Moolenaarefd88552020-06-18 20:50:10 +02006831 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 {
6833 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6834 {
6835 emsg(_("E1027: Missing return statement"));
6836 goto erret;
6837 }
6838
6839 // Return zero if there is no return at the end.
6840 generate_PUSHNR(&cctx, 0);
6841 generate_instr(&cctx, ISN_RETURN);
6842 }
6843
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006844 {
6845 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6846 + ufunc->uf_dfunc_idx;
6847 dfunc->df_deleted = FALSE;
6848 dfunc->df_instr = instr->ga_data;
6849 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006850 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006851 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006852 if (cctx.ctx_outer_used)
6853 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006854 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856
6857 ret = OK;
6858
6859erret:
6860 if (ret == FAIL)
6861 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006862 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006863 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6864 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006865
6866 for (idx = 0; idx < instr->ga_len; ++idx)
6867 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006869
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006870 // If using the last entry in the table and it was added above, we
6871 // might as well remove it.
6872 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006873 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006874 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006875 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006876 ufunc->uf_dfunc_idx = 0;
6877 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006878 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006879
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006880 while (cctx.ctx_scope != NULL)
6881 drop_scope(&cctx);
6882
Bram Moolenaar20431c92020-03-20 18:39:46 +01006883 // Don't execute this function body.
6884 ga_clear_strings(&ufunc->uf_lines);
6885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 if (errormsg != NULL)
6887 emsg(errormsg);
6888 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006889 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 }
6891
6892 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006893 if (do_estack_push)
6894 estack_pop();
6895
Bram Moolenaar20431c92020-03-20 18:39:46 +01006896 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006897 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006899 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900}
6901
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006902 void
6903set_function_type(ufunc_T *ufunc)
6904{
6905 int varargs = ufunc->uf_va_name != NULL;
6906 int argcount = ufunc->uf_args.ga_len;
6907
6908 // Create a type for the function, with the return type and any
6909 // argument types.
6910 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6911 // The type is included in "tt_args".
6912 if (argcount > 0 || varargs)
6913 {
6914 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6915 argcount, &ufunc->uf_type_list);
6916 // Add argument types to the function type.
6917 if (func_type_add_arg_types(ufunc->uf_func_type,
6918 argcount + varargs,
6919 &ufunc->uf_type_list) == FAIL)
6920 return;
6921 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6922 ufunc->uf_func_type->tt_min_argcount =
6923 argcount - ufunc->uf_def_args.ga_len;
6924 if (ufunc->uf_arg_types == NULL)
6925 {
6926 int i;
6927
6928 // lambda does not have argument types.
6929 for (i = 0; i < argcount; ++i)
6930 ufunc->uf_func_type->tt_args[i] = &t_any;
6931 }
6932 else
6933 mch_memmove(ufunc->uf_func_type->tt_args,
6934 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6935 if (varargs)
6936 {
6937 ufunc->uf_func_type->tt_args[argcount] =
6938 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6939 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6940 }
6941 }
6942 else
6943 // No arguments, can use a predefined type.
6944 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6945 argcount, &ufunc->uf_type_list);
6946}
6947
6948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949/*
6950 * Delete an instruction, free what it contains.
6951 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006952 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953delete_instr(isn_T *isn)
6954{
6955 switch (isn->isn_type)
6956 {
6957 case ISN_EXEC:
6958 case ISN_LOADENV:
6959 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006960 case ISN_LOADB:
6961 case ISN_LOADW:
6962 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006964 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 case ISN_PUSHEXC:
6966 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006967 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006969 case ISN_STOREB:
6970 case ISN_STOREW:
6971 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006972 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 vim_free(isn->isn_arg.string);
6974 break;
6975
6976 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006977 case ISN_STORES:
6978 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 break;
6980
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006981 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006982 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006983 vim_free(isn->isn_arg.unlet.ul_name);
6984 break;
6985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 case ISN_STOREOPT:
6987 vim_free(isn->isn_arg.storeopt.so_name);
6988 break;
6989
6990 case ISN_PUSHBLOB: // push blob isn_arg.blob
6991 blob_unref(isn->isn_arg.blob);
6992 break;
6993
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006994 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006996 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006997#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006998 break;
6999
7000 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007001#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007002 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007003#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007004 break;
7005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 case ISN_UCALL:
7007 vim_free(isn->isn_arg.ufunc.cuf_name);
7008 break;
7009
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007010 case ISN_FUNCREF:
7011 {
7012 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7013 + isn->isn_arg.funcref.fr_func;
7014 func_ptr_unref(dfunc->df_ufunc);
7015 }
7016 break;
7017
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007018 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007019 {
7020 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7021 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7022
7023 if (ufunc != NULL)
7024 {
7025 // Clear uf_dfunc_idx so that the function is deleted.
7026 clear_def_function(ufunc);
7027 ufunc->uf_dfunc_idx = 0;
7028 func_ptr_unref(ufunc);
7029 }
7030
7031 vim_free(lambda);
7032 vim_free(isn->isn_arg.newfunc.nf_global);
7033 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007034 break;
7035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 case ISN_2BOOL:
7037 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007038 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 case ISN_ADDBLOB:
7040 case ISN_ADDLIST:
7041 case ISN_BCALL:
7042 case ISN_CATCH:
7043 case ISN_CHECKNR:
7044 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007045 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 case ISN_COMPAREANY:
7047 case ISN_COMPAREBLOB:
7048 case ISN_COMPAREBOOL:
7049 case ISN_COMPAREDICT:
7050 case ISN_COMPAREFLOAT:
7051 case ISN_COMPAREFUNC:
7052 case ISN_COMPARELIST:
7053 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 case ISN_COMPARESPECIAL:
7055 case ISN_COMPARESTRING:
7056 case ISN_CONCAT:
7057 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007058 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 case ISN_DROP:
7060 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007061 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007062 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007064 case ISN_EXECCONCAT:
7065 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007067 case ISN_LISTINDEX:
7068 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007069 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007070 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007071 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 case ISN_JUMP:
7073 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007074 case ISN_LOADBDICT:
7075 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007076 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007078 case ISN_LOADSCRIPT:
7079 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007081 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 case ISN_NEGATENR:
7083 case ISN_NEWDICT:
7084 case ISN_NEWLIST:
7085 case ISN_OPNR:
7086 case ISN_OPFLOAT:
7087 case ISN_OPANY:
7088 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007089 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 case ISN_PUSHF:
7091 case ISN_PUSHNR:
7092 case ISN_PUSHBOOL:
7093 case ISN_PUSHSPEC:
7094 case ISN_RETURN:
7095 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007096 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007097 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007099 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007101 case ISN_STOREDICT:
7102 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 case ISN_THROW:
7104 case ISN_TRY:
7105 // nothing allocated
7106 break;
7107 }
7108}
7109
7110/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007111 * Free all instructions for "dfunc".
7112 */
7113 static void
7114delete_def_function_contents(dfunc_T *dfunc)
7115{
7116 int idx;
7117
7118 ga_clear(&dfunc->df_def_args_isn);
7119
7120 if (dfunc->df_instr != NULL)
7121 {
7122 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7123 delete_instr(dfunc->df_instr + idx);
7124 VIM_CLEAR(dfunc->df_instr);
7125 }
7126
7127 dfunc->df_deleted = TRUE;
7128}
7129
7130/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007131 * When a user function is deleted, clear the contents of any associated def
7132 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 */
7134 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007135clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007137 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 {
7139 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7140 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
Bram Moolenaar20431c92020-03-20 18:39:46 +01007142 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007143 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 }
7145}
7146
7147#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007148/*
7149 * Free all functions defined with ":def".
7150 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 void
7152free_def_functions(void)
7153{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007154 int idx;
7155
7156 for (idx = 0; idx < def_functions.ga_len; ++idx)
7157 {
7158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7159
7160 delete_def_function_contents(dfunc);
7161 }
7162
7163 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164}
7165#endif
7166
7167
7168#endif // FEAT_EVAL