blob: a52f2c7e2889cdbebaadabee011323ddcc5c497f [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* 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 * vim9cmds.c: Dealing with compiled function expressions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar078a4612022-01-04 15:17:03 +000096 if ((typep->type_curr == &t_any || typep->type_curr == &t_unknown)
97 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000098 vartype = VAR_DICT;
99 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
100 {
101 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
102 return FAIL;
103 if (is_slice)
104 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000105 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000106 if (need_type(idxtype, &t_number, -2, 0, cctx,
107 FALSE, FALSE) == FAIL)
108 return FAIL;
109 }
110 }
111
112 if (vartype == VAR_DICT)
113 {
114 if (is_slice)
115 {
116 emsg(_(e_cannot_slice_dictionary));
117 return FAIL;
118 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000119 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000120 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 typep->type_curr = typep->type_curr->tt_member;
122 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000123 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000124 typep->type_curr = &t_any;
125 if (typep->type_decl->tt_type == VAR_DICT)
126 {
127 typep->type_decl = typep->type_decl->tt_member;
128 if (typep->type_decl == &t_unknown)
129 // empty dict was used
130 typep->type_decl = &t_any;
131 }
132 else
133 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000134 }
135 else
136 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000137 if (need_type(typep->type_curr, &t_dict_any, -2, 0, cctx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000138 FALSE, FALSE) == FAIL)
139 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000140 typep->type_curr = &t_any;
141 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000142 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100143 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
144 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000145 return FAIL;
146 if (keeping_dict != NULL)
147 *keeping_dict = TRUE;
148 }
149 else if (vartype == VAR_STRING)
150 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000151 typep->type_curr = &t_string;
152 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000153 if ((is_slice
154 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
155 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
156 return FAIL;
157 }
158 else if (vartype == VAR_BLOB)
159 {
160 if (is_slice)
161 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000162 typep->type_curr = &t_blob;
163 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000164 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
165 return FAIL;
166 }
167 else
168 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000169 typep->type_curr = &t_number;
170 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
172 return FAIL;
173 }
174 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000175 else if (vartype == VAR_LIST || typep->type_curr == &t_any
176 || typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000177 {
178 if (is_slice)
179 {
180 if (generate_instr_drop(cctx,
181 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
182 2) == FAIL)
183 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000184 // a copy is made so the member type is no longer declared
185 if (typep->type_decl->tt_type == VAR_LIST)
186 typep->type_decl = &t_list_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000187 }
188 else
189 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000190 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000192 typep->type_curr = typep->type_curr->tt_member;
193 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000194 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000195 typep->type_curr = &t_any;
196 if (typep->type_decl->tt_type == VAR_LIST)
197 {
198 typep->type_decl = typep->type_decl->tt_member;
199 if (typep->type_decl == &t_unknown)
200 // empty list was used
201 typep->type_decl = &t_any;
202 }
203 else
204 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 }
206 if (generate_instr_drop(cctx,
207 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
208 == FAIL)
209 return FAIL;
210 }
211 }
212 else
213 {
214 switch (vartype)
215 {
216 case VAR_FUNC:
217 case VAR_PARTIAL:
218 emsg(_(e_cannot_index_a_funcref));
219 break;
220 case VAR_BOOL:
221 case VAR_SPECIAL:
222 case VAR_JOB:
223 case VAR_CHANNEL:
224 case VAR_INSTR:
225 case VAR_UNKNOWN:
226 case VAR_ANY:
227 case VAR_VOID:
228 emsg(_(e_cannot_index_special_variable));
229 break;
230 default:
231 emsg(_(e_string_list_dict_or_blob_required));
232 }
233 return FAIL;
234 }
235 return OK;
236}
237
238/*
239 * Generate an instruction to load script-local variable "name", without the
240 * leading "s:".
241 * Also finds imported variables.
242 */
243 int
244compile_load_scriptvar(
245 cctx_T *cctx,
246 char_u *name, // variable NUL terminated
247 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100248 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249{
250 scriptitem_T *si;
251 int idx;
252 imported_T *import;
253
254 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
255 return FAIL;
256 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000257 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000258 if (idx >= 0)
259 {
260 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
261
262 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
263 current_sctx.sc_sid, idx, sv->sv_type);
264 return OK;
265 }
266
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000267 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 if (import != NULL)
269 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000270 char_u *p = skipwhite(*end);
271 char_u *exp_name;
272 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100273 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000274 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000275 int done = FALSE;
276 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000277
278 // Need to lookup the member.
279 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000280 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000281 semsg(_(e_expected_dot_after_name_str), start);
282 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000284 ++p;
285 if (VIM_ISWHITE(*p))
286 {
287 emsg(_(e_no_white_space_allowed_after_dot));
288 return FAIL;
289 }
290
291 // isolate one name
292 exp_name = p;
293 while (eval_isnamec(*p))
294 ++p;
295 cc = *p;
296 *p = NUL;
297
Bram Moolenaard041f422022-01-12 19:54:00 +0000298 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100299 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
300 // "import autoload './dir/script.vim'" or
301 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100302 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100303
304 if (res == OK)
305 {
306 if (si->sn_autoload_prefix != NULL
307 && si->sn_state == SN_STATE_NOT_LOADED)
308 {
309 char_u *auto_name =
310 concat_str(si->sn_autoload_prefix, exp_name);
311
312 // autoload script must be loaded later, access by the autoload
313 // name. If a '(' follows it must be a function. Otherwise we
314 // don't know, it can be "script.Func".
315 if (cc == '(' || paren_follows_after_expr)
316 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
317 else
318 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
319 vim_free(auto_name);
320 done = TRUE;
321 }
322 else if (si->sn_import_autoload
323 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100324 {
325 // If a '(' follows it must be a function. Otherwise we don't
326 // know, it can be "script.Func".
327 if (cc == '(' || paren_follows_after_expr)
328 {
329 char_u sid_name[MAX_FUNC_NAME_LEN];
330
331 func_name_with_sid(exp_name, import->imp_sid, sid_name);
332 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any);
333 }
334 else
335 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
336 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100337 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100338 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100339 else
340 {
341 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
342 cctx, NULL, TRUE);
343 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100344 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100345
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000346 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000347 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000348 if (done)
349 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000350
351 if (idx < 0)
352 {
353 if (ufunc != NULL)
354 {
355 // function call or function reference
356 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL);
357 return OK;
358 }
359 return FAIL;
360 }
361
362 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
363 import->imp_sid,
364 idx,
365 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 return OK;
367 }
368
Bram Moolenaard0132f42022-05-12 11:05:40 +0100369 // Can only get here if we know "name" is a script variable and not in a
370 // Vim9 script (variable is not in sn_var_vals): old style script.
371 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000372 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373}
374
375 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000376generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000377{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000378 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000379 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000381 // Reject a global non-autoload function found without the "g:" prefix.
382 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 return FAIL;
384
385 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000386 compile_type = get_compile_type(ufunc);
387 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000388 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000389 return FAIL;
390 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
391}
392
393/*
394 * Compile a variable name into a load instruction.
395 * "end" points to just after the name.
396 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
397 * When "error" is FALSE do not give an error when not found.
398 */
399 int
400compile_load(
401 char_u **arg,
402 char_u *end_arg,
403 cctx_T *cctx,
404 int is_expr,
405 int error)
406{
407 type_T *type;
408 char_u *name = NULL;
409 char_u *end = end_arg;
410 int res = FAIL;
411 int prev_called_emsg = called_emsg;
412
413 if (*(*arg + 1) == ':')
414 {
415 if (end <= *arg + 2)
416 {
417 isntype_T isn_type;
418
419 // load dictionary of namespace
420 switch (**arg)
421 {
422 case 'g': isn_type = ISN_LOADGDICT; break;
423 case 'w': isn_type = ISN_LOADWDICT; break;
424 case 't': isn_type = ISN_LOADTDICT; break;
425 case 'b': isn_type = ISN_LOADBDICT; break;
426 default:
427 semsg(_(e_namespace_not_supported_str), *arg);
428 goto theend;
429 }
430 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
431 goto theend;
432 res = OK;
433 }
434 else
435 {
436 isntype_T isn_type = ISN_DROP;
437
438 // load namespaced variable
439 name = vim_strnsave(*arg + 2, end - (*arg + 2));
440 if (name == NULL)
441 return FAIL;
442
443 switch (**arg)
444 {
445 case 'v': res = generate_LOADV(cctx, name, error);
446 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000447 case 's': if (current_script_is_vim9())
448 {
449 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
450 *arg);
451 vim_free(name);
452 return FAIL;
453 }
454 if (is_expr && ASCII_ISUPPER(*name)
455 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000456 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000457 else
458 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100459 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000460 break;
461 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
462 {
463 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000464 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000465 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 else
467 isn_type = ISN_LOADG;
468 }
469 else
470 {
471 isn_type = ISN_LOADAUTO;
472 vim_free(name);
473 name = vim_strnsave(*arg, end - *arg);
474 if (name == NULL)
475 return FAIL;
476 }
477 break;
478 case 'w': isn_type = ISN_LOADW; break;
479 case 't': isn_type = ISN_LOADT; break;
480 case 'b': isn_type = ISN_LOADB; break;
481 default: // cannot happen, just in case
482 semsg(_(e_namespace_not_supported_str), *arg);
483 goto theend;
484 }
485 if (isn_type != ISN_DROP)
486 {
487 // Global, Buffer-local, Window-local and Tabpage-local
488 // variables can be defined later, thus we don't check if it
489 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000490 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 }
492 }
493 }
494 else
495 {
496 size_t len = end - *arg;
497 int idx;
498 int gen_load = FALSE;
499 int gen_load_outer = 0;
500
501 name = vim_strnsave(*arg, end - *arg);
502 if (name == NULL)
503 return FAIL;
504
505 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
506 {
507 script_autoload(name, FALSE);
508 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
509 }
510 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
511 == OK)
512 {
513 if (gen_load_outer == 0)
514 gen_load = TRUE;
515 }
516 else
517 {
518 lvar_T lvar;
519
520 if (lookup_local(*arg, len, &lvar, cctx) == OK)
521 {
522 type = lvar.lv_type;
523 idx = lvar.lv_idx;
524 if (lvar.lv_from_outer != 0)
525 gen_load_outer = lvar.lv_from_outer;
526 else
527 gen_load = TRUE;
528 }
529 else
530 {
531 // "var" can be script-local even without using "s:" if it
532 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000533 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000534 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100535 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
537 // When evaluating an expression and the name starts with an
538 // uppercase letter it can be a user defined function.
539 // generate_funcref() will fail if the function can't be found.
540 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000541 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000542 }
543 }
544 if (gen_load)
545 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
546 if (gen_load_outer > 0)
547 {
548 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
549 cctx->ctx_outer_used = TRUE;
550 }
551 }
552
553 *arg = end;
554
555theend:
556 if (res == FAIL && error && called_emsg == prev_called_emsg)
557 semsg(_(e_variable_not_found_str), name);
558 vim_free(name);
559 return res;
560}
561
562/*
563 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100564 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565 * Returns FAIL if compilation fails.
566 */
567 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100568compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569{
LemonBoyf3b48952022-05-05 13:53:03 +0100570 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000571 garray_T save_ga = cctx->ctx_instr;
572 int expr_res;
573 int trailing_error;
574 int instr_count;
575 isn_T *instr = NULL;
576
577 // Remove the string type from the stack.
578 --cctx->ctx_type_stack.ga_len;
579
580 // Temporarily reset the list of instructions so that the jump labels are
581 // correct.
582 cctx->ctx_instr.ga_len = 0;
583 cctx->ctx_instr.ga_maxlen = 0;
584 cctx->ctx_instr.ga_data = NULL;
585 expr_res = compile_expr0(&s, cctx);
586 s = skipwhite(s);
587 trailing_error = *s != NUL;
588
589 if (expr_res == FAIL || trailing_error
590 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
591 {
592 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000593 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594 clear_instr_ga(&cctx->ctx_instr);
595 cctx->ctx_instr = save_ga;
596 ++cctx->ctx_type_stack.ga_len;
597 return FAIL;
598 }
599
600 // Move the generated instructions into the ISN_INSTR instruction, then
601 // restore the list of instructions.
602 instr_count = cctx->ctx_instr.ga_len;
603 instr = cctx->ctx_instr.ga_data;
604 instr[instr_count].isn_type = ISN_FINISH;
605
606 cctx->ctx_instr = save_ga;
607 vim_free(isn->isn_arg.string);
608 isn->isn_type = ISN_INSTR;
609 isn->isn_arg.instr = instr;
610 return OK;
611}
612
613/*
LemonBoyf3b48952022-05-05 13:53:03 +0100614 * List of special functions for "compile_arguments".
615 */
616typedef enum {
617 CA_NOT_SPECIAL,
618 CA_SEARCHPAIR, // {skip} in searchpair() and searchpairpos()
619 CA_SUBSTITUTE, // {sub} in substitute(), when prefixed with \=
620} ca_special_T;
621
622/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000623 * Compile the argument expressions.
624 * "arg" points to just after the "(" and is advanced to after the ")"
625 */
626 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100627compile_arguments(
628 char_u **arg,
629 cctx_T *cctx,
630 int *argcount,
631 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000632{
633 char_u *p = *arg;
634 char_u *whitep = *arg;
635 int must_end = FALSE;
636 int instr_count;
637
638 for (;;)
639 {
640 if (may_get_next_line(whitep, &p, cctx) == FAIL)
641 goto failret;
642 if (*p == ')')
643 {
644 *arg = p + 1;
645 return OK;
646 }
647 if (must_end)
648 {
649 semsg(_(e_missing_comma_before_argument_str), p);
650 return FAIL;
651 }
652
653 instr_count = cctx->ctx_instr.ga_len;
654 if (compile_expr0(&p, cctx) == FAIL)
655 return FAIL;
656 ++*argcount;
657
LemonBoyf3b48952022-05-05 13:53:03 +0100658 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 && cctx->ctx_instr.ga_len == instr_count + 1)
660 {
661 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
662
663 // {skip} argument of searchpair() can be compiled if not empty
664 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100665 compile_string(isn, cctx, 0);
666 }
667 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
668 && cctx->ctx_instr.ga_len == instr_count + 1)
669 {
670 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
671
672 // {sub} argument of substitute() can be compiled if it starts
673 // with \=
674 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
675 && isn->isn_arg.string[1] == '=')
676 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677 }
678
679 if (*p != ',' && *skipwhite(p) == ',')
680 {
681 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
682 p = skipwhite(p);
683 }
684 if (*p == ',')
685 {
686 ++p;
687 if (*p != NUL && !VIM_ISWHITE(*p))
688 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
689 }
690 else
691 must_end = TRUE;
692 whitep = p;
693 p = skipwhite(p);
694 }
695failret:
696 emsg(_(e_missing_closing_paren));
697 return FAIL;
698}
699
700/*
701 * Compile a function call: name(arg1, arg2)
702 * "arg" points to "name", "arg + varlen" to the "(".
703 * "argcount_init" is 1 for "value->method()"
704 * Instructions:
705 * EVAL arg1
706 * EVAL arg2
707 * BCALL / DCALL / UCALL
708 */
709 static int
710compile_call(
711 char_u **arg,
712 size_t varlen,
713 cctx_T *cctx,
714 ppconst_T *ppconst,
715 int argcount_init)
716{
717 char_u *name = *arg;
718 char_u *p;
719 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100720 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 char_u fname_buf[FLEN_FIXED + 1];
722 char_u *tofree = NULL;
723 int error = FCERR_NONE;
724 ufunc_T *ufunc = NULL;
725 int res = FAIL;
726 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000727 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100728 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000729 imported_T *import;
730
731 if (varlen >= sizeof(namebuf))
732 {
733 semsg(_(e_name_too_long_str), name);
734 return FAIL;
735 }
736 vim_strncpy(namebuf, *arg, varlen);
737
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000738 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000739 if (import != NULL)
740 {
741 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
742 return FAIL;
743 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744
745 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100746 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100748 if ((varlen == 3
749 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
751 {
752 char_u *s = skipwhite(*arg + varlen + 1);
753 typval_T argvars[2];
754 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100755 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756
757 argvars[0].v_type = VAR_UNKNOWN;
758 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100759 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000760 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100761 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 s = skipwhite(s);
763 if (*s == ')' && argvars[0].v_type == VAR_STRING
764 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
765 || !is_has))
766 {
767 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
768
769 *arg = s + 1;
770 argvars[1].v_type = VAR_UNKNOWN;
771 tv->v_type = VAR_NUMBER;
772 tv->vval.v_number = 0;
773 if (is_has)
774 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100775 else if (is_len)
776 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000777 else
778 f_exists(argvars, tv);
779 clear_tv(&argvars[0]);
780 ++ppconst->pp_used;
781 return OK;
782 }
783 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100784 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785 {
786 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
787 return FAIL;
788 }
789 }
790
791 if (generate_ppconst(cctx, ppconst) == FAIL)
792 return FAIL;
793
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
795
796 // We handle the "skip" argument of searchpair() and searchpairpos()
797 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100798 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
799 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
800 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
801 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
802 special_fn = CA_SEARCHPAIR;
803 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
804 special_fn = CA_SUBSTITUTE;
805 else
806 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807
808 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100809 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000810 goto theend;
811
812 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
813 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
814 {
815 int idx;
816
817 // builtin function
818 idx = find_internal_func(name);
819 if (idx >= 0)
820 {
821 if (STRCMP(name, "flatten") == 0)
822 {
823 emsg(_(e_cannot_use_flatten_in_vim9_script));
824 goto theend;
825 }
826
827 if (STRCMP(name, "add") == 0 && argcount == 2)
828 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000829 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830
831 // add() can be compiled to instructions if we know the type
832 if (type->tt_type == VAR_LIST)
833 {
834 // inline "add(list, item)" so that the type can be checked
835 res = generate_LISTAPPEND(cctx);
836 idx = -1;
837 }
838 else if (type->tt_type == VAR_BLOB)
839 {
840 // inline "add(blob, nr)" so that the type can be checked
841 res = generate_BLOBAPPEND(cctx);
842 idx = -1;
843 }
844 }
845
846 if (idx >= 0)
847 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
848 }
849 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100850 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 goto theend;
852 }
853
Bram Moolenaar62aec932022-01-29 21:45:34 +0000854 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
855
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000856 // An argument or local variable can be a function reference, this
857 // overrules a function name.
858 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
859 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
860 {
861 // If we can find the function by name generate the right call.
862 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000863 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000864 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000866 if (!func_is_global(ufunc))
867 {
868 res = generate_CALL(cctx, ufunc, argcount);
869 goto theend;
870 }
871 if (!has_g_namespace
872 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
873 {
874 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100875 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000876 goto theend;
877 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878 }
879 }
880
881 // If the name is a variable, load it and use PCALL.
882 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000883 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000885 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
887 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000888 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889
890 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
891 goto theend;
892 }
893
894 // If we can find a global function by name generate the right call.
895 if (ufunc != NULL)
896 {
897 res = generate_CALL(cctx, ufunc, argcount);
898 goto theend;
899 }
900
901 // A global function may be defined only later. Need to figure out at
902 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000903 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904 res = generate_UCALL(cctx, name, argcount);
905 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100906 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000907
908theend:
909 vim_free(tofree);
910 return res;
911}
912
913// like NAMESPACE_CHAR but with 'a' and 'l'.
914#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
915
916/*
917 * Find the end of a variable or function name. Unlike find_name_end() this
918 * does not recognize magic braces.
919 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
920 * Return a pointer to just after the name. Equal to "arg" if there is no
921 * valid name.
922 */
923 char_u *
924to_name_end(char_u *arg, int use_namespace)
925{
926 char_u *p;
927
928 // Quick check for valid starting character.
929 if (!eval_isnamec1(*arg))
930 return arg;
931
932 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
933 // Include a namespace such as "s:var" and "v:var". But "n:" is not
934 // and can be used in slice "[n:]".
935 if (*p == ':' && (p != arg + 1
936 || !use_namespace
937 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
938 break;
939 return p;
940}
941
942/*
943 * Like to_name_end() but also skip over a list or dict constant.
944 * Also accept "<SNR>123_Func".
945 * This intentionally does not handle line continuation.
946 */
947 char_u *
948to_name_const_end(char_u *arg)
949{
950 char_u *p = arg;
951 typval_T rettv;
952
953 if (STRNCMP(p, "<SNR>", 5) == 0)
954 p = skipdigits(p + 5);
955 p = to_name_end(p, TRUE);
956 if (p == arg && *arg == '[')
957 {
958
959 // Can be "[1, 2, 3]->Func()".
960 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
961 p = arg;
962 }
963 return p;
964}
965
966/*
967 * parse a list: [expr, expr]
968 * "*arg" points to the '['.
969 * ppconst->pp_is_const is set if all items are a constant.
970 */
971 static int
972compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
973{
974 char_u *p = skipwhite(*arg + 1);
975 char_u *whitep = *arg + 1;
976 int count = 0;
977 int is_const;
978 int is_all_const = TRUE; // reset when non-const encountered
979
980 for (;;)
981 {
982 if (may_get_next_line(whitep, &p, cctx) == FAIL)
983 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000984 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000985 return FAIL;
986 }
987 if (*p == ',')
988 {
989 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
990 return FAIL;
991 }
992 if (*p == ']')
993 {
994 ++p;
995 break;
996 }
997 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
998 return FAIL;
999 if (!is_const)
1000 is_all_const = FALSE;
1001 ++count;
1002 if (*p == ',')
1003 {
1004 ++p;
1005 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1006 {
1007 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1008 return FAIL;
1009 }
1010 }
1011 whitep = p;
1012 p = skipwhite(p);
1013 }
1014 *arg = p;
1015
1016 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001017 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018}
1019
1020/*
1021 * Parse a lambda: "(arg, arg) => expr"
1022 * "*arg" points to the '('.
1023 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1024 */
1025 static int
1026compile_lambda(char_u **arg, cctx_T *cctx)
1027{
1028 int r;
1029 typval_T rettv;
1030 ufunc_T *ufunc;
1031 evalarg_T evalarg;
1032
1033 init_evalarg(&evalarg);
1034 evalarg.eval_flags = EVAL_EVALUATE;
1035 evalarg.eval_cctx = cctx;
1036
1037 // Get the funcref in "rettv".
1038 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1039 if (r != OK)
1040 {
1041 clear_evalarg(&evalarg, NULL);
1042 return r;
1043 }
1044
1045 // "rettv" will now be a partial referencing the function.
1046 ufunc = rettv.vval.v_partial->pt_func;
1047 ++ufunc->uf_refcount;
1048 clear_tv(&rettv);
1049
1050 // Compile it here to get the return type. The return type is optional,
1051 // when it's missing use t_unknown. This is recognized in
1052 // compile_return().
1053 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1054 ufunc->uf_ret_type = &t_unknown;
1055 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1056
1057 // When the outer function is compiled for profiling or debugging, the
1058 // lambda may be called without profiling or debugging. Compile it here in
1059 // the right context.
1060 if (cctx->ctx_compile_type == CT_DEBUG
1061#ifdef FEAT_PROFILE
1062 || cctx->ctx_compile_type == CT_PROFILE
1063#endif
1064 )
1065 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1066
Bram Moolenaar139575d2022-03-15 19:29:30 +00001067 // if the outer function is not compiled for debugging or profiling, this
1068 // one might be
1069 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001070 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001071 compiletype_T compile_type = get_compile_type(ufunc);
1072
1073 if (compile_type != CT_NONE)
1074 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001075 }
1076
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001077 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1078 // "*arg" may point into it. Point into the original line to avoid a
1079 // dangling pointer.
1080 if (evalarg.eval_using_cmdline)
1081 {
1082 garray_T *gap = &evalarg.eval_tofree_ga;
1083 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1084
1085 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1086 + off;
1087 }
1088
1089 clear_evalarg(&evalarg, NULL);
1090
1091 if (ufunc->uf_def_status == UF_COMPILED)
1092 {
1093 // The return type will now be known.
1094 set_function_type(ufunc);
1095
1096 // The function reference count will be 1. When the ISN_FUNCREF
1097 // instruction is deleted the reference count is decremented and the
1098 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001099 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100 }
1101
1102 func_ptr_unref(ufunc);
1103 return FAIL;
1104}
1105
1106/*
1107 * Get a lambda and compile it. Uses Vim9 syntax.
1108 */
1109 int
1110get_lambda_tv_and_compile(
1111 char_u **arg,
1112 typval_T *rettv,
1113 int types_optional,
1114 evalarg_T *evalarg)
1115{
1116 int r;
1117 ufunc_T *ufunc;
1118 int save_sc_version = current_sctx.sc_version;
1119
1120 // Get the funcref in "rettv".
1121 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1122 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1123 current_sctx.sc_version = save_sc_version;
1124 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001125 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126
1127 // "rettv" will now be a partial referencing the function.
1128 ufunc = rettv->vval.v_partial->pt_func;
1129
1130 // Compile it here to get the return type. The return type is optional,
1131 // when it's missing use t_unknown. This is recognized in
1132 // compile_return().
1133 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1134 ufunc->uf_ret_type = &t_unknown;
1135 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1136
1137 if (ufunc->uf_def_status == UF_COMPILED)
1138 {
1139 // The return type will now be known.
1140 set_function_type(ufunc);
1141 return OK;
1142 }
1143 clear_tv(rettv);
1144 return FAIL;
1145}
1146
1147/*
1148 * parse a dict: {key: val, [key]: val}
1149 * "*arg" points to the '{'.
1150 * ppconst->pp_is_const is set if all item values are a constant.
1151 */
1152 static int
1153compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1154{
1155 garray_T *instr = &cctx->ctx_instr;
1156 int count = 0;
1157 dict_T *d = dict_alloc();
1158 dictitem_T *item;
1159 char_u *whitep = *arg + 1;
1160 char_u *p;
1161 int is_const;
1162 int is_all_const = TRUE; // reset when non-const encountered
1163
1164 if (d == NULL)
1165 return FAIL;
1166 if (generate_ppconst(cctx, ppconst) == FAIL)
1167 return FAIL;
1168 for (;;)
1169 {
1170 char_u *key = NULL;
1171
1172 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1173 {
1174 *arg = NULL;
1175 goto failret;
1176 }
1177
1178 if (**arg == '}')
1179 break;
1180
1181 if (**arg == '[')
1182 {
1183 isn_T *isn;
1184
1185 // {[expr]: value} uses an evaluated key.
1186 *arg = skipwhite(*arg + 1);
1187 if (compile_expr0(arg, cctx) == FAIL)
1188 return FAIL;
1189 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1190 if (isn->isn_type == ISN_PUSHNR)
1191 {
1192 char buf[NUMBUFLEN];
1193
1194 // Convert to string at compile time.
1195 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1196 isn->isn_type = ISN_PUSHS;
1197 isn->isn_arg.string = vim_strsave((char_u *)buf);
1198 }
1199 if (isn->isn_type == ISN_PUSHS)
1200 key = isn->isn_arg.string;
1201 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1202 return FAIL;
1203 *arg = skipwhite(*arg);
1204 if (**arg != ']')
1205 {
1206 emsg(_(e_missing_matching_bracket_after_dict_key));
1207 return FAIL;
1208 }
1209 ++*arg;
1210 }
1211 else
1212 {
1213 // {"name": value},
1214 // {'name': value},
1215 // {name: value} use "name" as a literal key
1216 key = get_literal_key(arg);
1217 if (key == NULL)
1218 return FAIL;
1219 if (generate_PUSHS(cctx, &key) == FAIL)
1220 return FAIL;
1221 }
1222
1223 // Check for duplicate keys, if using string keys.
1224 if (key != NULL)
1225 {
1226 item = dict_find(d, key, -1);
1227 if (item != NULL)
1228 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001229 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001230 goto failret;
1231 }
1232 item = dictitem_alloc(key);
1233 if (item != NULL)
1234 {
1235 item->di_tv.v_type = VAR_UNKNOWN;
1236 item->di_tv.v_lock = 0;
1237 if (dict_add(d, item) == FAIL)
1238 dictitem_free(item);
1239 }
1240 }
1241
1242 if (**arg != ':')
1243 {
1244 if (*skipwhite(*arg) == ':')
1245 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1246 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001247 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248 return FAIL;
1249 }
1250 whitep = *arg + 1;
1251 if (!IS_WHITE_OR_NUL(*whitep))
1252 {
1253 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1254 return FAIL;
1255 }
1256
1257 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1258 {
1259 *arg = NULL;
1260 goto failret;
1261 }
1262
1263 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1264 return FAIL;
1265 if (!is_const)
1266 is_all_const = FALSE;
1267 ++count;
1268
1269 whitep = *arg;
1270 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1271 {
1272 *arg = NULL;
1273 goto failret;
1274 }
1275 if (**arg == '}')
1276 break;
1277 if (**arg != ',')
1278 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001279 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 goto failret;
1281 }
1282 if (IS_WHITE_OR_NUL(*whitep))
1283 {
1284 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1285 return FAIL;
1286 }
1287 whitep = *arg + 1;
1288 if (!IS_WHITE_OR_NUL(*whitep))
1289 {
1290 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1291 return FAIL;
1292 }
1293 *arg = skipwhite(whitep);
1294 }
1295
1296 *arg = *arg + 1;
1297
1298 // Allow for following comment, after at least one space.
1299 p = skipwhite(*arg);
1300 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1301 *arg += STRLEN(*arg);
1302
1303 dict_unref(d);
1304 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001305 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001306
1307failret:
1308 if (*arg == NULL)
1309 {
1310 semsg(_(e_missing_dict_end), _("[end of lines]"));
1311 *arg = (char_u *)"";
1312 }
1313 dict_unref(d);
1314 return FAIL;
1315}
1316
1317/*
1318 * Compile "&option".
1319 */
1320 static int
1321compile_get_option(char_u **arg, cctx_T *cctx)
1322{
1323 typval_T rettv;
1324 char_u *start = *arg;
1325 int ret;
1326
1327 // parse the option and get the current value to get the type.
1328 rettv.v_type = VAR_UNKNOWN;
1329 ret = eval_option(arg, &rettv, TRUE);
1330 if (ret == OK)
1331 {
1332 // include the '&' in the name, eval_option() expects it.
1333 char_u *name = vim_strnsave(start, *arg - start);
1334 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1335 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1336
1337 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1338 vim_free(name);
1339 }
1340 clear_tv(&rettv);
1341
1342 return ret;
1343}
1344
1345/*
1346 * Compile "$VAR".
1347 */
1348 static int
1349compile_get_env(char_u **arg, cctx_T *cctx)
1350{
1351 char_u *start = *arg;
1352 int len;
1353 int ret;
1354 char_u *name;
1355
1356 ++*arg;
1357 len = get_env_len(arg);
1358 if (len == 0)
1359 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001360 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361 return FAIL;
1362 }
1363
1364 // include the '$' in the name, eval_env_var() expects it.
1365 name = vim_strnsave(start, len + 1);
1366 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1367 vim_free(name);
1368 return ret;
1369}
1370
1371/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001372 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001373 */
1374 static int
1375compile_interp_string(char_u **arg, cctx_T *cctx)
1376{
1377 typval_T tv;
1378 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001379 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001380 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001381 int count = 0;
1382 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001383
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001384 // *arg is on the '$' character, move it to the first string character.
1385 ++*arg;
1386 quote = **arg;
1387 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001388
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001389 for (;;)
1390 {
1391 // Get the string up to the matching quote or to a single '{'.
1392 // "arg" is advanced to either the quote or the '{'.
1393 if (quote == '"')
1394 ret = eval_string(arg, &tv, evaluate, TRUE);
1395 else
1396 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1397 if (ret == FAIL)
1398 break;
1399 if (evaluate)
1400 {
1401 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1402 || (**arg != '{' && count == 0))
1403 {
1404 // generate non-empty string or empty string if it's the only
1405 // one
1406 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1407 return FAIL;
1408 tv.vval.v_string = NULL; // don't free it now
1409 ++count;
1410 }
1411 clear_tv(&tv);
1412 }
1413
1414 if (**arg != '{')
1415 {
1416 // found terminating quote
1417 ++*arg;
1418 break;
1419 }
1420
1421 p = compile_one_expr_in_str(*arg, cctx);
1422 if (p == NULL)
1423 {
1424 ret = FAIL;
1425 break;
1426 }
1427 ++count;
1428 *arg = p;
1429 }
LemonBoy2eaef102022-05-06 13:14:50 +01001430
1431 if (ret == FAIL || !evaluate)
1432 return ret;
1433
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001434 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1435 if (count > 1)
1436 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001437
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001438 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001439}
1440
1441/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 * Compile "@r".
1443 */
1444 static int
1445compile_get_register(char_u **arg, cctx_T *cctx)
1446{
1447 int ret;
1448
1449 ++*arg;
1450 if (**arg == NUL)
1451 {
1452 semsg(_(e_syntax_error_at_str), *arg - 1);
1453 return FAIL;
1454 }
1455 if (!valid_yank_reg(**arg, FALSE))
1456 {
1457 emsg_invreg(**arg);
1458 return FAIL;
1459 }
1460 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1461 ++*arg;
1462 return ret;
1463}
1464
1465/*
1466 * Apply leading '!', '-' and '+' to constant "rettv".
1467 * When "numeric_only" is TRUE do not apply '!'.
1468 */
1469 static int
1470apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1471{
1472 char_u *p = *end;
1473
1474 // this works from end to start
1475 while (p > start)
1476 {
1477 --p;
1478 if (*p == '-' || *p == '+')
1479 {
1480 // only '-' has an effect, for '+' we only check the type
1481#ifdef FEAT_FLOAT
1482 if (rettv->v_type == VAR_FLOAT)
1483 {
1484 if (*p == '-')
1485 rettv->vval.v_float = -rettv->vval.v_float;
1486 }
1487 else
1488#endif
1489 {
1490 varnumber_T val;
1491 int error = FALSE;
1492
1493 // tv_get_number_chk() accepts a string, but we don't want that
1494 // here
1495 if (check_not_string(rettv) == FAIL)
1496 return FAIL;
1497 val = tv_get_number_chk(rettv, &error);
1498 clear_tv(rettv);
1499 if (error)
1500 return FAIL;
1501 if (*p == '-')
1502 val = -val;
1503 rettv->v_type = VAR_NUMBER;
1504 rettv->vval.v_number = val;
1505 }
1506 }
1507 else if (numeric_only)
1508 {
1509 ++p;
1510 break;
1511 }
1512 else if (*p == '!')
1513 {
1514 int v = tv2bool(rettv);
1515
1516 // '!' is permissive in the type.
1517 clear_tv(rettv);
1518 rettv->v_type = VAR_BOOL;
1519 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1520 }
1521 }
1522 *end = p;
1523 return OK;
1524}
1525
1526/*
1527 * Recognize v: variables that are constants and set "rettv".
1528 */
1529 static void
1530get_vim_constant(char_u **arg, typval_T *rettv)
1531{
1532 if (STRNCMP(*arg, "v:true", 6) == 0)
1533 {
1534 rettv->v_type = VAR_BOOL;
1535 rettv->vval.v_number = VVAL_TRUE;
1536 *arg += 6;
1537 }
1538 else if (STRNCMP(*arg, "v:false", 7) == 0)
1539 {
1540 rettv->v_type = VAR_BOOL;
1541 rettv->vval.v_number = VVAL_FALSE;
1542 *arg += 7;
1543 }
1544 else if (STRNCMP(*arg, "v:null", 6) == 0)
1545 {
1546 rettv->v_type = VAR_SPECIAL;
1547 rettv->vval.v_number = VVAL_NULL;
1548 *arg += 6;
1549 }
1550 else if (STRNCMP(*arg, "v:none", 6) == 0)
1551 {
1552 rettv->v_type = VAR_SPECIAL;
1553 rettv->vval.v_number = VVAL_NONE;
1554 *arg += 6;
1555 }
1556}
1557
1558 exprtype_T
1559get_compare_type(char_u *p, int *len, int *type_is)
1560{
1561 exprtype_T type = EXPR_UNKNOWN;
1562 int i;
1563
1564 switch (p[0])
1565 {
1566 case '=': if (p[1] == '=')
1567 type = EXPR_EQUAL;
1568 else if (p[1] == '~')
1569 type = EXPR_MATCH;
1570 break;
1571 case '!': if (p[1] == '=')
1572 type = EXPR_NEQUAL;
1573 else if (p[1] == '~')
1574 type = EXPR_NOMATCH;
1575 break;
1576 case '>': if (p[1] != '=')
1577 {
1578 type = EXPR_GREATER;
1579 *len = 1;
1580 }
1581 else
1582 type = EXPR_GEQUAL;
1583 break;
1584 case '<': if (p[1] != '=')
1585 {
1586 type = EXPR_SMALLER;
1587 *len = 1;
1588 }
1589 else
1590 type = EXPR_SEQUAL;
1591 break;
1592 case 'i': if (p[1] == 's')
1593 {
1594 // "is" and "isnot"; but not a prefix of a name
1595 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1596 *len = 5;
1597 i = p[*len];
1598 if (!isalnum(i) && i != '_')
1599 {
1600 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1601 *type_is = TRUE;
1602 }
1603 }
1604 break;
1605 }
1606 return type;
1607}
1608
1609/*
1610 * Skip over an expression, ignoring most errors.
1611 */
1612 void
1613skip_expr_cctx(char_u **arg, cctx_T *cctx)
1614{
1615 evalarg_T evalarg;
1616
1617 init_evalarg(&evalarg);
1618 evalarg.eval_cctx = cctx;
1619 skip_expr(arg, &evalarg);
1620 clear_evalarg(&evalarg, NULL);
1621}
1622
1623/*
1624 * Check that the top of the type stack has a type that can be used as a
1625 * condition. Give an error and return FAIL if not.
1626 */
1627 int
1628bool_on_stack(cctx_T *cctx)
1629{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001630 type_T *type;
1631
Bram Moolenaar078a4612022-01-04 15:17:03 +00001632 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 if (type == &t_bool)
1634 return OK;
1635
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001636 if (type == &t_any
1637 || type == &t_unknown
1638 || type == &t_number
1639 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1641 // This requires a runtime type check.
1642 return generate_COND2BOOL(cctx);
1643
1644 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1645}
1646
1647/*
1648 * Give the "white on both sides" error, taking the operator from "p[len]".
1649 */
1650 void
1651error_white_both(char_u *op, int len)
1652{
1653 char_u buf[10];
1654
1655 vim_strncpy(buf, op, len);
1656 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1657}
1658
1659/*
1660 * Compile code to apply '-', '+' and '!'.
1661 * When "numeric_only" is TRUE do not apply '!'.
1662 */
1663 static int
1664compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1665{
1666 char_u *p = *end;
1667
1668 // this works from end to start
1669 while (p > start)
1670 {
1671 --p;
1672 while (VIM_ISWHITE(*p))
1673 --p;
1674 if (*p == '-' || *p == '+')
1675 {
1676 int negate = *p == '-';
1677 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678 type_T *type;
1679
Bram Moolenaar078a4612022-01-04 15:17:03 +00001680 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681 if (type != &t_float && need_type(type, &t_number,
1682 -1, 0, cctx, FALSE, FALSE) == FAIL)
1683 return FAIL;
1684
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685 // only '-' has an effect, for '+' we only check the type
1686 if (negate)
1687 {
1688 isn = generate_instr(cctx, ISN_NEGATENR);
1689 if (isn == NULL)
1690 return FAIL;
1691 }
1692 }
1693 else if (numeric_only)
1694 {
1695 ++p;
1696 break;
1697 }
1698 else
1699 {
1700 int invert = *p == '!';
1701
1702 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1703 {
1704 if (p[-1] == '!')
1705 invert = !invert;
1706 --p;
1707 }
1708 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1709 return FAIL;
1710 }
1711 }
1712 *end = p;
1713 return OK;
1714}
1715
1716/*
1717 * Compile "(expression)": recursive!
1718 * Return FAIL/OK.
1719 */
1720 static int
1721compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1722{
1723 int ret;
1724 char_u *p = *arg + 1;
1725
1726 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1727 return FAIL;
1728 if (ppconst->pp_used <= PPSIZE - 10)
1729 {
1730 ret = compile_expr1(arg, cctx, ppconst);
1731 }
1732 else
1733 {
1734 // Not enough space in ppconst, flush constants.
1735 if (generate_ppconst(cctx, ppconst) == FAIL)
1736 return FAIL;
1737 ret = compile_expr0(arg, cctx);
1738 }
1739 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1740 return FAIL;
1741 if (**arg == ')')
1742 ++*arg;
1743 else if (ret == OK)
1744 {
1745 emsg(_(e_missing_closing_paren));
1746 ret = FAIL;
1747 }
1748 return ret;
1749}
1750
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001751static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001752
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001753/*
1754 * Compile whatever comes after "name" or "name()".
1755 * Advances "*arg" only when something was recognized.
1756 */
1757 static int
1758compile_subscript(
1759 char_u **arg,
1760 cctx_T *cctx,
1761 char_u *start_leader,
1762 char_u **end_leader,
1763 ppconst_T *ppconst)
1764{
1765 char_u *name_start = *end_leader;
1766 int keeping_dict = FALSE;
1767
1768 for (;;)
1769 {
1770 char_u *p = skipwhite(*arg);
1771
1772 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1773 {
1774 char_u *next = peek_next_line_from_context(cctx);
1775
1776 // If a following line starts with "->{" or "->X" advance to that
1777 // line, so that a line break before "->" is allowed.
1778 // Also if a following line starts with ".x".
1779 if (next != NULL &&
1780 ((next[0] == '-' && next[1] == '>'
1781 && (next[2] == '{'
1782 || ASCII_ISALPHA(*skipwhite(next + 2))))
1783 || (next[0] == '.' && eval_isdictc(next[1]))))
1784 {
1785 next = next_line_from_context(cctx, TRUE);
1786 if (next == NULL)
1787 return FAIL;
1788 *arg = next;
1789 p = skipwhite(*arg);
1790 }
1791 }
1792
1793 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1794 // is not a function call.
1795 if (**arg == '(')
1796 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001797 type_T *type;
1798 int argcount = 0;
1799
1800 if (generate_ppconst(cctx, ppconst) == FAIL)
1801 return FAIL;
1802 ppconst->pp_is_const = FALSE;
1803
1804 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001805 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001806
1807 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001808 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001809 return FAIL;
1810 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1811 return FAIL;
1812 if (keeping_dict)
1813 {
1814 keeping_dict = FALSE;
1815 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1816 return FAIL;
1817 }
1818 }
1819 else if (*p == '-' && p[1] == '>')
1820 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001821 char_u *pstart = p;
1822 int alt;
1823 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001824
Bram Moolenaarc7349932022-01-16 20:59:39 +00001825 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826 if (generate_ppconst(cctx, ppconst) == FAIL)
1827 return FAIL;
1828 ppconst->pp_is_const = FALSE;
1829
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001830 // Apply the '!', '-' and '+' first:
1831 // -1.0->func() works like (-1.0)->func()
1832 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1833 return FAIL;
1834
1835 p += 2;
1836 *arg = skipwhite(p);
1837 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001838
1839 // Three alternatives handled here:
1840 // 1. "base->name(" only a name, use compile_call()
1841 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1842 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001844 alt = 2;
1845 else
1846 {
1847 // alternative 1 or 3
1848 p = *arg;
1849 if (!eval_isnamec1(*p))
1850 {
1851 semsg(_(e_trailing_characters_str), pstart);
1852 return FAIL;
1853 }
1854 if (ASCII_ISALPHA(*p) && p[1] == ':')
1855 p += 2;
1856 for ( ; eval_isnamec(*p); ++p)
1857 ;
1858 if (*p == '(')
1859 {
1860 // alternative 1
1861 alt = 1;
1862 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1863 return FAIL;
1864 }
1865 else
1866 {
1867 // Must be alternative 3, find the "(". Only works within
1868 // one line.
1869 alt = 3;
1870 paren = vim_strchr(p, '(');
1871 if (paren == NULL)
1872 {
1873 semsg(_(e_missing_parenthesis_str), *arg);
1874 return FAIL;
1875 }
1876 }
1877 }
1878
1879 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001880 {
1881 int argcount = 1;
1882 garray_T *stack = &cctx->ctx_type_stack;
1883 int type_idx_start = stack->ga_len;
1884 type_T *type;
1885 int expr_isn_start = cctx->ctx_instr.ga_len;
1886 int expr_isn_end;
1887 int arg_isn_count;
1888
Bram Moolenaarc7349932022-01-16 20:59:39 +00001889 if (alt == 2)
1890 {
1891 // Funcref call: list->(Refs[2])(arg)
1892 // or lambda: list->((arg) => expr)(arg)
1893 //
1894 // Fist compile the function expression.
1895 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1896 return FAIL;
1897 }
1898 else
1899 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001900 int fail;
1901 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001902 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001903
Bram Moolenaarc7349932022-01-16 20:59:39 +00001904 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001905
1906 // instead of using LOADG for "import.Func" use PUSHFUNC
1907 ++paren_follows_after_expr;
1908
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001909 // do not look in the next line
1910 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001911
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001912 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001913 || *skipwhite(*arg) != NUL;
1914 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001915 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001916 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001917
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001918 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001919 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001920 if (did_emsg == prev_did_emsg)
1921 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001922 return FAIL;
1923 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001924 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001925
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001926 // Compile the arguments.
1927 if (**arg != '(')
1928 {
1929 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001930 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001931 else
1932 semsg(_(e_missing_parenthesis_str), *arg);
1933 return FAIL;
1934 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001935
1936 // Remember the next instruction index, where the instructions
1937 // for arguments are being written.
1938 expr_isn_end = cctx->ctx_instr.ga_len;
1939
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001940 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001941 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1942 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943 return FAIL;
1944
1945 // Move the instructions for the arguments to before the
1946 // instructions of the expression and move the type of the
1947 // expression after the argument types. This is what ISN_PCALL
1948 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1950 if (arg_isn_count > 0)
1951 {
1952 int expr_isn_count = expr_isn_end - expr_isn_start;
1953 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001954 type_T *decl_type;
1955 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956
1957 if (isn == NULL)
1958 return FAIL;
1959 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1960 + expr_isn_start,
1961 sizeof(isn_T) * expr_isn_count);
1962 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1963 + expr_isn_start,
1964 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1965 sizeof(isn_T) * arg_isn_count);
1966 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1967 + expr_isn_start + arg_isn_count,
1968 isn, sizeof(isn_T) * expr_isn_count);
1969 vim_free(isn);
1970
Bram Moolenaar078a4612022-01-04 15:17:03 +00001971 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1972 type = typep->type_curr;
1973 decl_type = typep->type_decl;
1974 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1975 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1976 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001977 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001978 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1979 typep->type_curr = type;
1980 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001981 }
1982
Bram Moolenaar078a4612022-01-04 15:17:03 +00001983 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1985 return FAIL;
1986 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001987
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 if (keeping_dict)
1989 {
1990 keeping_dict = FALSE;
1991 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1992 return FAIL;
1993 }
1994 }
1995 else if (**arg == '[')
1996 {
1997 int is_slice = FALSE;
1998
1999 // list index: list[123]
2000 // dict member: dict[key]
2001 // string index: text[123]
2002 // blob index: blob[123]
2003 if (generate_ppconst(cctx, ppconst) == FAIL)
2004 return FAIL;
2005 ppconst->pp_is_const = FALSE;
2006
2007 ++p;
2008 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2009 return FAIL;
2010 if (**arg == ':')
2011 {
2012 // missing first index is equal to zero
2013 generate_PUSHNR(cctx, 0);
2014 }
2015 else
2016 {
2017 if (compile_expr0(arg, cctx) == FAIL)
2018 return FAIL;
2019 if (**arg == ':')
2020 {
2021 semsg(_(e_white_space_required_before_and_after_str_at_str),
2022 ":", *arg);
2023 return FAIL;
2024 }
2025 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2026 return FAIL;
2027 *arg = skipwhite(*arg);
2028 }
2029 if (**arg == ':')
2030 {
2031 is_slice = TRUE;
2032 ++*arg;
2033 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2034 {
2035 semsg(_(e_white_space_required_before_and_after_str_at_str),
2036 ":", *arg);
2037 return FAIL;
2038 }
2039 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2040 return FAIL;
2041 if (**arg == ']')
2042 // missing second index is equal to end of string
2043 generate_PUSHNR(cctx, -1);
2044 else
2045 {
2046 if (compile_expr0(arg, cctx) == FAIL)
2047 return FAIL;
2048 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2049 return FAIL;
2050 *arg = skipwhite(*arg);
2051 }
2052 }
2053
2054 if (**arg != ']')
2055 {
2056 emsg(_(e_missing_closing_square_brace));
2057 return FAIL;
2058 }
2059 *arg = *arg + 1;
2060
2061 if (keeping_dict)
2062 {
2063 keeping_dict = FALSE;
2064 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2065 return FAIL;
2066 }
2067 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2068 return FAIL;
2069 }
2070 else if (*p == '.' && p[1] != '.')
2071 {
2072 // dictionary member: dict.name
2073 if (generate_ppconst(cctx, ppconst) == FAIL)
2074 return FAIL;
2075 ppconst->pp_is_const = FALSE;
2076
2077 *arg = p + 1;
2078 if (IS_WHITE_OR_NUL(**arg))
2079 {
2080 emsg(_(e_missing_name_after_dot));
2081 return FAIL;
2082 }
2083 p = *arg;
2084 if (eval_isdictc(*p))
2085 while (eval_isnamec(*p))
2086 MB_PTR_ADV(p);
2087 if (p == *arg)
2088 {
2089 semsg(_(e_syntax_error_at_str), *arg);
2090 return FAIL;
2091 }
2092 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2093 return FAIL;
2094 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2095 return FAIL;
2096 keeping_dict = TRUE;
2097 *arg = p;
2098 }
2099 else
2100 break;
2101 }
2102
2103 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2104 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002105 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2106 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002107 return FAIL;
2108
2109 return OK;
2110}
2111
2112/*
2113 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2114 * "arg" is advanced until after the expression, skipping white space.
2115 *
2116 * If the value is a constant "ppconst->pp_used" will be non-zero.
2117 * Before instructions are generated, any values in "ppconst" will generated.
2118 *
2119 * This is the compiling equivalent of eval1(), eval2(), etc.
2120 */
2121
2122/*
2123 * number number constant
2124 * 0zFFFFFFFF Blob constant
2125 * "string" string constant
2126 * 'string' literal string constant
2127 * &option-name option value
2128 * @r register contents
2129 * identifier variable value
2130 * function() function call
2131 * $VAR environment variable
2132 * (expression) nested expression
2133 * [expr, expr] List
2134 * {key: val, [key]: val} Dictionary
2135 *
2136 * Also handle:
2137 * ! in front logical NOT
2138 * - in front unary minus
2139 * + in front unary plus (ignored)
2140 * trailing (arg) funcref/partial call
2141 * trailing [] subscript in String or List
2142 * trailing .name entry in Dictionary
2143 * trailing ->name() method call
2144 */
2145 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002146compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147 char_u **arg,
2148 cctx_T *cctx,
2149 ppconst_T *ppconst)
2150{
2151 char_u *start_leader, *end_leader;
2152 int ret = OK;
2153 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2154 int used_before = ppconst->pp_used;
2155
2156 ppconst->pp_is_const = FALSE;
2157
2158 /*
2159 * Skip '!', '-' and '+' characters. They are handled later.
2160 */
2161 start_leader = *arg;
2162 if (eval_leader(arg, TRUE) == FAIL)
2163 return FAIL;
2164 end_leader = *arg;
2165
2166 rettv->v_type = VAR_UNKNOWN;
2167 switch (**arg)
2168 {
2169 /*
2170 * Number constant.
2171 */
2172 case '0': // also for blob starting with 0z
2173 case '1':
2174 case '2':
2175 case '3':
2176 case '4':
2177 case '5':
2178 case '6':
2179 case '7':
2180 case '8':
2181 case '9':
2182 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2183 return FAIL;
2184 // Apply "-" and "+" just before the number now, right to
2185 // left. Matters especially when "->" follows. Stops at
2186 // '!'.
2187 if (apply_leader(rettv, TRUE,
2188 start_leader, &end_leader) == FAIL)
2189 {
2190 clear_tv(rettv);
2191 return FAIL;
2192 }
2193 break;
2194
2195 /*
2196 * String constant: "string".
2197 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002198 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002199 return FAIL;
2200 break;
2201
2202 /*
2203 * Literal string constant: 'str''ing'.
2204 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002205 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 return FAIL;
2207 break;
2208
2209 /*
2210 * Constant Vim variable.
2211 */
2212 case 'v': get_vim_constant(arg, rettv);
2213 ret = NOTDONE;
2214 break;
2215
2216 /*
2217 * "true" constant
2218 */
2219 case 't': if (STRNCMP(*arg, "true", 4) == 0
2220 && !eval_isnamec((*arg)[4]))
2221 {
2222 *arg += 4;
2223 rettv->v_type = VAR_BOOL;
2224 rettv->vval.v_number = VVAL_TRUE;
2225 }
2226 else
2227 ret = NOTDONE;
2228 break;
2229
2230 /*
2231 * "false" constant
2232 */
2233 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2234 && !eval_isnamec((*arg)[5]))
2235 {
2236 *arg += 5;
2237 rettv->v_type = VAR_BOOL;
2238 rettv->vval.v_number = VVAL_FALSE;
2239 }
2240 else
2241 ret = NOTDONE;
2242 break;
2243
2244 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002245 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002246 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002247 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002249 char_u *p = *arg + 4;
2250 int len;
2251
2252 for (len = 0; eval_isnamec(p[len]); ++len)
2253 ;
2254 ret = handle_predefined(*arg, len + 4, rettv);
2255 if (ret == FAIL)
2256 ret = NOTDONE;
2257 else
2258 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002259 }
2260 else
2261 ret = NOTDONE;
2262 break;
2263
2264 /*
2265 * List: [expr, expr]
2266 */
2267 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2268 return FAIL;
2269 ret = compile_list(arg, cctx, ppconst);
2270 break;
2271
2272 /*
2273 * Dictionary: {'key': val, 'key': val}
2274 */
2275 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2276 return FAIL;
2277 ret = compile_dict(arg, cctx, ppconst);
2278 break;
2279
2280 /*
2281 * Option value: &name
2282 */
2283 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2284 return FAIL;
2285 ret = compile_get_option(arg, cctx);
2286 break;
2287
2288 /*
2289 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002290 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 */
2292 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2293 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002294 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2295 ret = compile_interp_string(arg, cctx);
2296 else
2297 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002298 break;
2299
2300 /*
2301 * Register contents: @r.
2302 */
2303 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2304 return FAIL;
2305 ret = compile_get_register(arg, cctx);
2306 break;
2307 /*
2308 * nested expression: (expression).
2309 * lambda: (arg, arg) => expr
2310 * funcref: (arg, arg) => { statement }
2311 */
2312 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2313 ret = compile_lambda(arg, cctx);
2314 if (ret == NOTDONE)
2315 ret = compile_parenthesis(arg, cctx, ppconst);
2316 break;
2317
2318 default: ret = NOTDONE;
2319 break;
2320 }
2321 if (ret == FAIL)
2322 return FAIL;
2323
2324 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2325 {
2326 if (cctx->ctx_skip == SKIP_YES)
2327 clear_tv(rettv);
2328 else
2329 // A constant expression can possibly be handled compile time,
2330 // return the value instead of generating code.
2331 ++ppconst->pp_used;
2332 }
2333 else if (ret == NOTDONE)
2334 {
2335 char_u *p;
2336 int r;
2337
2338 if (!eval_isnamec1(**arg))
2339 {
2340 if (!vim9_bad_comment(*arg))
2341 {
2342 if (ends_excmd(*skipwhite(*arg)))
2343 semsg(_(e_empty_expression_str), *arg);
2344 else
2345 semsg(_(e_name_expected_str), *arg);
2346 }
2347 return FAIL;
2348 }
2349
2350 // "name" or "name()"
2351 p = to_name_end(*arg, TRUE);
2352 if (p - *arg == (size_t)1 && **arg == '_')
2353 {
2354 emsg(_(e_cannot_use_underscore_here));
2355 return FAIL;
2356 }
2357
2358 if (*p == '(')
2359 {
2360 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2361 }
2362 else
2363 {
2364 if (cctx->ctx_skip != SKIP_YES
2365 && generate_ppconst(cctx, ppconst) == FAIL)
2366 return FAIL;
2367 r = compile_load(arg, p, cctx, TRUE, TRUE);
2368 }
2369 if (r == FAIL)
2370 return FAIL;
2371 }
2372
2373 // Handle following "[]", ".member", etc.
2374 // Then deal with prefixed '-', '+' and '!', if not done already.
2375 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2376 ppconst) == FAIL)
2377 return FAIL;
2378 if (ppconst->pp_used > 0)
2379 {
2380 // apply the '!', '-' and '+' before the constant
2381 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2382 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2383 return FAIL;
2384 return OK;
2385 }
2386 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2387 return FAIL;
2388 return OK;
2389}
2390
2391/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002392 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 */
2394 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002395compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002396{
2397 type_T *want_type = NULL;
2398
2399 // Recognize <type>
2400 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2401 {
2402 ++*arg;
2403 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2404 if (want_type == NULL)
2405 return FAIL;
2406
2407 if (**arg != '>')
2408 {
2409 if (*skipwhite(*arg) == '>')
2410 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2411 else
2412 emsg(_(e_missing_gt));
2413 return FAIL;
2414 }
2415 ++*arg;
2416 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2417 return FAIL;
2418 }
2419
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002420 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421 return FAIL;
2422
2423 if (want_type != NULL)
2424 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425 type_T *actual;
2426 where_T where = WHERE_INIT;
2427
2428 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002429 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002430 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002432 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2433 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002434 return FAIL;
2435 }
2436 }
2437
2438 return OK;
2439}
2440
2441/*
2442 * * number multiplication
2443 * / number division
2444 * % number modulo
2445 */
2446 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002447compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002448{
2449 char_u *op;
2450 char_u *next;
2451 int ppconst_used = ppconst->pp_used;
2452
2453 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002454 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002455 return FAIL;
2456
2457 /*
2458 * Repeat computing, until no "*", "/" or "%" is following.
2459 */
2460 for (;;)
2461 {
2462 op = may_peek_next_line(cctx, *arg, &next);
2463 if (*op != '*' && *op != '/' && *op != '%')
2464 break;
2465 if (next != NULL)
2466 {
2467 *arg = next_line_from_context(cctx, TRUE);
2468 op = skipwhite(*arg);
2469 }
2470
2471 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2472 {
2473 error_white_both(op, 1);
2474 return FAIL;
2475 }
2476 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2477 return FAIL;
2478
2479 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002480 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 return FAIL;
2482
2483 if (ppconst->pp_used == ppconst_used + 2
2484 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2485 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2486 {
2487 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2488 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2489 varnumber_T res = 0;
2490 int failed = FALSE;
2491
2492 // both are numbers: compute the result
2493 switch (*op)
2494 {
2495 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2496 break;
2497 case '/': res = num_divide(tv1->vval.v_number,
2498 tv2->vval.v_number, &failed);
2499 break;
2500 case '%': res = num_modulus(tv1->vval.v_number,
2501 tv2->vval.v_number, &failed);
2502 break;
2503 }
2504 if (failed)
2505 return FAIL;
2506 tv1->vval.v_number = res;
2507 --ppconst->pp_used;
2508 }
2509 else
2510 {
2511 generate_ppconst(cctx, ppconst);
2512 generate_two_op(cctx, op);
2513 }
2514 }
2515
2516 return OK;
2517}
2518
2519/*
2520 * + number addition or list/blobl concatenation
2521 * - number subtraction
2522 * .. string concatenation
2523 */
2524 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002525compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526{
2527 char_u *op;
2528 char_u *next;
2529 int oplen;
2530 int ppconst_used = ppconst->pp_used;
2531
2532 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002533 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002534 return FAIL;
2535
2536 /*
2537 * Repeat computing, until no "+", "-" or ".." is following.
2538 */
2539 for (;;)
2540 {
2541 op = may_peek_next_line(cctx, *arg, &next);
2542 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2543 break;
2544 if (op[0] == op[1] && *op != '.' && next)
2545 // Finding "++" or "--" on the next line is a separate command.
2546 // But ".." is concatenation.
2547 break;
2548 oplen = (*op == '.' ? 2 : 1);
2549 if (next != NULL)
2550 {
2551 *arg = next_line_from_context(cctx, TRUE);
2552 op = skipwhite(*arg);
2553 }
2554
2555 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2556 {
2557 error_white_both(op, oplen);
2558 return FAIL;
2559 }
2560
2561 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2562 return FAIL;
2563
2564 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002565 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 return FAIL;
2567
2568 if (ppconst->pp_used == ppconst_used + 2
2569 && (*op == '.'
2570 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2571 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2572 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2573 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2574 {
2575 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2576 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2577
2578 // concat/subtract/add constant numbers
2579 if (*op == '+')
2580 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2581 else if (*op == '-')
2582 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2583 else
2584 {
2585 // concatenate constant strings
2586 char_u *s1 = tv1->vval.v_string;
2587 char_u *s2 = tv2->vval.v_string;
2588 size_t len1 = STRLEN(s1);
2589
2590 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2591 if (tv1->vval.v_string == NULL)
2592 {
2593 clear_ppconst(ppconst);
2594 return FAIL;
2595 }
2596 mch_memmove(tv1->vval.v_string, s1, len1);
2597 STRCPY(tv1->vval.v_string + len1, s2);
2598 vim_free(s1);
2599 vim_free(s2);
2600 }
2601 --ppconst->pp_used;
2602 }
2603 else
2604 {
2605 generate_ppconst(cctx, ppconst);
2606 ppconst->pp_is_const = FALSE;
2607 if (*op == '.')
2608 {
2609 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2610 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2611 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002612 if (generate_CONCAT(cctx, 2) == FAIL)
2613 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002614 }
2615 else
2616 generate_two_op(cctx, op);
2617 }
2618 }
2619
2620 return OK;
2621}
2622
2623/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002624 * expr6a >> expr6b
2625 * expr6a << expr6b
2626 *
2627 * Produces instructions:
2628 * OPNR bitwise left or right shift
2629 */
2630 static int
2631compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2632{
2633 exprtype_T type = EXPR_UNKNOWN;
2634 char_u *p;
2635 char_u *next;
2636 int len = 2;
2637 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002638 isn_T *isn;
2639
2640 // get the first variable
2641 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2642 return FAIL;
2643
2644 /*
2645 * Repeat computing, until no "+", "-" or ".." is following.
2646 */
2647 for (;;)
2648 {
2649 type = EXPR_UNKNOWN;
2650
2651 p = may_peek_next_line(cctx, *arg, &next);
2652 if (p[0] == '<' && p[1] == '<')
2653 type = EXPR_LSHIFT;
2654 else if (p[0] == '>' && p[1] == '>')
2655 type = EXPR_RSHIFT;
2656
2657 if (type == EXPR_UNKNOWN)
2658 return OK;
2659
2660 // Handle a bitwise left or right shift operator
2661 if (ppconst->pp_used == ppconst_used + 1)
2662 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002663 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002664 {
2665 // left operand should be a number
2666 emsg(_(e_bitshift_ops_must_be_number));
2667 return FAIL;
2668 }
2669 }
2670 else
2671 {
2672 type_T *t = get_type_on_stack(cctx, 0);
2673
2674 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2675 {
2676 emsg(_(e_bitshift_ops_must_be_number));
2677 return FAIL;
2678 }
2679 }
2680
2681 if (next != NULL)
2682 {
2683 *arg = next_line_from_context(cctx, TRUE);
2684 p = skipwhite(*arg);
2685 }
2686
2687 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2688 {
2689 error_white_both(p, len);
2690 return FAIL;
2691 }
2692
2693 // get the second variable
2694 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2695 return FAIL;
2696
2697 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2698 return FAIL;
2699
2700 if (ppconst->pp_used == ppconst_used + 2)
2701 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002702 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2703 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2704
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002705 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002706 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2707 {
2708 // right operand should be a positive number
2709 if (tv2->v_type != VAR_NUMBER)
2710 emsg(_(e_bitshift_ops_must_be_number));
2711 else
2712 emsg(_(e_bitshift_ops_must_be_postive));
2713 return FAIL;
2714 }
2715
2716 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2717 tv1->vval.v_number = 0;
2718 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002719 tv1->vval.v_number =
2720 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002721 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002722 tv1->vval.v_number =
2723 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002724 clear_tv(tv2);
2725 --ppconst->pp_used;
2726 }
2727 else
2728 {
2729 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2730 FALSE, FALSE) == FAIL)
2731 {
2732 emsg(_(e_bitshift_ops_must_be_number));
2733 return FAIL;
2734 }
2735
2736 generate_ppconst(cctx, ppconst);
2737
2738 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2739 if (isn == NULL)
2740 return FAIL;
2741
2742 if (isn != NULL)
2743 isn->isn_arg.op.op_type = type;
2744 }
2745 }
2746
2747 return OK;
2748}
2749
2750/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002751 * expr5a == expr5b
2752 * expr5a =~ expr5b
2753 * expr5a != expr5b
2754 * expr5a !~ expr5b
2755 * expr5a > expr5b
2756 * expr5a >= expr5b
2757 * expr5a < expr5b
2758 * expr5a <= expr5b
2759 * expr5a is expr5b
2760 * expr5a isnot expr5b
2761 *
2762 * Produces instructions:
2763 * EVAL expr5a Push result of "expr5a"
2764 * EVAL expr5b Push result of "expr5b"
2765 * COMPARE one of the compare instructions
2766 */
2767 static int
2768compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2769{
2770 exprtype_T type = EXPR_UNKNOWN;
2771 char_u *p;
2772 char_u *next;
2773 int len = 2;
2774 int type_is = FALSE;
2775 int ppconst_used = ppconst->pp_used;
2776
2777 // get the first variable
2778 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2779 return FAIL;
2780
2781 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002782
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002783 type = get_compare_type(p, &len, &type_is);
2784
2785 /*
2786 * If there is a comparative operator, use it.
2787 */
2788 if (type != EXPR_UNKNOWN)
2789 {
2790 int ic = FALSE; // Default: do not ignore case
2791
2792 if (next != NULL)
2793 {
2794 *arg = next_line_from_context(cctx, TRUE);
2795 p = skipwhite(*arg);
2796 }
2797 if (type_is && (p[len] == '?' || p[len] == '#'))
2798 {
2799 semsg(_(e_invalid_expression_str), *arg);
2800 return FAIL;
2801 }
2802 // extra question mark appended: ignore case
2803 if (p[len] == '?')
2804 {
2805 ic = TRUE;
2806 ++len;
2807 }
2808 // extra '#' appended: match case (ignored)
2809 else if (p[len] == '#')
2810 ++len;
2811 // nothing appended: match case
2812
2813 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2814 {
2815 error_white_both(p, len);
2816 return FAIL;
2817 }
2818
2819 // get the second variable
2820 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2821 return FAIL;
2822
2823 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2824 return FAIL;
2825
2826 if (ppconst->pp_used == ppconst_used + 2)
2827 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002828 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002829 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2830 int ret;
2831
2832 // Both sides are a constant, compute the result now.
2833 // First check for a valid combination of types, this is more
2834 // strict than typval_compare().
2835 if (check_compare_types(type, tv1, tv2) == FAIL)
2836 ret = FAIL;
2837 else
2838 {
2839 ret = typval_compare(tv1, tv2, type, ic);
2840 tv1->v_type = VAR_BOOL;
2841 tv1->vval.v_number = tv1->vval.v_number
2842 ? VVAL_TRUE : VVAL_FALSE;
2843 clear_tv(tv2);
2844 --ppconst->pp_used;
2845 }
2846 return ret;
2847 }
2848
2849 generate_ppconst(cctx, ppconst);
2850 return generate_COMPARE(cctx, type, ic);
2851 }
2852
2853 return OK;
2854}
2855
2856static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2857
2858/*
2859 * Compile || or &&.
2860 */
2861 static int
2862compile_and_or(
2863 char_u **arg,
2864 cctx_T *cctx,
2865 char *op,
2866 ppconst_T *ppconst,
2867 int ppconst_used UNUSED)
2868{
2869 char_u *next;
2870 char_u *p = may_peek_next_line(cctx, *arg, &next);
2871 int opchar = *op;
2872
2873 if (p[0] == opchar && p[1] == opchar)
2874 {
2875 garray_T *instr = &cctx->ctx_instr;
2876 garray_T end_ga;
2877 int save_skip = cctx->ctx_skip;
2878
2879 /*
2880 * Repeat until there is no following "||" or "&&"
2881 */
2882 ga_init2(&end_ga, sizeof(int), 10);
2883 while (p[0] == opchar && p[1] == opchar)
2884 {
2885 long start_lnum = SOURCING_LNUM;
2886 long save_sourcing_lnum;
2887 int start_ctx_lnum = cctx->ctx_lnum;
2888 int save_lnum;
2889 int const_used;
2890 int status;
2891 jumpwhen_T jump_when = opchar == '|'
2892 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2893
2894 if (next != NULL)
2895 {
2896 *arg = next_line_from_context(cctx, TRUE);
2897 p = skipwhite(*arg);
2898 }
2899
2900 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2901 {
2902 semsg(_(e_white_space_required_before_and_after_str_at_str),
2903 op, p);
2904 ga_clear(&end_ga);
2905 return FAIL;
2906 }
2907
2908 save_sourcing_lnum = SOURCING_LNUM;
2909 SOURCING_LNUM = start_lnum;
2910 save_lnum = cctx->ctx_lnum;
2911 cctx->ctx_lnum = start_ctx_lnum;
2912
2913 status = check_ppconst_bool(ppconst);
2914 if (status != FAIL)
2915 {
2916 // Use the last ppconst if possible.
2917 if (ppconst->pp_used > 0)
2918 {
2919 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2920 int is_true = tv2bool(tv);
2921
2922 if ((is_true && opchar == '|')
2923 || (!is_true && opchar == '&'))
2924 {
2925 // For "false && expr" and "true || expr" the "expr"
2926 // does not need to be evaluated.
2927 cctx->ctx_skip = SKIP_YES;
2928 clear_tv(tv);
2929 tv->v_type = VAR_BOOL;
2930 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2931 }
2932 else
2933 {
2934 // For "true && expr" and "false || expr" only "expr"
2935 // needs to be evaluated.
2936 --ppconst->pp_used;
2937 jump_when = JUMP_NEVER;
2938 }
2939 }
2940 else
2941 {
2942 // Every part must evaluate to a bool.
2943 status = bool_on_stack(cctx);
2944 }
2945 }
2946 if (status != FAIL)
2947 status = ga_grow(&end_ga, 1);
2948 cctx->ctx_lnum = save_lnum;
2949 if (status == FAIL)
2950 {
2951 ga_clear(&end_ga);
2952 return FAIL;
2953 }
2954
2955 if (jump_when != JUMP_NEVER)
2956 {
2957 if (cctx->ctx_skip != SKIP_YES)
2958 {
2959 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2960 ++end_ga.ga_len;
2961 }
2962 generate_JUMP(cctx, jump_when, 0);
2963 }
2964
2965 // eval the next expression
2966 SOURCING_LNUM = save_sourcing_lnum;
2967 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2968 {
2969 ga_clear(&end_ga);
2970 return FAIL;
2971 }
2972
2973 const_used = ppconst->pp_used;
2974 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2975 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2976 {
2977 ga_clear(&end_ga);
2978 return FAIL;
2979 }
2980
2981 // "0 || 1" results in true, "1 && 0" results in false.
2982 if (ppconst->pp_used == const_used + 1)
2983 {
2984 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2985
2986 if (tv->v_type == VAR_NUMBER
2987 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2988 {
2989 tv->vval.v_number = tv->vval.v_number == 1
2990 ? VVAL_TRUE : VVAL_FALSE;
2991 tv->v_type = VAR_BOOL;
2992 }
2993 }
2994
2995 p = may_peek_next_line(cctx, *arg, &next);
2996 }
2997
2998 if (check_ppconst_bool(ppconst) == FAIL)
2999 {
3000 ga_clear(&end_ga);
3001 return FAIL;
3002 }
3003
3004 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3005 // Every part must evaluate to a bool.
3006 if (bool_on_stack(cctx) == FAIL)
3007 {
3008 ga_clear(&end_ga);
3009 return FAIL;
3010 }
3011
3012 if (end_ga.ga_len > 0)
3013 {
3014 // Fill in the end label in all jumps.
3015 generate_ppconst(cctx, ppconst);
3016 while (end_ga.ga_len > 0)
3017 {
3018 isn_T *isn;
3019
3020 --end_ga.ga_len;
3021 isn = ((isn_T *)instr->ga_data)
3022 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3023 isn->isn_arg.jump.jump_where = instr->ga_len;
3024 }
3025 }
3026 ga_clear(&end_ga);
3027
3028 cctx->ctx_skip = save_skip;
3029 }
3030
3031 return OK;
3032}
3033
3034/*
3035 * expr4a && expr4a && expr4a logical AND
3036 *
3037 * Produces instructions:
3038 * EVAL expr4a Push result of "expr4a"
3039 * COND2BOOL convert to bool if needed
3040 * JUMP_IF_COND_FALSE end
3041 * EVAL expr4b Push result of "expr4b"
3042 * JUMP_IF_COND_FALSE end
3043 * EVAL expr4c Push result of "expr4c"
3044 * end:
3045 */
3046 static int
3047compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3048{
3049 int ppconst_used = ppconst->pp_used;
3050
3051 // get the first variable
3052 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3053 return FAIL;
3054
3055 // || and && work almost the same
3056 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3057}
3058
3059/*
3060 * expr3a || expr3b || expr3c logical OR
3061 *
3062 * Produces instructions:
3063 * EVAL expr3a Push result of "expr3a"
3064 * COND2BOOL convert to bool if needed
3065 * JUMP_IF_COND_TRUE end
3066 * EVAL expr3b Push result of "expr3b"
3067 * JUMP_IF_COND_TRUE end
3068 * EVAL expr3c Push result of "expr3c"
3069 * end:
3070 */
3071 static int
3072compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3073{
3074 int ppconst_used = ppconst->pp_used;
3075
3076 // eval the first expression
3077 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3078 return FAIL;
3079
3080 // || and && work almost the same
3081 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3082}
3083
3084/*
3085 * Toplevel expression: expr2 ? expr1a : expr1b
3086 * Produces instructions:
3087 * EVAL expr2 Push result of "expr2"
3088 * JUMP_IF_FALSE alt jump if false
3089 * EVAL expr1a
3090 * JUMP_ALWAYS end
3091 * alt: EVAL expr1b
3092 * end:
3093 *
3094 * Toplevel expression: expr2 ?? expr1
3095 * Produces instructions:
3096 * EVAL expr2 Push result of "expr2"
3097 * JUMP_AND_KEEP_IF_TRUE end jump if true
3098 * EVAL expr1
3099 * end:
3100 */
3101 int
3102compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3103{
3104 char_u *p;
3105 int ppconst_used = ppconst->pp_used;
3106 char_u *next;
3107
3108 // Ignore all kinds of errors when not producing code.
3109 if (cctx->ctx_skip == SKIP_YES)
3110 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003111 int prev_did_emsg = did_emsg;
3112
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003113 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003114 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003115 }
3116
3117 // Evaluate the first expression.
3118 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3119 return FAIL;
3120
3121 p = may_peek_next_line(cctx, *arg, &next);
3122 if (*p == '?')
3123 {
3124 int op_falsy = p[1] == '?';
3125 garray_T *instr = &cctx->ctx_instr;
3126 garray_T *stack = &cctx->ctx_type_stack;
3127 int alt_idx = instr->ga_len;
3128 int end_idx = 0;
3129 isn_T *isn;
3130 type_T *type1 = NULL;
3131 int has_const_expr = FALSE;
3132 int const_value = FALSE;
3133 int save_skip = cctx->ctx_skip;
3134
3135 if (next != NULL)
3136 {
3137 *arg = next_line_from_context(cctx, TRUE);
3138 p = skipwhite(*arg);
3139 }
3140
3141 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3142 {
3143 semsg(_(e_white_space_required_before_and_after_str_at_str),
3144 op_falsy ? "??" : "?", p);
3145 return FAIL;
3146 }
3147
3148 if (ppconst->pp_used == ppconst_used + 1)
3149 {
3150 // the condition is a constant, we know whether the ? or the :
3151 // expression is to be evaluated.
3152 has_const_expr = TRUE;
3153 if (op_falsy)
3154 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3155 else
3156 {
3157 int error = FALSE;
3158
3159 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3160 &error);
3161 if (error)
3162 return FAIL;
3163 }
3164 cctx->ctx_skip = save_skip == SKIP_YES ||
3165 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3166
3167 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3168 // "left ?? right" and "left" is truthy: produce "left"
3169 generate_ppconst(cctx, ppconst);
3170 else
3171 {
3172 clear_tv(&ppconst->pp_tv[ppconst_used]);
3173 --ppconst->pp_used;
3174 }
3175 }
3176 else
3177 {
3178 generate_ppconst(cctx, ppconst);
3179 if (op_falsy)
3180 end_idx = instr->ga_len;
3181 generate_JUMP(cctx, op_falsy
3182 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3183 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003184 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003185 }
3186
3187 // evaluate the second expression; any type is accepted
3188 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3189 return FAIL;
3190 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3191 return FAIL;
3192
3193 if (!has_const_expr)
3194 {
3195 generate_ppconst(cctx, ppconst);
3196
3197 if (!op_falsy)
3198 {
3199 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003200 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003201 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003202
3203 end_idx = instr->ga_len;
3204 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3205
3206 // jump here from JUMP_IF_FALSE
3207 isn = ((isn_T *)instr->ga_data) + alt_idx;
3208 isn->isn_arg.jump.jump_where = instr->ga_len;
3209 }
3210 }
3211
3212 if (!op_falsy)
3213 {
3214 // Check for the ":".
3215 p = may_peek_next_line(cctx, *arg, &next);
3216 if (*p != ':')
3217 {
3218 emsg(_(e_missing_colon_after_questionmark));
3219 return FAIL;
3220 }
3221 if (next != NULL)
3222 {
3223 *arg = next_line_from_context(cctx, TRUE);
3224 p = skipwhite(*arg);
3225 }
3226
3227 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3228 {
3229 semsg(_(e_white_space_required_before_and_after_str_at_str),
3230 ":", p);
3231 return FAIL;
3232 }
3233
3234 // evaluate the third expression
3235 if (has_const_expr)
3236 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3237 ? SKIP_YES : SKIP_NOT;
3238 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3239 return FAIL;
3240 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3241 return FAIL;
3242 }
3243
3244 if (!has_const_expr)
3245 {
3246 type_T **typep;
3247
3248 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003249 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003250
3251 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003252 typep = &((((type2_T *)stack->ga_data)
3253 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003254 common_type(type1, *typep, typep, cctx->ctx_type_list);
3255
3256 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3257 isn = ((isn_T *)instr->ga_data) + end_idx;
3258 isn->isn_arg.jump.jump_where = instr->ga_len;
3259 }
3260
3261 cctx->ctx_skip = save_skip;
3262 }
3263 return OK;
3264}
3265
3266/*
3267 * Toplevel expression.
3268 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3269 * Returns OK or FAIL.
3270 */
3271 int
3272compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3273{
3274 ppconst_T ppconst;
3275
3276 CLEAR_FIELD(ppconst);
3277 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3278 {
3279 clear_ppconst(&ppconst);
3280 return FAIL;
3281 }
3282 if (is_const != NULL)
3283 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3284 if (generate_ppconst(cctx, &ppconst) == FAIL)
3285 return FAIL;
3286 return OK;
3287}
3288
3289/*
3290 * Toplevel expression.
3291 */
3292 int
3293compile_expr0(char_u **arg, cctx_T *cctx)
3294{
3295 return compile_expr0_ext(arg, cctx, NULL);
3296}
3297
3298
3299#endif // defined(FEAT_EVAL)