blob: 0e44b7652f4ed2b5124461a18ae4acc7e46f3cea [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;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100499 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100500 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501
502 name = vim_strnsave(*arg, end - *arg);
503 if (name == NULL)
504 return FAIL;
505
506 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
507 {
508 script_autoload(name, FALSE);
509 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
510 }
511 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
512 == OK)
513 {
514 if (gen_load_outer == 0)
515 gen_load = TRUE;
516 }
517 else
518 {
519 lvar_T lvar;
520
521 if (lookup_local(*arg, len, &lvar, cctx) == OK)
522 {
523 type = lvar.lv_type;
524 idx = lvar.lv_idx;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100525 outer_loop_depth = lvar.lv_loop_depth;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100526 outer_loop_idx = lvar.lv_loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000527 if (lvar.lv_from_outer != 0)
528 gen_load_outer = lvar.lv_from_outer;
529 else
530 gen_load = TRUE;
531 }
532 else
533 {
534 // "var" can be script-local even without using "s:" if it
535 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000536 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000537 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100538 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539
540 // When evaluating an expression and the name starts with an
541 // uppercase letter it can be a user defined function.
542 // generate_funcref() will fail if the function can't be found.
543 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000544 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 }
546 }
547 if (gen_load)
548 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
549 if (gen_load_outer > 0)
550 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100551 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
552 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000553 cctx->ctx_outer_used = TRUE;
554 }
555 }
556
557 *arg = end;
558
559theend:
560 if (res == FAIL && error && called_emsg == prev_called_emsg)
561 semsg(_(e_variable_not_found_str), name);
562 vim_free(name);
563 return res;
564}
565
566/*
567 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100568 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569 * Returns FAIL if compilation fails.
570 */
571 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100572compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573{
LemonBoyf3b48952022-05-05 13:53:03 +0100574 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575 garray_T save_ga = cctx->ctx_instr;
576 int expr_res;
577 int trailing_error;
578 int instr_count;
579 isn_T *instr = NULL;
580
581 // Remove the string type from the stack.
582 --cctx->ctx_type_stack.ga_len;
583
584 // Temporarily reset the list of instructions so that the jump labels are
585 // correct.
586 cctx->ctx_instr.ga_len = 0;
587 cctx->ctx_instr.ga_maxlen = 0;
588 cctx->ctx_instr.ga_data = NULL;
589 expr_res = compile_expr0(&s, cctx);
590 s = skipwhite(s);
591 trailing_error = *s != NUL;
592
593 if (expr_res == FAIL || trailing_error
594 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
595 {
596 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000597 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598 clear_instr_ga(&cctx->ctx_instr);
599 cctx->ctx_instr = save_ga;
600 ++cctx->ctx_type_stack.ga_len;
601 return FAIL;
602 }
603
604 // Move the generated instructions into the ISN_INSTR instruction, then
605 // restore the list of instructions.
606 instr_count = cctx->ctx_instr.ga_len;
607 instr = cctx->ctx_instr.ga_data;
608 instr[instr_count].isn_type = ISN_FINISH;
609
610 cctx->ctx_instr = save_ga;
611 vim_free(isn->isn_arg.string);
612 isn->isn_type = ISN_INSTR;
613 isn->isn_arg.instr = instr;
614 return OK;
615}
616
617/*
618 * Compile the argument expressions.
619 * "arg" points to just after the "(" and is advanced to after the ")"
620 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100621 int
LemonBoyf3b48952022-05-05 13:53:03 +0100622compile_arguments(
623 char_u **arg,
624 cctx_T *cctx,
625 int *argcount,
626 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000627{
628 char_u *p = *arg;
629 char_u *whitep = *arg;
630 int must_end = FALSE;
631 int instr_count;
632
633 for (;;)
634 {
635 if (may_get_next_line(whitep, &p, cctx) == FAIL)
636 goto failret;
637 if (*p == ')')
638 {
639 *arg = p + 1;
640 return OK;
641 }
642 if (must_end)
643 {
644 semsg(_(e_missing_comma_before_argument_str), p);
645 return FAIL;
646 }
647
648 instr_count = cctx->ctx_instr.ga_len;
649 if (compile_expr0(&p, cctx) == FAIL)
650 return FAIL;
651 ++*argcount;
652
LemonBoyf3b48952022-05-05 13:53:03 +0100653 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000654 && cctx->ctx_instr.ga_len == instr_count + 1)
655 {
656 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
657
658 // {skip} argument of searchpair() can be compiled if not empty
659 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100660 compile_string(isn, cctx, 0);
661 }
662 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
663 && cctx->ctx_instr.ga_len == instr_count + 1)
664 {
665 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
666
667 // {sub} argument of substitute() can be compiled if it starts
668 // with \=
669 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
670 && isn->isn_arg.string[1] == '=')
671 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000672 }
673
674 if (*p != ',' && *skipwhite(p) == ',')
675 {
676 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
677 p = skipwhite(p);
678 }
679 if (*p == ',')
680 {
681 ++p;
682 if (*p != NUL && !VIM_ISWHITE(*p))
683 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
684 }
685 else
686 must_end = TRUE;
687 whitep = p;
688 p = skipwhite(p);
689 }
690failret:
691 emsg(_(e_missing_closing_paren));
692 return FAIL;
693}
694
695/*
696 * Compile a function call: name(arg1, arg2)
697 * "arg" points to "name", "arg + varlen" to the "(".
698 * "argcount_init" is 1 for "value->method()"
699 * Instructions:
700 * EVAL arg1
701 * EVAL arg2
702 * BCALL / DCALL / UCALL
703 */
704 static int
705compile_call(
706 char_u **arg,
707 size_t varlen,
708 cctx_T *cctx,
709 ppconst_T *ppconst,
710 int argcount_init)
711{
712 char_u *name = *arg;
713 char_u *p;
714 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100715 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 char_u fname_buf[FLEN_FIXED + 1];
717 char_u *tofree = NULL;
718 int error = FCERR_NONE;
719 ufunc_T *ufunc = NULL;
720 int res = FAIL;
721 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000722 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100723 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000724 imported_T *import;
725
726 if (varlen >= sizeof(namebuf))
727 {
728 semsg(_(e_name_too_long_str), name);
729 return FAIL;
730 }
731 vim_strncpy(namebuf, *arg, varlen);
732
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000733 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000734 if (import != NULL)
735 {
736 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
737 return FAIL;
738 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739
740 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100741 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100743 if ((varlen == 3
744 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
746 {
747 char_u *s = skipwhite(*arg + varlen + 1);
748 typval_T argvars[2];
749 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100750 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751
752 argvars[0].v_type = VAR_UNKNOWN;
753 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100754 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100756 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757 s = skipwhite(s);
758 if (*s == ')' && argvars[0].v_type == VAR_STRING
759 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
760 || !is_has))
761 {
762 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
763
764 *arg = s + 1;
765 argvars[1].v_type = VAR_UNKNOWN;
766 tv->v_type = VAR_NUMBER;
767 tv->vval.v_number = 0;
768 if (is_has)
769 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100770 else if (is_len)
771 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 else
773 f_exists(argvars, tv);
774 clear_tv(&argvars[0]);
775 ++ppconst->pp_used;
776 return OK;
777 }
778 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100779 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780 {
781 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
782 return FAIL;
783 }
784 }
785
786 if (generate_ppconst(cctx, ppconst) == FAIL)
787 return FAIL;
788
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
790
791 // We handle the "skip" argument of searchpair() and searchpairpos()
792 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100793 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
794 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
795 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
796 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
797 special_fn = CA_SEARCHPAIR;
798 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
799 special_fn = CA_SUBSTITUTE;
800 else
801 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802
803 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100804 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805 goto theend;
806
807 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
808 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
809 {
810 int idx;
811
812 // builtin function
813 idx = find_internal_func(name);
814 if (idx >= 0)
815 {
816 if (STRCMP(name, "flatten") == 0)
817 {
818 emsg(_(e_cannot_use_flatten_in_vim9_script));
819 goto theend;
820 }
821
822 if (STRCMP(name, "add") == 0 && argcount == 2)
823 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000824 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825
826 // add() can be compiled to instructions if we know the type
827 if (type->tt_type == VAR_LIST)
828 {
829 // inline "add(list, item)" so that the type can be checked
830 res = generate_LISTAPPEND(cctx);
831 idx = -1;
832 }
833 else if (type->tt_type == VAR_BLOB)
834 {
835 // inline "add(blob, nr)" so that the type can be checked
836 res = generate_BLOBAPPEND(cctx);
837 idx = -1;
838 }
839 }
840
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100841 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
842 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100843 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100844 // May have the "D" or "R" flag, reserve a variable for a
845 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100846 if (get_defer_var_idx(cctx) == 0)
847 idx = -1;
848 }
849
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 if (idx >= 0)
851 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
852 }
853 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100854 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 goto theend;
856 }
857
Bram Moolenaar62aec932022-01-29 21:45:34 +0000858 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
859
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 // An argument or local variable can be a function reference, this
861 // overrules a function name.
862 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
863 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
864 {
865 // If we can find the function by name generate the right call.
866 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000867 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000868 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000870 if (!func_is_global(ufunc))
871 {
872 res = generate_CALL(cctx, ufunc, argcount);
873 goto theend;
874 }
875 if (!has_g_namespace
876 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
877 {
878 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100879 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000880 goto theend;
881 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000882 }
883 }
884
885 // If the name is a variable, load it and use PCALL.
886 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000887 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000889 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
891 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000892 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893
894 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
895 goto theend;
896 }
897
898 // If we can find a global function by name generate the right call.
899 if (ufunc != NULL)
900 {
901 res = generate_CALL(cctx, ufunc, argcount);
902 goto theend;
903 }
904
905 // A global function may be defined only later. Need to figure out at
906 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000907 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000908 res = generate_UCALL(cctx, name, argcount);
909 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100910 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000911
912theend:
913 vim_free(tofree);
914 return res;
915}
916
917// like NAMESPACE_CHAR but with 'a' and 'l'.
918#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
919
920/*
921 * Find the end of a variable or function name. Unlike find_name_end() this
922 * does not recognize magic braces.
923 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
924 * Return a pointer to just after the name. Equal to "arg" if there is no
925 * valid name.
926 */
927 char_u *
928to_name_end(char_u *arg, int use_namespace)
929{
930 char_u *p;
931
932 // Quick check for valid starting character.
933 if (!eval_isnamec1(*arg))
934 return arg;
935
936 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
937 // Include a namespace such as "s:var" and "v:var". But "n:" is not
938 // and can be used in slice "[n:]".
939 if (*p == ':' && (p != arg + 1
940 || !use_namespace
941 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
942 break;
943 return p;
944}
945
946/*
947 * Like to_name_end() but also skip over a list or dict constant.
948 * Also accept "<SNR>123_Func".
949 * This intentionally does not handle line continuation.
950 */
951 char_u *
952to_name_const_end(char_u *arg)
953{
954 char_u *p = arg;
955 typval_T rettv;
956
957 if (STRNCMP(p, "<SNR>", 5) == 0)
958 p = skipdigits(p + 5);
959 p = to_name_end(p, TRUE);
960 if (p == arg && *arg == '[')
961 {
962
963 // Can be "[1, 2, 3]->Func()".
964 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
965 p = arg;
966 }
967 return p;
968}
969
970/*
971 * parse a list: [expr, expr]
972 * "*arg" points to the '['.
973 * ppconst->pp_is_const is set if all items are a constant.
974 */
975 static int
976compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
977{
978 char_u *p = skipwhite(*arg + 1);
979 char_u *whitep = *arg + 1;
980 int count = 0;
981 int is_const;
982 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100983 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984
985 for (;;)
986 {
987 if (may_get_next_line(whitep, &p, cctx) == FAIL)
988 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000989 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 return FAIL;
991 }
992 if (*p == ',')
993 {
994 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
995 return FAIL;
996 }
997 if (*p == ']')
998 {
999 ++p;
1000 break;
1001 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001002 if (must_end)
1003 {
1004 semsg(_(e_missing_comma_in_list_str), p);
1005 return FAIL;
1006 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1008 return FAIL;
1009 if (!is_const)
1010 is_all_const = FALSE;
1011 ++count;
1012 if (*p == ',')
1013 {
1014 ++p;
1015 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1016 {
1017 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1018 return FAIL;
1019 }
1020 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001021 else
1022 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001023 whitep = p;
1024 p = skipwhite(p);
1025 }
1026 *arg = p;
1027
1028 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001029 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001030}
1031
1032/*
1033 * Parse a lambda: "(arg, arg) => expr"
1034 * "*arg" points to the '('.
1035 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1036 */
1037 static int
1038compile_lambda(char_u **arg, cctx_T *cctx)
1039{
1040 int r;
1041 typval_T rettv;
1042 ufunc_T *ufunc;
1043 evalarg_T evalarg;
1044
1045 init_evalarg(&evalarg);
1046 evalarg.eval_flags = EVAL_EVALUATE;
1047 evalarg.eval_cctx = cctx;
1048
1049 // Get the funcref in "rettv".
1050 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1051 if (r != OK)
1052 {
1053 clear_evalarg(&evalarg, NULL);
1054 return r;
1055 }
1056
1057 // "rettv" will now be a partial referencing the function.
1058 ufunc = rettv.vval.v_partial->pt_func;
1059 ++ufunc->uf_refcount;
1060 clear_tv(&rettv);
1061
1062 // Compile it here to get the return type. The return type is optional,
1063 // when it's missing use t_unknown. This is recognized in
1064 // compile_return().
1065 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1066 ufunc->uf_ret_type = &t_unknown;
1067 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1068
1069 // When the outer function is compiled for profiling or debugging, the
1070 // lambda may be called without profiling or debugging. Compile it here in
1071 // the right context.
1072 if (cctx->ctx_compile_type == CT_DEBUG
1073#ifdef FEAT_PROFILE
1074 || cctx->ctx_compile_type == CT_PROFILE
1075#endif
1076 )
1077 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1078
Bram Moolenaar139575d2022-03-15 19:29:30 +00001079 // if the outer function is not compiled for debugging or profiling, this
1080 // one might be
1081 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001082 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001083 compiletype_T compile_type = get_compile_type(ufunc);
1084
1085 if (compile_type != CT_NONE)
1086 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001087 }
1088
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1090 // "*arg" may point into it. Point into the original line to avoid a
1091 // dangling pointer.
1092 if (evalarg.eval_using_cmdline)
1093 {
1094 garray_T *gap = &evalarg.eval_tofree_ga;
1095 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1096
1097 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1098 + off;
1099 }
1100
1101 clear_evalarg(&evalarg, NULL);
1102
1103 if (ufunc->uf_def_status == UF_COMPILED)
1104 {
1105 // The return type will now be known.
1106 set_function_type(ufunc);
1107
1108 // The function reference count will be 1. When the ISN_FUNCREF
1109 // instruction is deleted the reference count is decremented and the
1110 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001111 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112 }
1113
1114 func_ptr_unref(ufunc);
1115 return FAIL;
1116}
1117
1118/*
1119 * Get a lambda and compile it. Uses Vim9 syntax.
1120 */
1121 int
1122get_lambda_tv_and_compile(
1123 char_u **arg,
1124 typval_T *rettv,
1125 int types_optional,
1126 evalarg_T *evalarg)
1127{
1128 int r;
1129 ufunc_T *ufunc;
1130 int save_sc_version = current_sctx.sc_version;
1131
1132 // Get the funcref in "rettv".
1133 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1134 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1135 current_sctx.sc_version = save_sc_version;
1136 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001137 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001138
1139 // "rettv" will now be a partial referencing the function.
1140 ufunc = rettv->vval.v_partial->pt_func;
1141
1142 // Compile it here to get the return type. The return type is optional,
1143 // when it's missing use t_unknown. This is recognized in
1144 // compile_return().
1145 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1146 ufunc->uf_ret_type = &t_unknown;
1147 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1148
1149 if (ufunc->uf_def_status == UF_COMPILED)
1150 {
1151 // The return type will now be known.
1152 set_function_type(ufunc);
1153 return OK;
1154 }
1155 clear_tv(rettv);
1156 return FAIL;
1157}
1158
1159/*
1160 * parse a dict: {key: val, [key]: val}
1161 * "*arg" points to the '{'.
1162 * ppconst->pp_is_const is set if all item values are a constant.
1163 */
1164 static int
1165compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1166{
1167 garray_T *instr = &cctx->ctx_instr;
1168 int count = 0;
1169 dict_T *d = dict_alloc();
1170 dictitem_T *item;
1171 char_u *whitep = *arg + 1;
1172 char_u *p;
1173 int is_const;
1174 int is_all_const = TRUE; // reset when non-const encountered
1175
1176 if (d == NULL)
1177 return FAIL;
1178 if (generate_ppconst(cctx, ppconst) == FAIL)
1179 return FAIL;
1180 for (;;)
1181 {
1182 char_u *key = NULL;
1183
1184 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1185 {
1186 *arg = NULL;
1187 goto failret;
1188 }
1189
1190 if (**arg == '}')
1191 break;
1192
1193 if (**arg == '[')
1194 {
1195 isn_T *isn;
1196
1197 // {[expr]: value} uses an evaluated key.
1198 *arg = skipwhite(*arg + 1);
1199 if (compile_expr0(arg, cctx) == FAIL)
1200 return FAIL;
1201 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1202 if (isn->isn_type == ISN_PUSHNR)
1203 {
1204 char buf[NUMBUFLEN];
1205
1206 // Convert to string at compile time.
1207 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1208 isn->isn_type = ISN_PUSHS;
1209 isn->isn_arg.string = vim_strsave((char_u *)buf);
1210 }
1211 if (isn->isn_type == ISN_PUSHS)
1212 key = isn->isn_arg.string;
1213 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1214 return FAIL;
1215 *arg = skipwhite(*arg);
1216 if (**arg != ']')
1217 {
1218 emsg(_(e_missing_matching_bracket_after_dict_key));
1219 return FAIL;
1220 }
1221 ++*arg;
1222 }
1223 else
1224 {
1225 // {"name": value},
1226 // {'name': value},
1227 // {name: value} use "name" as a literal key
1228 key = get_literal_key(arg);
1229 if (key == NULL)
1230 return FAIL;
1231 if (generate_PUSHS(cctx, &key) == FAIL)
1232 return FAIL;
1233 }
1234
1235 // Check for duplicate keys, if using string keys.
1236 if (key != NULL)
1237 {
1238 item = dict_find(d, key, -1);
1239 if (item != NULL)
1240 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001241 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242 goto failret;
1243 }
1244 item = dictitem_alloc(key);
1245 if (item != NULL)
1246 {
1247 item->di_tv.v_type = VAR_UNKNOWN;
1248 item->di_tv.v_lock = 0;
1249 if (dict_add(d, item) == FAIL)
1250 dictitem_free(item);
1251 }
1252 }
1253
1254 if (**arg != ':')
1255 {
1256 if (*skipwhite(*arg) == ':')
1257 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1258 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001259 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260 return FAIL;
1261 }
1262 whitep = *arg + 1;
1263 if (!IS_WHITE_OR_NUL(*whitep))
1264 {
1265 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1266 return FAIL;
1267 }
1268
1269 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1270 {
1271 *arg = NULL;
1272 goto failret;
1273 }
1274
1275 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1276 return FAIL;
1277 if (!is_const)
1278 is_all_const = FALSE;
1279 ++count;
1280
1281 whitep = *arg;
1282 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1283 {
1284 *arg = NULL;
1285 goto failret;
1286 }
1287 if (**arg == '}')
1288 break;
1289 if (**arg != ',')
1290 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001291 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292 goto failret;
1293 }
1294 if (IS_WHITE_OR_NUL(*whitep))
1295 {
1296 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1297 return FAIL;
1298 }
1299 whitep = *arg + 1;
1300 if (!IS_WHITE_OR_NUL(*whitep))
1301 {
1302 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1303 return FAIL;
1304 }
1305 *arg = skipwhite(whitep);
1306 }
1307
1308 *arg = *arg + 1;
1309
1310 // Allow for following comment, after at least one space.
1311 p = skipwhite(*arg);
1312 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1313 *arg += STRLEN(*arg);
1314
1315 dict_unref(d);
1316 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001317 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001318
1319failret:
1320 if (*arg == NULL)
1321 {
1322 semsg(_(e_missing_dict_end), _("[end of lines]"));
1323 *arg = (char_u *)"";
1324 }
1325 dict_unref(d);
1326 return FAIL;
1327}
1328
1329/*
1330 * Compile "&option".
1331 */
1332 static int
1333compile_get_option(char_u **arg, cctx_T *cctx)
1334{
1335 typval_T rettv;
1336 char_u *start = *arg;
1337 int ret;
1338
1339 // parse the option and get the current value to get the type.
1340 rettv.v_type = VAR_UNKNOWN;
1341 ret = eval_option(arg, &rettv, TRUE);
1342 if (ret == OK)
1343 {
1344 // include the '&' in the name, eval_option() expects it.
1345 char_u *name = vim_strnsave(start, *arg - start);
1346 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1347 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1348
1349 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1350 vim_free(name);
1351 }
1352 clear_tv(&rettv);
1353
1354 return ret;
1355}
1356
1357/*
1358 * Compile "$VAR".
1359 */
1360 static int
1361compile_get_env(char_u **arg, cctx_T *cctx)
1362{
1363 char_u *start = *arg;
1364 int len;
1365 int ret;
1366 char_u *name;
1367
1368 ++*arg;
1369 len = get_env_len(arg);
1370 if (len == 0)
1371 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001372 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001373 return FAIL;
1374 }
1375
1376 // include the '$' in the name, eval_env_var() expects it.
1377 name = vim_strnsave(start, len + 1);
1378 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1379 vim_free(name);
1380 return ret;
1381}
1382
1383/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001384 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001385 */
1386 static int
1387compile_interp_string(char_u **arg, cctx_T *cctx)
1388{
1389 typval_T tv;
1390 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001391 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001392 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001393 int count = 0;
1394 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001395
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001396 // *arg is on the '$' character, move it to the first string character.
1397 ++*arg;
1398 quote = **arg;
1399 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001400
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001401 for (;;)
1402 {
1403 // Get the string up to the matching quote or to a single '{'.
1404 // "arg" is advanced to either the quote or the '{'.
1405 if (quote == '"')
1406 ret = eval_string(arg, &tv, evaluate, TRUE);
1407 else
1408 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1409 if (ret == FAIL)
1410 break;
1411 if (evaluate)
1412 {
1413 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1414 || (**arg != '{' && count == 0))
1415 {
1416 // generate non-empty string or empty string if it's the only
1417 // one
1418 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1419 return FAIL;
1420 tv.vval.v_string = NULL; // don't free it now
1421 ++count;
1422 }
1423 clear_tv(&tv);
1424 }
1425
1426 if (**arg != '{')
1427 {
1428 // found terminating quote
1429 ++*arg;
1430 break;
1431 }
1432
1433 p = compile_one_expr_in_str(*arg, cctx);
1434 if (p == NULL)
1435 {
1436 ret = FAIL;
1437 break;
1438 }
1439 ++count;
1440 *arg = p;
1441 }
LemonBoy2eaef102022-05-06 13:14:50 +01001442
1443 if (ret == FAIL || !evaluate)
1444 return ret;
1445
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001446 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1447 if (count > 1)
1448 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001449
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001450 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001451}
1452
1453/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 * Compile "@r".
1455 */
1456 static int
1457compile_get_register(char_u **arg, cctx_T *cctx)
1458{
1459 int ret;
1460
1461 ++*arg;
1462 if (**arg == NUL)
1463 {
1464 semsg(_(e_syntax_error_at_str), *arg - 1);
1465 return FAIL;
1466 }
1467 if (!valid_yank_reg(**arg, FALSE))
1468 {
1469 emsg_invreg(**arg);
1470 return FAIL;
1471 }
1472 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1473 ++*arg;
1474 return ret;
1475}
1476
1477/*
1478 * Apply leading '!', '-' and '+' to constant "rettv".
1479 * When "numeric_only" is TRUE do not apply '!'.
1480 */
1481 static int
1482apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1483{
1484 char_u *p = *end;
1485
1486 // this works from end to start
1487 while (p > start)
1488 {
1489 --p;
1490 if (*p == '-' || *p == '+')
1491 {
1492 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001493 if (rettv->v_type == VAR_FLOAT)
1494 {
1495 if (*p == '-')
1496 rettv->vval.v_float = -rettv->vval.v_float;
1497 }
1498 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001499 {
1500 varnumber_T val;
1501 int error = FALSE;
1502
1503 // tv_get_number_chk() accepts a string, but we don't want that
1504 // here
1505 if (check_not_string(rettv) == FAIL)
1506 return FAIL;
1507 val = tv_get_number_chk(rettv, &error);
1508 clear_tv(rettv);
1509 if (error)
1510 return FAIL;
1511 if (*p == '-')
1512 val = -val;
1513 rettv->v_type = VAR_NUMBER;
1514 rettv->vval.v_number = val;
1515 }
1516 }
1517 else if (numeric_only)
1518 {
1519 ++p;
1520 break;
1521 }
1522 else if (*p == '!')
1523 {
1524 int v = tv2bool(rettv);
1525
1526 // '!' is permissive in the type.
1527 clear_tv(rettv);
1528 rettv->v_type = VAR_BOOL;
1529 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1530 }
1531 }
1532 *end = p;
1533 return OK;
1534}
1535
1536/*
1537 * Recognize v: variables that are constants and set "rettv".
1538 */
1539 static void
1540get_vim_constant(char_u **arg, typval_T *rettv)
1541{
1542 if (STRNCMP(*arg, "v:true", 6) == 0)
1543 {
1544 rettv->v_type = VAR_BOOL;
1545 rettv->vval.v_number = VVAL_TRUE;
1546 *arg += 6;
1547 }
1548 else if (STRNCMP(*arg, "v:false", 7) == 0)
1549 {
1550 rettv->v_type = VAR_BOOL;
1551 rettv->vval.v_number = VVAL_FALSE;
1552 *arg += 7;
1553 }
1554 else if (STRNCMP(*arg, "v:null", 6) == 0)
1555 {
1556 rettv->v_type = VAR_SPECIAL;
1557 rettv->vval.v_number = VVAL_NULL;
1558 *arg += 6;
1559 }
1560 else if (STRNCMP(*arg, "v:none", 6) == 0)
1561 {
1562 rettv->v_type = VAR_SPECIAL;
1563 rettv->vval.v_number = VVAL_NONE;
1564 *arg += 6;
1565 }
1566}
1567
1568 exprtype_T
1569get_compare_type(char_u *p, int *len, int *type_is)
1570{
1571 exprtype_T type = EXPR_UNKNOWN;
1572 int i;
1573
1574 switch (p[0])
1575 {
1576 case '=': if (p[1] == '=')
1577 type = EXPR_EQUAL;
1578 else if (p[1] == '~')
1579 type = EXPR_MATCH;
1580 break;
1581 case '!': if (p[1] == '=')
1582 type = EXPR_NEQUAL;
1583 else if (p[1] == '~')
1584 type = EXPR_NOMATCH;
1585 break;
1586 case '>': if (p[1] != '=')
1587 {
1588 type = EXPR_GREATER;
1589 *len = 1;
1590 }
1591 else
1592 type = EXPR_GEQUAL;
1593 break;
1594 case '<': if (p[1] != '=')
1595 {
1596 type = EXPR_SMALLER;
1597 *len = 1;
1598 }
1599 else
1600 type = EXPR_SEQUAL;
1601 break;
1602 case 'i': if (p[1] == 's')
1603 {
1604 // "is" and "isnot"; but not a prefix of a name
1605 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1606 *len = 5;
1607 i = p[*len];
1608 if (!isalnum(i) && i != '_')
1609 {
1610 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1611 *type_is = TRUE;
1612 }
1613 }
1614 break;
1615 }
1616 return type;
1617}
1618
1619/*
1620 * Skip over an expression, ignoring most errors.
1621 */
1622 void
1623skip_expr_cctx(char_u **arg, cctx_T *cctx)
1624{
1625 evalarg_T evalarg;
1626
1627 init_evalarg(&evalarg);
1628 evalarg.eval_cctx = cctx;
1629 skip_expr(arg, &evalarg);
1630 clear_evalarg(&evalarg, NULL);
1631}
1632
1633/*
1634 * Check that the top of the type stack has a type that can be used as a
1635 * condition. Give an error and return FAIL if not.
1636 */
1637 int
1638bool_on_stack(cctx_T *cctx)
1639{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640 type_T *type;
1641
Bram Moolenaar078a4612022-01-04 15:17:03 +00001642 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 if (type == &t_bool)
1644 return OK;
1645
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001646 if (type == &t_any
1647 || type == &t_unknown
1648 || type == &t_number
1649 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1651 // This requires a runtime type check.
1652 return generate_COND2BOOL(cctx);
1653
1654 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1655}
1656
1657/*
1658 * Give the "white on both sides" error, taking the operator from "p[len]".
1659 */
1660 void
1661error_white_both(char_u *op, int len)
1662{
1663 char_u buf[10];
1664
1665 vim_strncpy(buf, op, len);
1666 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1667}
1668
1669/*
1670 * Compile code to apply '-', '+' and '!'.
1671 * When "numeric_only" is TRUE do not apply '!'.
1672 */
1673 static int
1674compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1675{
1676 char_u *p = *end;
1677
1678 // this works from end to start
1679 while (p > start)
1680 {
1681 --p;
1682 while (VIM_ISWHITE(*p))
1683 --p;
1684 if (*p == '-' || *p == '+')
1685 {
1686 int negate = *p == '-';
1687 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 type_T *type;
1689
Bram Moolenaar078a4612022-01-04 15:17:03 +00001690 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691 if (type != &t_float && need_type(type, &t_number,
1692 -1, 0, cctx, FALSE, FALSE) == FAIL)
1693 return FAIL;
1694
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 // only '-' has an effect, for '+' we only check the type
1696 if (negate)
1697 {
1698 isn = generate_instr(cctx, ISN_NEGATENR);
1699 if (isn == NULL)
1700 return FAIL;
1701 }
1702 }
1703 else if (numeric_only)
1704 {
1705 ++p;
1706 break;
1707 }
1708 else
1709 {
1710 int invert = *p == '!';
1711
1712 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1713 {
1714 if (p[-1] == '!')
1715 invert = !invert;
1716 --p;
1717 }
1718 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1719 return FAIL;
1720 }
1721 }
1722 *end = p;
1723 return OK;
1724}
1725
1726/*
1727 * Compile "(expression)": recursive!
1728 * Return FAIL/OK.
1729 */
1730 static int
1731compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1732{
1733 int ret;
1734 char_u *p = *arg + 1;
1735
1736 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1737 return FAIL;
1738 if (ppconst->pp_used <= PPSIZE - 10)
1739 {
1740 ret = compile_expr1(arg, cctx, ppconst);
1741 }
1742 else
1743 {
1744 // Not enough space in ppconst, flush constants.
1745 if (generate_ppconst(cctx, ppconst) == FAIL)
1746 return FAIL;
1747 ret = compile_expr0(arg, cctx);
1748 }
1749 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1750 return FAIL;
1751 if (**arg == ')')
1752 ++*arg;
1753 else if (ret == OK)
1754 {
1755 emsg(_(e_missing_closing_paren));
1756 ret = FAIL;
1757 }
1758 return ret;
1759}
1760
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001761static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001762
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001763/*
1764 * Compile whatever comes after "name" or "name()".
1765 * Advances "*arg" only when something was recognized.
1766 */
1767 static int
1768compile_subscript(
1769 char_u **arg,
1770 cctx_T *cctx,
1771 char_u *start_leader,
1772 char_u **end_leader,
1773 ppconst_T *ppconst)
1774{
1775 char_u *name_start = *end_leader;
1776 int keeping_dict = FALSE;
1777
1778 for (;;)
1779 {
1780 char_u *p = skipwhite(*arg);
1781
1782 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1783 {
1784 char_u *next = peek_next_line_from_context(cctx);
1785
1786 // If a following line starts with "->{" or "->X" advance to that
1787 // line, so that a line break before "->" is allowed.
1788 // Also if a following line starts with ".x".
1789 if (next != NULL &&
1790 ((next[0] == '-' && next[1] == '>'
1791 && (next[2] == '{'
1792 || ASCII_ISALPHA(*skipwhite(next + 2))))
1793 || (next[0] == '.' && eval_isdictc(next[1]))))
1794 {
1795 next = next_line_from_context(cctx, TRUE);
1796 if (next == NULL)
1797 return FAIL;
1798 *arg = next;
1799 p = skipwhite(*arg);
1800 }
1801 }
1802
1803 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1804 // is not a function call.
1805 if (**arg == '(')
1806 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001807 type_T *type;
1808 int argcount = 0;
1809
1810 if (generate_ppconst(cctx, ppconst) == FAIL)
1811 return FAIL;
1812 ppconst->pp_is_const = FALSE;
1813
1814 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001815 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001816
1817 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001818 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 return FAIL;
1820 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1821 return FAIL;
1822 if (keeping_dict)
1823 {
1824 keeping_dict = FALSE;
1825 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1826 return FAIL;
1827 }
1828 }
1829 else if (*p == '-' && p[1] == '>')
1830 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001831 char_u *pstart = p;
1832 int alt;
1833 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834
Bram Moolenaarc7349932022-01-16 20:59:39 +00001835 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001836 if (generate_ppconst(cctx, ppconst) == FAIL)
1837 return FAIL;
1838 ppconst->pp_is_const = FALSE;
1839
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001840 // Apply the '!', '-' and '+' first:
1841 // -1.0->func() works like (-1.0)->func()
1842 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1843 return FAIL;
1844
1845 p += 2;
1846 *arg = skipwhite(p);
1847 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001848
1849 // Three alternatives handled here:
1850 // 1. "base->name(" only a name, use compile_call()
1851 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1852 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001854 alt = 2;
1855 else
1856 {
1857 // alternative 1 or 3
1858 p = *arg;
1859 if (!eval_isnamec1(*p))
1860 {
1861 semsg(_(e_trailing_characters_str), pstart);
1862 return FAIL;
1863 }
1864 if (ASCII_ISALPHA(*p) && p[1] == ':')
1865 p += 2;
1866 for ( ; eval_isnamec(*p); ++p)
1867 ;
1868 if (*p == '(')
1869 {
1870 // alternative 1
1871 alt = 1;
1872 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1873 return FAIL;
1874 }
1875 else
1876 {
1877 // Must be alternative 3, find the "(". Only works within
1878 // one line.
1879 alt = 3;
1880 paren = vim_strchr(p, '(');
1881 if (paren == NULL)
1882 {
1883 semsg(_(e_missing_parenthesis_str), *arg);
1884 return FAIL;
1885 }
1886 }
1887 }
1888
1889 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890 {
1891 int argcount = 1;
1892 garray_T *stack = &cctx->ctx_type_stack;
1893 int type_idx_start = stack->ga_len;
1894 type_T *type;
1895 int expr_isn_start = cctx->ctx_instr.ga_len;
1896 int expr_isn_end;
1897 int arg_isn_count;
1898
Bram Moolenaarc7349932022-01-16 20:59:39 +00001899 if (alt == 2)
1900 {
1901 // Funcref call: list->(Refs[2])(arg)
1902 // or lambda: list->((arg) => expr)(arg)
1903 //
1904 // Fist compile the function expression.
1905 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1906 return FAIL;
1907 }
1908 else
1909 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001910 int fail;
1911 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001912 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001913
Bram Moolenaarc7349932022-01-16 20:59:39 +00001914 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001915
1916 // instead of using LOADG for "import.Func" use PUSHFUNC
1917 ++paren_follows_after_expr;
1918
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001919 // do not look in the next line
1920 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001921
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001922 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001923 || *skipwhite(*arg) != NUL;
1924 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001925 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001926 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001927
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001928 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001929 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001930 if (did_emsg == prev_did_emsg)
1931 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001932 return FAIL;
1933 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001934 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001935
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001936 // Compile the arguments.
1937 if (**arg != '(')
1938 {
1939 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001940 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 else
1942 semsg(_(e_missing_parenthesis_str), *arg);
1943 return FAIL;
1944 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001945
1946 // Remember the next instruction index, where the instructions
1947 // for arguments are being written.
1948 expr_isn_end = cctx->ctx_instr.ga_len;
1949
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001951 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1952 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953 return FAIL;
1954
1955 // Move the instructions for the arguments to before the
1956 // instructions of the expression and move the type of the
1957 // expression after the argument types. This is what ISN_PCALL
1958 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001959 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1960 if (arg_isn_count > 0)
1961 {
1962 int expr_isn_count = expr_isn_end - expr_isn_start;
1963 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001964 type_T *decl_type;
1965 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966
1967 if (isn == NULL)
1968 return FAIL;
1969 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1970 + expr_isn_start,
1971 sizeof(isn_T) * expr_isn_count);
1972 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1973 + expr_isn_start,
1974 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1975 sizeof(isn_T) * arg_isn_count);
1976 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1977 + expr_isn_start + arg_isn_count,
1978 isn, sizeof(isn_T) * expr_isn_count);
1979 vim_free(isn);
1980
Bram Moolenaar078a4612022-01-04 15:17:03 +00001981 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1982 type = typep->type_curr;
1983 decl_type = typep->type_decl;
1984 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1985 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1986 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001988 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1989 typep->type_curr = type;
1990 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001991 }
1992
Bram Moolenaar078a4612022-01-04 15:17:03 +00001993 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001994 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1995 return FAIL;
1996 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001997
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001998 if (keeping_dict)
1999 {
2000 keeping_dict = FALSE;
2001 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2002 return FAIL;
2003 }
2004 }
2005 else if (**arg == '[')
2006 {
2007 int is_slice = FALSE;
2008
2009 // list index: list[123]
2010 // dict member: dict[key]
2011 // string index: text[123]
2012 // blob index: blob[123]
2013 if (generate_ppconst(cctx, ppconst) == FAIL)
2014 return FAIL;
2015 ppconst->pp_is_const = FALSE;
2016
2017 ++p;
2018 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2019 return FAIL;
2020 if (**arg == ':')
2021 {
2022 // missing first index is equal to zero
2023 generate_PUSHNR(cctx, 0);
2024 }
2025 else
2026 {
2027 if (compile_expr0(arg, cctx) == FAIL)
2028 return FAIL;
2029 if (**arg == ':')
2030 {
2031 semsg(_(e_white_space_required_before_and_after_str_at_str),
2032 ":", *arg);
2033 return FAIL;
2034 }
2035 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2036 return FAIL;
2037 *arg = skipwhite(*arg);
2038 }
2039 if (**arg == ':')
2040 {
2041 is_slice = TRUE;
2042 ++*arg;
2043 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2044 {
2045 semsg(_(e_white_space_required_before_and_after_str_at_str),
2046 ":", *arg);
2047 return FAIL;
2048 }
2049 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2050 return FAIL;
2051 if (**arg == ']')
2052 // missing second index is equal to end of string
2053 generate_PUSHNR(cctx, -1);
2054 else
2055 {
2056 if (compile_expr0(arg, cctx) == FAIL)
2057 return FAIL;
2058 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2059 return FAIL;
2060 *arg = skipwhite(*arg);
2061 }
2062 }
2063
2064 if (**arg != ']')
2065 {
2066 emsg(_(e_missing_closing_square_brace));
2067 return FAIL;
2068 }
2069 *arg = *arg + 1;
2070
2071 if (keeping_dict)
2072 {
2073 keeping_dict = FALSE;
2074 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2075 return FAIL;
2076 }
2077 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2078 return FAIL;
2079 }
2080 else if (*p == '.' && p[1] != '.')
2081 {
2082 // dictionary member: dict.name
2083 if (generate_ppconst(cctx, ppconst) == FAIL)
2084 return FAIL;
2085 ppconst->pp_is_const = FALSE;
2086
2087 *arg = p + 1;
2088 if (IS_WHITE_OR_NUL(**arg))
2089 {
2090 emsg(_(e_missing_name_after_dot));
2091 return FAIL;
2092 }
2093 p = *arg;
2094 if (eval_isdictc(*p))
2095 while (eval_isnamec(*p))
2096 MB_PTR_ADV(p);
2097 if (p == *arg)
2098 {
2099 semsg(_(e_syntax_error_at_str), *arg);
2100 return FAIL;
2101 }
2102 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2103 return FAIL;
2104 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2105 return FAIL;
2106 keeping_dict = TRUE;
2107 *arg = p;
2108 }
2109 else
2110 break;
2111 }
2112
2113 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2114 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002115 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2116 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002117 return FAIL;
2118
2119 return OK;
2120}
2121
2122/*
2123 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2124 * "arg" is advanced until after the expression, skipping white space.
2125 *
2126 * If the value is a constant "ppconst->pp_used" will be non-zero.
2127 * Before instructions are generated, any values in "ppconst" will generated.
2128 *
2129 * This is the compiling equivalent of eval1(), eval2(), etc.
2130 */
2131
2132/*
2133 * number number constant
2134 * 0zFFFFFFFF Blob constant
2135 * "string" string constant
2136 * 'string' literal string constant
2137 * &option-name option value
2138 * @r register contents
2139 * identifier variable value
2140 * function() function call
2141 * $VAR environment variable
2142 * (expression) nested expression
2143 * [expr, expr] List
2144 * {key: val, [key]: val} Dictionary
2145 *
2146 * Also handle:
2147 * ! in front logical NOT
2148 * - in front unary minus
2149 * + in front unary plus (ignored)
2150 * trailing (arg) funcref/partial call
2151 * trailing [] subscript in String or List
2152 * trailing .name entry in Dictionary
2153 * trailing ->name() method call
2154 */
2155 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002156compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002157 char_u **arg,
2158 cctx_T *cctx,
2159 ppconst_T *ppconst)
2160{
2161 char_u *start_leader, *end_leader;
2162 int ret = OK;
2163 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2164 int used_before = ppconst->pp_used;
2165
2166 ppconst->pp_is_const = FALSE;
2167
2168 /*
2169 * Skip '!', '-' and '+' characters. They are handled later.
2170 */
2171 start_leader = *arg;
2172 if (eval_leader(arg, TRUE) == FAIL)
2173 return FAIL;
2174 end_leader = *arg;
2175
2176 rettv->v_type = VAR_UNKNOWN;
2177 switch (**arg)
2178 {
2179 /*
2180 * Number constant.
2181 */
2182 case '0': // also for blob starting with 0z
2183 case '1':
2184 case '2':
2185 case '3':
2186 case '4':
2187 case '5':
2188 case '6':
2189 case '7':
2190 case '8':
2191 case '9':
2192 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2193 return FAIL;
2194 // Apply "-" and "+" just before the number now, right to
2195 // left. Matters especially when "->" follows. Stops at
2196 // '!'.
2197 if (apply_leader(rettv, TRUE,
2198 start_leader, &end_leader) == FAIL)
2199 {
2200 clear_tv(rettv);
2201 return FAIL;
2202 }
2203 break;
2204
2205 /*
2206 * String constant: "string".
2207 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002208 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002209 return FAIL;
2210 break;
2211
2212 /*
2213 * Literal string constant: 'str''ing'.
2214 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002215 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002216 return FAIL;
2217 break;
2218
2219 /*
2220 * Constant Vim variable.
2221 */
2222 case 'v': get_vim_constant(arg, rettv);
2223 ret = NOTDONE;
2224 break;
2225
2226 /*
2227 * "true" constant
2228 */
2229 case 't': if (STRNCMP(*arg, "true", 4) == 0
2230 && !eval_isnamec((*arg)[4]))
2231 {
2232 *arg += 4;
2233 rettv->v_type = VAR_BOOL;
2234 rettv->vval.v_number = VVAL_TRUE;
2235 }
2236 else
2237 ret = NOTDONE;
2238 break;
2239
2240 /*
2241 * "false" constant
2242 */
2243 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2244 && !eval_isnamec((*arg)[5]))
2245 {
2246 *arg += 5;
2247 rettv->v_type = VAR_BOOL;
2248 rettv->vval.v_number = VVAL_FALSE;
2249 }
2250 else
2251 ret = NOTDONE;
2252 break;
2253
2254 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002255 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002256 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002257 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002259 char_u *p = *arg + 4;
2260 int len;
2261
2262 for (len = 0; eval_isnamec(p[len]); ++len)
2263 ;
2264 ret = handle_predefined(*arg, len + 4, rettv);
2265 if (ret == FAIL)
2266 ret = NOTDONE;
2267 else
2268 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002269 }
2270 else
2271 ret = NOTDONE;
2272 break;
2273
2274 /*
2275 * List: [expr, expr]
2276 */
2277 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2278 return FAIL;
2279 ret = compile_list(arg, cctx, ppconst);
2280 break;
2281
2282 /*
2283 * Dictionary: {'key': val, 'key': val}
2284 */
2285 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2286 return FAIL;
2287 ret = compile_dict(arg, cctx, ppconst);
2288 break;
2289
2290 /*
2291 * Option value: &name
2292 */
2293 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2294 return FAIL;
2295 ret = compile_get_option(arg, cctx);
2296 break;
2297
2298 /*
2299 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002300 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002301 */
2302 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2303 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002304 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2305 ret = compile_interp_string(arg, cctx);
2306 else
2307 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 break;
2309
2310 /*
2311 * Register contents: @r.
2312 */
2313 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2314 return FAIL;
2315 ret = compile_get_register(arg, cctx);
2316 break;
2317 /*
2318 * nested expression: (expression).
2319 * lambda: (arg, arg) => expr
2320 * funcref: (arg, arg) => { statement }
2321 */
2322 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2323 ret = compile_lambda(arg, cctx);
2324 if (ret == NOTDONE)
2325 ret = compile_parenthesis(arg, cctx, ppconst);
2326 break;
2327
2328 default: ret = NOTDONE;
2329 break;
2330 }
2331 if (ret == FAIL)
2332 return FAIL;
2333
2334 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2335 {
2336 if (cctx->ctx_skip == SKIP_YES)
2337 clear_tv(rettv);
2338 else
2339 // A constant expression can possibly be handled compile time,
2340 // return the value instead of generating code.
2341 ++ppconst->pp_used;
2342 }
2343 else if (ret == NOTDONE)
2344 {
2345 char_u *p;
2346 int r;
2347
2348 if (!eval_isnamec1(**arg))
2349 {
2350 if (!vim9_bad_comment(*arg))
2351 {
2352 if (ends_excmd(*skipwhite(*arg)))
2353 semsg(_(e_empty_expression_str), *arg);
2354 else
2355 semsg(_(e_name_expected_str), *arg);
2356 }
2357 return FAIL;
2358 }
2359
2360 // "name" or "name()"
2361 p = to_name_end(*arg, TRUE);
2362 if (p - *arg == (size_t)1 && **arg == '_')
2363 {
2364 emsg(_(e_cannot_use_underscore_here));
2365 return FAIL;
2366 }
2367
2368 if (*p == '(')
2369 {
2370 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2371 }
2372 else
2373 {
2374 if (cctx->ctx_skip != SKIP_YES
2375 && generate_ppconst(cctx, ppconst) == FAIL)
2376 return FAIL;
2377 r = compile_load(arg, p, cctx, TRUE, TRUE);
2378 }
2379 if (r == FAIL)
2380 return FAIL;
2381 }
2382
2383 // Handle following "[]", ".member", etc.
2384 // Then deal with prefixed '-', '+' and '!', if not done already.
2385 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2386 ppconst) == FAIL)
2387 return FAIL;
2388 if (ppconst->pp_used > 0)
2389 {
2390 // apply the '!', '-' and '+' before the constant
2391 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2392 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2393 return FAIL;
2394 return OK;
2395 }
2396 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2397 return FAIL;
2398 return OK;
2399}
2400
2401/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002402 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002403 */
2404 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002405compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002406{
2407 type_T *want_type = NULL;
2408
2409 // Recognize <type>
2410 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2411 {
2412 ++*arg;
2413 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2414 if (want_type == NULL)
2415 return FAIL;
2416
2417 if (**arg != '>')
2418 {
2419 if (*skipwhite(*arg) == '>')
2420 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2421 else
2422 emsg(_(e_missing_gt));
2423 return FAIL;
2424 }
2425 ++*arg;
2426 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2427 return FAIL;
2428 }
2429
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002430 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 return FAIL;
2432
2433 if (want_type != NULL)
2434 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002435 type_T *actual;
2436 where_T where = WHERE_INIT;
2437
2438 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002439 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002440 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002442 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2443 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 return FAIL;
2445 }
2446 }
2447
2448 return OK;
2449}
2450
2451/*
2452 * * number multiplication
2453 * / number division
2454 * % number modulo
2455 */
2456 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002457compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002458{
2459 char_u *op;
2460 char_u *next;
2461 int ppconst_used = ppconst->pp_used;
2462
2463 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002464 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002465 return FAIL;
2466
2467 /*
2468 * Repeat computing, until no "*", "/" or "%" is following.
2469 */
2470 for (;;)
2471 {
2472 op = may_peek_next_line(cctx, *arg, &next);
2473 if (*op != '*' && *op != '/' && *op != '%')
2474 break;
2475 if (next != NULL)
2476 {
2477 *arg = next_line_from_context(cctx, TRUE);
2478 op = skipwhite(*arg);
2479 }
2480
2481 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2482 {
2483 error_white_both(op, 1);
2484 return FAIL;
2485 }
2486 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2487 return FAIL;
2488
2489 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002490 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 return FAIL;
2492
2493 if (ppconst->pp_used == ppconst_used + 2
2494 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2495 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2496 {
2497 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2498 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2499 varnumber_T res = 0;
2500 int failed = FALSE;
2501
2502 // both are numbers: compute the result
2503 switch (*op)
2504 {
2505 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2506 break;
2507 case '/': res = num_divide(tv1->vval.v_number,
2508 tv2->vval.v_number, &failed);
2509 break;
2510 case '%': res = num_modulus(tv1->vval.v_number,
2511 tv2->vval.v_number, &failed);
2512 break;
2513 }
2514 if (failed)
2515 return FAIL;
2516 tv1->vval.v_number = res;
2517 --ppconst->pp_used;
2518 }
2519 else
2520 {
2521 generate_ppconst(cctx, ppconst);
2522 generate_two_op(cctx, op);
2523 }
2524 }
2525
2526 return OK;
2527}
2528
2529/*
2530 * + number addition or list/blobl concatenation
2531 * - number subtraction
2532 * .. string concatenation
2533 */
2534 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002535compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536{
2537 char_u *op;
2538 char_u *next;
2539 int oplen;
2540 int ppconst_used = ppconst->pp_used;
2541
2542 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002543 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002544 return FAIL;
2545
2546 /*
2547 * Repeat computing, until no "+", "-" or ".." is following.
2548 */
2549 for (;;)
2550 {
2551 op = may_peek_next_line(cctx, *arg, &next);
2552 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2553 break;
2554 if (op[0] == op[1] && *op != '.' && next)
2555 // Finding "++" or "--" on the next line is a separate command.
2556 // But ".." is concatenation.
2557 break;
2558 oplen = (*op == '.' ? 2 : 1);
2559 if (next != NULL)
2560 {
2561 *arg = next_line_from_context(cctx, TRUE);
2562 op = skipwhite(*arg);
2563 }
2564
2565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2566 {
2567 error_white_both(op, oplen);
2568 return FAIL;
2569 }
2570
2571 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2572 return FAIL;
2573
2574 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002575 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002576 return FAIL;
2577
2578 if (ppconst->pp_used == ppconst_used + 2
2579 && (*op == '.'
2580 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2581 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2582 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2583 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2584 {
2585 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2586 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2587
2588 // concat/subtract/add constant numbers
2589 if (*op == '+')
2590 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2591 else if (*op == '-')
2592 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2593 else
2594 {
2595 // concatenate constant strings
2596 char_u *s1 = tv1->vval.v_string;
2597 char_u *s2 = tv2->vval.v_string;
2598 size_t len1 = STRLEN(s1);
2599
2600 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2601 if (tv1->vval.v_string == NULL)
2602 {
2603 clear_ppconst(ppconst);
2604 return FAIL;
2605 }
2606 mch_memmove(tv1->vval.v_string, s1, len1);
2607 STRCPY(tv1->vval.v_string + len1, s2);
2608 vim_free(s1);
2609 vim_free(s2);
2610 }
2611 --ppconst->pp_used;
2612 }
2613 else
2614 {
2615 generate_ppconst(cctx, ppconst);
2616 ppconst->pp_is_const = FALSE;
2617 if (*op == '.')
2618 {
2619 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2620 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2621 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002622 if (generate_CONCAT(cctx, 2) == FAIL)
2623 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624 }
2625 else
2626 generate_two_op(cctx, op);
2627 }
2628 }
2629
2630 return OK;
2631}
2632
2633/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002634 * expr6a >> expr6b
2635 * expr6a << expr6b
2636 *
2637 * Produces instructions:
2638 * OPNR bitwise left or right shift
2639 */
2640 static int
2641compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2642{
2643 exprtype_T type = EXPR_UNKNOWN;
2644 char_u *p;
2645 char_u *next;
2646 int len = 2;
2647 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002648 isn_T *isn;
2649
2650 // get the first variable
2651 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2652 return FAIL;
2653
2654 /*
2655 * Repeat computing, until no "+", "-" or ".." is following.
2656 */
2657 for (;;)
2658 {
2659 type = EXPR_UNKNOWN;
2660
2661 p = may_peek_next_line(cctx, *arg, &next);
2662 if (p[0] == '<' && p[1] == '<')
2663 type = EXPR_LSHIFT;
2664 else if (p[0] == '>' && p[1] == '>')
2665 type = EXPR_RSHIFT;
2666
2667 if (type == EXPR_UNKNOWN)
2668 return OK;
2669
2670 // Handle a bitwise left or right shift operator
2671 if (ppconst->pp_used == ppconst_used + 1)
2672 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002673 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002674 {
2675 // left operand should be a number
2676 emsg(_(e_bitshift_ops_must_be_number));
2677 return FAIL;
2678 }
2679 }
2680 else
2681 {
2682 type_T *t = get_type_on_stack(cctx, 0);
2683
2684 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2685 {
2686 emsg(_(e_bitshift_ops_must_be_number));
2687 return FAIL;
2688 }
2689 }
2690
2691 if (next != NULL)
2692 {
2693 *arg = next_line_from_context(cctx, TRUE);
2694 p = skipwhite(*arg);
2695 }
2696
2697 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2698 {
2699 error_white_both(p, len);
2700 return FAIL;
2701 }
2702
2703 // get the second variable
2704 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2705 return FAIL;
2706
2707 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2708 return FAIL;
2709
2710 if (ppconst->pp_used == ppconst_used + 2)
2711 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002712 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2713 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2714
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002715 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002716 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2717 {
2718 // right operand should be a positive number
2719 if (tv2->v_type != VAR_NUMBER)
2720 emsg(_(e_bitshift_ops_must_be_number));
2721 else
2722 emsg(_(e_bitshift_ops_must_be_postive));
2723 return FAIL;
2724 }
2725
2726 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2727 tv1->vval.v_number = 0;
2728 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002729 tv1->vval.v_number =
2730 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002731 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002732 tv1->vval.v_number =
2733 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002734 clear_tv(tv2);
2735 --ppconst->pp_used;
2736 }
2737 else
2738 {
2739 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2740 FALSE, FALSE) == FAIL)
2741 {
2742 emsg(_(e_bitshift_ops_must_be_number));
2743 return FAIL;
2744 }
2745
2746 generate_ppconst(cctx, ppconst);
2747
2748 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2749 if (isn == NULL)
2750 return FAIL;
2751
2752 if (isn != NULL)
2753 isn->isn_arg.op.op_type = type;
2754 }
2755 }
2756
2757 return OK;
2758}
2759
2760/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002761 * expr5a == expr5b
2762 * expr5a =~ expr5b
2763 * expr5a != expr5b
2764 * expr5a !~ expr5b
2765 * expr5a > expr5b
2766 * expr5a >= expr5b
2767 * expr5a < expr5b
2768 * expr5a <= expr5b
2769 * expr5a is expr5b
2770 * expr5a isnot expr5b
2771 *
2772 * Produces instructions:
2773 * EVAL expr5a Push result of "expr5a"
2774 * EVAL expr5b Push result of "expr5b"
2775 * COMPARE one of the compare instructions
2776 */
2777 static int
2778compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2779{
2780 exprtype_T type = EXPR_UNKNOWN;
2781 char_u *p;
2782 char_u *next;
2783 int len = 2;
2784 int type_is = FALSE;
2785 int ppconst_used = ppconst->pp_used;
2786
2787 // get the first variable
2788 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2789 return FAIL;
2790
2791 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002792
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002793 type = get_compare_type(p, &len, &type_is);
2794
2795 /*
2796 * If there is a comparative operator, use it.
2797 */
2798 if (type != EXPR_UNKNOWN)
2799 {
2800 int ic = FALSE; // Default: do not ignore case
2801
2802 if (next != NULL)
2803 {
2804 *arg = next_line_from_context(cctx, TRUE);
2805 p = skipwhite(*arg);
2806 }
2807 if (type_is && (p[len] == '?' || p[len] == '#'))
2808 {
2809 semsg(_(e_invalid_expression_str), *arg);
2810 return FAIL;
2811 }
2812 // extra question mark appended: ignore case
2813 if (p[len] == '?')
2814 {
2815 ic = TRUE;
2816 ++len;
2817 }
2818 // extra '#' appended: match case (ignored)
2819 else if (p[len] == '#')
2820 ++len;
2821 // nothing appended: match case
2822
2823 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2824 {
2825 error_white_both(p, len);
2826 return FAIL;
2827 }
2828
2829 // get the second variable
2830 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2831 return FAIL;
2832
2833 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2834 return FAIL;
2835
2836 if (ppconst->pp_used == ppconst_used + 2)
2837 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002838 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002839 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2840 int ret;
2841
2842 // Both sides are a constant, compute the result now.
2843 // First check for a valid combination of types, this is more
2844 // strict than typval_compare().
2845 if (check_compare_types(type, tv1, tv2) == FAIL)
2846 ret = FAIL;
2847 else
2848 {
2849 ret = typval_compare(tv1, tv2, type, ic);
2850 tv1->v_type = VAR_BOOL;
2851 tv1->vval.v_number = tv1->vval.v_number
2852 ? VVAL_TRUE : VVAL_FALSE;
2853 clear_tv(tv2);
2854 --ppconst->pp_used;
2855 }
2856 return ret;
2857 }
2858
2859 generate_ppconst(cctx, ppconst);
2860 return generate_COMPARE(cctx, type, ic);
2861 }
2862
2863 return OK;
2864}
2865
2866static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2867
2868/*
2869 * Compile || or &&.
2870 */
2871 static int
2872compile_and_or(
2873 char_u **arg,
2874 cctx_T *cctx,
2875 char *op,
2876 ppconst_T *ppconst,
2877 int ppconst_used UNUSED)
2878{
2879 char_u *next;
2880 char_u *p = may_peek_next_line(cctx, *arg, &next);
2881 int opchar = *op;
2882
2883 if (p[0] == opchar && p[1] == opchar)
2884 {
2885 garray_T *instr = &cctx->ctx_instr;
2886 garray_T end_ga;
2887 int save_skip = cctx->ctx_skip;
2888
2889 /*
2890 * Repeat until there is no following "||" or "&&"
2891 */
2892 ga_init2(&end_ga, sizeof(int), 10);
2893 while (p[0] == opchar && p[1] == opchar)
2894 {
2895 long start_lnum = SOURCING_LNUM;
2896 long save_sourcing_lnum;
2897 int start_ctx_lnum = cctx->ctx_lnum;
2898 int save_lnum;
2899 int const_used;
2900 int status;
2901 jumpwhen_T jump_when = opchar == '|'
2902 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2903
2904 if (next != NULL)
2905 {
2906 *arg = next_line_from_context(cctx, TRUE);
2907 p = skipwhite(*arg);
2908 }
2909
2910 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2911 {
2912 semsg(_(e_white_space_required_before_and_after_str_at_str),
2913 op, p);
2914 ga_clear(&end_ga);
2915 return FAIL;
2916 }
2917
2918 save_sourcing_lnum = SOURCING_LNUM;
2919 SOURCING_LNUM = start_lnum;
2920 save_lnum = cctx->ctx_lnum;
2921 cctx->ctx_lnum = start_ctx_lnum;
2922
2923 status = check_ppconst_bool(ppconst);
2924 if (status != FAIL)
2925 {
2926 // Use the last ppconst if possible.
2927 if (ppconst->pp_used > 0)
2928 {
2929 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2930 int is_true = tv2bool(tv);
2931
2932 if ((is_true && opchar == '|')
2933 || (!is_true && opchar == '&'))
2934 {
2935 // For "false && expr" and "true || expr" the "expr"
2936 // does not need to be evaluated.
2937 cctx->ctx_skip = SKIP_YES;
2938 clear_tv(tv);
2939 tv->v_type = VAR_BOOL;
2940 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2941 }
2942 else
2943 {
2944 // For "true && expr" and "false || expr" only "expr"
2945 // needs to be evaluated.
2946 --ppconst->pp_used;
2947 jump_when = JUMP_NEVER;
2948 }
2949 }
2950 else
2951 {
2952 // Every part must evaluate to a bool.
2953 status = bool_on_stack(cctx);
2954 }
2955 }
2956 if (status != FAIL)
2957 status = ga_grow(&end_ga, 1);
2958 cctx->ctx_lnum = save_lnum;
2959 if (status == FAIL)
2960 {
2961 ga_clear(&end_ga);
2962 return FAIL;
2963 }
2964
2965 if (jump_when != JUMP_NEVER)
2966 {
2967 if (cctx->ctx_skip != SKIP_YES)
2968 {
2969 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2970 ++end_ga.ga_len;
2971 }
2972 generate_JUMP(cctx, jump_when, 0);
2973 }
2974
2975 // eval the next expression
2976 SOURCING_LNUM = save_sourcing_lnum;
2977 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2978 {
2979 ga_clear(&end_ga);
2980 return FAIL;
2981 }
2982
2983 const_used = ppconst->pp_used;
2984 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2985 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2986 {
2987 ga_clear(&end_ga);
2988 return FAIL;
2989 }
2990
2991 // "0 || 1" results in true, "1 && 0" results in false.
2992 if (ppconst->pp_used == const_used + 1)
2993 {
2994 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2995
2996 if (tv->v_type == VAR_NUMBER
2997 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2998 {
2999 tv->vval.v_number = tv->vval.v_number == 1
3000 ? VVAL_TRUE : VVAL_FALSE;
3001 tv->v_type = VAR_BOOL;
3002 }
3003 }
3004
3005 p = may_peek_next_line(cctx, *arg, &next);
3006 }
3007
3008 if (check_ppconst_bool(ppconst) == FAIL)
3009 {
3010 ga_clear(&end_ga);
3011 return FAIL;
3012 }
3013
3014 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3015 // Every part must evaluate to a bool.
3016 if (bool_on_stack(cctx) == FAIL)
3017 {
3018 ga_clear(&end_ga);
3019 return FAIL;
3020 }
3021
3022 if (end_ga.ga_len > 0)
3023 {
3024 // Fill in the end label in all jumps.
3025 generate_ppconst(cctx, ppconst);
3026 while (end_ga.ga_len > 0)
3027 {
3028 isn_T *isn;
3029
3030 --end_ga.ga_len;
3031 isn = ((isn_T *)instr->ga_data)
3032 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3033 isn->isn_arg.jump.jump_where = instr->ga_len;
3034 }
3035 }
3036 ga_clear(&end_ga);
3037
3038 cctx->ctx_skip = save_skip;
3039 }
3040
3041 return OK;
3042}
3043
3044/*
3045 * expr4a && expr4a && expr4a logical AND
3046 *
3047 * Produces instructions:
3048 * EVAL expr4a Push result of "expr4a"
3049 * COND2BOOL convert to bool if needed
3050 * JUMP_IF_COND_FALSE end
3051 * EVAL expr4b Push result of "expr4b"
3052 * JUMP_IF_COND_FALSE end
3053 * EVAL expr4c Push result of "expr4c"
3054 * end:
3055 */
3056 static int
3057compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3058{
3059 int ppconst_used = ppconst->pp_used;
3060
3061 // get the first variable
3062 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3063 return FAIL;
3064
3065 // || and && work almost the same
3066 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3067}
3068
3069/*
3070 * expr3a || expr3b || expr3c logical OR
3071 *
3072 * Produces instructions:
3073 * EVAL expr3a Push result of "expr3a"
3074 * COND2BOOL convert to bool if needed
3075 * JUMP_IF_COND_TRUE end
3076 * EVAL expr3b Push result of "expr3b"
3077 * JUMP_IF_COND_TRUE end
3078 * EVAL expr3c Push result of "expr3c"
3079 * end:
3080 */
3081 static int
3082compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3083{
3084 int ppconst_used = ppconst->pp_used;
3085
3086 // eval the first expression
3087 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3088 return FAIL;
3089
3090 // || and && work almost the same
3091 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3092}
3093
3094/*
3095 * Toplevel expression: expr2 ? expr1a : expr1b
3096 * Produces instructions:
3097 * EVAL expr2 Push result of "expr2"
3098 * JUMP_IF_FALSE alt jump if false
3099 * EVAL expr1a
3100 * JUMP_ALWAYS end
3101 * alt: EVAL expr1b
3102 * end:
3103 *
3104 * Toplevel expression: expr2 ?? expr1
3105 * Produces instructions:
3106 * EVAL expr2 Push result of "expr2"
3107 * JUMP_AND_KEEP_IF_TRUE end jump if true
3108 * EVAL expr1
3109 * end:
3110 */
3111 int
3112compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3113{
3114 char_u *p;
3115 int ppconst_used = ppconst->pp_used;
3116 char_u *next;
3117
3118 // Ignore all kinds of errors when not producing code.
3119 if (cctx->ctx_skip == SKIP_YES)
3120 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003121 int prev_did_emsg = did_emsg;
3122
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003123 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003124 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003125 }
3126
3127 // Evaluate the first expression.
3128 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3129 return FAIL;
3130
3131 p = may_peek_next_line(cctx, *arg, &next);
3132 if (*p == '?')
3133 {
3134 int op_falsy = p[1] == '?';
3135 garray_T *instr = &cctx->ctx_instr;
3136 garray_T *stack = &cctx->ctx_type_stack;
3137 int alt_idx = instr->ga_len;
3138 int end_idx = 0;
3139 isn_T *isn;
3140 type_T *type1 = NULL;
3141 int has_const_expr = FALSE;
3142 int const_value = FALSE;
3143 int save_skip = cctx->ctx_skip;
3144
3145 if (next != NULL)
3146 {
3147 *arg = next_line_from_context(cctx, TRUE);
3148 p = skipwhite(*arg);
3149 }
3150
3151 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3152 {
3153 semsg(_(e_white_space_required_before_and_after_str_at_str),
3154 op_falsy ? "??" : "?", p);
3155 return FAIL;
3156 }
3157
3158 if (ppconst->pp_used == ppconst_used + 1)
3159 {
3160 // the condition is a constant, we know whether the ? or the :
3161 // expression is to be evaluated.
3162 has_const_expr = TRUE;
3163 if (op_falsy)
3164 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3165 else
3166 {
3167 int error = FALSE;
3168
3169 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3170 &error);
3171 if (error)
3172 return FAIL;
3173 }
3174 cctx->ctx_skip = save_skip == SKIP_YES ||
3175 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3176
3177 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3178 // "left ?? right" and "left" is truthy: produce "left"
3179 generate_ppconst(cctx, ppconst);
3180 else
3181 {
3182 clear_tv(&ppconst->pp_tv[ppconst_used]);
3183 --ppconst->pp_used;
3184 }
3185 }
3186 else
3187 {
3188 generate_ppconst(cctx, ppconst);
3189 if (op_falsy)
3190 end_idx = instr->ga_len;
3191 generate_JUMP(cctx, op_falsy
3192 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3193 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003194 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003195 }
3196
3197 // evaluate the second expression; any type is accepted
3198 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3199 return FAIL;
3200 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3201 return FAIL;
3202
3203 if (!has_const_expr)
3204 {
3205 generate_ppconst(cctx, ppconst);
3206
3207 if (!op_falsy)
3208 {
3209 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003210 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003211 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003212
3213 end_idx = instr->ga_len;
3214 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3215
3216 // jump here from JUMP_IF_FALSE
3217 isn = ((isn_T *)instr->ga_data) + alt_idx;
3218 isn->isn_arg.jump.jump_where = instr->ga_len;
3219 }
3220 }
3221
3222 if (!op_falsy)
3223 {
3224 // Check for the ":".
3225 p = may_peek_next_line(cctx, *arg, &next);
3226 if (*p != ':')
3227 {
3228 emsg(_(e_missing_colon_after_questionmark));
3229 return FAIL;
3230 }
3231 if (next != NULL)
3232 {
3233 *arg = next_line_from_context(cctx, TRUE);
3234 p = skipwhite(*arg);
3235 }
3236
3237 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3238 {
3239 semsg(_(e_white_space_required_before_and_after_str_at_str),
3240 ":", p);
3241 return FAIL;
3242 }
3243
3244 // evaluate the third expression
3245 if (has_const_expr)
3246 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3247 ? SKIP_YES : SKIP_NOT;
3248 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3249 return FAIL;
3250 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3251 return FAIL;
3252 }
3253
3254 if (!has_const_expr)
3255 {
3256 type_T **typep;
3257
3258 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003259 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003260
3261 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003262 typep = &((((type2_T *)stack->ga_data)
3263 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003264 common_type(type1, *typep, typep, cctx->ctx_type_list);
3265
3266 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3267 isn = ((isn_T *)instr->ga_data) + end_idx;
3268 isn->isn_arg.jump.jump_where = instr->ga_len;
3269 }
3270
3271 cctx->ctx_skip = save_skip;
3272 }
3273 return OK;
3274}
3275
3276/*
3277 * Toplevel expression.
3278 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3279 * Returns OK or FAIL.
3280 */
3281 int
3282compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3283{
3284 ppconst_T ppconst;
3285
3286 CLEAR_FIELD(ppconst);
3287 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3288 {
3289 clear_ppconst(&ppconst);
3290 return FAIL;
3291 }
3292 if (is_const != NULL)
3293 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3294 if (generate_ppconst(cctx, &ppconst) == FAIL)
3295 return FAIL;
3296 return OK;
3297}
3298
3299/*
3300 * Toplevel expression.
3301 */
3302 int
3303compile_expr0(char_u **arg, cctx_T *cctx)
3304{
3305 return compile_expr0_ext(arg, cctx, NULL);
3306}
3307
3308
3309#endif // defined(FEAT_EVAL)