blob: a41972520bcbc8c9cb470fb55b41b0592cfa77e3 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200119 int ctx_closure_count; // number of closures created in the
120 // function
121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 garray_T ctx_imports; // imported items
123
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100124 int ctx_skip; // when TRUE skip commands, when FALSE skip
125 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 scope_T *ctx_scope; // current scope, NULL at toplevel
127
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128 cctx_T *ctx_outer; // outer scope for lambda or nested
129 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200130 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200133 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134};
135
136static char e_var_notfound[] = N_("E1001: variable not found: %s");
137static char e_syntax_at[] = N_("E1002: Syntax error at %s");
138
Bram Moolenaara5565e42020-05-09 15:44:01 +0200139static int compile_expr0(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100140static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200141static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
142static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143
144/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200145 * Lookup variable "name" in the local scope and return it.
146 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200148 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149lookup_local(char_u *name, size_t len, cctx_T *cctx)
150{
151 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200152 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100154 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156
157 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
159 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 if (STRNCMP(name, lvar->lv_name, len) == 0
162 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 {
164 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200168
169 // Find local in outer function scope.
170 if (cctx->ctx_outer != NULL)
171 {
172 lvar = lookup_local(name, len, cctx->ctx_outer);
173 if (lvar != NULL)
174 {
175 // TODO: are there situations we should not mark the outer scope as
176 // used?
177 cctx->ctx_outer_used = TRUE;
178 lvar->lv_from_outer = TRUE;
179 return lvar;
180 }
181 }
182
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200183 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184}
185
186/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200187 * Lookup an argument in the current function and an enclosing function.
188 * Returns the argument index in "idxp"
189 * Returns the argument type in "type"
190 * Sets "gen_load_outer" to TRUE if found in outer scope.
191 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192 */
193 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200194lookup_arg(
195 char_u *name,
196 size_t len,
197 int *idxp,
198 type_T **type,
199 int *gen_load_outer,
200 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201{
202 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200203 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100205 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200206 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
208 {
209 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
210
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200211 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
212 {
213 if (idxp != NULL)
214 {
215 // Arguments are located above the frame pointer. One further
216 // if there is a vararg argument
217 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
218 + STACK_FRAME_SIZE)
219 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
220
221 if (cctx->ctx_ufunc->uf_arg_types != NULL)
222 *type = cctx->ctx_ufunc->uf_arg_types[idx];
223 else
224 *type = &t_any;
225 }
226 return OK;
227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200230 va_name = cctx->ctx_ufunc->uf_va_name;
231 if (va_name != NULL
232 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
233 {
234 if (idxp != NULL)
235 {
236 // varargs is always the last argument
237 *idxp = -STACK_FRAME_SIZE - 1;
238 *type = cctx->ctx_ufunc->uf_va_type;
239 }
240 return OK;
241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200243 if (cctx->ctx_outer != NULL)
244 {
245 // Lookup the name for an argument of the outer function.
246 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
247 == OK)
248 {
249 *gen_load_outer = TRUE;
250 return OK;
251 }
252 }
253
254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255}
256
257/*
258 * Lookup a variable in the current script.
259 * Returns OK or FAIL.
260 */
261 static int
262lookup_script(char_u *name, size_t len)
263{
264 int cc;
265 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
266 dictitem_T *di;
267
268 cc = name[len];
269 name[len] = NUL;
270 di = find_var_in_ht(ht, 0, name, TRUE);
271 name[len] = cc;
272 return di == NULL ? FAIL: OK;
273}
274
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100275/*
276 * Check if "p[len]" is already defined, either in script "import_sid" or in
277 * compilation context "cctx".
278 * Return FAIL and give an error if it defined.
279 */
280 int
281check_defined(char_u *p, int len, cctx_T *cctx)
282{
283 if (lookup_script(p, len) == OK
284 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200285 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286 || find_imported(p, len, cctx) != NULL)))
287 {
288 semsg("E1073: imported name already defined: %s", p);
289 return FAIL;
290 }
291 return OK;
292}
293
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200294/*
295 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
296 * be freed later.
297 */
298 static type_T *
299alloc_type(garray_T *type_gap)
300{
301 type_T *type;
302
303 if (ga_grow(type_gap, 1) == FAIL)
304 return NULL;
305 type = ALLOC_CLEAR_ONE(type_T);
306 if (type != NULL)
307 {
308 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
309 ++type_gap->ga_len;
310 }
311 return type;
312}
313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200315get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316{
317 type_T *type;
318
319 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200320 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200322 if (member_type->tt_type == VAR_VOID
323 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100324 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100325 if (member_type->tt_type == VAR_BOOL)
326 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327 if (member_type->tt_type == VAR_NUMBER)
328 return &t_list_number;
329 if (member_type->tt_type == VAR_STRING)
330 return &t_list_string;
331
332 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200333 type = alloc_type(type_gap);
334 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100335 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 type->tt_type = VAR_LIST;
337 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200338 type->tt_argcount = 0;
339 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return type;
341}
342
343 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200344get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345{
346 type_T *type;
347
348 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200349 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200351 if (member_type->tt_type == VAR_VOID
352 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100353 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100354 if (member_type->tt_type == VAR_BOOL)
355 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 if (member_type->tt_type == VAR_NUMBER)
357 return &t_dict_number;
358 if (member_type->tt_type == VAR_STRING)
359 return &t_dict_string;
360
361 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200362 type = alloc_type(type_gap);
363 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100364 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 type->tt_type = VAR_DICT;
366 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200367 type->tt_argcount = 0;
368 type->tt_args = NULL;
369 return type;
370}
371
372/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200373 * Allocate a new type for a function.
374 */
375 static type_T *
376alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
377{
378 type_T *type = alloc_type(type_gap);
379
380 if (type == NULL)
381 return &t_any;
382 type->tt_type = VAR_FUNC;
383 type->tt_member = ret_type;
384 type->tt_argcount = argcount;
385 type->tt_args = NULL;
386 return type;
387}
388
389/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200390 * Get a function type, based on the return type "ret_type".
391 * If "argcount" is -1 or 0 a predefined type can be used.
392 * If "argcount" > 0 always create a new type, so that arguments can be added.
393 */
394 static type_T *
395get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200397 // recognize commonly used types
398 if (argcount <= 0)
399 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200400 if (ret_type == &t_unknown)
401 {
402 // (argcount == 0) is not possible
403 return &t_func_unknown;
404 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200405 if (ret_type == &t_void)
406 {
407 if (argcount == 0)
408 return &t_func_0_void;
409 else
410 return &t_func_void;
411 }
412 if (ret_type == &t_any)
413 {
414 if (argcount == 0)
415 return &t_func_0_any;
416 else
417 return &t_func_any;
418 }
419 if (ret_type == &t_number)
420 {
421 if (argcount == 0)
422 return &t_func_0_number;
423 else
424 return &t_func_number;
425 }
426 if (ret_type == &t_string)
427 {
428 if (argcount == 0)
429 return &t_func_0_string;
430 else
431 return &t_func_string;
432 }
433 }
434
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200435 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436}
437
Bram Moolenaara8c17702020-04-01 21:17:24 +0200438/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200439 * For a function type, reserve space for "argcount" argument types (including
440 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200441 */
442 static int
443func_type_add_arg_types(
444 type_T *functype,
445 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200446 garray_T *type_gap)
447{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200448 // To make it easy to free the space needed for the argument types, add the
449 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200450 if (ga_grow(type_gap, 1) == FAIL)
451 return FAIL;
452 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
453 if (functype->tt_args == NULL)
454 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200455 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
456 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200457 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200458 return OK;
459}
460
461/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200462 * Return the type_T for a typval. Only for primitive types.
463 */
464 static type_T *
465typval2type(typval_T *tv)
466{
467 if (tv->v_type == VAR_NUMBER)
468 return &t_number;
469 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200470 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200471 if (tv->v_type == VAR_STRING)
472 return &t_string;
473 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
474 return &t_list_string;
475 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
476 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200477 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200478}
479
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200480 static void
481type_mismatch(type_T *expected, type_T *actual)
482{
483 char *tofree1, *tofree2;
484
485 semsg(_("E1013: type mismatch, expected %s but got %s"),
486 type_name(expected, &tofree1), type_name(actual, &tofree2));
487 vim_free(tofree1);
488 vim_free(tofree2);
489}
490
491 static void
492arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
493{
494 char *tofree1, *tofree2;
495
496 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
497 argidx,
498 type_name(expected, &tofree1), type_name(actual, &tofree2));
499 vim_free(tofree1);
500 vim_free(tofree2);
501}
502
503/*
504 * Check if the expected and actual types match.
505 * Does not allow for assigning "any" to a specific type.
506 */
507 static int
508check_type(type_T *expected, type_T *actual, int give_msg)
509{
510 int ret = OK;
511
512 // When expected is "unknown" we accept any actual type.
513 // When expected is "any" we accept any actual type except "void".
514 if (expected->tt_type != VAR_UNKNOWN
515 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
516
517 {
518 if (expected->tt_type != actual->tt_type)
519 {
520 if (give_msg)
521 type_mismatch(expected, actual);
522 return FAIL;
523 }
524 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
525 {
526 // "unknown" is used for an empty list or dict
527 if (actual->tt_member != &t_unknown)
528 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
529 }
530 else if (expected->tt_type == VAR_FUNC)
531 {
532 if (expected->tt_member != &t_unknown)
533 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
534 if (ret == OK && expected->tt_argcount != -1
535 && (actual->tt_argcount < expected->tt_min_argcount
536 || actual->tt_argcount > expected->tt_argcount))
537 ret = FAIL;
538 }
539 if (ret == FAIL && give_msg)
540 type_mismatch(expected, actual);
541 }
542 return ret;
543}
544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545/////////////////////////////////////////////////////////////////////
546// Following generate_ functions expect the caller to call ga_grow().
547
Bram Moolenaar080457c2020-03-03 21:53:32 +0100548#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
549#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551/*
552 * Generate an instruction without arguments.
553 * Returns a pointer to the new instruction, NULL if failed.
554 */
555 static isn_T *
556generate_instr(cctx_T *cctx, isntype_T isn_type)
557{
558 garray_T *instr = &cctx->ctx_instr;
559 isn_T *isn;
560
Bram Moolenaar080457c2020-03-03 21:53:32 +0100561 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 if (ga_grow(instr, 1) == FAIL)
563 return NULL;
564 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
565 isn->isn_type = isn_type;
566 isn->isn_lnum = cctx->ctx_lnum + 1;
567 ++instr->ga_len;
568
569 return isn;
570}
571
572/*
573 * Generate an instruction without arguments.
574 * "drop" will be removed from the stack.
575 * Returns a pointer to the new instruction, NULL if failed.
576 */
577 static isn_T *
578generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581
Bram Moolenaar080457c2020-03-03 21:53:32 +0100582 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 stack->ga_len -= drop;
584 return generate_instr(cctx, isn_type);
585}
586
587/*
588 * Generate instruction "isn_type" and put "type" on the type stack.
589 */
590 static isn_T *
591generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
592{
593 isn_T *isn;
594 garray_T *stack = &cctx->ctx_type_stack;
595
596 if ((isn = generate_instr(cctx, isn_type)) == NULL)
597 return NULL;
598
599 if (ga_grow(stack, 1) == FAIL)
600 return NULL;
601 ((type_T **)stack->ga_data)[stack->ga_len] = type;
602 ++stack->ga_len;
603
604 return isn;
605}
606
607/*
608 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
609 */
610 static int
611may_generate_2STRING(int offset, cctx_T *cctx)
612{
613 isn_T *isn;
614 garray_T *stack = &cctx->ctx_type_stack;
615 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
616
617 if ((*type)->tt_type == VAR_STRING)
618 return OK;
619 *type = &t_string;
620
621 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
622 return FAIL;
623 isn->isn_arg.number = offset;
624
625 return OK;
626}
627
628 static int
629check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
630{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200631 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 {
635 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100636 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 else
638 semsg(_("E1036: %c requires number or float arguments"), *op);
639 return FAIL;
640 }
641 return OK;
642}
643
644/*
645 * Generate an instruction with two arguments. The instruction depends on the
646 * type of the arguments.
647 */
648 static int
649generate_two_op(cctx_T *cctx, char_u *op)
650{
651 garray_T *stack = &cctx->ctx_type_stack;
652 type_T *type1;
653 type_T *type2;
654 vartype_T vartype;
655 isn_T *isn;
656
Bram Moolenaar080457c2020-03-03 21:53:32 +0100657 RETURN_OK_IF_SKIP(cctx);
658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 // Get the known type of the two items on the stack. If they are matching
660 // use a type-specific instruction. Otherwise fall back to runtime type
661 // checking.
662 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
663 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200664 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 if (type1->tt_type == type2->tt_type
666 && (type1->tt_type == VAR_NUMBER
667 || type1->tt_type == VAR_LIST
668#ifdef FEAT_FLOAT
669 || type1->tt_type == VAR_FLOAT
670#endif
671 || type1->tt_type == VAR_BLOB))
672 vartype = type1->tt_type;
673
674 switch (*op)
675 {
676 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200677 && type1->tt_type != VAR_ANY
678 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 && check_number_or_float(
680 type1->tt_type, type2->tt_type, op) == FAIL)
681 return FAIL;
682 isn = generate_instr_drop(cctx,
683 vartype == VAR_NUMBER ? ISN_OPNR
684 : vartype == VAR_LIST ? ISN_ADDLIST
685 : vartype == VAR_BLOB ? ISN_ADDBLOB
686#ifdef FEAT_FLOAT
687 : vartype == VAR_FLOAT ? ISN_OPFLOAT
688#endif
689 : ISN_OPANY, 1);
690 if (isn != NULL)
691 isn->isn_arg.op.op_type = EXPR_ADD;
692 break;
693
694 case '-':
695 case '*':
696 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
697 op) == FAIL)
698 return FAIL;
699 if (vartype == VAR_NUMBER)
700 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
701#ifdef FEAT_FLOAT
702 else if (vartype == VAR_FLOAT)
703 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
704#endif
705 else
706 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
707 if (isn != NULL)
708 isn->isn_arg.op.op_type = *op == '*'
709 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
710 break;
711
Bram Moolenaar4c683752020-04-05 21:38:23 +0200712 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200714 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 && type2->tt_type != VAR_NUMBER))
716 {
717 emsg(_("E1035: % requires number arguments"));
718 return FAIL;
719 }
720 isn = generate_instr_drop(cctx,
721 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
722 if (isn != NULL)
723 isn->isn_arg.op.op_type = EXPR_REM;
724 break;
725 }
726
727 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200728 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 {
730 type_T *type = &t_any;
731
732#ifdef FEAT_FLOAT
733 // float+number and number+float results in float
734 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
735 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
736 type = &t_float;
737#endif
738 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
739 }
740
741 return OK;
742}
743
744/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200745 * Get the instruction to use for comparing "type1" with "type2"
746 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200748 static isntype_T
749get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750{
751 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
Bram Moolenaar4c683752020-04-05 21:38:23 +0200753 if (type1 == VAR_UNKNOWN)
754 type1 = VAR_ANY;
755 if (type2 == VAR_UNKNOWN)
756 type2 = VAR_ANY;
757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 if (type1 == type2)
759 {
760 switch (type1)
761 {
762 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
763 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
764 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
765 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
766 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
767 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
768 case VAR_LIST: isntype = ISN_COMPARELIST; break;
769 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
770 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 default: isntype = ISN_COMPAREANY; break;
772 }
773 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200774 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
776 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
777 isntype = ISN_COMPAREANY;
778
779 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
780 && (isntype == ISN_COMPAREBOOL
781 || isntype == ISN_COMPARESPECIAL
782 || isntype == ISN_COMPARENR
783 || isntype == ISN_COMPAREFLOAT))
784 {
785 semsg(_("E1037: Cannot use \"%s\" with %s"),
786 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200787 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 }
789 if (isntype == ISN_DROP
790 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
791 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
792 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
793 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
794 && exptype != EXPR_IS && exptype != EXPR_ISNOT
795 && (type1 == VAR_BLOB || type2 == VAR_BLOB
796 || type1 == VAR_LIST || type2 == VAR_LIST))))
797 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100798 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200800 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200802 return isntype;
803}
804
805/*
806 * Generate an ISN_COMPARE* instruction with a boolean result.
807 */
808 static int
809generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
810{
811 isntype_T isntype;
812 isn_T *isn;
813 garray_T *stack = &cctx->ctx_type_stack;
814 vartype_T type1;
815 vartype_T type2;
816
817 RETURN_OK_IF_SKIP(cctx);
818
819 // Get the known type of the two items on the stack. If they are matching
820 // use a type-specific instruction. Otherwise fall back to runtime type
821 // checking.
822 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
823 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
824 isntype = get_compare_isn(exptype, type1, type2);
825 if (isntype == ISN_DROP)
826 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 if ((isn = generate_instr(cctx, isntype)) == NULL)
829 return FAIL;
830 isn->isn_arg.op.op_type = exptype;
831 isn->isn_arg.op.op_ic = ic;
832
833 // takes two arguments, puts one bool back
834 if (stack->ga_len >= 2)
835 {
836 --stack->ga_len;
837 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
838 }
839
840 return OK;
841}
842
843/*
844 * Generate an ISN_2BOOL instruction.
845 */
846 static int
847generate_2BOOL(cctx_T *cctx, int invert)
848{
849 isn_T *isn;
850 garray_T *stack = &cctx->ctx_type_stack;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = invert;
856
857 // type becomes bool
858 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
859
860 return OK;
861}
862
863 static int
864generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
865{
866 isn_T *isn;
867 garray_T *stack = &cctx->ctx_type_stack;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
871 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200872 // TODO: whole type, e.g. for a function also arg and return types
873 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 isn->isn_arg.type.ct_off = offset;
875
876 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200877 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878
879 return OK;
880}
881
882/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200883 * Check that
884 * - "actual" is "expected" type or
885 * - "actual" is a type that can be "expected" type: add a runtime check; or
886 * - return FAIL.
887 */
888 static int
889need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
890{
891 if (check_type(expected, actual, FALSE) == OK)
892 return OK;
893 if (actual->tt_type != VAR_ANY
894 && actual->tt_type != VAR_UNKNOWN
895 && !(actual->tt_type == VAR_FUNC
896 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
897 {
898 type_mismatch(expected, actual);
899 return FAIL;
900 }
901 generate_TYPECHECK(cctx, expected, offset);
902 return OK;
903}
904
905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 * Generate an ISN_PUSHNR instruction.
907 */
908 static int
909generate_PUSHNR(cctx_T *cctx, varnumber_T number)
910{
911 isn_T *isn;
912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
918 return OK;
919}
920
921/*
922 * Generate an ISN_PUSHBOOL instruction.
923 */
924 static int
925generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
926{
927 isn_T *isn;
928
Bram Moolenaar080457c2020-03-03 21:53:32 +0100929 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
931 return FAIL;
932 isn->isn_arg.number = number;
933
934 return OK;
935}
936
937/*
938 * Generate an ISN_PUSHSPEC instruction.
939 */
940 static int
941generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
942{
943 isn_T *isn;
944
Bram Moolenaar080457c2020-03-03 21:53:32 +0100945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
947 return FAIL;
948 isn->isn_arg.number = number;
949
950 return OK;
951}
952
953#ifdef FEAT_FLOAT
954/*
955 * Generate an ISN_PUSHF instruction.
956 */
957 static int
958generate_PUSHF(cctx_T *cctx, float_T fnumber)
959{
960 isn_T *isn;
961
Bram Moolenaar080457c2020-03-03 21:53:32 +0100962 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
964 return FAIL;
965 isn->isn_arg.fnumber = fnumber;
966
967 return OK;
968}
969#endif
970
971/*
972 * Generate an ISN_PUSHS instruction.
973 * Consumes "str".
974 */
975 static int
976generate_PUSHS(cctx_T *cctx, char_u *str)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
982 return FAIL;
983 isn->isn_arg.string = str;
984
985 return OK;
986}
987
988/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100989 * Generate an ISN_PUSHCHANNEL instruction.
990 * Consumes "channel".
991 */
992 static int
993generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
994{
995 isn_T *isn;
996
Bram Moolenaar080457c2020-03-03 21:53:32 +0100997 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100998 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
999 return FAIL;
1000 isn->isn_arg.channel = channel;
1001
1002 return OK;
1003}
1004
1005/*
1006 * Generate an ISN_PUSHJOB instruction.
1007 * Consumes "job".
1008 */
1009 static int
1010generate_PUSHJOB(cctx_T *cctx, job_T *job)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar080457c2020-03-03 21:53:32 +01001014 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001015 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 return FAIL;
1017 isn->isn_arg.job = job;
1018
1019 return OK;
1020}
1021
1022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 * Generate an ISN_PUSHBLOB instruction.
1024 * Consumes "blob".
1025 */
1026 static int
1027generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1028{
1029 isn_T *isn;
1030
Bram Moolenaar080457c2020-03-03 21:53:32 +01001031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.blob = blob;
1035
1036 return OK;
1037}
1038
1039/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 * Generate an ISN_PUSHFUNC instruction with name "name".
1041 * Consumes "name".
1042 */
1043 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001044generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001049 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001050 return FAIL;
1051 isn->isn_arg.string = name;
1052
1053 return OK;
1054}
1055
1056/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 * Generate an ISN_STORE instruction.
1058 */
1059 static int
1060generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1061{
1062 isn_T *isn;
1063
Bram Moolenaar080457c2020-03-03 21:53:32 +01001064 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1066 return FAIL;
1067 if (name != NULL)
1068 isn->isn_arg.string = vim_strsave(name);
1069 else
1070 isn->isn_arg.number = idx;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1077 */
1078 static int
1079generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1085 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001086 isn->isn_arg.storenr.stnr_idx = idx;
1087 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
1089 return OK;
1090}
1091
1092/*
1093 * Generate an ISN_STOREOPT instruction
1094 */
1095 static int
1096generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1097{
1098 isn_T *isn;
1099
Bram Moolenaar080457c2020-03-03 21:53:32 +01001100 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1104 isn->isn_arg.storeopt.so_flags = opt_flags;
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_LOAD or similar instruction.
1111 */
1112 static int
1113generate_LOAD(
1114 cctx_T *cctx,
1115 isntype_T isn_type,
1116 int idx,
1117 char_u *name,
1118 type_T *type)
1119{
1120 isn_T *isn;
1121
Bram Moolenaar080457c2020-03-03 21:53:32 +01001122 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1124 return FAIL;
1125 if (name != NULL)
1126 isn->isn_arg.string = vim_strsave(name);
1127 else
1128 isn->isn_arg.number = idx;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001134 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001135 */
1136 static int
1137generate_LOADV(
1138 cctx_T *cctx,
1139 char_u *name,
1140 int error)
1141{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001142 int di_flags;
1143 int vidx = find_vim_var(name, &di_flags);
1144 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145
Bram Moolenaar080457c2020-03-03 21:53:32 +01001146 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001147 if (vidx < 0)
1148 {
1149 if (error)
1150 semsg(_(e_var_notfound), name);
1151 return FAIL;
1152 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001153 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001154
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001155 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001156}
1157
1158/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001159 * Generate an ISN_UNLET instruction.
1160 */
1161 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001162generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001163{
1164 isn_T *isn;
1165
1166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001167 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001168 return FAIL;
1169 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1170 isn->isn_arg.unlet.ul_forceit = forceit;
1171
1172 return OK;
1173}
1174
1175/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 * Generate an ISN_LOADS instruction.
1177 */
1178 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001179generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001181 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001183 int sid,
1184 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001189 if (isn_type == ISN_LOADS)
1190 isn = generate_instr_type(cctx, isn_type, type);
1191 else
1192 isn = generate_instr_drop(cctx, isn_type, 1);
1193 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001195 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1196 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197
1198 return OK;
1199}
1200
1201/*
1202 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1203 */
1204 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001205generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 cctx_T *cctx,
1207 isntype_T isn_type,
1208 int sid,
1209 int idx,
1210 type_T *type)
1211{
1212 isn_T *isn;
1213
Bram Moolenaar080457c2020-03-03 21:53:32 +01001214 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if (isn_type == ISN_LOADSCRIPT)
1216 isn = generate_instr_type(cctx, isn_type, type);
1217 else
1218 isn = generate_instr_drop(cctx, isn_type, 1);
1219 if (isn == NULL)
1220 return FAIL;
1221 isn->isn_arg.script.script_sid = sid;
1222 isn->isn_arg.script.script_idx = idx;
1223 return OK;
1224}
1225
1226/*
1227 * Generate an ISN_NEWLIST instruction.
1228 */
1229 static int
1230generate_NEWLIST(cctx_T *cctx, int count)
1231{
1232 isn_T *isn;
1233 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 type_T *type;
1235 type_T *member;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.number = count;
1241
1242 // drop the value types
1243 stack->ga_len -= count;
1244
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001245 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001246 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if (count > 0)
1248 member = ((type_T **)stack->ga_data)[stack->ga_len];
1249 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001250 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001251 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252
1253 // add the list type to the type stack
1254 if (ga_grow(stack, 1) == FAIL)
1255 return FAIL;
1256 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1257 ++stack->ga_len;
1258
1259 return OK;
1260}
1261
1262/*
1263 * Generate an ISN_NEWDICT instruction.
1264 */
1265 static int
1266generate_NEWDICT(cctx_T *cctx, int count)
1267{
1268 isn_T *isn;
1269 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 type_T *type;
1271 type_T *member;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1275 return FAIL;
1276 isn->isn_arg.number = count;
1277
1278 // drop the key and value types
1279 stack->ga_len -= 2 * count;
1280
Bram Moolenaar436472f2020-02-20 22:54:43 +01001281 // Use the first value type for the list member type. Use "void" for an
1282 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 if (count > 0)
1284 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1285 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001286 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001287 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 // add the dict type to the type stack
1290 if (ga_grow(stack, 1) == FAIL)
1291 return FAIL;
1292 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1293 ++stack->ga_len;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_FUNCREF instruction.
1300 */
1301 static int
1302generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1303{
1304 isn_T *isn;
1305 garray_T *stack = &cctx->ctx_type_stack;
1306
Bram Moolenaar080457c2020-03-03 21:53:32 +01001307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1309 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001310 isn->isn_arg.funcref.fr_func = dfunc_idx;
1311 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 if (ga_grow(stack, 1) == FAIL)
1314 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001315 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 // TODO: argument and return types
1317 ++stack->ga_len;
1318
1319 return OK;
1320}
1321
1322/*
1323 * Generate an ISN_JUMP instruction.
1324 */
1325 static int
1326generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1327{
1328 isn_T *isn;
1329 garray_T *stack = &cctx->ctx_type_stack;
1330
Bram Moolenaar080457c2020-03-03 21:53:32 +01001331 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.jump.jump_when = when;
1335 isn->isn_arg.jump.jump_where = where;
1336
1337 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1338 --stack->ga_len;
1339
1340 return OK;
1341}
1342
1343 static int
1344generate_FOR(cctx_T *cctx, int loop_idx)
1345{
1346 isn_T *isn;
1347 garray_T *stack = &cctx->ctx_type_stack;
1348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1351 return FAIL;
1352 isn->isn_arg.forloop.for_idx = loop_idx;
1353
1354 if (ga_grow(stack, 1) == FAIL)
1355 return FAIL;
1356 // type doesn't matter, will be stored next
1357 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1358 ++stack->ga_len;
1359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_BCALL instruction.
1365 * Return FAIL if the number of arguments is wrong.
1366 */
1367 static int
1368generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1369{
1370 isn_T *isn;
1371 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001372 type_T *argtypes[MAX_FUNC_ARGS];
1373 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if (check_internal_func(func_idx, argcount) == FAIL)
1377 return FAIL;
1378
1379 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1380 return FAIL;
1381 isn->isn_arg.bfunc.cbf_idx = func_idx;
1382 isn->isn_arg.bfunc.cbf_argcount = argcount;
1383
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001384 for (i = 0; i < argcount; ++i)
1385 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 stack->ga_len -= argcount; // drop the arguments
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
1390 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001391 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 ++stack->ga_len; // add return value
1393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_DCALL or ISN_UCALL instruction.
1399 * Return FAIL if the number of arguments is wrong.
1400 */
1401 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001402generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403{
1404 isn_T *isn;
1405 garray_T *stack = &cctx->ctx_type_stack;
1406 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001407 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 if (argcount > regular_args && !has_varargs(ufunc))
1411 {
1412 semsg(_(e_toomanyarg), ufunc->uf_name);
1413 return FAIL;
1414 }
1415 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1416 {
1417 semsg(_(e_toofewarg), ufunc->uf_name);
1418 return FAIL;
1419 }
1420
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001421 if (ufunc->uf_dfunc_idx >= 0)
1422 {
1423 int i;
1424
1425 for (i = 0; i < argcount; ++i)
1426 {
1427 type_T *expected;
1428 type_T *actual;
1429
1430 if (i < regular_args)
1431 {
1432 if (ufunc->uf_arg_types == NULL)
1433 continue;
1434 expected = ufunc->uf_arg_types[i];
1435 }
1436 else
1437 expected = ufunc->uf_va_type->tt_member;
1438 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001439 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001440 {
1441 arg_type_mismatch(expected, actual, i + 1);
1442 return FAIL;
1443 }
1444 }
1445 }
1446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if ((isn = generate_instr(cctx,
1448 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1449 return FAIL;
1450 if (ufunc->uf_dfunc_idx >= 0)
1451 {
1452 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1453 isn->isn_arg.dfunc.cdf_argcount = argcount;
1454 }
1455 else
1456 {
1457 // A user function may be deleted and redefined later, can't use the
1458 // ufunc pointer, need to look it up again at runtime.
1459 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1460 isn->isn_arg.ufunc.cuf_argcount = argcount;
1461 }
1462
1463 stack->ga_len -= argcount; // drop the arguments
1464 if (ga_grow(stack, 1) == FAIL)
1465 return FAIL;
1466 // add return value
1467 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1468 ++stack->ga_len;
1469
1470 return OK;
1471}
1472
1473/*
1474 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1475 */
1476 static int
1477generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1484 return FAIL;
1485 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1486 isn->isn_arg.ufunc.cuf_argcount = argcount;
1487
1488 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001489 if (ga_grow(stack, 1) == FAIL)
1490 return FAIL;
1491 // add return value
1492 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1493 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001500 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 */
1502 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001503generate_PCALL(
1504 cctx_T *cctx,
1505 int argcount,
1506 char_u *name,
1507 type_T *type,
1508 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509{
1510 isn_T *isn;
1511 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001512 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001515
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001516 if (type->tt_type == VAR_ANY)
1517 ret_type = &t_any;
1518 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001519 {
1520 if (type->tt_argcount != -1)
1521 {
1522 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1523
1524 if (argcount < type->tt_min_argcount - varargs)
1525 {
1526 semsg(_(e_toofewarg), "[reference]");
1527 return FAIL;
1528 }
1529 if (!varargs && argcount > type->tt_argcount)
1530 {
1531 semsg(_(e_toomanyarg), "[reference]");
1532 return FAIL;
1533 }
1534 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001535 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001536 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001537 else
1538 {
1539 semsg(_("E1085: Not a callable type: %s"), name);
1540 return FAIL;
1541 }
1542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1544 return FAIL;
1545 isn->isn_arg.pfunc.cpf_top = at_top;
1546 isn->isn_arg.pfunc.cpf_argcount = argcount;
1547
1548 stack->ga_len -= argcount; // drop the arguments
1549
1550 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001551 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001553 // If partial is above the arguments it must be cleared and replaced with
1554 // the return value.
1555 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1556 return FAIL;
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 return OK;
1559}
1560
1561/*
1562 * Generate an ISN_MEMBER instruction.
1563 */
1564 static int
1565generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1566{
1567 isn_T *isn;
1568 garray_T *stack = &cctx->ctx_type_stack;
1569 type_T *type;
1570
Bram Moolenaar080457c2020-03-03 21:53:32 +01001571 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.string = vim_strnsave(name, (int)len);
1575
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001576 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001578 if (type->tt_type != VAR_DICT && type != &t_any)
1579 {
1580 emsg(_(e_dictreq));
1581 return FAIL;
1582 }
1583 // change dict type to dict member type
1584 if (type->tt_type == VAR_DICT)
1585 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586
1587 return OK;
1588}
1589
1590/*
1591 * Generate an ISN_ECHO instruction.
1592 */
1593 static int
1594generate_ECHO(cctx_T *cctx, int with_white, int count)
1595{
1596 isn_T *isn;
1597
Bram Moolenaar080457c2020-03-03 21:53:32 +01001598 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1600 return FAIL;
1601 isn->isn_arg.echo.echo_with_white = with_white;
1602 isn->isn_arg.echo.echo_count = count;
1603
1604 return OK;
1605}
1606
Bram Moolenaarad39c092020-02-26 18:23:43 +01001607/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001608 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001609 */
1610 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001611generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612{
1613 isn_T *isn;
1614
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001615 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001616 return FAIL;
1617 isn->isn_arg.number = count;
1618
1619 return OK;
1620}
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 static int
1623generate_EXEC(cctx_T *cctx, char_u *line)
1624{
1625 isn_T *isn;
1626
Bram Moolenaar080457c2020-03-03 21:53:32 +01001627 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.string = vim_strsave(line);
1631 return OK;
1632}
1633
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001634 static int
1635generate_EXECCONCAT(cctx_T *cctx, int count)
1636{
1637 isn_T *isn;
1638
1639 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.number = count;
1642 return OK;
1643}
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645/*
1646 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001647 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001649 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1651{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 lvar_T *lvar;
1653
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001654 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 {
1656 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 }
1659
1660 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001661 return NULL;
1662 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001664 // Every local variable uses the next entry on the stack. We could re-use
1665 // the last ones when leaving a scope, but then variables used in a closure
1666 // might get overwritten. To keep things simple do not re-use stack
1667 // entries. This is less efficient, but memory is cheap these days.
1668 lvar->lv_idx = cctx->ctx_locals_count++;
1669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1671 lvar->lv_const = isConst;
1672 lvar->lv_type = type;
1673
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001674 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675}
1676
1677/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001678 * Remove local variables above "new_top".
1679 */
1680 static void
1681unwind_locals(cctx_T *cctx, int new_top)
1682{
1683 if (cctx->ctx_locals.ga_len > new_top)
1684 {
1685 int idx;
1686 lvar_T *lvar;
1687
1688 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1689 {
1690 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1691 vim_free(lvar->lv_name);
1692 }
1693 }
1694 cctx->ctx_locals.ga_len = new_top;
1695}
1696
1697/*
1698 * Free all local variables.
1699 */
1700 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001701free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001702{
1703 unwind_locals(cctx, 0);
1704 ga_clear(&cctx->ctx_locals);
1705}
1706
1707/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 * Skip over a type definition and return a pointer to just after it.
1709 */
1710 char_u *
1711skip_type(char_u *start)
1712{
1713 char_u *p = start;
1714
1715 while (ASCII_ISALNUM(*p) || *p == '_')
1716 ++p;
1717
1718 // Skip over "<type>"; this is permissive about white space.
1719 if (*skipwhite(p) == '<')
1720 {
1721 p = skipwhite(p);
1722 p = skip_type(skipwhite(p + 1));
1723 p = skipwhite(p);
1724 if (*p == '>')
1725 ++p;
1726 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001727 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1728 {
1729 // handle func(args): type
1730 ++p;
1731 while (*p != ')' && *p != NUL)
1732 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001733 char_u *sp = p;
1734
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001735 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001736 if (p == sp)
1737 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001738 if (*p == ',')
1739 p = skipwhite(p + 1);
1740 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001741 if (*p == ')')
1742 {
1743 if (p[1] == ':')
1744 p = skip_type(skipwhite(p + 2));
1745 else
1746 p = skipwhite(p + 1);
1747 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001748 }
1749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 return p;
1751}
1752
1753/*
1754 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001755 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 * Returns NULL in case of failure.
1757 */
1758 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001759parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760{
1761 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001762 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763
1764 if (**arg != '<')
1765 {
1766 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001767 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 else
1769 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001770 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 }
1772 *arg = skipwhite(*arg + 1);
1773
Bram Moolenaard77a8522020-04-03 21:59:57 +02001774 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775
1776 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001777 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 {
1779 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001780 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 ++*arg;
1783
1784 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001785 return get_list_type(member_type, type_gap);
1786 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787}
1788
1789/*
1790 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001791 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 */
1793 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001794parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795{
1796 char_u *p = *arg;
1797 size_t len;
1798
1799 // skip over the first word
1800 while (ASCII_ISALNUM(*p) || *p == '_')
1801 ++p;
1802 len = p - *arg;
1803
1804 switch (**arg)
1805 {
1806 case 'a':
1807 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1808 {
1809 *arg += len;
1810 return &t_any;
1811 }
1812 break;
1813 case 'b':
1814 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1815 {
1816 *arg += len;
1817 return &t_bool;
1818 }
1819 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1820 {
1821 *arg += len;
1822 return &t_blob;
1823 }
1824 break;
1825 case 'c':
1826 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1827 {
1828 *arg += len;
1829 return &t_channel;
1830 }
1831 break;
1832 case 'd':
1833 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1834 {
1835 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001836 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838 break;
1839 case 'f':
1840 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1841 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001842#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 *arg += len;
1844 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001845#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001847 return &t_any;
1848#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 }
1850 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1851 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001852 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001853 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 int argcount = -1;
1855 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001857 type_T *arg_type[MAX_FUNC_ARGS + 1];
1858
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001859 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001861 if (**arg == '(')
1862 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001863 // "func" may or may not return a value, "func()" does
1864 // not return a value.
1865 ret_type = &t_void;
1866
Bram Moolenaard77a8522020-04-03 21:59:57 +02001867 p = ++*arg;
1868 argcount = 0;
1869 while (*p != NUL && *p != ')')
1870 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871 if (*p == '?')
1872 {
1873 if (first_optional == -1)
1874 first_optional = argcount;
1875 ++p;
1876 }
1877 else if (first_optional != -1)
1878 {
1879 emsg(_("E1007: mandatory argument after optional argument"));
1880 return &t_any;
1881 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001882 else if (STRNCMP(p, "...", 3) == 0)
1883 {
1884 flags |= TTFLAG_VARARGS;
1885 p += 3;
1886 }
1887
1888 arg_type[argcount++] = parse_type(&p, type_gap);
1889
1890 // Nothing comes after "...{type}".
1891 if (flags & TTFLAG_VARARGS)
1892 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001893
Bram Moolenaard77a8522020-04-03 21:59:57 +02001894 if (*p != ',' && *skipwhite(p) == ',')
1895 {
1896 semsg(_(e_no_white_before), ",");
1897 return &t_any;
1898 }
1899 if (*p == ',')
1900 {
1901 ++p;
1902 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001903 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001904 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001905 return &t_any;
1906 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001907 }
1908 p = skipwhite(p);
1909 if (argcount == MAX_FUNC_ARGS)
1910 {
1911 emsg(_("E740: Too many argument types"));
1912 return &t_any;
1913 }
1914 }
1915
1916 p = skipwhite(p);
1917 if (*p != ')')
1918 {
1919 emsg(_(e_missing_close));
1920 return &t_any;
1921 }
1922 *arg = p + 1;
1923 }
1924 if (**arg == ':')
1925 {
1926 // parse return type
1927 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001928 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001929 semsg(_(e_white_after), ":");
1930 *arg = skipwhite(*arg);
1931 ret_type = parse_type(arg, type_gap);
1932 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001933 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001934 type = get_func_type(ret_type, argcount, type_gap);
1935 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001937 type = alloc_func_type(ret_type, argcount, type_gap);
1938 type->tt_flags = flags;
1939 if (argcount > 0)
1940 {
1941 type->tt_argcount = argcount;
1942 type->tt_min_argcount = first_optional == -1
1943 ? argcount : first_optional;
1944 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001945 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001946 return &t_any;
1947 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001948 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001949 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001950 }
1951 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
1953 break;
1954 case 'j':
1955 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1956 {
1957 *arg += len;
1958 return &t_job;
1959 }
1960 break;
1961 case 'l':
1962 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1963 {
1964 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001965 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 }
1967 break;
1968 case 'n':
1969 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1970 {
1971 *arg += len;
1972 return &t_number;
1973 }
1974 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 case 's':
1976 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1977 {
1978 *arg += len;
1979 return &t_string;
1980 }
1981 break;
1982 case 'v':
1983 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1984 {
1985 *arg += len;
1986 return &t_void;
1987 }
1988 break;
1989 }
1990
1991 semsg(_("E1010: Type not recognized: %s"), *arg);
1992 return &t_any;
1993}
1994
1995/*
1996 * Check if "type1" and "type2" are exactly the same.
1997 */
1998 static int
1999equal_type(type_T *type1, type_T *type2)
2000{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002001 int i;
2002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 if (type1->tt_type != type2->tt_type)
2004 return FALSE;
2005 switch (type1->tt_type)
2006 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002008 case VAR_ANY:
2009 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 case VAR_SPECIAL:
2011 case VAR_BOOL:
2012 case VAR_NUMBER:
2013 case VAR_FLOAT:
2014 case VAR_STRING:
2015 case VAR_BLOB:
2016 case VAR_JOB:
2017 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002018 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 case VAR_LIST:
2020 case VAR_DICT:
2021 return equal_type(type1->tt_member, type2->tt_member);
2022 case VAR_FUNC:
2023 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002024 if (!equal_type(type1->tt_member, type2->tt_member)
2025 || type1->tt_argcount != type2->tt_argcount)
2026 return FALSE;
2027 if (type1->tt_argcount < 0
2028 || type1->tt_args == NULL || type2->tt_args == NULL)
2029 return TRUE;
2030 for (i = 0; i < type1->tt_argcount; ++i)
2031 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2032 return FALSE;
2033 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 }
2035 return TRUE;
2036}
2037
2038/*
2039 * Find the common type of "type1" and "type2" and put it in "dest".
2040 * "type2" and "dest" may be the same.
2041 */
2042 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002043common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044{
2045 if (equal_type(type1, type2))
2046 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002047 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 return;
2049 }
2050
2051 if (type1->tt_type == type2->tt_type)
2052 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2054 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002055 type_T *common;
2056
Bram Moolenaard77a8522020-04-03 21:59:57 +02002057 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002058 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002059 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002060 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 return;
2063 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002064 if (type1->tt_type == VAR_FUNC)
2065 {
2066 type_T *common;
2067
2068 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2069 if (type1->tt_argcount == type2->tt_argcount
2070 && type1->tt_argcount >= 0)
2071 {
2072 int argcount = type1->tt_argcount;
2073 int i;
2074
2075 *dest = alloc_func_type(common, argcount, type_gap);
2076 if (type1->tt_args != NULL && type2->tt_args != NULL)
2077 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002078 if (func_type_add_arg_types(*dest, argcount,
2079 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002080 for (i = 0; i < argcount; ++i)
2081 common_type(type1->tt_args[i], type2->tt_args[i],
2082 &(*dest)->tt_args[i], type_gap);
2083 }
2084 }
2085 else
2086 *dest = alloc_func_type(common, -1, type_gap);
2087 return;
2088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 }
2090
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002091 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092}
2093
2094 char *
2095vartype_name(vartype_T type)
2096{
2097 switch (type)
2098 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002099 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002100 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 case VAR_SPECIAL: return "special";
2103 case VAR_BOOL: return "bool";
2104 case VAR_NUMBER: return "number";
2105 case VAR_FLOAT: return "float";
2106 case VAR_STRING: return "string";
2107 case VAR_BLOB: return "blob";
2108 case VAR_JOB: return "job";
2109 case VAR_CHANNEL: return "channel";
2110 case VAR_LIST: return "list";
2111 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002112
2113 case VAR_FUNC:
2114 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002116 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117}
2118
2119/*
2120 * Return the name of a type.
2121 * The result may be in allocated memory, in which case "tofree" is set.
2122 */
2123 char *
2124type_name(type_T *type, char **tofree)
2125{
2126 char *name = vartype_name(type->tt_type);
2127
2128 *tofree = NULL;
2129 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2130 {
2131 char *member_free;
2132 char *member_name = type_name(type->tt_member, &member_free);
2133 size_t len;
2134
2135 len = STRLEN(name) + STRLEN(member_name) + 3;
2136 *tofree = alloc(len);
2137 if (*tofree != NULL)
2138 {
2139 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2140 vim_free(member_free);
2141 return *tofree;
2142 }
2143 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002144 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002145 {
2146 garray_T ga;
2147 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002148 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002149
2150 ga_init2(&ga, 1, 100);
2151 if (ga_grow(&ga, 20) == FAIL)
2152 return "[unknown]";
2153 *tofree = ga.ga_data;
2154 STRCPY(ga.ga_data, "func(");
2155 ga.ga_len += 5;
2156
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002157 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002158 {
2159 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002160 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161 int len;
2162
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002163 if (type->tt_args == NULL)
2164 arg_type = "[unknown]";
2165 else
2166 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002167 if (i > 0)
2168 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002169 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002170 ga.ga_len += 2;
2171 }
2172 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002173 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 {
2175 vim_free(arg_free);
2176 return "[unknown]";
2177 }
2178 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002179 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002180 {
2181 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2182 ga.ga_len += 3;
2183 }
2184 else if (i >= type->tt_min_argcount)
2185 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002186 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002187 ga.ga_len += len;
2188 vim_free(arg_free);
2189 }
2190
2191 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002192 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002193 else
2194 {
2195 char *ret_free;
2196 char *ret_name = type_name(type->tt_member, &ret_free);
2197 int len;
2198
2199 len = (int)STRLEN(ret_name) + 4;
2200 if (ga_grow(&ga, len) == FAIL)
2201 {
2202 vim_free(ret_free);
2203 return "[unknown]";
2204 }
2205 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002206 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2207 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002208 vim_free(ret_free);
2209 }
2210 return ga.ga_data;
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212
2213 return name;
2214}
2215
2216/*
2217 * Find "name" in script-local items of script "sid".
2218 * Returns the index in "sn_var_vals" if found.
2219 * If found but not in "sn_var_vals" returns -1.
2220 * If not found returns -2.
2221 */
2222 int
2223get_script_item_idx(int sid, char_u *name, int check_writable)
2224{
2225 hashtab_T *ht;
2226 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002227 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 int idx;
2229
2230 // First look the name up in the hashtable.
2231 if (sid <= 0 || sid > script_items.ga_len)
2232 return -1;
2233 ht = &SCRIPT_VARS(sid);
2234 di = find_var_in_ht(ht, 0, name, TRUE);
2235 if (di == NULL)
2236 return -2;
2237
2238 // Now find the svar_T index in sn_var_vals.
2239 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2240 {
2241 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2242
2243 if (sv->sv_tv == &di->di_tv)
2244 {
2245 if (check_writable && sv->sv_const)
2246 semsg(_(e_readonlyvar), name);
2247 return idx;
2248 }
2249 }
2250 return -1;
2251}
2252
2253/*
2254 * Find "name" in imported items of the current script/
2255 */
2256 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002257find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002259 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 int idx;
2261
2262 if (cctx != NULL)
2263 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2264 {
2265 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2266 + idx;
2267
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002268 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2269 : STRLEN(import->imp_name) == len
2270 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 return import;
2272 }
2273
2274 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2275 {
2276 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2277
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002278 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2279 : STRLEN(import->imp_name) == len
2280 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 return import;
2282 }
2283 return NULL;
2284}
2285
2286/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002287 * Free all imported variables.
2288 */
2289 static void
2290free_imported(cctx_T *cctx)
2291{
2292 int idx;
2293
2294 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2295 {
2296 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2297
2298 vim_free(import->imp_name);
2299 }
2300 ga_clear(&cctx->ctx_imports);
2301}
2302
2303/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002304 * Get the next line of the function from "cctx".
2305 * Returns NULL when at the end.
2306 */
2307 static char_u *
2308next_line_from_context(cctx_T *cctx)
2309{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002310 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002311
2312 do
2313 {
2314 ++cctx->ctx_lnum;
2315 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002316 {
2317 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002318 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002319 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002320 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002321 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002322 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2323 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002324 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002325 return line;
2326}
2327
2328/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002329 * Return TRUE if "p" points at a "#" but not at "#{".
2330 */
2331 static int
2332comment_start(char_u *p)
2333{
2334 return p[0] == '#' && p[1] != '{';
2335}
2336
2337/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002338 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002339 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002340 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2341 */
2342 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002343may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002344{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002345 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002346 {
2347 char_u *next = next_line_from_context(cctx);
2348
2349 if (next == NULL)
2350 return FAIL;
2351 *arg = skipwhite(next);
2352 }
2353 return OK;
2354}
2355
Bram Moolenaara5565e42020-05-09 15:44:01 +02002356// Structure passed between the compile_expr* functions to keep track of
2357// constants that have been parsed but for which no code was produced yet. If
2358// possible expressions on these constants are applied at compile time. If
2359// that is not possible, the code to push the constants needs to be generated
2360// before other instructions.
2361typedef struct {
2362 typval_T pp_tv[10]; // stack of ppconst constants
2363 int pp_used; // active entries in pp_tv[]
2364} ppconst_T;
2365
2366/*
2367 * Generate a PUSH instruction for "tv".
2368 * "tv" will be consumed or cleared.
2369 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2370 */
2371 static int
2372generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2373{
2374 if (tv != NULL)
2375 {
2376 switch (tv->v_type)
2377 {
2378 case VAR_UNKNOWN:
2379 break;
2380 case VAR_BOOL:
2381 generate_PUSHBOOL(cctx, tv->vval.v_number);
2382 break;
2383 case VAR_SPECIAL:
2384 generate_PUSHSPEC(cctx, tv->vval.v_number);
2385 break;
2386 case VAR_NUMBER:
2387 generate_PUSHNR(cctx, tv->vval.v_number);
2388 break;
2389#ifdef FEAT_FLOAT
2390 case VAR_FLOAT:
2391 generate_PUSHF(cctx, tv->vval.v_float);
2392 break;
2393#endif
2394 case VAR_BLOB:
2395 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2396 tv->vval.v_blob = NULL;
2397 break;
2398 case VAR_STRING:
2399 generate_PUSHS(cctx, tv->vval.v_string);
2400 tv->vval.v_string = NULL;
2401 break;
2402 default:
2403 iemsg("constant type not supported");
2404 clear_tv(tv);
2405 return FAIL;
2406 }
2407 tv->v_type = VAR_UNKNOWN;
2408 }
2409 return OK;
2410}
2411
2412/*
2413 * Generate code for any ppconst entries.
2414 */
2415 static int
2416generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2417{
2418 int i;
2419 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002420 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002421
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002422 cctx->ctx_skip = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002423 for (i = 0; i < ppconst->pp_used; ++i)
2424 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2425 ret = FAIL;
2426 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002427 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002428 return ret;
2429}
2430
2431/*
2432 * Clear ppconst constants. Used when failing.
2433 */
2434 static void
2435clear_ppconst(ppconst_T *ppconst)
2436{
2437 int i;
2438
2439 for (i = 0; i < ppconst->pp_used; ++i)
2440 clear_tv(&ppconst->pp_tv[i]);
2441 ppconst->pp_used = 0;
2442}
2443
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002444/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002445 * Generate an instruction to load script-local variable "name", without the
2446 * leading "s:".
2447 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 */
2449 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002450compile_load_scriptvar(
2451 cctx_T *cctx,
2452 char_u *name, // variable NUL terminated
2453 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002454 char_u **end, // end of variable
2455 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002457 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2459 imported_T *import;
2460
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002461 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002463 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002464 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2465 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 }
2467 if (idx >= 0)
2468 {
2469 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2470
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002471 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 current_sctx.sc_sid, idx, sv->sv_type);
2473 return OK;
2474 }
2475
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002476 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 if (import != NULL)
2478 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002479 if (import->imp_all)
2480 {
2481 char_u *p = skipwhite(*end);
2482 int name_len;
2483 ufunc_T *ufunc;
2484 type_T *type;
2485
2486 // Used "import * as Name", need to lookup the member.
2487 if (*p != '.')
2488 {
2489 semsg(_("E1060: expected dot after name: %s"), start);
2490 return FAIL;
2491 }
2492 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002493 if (VIM_ISWHITE(*p))
2494 {
2495 emsg(_("E1074: no white space allowed after dot"));
2496 return FAIL;
2497 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002498
2499 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2500 // TODO: what if it is a function?
2501 if (idx < 0)
2502 return FAIL;
2503 *end = p;
2504
2505 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2506 import->imp_sid,
2507 idx,
2508 type);
2509 }
2510 else
2511 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002512 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002513 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2514 import->imp_sid,
2515 import->imp_var_vals_idx,
2516 import->imp_type);
2517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 return OK;
2519 }
2520
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002521 if (error)
2522 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 return FAIL;
2524}
2525
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002526 static int
2527generate_funcref(cctx_T *cctx, char_u *name)
2528{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002529 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002530
2531 if (ufunc == NULL)
2532 return FAIL;
2533
2534 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2535}
2536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537/*
2538 * Compile a variable name into a load instruction.
2539 * "end" points to just after the name.
2540 * When "error" is FALSE do not give an error when not found.
2541 */
2542 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002543compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544{
2545 type_T *type;
2546 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002547 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002549 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550
2551 if (*(*arg + 1) == ':')
2552 {
2553 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002554 if (end <= *arg + 2)
2555 name = vim_strsave((char_u *)"[empty]");
2556 else
2557 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 if (name == NULL)
2559 return FAIL;
2560
2561 if (**arg == 'v')
2562 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002563 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565 else if (**arg == 'g')
2566 {
2567 // Global variables can be defined later, thus we don't check if it
2568 // exists, give error at runtime.
2569 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2570 }
2571 else if (**arg == 's')
2572 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002573 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002575 else if (**arg == 'b')
2576 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002577 // Buffer-local variables can be defined later, thus we don't check
2578 // if it exists, give error at runtime.
2579 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002580 }
2581 else if (**arg == 'w')
2582 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002583 // Window-local variables can be defined later, thus we don't check
2584 // if it exists, give error at runtime.
2585 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002586 }
2587 else if (**arg == 't')
2588 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002589 // Tabpage-local variables can be defined later, thus we don't
2590 // check if it exists, give error at runtime.
2591 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 else
2594 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002595 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 goto theend;
2597 }
2598 }
2599 else
2600 {
2601 size_t len = end - *arg;
2602 int idx;
2603 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002604 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
2606 name = vim_strnsave(*arg, end - *arg);
2607 if (name == NULL)
2608 return FAIL;
2609
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002610 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002612 if (!gen_load_outer)
2613 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 }
2615 else
2616 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002617 lvar_T *lvar = lookup_local(*arg, len, cctx);
2618
2619 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002621 type = lvar->lv_type;
2622 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002623 if (lvar->lv_from_outer)
2624 gen_load_outer = TRUE;
2625 else
2626 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 }
2628 else
2629 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002630 // "var" can be script-local even without using "s:" if it
2631 // already exists.
2632 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2633 == SCRIPT_VERSION_VIM9
2634 || lookup_script(*arg, len) == OK)
2635 res = compile_load_scriptvar(cctx, name, *arg, &end,
2636 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002637
Bram Moolenaara5565e42020-05-09 15:44:01 +02002638 // When the name starts with an uppercase letter or "x:" it
2639 // can be a user defined function.
2640 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2641 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 }
2643 }
2644 if (gen_load)
2645 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002646 if (gen_load_outer)
2647 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649
2650 *arg = end;
2651
2652theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002653 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 semsg(_(e_var_notfound), name);
2655 vim_free(name);
2656 return res;
2657}
2658
2659/*
2660 * Compile the argument expressions.
2661 * "arg" points to just after the "(" and is advanced to after the ")"
2662 */
2663 static int
2664compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2665{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002666 char_u *p = *arg;
2667 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668
Bram Moolenaare6085c52020-04-12 20:19:16 +02002669 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002671 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002672 {
2673 p = next_line_from_context(cctx);
2674 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002675 goto failret;
2676 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002677 p = skipwhite(p);
2678 }
2679 if (*p == ')')
2680 {
2681 *arg = p + 1;
2682 return OK;
2683 }
2684
Bram Moolenaara5565e42020-05-09 15:44:01 +02002685 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 return FAIL;
2687 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002688
2689 if (*p != ',' && *skipwhite(p) == ',')
2690 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002691 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002692 p = skipwhite(p);
2693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002695 {
2696 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002697 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002698 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002699 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002701 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002703failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002704 emsg(_(e_missing_close));
2705 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706}
2707
2708/*
2709 * Compile a function call: name(arg1, arg2)
2710 * "arg" points to "name", "arg + varlen" to the "(".
2711 * "argcount_init" is 1 for "value->method()"
2712 * Instructions:
2713 * EVAL arg1
2714 * EVAL arg2
2715 * BCALL / DCALL / UCALL
2716 */
2717 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002718compile_call(
2719 char_u **arg,
2720 size_t varlen,
2721 cctx_T *cctx,
2722 ppconst_T *ppconst,
2723 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724{
2725 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002726 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 int argcount = argcount_init;
2728 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002729 char_u fname_buf[FLEN_FIXED + 1];
2730 char_u *tofree = NULL;
2731 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002733 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734
Bram Moolenaara5565e42020-05-09 15:44:01 +02002735 // we can evaluate "has('name')" at compile time
2736 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2737 {
2738 char_u *s = skipwhite(*arg + varlen + 1);
2739 typval_T argvars[2];
2740
2741 argvars[0].v_type = VAR_UNKNOWN;
2742 if (*s == '"')
2743 (void)get_string_tv(&s, &argvars[0], TRUE);
2744 else if (*s == '\'')
2745 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2746 s = skipwhite(s);
2747 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2748 {
2749 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2750
2751 *arg = s + 1;
2752 argvars[1].v_type = VAR_UNKNOWN;
2753 tv->v_type = VAR_NUMBER;
2754 tv->vval.v_number = 0;
2755 f_has(argvars, tv);
2756 clear_tv(&argvars[0]);
2757 ++ppconst->pp_used;
2758 return OK;
2759 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002760 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002761 }
2762
2763 if (generate_ppconst(cctx, ppconst) == FAIL)
2764 return FAIL;
2765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 if (varlen >= sizeof(namebuf))
2767 {
2768 semsg(_("E1011: name too long: %s"), name);
2769 return FAIL;
2770 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002771 vim_strncpy(namebuf, *arg, varlen);
2772 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
2774 *arg = skipwhite(*arg + varlen + 1);
2775 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002776 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002778 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 {
2780 int idx;
2781
2782 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002783 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002785 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002786 else
2787 semsg(_(e_unknownfunc), namebuf);
2788 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002792 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002794 {
2795 res = generate_CALL(cctx, ufunc, argcount);
2796 goto theend;
2797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798
2799 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002800 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002802 if (STRNCMP(namebuf, "g:", 2) != 0
2803 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002804 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002805 garray_T *stack = &cctx->ctx_type_stack;
2806 type_T *type;
2807
2808 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2809 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002810 goto theend;
2811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002813 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002814 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002815 if (STRNCMP(namebuf, "g:", 2) == 0)
2816 res = generate_UCALL(cctx, name, argcount);
2817 else
2818 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002819
2820theend:
2821 vim_free(tofree);
2822 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823}
2824
2825// like NAMESPACE_CHAR but with 'a' and 'l'.
2826#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2827
2828/*
2829 * Find the end of a variable or function name. Unlike find_name_end() this
2830 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002831 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 * Return a pointer to just after the name. Equal to "arg" if there is no
2833 * valid name.
2834 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002835 static char_u *
2836to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837{
2838 char_u *p;
2839
2840 // Quick check for valid starting character.
2841 if (!eval_isnamec1(*arg))
2842 return arg;
2843
2844 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2845 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2846 // and can be used in slice "[n:]".
2847 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002848 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2850 break;
2851 return p;
2852}
2853
2854/*
2855 * Like to_name_end() but also skip over a list or dict constant.
2856 */
2857 char_u *
2858to_name_const_end(char_u *arg)
2859{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002860 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 typval_T rettv;
2862
2863 if (p == arg && *arg == '[')
2864 {
2865
2866 // Can be "[1, 2, 3]->Func()".
2867 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2868 p = arg;
2869 }
2870 else if (p == arg && *arg == '#' && arg[1] == '{')
2871 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002872 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 ++p;
2874 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2875 p = arg;
2876 }
2877 else if (p == arg && *arg == '{')
2878 {
2879 int ret = get_lambda_tv(&p, &rettv, FALSE);
2880
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002881 // Can be "{x -> ret}()".
2882 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 if (ret == NOTDONE)
2884 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2885 if (ret != OK)
2886 p = arg;
2887 }
2888
2889 return p;
2890}
2891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892/*
2893 * parse a list: [expr, expr]
2894 * "*arg" points to the '['.
2895 */
2896 static int
2897compile_list(char_u **arg, cctx_T *cctx)
2898{
2899 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002900 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 int count = 0;
2902
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002903 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002905 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002906 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002907 p = next_line_from_context(cctx);
2908 if (p == NULL)
2909 {
2910 semsg(_(e_list_end), *arg);
2911 return FAIL;
2912 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002913 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002914 p = skipwhite(p);
2915 }
2916 if (*p == ']')
2917 {
2918 ++p;
2919 // Allow for following comment, after at least one space.
2920 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2921 p += STRLEN(p);
2922 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002923 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002924 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 break;
2926 ++count;
2927 if (*p == ',')
2928 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002929 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 p = skipwhite(p);
2931 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002932 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933
2934 generate_NEWLIST(cctx, count);
2935 return OK;
2936}
2937
2938/*
2939 * parse a lambda: {arg, arg -> expr}
2940 * "*arg" points to the '{'.
2941 */
2942 static int
2943compile_lambda(char_u **arg, cctx_T *cctx)
2944{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 typval_T rettv;
2946 ufunc_T *ufunc;
2947
2948 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002949 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002953 ++ufunc->uf_refcount;
2954 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002955 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 // The function will have one line: "return {expr}".
2958 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002959 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960
2961 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002962 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964}
2965
2966/*
2967 * Compile a lamda call: expr->{lambda}(args)
2968 * "arg" points to the "{".
2969 */
2970 static int
2971compile_lambda_call(char_u **arg, cctx_T *cctx)
2972{
2973 ufunc_T *ufunc;
2974 typval_T rettv;
2975 int argcount = 1;
2976 int ret = FAIL;
2977
2978 // Get the funcref in "rettv".
2979 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2980 return FAIL;
2981
2982 if (**arg != '(')
2983 {
2984 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002985 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 else
2987 semsg(_(e_missing_paren), "lambda");
2988 clear_tv(&rettv);
2989 return FAIL;
2990 }
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 ufunc = rettv.vval.v_partial->pt_func;
2993 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002994 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002995 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002996
2997 // The function will have one line: "return {expr}".
2998 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002999 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000
3001 // compile the arguments
3002 *arg = skipwhite(*arg + 1);
3003 if (compile_arguments(arg, cctx, &argcount) == OK)
3004 // call the compiled function
3005 ret = generate_CALL(cctx, ufunc, argcount);
3006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 return ret;
3008}
3009
3010/*
3011 * parse a dict: {'key': val} or #{key: val}
3012 * "*arg" points to the '{'.
3013 */
3014 static int
3015compile_dict(char_u **arg, cctx_T *cctx, int literal)
3016{
3017 garray_T *instr = &cctx->ctx_instr;
3018 int count = 0;
3019 dict_T *d = dict_alloc();
3020 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003021 char_u *whitep = *arg;
3022 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023
3024 if (d == NULL)
3025 return FAIL;
3026 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003027 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 {
3029 char_u *key = NULL;
3030
Bram Moolenaar2c330432020-04-13 14:41:35 +02003031 while (**arg == NUL || (literal && **arg == '"')
3032 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003033 {
3034 *arg = next_line_from_context(cctx);
3035 if (*arg == NULL)
3036 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003037 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003038 *arg = skipwhite(*arg);
3039 }
3040
3041 if (**arg == '}')
3042 break;
3043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 if (literal)
3045 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003046 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaar2c330432020-04-13 14:41:35 +02003048 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
3050 semsg(_("E1014: Invalid key: %s"), *arg);
3051 return FAIL;
3052 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003053 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 if (generate_PUSHS(cctx, key) == FAIL)
3055 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003056 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 }
3058 else
3059 {
3060 isn_T *isn;
3061
Bram Moolenaara5565e42020-05-09 15:44:01 +02003062 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 return FAIL;
3064 // TODO: check type is string
3065 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3066 if (isn->isn_type == ISN_PUSHS)
3067 key = isn->isn_arg.string;
3068 }
3069
3070 // Check for duplicate keys, if using string keys.
3071 if (key != NULL)
3072 {
3073 item = dict_find(d, key, -1);
3074 if (item != NULL)
3075 {
3076 semsg(_(e_duplicate_key), key);
3077 goto failret;
3078 }
3079 item = dictitem_alloc(key);
3080 if (item != NULL)
3081 {
3082 item->di_tv.v_type = VAR_UNKNOWN;
3083 item->di_tv.v_lock = 0;
3084 if (dict_add(d, item) == FAIL)
3085 dictitem_free(item);
3086 }
3087 }
3088
3089 *arg = skipwhite(*arg);
3090 if (**arg != ':')
3091 {
3092 semsg(_(e_missing_dict_colon), *arg);
3093 return FAIL;
3094 }
3095
Bram Moolenaar2c330432020-04-13 14:41:35 +02003096 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003098 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003099 {
3100 *arg = next_line_from_context(cctx);
3101 if (*arg == NULL)
3102 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003103 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003104 *arg = skipwhite(*arg);
3105 }
3106
Bram Moolenaara5565e42020-05-09 15:44:01 +02003107 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 return FAIL;
3109 ++count;
3110
Bram Moolenaar2c330432020-04-13 14:41:35 +02003111 whitep = *arg;
3112 p = skipwhite(*arg);
3113 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003114 {
3115 *arg = next_line_from_context(cctx);
3116 if (*arg == NULL)
3117 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003118 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003119 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003120 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 if (**arg == '}')
3123 break;
3124 if (**arg != ',')
3125 {
3126 semsg(_(e_missing_dict_comma), *arg);
3127 goto failret;
3128 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003129 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 *arg = skipwhite(*arg + 1);
3131 }
3132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 *arg = *arg + 1;
3134
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003135 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003136 p = skipwhite(*arg);
3137 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003138 *arg += STRLEN(*arg);
3139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 dict_unref(d);
3141 return generate_NEWDICT(cctx, count);
3142
3143failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003144 if (*arg == NULL)
3145 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 dict_unref(d);
3147 return FAIL;
3148}
3149
3150/*
3151 * Compile "&option".
3152 */
3153 static int
3154compile_get_option(char_u **arg, cctx_T *cctx)
3155{
3156 typval_T rettv;
3157 char_u *start = *arg;
3158 int ret;
3159
3160 // parse the option and get the current value to get the type.
3161 rettv.v_type = VAR_UNKNOWN;
3162 ret = get_option_tv(arg, &rettv, TRUE);
3163 if (ret == OK)
3164 {
3165 // include the '&' in the name, get_option_tv() expects it.
3166 char_u *name = vim_strnsave(start, *arg - start);
3167 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3168
3169 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3170 vim_free(name);
3171 }
3172 clear_tv(&rettv);
3173
3174 return ret;
3175}
3176
3177/*
3178 * Compile "$VAR".
3179 */
3180 static int
3181compile_get_env(char_u **arg, cctx_T *cctx)
3182{
3183 char_u *start = *arg;
3184 int len;
3185 int ret;
3186 char_u *name;
3187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 ++*arg;
3189 len = get_env_len(arg);
3190 if (len == 0)
3191 {
3192 semsg(_(e_syntax_at), start - 1);
3193 return FAIL;
3194 }
3195
3196 // include the '$' in the name, get_env_tv() expects it.
3197 name = vim_strnsave(start, len + 1);
3198 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3199 vim_free(name);
3200 return ret;
3201}
3202
3203/*
3204 * Compile "@r".
3205 */
3206 static int
3207compile_get_register(char_u **arg, cctx_T *cctx)
3208{
3209 int ret;
3210
3211 ++*arg;
3212 if (**arg == NUL)
3213 {
3214 semsg(_(e_syntax_at), *arg - 1);
3215 return FAIL;
3216 }
3217 if (!valid_yank_reg(**arg, TRUE))
3218 {
3219 emsg_invreg(**arg);
3220 return FAIL;
3221 }
3222 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3223 ++*arg;
3224 return ret;
3225}
3226
3227/*
3228 * Apply leading '!', '-' and '+' to constant "rettv".
3229 */
3230 static int
3231apply_leader(typval_T *rettv, char_u *start, char_u *end)
3232{
3233 char_u *p = end;
3234
3235 // this works from end to start
3236 while (p > start)
3237 {
3238 --p;
3239 if (*p == '-' || *p == '+')
3240 {
3241 // only '-' has an effect, for '+' we only check the type
3242#ifdef FEAT_FLOAT
3243 if (rettv->v_type == VAR_FLOAT)
3244 {
3245 if (*p == '-')
3246 rettv->vval.v_float = -rettv->vval.v_float;
3247 }
3248 else
3249#endif
3250 {
3251 varnumber_T val;
3252 int error = FALSE;
3253
3254 // tv_get_number_chk() accepts a string, but we don't want that
3255 // here
3256 if (check_not_string(rettv) == FAIL)
3257 return FAIL;
3258 val = tv_get_number_chk(rettv, &error);
3259 clear_tv(rettv);
3260 if (error)
3261 return FAIL;
3262 if (*p == '-')
3263 val = -val;
3264 rettv->v_type = VAR_NUMBER;
3265 rettv->vval.v_number = val;
3266 }
3267 }
3268 else
3269 {
3270 int v = tv2bool(rettv);
3271
3272 // '!' is permissive in the type.
3273 clear_tv(rettv);
3274 rettv->v_type = VAR_BOOL;
3275 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3276 }
3277 }
3278 return OK;
3279}
3280
3281/*
3282 * Recognize v: variables that are constants and set "rettv".
3283 */
3284 static void
3285get_vim_constant(char_u **arg, typval_T *rettv)
3286{
3287 if (STRNCMP(*arg, "v:true", 6) == 0)
3288 {
3289 rettv->v_type = VAR_BOOL;
3290 rettv->vval.v_number = VVAL_TRUE;
3291 *arg += 6;
3292 }
3293 else if (STRNCMP(*arg, "v:false", 7) == 0)
3294 {
3295 rettv->v_type = VAR_BOOL;
3296 rettv->vval.v_number = VVAL_FALSE;
3297 *arg += 7;
3298 }
3299 else if (STRNCMP(*arg, "v:null", 6) == 0)
3300 {
3301 rettv->v_type = VAR_SPECIAL;
3302 rettv->vval.v_number = VVAL_NULL;
3303 *arg += 6;
3304 }
3305 else if (STRNCMP(*arg, "v:none", 6) == 0)
3306 {
3307 rettv->v_type = VAR_SPECIAL;
3308 rettv->vval.v_number = VVAL_NONE;
3309 *arg += 6;
3310 }
3311}
3312
3313/*
Bram Moolenaar61a89812020-05-07 16:58:17 +02003314 * Evaluate an expression that is a constant:
3315 * has(arg)
3316 *
3317 * Also handle:
3318 * ! in front logical NOT
3319 *
3320 * Return FAIL if the expression is not a constant.
3321 */
3322 static int
3323evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3324{
3325 typval_T argvars[2];
3326 char_u *start_leader, *end_leader;
3327 int has_call = FALSE;
3328
3329 /*
3330 * Skip '!' characters. They are handled later.
3331 * TODO: '-' and '+' characters
3332 */
3333 start_leader = *arg;
3334 while (**arg == '!')
3335 *arg = skipwhite(*arg + 1);
3336 end_leader = *arg;
3337
3338 /*
3339 * Recognize only a few types of constants for now.
3340 */
3341 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3342 {
3343 tv->v_type = VAR_BOOL;
3344 tv->vval.v_number = VVAL_TRUE;
3345 *arg += 4;
3346 return OK;
3347 }
3348 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3349 {
3350 tv->v_type = VAR_BOOL;
3351 tv->vval.v_number = VVAL_FALSE;
3352 *arg += 5;
3353 return OK;
3354 }
3355
3356 if (STRNCMP("has(", *arg, 4) == 0)
3357 {
3358 has_call = TRUE;
3359 *arg = skipwhite(*arg + 4);
3360 }
3361
3362 if (**arg == '"')
3363 {
3364 if (get_string_tv(arg, tv, TRUE) == FAIL)
3365 return FAIL;
3366 }
3367 else if (**arg == '\'')
3368 {
3369 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3370 return FAIL;
3371 }
3372 else
3373 return FAIL;
3374
3375 if (has_call)
3376 {
3377 *arg = skipwhite(*arg);
3378 if (**arg != ')')
3379 return FAIL;
3380 *arg = *arg + 1;
3381
3382 argvars[0] = *tv;
3383 argvars[1].v_type = VAR_UNKNOWN;
3384 tv->v_type = VAR_NUMBER;
3385 tv->vval.v_number = 0;
3386 f_has(argvars, tv);
3387 clear_tv(&argvars[0]);
3388
3389 while (start_leader < end_leader)
3390 {
3391 if (*start_leader == '!')
3392 tv->vval.v_number = !tv->vval.v_number;
3393 ++start_leader;
3394 }
3395 }
3396 else if (end_leader > start_leader)
3397 {
3398 clear_tv(tv);
3399 return FAIL;
3400 }
3401
3402 return OK;
3403}
3404
3405/*
3406 * * number multiplication
3407 * / number division
3408 * % number modulo
3409 */
3410 static int
3411evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv)
3412{
3413 char_u *op;
3414
3415 // get the first variable
3416 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3417 return FAIL;
3418
3419 /*
3420 * Repeat computing, until no "*", "/" or "%" is following.
3421 */
3422 for (;;)
3423 {
3424 op = skipwhite(*arg);
3425 if (*op != '*' && *op != '/' && *op != '%')
3426 break;
3427 // TODO: not implemented yet.
3428 clear_tv(tv);
3429 return FAIL;
3430 }
3431 return OK;
3432}
3433
3434/*
3435 * + number addition
3436 * - number subtraction
3437 * .. string concatenation
3438 */
3439 static int
3440evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv)
3441{
3442 char_u *op;
3443 int oplen;
3444
3445 // get the first variable
3446 if (evaluate_const_expr6(arg, cctx, tv) == FAIL)
3447 return FAIL;
3448
3449 /*
3450 * Repeat computing, until no "+", "-" or ".." is following.
3451 */
3452 for (;;)
3453 {
3454 op = skipwhite(*arg);
3455 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3456 break;
3457 oplen = (*op == '.' ? 2 : 1);
3458
3459 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3460 {
3461 clear_tv(tv);
3462 return FAIL;
3463 }
3464
3465 if (*op == '.' && tv->v_type == VAR_STRING)
3466 {
3467 typval_T tv2;
3468 size_t len1;
3469 char_u *s1, *s2;
3470
3471 tv2.v_type = VAR_UNKNOWN;
3472 *arg = skipwhite(op + oplen);
3473
3474 // TODO: what if we fail???
3475 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
3476 return FAIL;
3477
3478 // get the second variable
3479 if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL)
3480 {
3481 clear_tv(tv);
3482 return FAIL;
3483 }
3484 if (tv2.v_type != VAR_STRING)
3485 {
3486 clear_tv(tv);
3487 clear_tv(&tv2);
3488 return FAIL;
3489 }
3490 s1 = tv->vval.v_string;
3491 len1 = STRLEN(s1);
3492 s2 = tv2.vval.v_string;
3493 tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3494 if (tv->vval.v_string == NULL)
3495 {
3496 vim_free(s1);
3497 vim_free(s2);
3498 return FAIL;
3499 }
3500 mch_memmove(tv->vval.v_string, s1, len1);
3501 STRCPY(tv->vval.v_string + len1, s2);
3502 continue;
3503 }
3504
3505 // TODO: Not implemented yet.
3506 clear_tv(tv);
3507 return FAIL;
3508 }
3509 return OK;
3510}
3511
3512 static exptype_T
3513get_compare_type(char_u *p, int *len, int *type_is)
3514{
3515 exptype_T type = EXPR_UNKNOWN;
3516 int i;
3517
3518 switch (p[0])
3519 {
3520 case '=': if (p[1] == '=')
3521 type = EXPR_EQUAL;
3522 else if (p[1] == '~')
3523 type = EXPR_MATCH;
3524 break;
3525 case '!': if (p[1] == '=')
3526 type = EXPR_NEQUAL;
3527 else if (p[1] == '~')
3528 type = EXPR_NOMATCH;
3529 break;
3530 case '>': if (p[1] != '=')
3531 {
3532 type = EXPR_GREATER;
3533 *len = 1;
3534 }
3535 else
3536 type = EXPR_GEQUAL;
3537 break;
3538 case '<': if (p[1] != '=')
3539 {
3540 type = EXPR_SMALLER;
3541 *len = 1;
3542 }
3543 else
3544 type = EXPR_SEQUAL;
3545 break;
3546 case 'i': if (p[1] == 's')
3547 {
3548 // "is" and "isnot"; but not a prefix of a name
3549 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3550 *len = 5;
3551 i = p[*len];
3552 if (!isalnum(i) && i != '_')
3553 {
3554 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3555 *type_is = TRUE;
3556 }
3557 }
3558 break;
3559 }
3560 return type;
3561}
3562
3563/*
3564 * Only comparing strings is supported right now.
3565 * expr5a == expr5b
3566 */
3567 static int
3568evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3569{
3570 exptype_T type = EXPR_UNKNOWN;
3571 char_u *p;
3572 int len = 2;
3573 int type_is = FALSE;
3574
3575 // get the first variable
3576 if (evaluate_const_expr5(arg, cctx, tv) == FAIL)
3577 return FAIL;
3578
3579 p = skipwhite(*arg);
3580 type = get_compare_type(p, &len, &type_is);
3581
3582 /*
3583 * If there is a comparative operator, use it.
3584 */
3585 if (type != EXPR_UNKNOWN)
3586 {
3587 typval_T tv2;
3588 char_u *s1, *s2;
3589 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3590 int n;
3591
3592 // TODO: Only string == string is supported now
3593 if (tv->v_type != VAR_STRING)
3594 return FAIL;
3595 if (type != EXPR_EQUAL)
3596 return FAIL;
3597
3598 // get the second variable
3599 init_tv(&tv2);
3600 *arg = skipwhite(p + len);
3601 if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL
3602 || tv2.v_type != VAR_STRING)
3603 {
3604 clear_tv(&tv2);
3605 return FAIL;
3606 }
3607 s1 = tv_get_string_buf(tv, buf1);
3608 s2 = tv_get_string_buf(&tv2, buf2);
3609 n = STRCMP(s1, s2);
3610 clear_tv(tv);
3611 clear_tv(&tv2);
3612 tv->v_type = VAR_BOOL;
3613 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
3614 }
3615
3616 return OK;
3617}
3618
3619static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3620
3621/*
3622 * Compile constant || or &&.
3623 */
3624 static int
3625evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3626{
3627 char_u *p = skipwhite(*arg);
3628 int opchar = *op;
3629
3630 if (p[0] == opchar && p[1] == opchar)
3631 {
3632 int val = tv2bool(tv);
3633
3634 /*
3635 * Repeat until there is no following "||" or "&&"
3636 */
3637 while (p[0] == opchar && p[1] == opchar)
3638 {
3639 typval_T tv2;
3640
3641 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3642 return FAIL;
3643
3644 // eval the next expression
3645 *arg = skipwhite(p + 2);
3646 tv2.v_type = VAR_UNKNOWN;
3647 tv2.v_lock = 0;
3648 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3649 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3650 {
3651 clear_tv(&tv2);
3652 return FAIL;
3653 }
3654 if ((opchar == '&') == val)
3655 {
3656 // false || tv2 or true && tv2: use tv2
3657 clear_tv(tv);
3658 *tv = tv2;
3659 val = tv2bool(tv);
3660 }
3661 else
3662 clear_tv(&tv2);
3663 p = skipwhite(*arg);
3664 }
3665 }
3666
3667 return OK;
3668}
3669
3670/*
3671 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3672 * Return FAIL if the expression is not a constant.
3673 */
3674 static int
3675evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3676{
3677 // evaluate the first expression
3678 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3679 return FAIL;
3680
3681 // || and && work almost the same
3682 return evaluate_const_and_or(arg, cctx, "&&", tv);
3683}
3684
3685/*
3686 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3687 * Return FAIL if the expression is not a constant.
3688 */
3689 static int
3690evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3691{
3692 // evaluate the first expression
3693 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3694 return FAIL;
3695
3696 // || and && work almost the same
3697 return evaluate_const_and_or(arg, cctx, "||", tv);
3698}
3699
3700/*
3701 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3702 * E.g. for "has('feature')".
3703 * This does not produce error messages. "tv" should be cleared afterwards.
3704 * Return FAIL if the expression is not a constant.
3705 */
3706 static int
3707evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3708{
3709 char_u *p;
3710
3711 // evaluate the first expression
3712 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3713 return FAIL;
3714
3715 p = skipwhite(*arg);
3716 if (*p == '?')
3717 {
3718 int val = tv2bool(tv);
3719 typval_T tv2;
3720
3721 // require space before and after the ?
3722 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3723 return FAIL;
3724
3725 // evaluate the second expression; any type is accepted
3726 clear_tv(tv);
3727 *arg = skipwhite(p + 1);
3728 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3729 return FAIL;
3730
3731 // Check for the ":".
3732 p = skipwhite(*arg);
3733 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3734 return FAIL;
3735
3736 // evaluate the third expression
3737 *arg = skipwhite(p + 1);
3738 tv2.v_type = VAR_UNKNOWN;
3739 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3740 {
3741 clear_tv(&tv2);
3742 return FAIL;
3743 }
3744 if (val)
3745 {
3746 // use the expr after "?"
3747 clear_tv(&tv2);
3748 }
3749 else
3750 {
3751 // use the expr after ":"
3752 clear_tv(tv);
3753 *tv = tv2;
3754 }
3755 }
3756 return OK;
3757}
3758
Bram Moolenaara5565e42020-05-09 15:44:01 +02003759static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760
Bram Moolenaar61a89812020-05-07 16:58:17 +02003761/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 * Compile code to apply '-', '+' and '!'.
3763 */
3764 static int
3765compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3766{
3767 char_u *p = end;
3768
3769 // this works from end to start
3770 while (p > start)
3771 {
3772 --p;
3773 if (*p == '-' || *p == '+')
3774 {
3775 int negate = *p == '-';
3776 isn_T *isn;
3777
3778 // TODO: check type
3779 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3780 {
3781 --p;
3782 if (*p == '-')
3783 negate = !negate;
3784 }
3785 // only '-' has an effect, for '+' we only check the type
3786 if (negate)
3787 isn = generate_instr(cctx, ISN_NEGATENR);
3788 else
3789 isn = generate_instr(cctx, ISN_CHECKNR);
3790 if (isn == NULL)
3791 return FAIL;
3792 }
3793 else
3794 {
3795 int invert = TRUE;
3796
3797 while (p > start && p[-1] == '!')
3798 {
3799 --p;
3800 invert = !invert;
3801 }
3802 if (generate_2BOOL(cctx, invert) == FAIL)
3803 return FAIL;
3804 }
3805 }
3806 return OK;
3807}
3808
3809/*
3810 * Compile whatever comes after "name" or "name()".
3811 */
3812 static int
3813compile_subscript(
3814 char_u **arg,
3815 cctx_T *cctx,
3816 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003817 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003818 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819{
3820 for (;;)
3821 {
3822 if (**arg == '(')
3823 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003824 garray_T *stack = &cctx->ctx_type_stack;
3825 type_T *type;
3826 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003828 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003829 return FAIL;
3830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003832 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 *arg = skipwhite(*arg + 1);
3835 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3836 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003837 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 return FAIL;
3839 }
3840 else if (**arg == '-' && (*arg)[1] == '>')
3841 {
3842 char_u *p;
3843
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003844 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003845 return FAIL;
3846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 // something->method()
3848 // Apply the '!', '-' and '+' first:
3849 // -1.0->func() works like (-1.0)->func()
3850 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3851 return FAIL;
3852 *start_leader = end_leader; // don't apply again later
3853
3854 *arg = skipwhite(*arg + 2);
3855 if (**arg == '{')
3856 {
3857 // lambda call: list->{lambda}
3858 if (compile_lambda_call(arg, cctx) == FAIL)
3859 return FAIL;
3860 }
3861 else
3862 {
3863 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003864 p = *arg;
3865 if (ASCII_ISALPHA(*p) && p[1] == ':')
3866 p += 2;
3867 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 ;
3869 if (*p != '(')
3870 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003871 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return FAIL;
3873 }
3874 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003875 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 return FAIL;
3877 }
3878 }
3879 else if (**arg == '[')
3880 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003881 garray_T *stack;
3882 type_T **typep;
3883
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003884 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003885 return FAIL;
3886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 // list index: list[123]
3888 // TODO: more arguments
3889 // TODO: dict member dict['name']
3890 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003891 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 return FAIL;
3893
3894 if (**arg != ']')
3895 {
3896 emsg(_(e_missbrac));
3897 return FAIL;
3898 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003899 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900
3901 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3902 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003903 stack = &cctx->ctx_type_stack;
3904 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3905 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3906 {
3907 emsg(_(e_listreq));
3908 return FAIL;
3909 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003910 if ((*typep)->tt_type == VAR_LIST)
3911 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 }
3913 else if (**arg == '.' && (*arg)[1] != '.')
3914 {
3915 char_u *p;
3916
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003918 return FAIL;
3919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 ++*arg;
3921 p = *arg;
3922 // dictionary member: dict.name
3923 if (eval_isnamec1(*p))
3924 while (eval_isnamec(*p))
3925 MB_PTR_ADV(p);
3926 if (p == *arg)
3927 {
3928 semsg(_(e_syntax_at), *arg);
3929 return FAIL;
3930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3932 return FAIL;
3933 *arg = p;
3934 }
3935 else
3936 break;
3937 }
3938
3939 // TODO - see handle_subscript():
3940 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3941 // Don't do this when "Func" is already a partial that was bound
3942 // explicitly (pt_auto is FALSE).
3943
3944 return OK;
3945}
3946
3947/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3949 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003951 * If the value is a constant "ppconst->pp_ret" will be set.
3952 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003953 *
3954 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 */
3956
3957/*
3958 * number number constant
3959 * 0zFFFFFFFF Blob constant
3960 * "string" string constant
3961 * 'string' literal string constant
3962 * &option-name option value
3963 * @r register contents
3964 * identifier variable value
3965 * function() function call
3966 * $VAR environment variable
3967 * (expression) nested expression
3968 * [expr, expr] List
3969 * {key: val, key: val} Dictionary
3970 * #{key: val, key: val} Dictionary with literal keys
3971 *
3972 * Also handle:
3973 * ! in front logical NOT
3974 * - in front unary minus
3975 * + in front unary plus (ignored)
3976 * trailing (arg) funcref/partial call
3977 * trailing [] subscript in String or List
3978 * trailing .name entry in Dictionary
3979 * trailing ->name() method call
3980 */
3981 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003982compile_expr7(
3983 char_u **arg,
3984 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 char_u *start_leader, *end_leader;
3988 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990
3991 /*
3992 * Skip '!', '-' and '+' characters. They are handled later.
3993 */
3994 start_leader = *arg;
3995 while (**arg == '!' || **arg == '-' || **arg == '+')
3996 *arg = skipwhite(*arg + 1);
3997 end_leader = *arg;
3998
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003999 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 switch (**arg)
4001 {
4002 /*
4003 * Number constant.
4004 */
4005 case '0': // also for blob starting with 0z
4006 case '1':
4007 case '2':
4008 case '3':
4009 case '4':
4010 case '5':
4011 case '6':
4012 case '7':
4013 case '8':
4014 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004015 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 return FAIL;
4017 break;
4018
4019 /*
4020 * String constant: "string".
4021 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 return FAIL;
4024 break;
4025
4026 /*
4027 * Literal string constant: 'str''ing'.
4028 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004029 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 return FAIL;
4031 break;
4032
4033 /*
4034 * Constant Vim variable.
4035 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004036 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 ret = NOTDONE;
4038 break;
4039
4040 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004041 * "true" constant
4042 */
4043 case 't': if (STRNCMP(*arg, "true", 4) == 0
4044 && !eval_isnamec((*arg)[4]))
4045 {
4046 *arg += 4;
4047 rettv->v_type = VAR_BOOL;
4048 rettv->vval.v_number = VVAL_TRUE;
4049 }
4050 else
4051 ret = NOTDONE;
4052 break;
4053
4054 /*
4055 * "false" constant
4056 */
4057 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4058 && !eval_isnamec((*arg)[5]))
4059 {
4060 *arg += 5;
4061 rettv->v_type = VAR_BOOL;
4062 rettv->vval.v_number = VVAL_FALSE;
4063 }
4064 else
4065 ret = NOTDONE;
4066 break;
4067
4068 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 * List: [expr, expr]
4070 */
4071 case '[': ret = compile_list(arg, cctx);
4072 break;
4073
4074 /*
4075 * Dictionary: #{key: val, key: val}
4076 */
4077 case '#': if ((*arg)[1] == '{')
4078 {
4079 ++*arg;
4080 ret = compile_dict(arg, cctx, TRUE);
4081 }
4082 else
4083 ret = NOTDONE;
4084 break;
4085
4086 /*
4087 * Lambda: {arg, arg -> expr}
4088 * Dictionary: {'key': val, 'key': val}
4089 */
4090 case '{': {
4091 char_u *start = skipwhite(*arg + 1);
4092
4093 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004094 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004096 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 if (ret != FAIL && *start == '>')
4098 ret = compile_lambda(arg, cctx);
4099 else
4100 ret = compile_dict(arg, cctx, FALSE);
4101 }
4102 break;
4103
4104 /*
4105 * Option value: &name
4106 */
4107 case '&': ret = compile_get_option(arg, cctx);
4108 break;
4109
4110 /*
4111 * Environment variable: $VAR.
4112 */
4113 case '$': ret = compile_get_env(arg, cctx);
4114 break;
4115
4116 /*
4117 * Register contents: @r.
4118 */
4119 case '@': ret = compile_get_register(arg, cctx);
4120 break;
4121 /*
4122 * nested expression: (expression).
4123 */
4124 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 ret = compile_expr0(arg, cctx); // recursive!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 *arg = skipwhite(*arg);
4127 if (**arg == ')')
4128 ++*arg;
4129 else if (ret == OK)
4130 {
4131 emsg(_(e_missing_close));
4132 ret = FAIL;
4133 }
4134 break;
4135
4136 default: ret = NOTDONE;
4137 break;
4138 }
4139 if (ret == FAIL)
4140 return FAIL;
4141
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004142 if (rettv->v_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 {
4144 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004145 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004147 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 return FAIL;
4149 }
4150 start_leader = end_leader; // don't apply again below
4151
Bram Moolenaara5565e42020-05-09 15:44:01 +02004152 if (cctx->ctx_skip == TRUE)
4153 clear_tv(rettv);
4154 else
4155 // A constant expression can possibly be handled compile time,
4156 // return the value instead of generating code.
4157 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 }
4159 else if (ret == NOTDONE)
4160 {
4161 char_u *p;
4162 int r;
4163
4164 if (!eval_isnamec1(**arg))
4165 {
4166 semsg(_("E1015: Name expected: %s"), *arg);
4167 return FAIL;
4168 }
4169
4170 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004171 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 {
4174 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004177 {
4178 if (generate_ppconst(cctx, ppconst) == FAIL)
4179 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 if (r == FAIL)
4183 return FAIL;
4184 }
4185
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004186 // Handle following "[]", ".member", etc.
4187 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 ppconst) == FAIL)
4190 return FAIL;
4191 if (ppconst->pp_used > 0)
4192 {
4193 // apply the '!', '-' and '+' before the constant
4194 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4195 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4196 return FAIL;
4197 return OK;
4198 }
4199 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004201 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202}
4203
4204/*
4205 * * number multiplication
4206 * / number division
4207 * % number modulo
4208 */
4209 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211{
4212 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004213 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 return FAIL;
4218
4219 /*
4220 * Repeat computing, until no "*", "/" or "%" is following.
4221 */
4222 for (;;)
4223 {
4224 op = skipwhite(*arg);
4225 if (*op != '*' && *op != '/' && *op != '%')
4226 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004227
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004228 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 {
4230 char_u buf[3];
4231
4232 vim_strncpy(buf, op, 1);
4233 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 }
4236 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004237 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004240 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004241 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004243
4244 if (ppconst->pp_used == ppconst_used + 2
4245 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4246 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004247 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004248 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4249 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004250 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004252 // both are numbers: compute the result
4253 switch (*op)
4254 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004255 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004256 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004257 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004258 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004259 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 break;
4261 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004262 tv1->vval.v_number = res;
4263 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004264 }
4265 else
4266 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004267 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004268 generate_two_op(cctx, op);
4269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 }
4271
4272 return OK;
4273}
4274
4275/*
4276 * + number addition
4277 * - number subtraction
4278 * .. string concatenation
4279 */
4280 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282{
4283 char_u *op;
4284 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004285 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
4287 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004288 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 return FAIL;
4290
4291 /*
4292 * Repeat computing, until no "+", "-" or ".." is following.
4293 */
4294 for (;;)
4295 {
4296 op = skipwhite(*arg);
4297 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4298 break;
4299 oplen = (*op == '.' ? 2 : 1);
4300
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004301 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 {
4303 char_u buf[3];
4304
4305 vim_strncpy(buf, op, oplen);
4306 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004307 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309
4310 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004311 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004312 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004314 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 return FAIL;
4317
Bram Moolenaara5565e42020-05-09 15:44:01 +02004318 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004319 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4321 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4322 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4323 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4326 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004327
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004328 // concat/subtract/add constant numbers
4329 if (*op == '+')
4330 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4331 else if (*op == '-')
4332 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4333 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004334 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004335 // concatenate constant strings
4336 char_u *s1 = tv1->vval.v_string;
4337 char_u *s2 = tv2->vval.v_string;
4338 size_t len1 = STRLEN(s1);
4339
4340 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4341 if (tv1->vval.v_string == NULL)
4342 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004343 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004344 return FAIL;
4345 }
4346 mch_memmove(tv1->vval.v_string, s1, len1);
4347 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004348 vim_free(s1);
4349 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004350 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 }
4353 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004355 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 if (*op == '.')
4357 {
4358 if (may_generate_2STRING(-2, cctx) == FAIL
4359 || may_generate_2STRING(-1, cctx) == FAIL)
4360 return FAIL;
4361 generate_instr_drop(cctx, ISN_CONCAT, 1);
4362 }
4363 else
4364 generate_two_op(cctx, op);
4365 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * expr5a == expr5b
4373 * expr5a =~ expr5b
4374 * expr5a != expr5b
4375 * expr5a !~ expr5b
4376 * expr5a > expr5b
4377 * expr5a >= expr5b
4378 * expr5a < expr5b
4379 * expr5a <= expr5b
4380 * expr5a is expr5b
4381 * expr5a isnot expr5b
4382 *
4383 * Produces instructions:
4384 * EVAL expr5a Push result of "expr5a"
4385 * EVAL expr5b Push result of "expr5b"
4386 * COMPARE one of the compare instructions
4387 */
4388 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004389compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390{
4391 exptype_T type = EXPR_UNKNOWN;
4392 char_u *p;
4393 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
4397 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 return FAIL;
4400
4401 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004402 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403
4404 /*
4405 * If there is a comparative operator, use it.
4406 */
4407 if (type != EXPR_UNKNOWN)
4408 {
4409 int ic = FALSE; // Default: do not ignore case
4410
4411 if (type_is && (p[len] == '?' || p[len] == '#'))
4412 {
4413 semsg(_(e_invexpr2), *arg);
4414 return FAIL;
4415 }
4416 // extra question mark appended: ignore case
4417 if (p[len] == '?')
4418 {
4419 ic = TRUE;
4420 ++len;
4421 }
4422 // extra '#' appended: match case (ignored)
4423 else if (p[len] == '#')
4424 ++len;
4425 // nothing appended: match case
4426
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004427 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 {
4429 char_u buf[7];
4430
4431 vim_strncpy(buf, p, len);
4432 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004433 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 }
4435
4436 // get the second variable
4437 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004438 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004439 return FAIL;
4440
Bram Moolenaara5565e42020-05-09 15:44:01 +02004441 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 return FAIL;
4443
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 if (ppconst->pp_used == ppconst_used + 2)
4445 {
4446 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4447 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4448 int ret;
4449
4450 // Both sides are a constant, compute the result now.
4451 // First check for a valid combination of types, this is more
4452 // strict than typval_compare().
4453 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4454 ret = FAIL;
4455 else
4456 {
4457 ret = typval_compare(tv1, tv2, type, ic);
4458 tv1->v_type = VAR_BOOL;
4459 tv1->vval.v_number = tv1->vval.v_number
4460 ? VVAL_TRUE : VVAL_FALSE;
4461 clear_tv(tv2);
4462 --ppconst->pp_used;
4463 }
4464 return ret;
4465 }
4466
4467 generate_ppconst(cctx, ppconst);
4468 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 }
4470
4471 return OK;
4472}
4473
4474/*
4475 * Compile || or &&.
4476 */
4477 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478compile_and_or(
4479 char_u **arg,
4480 cctx_T *cctx,
4481 char *op,
4482 ppconst_T *ppconst,
4483 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484{
4485 char_u *p = skipwhite(*arg);
4486 int opchar = *op;
4487
4488 if (p[0] == opchar && p[1] == opchar)
4489 {
4490 garray_T *instr = &cctx->ctx_instr;
4491 garray_T end_ga;
4492
4493 /*
4494 * Repeat until there is no following "||" or "&&"
4495 */
4496 ga_init2(&end_ga, sizeof(int), 10);
4497 while (p[0] == opchar && p[1] == opchar)
4498 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004499 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4500 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004502 return FAIL;
4503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 // TODO: use ppconst if the value is a constant
4506 generate_ppconst(cctx, ppconst);
4507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 if (ga_grow(&end_ga, 1) == FAIL)
4509 {
4510 ga_clear(&end_ga);
4511 return FAIL;
4512 }
4513 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4514 ++end_ga.ga_len;
4515 generate_JUMP(cctx, opchar == '|'
4516 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4517
4518 // eval the next expression
4519 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004520 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004521 return FAIL;
4522
Bram Moolenaara5565e42020-05-09 15:44:01 +02004523 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4524 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 {
4526 ga_clear(&end_ga);
4527 return FAIL;
4528 }
4529 p = skipwhite(*arg);
4530 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
4533 // Fill in the end label in all jumps.
4534 while (end_ga.ga_len > 0)
4535 {
4536 isn_T *isn;
4537
4538 --end_ga.ga_len;
4539 isn = ((isn_T *)instr->ga_data)
4540 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4541 isn->isn_arg.jump.jump_where = instr->ga_len;
4542 }
4543 ga_clear(&end_ga);
4544 }
4545
4546 return OK;
4547}
4548
4549/*
4550 * expr4a && expr4a && expr4a logical AND
4551 *
4552 * Produces instructions:
4553 * EVAL expr4a Push result of "expr4a"
4554 * JUMP_AND_KEEP_IF_FALSE end
4555 * EVAL expr4b Push result of "expr4b"
4556 * JUMP_AND_KEEP_IF_FALSE end
4557 * EVAL expr4c Push result of "expr4c"
4558 * end:
4559 */
4560 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563 int ppconst_used = ppconst->pp_used;
4564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 return FAIL;
4568
4569 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571}
4572
4573/*
4574 * expr3a || expr3b || expr3c logical OR
4575 *
4576 * Produces instructions:
4577 * EVAL expr3a Push result of "expr3a"
4578 * JUMP_AND_KEEP_IF_TRUE end
4579 * EVAL expr3b Push result of "expr3b"
4580 * JUMP_AND_KEEP_IF_TRUE end
4581 * EVAL expr3c Push result of "expr3c"
4582 * end:
4583 */
4584 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004585compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004587 int ppconst_used = ppconst->pp_used;
4588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 return FAIL;
4592
4593 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595}
4596
4597/*
4598 * Toplevel expression: expr2 ? expr1a : expr1b
4599 *
4600 * Produces instructions:
4601 * EVAL expr2 Push result of "expr"
4602 * JUMP_IF_FALSE alt jump if false
4603 * EVAL expr1a
4604 * JUMP_ALWAYS end
4605 * alt: EVAL expr1b
4606 * end:
4607 */
4608 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610{
4611 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613
Bram Moolenaar61a89812020-05-07 16:58:17 +02004614 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 return FAIL;
4617
4618 p = skipwhite(*arg);
4619 if (*p == '?')
4620 {
4621 garray_T *instr = &cctx->ctx_instr;
4622 garray_T *stack = &cctx->ctx_type_stack;
4623 int alt_idx = instr->ga_len;
4624 int end_idx;
4625 isn_T *isn;
4626 type_T *type1;
4627 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 int has_const_expr = FALSE;
4629 int const_value = FALSE;
4630 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004632 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4633 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004635 return FAIL;
4636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638 if (ppconst->pp_used == ppconst_used + 1)
4639 {
4640 // the condition is a constant, we know whether the ? or the :
4641 // expression is to be evaluated.
4642 has_const_expr = TRUE;
4643 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4644 clear_tv(&ppconst->pp_tv[ppconst_used]);
4645 --ppconst->pp_used;
4646 cctx->ctx_skip = save_skip == TRUE || !const_value;
4647 }
4648 else
4649 {
4650 generate_ppconst(cctx, ppconst);
4651 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
4654 // evaluate the second expression; any type is accepted
4655 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004656 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004657 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004659 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 if (!has_const_expr)
4662 {
4663 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 // remember the type and drop it
4666 --stack->ga_len;
4667 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 end_idx = instr->ga_len;
4670 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4671
4672 // jump here from JUMP_IF_FALSE
4673 isn = ((isn_T *)instr->ga_data) + alt_idx;
4674 isn->isn_arg.jump.jump_where = instr->ga_len;
4675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676
4677 // Check for the ":".
4678 p = skipwhite(*arg);
4679 if (*p != ':')
4680 {
4681 emsg(_(e_missing_colon));
4682 return FAIL;
4683 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004684 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4685 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004687 return FAIL;
4688 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689
4690 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 if (has_const_expr)
4692 cctx->ctx_skip = save_skip == TRUE || const_value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004694 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004695 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 if (!has_const_expr)
4700 {
4701 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 // If the types differ, the result has a more generic type.
4704 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4705 common_type(type1, type2, &type2, cctx->ctx_type_list);
4706
4707 // jump here from JUMP_ALWAYS
4708 isn = ((isn_T *)instr->ga_data) + end_idx;
4709 isn->isn_arg.jump.jump_where = instr->ga_len;
4710 }
4711
4712 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 }
4714 return OK;
4715}
4716
4717/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 * Toplevel expression.
4719 */
4720 static int
4721compile_expr0(char_u **arg, cctx_T *cctx)
4722{
4723 ppconst_T ppconst;
4724
4725 CLEAR_FIELD(ppconst);
4726 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4727 {
4728 clear_ppconst(&ppconst);
4729 return FAIL;
4730 }
4731 if (generate_ppconst(cctx, &ppconst) == FAIL)
4732 return FAIL;
4733 return OK;
4734}
4735
4736/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 * compile "return [expr]"
4738 */
4739 static char_u *
4740compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4741{
4742 char_u *p = arg;
4743 garray_T *stack = &cctx->ctx_type_stack;
4744 type_T *stack_type;
4745
4746 if (*p != NUL && *p != '|' && *p != '\n')
4747 {
4748 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004749 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 return NULL;
4751
4752 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4753 if (set_return_type)
4754 cctx->ctx_ufunc->uf_ret_type = stack_type;
4755 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4756 == FAIL)
4757 return NULL;
4758 }
4759 else
4760 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004761 // "set_return_type" cannot be TRUE, only used for a lambda which
4762 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004763 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4764 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 {
4766 emsg(_("E1003: Missing return value"));
4767 return NULL;
4768 }
4769
4770 // No argument, return zero.
4771 generate_PUSHNR(cctx, 0);
4772 }
4773
4774 if (generate_instr(cctx, ISN_RETURN) == NULL)
4775 return NULL;
4776
4777 // "return val | endif" is possible
4778 return skipwhite(p);
4779}
4780
4781/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004782 * Get a line from the compilation context, compatible with exarg_T getline().
4783 * Return a pointer to the line in allocated memory.
4784 * Return NULL for end-of-file or some error.
4785 */
4786 static char_u *
4787exarg_getline(
4788 int c UNUSED,
4789 void *cookie,
4790 int indent UNUSED,
4791 int do_concat UNUSED)
4792{
4793 cctx_T *cctx = (cctx_T *)cookie;
4794
4795 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4796 {
4797 iemsg("Heredoc got to end");
4798 return NULL;
4799 }
4800 ++cctx->ctx_lnum;
4801 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4802 [cctx->ctx_lnum]);
4803}
4804
4805/*
4806 * Compile a nested :def command.
4807 */
4808 static char_u *
4809compile_nested_function(exarg_T *eap, cctx_T *cctx)
4810{
4811 char_u *name_start = eap->arg;
4812 char_u *name_end = to_name_end(eap->arg, FALSE);
4813 char_u *name = get_lambda_name();
4814 lvar_T *lvar;
4815 ufunc_T *ufunc;
4816
4817 eap->arg = name_end;
4818 eap->getline = exarg_getline;
4819 eap->cookie = cctx;
4820 eap->skip = cctx->ctx_skip == TRUE;
4821 eap->forceit = FALSE;
4822 ufunc = def_function(eap, name, cctx);
4823
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004824 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004825 return NULL;
4826
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004827 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004828 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004829 TRUE, ufunc->uf_func_type);
4830
4831 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4832 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4833 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834
Bram Moolenaar61a89812020-05-07 16:58:17 +02004835 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004836 return (char_u *)"";
4837}
4838
4839/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 * Return the length of an assignment operator, or zero if there isn't one.
4841 */
4842 int
4843assignment_len(char_u *p, int *heredoc)
4844{
4845 if (*p == '=')
4846 {
4847 if (p[1] == '<' && p[2] == '<')
4848 {
4849 *heredoc = TRUE;
4850 return 3;
4851 }
4852 return 1;
4853 }
4854 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4855 return 2;
4856 if (STRNCMP(p, "..=", 3) == 0)
4857 return 3;
4858 return 0;
4859}
4860
4861// words that cannot be used as a variable
4862static char *reserved[] = {
4863 "true",
4864 "false",
4865 NULL
4866};
4867
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004868typedef enum {
4869 dest_local,
4870 dest_option,
4871 dest_env,
4872 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004873 dest_buffer,
4874 dest_window,
4875 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004876 dest_vimvar,
4877 dest_script,
4878 dest_reg,
4879} assign_dest_T;
4880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881/*
4882 * compile "let var [= expr]", "const var = expr" and "var = expr"
4883 * "arg" points to "var".
4884 */
4885 static char_u *
4886compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4887{
4888 char_u *p;
4889 char_u *ret = NULL;
4890 int var_count = 0;
4891 int semicolon = 0;
4892 size_t varlen;
4893 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004894 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004897 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004899 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 int oplen = 0;
4901 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004902 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004903 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 char_u *name;
4905 char_u *sp;
4906 int has_type = FALSE;
4907 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4908 int instr_count = -1;
4909
4910 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4911 if (p == NULL)
4912 return NULL;
4913 if (var_count > 0)
4914 {
4915 // TODO: let [var, var] = list
4916 emsg("Cannot handle a list yet");
4917 return NULL;
4918 }
4919
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004920 // "a: type" is declaring variable "a" with a type, not "a:".
4921 if (is_decl && p == arg + 2 && p[-1] == ':')
4922 --p;
4923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 varlen = p - arg;
4925 name = vim_strnsave(arg, (int)varlen);
4926 if (name == NULL)
4927 return NULL;
4928
Bram Moolenaar080457c2020-03-03 21:53:32 +01004929 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004931 if (*arg == '&')
4932 {
4933 int cc;
4934 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935
Bram Moolenaar080457c2020-03-03 21:53:32 +01004936 dest = dest_option;
4937 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004939 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 if (is_decl)
4943 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004944 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 goto theend;
4946 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004947 p = arg;
4948 p = find_option_end(&p, &opt_flags);
4949 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004951 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004952 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004953 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004954 }
4955 cc = *p;
4956 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004957 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004958 *p = cc;
4959 if (opt_type == -3)
4960 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004961 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004962 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004963 }
4964 if (opt_type == -2 || opt_type == 0)
4965 type = &t_string;
4966 else
4967 type = &t_number; // both number and boolean option
4968 }
4969 else if (*arg == '$')
4970 {
4971 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004972 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004973 if (is_decl)
4974 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004975 semsg(_("E1065: Cannot declare an environment variable: %s"),
4976 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004977 goto theend;
4978 }
4979 }
4980 else if (*arg == '@')
4981 {
4982 if (!valid_yank_reg(arg[1], TRUE))
4983 {
4984 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004985 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004986 }
4987 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004988 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004989 if (is_decl)
4990 {
4991 semsg(_("E1066: Cannot declare a register: %s"), name);
4992 goto theend;
4993 }
4994 }
4995 else if (STRNCMP(arg, "g:", 2) == 0)
4996 {
4997 dest = dest_global;
4998 if (is_decl)
4999 {
5000 semsg(_("E1016: Cannot declare a global variable: %s"), name);
5001 goto theend;
5002 }
5003 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02005004 else if (STRNCMP(arg, "b:", 2) == 0)
5005 {
5006 dest = dest_buffer;
5007 if (is_decl)
5008 {
5009 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
5010 goto theend;
5011 }
5012 }
5013 else if (STRNCMP(arg, "w:", 2) == 0)
5014 {
5015 dest = dest_window;
5016 if (is_decl)
5017 {
5018 semsg(_("E1079: Cannot declare a window variable: %s"), name);
5019 goto theend;
5020 }
5021 }
5022 else if (STRNCMP(arg, "t:", 2) == 0)
5023 {
5024 dest = dest_tab;
5025 if (is_decl)
5026 {
5027 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
5028 goto theend;
5029 }
5030 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01005031 else if (STRNCMP(arg, "v:", 2) == 0)
5032 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005033 typval_T *vtv;
5034 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005035
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005036 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005037 if (vimvaridx < 0)
5038 {
5039 semsg(_(e_var_notfound), arg);
5040 goto theend;
5041 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005042 // We use the current value of "sandbox" here, is that OK?
5043 if (var_check_ro(di_flags, name, FALSE))
5044 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005045 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005046 vtv = get_vim_var_tv(vimvaridx);
5047 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005048 if (is_decl)
5049 {
5050 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
5051 goto theend;
5052 }
5053 }
5054 else
5055 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005056 int idx;
5057
Bram Moolenaar080457c2020-03-03 21:53:32 +01005058 for (idx = 0; reserved[idx] != NULL; ++idx)
5059 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01005061 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 goto theend;
5063 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01005064
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005065 lvar = lookup_local(arg, varlen, cctx);
5066 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01005068 if (is_decl)
5069 {
5070 semsg(_("E1017: Variable already declared: %s"), name);
5071 goto theend;
5072 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005073 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005074 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005075 semsg(_("E1018: Cannot assign to a constant: %s"), name);
5076 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005077 }
5078 }
5079 else if (STRNCMP(arg, "s:", 2) == 0
5080 || lookup_script(arg, varlen) == OK
5081 || find_imported(arg, varlen, cctx) != NULL)
5082 {
5083 dest = dest_script;
5084 if (is_decl)
5085 {
5086 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005087 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005088 goto theend;
5089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005091 else if (name[1] == ':' && name[2] != NUL)
5092 {
5093 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
5094 goto theend;
5095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 }
5097 }
5098
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005099 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 {
5101 if (is_decl && *p == ':')
5102 {
5103 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005104 if (!VIM_ISWHITE(p[1]))
5105 {
5106 semsg(_(e_white_after), ":");
5107 goto theend;
5108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 p = skipwhite(p + 1);
5110 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 has_type = TRUE;
5112 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005113 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
5116
5117 sp = p;
5118 p = skipwhite(p);
5119 op = p;
5120 oplen = assignment_len(p, &heredoc);
5121 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5122 {
5123 char_u buf[4];
5124
5125 vim_strncpy(buf, op, oplen);
5126 semsg(_(e_white_both), buf);
5127 }
5128
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005129 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02005130 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005132 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 goto theend;
5134 }
5135
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005136 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 {
5138 if (oplen > 1 && !heredoc)
5139 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005140 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5142 name);
5143 goto theend;
5144 }
5145
5146 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02005147 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005148 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005149 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
5150 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02005152 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 }
5154
5155 if (heredoc)
5156 {
5157 list_T *l;
5158 listitem_T *li;
5159
5160 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005161 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005163 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164
5165 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005166 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 {
5168 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5169 li->li_tv.vval.v_string = NULL;
5170 }
5171 generate_NEWLIST(cctx, l->lv_len);
5172 type = &t_list_string;
5173 list_free(l);
5174 p += STRLEN(p);
5175 }
5176 else if (oplen > 0)
5177 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02005178 int r;
5179 type_T *stacktype;
5180 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 // for "+=", "*=", "..=" etc. first load the current value
5183 if (*op != '=')
5184 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005185 switch (dest)
5186 {
5187 case dest_option:
5188 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02005189 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005190 break;
5191 case dest_global:
5192 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5193 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005194 case dest_buffer:
5195 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5196 break;
5197 case dest_window:
5198 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5199 break;
5200 case dest_tab:
5201 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5202 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005203 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005204 compile_load_scriptvar(cctx,
5205 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005206 break;
5207 case dest_env:
5208 // Include $ in the name here
5209 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5210 break;
5211 case dest_reg:
5212 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5213 break;
5214 case dest_vimvar:
5215 generate_LOADV(cctx, name + 2, TRUE);
5216 break;
5217 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005218 if (lvar->lv_from_outer)
5219 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5220 NULL, type);
5221 else
5222 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005223 break;
5224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 }
5226
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005227 // Compile the expression. Temporarily hide the new local variable
5228 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005229 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005230 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 instr_count = instr->ga_len;
5232 p = skipwhite(p + oplen);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005233 r = compile_expr0(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005234 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005235 ++cctx->ctx_locals.ga_len;
5236 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 goto theend;
5238
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005239 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005241 stack = &cctx->ctx_type_stack;
5242 stacktype = stack->ga_len == 0 ? &t_void
5243 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005244 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005246 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005248 if (stacktype->tt_type == VAR_VOID)
5249 {
5250 emsg(_("E1031: Cannot use void value"));
5251 goto theend;
5252 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005253 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005254 {
5255 // An empty list or dict has a &t_void member, for a
5256 // variable that implies &t_any.
5257 if (stacktype == &t_list_empty)
5258 lvar->lv_type = &t_list_any;
5259 else if (stacktype == &t_dict_empty)
5260 lvar->lv_type = &t_dict_any;
5261 else
5262 lvar->lv_type = stacktype;
5263 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005264 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005265 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5266 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005268 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005269 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270 }
5271 }
5272 else if (cmdidx == CMD_const)
5273 {
5274 emsg(_("E1021: const requires a value"));
5275 goto theend;
5276 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005277 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 {
5279 emsg(_("E1022: type or initialization required"));
5280 goto theend;
5281 }
5282 else
5283 {
5284 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 if (ga_grow(instr, 1) == FAIL)
5286 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005287 switch (type->tt_type)
5288 {
5289 case VAR_BOOL:
5290 generate_PUSHBOOL(cctx, VVAL_FALSE);
5291 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005292 case VAR_FLOAT:
5293#ifdef FEAT_FLOAT
5294 generate_PUSHF(cctx, 0.0);
5295#endif
5296 break;
5297 case VAR_STRING:
5298 generate_PUSHS(cctx, NULL);
5299 break;
5300 case VAR_BLOB:
5301 generate_PUSHBLOB(cctx, NULL);
5302 break;
5303 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005304 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005305 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005306 case VAR_LIST:
5307 generate_NEWLIST(cctx, 0);
5308 break;
5309 case VAR_DICT:
5310 generate_NEWDICT(cctx, 0);
5311 break;
5312 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005313 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005314 break;
5315 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005316 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005317 break;
5318 case VAR_NUMBER:
5319 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005320 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005321 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005322 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005323 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005324 generate_PUSHNR(cctx, 0);
5325 break;
5326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327 }
5328
5329 if (oplen > 0 && *op != '=')
5330 {
5331 type_T *expected = &t_number;
5332 garray_T *stack = &cctx->ctx_type_stack;
5333 type_T *stacktype;
5334
5335 // TODO: if type is known use float or any operation
5336
5337 if (*op == '.')
5338 expected = &t_string;
5339 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5340 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5341 goto theend;
5342
5343 if (*op == '.')
5344 generate_instr_drop(cctx, ISN_CONCAT, 1);
5345 else
5346 {
5347 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5348
5349 if (isn == NULL)
5350 goto theend;
5351 switch (*op)
5352 {
5353 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5354 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5355 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5356 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5357 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5358 }
5359 }
5360 }
5361
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005362 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005364 case dest_option:
5365 generate_STOREOPT(cctx, name + 1, opt_flags);
5366 break;
5367 case dest_global:
5368 // include g: with the name, easier to execute that way
5369 generate_STORE(cctx, ISN_STOREG, 0, name);
5370 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005371 case dest_buffer:
5372 // include b: with the name, easier to execute that way
5373 generate_STORE(cctx, ISN_STOREB, 0, name);
5374 break;
5375 case dest_window:
5376 // include w: with the name, easier to execute that way
5377 generate_STORE(cctx, ISN_STOREW, 0, name);
5378 break;
5379 case dest_tab:
5380 // include t: with the name, easier to execute that way
5381 generate_STORE(cctx, ISN_STORET, 0, name);
5382 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005383 case dest_env:
5384 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5385 break;
5386 case dest_reg:
5387 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5388 break;
5389 case dest_vimvar:
5390 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5391 break;
5392 case dest_script:
5393 {
5394 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5395 imported_T *import = NULL;
5396 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005397 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005398
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005399 if (name[1] != ':')
5400 {
5401 import = find_imported(name, 0, cctx);
5402 if (import != NULL)
5403 sid = import->imp_sid;
5404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005406 idx = get_script_item_idx(sid, rawname, TRUE);
5407 // TODO: specific type
5408 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005409 {
5410 char_u *name_s = name;
5411
5412 // Include s: in the name for store_var()
5413 if (name[1] != ':')
5414 {
5415 int len = (int)STRLEN(name) + 3;
5416
5417 name_s = alloc(len);
5418 if (name_s == NULL)
5419 name_s = name;
5420 else
5421 vim_snprintf((char *)name_s, len, "s:%s", name);
5422 }
5423 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5424 if (name_s != name)
5425 vim_free(name_s);
5426 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005427 else
5428 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5429 sid, idx, &t_any);
5430 }
5431 break;
5432 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005433 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005434 {
5435 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005437 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5438 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005439 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5440 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005441 {
5442 varnumber_T val = isn->isn_arg.number;
5443 garray_T *stack = &cctx->ctx_type_stack;
5444
5445 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005446 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005447 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005448 if (stack->ga_len > 0)
5449 --stack->ga_len;
5450 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005451 else if (lvar->lv_from_outer)
5452 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005453 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005454 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005455 }
5456 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 }
5458 ret = p;
5459
5460theend:
5461 vim_free(name);
5462 return ret;
5463}
5464
5465/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005466 * Check if "name" can be "unlet".
5467 */
5468 int
5469check_vim9_unlet(char_u *name)
5470{
5471 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5472 {
5473 semsg(_("E1081: Cannot unlet %s"), name);
5474 return FAIL;
5475 }
5476 return OK;
5477}
5478
5479/*
5480 * Callback passed to ex_unletlock().
5481 */
5482 static int
5483compile_unlet(
5484 lval_T *lvp,
5485 char_u *name_end,
5486 exarg_T *eap,
5487 int deep UNUSED,
5488 void *coookie)
5489{
5490 cctx_T *cctx = coookie;
5491
5492 if (lvp->ll_tv == NULL)
5493 {
5494 char_u *p = lvp->ll_name;
5495 int cc = *name_end;
5496 int ret = OK;
5497
5498 // Normal name. Only supports g:, w:, t: and b: namespaces.
5499 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005500 if (*p == '$')
5501 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5502 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005503 ret = FAIL;
5504 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005505 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005506
5507 *name_end = cc;
5508 return ret;
5509 }
5510
5511 // TODO: unlet {list}[idx]
5512 // TODO: unlet {dict}[key]
5513 emsg("Sorry, :unlet not fully implemented yet");
5514 return FAIL;
5515}
5516
5517/*
5518 * compile "unlet var", "lock var" and "unlock var"
5519 * "arg" points to "var".
5520 */
5521 static char_u *
5522compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5523{
5524 char_u *p = arg;
5525
5526 if (eap->cmdidx != CMD_unlet)
5527 {
5528 emsg("Sorry, :lock and unlock not implemented yet");
5529 return NULL;
5530 }
5531
5532 if (*p == '!')
5533 {
5534 p = skipwhite(p + 1);
5535 eap->forceit = TRUE;
5536 }
5537
5538 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5539 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5540}
5541
5542/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543 * Compile an :import command.
5544 */
5545 static char_u *
5546compile_import(char_u *arg, cctx_T *cctx)
5547{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005548 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549}
5550
5551/*
5552 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5553 */
5554 static int
5555compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5556{
5557 garray_T *instr = &cctx->ctx_instr;
5558 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5559
5560 if (endlabel == NULL)
5561 return FAIL;
5562 endlabel->el_next = *el;
5563 *el = endlabel;
5564 endlabel->el_end_label = instr->ga_len;
5565
5566 generate_JUMP(cctx, when, 0);
5567 return OK;
5568}
5569
5570 static void
5571compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5572{
5573 garray_T *instr = &cctx->ctx_instr;
5574
5575 while (*el != NULL)
5576 {
5577 endlabel_T *cur = (*el);
5578 isn_T *isn;
5579
5580 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5581 isn->isn_arg.jump.jump_where = instr->ga_len;
5582 *el = cur->el_next;
5583 vim_free(cur);
5584 }
5585}
5586
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005587 static void
5588compile_free_jump_to_end(endlabel_T **el)
5589{
5590 while (*el != NULL)
5591 {
5592 endlabel_T *cur = (*el);
5593
5594 *el = cur->el_next;
5595 vim_free(cur);
5596 }
5597}
5598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599/*
5600 * Create a new scope and set up the generic items.
5601 */
5602 static scope_T *
5603new_scope(cctx_T *cctx, scopetype_T type)
5604{
5605 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5606
5607 if (scope == NULL)
5608 return NULL;
5609 scope->se_outer = cctx->ctx_scope;
5610 cctx->ctx_scope = scope;
5611 scope->se_type = type;
5612 scope->se_local_count = cctx->ctx_locals.ga_len;
5613 return scope;
5614}
5615
5616/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005617 * Free the current scope and go back to the outer scope.
5618 */
5619 static void
5620drop_scope(cctx_T *cctx)
5621{
5622 scope_T *scope = cctx->ctx_scope;
5623
5624 if (scope == NULL)
5625 {
5626 iemsg("calling drop_scope() without a scope");
5627 return;
5628 }
5629 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005630 switch (scope->se_type)
5631 {
5632 case IF_SCOPE:
5633 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5634 case FOR_SCOPE:
5635 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5636 case WHILE_SCOPE:
5637 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5638 case TRY_SCOPE:
5639 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5640 case NO_SCOPE:
5641 case BLOCK_SCOPE:
5642 break;
5643 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005644 vim_free(scope);
5645}
5646
5647/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648 * compile "if expr"
5649 *
5650 * "if expr" Produces instructions:
5651 * EVAL expr Push result of "expr"
5652 * JUMP_IF_FALSE end
5653 * ... body ...
5654 * end:
5655 *
5656 * "if expr | else" Produces instructions:
5657 * EVAL expr Push result of "expr"
5658 * JUMP_IF_FALSE else
5659 * ... body ...
5660 * JUMP_ALWAYS end
5661 * else:
5662 * ... body ...
5663 * end:
5664 *
5665 * "if expr1 | elseif expr2 | else" Produces instructions:
5666 * EVAL expr Push result of "expr"
5667 * JUMP_IF_FALSE elseif
5668 * ... body ...
5669 * JUMP_ALWAYS end
5670 * elseif:
5671 * EVAL expr Push result of "expr"
5672 * JUMP_IF_FALSE else
5673 * ... body ...
5674 * JUMP_ALWAYS end
5675 * else:
5676 * ... body ...
5677 * end:
5678 */
5679 static char_u *
5680compile_if(char_u *arg, cctx_T *cctx)
5681{
5682 char_u *p = arg;
5683 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005684 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005686 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687
Bram Moolenaara5565e42020-05-09 15:44:01 +02005688 CLEAR_FIELD(ppconst);
5689 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005690 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005691 clear_ppconst(&ppconst);
5692 return NULL;
5693 }
5694 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5695 {
5696 // The expression results in a constant.
5697 // TODO: how about nesting?
5698 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5699 clear_ppconst(&ppconst);
5700 }
5701 else
5702 {
5703 // Not a constant, generate instructions for the expression.
5704 cctx->ctx_skip = MAYBE;
5705 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005706 return NULL;
5707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
5709 scope = new_scope(cctx, IF_SCOPE);
5710 if (scope == NULL)
5711 return NULL;
5712
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005713 if (cctx->ctx_skip == MAYBE)
5714 {
5715 // "where" is set when ":elseif", "else" or ":endif" is found
5716 scope->se_u.se_if.is_if_label = instr->ga_len;
5717 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5718 }
5719 else
5720 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721
5722 return p;
5723}
5724
5725 static char_u *
5726compile_elseif(char_u *arg, cctx_T *cctx)
5727{
5728 char_u *p = arg;
5729 garray_T *instr = &cctx->ctx_instr;
5730 isn_T *isn;
5731 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005732 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734 if (scope == NULL || scope->se_type != IF_SCOPE)
5735 {
5736 emsg(_(e_elseif_without_if));
5737 return NULL;
5738 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005739 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740
Bram Moolenaar158906c2020-02-06 20:39:45 +01005741 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005742 {
5743 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005745 return NULL;
5746 // previous "if" or "elseif" jumps here
5747 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5748 isn->isn_arg.jump.jump_where = instr->ga_len;
5749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005751 // compile "expr"; if we know it evaluates to FALSE skip the block
5752 tv.v_type = VAR_UNKNOWN;
5753 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5754 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5755 else
5756 cctx->ctx_skip = MAYBE;
5757 clear_tv(&tv);
5758 if (cctx->ctx_skip == MAYBE)
5759 {
5760 p = arg;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005761 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005762 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005764 // "where" is set when ":elseif", "else" or ":endif" is found
5765 scope->se_u.se_if.is_if_label = instr->ga_len;
5766 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5767 }
5768 else
5769 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770
5771 return p;
5772}
5773
5774 static char_u *
5775compile_else(char_u *arg, cctx_T *cctx)
5776{
5777 char_u *p = arg;
5778 garray_T *instr = &cctx->ctx_instr;
5779 isn_T *isn;
5780 scope_T *scope = cctx->ctx_scope;
5781
5782 if (scope == NULL || scope->se_type != IF_SCOPE)
5783 {
5784 emsg(_(e_else_without_if));
5785 return NULL;
5786 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005787 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005789 // jump from previous block to the end, unless the else block is empty
5790 if (cctx->ctx_skip == MAYBE)
5791 {
5792 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005794 return NULL;
5795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796
Bram Moolenaar158906c2020-02-06 20:39:45 +01005797 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005798 {
5799 if (scope->se_u.se_if.is_if_label >= 0)
5800 {
5801 // previous "if" or "elseif" jumps here
5802 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5803 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005804 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005805 }
5806 }
5807
5808 if (cctx->ctx_skip != MAYBE)
5809 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
5811 return p;
5812}
5813
5814 static char_u *
5815compile_endif(char_u *arg, cctx_T *cctx)
5816{
5817 scope_T *scope = cctx->ctx_scope;
5818 ifscope_T *ifscope;
5819 garray_T *instr = &cctx->ctx_instr;
5820 isn_T *isn;
5821
5822 if (scope == NULL || scope->se_type != IF_SCOPE)
5823 {
5824 emsg(_(e_endif_without_if));
5825 return NULL;
5826 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005827 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005828 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005830 if (scope->se_u.se_if.is_if_label >= 0)
5831 {
5832 // previous "if" or "elseif" jumps here
5833 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5834 isn->isn_arg.jump.jump_where = instr->ga_len;
5835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 // Fill in the "end" label in jumps at the end of the blocks.
5837 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005838 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005840 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841 return arg;
5842}
5843
5844/*
5845 * compile "for var in expr"
5846 *
5847 * Produces instructions:
5848 * PUSHNR -1
5849 * STORE loop-idx Set index to -1
5850 * EVAL expr Push result of "expr"
5851 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5852 * - if beyond end, jump to "end"
5853 * - otherwise get item from list and push it
5854 * STORE var Store item in "var"
5855 * ... body ...
5856 * JUMP top Jump back to repeat
5857 * end: DROP Drop the result of "expr"
5858 *
5859 */
5860 static char_u *
5861compile_for(char_u *arg, cctx_T *cctx)
5862{
5863 char_u *p;
5864 size_t varlen;
5865 garray_T *instr = &cctx->ctx_instr;
5866 garray_T *stack = &cctx->ctx_type_stack;
5867 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005868 lvar_T *loop_lvar; // loop iteration variable
5869 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 type_T *vartype;
5871
5872 // TODO: list of variables: "for [key, value] in dict"
5873 // parse "var"
5874 for (p = arg; eval_isnamec1(*p); ++p)
5875 ;
5876 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005877 var_lvar = lookup_local(arg, varlen, cctx);
5878 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879 {
5880 semsg(_("E1023: variable already defined: %s"), arg);
5881 return NULL;
5882 }
5883
5884 // consume "in"
5885 p = skipwhite(p);
5886 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5887 {
5888 emsg(_(e_missing_in));
5889 return NULL;
5890 }
5891 p = skipwhite(p + 2);
5892
5893
5894 scope = new_scope(cctx, FOR_SCOPE);
5895 if (scope == NULL)
5896 return NULL;
5897
5898 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005899 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5900 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005901 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005902 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005903 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906
5907 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005908 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5909 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005910 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005911 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005912 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005916 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917
5918 // compile "expr", it remains on the stack until "endfor"
5919 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005920 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005921 {
5922 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925
5926 // now we know the type of "var"
5927 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5928 if (vartype->tt_type != VAR_LIST)
5929 {
5930 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005931 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932 return NULL;
5933 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005934 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005935 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936
5937 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005938 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005940 generate_FOR(cctx, loop_lvar->lv_idx);
5941 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
5943 return arg;
5944}
5945
5946/*
5947 * compile "endfor"
5948 */
5949 static char_u *
5950compile_endfor(char_u *arg, cctx_T *cctx)
5951{
5952 garray_T *instr = &cctx->ctx_instr;
5953 scope_T *scope = cctx->ctx_scope;
5954 forscope_T *forscope;
5955 isn_T *isn;
5956
5957 if (scope == NULL || scope->se_type != FOR_SCOPE)
5958 {
5959 emsg(_(e_for));
5960 return NULL;
5961 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005962 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005964 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965
5966 // At end of ":for" scope jump back to the FOR instruction.
5967 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5968
5969 // Fill in the "end" label in the FOR statement so it can jump here
5970 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5971 isn->isn_arg.forloop.for_end = instr->ga_len;
5972
5973 // Fill in the "end" label any BREAK statements
5974 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5975
5976 // Below the ":for" scope drop the "expr" list from the stack.
5977 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5978 return NULL;
5979
5980 vim_free(scope);
5981
5982 return arg;
5983}
5984
5985/*
5986 * compile "while expr"
5987 *
5988 * Produces instructions:
5989 * top: EVAL expr Push result of "expr"
5990 * JUMP_IF_FALSE end jump if false
5991 * ... body ...
5992 * JUMP top Jump back to repeat
5993 * end:
5994 *
5995 */
5996 static char_u *
5997compile_while(char_u *arg, cctx_T *cctx)
5998{
5999 char_u *p = arg;
6000 garray_T *instr = &cctx->ctx_instr;
6001 scope_T *scope;
6002
6003 scope = new_scope(cctx, WHILE_SCOPE);
6004 if (scope == NULL)
6005 return NULL;
6006
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006007 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008
6009 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006010 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 return NULL;
6012
6013 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006014 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 JUMP_IF_FALSE, cctx) == FAIL)
6016 return FAIL;
6017
6018 return p;
6019}
6020
6021/*
6022 * compile "endwhile"
6023 */
6024 static char_u *
6025compile_endwhile(char_u *arg, cctx_T *cctx)
6026{
6027 scope_T *scope = cctx->ctx_scope;
6028
6029 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6030 {
6031 emsg(_(e_while));
6032 return NULL;
6033 }
6034 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006035 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036
6037 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006038 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039
6040 // Fill in the "end" label in the WHILE statement so it can jump here.
6041 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006042 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043
6044 vim_free(scope);
6045
6046 return arg;
6047}
6048
6049/*
6050 * compile "continue"
6051 */
6052 static char_u *
6053compile_continue(char_u *arg, cctx_T *cctx)
6054{
6055 scope_T *scope = cctx->ctx_scope;
6056
6057 for (;;)
6058 {
6059 if (scope == NULL)
6060 {
6061 emsg(_(e_continue));
6062 return NULL;
6063 }
6064 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6065 break;
6066 scope = scope->se_outer;
6067 }
6068
6069 // Jump back to the FOR or WHILE instruction.
6070 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006071 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6072 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 return arg;
6074}
6075
6076/*
6077 * compile "break"
6078 */
6079 static char_u *
6080compile_break(char_u *arg, cctx_T *cctx)
6081{
6082 scope_T *scope = cctx->ctx_scope;
6083 endlabel_T **el;
6084
6085 for (;;)
6086 {
6087 if (scope == NULL)
6088 {
6089 emsg(_(e_break));
6090 return NULL;
6091 }
6092 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6093 break;
6094 scope = scope->se_outer;
6095 }
6096
6097 // Jump to the end of the FOR or WHILE loop.
6098 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006099 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006101 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6103 return FAIL;
6104
6105 return arg;
6106}
6107
6108/*
6109 * compile "{" start of block
6110 */
6111 static char_u *
6112compile_block(char_u *arg, cctx_T *cctx)
6113{
6114 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6115 return NULL;
6116 return skipwhite(arg + 1);
6117}
6118
6119/*
6120 * compile end of block: drop one scope
6121 */
6122 static void
6123compile_endblock(cctx_T *cctx)
6124{
6125 scope_T *scope = cctx->ctx_scope;
6126
6127 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006128 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 vim_free(scope);
6130}
6131
6132/*
6133 * compile "try"
6134 * Creates a new scope for the try-endtry, pointing to the first catch and
6135 * finally.
6136 * Creates another scope for the "try" block itself.
6137 * TRY instruction sets up exception handling at runtime.
6138 *
6139 * "try"
6140 * TRY -> catch1, -> finally push trystack entry
6141 * ... try block
6142 * "throw {exception}"
6143 * EVAL {exception}
6144 * THROW create exception
6145 * ... try block
6146 * " catch {expr}"
6147 * JUMP -> finally
6148 * catch1: PUSH exeception
6149 * EVAL {expr}
6150 * MATCH
6151 * JUMP nomatch -> catch2
6152 * CATCH remove exception
6153 * ... catch block
6154 * " catch"
6155 * JUMP -> finally
6156 * catch2: CATCH remove exception
6157 * ... catch block
6158 * " finally"
6159 * finally:
6160 * ... finally block
6161 * " endtry"
6162 * ENDTRY pop trystack entry, may rethrow
6163 */
6164 static char_u *
6165compile_try(char_u *arg, cctx_T *cctx)
6166{
6167 garray_T *instr = &cctx->ctx_instr;
6168 scope_T *try_scope;
6169 scope_T *scope;
6170
6171 // scope that holds the jumps that go to catch/finally/endtry
6172 try_scope = new_scope(cctx, TRY_SCOPE);
6173 if (try_scope == NULL)
6174 return NULL;
6175
6176 // "catch" is set when the first ":catch" is found.
6177 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006178 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179 if (generate_instr(cctx, ISN_TRY) == NULL)
6180 return NULL;
6181
6182 // scope for the try block itself
6183 scope = new_scope(cctx, BLOCK_SCOPE);
6184 if (scope == NULL)
6185 return NULL;
6186
6187 return arg;
6188}
6189
6190/*
6191 * compile "catch {expr}"
6192 */
6193 static char_u *
6194compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6195{
6196 scope_T *scope = cctx->ctx_scope;
6197 garray_T *instr = &cctx->ctx_instr;
6198 char_u *p;
6199 isn_T *isn;
6200
6201 // end block scope from :try or :catch
6202 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6203 compile_endblock(cctx);
6204 scope = cctx->ctx_scope;
6205
6206 // Error if not in a :try scope
6207 if (scope == NULL || scope->se_type != TRY_SCOPE)
6208 {
6209 emsg(_(e_catch));
6210 return NULL;
6211 }
6212
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006213 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 {
6215 emsg(_("E1033: catch unreachable after catch-all"));
6216 return NULL;
6217 }
6218
6219 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006220 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 JUMP_ALWAYS, cctx) == FAIL)
6222 return NULL;
6223
6224 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006225 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 if (isn->isn_arg.try.try_catch == 0)
6227 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006228 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229 {
6230 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006231 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 isn->isn_arg.jump.jump_where = instr->ga_len;
6233 }
6234
6235 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006236 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006238 scope->se_u.se_try.ts_caught_all = TRUE;
6239 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 }
6241 else
6242 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006243 char_u *end;
6244 char_u *pat;
6245 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006246 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006247 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 // Push v:exception, push {expr} and MATCH
6250 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6251
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006252 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006253 if (*end != *p)
6254 {
6255 semsg(_("E1067: Separator mismatch: %s"), p);
6256 vim_free(tofree);
6257 return FAIL;
6258 }
6259 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006260 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006261 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006262 len = (int)(end - tofree);
6263 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006264 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006265 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006266 if (pat == NULL)
6267 return FAIL;
6268 if (generate_PUSHS(cctx, pat) == FAIL)
6269 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6272 return NULL;
6273
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6276 return NULL;
6277 }
6278
6279 if (generate_instr(cctx, ISN_CATCH) == NULL)
6280 return NULL;
6281
6282 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6283 return NULL;
6284 return p;
6285}
6286
6287 static char_u *
6288compile_finally(char_u *arg, cctx_T *cctx)
6289{
6290 scope_T *scope = cctx->ctx_scope;
6291 garray_T *instr = &cctx->ctx_instr;
6292 isn_T *isn;
6293
6294 // end block scope from :try or :catch
6295 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6296 compile_endblock(cctx);
6297 scope = cctx->ctx_scope;
6298
6299 // Error if not in a :try scope
6300 if (scope == NULL || scope->se_type != TRY_SCOPE)
6301 {
6302 emsg(_(e_finally));
6303 return NULL;
6304 }
6305
6306 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 if (isn->isn_arg.try.try_finally != 0)
6309 {
6310 emsg(_(e_finally_dup));
6311 return NULL;
6312 }
6313
6314 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006315 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
Bram Moolenaar585fea72020-04-02 22:33:21 +02006317 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006318 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 {
6320 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 isn->isn_arg.jump.jump_where = instr->ga_len;
6323 }
6324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 // TODO: set index in ts_finally_label jumps
6326
6327 return arg;
6328}
6329
6330 static char_u *
6331compile_endtry(char_u *arg, cctx_T *cctx)
6332{
6333 scope_T *scope = cctx->ctx_scope;
6334 garray_T *instr = &cctx->ctx_instr;
6335 isn_T *isn;
6336
6337 // end block scope from :catch or :finally
6338 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6339 compile_endblock(cctx);
6340 scope = cctx->ctx_scope;
6341
6342 // Error if not in a :try scope
6343 if (scope == NULL || scope->se_type != TRY_SCOPE)
6344 {
6345 if (scope == NULL)
6346 emsg(_(e_no_endtry));
6347 else if (scope->se_type == WHILE_SCOPE)
6348 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006349 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 emsg(_(e_endfor));
6351 else
6352 emsg(_(e_endif));
6353 return NULL;
6354 }
6355
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006356 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6358 {
6359 emsg(_("E1032: missing :catch or :finally"));
6360 return NULL;
6361 }
6362
6363 // Fill in the "end" label in jumps at the end of the blocks, if not done
6364 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006365 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366
6367 // End :catch or :finally scope: set value in ISN_TRY instruction
6368 if (isn->isn_arg.try.try_finally == 0)
6369 isn->isn_arg.try.try_finally = instr->ga_len;
6370 compile_endblock(cctx);
6371
6372 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6373 return NULL;
6374 return arg;
6375}
6376
6377/*
6378 * compile "throw {expr}"
6379 */
6380 static char_u *
6381compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6382{
6383 char_u *p = skipwhite(arg);
6384
Bram Moolenaara5565e42020-05-09 15:44:01 +02006385 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 return NULL;
6387 if (may_generate_2STRING(-1, cctx) == FAIL)
6388 return NULL;
6389 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6390 return NULL;
6391
6392 return p;
6393}
6394
6395/*
6396 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006397 * compile "echomsg expr"
6398 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006399 * compile "execute expr"
6400 */
6401 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006402compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006403{
6404 char_u *p = arg;
6405 int count = 0;
6406
6407 for (;;)
6408 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006409 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006410 return NULL;
6411 ++count;
6412 p = skipwhite(p);
6413 if (ends_excmd(*p))
6414 break;
6415 }
6416
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006417 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6418 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6419 else if (cmdidx == CMD_execute)
6420 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6421 else if (cmdidx == CMD_echomsg)
6422 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6423 else
6424 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 return p;
6426}
6427
6428/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006429 * A command that is not compiled, execute with legacy code.
6430 */
6431 static char_u *
6432compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6433{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006434 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006435 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006436
6437 if (cctx->ctx_skip == TRUE)
6438 goto theend;
6439
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006440 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6441 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006442 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6443 {
6444 // expand filename in "syntax include [@group] filename"
6445 has_expr = TRUE;
6446 eap->arg = skipwhite(eap->arg + 7);
6447 if (*eap->arg == '@')
6448 eap->arg = skiptowhite(eap->arg);
6449 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006450
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006451 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006452 {
6453 int count = 0;
6454 char_u *start = skipwhite(line);
6455
6456 // :cmd xxx`=expr1`yyy`=expr2`zzz
6457 // PUSHS ":cmd xxx"
6458 // eval expr1
6459 // PUSHS "yyy"
6460 // eval expr2
6461 // PUSHS "zzz"
6462 // EXECCONCAT 5
6463 for (;;)
6464 {
6465 if (p > start)
6466 {
6467 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6468 ++count;
6469 }
6470 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006471 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006472 return NULL;
6473 may_generate_2STRING(-1, cctx);
6474 ++count;
6475 p = skipwhite(p);
6476 if (*p != '`')
6477 {
6478 emsg(_("E1083: missing backtick"));
6479 return NULL;
6480 }
6481 start = p + 1;
6482
6483 p = (char_u *)strstr((char *)start, "`=");
6484 if (p == NULL)
6485 {
6486 if (*skipwhite(start) != NUL)
6487 {
6488 generate_PUSHS(cctx, vim_strsave(start));
6489 ++count;
6490 }
6491 break;
6492 }
6493 }
6494 generate_EXECCONCAT(cctx, count);
6495 }
6496 else
6497 generate_EXEC(cctx, line);
6498
6499theend:
6500 return (char_u *)"";
6501}
6502
6503/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 * After ex_function() has collected all the function lines: parse and compile
6505 * the lines into instructions.
6506 * Adds the function to "def_functions".
6507 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6508 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006509 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006510 * This can be used recursively through compile_lambda(), which may reallocate
6511 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 */
6513 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006514compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 char_u *line = NULL;
6517 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 char *errormsg = NULL; // error message
6519 int had_return = FALSE;
6520 cctx_T cctx;
6521 garray_T *instr;
6522 int called_emsg_before = called_emsg;
6523 int ret = FAIL;
6524 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006525 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006528 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006529
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006530 if (ufunc->uf_dfunc_idx >= 0)
6531 {
6532 // Redefining a function that was compiled before.
6533 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6534
6535 // Free old instructions.
6536 delete_def_function_contents(dfunc);
6537 }
6538 else
6539 {
6540 // Add the function to "def_functions".
6541 if (ga_grow(&def_functions, 1) == FAIL)
6542 return;
6543 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006544 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006545 dfunc->df_idx = def_functions.ga_len;
6546 ufunc->uf_dfunc_idx = dfunc->df_idx;
6547 dfunc->df_ufunc = ufunc;
6548 ++def_functions.ga_len;
6549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 }
6551
Bram Moolenaara80faa82020-04-12 19:37:17 +02006552 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 cctx.ctx_ufunc = ufunc;
6554 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006555 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6557 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6558 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6559 cctx.ctx_type_list = &ufunc->uf_type_list;
6560 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6561 instr = &cctx.ctx_instr;
6562
6563 // Most modern script version.
6564 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6565
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006566 if (ufunc->uf_def_args.ga_len > 0)
6567 {
6568 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006569 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006570 int i;
6571 char_u *arg;
6572 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6573
6574 // Produce instructions for the default values of optional arguments.
6575 // Store the instruction index in uf_def_arg_idx[] so that we know
6576 // where to start when the function is called, depending on the number
6577 // of arguments.
6578 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6579 if (ufunc->uf_def_arg_idx == NULL)
6580 goto erret;
6581 for (i = 0; i < count; ++i)
6582 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006583 garray_T *stack = &cctx.ctx_type_stack;
6584 type_T *val_type;
6585 int arg_idx = first_def_arg + i;
6586
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006587 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6588 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006589 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006590 goto erret;
6591
6592 // If no type specified use the type of the default value.
6593 // Otherwise check that the default value type matches the
6594 // specified type.
6595 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6596 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6597 ufunc->uf_arg_types[arg_idx] = val_type;
6598 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6599 == FAIL)
6600 {
6601 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6602 arg_idx + 1);
6603 goto erret;
6604 }
6605
6606 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006607 goto erret;
6608 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006609 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6610 }
6611
6612 /*
6613 * Loop over all the lines of the function and generate instructions.
6614 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 for (;;)
6616 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006617 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006618 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006619
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006620 // Bail out on the first error to avoid a flood of errors and report
6621 // the right line number when inside try/catch.
6622 if (emsg_before != called_emsg)
6623 goto erret;
6624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 if (line != NULL && *line == '|')
6626 // the line continues after a '|'
6627 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006628 else if (line != NULL && *line != NUL
6629 && !(*line == '#' && (line == cctx.ctx_line_start
6630 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006632 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 goto erret;
6634 }
6635 else
6636 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006637 line = next_line_from_context(&cctx);
6638 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006639 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006642 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643
6644 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006645 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 ea.cmdlinep = &line;
6647 ea.cmd = skipwhite(line);
6648
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006649 // Some things can be recognized by the first character.
6650 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006652 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006653 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006654 if (ea.cmd[1] != '{')
6655 {
6656 line = (char_u *)"";
6657 continue;
6658 }
6659 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006661 case '}':
6662 {
6663 // "}" ends a block scope
6664 scopetype_T stype = cctx.ctx_scope == NULL
6665 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006667 if (stype == BLOCK_SCOPE)
6668 {
6669 compile_endblock(&cctx);
6670 line = ea.cmd;
6671 }
6672 else
6673 {
6674 emsg(_("E1025: using } outside of a block scope"));
6675 goto erret;
6676 }
6677 if (line != NULL)
6678 line = skipwhite(ea.cmd + 1);
6679 continue;
6680 }
6681
6682 case '{':
6683 // "{" starts a block scope
6684 // "{'a': 1}->func() is something else
6685 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6686 {
6687 line = compile_block(ea.cmd, &cctx);
6688 continue;
6689 }
6690 break;
6691
6692 case ':':
6693 is_ex_command = TRUE;
6694 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 }
6696
6697 /*
6698 * COMMAND MODIFIERS
6699 */
6700 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6701 {
6702 if (errormsg != NULL)
6703 goto erret;
6704 // empty line or comment
6705 line = (char_u *)"";
6706 continue;
6707 }
6708
6709 // Skip ":call" to get to the function name.
6710 if (checkforcmd(&ea.cmd, "call", 3))
6711 ea.cmd = skipwhite(ea.cmd);
6712
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006713 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006715 // Assuming the command starts with a variable or function name,
6716 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6717 // val".
6718 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6719 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006720 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006721 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006723 int oplen;
6724 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006726 oplen = assignment_len(skipwhite(p), &heredoc);
6727 if (oplen > 0)
6728 {
6729 // Recognize an assignment if we recognize the variable
6730 // name:
6731 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006732 // "local = expr" where "local" is a local var.
6733 // "script = expr" where "script" is a script-local var.
6734 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006735 // "&opt = expr"
6736 // "$ENV = expr"
6737 // "@r = expr"
6738 if (*ea.cmd == '&'
6739 || *ea.cmd == '$'
6740 || *ea.cmd == '@'
6741 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006742 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006743 || lookup_script(ea.cmd, p - ea.cmd) == OK
6744 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6745 {
6746 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6747 if (line == NULL)
6748 goto erret;
6749 continue;
6750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 }
6752 }
6753 }
6754
6755 /*
6756 * COMMAND after range
6757 */
6758 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006759 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6760 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006761 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762
6763 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6764 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006765 if (cctx.ctx_skip == TRUE)
6766 {
6767 line += STRLEN(line);
6768 continue;
6769 }
6770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 // Expression or function call.
6772 if (ea.cmdidx == CMD_eval)
6773 {
6774 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006775 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 goto erret;
6777
6778 // drop the return value
6779 generate_instr_drop(&cctx, ISN_DROP, 1);
6780 line = p;
6781 continue;
6782 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006783 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 iemsg("Command from find_ex_command() not handled");
6785 goto erret;
6786 }
6787
6788 p = skipwhite(p);
6789
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006790 if (cctx.ctx_skip == TRUE
6791 && ea.cmdidx != CMD_elseif
6792 && ea.cmdidx != CMD_else
6793 && ea.cmdidx != CMD_endif)
6794 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006795 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006796 continue;
6797 }
6798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 switch (ea.cmdidx)
6800 {
6801 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006802 ea.arg = p;
6803 line = compile_nested_function(&ea, &cctx);
6804 break;
6805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006807 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 goto erret;
6809
6810 case CMD_return:
6811 line = compile_return(p, set_return_type, &cctx);
6812 had_return = TRUE;
6813 break;
6814
6815 case CMD_let:
6816 case CMD_const:
6817 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6818 break;
6819
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006820 case CMD_unlet:
6821 case CMD_unlockvar:
6822 case CMD_lockvar:
6823 line = compile_unletlock(p, &ea, &cctx);
6824 break;
6825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 case CMD_import:
6827 line = compile_import(p, &cctx);
6828 break;
6829
6830 case CMD_if:
6831 line = compile_if(p, &cctx);
6832 break;
6833 case CMD_elseif:
6834 line = compile_elseif(p, &cctx);
6835 break;
6836 case CMD_else:
6837 line = compile_else(p, &cctx);
6838 break;
6839 case CMD_endif:
6840 line = compile_endif(p, &cctx);
6841 break;
6842
6843 case CMD_while:
6844 line = compile_while(p, &cctx);
6845 break;
6846 case CMD_endwhile:
6847 line = compile_endwhile(p, &cctx);
6848 break;
6849
6850 case CMD_for:
6851 line = compile_for(p, &cctx);
6852 break;
6853 case CMD_endfor:
6854 line = compile_endfor(p, &cctx);
6855 break;
6856 case CMD_continue:
6857 line = compile_continue(p, &cctx);
6858 break;
6859 case CMD_break:
6860 line = compile_break(p, &cctx);
6861 break;
6862
6863 case CMD_try:
6864 line = compile_try(p, &cctx);
6865 break;
6866 case CMD_catch:
6867 line = compile_catch(p, &cctx);
6868 break;
6869 case CMD_finally:
6870 line = compile_finally(p, &cctx);
6871 break;
6872 case CMD_endtry:
6873 line = compile_endtry(p, &cctx);
6874 break;
6875 case CMD_throw:
6876 line = compile_throw(p, &cctx);
6877 break;
6878
6879 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006881 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006882 case CMD_echomsg:
6883 case CMD_echoerr:
6884 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886
6887 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006888 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006889 // Not recognized, execute with do_cmdline_cmd().
6890 ea.arg = p;
6891 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 break;
6893 }
6894 if (line == NULL)
6895 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006896 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897
6898 if (cctx.ctx_type_stack.ga_len < 0)
6899 {
6900 iemsg("Type stack underflow");
6901 goto erret;
6902 }
6903 }
6904
6905 if (cctx.ctx_scope != NULL)
6906 {
6907 if (cctx.ctx_scope->se_type == IF_SCOPE)
6908 emsg(_(e_endif));
6909 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6910 emsg(_(e_endwhile));
6911 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6912 emsg(_(e_endfor));
6913 else
6914 emsg(_("E1026: Missing }"));
6915 goto erret;
6916 }
6917
6918 if (!had_return)
6919 {
6920 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6921 {
6922 emsg(_("E1027: Missing return statement"));
6923 goto erret;
6924 }
6925
6926 // Return zero if there is no return at the end.
6927 generate_PUSHNR(&cctx, 0);
6928 generate_instr(&cctx, ISN_RETURN);
6929 }
6930
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006931 {
6932 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6933 + ufunc->uf_dfunc_idx;
6934 dfunc->df_deleted = FALSE;
6935 dfunc->df_instr = instr->ga_data;
6936 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006937 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006938 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006939 if (cctx.ctx_outer_used)
6940 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006943 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006944 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006945 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006946
6947 // Create a type for the function, with the return type and any
6948 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006949 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6950 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006951 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006952 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006953 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6954 argcount, &ufunc->uf_type_list);
6955 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006956 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006957 argcount + varargs,
6958 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006959 {
6960 ret = FAIL;
6961 goto erret;
6962 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006963 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6964 ufunc->uf_func_type->tt_min_argcount =
6965 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006966 if (ufunc->uf_arg_types == NULL)
6967 {
6968 int i;
6969
6970 // lambda does not have argument types.
6971 for (i = 0; i < argcount; ++i)
6972 ufunc->uf_func_type->tt_args[i] = &t_any;
6973 }
6974 else
6975 mch_memmove(ufunc->uf_func_type->tt_args,
6976 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006977 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006978 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006979 ufunc->uf_func_type->tt_args[argcount] =
6980 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006981 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6982 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006983 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006984 else
6985 // No arguments, can use a predefined type.
6986 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6987 argcount, &ufunc->uf_type_list);
6988
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006989 }
6990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 ret = OK;
6992
6993erret:
6994 if (ret == FAIL)
6995 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006996 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006997 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6998 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006999
7000 for (idx = 0; idx < instr->ga_len; ++idx)
7001 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007005 if (!dfunc->df_deleted)
7006 --def_functions.ga_len;
7007
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007008 while (cctx.ctx_scope != NULL)
7009 drop_scope(&cctx);
7010
Bram Moolenaar20431c92020-03-20 18:39:46 +01007011 // Don't execute this function body.
7012 ga_clear_strings(&ufunc->uf_lines);
7013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 if (errormsg != NULL)
7015 emsg(errormsg);
7016 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007017 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 }
7019
7020 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007021 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007022 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024}
7025
7026/*
7027 * Delete an instruction, free what it contains.
7028 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007029 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030delete_instr(isn_T *isn)
7031{
7032 switch (isn->isn_type)
7033 {
7034 case ISN_EXEC:
7035 case ISN_LOADENV:
7036 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007037 case ISN_LOADB:
7038 case ISN_LOADW:
7039 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 case ISN_LOADOPT:
7041 case ISN_MEMBER:
7042 case ISN_PUSHEXC:
7043 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007044 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007046 case ISN_STOREB:
7047 case ISN_STOREW:
7048 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007049 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 vim_free(isn->isn_arg.string);
7051 break;
7052
7053 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007054 case ISN_STORES:
7055 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 break;
7057
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007058 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007059 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007060 vim_free(isn->isn_arg.unlet.ul_name);
7061 break;
7062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 case ISN_STOREOPT:
7064 vim_free(isn->isn_arg.storeopt.so_name);
7065 break;
7066
7067 case ISN_PUSHBLOB: // push blob isn_arg.blob
7068 blob_unref(isn->isn_arg.blob);
7069 break;
7070
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007071 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007073 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007074#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007075 break;
7076
7077 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007078#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007079 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007080#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007081 break;
7082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 case ISN_UCALL:
7084 vim_free(isn->isn_arg.ufunc.cuf_name);
7085 break;
7086
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007087 case ISN_FUNCREF:
7088 {
7089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7090 + isn->isn_arg.funcref.fr_func;
7091 func_ptr_unref(dfunc->df_ufunc);
7092 }
7093 break;
7094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 case ISN_2BOOL:
7096 case ISN_2STRING:
7097 case ISN_ADDBLOB:
7098 case ISN_ADDLIST:
7099 case ISN_BCALL:
7100 case ISN_CATCH:
7101 case ISN_CHECKNR:
7102 case ISN_CHECKTYPE:
7103 case ISN_COMPAREANY:
7104 case ISN_COMPAREBLOB:
7105 case ISN_COMPAREBOOL:
7106 case ISN_COMPAREDICT:
7107 case ISN_COMPAREFLOAT:
7108 case ISN_COMPAREFUNC:
7109 case ISN_COMPARELIST:
7110 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 case ISN_COMPARESPECIAL:
7112 case ISN_COMPARESTRING:
7113 case ISN_CONCAT:
7114 case ISN_DCALL:
7115 case ISN_DROP:
7116 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007117 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007118 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007119 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007120 case ISN_EXECCONCAT:
7121 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 case ISN_INDEX:
7124 case ISN_JUMP:
7125 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007126 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 case ISN_LOADSCRIPT:
7128 case ISN_LOADREG:
7129 case ISN_LOADV:
7130 case ISN_NEGATENR:
7131 case ISN_NEWDICT:
7132 case ISN_NEWLIST:
7133 case ISN_OPNR:
7134 case ISN_OPFLOAT:
7135 case ISN_OPANY:
7136 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007137 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 case ISN_PUSHF:
7139 case ISN_PUSHNR:
7140 case ISN_PUSHBOOL:
7141 case ISN_PUSHSPEC:
7142 case ISN_RETURN:
7143 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007144 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007145 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007147 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 case ISN_STORESCRIPT:
7149 case ISN_THROW:
7150 case ISN_TRY:
7151 // nothing allocated
7152 break;
7153 }
7154}
7155
7156/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007157 * Free all instructions for "dfunc".
7158 */
7159 static void
7160delete_def_function_contents(dfunc_T *dfunc)
7161{
7162 int idx;
7163
7164 ga_clear(&dfunc->df_def_args_isn);
7165
7166 if (dfunc->df_instr != NULL)
7167 {
7168 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7169 delete_instr(dfunc->df_instr + idx);
7170 VIM_CLEAR(dfunc->df_instr);
7171 }
7172
7173 dfunc->df_deleted = TRUE;
7174}
7175
7176/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 * When a user function is deleted, delete any associated def function.
7178 */
7179 void
7180delete_def_function(ufunc_T *ufunc)
7181{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 if (ufunc->uf_dfunc_idx >= 0)
7183 {
7184 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7185 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186
Bram Moolenaar20431c92020-03-20 18:39:46 +01007187 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 }
7189}
7190
7191#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007192/*
7193 * Free all functions defined with ":def".
7194 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 void
7196free_def_functions(void)
7197{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007198 int idx;
7199
7200 for (idx = 0; idx < def_functions.ga_len; ++idx)
7201 {
7202 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7203
7204 delete_def_function_contents(dfunc);
7205 }
7206
7207 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208}
7209#endif
7210
7211
7212#endif // FEAT_EVAL