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