blob: 18f29b6ae5b93b10bedf5fd1fca0a24593be8cad [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/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
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)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100316 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100317 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);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100332 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100333 }
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
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100356 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000357 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;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100390 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391}
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 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100445 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000446 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 }
Kota Kato948a3892022-08-16 16:09:59 +0100454 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000455 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 else
457 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100458 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 break;
460 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
461 {
462 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000463 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000464 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000465 else
466 isn_type = ISN_LOADG;
467 }
468 else
469 {
470 isn_type = ISN_LOADAUTO;
471 vim_free(name);
472 name = vim_strnsave(*arg, end - *arg);
473 if (name == NULL)
474 return FAIL;
475 }
476 break;
477 case 'w': isn_type = ISN_LOADW; break;
478 case 't': isn_type = ISN_LOADT; break;
479 case 'b': isn_type = ISN_LOADB; break;
480 default: // cannot happen, just in case
481 semsg(_(e_namespace_not_supported_str), *arg);
482 goto theend;
483 }
484 if (isn_type != ISN_DROP)
485 {
486 // Global, Buffer-local, Window-local and Tabpage-local
487 // variables can be defined later, thus we don't check if it
488 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000489 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000490 }
491 }
492 }
493 else
494 {
495 size_t len = end - *arg;
496 int idx;
497 int gen_load = FALSE;
498 int gen_load_outer = 0;
499
500 name = vim_strnsave(*arg, end - *arg);
501 if (name == NULL)
502 return FAIL;
503
504 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
505 {
506 script_autoload(name, FALSE);
507 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
508 }
509 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
510 == OK)
511 {
512 if (gen_load_outer == 0)
513 gen_load = TRUE;
514 }
515 else
516 {
517 lvar_T lvar;
518
519 if (lookup_local(*arg, len, &lvar, cctx) == OK)
520 {
521 type = lvar.lv_type;
522 idx = lvar.lv_idx;
523 if (lvar.lv_from_outer != 0)
524 gen_load_outer = lvar.lv_from_outer;
525 else
526 gen_load = TRUE;
527 }
528 else
529 {
530 // "var" can be script-local even without using "s:" if it
531 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000532 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000533 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100534 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535
536 // When evaluating an expression and the name starts with an
537 // uppercase letter it can be a user defined function.
538 // generate_funcref() will fail if the function can't be found.
539 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000540 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541 }
542 }
543 if (gen_load)
544 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
545 if (gen_load_outer > 0)
546 {
547 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
548 cctx->ctx_outer_used = TRUE;
549 }
550 }
551
552 *arg = end;
553
554theend:
555 if (res == FAIL && error && called_emsg == prev_called_emsg)
556 semsg(_(e_variable_not_found_str), name);
557 vim_free(name);
558 return res;
559}
560
561/*
562 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100563 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000564 * Returns FAIL if compilation fails.
565 */
566 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100567compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568{
LemonBoyf3b48952022-05-05 13:53:03 +0100569 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 garray_T save_ga = cctx->ctx_instr;
571 int expr_res;
572 int trailing_error;
573 int instr_count;
574 isn_T *instr = NULL;
575
576 // Remove the string type from the stack.
577 --cctx->ctx_type_stack.ga_len;
578
579 // Temporarily reset the list of instructions so that the jump labels are
580 // correct.
581 cctx->ctx_instr.ga_len = 0;
582 cctx->ctx_instr.ga_maxlen = 0;
583 cctx->ctx_instr.ga_data = NULL;
584 expr_res = compile_expr0(&s, cctx);
585 s = skipwhite(s);
586 trailing_error = *s != NUL;
587
588 if (expr_res == FAIL || trailing_error
589 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
590 {
591 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000592 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593 clear_instr_ga(&cctx->ctx_instr);
594 cctx->ctx_instr = save_ga;
595 ++cctx->ctx_type_stack.ga_len;
596 return FAIL;
597 }
598
599 // Move the generated instructions into the ISN_INSTR instruction, then
600 // restore the list of instructions.
601 instr_count = cctx->ctx_instr.ga_len;
602 instr = cctx->ctx_instr.ga_data;
603 instr[instr_count].isn_type = ISN_FINISH;
604
605 cctx->ctx_instr = save_ga;
606 vim_free(isn->isn_arg.string);
607 isn->isn_type = ISN_INSTR;
608 isn->isn_arg.instr = instr;
609 return OK;
610}
611
612/*
613 * Compile the argument expressions.
614 * "arg" points to just after the "(" and is advanced to after the ")"
615 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100616 int
LemonBoyf3b48952022-05-05 13:53:03 +0100617compile_arguments(
618 char_u **arg,
619 cctx_T *cctx,
620 int *argcount,
621 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622{
623 char_u *p = *arg;
624 char_u *whitep = *arg;
625 int must_end = FALSE;
626 int instr_count;
627
628 for (;;)
629 {
630 if (may_get_next_line(whitep, &p, cctx) == FAIL)
631 goto failret;
632 if (*p == ')')
633 {
634 *arg = p + 1;
635 return OK;
636 }
637 if (must_end)
638 {
639 semsg(_(e_missing_comma_before_argument_str), p);
640 return FAIL;
641 }
642
643 instr_count = cctx->ctx_instr.ga_len;
644 if (compile_expr0(&p, cctx) == FAIL)
645 return FAIL;
646 ++*argcount;
647
LemonBoyf3b48952022-05-05 13:53:03 +0100648 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000649 && cctx->ctx_instr.ga_len == instr_count + 1)
650 {
651 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
652
653 // {skip} argument of searchpair() can be compiled if not empty
654 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100655 compile_string(isn, cctx, 0);
656 }
657 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
658 && cctx->ctx_instr.ga_len == instr_count + 1)
659 {
660 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
661
662 // {sub} argument of substitute() can be compiled if it starts
663 // with \=
664 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
665 && isn->isn_arg.string[1] == '=')
666 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000667 }
668
669 if (*p != ',' && *skipwhite(p) == ',')
670 {
671 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
672 p = skipwhite(p);
673 }
674 if (*p == ',')
675 {
676 ++p;
677 if (*p != NUL && !VIM_ISWHITE(*p))
678 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
679 }
680 else
681 must_end = TRUE;
682 whitep = p;
683 p = skipwhite(p);
684 }
685failret:
686 emsg(_(e_missing_closing_paren));
687 return FAIL;
688}
689
690/*
691 * Compile a function call: name(arg1, arg2)
692 * "arg" points to "name", "arg + varlen" to the "(".
693 * "argcount_init" is 1 for "value->method()"
694 * Instructions:
695 * EVAL arg1
696 * EVAL arg2
697 * BCALL / DCALL / UCALL
698 */
699 static int
700compile_call(
701 char_u **arg,
702 size_t varlen,
703 cctx_T *cctx,
704 ppconst_T *ppconst,
705 int argcount_init)
706{
707 char_u *name = *arg;
708 char_u *p;
709 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100710 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000711 char_u fname_buf[FLEN_FIXED + 1];
712 char_u *tofree = NULL;
713 int error = FCERR_NONE;
714 ufunc_T *ufunc = NULL;
715 int res = FAIL;
716 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000717 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100718 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000719 imported_T *import;
720
721 if (varlen >= sizeof(namebuf))
722 {
723 semsg(_(e_name_too_long_str), name);
724 return FAIL;
725 }
726 vim_strncpy(namebuf, *arg, varlen);
727
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000728 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000729 if (import != NULL)
730 {
731 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
732 return FAIL;
733 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734
735 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100736 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100738 if ((varlen == 3
739 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000740 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
741 {
742 char_u *s = skipwhite(*arg + varlen + 1);
743 typval_T argvars[2];
744 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100745 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746
747 argvars[0].v_type = VAR_UNKNOWN;
748 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100749 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100751 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 s = skipwhite(s);
753 if (*s == ')' && argvars[0].v_type == VAR_STRING
754 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
755 || !is_has))
756 {
757 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
758
759 *arg = s + 1;
760 argvars[1].v_type = VAR_UNKNOWN;
761 tv->v_type = VAR_NUMBER;
762 tv->vval.v_number = 0;
763 if (is_has)
764 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100765 else if (is_len)
766 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 else
768 f_exists(argvars, tv);
769 clear_tv(&argvars[0]);
770 ++ppconst->pp_used;
771 return OK;
772 }
773 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100774 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775 {
776 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
777 return FAIL;
778 }
779 }
780
781 if (generate_ppconst(cctx, ppconst) == FAIL)
782 return FAIL;
783
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
785
786 // We handle the "skip" argument of searchpair() and searchpairpos()
787 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100788 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
789 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
790 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
791 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
792 special_fn = CA_SEARCHPAIR;
793 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
794 special_fn = CA_SUBSTITUTE;
795 else
796 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797
798 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100799 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000800 goto theend;
801
802 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
803 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
804 {
805 int idx;
806
807 // builtin function
808 idx = find_internal_func(name);
809 if (idx >= 0)
810 {
811 if (STRCMP(name, "flatten") == 0)
812 {
813 emsg(_(e_cannot_use_flatten_in_vim9_script));
814 goto theend;
815 }
816
817 if (STRCMP(name, "add") == 0 && argcount == 2)
818 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000819 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820
821 // add() can be compiled to instructions if we know the type
822 if (type->tt_type == VAR_LIST)
823 {
824 // inline "add(list, item)" so that the type can be checked
825 res = generate_LISTAPPEND(cctx);
826 idx = -1;
827 }
828 else if (type->tt_type == VAR_BLOB)
829 {
830 // inline "add(blob, nr)" so that the type can be checked
831 res = generate_BLOBAPPEND(cctx);
832 idx = -1;
833 }
834 }
835
836 if (idx >= 0)
837 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
838 }
839 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100840 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000841 goto theend;
842 }
843
Bram Moolenaar62aec932022-01-29 21:45:34 +0000844 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
845
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 // An argument or local variable can be a function reference, this
847 // overrules a function name.
848 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
849 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
850 {
851 // If we can find the function by name generate the right call.
852 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000853 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000854 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000856 if (!func_is_global(ufunc))
857 {
858 res = generate_CALL(cctx, ufunc, argcount);
859 goto theend;
860 }
861 if (!has_g_namespace
862 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
863 {
864 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100865 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000866 goto theend;
867 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868 }
869 }
870
871 // If the name is a variable, load it and use PCALL.
872 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000873 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000875 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
877 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000878 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879
880 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
881 goto theend;
882 }
883
884 // If we can find a global function by name generate the right call.
885 if (ufunc != NULL)
886 {
887 res = generate_CALL(cctx, ufunc, argcount);
888 goto theend;
889 }
890
891 // A global function may be defined only later. Need to figure out at
892 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000893 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894 res = generate_UCALL(cctx, name, argcount);
895 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100896 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897
898theend:
899 vim_free(tofree);
900 return res;
901}
902
903// like NAMESPACE_CHAR but with 'a' and 'l'.
904#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
905
906/*
907 * Find the end of a variable or function name. Unlike find_name_end() this
908 * does not recognize magic braces.
909 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
910 * Return a pointer to just after the name. Equal to "arg" if there is no
911 * valid name.
912 */
913 char_u *
914to_name_end(char_u *arg, int use_namespace)
915{
916 char_u *p;
917
918 // Quick check for valid starting character.
919 if (!eval_isnamec1(*arg))
920 return arg;
921
922 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
923 // Include a namespace such as "s:var" and "v:var". But "n:" is not
924 // and can be used in slice "[n:]".
925 if (*p == ':' && (p != arg + 1
926 || !use_namespace
927 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
928 break;
929 return p;
930}
931
932/*
933 * Like to_name_end() but also skip over a list or dict constant.
934 * Also accept "<SNR>123_Func".
935 * This intentionally does not handle line continuation.
936 */
937 char_u *
938to_name_const_end(char_u *arg)
939{
940 char_u *p = arg;
941 typval_T rettv;
942
943 if (STRNCMP(p, "<SNR>", 5) == 0)
944 p = skipdigits(p + 5);
945 p = to_name_end(p, TRUE);
946 if (p == arg && *arg == '[')
947 {
948
949 // Can be "[1, 2, 3]->Func()".
950 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
951 p = arg;
952 }
953 return p;
954}
955
956/*
957 * parse a list: [expr, expr]
958 * "*arg" points to the '['.
959 * ppconst->pp_is_const is set if all items are a constant.
960 */
961 static int
962compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
963{
964 char_u *p = skipwhite(*arg + 1);
965 char_u *whitep = *arg + 1;
966 int count = 0;
967 int is_const;
968 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100969 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970
971 for (;;)
972 {
973 if (may_get_next_line(whitep, &p, cctx) == FAIL)
974 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000975 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 return FAIL;
977 }
978 if (*p == ',')
979 {
980 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
981 return FAIL;
982 }
983 if (*p == ']')
984 {
985 ++p;
986 break;
987 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100988 if (must_end)
989 {
990 semsg(_(e_missing_comma_in_list_str), p);
991 return FAIL;
992 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
994 return FAIL;
995 if (!is_const)
996 is_all_const = FALSE;
997 ++count;
998 if (*p == ',')
999 {
1000 ++p;
1001 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1002 {
1003 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1004 return FAIL;
1005 }
1006 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001007 else
1008 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001009 whitep = p;
1010 p = skipwhite(p);
1011 }
1012 *arg = p;
1013
1014 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001015 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001016}
1017
1018/*
1019 * Parse a lambda: "(arg, arg) => expr"
1020 * "*arg" points to the '('.
1021 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1022 */
1023 static int
1024compile_lambda(char_u **arg, cctx_T *cctx)
1025{
1026 int r;
1027 typval_T rettv;
1028 ufunc_T *ufunc;
1029 evalarg_T evalarg;
1030
1031 init_evalarg(&evalarg);
1032 evalarg.eval_flags = EVAL_EVALUATE;
1033 evalarg.eval_cctx = cctx;
1034
1035 // Get the funcref in "rettv".
1036 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1037 if (r != OK)
1038 {
1039 clear_evalarg(&evalarg, NULL);
1040 return r;
1041 }
1042
1043 // "rettv" will now be a partial referencing the function.
1044 ufunc = rettv.vval.v_partial->pt_func;
1045 ++ufunc->uf_refcount;
1046 clear_tv(&rettv);
1047
1048 // Compile it here to get the return type. The return type is optional,
1049 // when it's missing use t_unknown. This is recognized in
1050 // compile_return().
1051 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1052 ufunc->uf_ret_type = &t_unknown;
1053 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1054
1055 // When the outer function is compiled for profiling or debugging, the
1056 // lambda may be called without profiling or debugging. Compile it here in
1057 // the right context.
1058 if (cctx->ctx_compile_type == CT_DEBUG
1059#ifdef FEAT_PROFILE
1060 || cctx->ctx_compile_type == CT_PROFILE
1061#endif
1062 )
1063 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1064
Bram Moolenaar139575d2022-03-15 19:29:30 +00001065 // if the outer function is not compiled for debugging or profiling, this
1066 // one might be
1067 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001068 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001069 compiletype_T compile_type = get_compile_type(ufunc);
1070
1071 if (compile_type != CT_NONE)
1072 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001073 }
1074
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001075 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1076 // "*arg" may point into it. Point into the original line to avoid a
1077 // dangling pointer.
1078 if (evalarg.eval_using_cmdline)
1079 {
1080 garray_T *gap = &evalarg.eval_tofree_ga;
1081 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1082
1083 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1084 + off;
1085 }
1086
1087 clear_evalarg(&evalarg, NULL);
1088
1089 if (ufunc->uf_def_status == UF_COMPILED)
1090 {
1091 // The return type will now be known.
1092 set_function_type(ufunc);
1093
1094 // The function reference count will be 1. When the ISN_FUNCREF
1095 // instruction is deleted the reference count is decremented and the
1096 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001097 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001098 }
1099
1100 func_ptr_unref(ufunc);
1101 return FAIL;
1102}
1103
1104/*
1105 * Get a lambda and compile it. Uses Vim9 syntax.
1106 */
1107 int
1108get_lambda_tv_and_compile(
1109 char_u **arg,
1110 typval_T *rettv,
1111 int types_optional,
1112 evalarg_T *evalarg)
1113{
1114 int r;
1115 ufunc_T *ufunc;
1116 int save_sc_version = current_sctx.sc_version;
1117
1118 // Get the funcref in "rettv".
1119 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1120 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1121 current_sctx.sc_version = save_sc_version;
1122 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001123 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124
1125 // "rettv" will now be a partial referencing the function.
1126 ufunc = rettv->vval.v_partial->pt_func;
1127
1128 // Compile it here to get the return type. The return type is optional,
1129 // when it's missing use t_unknown. This is recognized in
1130 // compile_return().
1131 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1132 ufunc->uf_ret_type = &t_unknown;
1133 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1134
1135 if (ufunc->uf_def_status == UF_COMPILED)
1136 {
1137 // The return type will now be known.
1138 set_function_type(ufunc);
1139 return OK;
1140 }
1141 clear_tv(rettv);
1142 return FAIL;
1143}
1144
1145/*
1146 * parse a dict: {key: val, [key]: val}
1147 * "*arg" points to the '{'.
1148 * ppconst->pp_is_const is set if all item values are a constant.
1149 */
1150 static int
1151compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1152{
1153 garray_T *instr = &cctx->ctx_instr;
1154 int count = 0;
1155 dict_T *d = dict_alloc();
1156 dictitem_T *item;
1157 char_u *whitep = *arg + 1;
1158 char_u *p;
1159 int is_const;
1160 int is_all_const = TRUE; // reset when non-const encountered
1161
1162 if (d == NULL)
1163 return FAIL;
1164 if (generate_ppconst(cctx, ppconst) == FAIL)
1165 return FAIL;
1166 for (;;)
1167 {
1168 char_u *key = NULL;
1169
1170 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1171 {
1172 *arg = NULL;
1173 goto failret;
1174 }
1175
1176 if (**arg == '}')
1177 break;
1178
1179 if (**arg == '[')
1180 {
1181 isn_T *isn;
1182
1183 // {[expr]: value} uses an evaluated key.
1184 *arg = skipwhite(*arg + 1);
1185 if (compile_expr0(arg, cctx) == FAIL)
1186 return FAIL;
1187 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1188 if (isn->isn_type == ISN_PUSHNR)
1189 {
1190 char buf[NUMBUFLEN];
1191
1192 // Convert to string at compile time.
1193 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1194 isn->isn_type = ISN_PUSHS;
1195 isn->isn_arg.string = vim_strsave((char_u *)buf);
1196 }
1197 if (isn->isn_type == ISN_PUSHS)
1198 key = isn->isn_arg.string;
1199 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1200 return FAIL;
1201 *arg = skipwhite(*arg);
1202 if (**arg != ']')
1203 {
1204 emsg(_(e_missing_matching_bracket_after_dict_key));
1205 return FAIL;
1206 }
1207 ++*arg;
1208 }
1209 else
1210 {
1211 // {"name": value},
1212 // {'name': value},
1213 // {name: value} use "name" as a literal key
1214 key = get_literal_key(arg);
1215 if (key == NULL)
1216 return FAIL;
1217 if (generate_PUSHS(cctx, &key) == FAIL)
1218 return FAIL;
1219 }
1220
1221 // Check for duplicate keys, if using string keys.
1222 if (key != NULL)
1223 {
1224 item = dict_find(d, key, -1);
1225 if (item != NULL)
1226 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001227 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228 goto failret;
1229 }
1230 item = dictitem_alloc(key);
1231 if (item != NULL)
1232 {
1233 item->di_tv.v_type = VAR_UNKNOWN;
1234 item->di_tv.v_lock = 0;
1235 if (dict_add(d, item) == FAIL)
1236 dictitem_free(item);
1237 }
1238 }
1239
1240 if (**arg != ':')
1241 {
1242 if (*skipwhite(*arg) == ':')
1243 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1244 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001245 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 return FAIL;
1247 }
1248 whitep = *arg + 1;
1249 if (!IS_WHITE_OR_NUL(*whitep))
1250 {
1251 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1252 return FAIL;
1253 }
1254
1255 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1256 {
1257 *arg = NULL;
1258 goto failret;
1259 }
1260
1261 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1262 return FAIL;
1263 if (!is_const)
1264 is_all_const = FALSE;
1265 ++count;
1266
1267 whitep = *arg;
1268 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1269 {
1270 *arg = NULL;
1271 goto failret;
1272 }
1273 if (**arg == '}')
1274 break;
1275 if (**arg != ',')
1276 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001277 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278 goto failret;
1279 }
1280 if (IS_WHITE_OR_NUL(*whitep))
1281 {
1282 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1283 return FAIL;
1284 }
1285 whitep = *arg + 1;
1286 if (!IS_WHITE_OR_NUL(*whitep))
1287 {
1288 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1289 return FAIL;
1290 }
1291 *arg = skipwhite(whitep);
1292 }
1293
1294 *arg = *arg + 1;
1295
1296 // Allow for following comment, after at least one space.
1297 p = skipwhite(*arg);
1298 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1299 *arg += STRLEN(*arg);
1300
1301 dict_unref(d);
1302 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001303 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304
1305failret:
1306 if (*arg == NULL)
1307 {
1308 semsg(_(e_missing_dict_end), _("[end of lines]"));
1309 *arg = (char_u *)"";
1310 }
1311 dict_unref(d);
1312 return FAIL;
1313}
1314
1315/*
1316 * Compile "&option".
1317 */
1318 static int
1319compile_get_option(char_u **arg, cctx_T *cctx)
1320{
1321 typval_T rettv;
1322 char_u *start = *arg;
1323 int ret;
1324
1325 // parse the option and get the current value to get the type.
1326 rettv.v_type = VAR_UNKNOWN;
1327 ret = eval_option(arg, &rettv, TRUE);
1328 if (ret == OK)
1329 {
1330 // include the '&' in the name, eval_option() expects it.
1331 char_u *name = vim_strnsave(start, *arg - start);
1332 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1333 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1334
1335 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1336 vim_free(name);
1337 }
1338 clear_tv(&rettv);
1339
1340 return ret;
1341}
1342
1343/*
1344 * Compile "$VAR".
1345 */
1346 static int
1347compile_get_env(char_u **arg, cctx_T *cctx)
1348{
1349 char_u *start = *arg;
1350 int len;
1351 int ret;
1352 char_u *name;
1353
1354 ++*arg;
1355 len = get_env_len(arg);
1356 if (len == 0)
1357 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001358 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001359 return FAIL;
1360 }
1361
1362 // include the '$' in the name, eval_env_var() expects it.
1363 name = vim_strnsave(start, len + 1);
1364 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1365 vim_free(name);
1366 return ret;
1367}
1368
1369/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001370 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001371 */
1372 static int
1373compile_interp_string(char_u **arg, cctx_T *cctx)
1374{
1375 typval_T tv;
1376 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001377 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001378 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001379 int count = 0;
1380 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001381
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001382 // *arg is on the '$' character, move it to the first string character.
1383 ++*arg;
1384 quote = **arg;
1385 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001386
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001387 for (;;)
1388 {
1389 // Get the string up to the matching quote or to a single '{'.
1390 // "arg" is advanced to either the quote or the '{'.
1391 if (quote == '"')
1392 ret = eval_string(arg, &tv, evaluate, TRUE);
1393 else
1394 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1395 if (ret == FAIL)
1396 break;
1397 if (evaluate)
1398 {
1399 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1400 || (**arg != '{' && count == 0))
1401 {
1402 // generate non-empty string or empty string if it's the only
1403 // one
1404 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1405 return FAIL;
1406 tv.vval.v_string = NULL; // don't free it now
1407 ++count;
1408 }
1409 clear_tv(&tv);
1410 }
1411
1412 if (**arg != '{')
1413 {
1414 // found terminating quote
1415 ++*arg;
1416 break;
1417 }
1418
1419 p = compile_one_expr_in_str(*arg, cctx);
1420 if (p == NULL)
1421 {
1422 ret = FAIL;
1423 break;
1424 }
1425 ++count;
1426 *arg = p;
1427 }
LemonBoy2eaef102022-05-06 13:14:50 +01001428
1429 if (ret == FAIL || !evaluate)
1430 return ret;
1431
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001432 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1433 if (count > 1)
1434 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001435
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001436 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001437}
1438
1439/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 * Compile "@r".
1441 */
1442 static int
1443compile_get_register(char_u **arg, cctx_T *cctx)
1444{
1445 int ret;
1446
1447 ++*arg;
1448 if (**arg == NUL)
1449 {
1450 semsg(_(e_syntax_error_at_str), *arg - 1);
1451 return FAIL;
1452 }
1453 if (!valid_yank_reg(**arg, FALSE))
1454 {
1455 emsg_invreg(**arg);
1456 return FAIL;
1457 }
1458 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1459 ++*arg;
1460 return ret;
1461}
1462
1463/*
1464 * Apply leading '!', '-' and '+' to constant "rettv".
1465 * When "numeric_only" is TRUE do not apply '!'.
1466 */
1467 static int
1468apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1469{
1470 char_u *p = *end;
1471
1472 // this works from end to start
1473 while (p > start)
1474 {
1475 --p;
1476 if (*p == '-' || *p == '+')
1477 {
1478 // only '-' has an effect, for '+' we only check the type
1479#ifdef FEAT_FLOAT
1480 if (rettv->v_type == VAR_FLOAT)
1481 {
1482 if (*p == '-')
1483 rettv->vval.v_float = -rettv->vval.v_float;
1484 }
1485 else
1486#endif
1487 {
1488 varnumber_T val;
1489 int error = FALSE;
1490
1491 // tv_get_number_chk() accepts a string, but we don't want that
1492 // here
1493 if (check_not_string(rettv) == FAIL)
1494 return FAIL;
1495 val = tv_get_number_chk(rettv, &error);
1496 clear_tv(rettv);
1497 if (error)
1498 return FAIL;
1499 if (*p == '-')
1500 val = -val;
1501 rettv->v_type = VAR_NUMBER;
1502 rettv->vval.v_number = val;
1503 }
1504 }
1505 else if (numeric_only)
1506 {
1507 ++p;
1508 break;
1509 }
1510 else if (*p == '!')
1511 {
1512 int v = tv2bool(rettv);
1513
1514 // '!' is permissive in the type.
1515 clear_tv(rettv);
1516 rettv->v_type = VAR_BOOL;
1517 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1518 }
1519 }
1520 *end = p;
1521 return OK;
1522}
1523
1524/*
1525 * Recognize v: variables that are constants and set "rettv".
1526 */
1527 static void
1528get_vim_constant(char_u **arg, typval_T *rettv)
1529{
1530 if (STRNCMP(*arg, "v:true", 6) == 0)
1531 {
1532 rettv->v_type = VAR_BOOL;
1533 rettv->vval.v_number = VVAL_TRUE;
1534 *arg += 6;
1535 }
1536 else if (STRNCMP(*arg, "v:false", 7) == 0)
1537 {
1538 rettv->v_type = VAR_BOOL;
1539 rettv->vval.v_number = VVAL_FALSE;
1540 *arg += 7;
1541 }
1542 else if (STRNCMP(*arg, "v:null", 6) == 0)
1543 {
1544 rettv->v_type = VAR_SPECIAL;
1545 rettv->vval.v_number = VVAL_NULL;
1546 *arg += 6;
1547 }
1548 else if (STRNCMP(*arg, "v:none", 6) == 0)
1549 {
1550 rettv->v_type = VAR_SPECIAL;
1551 rettv->vval.v_number = VVAL_NONE;
1552 *arg += 6;
1553 }
1554}
1555
1556 exprtype_T
1557get_compare_type(char_u *p, int *len, int *type_is)
1558{
1559 exprtype_T type = EXPR_UNKNOWN;
1560 int i;
1561
1562 switch (p[0])
1563 {
1564 case '=': if (p[1] == '=')
1565 type = EXPR_EQUAL;
1566 else if (p[1] == '~')
1567 type = EXPR_MATCH;
1568 break;
1569 case '!': if (p[1] == '=')
1570 type = EXPR_NEQUAL;
1571 else if (p[1] == '~')
1572 type = EXPR_NOMATCH;
1573 break;
1574 case '>': if (p[1] != '=')
1575 {
1576 type = EXPR_GREATER;
1577 *len = 1;
1578 }
1579 else
1580 type = EXPR_GEQUAL;
1581 break;
1582 case '<': if (p[1] != '=')
1583 {
1584 type = EXPR_SMALLER;
1585 *len = 1;
1586 }
1587 else
1588 type = EXPR_SEQUAL;
1589 break;
1590 case 'i': if (p[1] == 's')
1591 {
1592 // "is" and "isnot"; but not a prefix of a name
1593 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1594 *len = 5;
1595 i = p[*len];
1596 if (!isalnum(i) && i != '_')
1597 {
1598 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1599 *type_is = TRUE;
1600 }
1601 }
1602 break;
1603 }
1604 return type;
1605}
1606
1607/*
1608 * Skip over an expression, ignoring most errors.
1609 */
1610 void
1611skip_expr_cctx(char_u **arg, cctx_T *cctx)
1612{
1613 evalarg_T evalarg;
1614
1615 init_evalarg(&evalarg);
1616 evalarg.eval_cctx = cctx;
1617 skip_expr(arg, &evalarg);
1618 clear_evalarg(&evalarg, NULL);
1619}
1620
1621/*
1622 * Check that the top of the type stack has a type that can be used as a
1623 * condition. Give an error and return FAIL if not.
1624 */
1625 int
1626bool_on_stack(cctx_T *cctx)
1627{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628 type_T *type;
1629
Bram Moolenaar078a4612022-01-04 15:17:03 +00001630 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001631 if (type == &t_bool)
1632 return OK;
1633
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001634 if (type == &t_any
1635 || type == &t_unknown
1636 || type == &t_number
1637 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1639 // This requires a runtime type check.
1640 return generate_COND2BOOL(cctx);
1641
1642 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1643}
1644
1645/*
1646 * Give the "white on both sides" error, taking the operator from "p[len]".
1647 */
1648 void
1649error_white_both(char_u *op, int len)
1650{
1651 char_u buf[10];
1652
1653 vim_strncpy(buf, op, len);
1654 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1655}
1656
1657/*
1658 * Compile code to apply '-', '+' and '!'.
1659 * When "numeric_only" is TRUE do not apply '!'.
1660 */
1661 static int
1662compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1663{
1664 char_u *p = *end;
1665
1666 // this works from end to start
1667 while (p > start)
1668 {
1669 --p;
1670 while (VIM_ISWHITE(*p))
1671 --p;
1672 if (*p == '-' || *p == '+')
1673 {
1674 int negate = *p == '-';
1675 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676 type_T *type;
1677
Bram Moolenaar078a4612022-01-04 15:17:03 +00001678 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001679 if (type != &t_float && need_type(type, &t_number,
1680 -1, 0, cctx, FALSE, FALSE) == FAIL)
1681 return FAIL;
1682
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001683 // only '-' has an effect, for '+' we only check the type
1684 if (negate)
1685 {
1686 isn = generate_instr(cctx, ISN_NEGATENR);
1687 if (isn == NULL)
1688 return FAIL;
1689 }
1690 }
1691 else if (numeric_only)
1692 {
1693 ++p;
1694 break;
1695 }
1696 else
1697 {
1698 int invert = *p == '!';
1699
1700 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1701 {
1702 if (p[-1] == '!')
1703 invert = !invert;
1704 --p;
1705 }
1706 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1707 return FAIL;
1708 }
1709 }
1710 *end = p;
1711 return OK;
1712}
1713
1714/*
1715 * Compile "(expression)": recursive!
1716 * Return FAIL/OK.
1717 */
1718 static int
1719compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1720{
1721 int ret;
1722 char_u *p = *arg + 1;
1723
1724 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1725 return FAIL;
1726 if (ppconst->pp_used <= PPSIZE - 10)
1727 {
1728 ret = compile_expr1(arg, cctx, ppconst);
1729 }
1730 else
1731 {
1732 // Not enough space in ppconst, flush constants.
1733 if (generate_ppconst(cctx, ppconst) == FAIL)
1734 return FAIL;
1735 ret = compile_expr0(arg, cctx);
1736 }
1737 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1738 return FAIL;
1739 if (**arg == ')')
1740 ++*arg;
1741 else if (ret == OK)
1742 {
1743 emsg(_(e_missing_closing_paren));
1744 ret = FAIL;
1745 }
1746 return ret;
1747}
1748
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001749static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001750
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001751/*
1752 * Compile whatever comes after "name" or "name()".
1753 * Advances "*arg" only when something was recognized.
1754 */
1755 static int
1756compile_subscript(
1757 char_u **arg,
1758 cctx_T *cctx,
1759 char_u *start_leader,
1760 char_u **end_leader,
1761 ppconst_T *ppconst)
1762{
1763 char_u *name_start = *end_leader;
1764 int keeping_dict = FALSE;
1765
1766 for (;;)
1767 {
1768 char_u *p = skipwhite(*arg);
1769
1770 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1771 {
1772 char_u *next = peek_next_line_from_context(cctx);
1773
1774 // If a following line starts with "->{" or "->X" advance to that
1775 // line, so that a line break before "->" is allowed.
1776 // Also if a following line starts with ".x".
1777 if (next != NULL &&
1778 ((next[0] == '-' && next[1] == '>'
1779 && (next[2] == '{'
1780 || ASCII_ISALPHA(*skipwhite(next + 2))))
1781 || (next[0] == '.' && eval_isdictc(next[1]))))
1782 {
1783 next = next_line_from_context(cctx, TRUE);
1784 if (next == NULL)
1785 return FAIL;
1786 *arg = next;
1787 p = skipwhite(*arg);
1788 }
1789 }
1790
1791 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1792 // is not a function call.
1793 if (**arg == '(')
1794 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001795 type_T *type;
1796 int argcount = 0;
1797
1798 if (generate_ppconst(cctx, ppconst) == FAIL)
1799 return FAIL;
1800 ppconst->pp_is_const = FALSE;
1801
1802 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001803 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804
1805 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001806 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001807 return FAIL;
1808 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1809 return FAIL;
1810 if (keeping_dict)
1811 {
1812 keeping_dict = FALSE;
1813 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1814 return FAIL;
1815 }
1816 }
1817 else if (*p == '-' && p[1] == '>')
1818 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001819 char_u *pstart = p;
1820 int alt;
1821 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822
Bram Moolenaarc7349932022-01-16 20:59:39 +00001823 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001824 if (generate_ppconst(cctx, ppconst) == FAIL)
1825 return FAIL;
1826 ppconst->pp_is_const = FALSE;
1827
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001828 // Apply the '!', '-' and '+' first:
1829 // -1.0->func() works like (-1.0)->func()
1830 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1831 return FAIL;
1832
1833 p += 2;
1834 *arg = skipwhite(p);
1835 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001836
1837 // Three alternatives handled here:
1838 // 1. "base->name(" only a name, use compile_call()
1839 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1840 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001842 alt = 2;
1843 else
1844 {
1845 // alternative 1 or 3
1846 p = *arg;
1847 if (!eval_isnamec1(*p))
1848 {
1849 semsg(_(e_trailing_characters_str), pstart);
1850 return FAIL;
1851 }
1852 if (ASCII_ISALPHA(*p) && p[1] == ':')
1853 p += 2;
1854 for ( ; eval_isnamec(*p); ++p)
1855 ;
1856 if (*p == '(')
1857 {
1858 // alternative 1
1859 alt = 1;
1860 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1861 return FAIL;
1862 }
1863 else
1864 {
1865 // Must be alternative 3, find the "(". Only works within
1866 // one line.
1867 alt = 3;
1868 paren = vim_strchr(p, '(');
1869 if (paren == NULL)
1870 {
1871 semsg(_(e_missing_parenthesis_str), *arg);
1872 return FAIL;
1873 }
1874 }
1875 }
1876
1877 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001878 {
1879 int argcount = 1;
1880 garray_T *stack = &cctx->ctx_type_stack;
1881 int type_idx_start = stack->ga_len;
1882 type_T *type;
1883 int expr_isn_start = cctx->ctx_instr.ga_len;
1884 int expr_isn_end;
1885 int arg_isn_count;
1886
Bram Moolenaarc7349932022-01-16 20:59:39 +00001887 if (alt == 2)
1888 {
1889 // Funcref call: list->(Refs[2])(arg)
1890 // or lambda: list->((arg) => expr)(arg)
1891 //
1892 // Fist compile the function expression.
1893 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1894 return FAIL;
1895 }
1896 else
1897 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001898 int fail;
1899 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001900 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001901
Bram Moolenaarc7349932022-01-16 20:59:39 +00001902 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001903
1904 // instead of using LOADG for "import.Func" use PUSHFUNC
1905 ++paren_follows_after_expr;
1906
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001907 // do not look in the next line
1908 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001909
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001910 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001911 || *skipwhite(*arg) != NUL;
1912 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001913 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001914 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001915
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001916 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001917 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001918 if (did_emsg == prev_did_emsg)
1919 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001920 return FAIL;
1921 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001922 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924 // Compile the arguments.
1925 if (**arg != '(')
1926 {
1927 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001928 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001929 else
1930 semsg(_(e_missing_parenthesis_str), *arg);
1931 return FAIL;
1932 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001933
1934 // Remember the next instruction index, where the instructions
1935 // for arguments are being written.
1936 expr_isn_end = cctx->ctx_instr.ga_len;
1937
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001939 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1940 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 return FAIL;
1942
1943 // Move the instructions for the arguments to before the
1944 // instructions of the expression and move the type of the
1945 // expression after the argument types. This is what ISN_PCALL
1946 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1948 if (arg_isn_count > 0)
1949 {
1950 int expr_isn_count = expr_isn_end - expr_isn_start;
1951 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001952 type_T *decl_type;
1953 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001954
1955 if (isn == NULL)
1956 return FAIL;
1957 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1958 + expr_isn_start,
1959 sizeof(isn_T) * expr_isn_count);
1960 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1961 + expr_isn_start,
1962 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1963 sizeof(isn_T) * arg_isn_count);
1964 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1965 + expr_isn_start + arg_isn_count,
1966 isn, sizeof(isn_T) * expr_isn_count);
1967 vim_free(isn);
1968
Bram Moolenaar078a4612022-01-04 15:17:03 +00001969 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1970 type = typep->type_curr;
1971 decl_type = typep->type_decl;
1972 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1973 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1974 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001975 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001976 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1977 typep->type_curr = type;
1978 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001979 }
1980
Bram Moolenaar078a4612022-01-04 15:17:03 +00001981 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1983 return FAIL;
1984 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001985
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001986 if (keeping_dict)
1987 {
1988 keeping_dict = FALSE;
1989 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1990 return FAIL;
1991 }
1992 }
1993 else if (**arg == '[')
1994 {
1995 int is_slice = FALSE;
1996
1997 // list index: list[123]
1998 // dict member: dict[key]
1999 // string index: text[123]
2000 // blob index: blob[123]
2001 if (generate_ppconst(cctx, ppconst) == FAIL)
2002 return FAIL;
2003 ppconst->pp_is_const = FALSE;
2004
2005 ++p;
2006 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2007 return FAIL;
2008 if (**arg == ':')
2009 {
2010 // missing first index is equal to zero
2011 generate_PUSHNR(cctx, 0);
2012 }
2013 else
2014 {
2015 if (compile_expr0(arg, cctx) == FAIL)
2016 return FAIL;
2017 if (**arg == ':')
2018 {
2019 semsg(_(e_white_space_required_before_and_after_str_at_str),
2020 ":", *arg);
2021 return FAIL;
2022 }
2023 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2024 return FAIL;
2025 *arg = skipwhite(*arg);
2026 }
2027 if (**arg == ':')
2028 {
2029 is_slice = TRUE;
2030 ++*arg;
2031 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2032 {
2033 semsg(_(e_white_space_required_before_and_after_str_at_str),
2034 ":", *arg);
2035 return FAIL;
2036 }
2037 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2038 return FAIL;
2039 if (**arg == ']')
2040 // missing second index is equal to end of string
2041 generate_PUSHNR(cctx, -1);
2042 else
2043 {
2044 if (compile_expr0(arg, cctx) == FAIL)
2045 return FAIL;
2046 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2047 return FAIL;
2048 *arg = skipwhite(*arg);
2049 }
2050 }
2051
2052 if (**arg != ']')
2053 {
2054 emsg(_(e_missing_closing_square_brace));
2055 return FAIL;
2056 }
2057 *arg = *arg + 1;
2058
2059 if (keeping_dict)
2060 {
2061 keeping_dict = FALSE;
2062 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2063 return FAIL;
2064 }
2065 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2066 return FAIL;
2067 }
2068 else if (*p == '.' && p[1] != '.')
2069 {
2070 // dictionary member: dict.name
2071 if (generate_ppconst(cctx, ppconst) == FAIL)
2072 return FAIL;
2073 ppconst->pp_is_const = FALSE;
2074
2075 *arg = p + 1;
2076 if (IS_WHITE_OR_NUL(**arg))
2077 {
2078 emsg(_(e_missing_name_after_dot));
2079 return FAIL;
2080 }
2081 p = *arg;
2082 if (eval_isdictc(*p))
2083 while (eval_isnamec(*p))
2084 MB_PTR_ADV(p);
2085 if (p == *arg)
2086 {
2087 semsg(_(e_syntax_error_at_str), *arg);
2088 return FAIL;
2089 }
2090 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2091 return FAIL;
2092 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2093 return FAIL;
2094 keeping_dict = TRUE;
2095 *arg = p;
2096 }
2097 else
2098 break;
2099 }
2100
2101 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2102 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002103 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2104 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002105 return FAIL;
2106
2107 return OK;
2108}
2109
2110/*
2111 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2112 * "arg" is advanced until after the expression, skipping white space.
2113 *
2114 * If the value is a constant "ppconst->pp_used" will be non-zero.
2115 * Before instructions are generated, any values in "ppconst" will generated.
2116 *
2117 * This is the compiling equivalent of eval1(), eval2(), etc.
2118 */
2119
2120/*
2121 * number number constant
2122 * 0zFFFFFFFF Blob constant
2123 * "string" string constant
2124 * 'string' literal string constant
2125 * &option-name option value
2126 * @r register contents
2127 * identifier variable value
2128 * function() function call
2129 * $VAR environment variable
2130 * (expression) nested expression
2131 * [expr, expr] List
2132 * {key: val, [key]: val} Dictionary
2133 *
2134 * Also handle:
2135 * ! in front logical NOT
2136 * - in front unary minus
2137 * + in front unary plus (ignored)
2138 * trailing (arg) funcref/partial call
2139 * trailing [] subscript in String or List
2140 * trailing .name entry in Dictionary
2141 * trailing ->name() method call
2142 */
2143 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002144compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002145 char_u **arg,
2146 cctx_T *cctx,
2147 ppconst_T *ppconst)
2148{
2149 char_u *start_leader, *end_leader;
2150 int ret = OK;
2151 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2152 int used_before = ppconst->pp_used;
2153
2154 ppconst->pp_is_const = FALSE;
2155
2156 /*
2157 * Skip '!', '-' and '+' characters. They are handled later.
2158 */
2159 start_leader = *arg;
2160 if (eval_leader(arg, TRUE) == FAIL)
2161 return FAIL;
2162 end_leader = *arg;
2163
2164 rettv->v_type = VAR_UNKNOWN;
2165 switch (**arg)
2166 {
2167 /*
2168 * Number constant.
2169 */
2170 case '0': // also for blob starting with 0z
2171 case '1':
2172 case '2':
2173 case '3':
2174 case '4':
2175 case '5':
2176 case '6':
2177 case '7':
2178 case '8':
2179 case '9':
2180 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2181 return FAIL;
2182 // Apply "-" and "+" just before the number now, right to
2183 // left. Matters especially when "->" follows. Stops at
2184 // '!'.
2185 if (apply_leader(rettv, TRUE,
2186 start_leader, &end_leader) == FAIL)
2187 {
2188 clear_tv(rettv);
2189 return FAIL;
2190 }
2191 break;
2192
2193 /*
2194 * String constant: "string".
2195 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002196 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002197 return FAIL;
2198 break;
2199
2200 /*
2201 * Literal string constant: 'str''ing'.
2202 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002203 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204 return FAIL;
2205 break;
2206
2207 /*
2208 * Constant Vim variable.
2209 */
2210 case 'v': get_vim_constant(arg, rettv);
2211 ret = NOTDONE;
2212 break;
2213
2214 /*
2215 * "true" constant
2216 */
2217 case 't': if (STRNCMP(*arg, "true", 4) == 0
2218 && !eval_isnamec((*arg)[4]))
2219 {
2220 *arg += 4;
2221 rettv->v_type = VAR_BOOL;
2222 rettv->vval.v_number = VVAL_TRUE;
2223 }
2224 else
2225 ret = NOTDONE;
2226 break;
2227
2228 /*
2229 * "false" constant
2230 */
2231 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2232 && !eval_isnamec((*arg)[5]))
2233 {
2234 *arg += 5;
2235 rettv->v_type = VAR_BOOL;
2236 rettv->vval.v_number = VVAL_FALSE;
2237 }
2238 else
2239 ret = NOTDONE;
2240 break;
2241
2242 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002243 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002245 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002246 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002247 char_u *p = *arg + 4;
2248 int len;
2249
2250 for (len = 0; eval_isnamec(p[len]); ++len)
2251 ;
2252 ret = handle_predefined(*arg, len + 4, rettv);
2253 if (ret == FAIL)
2254 ret = NOTDONE;
2255 else
2256 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 }
2258 else
2259 ret = NOTDONE;
2260 break;
2261
2262 /*
2263 * List: [expr, expr]
2264 */
2265 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2266 return FAIL;
2267 ret = compile_list(arg, cctx, ppconst);
2268 break;
2269
2270 /*
2271 * Dictionary: {'key': val, 'key': val}
2272 */
2273 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2274 return FAIL;
2275 ret = compile_dict(arg, cctx, ppconst);
2276 break;
2277
2278 /*
2279 * Option value: &name
2280 */
2281 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2282 return FAIL;
2283 ret = compile_get_option(arg, cctx);
2284 break;
2285
2286 /*
2287 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002288 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 */
2290 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2291 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002292 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2293 ret = compile_interp_string(arg, cctx);
2294 else
2295 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002296 break;
2297
2298 /*
2299 * Register contents: @r.
2300 */
2301 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2302 return FAIL;
2303 ret = compile_get_register(arg, cctx);
2304 break;
2305 /*
2306 * nested expression: (expression).
2307 * lambda: (arg, arg) => expr
2308 * funcref: (arg, arg) => { statement }
2309 */
2310 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2311 ret = compile_lambda(arg, cctx);
2312 if (ret == NOTDONE)
2313 ret = compile_parenthesis(arg, cctx, ppconst);
2314 break;
2315
2316 default: ret = NOTDONE;
2317 break;
2318 }
2319 if (ret == FAIL)
2320 return FAIL;
2321
2322 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2323 {
2324 if (cctx->ctx_skip == SKIP_YES)
2325 clear_tv(rettv);
2326 else
2327 // A constant expression can possibly be handled compile time,
2328 // return the value instead of generating code.
2329 ++ppconst->pp_used;
2330 }
2331 else if (ret == NOTDONE)
2332 {
2333 char_u *p;
2334 int r;
2335
2336 if (!eval_isnamec1(**arg))
2337 {
2338 if (!vim9_bad_comment(*arg))
2339 {
2340 if (ends_excmd(*skipwhite(*arg)))
2341 semsg(_(e_empty_expression_str), *arg);
2342 else
2343 semsg(_(e_name_expected_str), *arg);
2344 }
2345 return FAIL;
2346 }
2347
2348 // "name" or "name()"
2349 p = to_name_end(*arg, TRUE);
2350 if (p - *arg == (size_t)1 && **arg == '_')
2351 {
2352 emsg(_(e_cannot_use_underscore_here));
2353 return FAIL;
2354 }
2355
2356 if (*p == '(')
2357 {
2358 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2359 }
2360 else
2361 {
2362 if (cctx->ctx_skip != SKIP_YES
2363 && generate_ppconst(cctx, ppconst) == FAIL)
2364 return FAIL;
2365 r = compile_load(arg, p, cctx, TRUE, TRUE);
2366 }
2367 if (r == FAIL)
2368 return FAIL;
2369 }
2370
2371 // Handle following "[]", ".member", etc.
2372 // Then deal with prefixed '-', '+' and '!', if not done already.
2373 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2374 ppconst) == FAIL)
2375 return FAIL;
2376 if (ppconst->pp_used > 0)
2377 {
2378 // apply the '!', '-' and '+' before the constant
2379 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2380 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2381 return FAIL;
2382 return OK;
2383 }
2384 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2385 return FAIL;
2386 return OK;
2387}
2388
2389/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002390 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 */
2392 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002393compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394{
2395 type_T *want_type = NULL;
2396
2397 // Recognize <type>
2398 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2399 {
2400 ++*arg;
2401 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2402 if (want_type == NULL)
2403 return FAIL;
2404
2405 if (**arg != '>')
2406 {
2407 if (*skipwhite(*arg) == '>')
2408 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2409 else
2410 emsg(_(e_missing_gt));
2411 return FAIL;
2412 }
2413 ++*arg;
2414 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2415 return FAIL;
2416 }
2417
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002418 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002419 return FAIL;
2420
2421 if (want_type != NULL)
2422 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 type_T *actual;
2424 where_T where = WHERE_INIT;
2425
2426 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002427 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002428 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002430 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2431 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 return FAIL;
2433 }
2434 }
2435
2436 return OK;
2437}
2438
2439/*
2440 * * number multiplication
2441 * / number division
2442 * % number modulo
2443 */
2444 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002445compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446{
2447 char_u *op;
2448 char_u *next;
2449 int ppconst_used = ppconst->pp_used;
2450
2451 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002452 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 return FAIL;
2454
2455 /*
2456 * Repeat computing, until no "*", "/" or "%" is following.
2457 */
2458 for (;;)
2459 {
2460 op = may_peek_next_line(cctx, *arg, &next);
2461 if (*op != '*' && *op != '/' && *op != '%')
2462 break;
2463 if (next != NULL)
2464 {
2465 *arg = next_line_from_context(cctx, TRUE);
2466 op = skipwhite(*arg);
2467 }
2468
2469 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2470 {
2471 error_white_both(op, 1);
2472 return FAIL;
2473 }
2474 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2475 return FAIL;
2476
2477 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002478 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479 return FAIL;
2480
2481 if (ppconst->pp_used == ppconst_used + 2
2482 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2483 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2484 {
2485 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2486 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2487 varnumber_T res = 0;
2488 int failed = FALSE;
2489
2490 // both are numbers: compute the result
2491 switch (*op)
2492 {
2493 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2494 break;
2495 case '/': res = num_divide(tv1->vval.v_number,
2496 tv2->vval.v_number, &failed);
2497 break;
2498 case '%': res = num_modulus(tv1->vval.v_number,
2499 tv2->vval.v_number, &failed);
2500 break;
2501 }
2502 if (failed)
2503 return FAIL;
2504 tv1->vval.v_number = res;
2505 --ppconst->pp_used;
2506 }
2507 else
2508 {
2509 generate_ppconst(cctx, ppconst);
2510 generate_two_op(cctx, op);
2511 }
2512 }
2513
2514 return OK;
2515}
2516
2517/*
2518 * + number addition or list/blobl concatenation
2519 * - number subtraction
2520 * .. string concatenation
2521 */
2522 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002523compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524{
2525 char_u *op;
2526 char_u *next;
2527 int oplen;
2528 int ppconst_used = ppconst->pp_used;
2529
2530 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002531 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 return FAIL;
2533
2534 /*
2535 * Repeat computing, until no "+", "-" or ".." is following.
2536 */
2537 for (;;)
2538 {
2539 op = may_peek_next_line(cctx, *arg, &next);
2540 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2541 break;
2542 if (op[0] == op[1] && *op != '.' && next)
2543 // Finding "++" or "--" on the next line is a separate command.
2544 // But ".." is concatenation.
2545 break;
2546 oplen = (*op == '.' ? 2 : 1);
2547 if (next != NULL)
2548 {
2549 *arg = next_line_from_context(cctx, TRUE);
2550 op = skipwhite(*arg);
2551 }
2552
2553 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2554 {
2555 error_white_both(op, oplen);
2556 return FAIL;
2557 }
2558
2559 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2560 return FAIL;
2561
2562 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002563 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002564 return FAIL;
2565
2566 if (ppconst->pp_used == ppconst_used + 2
2567 && (*op == '.'
2568 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2569 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2570 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2571 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2572 {
2573 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2574 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2575
2576 // concat/subtract/add constant numbers
2577 if (*op == '+')
2578 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2579 else if (*op == '-')
2580 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2581 else
2582 {
2583 // concatenate constant strings
2584 char_u *s1 = tv1->vval.v_string;
2585 char_u *s2 = tv2->vval.v_string;
2586 size_t len1 = STRLEN(s1);
2587
2588 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2589 if (tv1->vval.v_string == NULL)
2590 {
2591 clear_ppconst(ppconst);
2592 return FAIL;
2593 }
2594 mch_memmove(tv1->vval.v_string, s1, len1);
2595 STRCPY(tv1->vval.v_string + len1, s2);
2596 vim_free(s1);
2597 vim_free(s2);
2598 }
2599 --ppconst->pp_used;
2600 }
2601 else
2602 {
2603 generate_ppconst(cctx, ppconst);
2604 ppconst->pp_is_const = FALSE;
2605 if (*op == '.')
2606 {
2607 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2608 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2609 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002610 if (generate_CONCAT(cctx, 2) == FAIL)
2611 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002612 }
2613 else
2614 generate_two_op(cctx, op);
2615 }
2616 }
2617
2618 return OK;
2619}
2620
2621/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002622 * expr6a >> expr6b
2623 * expr6a << expr6b
2624 *
2625 * Produces instructions:
2626 * OPNR bitwise left or right shift
2627 */
2628 static int
2629compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2630{
2631 exprtype_T type = EXPR_UNKNOWN;
2632 char_u *p;
2633 char_u *next;
2634 int len = 2;
2635 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002636 isn_T *isn;
2637
2638 // get the first variable
2639 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2640 return FAIL;
2641
2642 /*
2643 * Repeat computing, until no "+", "-" or ".." is following.
2644 */
2645 for (;;)
2646 {
2647 type = EXPR_UNKNOWN;
2648
2649 p = may_peek_next_line(cctx, *arg, &next);
2650 if (p[0] == '<' && p[1] == '<')
2651 type = EXPR_LSHIFT;
2652 else if (p[0] == '>' && p[1] == '>')
2653 type = EXPR_RSHIFT;
2654
2655 if (type == EXPR_UNKNOWN)
2656 return OK;
2657
2658 // Handle a bitwise left or right shift operator
2659 if (ppconst->pp_used == ppconst_used + 1)
2660 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002661 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002662 {
2663 // left operand should be a number
2664 emsg(_(e_bitshift_ops_must_be_number));
2665 return FAIL;
2666 }
2667 }
2668 else
2669 {
2670 type_T *t = get_type_on_stack(cctx, 0);
2671
2672 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2673 {
2674 emsg(_(e_bitshift_ops_must_be_number));
2675 return FAIL;
2676 }
2677 }
2678
2679 if (next != NULL)
2680 {
2681 *arg = next_line_from_context(cctx, TRUE);
2682 p = skipwhite(*arg);
2683 }
2684
2685 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2686 {
2687 error_white_both(p, len);
2688 return FAIL;
2689 }
2690
2691 // get the second variable
2692 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2693 return FAIL;
2694
2695 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2696 return FAIL;
2697
2698 if (ppconst->pp_used == ppconst_used + 2)
2699 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002700 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2701 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2702
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002703 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002704 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2705 {
2706 // right operand should be a positive number
2707 if (tv2->v_type != VAR_NUMBER)
2708 emsg(_(e_bitshift_ops_must_be_number));
2709 else
2710 emsg(_(e_bitshift_ops_must_be_postive));
2711 return FAIL;
2712 }
2713
2714 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2715 tv1->vval.v_number = 0;
2716 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002717 tv1->vval.v_number =
2718 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002719 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002720 tv1->vval.v_number =
2721 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002722 clear_tv(tv2);
2723 --ppconst->pp_used;
2724 }
2725 else
2726 {
2727 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2728 FALSE, FALSE) == FAIL)
2729 {
2730 emsg(_(e_bitshift_ops_must_be_number));
2731 return FAIL;
2732 }
2733
2734 generate_ppconst(cctx, ppconst);
2735
2736 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2737 if (isn == NULL)
2738 return FAIL;
2739
2740 if (isn != NULL)
2741 isn->isn_arg.op.op_type = type;
2742 }
2743 }
2744
2745 return OK;
2746}
2747
2748/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002749 * expr5a == expr5b
2750 * expr5a =~ expr5b
2751 * expr5a != expr5b
2752 * expr5a !~ expr5b
2753 * expr5a > expr5b
2754 * expr5a >= expr5b
2755 * expr5a < expr5b
2756 * expr5a <= expr5b
2757 * expr5a is expr5b
2758 * expr5a isnot expr5b
2759 *
2760 * Produces instructions:
2761 * EVAL expr5a Push result of "expr5a"
2762 * EVAL expr5b Push result of "expr5b"
2763 * COMPARE one of the compare instructions
2764 */
2765 static int
2766compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2767{
2768 exprtype_T type = EXPR_UNKNOWN;
2769 char_u *p;
2770 char_u *next;
2771 int len = 2;
2772 int type_is = FALSE;
2773 int ppconst_used = ppconst->pp_used;
2774
2775 // get the first variable
2776 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2777 return FAIL;
2778
2779 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002780
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002781 type = get_compare_type(p, &len, &type_is);
2782
2783 /*
2784 * If there is a comparative operator, use it.
2785 */
2786 if (type != EXPR_UNKNOWN)
2787 {
2788 int ic = FALSE; // Default: do not ignore case
2789
2790 if (next != NULL)
2791 {
2792 *arg = next_line_from_context(cctx, TRUE);
2793 p = skipwhite(*arg);
2794 }
2795 if (type_is && (p[len] == '?' || p[len] == '#'))
2796 {
2797 semsg(_(e_invalid_expression_str), *arg);
2798 return FAIL;
2799 }
2800 // extra question mark appended: ignore case
2801 if (p[len] == '?')
2802 {
2803 ic = TRUE;
2804 ++len;
2805 }
2806 // extra '#' appended: match case (ignored)
2807 else if (p[len] == '#')
2808 ++len;
2809 // nothing appended: match case
2810
2811 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2812 {
2813 error_white_both(p, len);
2814 return FAIL;
2815 }
2816
2817 // get the second variable
2818 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2819 return FAIL;
2820
2821 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2822 return FAIL;
2823
2824 if (ppconst->pp_used == ppconst_used + 2)
2825 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002826 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002827 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2828 int ret;
2829
2830 // Both sides are a constant, compute the result now.
2831 // First check for a valid combination of types, this is more
2832 // strict than typval_compare().
2833 if (check_compare_types(type, tv1, tv2) == FAIL)
2834 ret = FAIL;
2835 else
2836 {
2837 ret = typval_compare(tv1, tv2, type, ic);
2838 tv1->v_type = VAR_BOOL;
2839 tv1->vval.v_number = tv1->vval.v_number
2840 ? VVAL_TRUE : VVAL_FALSE;
2841 clear_tv(tv2);
2842 --ppconst->pp_used;
2843 }
2844 return ret;
2845 }
2846
2847 generate_ppconst(cctx, ppconst);
2848 return generate_COMPARE(cctx, type, ic);
2849 }
2850
2851 return OK;
2852}
2853
2854static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2855
2856/*
2857 * Compile || or &&.
2858 */
2859 static int
2860compile_and_or(
2861 char_u **arg,
2862 cctx_T *cctx,
2863 char *op,
2864 ppconst_T *ppconst,
2865 int ppconst_used UNUSED)
2866{
2867 char_u *next;
2868 char_u *p = may_peek_next_line(cctx, *arg, &next);
2869 int opchar = *op;
2870
2871 if (p[0] == opchar && p[1] == opchar)
2872 {
2873 garray_T *instr = &cctx->ctx_instr;
2874 garray_T end_ga;
2875 int save_skip = cctx->ctx_skip;
2876
2877 /*
2878 * Repeat until there is no following "||" or "&&"
2879 */
2880 ga_init2(&end_ga, sizeof(int), 10);
2881 while (p[0] == opchar && p[1] == opchar)
2882 {
2883 long start_lnum = SOURCING_LNUM;
2884 long save_sourcing_lnum;
2885 int start_ctx_lnum = cctx->ctx_lnum;
2886 int save_lnum;
2887 int const_used;
2888 int status;
2889 jumpwhen_T jump_when = opchar == '|'
2890 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2891
2892 if (next != NULL)
2893 {
2894 *arg = next_line_from_context(cctx, TRUE);
2895 p = skipwhite(*arg);
2896 }
2897
2898 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2899 {
2900 semsg(_(e_white_space_required_before_and_after_str_at_str),
2901 op, p);
2902 ga_clear(&end_ga);
2903 return FAIL;
2904 }
2905
2906 save_sourcing_lnum = SOURCING_LNUM;
2907 SOURCING_LNUM = start_lnum;
2908 save_lnum = cctx->ctx_lnum;
2909 cctx->ctx_lnum = start_ctx_lnum;
2910
2911 status = check_ppconst_bool(ppconst);
2912 if (status != FAIL)
2913 {
2914 // Use the last ppconst if possible.
2915 if (ppconst->pp_used > 0)
2916 {
2917 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2918 int is_true = tv2bool(tv);
2919
2920 if ((is_true && opchar == '|')
2921 || (!is_true && opchar == '&'))
2922 {
2923 // For "false && expr" and "true || expr" the "expr"
2924 // does not need to be evaluated.
2925 cctx->ctx_skip = SKIP_YES;
2926 clear_tv(tv);
2927 tv->v_type = VAR_BOOL;
2928 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2929 }
2930 else
2931 {
2932 // For "true && expr" and "false || expr" only "expr"
2933 // needs to be evaluated.
2934 --ppconst->pp_used;
2935 jump_when = JUMP_NEVER;
2936 }
2937 }
2938 else
2939 {
2940 // Every part must evaluate to a bool.
2941 status = bool_on_stack(cctx);
2942 }
2943 }
2944 if (status != FAIL)
2945 status = ga_grow(&end_ga, 1);
2946 cctx->ctx_lnum = save_lnum;
2947 if (status == FAIL)
2948 {
2949 ga_clear(&end_ga);
2950 return FAIL;
2951 }
2952
2953 if (jump_when != JUMP_NEVER)
2954 {
2955 if (cctx->ctx_skip != SKIP_YES)
2956 {
2957 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2958 ++end_ga.ga_len;
2959 }
2960 generate_JUMP(cctx, jump_when, 0);
2961 }
2962
2963 // eval the next expression
2964 SOURCING_LNUM = save_sourcing_lnum;
2965 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2966 {
2967 ga_clear(&end_ga);
2968 return FAIL;
2969 }
2970
2971 const_used = ppconst->pp_used;
2972 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2973 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2974 {
2975 ga_clear(&end_ga);
2976 return FAIL;
2977 }
2978
2979 // "0 || 1" results in true, "1 && 0" results in false.
2980 if (ppconst->pp_used == const_used + 1)
2981 {
2982 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2983
2984 if (tv->v_type == VAR_NUMBER
2985 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2986 {
2987 tv->vval.v_number = tv->vval.v_number == 1
2988 ? VVAL_TRUE : VVAL_FALSE;
2989 tv->v_type = VAR_BOOL;
2990 }
2991 }
2992
2993 p = may_peek_next_line(cctx, *arg, &next);
2994 }
2995
2996 if (check_ppconst_bool(ppconst) == FAIL)
2997 {
2998 ga_clear(&end_ga);
2999 return FAIL;
3000 }
3001
3002 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3003 // Every part must evaluate to a bool.
3004 if (bool_on_stack(cctx) == FAIL)
3005 {
3006 ga_clear(&end_ga);
3007 return FAIL;
3008 }
3009
3010 if (end_ga.ga_len > 0)
3011 {
3012 // Fill in the end label in all jumps.
3013 generate_ppconst(cctx, ppconst);
3014 while (end_ga.ga_len > 0)
3015 {
3016 isn_T *isn;
3017
3018 --end_ga.ga_len;
3019 isn = ((isn_T *)instr->ga_data)
3020 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3021 isn->isn_arg.jump.jump_where = instr->ga_len;
3022 }
3023 }
3024 ga_clear(&end_ga);
3025
3026 cctx->ctx_skip = save_skip;
3027 }
3028
3029 return OK;
3030}
3031
3032/*
3033 * expr4a && expr4a && expr4a logical AND
3034 *
3035 * Produces instructions:
3036 * EVAL expr4a Push result of "expr4a"
3037 * COND2BOOL convert to bool if needed
3038 * JUMP_IF_COND_FALSE end
3039 * EVAL expr4b Push result of "expr4b"
3040 * JUMP_IF_COND_FALSE end
3041 * EVAL expr4c Push result of "expr4c"
3042 * end:
3043 */
3044 static int
3045compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3046{
3047 int ppconst_used = ppconst->pp_used;
3048
3049 // get the first variable
3050 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3051 return FAIL;
3052
3053 // || and && work almost the same
3054 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3055}
3056
3057/*
3058 * expr3a || expr3b || expr3c logical OR
3059 *
3060 * Produces instructions:
3061 * EVAL expr3a Push result of "expr3a"
3062 * COND2BOOL convert to bool if needed
3063 * JUMP_IF_COND_TRUE end
3064 * EVAL expr3b Push result of "expr3b"
3065 * JUMP_IF_COND_TRUE end
3066 * EVAL expr3c Push result of "expr3c"
3067 * end:
3068 */
3069 static int
3070compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3071{
3072 int ppconst_used = ppconst->pp_used;
3073
3074 // eval the first expression
3075 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3076 return FAIL;
3077
3078 // || and && work almost the same
3079 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3080}
3081
3082/*
3083 * Toplevel expression: expr2 ? expr1a : expr1b
3084 * Produces instructions:
3085 * EVAL expr2 Push result of "expr2"
3086 * JUMP_IF_FALSE alt jump if false
3087 * EVAL expr1a
3088 * JUMP_ALWAYS end
3089 * alt: EVAL expr1b
3090 * end:
3091 *
3092 * Toplevel expression: expr2 ?? expr1
3093 * Produces instructions:
3094 * EVAL expr2 Push result of "expr2"
3095 * JUMP_AND_KEEP_IF_TRUE end jump if true
3096 * EVAL expr1
3097 * end:
3098 */
3099 int
3100compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3101{
3102 char_u *p;
3103 int ppconst_used = ppconst->pp_used;
3104 char_u *next;
3105
3106 // Ignore all kinds of errors when not producing code.
3107 if (cctx->ctx_skip == SKIP_YES)
3108 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003109 int prev_did_emsg = did_emsg;
3110
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003111 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003112 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003113 }
3114
3115 // Evaluate the first expression.
3116 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3117 return FAIL;
3118
3119 p = may_peek_next_line(cctx, *arg, &next);
3120 if (*p == '?')
3121 {
3122 int op_falsy = p[1] == '?';
3123 garray_T *instr = &cctx->ctx_instr;
3124 garray_T *stack = &cctx->ctx_type_stack;
3125 int alt_idx = instr->ga_len;
3126 int end_idx = 0;
3127 isn_T *isn;
3128 type_T *type1 = NULL;
3129 int has_const_expr = FALSE;
3130 int const_value = FALSE;
3131 int save_skip = cctx->ctx_skip;
3132
3133 if (next != NULL)
3134 {
3135 *arg = next_line_from_context(cctx, TRUE);
3136 p = skipwhite(*arg);
3137 }
3138
3139 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3140 {
3141 semsg(_(e_white_space_required_before_and_after_str_at_str),
3142 op_falsy ? "??" : "?", p);
3143 return FAIL;
3144 }
3145
3146 if (ppconst->pp_used == ppconst_used + 1)
3147 {
3148 // the condition is a constant, we know whether the ? or the :
3149 // expression is to be evaluated.
3150 has_const_expr = TRUE;
3151 if (op_falsy)
3152 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3153 else
3154 {
3155 int error = FALSE;
3156
3157 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3158 &error);
3159 if (error)
3160 return FAIL;
3161 }
3162 cctx->ctx_skip = save_skip == SKIP_YES ||
3163 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3164
3165 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3166 // "left ?? right" and "left" is truthy: produce "left"
3167 generate_ppconst(cctx, ppconst);
3168 else
3169 {
3170 clear_tv(&ppconst->pp_tv[ppconst_used]);
3171 --ppconst->pp_used;
3172 }
3173 }
3174 else
3175 {
3176 generate_ppconst(cctx, ppconst);
3177 if (op_falsy)
3178 end_idx = instr->ga_len;
3179 generate_JUMP(cctx, op_falsy
3180 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3181 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003182 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003183 }
3184
3185 // evaluate the second expression; any type is accepted
3186 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3187 return FAIL;
3188 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3189 return FAIL;
3190
3191 if (!has_const_expr)
3192 {
3193 generate_ppconst(cctx, ppconst);
3194
3195 if (!op_falsy)
3196 {
3197 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003198 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003199 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003200
3201 end_idx = instr->ga_len;
3202 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3203
3204 // jump here from JUMP_IF_FALSE
3205 isn = ((isn_T *)instr->ga_data) + alt_idx;
3206 isn->isn_arg.jump.jump_where = instr->ga_len;
3207 }
3208 }
3209
3210 if (!op_falsy)
3211 {
3212 // Check for the ":".
3213 p = may_peek_next_line(cctx, *arg, &next);
3214 if (*p != ':')
3215 {
3216 emsg(_(e_missing_colon_after_questionmark));
3217 return FAIL;
3218 }
3219 if (next != NULL)
3220 {
3221 *arg = next_line_from_context(cctx, TRUE);
3222 p = skipwhite(*arg);
3223 }
3224
3225 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3226 {
3227 semsg(_(e_white_space_required_before_and_after_str_at_str),
3228 ":", p);
3229 return FAIL;
3230 }
3231
3232 // evaluate the third expression
3233 if (has_const_expr)
3234 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3235 ? SKIP_YES : SKIP_NOT;
3236 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3237 return FAIL;
3238 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3239 return FAIL;
3240 }
3241
3242 if (!has_const_expr)
3243 {
3244 type_T **typep;
3245
3246 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003247 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003248
3249 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003250 typep = &((((type2_T *)stack->ga_data)
3251 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003252 common_type(type1, *typep, typep, cctx->ctx_type_list);
3253
3254 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3255 isn = ((isn_T *)instr->ga_data) + end_idx;
3256 isn->isn_arg.jump.jump_where = instr->ga_len;
3257 }
3258
3259 cctx->ctx_skip = save_skip;
3260 }
3261 return OK;
3262}
3263
3264/*
3265 * Toplevel expression.
3266 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3267 * Returns OK or FAIL.
3268 */
3269 int
3270compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3271{
3272 ppconst_T ppconst;
3273
3274 CLEAR_FIELD(ppconst);
3275 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3276 {
3277 clear_ppconst(&ppconst);
3278 return FAIL;
3279 }
3280 if (is_const != NULL)
3281 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3282 if (generate_ppconst(cctx, &ppconst) == FAIL)
3283 return FAIL;
3284 return OK;
3285}
3286
3287/*
3288 * Toplevel expression.
3289 */
3290 int
3291compile_expr0(char_u **arg, cctx_T *cctx)
3292{
3293 return compile_expr0_ext(arg, cctx, NULL);
3294}
3295
3296
3297#endif // defined(FEAT_EVAL)